[plt-dev] Symptom of REPL's hopelessness?

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Thu Oct 1 16:23:03 EDT 2009

On Oct 1, 2009, at 4:01 PM, Casey Klein wrote:

> Can someone explain why this macro expands without error in a module
> but not in the REPL?
>
> (define-syntax (m stx)
> (syntax-case stx ()
>   [(_ x)
>    (with-syntax ([(y) (generate-temporaries (syntax (x)))])
>      (syntax (define (y) y)))]))
>
>> (m q)
> compile: unbound identifier (and no #%top syntax transformer is  
> bound) in: q1

Here's what I think is going on:

When you evaluate (m q) at the repl, you're *compiling* the whole term  
before you *execute* it. That means that the body of the definition is  
compiled before the definition takes effect. If the definition had  
taken effect, the reference to 'q1' (the generated name) would be  
okay, because there would be an entry for it in the compilation  
environment. But since the definition hasn't been *executed* yet,  
there is no such environment entry. That leads to '#%top' and craziness.

To back up my analysis, here's a variation of the macro. Because of  
begin-splicing etc etc, the definition is *executed* before the 'set!'  
expression is *compiled*. And it works. (Or so it seems.)

(define-syntax (m stx)
   (syntax-case stx ()
     [(_ x)
      (with-syntax ([(y) (generate-temporaries (syntax (x)))])
        (syntax (begin (define (y) #f)
                       (set! y (lambda () y)))))]))

Ryan



>
> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-dev



Posted on the dev mailing list.