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

Re: Unhygienic macros



Quoting Lauri Alanko:
> Still, the concepts behind the syntax system are hard to grasp. 

True.

> I was trying to implement an unhygienic macro

I plan to add the classic "loop" example to the manual. The key step is
using `datum->syntax-object' to generate the non-hygienic identifier,
as you've discovered:

> (define-syntax (rec stx)
>  (syntax-case stx () 
>   ((_ args body ...)
>    (with-syntax ((self (datum->syntax-object stx 'self)))
>      #'(letrec ((self (lambda args body ...)))
>          self)))))

The second argument to `datum->syntax-object' is the datum to convert
to a syntax object. The first argument is an existing syntax object;
the new syntax object gets the lexical context of the existing syntax
object.

In other words, the symbol 'self in the above example gets converted to
a syntax object that acts as if it were introduced in exactly the same
place in the code as stx. Consequently, the resulting `self' identifier
acts as if it appeared with the original `rec' expression, and it binds
uses of `self' in the body.

> Does the datum->syntax-object function return the
> identifier "self" that is _free_ inside the expression stx?

Free or bound is an orthogonal question. The context of the `rec' use
might have a binding `self' around it, in which case the generated
`self' would indeed be bound, except that it's used in a binding
position instead of a potentially-bound position.

> I'm afraid I'm right now unable to formulate any specific question. I'd
> just like some clarification on how the syntax system works, what
> information exactly does a syntax object contain, at what stage are
> identifiers resolved and variables bound, etc... The syntax chapter of
> the manual doesn't really explain these thoroughly.

True; fixing that is on my list of things to do. For now, your best bet
is to look at

  Dybvig, Hieb, and Bruggeman, ``Syntactic abstraction in Scheme'' in
  Lisp and Symbolic Computation, December 1993.

It's available form Kent Dybvig's home page.

> One practical question, though: is there a way to "unquote" inside a
> (syntax ...) form, so one could more easily embed a computed syntax
> object inside a template? Of course one can always pre-bind them to
> template variables with with-syntax, but it's not always as compact.

Others have requested a `quasiquote-syntax', too. On my list.

Matthew