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