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

252 lines
17 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 COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS 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/iss058_w.htm">
<LINK REL=UP HREF="../Issues/iss059.htm">
<LINK REL=NEXT HREF="../Issues/iss060_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/iss058_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss059.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss060_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS Writeup</H2>
<PRE><B>Forum:</B> Compiler<P>
<B>Issue:</B> <A HREF="iss059.htm">COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS</A><P>
<B>References:</B> CLtL pages 66-70, 143<P>
<B>Category:</B> CLARIFICATION<P>
<B>Edit history:</B> V1, 07 Oct 1987 Sandra Loosemore<P>
V2, 15 Oct 1987 Sandra Loosemore<P>
V3, 15 Jan 1988 Sandra Loosemore<P>
V4, 06 May 1988 Sandra Loosemore<P>
V5, 20 May 1988 Sandra Loosemore<P>
V6, 09 Jun 1988 Sandra Loosemore<P>
V7, 16 Dec 1988 Sandra Loosemore<P>
(Comments from Pitman, change <A REL=DEFINITION HREF="../Body/m_defcon.htm#defconstant"><B>DEFCONSTANT</B></A>, etc.)<P>
V8, 31 Dec 1988 Sandra Loosemore<P>
(CLOS additions, etc.)<P>
V9, 23 Jan 1989 Sandra Loosemore<P>
(remove the CLOS additions again)<P>
<B>Status:</B> Proposal CLARIFY passed Jan 89<P>
<P>
<P>
<B>Problem Description:<P>
</B><P>
Standard programming practices assume that, when calls to defining<P>
macros such as <A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>DEFMACRO</B></A> and <A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>DEFVAR</B></A> are processed by <A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A>,<P>
certain side-effects occur that affect how subsequent forms in the<P>
file are compiled. However, these side-effects are not mentioned in<P>
CLtL, except for a passing mention that macro definitions must be<P>
``seen'' by the compiler before it can compile calls to those macros<P>
correctly. In order to write portable programs, users must know<P>
exactly which defining macros have compile-time side-effects and what<P>
those side-effects are. <P>
<P>
Inter-file compilation dependencies are distinct from, and not<P>
addressed by, this issue. <P>
<P>
<P>
Proposal: <A HREF="iss059.htm">COMPILE-FILE-HANDLING-OF-TOP-LEVEL-FORMS:CLARIFY</A><P>
<P>
(1) Clarify that defining macros such as <A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>DEFMACRO</B></A> or <A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>DEFVAR</B></A>, appearing<P>
within a file being processed by <A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A>, normally have<P>
compile-time side effects which affect how subsequent forms in the<P>
same file are compiled. A convenient model for explaining how these<P>
side effects happen is that the defining macro expands into one or<P>
more <A REL=DEFINITION HREF="../Body/s_eval_w.htm#eval-when"><B>EVAL-WHEN</B></A> forms, and that the calls which cause the compile-time<P>
side effects to happen appear in the body of an (<A REL=DEFINITION HREF="../Body/s_eval_w.htm#eval-when"><B>EVAL-WHEN</B></A> (<A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A>)<P>
...) form.<P>
<P>
(2) The affected defining macros and their specific side effects are<P>
as follows. In each case, it is identified what users must do to<P>
ensure that their programs are conforming, and what compilers must do<P>
in order to correctly process a conforming program.<P>
<P>
DEFTYPE: Users must ensure that the body of a <A REL=DEFINITION HREF="../Body/m_deftp.htm#deftype"><B>DEFTYPE</B></A> form is<P>
evaluable at compile time if the type is referenced in subsequent type<P>
declarations. The compiler must ensure that the <A REL=DEFINITION HREF="../Body/m_deftp.htm#deftype"><B>DEFTYPE</B></A>'d type<P>
specifier is recognized in subsequent type declarations. If the<P>
expansion of a type specifier is not defined fully at compile time<P>
(perhaps because it expands into an unknown <A REL=DEFINITION HREF="../Body/a_type.htm#type"><B>type</B></A> specifier <A REL=DEFINITION HREF="../Body/a_or.htm#or"><B>or</B></A> a<P>
<A REL=DEFINITION HREF="../Body/t_satisf.htm#satisfies"><B>SATISFIES</B></A> of a named <A REL=DEFINITION HREF="../Body/a_fn.htm#function"><B>function</B></A> that isn't defined in <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> compile-time<P>
environment), an implementation may ignore any references to this type<P>
in declarations and/or signal a warning.<P>
<P>
<A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>DEFMACRO</B></A>, DEFINE-MODIFY-MACRO: The compiler must store macro<P>
definitions at compile time, so that occurrences of the macro later on<P>
in the file can be expanded correctly. Users must ensure that the<P>
body of the macro is evaluable at compile time if it is referenced<P>
within the file being compiled.<P>
<P>
DEFUN: <A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> is not required to perform any compile-time side effects.<P>
In particular, <A REL=DEFINITION HREF="../Body/m_defun.htm#defun"><B>DEFUN</B></A> does not make the function definition available<P>
at compile time. An implementation may choose to store information<P>
about the function for the purposes of compile-time error-checking<P>
(such as checking the number of arguments on calls), or to enable the<P>
function to be expanded <A REL=DEFINITION HREF="../Body/d_inline.htm#inline"><B>inline</B></A>.<P>
<P>
<A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>DEFVAR</B></A>, DEFPARAMETER: The compiler must recognize that the variables<P>
named by these forms have been proclaimed special. However, it must<P>
not evaluate the initial value form or <A REL=DEFINITION HREF="../Body/s_setq.htm#setq"><B>SETQ</B></A> the variable at compile<P>
time.<P>
<P>
DEFCONSTANT: The compiler must recognize that the symbol names a<P>
constant. An implementation may choose to evaluate the value-form at<P>
compile time, load time, or both. Therefore users must ensure that<P>
the value-form is evaluable at compile time (regardless of whether or<P>
not references to the constant appear in the file) and that it always<P>
evaluates to the same value. <P>
<P>
<A REL=DEFINITION HREF="../Body/m_defset.htm#defsetf"><B>DEFSETF</B></A>, DEFINE-SETF-METHOD: The compiler must make <A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>SETF</B></A> methods<P>
available so that it may be used to expand calls to <A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>SETF</B></A> later on in<P>
the file. Users must ensure that the body of DEFINE-SETF-METHOD and<P>
the complex form of <A REL=DEFINITION HREF="../Body/m_defset.htm#defsetf"><B>DEFSETF</B></A> are evaluable at compile time if the<P>
corresponding place is referred to in a subsequent <A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>SETF</B></A> in the same<P>
file. The compiler must make these <A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>SETF</B></A> methods available to <P>
compile-time calls to GET-SETF-METHOD when its environment argument is<P>
a value received as the <A REL=DEFINITION HREF="../Body/03_dd.htm#AMenvironment"><B>&amp;ENVIRONMENT</B></A> parameter of a macro.<P>
<P>
DEFSTRUCT: The compiler must make the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> type name recognized<P>
as a valid type name in subsequent declarations (as for <A REL=DEFINITION HREF="../Body/m_deftp.htm#deftype"><B>DEFTYPE</B></A>) and<P>
make the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> slot accessors known to <A REL=DEFINITION HREF="../Body/a_setf.htm#setf"><B>SETF</B></A>. In addition, the<P>
compiler must save enough information about the <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> type so that<P>
further <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A> definitions can :INCLUDE a <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A> type defined<P>
earlier in the file being compiled. The functions which <A REL=DEFINITION HREF="../Body/m_defstr.htm#defstruct"><B>DEFSTRUCT</B></A><P>
generates are not defined in the compile time environment, although<P>
the compiler may save enough information about the functions to code<P>
subsequent calls <A REL=DEFINITION HREF="../Body/d_inline.htm#inline"><B>inline</B></A>. The #S reader syntax may or may not be <P>
available at compile time. <P>
<P>
DEFINE-CONDITION: The rules are essentially the same as those for<P>
DEFSTRUCT; the compiler must make the condition type recognizable as a<P>
valid type name, and it must be possible to reference the condition<P>
type as the parent-type of another condition type in a subsequent<P>
<A REL=DEFINITION HREF="../Body/m_defi_5.htm#define-condition"><B>DEFINE-CONDITION</B></A> in the file being compiled.<P>
<P>
DEFPACKAGE: All of the actions normally performed by this macro at load<P>
time must also be performed at compile time.<P>
<P>
<P>
(3) The compile-time side effects may cause information about the<P>
definition to be stored differently than if the defining macro had<P>
been processed in the &quot;normal&quot; way (either interpretively or by loading<P>
the compiled file).<P>
<P>
In particular, the information stored by the defining macros at<P>
compile time may or may not be available to the interpreter (either<P>
during or after compilation), or during subsequent calls to <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A> or<P>
<A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A>. For example, the following code is nonportable because<P>
it assumes that the compiler stores the macro definition of FOO where<P>
it is available to the interpreter:<P>
<P>
(<A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>defmacro</B></A> foo (x) `(<A REL=DEFINITION HREF="../Body/f_car_c.htm#car"><B>car</B></A> ,x))<P>
(<A REL=DEFINITION HREF="../Body/s_eval_w.htm#eval-when"><B>eval-when</B></A> (<A REL=DEFINITION HREF="../Body/f_eval.htm#eval"><B>eval</B></A> <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>compile</B></A> <A REL=DEFINITION HREF="../Body/f_load.htm#load"><B>load</B></A>)<P>
(<A REL=DEFINITION HREF="../Body/f_wr_pr.htm#print"><B>print</B></A> (foo '(a b c))))<P>
<P>
A portable way to do the same thing would be to include the macro<P>
definition inside the EVAL-WHEN:<P>
<P>
(<A REL=DEFINITION HREF="../Body/s_eval_w.htm#eval-when"><B>eval-when</B></A> (<A REL=DEFINITION HREF="../Body/f_eval.htm#eval"><B>eval</B></A> <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>compile</B></A> <A REL=DEFINITION HREF="../Body/f_load.htm#load"><B>load</B></A>)<P>
(<A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>defmacro</B></A> foo (x) `(<A REL=DEFINITION HREF="../Body/f_car_c.htm#car"><B>car</B></A> ,x))<P>
(<A REL=DEFINITION HREF="../Body/f_wr_pr.htm#print"><B>print</B></A> (foo '(a b c))))<P>
<P>
<P>
<P>
<B>Rationale:<P>
</B><P>
The proposal generally reflects <A REL=DEFINITION HREF="../Body/07_ffb.htm#standard"><B>standard</B></A> programming practices. The<P>
primary purpose of the proposal is to make an explicit statement that<P>
CL supports the behavior that most programmers expect and many<P>
implementations already <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A>.<P>
<P>
The primary point of controversy on this issue has been the treatment<P>
of the initial value form by <A REL=DEFINITION HREF="../Body/m_defcon.htm#defconstant"><B>DEFCONSTANT</B></A>, where there is considerable<P>
variance between implementations. The effect of the current wording is<P>
to legitimize all of the variants.<P>
<P>
<P>
<B>Current Practice:<P>
</B><P>
Many (probably most) Common Lisp implementations, including VaxLisp<P>
and Lucid Lisp, are already largely in conformance. <P>
<P>
In VaxLisp, macro definitions that occur as a side effect of compiling<P>
a <A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>DEFMACRO</B></A> form are available to the compiler (even on subsequent<P>
calls to <A REL=DEFINITION HREF="../Body/f_cmp.htm#compile"><B>COMPILE</B></A> or <A REL=DEFINITION HREF="../Body/f_cmp_fi.htm#compile-file"><B>COMPILE-FILE</B></A>), but are not available to the<P>
interpreter (even within the file being compiled).<P>
<P>
By default, Kyoto Common Lisp evaluates *all* top level forms as they<P>
are compiled, which is clearly in violation of the behavior specified<P>
on p 69-70 of CLtL. There is a flag to disable the compile-time<P>
evaluation, but then macros such as <A REL=DEFINITION HREF="../Body/m_defmac.htm#defmacro"><B>DEFMACRO</B></A>, <A REL=DEFINITION HREF="../Body/m_defpar.htm#defvar"><B>DEFVAR</B></A>, etc. do not make<P>
their definitions available at compile-time either.<P>
<P>
<P>
<B>Cost to implementors:<P>
</B><P>
The intent of the proposal is specifically not to <A REL=DEFINITION HREF="../Body/f_provid.htm#require"><B>require</B></A> the compiler<P>
to have special knowledge about each of these macros. In<P>
implementations whose compilers do not treat these macros as special<P>
forms, it should be fairly straightforward to use EVAL-WHENs in their<P>
expansions to obtain the desired compile-time side effects.<P>
<P>
<P>
<B>Cost to users:<P>
</B><P>
Since CLtL does not specify whether and what compile-time side-effects<P>
happen, any user code which relies on them is, strictly speaking,<P>
nonportable. In practice, however, most programmers already expect<P>
most of the behavior described in this proposal and will not find it<P>
to be an incompatible change.<P>
<P>
<P>
<B>Benefits:<P>
</B><P>
Adoption of the proposal will <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A> more definite guidelines on how<P>
to write programs that will compile correctly under all CL<P>
implementations.<P>
<P>
<P>
<B>Discussion:<P>
</B><P>
Reaction to a preliminary version of this proposal on the common-lisp<P>
mailing list was overwhelmingly positive. More than one person<P>
responded with comments to the effect of &quot;but doesn't CLtL already<P>
*say* that somewhere?!?&quot; Others have since expressed a more lukewarm<P>
approval.<P>
<P>
It has been suggested that this proposal should also include <A REL=DEFINITION HREF="../Body/f_procla.htm#proclaim"><B>PROCLAIM</B></A>.<P>
However, since <A REL=DEFINITION HREF="../Body/f_procla.htm#proclaim"><B>PROCLAIM</B></A> is not a macro, its compile-time side effects<P>
cannot be handled using the <A REL=DEFINITION HREF="../Body/s_eval_w.htm#eval-when"><B>EVAL-WHEN</B></A> mechanism. A separate proposal<P>
seems more appropriate.<P>
<P>
Item (3) allows for significant deviations between implementations.<P>
While there is some sentiment to the effect that the compiler should<P>
store definitions in a manner identical to that of the interpreter,<P>
other people believe strongly that compiler side-effects should be<P>
completely invisible to the interpreter. The author is of the opinion<P>
that since this is a controversial issue, further attempts to restrict<P>
this behavior should be considered as separate proposals.<P>
<P>
It should be noted that user-written code-analysis programs must<P>
generally treat these defining macros as special forms and perform<P>
similar &quot;compile-time&quot; actions in order to correctly process<P>
conforming programs.<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>