[plt-scheme] composing syntax-case macros?

From: YC (yinso.chen at gmail.com)
Date: Fri Jan 16 19:36:57 EST 2009

On Fri, Jan 16, 2009 at 3:52 PM, Ryan Culpepper <ryanc at ccs.neu.edu> wrote:

> ;; the non-working cond-it
>> (define-syntax (cond-it stx)
>>  (syntax-case stx (else)
>>    ((cond-it (else exp exp2 ...))
>>     #'(begin exp exp2 ...))
>>    ((cond-it (test? exp exp2 ...))
>>     #'(when-it test? exp exp2 ...))
>>    ((cond-it (test? exp exp2 ...) cond1 cond2 ...)
>>     #'(if-it test? (begin exp exp2 ...)
>>              (cond-it cond1 cond2 ...)))))
> When 'cond-it' expands and produces an 'if-it' expression, the 'if-it' is
> marked by the macro expander as coming from a macro. That means its lexical
> context is different from the 'it' variables in the branches. That means
> that the 'it' variable binding produced by 'if-it' does not capture the 'it'
> references in the branches.

I see - based on this I tried to see if I can mark `if-it` to be the same
lexical context but i get a compiler warning instead...

(define-syntax (cond-it stx)
  (syntax-case stx (else)
    ((~) #'(void))
    ((~ (else exp exp2 ...))
     #'(begin exp exp2 ...))
    ((~ (test? exp exp2 ...) cond1 ...)
     (with-syntax ((if-it (datum->syntax #'~ 'if-it)))
       #'(if-it test? (begin exp exp2 ...)
                (cond-it cond1 ...))))))

> (cond-it (#f 'hello) ("test me" it) (else 'nothing))
compile: identifier used out of context in: if-it

I guess the app position cannot not be "captured" this way?

The macro stepper will show you this using colors. Try an example. The
> original code is in black. The parts introduced by 'cond-it' are in a
> different color (like red or blue).

I'm working in emacs so I don't have macro stepper handy at the moment - is
there a way to call macro stepper (a cmdline or repl version) from mzscheme?

The problem comes from having macros introduce unhygienic bindings of 'it'.
> A better way to do it would be to bind 'it' to a syntax parameter and update
> the meaning of 'it' in the expansion of an 'if-it' expression (using
> 'syntax-parameterize').

This is macro voodoo! ;)  I'll have to learn more about syntax parameters
and transformers.  Is there some sort of guide docs that talks about how and
why (usages) on these capabilites?

Thansks for demonstrating the cool technique and the clear explanation.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20090116/4c9fe155/attachment.html>

Posted on the users mailing list.