0.8alpha.0.2:
[sbcl/lichteblau.git] / doc / beyond-ansi.sgml
blobb1c0a39a32b301bf7aa883f11018530091b0faed
1 <!-- -*- mode: SGML; sgml-parent-document: ("user-manual.sgml" "BOOK") -*- -->
2 <chapter id="beyond-ansi"><title>Beyond The &ANSI; Standard</>
4 <para>&SBCL; is mostly an implementation of the &ANSI; standard for
5 Common Lisp. However, there's some important behavior which extends
6 or clarifies the standard, and various behavior which outright
7 violates the standard.
8 </para>
10 <sect1 id="non-conformance"><title>Non-Conformance With The &ANSI; Standard</>
12 <para>
13 Essentially every type of non-conformance is considered a bug.
14 (The exceptions involve internal inconsistencies in the standard.)
15 In &SBCL; 0.7.6, the master record of known bugs is in
16 the <filename>BUGS</> file in the distribution.
17 Some highlight information about bugs may also be found in the
18 manual page. The recommended way to report bugs is through the sbcl-help or
19 sbcl-devel mailings lists.
20 </para>
22 </sect1>
24 <sect1 id="idiosyncrasies"><title>Idiosyncrasies</>
26 <para>The information in this section describes some of the ways
27 that &SBCL; deals with choices that the &ANSI; standard
28 leaves to the implementation.</para>
30 <para>Declarations are generally treated as assertions. This general
31 principle, and its implications, and the bugs which still keep the
32 compiler from quite satisfying this principle, are discussed in the
33 <link linkend="compiler">chapter on the compiler</link>.</para>
35 <para>&SBCL; is essentially a compiler-only implementation of
36 &CommonLisp;. That is, for all but a few special cases,
37 <function>eval</> creates a
38 lambda expression, calls <function>compile</> on the lambda
39 expression to create a compiled function, and then calls
40 <function>funcall</> on the resulting function object. This
41 is explicitly allowed by the &ANSI; standard, but leads to some
42 oddities, e.g. collapsing <function>functionp</> and
43 <function>compiled-function-p</> into the same predicate.</para>
45 <para>&SBCL; is quite strict about ANSI's definition of
46 <function>defconstant</>. ANSI says that doing <function>defconstant</>
47 of the same symbol more than once is undefined unless the new value
48 is <function>eql</> to the old value. Conforming to this specification
49 is a nuisance when the "constant" value is only constant under some
50 weaker test like <function>string=</> or <function>equal</>. It's
51 especially annoying because <function>defconstant</> takes effect
52 not only at load time but also at compile time, so that just
53 compiling and loading reasonable code like
54 <programlisting>(defconstant +foobyte+ '(1 4))</>
55 runs into this undefined behavior. Many
56 implementations of Common Lisp try to help the programmer around
57 this annoyance by silently accepting the undefined code and
58 trying to do what the programmer probably meant. &SBCL; instead
59 treats the undefined behavior as an error. Often
60 such code can be rewritten
61 in portable &ANSI; Common Lisp which has the desired behavior.
62 E.g., the code above can be given an exactly defined meaning by replacing
63 <function>defconstant</> either with <function>defparameter</> or
64 with a customized macro which does the right thing, possibly along the
65 lines of the <function>defconstant-eqx</> macro used internally in the
66 implementation of SBCL itself.</para>
68 <para>&SBCL; gives style warnings about various kinds of perfectly
69 legal code, e.g.
70 <itemizedlist>
71 <listitem><para><function>defmethod</> without
72 <function>defgeneric</></para></listitem>
73 <listitem><para>multiple <function>defun</>s of the same
74 symbol</para></listitem>
75 <listitem><para>special variables not named in the conventional
76 <varname>*foo*</> style, and lexical variables unconventionally named
77 in the <varname>*foo*</> style</para></listitem>
78 </itemizedlist>
79 This causes friction with people
80 who point out that other ways of organizing code (especially
81 avoiding the use of <function>defgeneric</>)
82 are just as aesthetically stylish.
83 However, these warnings should be read not
84 as "warning, bad aesthetics detected, you have no style" but
85 "warning, this style keeps the compiler from understanding
86 the code as well as you might like." That is,
87 unless the compiler warns about such conditions, there's no
88 way for the compiler to warn
89 about some programming errors which would otherwise be
90 easy to overlook. (related bug: The warning about
91 multiple <function>defun</>s is pointlessly annoying when you compile
92 and then load a function containing <function>defun</> wrapped
93 in <function>eval-when</>, and ideally should be suppressed in
94 that case, but still isn't as of &SBCL; 0.7.6.)</para>
96 </sect1>
98 <sect1 id="extensions"><title>Extensions</>
100 <para>&SBCL; is derived from &CMUCL;, which implements many extensions
101 to the &ANSI; standard. &SBCL; doesn't support as many extensions as
102 &CMUCL;, but it still has quite a few.</para>
104 <sect2><title>Things Which Might Be In The Next &ANSI; Standard</>
106 <para>&SBCL; provides extensive support for
107 calling external C code, described
108 <link linkend="ffi">in its own chapter</link>.</para>
110 <para>&SBCL; provides additional garbage collection functionality not
111 specified by &ANSI;. Weak pointers allow references to objects to be
112 maintained without keeping them from being GCed. And "finalization"
113 hooks are available to cause code to be executed when an object is
114 GCed.</para> <!-- FIXME: Actually documenting these would be good.:-| -->
116 <para>&SBCL; supports Gray streams, user-overloadable CLOS classes
117 whose instances can be used as Lisp streams (e.g. passed as the
118 first argument to <function>format</>).</para>
120 <para>&SBCL; supports a MetaObject Protocol which is intended to be
121 compatible with &AMOP;; present exceptions to this (as distinct from
122 current bugs) are:
123 <itemizedlist>
124 <listitem><para>the abstract <classname>metaobject</> class is not
125 present in the class hierarchy;</para></listitem>
126 <listitem><para>the <classname>standard-object</> and
127 <classname>funcallable-standard-object</> classes are
128 disjoint;</para></listitem>
129 <listitem><para><function>compute-effective-method</> only returns
130 one value, not two;</para></listitem>
131 <listitem><para>the system-supplied <property>:around</> method for
132 <function>compute-slots</> specialized on
133 <classname>funcallable-standard-class</> does not respect the
134 requested order from a user-supplied primary method.
135 </itemizedlist>
137 </sect2>
139 <sect2><title>Threading (a.k.a Multiprocessing)</>
141 <para>&SBCL; (as of version 0.x.y, on Linux x86 only) supports a
142 fairly low-level threading interface that maps onto the host operating
143 system's concept of threads or lightweight processes.
145 <sect3><title>Lisp-level view</title>
147 <para>A rudimentary interface to creating and managing multiple threads
148 can be found in the <literal>sb-thread</literal> package. This is
149 intended for public consumption, so look at the exported symbols and
150 their documentation strings.
152 <para>Dynamic bindings to symbols are per-thread. Signal handlers
153 are per-thread.
155 <para><function>sb-ext:quit</function> exits the current thread, not
156 necessarily the whole environment. The environment will be shut down
157 when the last thread exits.
159 <para>Threads arbitrate between themselves for the user's attention.
160 A thread may be in one of three notional states: foreground,
161 background, or stopped. When a background process attempts to print a
162 repl prompt or to enter the debugger, it will stop and print a message
163 saying that it has stopped. The user at his leisure may switch to
164 that thread to find out what it needs. If a background thread enters
165 the debugger, selecting any restart will put it back into the
166 background before it resumes.
168 <para>If the user has multiple views onto the same Lisp image (for
169 example, using multiple terminals, or a windowing system, or network
170 access) they are typically set up as multiple `sessions' such that each
171 view has its own collection of foreground/background/stopped threads.
172 <function>sb-thread:make-listener-thread</function> can be used to
173 start a new thread in its own `session'.
175 <para>Mutexes and condition variables are available for
176 managing access to shared data: see
178 <itemizedlist>
179 <listitem>
180 <programlisting>(apropos "mutex" :sb-thread)</programlisting>
181 <listitem>
182 <programlisting>(apropos "condition" :sb-thread)</programlisting>
183 <listitem> <para>and the <structname>waitqueue</structname> structure
184 </para>
185 </listitem>
186 </itemizedlist>
188 and poke around in their documentation strings.
190 <sect3><title>Implementation (Linux x86)</title>
192 <para>On Linux x86, this is implemented using
193 <function>clone()</function> and does not involve pthreads. This is
194 not because there is anything wrong with pthreads <emphasis>per
195 se</emphasis>, but there is plenty wrong (from our perspective) with
196 LinuxThreads. &SBCL; threads are mapped 1:1 onto Linux tasks which
197 share a VM but nothing else - each has its own process id and can be
198 seen in e.g. <command>ps</command> output.
200 <para>Per-thread local bindings for special variables is achieved
201 using the %fs segment register to point to a per-thread storage area.
202 This may cause interesting results if you link to foreign code that
203 expects threading or creates new threads, and the thread library in
204 question uses %fs in an incompatible way.
206 <para>Threads waiting on queues (e.g. for locks or condition
207 variables) are put to sleep using <function>sigtimedwait()</function>
208 and woken with SIGCONT.
210 <para>&SBCL; at present will alway have at least two tasks running as
211 seen from Linux: when the first process has done startup
212 initialization (mapping files in place, installing signal handlers
213 etc) it creates a new thread to run the Lisp startup and initial listener.
214 The original thread is then used to run GC and to reap dead subthreads
215 when they exit.
217 <para>Garbage collection is done with the existing Conservative
218 Generational GC. Allocation is done in small (typically 8k) regions :
219 each thread has its own region so this involves no stopping. However,
220 when a region fills, a lock must be obtained while another is
221 allocated, and when a collection is required, all processes are
222 stopped. This is achieved using <function>ptrace()</function>, so you
223 should be very careful if you wish to examine an &SBCL; worker thread
224 using <command>strace</command>, <command>truss</command>,
225 <command>gdb</command> or similar. It may be prudent to disable GC
226 before doing so.
228 <para>Large amounts of the &SBCL; library have not been inspected for
229 thread-safety. Some of the obviously unsafe areas have large locks
230 around them, so compilation and fasl loading, for example, cannot be
231 parallelized. Work is ongoing in this area.
233 <para>A new thread by default is created in the same POSIX process
234 group and session as the thread it was created by. This has an impact
235 on keyboard interrupt handling: pressing your terminal's intr key
236 (typically Control-C) will interrupt all processes in the foreground
237 process group, including Lisp threads that &SBCL; considers to be
238 notionally `background'. This is undesirable, so background threads
239 are set to ignore the SIGINT signal. Arbitration for the input stream
240 is managed by locking on sb-thread::*session-lock*
242 <para>A thread can be created in a new Lisp 'session' (new terminal or
243 window) using <function>sb-thread:make-listener-thread</function>.
244 These sessions map directly onto POSIX sessions, so that pressing
245 Control-C in the wrong window will not interrupt them - this has been
246 found to be embarrassing.
248 <sect2><title>Support For Unix</>
250 <para>The UNIX command line can be read from the variable
251 <varname>sb-ext:*posix-argv*</>. The UNIX environment can be queried with the
252 <function>sb-ext:posix-getenv</> function.</para>
254 <para>The &SBCL; system can be terminated with <function>sb-ext:quit</>,
255 optionally returning a specified numeric value to the calling Unix
256 process. The normal Unix idiom of terminating on end of file on input
257 is also supported.</para>
259 </sect2>
261 <sect2><title>Customization Hooks for Users</title>
263 <para>The behaviour of <function>require</function> when called with only
264 one argument is implementation-defined. In &SBCL; it calls functions
265 on the user-settable list <varname>sb-ext:*module-provider-functions*</varname>
266 - see the <function>require</function> documentation string for details.
268 <para>The toplevel repl prompt may be customized, and the function
269 that reads user input may be replaced completely. <!-- FIXME but I
270 don't currently remember how -->
272 <sect2><title>Tools To Help Developers</title>
274 <para>&SBCL; provides a profiler and other extensions to the &ANSI;
275 <function>trace</> facility. See the online function documentation for
276 <function>trace</> for more information.</para>
278 <para>The debugger supports a number of options. Its documentation is
279 accessed by typing <userinput>help</> at the debugger prompt.</para>
280 <!-- FIXME:
281 A true debugger section in the manual would be good. Start
282 with CMU CL's debugger section, but remember:
283 * no QUIT command (TOPLEVEL restart instead)
284 * no GO command (CONTINUE restart instead)
285 * Limitations of the x86 port of the debugger should be
286 documented or fixed where possible.
287 * Discuss TRACE and its unification with PROFILE. -->
289 <para>Documentation for <function>inspect</> is accessed by typing
290 <userinput>help</> at the <function>inspect</> prompt.</para>
292 </sect2>
294 <sect2><title>Interface To Low-Level &SBCL; Implementation</title>
296 <para>&SBCL; has the ability to save its state as a file for later
297 execution. This functionality is important for its bootstrapping
298 process, and is also provided as an extension to the user See the
299 documentation for <function>sb-ext:save-lisp-and-die</> for more
300 information.</para>
302 <note><para>&SBCL; has inherited from &CMUCL; various hooks to allow
303 the user to tweak and monitor the garbage collection process. These
304 are somewhat stale code, and their interface might need to be cleaned
305 up. If you have urgent need of them, look at the code in
306 <filename>src/code/gc.lisp</filename> and bring it up on the
307 developers' mailing list.</para></note>
309 <note><para>&SBCL; has various hooks inherited from &CMUCL;, like
310 <function>sb-ext:float-denormalized-p</>, to allow a program to take
311 advantage of &IEEE; floating point arithmetic properties which aren't
312 conveniently or efficiently expressible using the &ANSI; standard. These
313 look good, and their interface looks good, but &IEEE; support is
314 slightly broken due to a stupid decision to remove some support for
315 infinities (because it wasn't in the &ANSI; spec and it didn't occur to
316 me that it was in the &IEEE; spec). If you need this stuff, take a look
317 at the code and bring it up on the developers' mailing
318 list.</para></note>
320 </sect2>
322 <sect2><title>Efficiency Hacks</title>
324 <para>The <function>sb-ext:purify</function> function causes &SBCL;
325 first to collect all garbage, then to mark all uncollected objects as
326 permanent, never again attempting to collect them as garbage. This can
327 cause a large increase in efficiency when using a primitive garbage
328 collector, or a more moderate increase in efficiency when using a more
329 sophisticated garbage collector which is well suited to the program's
330 memory usage pattern. It also allows permanent code to be frozen at
331 fixed addresses, a precondition for using copy-on-write to share code
332 between multiple Lisp processes. is less important with modern
333 generational garbage collectors. </para>
335 <para>The <function>sb-ext:truly-the</> operator does what the
336 <function>cl:the</> operator does in a more conventional
337 implementation of &CommonLisp;, declaring the type of its argument
338 without any runtime checks. (Ordinarily in &SBCL;, any type
339 declaration is treated as an assertion and checked at runtime.)</para>
341 <para>The <function>sb-ext:freeze-type</> declaration declares that a
342 type will never change, which can make type testing
343 (<function>typep</>, etc.) more efficient for structure types.</para>
345 <para>The <function>sb-ext:constant-function</> declaration specifies
346 that a function will always return the same value for the same
347 arguments, which may allow the compiler to optimize calls
348 to it. This is appropriate for functions like <function>sqrt</>, but
349 is <emphasis>not</> appropriate for functions like <function>aref</>,
350 which can change their return values when the underlying data are
351 changed.</para>
353 </sect2>
355 </sect1>
357 </chapter>