[plt-scheme] transformer environment and identifier-binding

From: Doug Orleans (dougo at place.org)
Date: Sun Mar 21 02:53:46 EST 2004

I was under the impression that importing a module with
`require-for-syntax' was pretty much the same as evaluating the module
body in the transformer environment, making it a good way to put
helper functions into a module instead of having to define them
locally in a transformer expression.

Well, I ran into a case where it seems to act differently:
`identifier-binding' reports a different binding when invoked directly
in a transformer environment than when invoked via an imported
procedure.

  (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,
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.  What I don't understand
is why this doesn't also happen when #'lambda is evaluated as part of
a transformer expression.

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?

--dougo at place.org

P.S. Is there a better way to evaluate an expression in the
transformer environment than `(define-syntax foo expr)'?


Posted on the users mailing list.