[plt-scheme] more macro
Ivanyi Peter skrev:
> Hi there,
>
> I have noticed the following with macros. I could use macros
> to remove the quote sign, for example:
>
> (define-syntax m1
> (lambda (x)
> (syntax-case x ()
> ((_ 'f e1 e2 ...)
> (syntax (f e1 e2 ...))))))
>
> Usage:
> (syntax-object->datum (expand-once #'(m1 't a b c d)))
> Result:
> (t a b c d)
>
> I do not know whether it is a feature or a bug. However if I
> use this
> "feature", then I have a problem when I try to use the quote
> sign
> inside the macro expansion, for example:
>
> (define-syntax m2
> (lambda (x)
> (syntax-case x ()
> ((_ 'f e1 e2 ...)
> (letrec
> ((vars (map (lambda (x) (list x 'x))
> (syntax-object->datum (syntax (e1 e2
> ...))))))
> (with-syntax
> (((lvars ...) vars))
> (syntax (lvars ...))))))))
>
> Usage:
> (syntax-object->datum (expand-once #'(m2 't a b c d)))
> Expected result:
> ((a 'a) (b 'b) (c 'c) (d 'd))
>
> On the other hand macro m2 would report an error saying:
> quote: illegal use of syntax in: (quote x)
I misunderstood the issue the first time. It might be a bug.
On 301.13 I get:
> (define-syntax m2
(lambda (x)
(syntax-case x ()
((_ 'f e1 e2 ...)
(letrec
((vars (map (lambda (x) (list x 'x))
(syntax-object->datum (syntax (e1 e2 ...))))))
(with-syntax
(((lvars ...) vars))
(syntax (lvars ...))))))))
> (syntax-object->datum (expand-once #'(m2 't a b c d)))
(#%app (#%top . m2) 't (#%top . a) (#%top . b) (#%top . c) (#%top . d))
> (syntax-object->datum (expand #'(m2 't a b c d)))
(#%app (#%top . m2) 't (#%top . a) (#%top . b) (#%top . c) (#%top . d))
> Is this a bug or a feature? I do not really understand what is
> happening with the quote sign inside these macros.
The problem with
(syntax-case x ()
((_ 'f e1 e2 ...) <template-using-'>))
is that the pattern is equivalent to
(syntax-case x ()
((_ (quote f) e1 e2 ...) <template-using-'>))
which means that in <template-using-'> quote will be a pattern variable
bound to whatever occured before f in the input syntax x. To be
sure ' refers to a quote use
(syntax-case x (quote)
((_ (quote f) e1 e2 ...) <template-using-'>))
In your m2 the quote in 'x is not in a template, so I don't think
it should be a problem in that particular macro. Unless ... Hmm.
--
Jens Axel Søgaard