[racket] Again on bindings visibility in eval

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Wed Jul 13 16:10:35 EDT 2011


[I hadn't forgotten your messages, it's just that deadlines got in the way.]

I still don't understand your desire to access the environmental variables specially: 

1.  if you generate a closure or a struct full of closures in your macro, the expression that you pass in captures the variables in its context: 

> (define-syntax-rule (mystuff y expr) (lambda (x) (displayln `(expr ,y)) (if expr x 0)))
> [  (let ((t 333)) (mystuff t (> t 444)))   22]
((> t 444) 333)
0

See it really does get t's value from the context. 


2a. do you have reason to access variables other than throwing them into the environment for eval? In that case your problem is solved. 

2b. if not, what are the reasons to guess at variable names in the context of the macro USE (not definition)? 

;; ----------------


SEPARATE QUESTION: Now you also write that you process the code before you throw it to eval. Is it possible to write functions on syntax that process the code and perform the optimizations that Racket doesn't perform for you? 

-- Matthias






On Jul 13, 2011, at 3:54 PM, Maurizio Giordano wrote:

> Hi all, hi Matthias,
> 
> I would like to come back to my (still unsolved) problem I 
> proposed some days ago:
> 
> when using define-syntax in a inner scope of bindings (like let),
> in this example:
> 
> (define-syntax mymacro
>   (syntax-rules ()
>     [(_ x)
>       (eval `(lambda (w) (print (quote x)) (if x w #f)))]))
> 
> (let* ((s 3) (f (mymacro (> s 0)))) (f 5))
> reference to undefined identifier: s
> 
> of course if you:
> 
> (define s 3)
> 
> in the top environment, the error will not appear.
> This is even more clear if you expand the macro:
> 
> (syntax->datum (expand '(mymacro (> s 0))))
> ... you see that "s" is a %top binding.
> 
> I know that if I make the macro to return directly the lambda,
> it works. Nevertheless, in my implementation, I still need to use
> the "(eval (quasiquote (lambda ...)))". 
> Why? my macro is like a compiler
> that generates a lambda code very huge: the code  is recursive in some of its part,
> but, for efficiency reasons, i preferred to inline all recursive calls. So I use a
> function "expander" that makes recursive inlining (or injecting) of code).
> It is more or less something like:
> 
> `(lambda (...) ... static-code...  ,(expander ...) ...)
> 
> where "expander call itself with different parameters.
>  
> This is just to know your opinion... if the "(eval (quasiquote (lambda ...)))"
> cannot see local bindings like in let*, than I have to choice:
> 1) renounce to the feature.
> 2) looking for alternative methods to generate my code with recursive inlining
> 
> Than you,
> Cheers,
> Maurizio.




Posted on the users mailing list.