* advice on how to use syntax transformers
@ 2015-03-06 14:51 Federico Beffa
2015-03-06 15:54 ` Panicz Maciej Godek
0 siblings, 1 reply; 3+ messages in thread
From: Federico Beffa @ 2015-03-06 14:51 UTC (permalink / raw)
To: guile-user
Hi,
I'm writing to ask for help in understanding syntax
transformers. Specifically, I'm trying to construct a function of the
following form
(define (key->value meta)
(match meta
(() '())
(((("name") value) rest ...)
value)
(((k value) rest ...)
(key->value (cdr meta)))
(_ "key Not fount")))
where I would like the ability to program the actual form of the ("name")
pattern with a second function argument. I've tried with the following
(define (key->value meta key)
(define-syntax match-key
;;(let ((key '("name")))
(lambda (x)
(syntax-case x ()
((_ e
(((k v) r ...) b ...) c ...)
#`(match e
(((#,key v) r ...)
v)
(((k v) r ...) b ...) c ...)))));)
(match-key meta
(((k v) r ...)
(key->value (cdr meta) key))
(_ "key not fount")))
which however, doesn't work and gives the following error:
---------------------------------------------------------
ice-9/psyntax.scm:1274:12: In procedure #<procedure 29405a0 (val key m)>:
ice-9/psyntax.scm:1274:12: Syntax error:
unknown location: reference to identifier outside its scope in form key
In ice-9/psyntax.scm:
1274:12 0 (#<procedure 29405a0 (val key m)> #(syntax-object key
((top) #(# …) …) …) …)
---------------------------------------------------------
If however I make key a local variable with the commented let expression, then
it works (but I loose the programming feature that I'm trying to implement).
I would greatly appreciate an hint on why the above is incorrect and what is
the correct form.
Thanks you in advance for your help.
Regards,
Fede
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: advice on how to use syntax transformers
2015-03-06 14:51 advice on how to use syntax transformers Federico Beffa
@ 2015-03-06 15:54 ` Panicz Maciej Godek
2015-03-06 16:23 ` Federico Beffa
0 siblings, 1 reply; 3+ messages in thread
From: Panicz Maciej Godek @ 2015-03-06 15:54 UTC (permalink / raw)
To: Federico Beffa; +Cc: guile-user@gnu.org
[-- Attachment #1: Type: text/plain, Size: 1914 bytes --]
2015-03-06 15:51 GMT+01:00 Federico Beffa <beffa@ieee.org>:
> Hi,
>
> I'm writing to ask for help in understanding syntax
> transformers. Specifically, I'm trying to construct a function of the
> following form
>
> (define (key->value meta)
> (match meta
> (() '())
> (((("name") value) rest ...)
> value)
> (((k value) rest ...)
> (key->value (cdr meta)))
> (_ "key Not fount")))
>
> where I would like the ability to program the actual form of the ("name")
> pattern with a second function argument.
I think that the following code (without additional syntax transformers)
should work for you:
(define (key->value meta key)
(match meta
(() '())
((((? (lambda(x) (equal? x key))) value) rest ...)
value)
(((key value) rest ...)
(key->value (cdr meta)))
(_
'key-not-found)))
but I don't know if that answers your question.
It seems to me that you are trying to use the value of a function argument
to construct a macro. Even if you managed to make it run somehow (which
should be possible through some nasty hacks), it would be an abuse, because
macros are intended to be used during macro expansion time, and not during
runtime (Oleg Kiselyov showed how to take advantage of macros in runtime,
if you're interested:
http://okmij.org/ftp/Scheme/macros.html#first-class-macros).
So don't interpret the code that you wrote as "a function that first
defines syntax 'match-key' and then uses that new syntax to generate
pattern matching code", because that's not how it works. The macro expander
tries to substitute the usage of "match-key" macro with appropriate code
immediately upon its usage, before you even run the function.
And although I'm not sure about this, because I don't know syntax-case
macros well enough, I think that your code is a very good example of the
difference between bound-identifier? and free-identifier?
(or isn't it?)
[-- Attachment #2: Type: text/html, Size: 2792 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: advice on how to use syntax transformers
2015-03-06 15:54 ` Panicz Maciej Godek
@ 2015-03-06 16:23 ` Federico Beffa
0 siblings, 0 replies; 3+ messages in thread
From: Federico Beffa @ 2015-03-06 16:23 UTC (permalink / raw)
To: Panicz Maciej Godek; +Cc: guile-user@gnu.org
On Fri, Mar 6, 2015 at 4:54 PM, Panicz Maciej Godek
<godek.maciek@gmail.com> wrote:
> I think that the following code (without additional syntax transformers)
> should work for you:
>
> (define (key->value meta key)
> (match meta
> (() '())
> ((((? (lambda(x) (equal? x key))) value) rest ...)
> value)
> (((key value) rest ...)
> (key->value (cdr meta)))
> (_
> 'key-not-found)))
>
> but I don't know if that answers your question.
Hi,
thanks for your reply! Indeed your function does what I need.
>
> It seems to me that you are trying to use the value of a function argument
> to construct a macro. Even if you managed to make it run somehow (which
> should be possible through some nasty hacks), it would be an abuse, because
> macros are intended to be used during macro expansion time, and not during
> runtime (Oleg Kiselyov showed how to take advantage of macros in runtime, if
> you're interested:
> http://okmij.org/ftp/Scheme/macros.html#first-class-macros).
>
> So don't interpret the code that you wrote as "a function that first defines
> syntax 'match-key' and then uses that new syntax to generate pattern
> matching code", because that's not how it works. The macro expander tries to
> substitute the usage of "match-key" macro with appropriate code immediately
> upon its usage, before you even run the function.
That is exactly the mistake I was doing in my mind.
Thanks again for your help!
Regards,
Fede
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2015-03-06 16:23 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-03-06 14:51 advice on how to use syntax transformers Federico Beffa
2015-03-06 15:54 ` Panicz Maciej Godek
2015-03-06 16:23 ` Federico Beffa
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).