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

From: YC (yinso.chen at gmail.com)
Date: Thu Jun 28 17:14:16 EDT 2007

Hi all -

I run into a problem that are better explained via the example, but
basically I am having difficulty to get syntax-rules' literals and module
interactions correct, and need some help understanding where it all goes
awry.

What I want to do is to write a macro that would match on a sexp that does
different things based on the app-position token.  The example just prints
the sexp out as is without nesting.

expected behavior:
(m (< 5 10)) ; => (< 5 10)
(m (> 5 10)) ; => (> 5 10)

So I write a macro like below:

(define-syntax m
  (syntax-rules (> <)
    ((_ (> exp exp2))
     '(> exp exp2))
    ((_ (< exp exp2))
     '(< exp exp2))
    ))

and it works as expected on REPL.

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))

But if I eval m in another module instead of REPL, then it seems to work:

(module foo mzscheme
  (define-syntax m
    (syntax-rules (> <)
      ((_ (> exp exp2))
       '(> exp exp2))
      ((_ (< exp exp2))
       '(< exp exp2))
      ))
  (provide (all-defined)))

(module bar mzscheme
  (require foo)
(display (m (< 5 10)))
(newline)
(display (m (> 5 10))))

(require bar)
; prints below
; (< 5 10)
; (> 5 10)

Any thoughts on why it behaves this way?  I guess I lacked some simple
understanding here and appreciate the pointers.

Thanks,
yc
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20070628/bba05081/attachment.html>

Posted on the users mailing list.