Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

syntax-case question

1 view
Skip to first unread message

mark

unread,
Aug 20, 2007, 6:32:34 PM8/20/07
to
Folks,

I'm learning the syntax-case library and I don't understand a problem
I'm having at the moment. I hope someone is kind enough to point me
into the right direction. I have the following code:

(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1)
(let ()
(define self
(lambda ()
(test2)))
(define (name2)
(display "hello world")
(newline))
self)))))))

(test test1 test2)

(let ((obj (test1)))
(obj))

This throws a test2 unbound variable exception. If I change name2 in
the syntax expression to test2 it does work but that's not what I
want. The part that's not clear to me is why name1 (= test1) can be
called while name2 (= test2) apparently cannot be called. They are
defined in a similar way.

Cheers,
Mark

Jens Axel Søgaard

unread,
Aug 20, 2007, 7:45:33 PM8/20/07
to

The identifier test2 in the expansion of a use of the macro test
refers to the binding of test2 at the place of the macro definition.
Since you haven't bound test2 above, you get the unbound variable
error. Try the following to see what I mean:

(define (test2)
(display "Hello")
(newline))

(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1)
(let ()
(define self
(lambda ()
(test2)))
(define (name2)
(display "hello world")
(newline))
self)))))))

(test test1 test2)

(let ((obj (test1)))
(obj))

--
Jens Axel Søgaard

Jens Axel Søgaard

unread,
Aug 20, 2007, 7:56:26 PM8/20/07
to
mark wrote:

> This throws a test2 unbound variable exception. If I change name2 in
> the syntax expression to test2 it does work but that's not what I
> want. The part that's not clear to me is why name1 (= test1) can be
> called while name2 (= test2) apparently cannot be called. They are
> defined in a similar way.

Forgot to say, that you probably want to do as follows:

(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax
(define (name1)
(let ()
(define self
(lambda ()

(name2)))


(define (name2)
(display "hello world")
(newline))
self)))))))

(test test1 test2)

(let ((obj (test1)))
(obj))

--
Jens Axel Søgaard

mark

unread,
Aug 21, 2007, 5:59:20 AM8/21/07
to
> The identifier test2 in the expansion of a use of the macro test
> refers to the binding of test2 at the place of the macro definition.
> Since you haven't bound test2 above, you get the unbound variable
> error. Try the following to see what I mean:

Thanks, this is an explanation I was looking for. I already found the
solution proposed by you in your second post, but now I know why it
works that way.

Mark

mark

unread,
Aug 21, 2007, 7:43:07 AM8/21/07
to
I now have another, somewhat related, problem. If I have the
following:

(define-syntax test
(lambda (x)
(syntax-case x ()
((_ name1 name2)
(syntax

(define (name1 #!rest rest)


(let ()
(define self
(lambda ()

(name2)))


(define (name2)
(display "hello world")
(newline))
self)))))))

(test test1 test2)

(let ((obj (test1)))
(obj))

the macro expansion thinks #!rest is a variable, but it shouldn't be
one. How can I work around this?

Jens Axel Søgaard

unread,
Aug 21, 2007, 8:04:09 AM8/21/07
to

A little more context is needed to answer that question.
Which implementation do you use?

(Since #!rest is non-standard most Scheme implementations
will interpret #!rest a variable name. Since you expecting
something different, you must be using an implementation
that has given #!rest another interpretation)

--
Jens Axel Søgaard

mark

unread,
Aug 21, 2007, 8:21:16 AM8/21/07
to
> A little more context is needed to answer that question.
> Which implementation do you use?

Chicken.

Jens Axel Søgaard

unread,
Aug 21, 2007, 10:54:51 AM8/21/07
to mark

I tried your example in Chicken.

The portable syntax-case implementation, doesn't support rest-arguments.
Since the Chicken implementation of syntax-case is derived from the
portable one, my *guess* is that rest-arguments were forgotten.

To get a better answer, try posting your example at the Chicken
mailing list.

--
Jens Axel Søgaard

mark

unread,
Aug 21, 2007, 11:18:30 AM8/21/07
to
> I tried your example in Chicken.
>
> The portable syntax-case implementation, doesn't support rest-arguments.
> Since the Chicken implementation of syntax-case is derived from the
> portable one, my *guess* is that rest-arguments were forgotten.
>
> To get a better answer, try posting your example at the Chicken
> mailing list.

I'll try that. Thanks for your prompt and useful responses.

0 new messages