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