134 lines
7.8 KiB
HTML
134 lines
7.8 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
|
|
<!Converted with LaTeX2HTML 0.6.5 (Tue Nov 15 1994) by Nikos Drakos (nikos@cbl.leeds.ac.uk), CBLU, University of Leeds >
|
|
<HEAD>
|
|
<TITLE>8.2. Macro Expansion</TITLE>
|
|
</HEAD>
|
|
<BODY>
|
|
<meta name="description" value=" Macro Expansion">
|
|
<meta name="keywords" value="clm">
|
|
<meta name="resource-type" value="document">
|
|
<meta name="distribution" value="global">
|
|
<P>
|
|
<b>Common Lisp the Language, 2nd Edition</b>
|
|
<BR> <HR><A NAME=tex2html2758 HREF="node100.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2756 HREF="node97.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2750 HREF="node98.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2760 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2761 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html2759 HREF="node100.html"> Destructuring</A>
|
|
<B>Up:</B> <A NAME=tex2html2757 HREF="node97.html"> Macros</A>
|
|
<B> Previous:</B> <A NAME=tex2html2751 HREF="node98.html"> Macro Definition</A>
|
|
<HR> <P>
|
|
<H1><A NAME=SECTION001220000000000000000>8.2. Macro Expansion</A></H1>
|
|
<P>
|
|
The <tt>macroexpand</tt> function is the conventional means for
|
|
expanding a macro call. A hook is provided for a user function
|
|
to gain control during the expansion process.
|
|
<P>
|
|
<BR><b>[Function]</b><BR>
|
|
<tt>macroexpand</tt> <tt><i>form</i></tt> <tt>&optional</tt> <tt><i>env</i></tt> <tt><BR></tt><tt>macroexpand-1</tt> <tt><i>form</i></tt> <tt>&optional</tt> <tt><i>env</i></tt><P>If <i>form</i> is a macro call, then <tt>macroexpand-1</tt> will expand the macro
|
|
call <i>once</i> and return two values: the expansion and <tt>t</tt>.
|
|
If <i>form</i> is not a macro call, then the two values <i>form</i> and <tt>nil</tt> are
|
|
returned.
|
|
<P>
|
|
A <i>form</i> is considered to be a macro call only if it is a cons whose
|
|
<i>car</i> is a symbol that names a macro. The environment <i>env</i> is similar
|
|
to that used within the evaluator (see <tt>evalhook</tt>);
|
|
it defaults to a null environment.
|
|
Any local macro definitions established within <i>env</i> by
|
|
<tt>macrolet</tt> will be considered. If only <i>form</i> is given as an
|
|
argument, then the environment is effectively null,
|
|
and only global macro definitions
|
|
(as established by <tt>defmacro</tt>) will be considered.
|
|
<P>
|
|
Macro expansion is carried out as follows. Once <tt>macroexpand-1</tt> has
|
|
determined that a symbol names a macro, it obtains the expansion
|
|
function for that macro. The value of the variable
|
|
<tt>*macroexpand-hook*</tt> is then called as a function of three arguments:
|
|
the expansion function, the <i>form</i>, and the environment <i>env</i>.
|
|
The value returned from
|
|
this call is taken to be the expansion of the macro call.
|
|
The initial value of <tt>*macroexpand-hook*</tt> is <tt>funcall</tt>,
|
|
and the net effect is to invoke the expansion function, giving
|
|
it <i>form</i> and <i>env</i> as its two arguments.
|
|
<P>
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
X3J13 voted in June 1988 (FUNCTION-TYPE) <A NAME=8549> </A> to specify
|
|
that the value of <tt>*macroexpand-hook*</tt> is first coerced to a
|
|
function before being called as the expansion interface hook.
|
|
Therefore its value may be a symbol, a lambda-expression, or any
|
|
object of type <tt>function</tt>.
|
|
<P>
|
|
X3J13 voted in March 1989 (MACRO-ENVIRONMENT-EXTENT) <A NAME=8554> </A>
|
|
to specify that macro environment objects received
|
|
by a <tt>*macroexpand-hook*</tt> function
|
|
have only dynamic extent. The consequences are undefined if such objects are
|
|
referred to outside the dynamic extent of that particular invocation of the hook
|
|
function. This allows implementations to use somewhat more efficient techniques
|
|
for representing environment objects.
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
<img align=bottom alt="old_change_begin" src="gif/old_change_begin.gif"><br>
|
|
(The purpose of
|
|
<tt>*macroexpand-hook*</tt> is to facilitate various techniques
|
|
for improving interpretation speed by caching macro expansions.)
|
|
<br><img align=bottom alt="old_change_end" src="gif/old_change_end.gif">
|
|
<P>
|
|
<img align=bottom alt="change_begin" src="gif/change_begin.gif"><br>
|
|
X3J13 voted in June 1989 (MACRO-CACHING) <A NAME=8561> </A> to clarify that, while
|
|
<tt>*macroexpand-hook*</tt> may be useful for debugging purposes, despite
|
|
the original design intent there is
|
|
currently no correct portable way to use it for caching macro expansions.
|
|
<UL><LI>
|
|
Caching by displacement (performing a side effect on the
|
|
macro-call form) won't work because the same (<tt>eq</tt>) macro-call
|
|
form may appear in distinct lexical contexts. In addition, the macro-call
|
|
form may be a read-only constant (see <tt>quote</tt> and also
|
|
section <A HREF="node224.html#COMPILERSECTION">25.1</A>).<p>
|
|
<LI>
|
|
Caching by table lookup won't work because such a table would have to
|
|
be keyed by both the macro-call form and the environment,
|
|
but X3J13 voted in March 1989 (MACRO-ENVIRONMENT-EXTENT) <A NAME=8567> </A>
|
|
to permit macro environments to have only dynamic extent.<p>
|
|
<LI>
|
|
Caching by storing macro-call forms and expansions within the
|
|
environment object itself would work, but there are no portable
|
|
primitives that would allow users to do this.
|
|
</UL>
|
|
X3J13 also noted that, although there seems to be no correct portable way to use
|
|
<tt>*macroexpand-hook*</tt> to cache macro expansions, there is no
|
|
requirement that an implementation call the macro expansion
|
|
function more than once for a given form and lexical environment.
|
|
<P>
|
|
X3J13 voted in March 1989
|
|
(SYMBOL-MACROLET-SEMANTICS) <A NAME=8572> </A>
|
|
to specify that <tt>macroexpand-1</tt> will also expand symbol macros
|
|
defined by <tt>symbol-macrolet</tt>; therefore a <i>form</i> may also be
|
|
a macro call if it is a symbol. The vote did not address the interaction
|
|
of this feature with the <tt>*macroexpand-hook*</tt> function. An obvious
|
|
implementation choice is that the hook function is indeed called
|
|
and given a special expansion function that, when applied to the
|
|
<i>form</i> (a symbol) and <i>env</i>, will produce the expansion,
|
|
just as for an ordinary macro; but this is only my suggestion.
|
|
<br><img align=bottom alt="change_end" src="gif/change_end.gif">
|
|
<P>
|
|
The evaluator expands macro calls as if through the use of <tt>macroexpand-1</tt>;
|
|
the point is that <tt>eval</tt> also uses <tt>*macroexpand-hook*</tt>.
|
|
<P>
|
|
<tt>macroexpand</tt> is similar to <tt>macroexpand-1</tt>,
|
|
but repeatedly expands <i>form</i> until it is no longer a macro call.
|
|
(In effect, <tt>macroexpand</tt> simply calls <tt>macroexpand-1</tt> repeatedly
|
|
until the second value returned is <tt>nil</tt>.)
|
|
A second value of <tt>t</tt> or <tt>nil</tt> is returned as for <tt>macroexpand-1</tt>,
|
|
indicating whether the original <i>form</i> was a macro call.
|
|
<P>
|
|
<BR><b>[Variable]</b><BR>
|
|
<tt>*macroexpand-hook*</tt><P>The value of <tt>*macroexpand-hook*</tt> is used as the expansion
|
|
interface hook by <tt>macroexpand-1</tt>.
|
|
<P>
|
|
<BR> <HR><A NAME=tex2html2758 HREF="node100.html"><IMG ALIGN=BOTTOM ALT="next" SRC="icons/next_motif.gif"></A> <A NAME=tex2html2756 HREF="node97.html"><IMG ALIGN=BOTTOM ALT="up" SRC="icons/up_motif.gif"></A> <A NAME=tex2html2750 HREF="node98.html"><IMG ALIGN=BOTTOM ALT="previous" SRC="icons/previous_motif.gif"></A> <A NAME=tex2html2760 HREF="node1.html"><IMG ALIGN=BOTTOM ALT="contents" SRC="icons/contents_motif.gif"></A> <A NAME=tex2html2761 HREF="index.html"><IMG ALIGN=BOTTOM ALT="index" SRC="icons/index_motif.gif"></A> <BR>
|
|
<B> Next:</B> <A NAME=tex2html2759 HREF="node100.html"> Destructuring</A>
|
|
<B>Up:</B> <A NAME=tex2html2757 HREF="node97.html"> Macros</A>
|
|
<B> Previous:</B> <A NAME=tex2html2751 HREF="node98.html"> Macro Definition</A>
|
|
<HR> <P>
|
|
<HR>
|
|
<P><ADDRESS>
|
|
AI.Repository@cs.cmu.edu
|
|
</ADDRESS>
|
|
</BODY>
|