More incorrect next_free_page usage.
[sbcl.git] / doc / manual / intro.texinfo
blobe0f4c4f8360dcf3b5043e10865b686802e587436
1 @node Introduction
2 @comment  node-name,  next,  previous,  up
3 @chapter Introduction
5 SBCL is a mostly-conforming implementation of the ANSI Common Lisp
6 standard. This manual focuses on behavior which is specific to SBCL,
7 not on behavior which is common to all implementations of ANSI Common
8 Lisp.
10 @menu
11 * ANSI Conformance::
12 * Extensions::
13 * Idiosyncrasies::
14 * Development Tools::
15 * More SBCL Information::
16 * More Common Lisp Information::
17 * History and Implementation of SBCL::
18 @end menu
22 @node ANSI Conformance
23 @comment  node-name,  next,  previous,  up
24 @section ANSI Conformance
26 Essentially every type of non-conformance is considered a bug. (The
27 exceptions involve internal inconsistencies in the standard.)
28 @xref{Reporting Bugs}.
30 @subsection Exceptions
32 @itemize
34 @item
35 @findex @cl{prog2}
36 @code{prog2} returns the primary value of its second form, as
37 specified in the @strong{Arguments and Values} section of the
38 specification for that operator, not that of its first form, as
39 specified in the @strong{Description}.
41 @item
42 @tindex @cl{string}
43 @tindex @cl{character}
44 @tindex @cl{nil}
45 The @code{string} type is considered to be the union of all types
46 @code{(array @emph{c} (@emph{size}))} for all non-@code{nil} subtypes @code{@emph{c}} of
47 @code{character}, excluding arrays specialized to the empty type.
49 @item
50 @findex @cl{define-method-combination}
51 @vindex @cl{nil}
52 The @code{:order} long form option in @code{define-method-combination}
53 method group specifiers accepts the value @code{nil} as well as
54 @code{:most-specific-first} and @code{:most-specific-last}, in order
55 to allow programmers to declare that the order of methods playing that
56 role in the method combination does not matter.
58 @end itemize
60 @node Extensions
61 @comment  node-name,  next,  previous,  up
62 @section Extensions
64 SBCL comes with numerous extensions, some in core and some in modules
65 loadable with @code{require}. Unfortunately, not all of these
66 extensions have proper documentation yet.
68 @c FIXME: Once bits and pieces referred to here get real documentation
69 @c add xrefs there.
71 @table @strong
73 @item System Definition Tool
74 @code{asdf} is a flexible and popular protocol-oriented system
75 definition tool by Daniel Barlow. @inforef{Top,the asdf manual,asdf}, for
76 more information.
78 @item Foreign Function Interface
79 @code{sb-alien} package allows interfacing with C-code, loading shared
80 object files, etc. @xref{Foreign Function Interface}.
82 @code{sb-grovel} can be used to partially automate generation of
83 foreign function interface definitions. @xref{sb-grovel}.
85 @item Recursive Event Loop
86 SBCL provides a recursive event loop (@code{serve-event}) for doing
87 non-blocking IO on multiple streams without using threads.
89 @item Timeouts and Deadlines
90 SBCL allows restricting the execution time of individual operations or
91 parts of a computation using @code{:timeout} arguments to certain
92 blocking operations, synchronous timeouts and asynchronous timeouts. The
93 latter two affect operations without explicit timeout support (such as
94 standard functions and macros). @xref{Timeouts and Deadlines}.
96 @item Metaobject Protocol
97 @code{sb-mop} package provides a metaobject protocol for the Common
98 Lisp Object System as described in @cite{Art of Metaobject Protocol}.
100 @item Extensible Sequences
101 SBCL allows users to define subclasses of the @code{sequence}
102 class. @xref{Extensible Sequences}.
104 @item Native Threads
105 SBCL has native threads on x86/Linux, capable of taking advantage
106 of SMP on multiprocessor machines. @xref{Threading}.
108 @item Network Interface
109 @code{sb-bsd-sockets} is a low-level networking interface, providing
110 both TCP and UDP sockets. @xref{Networking}.
112 @item Introspective Facilities
113 @code{sb-introspect} module offers numerous introspective extensions,
114 including access to function lambda-lists and a cross referencing
115 facility.
117 @item Operating System Interface
118 @code{sb-ext} contains a number of functions for running external
119 processes, accessing environment variables, etc.
121 @code{sb-posix} module provides a lispy interface to standard POSIX
122 facilities.
124 @item Extensible Streams
125 @code{sb-gray} is an implementation of @emph{Gray Streams}. @xref{Gray
126 Streams}.
128 @code{sb-simple-streams} is an implementation of the @emph{simple
129 streams} API proposed by Franz Inc. @xref{Simple Streams}.
131 @item Profiling
132 @code{sb-profile} is a exact per-function profiler. @xref{Deterministic
133 Profiler}.
135 @code{sb-sprof} is a statistical profiler, capable of call-graph
136 generation and instruction level profiling, which also supports
137 allocation profiling. @xref{Statistical Profiler}.
139 @item Customization Hooks
140 SBCL contains a number of extra-standard customization hooks that
141 can be used to tweak the behaviour of the system. @xref{Customization
142 Hooks for Users}.
144 @code{sb-aclrepl} provides an Allegro CL -style toplevel for SBCL,
145 as an alternative to the classic CMUCL-style one. @xref{sb-aclrepl}.
147 @item CLTL2 Compatibility Layer
148 @code{sb-cltl2} module provides @code{compiler-let} and environment
149 access functionality described in @cite{Common Lisp The Language, 2nd
150 Edition} which were removed from the language during the ANSI
151 standardization process.
153 @item Executable Delivery
154 The @code{:executable} argument to @ref{Function
155 sb-ext save-lisp-and-die} can produce a `standalone' executable
156 containing both an image of the current Lisp session and an SBCL
157 runtime.
159 @item Bitwise Rotation
160 @code{sb-rotate-byte} provides an efficient primitive for bitwise
161 rotation of integers, an operation required by e.g. numerous
162 cryptographic algorithms, but not available as a primitive in ANSI
163 Common Lisp. @xref{sb-rotate-byte}.
165 @item Test Harness
166 @code{sb-rt} module is a simple yet attractive regression and
167 unit-test framework.
169 @item MD5 Sums
170 @code{sb-md5} is an implementation of the MD5 message digest algorithm
171 for Common Lisp, using the modular arithmetic optimizations provided
172 by SBCL. @xref{sb-md5}.
174 @end table
179 @node Idiosyncrasies
180 @comment  node-name,  next,  previous,  up
181 @section Idiosyncrasies
183 The information in this section describes some of the ways that SBCL
184 deals with choices that the ANSI standard leaves to the
185 implementation.
187 @menu
188 * Declarations::
189 * FASL Format::
190 * Compiler-only Implementation::
191 * Defining Constants::
192 * Style Warnings::
193 @end menu
195 @node Declarations
196 @comment  node-name,  next,  previous,  up
197 @subsection Declarations
199 Declarations are generally treated as assertions. This general
200 principle, and its implications, and the bugs which still keep the
201 compiler from quite satisfying this principle, are discussed in
202 @ref{Declarations as Assertions}.
205 @node FASL Format
206 @comment  node-name,  next,  previous,  up
207 @subsection FASL Format
209 SBCL fasl-format is binary compatible only with the exact SBCL version
210 it was generated with. While this is obviously suboptimal, it has
211 proven more robust than trying to maintain fasl compatibility across
212 versions: accidentally breaking things is far too easy, and can lead
213 to hard to diagnose bugs.
215 The following snippet handles fasl recompilation automatically for
216 ASDF-based systems, and makes a good candidate for inclusion in
217 the user or system initialization file (@pxref{Initialization Files}.)
219 @lisp
220 (require :asdf)
222 ;;; If a fasl was stale, try to recompile and load (once).
223 (defmethod asdf:perform :around ((o asdf:load-op)
224                                  (c asdf:cl-source-file))
225    (handler-case (call-next-method o c)
226       ;; If a fasl was stale, try to recompile and load (once).
227       (sb-ext:invalid-fasl ()
228          (asdf:perform (make-instance 'asdf:compile-op) c)
229          (call-next-method))))
230 @end lisp
233 @node Compiler-only Implementation
234 @comment  node-name,  next,  previous,  up
235 @subsection Compiler-only Implementation
237 SBCL is essentially a compiler-only implementation of Common Lisp.
238 That is, for all but a few special cases, @code{eval} creates a lambda
239 expression, calls @code{compile} on the lambda expression to create a
240 compiled function, and then calls @code{funcall} on the resulting
241 function object. A more traditional interpreter is also available on
242 default builds; it is usually only called internally.  This is
243 explicitly allowed by the ANSI standard, but leads to some oddities;
244 e.g. at default settings, @code{functionp} and
245 @code{compiled-function-p} are equivalent, and they collapse into the
246 same function when SBCL is built without the interpreter.
248 @node Defining Constants
249 @comment  node-name,  next,  previous,  up
250 @subsection Defining Constants
251 @findex @cl{defconstant}
253 SBCL is quite strict about ANSI's definition of @code{defconstant}.
254 ANSI says that doing @code{defconstant} of the same symbol more than
255 once is undefined unless the new value is @code{eql} to the old value.
256 Conforming to this specification is a nuisance when the ``constant''
257 value is only constant under some weaker test like @code{string=} or
258 @code{equal}.
260 It's especially annoying because, in SBCL, @code{defconstant} takes
261 effect not only at load time but also at compile time, so that just
262 compiling and loading reasonable code like
263 @lisp
264 (defconstant +foobyte+ '(1 4))
265 @end lisp
266 runs into this undefined behavior. Many implementations of Common Lisp
267 try to help the programmer around this annoyance by silently accepting
268 the undefined code and trying to do what the programmer probably
269 meant.
271 SBCL instead treats the undefined behavior as an error. Often such
272 code can be rewritten in portable ANSI Common Lisp which has the
273 desired behavior. E.g., the code above can be given an exactly defined
274 meaning by replacing @code{defconstant} either with
275 @code{defparameter} or with a customized macro which does the right
276 thing, e.g.
277 @lisp
278 (defmacro define-constant (name value &optional doc)
279   `(defconstant ,name (if (boundp ',name) (symbol-value ',name) ,value)
280                       ,@@(when doc (list doc))))
281 @end lisp
282 or possibly along the lines of the @code{defconstant-eqx} macro used
283 internally in the implementation of SBCL itself. In circumstances
284 where this is not appropriate, the programmer can handle the condition
285 type @code{sb-ext:defconstant-uneql}, and choose either the
286 @command{continue} or @command{abort} restart as appropriate.
288 @node Style Warnings
289 @comment  node-name,  next,  previous,  up
290 @subsection Style Warnings
292 SBCL gives style warnings about various kinds of perfectly legal code,
293 e.g.
295 @itemize
297 @item
298 multiple @code{defun}s of the same symbol in different units;
300 @item
301 special variables not named in the conventional @code{*foo*} style,
302 and lexical variables unconventionally named in the @code{*foo*} style
304 @end itemize
306 This causes friction with people who point out that other ways of
307 organizing code (especially avoiding the use of @code{defgeneric}) are
308 just as aesthetically stylish.  However, these warnings should be read
309 not as ``warning, bad aesthetics detected, you have no style'' but
310 ``warning, this style keeps the compiler from understanding the code
311 as well as you might like.'' That is, unless the compiler warns about
312 such conditions, there's no way for the compiler to warn about some
313 programming errors which would otherwise be easy to overlook. (Related
314 bug: The warning about multiple @code{defun}s is pointlessly annoying
315 when you compile and then load a function containing @code{defun}
316 wrapped in @code{eval-when}, and ideally should be suppressed in that
317 case, but still isn't as of SBCL 0.7.6.)
322 @node Development Tools
323 @comment  node-name,  next,  previous,  up
324 @section Development Tools
326 @menu
327 * Editor Integration::
328 * Language Reference::
329 * Generating Executables::
330 @end menu
332 @node Editor Integration
333 @comment  node-name,  next,  previous,  up
334 @subsection Editor Integration
336 Though SBCL can be used running ``bare'', the recommended mode of
337 development is with an editor connected to SBCL, supporting not
338 only basic lisp editing (paren-matching, etc), but providing among
339 other features an integrated debugger, interactive compilation, and
340 automated documentation lookup.
342 Currently @dfn{SLIME}@footnote{Historically, the ILISP package at
343 @uref{http://ilisp.cons.org/} provided similar functionality, but it
344 does not support modern SBCL versions.} (Superior Lisp Interaction
345 Mode for Emacs) together with Emacs is recommended for use with
346 SBCL, though other options exist as well.
348 SLIME can be downloaded from
349 @uref{http://www.common-lisp.net/project/slime/}.
351 @node Language Reference
352 @comment  node-name,  next,  previous,  up
353 @subsection Language Reference
355 @dfn{CLHS} (Common Lisp Hyperspec) is a hypertext version of the ANSI
356 standard, made freely available by @emph{LispWorks} -- an invaluable
357 reference.
359 See: @uref{http://www.lispworks.com/reference/HyperSpec/index.html}
361 @node Generating Executables
362 @comment  node-name,  next,  previous,  up
363 @subsection Generating Executables
365 SBCL can generate stand-alone executables.  The generated executables
366 include the SBCL runtime itself, so no restrictions are placed on
367 program functionality.  For example, a deployed program can call
368 @code{compile} and @code{load}, which requires the compiler to be present
369 in the executable.  For further information, @xref{Function
370 sb-ext save-lisp-and-die}.
373 @node More SBCL Information
374 @comment  node-name,  next,  previous,  up
375 @section More SBCL Information
377 @menu
378 * SBCL Homepage::
379 * Online Documentation::
380 * Additional Documentation Files::
381 * Internals Documentation::
382 @end menu
384 @node SBCL Homepage
385 @comment  node-name,  next,  previous,  up
386 @subsection SBCL Homepage
388 The SBCL website at @uref{http://www.sbcl.org/} has some general
389 information, plus links to mailing lists devoted to SBCL, and to
390 archives of these mailing lists. Subscribing to the mailing lists
391 @cite{sbcl-help} and @cite{sbcl-announce} is recommended: both are
392 fairly low-volume, and help you keep abreast with SBCL development.
394 @node Online Documentation
395 @comment  node-name,  next,  previous,  up
396 @subsection Online Documentation
398 Documentation for non-ANSI extensions for various commands is
399 available online from the SBCL executable itself. The extensions
400 for functions which have their own command prompts (e.g. the debugger,
401 and @code{inspect}) are documented in text available by typing
402 @command{help} at their command prompts. The extensions for functions
403 which don't have their own command prompt (such as @code{trace}) are
404 described in their documentation strings, unless your SBCL was
405 compiled with an option not to include documentation strings, in which
406 case the documentation strings are only readable in the source code.
408 @node Additional Documentation Files
409 @comment  node-name,  next,  previous,  up
410 @subsection Additional Documentation Files
412 Besides this user manual both SBCL source and binary distributions
413 include some other SBCL-specific documentation files, which should be
414 installed along with this manual on your system, e.g. in
415 @file{/usr/local/share/doc/sbcl/}.
417 @table @file
419 @item COPYING
420 Licence and copyright summary.
422 @item CREDITS
423 Authorship information on various parts of SBCL.
425 @item INSTALL
426 Covers installing SBCL from both source and binary distributions on
427 your system, and also has some installation related troubleshooting
428 information.
430 @item NEWS
431 Summarizes changes between various SBCL versions.
433 @end table
435 @node Internals Documentation
436 @comment  node-name,  next,  previous,  up
437 @subsection Internals Documentation
439 If you're interested in the development of the SBCL system itself,
440 then subscribing to @cite{sbcl-devel} is a good idea.
442 SBCL internals documentation -- besides comments in the source -- is
443 currently maintained as a @emph{wiki-like} website:
444 @uref{http://sbcl-internals.cliki.net/}.
446 Some low-level information describing the programming details of the
447 conversion from CMUCL to SBCL is available in the
448 @file{doc/FOR-CMUCL-DEVELOPERS} file in the SBCL distribution, though
449 it is not installed by default.
451 @node More Common Lisp Information
452 @comment  node-name,  next,  previous,  up
453 @section More Common Lisp Information
455 @menu
456 * Internet Community::
457 * Third-party Libraries::
458 * Common Lisp Books::
459 @end menu
461 @node Internet Community
462 @comment  node-name,  next,  previous,  up
463 @subsection Internet Community
465 @c FIXME: Say something smart here
467 The Common Lisp internet community is fairly diverse:
468 @uref{news://comp.lang.lisp} is fairly high volume newsgroup, but has
469 a rather poor signal/noise ratio. Various special interest mailing
470 lists and IRC tend to provide more content and less flames.
471 @uref{http://www.lisp.org} and @uref{http://www.cliki.net} contain
472 numerous pointers places in the net where lispers talks shop.
474 @node Third-party Libraries
475 @comment  node-name,  next,  previous,  up
476 @subsection Third-party Libraries
478 For a wealth of information about free Common Lisp libraries and tools
479 we recommend checking out @emph{CLiki}: @uref{http://www.cliki.net/}.
481 @node Common Lisp Books
482 @comment  node-name,  next,  previous,  up
483 @subsection Common Lisp Books
485 If you're not a programmer and you're trying to learn, many
486 introductory Lisp books are available. However, we don't have any
487 standout favorites. If you can't decide, try checking the Usenet
488 @uref{news://comp.lang.lisp} FAQ for recent recommendations.
490 @c FIXME: This non-stance is silly. Maybe we could recommend SICP,
491 @c Touretzky, or something at least.
493 If you are an experienced programmer in other languages but need to
494 learn about Common Lisp, some books stand out:
496 @table @cite
498 @item Practical Common Lisp, by Peter Seibel
499 An excellent introduction to the language, covering both the basics
500 and ``advanced topics'' like macros, CLOS, and packages. Available
501 both in print format and on the web: @uref{http://www.gigamonkeys.com/book/}.
503 @item Paradigms Of Artificial Intelligence Programming, by Peter Norvig
504 Good information on general Common Lisp programming, and many
505 nontrivial examples. Whether or not your work is AI, it's a very good
506 book to look at.
508 @item On Lisp, by Paul Graham
509 An in-depth treatment of macros, but not recommended as a first Common
510 Lisp book, since it is slightly pre-ANSI so you need to be on your
511 guard against non-standard usages, and since it doesn't really even
512 try to cover the language as a whole, focusing solely on macros.
513 Downloadable from @uref{http://www.paulgraham.com/onlisp.html}.
515 @item Object-Oriented Programming In Common Lisp, by Sonya Keene
516 With the exception of @cite{Practical Common Lisp} most introductory
517 books don't emphasize CLOS. This one does. Even if you're very
518 knowledgeable about object oriented programming in the abstract, it's
519 worth looking at this book if you want to do any OO in Common Lisp.
520 Some abstractions in CLOS (especially multiple dispatch) go beyond
521 anything you'll see in most OO systems, and there are a number of
522 lesser differences as well. This book tends to help with the culture
523 shock.
525 @item Art Of Metaobject Programming, by Gregor Kiczales et al.
526 Currently the prime source of information on the Common Lisp Metaobject
527 Protocol, which is supported by SBCL. Section 2 (Chapters 5 and 6) are
528 freely available at @uref{http://mop.lisp.se/www.alu.org/mop/}.
530 @end table
535 @node History and Implementation of SBCL
536 @comment  node-name,  next,  previous,  up
537 @section History and Implementation of SBCL
539 You can work productively with SBCL without knowing or
540 understanding anything about where it came from, how it is
541 implemented, or how it extends the ANSI Common Lisp standard. However,
542 a little knowledge can be helpful in order to understand error
543 messages, to troubleshoot problems, to understand why some parts of
544 the system are better debugged than others, and to anticipate which
545 known bugs, known performance problems, and missing extensions are
546 likely to be fixed, tuned, or added.
548 SBCL is descended from CMUCL, which is itself descended from Spice
549 Lisp, including early implementations for the Mach operating system on
550 the IBM RT, back in the 1980s. Some design decisions from that time are
551 still reflected in the current implementation:
553 @itemize
555 @item
556 The system expects to be loaded into a fixed-at-compile-time location
557 in virtual memory, and also expects the location of all of its heap
558 storage to be specified at compile time.
560 @item
561 The system overcommits memory, allocating large amounts of address
562 space from the system (often more than the amount of virtual memory
563 available) and then failing if ends up using too much of the allocated
564 storage.
566 @item
567 The system is implemented as a C program which is responsible for
568 supplying low-level services and loading a Lisp @file{.core}
569 file.
571 @end itemize
573 @cindex Garbage Collection, generational
574 SBCL also inherited some newer architectural features from CMUCL. The
575 most important is that on some architectures it has a generational
576 garbage collector (``GC''), which has various implications (mostly
577 good) for performance. These are discussed in another chapter,
578 @ref{Efficiency}.
580 SBCL has diverged from CMUCL in that SBCL is now essentially a
581 ``compiler-only implementation'' of Common Lisp. This is a change in
582 implementation strategy, taking advantage of the freedom ``any of these
583 facilities might share the same execution strategy'' guaranteed in the
584 ANSI specification section 3.1 (``Evaluation''). It does not mean SBCL
585 can't be used interactively, and in fact the change is largely invisible
586 to the casual user, since SBCL still can and does execute code
587 interactively by compiling it on the fly. (It is visible if you know how
588 to look, like using @code{compiled-function-p}; and it is visible in the
589 way that SBCL doesn't have many bugs which behave differently in
590 interpreted code than in compiled code.) What it means is that in SBCL,
591 the @code{eval} function only truly ``interprets'' a few easy kinds of
592 forms, such as symbols which are @code{boundp}. More complicated forms
593 are evaluated by calling @code{compile} and then calling @code{funcall}
594 on the returned result.
596 The direct ancestor of SBCL is the x86 port of CMUCL. This port was in
597 some ways the most cobbled-together of all the CMUCL ports, since a
598 number of strange changes had to be made to support the register-poor
599 x86 architecture. Some things (like tracing and debugging) do not work
600 particularly well there. SBCL should be able to improve in these areas
601 (and has already improved in some other areas), but it takes a while.
603 @cindex Garbage Collection, conservative
604 On the x86 SBCL -- like the x86 port of CMUCL -- uses a
605 @emph{conservative} GC. This means that it doesn't maintain a strict
606 separation between tagged and untagged data, instead treating some
607 untagged data (e.g. raw floating point numbers) as possibly-tagged
608 data and so not collecting any Lisp objects that they point to. This
609 has some negative consequences for average time efficiency (though
610 possibly no worse than the negative consequences of trying to
611 implement an exact GC on a processor architecture as register-poor as
612 the X86) and also has potentially unlimited consequences for
613 worst-case memory efficiency. In practice, conservative garbage
614 collectors work reasonably well, not getting anywhere near the worst
615 case. But they can occasionally cause odd patterns of memory usage.
617 The fork from CMUCL was based on a major rewrite of the system
618 bootstrap process. CMUCL has for many years tolerated a very unusual
619 ``build'' procedure which doesn't actually build the complete system
620 from scratch, but instead progressively overwrites parts of a running
621 system with new versions. This quasi-build procedure can cause various
622 bizarre bootstrapping hangups, especially when a major change is made
623 to the system. It also makes the connection between the current source
624 code and the current executable more tenuous than in other software
625 systems -- it's easy to accidentally ``build'' a CMUCL system
626 containing characteristics not reflected in the current version of the
627 source code.
629 Other major changes since the fork from CMUCL include
631 @itemize
633 @item
634 SBCL has removed many CMUCL extensions, (e.g. IP networking,
635 remote procedure call, Unix system interface, and X11 interface) from
636 the core system. Most of these are available as contributed modules
637 (distributed with SBCL) or third-party modules instead.
639 @item
640 SBCL has deleted or deprecated some nonstandard features and code
641 complexity which helped efficiency at the price of
642 maintainability. For example, the SBCL compiler no longer implements
643 memory pooling internally (and so is simpler and more maintainable,
644 but generates more garbage and runs more slowly).
646 @end itemize