[plt-scheme] rules on syntax-rule literals?

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Thu Jun 28 17:34:05 EDT 2007

YC wrote:

> However, if I put the above into a module and run within REPL, then I 
> ran into bad-syntax error.
> 
> (module foo mzscheme
> (define-syntax m
>   (syntax-rules (> <)
>     ((_ (> exp exp2))
>      '(> exp exp2))
>     ((_ (< exp exp2))
>      '(< exp exp2))
>     ))
> (provide (all-defined)))
> (require foo)
> (m (< 5 10)) ; => m: bad syntax in: (m (< 5 10))
> (m (> 5 10)) ; => m: bad syntax in: (m (> 5 10))

The reason is subtle.

The < exported from mzscheme and the top-level <
isn't the same. Intuitively the reason is that
the mzscheme < is constant (and is always inlined),
whereas the top-level < can be assigned to.

You have two options. Either import the mzscheme
to the top-level before using m:

   (require foo)
   (require mzscheme)
   (m (< 5 10))

Or use syntax-case* with module-or-top-identifier=?

(module foo mzscheme
   (require-for-syntax (lib "stx.ss" "syntax"))
   (define-syntax (m stx)
     (syntax-case* stx (> <) module-or-top-identifier=?
       ((_ (> exp exp2))
        #''(> exp exp2))
       ((_ (< exp exp2))
        #''(< exp exp2))
       ))
   (provide (all-defined)))

(require foo)
(m (< 5 10))

-- 
Jens Axel Søgaard




Posted on the users mailing list.