[plt-scheme] transformer environment and identifier-binding

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Mar 21 09:49:15 EST 2004

At Sun, 21 Mar 2004 02:53:46 -0500, Doug Orleans wrote:
>   (define-syntax foo (printf "~a~%" (identifier-binding #'lambda)))
>   ;; prints "(#%kernel lambda mzscheme lambda)"
>   (module lambda mzscheme
>     (provide lambda-binding)
>     (define (lambda-binding)
>       (identifier-binding #'lambda)))
>   (require-for-syntax lambda)
>   (define-syntax foo (printf "~a~%" (lambda-binding)))
>   ;; prints "#f"
> My guess as to what's happening is that #'lambda creates a syntax
> object whose lexical context is the current transformer environment,

It's both the transformer and run-time environment...

> and `require-for-syntax' shifts the phase so that it ends up with the
> meta-transformer environment, in which no symbols are bound, which is
> why lambda is reported as a free identifier. 

but this is right. The run-time bindings get shifted to transformer,
and transformer bindings gets shifted to meta-transformer, leaving
nothing at the run-time level.

> This is a contrived example, but I'm running into this problem when
> using `syntax-case' in a required-for-syntax module: it can't detect
> `lambda' in a syntax object, because it's not `module-identifier=?' to
> the `lambda' that appears in the literal list (because that `lambda'
> is a free identifier).  If I copy the `syntax-case' expression directly
> into the transformer expression, it works fine, but isn't the point of
> `require-for-syntax' that I should be able to put it into a module
> instead?

This is a common problem. When an imported function needs to compare
identifiers to some constant, one option is to pass the constant into
the helper function (because the caller has a suitable binding).

A `require-for-meta-run-time' form would be a better solution, so that
the helper module could import suitable bindings into its meta-run-time
environment (which would get shifted to run-time). But we haven't added
that form, yet.


Posted on the users mailing list.