-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdoc.diff
More file actions
4906 lines (4703 loc) · 204 KB
/
doc.diff
File metadata and controls
4906 lines (4703 loc) · 204 KB
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
diff -urd 7.8.2-original/glasgow_exts.xml original/glasgow_exts.xml
--- 7.8.2-original/glasgow_exts.xml 2014-04-08 03:26:08.000000000 +0900
+++ original/glasgow_exts.xml 2016-04-09 21:36:11.394998369 +0900
@@ -3,7 +3,7 @@
<indexterm><primary>language, GHC</primary></indexterm>
<indexterm><primary>extensions, GHC</primary></indexterm>
As with all known Haskell systems, GHC implements some extensions to
-the language. They can all be enabled or disabled by commandline flags
+the language. They can all be enabled or disabled by command line flags
or language pragmas. By default GHC understands the most recent Haskell
version it supports, plus a handful of extensions.
</para>
@@ -456,10 +456,10 @@
</para>
<para>
- This can make a difference when the positive and negative range of
- a numeric data type don't match up. For example,
+ This can make a difference when the positive and negative range of
+ a numeric data type don't match up. For example,
in 8-bit arithmetic -128 is representable, but +128 is not.
- So <literal>negate (fromInteger 128)</literal> will elicit an
+ So <literal>negate (fromInteger 128)</literal> will elicit an
unexpected integer-literal-overflow message.
</para>
</sect2>
@@ -480,6 +480,26 @@
</para>
</sect2>
+ <sect2 id="binary-literals">
+ <title>Binary integer literals</title>
+ <para>
+ Haskell 2010 and Haskell 98 allows for integer literals to
+ be given in decimal, octal (prefixed by
+ <literal>0o</literal> or <literal>0O</literal>), or
+ hexadecimal notation (prefixed by <literal>0x</literal> or
+ <literal>0X</literal>).
+ </para>
+
+ <para>
+ The language extension <option>-XBinaryLiterals</option>
+ adds support for expressing integer literals in binary
+ notation with the prefix <literal>0b</literal> or
+ <literal>0B</literal>. For instance, the binary integer
+ literal <literal>0b11001001</literal> will be desugared into
+ <literal>fromInteger 201</literal> when
+ <option>-XBinaryLiterals</option> is enabled.
+ </para>
+ </sect2>
<!-- ====================== HIERARCHICAL MODULES ======================= -->
@@ -857,8 +877,8 @@
<para>
Pattern synonyms are enabled by the flag
-<literal>-XPatternSynonyms</literal>, which is required for both
-defining them <emphasis>and</emphasis> using them. More information
+<literal>-XPatternSynonyms</literal>, which is required for defining
+them, but <emphasis>not</emphasis> for using them. More information
and examples of view patterns can be found on the <ulink
url="http://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms">Wiki
page</ulink>.
@@ -967,143 +987,216 @@
In this case, <literal>Head</literal> <replaceable>x</replaceable>
cannot be used in expressions, only patterns, since it wouldn't
specify a value for the <replaceable>xs</replaceable> on the
-right-hand side.
+right-hand side. We can give an explicit inversion of a pattern
+synonym using the following syntax:
</para>
-<para>
-The semantics of a unidirectional pattern synonym declaration and
-usage are as follows:
+<programlisting>
+ pattern Head x <- x:xs where
+ Head x = [x]
+</programlisting>
-<itemizedlist>
+<para>
+The syntax and semantics of pattern synonyms are elaborated in the
+following subsections.
+See the <ulink
+url="http://ghc.haskell.org/trac/ghc/wiki/PatternSynonyms">Wiki
+page</ulink> for more details.
+</para>
-<listitem> Syntax:
+<sect3> <title>Syntax and scoping of pattern synonyms</title>
<para>
A pattern synonym declaration can be either unidirectional or
bidirectional. The syntax for unidirectional pattern synonyms is:
-</para>
<programlisting>
pattern Name args <- pat
</programlisting>
-<para>
and the syntax for bidirectional pattern synonyms is:
-</para>
<programlisting>
pattern Name args = pat
+</programlisting> or
+<programlisting>
+ pattern Name args <- pat where
+ Name args = expr
</programlisting>
+ Either prefix or infix syntax can be
+ used.
+</para>
<para>
Pattern synonym declarations can only occur in the top level of a
module. In particular, they are not allowed as local
- definitions. Currently, they also don't work in GHCi, but that is a
- technical restriction that will be lifted in later versions.
+ definitions.
+</para>
+<para>
+ The variables in the left-hand side of the definition are bound by
+ the pattern on the right-hand side. For implicitly bidirectional
+ pattern synonyms, all the variables of the right-hand side must also
+ occur on the left-hand side; also, wildcard patterns and view
+ patterns are not allowed. For unidirectional and
+ explicitly-bidirectional pattern synonyms, there is no restriction
+ on the right-hand side pattern.
+</para>
+
+<para>
+ Pattern synonyms cannot be defined recursively.
</para>
+</sect3>
+
+<sect3 id="patsyn-impexp"> <title>Import and export of pattern synonyms</title>
+
<para>
The name of the pattern synonym itself is in the same namespace as
- proper data constructors. Either prefix or infix syntax can be
- used. In export/import specifications, you have to prefix pattern
+ proper data constructors. In an export or import specification,
+ you must prefix pattern
names with the <literal>pattern</literal> keyword, e.g.:
-</para>
<programlisting>
module Example (pattern Single) where
pattern Single x = [x]
</programlisting>
-</listitem>
-
-<listitem> Scoping:
-
-<para>
- The variables in the left-hand side of the definition are bound by
- the pattern on the right-hand side. For bidirectional pattern
- synonyms, all the variables of the right-hand side must also occur
- on the left-hand side; also, wildcard patterns and view patterns are
- not allowed. For unidirectional pattern synonyms, there is no
- restriction on the right-hand side pattern.
+Without the <literal>pattern</literal> prefix, <literal>Single</literal> would
+be interpreted as a type constructor in the export list.
</para>
-
<para>
- Pattern synonyms cannot be defined recursively.
+You may also use the <literal>pattern</literal> keyword in an import/export
+specification to import or export an ordinary data constructor. For example:
+<programlisting>
+ import Data.Maybe( pattern Just )
+</programlisting>
+would bring into scope the data constructor <literal>Just</literal> from the
+<literal>Maybe</literal> type, without also bringing the type constructor
+<literal>Maybe</literal> into scope.
</para>
+</sect3>
-</listitem>
-
-<listitem> Typing:
+<sect3> <title>Typing of pattern synonyms</title>
<para>
Given a pattern synonym definition of the form
-</para>
<programlisting>
pattern P var1 var2 ... varN <- pat
</programlisting>
-<para>
it is assigned a <emphasis>pattern type</emphasis> of the form
-</para>
<programlisting>
- pattern CProv => P t1 t2 ... tN :: CReq => t
+ pattern P :: CProv => CReq => t1 -> t2 -> ... -> tN -> t
</programlisting>
-<para>
where <replaceable>CProv</replaceable> and
<replaceable>CReq</replaceable> are type contexts, and
<replaceable>t1</replaceable>, <replaceable>t2</replaceable>, ...,
<replaceable>tN</replaceable> and <replaceable>t</replaceable> are
types.
-</para>
-
-<para>
-A pattern synonym of this type can be used in a pattern if the
-instatiated (monomorphic) type satisfies the constraints of
-<replaceable>CReq</replaceable>. In this case, it extends the context
-available in the right-hand side of the match with
-<replaceable>CProv</replaceable>, just like how an existentially-typed
-data constructor can extend the context.
-</para>
-
-<para>
-For example, in the following program:
-</para>
+Notice the unusual form of the type, with two contexts <replaceable>CProv</replaceable> and <replaceable>CReq</replaceable>:
+<itemizedlist>
+<listitem><para><replaceable>CReq</replaceable> are the constraints <emphasis>required</emphasis> to match the pattern.</para></listitem>
+<listitem><para><replaceable>CProv</replaceable> are the constraints <emphasis>made available (provided)</emphasis>
+by a successful pattern match.</para></listitem>
+</itemizedlist>
+For example, consider
<programlisting>
-{-# LANGUAGE PatternSynonyms, GADTs #-}
-module ShouldCompile where
-
data T a where
- MkT :: (Show b) => a -> b -> T a
+ MkT :: (Show b) => a -> b -> T a
-pattern ExNumPat x = MkT 42 x
-</programlisting>
+f1 :: (Eq a, Num a) => MkT a -> String
+f1 (MkT 42 x) = show x
-<para>
-the pattern type of <literal>ExNumPat</literal> is
-</para>
+pattern ExNumPat :: (Show b) => (Num a, Eq a) => b -> T a
+pattern ExNumPat x = MkT 42 x
-<programlisting>
-pattern (Show b) => ExNumPat b :: (Num a, Eq a) => T a
+f2 :: (Eq a, Num a) => MkT a -> String
+f2 (ExNumPat x) = show x
</programlisting>
-
+Here <literal>f1</literal> does not use pattern synonyms. To match against the
+numeric pattern <literal>42</literal> <emphasis>requires</emphasis> the caller to
+satisfy the constraints <literal>(Num a, Eq a)</literal>,
+so they appear in <literal>f1</literal>'s type. The call to <literal>show</literal> generates a <literal>(Show b)</literal>
+constraint, where <literal>b</literal> is an existentially type variable bound by the pattern match
+on <literal>MkT</literal>. But the same pattern match also <emphasis>provides</emphasis> the constraint
+<literal>(Show b)</literal> (see <literal>MkT</literal>'s type), and so all is well.
+</para>
<para>
- and so can be used in a function definition like the following:
+Exactly the same reasoning applies to <literal>ExNumPat</literal>:
+matching against <literal>ExNumPat</literal> <emphasis>requires</emphasis>
+the constraints <literal>(Num a, Eq a)</literal>, and <emphasis>provides</emphasis>
+the constraint <literal>(Show b)</literal>.
</para>
+<para>
+Note also the following points
+<itemizedlist>
+<listitem><para>
+In the common case where <replaceable>CReq</replaceable> is empty,
+ <literal>()</literal>, it can be omitted altogether.
+</para> </listitem>
+<listitem><para>
+You may specify an explicit <emphasis>pattern signature</emphasis>, as
+we did for <literal>ExNumPat</literal> above, to specify the type of a pattern,
+just as you can for a function. As usual, the type signature can be less polymorphic
+than the inferred type. For example
<programlisting>
- f :: (Num t, Eq t) => T t -> String
- f (ExNumPat x) = show x
+ -- Inferred type would be 'a -> [a]'
+ pattern SinglePair :: (a, a) -> [(a, a)]
+ pattern SinglePair x = [x]
</programlisting>
+</para> </listitem>
-<para>
- For bidirectional pattern synonyms, uses as expressions have the type
-</para>
+<listitem><para>
+The GHCi <literal>:info</literal> command shows pattern types in this format.
+</para> </listitem>
+
+<listitem><para>
+For a bidirectional pattern synonym, a use of the pattern synonym as an expression has the type
<programlisting>
(CProv, CReq) => t1 -> t2 -> ... -> tN -> t
</programlisting>
-
-<para>
- So in the previous example, <literal>ExNumPat</literal>,
- when used in an expression, has type
-</para>
+ So in the previous example, when used in an expression, <literal>ExNumPat</literal> has type
<programlisting>
ExNumPat :: (Show b, Num a, Eq a) => b -> T t
</programlisting>
+Notice that this is a tiny bit more restrictive than the expression <literal>MkT 42 x</literal>
+which would not require <literal>(Eq a)</literal>.
+</para> </listitem>
-</listitem>
+<listitem><para>
+Consider these two pattern synonyms:
+<programlisting>
+data S a where
+ S1 :: Bool -> S Bool
-<listitem> Matching:
+pattern P1 b = Just b -- P1 :: Bool -> Maybe Bool
+pattern P2 b = S1 b -- P2 :: (b~Bool) => Bool -> S b
+
+f :: Maybe a -> String
+f (P1 x) = "no no no" -- Type-incorrect
+
+g :: S a -> String
+g (P2 b) = "yes yes yes" -- Fine
+</programlisting>
+Pattern <literal>P1</literal> can only match against a value of type <literal>Maybe Bool</literal>,
+so function <literal>f</literal> is rejected because the type signature is <literal>Maybe a</literal>.
+(To see this, imagine expanding the pattern synonym.)
+</para>
+<para>
+On the other hand, function <literal>g</literal> works fine, becuase matching against <literal>P2</literal>
+(which wraps the GADT <literal>S</literal>) provides the local equality <literal>(a~Bool)</literal>.
+If you were to give an explicit pattern signature <literal>P2 :: Bool -> S Bool</literal>, then <literal>P2</literal>
+would become less polymorphic, and would behave exactly like <literal>P1</literal> so that <literal>g</literal>
+would then be rejected.
+</para>
+<para>
+In short, if you want GADT-like behaviour for pattern synonyms,
+then (unlike unlike concrete data constructors like <literal>S1</literal>)
+you must write its type with explicit provided equalities.
+For a concrete data construoctr like <literal>S1</literal> you can write
+its type signature as eigher <literal>S1 :: Bool -> S Bool</literal> or
+<literal>S1 :: (b~Bool) => Bool -> S b</literal>; the two are equivalent.
+Not so for pattern synonyms: the two forms are different, in order to
+distinguish the two cases above. (See <ulink url="https://ghc.haskell.org/trac/ghc/ticket/9953">Trac #9953</ulink> for
+discussion of this choice.)
+</para></listitem>
+</itemizedlist>
+</para>
+</sect3>
+
+<sect3><title>Matching of pattern synonyms</title>
<para>
A pattern synonym occurrence in a pattern is evaluated by first
@@ -1119,14 +1212,12 @@
f _ = False
f' [x, y] | True <- x, True <- y = True
-f' _ = False
+f' _ = False
</programlisting>
<para>
Note that the strictness of <literal>f</literal> differs from that
of <literal>g</literal> defined below:
-</para>
-
<programlisting>
g [True, True] = True
g _ = False
@@ -1136,9 +1227,8 @@
*Main> g (False:undefined)
False
</programlisting>
-</listitem>
-</itemizedlist>
</para>
+</sect3>
</sect2>
@@ -1883,7 +1973,8 @@
functions <literal>(>>=)</literal>,
<literal>(>>)</literal>, and <literal>fail</literal>,
are in scope (not the Prelude
- versions). List comprehensions, mdo (<xref linkend="recursive-do-notation"/>), and parallel array
+ versions). List comprehensions, <literal>mdo</literal>
+ (<xref linkend="recursive-do-notation"/>), and parallel array
comprehensions, are unaffected. </para></listitem>
<listitem>
@@ -2284,8 +2375,8 @@
More details:
<itemizedlist>
<listitem><para>
-Wildcards can be mixed with other patterns, including puns
-(<xref linkend="record-puns"/>); for example, in a pattern <literal>C {a
+Record wildcards in patterns can be mixed with other patterns, including puns
+(<xref linkend="record-puns"/>); for example, in a pattern <literal>(C {a
= 1, b, ..})</literal>. Additionally, record wildcards can be used
wherever record patterns occur, including in <literal>let</literal>
bindings and at the top-level. For example, the top-level binding
@@ -2297,7 +2388,7 @@
</para></listitem>
<listitem><para>
-Record wildcards can also be used in expressions, writing, for example,
+Record wildcards can also be used in an expression, when constructing a record. For example,
<programlisting>
let {a = 1; b = 2; c = 3; d = 4} in C {..}
</programlisting>
@@ -2311,7 +2402,15 @@
</para></listitem>
<listitem><para>
-The "<literal>..</literal>" expands to the missing
+Record wildcards may <emphasis>not</emphasis> be used in record <emphasis>updates</emphasis>. For example this
+is illegal:
+<programlisting>
+f r = r { x = 3, .. }
+</programlisting>
+</para></listitem>
+
+<listitem><para>
+For both pattern and expression wildcards, the "<literal>..</literal>" expands to the missing
<emphasis>in-scope</emphasis> record fields.
Specifically the expansion of "<literal>C {..}</literal>" includes
<literal>f</literal> if and only if:
@@ -2328,6 +2427,8 @@
apart from the binding of the record selector itself.
</para></listitem>
</itemizedlist>
+These rules restrict record wildcards to the situations in which the user
+could have written the expanded version.
For example
<programlisting>
module M where
@@ -2342,6 +2443,18 @@
is not in scope (apart from the binding of the
record selector <literal>c</literal>, of course).
</para></listitem>
+
+<listitem><para>
+Record wildcards cannot be used (a) in a record update construct, and (b) for data
+constructors that are not declared with record fields. For example:
+<programlisting>
+f x = x { v=True, .. } -- Illegal (a)
+
+data T = MkT Int Bool
+g = MkT { .. } -- Illegal (b)
+h (MkT { .. }) = True -- Illegal (b)
+</programlisting>
+</para></listitem>
</itemizedlist>
</para>
@@ -2391,7 +2504,36 @@
</sect2>
<sect2 id="package-imports">
- <title>Package-qualified imports</title>
+<title>Import and export extensions</title>
+
+<sect3>
+ <title>Hiding things the imported module doesn't export</title>
+
+<para>
+Technically in Haskell 2010 this is illegal:
+<programlisting>
+module A( f ) where
+ f = True
+
+module B where
+ import A hiding( g ) -- A does not export g
+ g = f
+</programlisting>
+The <literal>import A hiding( g )</literal> in module <literal>B</literal>
+is technically an error (<ulink url="http://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-1020005.3.1">Haskell Report, 5.3.1</ulink>)
+because <literal>A</literal> does not export <literal>g</literal>.
+However GHC allows it, in the interests of supporting backward compatibility; for example, a newer version of
+<literal>A</literal> might export <literal>g</literal>, and you want <literal>B</literal> to work
+in either case.
+</para>
+<para>
+The warning <literal>-fwarn-dodgy-imports</literal>, which is off by default but included with <literal>-W</literal>,
+warns if you hide something that the imported module does not export.
+</para>
+</sect3>
+
+<sect3>
+ <title id="package-qualified-imports">Package-qualified imports</title>
<para>With the <option>-XPackageImports</option> flag, GHC allows
import declarations to be qualified by the package name that the
@@ -2414,10 +2556,12 @@
added mainly so that we can build backwards-compatible versions of
packages when APIs change. It can lead to fragile dependencies in
the common case: modules occasionally move from one package to
- another, rendering any package-qualified imports broken.</para>
-</sect2>
+ another, rendering any package-qualified imports broken.
+ See also <xref linkend="package-thinning-and-renaming" /> for
+ an alternative way of disambiguating between module names.</para>
+</sect3>
-<sect2 id="safe-imports-ext">
+<sect3 id="safe-imports-ext">
<title>Safe imports</title>
<para>With the <option>-XSafe</option>, <option>-XTrustworthy</option>
@@ -2435,15 +2579,15 @@
safely imported. For a description of when a import is
considered safe see <xref linkend="safe-haskell"/></para>
-</sect2>
+</sect3>
-<sect2 id="explicit-namespaces">
+<sect3 id="explicit-namespaces">
<title>Explicit namespaces in import/export</title>
-<para> In an import or export list, such as
+<para> In an import or export list, such as
<programlisting>
module M( f, (++) ) where ...
- import N( f, (++) )
+ import N( f, (++) )
...
</programlisting>
the entities <literal>f</literal> and <literal>(++)</literal> are <emphasis>values</emphasis>.
@@ -2452,12 +2596,12 @@
case, how would you export or import it?
</para>
<para>
-The <option>-XExplicitNamespaces</option> extension allows you to prefix the name of
-a type constructor in an import or export list with "<literal>type</literal>" to
+The <option>-XExplicitNamespaces</option> extension allows you to prefix the name of
+a type constructor in an import or export list with "<literal>type</literal>" to
disambiguate this case, thus:
<programlisting>
module M( f, type (++) ) where ...
- import N( f, type (++) )
+ import N( f, type (++) )
...
module N( f, type (++) ) where
data family a ++ b = L a | R b
@@ -2465,6 +2609,14 @@
The extension <option>-XExplicitNamespaces</option>
is implied by <option>-XTypeOperators</option> and (for some reason) by <option>-XTypeFamilies</option>.
</para>
+<para>
+In addition, with <option>-XPatternSynonyms</option> you can prefix the name of
+a data constructor in an import or export list with the keyword <literal>pattern</literal>,
+to allow the import or export of a data constructor without its parent type constructor
+(see <xref linkend="patsyn-impexp"/>).
+</para>
+</sect3>
+
</sect2>
<sect2 id="syntax-stolen">
@@ -2688,8 +2840,11 @@
to be written infix, very much like expressions. More specifically:
<itemizedlist>
<listitem><para>
- A type constructor or class can be an operator, beginning with a colon; e.g. <literal>:*:</literal>.
- The lexical syntax is the same as that for data constructors.
+ A type constructor or class can be any non-reserved operator.
+ Symbols used in types are always like capitalized identifiers; they
+ are never variables. Note that this is different from the lexical
+ syntax of data constructors, which are required to begin with a
+ <literal>:</literal>.
</para></listitem>
<listitem><para>
Data type and type-synonym declarations can be written infix, parenthesised
@@ -2753,11 +2908,11 @@
The language <option>-XTypeOperators</option> changes this behaviour:
<itemizedlist>
<listitem><para>
-Operator symbols become type <emphasis>constructors</emphasis> rather than
+Operator symbols become type <emphasis>constructors</emphasis> rather than
type <emphasis>variables</emphasis>.
</para></listitem>
<listitem><para>
-Operator symbols in types can be written infix, both in definitions and uses.
+Operator symbols in types can be written infix, both in definitions and uses.
for example:
<programlisting>
data a + b = Plus a b
@@ -2766,8 +2921,8 @@
</para></listitem>
<listitem><para>
There is now some potential ambiguity in import and export lists; for example
-if you write <literal>import M( (+) )</literal> do you mean the
-<emphasis>function</emphasis> <literal>(+)</literal> or the
+if you write <literal>import M( (+) )</literal> do you mean the
+<emphasis>function</emphasis> <literal>(+)</literal> or the
<emphasis>type constructor</emphasis> <literal>(+)</literal>?
The default is the former, but with <option>-XExplicitNamespaces</option> (which is implied
by <option>-XExplicitTypeOperators</option>) GHC allows you to specify the latter
@@ -3623,12 +3778,13 @@
Tim Sheard. There is a longer introduction
<ulink url="http://www.haskell.org/haskellwiki/GADT">on the wiki</ulink>,
and Ralf Hinze's
-<ulink url="http://www.informatik.uni-bonn.de/~ralf/publications/With.pdf">Fun with phantom types</ulink> also has a number of examples. Note that papers
+<ulink url="http://www.cs.ox.ac.uk/ralf.hinze/publications/With.pdf">Fun with phantom types</ulink> also has a number of examples. Note that papers
may use different notation to that implemented in GHC.
</para>
<para>
The rest of this section outlines the extensions to GHC that support GADTs. The extension is enabled with
-<option>-XGADTs</option>. The <option>-XGADTs</option> flag also sets <option>-XRelaxedPolyRec</option>.
+<option>-XGADTs</option>. The <option>-XGADTs</option> flag also sets <option>-XGADTSyntax</option>
+and <option>-XMonoLocalBinds</option>.
<itemizedlist>
<listitem><para>
A GADT can only be declared using GADT-style syntax (<xref linkend="gadt-style"/>);
@@ -3809,6 +3965,13 @@
because <literal>T</literal> is a GADT, but you <emphasis>can</emphasis> generate
the instance declaration using stand-alone deriving.
</para>
+<para>
+The down-side is that,
+if the boilerplate code fails to typecheck, you will get an error message about that
+code, which you did not write. Whereas, with a <literal>deriving</literal> clause
+the side-conditions are necessarily more conservative, but any error message
+may be more comprehensible.
+</para>
</listitem>
</itemizedlist></para>
@@ -3837,9 +4000,8 @@
</sect2>
-
-<sect2 id="deriving-typeable">
-<title>Deriving clause for extra classes (<literal>Typeable</literal>, <literal>Data</literal>, etc)</title>
+<sect2 id="deriving-extra">
+<title>Deriving instances of extra classes (<literal>Data</literal>, etc)</title>
<para>
Haskell 98 allows the programmer to add "<literal>deriving( Eq, Ord )</literal>" to a data type
@@ -3851,27 +4013,6 @@
<para>
GHC extends this list with several more classes that may be automatically derived:
<itemizedlist>
-<listitem><para> With <option>-XDeriveDataTypeable</option>, you can derive instances of the classes
-<literal>Typeable</literal>, and <literal>Data</literal>, defined in the library
-modules <literal>Data.Typeable</literal> and <literal>Data.Data</literal> respectively.
-</para>
-<para>Since GHC 7.8.1, <literal>Typeable</literal> is kind-polymorphic (see
-<xref linkend="kind-polymorphism"/>) and can be derived for any datatype and
-type class. Instances for datatypes can be derived by attaching a
-<literal>deriving Typeable</literal> clause to the datatype declaration, or by
-using standalone deriving (see <xref linkend="stand-alone-deriving"/>).
-Instances for type classes can only be derived using standalone deriving.
-For data families, <literal>Typeable</literal> should only be derived for the
-uninstantiated family type; each instance will then automatically have a
-<literal>Typeable</literal> instance too.
-See also <xref linkend="auto-derive-typeable"/>.
-</para>
-<para>
-Also since GHC 7.8.1, handwritten (ie. not derived) instances of
-<literal>Typeable</literal> are forbidden, and will result in an error.
-</para>
-</listitem>
-
<listitem><para> With <option>-XDeriveGeneric</option>, you can derive
instances of the classes <literal>Generic</literal> and
<literal>Generic1</literal>, defined in <literal>GHC.Generics</literal>.
@@ -3884,6 +4025,12 @@
defined in <literal>GHC.Base</literal>.
</para></listitem>
+<listitem><para> With <option>-XDeriveDataTypeable</option>, you can derive instances of
+the class <literal>Data</literal>,
+defined in <literal>Data.Data</literal>. See <xref linkend="deriving-typeable"/> for
+deriving <literal>Typeable</literal>.
+</para></listitem>
+
<listitem><para> With <option>-XDeriveFoldable</option>, you can derive instances of
the class <literal>Foldable</literal>,
defined in <literal>Data.Foldable</literal>.
@@ -3891,24 +4038,78 @@
<listitem><para> With <option>-XDeriveTraversable</option>, you can derive instances of
the class <literal>Traversable</literal>,
-defined in <literal>Data.Traversable</literal>.
+defined in <literal>Data.Traversable</literal>. Since the <literal>Traversable</literal>
+instance dictates the instances of <literal>Functor</literal> and
+<literal>Foldable</literal>, you'll probably want to derive them too, so
+<option>-XDeriveTraversable</option> implies
+<option>-XDeriveFunctor</option> and <option>-XDeriveFoldable</option>.
</para></listitem>
</itemizedlist>
+You can also use a standalone deriving declaration instead
+(see <xref linkend="stand-alone-deriving"/>).
+</para>
+<para>
In each case the appropriate class must be in scope before it
can be mentioned in the <literal>deriving</literal> clause.
</para>
</sect2>
-<sect2 id="auto-derive-typeable">
-<title>Automatically deriving <literal>Typeable</literal> instances</title>
+<sect2 id="deriving-typeable">
+<title>Deriving <literal>Typeable</literal> instances</title>
+
+<para>The class <literal>Typeable</literal> is very special:
+<itemizedlist>
+<listitem><para>
+<literal>Typeable</literal> is kind-polymorphic (see
+<xref linkend="kind-polymorphism"/>).
+</para></listitem>
+
+<listitem><para>
+GHC has a custom solver for discharging constraints that involve
+class <literal>Typeable</literal>, and handwritten instances are forbidden.
+This ensures that the programmer cannot subert the type system by
+writing bogus instances.
+</para></listitem>
+
+<listitem><para>
+Derived instances of <literal>Typeable</literal> are ignored,
+and may be reported as an error in a later version of the compiler.
+</para></listitem>
+
+<listitem><para>
+The rules for solving `Typeable` constraints are as follows:
+<itemizedlist>
+<listitem><para>A concrete type constructor applied to some types.
+<programlisting>
+instance (Typeable t1, .., Typeable t_n) =>
+ Typeable (T t1 .. t_n)
+</programlisting>
+This rule works for any concrete type constructor, including type
+constructors with polymorhic kinds. The only restriction is that
+if the type constructor has a polymorhic kind, then it has to be applied
+to all of its kinds parameters, and these kinds need to be concrete
+(i.e., they cannot mention kind variables).
+</para></listitem>
+
+<listitem><para>
+<programlisting>A type variable applied to some types.
+instance (Typeable f, Typeable t1, .., Typeable t_n) =>
+ Typeable (f t1 .. t_n)
+</programlisting>
+</para></listitem>
+
+<listitem><para>
+<programlisting>A concrete type literal.
+instance Typeable 0 -- Type natural literals
+instance Typeable "Hello" -- Type-level symbols
+</programlisting>
+</para></listitem>
+</itemizedlist>
+</para></listitem>
+
+
+</itemizedlist>
-<para>
-The flag <option>-XAutoDeriveTypeable</option> triggers the generation
-of derived <literal>Typeable</literal> instances for every datatype and type
-class declaration in the module it is used. It will also generate
-<literal>Typeable</literal> instances for any promoted data constructors
-(<xref linkend="promotion"/>). This flag implies
-<option>-XDeriveDataTypeable</option> (<xref linkend="deriving-typeable"/>).
</para>
</sect2>
@@ -4034,47 +4235,52 @@
<sect3> <title> A more precise specification </title>
<para>
-Derived instance declarations are constructed as follows. Consider the
-declaration (after expansion of any type synonyms)
+A derived instance is derived only for declarations of these forms (after expansion of any type synonyms)
<programlisting>
- newtype T v1...vn = T' (t vk+1...vn) deriving (c1...cm)
+ newtype T v1..vn = MkT (t vk+1..vn) deriving (C t1..tj)
+ newtype instance T s1..sk vk+1..vn = MkT (t vk+1..vn) deriving (C t1..tj)
</programlisting>
-
where
<itemizedlist>
<listitem><para>
- The <literal>ci</literal> are partial applications of
- classes of the form <literal>C t1'...tj'</literal>, where the arity of <literal>C</literal>
+<literal>v1..vn</literal> are type variables, and <literal>t</literal>,
+<literal>s1..sk</literal>, <literal>t1..tj</literal> are types.
+</para></listitem>
+<listitem><para>
+ The <literal>(C t1..tj)</literal> is a partial applications of the class <literal>C</literal>,
+ where the arity of <literal>C</literal>
is exactly <literal>j+1</literal>. That is, <literal>C</literal> lacks exactly one type argument.
</para></listitem>
<listitem><para>
- The <literal>k</literal> is chosen so that <literal>ci (T v1...vk)</literal> is well-kinded.
+ <literal>k</literal> is chosen so that <literal>C t1..tj (T v1...vk)</literal> is well-kinded.
+(Or, in the case of a <literal>data instance</literal>, so that <literal>C t1..tj (T s1..sk)</literal> is
+well kinded.)
</para></listitem>
<listitem><para>
The type <literal>t</literal> is an arbitrary type.
</para></listitem>
<listitem><para>
- The type variables <literal>vk+1...vn</literal> do not occur in <literal>t</literal>,
- nor in the <literal>ci</literal>, and
+ The type variables <literal>vk+1...vn</literal> do not occur in the types <literal>t</literal>,
+ <literal>s1..sk</literal>, or <literal>t1..tj</literal>.
</para></listitem>
<listitem><para>
- None of the <literal>ci</literal> is <literal>Read</literal>, <literal>Show</literal>,
+ <literal>C</literal> is not <literal>Read</literal>, <literal>Show</literal>,
<literal>Typeable</literal>, or <literal>Data</literal>. These classes
should not "look through" the type or its constructor. You can still
derive these classes for a newtype, but it happens in the usual way, not
via this new mechanism.
</para></listitem>
<listitem><para>
- It is safe to coerce each of the methods of <literal>ci</literal>. That is,
- the missing last argument to each of the <literal>ci</literal> is not used
- at a nominal role in any of the <literal>ci</literal>'s methods.
+ It is safe to coerce each of the methods of <literal>C</literal>. That is,
+ the missing last argument to <literal>C</literal> is not used
+ at a nominal role in any of the <literal>C</literal>'s methods.
(See <xref linkend="roles"/>.)</para></listitem>
</itemizedlist>
-Then, for each <literal>ci</literal>, the derived instance
+Then the derived instance is of form
declaration is:
<programlisting>
- instance ci t => ci (T v1...vk)
+ instance C t1..tj t => C t1..tj (T v1...vk)
</programlisting>
As an example which does <emphasis>not</emphasis> work, consider
<programlisting>
@@ -4116,6 +4322,25 @@
</para>
</sect3>
</sect2>
+
+<sect2 id="derive-any-class">
+<title>Deriving any other class</title>
+
+<para>
+With <option>-XDeriveAnyClass</option> you can derive any other class. The
+compiler will simply generate an empty instance. The instance context will be
+generated according to the same rules used when deriving <literal>Eq</literal>.
+This is mostly useful in classes whose <link linkend="minimal-pragma">minimal
+set</link> is empty, and especially when writing
+<link linkend="generic-programming">generic functions</link>.
+
+In case you try to derive some class on a newtype, and
+<option>-XGeneralizedNewtypeDeriving</option> is also on,
+<option>-XDeriveAnyClass</option> takes precedence.
+</para>
+
+</sect2>
+
</sect1>
@@ -4276,7 +4501,9 @@
<sect3 id="nullary-type-classes">
<title>Nullary type classes</title>
-Nullary (no parameter) type classes are enabled with <option>-XNullaryTypeClasses</option>.
+Nullary (no parameter) type classes are enabled with
+<option>-XMultiTypeClasses</option>; historically, they were enabled with the
+(now deprecated) <option>-XNullaryTypeClasses</option>.
Since there are no available parameters, there can be at most one instance
of a nullary class. A nullary type class might be used to document some assumption
in a type signature (such as reliance on the Riemann hypothesis) or add some
@@ -4887,6 +5114,11 @@
with <option>-fcontext-stack=</option><emphasis>N</emphasis>.
</para>
+<para>
+The <option>-XUndecidableInstances</option> flag is also used to lift some of the
+restricitions imposed on type family instances. See <xref linkend="type-family-decidability"/>.
+</para>
+
</sect3>
@@ -4897,40 +5129,128 @@
In general, as discussed in <xref linkend="instance-resolution"/>,
<emphasis>GHC requires that it be unambiguous which instance
declaration
-should be used to resolve a type-class constraint</emphasis>. This behaviour
-can be modified by two flags: <option>-XOverlappingInstances</option>
+should be used to resolve a type-class constraint</emphasis>.
+GHC also provides a way to to loosen
+the instance resolution, by
+allowing more than one instance to match, <emphasis>provided there is a most
+specific one</emphasis>. Moreover, it can be loosened further, by allowing more than one instance to match
+irespective of whether there is a most specific one.
+This section gives the details.
+</para>
+<para>
+To control the choice of instance, it is possible to specify the overlap behavior for individual
+instances with a pragma, written immediately after the
+<literal>instance</literal> keyword. The pragma may be one of:
+<literal>{-# OVERLAPPING #-}</literal>,
+<literal>{-# OVERLAPPABLE #-}</literal>,
+<literal>{-# OVERLAPS #-}</literal>,
+or <literal>{-# INCOHERENT #-}</literal>.
+</para>
+<para>
+The matching behaviour is also influenced by two module-level language extension flags: <option>-XOverlappingInstances</option>
<indexterm><primary>-XOverlappingInstances
</primary></indexterm>
and <option>-XIncoherentInstances</option>
<indexterm><primary>-XIncoherentInstances
-</primary></indexterm>, as this section discusses. Both these
-flags are dynamic flags, and can be set on a per-module basis, using
-an <literal>LANGUAGE</literal> pragma if desired (<xref linkend="language-pragma"/>).</para>
+</primary></indexterm>. These flags are now deprecated (since GHC 7.10) in favour of
+the fine-grained per-instance pragmas.
+</para>
+
<para>
-The <option>-XOverlappingInstances</option> flag instructs GHC to loosen
-the instance resolution described in <xref linkend="instance-resolution"/>, by
-allowing more than one instance to match, <emphasis>provided there is a most
-specific one</emphasis>. The <option>-XIncoherentInstances</option> flag
-further loosens the resolution, by allowing more than one instance to match,
-irespective of whether there is a most specific one.
+A more precise specification is as follows.
+The willingness to be overlapped or incoherent is a property of
+the <emphasis>instance declaration</emphasis> itself, controlled as follows:
+<itemizedlist>
+<listitem><para>An instance is <emphasis>incoherent</emphasis> if: it has an <literal>INCOHERENT</literal> pragma; or if the instance has no pragma and it appears in a module compiled with <literal>-XIncoherentInstances</literal>.
+</para></listitem>
+<listitem><para>An instance is <emphasis>overlappable</emphasis> if: it has an <literal>OVERLAPPABLE</literal> or <literal>OVERLAPS</literal> pragma; or if the instance has no pragma and it appears in a module compiled with <literal>-XOverlappingInstances</literal>; or if the instance is incoherent.
+</para></listitem>
+<listitem><para>An instance is <emphasis>overlapping</emphasis> if: it has an <literal>OVERLAPPING</literal> or <literal>OVERLAPS</literal> pragma; or if the instance has no pragma and it appears in a module compiled with <literal>-XOverlappingInstances</literal>; or if the instance is incoherent.
+</para></listitem>
+</itemizedlist>
</para>
<para>
-For example, consider
+Now suppose that, in some client module, we are searching for an instance of the
+<emphasis>target constraint</emphasis> <literal>(C ty1 .. tyn)</literal>.
+The search works like this.
+<itemizedlist>
+<listitem><para>
+Find all instances I that <emphasis>match</emphasis> the target constraint;
+that is, the target constraint is a substitution instance of I. These
+instance declarations are the <emphasis>candidates</emphasis>.
+</para></listitem>
+
+<listitem><para>
+Eliminate any candidate IX for which both of the following hold:
+
+<itemizedlist>
+ <listitem><para>There is another candidate IY that is strictly more specific;
+ that is, IY is a substitution instance of IX but not vice versa.
+ </para></listitem>
+ <listitem><para>
+ Either IX is <emphasis>overlappable</emphasis>, or IY is
+ <emphasis>overlapping</emphasis>. (This "either/or" design, rather than a "both/and" design,
+ allow a client to deliberately override an instance from a library, without requiring a change to the library.)
+ </para></listitem>
+ </itemizedlist>
+</para>
+</listitem>
+
+<listitem><para>
+If exactly one non-incoherent candidate remains, select it. If all
+remaining candidates are incoherent, select an arbitary
+one. Otherwise the search fails (i.e. when more than one surviving candidate is not incoherent).
+</para></listitem>
+
+<listitem><para>
+If the selected candidate (from the previous step) is incoherent, the search succeeds, returning that candidate.
+</para></listitem>
+
+<listitem><para>
+If not, find all instances that <emphasis>unify</emphasis> with the target
+constraint, but do not <emphasis>match</emphasis> it.
+Such non-candidate instances might match when the target constraint is further
+instantiated. If all of them are incoherent, the search succeeds, returning the selected candidate;
+if not, the search fails.
+</para></listitem>
+
+</itemizedlist>
+Notice that these rules are not influenced by flag settings in the client module, where
+the instances are <emphasis>used</emphasis>.
+These rules make it possible for a library author to design a library that relies on
+overlapping instances without the client having to know.
+</para>
+<para>
+Errors are reported <emphasis>lazily</emphasis> (when attempting to solve a constraint), rather than <emphasis>eagerly</emphasis>