[plt-scheme] #%top usage

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Apr 15 03:35:00 EDT 2007

At Sat, 14 Apr 2007 09:40:18 +0000, support at taxupdate.com wrote:
> This fixes the "definition already imported" error, thanks.  Here's the
> code:
> 
> (module newlang mzscheme
>   (provide (all-from-except mzscheme #%top #%module-begin))
>   (provide (rename top #%top)
>            (rename #%plain-module-begin #%module-begin))
>   (define-syntax (top stx)
>     (syntax-case stx ()
>       ((_ . identifier)
>        (let ((s (syntax-object->datum #'identifier)))
>          (if (symbol? s)
>              (syntax/loc stx
>                (if (not (namespace-variable-value 'identifier #f (lambda () 
> #f)))
>                    (printf "not defined: ~a\n" 'identifier)
>                    (#%top . identifier)))
>              (syntax/loc stx (#%top . identifier))))))))
> 
> (module test newlang
>   (require-for-syntax newlang)
>   x
>   )
> 
> (require test)
> 
> This gets a message "expand: unbound variable in module in: x".  Why isn't the
> new #%top kicking in within the test module?

It is. If you add a printf in `top' before the `syntax-case'
expression, then you see that it's used.

The expansion, though, is `(if ... (#%top . x))' using MzScheme's
`#%top' --- and inside `module', the `#%top' form always reports an
unbound-variable message.

You need to avoid `#%top' in the expansion, probably like this:

 (syntax/loc stx
  (namespace-variable-value 
   'identifier 
   #f
   (lambda () (printf "not defined: ~a\n" 'identifier))))


Matthew



Posted on the users mailing list.