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