concept-eval.tex 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. % -*- Mode: TeX -*-
  2. %%Evaluation
  3. \term{Execution} of \term{code} can be accomplished by a variety of means ranging
  4. from direct interpretation of a \term{form} representing a \term{program}
  5. to invocation of \term{compiled code} produced by a \term{compiler}.
  6. \newtermidx{Evaluation}{evaluation} is the process by which a \term{program} is \term{executed} in \clisp.
  7. The mechanism of \term{evaluation} is manifested
  8. both implicitly through the effect of the \term{Lisp read-eval-print loop},
  9. and explicitly through the presence of the \term{functions}
  10. \funref{eval},
  11. \funref{compile},
  12. \funref{compile-file},
  13. and \funref{load}.
  14. Any of these facilities might share the same execution strategy,
  15. or each might use a different one.
  16. The behavior of a \term{conforming program} processed by \funref{eval}
  17. and by \funref{compile-file} might differ; \seesection\SemanticConstraints.
  18. \term{Evaluation} can be understood in terms of a model in which an
  19. interpreter recursively traverses a \term{form} performing each
  20. step of the computation as it goes.
  21. This model, which describes the semantics of \clisp\ \term{programs},
  22. is described in \secref\EvaluationModel.
  23. \beginsubSection{Introduction to Environments}
  24. \DefineSection{IntroToEnvs}
  25. A \newterm{binding} is an association between a \term{name} and
  26. that which the name denotes. \term{Bindings} are \term{established}
  27. in a \term{lexical environment} or a \term{dynamic environment}
  28. by particular \term{special operators}.
  29. An \newterm{environment} is a set of \term{bindings} and other information
  30. used during evaluation (\eg to associate meanings with names).
  31. \term{Bindings} in an \term{environment} are partitioned into \newtermidx{namespaces}{namespace}.
  32. A single \term{name} can simultaneously have more than one
  33. associated \term{binding} per \term{environment},
  34. but can have only one associated \term{binding} per \term{namespace}.
  35. %!!!! Moon: There out to be a list of the namespaces!!
  36. % Do they even have agreed-upon names?
  37. \beginsubsubsection{The Global Environment}
  38. The \newterm{global environment} is that part of an \term{environment}
  39. that contains \term{bindings} with both \term{indefinite scope}
  40. and \term{indefinite extent}.
  41. The \term{global environment} contains, among other things, the following:
  42. \beginlist
  43. \item{\bull} \term{bindings} of \term{dynamic variables} and \term{constant variables}.
  44. \item{\bull} \term{bindings} of \term{functions}, \term{macros}, and \term{special operators}.
  45. \item{\bull}%
  46. \issue{DEFINE-COMPILER-MACRO:X3J13-NOV89}%
  47. \term{bindings} of \term{compiler macros}.
  48. \endissue{DEFINE-COMPILER-MACRO:X3J13-NOV89}%
  49. \item{\bull} \term{bindings} of \term{type} and \term{class} \term{names}
  50. \item{\bull} information about \term{proclamations}.
  51. \endlist
  52. \endsubsubsection%{The Global Environment}
  53. \beginsubsubsection{Dynamic Environments}
  54. A \newterm{dynamic environment} for \term{evaluation} is that part of an
  55. \term{environment} that contains \term{bindings} whose duration
  56. is bounded by points of \term{establishment} and \term{disestablishment}
  57. within the execution of the \term{form} that
  58. established the \term{binding}.
  59. A \term{dynamic environment} contains, among other things, the following:
  60. \beginlist
  61. \item{\bull} \term{bindings} for \term{dynamic variables}.
  62. \item{\bull} information about \term{active} \term{catch tags}.
  63. \item{\bull} information about \term{exit points} established by \specref{unwind-protect}.
  64. \item{\bull} information about \term{active} \term{handlers} and \term{restarts}.
  65. \endlist
  66. The \term{dynamic environment} that is active at any given point
  67. in the \term{execution} of a \term{program} is referred to by
  68. definite reference as ``the current \term{dynamic environment},''
  69. or sometimes as just ``the \term{dynamic environment}.''
  70. Within a given \term{namespace},
  71. a \term{name} is said to be \term{bound}
  72. in a \term{dynamic environment} if there is a \term{binding}
  73. %or mapping
  74. associated with its \term{name} in the \term{dynamic environment}
  75. or, if not, there is a \term{binding}
  76. %or mapping
  77. associated with its name in the \term{global environment}.
  78. \endsubsubsection%{Dynamic Environments}
  79. \beginsubsubsection{Lexical Environments}
  80. A \newterm{lexical environment} for \term{evaluation} at some position in a \term{program}
  81. is that part of the \term{environment} that contains information having
  82. \term{lexical scope} within the \term{forms} containing that position.
  83. A \term{lexical environment} contains, among other things, the following:
  84. \beginlist
  85. \item{\bull} \term{bindings} of \term{lexical variables} and \term{symbol macros}.
  86. \item{\bull} \term{bindings} of \term{functions} and \term{macros}.
  87. (Implicit in this is information about those \term{compiler macros}
  88. that are locally disabled.)
  89. %% Replaced by parenthetical remark on previous bullet. -kmp 5-Dec-91
  90. % \item{\bull} \term{bindings} of \term{compiler macros}
  91. % (specifically, information about those \term{compiler macros}
  92. % that are locally disabled)
  93. \item{\bull} \term{bindings} of \term{block tags}.
  94. \item{\bull} \term{bindings} of \term{go tags}.
  95. \item{\bull} information about \term{declarations}.
  96. \endlist
  97. The \term{lexical environment} that is active at any given position
  98. in a \term{program} being semantically processed is referred to by
  99. definite reference as ``the current \term{lexical environment},''
  100. or sometimes as just ``the \term{lexical environment}.''
  101. Within a given \term{namespace},
  102. a \term{name} is said to be \term{bound} in a \term{lexical environment}
  103. if there is a \term{binding}
  104. %or mapping
  105. associated with its \term{name}
  106. in the \term{lexical environment} or, if not, there is a \term{binding}
  107. %or mapping
  108. associated with its name in the \term{global environment}.
  109. \beginsubsubsubsection{The Null Lexical Environment}
  110. \DefineSection{NullLexicalEnv}
  111. The \newterm{null lexical environment} is equivalent to the \term{global environment}.
  112. Although in general the representation of an \term{environment} \term{object}
  113. is \term{implementation-dependent}, \nil\ can be used in any situation where an
  114. \term{environment} \term{object} is called for in order to denote
  115. the \term{null lexical environment}.
  116. \endsubsubsubsection%{The Null Lexical Environment}
  117. \endsubsubsection%{Lexical Environments}
  118. \beginsubsubsection{Environment Objects}
  119. \DefineSection{EnvObjs}
  120. Some \term{operators} make use of an \term{object},
  121. called an \newterm{environment object},
  122. that represents the set of \term{lexical bindings} needed to perform
  123. semantic analysis on a \term{form} in a given \term{lexical environment}.
  124. The set of \term{bindings} in an \term{environment object}
  125. may be a subset of the \term{bindings} that would be needed to actually
  126. perform an \term{evaluation}; for example, \term{values} associated with
  127. \term{variable} \term{names} and \term{function names} in the corresponding
  128. \term{lexical environment} might not be available in an \term{environment object}.
  129. The \term{type} and nature of an \term{environment object} is \term{implementation-dependent}.
  130. The \term{values} of \term{environment parameters} to \term{macro functions}
  131. are examples of \term{environment objects}.
  132. The \term{object} \nil\ when used as an \term{environment object}
  133. denotes the \term{null lexical environment};
  134. \seesection\NullLexicalEnv.
  135. \endsubsubsection%{Environment Objects}
  136. \endSubsection%{Introduction to Environments}
  137. \beginSubsection{The Evaluation Model}
  138. \DefineSection{EvaluationModel}
  139. A \clisp\ system evaluates \term{forms} with respect to lexical,
  140. dynamic, and global \term{environments}. The following sections
  141. describe the components of the \clisp\ evaluation model.
  142. \beginsubsubsection{Form Evaluation}
  143. %% 5.1.0 2
  144. %% 5.1.1 1
  145. \term{Forms} fall into three categories:
  146. %!!!Is this a correct use of self-evaluating? -kmp 09-Apr-91
  147. \term{symbols}, \term{conses}, and \term{self-evaluating objects}.
  148. The following sections explain these categories.
  149. \beginsubsubsubsection{Symbols as Forms}
  150. \DefineSection{SymbolsAsForms}
  151. If a \term{form} is a \term{symbol},
  152. then it is either a \term{symbol macro} or a \term{variable}.
  153. The \term{symbol} names a \term{symbol macro}
  154. if there is a \term{binding} of the \term{symbol} as a \term{symbol macro}
  155. in the current \term{lexical environment}
  156. \issue{ISO-COMPATIBILITY:ADD-SUBSTRATE}
  157. (see \macref{define-symbol-macro} and \specref{symbol-macrolet}).
  158. \endissue{ISO-COMPATIBILITY:ADD-SUBSTRATE}
  159. If the \term{symbol} is a \term{symbol macro},
  160. its expansion function is obtained.
  161. The expansion function is a function of two arguments, and is invoked
  162. by calling the \term{macroexpand hook} with
  163. the expansion function as its first argument,
  164. the \term{symbol} as its second argument,
  165. and an \term{environment object} (corresponding to the current \term{lexical environment})
  166. as its third argument.
  167. The \term{macroexpand hook}, in turn, calls the expansion function with the
  168. \term{form} as its first argument and the \term{environment} as its second argument.
  169. The \term{value} of the expansion function, which is passed through
  170. by the \term{macroexpand hook}, is a \term{form}.
  171. This resulting \term{form} is processed in place of the original \term{symbol}.
  172. If a \term{form} is a \term{symbol} that is not a \term{symbol macro},
  173. then it is the \term{name} of a \term{variable}, and the \term{value} of that
  174. \term{variable} is returned. There are three kinds of variables:
  175. \term{lexical variables},
  176. \term{dynamic variables},
  177. and
  178. \term{constant variables}.
  179. % global variables.
  180. A \term{variable} can store one \term{object}.
  181. The main operations on a \term{variable} are
  182. to \term{read}\meaning{1} and
  183. to \term{write}\meaning{1}
  184. its \term{value}.
  185. %% 7.1.1 1
  186. %% 7.2.0 2
  187. %% 7.2.0 3
  188. An error \oftype{unbound-variable} should be signaled if
  189. an \term{unbound variable} is referenced.
  190. \term{Non-constant variables} can be \term{assigned} by using \specref{setq}
  191. or \term{bound}\meaning{3} by using \specref{let}.
  192. \Thenextfigure\ lists some \term{defined names} that
  193. are applicable to assigning, binding, and defining \term{variables}.
  194. \displaythree{Some Defined Names Applicable to Variables}{
  195. boundp&let&progv\cr
  196. defconstant&let*&psetq\cr
  197. defparameter&makunbound&set\cr
  198. defvar&multiple-value-bind&setq\cr
  199. lambda&multiple-value-setq&symbol-value\cr
  200. }
  201. The following is a description of each kind of variable.
  202. \beginsubsubsubsubsection{Lexical Variables}
  203. %!!! Issue of (let ((x x)) (declare (special x)) ...). Is the outer X special? -kmp
  204. %% 5.1.2 2
  205. %% Rewritten by KAB and KMP.
  206. % A \term{variable} that occurs textually within a \term{form}
  207. % that creates a \term{lexical binding} for a \term{variable}
  208. % of the same name is a \term{lexical variable} unless the reference is locally
  209. % declared \declref{special} (see \misc{declare}) or the \term{binding}
  210. % is shadowed by a \term{form} that creates a \term{dynamic binding} of the same name.
  211. %% 5.1.2 3
  212. A \term{lexical variable} is a \term{variable} that can be referenced only within
  213. the \term{lexical scope} of the \term{form} that establishes that \term{variable};
  214. \term{lexical variables} have \term{lexical scope}.
  215. Each time a \term{form} creates a \term{lexical binding} of a \term{variable},
  216. a \term{fresh} \term{binding} is \term{established}.
  217. Within the \term{scope} of a \term{binding} for a \term{lexical variable} \term{name},
  218. uses of that \term{name} as a \term{variable} are considered to be references
  219. to that \term{binding} except where the \term{variable} is \term{shadowed}\meaning{2}
  220. by a \term{form} that \term{establishes} a \term{fresh} \term{binding} for that
  221. \term{variable} \term{name},
  222. or by a \term{form} that locally \term{declares} the \term{name} \declref{special}.
  223. A \term{lexical variable} always has a \term{value}.
  224. There is no \term{operator} that introduces a \term{binding} for a
  225. \term{lexical variable} without giving it an initial \term{value}, nor
  226. is there any \term{operator} that can make a \term{lexical variable} be \term{unbound}.
  227. \term{Bindings} of \term{lexical variables} are found in the \term{lexical environment}.
  228. \endsubsubsubsubsection%{Lexical Variables}
  229. \beginsubsubsubsubsection{Dynamic Variables}
  230. A \term{variable} is a \term{dynamic variable} if one of the following
  231. conditions hold:
  232. \beginlist
  233. \item{\bull} It is locally declared or globally proclaimed \declref{special}.
  234. \item{\bull} It occurs textually within a \term{form} that
  235. creates a \term{dynamic binding} for a \term{variable} of the \term{same} \term{name},
  236. and the \term{binding} is not \term{shadowed}\meaning{2} by a \term{form}
  237. that creates a \term{lexical binding} of the same \term{variable} \term{name}.
  238. %% Is this right? Is there an issue that applies to this? -kmp
  239. %% Removed for now because Barrett and Moon balked, too. -kmp 13-Oct-91
  240. % \item{\bull} It is not locally declared \declref{special},
  241. % and it does not occur textually within a \term{form} that creates
  242. % a lexical \term{binding} for a variable of the same name.
  243. \endlist
  244. %!!! Barrett wants to mention "dynamic scope" here someplace.
  245. A \term{dynamic variable} can be referenced at any time in any \term{program};
  246. there is no textual limitation on references to \term{dynamic variables}.
  247. At any given time, all \term{dynamic variables} with a given name refer to
  248. exactly one \term{binding}, either in the \term{dynamic environment}
  249. or in the \term{global environment}.
  250. %% 5.1.2 5
  251. The \term{value} part of the \term{binding} for a \term{dynamic variable} might
  252. be empty; in this case, the \term{dynamic variable} is said to have no \term{value},
  253. or to be \term{unbound}. A \term{dynamic variable} can be made \term{unbound}
  254. by using \funref{makunbound}.
  255. The effect of \term{binding} a \term{dynamic variable} is to create
  256. a new \term{binding} to which all references to that \term{dynamic variable}
  257. in any \term{program} refer for the duration of the \term{evaluation} of the \term{form}
  258. that creates the \term{dynamic binding}.
  259. % %!!! Need to merge this in with previous or delete it.
  260. % %% 5.1.2 4
  261. % A \term{dynamic variable} that is referenced outside the \term{dynamic extent} of
  262. % a \term{form} that \term{binds} the \term{symbol} that names the
  263. % variable is a global variable. A global variable is \term{unbound}
  264. % unless and until explicitly assigned a value, except for global
  265. % variables defined in this specification or by an implementation.
  266. A \term{dynamic variable} can be referenced outside the \term{dynamic extent} of
  267. a \term{form} that \term{binds} it. Such a \term{variable} is sometimes called
  268. a ``global variable'' but is still in all respects just a \term{dynamic variable}
  269. whose \term{binding} happens to exist in the \term{global environment} rather than in some
  270. \term{dynamic environment}.
  271. % slight rewording --sjl 3 Mar 92
  272. A \term{dynamic variable} is \term{unbound}
  273. unless and until explicitly assigned a value, except for
  274. those variables whose initial value is
  275. defined in this specification or by an \term{implementation}.
  276. %% Pretty much said above.
  277. % \term{Bindings} of \term{dynamic variables} are found in the \term{dynamic environment}
  278. % or the \term{global environment}.
  279. \endsubsubsubsubsection%{Dynamic Variables}
  280. \beginsubsubsubsubsection{Constant Variables}
  281. \DefineSection{ConstantVars}
  282. %% 1.2.5 6
  283. %% 5.1.2 6
  284. Certain variables, called \term{constant variables}, are reserved as ``named constants.''
  285. The consequences are undefined if an attempt is made to
  286. assign a value to,
  287. or create
  288. a \term{binding} for a \term{constant variable},
  289. except that a `compatible' redefinition of a \term{constant variable}
  290. using \macref{defconstant} is permitted; \seemac{defconstant}.
  291. \term{Keywords},
  292. \term{symbols} defined by \clisp\ or the \term{implementation}
  293. as constant (such as \nil, \t, and \conref{pi}),
  294. and \term{symbols} declared as constant using \macref{defconstant}
  295. are \term{constant variables}.
  296. \endsubsubsubsubsection%{Constant Variables}
  297. \beginsubsubsubsubsection{Symbols Naming Both Lexical and Dynamic Variables}
  298. %A \term{symbol} can name both a \term{lexical variable} and a \term{dynamic variable}.
  299. %%KAB: The above is a little confusing, as it could be read to mean they
  300. %% are somehow in different namespaces.
  301. %%KMP: Replaced by the following section:
  302. The same \term{symbol} can name both
  303. a \term{lexical variable}
  304. and a \term{dynamic variable},
  305. but never in the same \term{lexical environment}.
  306. In the following example, the \term{symbol} \f{x} is used,
  307. at different times,
  308. as the \term{name} of a \term{lexical variable}
  309. and as the \term{name} of a \term{dynamic variable}.
  310. \code
  311. (let ((x 1)) ;Binds a special variable X
  312. (declare (special x))
  313. (let ((x 2)) ;Binds a lexical variable X
  314. (+ x ;Reads a lexical variable X
  315. (locally (declare (special x))
  316. x)))) ;Reads a special variable X
  317. \EV 3
  318. \endcode
  319. \endsubsubsubsubsection%{Symbols Naming Both Lexical and Dynamic Variables}
  320. \endsubsubsubsection%{Symbols as Forms}
  321. \beginsubsubsubsection{Conses as Forms}
  322. A \term{cons} that is used as a \term{form} is called a \term{compound form}.
  323. If the \term{car} of that \term{compound form} is a \term{symbol},
  324. that \term{symbol} is the \term{name} of an \term{operator},
  325. and the \term{form} is either a \term{special form}, a \term{macro form},
  326. or a \term{function form}, depending on the \term{function} \term{binding}
  327. of the \term{operator} in the current \term{lexical environment}.
  328. If the \term{operator} is neither a \term{special operator}
  329. nor a \term{macro name}, it is assumed to be a \term{function name}
  330. (even if there is no definition for such a \term{function}).
  331. If the \term{car} of the \term{compound form} is not a \term{symbol},
  332. then that \term{car} must be a \term{lambda expression},
  333. in which case the \term{compound form} is a \term{lambda form}.
  334. How a \term{compound form} is processed depends on whether it is
  335. classified as a \term{special form}, a \term{macro form},
  336. a \term{function form}, or a \term{lambda form}.
  337. \beginsubsubsubsubsection{Special Forms}
  338. % special form changed to special operator in a few places --sjl 3 Mar 92
  339. A \term{special form} is a \term{form} with special syntax,
  340. special evaluation rules, or both, possibly manipulating the
  341. evaluation environment, control flow, or both.
  342. % When the operator names a \term{special form},
  343. % the operation is performed using special purpose code
  344. % in the current \term{lexical environment} and \term{dynamic environments}.
  345. %% Simplified for Sandra:
  346. A \term{special operator} has access to
  347. the current \term{lexical environment}
  348. and the current \term{dynamic environment}.
  349. Each \term{special operator} defines the manner in which its \term{subexpressions}
  350. are treated---which are \term{forms}, which are special syntax, \etc.
  351. Some \term{special operators} create new
  352. lexical or dynamic \term{environments} for use during the
  353. \term{evaluation} of \term{subforms}
  354. of the \term{special form}. For example, \specref{block} creates a
  355. new \term{lexical environment} that is the same as the one in force
  356. at the point of evaluation of the \specref{block} \term{form}
  357. with the addition of a \term{binding} of the \specref{block} name
  358. to an \term{exit point} from the \specref{block}.
  359. %Moon observes that this is no different than for any other form:
  360. %
  361. % Generally, \term{special forms} will return normally, \term{yielding}
  362. % one or more values, but some \term{special forms} may cause a non-local exit.
  363. The set of \term{special operator} \term{names} is fixed in \clisp;
  364. no way is provided for the user to define a \term{special operator}.
  365. \Thenextfigure\ lists all of the \clisp\ \term{symbols}
  366. that have definitions as \term{special operators}.
  367. \DefineFigure{CLSpecialOps}
  368. %% 5.1.3 2
  369. %% 7.0.0 5
  370. %% 7.7.0 1
  371. %% 7.8.3 3
  372. %% 7.8.3 5
  373. %% 7.8.5 3
  374. %% 7.10.0 1
  375. \issue{COMPILER-LET-CONFUSION:ELIMINATE}
  376. \issue{WITH-ADDED-METHODS:DELETE}
  377. \issue{LOAD-TIME-EVAL:R**2-NEW-SPECIAL-FORM}
  378. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  379. \displaythree{Common Lisp Special Operators}{
  380. block&let*&return-from\cr
  381. catch&load-time-value&setq\cr
  382. eval-when&locally&symbol-macrolet\cr
  383. flet&macrolet&tagbody\cr
  384. function&multiple-value-call&the\cr
  385. go&multiple-value-prog1&throw\cr
  386. if&progn&unwind-protect\cr
  387. labels&progv&\cr
  388. let&quote&\cr
  389. }
  390. %Removed COMPILER-LET -kmp 3-Jan-91
  391. %Removed WITH-ADDED-METHODS -kmp 7-Jan-91
  392. %Removed DECLARE per Moon's comments. -kmp 10-Mar-91
  393. %Added LOAD-TIME-VALUE -kmp 26-May-91
  394. %Added LOCALLY per Barrett comments. -kmp 13-Oct-91
  395. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  396. \endissue{LOAD-TIME-EVAL:R**2-NEW-SPECIAL-FORM}
  397. \endissue{WITH-ADDED-METHODS:DELETE}
  398. \endissue{COMPILER-LET-CONFUSION:ELIMINATE}
  399. %% 5.1.3 9
  400. \endsubsubsubsubsection%{Special Forms}
  401. \beginsubsubsubsubsection{Macro Forms}
  402. If the \term{operator} names a \term{macro},
  403. its associated \term{macro function} is applied
  404. to the entire \term{form} and the result of that application is
  405. used in place of the original \term{form}.
  406. Specifically, a \term{symbol} names a \term{macro} in a given \term{lexical environment} if
  407. \funref{macro-function} is \term{true} of the
  408. \term{symbol} and that \term{environment}.
  409. The \term{function} returned by \funref{macro-function}
  410. is a \term{function} of two arguments, called the
  411. expansion function.
  412. The expansion function is invoked by calling the \term{macroexpand hook} with
  413. the expansion function as its first argument,
  414. the entire \term{macro form} as its second argument,
  415. % and the current \term{lexical environment}
  416. % %% KAB thinks this is a bogus distinction.
  417. % % (or with the current compilation \term{environment},
  418. % % if the \term{form} is being processed by \funref{compile-file})
  419. %% Trying again... -kmp&kab 8-Feb-92
  420. and an \term{environment object} (corresponding to the current \term{lexical environment})
  421. as its third argument.
  422. The \term{macroexpand hook}, in turn, calls the expansion function with the
  423. \term{form} as its first argument and the \term{environment} as its second argument.
  424. The \term{value} of the expansion function, which is passed through
  425. by the \term{macroexpand hook}, is a \term{form}.
  426. % (\funref{macroexpand}
  427. % and \funref{macroexpand-1} attach a second, \term{generalized boolean} return value
  428. % which is always \term{true} in this context, and hence always ignored.)
  429. The returned \term{form} is \term{evaluated} in place of the original \term{form}.
  430. \issue{SELF-MODIFYING-CODE:FORBID}
  431. The consequences are undefined if a \term{macro function} destructively modifies
  432. any part of its \term{form} argument.
  433. \endissue{SELF-MODIFYING-CODE:FORBID}
  434. %% 8.0.0 6
  435. A \term{macro name} is not a \term{function designator},
  436. and cannot be used as the \param{function} argument to \term{functions}
  437. such as \funref{apply}, \funref{funcall}, or \funref{map}.
  438. An \term{implementation} is free to implement a \clisp\ \term{special operator}
  439. as a \term{macro}. An \term{implementation} is free to implement any
  440. \term{macro} \term{operator} as a \term{special operator}, but only
  441. if an equivalent definition of the \term{macro} is also provided.
  442. % I don't think this adds anything to the above, and it's more confusing.
  443. % So I commented it out. --sjl 3 Mar 92
  444. % %% 7.1.1 23
  445. % A \clisp\ \term{symbol} may name both a \term{special operator}
  446. % and a \term{macro},
  447. % %I added this: -kmp -kmp 12-Mar-91
  448. % but in that case the definitions must be semantically equivalent.
  449. \Thenextfigure\ lists some \term{defined names} that are applicable
  450. to \term{macros}.
  451. \displaythree{Defined names applicable to macros}{
  452. *macroexpand-hook*&macro-function&macroexpand-1\cr
  453. defmacro&macroexpand&macrolet\cr
  454. }
  455. %DEFINE-COMPILER-MACRO and COMPILER-MACRO-FUNCTION removed per Moon. -kmp 10-Mar-91
  456. \endsubsubsubsubsection%{Macro Forms}
  457. \beginsubsubsubsubsection{Function Forms}
  458. \DefineSection{FunctionForms}
  459. If the \term{operator} is a \term{symbol} naming a \term{function},
  460. the \term{form} represents a \term{function form},
  461. and the \term{cdr} of the list contains the \term{forms}
  462. which when evaluated will supply the arguments passed to the \term{function}.
  463. When a \term{function name} is not defined,
  464. an error \oftype{undefined-function} should be signaled at run time;
  465. \seesection\SemanticConstraints.
  466. A \term{function form} is evaluated as follows:
  467. The \term{subforms} in the \term{cdr} of the original \term{form}
  468. are evaluated in left-to-right order in the current lexical and
  469. dynamic \term{environments}. The \term{primary value} of each
  470. such \term{evaluation} becomes an \term{argument} to the named \term{function};
  471. % removed inappropriate reference to Lambda List section. --sjl 3 Mar 92
  472. any additional \term{values} returned by the \term{subforms} are discarded.
  473. The \term{functional value} of the \term{operator}
  474. is retrieved from the \term{lexical environment},
  475. and that \term{function} is invoked with the indicated arguments.
  476. %%How the function treats its arguments, what bindings it establishes, etc.
  477. %% does not belong here. -kmp,moon
  478. % and a new \term{lexical environment} is created that contains only
  479. % the \term{bindings} established by \term{binding} each parameter
  480. % name in the lambda list of the \term{function} with the
  481. % corresponding value in the evaluated arguments along with a binding of
  482. % the name of the \term{function} with the \term{exit point} that
  483. % returns from the \term{function}. This is the lexical
  484. % \term{environment} used for evaluating the body of the \term{function}.
  485. \issue{FUNCTION-CALL-EVALUATION-ORDER:MORE-UNSPECIFIED}
  486. Although the order of \term{evaluation} of
  487. the \term{argument} \term{subforms} themselves is
  488. strictly left-to-right, it is not specified whether
  489. the definition of the \term{operator} in a \term{function form} is looked up
  490. before the \term{evaluation} of the \term{argument} \term{subforms},
  491. after the \term{evaluation} of the \term{argument} \term{subforms},
  492. or between the \term{evaluation} of any two \term{argument} \term{subforms}
  493. if there is more than one such \term{argument} \term{subform}.
  494. For example, the following might return 23 or~24.
  495. \code
  496. (defun foo (x) (+ x 3))
  497. (defun bar () (setf (symbol-function 'foo) #'(lambda (x) (+ x 4))))
  498. (foo (progn (bar) 20))
  499. \endcode
  500. \endissue{FUNCTION-CALL-EVALUATION-ORDER:MORE-UNSPECIFIED}
  501. A \term{binding} for a \term{function name} can be \term{established} in
  502. one of several ways. A \term{binding} for a \term{function name} in
  503. the \term{global environment} can be \term{established} by
  504. \macref{defun},
  505. \SETFof{fdefinition},
  506. % added symbol-function --sjl 3 Mar 92
  507. \SETFof{symbol-function},
  508. %% KAB: Doesn't belong here.
  509. %\macref{defmacro},
  510. %\SETFof{macro-function},
  511. \funref{ensure-generic-function},
  512. \macref{defmethod} (implicitly, due to \funref{ensure-generic-function}),
  513. or
  514. \macref{defgeneric}.
  515. A \term{binding} for a \term{function name} in the \term{lexical environment}
  516. can be \term{established} by
  517. \specref{flet}
  518. or \specref{labels}.
  519. \issue{WITH-ADDED-METHODS:DELETE}
  520. %\specref{with-added-methods},
  521. \endissue{WITH-ADDED-METHODS:DELETE}%
  522. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  523. % \specref{generic-flet},
  524. % or
  525. % \specref{generic-labels}.
  526. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  527. %%Moon thinks this is redundant, so asked that it be deleted. -kmp 11-Mar-91
  528. % If a \term{symbol} that names a \term{function} appears
  529. % as the first element of a function call \term{form}, then it refers
  530. % to the definition established by the innermost
  531. % \specref{flet},
  532. % \specref{labels},
  533. % \issue{WITH-ADDED-METHODS:DELETE}
  534. % %\specref{with-added-methods},
  535. % \endissue{WITH-ADDED-METHODS:DELETE}%
  536. % \specref{generic-flet},
  537. % or
  538. % \specref{generic-labels}
  539. % that textually contains the reference, or to the global definition (if any) if there
  540. % is no such containing \term{form}. As noted earlier, this use
  541. % of \term{symbols} to name \term{functions} is
  542. % independent of their use in naming dynamic and lexical variables.
  543. \Thenextfigure\ lists some \term{defined names} that are applicable to \term{functions}.
  544. \issue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  545. \issue{WITH-ADDED-METHODS:DELETE}
  546. \displaythree{Some function-related defined names}{
  547. apply&fdefinition&mapcan\cr
  548. call-arguments-limit&flet&mapcar\cr
  549. complement&fmakunbound&mapcon\cr
  550. constantly&funcall&mapl\cr
  551. defgeneric&function&maplist\cr
  552. defmethod&functionp&multiple-value-call\cr
  553. defun&labels&reduce\cr
  554. fboundp&map&symbol-function\cr
  555. }
  556. %Removed GENERIC-FLET, GENERIC-LABELS. -kmp 7-Feb-92
  557. %Removed WITH-ADDED-METHODS -kmp 7-Jan-91
  558. %Added MAPC, MAPCAR, MAPCAN, MAPLIST, MAPL, MAPCON,
  559. % CONSTANTLY, COMPLEMENT, REDUCE, FDEFINITION.
  560. %Removed LAMBDA-LIST-KEYWORDS.
  561. %Not including all the things with :TEST, :TEST-NOT, :KEY arguments, though.
  562. % -kmp 16-Oct-91
  563. \endissue{WITH-ADDED-METHODS:DELETE}
  564. \endissue{GENERIC-FLET-POORLY-DESIGNED:DELETE}
  565. \endsubsubsubsubsection%{Function Forms}
  566. \beginsubsubsubsubsection{Lambda Forms}
  567. \DefineSection{LambdaForms}
  568. A \term{lambda form} is similar to a \term{function form}, except that
  569. the \term{function name} is replaced by a \term{lambda expression}.
  570. A \term{lambda form} is equivalent to using \term{funcall} of a
  571. \term{lexical closure} of the \term{lambda expression} on the given \term{arguments}.
  572. (In practice, some compilers are more likely to produce inline code
  573. for a \term{lambda form} than for an arbitrary named function
  574. that has been declared \declref{inline}; however, such a difference
  575. is not semantic.)
  576. For further information, \seesection\LambdaExpressions.
  577. \endsubsubsubsubsection%{Lambda Forms}
  578. \endsubsubsubsection%{Conses as forms}
  579. \beginsubsubsubsection{Self-Evaluating Objects}
  580. \issue{EVAL-OTHER:SELF-EVALUATE}
  581. A \term{form} that is neither a \term{symbol} nor a \term{cons} is
  582. defined to be a \term{self-evaluating object}. \term{Evaluating}
  583. such an \term{object} \term{yields} the \term{same} \term{object}
  584. as a result.
  585. \endissue{EVAL-OTHER:SELF-EVALUATE}
  586. Certain specific \term{symbols} and \term{conses} might also happen
  587. to be ``self-evaluating'' but only as a special case of a more
  588. general set of rules for the \term{evaluation} of \term{symbols} and
  589. \term{conses}; such \term{objects} are not considered to be
  590. \term{self-evaluating objects}.
  591. % added --sjl 3 Mar 92
  592. The consequences are undefined if \term{literal objects} (including
  593. \term{self-evaluating objects}) are destructively modified.
  594. \beginsubsubsubsubsection{Examples of Self-Evaluating Objects}
  595. \term{Numbers}, \term{pathnames}, and \term{arrays} are examples of
  596. \term{self-evaluating objects}.
  597. \code
  598. 3 \EV 3
  599. #c(2/3 5/8) \EV #C(2/3 5/8)
  600. #p"S:[BILL]OTHELLO.TXT" \EV #P"S:[BILL]OTHELLO.TXT"
  601. #(a b c) \EV #(A B C)
  602. "fred smith" \EV "fred smith"
  603. \endcode
  604. \endsubsubsubsubsection%{Examples of Self-Evaluating Objects}
  605. \endsubsubsubsection%{Self-Evaluating Objects}
  606. \endsubsubsection%{Form Evaluation}
  607. \endSubsection%{The Evaluation Model}
  608. \beginsubsection{Lambda Expressions}
  609. \DefineSection{LambdaExpressions}
  610. % I found the last paragraph of this section very confusing, so I
  611. % reworded the first paragraph to make it unnecessary. -- sjl 3 Mar 92
  612. % In a \term{lambda expression},
  613. % the current lexical \term{environment} is extended by adding the \term{binding} of
  614. % each \term{parameter} in the \term{lambda list} of the \term{lambda expression}
  615. % with the corresponding \term{value} from the \term{arguments}.
  616. % This is the \term{lexical environment} used for evaluating the body
  617. % of the \term{lambda expression}.
  618. In a \term{lambda expression},
  619. the body is evaluated in a lexical \term{environment} that is formed by
  620. adding the \term{binding} of
  621. each \term{parameter} in the \term{lambda list}
  622. with the corresponding \term{value} from the \term{arguments}
  623. to the current lexical \term{environment}.
  624. For further discussion of how \term{bindings} are \term{established}
  625. based on the \term{lambda list}, \seesection\LambdaLists.
  626. The body of a \term{lambda expression} is an \term{implicit progn};
  627. the \term{values} it returns are returned by the \term{lambda expression}.
  628. % When the \term{function} returns,
  629. % the \term{lexical environment} that was in effect
  630. % when the original \term{form} was evaluated
  631. % is re-established.
  632. \endsubsection%{Lambda Expressions}
  633. \beginSubsection{Closures and Lexical Binding}
  634. %% 7.1.1 5
  635. A \term{lexical closure} is a \term{function} that can refer to and alter
  636. the values of \term{lexical bindings} \term{established} by \term{binding} \term{forms}
  637. that textually include the function definition.
  638. Consider this code, where \f{x} is not declared \declref{special}:
  639. \code
  640. (defun two-funs (x)
  641. (list (function (lambda () x))
  642. (function (lambda (y) (setq x y)))))
  643. (setq funs (two-funs 6))
  644. (funcall (car funs)) \EV 6
  645. (funcall (cadr funs) 43) \EV 43
  646. (funcall (car funs)) \EV 43
  647. \endcode
  648. \Thespecform{function} coerces a
  649. \term{lambda expression} into a \term{closure} in which the
  650. \term{lexical environment} in effect when the \term{special form} is
  651. evaluated is captured along with the \term{lambda expression}.
  652. The function \f{two-funs} returns a \term{list} of two
  653. \term{functions}, each of which refers to the \term{binding} of the
  654. variable \f{x} created on entry to the function \f{two-funs} when it
  655. was called.
  656. %% For Moon.
  657. %with argument \f{6}.
  658. This variable has the value \f{6}
  659. initially, but \specref{setq} can alter this \term{binding}.
  660. % %Barrett finds the next sentence confusing.
  661. % %Moon, too.
  662. % The \term{lexical closure} created for the first
  663. % \term{lambda expression} does not capture the value \f{6} for \f{x}
  664. % when the \term{closure} is created. The second \term{function} can be
  665. % used to alter the variable binding (to \f{43}, in the example), and
  666. % this altered variable binding then becomes accessible to the first
  667. % \term{function}.
  668. The \term{lexical closure} created for the first
  669. \term{lambda expression} does not ``snapshot'' the \term{value} \f{6} for \f{x}
  670. when the \term{closure} is created; rather it captures the \term{binding} of \f{x}.
  671. The second \term{function} can be used to alter the \term{value} in the same (captured)
  672. \term{binding} (to \f{43}, in the example), and
  673. this altered variable binding then affects the value returned by the first \term{function}.
  674. %% 7.1.1 6
  675. In situations where a \term{closure} of a
  676. \term{lambda expression} over the same set of \term{bindings} may be
  677. produced more than once, the various resulting \term{closures} may
  678. or may not be \term{identical}, at the discretion of the \term{implementation}.
  679. That is, two \term{functions} that are behaviorally
  680. indistinguishable might or might not be \term{identical}.
  681. Two \term{functions} that are behaviorally distinguishable are \term{distinct}.
  682. For example:
  683. \code
  684. (let ((x 5) (funs '()))
  685. (dotimes (j 10)
  686. (push #'(lambda (z)
  687. (if (null z) (setq x 0) (+ x z)))
  688. funs))
  689. funs)
  690. \endcode
  691. The result of the above \term{form} is a \term{list} of ten \term{closures}.
  692. Each requires only the \term{binding} of \f{x}.
  693. It is the same \term{binding} in each case,
  694. but the ten \term{closure} \term{objects} might or might not be \term{identical}.
  695. On the other hand, the result of the \term{form}
  696. \code
  697. (let ((funs '()))
  698. (dotimes (j 10)
  699. (let ((x 5))
  700. (push (function (lambda (z)
  701. (if (null z) (setq x 0) (+ x z))))
  702. funs)))
  703. funs)
  704. \endcode
  705. is also a \term{list} of ten \term{closures}.
  706. However, in this case no two of the \term{closure} \term{objects} can
  707. be \term{identical} because each \term{closure} is closed over a distinct
  708. \term{binding} of \f{x}, and these \term{bindings} can be behaviorally
  709. distinguished because of the use of \specref{setq}.
  710. %% 7.1.1 7
  711. The result of the \term{form}
  712. \code
  713. (let ((funs '()))
  714. (dotimes (j 10)
  715. (let ((x 5))
  716. (push (function (lambda (z) (+ x z)))
  717. funs)))
  718. funs)
  719. \endcode
  720. is a \term{list} of ten \term{closure} \term{objects} that
  721. might or might not be \term{identical}.
  722. A different \term{binding} of \f{x} is involved for
  723. each \term{closure}, but the \term{bindings} cannot be distinguished
  724. because their values are the \term{same} and immutable (there being no occurrence
  725. of \specref{setq} on \f{x}). A compiler could internally
  726. transform the \term{form} to
  727. \code
  728. (let ((funs '()))
  729. (dotimes (j 10)
  730. (push (function (lambda (z) (+ 5 z)))
  731. funs))
  732. funs)
  733. \endcode
  734. where the \term{closures} may be \term{identical}.
  735. %% Moon thought this was redundant and distracting. -kmp 8-Feb-92
  736. % In some \term{implementations},
  737. % two distinct evaluations of the same \specref{function} \term{form} might
  738. % produce \term{identical} \term{closures} if they must be behaviorally
  739. % identical with respect to invocation.
  740. %% 7.1.1 9
  741. It is possible that a \term{closure} does not
  742. close over any variable bindings.
  743. In the code fragment
  744. \code
  745. (mapcar (function (lambda (x) (+ x 2))) y)
  746. \endcode
  747. the function \f{(lambda (x) (+ x 2))} contains no references to any outside
  748. object. In this case, the same \term{closure} might be returned
  749. for all evaluations of the \specref{function} \term{form}.
  750. \endSubsection%{Closures and Lexical Binding}
  751. \beginSubsection{Shadowing}
  752. \DefineSection{Shadowing}
  753. %% 5.1.3 10
  754. %% 3.0.0 13
  755. %
  756. % If two \term{forms} that establish lexical entities with the same
  757. % \term{name} are textually nested, then references within the inner
  758. % \term{form} refer to the entity established by the inner one; the
  759. % inner one shadows the outer one. Outside the inner \term{form} but
  760. % inside the outer one, references refer to the entity established by
  761. % the outer \term{form}. For example:
  762. %
  763. %% Rewritten for Sandra:
  764. If two \term{forms} that \term{establish} \term{lexical bindings} with
  765. the same \term{name} $N$ are textually nested, then references to $N$
  766. within the inner \term{form} refer to the \term{binding} established by
  767. the inner \term{form}; the inner \term{binding} for $N$
  768. \newtermidx{shadows}{shadow} the outer \term{binding} for $N$. Outside the inner
  769. \term{form} but inside the outer one, references to $N$ refer to the
  770. \term{binding} established by the outer \term{form}. For example:
  771. %!!! Barrett thinks this doesn't explain, and that we should discuss,
  772. % (let ((x ...)) (let ((x ...)) (declare (special x)) ...))
  773. \code
  774. (defun test (x z)
  775. (let ((z (* x 2)))
  776. (print z))
  777. z)
  778. \endcode
  779. The \term{binding} of the variable \f{z} by
  780. \specref{let} shadows
  781. the \term{parameter} binding for the function \f{test}. The reference to the
  782. variable \f{z} in the \funref{print} \term{form} refers to the \specref{let} binding.
  783. The reference to \f{z} at the end of the function \f{test}
  784. refers to the \term{parameter} named \f{z}.
  785. %!!! What to do with this use of "construct"? -kmp 3-Sep-91
  786. %% 3.0.0 26
  787. Constructs that are lexically scoped act as if new names were
  788. generated for each \term{object} on each execution. Therefore,
  789. dynamic shadowing cannot occur. For example:
  790. \code
  791. (defun contorted-example (f g x)
  792. (if (= x 0)
  793. (funcall f)
  794. (block here
  795. (+ 5 (contorted-example g
  796. #'(lambda () (return-from here 4))
  797. (- x 1))))))
  798. \endcode
  799. Consider the call \f{(contorted-example nil nil 2)}. This produces
  800. \f{4}. During the course of execution, there are three
  801. calls to \f{contorted-example}, interleaved with two
  802. blocks:
  803. \code
  804. (contorted-example nil nil 2)
  805. (block here\ssso ...)
  806. (contorted-example nil #'(lambda () (return-from here\ssso 4)) 1)
  807. (block here\ssst ...)
  808. (contorted-example #'(lambda () (return-from here\ssso 4))
  809. #'(lambda () (return-from here\ssst 4))
  810. 0)
  811. (funcall f)
  812. where f \EV #'(lambda () (return-from here\ssso 4))
  813. (return-from here\ssso 4)
  814. \endcode
  815. At the time the \f{funcall} is executed
  816. there are two \specref{block} \term{exit points} outstanding, each apparently
  817. named \f{here}.
  818. The \specref{return-from} \term{form} executed as a result of the \f{funcall}
  819. operation
  820. refers to the outer outstanding \term{exit point}
  821. (here\ssso), not the
  822. inner one (here\ssst).
  823. It
  824. refers to that \term{exit point} textually visible at the point of
  825. execution of \specref{function}
  826. (here abbreviated by the \f{\#'} syntax) that resulted
  827. in creation of the \term{function} \term{object} actually invoked by
  828. \funref{funcall}.
  829. %% 3.0.0 27
  830. If, in this example, one were to change the \f{(funcall f)} to
  831. \f{(funcall g)}, then the value of the call \f{(contorted-example nil nil 2)}
  832. would be \f{9}. The value would change because
  833. \funref{funcall} would cause the
  834. execution of \f{(return-from here\ssst\ 4)}, thereby causing
  835. a return from the inner \term{exit point} (here\ssst).
  836. When that occurs, the value \f{4} is returned from the
  837. middle invocation of \f{contorted-example}, \f{5} is added to that
  838. to get \f{9}, and that value is returned from the outer block
  839. and the outermost call to \f{contorted-example}. The point
  840. is that the choice of \term{exit point}
  841. returned from has nothing to do with its
  842. being innermost or outermost; rather,
  843. it depends on the lexical environment
  844. that is packaged up with a \term{lambda expression} when
  845. \specref{function} is executed.
  846. \endSubsection%{Shadowing}
  847. \beginSubsection{Extent}
  848. %% 3.0.0 28
  849. \f{Contorted-example} works only because the
  850. \term{function} named by \f{f} is invoked during the \term{extent} of the
  851. \term{exit point}.
  852. Once the flow of execution has left the block,
  853. the \term{exit point} is \term{disestablished}. For example:
  854. \code
  855. (defun invalid-example ()
  856. (let ((y (block here #'(lambda (z) (return-from here z)))))
  857. (if (numberp y) y (funcall y 5))))
  858. \endcode
  859. One might expect the call \f{(invalid-example)} to produce \f{5}
  860. by the following incorrect reasoning:
  861. \specref{let} binds \f{y} to the
  862. value of \specref{block}; this value is a \term{function} resulting
  863. from the \term{lambda expression}. Because \f{y} is not a number, it is
  864. invoked on the value \f{5}. The \specref{return-from} should then
  865. return this value from the
  866. \term{exit point} named \f{here}, thereby
  867. exiting from the block again and giving \f{y} the value \f{5}
  868. which, being a number, is then returned as the value of the call
  869. to \f{invalid-example}.
  870. %% 3.0.0 29
  871. The argument fails only because \term{exit points} have
  872. \term{dynamic extent}. The argument is correct up to the execution of
  873. \specref{return-from}. The execution of \specref{return-from}
  874. should signal an error \oftype{control-error}, however, not
  875. because it cannot refer to the \term{exit point}, but because it
  876. does correctly refer to an \term{exit point} and that
  877. \term{exit point} has been \term{disestablished}.
  878. %% 3.0.0 14
  879. %% 3.0.0 16
  880. A reference by name to a dynamic \term{exit point} binding such as
  881. a \term{catch tag} refers to the most recently
  882. \term{established} \term{binding} of that name that has not been
  883. \term{disestablished}. For example:
  884. \code
  885. (defun fun1 (x)
  886. (catch 'trap (+ 3 (fun2 x))))
  887. (defun fun2 (y)
  888. (catch 'trap (* 5 (fun3 y))))
  889. (defun fun3 (z)
  890. (throw 'trap z))
  891. \endcode
  892. Consider the call \f{(fun1 7)}. The result is \f{10}. At the time
  893. the \specref{throw} is executed, there are two outstanding catchers with the
  894. name \f{trap}: one established within procedure \f{fun1}, and the other
  895. within procedure \f{fun2}. The latter is the more recent, and so the
  896. value \f{7} is returned from \specref{catch} in \f{fun2}.
  897. Viewed from within \f{fun3}, the \specref{catch}
  898. in \f{fun2} shadows the one in \f{fun1}.
  899. Had \f{fun2} been defined as
  900. \code
  901. (defun fun2 (y)
  902. (catch 'snare (* 5 (fun3 y))))
  903. \endcode
  904. then the two \term{exit points}
  905. would have different \term{names}, and therefore the one
  906. in \f{fun1} would not be shadowed. The result would then have been \f{7}.
  907. \endSubsection%{Extent}
  908. \beginSubsection{Return Values}
  909. Ordinarily the result of calling a \term{function} is a single \term{object}.
  910. Sometimes, however, it is convenient for a function to compute several
  911. \term{objects} and return them.
  912. % slight rewording -- sjl 3 Mar 92
  913. In order to receive other than exactly one value from a \term{form},
  914. one of several \term{special forms} or \term{macros} must be used to request those
  915. values. If a \term{form} produces \term{multiple values} which were not
  916. requested in this way, then the first value is given to the caller and
  917. all others are discarded; if the \term{form} produces zero values,
  918. then the caller receives \nil\ as a value.
  919. \Thenextfigure\ lists
  920. some \term{operators} for receiving \term{multiple values}\meaning{2}.
  921. These \term{operators} can be used to specify
  922. one or more \term{forms} to \term{evaluate}
  923. and where to put the \term{values} returned by those \term{forms}.
  924. \displaythree{Some operators applicable to receiving multiple values}{
  925. multiple-value-bind&multiple-value-prog1&return-from\cr
  926. multiple-value-call&multiple-value-setq&throw\cr
  927. multiple-value-list&return&\cr
  928. }
  929. \Thefunction{values} can produce \term{multiple values}\meaning{2}.
  930. \f{(values)} returns zero values;
  931. \f{(values \param{form})} returns the \term{primary value} returned by \param{form};
  932. \f{(values \param{form1} \param{form2})} returns two values,
  933. the \term{primary value} of \param{form1}
  934. and the \term{primary value} of \param{form2};
  935. and so on.
  936. See \conref{multiple-values-limit} and \funref{values-list}.
  937. %% This little piece of language analysis isn't useful to the spec.
  938. %% -kmp&kab 13-Oct-91
  939. %
  940. % \term{Multiple values} can result from a \term{form} under precisely these
  941. % circumstances:
  942. % \beginlist
  943. % \itemitem{\b{Evaluation and Application}}
  944. %
  945. % \beginlist
  946. % \itemitem{\funref{eval}}
  947. %
  948. % \funref{eval} returns \term{multiple values} if the form given it to
  949. % evaluate produces \term{multiple values}.
  950. %
  951. % \itemitem{\funref{apply}, \funref{funcall}, \specref{multiple-value-call}}
  952. %
  953. % \funref{apply}, \funref{funcall}, and \specref{multiple-value-call}
  954. % can return \term{multiple values} from the function applied or called.
  955. % \endlist
  956. %
  957. % \itemitem{\b{Sequential Forms}}
  958. %
  959. % \beginlist
  960. % \itemitem{\specref{progn}}
  961. %
  962. % The special form \specref{progn} can return \term{multiple values} resulting
  963. % from evaluation of the last \term{subform}.
  964. %
  965. % \itemitem{\term{implicit progn}}
  966. %
  967. % Other situations are referred to as an \term{implicit progn}, in
  968. % which several forms are evaluated and the results of all but the last
  969. % form are discarded. An \term{implicit progn} returns multiple
  970. % values if the last form does. These situations include the body of a
  971. % \term{lambda expression}, including those constructed implicitly by
  972. % \macref{defmethod}, \macref{defun}, \macref{defmacro},
  973. % \issue{DEFINE-COMPILER-MACRO:X3J13-NOV89}
  974. % \macref{define-compiler-macro},
  975. % \endissue{DEFINE-COMPILER-MACRO:X3J13-NOV89}
  976. % \specref{flet},
  977. % \specref{macrolet}, \specref{generic-flet}, \specref{labels},
  978. % \specref{generic-labels}, \macref{deftype}. Also included are
  979. % bodies of the \specref{eval-when}, \specref{progv},
  980. % \specref{let}, \specref{let*}, \macref{when},
  981. % \macref{unless}, \specref{block},
  982. % \macref{multiple-value-bind}, and \specref{catch} \term{forms}, as well as
  983. % clauses in such conditional \term{forms} as \macref{case},
  984. % \macref{typecase}, \macref{ecase}, \macref{etypecase},
  985. % \macref{ccase}, and \macref{ctypecase}. If a body has no
  986. % \term{forms}, a single \nil\ is returned.
  987. % \endlist
  988. %
  989. % \itemitem{\b{Conditional Forms}}
  990. %
  991. % \beginlist
  992. % \itemitem{\specref{if}}
  993. %
  994. % \specref{if} can return \term{multiple values} from whichever \term{subform} is selected
  995. % (the \i{then} form or the \i{else} form).
  996. %
  997. % \itemitem{\macref{and}, \macref{or}}
  998. %
  999. % \macref{and} and \macref{or} can return \term{multiple values} from the last \term{subform}
  1000. % but not from \term{subforms} other than the last.
  1001. %
  1002. % \itemitem{\macref{cond}}
  1003. %
  1004. % \macref{cond} can return \term{multiple values} from the last \term{subform} of
  1005. % the \term{implicit progn} of the selected clause.
  1006. % If, however, the clause selected is a singleton clause,
  1007. % then only a single value (the \term{true} value returned by the predicate)
  1008. % is returned. This is true even if the singleton clause is
  1009. % the last clause of the \macref{cond}. A final clause
  1010. % {\tt (\i{form})} is not equivalent to {\tt (t \i{form})}
  1011. % because the latter returns \term{multiple values} from \i{form}
  1012. % while the former does not; a final clause of {\tt (\i{form})}
  1013. % is instead equivalent to {\tt (t (values \i{forms}))}.
  1014. % \endlist
  1015. %
  1016. % \itemitem{\b{Returning from a Block}}
  1017. %
  1018. % \beginlist
  1019. % \itemitem{\specref{block}}
  1020. %
  1021. % The \specref{block} special form can return \term{multiple values}
  1022. % from its last \term{subform} when it exits normally.
  1023. % If \specref{return-from} (or \macref{return}) is used to terminate
  1024. % the \term{block} prematurely, then \specref{return-from} can
  1025. % return \term{multiple values}
  1026. % %from its \term{subform}
  1027. % as the \term{values} of the terminated \term{block}.
  1028. % Other \term{operators} that implicitly create \term{blocks},
  1029. % such as \macref{do}, \macref{dolist}, \macref{dotimes},
  1030. % \macref{prog}, and \macref{prog*},
  1031. % also can return \term{multiple values} specified by
  1032. % \specref{return-from} (or \macref{return}).
  1033. %
  1034. % \itemitem{\macref{do}}
  1035. %
  1036. % \macref{do} can return \term{multiple values} from the last form of the
  1037. % exit clause, exactly as if the exit clause were a \macref{cond}
  1038. % clause. Similarly, \macref{dolist} and \macref{dotimes} can
  1039. % return \term{multiple values} from the \param{result-form} if that is
  1040. % executed. \macref{loop} can return \term{multiple values} by using
  1041. % \macref{return} and \specref{return-from}. These situations are
  1042. % all examples of implicit uses of \specref{return-from}.
  1043. %
  1044. % \endlist
  1045. %
  1046. % \itemitem{\b{Throwing out of a Catch}}
  1047. %
  1048. % \beginlist
  1049. %
  1050. % \itemitem{\specref{catch}}
  1051. %
  1052. % The \specref{catch} \term{form} returns \term{multiple values} if
  1053. % the result form in a \specref{throw} exiting from
  1054. % such a catch produces \term{multiple values}.
  1055. % \endlist
  1056. %
  1057. % \itemitem{\b{Miscellaneous Situations}}
  1058. %
  1059. % \beginlist
  1060. %
  1061. % \itemitem{\macref{multiple-value-prog1}}
  1062. %
  1063. % \specref{multiple-value-prog1} can return \term{multiple values} from its first
  1064. % \term{subform}. \macref{prog1} always returns a single value.
  1065. %
  1066. % \itemitem{\specref{unwind-protect}}
  1067. %
  1068. % \specref{unwind-protect} returns \term{multiple values} if the form it protects
  1069. % returns \term{multiple values}.
  1070. %
  1071. % \itemitem{\specref{the}}
  1072. %
  1073. % \specref{the} returns \term{multiple values} if the form it contains returns
  1074. % \term{multiple values}.
  1075. % \endlist
  1076. % \endlist
  1077. %
  1078. % Among special forms that never return \term{multiple values} are
  1079. % \specref{setq}, \macref{multiple-value-setq}, \macref{prog1}, and
  1080. % \macref{prog2}.
  1081. %
  1082. % In an ordinary function call,
  1083. % each argument form produces exactly one argument; if such a form
  1084. % returns zero values, \nil\ is used for the argument, and if more than one
  1085. % value, all but the first are discarded.
  1086. % Similarly, conditional \term{forms} (such as \specref{if} \term{forms})
  1087. % that test the value of a form
  1088. % will use exactly one value, the first one, from that form and discard the rest;
  1089. % such \term{forms} will use \nil\ as the test value if zero values are returned.
  1090. %
  1091. \endSubsection%{Return Values}