[plt-scheme] macro question

From: Michael Vanier (mvanier at cs.caltech.edu)
Date: Sat Jun 7 15:58:13 EDT 2008

I'm using 3.72[3m]; I didn't know that 4.0 was ready yet.  I may not know what I'm doing, but when I 
hit the "macro stepper" button it expands the entire Scheme file form-by-form.  When it gets to the 
get-stack-values macro calls it just expands them naively with no error messages.  I don't know if 
this is a bug in 3.72 or if I'm not doing it right.

Completely off-topic, I have to say that I am really keen on the idea of typed Scheme (which I 
haven't used yet, though I've read the paper).  I found that about 90% of the bugs in my code were 
stupid type errors.  Contracts would probably have helped a lot too.


Matthias Felleisen wrote:
> Michael, when I run the macro stepper (in 4.0), I get
>> expand: unbound variable in module
>> es
> with high-lighting, exactly where I expected it. Same for RUN.
> Jos explained the why and the how to fix it.
> I'm surprised to hear that the macro stepper wouldn't show you this 
> error. What version are you using? What language?
> Thanks -- Matthias
> On Jun 7, 2008, at 5:17 AM, Michael Vanier wrote:
>> I've run into a problem with a macro I don't know how to solve.  
>> Here's the macro:
>> (define-syntax get-stack-vals
>>   (syntax-rules ()
>>     ((_ n (env first rest) expr ...)
>>      (let*-values (((env stk) (es-values es))
>>                    ((first rest) (take n stk)))
>>        expr ...))))
>> Here's a use:
>> (define (do-def es)
>>   (get-stack-vals 2 (env first rest)
>>      (let ((sym (cadr first))
>>            (val (car first)))
>>        (if (symbol? sym)
>>            (let ((env2 (env-add env sym val)))
>>              (make-es env2 rest))
>>            (error "cannot define non-symbol: " sym)))))
>> which should expand to:
>> (define (do-def es)
>>   (let*-values (((env stk) (es-values es))
>>                 ((first rest) (take 2 stk)))
>>     (let ((sym (cadr first))
>>           (val (car first)))
>>       (if (symbol? sym)
>>           (let ((env2 (env-add env sym val)))
>>             (make-es env2 rest))
>>           (error "cannot define non-symbol: " sym)))))
>> and apparently does, at least according to the macro stepper.  
>> However, when I call do-def I get an error message: "reference to 
>> undefined identifier: es".  This seems odd, because es is in scope at 
>> the macro expansion location.  Is the es in the get-stack-vals macro 
>> bound in the lexical scope of that macro?  All the other external 
>> identifiers in the macro are bound at the top-level, so it works 
>> either way for them.  If this is the case, how do I get es to be bound 
>> the way I want it to be?  I realize that I'm trying to do something a 
>> bit naughty since I'm assuming a binding for es will exist at all 
>> macro expansion locations; alternative suggestions are welcome (other 
>> than "don't give up your day job").
>> I'm a macro newbie, so please be gentle ;-)  PLT Scheme's macro system 
>> is an intricate and beautiful thing that I'm just beginning to wrap my 
>> head around.
>> Mike
>> _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme

Posted on the users mailing list.