Adding TcGadt.lhs
[ghc-hetmet.git] / 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 is a little less strict about the layout rule when used
52               in <literal>do</literal> expressions.  Specifically, the
53               restriction that "a nested context must be indented further to
54               the right than the enclosing context" is relaxed to allow the
55               nested context to be at the same level as the enclosing context,
56               if the enclosing context is a <literal>do</literal>
57               expression.</para>
58
59             <para>For example, the following code is accepted by GHC:
60
61 <programlisting>
62 main = do args &lt;- getArgs
63           if null args then return [] else do
64           ps &lt;- mapM process args
65           mapM print ps</programlisting>
66
67               </para>
68           </listitem>
69
70         <listitem>
71           <para>GHC doesn't do fixity resolution in expressions during
72           parsing.  For example, according to the Haskell report, the
73           following expression is legal Haskell:
74 <programlisting>
75     let x = 42 in x == 42 == True</programlisting>
76         and parses as:
77 <programlisting>
78     (let x = 42 in x == 42) == True</programlisting>
79
80           because according to the report, the <literal>let</literal>
81           expression <quote>extends as far to the right as
82           possible</quote>.  Since it can't extend past the second
83           equals sign without causing a parse error
84           (<literal>==</literal> is non-fix), the
85           <literal>let</literal>-expression must terminate there.  GHC
86           simply gobbles up the whole expression, parsing like this:
87 <programlisting>
88     (let x = 42 in x == 42 == True)</programlisting>
89
90           The Haskell report is arguably wrong here, but nevertheless
91           it's a difference between GHC &amp; Haskell 98.</para>
92         </listitem>
93       </itemizedlist>
94     </sect3>
95
96   <sect3 id="infelicities-exprs-pats">
97       <title>Expressions and patterns</title>
98
99         <para>None known.</para>
100     </sect3>
101
102     <sect3 id="infelicities-decls">
103       <title>Declarations and bindings</title>
104
105       <para>GHC's typechecker makes all pattern bindings monomorphic
106       by default; this behaviour can be disabled with
107       <option>-fno-mono-pat-binds</option>.  See <xref
108       linkend="options-language" />.</para>
109     </sect3>
110       
111       <sect3 id="infelicities-Modules">
112         <title>Module system and interface files</title>
113         
114         <para>None known.</para>
115     </sect3>
116
117     <sect3 id="infelicities-numbers">
118       <title>Numbers, basic types, and built-in classes</title>
119
120       <variablelist>
121         <varlistentry>
122           <term>Multiply-defined array elements&mdash;not checked:</term>
123           <listitem>
124             <para>This code fragment should
125             elicit a fatal error, but it does not:
126
127 <programlisting>
128 main = print (array (1,1) [(1,2), (1,3)])</programlisting>
129 GHC's implementation of <literal>array</literal> takes the value of an
130 array slot from the last (index,value) pair in the list, and does no
131 checking for duplicates.  The reason for this is efficiency, pure and simple.
132             </para>
133           </listitem>
134         </varlistentry>
135       </variablelist>
136       
137     </sect3>
138
139       <sect3 id="infelicities-Prelude">
140         <title>In <literal>Prelude</literal> support</title>
141
142       <variablelist>
143         <varlistentry>
144           <term>Arbitrary-sized tuples</term>
145           <listitem>
146             <para>Tuples are currently limited to size 100.  HOWEVER:
147             standard instances for tuples (<literal>Eq</literal>,
148             <literal>Ord</literal>, <literal>Bounded</literal>,
149             <literal>Ix</literal> <literal>Read</literal>, and
150             <literal>Show</literal>) are available
151             <emphasis>only</emphasis> up to 16-tuples.</para>
152
153             <para>This limitation is easily subvertible, so please ask
154             if you get stuck on it.</para>
155             </listitem>
156           </varlistentry>
157
158           <varlistentry>
159             <term><literal>Read</literal>ing integers</term>
160             <listitem>
161               <para>GHC's implementation of the
162               <literal>Read</literal> class for integral types accepts
163               hexadecimal and octal literals (the code in the Haskell
164               98 report doesn't).  So, for example,
165 <programlisting>read "0xf00" :: Int</programlisting>
166               works in GHC.</para>
167               <para>A possible reason for this is that <literal>readLitChar</literal> accepts hex and
168                 octal escapes, so it seems inconsistent not to do so for integers too.</para>
169             </listitem>
170           </varlistentry>
171
172           <varlistentry>
173             <term><literal>isAlpha</literal></term>
174             <listitem>
175               <para>The Haskell 98 definition of <literal>isAlpha</literal>
176               is:</para>
177
178 <programlisting>isAlpha c = isUpper c || isLower c</programlisting>
179
180               <para>GHC's implementation diverges from the Haskell 98
181               definition in the sense that Unicode alphabetic characters which
182               are neither upper nor lower case will still be identified as
183               alphabetic by <literal>isAlpha</literal>.</para>
184             </listitem>
185           </varlistentry>
186         </variablelist>
187     </sect3>
188   </sect2>
189
190   <sect2 id="haskell98-undefined">
191     <title>GHC's interpretation of undefined behaviour in
192     Haskell&nbsp;98</title>
193
194     <para>This section documents GHC's take on various issues that are
195     left undefined or implementation specific in Haskell 98.</para>
196
197     <variablelist>
198       <varlistentry>
199         <term>
200           The <literal>Char</literal> type
201           <indexterm><primary><literal>Char</literal></primary><secondary>size of</secondary></indexterm>
202         </term>
203         <listitem>
204           <para>Following the ISO-10646 standard,
205           <literal>maxBound :: Char</literal> in GHC is
206           <literal>0x10FFFF</literal>.</para>
207         </listitem>
208       </varlistentry>
209
210       <varlistentry>
211         <term>
212           Sized integral types
213           <indexterm><primary><literal>Int</literal></primary><secondary>size of</secondary></indexterm>
214         </term>
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   </sect1>
284
285
286   <sect1 id="bugs">
287     <title>Known bugs or infelicities</title>
288
289     <para>The bug tracker lists bugs that have been reported in GHC but not
290       yet fixed: see the <ulink url="http://sourceforge.net/projects/ghc/">SourceForge GHC
291     page</ulink>.  In addition to those, GHC also has the following known bugs
292       or  infelicities.  These bugs are more permanent; it is unlikely that
293       any of them will be fixed in the short term.</para>
294
295   <sect2 id="bugs-ghc">
296     <title>Bugs in GHC</title>
297
298     <itemizedlist>
299       <listitem>
300         <para> GHC can warn about non-exhaustive or overlapping
301         patterns (see <xref linkend="options-sanity"/>), and usually
302         does so correctly.  But not always.  It gets confused by
303         string patterns, and by guards, and can then emit bogus
304         warnings.  The entire overlap-check code needs an overhaul
305         really.</para>
306       </listitem>
307
308       <listitem>
309         <para>GHC does not allow you to have a data type with a context 
310            that mentions type variables that are not data type parameters.
311           For example:
312 <programlisting>
313   data C a b => T a = MkT a
314 </programlisting>
315           so that <literal>MkT</literal>'s type is
316 <programlisting>
317   MkT :: forall a b. C a b => a -> T a
318 </programlisting>
319         In principle, with a suitable class declaration with a functional dependency,
320          it's possible that this type is not ambiguous; but GHC nevertheless rejects
321           it.  The type variables mentioned in the context of the data type declaration must
322         be among the type parameters of the data type.</para>
323       </listitem>
324
325       <listitem>
326         <para>GHC's inliner can be persuaded into non-termination
327         using the standard way to encode recursion via a data type:</para>
328 <programlisting>
329   data U = MkU (U -> Bool)
330        
331   russel :: U -> Bool
332   russel u@(MkU p) = not $ p u
333   
334   x :: Bool
335   x = russel (MkU russel)
336 </programlisting>
337
338         <para>We have never found another class of programs, other
339         than this contrived one, that makes GHC diverge, and fixing
340         the problem would impose an extra overhead on every
341         compilation.  So the bug remains un-fixed.  There is more
342         background in <ulink
343         url="http://research.microsoft.com/~simonpj/Papers/inlining">
344         Secrets of the GHC inliner</ulink>.</para>
345       </listitem>
346
347       <listitem>
348         <para>GHC does not keep careful track of
349             what instance declarations are 'in scope' if they come from other packages.
350         Instead, all instance declarations that GHC has seen in other
351         packages are all in scope everywhere, whether or not the
352         module from that package is used by the command-line
353         expression.  This bug affects only the <option>--make</option> mode and
354           GHCi.</para>
355       </listitem>
356
357     </itemizedlist>
358   </sect2>
359
360   <sect2 id="bugs-ghci">
361     <title>Bugs in GHCi (the interactive GHC)</title>
362     <itemizedlist>
363       <listitem>
364         <para>GHCi does not respect the <literal>default</literal>
365         declaration in the module whose scope you are in.  Instead,
366         for expressions typed at the command line, you always get the
367         default default-type behaviour; that is,
368         <literal>default(Int,Double)</literal>.</para>
369
370         <para>It would be better for GHCi to record what the default
371         settings in each module are, and use those of the 'current'
372         module (whatever that is).</para>
373       </listitem>
374
375       <listitem> 
376       <para>On Windows, there's a GNU ld/BFD bug
377       whereby it emits bogus PE object files that have more than
378       0xffff relocations. When GHCi tries to load a package affected by this
379       bug, you get an error message of the form
380 <screen>
381 Loading package javavm ... linking ... WARNING: Overflown relocation field (# relocs found: 30765)
382 </screen>
383       The last time we looked, this bug still
384       wasn't fixed in the BFD codebase, and there wasn't any
385       noticeable interest in fixing it when we reported the bug
386       back in 2001 or so.
387       </para>
388       <para>The workaround is to split up the .o files that make up
389       your package into two or more .o's, along the lines of
390       how the "base" package does it.</para>
391       </listitem>
392     </itemizedlist>
393   </sect2>
394   </sect1>
395
396 </chapter>
397
398 <!-- Emacs stuff:
399      ;;; Local Variables: ***
400      ;;; mode: xml ***
401      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
402      ;;; End: ***
403  -->