Move SET-INFO-VALUE's DEFKNOWN into fndb, also fix a style-warning.
[sbcl.git] / doc / manual / beyond-ansi.texinfo
blobebdff0534f0628880d9d7c4653f965f7f2e16fa1
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 <package-name>::<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 In addition to the mandatory functions above, methods on the sequence
478 functions listed below can be defined.
480 There are two noteworthy irregularities:
481 @itemize
482 @item
483 The function @code{sb-sequence:emptyp} does not have a counterpart in
484 the @code{cl} package. It is intended to be used instead of
485 @code{length} when working with lazy or infinite sequences.
487 @item
488 The functions @code{map}, @code{concatenate} and @code{merge} receive a
489 type designator specifying the type of the constructed sequence as their
490 first argument. However, the corresponding generic functions
491 @code{sb-sequence:map}, @code{sb-sequence:concatenate} and
492 @code{sb-sequence:merge} receive a prototype instance of the requested
493 @code{sequence} subclass instead.
494 @end itemize
496 @include fun-sb-sequence-emptyp.texinfo
498 @itemize
499 @item
500 @code{sb-sequence:count}, @code{sb-sequence:count-if}, @code{sb-sequence:count-if-not}
502 @item
503 @code{sb-sequence:find}, @code{sb-sequence:find-if}, @code{sb-sequence:find-if-not}
505 @item
506 @code{sb-sequence:position}, @code{sb-sequence:position-if}, @code{sb-sequence:position-if-not}
508 @item
509 @code{sb-sequence:subseq}
511 @item
512 @code{sb-sequence:copy-seq}
514 @item
515 @code{sb-sequence:fill}
517 @item
518 @include fun-sb-sequence-map.texinfo
520 @item
521 @code{sb-sequence:nsubstitute}, @code{sb-sequence:nsubstitute-if},
522 @code{sb-sequence:nsubstitute-if-not}, @code{sb-sequence:substitute},
523 @code{sb-sequence:substitute-if}, @code{sb-sequence:substitute-if-not}
525 @item
526 @code{sb-sequence:replace}
528 @item
529 @code{sb-sequence:nreverse}, @code{sb-sequence:reverse}
531 @item
532 @include fun-sb-sequence-concatenate.texinfo
534 @item
535 @code{sb-sequence:reduce}
537 @item
538 @code{sb-sequence:mismatch}
540 @item
541 @code{sb-sequence:search}
543 @item
544 @code{sb-sequence:delete}, @code{sb-sequence:delete-if}, @code{sb-sequence:delete-if-not},
545 @code{sb-sequence:remove}, @code{sb-sequence:remove-if}, @code{sb-sequence:remove-if-not},
547 @item
548 @code{sb-sequence:delete-duplicates}, @code{sb-sequence:remove-duplicates}
550 @item
551 @code{sb-sequence:sort}, @code{sb-sequence:stable-sort}
553 @item
554 @include fun-sb-sequence-merge.texinfo
555 @end itemize
557 In the spirit of @code{dolist}, generic sequences can be traversed using
558 the macro
559 @findex @cl{dolist}
561 @include macro-sb-sequence-dosequence.texinfo
563 @node Iterator Protocol
564 @comment  node-name,  next,  previous,  up
565 @subsection Iterator Protocol
567 The iterator protocol allows subsequently accessing some or all elements
568 of a sequence in forward or reverse direction. Users first call
569 @code{make-sequence-iterator} to create an iteration state and
570 receive functions to query and mutate it. These functions allow, among
571 other things, moving to, retrieving or modifying elements of the
572 sequence. An iteration state consists of a state object, a limit object,
573 a from-end indicator and the following six functions to query or mutate
574 this state:
575 @findex @sequence{make-sequence-iterator}
576 @deffn {Function} @code{step function} sequence iterator from-end
577 Moves the iterator one position forward or backward in the associated
578 sequence depending on the iteration direction.
579 @end deffn
580 @deffn {Function} @code{endp function} sequence iterator limit from-end
581 Returns non-@code{nil} when the iterator has reached the end of the
582 associated sequence with respect to the iteration direction.
583 @end deffn
584 @deffn {Function} @code{element function} sequence iterator
585 Returns the sequence element associated to the current position of the
586 iteration.
587 @end deffn
588 @deffn {Function} @code{setf element function} new-value sequence iterator
589 Destructively modifies the associates sequence by replacing the sequence
590 element associated to the current iteration position with a new value.
591 @end deffn
592 @deffn {Function} @code{index function} sequence iterator
593 Returns the position of the iteration in the associated sequence.
594 @end deffn
595 @deffn {Function} @code{copy function} sequence iterator
596 Returns a copy of the iteration state which can be mutated independently
597 of the copied iteration state.
598 @end deffn
600 An iterator is created by calling:
602 @include fun-sb-sequence-make-sequence-iterator.texinfo
604 Note that @code{make-sequence-iterator} calls
605 @code{make-simple-sequence-iterator} when there is no specialized
606 method for a particular @code{sequence} subclass. @xref{Simple Iterator
607 Protocol}.
608 @findex @sequence{make-sequence-iterator}
609 @findex @sequence{make-simple-sequence-iterator}
610 @tindex @cl{sequence}
612 The following convenience macros simplify traversing sequences using
613 iterators:
615 @include macro-sb-sequence-with-sequence-iterator.texinfo
616 @include macro-sb-sequence-with-sequence-iterator-functions.texinfo
618 @node Simple Iterator Protocol
619 @comment  node-name,  next,  previous,  up
620 @subsection Simple Iterator Protocol
622 For cases in which the full flexibility and performance of the general
623 sequence iterator protocol is not required, there is a simplified
624 sequence iterator protocol consisting of a few generic functions which
625 can be specialized for iterator classes:
627 @include fun-sb-sequence-iterator-step.texinfo
628 @include fun-sb-sequence-iterator-endp.texinfo
629 @include fun-sb-sequence-iterator-element.texinfo
630 @include fun-sb-sequence-setf-iterator-element.texinfo
631 @include fun-sb-sequence-iterator-index.texinfo
632 @include fun-sb-sequence-iterator-copy.texinfo
634 Iterator objects implementing the above simple iteration protocol are
635 created by calling the following generic function:
637 @include fun-sb-sequence-make-simple-sequence-iterator.texinfo
639 @node  Support For Unix
640 @comment  node-name,  next,  previous,  up
641 @section Support For Unix
643 @menu
644 * Command-line arguments::
645 * Querying the process environment::
646 * Running external programs::
647 @end menu
649 @node Command-line arguments
650 @subsection Command-line arguments
651 @vindex @sbext{@earmuffs{posix-argv}}
653 The UNIX command line can be read from the variable
654 @code{sb-ext:*posix-argv*}.
656 @node Querying the process environment
657 @subsection Querying the process environment
659 The UNIX environment can be queried with the
660 @code{sb-ext:posix-getenv} function.
662 @include fun-sb-ext-posix-getenv.texinfo
664 @node Running external programs
665 @subsection Running external programs
667 External programs can be run with @code{sb-ext:run-program}.
668 @footnote{In SBCL versions prior to 1.0.13, @code{sb-ext:run-program}
669 searched for executables in a manner somewhat incompatible with other
670 languages.  As of this version, SBCL uses the system library routine
671 @code{execvp(3)}, and no longer contains the function,
672 @code{find-executable-in-search-path}, which implemented the old
673 search.  Users who need this function may find it
674 in @file{run-program.lisp} versions 1.67 and earlier in SBCL's CVS
675 repository here
676 @url{http://sbcl.cvs.sourceforge.net/sbcl/sbcl/src/code/run-program.lisp?view=log}. However,
677 we caution such users that this search routine finds executables that
678 system library routines do not.}
680 @include fun-sb-ext-run-program.texinfo
682 When @code{sb-ext:run-program} is called with @code{wait} equal to
683 NIL, an instance of class @var{sb-ext:process} is returned.  The
684 following functions are available for use with processes:
686 @include fun-sb-ext-process-p.texinfo
688 @include fun-sb-ext-process-input.texinfo
690 @include fun-sb-ext-process-output.texinfo
692 @include fun-sb-ext-process-error.texinfo
694 @include fun-sb-ext-process-alive-p.texinfo
696 @include fun-sb-ext-process-status.texinfo
698 @include fun-sb-ext-process-wait.texinfo
700 @include fun-sb-ext-process-exit-code.texinfo
702 @include fun-sb-ext-process-core-dumped.texinfo
704 @include fun-sb-ext-process-close.texinfo
706 @include fun-sb-ext-process-kill.texinfo
708 @node  Unicode Support
709 @comment  node-name,  next,  previous,  up
710 @section Unicode Support
712 SBCL provides support for working with Unicode text and querying the
713 standard Unicode database for information about individual codepoints.
714 Unicode-related functions are located in the @code{sb-unicode} package.
716 SBCL also extends ANSI character literal syntax to support Unicode
717 codepoints. You can either specify a character by its Unicode name, with
718 spaces replaced by underscores, if a unique name exists @footnote{Please
719 note that the codepoint U+1F5CF (PAGE) introduced in Unicode 7.0 is
720 named @code{UNICODE_PAGE}, since the name ``Page'' is required to be
721 assigned to form-feed (U+0C) by the ANSI standard.} or
722 by giving its hexadecimal codepoint preceded by a ``U'', an optional
723 ``+'', and an arbitrary number of leading zeros. You may also input the
724 character directly into your source code if it can be encoded in your
725 file. If a character had an assigned name in Unicode 1.0 that was
726 distinct from its current name, you may also use that name (with spaces
727 replaced by underscores) to specify the character, unless the name is
728 already associated with a codepoint in the latest Unicode standard (such
729 as ``BELL'').
731 For example, you can specify the codepoint U+00E1 (``Latin
732 Small Letter A With Acute'') as
733 @itemize @bullet
734 @item
735 @code{#\LATIN_SMALL_LETTER_A_WITH_ACUTE}
736 @item
737 @code{#\LATIN_SMALL_LETTER_A_ACUTE}
738 @item
739 @code{#\á} assuming a Unicode source file
740 @item
741 @code{#\U00E1}
742 @item
743 @code{#\UE1}
744 @item
745 @code{#\U+00E1}
746 @end itemize
748 @subsection Unicode property access
750 The following functions can be used to find information about a Unicode
751 codepoint.
753 @include fun-sb-unicode-general-category.texinfo
755 @include fun-sb-unicode-bidi-class.texinfo
757 @include fun-sb-unicode-combining-class.texinfo
759 @include fun-sb-unicode-decimal-value.texinfo
761 @include fun-sb-unicode-digit-value.texinfo
763 @include fun-sb-unicode-numeric-value.texinfo
765 @include fun-sb-unicode-mirrored-p.texinfo
767 @include fun-sb-unicode-bidi-mirroring-glyph.texinfo
769 @include fun-sb-unicode-age.texinfo
771 @include fun-sb-unicode-hangul-syllable-type.texinfo
773 @include fun-sb-unicode-east-asian-width.texinfo
775 @include fun-sb-unicode-script.texinfo
777 @include fun-sb-unicode-char-block.texinfo
779 @include fun-sb-unicode-unicode-1-name.texinfo
781 @include fun-sb-unicode-proplist-p.texinfo
783 @include fun-sb-unicode-uppercase-p.texinfo
785 @include fun-sb-unicode-lowercase-p.texinfo
787 @include fun-sb-unicode-cased-p.texinfo
789 @include fun-sb-unicode-case-ignorable-p.texinfo
791 @include fun-sb-unicode-alphabetic-p.texinfo
793 @include fun-sb-unicode-ideographic-p.texinfo
795 @include fun-sb-unicode-math-p.texinfo
797 @include fun-sb-unicode-whitespace-p.texinfo
799 @include fun-sb-unicode-soft-dotted-p.texinfo
801 @include fun-sb-unicode-hex-digit-p.texinfo
803 @include fun-sb-unicode-default-ignorable-p.texinfo
805 @include fun-sb-unicode-grapheme-break-class.texinfo
807 @include fun-sb-unicode-word-break-class.texinfo
809 @include fun-sb-unicode-sentence-break-class.texinfo
811 @include fun-sb-unicode-line-break-class.texinfo
813 @subsection String operations
815 SBCL can normalize strings using:
817 @include fun-sb-unicode-normalize-string.texinfo
819 @include fun-sb-unicode-normalized-p.texinfo
821 SBCL implements the full range of Unicode case operations with the
822 functions
824 @include fun-sb-unicode-uppercase.texinfo
826 @include fun-sb-unicode-lowercase.texinfo
828 @include fun-sb-unicode-titlecase.texinfo
830 @include fun-sb-unicode-casefold.texinfo
832 It also extends standard Common Lisp case functions such as
833 @findex @cl{string-upcase}
834 @code{string-upcase} and
835 @findex @cl{char-downcase}
836 @code{string-downcase} to support a subset of Unicode's casing behavior.
837 Specifically, a character is
838 @findex @cl{both-case-p}
839 @code{both-case-p} if its case mapping in Unicode is one-to-one and
840 invertable.
842 The @code{sb-unicode} package also provides functions for
843 collating/sorting strings according to the Unicode Collation Algorithm.
845 @include fun-sb-unicode-unicode-lt.texinfo
847 @include fun-sb-unicode-unicode=.texinfo
849 @include fun-sb-unicode-unicode-equal.texinfo
851 @include fun-sb-unicode-unicode-lt=.texinfo
853 @include fun-sb-unicode-unicode-gt.texinfo
855 @include fun-sb-unicode-unicode-gt=.texinfo
857 The following functions are provided for detecting visually confusable strings:
859 @include fun-sb-unicode-confusable-p.texinfo
861 @subsection Breaking strings
863 The @code{sb-unicode} package includes several functions for breaking a
864 Unicode string into useful parts.
866 @include fun-sb-unicode-graphemes.texinfo
868 @include fun-sb-unicode-words.texinfo
870 @include fun-sb-unicode-sentences.texinfo
872 @include fun-sb-unicode-lines.texinfo
874 @node  Customization Hooks for Users
875 @comment  node-name,  next,  previous,  up
876 @section Customization Hooks for Users
878 The toplevel repl prompt may be customized, and the function
879 that reads user input may be replaced completely.
880 @c <!-- FIXME but I don't currently remember how -->
882 The behaviour of @code{require} when called with only one argument is
883 implementation-defined.  In SBCL, @code{require} behaves in the
884 following way:
886 @include fun-common-lisp-require.texinfo
887 @include var-sb-ext-star-module-provider-functions-star.texinfo
889 Although SBCL does not provide a resident editor, the @code{ed}
890 function can be customized to hook into user-provided editing
891 mechanisms as follows:
893 @include fun-common-lisp-ed.texinfo
894 @include var-sb-ext-star-ed-functions-star.texinfo
896 Conditions of type @code{warning} and @code{style-warning} are
897 sometimes signaled at runtime, especially during execution of Common
898 Lisp defining forms such as @code{defun}, @code{defmethod}, etc.  To
899 muffle these warnings at runtime, SBCL provides a variable
900 @code{sb-ext:*muffled-warnings*}:
902 @include var-sb-ext-star-muffled-warnings-star.texinfo
904 @node Tools To Help Developers
905 @comment  node-name,  next,  previous,  up
906 @section Tools To Help Developers
907 @findex @cl{trace}
908 @findex @cl{inspect}
910 SBCL provides a profiler and other extensions to the ANSI @code{trace}
911 facility.  For more information, see @ref{Macro common-lisp:trace}.
913 The debugger supports a number of options. Its documentation is
914 accessed by typing @kbd{help} at the debugger prompt. @xref{Debugger}.
916 Documentation for @code{inspect} is accessed by typing @kbd{help} at
917 the @code{inspect} prompt.
919 @node Resolution of Name Conflicts
920 @section Resolution of Name Conflicts
921 @tindex @sbext{name-conflict}
922 @findex @sbext{name-conflict-symbols}
924 The ANSI standard (section 11.1.1.2.5) requires that name conflicts in
925 packages be resolvable in favour of any of the conflicting symbols.  In
926 the interactive debugger, this is achieved by prompting for the symbol
927 in whose favour the conflict should be resolved; for programmatic use,
928 the @code{sb-ext:resolve-conflict} restart should be invoked with one
929 argument, which should be a member of the list returned by the condition
930 accessor @code{sb-ext:name-conflict-symbols}.
932 @node    Hash Table Extensions
933 @comment  node-name,  next,  previous,  up
934 @section Hash Table Extensions
935 @cindex Hash tables
937 Hash table extensions supported by SBCL are all controlled by keyword
938 arguments to @code{make-hash-table}.
940 @include fun-common-lisp-make-hash-table.texinfo
942 @include macro-sb-ext-define-hash-table-test.texinfo
944 @include macro-sb-ext-with-locked-hash-table.texinfo
946 @include fun-sb-ext-hash-table-synchronized-p.texinfo
948 @include fun-sb-ext-hash-table-weakness.texinfo
950 @node    Random Number Generation
951 @comment  node-name,  next,  previous,  up
952 @section Random Number Generation
953 @cindex Random Number Generation
955 The initial value of @code{*random-state*} is the same each time SBCL
956 is started. This makes it possible for user code to obtain repeatable
957 pseudo random numbers using only standard-provided functionality. See
958 @code{seed-random-state} below for an SBCL extension that allows to
959 seed the random number generator from given data for an additional
960 possibility to achieve this. Non-repeatable random numbers can always
961 be obtained using @code{(make-random-state t)}.
963 The sequence of numbers produced by repeated calls to @code{random}
964 starting with the same random state and using the same sequence of
965 @code{limit} arguments is guaranteed to be reproducible only in the
966 same version of SBCL on the same platform, using the same code under
967 the same evaluator mode and compiler optimization qualities. Just two
968 examples of differences that may occur otherwise: calls to
969 @code{random} can be compiled differently depending on how much is
970 known about the @code{limit} argument at compile time, yielding
971 different results even if called with the same argument at run time,
972 and the results can differ depending on the machine's word size, for
973 example for limits that are fixnums under 64-bit word size but bignums
974 under 32-bit word size.
976 @include fun-sb-ext-seed-random-state.texinfo
978 Some notes on random floats: The standard doesn't prescribe a specific
979 method of generating random floats. The following paragraph describes
980 SBCL's current implementation and should be taken purely informational,
981 that is, user code should not depend on any of its specific properties.
982 The method used has been chosen because it is common, conceptually
983 simple and fast.
985 To generate random floats, SBCL evaluates code that has an equivalent
986 effect as
987 @lisp
988 (* limit
989    (float (/ (random (expt 2 23)) (expt 2 23)) 1.0f0))
990 @end lisp
991 (for single-floats) and correspondingly (with @code{52} and
992 @code{1.0d0} instead of @code{23} and @code{1.0f0}) for double-floats.
993 Note especially that this means that zero is a possible return value
994 occurring with probability @code{(expt 2 -23)} respectively
995 @code{(expt 2 -52)}. Also note that there exist twice as many
996 equidistant floats between 0 and 1 as are generated. For example, the
997 largest number that @code{(random 1.0f0)} ever returns is
998 @code{(float (/ (1- (expt 2 23)) (expt 2 23)) 1.0f0)} while
999 @code{(float (/ (1- (expt 2 24)) (expt 2 24)) 1.0f0)} is the
1000 largest single-float less than 1. This is a side effect of the fact
1001 that the implementation uses the fastest possible conversion from bits
1002 to floats.
1004 SBCL currently uses the Mersenne Twister as its random number
1005 generator, specifically the 32-bit version under both 32- and 64-bit
1006 word size. The seeding algorithm has been improved several times by
1007 the authors of the Mersenne Twister; SBCL uses the third version
1008 (from 2002) which is still the most recent as of June 2012. The
1009 implementation has been tested to provide output identical to the
1010 recommended C implementation.
1012 While the Mersenne Twister generates random numbers of much better
1013 statistical quality than other widely used generators, it uses only
1014 linear operations modulo 2 and thus fails some statistical
1015 tests@footnote{See chapter 7 "Testing widely used RNGs" in
1016 @cite{TestU01: A C Library for Empirical Testing of Random Number
1017 Generators} by Pierre L'Ecuyer and Richard Simard, ACM Transactions on
1018 Mathematical Software, Vol. 33, article 22, 2007.}.
1019 For example, the distribution of ranks of (sufficiently large) random
1020 binary matrices is much distorted compared to the theoretically
1021 expected one when the matrices are generated by the Mersenne Twister.
1022 Thus, applications that are sensitive to this aspect should use a
1023 different type of generator.
1025 @node    Miscellaneous Extensions
1026 @comment  node-name,  next,  previous,  up
1027 @section Miscellaneous Extensions
1029 @include fun-sb-ext-array-storage-vector.texinfo
1030 @include fun-sb-ext-delete-directory.texinfo
1031 @include fun-sb-ext-get-time-of-day.texinfo
1032 @include macro-sb-ext-wait-for.texinfo
1033 @include fun-sb-ext-assert-version-gt=.texinfo
1035 @node Stale Extensions
1036 @comment  node-name,  next,  previous,  up
1037 @section Stale Extensions
1039 SBCL has inherited from CMUCL various hooks to allow the user to
1040 tweak and monitor the garbage collection process. These are somewhat
1041 stale code, and their interface might need to be cleaned up. If you
1042 have urgent need of them, look at the code in @file{src/code/gc.lisp}
1043 and bring it up on the developers' mailing list.
1045 SBCL has various hooks inherited from CMUCL, like
1046 @code{sb-ext:float-denormalized-p}, to allow a program to take
1047 advantage of IEEE floating point arithmetic properties which aren't
1048 conveniently or efficiently expressible using the ANSI standard. These
1049 look good, and their interface looks good, but IEEE support is
1050 slightly broken due to a stupid decision to remove some support for
1051 infinities (because it wasn't in the ANSI spec and it didn't occur to
1052 me that it was in the IEEE spec). If you need this stuff, take a look
1053 at the code and bring it up on the developers' mailing
1054 list.
1057 @node  Efficiency Hacks
1058 @comment  node-name,  next,  previous,  up
1059 @section Efficiency Hacks
1061 The @code{sb-ext:purify} function causes SBCL first to collect all
1062 garbage, then to mark all uncollected objects as permanent, never again
1063 attempting to collect them as garbage. This can cause a large increase
1064 in efficiency when using a primitive garbage collector, or a more
1065 moderate increase in efficiency when using a more sophisticated garbage
1066 collector which is well suited to the program's memory usage pattern. It
1067 also allows permanent code to be frozen at fixed addresses, a
1068 precondition for using copy-on-write to share code between multiple Lisp
1069 processes.  This is less important with modern generational garbage
1070 collectors, but not all SBCL platforms use such a garbage collector.
1072 @include fun-sb-ext-purify.texinfo
1074 The @code{sb-ext:truly-the} special form declares the type of the
1075 result of the operations, producing its argument; the declaration is
1076 not checked. In short: don't use it.
1078 @include special-operator-sb-ext-truly-the.texinfo
1080 The @code{sb-ext:freeze-type} declaration declares that a
1081 type will never change, which can make type testing
1082 (@code{typep}, etc.) more efficient for structure types.