Fix grammar in lossage message
[sbcl.git] / doc / manual / beyond-ansi.texinfo
blob9f415048be25426e50681cbb0bf27b0f3e7fa77b
1 @node  Beyond the ANSI Standard
2 @comment  node-name,  next,  previous,  up
3 @chapter Beyond the ANSI Standard
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}.
9 @menu
10 * Reader Extensions::
11 * Package-Local Nicknames::
12 * Package Variance::
13 * Garbage Collection::
14 * Metaobject Protocol::
15 * Extensible Sequences::
16 * Support For Unix::
17 * Unicode Support::
18 * Customization Hooks for Users::
19 * Tools To Help Developers::
20 * Resolution of Name Conflicts::
21 * Hash Table Extensions::
22 * Random Number Generation::
23 * Miscellaneous Extensions::
24 * Stale Extensions::
25 * Efficiency Hacks::
26 @end menu
28 @node Reader Extensions
29 @comment  node-name,  next,  previous,  up
30 @section Reader Extensions
31 @cindex Reader Extensions
33 SBCL supports extended package prefix syntax, which allows specifying
34 an alternate package instead of @code{*package*} for the reader to use
35 as the default package for interning symbols:
37 @lisp
38 @var{package-name}::@var{form-with-interning-into-package}
39 @end lisp
41 Example:
43 @lisp
44   'foo::(bar quux zot) == '(foo::bar foo::quux foo::zot)
45 @end lisp
47 Doesn't alter @code{*package*}: if @code{foo::bar} would cause a
48 read-time package lock violation, so does @code{foo::(bar)}.
50 SBCL also extends the reader to normalize all symbols to Normalization
51 Form KC in builds with Unicode enabled. Whether symbols are normalized
52 is controlled by
54 @include fun-sb-ext-readtable-normalization.texinfo
56 Symbols created by
57 @findex @cl{intern}
58 @code{intern} and similar functions are not affected by this setting. If
59 @code{sb-ext:readtable-normalization} is @code{t}, symbols that are not
60 normalized are escaped during printing.
62 @node  Package-Local Nicknames
63 @comment  node-name,  next,  previous,  up
64 @section Package-Local Nicknames
65 @cindex Package-Local Nicknames
67 SBCL allows giving packages local nicknames: they allow short and
68 easy-to-use names to be used without fear of name conflict associated
69 with normal nicknames.
71 A local nickname is valid only when inside the package for which it
72 has been specified. Different packages can use same local nickname for
73 different global names, or different local nickname for same global
74 name.
76 Symbol @code{:package-local-nicknames} in @code{*features*} denotes the
77 support for this feature.
79 @findex @cl{defpackage}
80 @defmac @cl{defpackage} name [[option]]* @result{} package
82 Options are extended to include
84 @itemize
85 @item
86 @code{:local-nicknames (@var{local-nickname} @var{actual-package-name})*}
88 The package has the specified local nicknames for the corresponding
89 actual packages.
90 @end itemize
92 Example:
94 @lisp
95 (defpackage :bar (:intern "X"))
96 (defpackage :foo (:intern "X"))
97 (defpackage :quux (:use :cl) (:local-nicknames (:bar :foo) (:foo :bar)))
98 (find-symbol "X" :foo) ; => FOO::X
99 (find-symbol "X" :bar) ; => BAR::X
100 (let ((*package* (find-package :quux)))
101   (find-symbol "X" :foo))               ; => BAR::X
102 (let ((*package* (find-package :quux)))
103   (find-symbol "X" :bar))               ; => FOO::X
104 @end lisp
105 @end defmac
107 @include fun-sb-ext-package-local-nicknames.texinfo
108 @include fun-sb-ext-package-locally-nicknamed-by-list.texinfo
109 @include fun-sb-ext-add-package-local-nickname.texinfo
110 @include fun-sb-ext-remove-package-local-nickname.texinfo
112 @node  Package Variance
113 @comment  node-name,  next,  previous,  up
114 @section Package Variance
116 Common Lisp standard specifies that ``If the new definition is at
117 variance with the current state of that package, the consequences are
118 undefined;'' SBCL by default signals a full warning and retains as
119 much of the package state as possible.
121 This can be adjusted using @code{sb-ext:*on-package-variance*}:
123 @include var-sb-ext-star-on-package-variance-star.texinfo
125 @node  Garbage Collection
126 @comment  node-name,  next,  previous,  up
127 @section Garbage Collection
128 @cindex Garbage collection
130 SBCL provides additional garbage collection functionality not
131 specified by ANSI.
133 @include var-sb-ext-star-after-gc-hooks-star.texinfo
134 @include fun-sb-ext-gc.texinfo
136 @subsection Finalization
137 @cindex Finalization
139 Finalization allows code to be executed after an object has been
140 garbage collected. This is useful for example for releasing foreign
141 memory associated with a Lisp object.
143 @include fun-sb-ext-finalize.texinfo
144 @include fun-sb-ext-cancel-finalization.texinfo
146 @subsection Weak Pointers
147 @cindex Weak pointers
149 Weak pointers allow references to objects to be maintained without
150 keeping them from being garbage collected: useful for building caches
151 among other things.
153 Hash tables can also have weak keys and values: @pxref{Hash Table
154 Extensions}.
156 @include fun-sb-ext-make-weak-pointer.texinfo
157 @include fun-sb-ext-weak-pointer-value.texinfo
159 @subsection Introspection and Tuning
161 @include var-sb-ext-star-gc-run-time-star.texinfo
162 @include fun-sb-ext-bytes-consed-between-gcs.texinfo
163 @include fun-sb-ext-dynamic-space-size.texinfo
164 @include fun-sb-ext-get-bytes-consed.texinfo
165 @include fun-sb-ext-gc-logfile.texinfo
166 @include fun-sb-ext-generation-average-age.texinfo
167 @include fun-sb-ext-generation-bytes-allocated.texinfo
168 @include fun-sb-ext-generation-bytes-consed-between-gcs.texinfo
169 @include fun-sb-ext-generation-minimum-age-before-gc.texinfo
170 @include fun-sb-ext-generation-number-of-gcs-before-promotion.texinfo
171 @include fun-sb-ext-generation-number-of-gcs.texinfo
173 @node Metaobject Protocol
174 @comment  node-name,  next,  previous,  up
175 @section Metaobject Protocol
177 @subsection AMOP Compatibility of Metaobject Protocol
179 SBCL supports a metaobject protocol which is intended to be compatible
180 with AMOP; present exceptions to this (as distinct from current bugs)
181 are:
183 @itemize
185 @item
186 @findex @sbmop{compute-effective-method}
187 @code{compute-effective-method} only returns one value, not two.
189 There is no record of what the second return value was meant to
190 indicate, and apparently no clients for it.
192 @item
193 @tindex @cl{generic-function}
194 @tindex @cl{standard-generic-function}
195 @tindex @sbmop{funcallable-standard-object}
196 @tindex @cl{standard-object}
197 @tindex @cl{function}
198 The direct superclasses of @code{funcallable-standard-object} are
199 @code{(function standard-object)}, not @code{(standard-object function)}.
201 This is to ensure that the @code{standard-object} class is the last of
202 the standardized classes before @code{t} appearing in the class
203 precedence list of @code{generic-function} and
204 @code{standard-generic-function}, as required by section 1.4.4.5 of the
205 ANSI specification.
207 @item
208 @findex @cl{ensure-generic-function}
209 @findex @sbmop{generic-function-declarations}
210 the arguments @code{:declare} and @code{:declarations} to
211 @code{ensure-generic-function} are both accepted, with the leftmost
212 argument defining the declarations to be stored and returned by
213 @code{generic-function-declarations}.
215 Where AMOP specifies @code{:declarations} as the keyword argument to
216 @code{ensure-generic-function}, the Common Lisp standard specifies
217 @code{:declare}.  Portable code should use @code{:declare}.
219 @item
220 @findex @sbmop{validate-superclass}
221 @findex @sbmop{finalize-inheritance}
222 @tindex @cl{standard-class}
223 @tindex @sbmop{funcallable-standard-class}
224 @tindex @cl{function}
225 @findex @sbmop{class-prototype}
226 although SBCL obeys the requirement in AMOP that
227 @code{validate-superclass} should treat @code{standard-class} and
228 @code{funcallable-standard-class} as compatible metaclasses, we
229 impose an additional requirement at class finalization time: a class
230 of metaclass @code{funcallable-standard-class} must have
231 @code{function} in its superclasses, and a class of metaclass
232 @code{standard-class} must not.
234 @findex @cl{typep}
235 @findex @cl{class-of}
236 @findex @cl{subtypep}
237 After a class has been finalized, it is associated with a class
238 prototype which is accessible by a standard mop function
239 @code{class-prototype}.  The user can then ask whether this object is a
240 @code{function} or not in several different ways: whether it is a
241 function according to @code{typep}; whether its @code{class-of} is
242 @code{subtypep} @code{function}, or whether @code{function} appears in
243 the superclasses of the class.  The additional consistency requirement
244 comes from the desire to make all of these answers the same.
246 The following class definitions are bad, and will lead to errors
247 either immediately or if an instance is created:
248 @lisp
249 (defclass bad-object (funcallable-standard-object)
250   ()
251   (:metaclass standard-class))
252 @end lisp
253 @lisp
254 (defclass bad-funcallable-object (standard-object)
255   ()
256   (:metaclass funcallable-standard-class))
257 @end lisp
258 The following definition is acceptable:
259 @lisp
260 (defclass mixin ()
261   ((slot :initarg slot)))
262 (defclass funcallable-object (funcallable-standard-object mixin)
263   ()
264   (:metaclass funcallable-standard-class))
265 @end lisp
266 and leads to a class whose instances are funcallable and have one slot.
268 @tindex @sbmop{funcallable-standard-object}
269 Note that this requirement also applies to the class
270 @code{funcallable-standard-object}, which has metaclass
271 @code{funcallable-standard-class} rather than
272 @code{standard-class} as AMOP specifies.
274 @item
275 the requirement that ``No portable class @math{C_p} may inherit, by
276 virtue of being a direct or indirect subclass of a specified class, any
277 slot for which the name is a symbol accessible in the
278 @code{common-lisp-user} package or exported by any package defined in
279 the ANSI Common Lisp standard.'' is interpreted to mean that the
280 standardized classes themselves should not have slots named by external
281 symbols of public packages.
283 The rationale behind the restriction is likely to be similar to the ANSI
284 Common Lisp restriction on defining functions, variables and types named
285 by symbols in the Common Lisp package: preventing two independent pieces
286 of software from colliding with each other.
288 @item
289 @findex @sbmop{slot-value-using-class}
290 @findex @setf{@sbmop{slot-value-using-class}}
291 @findex @sbmop{slot-boundp-using-class}
292 specializations of the @code{new-value} argument to @code{(setf
293 slot-value-using-class)} are not allowed: all user-defined methods must
294 have a specializer of the class @code{t}.
296 This prohibition is motivated by a separation of layers: the
297 @code{slot-value-using-class} family of functions is intended for use in
298 implementing different and new slot allocation strategies, rather than
299 in performing application-level dispatching.  Additionally, with this
300 requirement, there is a one-to-one mapping between metaclass, class and
301 slot-definition-class tuples and effective methods of @code{(setf
302 slot-value-using-class)}, which permits optimization of @code{(setf
303 slot-value-using-class)}'s discriminating function in the same manner as
304 for @code{slot-value-using-class} and @code{slot-boundp-using-class}.
306 Note that application code may specialize on the @code{new-value}
307 argument of slot accessors.
309 @item
310 @findex @cl{defclass}
311 @findex @sbmop{ensure-class}
312 @findex @sbmop{ensure-class-using-class}
313 @findex @cl{find-class}
314 @findex @cl{class-name}
315 the class named by the @code{name} argument to @code{ensure-class}, if
316 any, is only redefined if it is the proper name of that class;
317 otherwise, a new class is created.
319 This is consistent with the description of @code{ensure-class} in AMOP
320 as the functional version of @code{defclass}, which has this behaviour;
321 however, it is not consistent with the weaker requirement in AMOP, which
322 states that any class found by @code{find-class}, no matter what its
323 @code{class-name}, is redefined.
325 @item
326 @findex @sbmop{slot-definition-name}
327 @tindex @cl{structure-class}
328 @findex @cl{defstruct}
329 an error is not signaled in the case of the @code{:name} initialization
330 argument for @code{slot-definition} objects being a constant, when the
331 slot definition is of type @code{structure-slot-definition} (i.e. it is
332 associated with a class of type @code{structure-class}).
334 This allows code which uses constant names for structure slots to
335 continue working as specified in ANSI, while enforcing the constraint
336 for all other types of slot.
338 @item
339 @tindex @cl{t}
340 @tindex @cl{built-in-class}
341 @findex @sbmop{validate-superclass}
342 @findex @cl{defclass}
343 the class named @code{t} is not an instance of the @code{built-in-class}
344 metaclass.
346 AMOP specifies, in the ``Inheritance Structure of Metaobject Classes''
347 section, that the class named @code{t} should be an instance of
348 @code{built-in-class}.  However, it also specifies that
349 @code{validate-superclass} should return true (indicating that a direct
350 superclass relationship is permissible) if the second argument is the
351 class named @code{t}.  Also, ANSI specifies that classes with metaclass
352 @code{built-in-class} may not be subclassed using @code{defclass}, and
353 also that the class named @code{t} is the universal superclass,
354 inconsistent with it being a @code{built-in-class}.
356 @end itemize
358 @subsection Metaobject Protocol Extensions
360 In addition, SBCL supports extensions to the Metaobject protocol from
361 AMOP; at present, they are:
363 @itemize
365 @item
366 @findex @cl{defmethod}
367 @findex @cl{find-class}
368 @findex @sbmop{intern-eql-specializer}
369 @findex @sbpcl{make-method-specializers-form}
370 @findex @sbmop{make-method-lambda}
371 compile-time support for generating specializer metaobjects from
372 specializer names in @code{defmethod} forms is provided by the
373 @code{make-method-specializers-form} function, which returns a form
374 which, when evaluated in the lexical environment of the
375 @code{defmethod}, returns a list of specializer metaobjects.  This
376 operator suffers from similar restrictions to those affecting
377 @code{make-method-lambda}, namely that the generic function must be
378 defined when the @code{defmethod} form is expanded, so that the
379 correct method of @code{make-method-specializers-form} is invoked.
380 The system-provided method on @code{make-method-specializers-form}
381 generates a call to @code{find-class} for each symbol specializer
382 name, and a call to @code{intern-eql-specializer} for each @code{(eql
383 @var{x})} specializer name.
385 @item
386 @findex @cl{find-method}
387 @findex @sbpcl{parse-specializer-using-class}
388 @findex @sbpcl{unparse-specializer-using-class}
389 run-time support for converting between specializer names and
390 specializer metaobjects, mostly for the purposes of
391 @code{find-method}, is provided by
392 @code{parse-specializer-using-class} and
393 @code{unparse-specializer-using-class}, which dispatch on their first
394 argument, the generic function associated with a method with the given
395 specializer.  The system-provided methods on those methods convert
396 between classes and proper names and between lists of the form
397 @code{(eql @var{x})} and interned eql specializer objects.
399 @item
400 @vindex @sbpcl{+slot-unbound+}
401 @findex @sbmop{standard-instance-access}
402 @findex @sbmop{funcallable-standard-instance-access}
403 distinguishing unbound instance allocated slots from bound ones when
404 using @code{standard-instance-access} and
405 @code{funcallable-standard-instance-access} is possible by comparison
406 to the constant @code{+slot-unbound+}.
408 @end itemize
410 @node Extensible Sequences
411 @comment  node-name,  next,  previous,  up
412 @section Extensible Sequences
414 @menu
415 * Iterator Protocol::
416 * Simple Iterator Protocol::
417 @end menu
419 ANSI Common Lisp has a class @code{sequence} with subclasses @code{list} and
420 @code{vector} on which the ``sequence functions'' like @code{find},
421 @code{subseq}, etc. operate. As an extension to the ANSI specification,
422 SBCL allows additional subclasses of @code{sequence} to be defined
423 @footnote{A motivation, rationale and additional examples for the design
424 of this extension can be found in the paper @cite{Rhodes, Christophe
425 (2007): User-extensible sequences in Common Lisp} available for download
427 @url{http://www.doc.gold.ac.uk/~mas01cr/papers/ilc2007/sequences-20070301.pdf}.}.
428 @tindex @cl{sequence}
429 @tindex @cl{vector}
430 @findex @cl{find}
431 @findex @cl{subseq}
433 Users of this extension just make instances of @code{sequence} subclasses
434 and transparently operate on them using sequence functions:
435 @lisp
436 (coerce (subseq (make-instance 'my-sequence) 5 10) 'list)
437 @end lisp
438 From this perspective, no distinction between builtin and user-defined
439 @code{sequence} subclasses should be necessary.
440 @findex @cl{coerce}
441 @findex @cl{subseq}
442 @findex @cl{make-instance}
443 @tindex @cl{list}
445 Providers of the extension, that is of user-defined @code{sequence}
446 subclasses, have to adhere to a ``sequence protocol'' which consists of
447 a set of generic functions in the @code{sequence} package.
449 A minimal @code{sequence} subclass has to specify @code{standard-object} and
450 @code{sequence} as its superclasses and has to be the specializer of the
451 @code{sequence} parameter of methods on at least the following generic
452 functions:
453 @tindex @cl{sequence}
454 @tindex @cl{standard-object}
456 @include fun-sb-sequence-length.texinfo
457 @include fun-sb-sequence-elt.texinfo
458 @include fun-sb-sequence-setf-elt.texinfo
459 @include fun-sb-sequence-adjust-sequence.texinfo
460 @include fun-sb-sequence-make-sequence-like.texinfo
462 @code{make-sequence-like} is needed for functions returning
463 freshly-allocated sequences such as @code{subseq} or
464 @code{copy-seq}. @code{adjust-sequence} is needed for functions which
465 destructively modify their arguments such as @code{delete}. In fact, all
466 other sequence functions can be implemented in terms of the above
467 functions and actually are, if no additional methods are
468 defined. However, relying on these generic implementations, in
469 particular not implementing the iterator protocol can incur a high
470 performance penalty @xref{Iterator Protocol}.
471 @tindex @cl{sequence}
472 @findex @sequence{make-sequence-like}
473 @findex @cl{subseq}
474 @findex @cl{copy-seq}
475 @findex @sequence{adjust-sequence}
477 When the sequence protocol is only partially implemented for a given
478 @code{sequence} subclass, an attempt to apply one of the missing
479 operations to instances of that class signals the following condition:
481 @include condition-sb-sequence-protocol-unimplemented.texinfo
483 In addition to the mandatory functions above, methods on the sequence
484 functions listed below can be defined.
486 There are two noteworthy irregularities:
487 @itemize
488 @item
489 The function @code{sb-sequence:emptyp} does not have a counterpart in
490 the @code{cl} package. It is intended to be used instead of
491 @code{length} when working with lazy or infinite sequences.
493 @item
494 The functions @code{map}, @code{concatenate} and @code{merge} receive a
495 type designator specifying the type of the constructed sequence as their
496 first argument. However, the corresponding generic functions
497 @code{sb-sequence:map}, @code{sb-sequence:concatenate} and
498 @code{sb-sequence:merge} receive a prototype instance of the requested
499 @code{sequence} subclass instead.
500 @end itemize
502 @include fun-sb-sequence-emptyp.texinfo
504 @itemize
505 @item
506 @code{sb-sequence:count}, @code{sb-sequence:count-if}, @code{sb-sequence:count-if-not}
508 @item
509 @code{sb-sequence:find}, @code{sb-sequence:find-if}, @code{sb-sequence:find-if-not}
511 @item
512 @code{sb-sequence:position}, @code{sb-sequence:position-if}, @code{sb-sequence:position-if-not}
514 @item
515 @code{sb-sequence:subseq}
517 @item
518 @code{sb-sequence:copy-seq}
520 @item
521 @code{sb-sequence:fill}
523 @item
524 @include fun-sb-sequence-map.texinfo
526 @item
527 @code{sb-sequence:nsubstitute}, @code{sb-sequence:nsubstitute-if},
528 @code{sb-sequence:nsubstitute-if-not}, @code{sb-sequence:substitute},
529 @code{sb-sequence:substitute-if}, @code{sb-sequence:substitute-if-not}
531 @item
532 @code{sb-sequence:replace}
534 @item
535 @code{sb-sequence:nreverse}, @code{sb-sequence:reverse}
537 @item
538 @include fun-sb-sequence-concatenate.texinfo
540 @item
541 @code{sb-sequence:reduce}
543 @item
544 @code{sb-sequence:mismatch}
546 @item
547 @code{sb-sequence:search}
549 @item
550 @code{sb-sequence:delete}, @code{sb-sequence:delete-if}, @code{sb-sequence:delete-if-not},
551 @code{sb-sequence:remove}, @code{sb-sequence:remove-if}, @code{sb-sequence:remove-if-not},
553 @item
554 @code{sb-sequence:delete-duplicates}, @code{sb-sequence:remove-duplicates}
556 @item
557 @code{sb-sequence:sort}, @code{sb-sequence:stable-sort}
559 @item
560 @include fun-sb-sequence-merge.texinfo
561 @end itemize
563 In the spirit of @code{dolist}, generic sequences can be traversed using
564 the macro
565 @findex @cl{dolist}
567 @include macro-sb-sequence-dosequence.texinfo
569 @node Iterator Protocol
570 @comment  node-name,  next,  previous,  up
571 @subsection Iterator Protocol
573 The iterator protocol allows subsequently accessing some or all elements
574 of a sequence in forward or reverse direction. Users first call
575 @code{make-sequence-iterator} to create an iteration state and
576 receive functions to query and mutate it. These functions allow, among
577 other things, moving to, retrieving or modifying elements of the
578 sequence. An iteration state consists of a state object, a limit object,
579 a from-end indicator and the following six functions to query or mutate
580 this state:
581 @findex @sequence{make-sequence-iterator}
582 @deffn {Function} @code{step function} sequence iterator from-end
583 Moves the iterator one position forward or backward in the associated
584 sequence depending on the iteration direction.
585 @end deffn
586 @deffn {Function} @code{endp function} sequence iterator limit from-end
587 Returns non-@code{nil} when the iterator has reached the end of the
588 associated sequence with respect to the iteration direction.
589 @end deffn
590 @deffn {Function} @code{element function} sequence iterator
591 Returns the sequence element associated to the current position of the
592 iteration.
593 @end deffn
594 @deffn {Function} @code{setf element function} new-value sequence iterator
595 Destructively modifies the associates sequence by replacing the sequence
596 element associated to the current iteration position with a new value.
597 @end deffn
598 @deffn {Function} @code{index function} sequence iterator
599 Returns the position of the iteration in the associated sequence.
600 @end deffn
601 @deffn {Function} @code{copy function} sequence iterator
602 Returns a copy of the iteration state which can be mutated independently
603 of the copied iteration state.
604 @end deffn
606 An iterator is created by calling:
608 @include fun-sb-sequence-make-sequence-iterator.texinfo
610 Note that @code{make-sequence-iterator} calls
611 @code{make-simple-sequence-iterator} when there is no specialized
612 method for a particular @code{sequence} subclass. @xref{Simple Iterator
613 Protocol}.
614 @findex @sequence{make-sequence-iterator}
615 @findex @sequence{make-simple-sequence-iterator}
616 @tindex @cl{sequence}
618 The following convenience macros simplify traversing sequences using
619 iterators:
621 @include macro-sb-sequence-with-sequence-iterator.texinfo
622 @include macro-sb-sequence-with-sequence-iterator-functions.texinfo
624 @node Simple Iterator Protocol
625 @comment  node-name,  next,  previous,  up
626 @subsection Simple Iterator Protocol
628 For cases in which the full flexibility and performance of the general
629 sequence iterator protocol is not required, there is a simplified
630 sequence iterator protocol consisting of a few generic functions which
631 can be specialized for iterator classes:
633 @include fun-sb-sequence-iterator-step.texinfo
634 @include fun-sb-sequence-iterator-endp.texinfo
635 @include fun-sb-sequence-iterator-element.texinfo
636 @include fun-sb-sequence-setf-iterator-element.texinfo
637 @include fun-sb-sequence-iterator-index.texinfo
638 @include fun-sb-sequence-iterator-copy.texinfo
640 Iterator objects implementing the above simple iteration protocol are
641 created by calling the following generic function:
643 @include fun-sb-sequence-make-simple-sequence-iterator.texinfo
645 @node  Support For Unix
646 @comment  node-name,  next,  previous,  up
647 @section Support For Unix
649 @menu
650 * Command-line arguments::
651 * Querying the process environment::
652 * Running external programs::
653 @end menu
655 @node Command-line arguments
656 @subsection Command-line arguments
657 @vindex @sbext{@earmuffs{posix-argv}}
659 The UNIX command line can be read from the variable
660 @code{sb-ext:*posix-argv*}.
662 @node Querying the process environment
663 @subsection Querying the process environment
665 The UNIX environment can be queried with the
666 @code{sb-ext:posix-getenv} function.
668 @include fun-sb-ext-posix-getenv.texinfo
670 @node Running external programs
671 @subsection Running external programs
673 External programs can be run with @code{sb-ext:run-program}.
674 @footnote{In SBCL versions prior to 1.0.13, @code{sb-ext:run-program}
675 searched for executables in a manner somewhat incompatible with other
676 languages.  As of this version, SBCL uses the system library routine
677 @code{execvp(3)}, and no longer contains the function,
678 @code{find-executable-in-search-path}, which implemented the old
679 search.  Users who need this function may find it
680 in @file{run-program.lisp} versions 1.67 and earlier in SBCL's CVS
681 repository here
682 @url{http://sbcl.cvs.sourceforge.net/sbcl/sbcl/src/code/run-program.lisp?view=log}. However,
683 we caution such users that this search routine finds executables that
684 system library routines do not.}
686 @include fun-sb-ext-run-program.texinfo
688 When @code{sb-ext:run-program} is called with @code{wait} equal to
689 NIL, an instance of class @var{sb-ext:process} is returned.  The
690 following functions are available for use with processes:
692 @include fun-sb-ext-process-p.texinfo
694 @include fun-sb-ext-process-input.texinfo
696 @include fun-sb-ext-process-output.texinfo
698 @include fun-sb-ext-process-error.texinfo
700 @include fun-sb-ext-process-alive-p.texinfo
702 @include fun-sb-ext-process-status.texinfo
704 @include fun-sb-ext-process-wait.texinfo
706 @include fun-sb-ext-process-exit-code.texinfo
708 @include fun-sb-ext-process-core-dumped.texinfo
710 @include fun-sb-ext-process-close.texinfo
712 @include fun-sb-ext-process-kill.texinfo
714 @node  Unicode Support
715 @comment  node-name,  next,  previous,  up
716 @section Unicode Support
718 SBCL provides support for working with Unicode text and querying the
719 standard Unicode database for information about individual codepoints.
720 Unicode-related functions are located in the @code{sb-unicode} package.
722 SBCL also extends ANSI character literal syntax to support Unicode
723 codepoints. You can either specify a character by its Unicode name, with
724 spaces replaced by underscores, if a unique name exists @footnote{Please
725 note that the codepoint U+1F5CF (PAGE) introduced in Unicode 7.0 is
726 named @code{UNICODE_PAGE}, since the name ``Page'' is required to be
727 assigned to form-feed (U+0C) by the ANSI standard.} or
728 by giving its hexadecimal codepoint preceded by a ``U'', an optional
729 ``+'', and an arbitrary number of leading zeros. You may also input the
730 character directly into your source code if it can be encoded in your
731 file. If a character had an assigned name in Unicode 1.0 that was
732 distinct from its current name, you may also use that name (with spaces
733 replaced by underscores) to specify the character, unless the name is
734 already associated with a codepoint in the latest Unicode standard (such
735 as ``BELL'').
737 For example, you can specify the codepoint U+00E1 (``Latin
738 Small Letter A With Acute'') as
739 @itemize @bullet
740 @item
741 @code{#\LATIN_SMALL_LETTER_A_WITH_ACUTE}
742 @item
743 @code{#\LATIN_SMALL_LETTER_A_ACUTE}
744 @item
745 @code{#\á} assuming a Unicode source file
746 @item
747 @code{#\U00E1}
748 @item
749 @code{#\UE1}
750 @item
751 @code{#\U+00E1}
752 @end itemize
754 @subsection Unicode property access
756 The following functions can be used to find information about a Unicode
757 codepoint.
759 @include fun-sb-unicode-general-category.texinfo
761 @include fun-sb-unicode-bidi-class.texinfo
763 @include fun-sb-unicode-combining-class.texinfo
765 @include fun-sb-unicode-decimal-value.texinfo
767 @include fun-sb-unicode-digit-value.texinfo
769 @include fun-sb-unicode-numeric-value.texinfo
771 @include fun-sb-unicode-mirrored-p.texinfo
773 @include fun-sb-unicode-bidi-mirroring-glyph.texinfo
775 @include fun-sb-unicode-age.texinfo
777 @include fun-sb-unicode-hangul-syllable-type.texinfo
779 @include fun-sb-unicode-east-asian-width.texinfo
781 @include fun-sb-unicode-script.texinfo
783 @include fun-sb-unicode-char-block.texinfo
785 @include fun-sb-unicode-unicode-1-name.texinfo
787 @include fun-sb-unicode-proplist-p.texinfo
789 @include fun-sb-unicode-uppercase-p.texinfo
791 @include fun-sb-unicode-lowercase-p.texinfo
793 @include fun-sb-unicode-cased-p.texinfo
795 @include fun-sb-unicode-case-ignorable-p.texinfo
797 @include fun-sb-unicode-alphabetic-p.texinfo
799 @include fun-sb-unicode-ideographic-p.texinfo
801 @include fun-sb-unicode-math-p.texinfo
803 @include fun-sb-unicode-whitespace-p.texinfo
805 @include fun-sb-unicode-soft-dotted-p.texinfo
807 @include fun-sb-unicode-hex-digit-p.texinfo
809 @include fun-sb-unicode-default-ignorable-p.texinfo
811 @include fun-sb-unicode-grapheme-break-class.texinfo
813 @include fun-sb-unicode-word-break-class.texinfo
815 @include fun-sb-unicode-sentence-break-class.texinfo
817 @include fun-sb-unicode-line-break-class.texinfo
819 @subsection String operations
821 SBCL can normalize strings using:
823 @include fun-sb-unicode-normalize-string.texinfo
825 @include fun-sb-unicode-normalized-p.texinfo
827 SBCL implements the full range of Unicode case operations with the
828 functions
830 @include fun-sb-unicode-uppercase.texinfo
832 @include fun-sb-unicode-lowercase.texinfo
834 @include fun-sb-unicode-titlecase.texinfo
836 @include fun-sb-unicode-casefold.texinfo
838 It also extends standard Common Lisp case functions such as
839 @findex @cl{string-upcase}
840 @code{string-upcase} and
841 @findex @cl{char-downcase}
842 @code{string-downcase} to support a subset of Unicode's casing behavior.
843 Specifically, a character is
844 @findex @cl{both-case-p}
845 @code{both-case-p} if its case mapping in Unicode is one-to-one and
846 invertable.
848 The @code{sb-unicode} package also provides functions for
849 collating/sorting strings according to the Unicode Collation Algorithm.
851 @include fun-sb-unicode-unicode-lt.texinfo
853 @include fun-sb-unicode-unicode=.texinfo
855 @include fun-sb-unicode-unicode-equal.texinfo
857 @include fun-sb-unicode-unicode-lt=.texinfo
859 @include fun-sb-unicode-unicode-gt.texinfo
861 @include fun-sb-unicode-unicode-gt=.texinfo
863 The following functions are provided for detecting visually confusable strings:
865 @include fun-sb-unicode-confusable-p.texinfo
867 @subsection Breaking strings
869 The @code{sb-unicode} package includes several functions for breaking a
870 Unicode string into useful parts.
872 @include fun-sb-unicode-graphemes.texinfo
874 @include fun-sb-unicode-words.texinfo
876 @include fun-sb-unicode-sentences.texinfo
878 @include fun-sb-unicode-lines.texinfo
880 @node  Customization Hooks for Users
881 @comment  node-name,  next,  previous,  up
882 @section Customization Hooks for Users
884 The toplevel repl prompt may be customized, and the function
885 that reads user input may be replaced completely.
886 @c <!-- FIXME but I don't currently remember how -->
888 The behaviour of @code{require} when called with only one argument is
889 implementation-defined.  In SBCL, @code{require} behaves in the
890 following way:
892 @include fun-common-lisp-require.texinfo
893 @include var-sb-ext-star-module-provider-functions-star.texinfo
895 Although SBCL does not provide a resident editor, the @code{ed}
896 function can be customized to hook into user-provided editing
897 mechanisms as follows:
899 @include fun-common-lisp-ed.texinfo
900 @include var-sb-ext-star-ed-functions-star.texinfo
902 Conditions of type @code{warning} and @code{style-warning} are
903 sometimes signaled at runtime, especially during execution of Common
904 Lisp defining forms such as @code{defun}, @code{defmethod}, etc.  To
905 muffle these warnings at runtime, SBCL provides a variable
906 @code{sb-ext:*muffled-warnings*}:
908 @include var-sb-ext-star-muffled-warnings-star.texinfo
910 @node Tools To Help Developers
911 @comment  node-name,  next,  previous,  up
912 @section Tools To Help Developers
913 @findex @cl{trace}
914 @findex @cl{inspect}
916 SBCL provides a profiler and other extensions to the ANSI @code{trace}
917 facility.  For more information, see @ref{Macro common-lisp:trace}.
919 The debugger supports a number of options. Its documentation is
920 accessed by typing @kbd{help} at the debugger prompt. @xref{Debugger}.
922 Documentation for @code{inspect} is accessed by typing @kbd{help} at
923 the @code{inspect} prompt.
925 @node Resolution of Name Conflicts
926 @section Resolution of Name Conflicts
927 @tindex @sbext{name-conflict}
928 @findex @sbext{name-conflict-symbols}
930 The ANSI standard (section 11.1.1.2.5) requires that name conflicts in
931 packages be resolvable in favour of any of the conflicting symbols.  In
932 the interactive debugger, this is achieved by prompting for the symbol
933 in whose favour the conflict should be resolved; for programmatic use,
934 the @code{sb-ext:resolve-conflict} restart should be invoked with one
935 argument, which should be a member of the list returned by the condition
936 accessor @code{sb-ext:name-conflict-symbols}.
938 @node    Hash Table Extensions
939 @comment  node-name,  next,  previous,  up
940 @section Hash Table Extensions
941 @cindex Hash tables
943 Hash table extensions supported by SBCL are all controlled by keyword
944 arguments to @code{make-hash-table}.
946 @include fun-common-lisp-make-hash-table.texinfo
948 @include macro-sb-ext-define-hash-table-test.texinfo
950 @include macro-sb-ext-with-locked-hash-table.texinfo
952 @include fun-sb-ext-hash-table-synchronized-p.texinfo
954 @include fun-sb-ext-hash-table-weakness.texinfo
956 @node    Random Number Generation
957 @comment  node-name,  next,  previous,  up
958 @section Random Number Generation
959 @cindex Random Number Generation
961 The initial value of @code{*random-state*} is the same each time SBCL
962 is started. This makes it possible for user code to obtain repeatable
963 pseudo random numbers using only standard-provided functionality. See
964 @code{seed-random-state} below for an SBCL extension that allows to
965 seed the random number generator from given data for an additional
966 possibility to achieve this. Non-repeatable random numbers can always
967 be obtained using @code{(make-random-state t)}.
969 The sequence of numbers produced by repeated calls to @code{random}
970 starting with the same random state and using the same sequence of
971 @code{limit} arguments is guaranteed to be reproducible only in the
972 same version of SBCL on the same platform, using the same code under
973 the same evaluator mode and compiler optimization qualities. Just two
974 examples of differences that may occur otherwise: calls to
975 @code{random} can be compiled differently depending on how much is
976 known about the @code{limit} argument at compile time, yielding
977 different results even if called with the same argument at run time,
978 and the results can differ depending on the machine's word size, for
979 example for limits that are fixnums under 64-bit word size but bignums
980 under 32-bit word size.
982 @include fun-sb-ext-seed-random-state.texinfo
984 Some notes on random floats: The standard doesn't prescribe a specific
985 method of generating random floats. The following paragraph describes
986 SBCL's current implementation and should be taken purely informational,
987 that is, user code should not depend on any of its specific properties.
988 The method used has been chosen because it is common, conceptually
989 simple and fast.
991 To generate random floats, SBCL evaluates code that has an equivalent
992 effect as
993 @lisp
994 (* limit
995    (float (/ (random (expt 2 23)) (expt 2 23)) 1.0f0))
996 @end lisp
997 (for single-floats) and correspondingly (with @code{52} and
998 @code{1.0d0} instead of @code{23} and @code{1.0f0}) for double-floats.
999 Note especially that this means that zero is a possible return value
1000 occurring with probability @code{(expt 2 -23)} respectively
1001 @code{(expt 2 -52)}. Also note that there exist twice as many
1002 equidistant floats between 0 and 1 as are generated. For example, the
1003 largest number that @code{(random 1.0f0)} ever returns is
1004 @code{(float (/ (1- (expt 2 23)) (expt 2 23)) 1.0f0)} while
1005 @code{(float (/ (1- (expt 2 24)) (expt 2 24)) 1.0f0)} is the
1006 largest single-float less than 1. This is a side effect of the fact
1007 that the implementation uses the fastest possible conversion from bits
1008 to floats.
1010 SBCL currently uses the Mersenne Twister as its random number
1011 generator, specifically the 32-bit version under both 32- and 64-bit
1012 word size. The seeding algorithm has been improved several times by
1013 the authors of the Mersenne Twister; SBCL uses the third version
1014 (from 2002) which is still the most recent as of June 2012. The
1015 implementation has been tested to provide output identical to the
1016 recommended C implementation.
1018 While the Mersenne Twister generates random numbers of much better
1019 statistical quality than other widely used generators, it uses only
1020 linear operations modulo 2 and thus fails some statistical
1021 tests@footnote{See chapter 7 "Testing widely used RNGs" in
1022 @cite{TestU01: A C Library for Empirical Testing of Random Number
1023 Generators} by Pierre L'Ecuyer and Richard Simard, ACM Transactions on
1024 Mathematical Software, Vol. 33, article 22, 2007.}.
1025 For example, the distribution of ranks of (sufficiently large) random
1026 binary matrices is much distorted compared to the theoretically
1027 expected one when the matrices are generated by the Mersenne Twister.
1028 Thus, applications that are sensitive to this aspect should use a
1029 different type of generator.
1031 @node    Miscellaneous Extensions
1032 @comment  node-name,  next,  previous,  up
1033 @section Miscellaneous Extensions
1035 @include fun-sb-ext-array-storage-vector.texinfo
1036 @include fun-sb-ext-delete-directory.texinfo
1037 @include fun-sb-ext-get-time-of-day.texinfo
1038 @include macro-sb-ext-wait-for.texinfo
1039 @include fun-sb-ext-assert-version-gt=.texinfo
1041 @node Stale Extensions
1042 @comment  node-name,  next,  previous,  up
1043 @section Stale Extensions
1045 SBCL has inherited from CMUCL various hooks to allow the user to
1046 tweak and monitor the garbage collection process. These are somewhat
1047 stale code, and their interface might need to be cleaned up. If you
1048 have urgent need of them, look at the code in @file{src/code/gc.lisp}
1049 and bring it up on the developers' mailing list.
1051 SBCL has various hooks inherited from CMUCL, like
1052 @code{sb-ext:float-denormalized-p}, to allow a program to take
1053 advantage of IEEE floating point arithmetic properties which aren't
1054 conveniently or efficiently expressible using the ANSI standard. These
1055 look good, and their interface looks good, but IEEE support is
1056 slightly broken due to a stupid decision to remove some support for
1057 infinities (because it wasn't in the ANSI spec and it didn't occur to
1058 me that it was in the IEEE spec). If you need this stuff, take a look
1059 at the code and bring it up on the developers' mailing
1060 list.
1063 @node  Efficiency Hacks
1064 @comment  node-name,  next,  previous,  up
1065 @section Efficiency Hacks
1067 The @code{sb-ext:purify} function causes SBCL first to collect all
1068 garbage, then to mark all uncollected objects as permanent, never again
1069 attempting to collect them as garbage. This can cause a large increase
1070 in efficiency when using a primitive garbage collector, or a more
1071 moderate increase in efficiency when using a more sophisticated garbage
1072 collector which is well suited to the program's memory usage pattern. It
1073 also allows permanent code to be frozen at fixed addresses, a
1074 precondition for using copy-on-write to share code between multiple Lisp
1075 processes.  This is less important with modern generational garbage
1076 collectors, but not all SBCL platforms use such a garbage collector.
1078 @include fun-sb-ext-purify.texinfo
1080 The @code{sb-ext:truly-the} special form declares the type of the
1081 result of the operations, producing its argument; the declaration is
1082 not checked. In short: don't use it.
1084 @include special-operator-sb-ext-truly-the.texinfo
1086 The @code{sb-ext:freeze-type} declaration declares that a
1087 type will never change, which can make type testing
1088 (@code{typep}, etc.) more efficient for structure types.