[plt-scheme] Macros in the same module

From: Paulo J. Matos (pocmatos at gmail.com)
Date: Thu May 7 15:41:02 EDT 2009

On Thu, May 7, 2009 at 1:25 PM, Noel Welsh <noelwelsh at gmail.com> wrote:
> I think you want a define-for-syntax not a define-syntax, or you need
> to put 'expand-rules' into a (syntax ...). One macro can definitely
> expand into another:
>
> #lang scheme/base
>
> (define-syntax foo
>  (syntax-rules ()
>    [(foo name val)
>     (bar name val)]))
>
> (define-syntax bar
>  (syntax-rules ()
>    [(bar name val)
>     (define name val)]))
>
> (foo a 42)
> a
>

This is what currently is happening to my macro:
#lang scheme

(define-for-syntax (bar a)
  (syntax-case a ()
    [(_ name val)
     #'(define name val)]))

(define-syntax (foo a)
  (syntax-case a ()
    [(_ name val)
     #'(bar name val)]))

(foo a 42)

expand: unbound identifier in module in: bar

I never really looked very deeply into syntax-case but from what I can
see now, #' transforms an s-expr into syntax which will represent the
final code.
It doesn't transform bar because bar is another macro, but if I try to
#, bar it doesn't work either saying:
 expand: unbound identifier in module in: a

Now, from the stepper I get the transformed expression:
(module page scheme (define-for-syntax (bar a) (syntax-case a () ((_
name val) (syntax (define name val))))) (define-syntax (foo a)
(syntax-case a () ((_ name val) (quasisyntax ((unsyntax bar) name
val))))) (#<procedure> a 42))

Which is strange because either the procedure is bar (which is strange
because it is not defined), or it is define which is even stranger
because define is not a procedure.

Any hint?

Cheers,

Paulo Matos

> N.
>
> On Thu, May 7, 2009 at 1:00 PM, Paulo J. Matos <pocmatos at gmail.com> wrote:
>> Hi all,
>>
>> I have defined a macro which expands into a define and then uses
>> another macro defined in the same module to expand into the define
>> body.
>> However, the first macro cannot 'see' the second:
>>  expand: unbound identifier in module (in the transformer environment,
>> which does not include the macro definition that is visible to
>> run-time expressions) in: expand-rules
> ...
>



-- 
Paulo Jorge Matos - pocmatos at gmail.com
Webpage: http://www.personal.soton.ac.uk/pocm


Posted on the users mailing list.