[R6RS] SYNTAX-CASE

dyb at cs.indiana.edu dyb
Mon Apr 11 16:52:29 EDT 2005


> (define-expander + 
>    (lambda (x e)   ;; x is the form to be macro-expansed, e is an expander
>       (match-case x
>          ((?-)
>           0)
>          ((?- (and (? integer?) ?v1) (and (? integer?) ?v2))
>           (+ v1 v2))
>          ((?- (and (? fixnum?) ?v1) (and (? fixnum?) ?v2))
>           `(+fixnum ,v1 ,v2))
>          ((?- (and (? flonum?) ?v1) (and (? flonum?) ?v2))
>           `(+flonum ,v1 ,v2))
>          ((?- ?v1 . ?vs)
>           `(+ ,(e v2 e) ,(e `(+ , at vs) e))))))

I don't understand your syntax or perhaps the semantics of your operators.
It looks like your fixnum case will never be taken, and your fixnum
and flonum cases might as well do the computation rather than return
an application.  But I think I know what you're getting at.

Here's how I might write something similar using syntax-case in Chez Scheme.

(define-syntax +
  (lambda (x)
    (import scheme) ; so we can use the built-in +
    (syntax-case x ()
      [(_) 0]
      [(_ x) #'x]
      [(_ x y)
       (and (integer? (datum x)) (integer? (datum y)))
       (+ (datum x) (datum y))]
      [(k x m ...) #'(+ x (k m ...))])))

Datum is a handy little helper analogous to syntax:

(define-syntax datum
  (syntax-rules ()
    ((_ x) (syntax-object->datum (syntax x)))))

It wouldn't be difficult to generalize this to a more complete simplifier.

Kent


More information about the R6RS mailing list