[plt-scheme] whole-module compiler extensions

From: Jacob Matthews (jacobm at cs.uchicago.edu)
Date: Sat Dec 27 08:54:31 EST 2003

David A. Herman wrote:

>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme
>I'd like to perform static analysis on Scheme programs that isn't local to
>a single invocation, i.e., can't be done with a macro. I'm fine with
>closing the world at the module level, but I need to be able to analyze
>the entire module.
You might actually be able to get by with a macro - look up 
#%module-begin and #%plain-module-begin in the Help Desk. Each of these 
is automatically introduced by the expander (just like #%datum or #%top) 
wrapping an entire module body and serves as a hook to let you perform 
arbitrary computation on the whole body of the module treated as a 
Scheme syntax object.

Here's an example of a language that checks to make sure that the module 
includes at least one define-values, and raises a syntax error otherwise:


(module check mzscheme
  (provide (all-from-except mzscheme #%module-begin)
           (rename module-begin #%module-begin))
  (define-syntax (module-begin stx)
    (syntax-case stx ()
      [(_ body-statements ...)
       (if (ormap (lambda (x) (syntax-case x (define) [(define-values _ 
...) #t] [_ #f]))
                  (syntax-e #'(body-statements ...)))
           #'(#%module-begin body-statements ...)
           (raise-syntax-error #f "No define-values!" stx))])))


 > (module a check (define a 1))
 > (module b check 123)
#%module-begin: No define-values! in: (#%module-begin 234)


Posted on the users mailing list.