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

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Thu Oct 1 16:35:15 EDT 2009

At Thu, 1 Oct 2009 15:01:56 -0500, 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

Yes, it's a hopeless-top-level sort of problem. At the point where the
expander is trying to figure out where `y' comes from, there is no
suitable `y', yet, and there won't be until the `define' is actually
evaluated.

There's a special hack to solve this problem: when a top-level
`define-syntaxes' gets zero results from the RHS expression, it just
makes the identifiers exist (in a suitable way) without defining
anything. So, this works:

 (define-syntax (m stx)
  (syntax-case stx ()
    [(_ x)
     (with-syntax ([(y) (generate-temporaries (syntax (x)))])
       (syntax
        (begin
         (define-syntaxes (y) (values))
         (define (y) y))))]))

I see that the problem and workaround are not documented (at least not
where I can find it), so I'll work on the docs.



Posted on the dev mailing list.