[plt-scheme] define-syntax by itself

From: Blake McBride (blake at mcbride.name)
Date: Sat Sep 9 18:10:12 EDT 2006

At 04:49 PM 9/9/2006, Jens Axel Søgaard wrote:
>Blake McBride skrev:
>
>>If I try:
>>(define-syntax foo
>>   (lambda (stx)
>>     (datum->syntax-object stx (car (syntax-object->datum stx)))))
>>(foo 6)
>>I get:  car: expects argument of type <pair>; given foo
>>So if stx represents the "foo" token, how do you get to the 6?
>
>You missed that the transformer is called twice.
>
>  To expand (foo 6) the transformer is called with the syntax-object
>  #'(foo 6).
>  The (car (syntax-object->datum stx)) extract the symbol foo,
>  and the (datum->syntax-object stx ...) converts it to an identifier
>  that are returned as the result.
>
>  Now the result is expanded. Now the transformer is called with
>  the single identifier #'foo. The (syntax-object->datum stx)
>  converts it to the symbol 'foo. And now (car 'foo) gives
>  the error you see.
>
>To get the 6 take the cadr instead of the car.
>
>(define-syntax foo
>   (lambda (stx)
>     (datum->syntax-object stx
>                           (cadr (syntax-object->datum stx)))))

I'm confused.  In my example car of (foo 6) returns foo.
Then its called again and I get the error when attempting
a car on foo.

You use cadr.  Let's follow this through with the same logic.
The first time you are doing a cadr on (foo 6) yielding 6.
But, when it goes through again it should be attempting a
cadr on the 6.  That shouldn't work either.

I think I am confused about the steps happening.  In lisp
macros it too goes through two passes.  The first time
the macro code is executed to create an s-expression.
Then the s-expression is evaluated.  Something different is
happening with define-syntax.  First of all I'm dealing with
a syntax object instead of an s-expression in the first pass
at least.

If I do (syntax-object->datum stx) do I get the s-expression
just like the lisp macro stuff?

If so, I should be able to manipulate it any way I like and then
run datum->syntax-object on it in the end to turn it back into
a syntax object to be evaluated.

This is my attempt to understand it but my experiments indicate
that my idea is wrong.

Thanks for working with me!

Blake McBride
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20060909/70a71152/attachment.html>

Posted on the users mailing list.