1
0
Fork 0
cl-sites/HyperSpec-7-0/HyperSpec/Issues/iss229_w.htm
2024-04-01 10:24:07 +02:00

260 lines
15 KiB
HTML

<!-- Common Lisp HyperSpec (TM), version 7.0 generated by Kent M. Pitman on Mon, 11-Apr-2005 2:31am EDT -->
<HTML>
<HEAD>
<TITLE>CLHS: Issue MACRO-ENVIRONMENT-EXTENT Writeup</TITLE>
<LINK HREF="../Data/clhs.css" REL="stylesheet" TYPE="text/css" />
<META HTTP-EQUIV="Author" CONTENT="Kent M. Pitman">
<META HTTP-EQUIV="Organization" CONTENT="LispWorks Ltd.">
<LINK REL=TOP HREF="../Front/index.htm">
<LINK REL=COPYRIGHT HREF="../Front/Help.htm#Legal">
<LINK REL=DISCLAIMER HREF="../Front/Help.htm#Disclaimer">
<LINK REL=PREV HREF="../Issues/iss228_w.htm">
<LINK REL=UP HREF="../Issues/iss229.htm">
<LINK REL=NEXT HREF="../Issues/iss230_w.htm">
</HEAD>
<BODY>
<H1><A REV=MADE HREF="http://www.lispworks.com/"><IMG WIDTH=80 HEIGHT=65 ALT="[LISPWORKS]" SRC="../Graphics/LWSmall.gif" ALIGN=Bottom></A><A REL=TOP HREF="../Front/index.htm"><IMG WIDTH=237 HEIGHT=65 ALT="[Common Lisp HyperSpec (TM)]" SRC="../Graphics/CLHS_Sm.gif" ALIGN=Bottom></A> <A REL=PREV HREF="../Issues/iss228_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss229.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss230_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue MACRO-ENVIRONMENT-EXTENT Writeup</H2>
<PRE><B>Forum:</B> Compiler<P>
<B>Issue:</B> <A HREF="iss229.htm">MACRO-ENVIRONMENT-EXTENT</A><P>
<B>References:</B> CLtL p. 145-146<P>
Issue <A HREF="iss066.htm">COMPILER-LET-CONFUSION</A><P>
Issue MACRO-CACHING<P>
Issue <A HREF="iss147.htm">EVAL-WHEN-NON-TOP-LEVEL</A><P>
Issue <A HREF="iss342.htm">SYNTACTIC-ENVIRONMENT-ACCESS</A><P>
CLOS Chapter 3 (89-003)<P>
<B>Category:</B> CLARIFICATION,CHANGE<P>
<B>Edit History:</B> V1, 10 Jan 1989, Sandra Loosemore<P>
V2, 09 Mar 1989, Sandra Loosemore<P>
V3, 13 Mar 1989, Sandra Loosemore (last-minute discussion)<P>
<B>Status:</B> **DRAFT**<P>
<P>
<P>
<B>Problem Description:<P>
</B><P>
What is the extent of environment objects received as the <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A><P>
argument of a macro function?<P>
<P>
CLtL says that <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A> is &quot;useful primarily in the rare cases<P>
where a macro definition must explicitly expand any macros in a<P>
subform of the macro call before computing its own expansion&quot;. While<P>
this suggests that macro environment objects are typically used within<P>
the dynamic scope of the macro function, the use of the word<P>
&quot;primarily&quot; (rather than &quot;only&quot; or &quot;exclusively&quot; or some similarly<P>
strong language) suggests that there may be other legitimate uses for<P>
environment objects. But, because CLtL is silent about what those<P>
uses might be, many users and implementors are under the impression<P>
that environment objects have only dynamic extent.<P>
<P>
There are some situations where using environment objects as if they<P>
had indefinite extent provides a very useful viewpoint from which to<P>
solve a problem. Consider the following example:<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>defmacro</B></A> typed-var (var) var)<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>defmacro</B></A> local-type-declare (declarations <A REL=DEFINITION HREF="../Body/03_dd.htm#AMbody"><B>&amp;body</B></A> forms <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;environment</B></A> env)<P>
`(<A REL=DEFINITION HREF="../Body/s_flet_.htm#macrolet"><B>macrolet</B></A> ((typed-var (<A REL=DEFINITION HREF="../Body/03_dd.htm#AMwhole"><B>&amp;whole</B></A> w var)<P>
(<A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>let</B></A> ((<A REL=DEFINITION HREF="../Body/a_type.htm#type"><B>type</B></A> (<A REL=DEFINITION HREF="../Body/f_assocc.htm#assoc"><B>assoc</B></A> var ',declarations)))<P>
(<A REL=DEFINITION HREF="../Body/s_if.htm#if"><B>if</B></A> <A REL=DEFINITION HREF="../Body/a_type.htm#type"><B>type</B></A> <P>
`(<A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> ,(<A REL=DEFINITION HREF="../Body/f_car_c.htm#cadr"><B>cadr</B></A> <A REL=DEFINITION HREF="../Body/a_type.htm#type"><B>type</B></A>) ,var)<P>
(<A REL=DEFINITION HREF="../Body/f_mexp_.htm#macroexpand"><B>macroexpand</B></A> w ',env)))))<P>
,@forms))<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>defun</B></A> f (x y)<P>
(local-type-declare ((x <A REL=DEFINITION HREF="../Body/t_fixnum.htm#fixnum"><B>fixnum</B></A>) (y <A REL=DEFINITION HREF="../Body/a_float.htm#float"><B>float</B></A>))<P>
(+ (typed-var x) (typed-var y))))<P>
<P>
Here, local macro TYPED-VAR is defined to look first in the innermost<P>
lexical environment for information about the variable, and if there<P>
isn't any then it recurses on the next outermost lexical environment.<P>
The global definition of TYPED-VAR provides a terminal case to stop<P>
the recursion.<P>
<P>
There are other situations where the extent of macro environment<P>
objects comes into play. For example, if we allow caching of macro<P>
expansions (issue MACRO-CACHING), environments must have indefinite<P>
extent. It is unclear whether CLOS would be affected by allowing<P>
macro environments to have only dynamic extent. (The descriptions of<P>
the CLOS defining macros in document 89-003 seem to imply that the<P>
value of the <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A> argument appears in the expansion of the<P>
macro, but there now seems to be sentiment that the model of how the<P>
defining macros work that is presented there is broken.)<P>
<P>
<P>
<B>Proposal MACRO-ENVIRONMENT-EXTENT:INDEFINITE:<P>
</B><P>
State that macro environment objects received with the <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A><P>
argument of a macro function or as the argument to a <A REL=DEFINITION HREF="../Body/v_mexp_h.htm#STmacroexpand-hookST"><B>*MACROEXPAND-HOOK*</B></A><P>
function have indefinite extent.<P>
<P>
Note that implementations are not permitted to destructively modify<P>
lexical information in environment objects once they have been passed<P>
to a macro function. It is, however, permissible to add or remove<P>
global definitions that are accessible through the environment.<P>
<P>
Rationale:<P>
<P>
This legitimizes the use of idioms which depend on macro environments<P>
having indefinite extent.<P>
<P>
Since data objects in Lisp otherwise have indefinite extent, it is<P>
more consistent to give environment objects indefinite extent as<P>
well.<P>
<P>
<P>
<B>Proposal MACRO-ENVIRONMENT-EXTENT:DYNAMIC:<P>
</B><P>
State that macro environment objects received with the <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A><P>
argument of a macro function or with a <A REL=DEFINITION HREF="../Body/v_mexp_h.htm#STmacroexpand-hookST"><B>*MACROEXPAND-HOOK*</B></A> function<P>
have only dynamic extent; the consequences are undefined if they are<P>
referred to outside the dynamic extent of that macro function or hook<P>
function.<P>
<P>
Rationale:<P>
<P>
This allows implementations to use somewhat more efficient techniques<P>
for representing environment objects. For example, the storage could<P>
be stack-allocated, or environments could be bashed destructively<P>
instead of always being freshly heap-allocated.<P>
<P>
<P>
<B>Proposal MACRO-ENVIRONMENT-EXTENT:DYNAMIC-WITH-COPIER:<P>
</B><P>
State that macro environment objects received with the <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A><P>
argument of a macro function or with a <A REL=DEFINITION HREF="../Body/v_mexp_h.htm#STmacroexpand-hookST"><B>*MACROEXPAND-HOOK*</B></A> function<P>
have only dynamic extent; the consequences are undefined if they are<P>
referred to outside the dynamic extent of that macro function or hook<P>
function.<P>
<P>
Add a new function:<P>
<P>
COPY-ENVIRONMENT environment [function]<P>
<P>
This function returns an environment object that is semantically<P>
equivalent to &quot;environment&quot; (which must be an object of the type<P>
received with an <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A> argument to a macro or as an argument to<P>
a <A REL=DEFINITION HREF="../Body/v_mexp_h.htm#STmacroexpand-hookST"><B>*MACROEXPAND-HOOK*</B></A> function), but which may safely be referred to<P>
outside the dynamic extent of the macro function. This function is<P>
permitted to return an object that is <A REL=DEFINITION HREF="../Body/f_eq.htm#eq"><B>EQ</B></A> to its argument if that <P>
object may be safely used.<P>
<P>
Rationale:<P>
<P>
This allows implementations to use somewhat more efficient techniques<P>
for representing environment objects. For example, the storage could<P>
be stack-allocated, or environments could be bashed destructively<P>
instead of always being freshly heap-allocated.<P>
<P>
It also allows programmers to use idioms that rely on environment<P>
objects having indefinite extent.<P>
<P>
<P>
<B>Current Practice:<P>
</B><P>
Macro environments appear to have indefinite extent in Lucid Common<P>
Lisp, Kyoto Common Lisp, CMU Common Lisp (at least in the<P>
interpreter), Utah Common Lisp, and A-Lisp. A-Lisp internally uses<P>
the kind of idiom shown in the example above to implement <A REL=DEFINITION HREF="../Body/s_flet_.htm#flet"><B>FLET</B></A>,<P>
<A REL=DEFINITION HREF="../Body/s_flet_.htm#labels"><B>LABELS</B></A>, and <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>FUNCTION</B></A> as macros.<P>
<P>
Macro environments are stack-allocated in Symbolics Genera, and hence<P>
have dynamic extent.<P>
<P>
Macro environments also have dynamic extent on the TI Explorer,<P>
because the compiler uses special variables are used to keep track of<P>
lexical definitions.<P>
<P>
<P>
<B>Cost to implementors:<P>
</B><P>
For proposal INDEFINITE, some implementations would need to change. A<P>
simple implementation of macro environments that would fit the<P>
requirements of this proposal is to represent them as lists, pushing<P>
information for inner contours onto the front of the list as the<P>
contour is entered and popping the list when the contour is exited.<P>
<P>
For proposal DYNAMIC, there is no associated implementation cost.<P>
<P>
For proposal DYNAMIC-WITH-COPIER, the implementation cost is unknown<P>
but probably fairly small in most implementations.<P>
<P>
<P>
<B>Cost to users:<P>
</B><P>
For proposal INDEFINITE, there is no associated cost to users.<P>
<P>
For proposal DYNAMIC, users would not be able to portably use a<P>
simple and elegant approach to solving certain kinds of problems.<P>
<P>
For proposal DYNAMIC-WITH-COPIER, users would have to remember to make<P>
a copy of an environment object in some situations.<P>
<P>
<P>
<B>Benefits:<P>
</B><P>
It is made clear whether treating environment objects as if they had<P>
indefinite extent is portable usage.<P>
<P>
<P>
<B>Discussion:<P>
</B><P>
Proposal SYNTACTIC-ENVIRONMENT-ACCESS:ADD-FUNCTIONAL-INTERFACE<P>
includes adding a function called AUGMENT-ENVIRONMENT which could also<P>
be used to create a copy of an environment. However, it returns an<P>
object with the same extent as its argument, and therefore can not<P>
replace the function COPY-ENVIRONMENT under proposal<P>
DYNAMIC-WITH-COPIER.<P>
<P>
We have also considered a couple of other alternatives on this <P>
issue.<P>
<P>
One alternative would be to give &quot;remote&quot; environments created by<P>
<A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A> the extent of that call to <A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A>, while &quot;local&quot;<P>
environments (the null lexical environment and environments created by<P>
<A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A> and <A REL=DEFINITION HREF="../Body/f_eval.htm#eval"><B>EVAL</B></A>) would have indefinite extent.<P>
<P>
Another alternative would be to say that environments created by<P>
<A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A>, <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A>, or <A REL=DEFINITION HREF="../Body/f_eval.htm#eval"><B>EVAL</B></A> have a dynamic extent that includes the<P>
time when any other macro calls appearing lexically within the<P>
expansion returned by the macro function are expanded. This is<P>
similar to the extent of the special bindings made by COMPILER-LET.<P>
<P>
Both of these proposals could be combined with adding a copier<P>
function to deal with those implementations where environments are<P>
stack-allocated. They would both solve the extent problem for the<P>
example given in the problem description section, but not the general<P>
problem of macro caching. In conjunction with the proposals for issue<P>
<A HREF="iss342.htm">SYNTACTIC-ENVIRONMENT-ACCESS</A>, they would both <A REL=DEFINITION HREF="../Body/f_provid.htm#require"><B>require</B></A> some<P>
modifications to implementations that currently give macro<P>
environments dynamic extent.<P>
<P>
Loosemore supports proposal MACRO-ENVIRONMENT-EXTENT:INDEFINITE.<P>
<P>
Moon says:<P>
My opinion is that anything in CLOS that seems to depend on indefinite<P>
extent for macro environments is broken and needs to be fixed. It's not<P>
broken because of the environment extent, but for other reasons.<P>
Thus I believe in dynamic extent for environments.<P>
<P>
Neil Goldman says:<P>
In my code walker I have a pretty ugly way of dealing with <A REL=DEFINITION HREF="../Body/s_flet_.htm#macrolet"><B>MACROLET</B></A><P>
that would have been trivial if I could have counted on the<P>
ENVIRONMENT having indefinite extent. There may be some cleaner way<P>
than what I did, but I just looked at PCL's code walker, and it also<P>
is much more complex than would be necessary if environments had<P>
indefinite extent.<P>
<P>
</PRE>
<HR>
<A REL=NAVIGATOR HREF="../Front/StartPts.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Starting Points]" SRC="../Graphics/StartPts.gif" ALIGN=Bottom></A><A REL=TOC HREF="../Front/Contents.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Contents]" SRC="../Graphics/Contents.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Master.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Index]" SRC="../Graphics/Index.gif" ALIGN=Bottom></A><A REL=INDEX HREF="../Front/X_Symbol.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Symbols]" SRC="../Graphics/Symbols.gif" ALIGN=Bottom></A><A REL=GLOSSARY HREF="../Body/26_a.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Glossary]" SRC="../Graphics/Glossary.gif" ALIGN=Bottom></A><A HREF="../Front/X3J13Iss.htm"><IMG WIDTH=80 HEIGHT=40 ALT="[Issues]" SRC="../Graphics/Issues.gif" ALIGN=Bottom></A><BR>
<A REL=COPYRIGHT HREF="../Front/Help.htm#Legal"><I>Copyright 1996-2005, LispWorks Ltd. All rights reserved.</I></A><P>
</BODY>
</HTML>