I'm writing a compiler from an esolang to WebAssembly, using CHICKEN Scheme's miniKanren. My compiler already works somewhat, but I'd like to extend it with support for named local registers and I'm not sure how to proceed.
CHICKEN has a gensym with the following non-relational behavior:
#;1> (gensym)
g18
#;2> (gensym "asdf")
asdf19
#;3> (gensym 'asdf)
asdf20
It would be nice if I could generate semi-readable registers in miniKanren. WebAssembly text tools usually want named registers to start with "$", as well. Given these constraints, I presumed that I could hack up a register-naming relation using gensym
, symbolo
, and project
, but I'm not sure whether that is a practical solution. I also presume that I could write a custom walker against the underlying µKanren API, but that goes beyond portable miniKanren.
Is there a standard solution for this?
My compiler source is available, but likely the only relevant part is my current hack with project
and little-endian->number
for numbered registers:
; The successor function.
; Nat <-> Nat
(define (succ° n m) (+o n (build-num 1) m))
; A prelude which loads the given number of local params onto the stack.
; Nat <-> WAT Insts
(define (prelude° plen prelude)
(conde
((zeroo plen) (== prelude '()))
((fresh (len lude) (succ° len plen) (prelude° len lude)
(project (len) (appendo lude `((local.get ,(little-endian->number len))) prelude))))))