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