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