3ede78f9909fec04351def9d2682618a8fdc3396
[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 </sect2>
120
121 <sect2><title>Support For Unix</>
122
123 <para>The UNIX command line can be read from the variable
124 <varname>sb-ext:*posix-argv*</>. The UNIX environment can be queried with the
125 <function>sb-ext:posix-getenv</> function.</para>
126
127 <para>The &SBCL; system can be terminated with <function>sb-ext:quit</>,
128 optionally returning a specified numeric value to the calling Unix
129 process. The normal Unix idiom of terminating on end of file on input
130 is also supported.</para>
131
132 </sect2>
133
134 <sect2><title>Tools To Help Developers</title>
135
136 <para>&SBCL; provides a profiler and other extensions to the &ANSI;
137 <function>trace</> facility. See the online function documentation for
138 <function>trace</> for more information.</para>
139
140 <para>The debugger supports a number of options. Its documentation is
141 accessed by typing <userinput>help</> at the debugger prompt.</para>
142 <!-- FIXME:
143      A true debugger section in the manual would be good. Start
144      with CMU CL's debugger section, but remember:
145        * no QUIT command (TOPLEVEL restart instead)
146        * no GO command (CONTINUE restart instead)
147        * Limitations of the x86 port of the debugger should be 
148          documented or fixed where possible.
149        * Discuss TRACE and its unification with PROFILE. -->
150
151 <para>Documentation for <function>inspect</> is accessed by typing
152 <userinput>help</> at the <function>inspect</> prompt.</para>
153
154 </sect2>
155
156 <sect2><title>Interface To Low-Level &SBCL; Implementation</title>
157
158 <para>&SBCL; has the ability to save its state as a file for later
159 execution. This functionality is important for its bootstrapping
160 process, and is also provided as an extension to the user See the
161 documentation for <function>sb-ext:save-lisp-and-die</> for more
162 information.</para>
163
164 <note><para>&SBCL; has inherited from &CMUCL; various hooks to allow
165 the user to tweak and monitor the garbage collection process. These
166 are somewhat stale code, and their interface might need to be cleaned
167 up. If you have urgent need of them, look at the code in
168 <filename>src/code/gc.lisp</filename> and bring it up on the
169 developers' mailing list.</para></note>
170
171 <note><para>&SBCL; has various hooks inherited from &CMUCL;, like
172 <function>sb-ext:float-denormalized-p</>, to allow a program to take
173 advantage of &IEEE; floating point arithmetic properties which aren't
174 conveniently or efficiently expressible using the &ANSI; standard. These
175 look good, and their interface looks good, but &IEEE; support is
176 slightly broken due to a stupid decision to remove some support for
177 infinities (because it wasn't in the &ANSI; spec and it didn't occur to
178 me that it was in the &IEEE; spec). If you need this stuff, take a look
179 at the code and bring it up on the developers' mailing
180 list.</para></note>
181
182 </sect2>
183
184 <sect2><title>Efficiency Hacks</title>
185
186 <para>The <function>sb-ext:purify</function> function causes &SBCL;
187 first to collect all garbage, then to mark all uncollected objects as
188 permanent, never again attempting to collect them as garbage. This can
189 cause a large increase in efficiency when using a primitive garbage
190 collector, or a more moderate increase in efficiency when using a more
191 sophisticated garbage collector which is well suited to the program's
192 memory usage pattern. It also allows permanent code to be frozen at
193 fixed addresses, a precondition for using copy-on-write to share code
194 between multiple Lisp processes. is less important with modern
195 generational garbage collectors. </para>
196
197 <para>The <function>sb-ext:truly-the</> operator does what the
198 <function>cl:the</> operator does in a more conventional
199 implementation of &CommonLisp;, declaring the type of its argument
200 without any runtime checks. (Ordinarily in &SBCL;, any type
201 declaration is treated as an assertion and checked at runtime.)</para>
202
203 <para>The <function>sb-ext:freeze-type</> declaration declares that a
204 type will never change, which can make type testing
205 (<function>typep</>, etc.) more efficient for structure types.</para>
206
207 <para>The <function>sb-ext:constant-function</> declaration specifies
208 that a function will always return the same value for the same
209 arguments, which may allow the compiler to optimize calls
210 to it. This is appropriate for functions like <function>sqrt</>, but
211 is <emphasis>not</> appropriate for functions like <function>aref</>,
212 which can change their return values when the underlying data are
213 changed.</para>
214
215 </sect2>
216
217 </sect1>
218
219 </chapter>