0bdb38c2a8fb26f1423a73768c489593b451a80e
[ghc-hetmet.git] / ghc / docs / users_guide / bugs.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="bugs-and-infelicities">
3   <title>Known bugs and infelicities</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       </itemizedlist>
44     </sect3>
45       
46       <sect3 id="infelicities-syntax">
47         <title>Context-free syntax</title>
48         
49       <itemizedlist>
50         <listitem>
51           <para>GHC doesn't do fixity resolution in expressions during
52           parsing.  For example, according to the Haskell report, the
53           following expression is legal Haskell:
54 <programlisting>
55     let x = 42 in x == 42 == True</programlisting>
56         and parses as:
57 <programlisting>
58     (let x = 42 in x == 42) == True</programlisting>
59
60           because according to the report, the <literal>let</literal>
61           expression <quote>extends as far to the right as
62           possible</quote>.  Since it can't extend past the second
63           equals sign without causing a parse error
64           (<literal>==</literal> is non-fix), the
65           <literal>let</literal>-expression must terminate there.  GHC
66           simply gobbles up the whole expression, parsing like this:
67 <programlisting>
68     (let x = 42 in x == 42 == True)</programlisting>
69
70           The Haskell report is arguably wrong here, but nevertheless
71           it's a difference between GHC &amp; Haskell 98.</para>
72         </listitem>
73       </itemizedlist>
74     </sect3>
75
76   <sect3 id="infelicities-exprs-pats">
77       <title>Expressions and patterns</title>
78
79         <para>None known.</para>
80     </sect3>
81
82     <sect3 id="infelicities-decls">
83       <title>Declarations and bindings</title>
84
85       <para>None known.</para>
86     </sect3>
87       
88       <sect3 id="infelicities-Modules">
89         <title>Module system and interface files</title>
90         
91         <para>None known.</para>
92     </sect3>
93
94     <sect3 id="infelicities-numbers">
95       <title>Numbers, basic types, and built-in classes</title>
96
97       <variablelist>
98         <varlistentry>
99           <term>Multiply-defined array elements&mdash;not checked:</term>
100           <listitem>
101             <para>This code fragment should
102             elicit a fatal error, but it does not:
103
104 <programlisting>
105 main = print (array (1,1) [(1,2), (1,3)])</programlisting>
106 GHC's implementation of <literal>array</literal> takes the value of an
107 array slot from the last (index,value) pair in the list, and does no
108 checking for duplicates.  The reason for this is efficiency, pure and simple.
109             </para>
110           </listitem>
111         </varlistentry>
112       </variablelist>
113       
114     </sect3>
115
116       <sect3 id="infelicities-Prelude">
117         <title>In <literal>Prelude</literal> support</title>
118
119       <variablelist>
120         <varlistentry>
121           <term>Arbitrary-sized tuples</term>
122           <listitem>
123             <para>Tuples are currently limited to size 100.  HOWEVER:
124             standard instances for tuples (<literal>Eq</literal>,
125             <literal>Ord</literal>, <literal>Bounded</literal>,
126             <literal>Ix</literal> <literal>Read</literal>, and
127             <literal>Show</literal>) are available
128             <emphasis>only</emphasis> up to 16-tuples.</para>
129
130             <para>This limitation is easily subvertible, so please ask
131             if you get stuck on it.</para>
132             </listitem>
133           </varlistentry>
134
135           <varlistentry>
136             <term><literal>Read</literal>ing integers</term>
137             <listitem>
138               <para>GHC's implementation of the
139               <literal>Read</literal> class for integral types accepts
140               hexadecimal and octal literals (the code in the Haskell
141               98 report doesn't).  So, for example,
142 <programlisting>read "0xf00" :: Int</programlisting>
143               works in GHC.</para>
144               <para>A possible reason for this is that <literal>readLitChar</literal> accepts hex and
145                 octal escapes, so it seems inconsistent not to do so for integers too.</para>
146             </listitem>
147           </varlistentry>
148
149           <varlistentry>
150             <term><literal>isAlpha</literal></term>
151             <listitem>
152               <para>The Haskell 98 definition of <literal>isAlpha</literal>
153               is:</para>
154
155 <programlisting>isAlpha c = isUpper c || isLower c</programlisting>
156
157               <para>GHC's implementation diverges from the Haskell 98
158               definition in the sense that Unicode alphabetic characters which
159               are neither upper nor lower case will still be identified as
160               alphabetic by <literal>isAlpha</literal>.</para>
161             </listitem>
162           </varlistentry>
163         </variablelist>
164     </sect3>
165   </sect2>
166
167   <sect2 id="haskell98-undefined">
168     <title>GHC's interpretation of undefined behaviour in
169     Haskell&nbsp;98</title>
170
171     <para>This section documents GHC's take on various issues that are
172     left undefined or implementation specific in Haskell 98.</para>
173
174     <variablelist>
175       <varlistentry>
176         <term>
177           The <literal>Char</literal> type
178           <indexterm><primary><literal>Char</literal></primary><secondary>size of</secondary></indexterm>
179         </term>
180         <listitem>
181           <para>Following the ISO-10646 standard,
182           <literal>maxBound :: Char</literal> in GHC is
183           <literal>0x10FFFF</literal>.</para>
184         </listitem>
185       </varlistentry>
186
187       <varlistentry>
188         <term>
189           Sized integral types
190           <indexterm><primary><literal>Int</literal></primary><secondary>size of</secondary></indexterm>
191         </term>
192         <listitem>
193           <para>In GHC the <literal>Int</literal> type follows the
194           size of an address on the host architecture; in other words
195           it holds 32 bits on a 32-bit machine, and 64-bits on a
196           64-bit machine.</para>
197
198           <para>Arithmetic on <literal>Int</literal> is unchecked for
199           overflow<indexterm><primary>overflow</primary><secondary><literal>Int</literal></secondary>
200             </indexterm>, so all operations on <literal>Int</literal> happen
201           modulo
202           2<superscript><replaceable>n</replaceable></superscript>
203           where <replaceable>n</replaceable> is the size in bits of
204           the <literal>Int</literal> type.</para>
205
206           <para>The <literal>fromInteger</literal><indexterm><primary><literal>fromInteger</literal></primary>
207             </indexterm>function (and hence
208           also <literal>fromIntegral</literal><indexterm><primary><literal>fromIntegral</literal></primary>
209             </indexterm>) is a special case when
210           converting to <literal>Int</literal>.  The value of
211           <literal>fromIntegral x :: Int</literal> is given by taking
212           the lower <replaceable>n</replaceable> bits of <literal>(abs
213           x)</literal>, multiplied by the sign of <literal>x</literal>
214           (in 2's complement <replaceable>n</replaceable>-bit
215           arithmetic).  This behaviour was chosen so that for example
216           writing <literal>0xffffffff :: Int</literal> preserves the
217           bit-pattern in the resulting <literal>Int</literal>.</para>
218
219
220            <para>Negative literals, such as <literal>-3</literal>, are
221              specified by (a careful reading of) the Haskell Report as 
222              meaning <literal>Prelude.negate (Prelude.fromInteger 3)</literal>.
223              So <literal>-2147483648</literal> means <literal>negate (fromInteger 2147483648)</literal>.
224              Since <literal>fromInteger</literal> takes the lower 32 bits of the representation,
225              <literal>fromInteger (2147483648::Integer)</literal>, computed at type <literal>Int</literal> is
226              <literal>-2147483648::Int</literal>.  The <literal>negate</literal> operation then
227              overflows, but it is unchecked, so <literal>negate (-2147483648::Int)</literal> is just
228              <literal>-2147483648</literal>.  In short, one can write <literal>minBound::Int</literal> as
229              a literal with the expected meaning (but that is not in general guaranteed.
230              </para>
231
232           <para>The <literal>fromIntegral</literal> function also
233           preserves bit-patterns when converting between the sized
234           integral types (<literal>Int8</literal>,
235           <literal>Int16</literal>, <literal>Int32</literal>,
236           <literal>Int64</literal> and the unsigned
237           <literal>Word</literal> variants), see the modules
238           <literal>Data.Int</literal> and <literal>Data.Word</literal>
239           in the library documentation.</para>
240         </listitem>
241       </varlistentry>
242
243       <varlistentry>
244         <term>Unchecked float arithmetic</term>
245         <listitem>
246           <para>Operations on <literal>Float</literal> and
247           <literal>Double</literal> numbers are
248           <emphasis>unchecked</emphasis> for overflow, underflow, and
249           other sad occurrences.  (note, however that some
250           architectures trap floating-point overflow and
251           loss-of-precision and report a floating-point exception,
252           probably terminating the
253           program)<indexterm><primary>floating-point
254           exceptions</primary></indexterm>.</para>
255         </listitem>
256       </varlistentry>
257     </variablelist>
258       
259     </sect2>
260   </sect1>
261
262
263   <sect1 id="bugs">
264     <title>Known bugs or infelicities</title>
265
266     <para>In addition to the divergences from the Haskell 98 standard
267     listed above, GHC has the following known bugs or
268     infelicities.</para>
269
270   <sect2 id="bugs-ghc">
271     <title>Bugs in GHC</title>
272
273     <itemizedlist>
274       <listitem>
275         <para> GHC can warn about non-exhaustive or overlapping
276         patterns (see <xref linkend="options-sanity"/>), and usually
277         does so correctly.  But not always.  It gets confused by
278         string patterns, and by guards, and can then emit bogus
279         warnings.  The entire overlap-check code needs an overhaul
280         really.</para>
281       </listitem>
282
283       <listitem>
284         <para>GHC does not allow you to have a data type with a context 
285            that mentions type variables that are not data type parameters.
286           For example:
287 <programlisting>
288   data C a b => T a = MkT a
289 </programlisting>
290           so that <literal>MkT</literal>'s type is
291 <programlisting>
292   MkT :: forall a b. C a b => a -> T a
293 </programlisting>
294         In principle, with a suitable class declaration with a functional dependency,
295          it's possible that this type is not ambiguous; but GHC nevertheless rejects
296           it.  The type variables mentioned in the context of the data type declaration must
297         be among the type parameters of the data type.</para>
298       </listitem>
299
300       <listitem>
301         <para>GHC's inliner can be persuaded into non-termination
302         using the standard way to encode recursion via a data type:</para>
303 <programlisting>
304   data U = MkU (U -> Bool)
305        
306   russel :: U -> Bool
307   russel u@(MkU p) = not $ p u
308   
309   x :: Bool
310   x = russel (MkU russel)
311 </programlisting>
312
313         <para>We have never found another class of programs, other
314         than this contrived one, that makes GHC diverge, and fixing
315         the problem would impose an extra overhead on every
316         compilation.  So the bug remains un-fixed.  There is more
317         background in <ulink
318         url="http://research.microsoft.com/~simonpj/Papers/inlining">
319         Secrets of the GHC inliner</ulink>.</para>
320       </listitem>
321     </itemizedlist>
322   </sect2>
323
324   <sect2 id="bugs-ghci">
325     <title>Bugs in GHCi (the interactive GHC)</title>
326     <itemizedlist>
327       <listitem>
328         <para>GHCi does not respect the <literal>default</literal>
329         declaration in the module whose scope you are in.  Instead,
330         for expressions typed at the command line, you always get the
331         default default-type behaviour; that is,
332         <literal>default(Int,Double)</literal>.</para>
333
334         <para>It would be better for GHCi to record what the default
335         settings in each module are, and use those of the 'current'
336         module (whatever that is).</para>
337       </listitem>
338
339       <listitem>
340         <para>GHCi does not keep careful track of what instance
341         declarations are 'in scope' if they come from other packages.
342         Instead, all instance declarations that GHC has seen in other
343         packages are all in scope everywhere, whether or not the
344         module from that package is used by the command-line
345         expression.</para>
346       </listitem>
347
348       <listitem> 
349       <para>On Windows, there's a GNU ld/BFD bug
350       whereby it emits bogus PE object files that have more than
351       0xffff relocations. When GHCi tries to load a package affected by this
352       bug, you get an error message of the form
353 <screen>
354 Loading package javavm ... linking ... WARNING: Overflown relocation field (# relocs found: 30765)
355 </screen>
356       The last time we looked, this bug still
357       wasn't fixed in the BFD codebase, and there wasn't any
358       noticeable interest in fixing it when we reported the bug
359       back in 2001 or so.
360       </para>
361       <para>The workaround is to split up the .o files that make up
362       your package into two or more .o's, along the lines of
363       how the "base" package does it.</para>
364       </listitem>
365     </itemizedlist>
366   </sect2>
367   </sect1>
368
369 </chapter>
370
371 <!-- Emacs stuff:
372      ;;; Local Variables: ***
373      ;;; mode: xml ***
374      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
375      ;;; End: ***
376  -->