[plt-scheme] Expand-syntax and fully expanded programs

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Thu Jul 16 20:07:03 EDT 2009

Jens Axel Søgaard wrote:
> Is it possible for expand-syntax to non-fully expanded code?
> I am seeing lambda-s instead of #%plain-lambda-s.
> 
> Consider the following:
> 
> The file "a.scm" contains:
> 
> #lang scheme/base
> (require (for-syntax scheme/base))
> (provide x inc-x! mx)
> (define x 0)
> (define-syntax mx (lambda (stx) #'x))
> (define (inc-x!) (set! x (+ x 1)))
> (if 1 2 3)
> (set! x 4)
> 
> In the DrScheme REPL:
> 
>   (read-accept-reader #t)
>   (parameterize ([current-namespace (make-base-namespace)])
>     (namespace-require 'scheme)
>     (namespace-require '(for-syntax scheme))
>     (syntax->datum
>      (expand-syntax
>       (namespace-syntax-introduce ; make sure "module" is a known construct ...
>        (with-input-from-file "a.scm"
>          (lambda () (read-syntax)))))))
> 
> The result:
> 
> (module a scheme/base
>   (#%module-begin
>    (#%require (for-meta 1 scheme/base))
>    (#%provide x inc-x! mx)
>    (define-values (x) '0)
>    (define-syntaxes (mx) (lambda (stx) (quote-syntax x)))
>    (define-values (inc-x!) (lambda () (set! x (#%app + x '1))))
>    (#%app call-with-values (lambda () (if '1 '2 '3)) print-values)
>    (#%app call-with-values (lambda () (set! x '4)) print-values)))
> 
> Besides the fishy lambda, the two last #%app-s are fishy too.
> They won't match #%app from scheme/base (I think).

Those lambdas really are #%plain-lambda. Likewise for the #%apps. See 
the yellow warning box (in the right margin) here:

http://docs.plt-scheme.org/reference/syntax-model.html#(part._fully-expanded)

Here's a smaller example. Try just fully expanding a lambda expression:

   > (syntax->datum (expand #'(lambda (x) x)))
   (#%expression (lambda (x) x))

If you extract the 'lambda' identifier, you can verify that it's really 
'#%plain-lambda':

   > (syntax-case (expand #'(lambda (x) x)) ()
       [(expr (lam . _))
        (free-identifier=? #'lam #'#%plain-lambda)])
   #t

It isn't the 'lambda' from scheme/base:

   > (syntax-case (expand #'(lambda (x) x)) ()
       [(expr (lam . _))
        (free-identifier=? #'lam #'lambda)])
   #f

Ryan


Posted on the users mailing list.