[plt-scheme] Dybvig's 'case' macro doesn't work?

From: Ken Williams (klw at acm.org)
Date: Mon Dec 30 10:35:07 EST 2002

On Mon, 2002-12-30 at 09:44, Jens Axel Søgaard wrote:
> 
> Ok - c1 and cn are pieces of syntax.
> >  ...
> >                    (with-syntax ([rest (loop (car cn) (cdr cn))])
> 
> Here you take car to a piece of syntax. Althoug the syntax represents
> a list, it isn't a list itself. Just turn the syntax into a list of syntax
> objects
> using syntax->list:
> 
>                    (with-syntax ([rest (loop (car (syntax->list cn)) (cdr
> (syntax->list cn)))])

Ahh, thanks.  After posting the question, I noticed I could make it work
by using syntax-object->datum where you're suggesting syntax->list, but
that seemed like a useless discarding of syntax information.  I was
unaware of syntax->list.

Actually, I can factor out the two uses of syntax->list into one by
putting it at the top of the with-syntax, like so:

(define-syntax casex
  (lambda (stx)
    (syntax-case stx ()
      [(_ e c1 c2 ...)
       (with-syntax
           ([body
             (let loop ([c1 (syntax c1)]
                        [cn (syntax->list (syntax (c2 ...)))])
               (if (null? cn)
                   ; last arm of case
                   (syntax-case c1 (else)
                     [(else e1 e2 ...)
                      (syntax
                       (begin e1 e2 ...))]
                     [((k ...) e1 e2 ...)
                      (syntax
                       (if (memv t '(k ...))
                           (begin e1 e2 ...)))])
                   ; interior arm of case
                   (with-syntax ([rest (loop (car cn)
                                             (cdr cn))])
                     (syntax-case c1 ()
                       [((k ...) e1 e2 ...)
                        (syntax
                         (if (memv t '(k ...))
                             (begin e1 e2 ...)
                             rest))]))))])
         (syntax
          (let ([t e]) body)))])))



> Have you checked whether it works in PetiteChezScheme?

No, I haven't.  Guess I need to download and install it.

Assuming that Dybvig's macros do actually run correctly on some
platform, I'm curious about what might have changed between his original
implementation and PLT Scheme's (apparently) more persnickety
implementation.

> Hm. Strange the case - macro at
> 
> http://www.scheme.com/tspl2d/syntax.html#g2237
> 
> is different:

Well, I had changed the name of some bound variables and added a couple
of comments, but other than that, they were the same.

Thanks again,

++ Ken Williams




Posted on the users mailing list.