Documentation and news entry for SB-SEQUENCE:{MAP,CONCATENATE,MERGE}
[sbcl.git] / doc / manual / beyond-ansi.texinfo
blob8ca9ef33897050e0ed884c2eee385ed52f711279
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 * Customization Hooks for Users::
18 * Tools To Help Developers::
19 * Resolution of Name Conflicts::
20 * Hash Table Extensions::
21 * Random Number Generation::
22 * Miscellaneous Extensions::
23 * Stale Extensions::
24 * Efficiency Hacks::
25 @end menu
27 @node Reader Extensions
28 @comment  node-name,  next,  previous,  up
29 @section Reader Extensions
30 @cindex Reader Extensions
32 SBCL supports extended package prefix syntax, which allows specifying
33 an alternate package instead of @code{*package*} for the reader to use
34 as the default package for interning symbols:
36 @lisp
37 <package-name>::<form-with-interning-into-package>
38 @end lisp
40 Example:
42 @lisp
43   'foo::(bar quux zot) == '(foo::bar foo::quux foo::zot)
44 @end lisp
46 Doesn't alter @code{*package*}: if @code{foo::bar} would cause a
47 read-time package lock violation, so does @code{foo::(bar)}.
49 @node  Package-Local Nicknames
50 @comment  node-name,  next,  previous,  up
51 @section Package-Local Nicknames
52 @cindex Package-Local Nicknames
54 SBCL allows giving packages local nicknames: they allow short and
55 easy-to-use names to be used without fear of name conflict associated
56 with normal nicknames.
58 A local nickname is valid only when inside the package for which it
59 has been specified. Different packages can use same local nickname for
60 different global names, or different local nickname for same global
61 name.
63 Symbol @code{:package-local-nicknames} in @code{*features*} denotes the
64 support for this feature.
66 @findex @cl{defpackage}
67 @defmac @cl{defpackage} name [[option]]* @result{} package
69 Options are extended to include
71 @itemize
72 @item
73 @code{:local-nicknames (@var{local-nickname} @var{actual-package-name})*}
75 The package has the specified local nicknames for the corresponding
76 actual packages.
77 @end itemize
79 Example:
81 @lisp
82 (defpackage :bar (:intern "X"))
83 (defpackage :foo (:intern "X"))
84 (defpackage :quux (:use :cl) (:local-nicknames (:bar :foo) (:foo :bar)))
85 (find-symbol "X" :foo) ; => FOO::X
86 (find-symbol "X" :bar) ; => BAR::X
87 (let ((*package* (find-package :quux)))
88   (find-symbol "X" :foo))               ; => BAR::X
89 (let ((*package* (find-package :quux)))
90   (find-symbol "X" :bar))               ; => FOO::X
91 @end lisp
92 @end defmac
94 @include fun-sb-ext-package-local-nicknames.texinfo
95 @include fun-sb-ext-package-locally-nicknamed-by-list.texinfo
96 @include fun-sb-ext-add-package-local-nickname.texinfo
97 @include fun-sb-ext-remove-package-local-nickname.texinfo
99 @node  Package Variance
100 @comment  node-name,  next,  previous,  up
101 @section Package Variance
103 Common Lisp standard specifies that ``If the new definition is at
104 variance with the current state of that package, the consequences are
105 undefined;'' SBCL by default signals a full warning and retains as
106 much of the package state as possible.
108 This can be adjusted using @code{sb-ext:*on-package-variance*}:
110 @include var-sb-ext-star-on-package-variance-star.texinfo
112 @node  Garbage Collection
113 @comment  node-name,  next,  previous,  up
114 @section Garbage Collection
115 @cindex Garbage collection
117 SBCL provides additional garbage collection functionality not
118 specified by ANSI.
120 @include var-sb-ext-star-after-gc-hooks-star.texinfo
121 @include fun-sb-ext-gc.texinfo
123 @subsection Finalization
124 @cindex Finalization
126 Finalization allows code to be executed after an object has been
127 garbage collected. This is useful for example for releasing foreign
128 memory associated with a Lisp object.
130 @include fun-sb-ext-finalize.texinfo
131 @include fun-sb-ext-cancel-finalization.texinfo
133 @subsection Weak Pointers
134 @cindex Weak pointers
136 Weak pointers allow references to objects to be maintained without
137 keeping them from being garbage collected: useful for building caches
138 among other things.
140 Hash tables can also have weak keys and values: @pxref{Hash Table
141 Extensions}.
143 @include fun-sb-ext-make-weak-pointer.texinfo
144 @include fun-sb-ext-weak-pointer-value.texinfo
146 @subsection Introspection and Tuning
148 @include var-sb-ext-star-gc-run-time-star.texinfo
149 @include fun-sb-ext-bytes-consed-between-gcs.texinfo
150 @include fun-sb-ext-dynamic-space-size.texinfo
151 @include fun-sb-ext-get-bytes-consed.texinfo
152 @include fun-sb-ext-gc-logfile.texinfo
153 @include fun-sb-ext-generation-average-age.texinfo
154 @include fun-sb-ext-generation-bytes-allocated.texinfo
155 @include fun-sb-ext-generation-bytes-consed-between-gcs.texinfo
156 @include fun-sb-ext-generation-minimum-age-before-gc.texinfo
157 @include fun-sb-ext-generation-number-of-gcs-before-promotion.texinfo
158 @include fun-sb-ext-generation-number-of-gcs.texinfo
160 @node Metaobject Protocol
161 @comment  node-name,  next,  previous,  up
162 @section Metaobject Protocol
164 @subsection AMOP Compatibility of Metaobject Protocol
166 SBCL supports a metaobject protocol which is intended to be compatible
167 with AMOP; present exceptions to this (as distinct from current bugs)
168 are:
170 @itemize
172 @item
173 @findex @sbmop{compute-effective-method}
174 @code{compute-effective-method} only returns one value, not two.
176 There is no record of what the second return value was meant to
177 indicate, and apparently no clients for it.
179 @item
180 @tindex @cl{generic-function}
181 @tindex @cl{standard-generic-function}
182 @tindex @sbmop{funcallable-standard-object}
183 @tindex @cl{standard-object}
184 @tindex @cl{function}
185 The direct superclasses of @code{funcallable-standard-object} are
186 @code{(function standard-object)}, not @code{(standard-object function)}.
188 This is to ensure that the @code{standard-object} class is the last of
189 the standardized classes before @code{t} appearing in the class
190 precedence list of @code{generic-function} and
191 @code{standard-generic-function}, as required by section 1.4.4.5 of the
192 ANSI specification.
194 @item
195 @findex @cl{ensure-generic-function}
196 @findex @sbmop{generic-function-declarations}
197 the arguments @code{:declare} and @code{:declarations} to
198 @code{ensure-generic-function} are both accepted, with the leftmost
199 argument defining the declarations to be stored and returned by
200 @code{generic-function-declarations}.
202 Where AMOP specifies @code{:declarations} as the keyword argument to
203 @code{ensure-generic-function}, the Common Lisp standard specifies
204 @code{:declare}.  Portable code should use @code{:declare}.
206 @item
207 @findex @sbmop{validate-superclass}
208 @findex @sbmop{finalize-inheritance}
209 @tindex @cl{standard-class}
210 @tindex @sbmop{funcallable-standard-class}
211 @tindex @cl{function}
212 @findex @sbmop{class-prototype}
213 although SBCL obeys the requirement in AMOP that
214 @code{validate-superclass} should treat @code{standard-class} and
215 @code{funcallable-standard-class} as compatible metaclasses, we
216 impose an additional requirement at class finalization time: a class
217 of metaclass @code{funcallable-standard-class} must have
218 @code{function} in its superclasses, and a class of metaclass
219 @code{standard-class} must not.
221 @findex @cl{typep}
222 @findex @cl{class-of}
223 @findex @cl{subtypep}
224 After a class has been finalized, it is associated with a class
225 prototype which is accessible by a standard mop function
226 @code{class-prototype}.  The user can then ask whether this object is a
227 @code{function} or not in several different ways: whether it is a
228 function according to @code{typep}; whether its @code{class-of} is
229 @code{subtypep} @code{function}, or whether @code{function} appears in
230 the superclasses of the class.  The additional consistency requirement
231 comes from the desire to make all of these answers the same.
233 The following class definitions are bad, and will lead to errors
234 either immediately or if an instance is created:
235 @lisp
236 (defclass bad-object (funcallable-standard-object)
237   ()
238   (:metaclass standard-class))
239 @end lisp
240 @lisp
241 (defclass bad-funcallable-object (standard-object)
242   ()
243   (:metaclass funcallable-standard-class))
244 @end lisp
245 The following definition is acceptable:
246 @lisp
247 (defclass mixin ()
248   ((slot :initarg slot)))
249 (defclass funcallable-object (funcallable-standard-object mixin)
250   ()
251   (:metaclass funcallable-standard-class))
252 @end lisp
253 and leads to a class whose instances are funcallable and have one slot.
255 @tindex @sbmop{funcallable-standard-object}
256 Note that this requirement also applies to the class
257 @code{funcallable-standard-object}, which has metaclass
258 @code{funcallable-standard-class} rather than
259 @code{standard-class} as AMOP specifies.
261 @item
262 the requirement that ``No portable class @math{C_p} may inherit, by
263 virtue of being a direct or indirect subclass of a specified class, any
264 slot for which the name is a symbol accessible in the
265 @code{common-lisp-user} package or exported by any package defined in
266 the ANSI Common Lisp standard.'' is interpreted to mean that the
267 standardized classes themselves should not have slots named by external
268 symbols of public packages.
270 The rationale behind the restriction is likely to be similar to the ANSI
271 Common Lisp restriction on defining functions, variables and types named
272 by symbols in the Common Lisp package: preventing two independent pieces
273 of software from colliding with each other.
275 @item
276 @findex @sbmop{slot-value-using-class}
277 @findex @setf{@sbmop{slot-value-using-class}}
278 @findex @sbmop{slot-boundp-using-class}
279 specializations of the @code{new-value} argument to @code{(setf
280 slot-value-using-class)} are not allowed: all user-defined methods must
281 have a specializer of the class @code{t}.
283 This prohibition is motivated by a separation of layers: the
284 @code{slot-value-using-class} family of functions is intended for use in
285 implementing different and new slot allocation strategies, rather than
286 in performing application-level dispatching.  Additionally, with this
287 requirement, there is a one-to-one mapping between metaclass, class and
288 slot-definition-class tuples and effective methods of @code{(setf
289 slot-value-using-class)}, which permits optimization of @code{(setf
290 slot-value-using-class)}'s discriminating function in the same manner as
291 for @code{slot-value-using-class} and @code{slot-boundp-using-class}.
293 Note that application code may specialize on the @code{new-value}
294 argument of slot accessors.
296 @item
297 @findex @cl{defclass}
298 @findex @sbmop{ensure-class}
299 @findex @sbmop{ensure-class-using-class}
300 @findex @cl{find-class}
301 @findex @cl{class-name}
302 the class named by the @code{name} argument to @code{ensure-class}, if
303 any, is only redefined if it is the proper name of that class;
304 otherwise, a new class is created.
306 This is consistent with the description of @code{ensure-class} in AMOP
307 as the functional version of @code{defclass}, which has this behaviour;
308 however, it is not consistent with the weaker requirement in AMOP, which
309 states that any class found by @code{find-class}, no matter what its
310 @code{class-name}, is redefined.
312 @item
313 @findex @sbmop{slot-definition-name}
314 @tindex @cl{structure-class}
315 @findex @cl{defstruct}
316 an error is not signaled in the case of the @code{:name} initialization
317 argument for @code{slot-definition} objects being a constant, when the
318 slot definition is of type @code{structure-slot-definition} (i.e. it is
319 associated with a class of type @code{structure-class}).
321 This allows code which uses constant names for structure slots to
322 continue working as specified in ANSI, while enforcing the constraint
323 for all other types of slot.
325 @item
326 @tindex @cl{t}
327 @tindex @cl{built-in-class}
328 @findex @sbmop{validate-superclass}
329 @findex @cl{defclass}
330 the class named @code{t} is not an instance of the @code{built-in-class}
331 metaclass.
333 AMOP specifies, in the ``Inheritance Structure of Metaobject Classes''
334 section, that the class named @code{t} should be an instance of
335 @code{built-in-class}.  However, it also specifies that
336 @code{validate-superclass} should return true (indicating that a direct
337 superclass relationship is permissible) if the second argument is the
338 class named @code{t}.  Also, ANSI specifies that classes with metaclass
339 @code{built-in-class} may not be subclassed using @code{defclass}, and
340 also that the class named @code{t} is the universal superclass,
341 inconsistent with it being a @code{built-in-class}.
343 @end itemize
345 @subsection Metaobject Protocol Extensions
347 In addition, SBCL supports extensions to the Metaobject protocol from
348 AMOP; at present, they are:
350 @itemize
352 @item
353 @findex @cl{defmethod}
354 @findex @cl{find-class}
355 @findex @sbmop{intern-eql-specializer}
356 @findex @sbpcl{make-method-specializers-form}
357 @findex @sbmop{make-method-lambda}
358 compile-time support for generating specializer metaobjects from
359 specializer names in @code{defmethod} forms is provided by the
360 @code{make-method-specializers-form} function, which returns a form
361 which, when evaluated in the lexical environment of the
362 @code{defmethod}, returns a list of specializer metaobjects.  This
363 operator suffers from similar restrictions to those affecting
364 @code{make-method-lambda}, namely that the generic function must be
365 defined when the @code{defmethod} form is expanded, so that the
366 correct method of @code{make-method-specializers-form} is invoked.
367 The system-provided method on @code{make-method-specializers-form}
368 generates a call to @code{find-class} for each symbol specializer
369 name, and a call to @code{intern-eql-specializer} for each @code{(eql
370 @var{x})} specializer name.
372 @item
373 @findex @cl{find-method}
374 @findex @sbpcl{parse-specializer-using-class}
375 @findex @sbpcl{unparse-specializer-using-class}
376 run-time support for converting between specializer names and
377 specializer metaobjects, mostly for the purposes of
378 @code{find-method}, is provided by
379 @code{parse-specializer-using-class} and
380 @code{unparse-specializer-using-class}, which dispatch on their first
381 argument, the generic function associated with a method with the given
382 specializer.  The system-provided methods on those methods convert
383 between classes and proper names and between lists of the form
384 @code{(eql @var{x})} and interned eql specializer objects.
386 @item
387 @vindex @sbpcl{+slot-unbound+}
388 @findex @sbmop{standard-instance-access}
389 @findex @sbmop{funcallable-standard-instance-access}
390 distinguishing unbound instance allocated slots from bound ones when
391 using @code{standard-instance-access} and
392 @code{funcallable-standard-instance-access} is possible by comparison
393 to the constant @code{+slot-unbound+}.
395 @end itemize
397 @node Extensible Sequences
398 @comment  node-name,  next,  previous,  up
399 @section Extensible Sequences
401 @menu
402 * Iterator Protocol::
403 * Simple Iterator Protocol::
404 @end menu
406 ANSI Common Lisp has a class @code{sequence} with subclasses @code{list} and
407 @code{vector} on which the ``sequence functions'' like @code{find},
408 @code{subseq}, etc. operate. As an extension to the ANSI specification,
409 SBCL allows additional subclasses of @code{sequence} to be defined
410 @footnote{A motivation, rationale and additional examples for the design
411 of this extension can be found in the paper @cite{Rhodes, Christophe
412 (2007): User-extensible sequences in Common Lisp} available for download
414 @url{http://www.doc.gold.ac.uk/~mas01cr/papers/ilc2007/sequences-20070301.pdf}.}.
415 @tindex @cl{sequence}
416 @tindex @cl{vector}
417 @findex @cl{find}
418 @findex @cl{subseq}
420 Users of this extension just make instances of @code{sequence} subclasses
421 and transparently operate on them using sequence functions:
422 @lisp
423 (coerce (subseq (make-instance 'my-sequence) 5 10) 'list)
424 @end lisp
425 From this perspective, no distinction between builtin and user-defined
426 @code{sequence} subclasses should be necessary.
427 @findex @cl{coerce}
428 @findex @cl{subseq}
429 @findex @cl{make-instance}
430 @tindex @cl{list}
432 Providers of the extension, that is of user-defined @code{sequence}
433 subclasses, have to adhere to a ``sequence protocol'' which consists of
434 a set of generic functions in the @code{sequence} package.
436 A minimal @code{sequence} subclass has to specify @code{standard-object} and
437 @code{sequence} as its superclasses and has to be the specializer of the
438 @code{sequence} parameter of methods on at least the following generic
439 functions:
440 @tindex @cl{sequence}
441 @tindex @cl{standard-object}
443 @include fun-sb-sequence-length.texinfo
444 @include fun-sb-sequence-elt.texinfo
445 @include fun-sb-sequence-setf-elt.texinfo
446 @include fun-sb-sequence-adjust-sequence.texinfo
447 @include fun-sb-sequence-make-sequence-like.texinfo
449 @code{make-sequence-like} is needed for functions returning
450 freshly-allocated sequences such as @code{subseq} or
451 @code{copy-seq}. @code{adjust-sequence} is needed for functions which
452 destructively modify their arguments such as @code{delete}. In fact, all
453 other sequence functions can be implemented in terms of the above
454 functions and actually are, if no additional methods are
455 defined. However, relying on these generic implementations, in
456 particular not implementing the iterator protocol can incur a high
457 performance penalty @xref{Iterator Protocol}.
458 @tindex @cl{sequence}
459 @findex @sequence{make-sequence-like}
460 @findex @cl{subseq}
461 @findex @cl{copy-seq}
462 @findex @sequence{adjust-sequence}
464 In addition to the mandatory functions above, methods on the sequence
465 functions listed below can be defined.
467 There are two noteworthy irregularities:
468 @itemize
469 @item
470 The function @code{sb-sequence:emptyp} does not have a counterpart in
471 the @code{cl} package. It is intended to be used instead of
472 @code{length} when working with lazy or infinite sequences.
474 @item
475 The functions @code{map}, @code{concatenate} and @code{merge} receive a
476 type designator specifying the type of the constructed sequence as their
477 first argument. However, the corresponding generic functions
478 @code{sb-sequence:map}, @code{sb-sequence:concatenate} and
479 @code{sb-sequence:merge} receive a prototype instance of the requested
480 @code{sequence} subclass instead.
481 @end itemize
483 @include fun-sb-sequence-emptyp.texinfo
485 @itemize
486 @item
487 @code{sb-sequence:count}, @code{sb-sequence:count-if}, @code{sb-sequence:count-if-not}
489 @item
490 @code{sb-sequence:find}, @code{sb-sequence:find-if}, @code{sb-sequence:find-if-not}
492 @item
493 @code{sb-sequence:position}, @code{sb-sequence:position-if}, @code{sb-sequence:position-if-not}
495 @item
496 @code{sb-sequence:subseq}
498 @item
499 @code{sb-sequence:copy-seq}
501 @item
502 @code{sb-sequence:fill}
504 @item
505 @include fun-sb-sequence-map.texinfo
507 @item
508 @code{sb-sequence:nsubstitute}, @code{sb-sequence:nsubstitute-if},
509 @code{sb-sequence:nsubstitute-if-not}, @code{sb-sequence:substitute},
510 @code{sb-sequence:substitute-if}, @code{sb-sequence:substitute-if-not}
512 @item
513 @code{sb-sequence:replace}
515 @item
516 @code{sb-sequence:nreverse}, @code{sb-sequence:reverse}
518 @item
519 @include fun-sb-sequence-concatenate.texinfo
521 @item
522 @code{sb-sequence:reduce}
524 @item
525 @code{sb-sequence:mismatch}
527 @item
528 @code{sb-sequence:search}
530 @item
531 @code{sb-sequence:delete}, @code{sb-sequence:delete-if}, @code{sb-sequence:delete-if-not},
532 @code{sb-sequence:remove}, @code{sb-sequence:remove-if}, @code{sb-sequence:remove-if-not},
534 @item
535 @code{sb-sequence:delete-duplicates}, @code{sb-sequence:remove-duplicates}
537 @item
538 @code{sb-sequence:sort}, @code{sb-sequence:stable-sort}
540 @item
541 @include fun-sb-sequence-merge.texinfo
542 @end itemize
544 In the spirit of @code{dolist}, generic sequences can be traversed using
545 the macro
546 @findex @cl{dolist}
548 @include macro-sb-sequence-dosequence.texinfo
550 @node Iterator Protocol
551 @comment  node-name,  next,  previous,  up
552 @subsection Iterator Protocol
554 The iterator protocol allows subsequently accessing some or all elements
555 of a sequence in forward or reverse direction. Users first call
556 @code{make-sequence-iterator} to create an iteration state and
557 receive functions to query and mutate it. These functions allow, among
558 other things, moving to, retrieving or modifying elements of the
559 sequence. An iteration state consists of a state object, a limit object,
560 a from-end indicator and the following six functions to query or mutate
561 this state:
562 @findex @sequence{make-sequence-iterator}
563 @deffn {Function} @code{step function} sequence iterator from-end
564 Moves the iterator one position forward or backward in the associated
565 sequence depending on the iteration direction.
566 @end deffn
567 @deffn {Function} @code{endp function} sequence iterator limit from-end
568 Returns non-@code{nil} when the iterator has reached the end of the
569 associated sequence with respect to the iteration direction.
570 @end deffn
571 @deffn {Function} @code{element function} sequence iterator
572 Returns the sequence element associated to the current position of the
573 iteration.
574 @end deffn
575 @deffn {Function} @code{setf element function} new-value sequence iterator
576 Destructively modifies the associates sequence by replacing the sequence
577 element associated to the current iteration position with a new value.
578 @end deffn
579 @deffn {Function} @code{index function} sequence iterator
580 Returns the position of the iteration in the associated sequence.
581 @end deffn
582 @deffn {Function} @code{copy function} sequence iterator
583 Returns a copy of the iteration state which can be mutated independently
584 of the copied iteration state.
585 @end deffn
587 An iterator is created by calling:
589 @include fun-sb-sequence-make-sequence-iterator.texinfo
591 Note that @code{make-sequence-iterator} calls
592 @code{make-simple-sequence-iterator} when there is no specialized
593 method for a particular @code{sequence} subclass. @xref{Simple Iterator
594 Protocol}.
595 @findex @sequence{make-sequence-iterator}
596 @findex @sequence{make-simple-sequence-iterator}
597 @tindex @cl{sequence}
599 The following convenience macros simplify traversing sequences using
600 iterators:
602 @include macro-sb-sequence-with-sequence-iterator.texinfo
603 @include macro-sb-sequence-with-sequence-iterator-functions.texinfo
605 @node Simple Iterator Protocol
606 @comment  node-name,  next,  previous,  up
607 @subsection Simple Iterator Protocol
609 For cases in which the full flexibility and performance of the general
610 sequence iterator protocol is not required, there is a simplified
611 sequence iterator protocol consisting of a few generic functions which
612 can be specialized for iterator classes:
614 @include fun-sb-sequence-iterator-step.texinfo
615 @include fun-sb-sequence-iterator-endp.texinfo
616 @include fun-sb-sequence-iterator-element.texinfo
617 @include fun-sb-sequence-setf-iterator-element.texinfo
618 @include fun-sb-sequence-iterator-index.texinfo
619 @include fun-sb-sequence-iterator-copy.texinfo
621 Iterator objects implementing the above simple iteration protocol are
622 created by calling the following generic function:
624 @include fun-sb-sequence-make-simple-sequence-iterator.texinfo
626 @node  Support For Unix
627 @comment  node-name,  next,  previous,  up
628 @section Support For Unix
630 @menu
631 * Command-line arguments::
632 * Querying the process environment::
633 * Running external programs::
634 @end menu
636 @node Command-line arguments
637 @subsection Command-line arguments
638 @vindex @sbext{@earmuffs{posix-argv}}
640 The UNIX command line can be read from the variable
641 @code{sb-ext:*posix-argv*}.
643 @node Querying the process environment
644 @subsection Querying the process environment
646 The UNIX environment can be queried with the
647 @code{sb-ext:posix-getenv} function.
649 @include fun-sb-ext-posix-getenv.texinfo
651 @node Running external programs
652 @subsection Running external programs
654 External programs can be run with @code{sb-ext:run-program}.
655 @footnote{In SBCL versions prior to 1.0.13, @code{sb-ext:run-program}
656 searched for executables in a manner somewhat incompatible with other
657 languages.  As of this version, SBCL uses the system library routine
658 @code{execvp(3)}, and no longer contains the function,
659 @code{find-executable-in-search-path}, which implemented the old
660 search.  Users who need this function may find it
661 in @file{run-program.lisp} versions 1.67 and earlier in SBCL's CVS
662 repository here
663 @url{http://sbcl.cvs.sourceforge.net/sbcl/sbcl/src/code/run-program.lisp?view=log}. However,
664 we caution such users that this search routine finds executables that
665 system library routines do not.}
667 @include fun-sb-ext-run-program.texinfo
669 When @code{sb-ext:run-program} is called with @code{wait} equal to
670 NIL, an instance of class @var{sb-ext:process} is returned.  The
671 following functions are available for use with processes:
673 @include fun-sb-ext-process-p.texinfo
675 @include fun-sb-ext-process-input.texinfo
677 @include fun-sb-ext-process-output.texinfo
679 @include fun-sb-ext-process-error.texinfo
681 @include fun-sb-ext-process-alive-p.texinfo
683 @include fun-sb-ext-process-status.texinfo
685 @include fun-sb-ext-process-wait.texinfo
687 @include fun-sb-ext-process-exit-code.texinfo
689 @include fun-sb-ext-process-core-dumped.texinfo
691 @include fun-sb-ext-process-close.texinfo
693 @include fun-sb-ext-process-kill.texinfo
695 @node  Customization Hooks for Users
696 @comment  node-name,  next,  previous,  up
697 @section Customization Hooks for Users
699 The toplevel repl prompt may be customized, and the function
700 that reads user input may be replaced completely.
701 @c <!-- FIXME but I don't currently remember how -->
703 The behaviour of @code{require} when called with only one argument is
704 implementation-defined.  In SBCL, @code{require} behaves in the
705 following way:
707 @include fun-common-lisp-require.texinfo
708 @include var-sb-ext-star-module-provider-functions-star.texinfo
710 Although SBCL does not provide a resident editor, the @code{ed}
711 function can be customized to hook into user-provided editing
712 mechanisms as follows:
714 @include fun-common-lisp-ed.texinfo
715 @include var-sb-ext-star-ed-functions-star.texinfo
717 Conditions of type @code{warning} and @code{style-warning} are
718 sometimes signaled at runtime, especially during execution of Common
719 Lisp defining forms such as @code{defun}, @code{defmethod}, etc.  To
720 muffle these warnings at runtime, SBCL provides a variable
721 @code{sb-ext:*muffled-warnings*}:
723 @include var-sb-ext-star-muffled-warnings-star.texinfo
725 @node Tools To Help Developers
726 @comment  node-name,  next,  previous,  up
727 @section Tools To Help Developers
728 @findex @cl{trace}
729 @findex @cl{inspect}
731 SBCL provides a profiler and other extensions to the ANSI @code{trace}
732 facility.  For more information, see @ref{Macro common-lisp:trace}.
734 The debugger supports a number of options. Its documentation is
735 accessed by typing @kbd{help} at the debugger prompt. @xref{Debugger}.
737 Documentation for @code{inspect} is accessed by typing @kbd{help} at
738 the @code{inspect} prompt.
740 @node Resolution of Name Conflicts
741 @section Resolution of Name Conflicts
742 @tindex @sbext{name-conflict}
743 @findex @sbext{name-conflict-symbols}
745 The ANSI standard (section 11.1.1.2.5) requires that name conflicts in
746 packages be resolvable in favour of any of the conflicting symbols.  In
747 the interactive debugger, this is achieved by prompting for the symbol
748 in whose favour the conflict should be resolved; for programmatic use,
749 the @code{sb-ext:resolve-conflict} restart should be invoked with one
750 argument, which should be a member of the list returned by the condition
751 accessor @code{sb-ext:name-conflict-symbols}.
753 @node    Hash Table Extensions
754 @comment  node-name,  next,  previous,  up
755 @section Hash Table Extensions
756 @cindex Hash tables
758 Hash table extensions supported by SBCL are all controlled by keyword
759 arguments to @code{make-hash-table}.
761 @include fun-common-lisp-make-hash-table.texinfo
763 @include macro-sb-ext-define-hash-table-test.texinfo
765 @include macro-sb-ext-with-locked-hash-table.texinfo
767 @include fun-sb-ext-hash-table-synchronized-p.texinfo
769 @include fun-sb-ext-hash-table-weakness.texinfo
771 @node    Random Number Generation
772 @comment  node-name,  next,  previous,  up
773 @section Random Number Generation
774 @cindex Random Number Generation
776 The initial value of @code{*random-state*} is the same each time SBCL
777 is started. This makes it possible for user code to obtain repeatable
778 pseudo random numbers using only standard-provided functionality. See
779 @code{seed-random-state} below for an SBCL extension that allows to
780 seed the random number generator from given data for an additional
781 possibility to achieve this. Non-repeatable random numbers can always
782 be obtained using @code{(make-random-state t)}.
784 The sequence of numbers produced by repeated calls to @code{random}
785 starting with the same random state and using the same sequence of
786 @code{limit} arguments is guaranteed to be reproducible only in the
787 same version of SBCL on the same platform, using the same code under
788 the same evaluator mode and compiler optimization qualities. Just two
789 examples of differences that may occur otherwise: calls to
790 @code{random} can be compiled differently depending on how much is
791 known about the @code{limit} argument at compile time, yielding
792 different results even if called with the same argument at run time,
793 and the results can differ depending on the machine's word size, for
794 example for limits that are fixnums under 64-bit word size but bignums
795 under 32-bit word size.
797 @include fun-sb-ext-seed-random-state.texinfo
799 Some notes on random floats: The standard doesn't prescribe a specific
800 method of generating random floats. The following paragraph describes
801 SBCL's current implementation and should be taken purely informational,
802 that is, user code should not depend on any of its specific properties.
803 The method used has been chosen because it is common, conceptually
804 simple and fast.
806 To generate random floats, SBCL evaluates code that has an equivalent
807 effect as
808 @lisp
809 (* limit
810    (float (/ (random (expt 2 23)) (expt 2 23)) 1.0f0))
811 @end lisp
812 (for single-floats) and correspondingly (with @code{52} and
813 @code{1.0d0} instead of @code{23} and @code{1.0f0}) for double-floats.
814 Note especially that this means that zero is a possible return value
815 occurring with probability @code{(expt 2 -23)} respectively
816 @code{(expt 2 -52)}. Also note that there exist twice as many
817 equidistant floats between 0 and 1 as are generated. For example, the
818 largest number that @code{(random 1.0f0)} ever returns is
819 @code{(float (/ (1- (expt 2 23)) (expt 2 23)) 1.0f0)} while
820 @code{(float (/ (1- (expt 2 24)) (expt 2 24)) 1.0f0)} is the
821 largest single-float less than 1. This is a side effect of the fact
822 that the implementation uses the fastest possible conversion from bits
823 to floats.
825 SBCL currently uses the Mersenne Twister as its random number
826 generator, specifically the 32-bit version under both 32- and 64-bit
827 word size. The seeding algorithm has been improved several times by
828 the authors of the Mersenne Twister; SBCL uses the third version
829 (from 2002) which is still the most recent as of June 2012. The
830 implementation has been tested to provide output identical to the
831 recommended C implementation.
833 While the Mersenne Twister generates random numbers of much better
834 statistical quality than other widely used generators, it uses only
835 linear operations modulo 2 and thus fails some statistical
836 tests@footnote{See chapter 7 "Testing widely used RNGs" in
837 @cite{TestU01: A C Library for Empirical Testing of Random Number
838 Generators} by Pierre L'Ecuyer and Richard Simard, ACM Transactions on
839 Mathematical Software, Vol. 33, article 22, 2007.}.
840 For example, the distribution of ranks of (sufficiently large) random
841 binary matrices is much distorted compared to the theoretically
842 expected one when the matrices are generated by the Mersenne Twister.
843 Thus, applications that are sensitive to this aspect should use a
844 different type of generator.
846 @node    Miscellaneous Extensions
847 @comment  node-name,  next,  previous,  up
848 @section Miscellaneous Extensions
850 @include fun-sb-ext-array-storage-vector.texinfo
851 @include fun-sb-ext-delete-directory.texinfo
852 @include fun-sb-ext-get-time-of-day.texinfo
853 @include macro-sb-ext-wait-for.texinfo
854 @include fun-sb-ext-assert-version-gt=.texinfo
856 @node Stale Extensions
857 @comment  node-name,  next,  previous,  up
858 @section Stale Extensions
860 SBCL has inherited from CMUCL various hooks to allow the user to
861 tweak and monitor the garbage collection process. These are somewhat
862 stale code, and their interface might need to be cleaned up. If you
863 have urgent need of them, look at the code in @file{src/code/gc.lisp}
864 and bring it up on the developers' mailing list.
866 SBCL has various hooks inherited from CMUCL, like
867 @code{sb-ext:float-denormalized-p}, to allow a program to take
868 advantage of IEEE floating point arithmetic properties which aren't
869 conveniently or efficiently expressible using the ANSI standard. These
870 look good, and their interface looks good, but IEEE support is
871 slightly broken due to a stupid decision to remove some support for
872 infinities (because it wasn't in the ANSI spec and it didn't occur to
873 me that it was in the IEEE spec). If you need this stuff, take a look
874 at the code and bring it up on the developers' mailing
875 list.
878 @node  Efficiency Hacks
879 @comment  node-name,  next,  previous,  up
880 @section Efficiency Hacks
882 The @code{sb-ext:purify} function causes SBCL first to collect all
883 garbage, then to mark all uncollected objects as permanent, never again
884 attempting to collect them as garbage. This can cause a large increase
885 in efficiency when using a primitive garbage collector, or a more
886 moderate increase in efficiency when using a more sophisticated garbage
887 collector which is well suited to the program's memory usage pattern. It
888 also allows permanent code to be frozen at fixed addresses, a
889 precondition for using copy-on-write to share code between multiple Lisp
890 processes.  This is less important with modern generational garbage
891 collectors, but not all SBCL platforms use such a garbage collector.
893 @include fun-sb-ext-purify.texinfo
895 The @code{sb-ext:truly-the} special form declares the type of the
896 result of the operations, producing its argument; the declaration is
897 not checked. In short: don't use it.
899 @include special-operator-sb-ext-truly-the.texinfo
901 The @code{sb-ext:freeze-type} declaration declares that a
902 type will never change, which can make type testing
903 (@code{typep}, etc.) more efficient for structure types.