[racket-dev] submodule in macro

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Jan 29 08:42:40 EST 2013

At Sat, 26 Jan 2013 10:36:52 -0500, Matthias Felleisen wrote:
> 
> [[ I don't really understand the answer. I mean 
>    I understand the technicality but not the spirit. ]]
> 
> The 'f' comes from the macro input in both cases. 
> Hence the rename-out could be seen as the actual
> name required. 

Exports from a module are keyed on symbols, not identifiers.

In contrast, exports from a `racket/package' package are keyed on
identifiers:

 #lang racket
 (require racket/package)

 (define-syntax (def-wrapped stx)
   (syntax-case stx ()
     [(_ (f arg ...) body ...)
      #'(begin
          (define-package tmp-package (f)
            (define (f-tmp arg ...) (displayln "wrapper") body ...)
            (define*-syntaxes (f) (make-rename-transformer #'f-tmp)))
          (open-package tmp-package))]))

 (def-wrapped (f x) (+ x 1))
 (f 100)


> On Jan 26, 2013, at 6:55 AM, Matthew Flatt wrote:
> 
> > At Sat, 26 Jan 2013 01:12:04 -0500, Stephen Chang wrote:
> >> Is this a supported use of submodules?
> >> 
> >> 
> >> #lang racket
> >> 
> >> (define-syntax (def-wrapped stx)
> >>  (syntax-case stx ()
> >>    [(_ (f arg ...) body ...)
> >>     #'(begin
> >>         (module tmp-module-name racket
> >>           (define (f-tmp arg ...) (displayln "wrapper") body ...)
> >>           (provide (rename-out [f-tmp f])))
> >>         (require (quote tmp-module-name)))]))
> >> 
> >> (def-wrapped (f x) (+ x 1))
> >> (f 100)
> >> 
> >> 
> >> Welcome to DrRacket, version 5.3.1.3 [3m].
> >> Language: racket [custom].
> >> . f: unbound identifier in module in: f
> > 
> > Your `require' is macro-introduced, so it only binds uses that are also
> > macro-introduced. Try renaming on import, since the rename target is an
> > identifier supplied to the macro:
> > 
> > #lang racket
> > 
> > (define-syntax (def-wrapped stx)
> >  (syntax-case stx ()
> >    [(_ (f arg ...) body ...)
> >     #'(begin
> >         (module tmp-module-name racket
> >           (define (f-tmp arg ...) (displayln "wrapper") body ...)
> >           (provide f-tmp))
> >         (require (rename-in (quote tmp-module-name) 
> >                             [f-tmp f])))]))
> > 
> > (def-wrapped (f x) (+ x 1))
> > (f 100)
> > 
> > _________________________
> >  Racket Developers list:
> >  http://lists.racket-lang.org/dev

Posted on the dev mailing list.