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