[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to use-modules within macro?
From: |
Mark H Weaver |
Subject: |
Re: How to use-modules within macro? |
Date: |
Wed, 04 Sep 2019 17:55:58 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) |
Hello again,
I wrote earlier:
> So, instead of using 'match' on the result of 'syntax->datum', you
> should instead use 'syntax-case' on the syntax object itself, like this
> (untested):
>
> (let loop ((e #'exp))
> (syntax-case e ()
> (num
> (number? (syntax->datum #'num))
> #'(1+ num))
> ((x ...)
> (map loop #'(x ...)))
> (y
> #'y)))
I should mention that the use of 'map' directly on a syntax object is
only allowable in certain cases. Here, we assume that the syntax object
produced by #'(x ...) is a normal Scheme list. That is _not_ the case
for arbitrary syntax objects that correspond to a list. For example, it
would _not_ be safe to pass 'e' as the list argument to 'map', although
'e' is in some sense equivalent to #(x ...) in the second clause above.
The reason is that syntax objects contain information about the
associated lexical environment which starts at the top of the expression
tree, and is *lazily* pushed down into the subexpressions as the tree is
taken apart using 'syntax-case' and put back together using 'syntax'
(a.k.a. "#'").
As a result, there are only a few cases when you can safely assume that
the top structure of a syntax object is a normal list or pair, and they
are spelled out in the documentation for 'syntax' in the R6RS Standard
Libraries specification:
http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-13.html#node_sec_12.4
Here's the relevant excerpt:
The output produced by syntax is wrapped or unwrapped according to the
following rules.
* the copy of (<t1> . <t2>) is a pair if <t1> or <t2> contain any
pattern variables,
* the copy of (<t> <ellipsis>) is a list if <t> contains any pattern
variables,
* the copy of #(<t1> ... <tn>) is a vector if any of <t1>, ..., <tn>
contain any pattern variables, and
* the copy of any portion of <t> not containing any pattern variables
is a wrapped syntax object.
A "wrapped syntax object" is one where the lexical environment
information has not yet been pushed down into the subexpressions. It is
a special kind of object that you can only take apart using
'syntax-case'.
So, in the clause above where 'map' is used, 'e' might be a "wrapped
syntax object", but when the elements are extracted from it using the
'syntax-case' pattern (x ...) and then put back together using
#'(x ...), you can then assume that the resulting syntax object is
a normal Scheme list of syntax objects, and therefore it is safe to
use 'map' on it.
Mark