0.7.9.36:
[sbcl.git] / doc / beyond-ansi.sgml
1 <chapter id="beyond-ansi"><title>Beyond The &ANSI; Standard</>
2
3 <para>&SBCL; is mostly an implementation of the &ANSI; standard for
4 Common Lisp. However, there's some important behavior which extends
5 or clarifies the standard, and various behavior which outright
6 violates the standard.
7 </para>
8
9 <sect1 id="non-conformance"><title>Non-Conformance With The &ANSI; Standard</>
10
11 <para>
12 Essentially every type of non-conformance is considered a bug.
13 (The exceptions involve internal inconsistencies in the standard.)
14 In &SBCL; 0.7.6, the master record of known bugs is in
15 the <filename>BUGS</> file in the distribution.
16 Some highlight information about bugs may also be found in the
17 manual page. The recommended way to report bugs is through the sbcl-help or
18 sbcl-devel mailings lists.
19 </para>
20
21 </sect1>
22
23 <sect1 id="idiosyncrasies"><title>Idiosyncrasies</>
24
25 <para>The information in this section describes some of the ways
26 that &SBCL; deals with choices that the &ANSI; standard 
27 leaves to the implementation.</para>
28
29 <para>Declarations are generally treated as assertions. This general
30 principle, and its implications, and the bugs which still keep the
31 compiler from quite satisfying this principle, are discussed in the
32 <link linkend="compiler">chapter on the compiler</link>.</para>
33
34 <para>&SBCL; is essentially a compiler-only implementation of
35 &CommonLisp;. That is, for all but a few special cases,
36 <function>eval</> creates a
37 lambda expression, calls <function>compile</> on the lambda
38 expression to create a compiled function, and then calls
39 <function>funcall</> on the resulting function object. This 
40 is explicitly allowed by the &ANSI; standard, but leads to some
41 oddities, e.g. collapsing <function>functionp</> and 
42 <function>compiled-function-p</> into the same predicate.</para>
43
44 <para>&SBCL; is quite strict about ANSI's definition of
45 <function>defconstant</>. ANSI says that doing <function>defconstant</>
46 of the same symbol more than once is undefined unless the new value
47 is <function>eql</> to the old value. Conforming to this specification
48 is a nuisance when the "constant" value is only constant under some
49 weaker test like <function>string=</> or <function>equal</>. It's
50 especially annoying because <function>defconstant</> takes effect
51 not only at load time but also at compile time, so that just 
52 compiling and loading reasonable code like 
53 <programlisting>(defconstant +foobyte+ '(1 4))</>
54 runs into this undefined behavior. Many
55 implementations of Common Lisp try to help the programmer around
56 this annoyance by silently accepting the undefined code and 
57 trying to do what the programmer probably meant. &SBCL; instead
58 treats the undefined behavior as an error. Often
59 such code can be rewritten
60 in portable &ANSI; Common Lisp which has the desired behavior.
61 E.g., the code above can be given an exactly defined meaning by replacing
62 <function>defconstant</> either with <function>defparameter</> or 
63 with a customized macro which does the right thing, possibly along the
64 lines of the <function>defconstant-eqx</> macro used internally in the
65 implementation of SBCL itself.</para>
66
67 <para>&SBCL; gives style warnings about various kinds of perfectly
68 legal code, e.g.
69 <itemizedlist>
70   <listitem><para><function>defmethod</> without
71     <function>defgeneric</></para></listitem>
72   <listitem><para>multiple <function>defun</>s of the same
73     symbol</para></listitem>
74   <listitem><para>special variables not named in the conventional
75     <varname>*foo*</> style, and lexical variables unconventionally named
76     in the <varname>*foo*</> style</para></listitem>
77 </itemizedlist>
78 This causes friction with people
79 who point out that other ways of organizing code (especially
80 avoiding the use of <function>defgeneric</>)
81 are just as aesthetically stylish.
82 However, these warnings should be read not
83 as "warning, bad aesthetics detected, you have no style" but
84 "warning, this style keeps the compiler from understanding
85 the code as well as you might like." That is, 
86 unless the compiler warns about such conditions, there's no
87 way for the compiler to warn 
88 about some programming errors which would otherwise be
89 easy to overlook. (related bug: The warning about
90 multiple <function>defun</>s is pointlessly annoying when you compile
91 and then load a function containing <function>defun</> wrapped
92 in <function>eval-when</>, and ideally should be suppressed in 
93 that case, but still isn't as of &SBCL; 0.7.6.)</para>
94
95 </sect1>
96
97 <sect1 id="extensions"><title>Extensions</>
98
99 <para>&SBCL; is derived from &CMUCL;, which implements many extensions
100 to the &ANSI; standard. &SBCL; doesn't support as many extensions as
101 &CMUCL;, but it still has quite a few.</para>
102
103 <sect2><title>Things Which Might Be In The Next &ANSI; Standard</>
104
105 <para>&SBCL; provides extensive support for 
106 calling external C code, described 
107 <link linkend="ffi">in its own chapter</link>.</para>
108
109 <para>&SBCL; provides additional garbage collection functionality not
110 specified by &ANSI;. Weak pointers allow references to objects to be
111 maintained without keeping them from being GCed. And "finalization"
112 hooks are available to cause code to be executed when an object is
113 GCed.</para> <!-- FIXME: Actually documenting these would be good.:-| -->
114
115 <para>&SBCL; supports Gray streams, user-overloadable CLOS classes
116 whose instances can be used as Lisp streams (e.g. passed as the
117 first argument to <function>format</>).</para>
118
119 <para>&SBCL; supports a MetaObject Protocol which is intended to be
120 compatible with &AMOP;; exceptions to this (as distinct from current
121 bugs<!-- Such as the distinction between CL:FIND-CLASS and
122 SB-PCL::FIND-CLASS :-( -->) are that
123 <function>compute-effective-method</> only returns one value, not
124 two<!-- FIXME: anything else? What about extensions? (e.g. COMPUTE-SLOTS
125 behaviour) -->.</para>
126
127 </sect2>
128
129 <sect2><title>Support For Unix</>
130
131 <para>The UNIX command line can be read from the variable
132 <varname>sb-ext:*posix-argv*</>. The UNIX environment can be queried with the
133 <function>sb-ext:posix-getenv</> function.</para>
134
135 <para>The &SBCL; system can be terminated with <function>sb-ext:quit</>,
136 optionally returning a specified numeric value to the calling Unix
137 process. The normal Unix idiom of terminating on end of file on input
138 is also supported.</para>
139
140 </sect2>
141
142 <sect2><title>Tools To Help Developers</title>
143
144 <para>&SBCL; provides a profiler and other extensions to the &ANSI;
145 <function>trace</> facility. See the online function documentation for
146 <function>trace</> for more information.</para>
147
148 <para>The debugger supports a number of options. Its documentation is
149 accessed by typing <userinput>help</> at the debugger prompt.</para>
150 <!-- FIXME:
151      A true debugger section in the manual would be good. Start
152      with CMU CL's debugger section, but remember:
153        * no QUIT command (TOPLEVEL restart instead)
154        * no GO command (CONTINUE restart instead)
155        * Limitations of the x86 port of the debugger should be 
156          documented or fixed where possible.
157        * Discuss TRACE and its unification with PROFILE. -->
158
159 <para>Documentation for <function>inspect</> is accessed by typing
160 <userinput>help</> at the <function>inspect</> prompt.</para>
161
162 </sect2>
163
164 <sect2><title>Interface To Low-Level &SBCL; Implementation</title>
165
166 <para>&SBCL; has the ability to save its state as a file for later
167 execution. This functionality is important for its bootstrapping
168 process, and is also provided as an extension to the user See the
169 documentation for <function>sb-ext:save-lisp-and-die</> for more
170 information.</para>
171
172 <note><para>&SBCL; has inherited from &CMUCL; various hooks to allow
173 the user to tweak and monitor the garbage collection process. These
174 are somewhat stale code, and their interface might need to be cleaned
175 up. If you have urgent need of them, look at the code in
176 <filename>src/code/gc.lisp</filename> and bring it up on the
177 developers' mailing list.</para></note>
178
179 <note><para>&SBCL; has various hooks inherited from &CMUCL;, like
180 <function>sb-ext:float-denormalized-p</>, to allow a program to take
181 advantage of &IEEE; floating point arithmetic properties which aren't
182 conveniently or efficiently expressible using the &ANSI; standard. These
183 look good, and their interface looks good, but &IEEE; support is
184 slightly broken due to a stupid decision to remove some support for
185 infinities (because it wasn't in the &ANSI; spec and it didn't occur to
186 me that it was in the &IEEE; spec). If you need this stuff, take a look
187 at the code and bring it up on the developers' mailing
188 list.</para></note>
189
190 </sect2>
191
192 <sect2><title>Efficiency Hacks</title>
193
194 <para>The <function>sb-ext:purify</function> function causes &SBCL;
195 first to collect all garbage, then to mark all uncollected objects as
196 permanent, never again attempting to collect them as garbage. This can
197 cause a large increase in efficiency when using a primitive garbage
198 collector, or a more moderate increase in efficiency when using a more
199 sophisticated garbage collector which is well suited to the program's
200 memory usage pattern. It also allows permanent code to be frozen at
201 fixed addresses, a precondition for using copy-on-write to share code
202 between multiple Lisp processes. is less important with modern
203 generational garbage collectors. </para>
204
205 <para>The <function>sb-ext:truly-the</> operator does what the
206 <function>cl:the</> operator does in a more conventional
207 implementation of &CommonLisp;, declaring the type of its argument
208 without any runtime checks. (Ordinarily in &SBCL;, any type
209 declaration is treated as an assertion and checked at runtime.)</para>
210
211 <para>The <function>sb-ext:freeze-type</> declaration declares that a
212 type will never change, which can make type testing
213 (<function>typep</>, etc.) more efficient for structure types.</para>
214
215 <para>The <function>sb-ext:constant-function</> declaration specifies
216 that a function will always return the same value for the same
217 arguments, which may allow the compiler to optimize calls
218 to it. This is appropriate for functions like <function>sqrt</>, but
219 is <emphasis>not</> appropriate for functions like <function>aref</>,
220 which can change their return values when the underlying data are
221 changed.</para>
222
223 </sect2>
224
225 </sect1>
226
227 </chapter>