[plt-scheme] identifier macros in application position

From: David A. Herman (dherman at ccs.neu.edu)
Date: Thu Jul 8 19:14:42 EDT 2004

Why doesn't the following work?

    ;; add1*: add1 with constant folding
    (define-syntax (add1* stx)
      (syntax-case stx ()
        [(add1* n)
         (number? (syntax-object->datum #'n))
         (datum->syntax-object stx (add1 (syntax-object->datum #'n)))]
        [add1*
         (identifier? #'add1*)
         #'add1]))

    (add1* (+ 2 3))

The expression fails to match, which surprised me. Apparently identifier
macros only match in operand position. Why is that? On the one hand, this
makes it easy to tell what position the macro is being used in, but on the
other hand, if identifier macros matched in any context, you could still
distinguish the position by capturing all application contexts in the
first patterns followed by the identifier macro, e.g.:

    (define-syntax (foo stx)
      (syntax-case stx ()
        [ ... special cases ... ]
        [(foo . anything)
         ... default case for application position ... ]
        [foo
         (identifier? #'foo)
         ... operand position case ...]))

Dave


Posted on the users mailing list.