Commit | Line | Data |
---|---|---|
fcb6c0fb MW |
1 | %%% -*-latex-*- |
2 | %%% | |
3 | %%% Miscellaneous functionality | |
4 | %%% | |
5 | %%% (c) 2015 Straylight/Edgeware | |
6 | %%% | |
7 | ||
8 | %%%----- Licensing notice --------------------------------------------------- | |
9 | %%% | |
e0808c47 | 10 | %%% This file is part of the Sensible Object Design, an object system for C. |
fcb6c0fb MW |
11 | %%% |
12 | %%% SOD is free software; you can redistribute it and/or modify | |
13 | %%% it under the terms of the GNU General Public License as published by | |
14 | %%% the Free Software Foundation; either version 2 of the License, or | |
15 | %%% (at your option) any later version. | |
16 | %%% | |
17 | %%% SOD is distributed in the hope that it will be useful, | |
18 | %%% but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | %%% GNU General Public License for more details. | |
21 | %%% | |
22 | %%% You should have received a copy of the GNU General Public License | |
23 | %%% along with SOD; if not, write to the Free Software Foundation, | |
24 | %%% Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
25 | ||
26 | \chapter{Miscellaneous functionality} \label{ch:misc} | |
27 | ||
28 | %%%-------------------------------------------------------------------------- | |
29 | \section{Utilities} \label{sec:misc.utilities} | |
30 | ||
31 | These symbols are defined in the @|sod-utilities| package. | |
32 | ||
c1ac1524 MW |
33 | |
34 | \subsection{Macro utilities} | |
35 | ||
d9db9c73 MW |
36 | We begin with some simple utilities which help with writing macros. Several |
37 | of these are standard. | |
38 | ||
fcb6c0fb | 39 | \begin{describe}{mac} |
020b9e2b MW |
40 | {with-gensyms (@{ @<var> @! (@<var> @[@<name>@]) @}^*) \\ \ind |
41 | @<declaration>^* \\ | |
b9d79c4c MW |
42 | @<form>^* |
43 | \-\nlret @<value>^*} | |
d9db9c73 MW |
44 | Bind each @<var> (a symbol, not evaluated) to a freshly made gensym whose |
45 | name is based on the corresponding @<name> (a string, evaluated), and | |
46 | evaluate the @<form>s as an implicit @|progn| in the resulting environment. | |
47 | If @<name> is omitted, then the name of the @<var> is used as a default; a | |
48 | bare symbol may be written in place of a singleton list. | |
fcb6c0fb MW |
49 | \end{describe} |
50 | ||
51 | \begin{describe}{mac} | |
52 | {once-only (@[[ :environment @<env> @]] | |
020b9e2b MW |
53 | @{ @<var> @! (@<var> @[@<value-form>@]) @}^*) \\ \ind |
54 | @<declaration>^* \\ | |
d9db9c73 | 55 | @<form>^* |
b9d79c4c | 56 | \-\nlret @<result-form>} |
d9db9c73 MW |
57 | This is a helper to ensure that macro expansions evaluate their arguments |
58 | exactly once each, in the correct order. | |
59 | ||
60 | Each @<var> is bound to an appropriate value (often a gensym) and then the | |
61 | @<form>s are evaluated as an implicit @|progn| in the resulting environment | |
62 | to produce an output form. This output form is then enclosed in one or | |
63 | more binding forms to produce a @<result-form>. When the @<result-form> is | |
64 | evaluated, the behaviour will be as if each @<value-form> is evaluated | |
65 | exactly once each, in order, and each value is captured in the | |
66 | corresponding @<var>. | |
67 | ||
68 | A simple @|once-only| expansion might look something like | |
69 | \begin{prog} | |
70 | (let (\=(@<var>_1 (gensym)) \\ | |
71 | \>\qquad\vdots \\ | |
72 | \>(@<var>_n (gensym))) \\ \ind | |
73 | `(let (\=(,@<var>_1 ,@<value-form>_1) \\ | |
74 | \>\qquad\vdots \\ | |
75 | \>(,@<var>_n ,@<value-form>_n)) \\ \ind | |
76 | @<declaration>_1 \dots\ @<declaration>_m \\ | |
77 | @<form>_1 \dots\ @<form>_\ell)) | |
78 | \end{prog} | |
79 | However, if @|once-only| can determine that some @<value-form> is a | |
80 | constant (e.g., it is @|quote|d, self-evaluating, or reported as | |
81 | @|constantp| in the given environment @<env>), then it need not allocate a | |
82 | gensym: it can instead bind the @<var> directly to the constant value. | |
83 | ||
84 | If a @<value-form> is omitted, then the value of the corresponding @<var> | |
85 | is used. It is conventional usage for a macro to wrap @|once-only| around | |
86 | its body so as to convert the arguments which it should evaluate into safe | |
87 | gensyms capturing their runtime values. (Not that the simple expansion | |
88 | given above can't do this correctly.) A bare symbol may be written in | |
89 | place of a singleton list. | |
fcb6c0fb MW |
90 | \end{describe} |
91 | ||
3177ccb9 | 92 | \begin{describe}{fun} |
cac85e0b MW |
93 | {parse-body @<body> \&key :docp :declp |
94 | @> @<doc-string> @<declarations> @<body-forms>} | |
d9db9c73 MW |
95 | Parse the @<body> into a @<doc-string>, some @<declaration>s, and a list of |
96 | @<body-forms>. | |
97 | ||
98 | The @<body> is assumed to have the general syntax | |
99 | \begin{prog} | |
100 | @[[ @<doc-string> @! @<declaration>^* @]] \\ | |
101 | @<form>^* | |
102 | \end{prog} | |
103 | A @<doc-string> is permitted if and only if @<docp> is non-nil, and | |
104 | declarations are permitted if and only if @<declp> is non-nil; both are | |
105 | true by default. | |
106 | ||
107 | Each return value is a list, which is empty if the corresponding part of | |
108 | the input @<body> is missing. Specifically: | |
109 | \begin{itemize} | |
110 | \item @<doc-string> is either nil, or a singleton list containing a string; | |
111 | \item @<declarations> is either nil, or a singleton list containing a | |
112 | @|(declare \dots)| form gathering up all of the individual | |
113 | @<declaration>s within the @<body>; and | |
114 | \item @<body-forms> is a list of the remaining forms in the @<body>. | |
115 | \end{itemize} | |
116 | Thus, the parsed body-parts can conveniently be spliced into a macro | |
117 | expansion using @|,@@|. | |
fcb6c0fb MW |
118 | \end{describe} |
119 | ||
c1ac1524 | 120 | \begin{describe}{fun}{symbolicate \&rest @<symbols> @> @<symbol>} |
d9db9c73 MW |
121 | Return the symbol, interned in the current @|*package*|, whose name is the |
122 | concatenation of the names of the given @<symbols>. | |
c1ac1524 MW |
123 | \end{describe} |
124 | ||
125 | ||
126 | \subsection{Locatives} | |
127 | ||
d9db9c73 MW |
128 | A \emph{locative} is a value which remembers where another value is stored, |
129 | -- whether it's in a variable, an array element, a structure slot, a hash | |
130 | table, etc.\ -- and can modify and retrieve it. | |
131 | ||
132 | Some Lisp systems have highly efficient locatives which actually keep track | |
133 | of the machine addresses of the places to which they refer. Common Lisp does | |
134 | not implement true locatives of this kind, but something sufficiently useful | |
135 | can be synthesized. | |
136 | ||
137 | These locatives can't usefully be compared. It should be possible to compare | |
138 | true locatives, such that two locatives compare equal if and only if they | |
139 | refer to the same place; but that doesn't work for these locatives. | |
140 | ||
6f7ac504 | 141 | \begin{describe}{cls}{loc} |
d9db9c73 | 142 | The type of locative objects. |
6f7ac504 MW |
143 | \end{describe} |
144 | ||
7262683b | 145 | \begin{describe}{fun}{locp @<object> @> @<generalized-boolean>} |
d9db9c73 | 146 | Return non-nil if and only if @<object> is a locative. |
7262683b MW |
147 | \end{describe} |
148 | ||
cac85e0b | 149 | \begin{describe}{mac}{locf @<place> @> @<locative>} |
d9db9c73 MW |
150 | Return a fresh locative capturing the @<place>, which may be any expression |
151 | usable as the first operand to @|setf|. | |
7262683b MW |
152 | \end{describe} |
153 | ||
154 | \begin{describe*} | |
155 | {\dhead{fun}{ref @<locative> @> @<value>} | |
156 | \dhead{fun}{setf (ref @<locative>) @<value>}} | |
d9db9c73 MW |
157 | Retrieve and return the current value stored in the place captured by the |
158 | @<locative>. With @|setf|, store the new @<value> in the place captured by | |
159 | the @<locative>. | |
7262683b MW |
160 | \end{describe*} |
161 | ||
162 | \begin{describe}{mac} | |
163 | {with-locatives | |
164 | @{ @<var> @! (@{ @<var> @! | |
020b9e2b MW |
165 | (@<var> @[@<locative>@]) @}^*) @} \\ \ind |
166 | @<declaration>^* \\ | |
b9d79c4c MW |
167 | @<form>^* |
168 | \-\nlret @<values>^*} | |
d9db9c73 MW |
169 | This is a macro which hides the use of locatives from its caller using |
170 | symbol-macros. | |
171 | ||
172 | Each @<locative> should be an expression which evaluates to a locative | |
173 | value (not a general place). These are evaluated once each, left to | |
174 | right. The @<form>s are then evaluated as an implicit @|progn|, with each | |
175 | @<var> defined as a symbol macro which will retrieve -- or, with @|setf|, | |
176 | modify -- the value referred to by the corresponding locative. | |
177 | ||
178 | If a @<locative> is omitted, it defaults to the value of @<var>; a | |
179 | bare symbol may be used in place of a singleton list. | |
7262683b MW |
180 | \end{describe} |
181 | ||
c1ac1524 MW |
182 | |
183 | \subsection{Anaphorics} | |
184 | ||
d9db9c73 MW |
185 | An anaphoric macro implicitly binds a well-known name to a value of interest, |
186 | in the course of doing something else. The concept was popularized by Paul | |
72456c11 | 187 | Graham \cite{FIXME:OnLisp}. |
d9db9c73 MW |
188 | |
189 | The macros described here all bind the variable @|it|. | |
190 | ||
fcb6c0fb | 191 | \begin{describe}{sym}{it} |
d9db9c73 | 192 | The symbol @|it| is exported by the @|sod-utilities| package. |
fcb6c0fb MW |
193 | \end{describe} |
194 | ||
b9d79c4c | 195 | \begin{describe}{mac}{aif @<condition> @<consequent> @[@<alt>@] @> @<value>^*} |
d9db9c73 MW |
196 | Evaluate the @<condition>. If @<condition> is non-nil, then bind @|it| to |
197 | the resulting value and evaluate the @<consequent>, returning all of its | |
198 | values. Otherwise, evaluate @<alt>, returning all of its values. | |
fcb6c0fb MW |
199 | \end{describe} |
200 | ||
b9d79c4c | 201 | \begin{describe}{mac}{aand @<form>^* @> @<value>^*} |
d9db9c73 MW |
202 | Evaluate each @<form> in turn. If any @<form> evaluates to nil, then stop |
203 | and return nil. Each form except the first is evaluated with @|it| bound | |
204 | to the (necessarily non-nil) value of the previous form. If all but the | |
205 | last form evaluate non-nil, then return all the values of the final form. | |
3e166443 MW |
206 | \end{describe} |
207 | ||
d9db9c73 MW |
208 | (No @|aor| is provided, since @|it| would necessarily be bound to nil.) |
209 | ||
b9d79c4c | 210 | \begin{describe}{mac}{awhen @<condition> @<form>^* @> nil} |
d9db9c73 MW |
211 | If @<condition> evaluates to a non-nil value, bind @|it| to that value, and |
212 | evaluate the @<form>s as an implicit @|progn|. Otherwise, return nil. | |
fcb6c0fb MW |
213 | \end{describe} |
214 | ||
b9d79c4c | 215 | \begin{describe}{mac}{acond @{ (@<condition> @<form>^*) @}^* @> @<value>^*} |
d9db9c73 MW |
216 | Evaluate each @<condition> in turn, until one of them produces a non-nil |
217 | value. If the @<condition> is followed by one or more @<form>s, then bind | |
218 | @|it| to the non-nil value of the @<condition> and evaluate the @<form>s as | |
219 | an implicit @|progn|; otherwise, simply return the value of the | |
220 | @<condition>. If no @<condition> produces a non-nil value then return nil. | |
fcb6c0fb MW |
221 | \end{describe} |
222 | ||
223 | \begin{describe*} | |
b9d79c4c MW |
224 | {\dhead{mac}{acase @<scrutinee> |
225 | @{ (@{ @<case> @! (@<case>^*) @} @<form>^*) @}^* | |
226 | @> @<value>^*} | |
227 | \dhead{mac}{aecase @<scrutinee> | |
228 | @{ (@{ @<case> @! (@<case>^*) @} @<form>^*) @}^* | |
229 | @> @<value>^*} | |
230 | \dhead{mac}{atypecase @<scrutinee> @{ (@<type> @<form>^*) @}^* | |
231 | @> @<value>^*} | |
232 | \dhead{mac}{aetypecase @<scrutinee> @{ (@<type> @<form>^*) @}^* | |
233 | @> @<value>^*}} | |
d9db9c73 MW |
234 | These are like the Common Lisp macros @|case|, @|ecase|, @|typecase|, and |
235 | @|etypecase|, except that @|it| is bound to the value of the @<scrutinee> | |
236 | while evaluating the matching @<form>s. | |
fcb6c0fb MW |
237 | \end{describe*} |
238 | ||
b9d79c4c | 239 | \begin{describe}{mac}{asetf @{ @<place> @<value> @}^* @> @<value>^*} |
d9db9c73 MW |
240 | For each @<place> and @<value> in turn: bind @|it| to the current value of |
241 | the @<place>, evaluate the @<value> expression, and store the resulting | |
b9d79c4c MW |
242 | value back in the @<place>. Return the @<value>(s) stored by the final |
243 | pair: there may be more than one value, e.g., if @<place> is a @|values| | |
244 | form. | |
d9db9c73 MW |
245 | |
246 | For example, @|(asetf @<place> (1+ it))| is almost equivalent to @|(incf | |
247 | @<place>)|, even if evaluating @<place> has side-effects. | |
fcb6c0fb MW |
248 | \end{describe} |
249 | ||
c1ac1524 MW |
250 | |
251 | \subsection{Metaobject protocol utilities} | |
252 | ||
d9db9c73 MW |
253 | The following utilities make use of the introspection features of the CLOS |
254 | metaobject protocol. | |
255 | ||
b9d79c4c | 256 | \begin{describe}{gf}{instance-initargs @<instance> @> @<initargs-list>} |
d9db9c73 MW |
257 | Return a fresh list of plausible initargs for the given @<instance>. |
258 | ||
259 | This is done by digging through the instance's class's slot definitions and | |
260 | enquiring about their initargs. Initargs which are handled by methods on | |
261 | @|shared-initialize| or similar generic functions won't be discovered. | |
fcb6c0fb MW |
262 | \end{describe} |
263 | ||
264 | \begin{describe*} | |
d9db9c73 MW |
265 | {\dhead{fun}{copy-instance @<instance> \&rest @<initargs> |
266 | @> @<new-instance>} | |
fcb6c0fb | 267 | \dhead{gf}{copy-instance-using-class @<class> @<instance> |
d9db9c73 MW |
268 | \&rest @<initargs> |
269 | @> @<new-instance>}} | |
270 | The @|copy-instance| function creates and returns a fresh copy of a given | |
271 | @<instance>, possibly modifying it according to the given @<initargs>. | |
272 | ||
273 | It immediately calls @|copy-instance-using-class|, calling it with the | |
274 | instance's class and the instance itself, and simply returns the result of | |
275 | that generic function. | |
276 | ||
277 | The default method on @|copy-instance-using-class| should work for most | |
278 | classes, but may be overridden to cope with special effects. It works as | |
279 | follows. | |
280 | \begin{enumerate} | |
281 | \item Allocate a fresh instance of @<class>, using @|allocate-instance|. | |
282 | \item For each slot defined by @<class>, if that slot is bound in the | |
283 | original instance, then set the corresponding slot in the new instance to | |
284 | the same value. | |
285 | \item Call @|shared-initialize| on the new instance, providing it the given | |
286 | list of @<initargs>, but inhibiting the usual initialization of slots | |
287 | from their initforms. | |
288 | \item Return the new instance. | |
289 | \end{enumerate} | |
fcb6c0fb MW |
290 | \end{describe*} |
291 | ||
292 | \begin{describe*} | |
293 | {\dhead{gf}{generic-function-methods @<generic-function> @> @<list>} | |
294 | \dhead{gf}{method-specializers @<method> @> @<list>} | |
295 | \dhead{cls}{eql-specializer} | |
296 | \dhead{gf}{eql-specializer-object @<specializer> @> @<value>}} | |
d9db9c73 MW |
297 | These are precisely the MOP functions and class: the symbols are |
298 | re-exported for portability, because different Lisp systems define these | |
299 | symbols in different packages. | |
fcb6c0fb MW |
300 | \end{describe*} |
301 | ||
c1ac1524 MW |
302 | |
303 | \subsection{Other CLOS utilities} | |
304 | ||
d9db9c73 MW |
305 | Some other minor CLOS utilities. |
306 | ||
c1ac1524 MW |
307 | \begin{describe}{mac} |
308 | {default-slot (@<instance> @<slot> @[@<slot-names>@]) \\ \ind | |
309 | @<form>^*} | |
d9db9c73 MW |
310 | This macro is useful in methods (usually @|:after| methods) on |
311 | @|shared-initialize|, to set slots to some sensible default values in the | |
312 | case where no suitable initarg was given, and default initialization is too | |
313 | complicated to be done using an initform. | |
314 | ||
315 | Set a slot to a default value, obeying the @|shared-initialize| protocol. | |
316 | If (a) the named @<slot> of @<instance> is unbound, and (b) either | |
317 | @<slot-names> is @|t|, or @<slot> is a member of the list @<slot-names>, | |
318 | then evaluate the @<form>s as an implicit @|progn| and store their | |
319 | value in the @<slot>. Otherwise do nothing. | |
320 | ||
321 | The @<instance>, @<slot>, and @<slot-names> (if any) are evaluated once | |
322 | each, left-to-right. | |
c1ac1524 MW |
323 | \end{describe} |
324 | ||
325 | \begin{describe}{mac} | |
326 | {define-on-demand-slot @<class> @<slot> (@<instance>) \\ \ind | |
327 | @[[ @<declaration>^* @! @<doc-string> @]] \\ | |
328 | @<form>^*} | |
d9db9c73 MW |
329 | This macro makes slots with delayed initialization: rather than being |
330 | set when the object is constructed, the slot's initial value is only | |
331 | calculated when it's first requested. This is useful if calculating the | |
332 | slot value is expensive and often not required, or if it's not possible to | |
333 | initialize the slot along with the rest of the object because of dependency | |
334 | cycles. | |
335 | ||
336 | The macro arranges things as follows. Whenever @|slot-value| is called | |
337 | (possibly indirectly, via a reader function) to read the named @<slot> (a | |
338 | symbol, not evaluated) on an (indirect) instance of @<class>, but the slot | |
339 | is unbound, then @<instance> is bound to the instance in question and the | |
340 | @<form>s are evaluated as an implicit @|progn| within the lexical | |
341 | environment of the @|define-on-demand-slot| call, and the resulting value | |
342 | is used as the initial value of the slot. (Furthermore, a block named | |
343 | @<slot> is wrapped around the @<form>s, allowing an early return if that | |
344 | should be useful.) | |
345 | ||
346 | This macro currently works by defining a method on @|slot-unbound|. | |
c1ac1524 MW |
347 | \end{describe} |
348 | ||
349 | ||
350 | \subsection{Building lists} | |
351 | ||
d9db9c73 MW |
352 | Many Lisp functions end up constructing lists. In simple cases, a function |
353 | like @|mapcar| will just do the job directly. In more complex cases, a | |
354 | common idiom is to build the list using @|push| for each element in turn; but | |
355 | a list built this way ends up in the wrong order, so an additional pass, | |
356 | usually using @|nreverse|, is necessary to fix it. | |
357 | ||
358 | A `list builder' is an object which can be used to construct a list in the | |
359 | right order. (Currently, a list-builder is simply a cons cell, whose cdr | |
360 | points to the first cons-cell of the list, and whose car points to its last | |
361 | cons; an empty list-builder is a cons whose cdr is nil and whose car is the | |
362 | cons itself, i.e., @|\#1=(\#1\# . nil)|.) | |
363 | ||
fcb6c0fb | 364 | \begin{describe}{fun}{make-list-builder \&optional @<initial> @> @<builder>} |
d9db9c73 | 365 | Return a fresh new list-builder, initially containing no items. |
fcb6c0fb MW |
366 | \end{describe} |
367 | ||
368 | \begin{describe}{fun}{lbuild-add @<builder> @<item> @> @<builder>} | |
d9db9c73 | 369 | Add @<item> to the end of the list being constructed in @<builder>. |
fcb6c0fb MW |
370 | \end{describe} |
371 | ||
372 | \begin{describe}{fun}{lbuild-add-list @<builder> @<list> @> @<builder>} | |
d9db9c73 MW |
373 | Append @<list> to the list being constructed in @<builder>. The list is |
374 | \emph{not} copied: adding further items to the list will clobber cdr of its | |
375 | final cons-cell. | |
fcb6c0fb MW |
376 | \end{describe} |
377 | ||
378 | \begin{describe}{fun}{lbuild-list @<builder> @> @<list>} | |
d9db9c73 MW |
379 | Return the list being constructed in the @<builder>. |
380 | ||
381 | It is permitted to continue adding items to the list: this will mutate the | |
382 | list in-place. Often, this is what you want. For example, one might write | |
383 | an analogue to @|pushnew| like this: | |
384 | \begin{prog} | |
385 | (defun lbuild-add-new | |
386 | (builder item \&key key test test-not \&rest keywords) \\ \ind | |
387 | (declare (ignore key test test-not)) \\ | |
388 | (when (apply \#'member item (lbuild-list builder) | |
389 | keywords) \\ \ind | |
390 | (lbuild-add builder item))) | |
391 | \end{prog} | |
fcb6c0fb MW |
392 | \end{describe} |
393 | ||
fcb6c0fb | 394 | |
c1ac1524 | 395 | \subsection{Merging lists} |
17c7c784 | 396 | |
d9db9c73 MW |
397 | The following machinery merges lists representing a partial order. The |
398 | primary use for this is in computing class precedence lists during class | |
399 | finalization. By building the input lists and choosing the tie-breaking | |
400 | @<pick> function appropriately, many different linearization algorithms can | |
401 | be implemented fairly easily using @|merge-lists| below. | |
fcb6c0fb | 402 | |
d9db9c73 MW |
403 | \begin{describe*} |
404 | {\dhead{cls} | |
405 | {inconsistent-merge-error (error) \&key :candidates :present} | |
406 | \dhead{gf}{merge-error-candidates @<error> @> @<list>} | |
407 | \dhead{gf}{merge-error-present-function @<error> @> @<function>}} | |
408 | The @|inconsistent-merge-error| condition class used to represent a failure | |
e8d70b1b | 409 | of the \descref{fun}{merge-lists}[function]. |
d9db9c73 MW |
410 | |
411 | The @<candidates> are a list of offending items from the input lists, in | |
412 | some order: the error is reporting that the function has failed because it | |
413 | is not possible to order the items listed in @<candidates> in any way | |
414 | without being inconsistent with at least one of the input lists. There is | |
415 | no default. | |
416 | ||
417 | The @<present> function is used to convert the input items into | |
418 | human-readable descriptions (printed using @|princ|); the default is | |
419 | @|identity|, which will simply print the items in a `friendly' format. | |
420 | (Using @|prin1-to-string| would print their machine-readable escaped forms | |
421 | instead.) | |
422 | ||
423 | The functions @|merge-error-candidates| and @|merge-error-present-function| | |
424 | respectively retrieve the candidates list and presentation function | |
425 | assigned to a condition when it was created. | |
426 | \end{describe*} | |
38b78e87 | 427 | |
fcb6c0fb | 428 | \begin{describe}{fun} |
d9db9c73 MW |
429 | {merge-lists @<lists> \&key :pick :test :present @> @<list>} |
430 | Return a merge of the @<lists>, considered as partial orderings. | |
431 | ||
432 | In more detail: @<lists> should be a list of lists. Each distinct item, as | |
433 | determined by the @<test> function (by default, @|eql|) appears in the | |
434 | result list exactly once. Furthermore, if, in some input list, an item $x$ | |
435 | appears earlier than a different item $y$, then $x$ will also precede $y$ | |
436 | in the output list. | |
437 | ||
438 | If the input lists contradict each other (e.g., list $A$ has $x$ before | |
439 | $y$, but list $B$ has $y$ before $x$), then an error of type | |
440 | @|inconsistent-merge-error| is signalled, with the offending items attached | |
441 | as candidates, and the function @<present> (by default, @|identity|) as the | |
442 | presentation function. | |
443 | ||
444 | Frequently, a collection of input lists has multiple valid merges. | |
445 | Whenever @|merge-lists| must decide between two or more equally good | |
446 | candidates, it calls the @<pick> function to choose one of them. | |
447 | Specifically, it invokes @|(funcall @<pick> @<candidates> | |
448 | @<merge-so-far>)|, where @<candidates> are the items it needs to choose | |
449 | between, and @<merge-so-far> is the currently determined prefix of the | |
450 | final merge. The order of items in the @<candidates> list reflects their | |
451 | order in the input lists: item $x$ precedes item $y$ in @<candidates> if | |
452 | any only if an occurrence of $x$ appears in an earlier input list than | |
453 | $y$. (This completely determines the order of candidates: if two items | |
454 | appear in the same list, then that list would have ordered them and we | |
455 | wouldn't have to call @<pick> to break the tie.) The default @<pick> | |
456 | function simply chooses the item appearing in the earliest list, i.e., | |
457 | effectively | |
458 | \begin{prog} | |
459 | (lambda (candidates merge-so-far) \\ \ind | |
460 | (declare (ignore merge-so-far)) \\ | |
461 | (car candidates)) | |
462 | \end{prog} | |
fcb6c0fb MW |
463 | \end{describe} |
464 | ||
c1ac1524 MW |
465 | |
466 | \subsection{Other list utilities} | |
467 | ||
468 | \begin{describe}{fun} | |
469 | {mappend @<function> @<list> \&rest @<more-lists> @> @<result-list>} | |
d9db9c73 MW |
470 | Return the result of appending @<list> and @<more-lists>, in order. All |
471 | but the final list are copied into the @<result-list>; the last one is used | |
472 | as-is. | |
c1ac1524 MW |
473 | \end{describe} |
474 | ||
fcb6c0fb MW |
475 | \begin{describe}{mac} |
476 | {categorize (\=@<item-var> @<items> | |
020b9e2b MW |
477 | @[[ :bind (@{ @<var> @! |
478 | (@<var> @[@<value>@]) @}^*) @]]) | |
479 | \\ \ind\ind | |
480 | (@{ (@<cat-var> @<cat-predicate>) @}^*) \-\\ | |
481 | @<declaration>^* \\ | |
cd35a54e | 482 | @<form>^* |
020b9e2b | 483 | \-\nlret @<value>^*} |
d9db9c73 MW |
484 | Partition an input list of @<items> according to the @<cat-predicate>s. |
485 | ||
486 | First, @<items> is evaluated, to yield a list. The @<item-var> is bound, | |
487 | an empty list is created for each @|(@<cat-var> @<cat-predicate>)| pair, | |
488 | and an iteration is begun. For each item in the list in turn is assigned | |
489 | to @<item-var>; then, the bindings given by the @|:bind| keyword are | |
490 | performed, as if by @|let*|; and the @<cat-predicate>s are evaluated in the | |
491 | resulting environment, one by one, until one of them returns non-nil. When | |
492 | this happens, the item is added to the corresponding list. If no predicate | |
493 | matches the item, an error is signalled. | |
494 | ||
495 | Once this iteration is complete, each @<cat-var> is bound to its | |
496 | corresponding completed list, and the body @<form>s are evaluated in the | |
497 | resulting environment (which does not include @<item-var>), as an implicit | |
498 | @|progn|, and the macro yields the values of the final @<form>. | |
fcb6c0fb MW |
499 | \end{describe} |
500 | ||
42291726 | 501 | \begin{describe}{fun}{partial-order-minima @<items> @<order> @> @<list>} |
d9db9c73 MW |
502 | Return a list of minimal items from the list @<items> according to a |
503 | non-strict partial order defined by the function @<order>: @|(funcall | |
504 | @<order> $x$ $y$)| should return non-nil if and only if $x \preceq y$ in | |
505 | the partial order. | |
42291726 MW |
506 | \end{describe} |
507 | ||
0a8f78ec MW |
508 | \begin{describe}{fun}{cross-product \&rest @<pieces>} |
509 | Return the cross product of the @<pieces>. | |
510 | ||
511 | Each arguments may be a list, or a (non-nil) atom, which is equivalent to a | |
512 | singleton list containing just that atom. Return a list of all possible | |
513 | lists which can be constructed by taking one item from each argument list | |
514 | in turn, in an arbitrary order. | |
515 | \end{describe} | |
516 | ||
64cbfb65 | 517 | \begin{describe}{fun} |
b8d8dada | 518 | {find-duplicates @<report> @<sequence> \&key :key :test} |
d9db9c73 MW |
519 | Call @<report> on each pair of duplicate items in a @<sequence>. |
520 | Duplicates are determined according to the @<key> (by default @|identity|) | |
521 | and @<test> (by default @|eql|) functions, in the usual way: two items $x$ | |
522 | and $y$ are considered equal if and only if @|(funcall @<test> (funcall | |
523 | @<key> $x$) (funcall @<key> $y$))| returns non-nil. | |
524 | ||
9a3cb461 MW |
525 | The @<report> function is called as @|(funcall @<report> @<duplicate> |
526 | @<previous>)|. Duplicates are reported in order; the @<previous> item is | |
527 | always the first matching item in the sequence. | |
528 | ||
d9db9c73 | 529 | This function will work for arbitrary @<test> functions, but it will run |
9a3cb461 MW |
530 | much more efficiently if @<test> is @|eq|, @|eql|, @|equal|, or @|equalp|, |
531 | because it can use hash-tables. (The generic implementation for lists is | |
532 | especially inefficient.) | |
64cbfb65 MW |
533 | \end{describe} |
534 | ||
c1ac1524 MW |
535 | |
536 | \subsection{Position tracking} | |
fcb6c0fb | 537 | |
d9db9c73 MW |
538 | The following functions are used to maintain file positions: see |
539 | \xref{sec:parsing.floc}. Columns are counted starting from zero at the far | |
540 | left. (No particular origin is needed for line numbers.) Newlines, vertical | |
541 | tabs, and form-feeds all move to the start of the next line; horizontal tabs | |
542 | move to the next multiple of eight columns; other characters simply advance | |
543 | to the next column. | |
544 | ||
fcb6c0fb MW |
545 | \begin{describe}{fun} |
546 | {update-position @<character> @<line> @<column> | |
547 | @> @<new-line> @<new-column>} | |
d9db9c73 MW |
548 | Assume that we found @<character> at a particular @<line> and @<column> in |
549 | a file: return the @<new-line> and @<new-column> for the next character. | |
fcb6c0fb MW |
550 | \end{describe} |
551 | ||
552 | \begin{describe}{fun} | |
553 | {backtrack-position @<character> @<line> @<column> | |
554 | @> @<old-line> @<old-column>} | |
d9db9c73 MW |
555 | Assume that we are currently at a particular @<line> and @<column> in a |
556 | file, and wish to \emph{unread} @<character>: return an @<old-line> and | |
557 | @<old-column> at which we might plausibly re-read the character, so that | |
e8d70b1b | 558 | the next call to \descref{fun}{update-position} will return us to @<line> |
d9db9c73 MW |
559 | and @<column>. (Specifically, the @<old-column> will likely be wrong if |
560 | @<character> is a horizontal tab. It is expected that this won't matter: | |
561 | the purpose of this function is to set things up so that the | |
562 | @|update-position| call that will accompany re-reading the character will | |
563 | return the correct values, rather than to use the @<old-line> and | |
564 | @<old-column> for any other purpose.) | |
fcb6c0fb MW |
565 | \end{describe} |
566 | ||
c34b237d | 567 | |
c1ac1524 | 568 | \subsection{Object printing} |
fcb6c0fb MW |
569 | |
570 | \begin{describe}{mac} | |
020b9e2b MW |
571 | {maybe-print-unreadable-object |
572 | (@<object> @<stream> | |
573 | @[[ :type @<type> @! | |
574 | :identity @<identity> @]]) \\ \ind | |
575 | @<declaration>^* \\ | |
cac85e0b | 576 | @<form>^*} |
d9db9c73 MW |
577 | If @|*print-escape*| is nil, then simply evaluate the @<form>s as an |
578 | implicit @|progn|; otherwise, print an `unreadable' object, as if by | |
579 | \begin{prog} | |
580 | (print-unreadable-object | |
581 | (@<object> @<stream> | |
582 | @[:type @<type>@] | |
583 | @[:identity @<identity>@]) \\ \ind | |
584 | @<form>^*) | |
585 | \end{prog} | |
fcb6c0fb MW |
586 | \end{describe} |
587 | ||
08b6e064 | 588 | \begin{describe}{fun}{print-ugly-stuff @<stream> @<func> @> @<value>^*} |
d9db9c73 MW |
589 | If @<stream> is a pretty-printing stream, then print a mandatory newline, |
590 | and call @<func> on the underlying non-pretty-printing stream. If | |
591 | @<stream> is not a pretty-printing stream, then simply call @<func> on | |
592 | @<stream> directly. | |
593 | ||
594 | The main purpose for this is to be able to access features of the | |
595 | underlying stream which a pretty-printing stream can't proxy. Most | |
596 | notably, this is used by C fragment output, which takes advantage of an | |
e8d70b1b | 597 | underlying \descref{cls}{position-aware-output-stream} to print @|\#line| |
d9db9c73 MW |
598 | directives, so that a C~compiler will blame the original fragment in the |
599 | Sod module source rather than the generated C code. | |
08b6e064 MW |
600 | \end{describe} |
601 | ||
fcb6c0fb | 602 | |
c1ac1524 | 603 | \subsection{Condition utilities} |
fcb6c0fb | 604 | |
d9db9c73 MW |
605 | The following definitions are useful when working with conditions. |
606 | ||
f7b60deb MW |
607 | \begin{describe}{cls} |
608 | {simple-control-error (control-error simple-error) | |
609 | \&key :format-control :format-arguments} | |
d9db9c73 MW |
610 | This is the obvious multiply-inherited subclass of @|control-error| whose |
611 | print form is determined by a @<format-control> and a @<format-arguments> | |
612 | list. | |
f7b60deb MW |
613 | \end{describe} |
614 | ||
db6c3279 MW |
615 | \begin{describe}{fun} |
616 | {designated-condition | |
617 | \=@<default-type> @<datum> @<arguments> \\ | |
618 | \>\&key :allow-pointless-arguments | |
619 | \nlret @<condition>} | |
d9db9c73 MW |
620 | Creates and returns a condition object of @<default-type>, given a |
621 | condition designator @<datum> and @<arguments>. | |
622 | ||
623 | The Common Lisp specification carefully explains how a `datum' and an | |
624 | argument list together form a `condition designator', and how such a pair | |
625 | are to be converted into a condition object with some default type, but | |
626 | there's no mechanism provided to simply do this task. (Functions like | |
627 | @|error| and @|signal| implicitly, but have possibly-undesirable | |
628 | side-effects, and don't allow control over the default type.) | |
629 | ||
630 | \begin{itemize} | |
631 | ||
632 | \item If @<datum> is a condition object, then the designated condition is | |
633 | simply @<datum>. In this case, if @<arguments> is not an empty list and | |
634 | @<allow-pointless-arguments> is nil (the default), an error is signalled; | |
635 | otherwise, the @<arguments> are ignored. | |
636 | ||
637 | \item If @<datum> is a symbol, then the designated condition is constructed | |
638 | by calling | |
639 | \begin{prog} | |
640 | (apply \#'make-condition @<datum> @<arguments>) | |
641 | \end{prog} | |
642 | ||
643 | \item If @<datum> is a string or function (i.e., a `format-control'), then | |
644 | the designated condition is constructed by calling | |
645 | \begin{prog} | |
646 | (make-condition \=@<default-type> \\ | |
647 | \>:format-control @<datum> \\ | |
648 | \>:format-arguments @<arguments>) | |
649 | \end{prog} | |
650 | ||
651 | \item Otherwise the designator is malformed, and an error is signalled. | |
652 | \end{itemize} | |
db6c3279 MW |
653 | \end{describe} |
654 | ||
f7b60deb MW |
655 | \begin{describe}{fun} |
656 | {invoke-associated-restart @<restart> @<condition> \&rest @<arguments>} | |
d9db9c73 MW |
657 | Invoke the active restart named @<restart>, associated with the given |
658 | @<condition>, passing a list of @<arguments>. | |
659 | ||
660 | The function attempts to find and invoke a restart with the given name. If | |
661 | @<condition> is non-nil, then it searches among restarts associated with | |
662 | that specific condition, and restarts associated with no condition; if | |
663 | @<condition> is nil, then it searches among all restarts. | |
664 | ||
665 | If a matching restart is found, it is invoked, passing the @<arguments> | |
666 | list. Otherwise, an error (of class @|control-error|) is signalled. | |
f7b60deb MW |
667 | \end{describe} |
668 | ||
c884ec24 MW |
669 | \begin{describe*} |
670 | {\dhead{cls}{enclosing-condition (condition) \&key :condition} | |
671 | \dhead{gf}{enclosed-condition @<enclosing-condition> @> @<condition>}} | |
d9db9c73 MW |
672 | An @|enclosing condition| is a condition which contains another condition |
673 | within it. Objects of type @|enclosing-condition| are used to add | |
674 | additional information to an existing condition, or to alter the type of a | |
675 | condition without losing information. | |
676 | ||
677 | When an @|enclosing-condition| is constructed, the @<condition> argument | |
678 | names the existing condition to be enclosed. This enclosed condition can | |
679 | be retrieved by calling @|enclosed-condition|. | |
c884ec24 MW |
680 | \end{describe*} |
681 | ||
682 | \begin{describe}{cls}{information (condition) \&key} | |
d9db9c73 MW |
683 | A condition of class @|information| conveys information which might be of |
684 | interest, but does not of itself indicate that anything is wrong. | |
685 | ||
686 | Within a compiler, @|information| conditions may be signalled in order to | |
687 | present the user with additional diagnostic information about a recently | |
688 | reported error. | |
c884ec24 MW |
689 | \end{describe} |
690 | ||
691 | \begin{describe}{cls} | |
692 | {simple-information (simple-condition information) \\ \ind | |
693 | \&key :format-control :format-arguments} | |
d9db9c73 MW |
694 | This is the obvious multiply-inherited subclass of @|information| |
695 | whose print-representation is determined by a @<format-control> and a | |
696 | @<format-arguments> list. | |
c884ec24 MW |
697 | \end{describe} |
698 | ||
699 | \begin{describe*} | |
700 | {\dhead{fun}{info @<datum> \&rest @<arguments> @> @<flag>} | |
701 | \dhead{rst}{noted} | |
702 | \dhead{fun}{noted \&optional @<condition>}} | |
d9db9c73 MW |
703 | The @|info| function establishes a restart named @|noted| and signals a |
704 | condition of default type @|simple-information|, designated by the @<datum> | |
705 | and @<arguments>. The @|info| function returns non-nil if and only if the | |
706 | associated @|noted| restart was invoked. | |
707 | ||
708 | The @|noted| restart accepts no arguments. | |
709 | ||
710 | The @|noted| function finds and invokes a @|noted| restart: if @<condition> | |
711 | is non-nil, then only the restart associated with that condition (and those | |
712 | not associated with any condition) are considered; otherwise, all | |
713 | conditions are considered. | |
c884ec24 MW |
714 | \end{describe*} |
715 | ||
716 | \begin{describe}{fun} | |
717 | {promiscuous-cerror @<continue-string> @<datum> \&rest @<arguments>} | |
d9db9c73 MW |
718 | Establish a @|continue| restart and signal an error of default type |
719 | @|simple-error|, designated by @<datum> and @<arguments>. The restart's | |
720 | report format is determined by @<continue-string> and the @<arguments>. | |
721 | ||
722 | Some implementations of @|cerror| associate the @|continue| restart which | |
723 | they establish with the condition they signal. This interferes with | |
724 | special effects -- specifically, enclosing the signalled condition and | |
725 | resignalling it. The @|promiscuous-cerror| function carefully avoids | |
726 | associating its restart with the condition. | |
c884ec24 MW |
727 | \end{describe} |
728 | ||
729 | \begin{describe}{fun}{cerror* @<datum> \&rest @<arguments>} | |
e8d70b1b | 730 | A simplified version of \descref{fun}{promiscuous-cerror} which uses the |
d9db9c73 MW |
731 | hardcoded string @|Continue| for the restart. This makes calling the |
732 | function more similar to other condition-signalling functions, at the | |
733 | expense of some usability in environments which don't continue after | |
734 | continuable errors automatically. | |
c884ec24 MW |
735 | \end{describe} |
736 | ||
c1ac1524 MW |
737 | |
738 | \subsection{Very miscellaneous utilities} | |
739 | ||
6f7ac504 MW |
740 | \begin{describe}{fun} |
741 | {whitespace-char-p @<character> @> @<generalized-boolean>} | |
d9db9c73 MW |
742 | Return non-nil if and only if @<character> is a whitespace character. |
743 | ||
744 | A character is whitespace if @|(peek-char t @<stream>)| would skip it. | |
6f7ac504 MW |
745 | \end{describe} |
746 | ||
c1ac1524 MW |
747 | \begin{describe}{fun} |
748 | {frob-identifier @<string> \&key :swap-case :swap-hyphen | |
749 | @> @<frobbed-string>} | |
7f9f8b07 MW |
750 | Return a `frobbed' version of the identifier @<string>. Two different |
751 | transformations can be applied. | |
752 | ||
753 | \begin{itemize} | |
754 | ||
755 | \item If @<swap-case> is non-nil (the default), and the letters in | |
756 | @<string> are either all uppercase or all lowercase, then switch the case | |
757 | of all of the letters. | |
758 | ||
759 | \item If @<swap-hyphen> is non-nil (the default), and @<string> contains | |
760 | either hyphens @`--' or underscores @`_', but not both, then replace the | |
761 | hyphens by underscores or \emph{vice-versa}. | |
762 | ||
763 | \end{itemize} | |
764 | ||
765 | (These are the `obvious' transformations to convert a C identifier into a | |
766 | Lisp symbol.) | |
767 | ||
768 | Some examples: | |
769 | \begin{itemize} | |
770 | \item @|(frob-identifier "foo")| $\Longrightarrow$ @|"FOO"| | |
771 | \item @|(frob-identifier "FOO")| $\Longrightarrow$ @|"foo"| | |
772 | \item @|(frob-identifier "FooBar")| $\Longrightarrow$ @|"FooBar"| | |
773 | \item @|(frob-identifier "Foo-Bar")| $\Longrightarrow$ @|"Foo_Bar"| | |
774 | \item @|(frob-identifier "Foo_Bar")| $\Longrightarrow$ @|"Foo-Bar"| | |
775 | \item @|(frob-identifier "foo_bar")| $\Longrightarrow$ @|"FOO-BAR"| | |
776 | \item @|(frob-identifier "foo_bar" :swap-hyphen nil)| $\Longrightarrow$ | |
777 | @|"FOO_BAR"| | |
778 | \item @|(frob-identifier "foo_bar" :swap-case nil)| $\Longrightarrow$ | |
779 | @|"foo-bar"| | |
780 | \item @|(frob-identifier "foo_bar" :swap-case nil :swap-hyphen nil)| | |
781 | $\Longrightarrow$ @|"foo_bar"| | |
782 | \end{itemize} | |
c1ac1524 MW |
783 | \end{describe} |
784 | ||
785 | \begin{describe}{fun} | |
7f9f8b07 MW |
786 | {compose @<functions> @> @<function>} |
787 | Return the left-to-right composition zero or more @<functions>. | |
788 | ||
789 | Let $f_1$, $f_2$, \ldots, $f_n$ be functions, and let $g = @|(compose $f_1$ | |
790 | $f_2$ $\cdots$ $f_n$)|$ is their composition. If $g$ is applied to | |
791 | arguments, the effect is as follows: first, $f_1$ is applied to the | |
792 | arguments, yielding some value; $f_2$ is applied to this value, yielding a | |
793 | second value; and so on, until finally the value yielded by $f_n$ is | |
794 | returned as the result of $g$. Note that this is the reverse of the usual | |
795 | mathematician's convention, but the author finds this ordering | |
796 | significantly easier to work with: | |
797 | \[ g = f_n \circ \cdots \circ f_2 \circ f_1 \] | |
798 | ||
799 | If any of the input functions return multiple values then \emph{all} of the | |
800 | values are passed on to the next function in the list. (If the last | |
801 | function returns multiple values then all of the values are returned from | |
802 | the composition. | |
803 | ||
804 | The result of composing no functions is a function which simply returns all | |
805 | of its arguments as values; essentially, $@|(compose)| \equiv | |
806 | @|\#'values|$. | |
c1ac1524 MW |
807 | \end{describe} |
808 | ||
809 | \begin{describe}{mac}{defvar-unbound @<name> @<documentation> @> @<name>} | |
7f9f8b07 MW |
810 | Define a variable called @<name>, with a @<documentation> string. |
811 | ||
812 | The Common Lisp @|defvar| macro accepts both an initial value and a | |
813 | doc-string as optional arguments, in that order, with the result that it's | |
814 | not possible to define a variable and establish a documentation string for | |
815 | it without also giving it an initial value. The @|defvar-unbound| macro, | |
816 | on the other hand, never changes the symbol's variable-value. | |
c1ac1524 MW |
817 | \end{describe} |
818 | ||
fcb6c0fb | 819 | \begin{describe}{mac} |
c1ac1524 MW |
820 | {dosequence (@<var> @<sequence> |
821 | @[[ :start @<start> @! :end @<end> @! | |
7f9f8b07 | 822 | :indexvar @<index-var> @]]) \\ \ind |
c1ac1524 MW |
823 | @<declaration>^* \\ |
824 | @{ @<tag> @! @<statement> @}^*} | |
7f9f8b07 MW |
825 | Iterate over a @<sequence>. Common Lisp has a rich collection of iteration |
826 | primitives, and a rich collection of functions for working with sequences, | |
827 | but no macro for iterating over the items of a sequence. | |
828 | ||
829 | First, the @<sequence> is evaluated. If @<start> and/or @<end> are | |
830 | provided, they are also evaluated (in that order), which should produce | |
831 | integers; @<end> may be also be nil. If not provided, or nil (in the case | |
832 | of @<end>), @<start> and @<end> default respectively to zero and the length | |
833 | of the @<sequence>. For each item in the sequence between the @<start> and | |
834 | @<end> positions (i.e., each item in @|(subseq @<sequence> @<start> | |
835 | @<end>)|, in order, the body is evaluated as an implicit @|tagbody|, with | |
836 | @<var> bound to the item and, if provided, @<index-var> bound to the item's | |
837 | index. It is not specified whether the @<var> and @<index-var> are | |
838 | let-bound or mutated in each iteration. | |
839 | ||
840 | Unlike other Common Lisp @|do|\dots\ forms, there is no `result' form. | |
fcb6c0fb MW |
841 | \end{describe} |
842 | ||
843 | \begin{describe}{mac} | |
c1ac1524 MW |
844 | {define-access-wrapper @<from> @<to> |
845 | @[[ :read-only @<read-only-flag> @]]} | |
7f9f8b07 MW |
846 | Define @<from> as a function of one argument, so that @|(@<from> @<thing>)| |
847 | is equivalent to @|(@<to> @<thing>)|. If @<read-only-flag> is nil (the | |
848 | default), then also define @|(setf @<from>)| so that @|(setf (@<from> | |
849 | @<thing>) @<value>)| is equivalent to @|(setf (@<to> @<thing>) @<value>)|. | |
850 | ||
851 | In a @|defstruct| form, the accessor function names are constructed based | |
852 | on the structure name and slot names. The structure name and accessor | |
853 | names are part of the exported interface, but the slot names ideally | |
854 | shouldn't be. This causes a problem when the slot name which will lead to | |
855 | the right accessor is already an external symbol in some package. You can | |
856 | solve this problem by choosing an internal name for the symbol, and then | |
857 | using this macro to define an accessor function with the name that you | |
858 | want, in terms of the accessor that @|defstruct| made. | |
fcb6c0fb MW |
859 | \end{describe} |
860 | ||
6f7ac504 MW |
861 | \begin{describe}{fun} |
862 | {distinguished-point-shortest-paths @<root> @<neighbours-func> | |
863 | @> @<list>} | |
7f9f8b07 MW |
864 | Calculate the shortest path from the @<root> to each node reachable from it |
865 | in a directed graph. The nodes of the graph can be any kind of object; | |
866 | they will be compared using @|eql|. | |
867 | ||
868 | The @<neighbours-func> should be a function which, given a node~$v$ as its | |
869 | only argument, returns a list of cons cells @|($v'$ . $c'$)|, one for each | |
870 | node~$v'$ adjacent to $v$, indicating the cost $c'$ of traversing the arc | |
871 | from $v$ to $v'$. | |
872 | ||
873 | The return value is a list of cons cells @|($c$ . $p$)|, where $p$ is list | |
874 | of nodes, in reverse order, along a path from the @<root> to some other | |
875 | node, and $c$ is the total cost of traversing this path. (Therefore @|(car | |
876 | $p$)| is the destination node, and @|(car (last $p$))| is always the | |
877 | @<root> itself.) | |
878 | ||
879 | The function runs in $O(n^2)$ time, where $n$ is the number of nodes | |
880 | reachable from the @<root>. Currently, it uses an algorithm due to Edsger | |
881 | Dijkstra. | |
6f7ac504 MW |
882 | \end{describe} |
883 | ||
f0b1f2bf MW |
884 | |
885 | \subsection{Other exported symbols} | |
886 | ||
887 | \begin{describe}{sym}{int} | |
888 | The symbol @|int| is exported by the @|sod-utilities| package, without | |
889 | giving it any particular meaning. This is done because it's given | |
890 | non-conflicting meanings by two different packages, and it's more | |
891 | convenient for user code not to have to deal with an unnecessary symbol | |
892 | conflict. Specifically, the @|sod| package wants to define it as a C type | |
893 | specifier, see \descref{cls}{simple-c-type}; and @|optparse| wants to | |
894 | define it as an option handler, see \descref{opt}{int}. | |
895 | \end{describe} | |
896 | ||
fcb6c0fb MW |
897 | %%%-------------------------------------------------------------------------- |
898 | \section{Option parser} \label{sec:misc.optparse} | |
899 | ||
d9bd7c90 | 900 | Most of these symbols are defined in the @|optparse| package. |
fcb6c0fb MW |
901 | |
902 | \begin{describe}{fun}{exit \&optional (@<code> 0) \&key :abrupt} | |
903 | \end{describe} | |
904 | ||
905 | \begin{describe}{var}{*program-name*} | |
906 | \end{describe} | |
907 | ||
908 | \begin{describe}{var}{*command-line*} | |
909 | \end{describe} | |
910 | ||
911 | \begin{describe}{fun}{set-command-line-arguments} | |
912 | \end{describe} | |
913 | ||
fcb6c0fb MW |
914 | \begin{describe}{fun}{moan @<format-string> \&rest @<format-args>} |
915 | \end{describe} | |
916 | ||
917 | \begin{describe}{fun}{die @<format-string> \&rest @<format-args>} | |
918 | \end{describe} | |
919 | ||
920 | \begin{describe}{var}{*options*} | |
921 | \end{describe} | |
922 | ||
923 | \begin{describe}{cls}{option} | |
924 | \end{describe} | |
925 | ||
926 | \begin{describe}{fun}{optionp @<object> @> @<generalized-boolean>} | |
927 | \end{describe} | |
928 | ||
929 | \begin{describe}{fun} | |
020b9e2b MW |
930 | {make-option \=@<long-name> @<short-name> \+\\ |
931 | \&optional @<arg-name> \\ | |
932 | \&key :tag :negated-tag | |
933 | :arg-optional-p :documentation \- | |
fcb6c0fb MW |
934 | \nlret @<option>} |
935 | \end{describe} | |
936 | ||
937 | \begin{describe*} | |
938 | {\dhead{fun}{opt-short-name @<option> @> @<character-or-null>} | |
939 | \dhead{fun}{setf (opt-short-name @<option>) @<character-or-null>} | |
940 | \dhead{fun}{opt-long-name @<option> @> @<string-or-null>} | |
941 | \dhead{fun}{setf (opt-long-name @<option>) @<string-or-null>} | |
942 | \dhead{fun}{opt-tag @<option> @> @<tag>} | |
943 | \dhead{fun}{setf (opt-tag @<option>) @<tag>} | |
944 | \dhead{fun}{opt-negated-tag @<option> @> @<tag>} | |
945 | \dhead{fun}{setf (opt-negated-tag @<option>) @<tag>} | |
946 | \dhead{fun}{opt-arg-name @<option> @> @<string-or-null>} | |
947 | \dhead{fun}{setf (opt-arg-name @<option>) @<string-or-null>} | |
a75cd932 MW |
948 | \dhead{fun}{opt-arg-optional-p @<option> @> @<generalized-boolean>} |
949 | \dhead{fun}{setf (opt-arg-optional-p @<option>) @<generalized-boolean>} | |
fcb6c0fb MW |
950 | \dhead{fun}{opt-documentation @<option> @> @<string-or-null>} |
951 | \dhead{fun}{setf (opt-documentation @<option>) @<string-or-null>}} | |
952 | \end{describe*} | |
953 | ||
954 | \begin{describe}{cls}{option-parser} | |
955 | \end{describe} | |
956 | ||
957 | \begin{describe}{fun}{option-parser-p @<object> @> @<generalized-boolean>} | |
958 | \end{describe} | |
959 | ||
960 | \begin{describe}{fun} | |
961 | {make-option-parser \&key \=:args :options :non-option :numericp \+ \\ | |
962 | :negated-numeric-p long-only-p \- | |
963 | \nlret @<option-parser>} | |
964 | \end{describe} | |
965 | ||
966 | \begin{describe*} | |
967 | {\dhead{fun}{op-options @<option-parser> @> @<list>} | |
968 | \dhead{fun}{setf (op-options @<option-parser>) @<list>} | |
969 | \dhead{fun}{op-non-option @<option-parser> @> @<action>} | |
970 | \dhead{fun}{setf (op-non-option @<option-parser>) @<action>} | |
971 | \dhead{fun}{op-long-only-p @<option-parser> @> @<generalized-boolean>} | |
972 | \dhead{fun}{setf (op-long-only-p @<option-parser>) @<generalized-boolean>} | |
973 | \dhead{fun}{op-numeric-p @<option-parser> @> @<generalized-boolean>} | |
974 | \dhead{fun}{setf (op-numeric-p @<option-parser>) @<generalized-boolean>} | |
975 | \dhead{fun}{op-negated-numeric-p @<option-parser> @<generalized-boolean>} | |
976 | \dhead{fun}{setf (op-negated-numeric-p @<option-parser>) @<generalized-boolean>} | |
977 | \dhead{fun}{op-negated-p @<option-parser> @> @<generalized-boolean>} | |
978 | \dhead{fun}{setf (op-negated-p @<option-parser>) @<generalized-boolean>}} | |
979 | \end{describe*} | |
980 | ||
981 | \begin{describe}{cls} | |
982 | {option-parse-error (error simple-condition) | |
983 | \&key :format-control :format-arguments} | |
984 | \end{describe} | |
985 | ||
a75cd932 MW |
986 | \begin{describe}{fun}{option-parse-error @<msg> \&optional @<args>} |
987 | \end{describe} | |
988 | ||
fcb6c0fb MW |
989 | \begin{describe}{fun}{option-parse-remainder @<option-parser>} |
990 | \end{describe} | |
991 | ||
992 | \begin{describe}{fun}{option-parse-return @<tag> \&optional @<argument>} | |
993 | \end{describe} | |
994 | ||
995 | \begin{describe}{fun}{option-parse-next @<option-parser>} | |
996 | \end{describe} | |
997 | ||
cac85e0b | 998 | \begin{describe}{mac}{option-parse-try @<form>^*} |
fcb6c0fb MW |
999 | \end{describe} |
1000 | ||
cac85e0b | 1001 | \begin{describe}{mac}{with-unix-error-reporting () @<form>^*} |
fcb6c0fb MW |
1002 | \end{describe} |
1003 | ||
1004 | \begin{describe}{mac} | |
1005 | {defopthandler @<name> (@<var> @[@<arg>@]) @<lambda-list> \\ \ind | |
cac85e0b MW |
1006 | @[[ @<declaration>^* @! @<doc-string> @]] \\ |
1007 | @<form>^*} | |
fcb6c0fb MW |
1008 | \end{describe} |
1009 | ||
1010 | \begin{describe}{fun} | |
1011 | {invoke-option-handler @<handler> @<locative> @<arg> @<arguments>} | |
1012 | \end{describe} | |
1013 | ||
1014 | \begin{describe}{opt}{set \&optional @<value>} | |
1015 | \end{describe} | |
1016 | ||
1017 | \begin{describe}{opt}{clear \&optional @<value>} | |
1018 | \end{describe} | |
1019 | ||
1020 | \begin{describe}{opt}{inc \&optional @<maximum> @<step>} | |
1021 | \end{describe} | |
1022 | ||
a7011782 | 1023 | \begin{describe}{opt}{dec \&optional @<minimum> @<step>} |
fcb6c0fb MW |
1024 | \end{describe} |
1025 | ||
1026 | \begin{describe}{opt}{read} | |
1027 | \end{describe} | |
1028 | ||
1029 | \begin{describe}{opt}{int \&key :radix :min :max} | |
1030 | \end{describe} | |
1031 | ||
1032 | \begin{describe}{opt}{string} | |
1033 | \end{describe} | |
1034 | ||
1035 | \begin{describe}{opt}{keyword \&optional @<valid>} | |
1036 | \end{describe} | |
1037 | ||
1038 | \begin{describe}{opt}{list \&optional @<handler> \&rest @<handler-args>} | |
1039 | \end{describe} | |
1040 | ||
cac85e0b MW |
1041 | \begin{describe}{mac} |
1042 | {defoptmacro @<name> @<lambda-list> \\ \ind | |
1043 | @[[ @<declaration>^* @! @<doc-string> @]] \\ | |
1044 | @<form>^*} | |
fcb6c0fb MW |
1045 | \end{describe} |
1046 | ||
1047 | \begin{describe}{fun}{parse-option-form @<form>} | |
1048 | \end{describe} | |
1049 | ||
1050 | \begin{describe}{mac} | |
1051 | {options @{ \=@<string> @! \+ \\ | |
1052 | @<option-macro> @! (@<option-macro> @<macro-arg>^*) @! \\ | |
1053 | (@[[ \=@<character> @! (:short-name @<character>) @! \+ \\ | |
1054 | @<string>^* @! @<symbol> @! @<rational> @! | |
1055 | (:long-name @<string>) @! \\ | |
1056 | (@<string> @<format-arg>^+) @! | |
1057 | (:doc @<string> @<format-arg>^*) @! \\ | |
984c3fc3 | 1058 | (:arg @<arg-name>) @! (:opt-arg @<arg-name>) @! \\ |
fcb6c0fb MW |
1059 | @<keyword> @! (:tag @<tag>) @! |
1060 | (:negated-tag @<tag>) @! \\ | |
cac85e0b MW |
1061 | @{ (@<handler> @<var> @<handler-arg>^*) @}^* |
1062 | @]]) @}^*} | |
fcb6c0fb MW |
1063 | \end{describe} |
1064 | ||
1065 | \begin{describe}{fun} | |
1066 | {simple-usage @<option-list> \&optional @<mandatory-args> @> @<list>} | |
1067 | \end{describe} | |
1068 | ||
1069 | \begin{describe}{fun}{show-usage @<prog> @<usage> \&optional @<stream>} | |
1070 | \end{describe} | |
1071 | ||
1072 | \begin{describe}{fun} | |
1073 | {show-help @<prog> @<usage> @<option-list> \&optional @<stream>} | |
1074 | \end{describe} | |
1075 | ||
1076 | \begin{describe}{fun}{sanity-check-option-list @<option-list>} | |
1077 | \end{describe} | |
1078 | ||
1079 | \begin{describe*} | |
1080 | {\dhead{var}{*help*} | |
1081 | \dhead{var}{*version*} | |
1082 | \dhead{var}{*usage*}} | |
1083 | \end{describe*} | |
1084 | ||
1085 | \begin{describe}{fun}{do-usage \&optional @<stream>} | |
1086 | \end{describe} | |
1087 | ||
1088 | \begin{describe}{fun}{die-usage} | |
1089 | \end{describe} | |
1090 | ||
1091 | \begin{describe}{optmac} | |
1092 | {help-options \&key :short-help :short-version :short-usage} | |
1093 | \end{describe} | |
1094 | ||
1095 | \begin{describe}{fun} | |
1096 | {define-program \&key \=:program-name \+ \\ | |
1097 | :help :version :usage :full-usage \\ | |
1098 | :options} | |
1099 | \end{describe} | |
1100 | ||
1101 | \begin{describe}{mac} | |
1102 | {do-options (@[[ :parser @<option-parser> @]]) \\ \ind | |
1103 | @{ (@{ @<case> @! (@<case>^*)@} (@[@[@<opt-var>@] @<arg-var>@]) | |
1104 | @<form>^*) @}^*} | |
1105 | \end{describe} | |
1106 | ||
8fafc463 | 1107 | \begin{describe}{fun}{sod-frontend:augment-options @<options-list>} |
d9bd7c90 MW |
1108 | \end{describe} |
1109 | ||
fcb6c0fb MW |
1110 | %%%-------------------------------------------------------------------------- |
1111 | \section{Property sets} \label{sec:misc.pset} | |
1112 | ||
2c7465ac MW |
1113 | \begin{describe}{fun}{property-key @<name> @> @<keyword>} |
1114 | \end{describe} | |
1115 | ||
1116 | \begin{describe}{gf}{decode-property @<raw-value> @> @<type> @<value>} | |
1117 | \end{describe} | |
1118 | ||
1119 | \begin{describe}{cls}{property} | |
1120 | \end{describe} | |
1121 | ||
1122 | \begin{describe}{fun}{propertyp @<object> @> @<generalized-boolean>} | |
1123 | \end{describe} | |
1124 | ||
1125 | \begin{describe}{fun} | |
1126 | {make-property @<name> @<raw-value> \&key :type :location :seenp} | |
1127 | \end{describe} | |
1128 | ||
1129 | \begin{describe*} | |
1130 | {\dhead{fun}{p-name @<property> @> @<name>} | |
1dd7dba9 | 1131 | \dhead{meth}{property}{file-location (@<property> property) @> @<floc>} |
2c7465ac | 1132 | \dhead{fun}{p-value @<property> @> @<value>} |
2c7465ac | 1133 | \dhead{fun}{p-type @<property> @> @<type>} |
2c7465ac | 1134 | \dhead{fun}{p-key @<property> @> @<symbol>} |
2c7465ac MW |
1135 | \dhead{fun}{p-seenp @<property> @> @<boolean>} |
1136 | \dhead{fun}{setf (p-seenp @<property>) @<boolean>}} | |
1137 | \end{describe*} | |
1138 | ||
2c7465ac MW |
1139 | \begin{describe}{gf} |
1140 | {coerce-property-value @<value> @<type> @<wanted> @> @<coerced-value>} | |
1141 | \end{describe} | |
1142 | ||
1143 | \begin{describe}{cls}{pset} | |
1144 | \end{describe} | |
1145 | ||
1146 | \begin{describe}{fun}{psetp @<object> @> @<generalized-boolean>} | |
1147 | \end{describe} | |
1148 | ||
1149 | \begin{describe}{fun}{make-pset @> @<pset>} | |
1150 | \end{describe} | |
1151 | ||
1152 | \begin{describe}{fun}{pset-get @<pset> @<key> @> @<property-or-nil>} | |
1153 | \end{describe} | |
1154 | ||
1155 | \begin{describe}{fun}{pset-store @<pset> @<property> @> @<property>} | |
1156 | \end{describe} | |
1157 | ||
1158 | \begin{describe}{fun}{pset-map @<func> @<pset>} | |
1159 | \end{describe} | |
1160 | ||
cac85e0b MW |
1161 | \begin{describe}{mac} |
1162 | {with-pset-iterator (@<iter> @<pset>) @<declaration>^* @<form>^*} | |
2c7465ac MW |
1163 | \end{describe} |
1164 | ||
1165 | \begin{describe}{fun} | |
1166 | {store-property @<pset> @<name> @<value> \&key :type :location | |
1167 | @> @<property>} | |
1168 | \end{describe} | |
1169 | ||
1170 | \begin{describe}{fun} | |
1171 | {get-property @<pset> @<name> @<type> \&optional @<default> | |
1172 | @> @<value> @<floc-or-nil>} | |
1173 | \end{describe} | |
1174 | ||
1175 | \begin{describe}{fun} | |
1176 | {add-property @<pset> @<name> @<value> \&key :type :location | |
1177 | @> @<property>} | |
1178 | \end{describe} | |
1179 | ||
1180 | \begin{describe}{fun}{make-property-set \&rest @<plist> @> @<pset>} | |
1181 | \end{describe} | |
1182 | ||
1183 | \begin{describe}{gf}{property-set @<thing> @> @<pset>} | |
1184 | \end{describe} | |
1185 | ||
1186 | \begin{describe}{fun}{check-unused-properties @<pset>} | |
1187 | \end{describe} | |
1188 | ||
1189 | \begin{describe}{mac} | |
1190 | {default-slot-from-property | |
1191 | (@<instance> @<slot> @[@<slot-names>@]) \\ \ind\ind | |
1192 | (@<pset> @<property> @<type> @[@<prop-var> @<convert-form>^*@]) \- \\ | |
cac85e0b | 1193 | @<declaration>^* \\ |
2c7465ac MW |
1194 | @<default-form>^*} |
1195 | \end{describe} | |
1196 | ||
b944e68b MW |
1197 | \begin{describe}{fun} |
1198 | {parse-property @<scanner> @<pset> | |
1199 | @> @<result> @<success-flag> @<consumed-flag>} | |
1200 | \end{describe} | |
1201 | ||
2c7465ac MW |
1202 | \begin{describe}{fun} |
1203 | {parse-property-set @<scanner> | |
1204 | @> @<result> @<success-flag> @<consumed-flag>} | |
1205 | \end{describe} | |
1206 | ||
fcb6c0fb | 1207 | %%%-------------------------------------------------------------------------- |
8fc2932b | 1208 | \section{Miscellaneous translator features} \label{sec:misc.misc} |
fcb6c0fb MW |
1209 | |
1210 | \begin{describe}{var}{*sod-version*} | |
1211 | \end{describe} | |
1212 | ||
1213 | \begin{describe}{var}{*debugout-pathname*} | |
1214 | \end{describe} | |
1215 | ||
60529354 MW |
1216 | \begin{describe}{fun} |
1217 | {test-module @<path> \&key :reason :clear :backtrace @> @<status>} | |
fcb6c0fb MW |
1218 | \end{describe} |
1219 | ||
4fd69126 MW |
1220 | \begin{describe}{fun} |
1221 | {test-parse-c-type @<string> | |
1222 | @> t @<c-type> @<kernel> @<string> @! nil @<indicator>} | |
1223 | \end{describe} | |
1224 | ||
bf34c708 MW |
1225 | \begin{describe}{fun} |
1226 | {test-parse-pset @<string> | |
1227 | @> t @<pset> @! nil @<indicator>} | |
1228 | \end{describe} | |
1229 | ||
3e21ae3f | 1230 | \begin{describe}{mac} |
2b7ce7a5 MW |
1231 | {test-parser (@<scanner> \&key :backtrace) @<parser> @<input> |
1232 | @> @<result> @<status> @<remainder>} | |
3e21ae3f MW |
1233 | \end{describe} |
1234 | ||
fcb6c0fb MW |
1235 | \begin{describe}{fun}{exercise} |
1236 | \end{describe} | |
1237 | ||
2c7465ac MW |
1238 | \begin{describe}{fun}{sod-frontend:main} |
1239 | \end{describe} | |
1240 | ||
fcb6c0fb MW |
1241 | %%%----- That's all, folks -------------------------------------------------- |
1242 | ||
1243 | %%% Local variables: | |
1244 | %%% mode: LaTeX | |
1245 | %%% TeX-master: "sod.tex" | |
1246 | %%% TeX-PDF-mode: t | |
1247 | %%% End: |