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

240 lines
16 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 LOOP-INITFORM-ENVIRONMENT 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/iss221_w.htm">
<LINK REL=UP HREF="../Issues/iss222.htm">
<LINK REL=NEXT HREF="../Issues/iss223_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/iss221_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Previous]" SRC="../Graphics/Prev.gif" ALIGN=Bottom></A><A REL=UP HREF="../Issues/iss222.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Up]" SRC="../Graphics/Up.gif" ALIGN=Bottom></A><A REL=NEXT HREF="../Issues/iss223_w.htm"><IMG WIDTH=40 HEIGHT=40 ALT="[Next]" SRC="../Graphics/Next.gif" ALIGN=Bottom></A></H1>
<HR>
<H2>Issue LOOP-INITFORM-ENVIRONMENT Writeup</H2>
<PRE><B>Issue:</B> <A HREF="iss222.htm">LOOP-INITFORM-ENVIRONMENT</A><P>
<B>Forum:</B> Editorial<P>
<B>References:</B> <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> (pages 8-85, <P>
Draft 8.81 <P>
<B>Category:</B> CLARIFICATION<P>
<B>Edit history:</B> 05-Mar-91, Version 1 by Pitman<P>
15-Mar-91, Version 2 by Pitman<P>
<B>Status:</B> For X3J13 consideration<P>
<P>
<B>Problem Description:<P>
</B><P>
The Symbolics implementation of X3J13's <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> spec surprised some users <P>
by being incompatible with the old Symbolics Genera <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> on the <P>
following test case:<P>
<P>
(<A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>let</B></A> ((<A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> '(1 2 3)))<P>
(<A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>loop</B></A> for <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> = <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> then (<A REL=DEFINITION HREF="../Body/f_car_c.htm#cdr"><B>cdr</B></A> <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A>)<P>
until (<A REL=DEFINITION HREF="../Body/a_null.htm#null"><B>null</B></A> <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A>)<P>
<A REL=DEFINITION HREF="../Body/m_do_do.htm#do"><B>do</B></A> (<A REL=DEFINITION HREF="../Body/f_wr_pr.htm#princ"><B>princ</B></A> (<A REL=DEFINITION HREF="../Body/f_car_c.htm#car"><B>car</B></A> <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A>))))<P>
<P>
The Symbolics Genera implementation prints nothing, but old Symbolics <P>
Genera <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> prints 123. In double-checking the implementation against the<P>
working draft specification to determine if the implementation was in<P>
error, more than one Symbolics implementors did not feel this behavior <P>
was clearly enough spelled out and worried that implementations might not<P>
agree.<P>
<P>
Further investigation showed that Lucid Common Lisp had the same behavior.<P>
<P>
The following are the only references which appeared relevant:<P>
<P>
From page 8-85:<P>
<P>
The <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> macro translates the given &lt;form&gt; into code and returns the<P>
expanded &lt;form&gt;. The expanded &lt;form&gt; is one or more &lt;lambda<P>
expressions&gt; for the local &lt;binding&gt; of loop variables and a block<P>
and a <A REL=DEFINITION HREF="../Body/s_tagbod.htm#tagbody"><B>tagbody</B></A> that express a looping control <A REL=DEFINITION HREF="../Body/f_docume.htm#structure"><B>structure</B></A>. The variables<P>
established in <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> are bound as if by <A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>LET</B></A> or <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A>.<P>
Implementations can interleave the setting of initial values with<P>
the bindings. However, the assignment of the initial values is<P>
always calculated in othe order specified by the user. A variable is <P>
thus sometimes bound to a meaningless value of the correct type<P>
and then later in the prologue is set to the true initial value by<P>
using <A REL=DEFINITION HREF="../Body/s_setq.htm#setq"><B>SETQ</B></A>.<P>
<P>
Later, on page 8-86:<P>
<P>
The FOR and AS constructs <A REL=DEFINITION HREF="../Body/f_provid.htm#provide"><B>provide</B></A> iteration control clauses that<P>
establish a variable to be initialized. FOR and AS clauses can be<P>
combined with the <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> keyword <A REL=DEFINITION HREF="../Body/a_and.htm#and"><B>AND</B></A> to get parallel initialization and<P>
stepping. Otherwise, the initialization and stepping are sequential.<P>
<P>
The need for this information is so fundamental that it should be very<P>
plainly stated. These extremely vague phrases don't really say much<P>
about the environment, and since they don't say what goes into the <A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>LET</B></A><P>
or the <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A>, or even how many <A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>LET</B></A> or <A REL=DEFINITION HREF="../Body/a_lambda.htm#lambda"><B>LAMBDA</B></A> forms are involved, they<P>
don't really say much at all.<P>
<P>
Further, the vague statement on p8-85 about how LET+SETQ might be used to<P>
implement binding leaves an unusually large amount of latitude to <P>
implementations.<P>
<P>
Proposal (LOOP-INITFORM-ENVIRONMENT:INITFORM-BEFORE-BINDING)<P>
<P>
Clarify that the initforms in a FOR-AS clause<P>
(each being variously called <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> &lt;form1&gt;, <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> &lt;hash-table&gt;, <A REL=DEFINITION HREF="../Body/a_or.htm#or"><B>or</B></A> <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A><P>
&lt;package&gt; in <A REL=DEFINITION HREF="../Body/s_the.htm#the"><B>the</B></A> `bnf' on pages 8-82..8-83)<P>
are all evaluated (in left to right order) prior to establishing<P>
the binding for any of the the &lt;vars&gt; in the same clause.<P>
<P>
Clarify that the initforms in a WITH clause (each being<P>
called a &lt;form1&gt; in the `bnf' on pages 8-82..8-83)<P>
are all evaluated (in left to right order) prior to establishing<P>
the binding for any &lt;varN&gt; in the same clause.<P>
<P>
<B>Test Case:<P>
</B><P>
(<A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>let</B></A> ((<A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> '(1 2 3)))<P>
(<A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>loop</B></A> for <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> = <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> then (<A REL=DEFINITION HREF="../Body/f_car_c.htm#cdr"><B>cdr</B></A> <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A>)<P>
until (<A REL=DEFINITION HREF="../Body/a_null.htm#null"><B>null</B></A> <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A>)<P>
collect (<A REL=DEFINITION HREF="../Body/f_car_c.htm#car"><B>car</B></A> <A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A>)))<P>
=&gt; (1 2 3)<P>
<P>
<B>Rationale:<P>
</B><P>
This is what most users who make an analogy to <A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>LET</B></A> or <A REL=DEFINITION HREF="../Body/m_do_do.htm#do"><B>DO</B></A> will expect.<P>
<P>
<B>Current Practice:<P>
</B><P>
Symbolics Genera's FUTURE-COMMON-LISP:LOOP (which purports to conform<P>
to the draft specification) returns <A REL=DEFINITION HREF="../Body/a_nil.htm#nil"><B>NIL</B></A>.<P>
<P>
Symbolics Genera's LISP:LOOP (which purports to conform to CLtL)<P>
returns (1 2 3) for the test case.<P>
<P>
<B>Cost to Implementors:<P>
</B><P>
Hopefully small.<P>
<P>
<B>Cost to Users:<P>
</B><P>
Hopefully most users will consider this the status quo.<P>
<P>
<B>Cost of Non-Adoption:<P>
</B><P>
The specification might be ambiguous on an important point.<P>
<P>
<B>Benefits:<P>
</B><P>
Cost of non-adoption is avoided.<P>
<P>
<B>Aesthetics:<P>
</B><P>
Clarity improves aesthetics.<P>
<P>
<B>Discussion:<P>
</B><P>
Pitman and Moon support this proposal.<P>
JonL opposes this proposal.<P>
<P>
A long-winded discussion ensued, excerpts of which follow.<P>
<P>
Moon writes:<P>
``It's intentional that implementations should have latitude in <P>
the expansion of <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A>, but not to the extent that the meaning of<P>
the program changes! The problem is that the only thing this<P>
version of the <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> specification says about the scope of <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A><P>
variables is that they are lexical unless proclaimed special and<P>
their scope is the loop. This isn't specific enough.<P>
<P>
``Also in CLtL2 p.722 there is an example expansion which could be<P>
taken as an argument against the INITFORM-BEFORE-BINDING proposal.<P>
However I don't think that example was intended to talk about<P>
variable scoping. This example does not appear in the draft ANSI<P>
CL spec, which is good in my opinion.''<P>
<P>
JonL writes:<P>
``I don't see this as a clarification but as a change; in particular,<P>
it isn't consistent with the section of CLtL/II that you cite:<P>
[... page 8-85 ...]<P>
since it forbids the aforementioned &quot;interleaving&quot;. As Moon said, <P>
it is &quot;intentional that implementations should have latitude in<P>
the expansion of <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A>, ...&quot;. So the question is just how<P>
importantant is the case of loop variables &quot;shadowing&quot; local<P>
lexical variables? That is, I agree that the current spec is<P>
ambiguous on just when the binding of 'list' occurs in<P>
(let ((list '(1 2 3)))<P>
(loop for list = list then (<A REL=DEFINITION HREF="../Body/f_car_c.htm#cdr"><B>cdr</B></A> list)<P>
. . . ))<P>
but I'm more inclined to say SO WHAT? Is the world going into<P>
terminal meltdown just because this case isn't completely portable?<P>
is <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> completely unusable <A REL=DEFINITION HREF="../Body/v__stst_.htm#STSTST"><B>***</B></A> just because of this lexical<P>
shadowing ambiguity? <P>
<P>
``The point I'm trying to make is not that this case isn't a problem, <P>
but that it isn't a *big* problem; and even if it remained<P>
ambiguous (as was the intention for the draft proposal -- reasons<P>
cited below), it is nowhere near the magnitude of problem that we <P>
face in other areas.<P>
<P>
``... Lucid's <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> (unchanged for many years now) was modeled after<P>
GSB's MIT code, and it has the same behaviour as Genera. Even if<P>
the &quot;original designers&quot; intended something here, I think Glenn's<P>
execution of it might have led people to think otherwise. ...''<P>
<P>
Moon writes:<P>
<P>
``I don't see this particular issue as some technical issue of angels<P>
dancing on the heads of pins that is only of real interest to<P>
specialists. It's not uncommon for user programs to use the same<P>
variable name more than once. I hear that users very often find it<P>
frustrating to try to use <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> because there are implementations around<P>
that do unintuitive things and documentation around that they can't<P>
decode. Those problems are so unnecessary.<P>
<P>
``... I just now loaded up the MIT <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A>, unchanged for nearly five <P>
years (unless someone's been changing it without updating the edit<P>
history at the front) from REAGAN.AI.MIT.EDU, and tried the test case<P>
from the cleanup issue. It prints 123, so it conforms to the cleanup.<P>
This makes me suspect that the MIT <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> has always had the intuitive<P>
behavior, but programmers at Lucid and also at Symbolics broke it in the<P>
process of improving it. I don't think it's true that Glenn implemented<P>
it differently from what the cleanup says.<P>
<P>
``So then I tried the CMU implementation referenced in Scott Fahlman's<P>
recent message. It, too, conforms to the cleanup, although it seems to<P>
have its share of bugs (several compiler warnings and errors while<P>
loading that I fixed up and proceeded past).<P>
<P>
``... We can always specify a language feature as doing something<P>
unpredictable, but is that really necessary?''<P>
<P>
JonL writes:<P>
<P>
``I claim this ambiguity is purposeful -- the &quot;interleaving&quot; of binding<P>
and initialization clauses, which in some implementations has <P>
performance consequences -- and that the case that brought this <P>
ambiguity to public attention is not of great consequence to the<P>
language. <P>
<P>
``Of course, I agree that we must specify *exactly* the behaviour<P>
of <A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>LET</B></A> when local variables are being shadowed, such as in:<P>
(<A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>let</B></A> ((x (<A REL=DEFINITION HREF="../Body/a_list.htm#list"><B>list</B></A> x))) . . .)<P>
because renaming of bound variables is an important concept (not just<P>
&quot;pinheads dancing on angels&quot;). But <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> bindings are not documented to<P>
be *exactly* like <A REL=DEFINITION HREF="../Body/s_let_l.htm#let"><B>LET</B></A>, and they aren't the primariy means to achieve<P>
lexical &quot;shadows&quot;; thus I strongly disagree that seeking out and &quot;fixing&quot;<P>
documented ambiguities in the binding-order for <A REL=DEFINITION HREF="../Body/m_loop.htm#loop"><B>LOOP</B></A> &quot;shadows&quot; is of high<P>
priority.''<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>