[project @ 2003-04-09 08:44:37 by simonpj]
[ghc-hetmet.git] / ghc / docs / users_guide / bugs.sgml
1 <Chapter id="bugs-and-infelicities">
2   <title>Known bugs and infelicities
3 </title>
4
5 <sect1 id="vs-Haskell-defn">
6   <title>Haskell&nbsp;98 vs.&nbsp;Glasgow Haskell: language non-compliance
7 </title>
8
9   <indexterm><primary>GHC vs the Haskell 98 language</primary></indexterm>
10   <indexterm><primary>Haskell 98 language vs GHC</primary></indexterm>
11
12   <para>This section lists Glasgow Haskell infelicities in its
13   implementation of Haskell&nbsp;98.  See also the &ldquo;when things
14   go wrong&rdquo; section (<XRef LinkEnd="wrong">) for information
15   about crashes, space leaks, and other undesirable phenomena.</para>
16
17   <para>The limitations here are listed in Haskell Report order
18   (roughly).</para>
19
20   <sect2 id="haskell98-divergence">
21     <title>Divergence from Haskell&nbsp;98</title>
22     
23       
24     <sect3 id="infelicities-lexical">
25       <title>Lexical syntax</title>
26       
27       <itemizedlist>
28         <listitem>
29           <para>The Haskell report specifies that programs may be
30           written using Unicode.  GHC only accepts the ISO-8859-1
31           character set at the moment.</para>
32         </listitem>
33
34         <listitem>
35           <para>Certain lexical rules regarding qualified identifiers
36           are slightly different in GHC compared to the Haskell
37           report.  When you have
38           <replaceable>module</replaceable><literal>.</literal><replaceable>reservedop</replaceable>,
39           such as <literal>M.\</literal>, GHC will interpret it as a
40           single qualified operator rather than the two lexemes
41           <literal>M</literal> and <literal>.\</literal>.</para>
42         </listitem>
43
44         <listitem>
45           <para>When <option>-fglasgow-exts</option> is on, GHC
46           reserves several keywords beginning with two underscores.
47           This is due to the fact that GHC uses the same lexical
48           analyser for interface file parsing as it does for source
49           file parsing, and these keywords are used in interface
50           files.  Do not use any identifiers beginning with a double
51           underscore in <option>-fglasgow-exts</option> mode.</para>
52         </listitem>
53       </itemizedlist>
54     </sect3>
55       
56       <sect3 id="infelicities-syntax">
57         <title>Context-free syntax</title>
58         
59       <itemizedlist>
60         <listitem>
61           <para>GHC doesn't do fixity resolution in expressions during
62           parsing.  For example, according to the Haskell report, the
63           following expression is legal Haskell:
64 <programlisting>
65     let x = 42 in x == 42 == True</programlisting>
66         and parses as:
67 <programlisting>
68     (let x = 42 in x == 42) == True</programlisting>
69
70           because according to the report, the <literal>let</literal>
71           expression <quote>extends as far to the right as
72           possible</quote>.  Since it can't extend past the second
73           equals sign without causing a parse error
74           (<literal>==</literal> is non-fix), the
75           <literal>let</literal>-expression must terminate there.  GHC
76           simply gobbles up the whole expression, parsing like this:
77 <programlisting>
78     (let x = 42 in x == 42 == True)</programlisting>
79
80           The Haskell report is arguably wrong here, but nevertheless
81           it's a difference between GHC & Haskell 98.</para>
82         </listitem>
83       </itemizedlist>
84     </sect3>
85
86   <sect3 id="infelicities-exprs-pats">
87       <title>Expressions and patterns</title>
88
89       <variablelist>
90         <varlistentry>
91           <term>Very long <literal>String</literal> constants:</term>
92           <listitem>
93             <para>May not go through.  If you add a &ldquo;string
94             gap&rdquo; every few thousand characters, then the strings
95             can be as long as you like.</para>
96
97             <para>Bear in mind that string gaps and the
98             <option>-cpp</option><indexterm><primary><option>-cpp</option>
99             </primary></indexterm> option don't mix very well (see
100             <xref linkend="c-pre-processor">).</para>
101           </listitem>
102         </varlistentry>
103       </variablelist>
104
105     </sect3>
106
107     <sect3 id="infelicities-decls">
108       <title>Declarations and bindings</title>
109
110       <para>None known.</para>
111
112     </sect3>
113
114     <sect3 id="infelicities-Modules">
115       <title>Module system and interface files</title>
116
117       <variablelist>
118
119         <varlistentry>
120           <term> Namespace pollution </term>
121           <listitem>
122             <para>Several modules internal to GHC are visible in the
123             standard namespace.  All of these modules begin with
124             <literal>Prel</literal>, so the rule is: don't use any
125             modules beginning with <literal>Prel</literal> in your
126             program, or you may be comprehensively screwed.</para>
127           </listitem>
128         </varlistentry>
129       </variablelist>
130
131     </sect3>
132
133     <sect3 id="infelicities-numbers">
134       <title>Numbers, basic types, and built-in classes</title>
135
136       <variablelist>
137         <varlistentry>
138           <term>Multiply-defined array elements&mdash;not checked:</term>
139           <listitem>
140             <para>This code fragment <emphasis>should</emphasis>
141             elicit a fatal error, but it does not:
142
143 <programlisting>
144 main = print (array (1,1) [(1,2), (1,3)])</programlisting>
145
146             </para>
147           </listitem>
148         </varlistentry>
149       </variablelist>
150       
151     </sect3>
152
153       <sect3 id="infelicities-Prelude">
154         <title>In Prelude support</title>
155
156       <variablelist>
157         <varlistentry>
158           <term>The <literal>Char</literal> type</term>
159           <indexterm><primary><literal>Char</literal></primary><secondary>size
160           of</secondary></indexterm>
161           <listitem>
162             <para>The Haskell report says that the
163             <literal>Char</literal> type holds 16 bits.  GHC follows
164             the ISO-10646 standard a little more closely:
165             <literal>maxBound :: Char</literal> in GHC is
166             <literal>0x10FFFF</literal>.</para>
167           </listitem>
168         </varlistentry>
169
170         <varlistentry>
171           <term>Arbitrary-sized tuples</term>
172           <listitem>
173             <para>Tuples are currently limited to size 61.  HOWEVER:
174             standard instances for tuples (<literal>Eq</literal>,
175             <literal>Ord</literal>, <literal>Bounded</literal>,
176             <literal>Ix</literal> <literal>Read</literal>, and
177             <literal>Show</literal>) are available
178             <emphasis>only</emphasis> up to 5-tuples.</para>
179
180             <para>This limitation is easily subvertible, so please ask
181             if you get stuck on it.</para>
182             </listitem>
183           </varlistentry>
184
185           <varlistentry>
186             <term><literal>Read</literal>ing integers</term>
187             <listitem>
188               <para>GHC's implementation of the
189               <literal>Read</literal> class for integral types accepts
190               hexadeciaml and octal literals (the code in the Haskell
191               98 report doesn't).  So, for example,
192 <programlisting>read "0xf00" :: Int</programlisting>
193               works in GHC.</para>
194               <para>A possible reason for this is that <literal>readLitChar</literal> accepts hex and
195                 octal escapes, so it seems inconsistent not to do so for integers too.</para>
196             </listitem>
197           </varlistentry>
198         </variablelist>
199     </sect3>
200   </sect2>
201
202   <sect2 id="haskell98-undefined">
203     <title>GHC's interpretation of undefined behaviour in
204     Haskell&nbsp;98</title>
205
206     <para>This section documents GHC's take on various issues that are
207     left undefined or implementation specific in Haskell 98.</para>
208
209     <variablelist>
210       <varlistentry>
211         <term>Sized integral types</term>
212         <indexterm><primary><literal>Int</literal></primary><secondary>size of</secondary>
213         </indexterm>
214         
215         <listitem>
216           <para>In GHC the <literal>Int</literal> type follows the
217           size of an address on the host architecture; in other words
218           it holds 32 bits on a 32-bit machine, and 64-bits on a
219           64-bit machine.</para>
220
221           <para>Arithmetic on <literal>Int</literal> is unchecked for
222           overflow<indexterm><primary>overflow</primary><secondary><literal>Int</literal></secondary>
223             </indexterm>, so all operations on <literal>Int</literal> happen
224           modulo
225           2<superscript><replaceable>n</replaceable></superscript>
226           where <replaceable>n</replaceable> is the size in bits of
227           the <literal>Int</literal> type.</para>
228
229           <para>The <literal>fromInteger</literal><indexterm><primary><literal>fromInteger</literal></primary>
230             </indexterm>function (and hence
231           also <literal>fromIntegral</literal><indexterm><primary><literal>fromIntegral</literal></primary>
232             </indexterm>) is a special case when
233           converting to <literal>Int</literal>.  The value of
234           <literal>fromIntegral x :: Int</literal> is given by taking
235           the lower <replaceable>n</replaceable> bits of <literal>(abs
236           x)</literal>, multiplied by the sign of <literal>x</literal>
237           (in 2's complement <replaceable>n</replaceable>-bit
238           arithmetic).  This behaviour was chosen so that for example
239           writing <literal>0xffffffff :: Int</literal> preserves the
240           bit-pattern in the resulting <literal>Int</literal>.</para>
241
242
243            <para>Negative literals, such as <literal>-3</literal>, are
244              specified by (a careful reading of) the Haskell Report as 
245              meaning <literal>Prelude.negate (Prelude.fromInteger 3)</literal>.
246              So <literal>-2147483648</literal> means <literal>negate (fromInteger 2147483648)</literal>.
247              Since <literal>fromInteger</literal> takes the lower 32 bits of the representation,
248              <literal>fromInteger (2147483648::Integer)</literal>, computed at type <literal>Int</literal> is
249              <literal>-2147483648::Int</literal>.  The <literal>negate</literal> operation then
250              overflows, but it is unchecked, so <literal>negate (-2147483648::Int)</literal> is just
251              <literal>-2147483648</literal>.  In short, one can write <literal>minBound::Int</literal> as
252              a literal with the expected meaning (but that is not in general guaranteed.
253              </para>
254
255           <para>The <literal>fromIntegral</literal> function also
256           preserves bit-patterns when converting between the sized
257           integral types (<literal>Int8</literal>,
258           <literal>Int16</literal>, <literal>Int32</literal>,
259           <literal>Int64</literal> and the unsigned
260           <literal>Word</literal> variants), see the modules
261           <literal>Data.Int</literal> and <literal>Data.Word</literal>
262           in the library documentation.</para>
263         </listitem>
264       </varlistentry>
265
266       <varlistentry>
267         <term>Unchecked float arithmetic</term>
268         <listitem>
269           <para>Operations on <literal>Float</literal> and
270           <literal>Double</literal> numbers are
271           <emphasis>unchecked</emphasis> for overflow, underflow, and
272           other sad occurrences.  (note, however that some
273           architectures trap floating-point overflow and
274           loss-of-precision and report a floating-point exception,
275           probably terminating the
276           program)<indexterm><primary>floating-point
277           exceptions</primary></indexterm>.</para>
278         </listitem>
279       </varlistentry>
280     </variablelist>
281       
282   </sect2>
283
284 </sect1>
285
286
287 <sect1 id="bugs">
288   <title>Known bugs or infelicities</title>
289
290 <para>GHC has the following known bugs or infelicities:
291 <itemizedlist>
292
293 <listitem><para>
294 GHC only provides tuples up to size 62, and derived tuple instances (for
295 Eq, Ord, etc) up to size 15.
296 </para></listitem>
297
298 <listitem><para>
299 GHC can warn about non-exhaustive or overlapping patterns, and usually does so correctly.
300 But not always.  It gets confused by string patterns, and by guards, and can then 
301 emit bogus warnings.  The entire overlap-check code needs an overhaul really.
302 </para></listitem>
303
304
305
306 <listitem><para>Dangers with multiple Main modules.</para>
307
308         <para>
309         GHC does not insist that module <literal>Main</literal> lives in a file called <filename>Main.hs</filename>.
310         This is useful if you want multiple versions of <literal>Main</literal>.  But there's a danger: when
311         compiling module  <literal>Main</literal> (regardless of what file it comes from), GHC looks for 
312         the interface <filename>Main.hi</filename>; it uses this to get version information from the last
313         time it recompiled <literal>Main</literal>.   The trouble is that this  <filename>Main.hi</filename>
314         may not correspond to the source file being compiled.
315           </para>
316         <para>
317           Solution: remove <filename>Main.hi</filename> first.  A better solution would be for GHC to
318             record the source-file filename in the interface file, or even an MD5 checksum.
319             </para>
320      </listitem>
321     
322
323 <listitem><para>
324 GHCi does not respect the <literal>default</literal> declaration in the module whose
325 scope you are in.  Instead, for expressions typed at the command line, you always 
326 get the default default-type behaviour; that is, <literal>default(Int,Double)</literal>.
327 </para>
328 <para>
329 It would be better for GHCi to record what the default settings in each module are, and
330 use those of the 'current' module (whatever that is).
331 </para></listitem>
332
333 <listitem><para>
334 GHCi does not keep careful track of what instance declarations are 'in scope' if they
335 come from other packages.
336 Instead, all instance declarations that GHC has seen in other packages are all in scope
337 everywhere, whether or not the module from that package is used by the command-line expression.
338 </para></listitem>
339
340 <listitem><para>
341 GHC's inliner can be persuaded into non-termination using the standard way to encode
342 recursion via a data type:
343 <programlisting>
344   data U = MkU (U -> Bool)
345        
346   russel :: U -> Bool
347   russel u@(MkU p) = not $ p u
348   
349   x :: Bool
350   x = russel (MkU russel)
351 </programlisting>
352 We have never found another class of programs, other than this contrived one, that makes GHC
353 diverge, and fixing the problem would impose an extra overhead on every compilation.  So the
354 bug remains un-fixed.  There is more background in
355 <ulink
356 url="http://research.microsoft.com/~simonpj/Papers/inlining">
357 Secrets of the GHC inliner</ulink>.
358 </para></listitem>
359 </itemizedlist></para>
360 </sect1>
361
362 </Chapter>
363
364 <!-- Emacs stuff:
365      ;;; Local Variables: ***
366      ;;; mode: sgml ***
367      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter" "sect1") ***
368      ;;; End: ***
369  -->