Clarify parsing infelicity.
[ghc.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;standards vs.&nbsp;Glasgow Haskell: language non-compliance
7 </title>
8
9 <indexterm><primary>GHC vs the Haskell standards</primary></indexterm>
10 <indexterm><primary>Haskell standards vs GHC</primary></indexterm>
11
12 <para>
13 This section lists Glasgow Haskell infelicities in its
14 implementation of Haskell&nbsp;98 and Haskell&nbsp;2010.
15 See also the &ldquo;when things go wrong&rdquo; section
16 (<xref linkend="wrong"/>) for information about crashes,
17 space leaks, and other undesirable phenomena.
18 </para>
19
20 <para>
21 The limitations here are listed in Haskell Report order
22 (roughly).
23 </para>
24
25 <sect2 id="haskell-standards-divergence">
26 <title>Divergence from Haskell&nbsp;98 and Haskell&nbsp;2010</title>
27
28 <para>
29 By default, GHC mainly aims to behave (mostly) like a Haskell&nbsp;2010
30 compiler, although you can tell it to try to behave like a
31 particular version of the language with the
32 <literal>-XHaskell98</literal> and
33 <literal>-XHaskell2010</literal> flags. The known deviations
34 from the standards are described below. Unless otherwise stated,
35 the deviation applies in Haskell&nbsp;98, Haskell&nbsp;2010 and
36 the default modes.
37 </para>
38
39 <sect3 id="infelicities-lexical">
40 <title>Lexical syntax</title>
41
42 <itemizedlist>
43 <listitem>
44 <para>Certain lexical rules regarding qualified identifiers
45 are slightly different in GHC compared to the Haskell
46 report. When you have
47 <replaceable>module</replaceable><literal>.</literal><replaceable>reservedop</replaceable>,
48 such as <literal>M.\</literal>, GHC will interpret it as a
49 single qualified operator rather than the two lexemes
50 <literal>M</literal> and <literal>.\</literal>.</para>
51 </listitem>
52 </itemizedlist>
53 </sect3>
54
55 <sect3 id="infelicities-syntax">
56 <title>Context-free syntax</title>
57
58 <itemizedlist>
59 <listitem>
60 <para>In Haskell&nbsp;98 mode and by default (but not in
61 Haskell&nbsp;2010 mode), GHC is a little less strict about the
62 layout rule when used
63 in <literal>do</literal> expressions. Specifically, the
64 restriction that "a nested context must be indented further to
65 the right than the enclosing context" is relaxed to allow the
66 nested context to be at the same level as the enclosing context,
67 if the enclosing context is a <literal>do</literal>
68 expression.</para>
69
70 <para>For example, the following code is accepted by GHC:
71
72 <programlisting>
73 main = do args &lt;- getArgs
74 if null args then return [] else do
75 ps &lt;- mapM process args
76 mapM print ps</programlisting>
77
78 This behaviour is controlled by the
79 <literal>NondecreasingIndentation</literal> extension.
80 </para>
81 </listitem>
82
83 <listitem>
84 <para>GHC doesn't do the fixity resolution in expressions during
85 parsing as required by Haskell&nbsp;98 (but not by Haskell&nbsp;2010).
86 For example, according to the Haskell&nbsp;98 report, the
87 following expression is legal:
88 <programlisting>
89 let x = 42 in x == 42 == True</programlisting>
90 and parses as:
91 <programlisting>
92 (let x = 42 in x == 42) == True</programlisting>
93
94 because according to the report, the <literal>let</literal>
95 expression <quote>extends as far to the right as
96 possible</quote>. Since it can't extend past the second
97 equals sign without causing a parse error
98 (<literal>==</literal> is non-fix), the
99 <literal>let</literal>-expression must terminate there. GHC
100 simply gobbles up the whole expression, parsing like this:
101 <programlisting>
102 (let x = 42 in x == 42 == True)</programlisting></para>
103 </listitem>
104
105 <listitem>
106 <para>The Haskell Report allows you to put a unary
107 <literal>-</literal> preceding certain expressions headed by
108 keywords, allowing constructs like <literal>- case x of
109 ...</literal> or <literal>- do { ... }</literal>. GHC does
110 not allow this. Instead, unary <literal>-</literal> is
111 allowed before only expressions that could potentially
112 be applied as a function.
113 </para>
114 </listitem>
115
116 </itemizedlist>
117 </sect3>
118
119 <sect3 id="infelicities-exprs-pats">
120 <title>Expressions and patterns</title>
121
122 <para>In its default mode, GHC makes some programs slightly more defined
123 than they should be. For example, consider
124 <programlisting>
125 f :: [a] -> b -> b
126 f [] = error "urk"
127 f (x:xs) = \v -> v
128
129 main = print (f [] `seq` True)
130 </programlisting>
131 This should call <literal>error</literal> but actually prints <literal>True</literal>.
132 Reason: GHC eta-expands <literal>f</literal> to
133 <programlisting>
134 f :: [a] -> b -> b
135 f [] v = error "urk"
136 f (x:xs) v = v
137 </programlisting>
138 This improves efficiency slightly but significantly for most programs, and
139 is bad for only a few. To suppress this bogus "optimisation" use <option>-fpedantic-bottoms</option>.
140 </para>
141
142 </sect3>
143
144 <sect3 id="infelicities-decls">
145 <title>Declarations and bindings</title>
146
147 <para>In its default mode, GHC does not accept datatype contexts,
148 as it has been decided to remove them from the next version of the
149 language standard. This behaviour can be controlled with the
150 <option>DatatypeContexts</option> extension.
151 See <xref linkend="datatype-contexts" />.</para>
152 </sect3>
153
154 <sect3 id="infelicities-Modules">
155 <title>Module system and interface files</title>
156
157 <para>GHC requires the use of <literal>hs-boot</literal>
158 files to cut the recursive loops among mutually recursive modules
159 as described in <xref linkend="mutual-recursion"/>. This more of an infelicity
160 than a bug: the Haskell Report says
161 (<ulink url="http://haskell.org/onlinereport/modules.html#sect5.7">Section 5.7</ulink>) "Depending on the Haskell
162 implementation used, separate compilation of mutually
163 recursive modules may require that imported modules contain
164 additional information so that they may be referenced before
165 they are compiled. Explicit type signatures for all exported
166 values may be necessary to deal with mutual recursion. The
167 precise details of separate compilation are not defined by
168 this Report."
169
170 </para>
171
172 </sect3>
173
174 <sect3 id="infelicities-numbers">
175 <title>Numbers, basic types, and built-in classes</title>
176
177 <variablelist>
178 <varlistentry>
179 <term>Num superclasses</term>
180 <listitem>
181 <para>
182 The <literal>Num</literal> class does not have
183 <literal>Show</literal> or <literal>Eq</literal>
184 superclasses.
185 </para>
186
187 <para>
188 You can make code that works with both
189 Haskell98/Haskell2010 and GHC by:
190 <itemizedlist>
191 <listitem>
192 <para>
193 Whenever you make a <literal>Num</literal> instance
194 of a type, also make <literal>Show</literal> and
195 <literal>Eq</literal> instances, and
196 </para>
197 </listitem>
198 <listitem>
199 <para>
200 Whenever you give a function, instance or class a
201 <literal>Num t</literal> constraint, also give it
202 <literal>Show t</literal> and
203 <literal>Eq t</literal> constraints.
204 </para>
205 </listitem>
206 </itemizedlist>
207 </para>
208 </listitem>
209 </varlistentry>
210
211 <varlistentry>
212 <term>Bits superclasses</term>
213 <listitem>
214 <para>
215 The <literal>Bits</literal> class does not have
216 a <literal>Num</literal> superclasses. It therefore
217 does not have default methods for the
218 <literal>bit</literal>,
219 <literal>testBit</literal> and
220 <literal>popCount</literal> methods.
221 </para>
222
223 <para>
224 You can make code that works with both
225 Haskell2010 and GHC by:
226 <itemizedlist>
227 <listitem>
228 <para>
229 Whenever you make a <literal>Bits</literal> instance
230 of a type, also make a <literal>Num</literal>
231 instance, and
232 </para>
233 </listitem>
234 <listitem>
235 <para>
236 Whenever you give a function, instance or class a
237 <literal>Bits t</literal> constraint, also give it
238 a <literal>Num t</literal> constraint, and
239 </para>
240 </listitem>
241 <listitem>
242 <para>
243 Always define the <literal>bit</literal>,
244 <literal>testBit</literal> and
245 <literal>popCount</literal> methods in
246 <literal>Bits</literal> instances.
247 </para>
248 </listitem>
249 </itemizedlist>
250 </para>
251 </listitem>
252 </varlistentry>
253
254 <varlistentry>
255 <term>Extra instances</term>
256 <listitem>
257 <para>
258 The following extra instances are defined:
259 </para>
260 <programlisting>
261 instance Functor ((->) r)
262 instance Monad ((->) r)
263 instance Functor ((,) a)
264 instance Functor (Either a)
265 instance Monad (Either e)
266 </programlisting>
267 </listitem>
268 </varlistentry>
269
270 <varlistentry>
271 <term>Multiply-defined array elements&mdash;not checked:</term>
272 <listitem>
273 <para>This code fragment should
274 elicit a fatal error, but it does not:
275
276 <programlisting>
277 main = print (array (1,1) [(1,2), (1,3)])</programlisting>
278 GHC's implementation of <literal>array</literal> takes the value of an
279 array slot from the last (index,value) pair in the list, and does no
280 checking for duplicates. The reason for this is efficiency, pure and simple.
281 </para>
282 </listitem>
283 </varlistentry>
284 </variablelist>
285
286 </sect3>
287
288 <sect3 id="infelicities-Prelude">
289 <title>In <literal>Prelude</literal> support</title>
290
291 <variablelist>
292 <varlistentry>
293 <term>Arbitrary-sized tuples</term>
294 <listitem>
295 <para>Tuples are currently limited to size 100. HOWEVER:
296 standard instances for tuples (<literal>Eq</literal>,
297 <literal>Ord</literal>, <literal>Bounded</literal>,
298 <literal>Ix</literal> <literal>Read</literal>, and
299 <literal>Show</literal>) are available
300 <emphasis>only</emphasis> up to 16-tuples.</para>
301
302 <para>This limitation is easily subvertible, so please ask
303 if you get stuck on it.</para>
304 </listitem>
305 </varlistentry>
306 <varlistentry>
307 <term><literal>splitAt</literal> semantics</term>
308 <para><literal>Data.List.splitAt</literal> is stricter than specified in the
309 Report. Specifically, the Report specifies that
310 <programlisting>splitAt n xs = (take n xs, drop n xs)</programlisting>
311 which implies that
312 <programlisting>splitAt undefined undefined = (undefined, undefined)</programlisting>
313 but GHC's implementation is strict in its first argument, so
314 <programlisting>splitAt undefined [] = undefined</programlisting>
315 </para>
316 </varlistentry>
317 <varlistentry>
318 <term><literal>Read</literal>ing integers</term>
319 <listitem>
320 <para>GHC's implementation of the
321 <literal>Read</literal> class for integral types accepts
322 hexadecimal and octal literals (the code in the Haskell
323 98 report doesn't). So, for example,
324 <programlisting>read "0xf00" :: Int</programlisting>
325 works in GHC.</para>
326 <para>A possible reason for this is that <literal>readLitChar</literal> accepts hex and
327 octal escapes, so it seems inconsistent not to do so for integers too.</para>
328 </listitem>
329 </varlistentry>
330
331 <varlistentry>
332 <term><literal>isAlpha</literal></term>
333 <listitem>
334 <para>The Haskell 98 definition of <literal>isAlpha</literal>
335 is:</para>
336
337 <programlisting>isAlpha c = isUpper c || isLower c</programlisting>
338
339 <para>GHC's implementation diverges from the Haskell 98
340 definition in the sense that Unicode alphabetic characters which
341 are neither upper nor lower case will still be identified as
342 alphabetic by <literal>isAlpha</literal>.</para>
343 </listitem>
344 </varlistentry>
345
346 <varlistentry>
347 <term><literal>hGetContents</literal></term>
348 <listitem>
349 <para>
350 Lazy I/O throws an exception if an error is
351 encountered, in contrast to the Haskell 98 spec which
352 requires that errors are discarded (see Section 21.2.2
353 of the Haskell 98 report). The exception thrown is
354 the usual IO exception that would be thrown if the
355 failing IO operation was performed in the IO monad, and can
356 be caught by <literal>System.IO.Error.catch</literal>
357 or <literal>Control.Exception.catch</literal>.
358 </para>
359 </listitem>
360 </varlistentry>
361 </variablelist>
362 </sect3>
363
364 <sect3 id="infelicities-ffi">
365 <title>The Foreign Function Interface</title>
366 <variablelist>
367 <varlistentry>
368 <term><literal>hs_init()</literal> not allowed
369 after <literal>hs_exit()</literal></term>
370 <listitem>
371 <para>The FFI spec requires the implementation to support
372 re-initialising itself after being shut down
373 with <literal>hs_exit()</literal>, but GHC does not
374 currently support that.</para>
375 </listitem>
376 </varlistentry>
377 </variablelist>
378 </sect3>
379
380 </sect2>
381
382 <sect2 id="haskell-98-2010-undefined">
383 <title>GHC's interpretation of undefined behaviour in
384 Haskell&nbsp;98 and Haskell&nbsp;2010</title>
385
386 <para>This section documents GHC's take on various issues that are
387 left undefined or implementation specific in Haskell 98.</para>
388
389 <variablelist>
390 <varlistentry>
391 <term>
392 The <literal>Char</literal> type
393 <indexterm><primary><literal>Char</literal></primary><secondary>size of</secondary></indexterm>
394 </term>
395 <listitem>
396 <para>Following the ISO-10646 standard,
397 <literal>maxBound :: Char</literal> in GHC is
398 <literal>0x10FFFF</literal>.</para>
399 </listitem>
400 </varlistentry>
401
402 <varlistentry>
403 <term>
404 Sized integral types
405 <indexterm><primary><literal>Int</literal></primary><secondary>size of</secondary></indexterm>
406 </term>
407 <listitem>
408 <para>In GHC the <literal>Int</literal> type follows the
409 size of an address on the host architecture; in other words
410 it holds 32 bits on a 32-bit machine, and 64-bits on a
411 64-bit machine.</para>
412
413 <para>Arithmetic on <literal>Int</literal> is unchecked for
414 overflow<indexterm><primary>overflow</primary><secondary><literal>Int</literal></secondary>
415 </indexterm>, so all operations on <literal>Int</literal> happen
416 modulo
417 2<superscript><replaceable>n</replaceable></superscript>
418 where <replaceable>n</replaceable> is the size in bits of
419 the <literal>Int</literal> type.</para>
420
421 <para>The <literal>fromInteger</literal><indexterm><primary><literal>fromInteger</literal></primary>
422 </indexterm> function (and hence
423 also <literal>fromIntegral</literal><indexterm><primary><literal>fromIntegral</literal></primary>
424 </indexterm>) is a special case when
425 converting to <literal>Int</literal>. The value of
426 <literal>fromIntegral x :: Int</literal> is given by taking
427 the lower <replaceable>n</replaceable> bits of <literal>(abs
428 x)</literal>, multiplied by the sign of <literal>x</literal>
429 (in 2's complement <replaceable>n</replaceable>-bit
430 arithmetic). This behaviour was chosen so that for example
431 writing <literal>0xffffffff :: Int</literal> preserves the
432 bit-pattern in the resulting <literal>Int</literal>.</para>
433
434
435 <para>Negative literals, such as <literal>-3</literal>, are
436 specified by (a careful reading of) the Haskell Report as
437 meaning <literal>Prelude.negate (Prelude.fromInteger 3)</literal>.
438 So <literal>-2147483648</literal> means <literal>negate (fromInteger 2147483648)</literal>.
439 Since <literal>fromInteger</literal> takes the lower 32 bits of the representation,
440 <literal>fromInteger (2147483648::Integer)</literal>, computed at type <literal>Int</literal> is
441 <literal>-2147483648::Int</literal>. The <literal>negate</literal> operation then
442 overflows, but it is unchecked, so <literal>negate (-2147483648::Int)</literal> is just
443 <literal>-2147483648</literal>. In short, one can write <literal>minBound::Int</literal> as
444 a literal with the expected meaning (but that is not in general guaranteed).
445 </para>
446
447 <para>The <literal>fromIntegral</literal> function also
448 preserves bit-patterns when converting between the sized
449 integral types (<literal>Int8</literal>,
450 <literal>Int16</literal>, <literal>Int32</literal>,
451 <literal>Int64</literal> and the unsigned
452 <literal>Word</literal> variants), see the modules
453 <literal>Data.Int</literal> and <literal>Data.Word</literal>
454 in the library documentation.</para>
455 </listitem>
456 </varlistentry>
457
458 <varlistentry>
459 <term>Unchecked float arithmetic</term>
460 <listitem>
461 <para>Operations on <literal>Float</literal> and
462 <literal>Double</literal> numbers are
463 <emphasis>unchecked</emphasis> for overflow, underflow, and
464 other sad occurrences. (note, however, that some
465 architectures trap floating-point overflow and
466 loss-of-precision and report a floating-point exception,
467 probably terminating the
468 program)<indexterm><primary>floating-point
469 exceptions</primary></indexterm>.</para>
470 </listitem>
471 </varlistentry>
472 </variablelist>
473 </sect2>
474
475 </sect1>
476
477
478 <sect1 id="bugs">
479 <title>Known bugs or infelicities</title>
480
481 <para>The bug tracker lists bugs that have been reported in GHC but not
482 yet fixed: see the <ulink url="http://ghc.haskell.org/trac/ghc/">GHC Trac</ulink>. In addition to those, GHC also has the following known bugs
483 or infelicities. These bugs are more permanent; it is unlikely that
484 any of them will be fixed in the short term.</para>
485
486 <sect2 id="bugs-ghc">
487 <title>Bugs in GHC</title>
488
489 <itemizedlist>
490 <listitem>
491 <para>GHC's runtime system implements cooperative multitasking, with
492 context switching potentially occurring only when a program allocates.
493 This means that programs that do not allocate may never context switch.
494 See
495 <ulink url="https://ghc.haskell.org/trac/ghc/ticket/367">Trac #367</ulink>
496 for further discussion.
497 </para>
498 <para>If you are hit by this, you may want to compile the affected module
499 with <literal>-fno-omit-yields</literal>. This flag ensures that yield points
500 are inserted at every function entrypoint (at the expense of a bit of
501 performance).
502 </para>
503 </listitem>
504
505 <listitem>
506 <para> GHC can warn about non-exhaustive or overlapping
507 patterns (see <xref linkend="options-sanity"/>), and usually
508 does so correctly. But not always. It gets confused by
509 string patterns, and by guards, and can then emit bogus
510 warnings. The entire overlap-check code needs an overhaul
511 really.</para>
512 </listitem>
513
514 <listitem>
515 <para>GHC does not allow you to have a data type with a context
516 that mentions type variables that are not data type parameters.
517 For example:
518 <programlisting>
519 data C a b => T a = MkT a
520 </programlisting>
521 so that <literal>MkT</literal>'s type is
522 <programlisting>
523 MkT :: forall a b. C a b => a -> T a
524 </programlisting>
525 In principle, with a suitable class declaration with a functional dependency,
526 it's possible that this type is not ambiguous; but GHC nevertheless rejects
527 it. The type variables mentioned in the context of the data type declaration must
528 be among the type parameters of the data type.</para>
529 </listitem>
530
531 <listitem>
532 <para>GHC's inliner can be persuaded into non-termination
533 using the standard way to encode recursion via a data type:
534 <programlisting>
535 data U = MkU (U -> Bool)
536
537 russel :: U -> Bool
538 russel u@(MkU p) = not $ p u
539
540 x :: Bool
541 x = russel (MkU russel)
542 </programlisting>
543 The non-termination is reported like this:
544 <programlisting>
545 ghc: panic! (the 'impossible' happened)
546 (GHC version 7.10.1 for x86_64-unknown-linux):
547 Simplifier ticks exhausted
548 When trying UnfoldingDone x_alB
549 To increase the limit, use -fsimpl-tick-factor=N (default 100)
550 </programlisting>
551 with the panic being reported no matter how high a <literal>-fsimpl-tick-factor</literal> you supply.
552 </para>
553 <para>
554 We have never found another class of programs, other
555 than this contrived one, that makes GHC diverge, and fixing
556 the problem would impose an extra overhead on every
557 compilation. So the bug remains un-fixed. There is more
558 background in <ulink
559 url="http://research.microsoft.com/~simonpj/Papers/inlining/">
560 Secrets of the GHC inliner</ulink>.</para>
561 </listitem>
562
563 <listitem>
564 <para>On 32-bit x86 platforms when using the native code
565 generator, the
566 <option>-fexcess-precision</option><indexterm><primary><option>-fexcess-precision</option></primary></indexterm> option
567 is always on. This means that floating-point calculations are
568 non-deterministic, because depending on how the program is
569 compiled (optimisation settings, for example), certain
570 calculations might be done at 80-bit precision instead of the
571 intended 32-bit or 64-bit precision. Floating-point results
572 may differ when optimisation is turned on. In the worst case,
573 referential transparency is violated, because for example
574 <literal>let x = E1 in E2</literal> can evaluate to a
575 different value than <literal>E2[E1/x]</literal>.</para>
576
577 <para>
578 One workaround is to use the
579 <option>-msse2</option><indexterm><primary><option>-msse2</option></primary></indexterm>
580 option (see <xref linkend="options-platform" />, which
581 generates code to use the SSE2 instruction set instead of
582 the x87 instruction set. SSE2 code uses the correct
583 precision for all floating-point operations, and so gives
584 deterministic results. However, note that this only works
585 with processors that support SSE2 (Intel Pentium 4 or AMD
586 Athlon 64 and later), which is why the option is not enabled
587 by default. The libraries that come with GHC are probably
588 built without this option, unless you built GHC yourself.
589 </para>
590 </listitem>
591
592 </itemizedlist>
593 </sect2>
594
595 <sect2 id="bugs-ghci">
596 <title>Bugs in GHCi (the interactive GHC)</title>
597 <itemizedlist>
598 <listitem>
599 <para>GHCi does not respect the <literal>default</literal>
600 declaration in the module whose scope you are in. Instead,
601 for expressions typed at the command line, you always get the
602 default default-type behaviour; that is,
603 <literal>default(Int,Double)</literal>.</para>
604
605 <para>It would be better for GHCi to record what the default
606 settings in each module are, and use those of the 'current'
607 module (whatever that is).</para>
608 </listitem>
609
610 <listitem>
611 <para>On Windows, there's a GNU ld/BFD bug
612 whereby it emits bogus PE object files that have more than
613 0xffff relocations. When GHCi tries to load a package affected by this
614 bug, you get an error message of the form
615 <screen>
616 Loading package javavm ... linking ... WARNING: Overflown relocation field (# relocs found: 30765)
617 </screen>
618 The last time we looked, this bug still
619 wasn't fixed in the BFD codebase, and there wasn't any
620 noticeable interest in fixing it when we reported the bug
621 back in 2001 or so.
622 </para>
623 <para>The workaround is to split up the .o files that make up
624 your package into two or more .o's, along the lines of
625 how the "base" package does it.</para>
626 </listitem>
627 </itemizedlist>
628 </sect2>
629 </sect1>
630
631 </chapter>
632
633 <!-- Emacs stuff:
634 ;;; Local Variables: ***
635 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
636 ;;; ispell-local-dictionary: "british" ***
637 ;;; End: ***
638 -->