0.8.12.38:
[sbcl.git] / doc / manual / beyond-ansi.texinfo
1 @node  Beyond The ANSI Standard
2 @comment  node-name,  next,  previous,  up
3 @chapter Beyond The ANSI Standard
4
5 SBCL is mostly an implementation of the ANSI standard for
6 Common Lisp. However, there's some important behavior which extends
7 or clarifies the standard, and various behavior which outright
8 violates the standard.
9
10
11 @menu
12 * Non-Conformance With The ANSI Standard::  
13 * Idiosyncrasies::              
14 * Extensions::                  
15 @end menu
16
17 @node Non-Conformance With The ANSI Standard
18 @comment  node-name,  next,  previous,  up
19 @section Non-Conformance With The ANSI Standard
20
21 Essentially every type of non-conformance is considered a bug.  (The
22 exceptions involve internal inconsistencies in the standard.)  In SBCL
23 0.7.6, the master record of known bugs is in the @file{BUGS} file in
24 the distribution.  Some highlight information about bugs may also be
25 found in the manual page. The recommended way to report bugs is
26 through the sbcl-help or sbcl-devel mailing lists.  For mailing list
27 addresses, @xref{More SBCL Information}.
28
29
30 @node Idiosyncrasies
31 @comment  node-name,  next,  previous,  up
32 @section Idiosyncrasies
33
34 The information in this section describes some of the ways that SBCL
35 deals with choices that the ANSI standard leaves to the
36 implementation.
37
38 Declarations are generally treated as assertions. This general
39 principle, and its implications, and the bugs which still keep the
40 compiler from quite satisfying this principle, are discussed in
41 @ref{The Compiler}.
42
43 SBCL is essentially a compiler-only implementation of Common
44 Lisp. That is, for all but a few special cases, @code{eval} creates a
45 lambda expression, calls @code{compile} on the lambda expression to
46 create a compiled function, and then calls @code{funcall} on the
47 resulting function object. This is explicitly allowed by the ANSI
48 standard, but leads to some oddities, e.g. collapsing @code{functionp}
49 and @code{compiled-function-p} into the same predicate.
50
51 @findex defconstant
52 SBCL is quite strict about ANSI's definition of
53 @code{defconstant}. ANSI says that doing @code{defconstant} of the
54 same symbol more than once is undefined unless the new value is
55 @code{eql} to the old value. Conforming to this specification is a
56 nuisance when the ``constant'' value is only constant under some
57 weaker test like @code{string=} or @code{equal}. It's especially
58 annoying because, in SBCL, @code{defconstant} takes effect not only at
59 load time but also at compile time, so that just compiling and loading
60 reasonable code like
61 @lisp
62 (defconstant +foobyte+ '(1 4))
63 @end lisp
64 runs into this undefined behavior. Many implementations of Common Lisp
65 try to help the programmer around this annoyance by silently accepting
66 the undefined code and trying to do what the programmer probably
67 meant. SBCL instead treats the undefined behavior as an error. Often
68 such code can be rewritten in portable ANSI Common Lisp which has the
69 desired behavior.  E.g., the code above can be given an exactly
70 defined meaning by replacing @code{defconstant} either with
71 @code{defparameter} or with a customized macro which does the right
72 thing, possibly along the lines of the @code{defconstant-eqx} macro
73 used internally in the implementation of SBCL itself.  In
74 circumstances where this is not appropriate, the programmer can handle
75 the condition type @code{sb-ext:defconstant-uneql}, and choose either
76 the @command{continue} or @command{abort} restart as appropriate.
77
78 SBCL gives style warnings about various kinds of perfectly legal code,
79 e.g.
80
81 @itemize
82   
83 @item
84 @code{defmethod} without a preceding @code{defgeneric};
85   
86 @item
87 multiple @code{defun}s of the same symbol in different units;
88   
89 @item
90 special variables not named in the conventional @code{*foo*} style,
91 and lexical variables unconventionally named in the @code{*foo*} style
92
93 @end itemize
94
95 This causes friction with people who point out that other ways of
96 organizing code (especially avoiding the use of @code{defgeneric}) are
97 just as aesthetically stylish.  However, these warnings should be read
98 not as ``warning, bad aesthetics detected, you have no style'' but
99 ``warning, this style keeps the compiler from understanding the code
100 as well as you might like.'' That is, unless the compiler warns about
101 such conditions, there's no way for the compiler to warn about some
102 programming errors which would otherwise be easy to overlook. (Related
103 bug: The warning about multiple @code{defun}s is pointlessly annoying
104 when you compile and then load a function containing @code{defun}
105 wrapped in @code{eval-when}, and ideally should be suppressed in that
106 case, but still isn't as of SBCL 0.7.6.)
107
108
109 @node  Extensions
110 @comment  node-name,  next,  previous,  up
111 @section Extensions
112
113 SBCL is derived from CMUCL, which implements many extensions to the
114 ANSI standard. SBCL doesn't support as many extensions as CMUCL, but
115 it still has quite a few.  @xref{Contributed Modules}.
116
117
118 @menu
119 * Things Which Might Be In The Next ANSI Standard::  
120 * Threading::                   
121 * Support For Unix::            
122 * Customization Hooks for Users::  
123 * Tools To Help Developers::    
124 * Interface To Low-Level SBCL Implementation::  
125 * Stale Extensions::            
126 * Efficiency Hacks::            
127 @end menu
128
129 @node  Things Which Might Be In The Next ANSI Standard
130 @comment  node-name,  next,  previous,  up
131 @subsection Things Which Might Be In The Next ANSI Standard
132
133 SBCL provides extensive support for calling external C code, @ref{The
134 Foreign Function Interface}.
135
136 SBCL provides additional garbage collection functionality not
137 specified by ANSI. Weak pointers allow references to objects to be
138 maintained without keeping them from being GCed (garbage
139 collected). And ``finalization'' hooks are available to cause code to
140 be executed when an object has been GCed.
141 @c <!-- FIXME: Actually documenting these would be good.:-| -->
142
143 SBCL supports @dfn{Gray streams}, user-overloadable CLOS classes whose
144 instances can be used as Lisp streams (e.g. passed as the first
145 argument to @code{format}).  Additionally, the bundled contrib module
146 @dfn{sb-simple-streams} implements a subset of the Franz Allegro
147 simple-streams proposal.
148
149 SBCL supports a MetaObject Protocol which is intended to be compatible
150 with AMOP; present exceptions to this (as distinct from current bugs)
151 are:
152
153 @itemize
154   
155 @item
156 the abstract @code{metaobject} class is not present in the class
157 hierarchy;
158   
159 @item
160 the @code{standard-object} and @code{funcallable-standard-object}
161 classes are disjoint;
162   
163 @item
164 @code{compute-effective-method} only returns one value, not two;
165   
166 @item
167 the system-supplied @code{:around} method for @code{compute-slots}
168 specialized on @code{funcallable-standard-class} does not respect the
169 requested order from a user-supplied primary method.
170
171 @end itemize
172
173
174 @node  Threading
175 @comment  node-name,  next,  previous,  up
176 @subsection Threading (a.k.a Multiprocessing)
177
178 SBCL (as of version 0.8.3, on Linux x86 only) supports a fairly
179 low-level threading interface that maps onto the host operating
180 system's concept of threads or lightweight processes.
181
182 @subsubsection Lisp-level view
183
184 A rudimentary interface to creating and managing multiple threads can
185 be found in the @dfn{sb-thread} package.  This is intended for public
186 consumption, so look at the exported symbols and their documentation
187 strings.
188
189 Dynamic bindings to symbols are per-thread.  Signal handlers are
190 per-thread.
191
192 Mutexes and condition variables are available for managing access to
193 shared data: see
194
195 @itemize
196
197 @item
198 @code{(apropos "mutex" :sb-thread)}
199   
200 @item
201 @code{(apropos "condition" :sb-thread)}
202   
203 @item
204 and the @code{waitqueue} structure
205
206 @end itemize
207
208 and poke around in their documentation strings.
209
210 @subsubsection Sessions
211
212 If the user has multiple views onto the same Lisp image (for example,
213 using multiple terminals, or a windowing system, or network access)
214 they are typically set up as multiple @dfn{sessions} such that each
215 view has its own collection of foreground/background/stopped threads.
216 A thread which wishes to create a new session can use
217 @code{sb-thread:with-new-session} to remove itself from the current
218 session (which it shares with its parent and siblings) and create a
219 fresh one.  See also @code{sb-thread:make-listener-thread}.
220
221 Within a single session, threads arbitrate between themselves for the
222 user's attention.  A thread may be in one of three notional states:
223 foreground, background, or stopped.  When a background process
224 attempts to print a repl prompt or to enter the debugger, it will stop
225 and print a message saying that it has stopped.  The user at his
226 leisure may switch to that thread to find out what it needs.  If a
227 background thread enters the debugger, selecting any restart will put
228 it back into the background before it resumes.  Arbitration for the
229 input stream is managed by calls to @code{sb-thread:get-foreground}
230 (which may block) and @code{sb-thread:release-foreground}.
231
232 @code{sb-ext:quit} terminates all threads in the current session, but
233 leaves other sessions running.
234
235
236 @subsubsection Implementation (Linux x86)
237
238 On Linux x86, threading is implemented using @code{clone()} and does
239 not involve pthreads.  This is not because there is anything wrong
240 with pthreads @emph{per se}, but there is plenty wrong (from our
241 perspective) with LinuxThreads.  SBCL threads are mapped 1:1 onto
242 Linux tasks which share a VM but nothing else - each has its own
243 process id and can be seen in e.g. @command{ps} output.
244
245 Per-thread local bindings for special variables is achieved using the
246 %fs segment register to point to a per-thread storage area.  This may
247 cause interesting results if you link to foreign code that expects
248 threading or creates new threads, and the thread library in question
249 uses %fs in an incompatible way.
250
251 There are two implementation mechanisms for queueing.  If SBCL was
252 built on an NPTL-capable Linux system (2.6 or some vendor 2.4 ports)
253 with the @code{:SB-FUTEX} feature, queuing will be done using the
254 @code{sys_futex()} system call if it's available at runtime.
255 Otherwise it will fall back to using @code{sigtimedwait()} to sleep
256 and a signal (@code{SIG_DEQUEUE}, one of the POSIX RT signals) to
257 wake.
258
259 Garbage collection is done with the existing Conservative Generational
260 GC.  Allocation is done in small (typically 8k) regions: each thread
261 has its own region so this involves no stopping. However, when a
262 region fills, a lock must be obtained while another is allocated, and
263 when a collection is required, all processes are stopped.  This is
264 achieved by sending them signals, which may make for interesting
265 behaviour if they are interrupted in system calls.  The streams
266 interface is believed to handle the required system call restarting
267 correctly, but this may be a consideration when making other blocking
268 calls e.g. from foreign library code.
269
270 Large amounts of the SBCL library have not been inspected for
271 thread-safety.  Some of the obviously unsafe areas have large locks
272 around them, so compilation and fasl loading, for example, cannot be
273 parallelized.  Work is ongoing in this area.
274
275 A new thread by default is created in the same POSIX process group and
276 session as the thread it was created by.  This has an impact on
277 keyboard interrupt handling: pressing your terminal's intr key
278 (typically @kbd{Control-C}) will interrupt all processes in the
279 foreground process group, including Lisp threads that SBCL considers
280 to be notionally `background'.  This is undesirable, so background
281 threads are set to ignore the SIGINT signal.
282
283 @code{sb-thread:make-listener-thread} in addition to creating a new
284 Lisp session makes a new POSIX session, so that pressing
285 @kbd{Control-C} in one window will not interrupt another listener -
286 this has been found to be embarrassing.
287
288
289 @node  Support For Unix
290 @comment  node-name,  next,  previous,  up
291 @subsection Support For Unix
292
293 The UNIX command line can be read from the variable
294 @code{sb-ext:*posix-argv*}. The UNIX environment can be queried with
295 the @code{sb-ext:posix-getenv} function.
296
297 @include fun-sb-ext-posix-getenv.texinfo
298
299 The SBCL system can be terminated with @code{sb-ext:quit}, (but see
300 notes in @ref{Threading} about the interaction between this feature and
301 sessions) optionally returning a specified numeric value to the
302 calling Unix process. The normal Unix idiom of terminating on end of
303 file on input is also supported.
304
305 @include fun-sb-ext-quit.texinfo
306
307 @node  Customization Hooks for Users
308 @comment  node-name,  next,  previous,  up
309 @subsection Customization Hooks for Users
310
311 The toplevel repl prompt may be customized, and the function
312 that reads user input may be replaced completely.
313 @c <!-- FIXME but I don't currently remember how -->
314
315 The behaviour of @code{require} when called with only one argument is
316 implementation-defined.  In SBCL, @code{require} behaves in the
317 following way:
318
319 @include fun-common-lisp-require.texinfo
320 @include var-sb-ext-star-module-provider-functions-star.texinfo
321
322 Although SBCL does not provide a resident editor, the @code{ed}
323 function can be customized to hook into user-provided editing
324 mechanisms as follows:
325
326 @include fun-common-lisp-ed.texinfo
327 @include var-sb-ext-star-ed-functions-star.texinfo
328
329 @node  Tools To Help Developers
330 @comment  node-name,  next,  previous,  up
331 @subsection Tools To Help Developers
332
333 SBCL provides a profiler and other extensions to the ANSI @code{trace}
334 facility.  For more information, see @ref{macro-common-lisp-trace}.
335
336 The debugger supports a number of options. Its documentation is
337 accessed by typing @kbd{help} at the debugger prompt.  @xref{The
338 Debugger}.
339
340 Documentation for @code{inspect} is accessed by typing @kbd{help} at
341 the @code{inspect} prompt.
342
343 @node  Interface To Low-Level SBCL Implementation
344 @comment  node-name,  next,  previous,  up
345 @subsection Interface To Low-Level SBCL Implementation
346
347 SBCL has the ability to save its state as a file for later
348 execution. This functionality is important for its bootstrapping
349 process, and is also provided as an extension to the user.  Note that
350 foreign libraries loaded via @code{load-shared-object} don't survive
351 this process; a core should not be saved in this case.
352
353 @emph{FIXME: what should be done for foreign libraries?}
354
355 @emph{FIXME: document load-shared-object somewhere - it's in
356 ffi.texinfo?}
357
358 @include fun-sb-ext-save-lisp-and-die.texinfo
359
360
361 @node Stale Extensions
362 @comment  node-name,  next,  previous,  up
363 @subsection Stale Extensions
364
365 SBCL has inherited from CMUCL various hooks to allow the user to
366 tweak and monitor the garbage collection process. These are somewhat
367 stale code, and their interface might need to be cleaned up. If you
368 have urgent need of them, look at the code in @file{src/code/gc.lisp}
369 and bring it up on the developers' mailing list.
370
371 SBCL has various hooks inherited from CMUCL, like
372 @code{sb-ext:float-denormalized-p}, to allow a program to take
373 advantage of IEEE floating point arithmetic properties which aren't
374 conveniently or efficiently expressible using the ANSI standard. These
375 look good, and their interface looks good, but IEEE support is
376 slightly broken due to a stupid decision to remove some support for
377 infinities (because it wasn't in the ANSI spec and it didn't occur to
378 me that it was in the IEEE spec). If you need this stuff, take a look
379 at the code and bring it up on the developers' mailing
380 list.
381
382
383 @node  Efficiency Hacks
384 @comment  node-name,  next,  previous,  up
385 @subsection Efficiency Hacks
386
387 The @code{sb-ext:purify} function causes SBCL first to collect all
388 garbage, then to mark all uncollected objects as permanent, never
389 again attempting to collect them as garbage. This can cause a large
390 increase in efficiency when using a primitive garbage collector, or a
391 more moderate increase in efficiency when using a more sophisticated
392 garbage collector which is well suited to the program's memory usage
393 pattern. It also allows permanent code to be frozen at fixed
394 addresses, a precondition for using copy-on-write to share code
395 between multiple Lisp processes. it is less important with modern
396 generational garbage collectors.
397
398 @include fun-sb-ext-purify.texinfo
399
400 The @code{sb-ext:truly-the} special form declares the type of the
401 result of the operations, producing its argument; the declaration is
402 not checked. In short: don't use it.
403
404 @include special-operator-sb-ext-truly-the.texinfo
405
406 The @code{sb-ext:freeze-type} declaration declares that a
407 type will never change, which can make type testing
408 (@code{typep}, etc.) more efficient for structure types.
409
410 The @code{sb-ext:constant-function} declaration specifies
411 that a function will always return the same value for the same
412 arguments, which may allow the compiler to optimize calls
413 to it. This is appropriate for functions like @code{sqrt}, but
414 is @emph{not} appropriate for functions like @code{aref},
415 which can change their return values when the underlying data are
416 changed.
417 @c <!-- FIXME: This declaration does not seem to be supported in the 
418 @c      current compiler. -->