0.8.9.32:
[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 * 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
321 @include var-sb-ext-star-module-provider-functions-star.texinfo
322
323
324 @node  Tools To Help Developers
325 @comment  node-name,  next,  previous,  up
326 @subsection Tools To Help Developers
327
328 SBCL provides a profiler and other extensions to the ANSI @code{trace}
329 facility.  For more information, see @ref{macro-common-lisp-trace}.
330
331 The debugger supports a number of options. Its documentation is
332 accessed by typing @kbd{help} at the debugger prompt.  @xref{The
333 Debugger}.
334
335 Documentation for @code{inspect} is accessed by typing @kbd{help} at
336 the @code{inspect} prompt.
337
338
339 @node  Interface To Low-Level SBCL Implementation
340 @comment  node-name,  next,  previous,  up
341 @subsection Interface To Low-Level SBCL Implementation
342
343 SBCL has the ability to save its state as a file for later
344 execution. This functionality is important for its bootstrapping
345 process, and is also provided as an extension to the user. See the
346 documentation string for @code{sb-ext:save-lisp-and-die} for more
347 information.
348
349 @quotation
350 Note: SBCL has inherited from CMUCL various hooks to allow the user to
351 tweak and monitor the garbage collection process. These are somewhat
352 stale code, and their interface might need to be cleaned up. If you
353 have urgent need of them, look at the code in @file{src/code/gc.lisp}
354 and bring it up on the developers' mailing list.
355 @end quotation
356
357 @quotation
358 Note: SBCL has various hooks inherited from CMUCL, like
359 @code{sb-ext:float-denormalized-p}, to allow a program to take
360 advantage of IEEE floating point arithmetic properties which aren't
361 conveniently or efficiently expressible using the ANSI standard. These
362 look good, and their interface looks good, but IEEE support is
363 slightly broken due to a stupid decision to remove some support for
364 infinities (because it wasn't in the ANSI spec and it didn't occur to
365 me that it was in the IEEE spec). If you need this stuff, take a look
366 at the code and bring it up on the developers' mailing
367 list.
368 @end quotation
369
370
371 @node  Efficiency Hacks
372 @comment  node-name,  next,  previous,  up
373 @subsection Efficiency Hacks
374
375 The @code{sb-ext:purify} function causes SBCL first to collect all
376 garbage, then to mark all uncollected objects as permanent, never
377 again attempting to collect them as garbage. This can cause a large
378 increase in efficiency when using a primitive garbage collector, or a
379 more moderate increase in efficiency when using a more sophisticated
380 garbage collector which is well suited to the program's memory usage
381 pattern. It also allows permanent code to be frozen at fixed
382 addresses, a precondition for using copy-on-write to share code
383 between multiple Lisp processes. it is less important with modern
384 generational garbage collectors.
385
386 @include fun-sb-ext-purify.texinfo
387
388 @code{sb-ext:truly-the} special form declares the type of the result
389 of the operations, producing its argument; the declaration is not
390 checked. In short: don't use it.
391
392 @include special-operator-sb-ext-truly-the.texinfo
393
394 The @code{sb-ext:freeze-type} declaration declares that a
395 type will never change, which can make type testing
396 (@code{typep}, etc.) more efficient for structure types.
397
398 The @code{sb-ext:constant-function} declaration specifies
399 that a function will always return the same value for the same
400 arguments, which may allow the compiler to optimize calls
401 to it. This is appropriate for functions like @code{sqrt}, but
402 is @emph{not} appropriate for functions like @code{aref},
403 which can change their return values when the underlying data are
404 changed.
405 @c <!-- FIXME: This declaration does not seem to be supported in the 
406 @c      current compiler. -->