[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Unhygienic macros



Let me add a bit to this, though it may not be very useful.  In the
original definition of SYNTAX-CASE, Dybvig and co had a very nice
construct called IMPLICIT-IDENTIFIER.  If defined ... guess what.
However, it's less general than DATUM->SYNTAX-OBJECT, so it didn't
survive into their final versions, nor into PLT Scheme's.

Let me offer you a simple example that uses this.  (Lauri, you seem to 
have figured this out for yourself, but perhaps someone else will
benefit from this.)

Here's the definition of IMPLICIT-IDENTIFIER:

  (define (implicit-identifier original-context-id new-id-sym)
    (datum->syntax-object original-context-id new-id-sym))

Now, I'm going to define a simple macro that creates a special,
binding form of IF, called ARC-IF.  ARC-IF binds `IT' to the value of
the conditional.  That is,

  (arc-if (+ 1 2) it) ==> 3
  (arc-if #f 4 it) ==> #f

and so forth.

Now, here's the macro for ARC-IF:

  (define-syntax (arc-if expr)
    (syntax-case expr ()
      [(_ test then)
       (syntax (arc-if test then (void)))]
      [(_ test then else)
       (with-syntax ([it-id (implicit-identifier (syntax test) 'it)])
         (syntax (let ([it-id test])
                   (if it-id then else))))]))

Notice that I had to give IMPLICIT-IDENTIFIER two arguments:

- the expression to pretend IT came from
- the variable itself

I could write 

  (syntax _)

for the first argument, but I prefer to think of IT as if it were
bound by the TEST expression.  This has some good uses -- if anyone
cares, I can post them here.  But the bottom line is, the code above
is pretty straight-forward.  Indeed, I would argue that it's worth
considering building this special case of DATUM->SYNTAX-OBJECT back
into the macro system ...

Shriram