fix haddock submodule pointer
[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>-XNoMonoPatBinds</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>GHC requires the use of <literal>hs-boot</literal>
109           files to cut the recursive loops among mutually recursive modules
110           as described in <xref linkend="mutual-recursion"/>.  This more of an infelicity
111             than a bug: the Haskell Report says
112           (<ulink url="http://haskell.org/onlinereport/modules.html#sect5.7">Section 5.7</ulink>) "Depending on the Haskell
113         implementation used, separate compilation of mutually
114         recursive modules may require that imported modules contain
115         additional information so that they may be referenced before
116         they are compiled. Explicit type signatures for all exported
117         values may be necessary to deal with mutual recursion. The
118         precise details of separate compilation are not defined by
119         this Report."
120
121         </para>
122
123     </sect3>
124
125     <sect3 id="infelicities-numbers">
126       <title>Numbers, basic types, and built-in classes</title>
127
128       <variablelist>
129         <varlistentry>
130           <term>Multiply-defined array elements&mdash;not checked:</term>
131           <listitem>
132             <para>This code fragment should
133             elicit a fatal error, but it does not:
134
135 <programlisting>
136 main = print (array (1,1) [(1,2), (1,3)])</programlisting>
137 GHC's implementation of <literal>array</literal> takes the value of an
138 array slot from the last (index,value) pair in the list, and does no
139 checking for duplicates.  The reason for this is efficiency, pure and simple.
140             </para>
141           </listitem>
142         </varlistentry>
143       </variablelist>
144
145     </sect3>
146
147       <sect3 id="infelicities-Prelude">
148         <title>In <literal>Prelude</literal> support</title>
149
150       <variablelist>
151         <varlistentry>
152           <term>Arbitrary-sized tuples</term>
153           <listitem>
154             <para>Tuples are currently limited to size 100.  HOWEVER:
155             standard instances for tuples (<literal>Eq</literal>,
156             <literal>Ord</literal>, <literal>Bounded</literal>,
157             <literal>Ix</literal> <literal>Read</literal>, and
158             <literal>Show</literal>) are available
159             <emphasis>only</emphasis> up to 16-tuples.</para>
160
161             <para>This limitation is easily subvertible, so please ask
162             if you get stuck on it.</para>
163             </listitem>
164           </varlistentry>
165
166           <varlistentry>
167             <term><literal>Read</literal>ing integers</term>
168             <listitem>
169               <para>GHC's implementation of the
170               <literal>Read</literal> class for integral types accepts
171               hexadecimal and octal literals (the code in the Haskell
172               98 report doesn't).  So, for example,
173 <programlisting>read "0xf00" :: Int</programlisting>
174               works in GHC.</para>
175               <para>A possible reason for this is that <literal>readLitChar</literal> accepts hex and
176                 octal escapes, so it seems inconsistent not to do so for integers too.</para>
177             </listitem>
178           </varlistentry>
179
180           <varlistentry>
181             <term><literal>isAlpha</literal></term>
182             <listitem>
183               <para>The Haskell 98 definition of <literal>isAlpha</literal>
184               is:</para>
185
186 <programlisting>isAlpha c = isUpper c || isLower c</programlisting>
187
188               <para>GHC's implementation diverges from the Haskell 98
189               definition in the sense that Unicode alphabetic characters which
190               are neither upper nor lower case will still be identified as
191               alphabetic by <literal>isAlpha</literal>.</para>
192             </listitem>
193           </varlistentry>
194
195           <varlistentry>
196             <term><literal>hGetContents</literal></term>
197             <listitem>
198               <para>
199                 Lazy I/O throws an exception if an error is
200                 encountered, in contrast to the Haskell 98 spec which
201                 requires that errors are discarded (see Section 21.2.2
202                 of the Haskell 98 report).  The exception thrown is
203                 the usual IO exception that would be thrown if the
204                 failing IO operation was performed in the IO monad, and can
205                 be caught by <literal>System.IO.Error.catch</literal>
206                 or <literal>Control.Exception.catch</literal>.
207               </para>
208             </listitem>
209           </varlistentry>
210         </variablelist>
211     </sect3>
212   </sect2>
213
214   <sect2 id="haskell98-undefined">
215     <title>GHC's interpretation of undefined behaviour in
216     Haskell&nbsp;98</title>
217
218     <para>This section documents GHC's take on various issues that are
219     left undefined or implementation specific in Haskell 98.</para>
220
221     <variablelist>
222       <varlistentry>
223         <term>
224           The <literal>Char</literal> type
225           <indexterm><primary><literal>Char</literal></primary><secondary>size of</secondary></indexterm>
226         </term>
227         <listitem>
228           <para>Following the ISO-10646 standard,
229           <literal>maxBound :: Char</literal> in GHC is
230           <literal>0x10FFFF</literal>.</para>
231         </listitem>
232       </varlistentry>
233
234       <varlistentry>
235         <term>
236           Sized integral types
237           <indexterm><primary><literal>Int</literal></primary><secondary>size of</secondary></indexterm>
238         </term>
239         <listitem>
240           <para>In GHC the <literal>Int</literal> type follows the
241           size of an address on the host architecture; in other words
242           it holds 32 bits on a 32-bit machine, and 64-bits on a
243           64-bit machine.</para>
244
245           <para>Arithmetic on <literal>Int</literal> is unchecked for
246           overflow<indexterm><primary>overflow</primary><secondary><literal>Int</literal></secondary>
247             </indexterm>, so all operations on <literal>Int</literal> happen
248           modulo
249           2<superscript><replaceable>n</replaceable></superscript>
250           where <replaceable>n</replaceable> is the size in bits of
251           the <literal>Int</literal> type.</para>
252
253           <para>The <literal>fromInteger</literal><indexterm><primary><literal>fromInteger</literal></primary>
254             </indexterm> function (and hence
255           also <literal>fromIntegral</literal><indexterm><primary><literal>fromIntegral</literal></primary>
256             </indexterm>) is a special case when
257           converting to <literal>Int</literal>.  The value of
258           <literal>fromIntegral x :: Int</literal> is given by taking
259           the lower <replaceable>n</replaceable> bits of <literal>(abs
260           x)</literal>, multiplied by the sign of <literal>x</literal>
261           (in 2's complement <replaceable>n</replaceable>-bit
262           arithmetic).  This behaviour was chosen so that for example
263           writing <literal>0xffffffff :: Int</literal> preserves the
264           bit-pattern in the resulting <literal>Int</literal>.</para>
265
266
267            <para>Negative literals, such as <literal>-3</literal>, are
268              specified by (a careful reading of) the Haskell Report as
269              meaning <literal>Prelude.negate (Prelude.fromInteger 3)</literal>.
270              So <literal>-2147483648</literal> means <literal>negate (fromInteger 2147483648)</literal>.
271              Since <literal>fromInteger</literal> takes the lower 32 bits of the representation,
272              <literal>fromInteger (2147483648::Integer)</literal>, computed at type <literal>Int</literal> is
273              <literal>-2147483648::Int</literal>.  The <literal>negate</literal> operation then
274              overflows, but it is unchecked, so <literal>negate (-2147483648::Int)</literal> is just
275              <literal>-2147483648</literal>.  In short, one can write <literal>minBound::Int</literal> as
276              a literal with the expected meaning (but that is not in general guaranteed.
277              </para>
278
279           <para>The <literal>fromIntegral</literal> function also
280           preserves bit-patterns when converting between the sized
281           integral types (<literal>Int8</literal>,
282           <literal>Int16</literal>, <literal>Int32</literal>,
283           <literal>Int64</literal> and the unsigned
284           <literal>Word</literal> variants), see the modules
285           <literal>Data.Int</literal> and <literal>Data.Word</literal>
286           in the library documentation.</para>
287         </listitem>
288       </varlistentry>
289
290       <varlistentry>
291         <term>Unchecked float arithmetic</term>
292         <listitem>
293           <para>Operations on <literal>Float</literal> and
294           <literal>Double</literal> numbers are
295           <emphasis>unchecked</emphasis> for overflow, underflow, and
296           other sad occurrences.  (note, however that some
297           architectures trap floating-point overflow and
298           loss-of-precision and report a floating-point exception,
299           probably terminating the
300           program)<indexterm><primary>floating-point
301           exceptions</primary></indexterm>.</para>
302         </listitem>
303       </varlistentry>
304     </variablelist>
305
306     </sect2>
307
308   <sect2 id="ffi-divergence">
309     <title>Divergence from the FFI specification</title>
310
311     <variablelist>
312       <varlistentry>
313         <term><literal>hs_init()</literal> not allowed
314         after <literal>hs_exit()</literal></term>
315         <listitem>
316           <para>The FFI spec requires the implementation to support
317             re-initialising itself after being shut down
318             with <literal>hs_exit()</literal>, but GHC does not
319             currently support that.</para>
320         </listitem>
321       </varlistentry>
322     </variablelist>
323   </sect2>
324
325   </sect1>
326
327
328   <sect1 id="bugs">
329     <title>Known bugs or infelicities</title>
330
331     <para>The bug tracker lists bugs that have been reported in GHC but not
332       yet fixed: see the <ulink url="http://sourceforge.net/projects/ghc/">SourceForge GHC
333     page</ulink>.  In addition to those, GHC also has the following known bugs
334       or  infelicities.  These bugs are more permanent; it is unlikely that
335       any of them will be fixed in the short term.</para>
336
337   <sect2 id="bugs-ghc">
338     <title>Bugs in GHC</title>
339
340     <itemizedlist>
341       <listitem>
342         <para> GHC can warn about non-exhaustive or overlapping
343         patterns (see <xref linkend="options-sanity"/>), and usually
344         does so correctly.  But not always.  It gets confused by
345         string patterns, and by guards, and can then emit bogus
346         warnings.  The entire overlap-check code needs an overhaul
347         really.</para>
348       </listitem>
349
350       <listitem>
351         <para>GHC does not allow you to have a data type with a context
352            that mentions type variables that are not data type parameters.
353           For example:
354 <programlisting>
355   data C a b => T a = MkT a
356 </programlisting>
357           so that <literal>MkT</literal>'s type is
358 <programlisting>
359   MkT :: forall a b. C a b => a -> T a
360 </programlisting>
361         In principle, with a suitable class declaration with a functional dependency,
362          it's possible that this type is not ambiguous; but GHC nevertheless rejects
363           it.  The type variables mentioned in the context of the data type declaration must
364         be among the type parameters of the data type.</para>
365       </listitem>
366
367       <listitem>
368         <para>GHC's inliner can be persuaded into non-termination
369         using the standard way to encode recursion via a data type:</para>
370 <programlisting>
371   data U = MkU (U -> Bool)
372
373   russel :: U -> Bool
374   russel u@(MkU p) = not $ p u
375
376   x :: Bool
377   x = russel (MkU russel)
378 </programlisting>
379
380         <para>We have never found another class of programs, other
381         than this contrived one, that makes GHC diverge, and fixing
382         the problem would impose an extra overhead on every
383         compilation.  So the bug remains un-fixed.  There is more
384         background in <ulink
385         url="http://research.microsoft.com/~simonpj/Papers/inlining/">
386         Secrets of the GHC inliner</ulink>.</para>
387       </listitem>
388
389       <listitem>
390         <para>GHC does not keep careful track of
391             what instance declarations are 'in scope' if they come from other packages.
392         Instead, all instance declarations that GHC has seen in other
393         packages are all in scope everywhere, whether or not the
394         module from that package is used by the command-line
395         expression.  This bug affects only the <option>--make</option> mode and
396           GHCi.</para>
397       </listitem>
398
399     </itemizedlist>
400   </sect2>
401
402   <sect2 id="bugs-ghci">
403     <title>Bugs in GHCi (the interactive GHC)</title>
404     <itemizedlist>
405       <listitem>
406         <para>GHCi does not respect the <literal>default</literal>
407         declaration in the module whose scope you are in.  Instead,
408         for expressions typed at the command line, you always get the
409         default default-type behaviour; that is,
410         <literal>default(Int,Double)</literal>.</para>
411
412         <para>It would be better for GHCi to record what the default
413         settings in each module are, and use those of the 'current'
414         module (whatever that is).</para>
415       </listitem>
416
417       <listitem>
418       <para>On Windows, there's a GNU ld/BFD bug
419       whereby it emits bogus PE object files that have more than
420       0xffff relocations. When GHCi tries to load a package affected by this
421       bug, you get an error message of the form
422 <screen>
423 Loading package javavm ... linking ... WARNING: Overflown relocation field (# relocs found: 30765)
424 </screen>
425       The last time we looked, this bug still
426       wasn't fixed in the BFD codebase, and there wasn't any
427       noticeable interest in fixing it when we reported the bug
428       back in 2001 or so.
429       </para>
430       <para>The workaround is to split up the .o files that make up
431       your package into two or more .o's, along the lines of
432       how the "base" package does it.</para>
433       </listitem>
434     </itemizedlist>
435   </sect2>
436   </sect1>
437
438 </chapter>
439
440 <!-- Emacs stuff:
441      ;;; Local Variables: ***
442      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
443      ;;; End: ***
444  -->