[racket] metacircular interpreter: hygiene, lexical variable, and substitution ==> Re: recursive macro expanding order

From: YC (yinso.chen at gmail.com)
Date: Mon Dec 13 02:01:09 EST 2010

On Sat, Dec 11, 2010 at 2:49 PM, YC <yinso.chen at gmail.com> wrote:

>
> On Sat, Dec 11, 2010 at 8:56 AM, Ryan Culpepper <ryanc at ccs.neu.edu> wrote:
>
>>
>> If you are only supporting syntax-rules, then I recommend implementing the
>> algorithm from "Macros that Work" by Clinger and Rees. Hygienic macro
>> expansion does typically involve alpha-conversion---renaming lexical
>> variables to fresh names. The challenge, as you mention above, is in not
>> renaming too eagerly.
>
>
Looking at the paper, it occurred to me that one way to ensure that macro
expands to the correct binding is for the macro to carry the references to
the environment itself.

So for the example in the paper:

(let-syntax ((push (syntax-rules
                       ((push ?v ?x)
                        =>
                        (set! ?x (cons ?v ?x))))))
  (let ((pros (list "cheap" "fast"))
        (cons (list)))
    (push "unreliable" cons)))


The push macro already contains the bindings to set! and cons, so the later
shadowing of cons does not overwrite push's own copy of cons.  In that sense
a macro transformer's job is to merge its own environment along with the
call-site's environment.

The key to correctly implement macros is then to ensure that every macro
holds references to a environment at the point of its definition, so it can
then be used for merging during the expansion.

Is this a fair way of thinking about macro expansions?

Thanks,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20101212/78950fd4/attachment.html>

Posted on the users mailing list.