[plt-scheme] Where is macroexpand in PLT Scheme?

From: John Clements (clements at brinckerhoff.org)
Date: Wed Nov 12 16:10:08 EST 2003

On Wednesday, November 12, 2003, at 04:01  PM, Bill Clementson wrote:

> --- John Clements <clements at brinckerhoff.org> wrote:
>> On Wednesday, November 12, 2003, at 01:35  PM, Bill
>> Clementson wrote:
>>> Nice. And very useful. However, I'm surprised that
>>> there isn't a :top parameter in this code. One
>> that
>>> (in the case statement) would be: ((:top)
>>> (expand-to-top-form v))
>>> It seems like I'm the only person who finds the
>>> expand-to-top-form output more valuable than the
>>> expand output. This might be a consequence of me
>>> having come to Scheme from CL and I'm not seeing
>>> certain idioms that Schemers take for granted. Why
>> do
>>> people prefer to see the "intermediate" (for lack
>> of a
>>> better term to refer to it) output that you get
>> from
>>> expand rather than the more-fully expanded output
>> that
>>> you get from expand-to-top-form?
>> Hang on; the result of expand-to-top-form is
>> _less_-fully expanded.
> [snip documenation for expand-to-top-form]
>> the key word here is 'partially'. So, for instance:
>> (define-syntax foo
>>    (syntax-rules () [(foo x) (+ x 13)]))
>> (syntax-object->datum (expand-to-top-form #`(+ (foo
>> 12) 9)))
>> =>
>> (+ (foo 12) 9)
>> so expand-to-top-form does nothing here.  The result
>> of 'expand' is the
>> fully-expanded output.
> I think the differences are starting to become clearer
> to me: expand-to-top-form does a single-level
> expansion while expand does a full expansion.
> However, "expand-to-top-form" is probably what many
> people are interested in if they want to see what
> their macro expands out into. Since "expand" expands
> everything (including other macros that are referenced
> inside the top-level form it provides a lot more
> information. For the programmer who is just trying to
> debug a macro that they've created, isn't
> expand-to-top-form generally going to provide them
> with the right level of expansion? To use your example
> above, wouldn't (syntax-object->datum
> (expand-to-top-form '(foo 12))) be the expansion that
> I would do to test whether foo was doing what I
> expected it to do?

I think what you probably want is 'expand-once'.

 From the manual:

"(expand-once stx-or-sexpr) partially expands syntax in the 
stx-or-sexpr and returns a syntax object for the partially-expanded 
expression. Due to limitations in the expansion mechanism, some context 
information may be lost. In particular, calling expand-once on the 
result may produce a result that is different from expansion via 
expand. Before stx-or-sexpr is expanded, its lexical context is 
enriched with namespace-syntax-introduce (see section 8.3)."

The syntax chapter of the manual might have other useful information 
for you.


Posted on the users mailing list.