[plt-scheme] identifier for a module-body definition already has a different module context

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Dec 28 11:31:14 EST 2004

At Mon, 27 Dec 2004 04:51:03 +0900, Daniel Pinto de Mello e Silva wrote:
> (module b mzscheme
>     (provide def-b)
>     (define-syntax (def-b stx)
>       (syntax-case stx ()
>         [(_ val)
>          (with-syntax ([b (datum->syntax-object stx 'b)])
>            #`(define b val))])))
> 
> (module b5 mzscheme
>     (require b)
>     (provide def-b5)
>     (define-syntax (def-b5 stx)
>       (syntax-case stx ()
>         [(_)
>          #`(def-b 5)])))
> 
> > (require b5)
> > (def-b5)
> define-values: identifier for a top-level definition already has a
> module context in: b
> 
> 
> I thought 'b' had the macro invocation (stx) context... 

In this case, stx originates in the `b5' module, at #`(def-b 5).

If you meant for `b' to be define non-hygienically, this is the usual
problem with macros that expand into uses of non-hygienic macros. In
this example, you can solve the problem with

 (module b5 mzscheme
     (require b)
     (provide def-b5)
     (define-syntax (def-b5 stx)
       (syntax-case stx ()
         [(_)
          (datum->syntax-object stx (syntax-e #`(def-b 5)) stx)])))

If you meant a hygienic introduction and you're just trying to avoid
"the already has a context" error that result from using a plain #'b in
the `b' module, then use `generate-temporaries' to generate the
identifier for `b':

 (module b mzscheme
     (provide def-b)
     (define-syntax (def-b stx)
       (syntax-case stx ()
         [(_ val)
          (with-syntax ([(b) (generate-temporaries '(b))])
            #`(define b val))])))


Matthew



Posted on the users mailing list.