[plt-scheme] macros for unit/sig

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Mon May 26 08:50:59 EDT 2003

At Sun, 25 May 2003 23:30:26 EDT, Benderjg2 at aol.com wrote:
> This (toy) macro defines a signed unit form that has an export signature, 
> but no imports. It is supposed to generate a signed unit with an empty 
> import list. The macro looks like it "ought" to work--but doesn't.
> (require (lib "unitsig.ss"))
> (define-syntax bad-unit
>   (syntax-rules ()
>     ((_ export-sig . body)
>      (unit/sig export-sig (import) . body))))
> (define-signature test^ (get set tick))
> (define test@
>   (bad-unit test^
>      (define xx 0)
>      (define (get) xx)
>      (define (set n) (set! xx n) xx)
>      (define (tick) (set! xx (+ xx 1)) xx)))
> The result is an error message:
>   unit: exported variable is not defined in: get

The `unit/sig' macro is broken. It gives all imported and exported
names the context of the `unit/sig' expression. In this case, the
`unit/sig' expression is generated by `bad-unit', so `get' acts like it
was introduced by the macro.

The following `bad-unit' works around the problem:

 (define-syntax (bad-unit stx)
   (syntax-case stx ()
     ((_ export-sig . body)
       (syntax-e #'(unit/sig export-sig (import) . body))))))

The same problem plauges David's example. Here's a workaround for that case:

 (define-syntax (define-algorithm expr)
   (syntax-case expr (define compute)
     [(_ name d1 d2 ...)
      #`(define name
              (syntax (unit/sig algorithm^
                        (import math^)
                        d1 d2 ...)))))]))

The current `unit/sig' is a hacked variant of the v100 implementation,
which means that it originally worked on S-expressions instead of
syntax objects, and that's why it's so broken. I'm working on a


Posted on the users mailing list.