[plt-scheme] defines and modules inside macro

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Thu Nov 12 16:53:51 EST 2009

On Nov 12, 2009, at 3:59 PM, Chognkai Zhu wrote:

> Reading PLT doc on valid syntax in detail, and you will find out  
> that "module" can't be put inside "begin".

Yes it can: http://docs.plt-scheme.org/reference/syntax-model.html#(part._fully-expanded)

top-level-form =
     (module id name-id (#%plain-module-begin module-level-form ...))
   | (begin top-level-form ...)
   | <more>

(Unless there's some other place that says different.)

--

Here's the problem: The require form in the second module is  
introduced by the macro (it has a mark). Require is a binding form,  
and by default it only binds identifiers with the same mark as the  
require spec. (Require is not hygienic per se, but it tries to follow  
the spirit of hygiene.) So the module only has a binding of marked-f,  
not the unmarked f from the original macro use.

You can fix this by explicitly binding the unmarked f using only-in:

(define-syntax M
  (syntax-rules ()
    ((_ n form)
      (begin
         (module x scheme form)
         (module y scheme
           (require (only-in 'x [n n]))
           (write n))))))

Ryan


>
> Chongkai
>
> Ivanov Alexey wrote:
>> I am writing a macro that defines some exported values. I want one  
>> more macro to test if it has exports them
>>
>> (define-syntax M
>>  (syntax-rules ()
>>    ((_ n form)
>>      (begin          (module x scheme form)
>>         (module y scheme
>>              (require 'x)
>>              (write n))))))
>>
>> (M f (begin (provide f) (define f 1)))
>>
>> but mzscheme -r throws an error:
>> compile: unbound identifier in module in: f
>>
>> Though the expected expansion  is compiled with no errors
>> (begin
>>  (module x scheme (begin (provide f) (define f 1)))
>>  (module y scheme
>>    (require 'x)
>>    (write f)))
>>
>> Why? _________________________________________________
>>  For list-related administrative tasks:
>>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>>
>
> _________________________________________________
> For list-related administrative tasks:
> http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.