[plt-scheme] Macros and define-package

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Sun Dec 7 19:01:23 EST 2008

At Mon, 08 Dec 2008 00:35:46 +0100, Jens Axel Soegaard wrote:
> (define-syntax (define-Zn stx)
>   (syntax-case stx ()
>     [(_ pkg-id n-expr)
>      #`(let ([n n-expr])
>          (define-package pkg-id (+ *)
>            (define (+ a b) (modulo (mz:+ a b) n))
>            (define (* a b) (modulo (mz:* a b) n)))
>          (void))]))

One problem is that `define-package' is like any other definition form:
if you put it under `let', then it's not visible outside the `let'
form.

Another issue is that this macro introduces `+' and `*' bindings, so by
the usual scoping rules, they're not visible to expressions that
weren't also introduced by the macro. One solution is to introduce them
non-hygienically:

 (define-syntax (define-Zn stx)
   (syntax-case stx ()
     [(_ pkg-id n-expr)
      (with-syntax ([+ (datum->syntax #'pkg-id '+)]
                    [* (datum->syntax #'pkg-id '*)])
        #`(define-package pkg-id (+ *)
            (define n n-expr)
            (define (+ a b) (modulo (mz:+ a b) n))
            (define (* a b) (modulo (mz:* a b) n))))]))


Matthew



Posted on the users mailing list.