1.0.43.61: teach docstrings.lisp about parentheses
[sbcl.git] / doc / manual / beyond-ansi.texinfo
1 @node  Beyond the ANSI Standard
2 @comment  node-name,  next,  previous,  up
3 @chapter Beyond the ANSI Standard
4
5 SBCL is derived from CMUCL, which implements many extensions to the
6 ANSI standard. SBCL doesn't support as many extensions as CMUCL, but
7 it still has quite a few.  @xref{Contributed Modules}.
8
9 @menu
10 * Garbage Collection::
11 * Metaobject Protocol::
12 * Support For Unix::
13 * Customization Hooks for Users::
14 * Tools To Help Developers::
15 * Resolution of Name Conflicts::
16 * Hash Table Extensions::
17 * Miscellaneous Extensions::
18 * Stale Extensions::
19 * Efficiency Hacks::
20 @end menu
21
22 @node  Garbage Collection
23 @comment  node-name,  next,  previous,  up
24 @section Garbage Collection
25 @cindex Garbage collection
26
27 SBCL provides additional garbage collection functionality not
28 specified by ANSI.
29
30 @include var-sb-ext-star-after-gc-hooks-star.texinfo
31 @include fun-sb-ext-gc.texinfo
32
33 @subsection Finalization
34 @cindex Finalization
35
36 Finalization allows code to be executed after an object has been
37 garbage collected. This is useful for example for releasing foreign
38 memory associated with a Lisp object.
39
40 @include fun-sb-ext-finalize.texinfo
41 @include fun-sb-ext-cancel-finalization.texinfo
42
43 @subsection Weak Pointers
44 @cindex Weak pointers
45
46 Weak pointers allow references to objects to be maintained without
47 keeping them from being garbage collected: useful for building caches
48 among other things.
49
50 Hash tables can also have weak keys and values: @pxref{Hash Table
51 Extensions}.
52
53 @include fun-sb-ext-make-weak-pointer.texinfo
54 @include fun-sb-ext-weak-pointer-value.texinfo
55
56 @subsection Introspection and Tuning
57
58 @include var-sb-ext-star-gc-run-time-star.texinfo
59 @include fun-sb-ext-bytes-consed-between-gcs.texinfo
60 @include fun-sb-ext-generation-average-age.texinfo
61 @include fun-sb-ext-generation-bytes-allocated.texinfo
62 @include fun-sb-ext-generation-bytes-consed-between-gcs.texinfo
63 @include fun-sb-ext-generation-minimum-age-before-gc.texinfo
64 @include fun-sb-ext-generation-number-of-gcs-before-promotion.texinfo
65 @include fun-sb-ext-generation-number-of-gcs.texinfo
66 @include fun-sb-ext-get-bytes-consed.texinfo
67
68 @node Metaobject Protocol
69 @comment  node-name,  next,  previous,  up
70 @section Metaobject Protocol
71
72 SBCL supports a metaobject protocol which is intended to be compatible
73 with AMOP; present exceptions to this (as distinct from current bugs)
74 are:
75
76 @itemize
77
78 @item
79 @findex @sbmop{compute-effective-method}
80 @code{compute-effective-method} only returns one value, not two.
81
82 There is no record of what the second return value was meant to
83 indicate, and apparently no clients for it.
84
85 @item
86 @tindex @cl{generic-function}
87 @tindex @cl{standard-generic-function}
88 @tindex @sbmop{funcallable-standard-object}
89 @tindex @cl{standard-object}
90 @tindex @cl{function}
91 The direct superclasses of @code{sb-mop:funcallable-standard-object} are
92 @code{(function standard-object)}, not @code{(standard-object function)}.
93
94 This is to ensure that the @code{standard-object} class is the last of
95 the standardized classes before @code{t} appearing in the class
96 precedence list of @code{generic-function} and
97 @code{standard-generic-function}, as required by section 1.4.4.5 of the
98 ANSI specification.
99
100 @item
101 @findex @cl{ensure-generic-function}
102 @findex @sbmop{generic-function-declarations}
103 the arguments @code{:declare} and @code{:declarations} to
104 @code{ensure-generic-function} are both accepted, with the leftmost
105 argument defining the declarations to be stored and returned by
106 @code{generic-function-declarations}.
107
108 Where AMOP specifies @code{:declarations} as the keyword argument to
109 @code{ensure-generic-function}, the Common Lisp standard specifies
110 @code{:declare}.  Portable code should use @code{:declare}.
111
112 @item
113 @findex @sbmop{validate-superclass}
114 @findex @sbmop{finalize-inheritance}
115 @tindex @cl{standard-class}
116 @tindex @sbmop{funcallable-standard-class}
117 @tindex @cl{function}
118 @findex @sbmop{class-prototype}
119 although SBCL obeys the requirement in AMOP that
120 @code{validate-superclass} should treat @code{standard-class} and
121 @code{funcallable-standard-class} as compatible metaclasses, we
122 impose an additional requirement at class finalization time: a class
123 of metaclass @code{funcallable-standard-class} must have
124 @code{function} in its superclasses, and a class of metaclass
125 @code{standard-class} must not.
126
127 @findex @cl{typep}
128 @findex @cl{class-of}
129 @findex @cl{subtypep}
130 After a class has been finalized, it is associated with a class
131 prototype which is accessible by a standard mop function
132 @code{sb-mop:class-prototype}.  The user can then ask whether this
133 object is a @code{function} or not in several different ways: whether it
134 is a function according to @code{typep}; whether its @code{class-of} is
135 @code{subtypep} @code{function}, or whether @code{function} appears in
136 the superclasses of the class.  The additional consistency requirement
137 comes from the desire to make all of these answers the same.
138
139 The following class definitions are bad, and will lead to errors
140 either immediately or if an instance is created:
141 @lisp
142 (defclass bad-object (funcallable-standard-object)
143   ()
144   (:metaclass standard-class))
145 @end lisp
146 @lisp
147 (defclass bad-funcallable-object (standard-object)
148   ()
149   (:metaclass funcallable-standard-class))
150 @end lisp
151 The following definition is acceptable:
152 @lisp
153 (defclass mixin ()
154   ((slot :initarg slot)))
155 (defclass funcallable-object (funcallable-standard-object mixin)
156   ()
157   (:metaclass funcallable-standard-class))
158 @end lisp
159 and leads to a class whose instances are funcallable and have one slot.
160
161 @tindex @sbmop{funcallable-standard-object}
162 Note that this requirement also applies to the class
163 @code{sb-mop:funcallable-standard-object}, which has metaclass
164 @code{sb-mop:funcallable-standard-class} rather than
165 @code{standard-class} as AMOP specifies.
166
167 @item
168 the requirement that ``No portable class @math{C_p} may inherit, by
169 virtue of being a direct or indirect subclass of a specified class, any
170 slot for which the name is a symbol accessible in the
171 @code{common-lisp-user} package or exported by any package defined in
172 the ANSI Common Lisp standard.'' is interpreted to mean that the
173 standardized classes themselves should not have slots named by external
174 symbols of public packages.
175
176 The rationale behind the restriction is likely to be similar to the ANSI
177 Common Lisp restriction on defining functions, variables and types named
178 by symbols in the Common Lisp package: preventing two independent pieces
179 of software from colliding with each other.
180
181 @item
182 @findex @sbmop{slot-value-using-class}
183 @findex @setf{@sbmop{slot-value-using-class}}
184 @findex @sbmop{slot-boundp-using-class}
185 specializations of the @code{new-value} argument to @code{(setf
186 sb-mop:slot-value-using-class)} are not allowed: all user-defined
187 methods must have a specializer of the class @code{t}.
188
189 This prohibition is motivated by a separation of layers: the
190 @code{slot-value-using-class} family of functions is intended for use in
191 implementing different and new slot allocation strategies, rather than
192 in performing application-level dispatching.  Additionally, with this
193 requirement, there is a one-to-one mapping between metaclass, class and
194 slot-definition-class tuples and effective methods of @code{(setf
195 slot-value-using-class)}, which permits optimization of @code{(setf
196 slot-value-using-class)}'s discriminating function in the same manner as
197 for @code{slot-value-using-class} and @code{slot-boundp-using-class}.
198
199 Note that application code may specialize on the @code{new-value}
200 argument of slot accessors.
201
202 @item
203 @findex @cl{defclass}
204 @findex @sbmop{ensure-class}
205 @findex @sbmop{ensure-class-using-class}
206 @findex @cl{find-class}
207 @findex @cl{class-name}
208 the class named by the @code{name} argument to @code{ensure-class}, if
209 any, is only redefined if it is the proper name of that class;
210 otherwise, a new class is created.
211
212 This is consistent with the description of @code{ensure-class} in AMOP
213 as the functional version of @code{defclass}, which has this behaviour;
214 however, it is not consistent with the weaker requirement in AMOP, which
215 states that any class found by @code{find-class}, no matter what its
216 @code{class-name}, is redefined.
217
218 @end itemize
219
220 In addition, SBCL supports extensions to the Metaobject protocol from
221 AMOP; at present, they are:
222
223 @itemize
224
225 @item
226 @findex @cl{defmethod}
227 @findex @cl{find-class}
228 @findex @sbmop{intern-eql-specializer}
229 @findex @sbpcl{make-method-specializers-form}
230 @findex @sbmop{make-method-lambda}
231 compile-time support for generating specializer metaobjects from
232 specializer names in @code{defmethod} forms is provided by the
233 @code{make-method-specializers-form} function, which returns a form
234 which, when evaluated in the lexical environment of the
235 @code{defmethod}, returns a list of specializer metaobjects.  This
236 operator suffers from similar restrictions to those affecting
237 @code{make-method-lambda}, namely that the generic function must be
238 defined when the @code{defmethod} form is expanded, so that the
239 correct method of @code{make-method-specializers-form} is invoked.
240 The system-provided method on @code{make-method-specializers-form}
241 generates a call to @code{find-class} for each symbol specializer
242 name, and a call to @code{intern-eql-specializer} for each @code{(eql
243 @var{x})} specializer name.
244
245 @item
246 @findex @cl{find-method}
247 @findex @sbpcl{parse-specializer-using-class}
248 @findex @sbpcl{unparse-specializer-using-class}
249 run-time support for converting between specializer names and
250 specializer metaobjects, mostly for the purposes of
251 @code{find-method}, is provided by
252 @code{parse-specializer-using-class} and
253 @code{unparse-specializer-using-class}, which dispatch on their first
254 argument, the generic function associated with a method with the given
255 specializer.  The system-provided methods on those methods convert
256 between classes and proper names and between lists of the form
257 @code{(eql @var{x})} and interned eql specializer objects.
258
259 @end itemize
260
261 @node  Support For Unix
262 @comment  node-name,  next,  previous,  up
263 @section Support For Unix
264
265 @menu
266 * Command-line arguments::
267 * Querying the process environment::
268 * Running external programs::
269 @end menu
270
271 @node Command-line arguments
272 @subsection Command-line arguments
273 @vindex @sbext{@earmuffs{posix-argv}}
274
275 The UNIX command line can be read from the variable
276 @code{sb-ext:*posix-argv*}.
277
278 @node Querying the process environment
279 @subsection Querying the process environment
280
281 The UNIX environment can be queried with the
282 @code{sb-ext:posix-getenv} function.
283
284 @include fun-sb-ext-posix-getenv.texinfo
285
286 @node Running external programs
287 @subsection Running external programs
288
289 External programs can be run with @code{sb-ext:run-program}.
290 @footnote{In SBCL versions prior to 1.0.13, @code{sb-ext:run-program}
291 searched for executables in a manner somewhat incompatible with other
292 languages.  As of this version, SBCL uses the system library routine
293 @code{execvp(3)}, and no longer contains the function,
294 @code{find-executable-in-search-path}, which implemented the old
295 search.  Users who need this function may find it
296 in @file{run-program.lisp} versions 1.67 and earlier in SBCL's CVS
297 repository here
298 @url{http://sbcl.cvs.sourceforge.net/sbcl/sbcl/src/code/run-program.lisp?view=log}. However,
299 we caution such users that this search routine finds executables that
300 system library routines do not.}
301
302 @include fun-sb-ext-run-program.texinfo
303
304 When @code{sb-ext:run-program} is called with @code{wait} equal to
305 NIL, an instance of class @var{sb-ext:process} is returned.  The
306 following functions are available for use with processes:
307
308 @include fun-sb-ext-process-p.texinfo
309
310 @include fun-sb-ext-process-input.texinfo
311
312 @include fun-sb-ext-process-output.texinfo
313
314 @include fun-sb-ext-process-error.texinfo
315
316 @include fun-sb-ext-process-alive-p.texinfo
317
318 @include fun-sb-ext-process-status.texinfo
319
320 @include fun-sb-ext-process-wait.texinfo
321
322 @include fun-sb-ext-process-exit-code.texinfo
323
324 @include fun-sb-ext-process-core-dumped.texinfo
325
326 @include fun-sb-ext-process-close.texinfo
327
328 @include fun-sb-ext-process-kill.texinfo
329
330 @node  Customization Hooks for Users
331 @comment  node-name,  next,  previous,  up
332 @section Customization Hooks for Users
333
334 The toplevel repl prompt may be customized, and the function
335 that reads user input may be replaced completely.
336 @c <!-- FIXME but I don't currently remember how -->
337
338 The behaviour of @code{require} when called with only one argument is
339 implementation-defined.  In SBCL, @code{require} behaves in the
340 following way:
341
342 @include fun-common-lisp-require.texinfo
343 @include var-sb-ext-star-module-provider-functions-star.texinfo
344
345 Although SBCL does not provide a resident editor, the @code{ed}
346 function can be customized to hook into user-provided editing
347 mechanisms as follows:
348
349 @include fun-common-lisp-ed.texinfo
350 @include var-sb-ext-star-ed-functions-star.texinfo
351
352 Conditions of type @code{warning} and @code{style-warning} are
353 sometimes signaled at runtime, especially during execution of Common
354 Lisp defining forms such as @code{defun}, @code{defmethod}, etc.  To
355 muffle these warnings at runtime, SBCL provides a variable
356 @code{sb-ext:*muffled-warnings*}:
357
358 @include var-sb-ext-star-muffled-warnings-star.texinfo
359
360 @node Tools To Help Developers
361 @comment  node-name,  next,  previous,  up
362 @section Tools To Help Developers
363 @findex @cl{trace}
364 @findex @cl{inspect}
365
366 SBCL provides a profiler and other extensions to the ANSI @code{trace}
367 facility.  For more information, see @ref{Macro common-lisp:trace}.
368
369 The debugger supports a number of options. Its documentation is
370 accessed by typing @kbd{help} at the debugger prompt. @xref{Debugger}.
371
372 Documentation for @code{inspect} is accessed by typing @kbd{help} at
373 the @code{inspect} prompt.
374
375 @node Resolution of Name Conflicts
376 @section Resolution of Name Conflicts
377 @tindex @sbext{name-conflict}
378 @findex @sbext{name-conflict-symbols}
379
380 The ANSI standard (section 11.1.1.2.5) requires that name conflicts in
381 packages be resolvable in favour of any of the conflicting symbols.  In
382 the interactive debugger, this is achieved by prompting for the symbol
383 in whose favour the conflict should be resolved; for programmatic use,
384 the @code{sb-ext:resolve-conflict} restart should be invoked with one
385 argument, which should be a member of the list returned by the condition
386 accessor @code{sb-ext:name-conflict-symbols}.
387
388 @node    Hash Table Extensions
389 @comment  node-name,  next,  previous,  up
390 @section Hash Table Extensions
391 @cindex Hash tables
392
393 Hash table extensions supported by SBCL are all controlled by keyword
394 arguments to @code{make-hash-table}.
395
396 @include fun-common-lisp-make-hash-table.texinfo
397
398 @include macro-sb-ext-define-hash-table-test.texinfo
399
400 @include macro-sb-ext-with-locked-hash-table.texinfo
401
402 @include fun-sb-ext-hash-table-synchronized-p.texinfo
403
404 @include fun-sb-ext-hash-table-weakness.texinfo
405
406 @node    Miscellaneous Extensions
407 @comment  node-name,  next,  previous,  up
408 @section Miscellaneous Extensions
409
410 @include fun-sb-ext-array-storage-vector.texinfo
411 @include fun-sb-ext-get-time-of-day.texinfo
412 @include fun-sb-ext-seed-random-state.texinfo
413
414 @node Stale Extensions
415 @comment  node-name,  next,  previous,  up
416 @section Stale Extensions
417
418 SBCL has inherited from CMUCL various hooks to allow the user to
419 tweak and monitor the garbage collection process. These are somewhat
420 stale code, and their interface might need to be cleaned up. If you
421 have urgent need of them, look at the code in @file{src/code/gc.lisp}
422 and bring it up on the developers' mailing list.
423
424 SBCL has various hooks inherited from CMUCL, like
425 @code{sb-ext:float-denormalized-p}, to allow a program to take
426 advantage of IEEE floating point arithmetic properties which aren't
427 conveniently or efficiently expressible using the ANSI standard. These
428 look good, and their interface looks good, but IEEE support is
429 slightly broken due to a stupid decision to remove some support for
430 infinities (because it wasn't in the ANSI spec and it didn't occur to
431 me that it was in the IEEE spec). If you need this stuff, take a look
432 at the code and bring it up on the developers' mailing
433 list.
434
435
436 @node  Efficiency Hacks
437 @comment  node-name,  next,  previous,  up
438 @section Efficiency Hacks
439
440 The @code{sb-ext:purify} function causes SBCL first to collect all
441 garbage, then to mark all uncollected objects as permanent, never again
442 attempting to collect them as garbage. This can cause a large increase
443 in efficiency when using a primitive garbage collector, or a more
444 moderate increase in efficiency when using a more sophisticated garbage
445 collector which is well suited to the program's memory usage pattern. It
446 also allows permanent code to be frozen at fixed addresses, a
447 precondition for using copy-on-write to share code between multiple Lisp
448 processes.  This is less important with modern generational garbage
449 collectors, but not all SBCL platforms use such a garbage collector.
450
451 @include fun-sb-ext-purify.texinfo
452
453 The @code{sb-ext:truly-the} special form declares the type of the
454 result of the operations, producing its argument; the declaration is
455 not checked. In short: don't use it.
456
457 @include special-operator-sb-ext-truly-the.texinfo
458
459 The @code{sb-ext:freeze-type} declaration declares that a
460 type will never change, which can make type testing
461 (@code{typep}, etc.) more efficient for structure types.