[plt-scheme] On hygiene and trust

From: Abdulaziz Ghuloum (aghuloum at gmail.com)
Date: Thu Jul 9 17:16:39 EDT 2009

On Jul 9, 2009, at 9:51 PM, Joe Marshall wrote:

> syntax-case is just too low-level.
> Sure, defmacro is even *lower* level, ...

On what basis are you comparing "syntax-case" with "defmacro".
Syntax-case is a pattern-matching facility for syntax objects.
Are you comparing it to the pattern-matching facilities of
defmacro, e.g., (define-macro foo (arg0 &rest args) ---) ?

I don't see how syntax-case as a pattern matcher is "just too
low-level".  How can a pattern matcher be low-level anyways?

> Suppose I want a macro that examines one of its arguments to see if it
> is a lambda expression.  The ideal thing, of course, is a function  
> that
> does just that:
> (define-syntax foo
>   (lambda (stx)
>     (if (lambda-expression? (first-argument stx))
>         ....)))

(define-syntax foo
   (lambda (stx)
     (define (first-argument x)
       (syntax-case x ()
         [(_ fst . rest) #'fst]))
     (define (lambda-expression? x)
       (syntax-case x (lambda)
         [(lambda . rest) #t]
         [_               #f]))
     (if (lambda-expression? (first-argument stx))

> At least defmacro allows this:
> (defmacro foo (arg0 &rest args)
>   (if (eq? (car arg0) 'lambda)    ;; crude, but effective

This is poor because it's forcing the use of car instead of
being a general pattern matcher like syntax-case.  If it
were a little more useful, it would've allowed you to say

(defmacro foo ((arg0 &rest rest0) &rest args)
   (if (eq? arg0 'lambda) ---))

or, even better,

(defmacro foo (('lambda &rest rest0) &rest args)

but hey, syntax-case does just that:

(define-syntax foo
   (lambda (stx)
     (syntax-case stx (lambda)
       [(_ (lambda . rest0) . args)
        --- handle lambda case ---]
       --- handle other cases ---)))

You almost never need to use something like mapcar or what
have you when writing macros using syntax-case since it (and
syntax) take care of destructuring and and constructing the
syntax objects.  This is effective, and not crude.


Posted on the users mailing list.