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