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