I have a situation where I’d like to do something like…
(define (def a b)
(store a b) ; store the definition of 'a' somewhere
(define-global a b)) ; also define 'a' so that its definition
; is accessible later in the program
Is this possible somehow? As far as I know define-global doesn’t exist, so define statements inside procedures apply only to the local environment.
This is intended for creating a ‘def’ procedure for an embedded DSL in scheme, so in addition to making the definition I need to store the definition in my own symbol table. Eventually I want to ‘intercept’ symbol look-ups to apply my own transformation, returning an expression for the symbol look-up instead of actually performing it.
I’m using Gambit-C Scheme.
Thanks.
No, at least not without any Gambit specific low-level hooks. But this is for a good reason: what you’re suggesting would make it impossible to do efficient compilation, since bindings can change at any time.
If your goal is to implement a DSL, then keeping the values in your own table, and doing the lookup in code that implements variable lookup in your DSL makes a lot of sense. In addition, it will naturally lead you to an implementation where the DSL lives in its own world, separately from the code that implements it. (For example, if you have the above, then what would you expect to happen when code inside the DSL defines a variable called
def?)