[plt-scheme] Illegal Use of #%plain-module-begin

From: Casey Klein (clklein at calpoly.edu)
Date: Thu Jul 13 10:50:32 EDT 2006

As an exercise, I tried to write a syntax transformation which wraps all
constants in "begin" expressions which print them. This was my first
attempt:

   (define (annotate1 stx)
    (syntax-case stx (#%datum)
      [(#%datum . d) #'(begin (#%app write (#%datum . d)) (#%datum . d))]
      [(first . rest) #`(#,(annotate1 #'first) . #,(annotate1 #'rest))]
      [else #'else]))

The result, printed as an s-exp using syntax-object->datum, looked 
promising, but evaluation of the result produces the error 
"#%plain-module-begin: illegal use (not a module body)". Manually 
expanding the result, i.e. (expand (annotate1 src)), also produces this 
error.

This second transformation, however, produces syntax which evaluates 
cleanly:

   (define (annotate2 stx)
    (syntax-case stx (module #%plain-module-begin #%datum)
      [(#%datum . d) 
       #'(begin (#%app write (#%datum . d)) (#%datum . d))]
      [(module name lang (#%plain-module-begin . bodies))
       #`(module name lang (#%plain-module-begin . #,(annotate2 #'bodies)))]
      [(first . rest)
       #`(#,(annotate2 #'first) . #,(annotate2 #'rest))]
      [else #'else]))

There appears to be some relationship between the #%plain-module-begin
and the enclosing module which only annotate2 preserves, but I'm having
trouble identifying it (or maybe there's not).

Can someone explain this?

Thanks,
Casey


Posted on the users mailing list.