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