how to write macroexpand

Per Bothner per@bothner.com
Wed Oct 20 07:11:00 GMT 2010


Thanks for the improvements!  A few more questions.

First, a meta-question: Is macroexpand useful for anything besides
debugging or learning the expansion process?  In other words,
is there a use case beyond printing it out and then reading it
by a human?

This may affect how (and where) I document it in the Kawa manual.
It may also affect what name to call the function, and in which
module ('require' name) to give it.  They may also affect the priority
of returning "the right" sexpr vs just something that prints ok.

Note that macroexpand (or whatever we call it) will not be in the default
pre-defined environment - though I still think it is worth a little
bit of thought to pick the right name.  (I admit I may obsess too much
over details like names ...)

On 10/18/2010 02:24 AM, Helmut Eller wrote:
>> (2) I was trying to figure out if there is a standard name for this kind
>> of function.  SRFI-96 http://srfi.schemers.org/srfi-96/srfi-96.html does
>> have macroexpand, but only in the context of supporting defmacro. I see that
>> Guile has a macroexpand, but I haven't found the documentation, and it
>> doesn't seem to do as much.  Racket has expand-syntax and related forms,
>> but these yield syntax objects, not sexprs.  Common Lisp has macroexpand,
>> but it doesn't seem like it does a full expansion - just at the toplevel.
>>
>> The name macroexpand is fine - I just wonder if someone knows if other
>> Schemes
>> use a different name.
>
> It's called expand in Chez, Ikarus and Scheme48.

I haven't been able to find clear documentation on these.  I.e. do they
expand "inner" syntax or just outer-most forms?  Do they return a sexpr,
a syntax object (as in Racket), or something else?

Since this function expands not just macros but also primitive syntax,
perhaps syntax-expand might be a better name.  (It's probably best to avoid
expand-syntax, since Racket has a expand-syntax function that returns a 
syntax object,
not an sexpr.)

Perhaps expand-syntax->datum would be a bit verbose/pedantic side, but 
it makes
it clear the result is a datum (sexpr), not a syntax object (or an
Expression object).  So I'm tempted to go with that.
-- 
	--Per Bothner
per@bothner.com   http://per.bothner.com/



More information about the Kawa mailing list