glasgow_exts.xml 378 KB
Newer Older
1
<?xml version="1.0" encoding="iso-8859-1"?>
2
3
4
<para>
<indexterm><primary>language, GHC</primary></indexterm>
<indexterm><primary>extensions, GHC</primary></indexterm>
rrt's avatar
rrt committed
5
As with all known Haskell systems, GHC implements some extensions to
Ian Lynagh's avatar
Ian Lynagh committed
6
7
8
the language.  They can all be enabled or disabled by commandline flags
or language pragmas. By default GHC understands the most recent Haskell
version it supports, plus a handful of extensions.
9
</para>
rrt's avatar
rrt committed
10

11
<para>
12
13
14
15
16
17
18
19
Some of the Glasgow extensions serve to give you access to the
underlying facilities with which we implement Haskell.  Thus, you can
get at the Raw Iron, if you are willing to write some non-portable
code at a more primitive level.  You need not be &ldquo;stuck&rdquo;
on performance because of the implementation costs of Haskell's
&ldquo;high-level&rdquo; features&mdash;you can always code
&ldquo;under&rdquo; them.  In an extreme case, you can write all your
time-critical code in C, and then just glue it together with Haskell!
20
</para>
rrt's avatar
rrt committed
21

22
<para>
rrt's avatar
rrt committed
23
Before you get too carried away working at the lowest level (e.g.,
24
sloshing <literal>MutableByteArray&num;</literal>s around your
25
program), you may wish to check if there are libraries that provide a
26
&ldquo;Haskellised veneer&rdquo; over the features you want.  The
27
28
separate <ulink url="../libraries/index.html">libraries
documentation</ulink> describes all the libraries that come with GHC.
29
</para>
rrt's avatar
rrt committed
30

31
<!-- LANGUAGE OPTIONS -->
32
33
  <sect1 id="options-language">
    <title>Language options</title>
34

35
36
37
38
39
40
    <indexterm><primary>language</primary><secondary>option</secondary>
    </indexterm>
    <indexterm><primary>options</primary><secondary>language</secondary>
    </indexterm>
    <indexterm><primary>extensions</primary><secondary>options controlling</secondary>
    </indexterm>
41

42
    <para>The language option flags control what variation of the language are
Ian Lynagh's avatar
Ian Lynagh committed
43
    permitted.</para>
44

45
46
    <para>Language options can be controlled in two ways:
    <itemizedlist>
47
48
      <listitem><para>Every language option can switched on by a command-line flag "<option>-X...</option>"
        (e.g. <option>-XTemplateHaskell</option>), and switched off by the flag "<option>-XNo...</option>";
49
50
51
52
53
54
        (e.g. <option>-XNoTemplateHaskell</option>).</para></listitem>
      <listitem><para>
          Language options recognised by Cabal can also be enabled using the <literal>LANGUAGE</literal> pragma,
          thus <literal>{-# LANGUAGE TemplateHaskell #-}</literal> (see <xref linkend="language-pragma"/>). </para>
          </listitem>
      </itemizedlist></para>
55

56
    <para>The flag <option>-fglasgow-exts</option>
57
          <indexterm><primary><option>-fglasgow-exts</option></primary></indexterm>
58
	  is equivalent to enabling the following extensions:
59
          &what_glasgow_exts_does;
60
	    Enabling these options is the <emphasis>only</emphasis>
Simon Marlow's avatar
Simon Marlow committed
61
	    effect of <option>-fglasgow-exts</option>.
62
          We are trying to move away from this portmanteau flag,
63
	  and towards enabling features individually.</para>
64

65
  </sect1>
66

67
<!-- UNBOXED TYPES AND PRIMITIVE OPERATIONS -->
68
69
70
<sect1 id="primitives">
  <title>Unboxed types and primitive operations</title>

71
72
<para>GHC is built on a raft of primitive data types and operations;
"primitive" in the sense that they cannot be defined in Haskell itself.
73
While you really can use this stuff to write fast code,
74
75
76
77
78
we generally find it a lot less painful, and more satisfying in the
long run, to use higher-level language features and libraries.  With
any luck, the code you write will be optimised to the efficient
unboxed version in any case.  And if it isn't, we'd like to know
about it.</para>
79

80
81
<para>All these primitive data types and operations are exported by the
library <literal>GHC.Prim</literal>, for which there is
82
<ulink url="&libraryGhcPrimLocation;/GHC-Prim.html">detailed online documentation</ulink>.
83
84
(This documentation is generated from the file <filename>compiler/prelude/primops.txt.pp</filename>.)
</para>
85

86
87
88
89
90
91
92
<para>
If you want to mention any of the primitive data types or operations in your
program, you must first import <literal>GHC.Prim</literal> to bring them
into scope.  Many of them have names ending in "&num;", and to mention such
names you need the <option>-XMagicHash</option> extension (<xref linkend="magic-hash"/>).
</para>

93
<para>The primops make extensive use of <link linkend="glasgow-unboxed">unboxed types</link>
94
95
and <link linkend="unboxed-tuples">unboxed tuples</link>, which
we briefly summarise here. </para>
96

97
<sect2 id="glasgow-unboxed">
98
<title>Unboxed types</title>
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

<para>
<indexterm><primary>Unboxed types (Glasgow extension)</primary></indexterm>
</para>

<para>Most types in GHC are <firstterm>boxed</firstterm>, which means
that values of that type are represented by a pointer to a heap
object.  The representation of a Haskell <literal>Int</literal>, for
example, is a two-word heap object.  An <firstterm>unboxed</firstterm>
type, however, is represented by the value itself, no pointers or heap
allocation are involved.
</para>

<para>
Unboxed types correspond to the &ldquo;raw machine&rdquo; types you
would use in C: <literal>Int&num;</literal> (long int),
<literal>Double&num;</literal> (double), <literal>Addr&num;</literal>
(void *), etc.  The <emphasis>primitive operations</emphasis>
(PrimOps) on these types are what you might expect; e.g.,
<literal>(+&num;)</literal> is addition on
<literal>Int&num;</literal>s, and is the machine-addition that we all
know and love&mdash;usually one instruction.
</para>

<para>
Primitive (unboxed) types cannot be defined in Haskell, and are
therefore built into the language and compiler.  Primitive types are
always unlifted; that is, a value of a primitive type cannot be
127
bottom.  We use the convention (but it is only a convention)
128
129
130
131
that primitive types, values, and
operations have a <literal>&num;</literal> suffix (see <xref linkend="magic-hash"/>).
For some primitive types we have special syntax for literals, also
described in the <link linkend="magic-hash">same section</link>.
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
</para>

<para>
Primitive values are often represented by a simple bit-pattern, such
as <literal>Int&num;</literal>, <literal>Float&num;</literal>,
<literal>Double&num;</literal>.  But this is not necessarily the case:
a primitive value might be represented by a pointer to a
heap-allocated object.  Examples include
<literal>Array&num;</literal>, the type of primitive arrays.  A
primitive array is heap-allocated because it is too big a value to fit
in a register, and would be too expensive to copy around; in a sense,
it is accidental that it is represented by a pointer.  If a pointer
represents a primitive value, then it really does point to that value:
no unevaluated thunks, no indirections&hellip;nothing can be at the
other end of the pointer than the primitive value.
147
148
149
A numerically-intensive program using unboxed types can
go a <emphasis>lot</emphasis> faster than its &ldquo;standard&rdquo;
counterpart&mdash;we saw a threefold speedup on one example.
150
151
152
</para>

<para>
153
154
155
156
There are some restrictions on the use of primitive types:
<itemizedlist>
<listitem><para>The main restriction
is that you can't pass a primitive value to a polymorphic
157
158
159
160
161
162
163
164
165
166
167
function or store one in a polymorphic data type.  This rules out
things like <literal>[Int&num;]</literal> (i.e. lists of primitive
integers).  The reason for this restriction is that polymorphic
arguments and constructor fields are assumed to be pointers: if an
unboxed integer is stored in one of these, the garbage collector would
attempt to follow it, leading to unpredictable space leaks.  Or a
<function>seq</function> operation on the polymorphic component may
attempt to dereference the pointer, with disastrous results.  Even
worse, the unboxed value might be larger than a pointer
(<literal>Double&num;</literal> for instance).
</para>
168
</listitem>
169
170
171
172
173
174
175
<listitem><para> You cannot define a newtype whose representation type
(the argument type of the data constructor) is an unboxed type.  Thus,
this is illegal:
<programlisting>
  newtype A = MkA Int#
</programlisting>
</para></listitem>
176
177
178
179
180
181
182
<listitem><para> You cannot bind a variable with an unboxed type
in a <emphasis>top-level</emphasis> binding.
</para></listitem>
<listitem><para> You cannot bind a variable with an unboxed type
in a <emphasis>recursive</emphasis> binding.
</para></listitem>
<listitem><para> You may bind unboxed variables in a (non-recursive,
183
184
non-top-level) pattern binding, but you must make any such pattern-match
strict.  For example, rather than:
185
186
<programlisting>
  data Foo = Foo Int Int#
187

188
189
  f x = let (Foo a b, w) = ..rhs.. in ..body..
</programlisting>
190
you must write:
191
192
193
<programlisting>
  data Foo = Foo Int Int#

194
  f x = let !(Foo a b, w) = ..rhs.. in ..body..
195
</programlisting>
196
since <literal>b</literal> has type <literal>Int#</literal>.
197
198
199
</para>
</listitem>
</itemizedlist>
200
201
202
203
204
</para>

</sect2>

<sect2 id="unboxed-tuples">
205
<title>Unboxed tuples</title>
206
207

<para>
208
209
Unboxed tuples aren't really exported by <literal>GHC.Exts</literal>;
they are a syntactic extension enabled by the language flag <option>-XUnboxedTuples</option>.  An
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
unboxed tuple looks like this:
</para>

<para>

<programlisting>
(# e_1, ..., e_n #)
</programlisting>

</para>

<para>
where <literal>e&lowbar;1..e&lowbar;n</literal> are expressions of any
type (primitive or non-primitive).  The type of an unboxed tuple looks
the same.
</para>

227
228
229
230
231
232
233
234
<para>
Note that when unboxed tuples are enabled,
<literal>(#</literal> is a single lexeme, so for example when using
operators like <literal>#</literal> and <literal>#-</literal> you need
to write <literal>( # )</literal> and <literal>( #- )</literal> rather than
<literal>(#)</literal> and <literal>(#-)</literal>.
</para>

235
236
237
238
239
240
<para>
Unboxed tuples are used for functions that need to return multiple
values, but they avoid the heap allocation normally associated with
using fully-fledged tuples.  When an unboxed tuple is returned, the
components are put directly into registers or on the stack; the
unboxed tuple itself does not have a composite representation.  Many
241
of the primitive operations listed in <literal>primops.txt.pp</literal> return unboxed
242
tuples.
243
244
In particular, the <literal>IO</literal> and <literal>ST</literal> monads use unboxed
tuples to avoid unnecessary allocation during sequences of operations.
245
246
247
</para>

<para>
248
There are some restrictions on the use of unboxed tuples:
249
250
<itemizedlist>

251
<listitem>
252
<para>
253
Values of unboxed tuple types are subject to the same restrictions as
254
255
256
257
258
other unboxed types; i.e. they may not be stored in polymorphic data
structures or passed to polymorphic functions.
</para>
</listitem>

259
260
<listitem>
<para>
261
262
263
264
265
266
267
268
269
270
271
The typical use of unboxed tuples is simply to return multiple values,
binding those multiple results with a <literal>case</literal> expression, thus:
<programlisting>
  f x y = (# x+1, y-1 #)
  g x = case f x x of { (# a, b #) -&#62; a + b }
</programlisting>
You can have an unboxed tuple in a pattern binding, thus
<programlisting>
  f x = let (# p,q #) = h x in ..body..
</programlisting>
If the types of <literal>p</literal> and <literal>q</literal> are not unboxed,
272
the resulting binding is lazy like any other Haskell pattern binding.  The
273
274
275
276
277
278
279
280
above example desugars like this:
<programlisting>
  f x = let t = case h x o f{ (# p,q #) -> (p,q)
            p = fst t
            q = snd t
        in ..body..
</programlisting>
Indeed, the bindings can even be recursive.
281
282
283
284
</para>
</listitem>
</itemizedlist>

285
286
287
288
289
</para>

</sect2>
</sect1>

rrt's avatar
rrt committed
290

291
292
293
294
<!-- ====================== SYNTACTIC EXTENSIONS =======================  -->

<sect1 id="syntax-extns">
<title>Syntactic extensions</title>
295

Simon Marlow's avatar
Simon Marlow committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
    <sect2 id="unicode-syntax">
      <title>Unicode syntax</title>
      <para>The language
      extension <option>-XUnicodeSyntax</option><indexterm><primary><option>-XUnicodeSyntax</option></primary></indexterm>
      enables Unicode characters to be used to stand for certain ASCII
      character sequences.  The following alternatives are provided:</para>

      <informaltable>
	<tgroup cols="2" align="left" colsep="1" rowsep="1">
	  <thead>
	    <row>
	      <entry>ASCII</entry>
              <entry>Unicode alternative</entry>
	      <entry>Code point</entry>
	      <entry>Name</entry>
	    </row>
	  </thead>
313
314
315
316
317
318
319
320
321

<!--
               to find the DocBook entities for these characters, find
               the Unicode code point (e.g. 0x2237), and grep for it in
               /usr/share/sgml/docbook/xml-dtd-*/ent/* (or equivalent on
               your system.  Some of these Unicode code points don't have
               equivalent DocBook entities.
            -->

Simon Marlow's avatar
Simon Marlow committed
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
	  <tbody>
	    <row>
	      <entry><literal>::</literal></entry>
	      <entry>::</entry> <!-- no special char, apparently -->
              <entry>0x2237</entry>
	      <entry>PROPORTION</entry>
	    </row>
          </tbody>
	  <tbody>
	    <row>
	      <entry><literal>=&gt;</literal></entry>
	      <entry>&rArr;</entry>
	      <entry>0x21D2</entry>
              <entry>RIGHTWARDS DOUBLE ARROW</entry>
	    </row>
          </tbody>
	  <tbody>
	    <row>
	      <entry><literal>forall</literal></entry>
	      <entry>&forall;</entry>
	      <entry>0x2200</entry>
              <entry>FOR ALL</entry>
	    </row>
          </tbody>
	  <tbody>
	    <row>
	      <entry><literal>-&gt;</literal></entry>
	      <entry>&rarr;</entry>
	      <entry>0x2192</entry>
              <entry>RIGHTWARDS ARROW</entry>
	    </row>
          </tbody>
	  <tbody>
	    <row>
	      <entry><literal>&lt;-</literal></entry>
	      <entry>&larr;</entry>
	      <entry>0x2190</entry>
              <entry>LEFTWARDS ARROW</entry>
	    </row>
          </tbody>
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

	  <tbody>
	    <row>
	      <entry>-&lt;</entry>
	      <entry>&larrtl;</entry>
	      <entry>0x2919</entry>
	      <entry>LEFTWARDS ARROW-TAIL</entry>
	    </row>
          </tbody>

	  <tbody>
	    <row>
	      <entry>&gt;-</entry>
	      <entry>&rarrtl;</entry>
	      <entry>0x291A</entry>
	      <entry>RIGHTWARDS ARROW-TAIL</entry>
	    </row>
          </tbody>

	  <tbody>
	    <row>
	      <entry>-&lt;&lt;</entry>
	      <entry></entry>
	      <entry>0x291B</entry>
	      <entry>LEFTWARDS DOUBLE ARROW-TAIL</entry>
	    </row>
          </tbody>

	  <tbody>
	    <row>
	      <entry>&gt;&gt;-</entry>
	      <entry></entry>
	      <entry>0x291C</entry>
	      <entry>RIGHTWARDS DOUBLE ARROW-TAIL</entry>
	    </row>
          </tbody>

	  <tbody>
	    <row>
	      <entry>*</entry>
	      <entry>&starf;</entry>
	      <entry>0x2605</entry>
	      <entry>BLACK STAR</entry>
	    </row>
          </tbody>

Simon Marlow's avatar
Simon Marlow committed
408
409
410
411
        </tgroup>
      </informaltable>
    </sect2>

412
413
414
415
416
417
    <sect2 id="magic-hash">
      <title>The magic hash</title>
      <para>The language extension <option>-XMagicHash</option> allows "&num;" as a
	postfix modifier to identifiers.  Thus, "x&num;" is a valid variable, and "T&num;" is
	a valid type constructor or data constructor.</para>

418
419
      <para>The hash sign does not change semantics at all.  We tend to use variable
	names ending in "&num;" for unboxed values or types (e.g. <literal>Int&num;</literal>),
420
        but there is no requirement to do so; they are just plain ordinary variables.
421
	Nor does the <option>-XMagicHash</option> extension bring anything into scope.
422
423
	For example, to bring <literal>Int&num;</literal> into scope you must
	import <literal>GHC.Prim</literal> (see <xref linkend="primitives"/>);
424
425
426
427
	the <option>-XMagicHash</option> extension
	then allows you to <emphasis>refer</emphasis> to the <literal>Int&num;</literal>
	that is now in scope.</para>
      <para> The <option>-XMagicHash</option> also enables some new forms of literals (see <xref linkend="glasgow-unboxed"/>):
428
	<itemizedlist>
429
430
431
	  <listitem><para> <literal>'x'&num;</literal> has type <literal>Char&num;</literal></para> </listitem>
	  <listitem><para> <literal>&quot;foo&quot;&num;</literal> has type <literal>Addr&num;</literal></para> </listitem>
	  <listitem><para> <literal>3&num;</literal> has type <literal>Int&num;</literal>. In general,
Ian Lynagh's avatar
Ian Lynagh committed
432
	  any Haskell integer lexeme followed by a <literal>&num;</literal> is an <literal>Int&num;</literal> literal, e.g.
433
434
            <literal>-0x3A&num;</literal> as well as <literal>32&num;</literal></para>.</listitem>
	  <listitem><para> <literal>3&num;&num;</literal> has type <literal>Word&num;</literal>. In general,
Ian Lynagh's avatar
Ian Lynagh committed
435
	  any non-negative Haskell integer lexeme followed by <literal>&num;&num;</literal>
436
437
438
439
440
441
442
	      is a <literal>Word&num;</literal>. </para> </listitem>
	  <listitem><para> <literal>3.2&num;</literal> has type <literal>Float&num;</literal>.</para> </listitem>
	  <listitem><para> <literal>3.2&num;&num;</literal> has type <literal>Double&num;</literal></para> </listitem>
	  </itemizedlist>
      </para>
   </sect2>

443
444
    <!-- ====================== HIERARCHICAL MODULES =======================  -->

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
    <sect2 id="hierarchical-modules">
      <title>Hierarchical Modules</title>

      <para>GHC supports a small extension to the syntax of module
      names: a module name is allowed to contain a dot
      <literal>&lsquo;.&rsquo;</literal>.  This is also known as the
      &ldquo;hierarchical module namespace&rdquo; extension, because
      it extends the normally flat Haskell module namespace into a
      more flexible hierarchy of modules.</para>

      <para>This extension has very little impact on the language
      itself; modules names are <emphasis>always</emphasis> fully
      qualified, so you can just think of the fully qualified module
      name as <quote>the module name</quote>.  In particular, this
      means that the full module name must be given after the
      <literal>module</literal> keyword at the beginning of the
      module; for example, the module <literal>A.B.C</literal> must
      begin</para>

<programlisting>module A.B.C</programlisting>


      <para>It is a common strategy to use the <literal>as</literal>
      keyword to save some typing when using qualified names with
      hierarchical modules.  For example:</para>

<programlisting>
import qualified Control.Monad.ST.Strict as ST
</programlisting>

476
477
      <para>For details on how GHC searches for source and interface
      files in the presence of hierarchical modules, see <xref
478
      linkend="search-path"/>.</para>
479
480

      <para>GHC comes with a large collection of libraries arranged
481
482
483
484
485
      hierarchically; see the accompanying <ulink
      url="../libraries/index.html">library
      documentation</ulink>.  More libraries to install are available
      from <ulink
      url="http://hackage.haskell.org/packages/hackage.html">HackageDB</ulink>.</para>
486
487
488
489
490
491
492
493
494
    </sect2>

    <!-- ====================== PATTERN GUARDS =======================  -->

<sect2 id="pattern-guards">
<title>Pattern guards</title>

<para>
<indexterm><primary>Pattern guards (Glasgow extension)</primary></indexterm>
495
The discussion that follows is an abbreviated version of Simon Peyton Jones's original <ulink url="http://research.microsoft.com/~simonpj/Haskell/guards.html">proposal</ulink>. (Note that the proposal was written before pattern guards were implemented, so refers to them as unimplemented.)
496
497
498
499
500
501
502
503
504
505
506
</para>

<para>
Suppose we have an abstract data type of finite maps, with a
lookup operation:

<programlisting>
lookup :: FiniteMap -> Int -> Maybe Int
</programlisting>

The lookup returns <function>Nothing</function> if the supplied key is not in the domain of the mapping, and <function>(Just v)</function> otherwise,
507
where <varname>v</varname> is the value that the key maps to.  Now consider the following definition:
508
509
510
</para>

<programlisting>
511
clunky env var1 var2 | ok1 &amp;&amp; ok2 = val1 + val2
512
513
514
515
516
517
518
519
520
521
522
| otherwise  = var1 + var2
where
  m1 = lookup env var1
  m2 = lookup env var2
  ok1 = maybeToBool m1
  ok2 = maybeToBool m2
  val1 = expectJust m1
  val2 = expectJust m2
</programlisting>

<para>
523
The auxiliary functions are
524
525
526
527
528
529
530
531
532
533
534
535
536
</para>

<programlisting>
maybeToBool :: Maybe a -&gt; Bool
maybeToBool (Just x) = True
maybeToBool Nothing  = False

expectJust :: Maybe a -&gt; a
expectJust (Just x) = x
expectJust Nothing  = error "Unexpected Nothing"
</programlisting>

<para>
537
What is <function>clunky</function> doing? The guard <literal>ok1 &amp;&amp;
538
539
540
541
ok2</literal> checks that both lookups succeed, using
<function>maybeToBool</function> to convert the <function>Maybe</function>
types to booleans. The (lazily evaluated) <function>expectJust</function>
calls extract the values from the results of the lookups, and binds the
542
returned values to <varname>val1</varname> and <varname>val2</varname>
543
544
545
546
547
548
549
550
551
552
553
respectively.  If either lookup fails, then clunky takes the
<literal>otherwise</literal> case and returns the sum of its arguments.
</para>

<para>
This is certainly legal Haskell, but it is a tremendously verbose and
un-obvious way to achieve the desired effect.  Arguably, a more direct way
to write clunky would be to use case expressions:
</para>

<programlisting>
554
clunky env var1 var2 = case lookup env var1 of
555
556
557
558
559
  Nothing -&gt; fail
  Just val1 -&gt; case lookup env var2 of
    Nothing -&gt; fail
    Just val2 -&gt; val1 + val2
where
Simon Marlow's avatar
Simon Marlow committed
560
  fail = var1 + var2
561
562
563
564
565
566
567
</programlisting>

<para>
This is a bit shorter, but hardly better.  Of course, we can rewrite any set
of pattern-matching, guarded equations as case expressions; that is
precisely what the compiler does when compiling equations! The reason that
Haskell provides guarded equations is because they allow us to write down
568
the cases we want to consider, one at a time, independently of each other.
569
570
This structure is hidden in the case version.  Two of the right-hand sides
are really the same (<function>fail</function>), and the whole expression
571
tends to become more and more indented.
572
573
574
575
576
577
578
</para>

<para>
Here is how I would write clunky:
</para>

<programlisting>
579
clunky env var1 var2
580
581
582
583
584
585
586
  | Just val1 &lt;- lookup env var1
  , Just val2 &lt;- lookup env var2
  = val1 + val2
...other equations for clunky...
</programlisting>

<para>
587
The semantics should be clear enough.  The qualifiers are matched in order.
588
For a <literal>&lt;-</literal> qualifier, which I call a pattern guard, the
589
right hand side is evaluated and matched against the pattern on the left.
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
If the match fails then the whole guard fails and the next equation is
tried.  If it succeeds, then the appropriate binding takes place, and the
next qualifier is matched, in the augmented environment.  Unlike list
comprehensions, however, the type of the expression to the right of the
<literal>&lt;-</literal> is the same as the type of the pattern to its
left.  The bindings introduced by pattern guards scope over all the
remaining guard qualifiers, and over the right hand side of the equation.
</para>

<para>
Just as with list comprehensions, boolean expressions can be freely mixed
with among the pattern guards.  For example:
</para>

<programlisting>
605
f x | [y] &lt;- x
606
    , y > 3
607
    , Just z &lt;- h y
608
609
610
611
612
613
614
    = ...
</programlisting>

<para>
Haskell's current guards therefore emerge as a special case, in which the
qualifier list has just one element, a boolean expression.
</para>
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
</sect2>

    <!-- ===================== View patterns ===================  -->

<sect2 id="view-patterns">
<title>View patterns
</title>

<para>
View patterns are enabled by the flag <literal>-XViewPatterns</literal>.
More information and examples of view patterns can be found on the
<ulink url="http://hackage.haskell.org/trac/ghc/wiki/ViewPatterns">Wiki
page</ulink>.
</para>

<para>
View patterns are somewhat like pattern guards that can be nested inside
of other patterns.  They are a convenient way of pattern-matching
against values of abstract types. For example, in a programming language
implementation, we might represent the syntax of the types of the
language as follows:

<programlisting>
type Typ
639

640
641
642
data TypView = Unit
             | Arrow Typ Typ

Gabor Greif's avatar
Gabor Greif committed
643
view :: Typ -> TypView
644
645
646
647
648

-- additional operations for constructing Typ's ...
</programlisting>

The representation of Typ is held abstract, permitting implementations
SamB's avatar
SamB committed
649
to use a fancy representation (e.g., hash-consing to manage sharing).
650

651
Without view patterns, using this signature a little inconvenient:
652
653
654
655
656
657
658
659
660
661
662
663
664
665
<programlisting>
size :: Typ -> Integer
size t = case view t of
  Unit -> 1
  Arrow t1 t2 -> size t1 + size t2
</programlisting>

It is necessary to iterate the case, rather than using an equational
function definition. And the situation is even worse when the matching
against <literal>t</literal> is buried deep inside another pattern.
</para>

<para>
View patterns permit calling the view function inside the pattern and
666
matching against the result:
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
<programlisting>
size (view -> Unit) = 1
size (view -> Arrow t1 t2) = size t1 + size t2
</programlisting>

That is, we add a new form of pattern, written
<replaceable>expression</replaceable> <literal>-></literal>
<replaceable>pattern</replaceable> that means "apply the expression to
whatever we're trying to match against, and then match the result of
that application against the pattern". The expression can be any Haskell
expression of function type, and view patterns can be used wherever
patterns are used.
</para>

<para>
The semantics of a pattern <literal>(</literal>
<replaceable>exp</replaceable> <literal>-></literal>
<replaceable>pat</replaceable> <literal>)</literal> are as follows:

<itemizedlist>

<listitem> Scoping:

<para>The variables bound by the view pattern are the variables bound by
<replaceable>pat</replaceable>.
</para>

<para>
Any variables in <replaceable>exp</replaceable> are bound occurrences,
but variables bound "to the left" in a pattern are in scope.  This
feature permits, for example, one argument to a function to be used in
the view of another argument.  For example, the function
<literal>clunky</literal> from <xref linkend="pattern-guards" /> can be
written using view patterns as follows:

<programlisting>
clunky env (lookup env -> Just val1) (lookup env -> Just val2) = val1 + val2
...other equations for clunky...
</programlisting>
</para>

<para>
709
More precisely, the scoping rules are:
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
<itemizedlist>
<listitem>
<para>
In a single pattern, variables bound by patterns to the left of a view
pattern expression are in scope. For example:
<programlisting>
example :: Maybe ((String -> Integer,Integer), String) -> Bool
example Just ((f,_), f -> 4) = True
</programlisting>

Additionally, in function definitions, variables bound by matching earlier curried
arguments may be used in view pattern expressions in later arguments:
<programlisting>
example :: (String -> Integer) -> String -> Bool
example f (f -> 4) = True
</programlisting>
That is, the scoping is the same as it would be if the curried arguments
727
were collected into a tuple.
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
</para>
</listitem>

<listitem>
<para>
In mutually recursive bindings, such as <literal>let</literal>,
<literal>where</literal>, or the top level, view patterns in one
declaration may not mention variables bound by other declarations.  That
is, each declaration must be self-contained.  For example, the following
program is not allowed:
<programlisting>
let {(x -> y) = e1 ;
     (y -> x) = e2 } in x
</programlisting>

743
(For some amplification on this design choice see
744
<ulink url="http://hackage.haskell.org/trac/ghc/ticket/4061">Trac #4061</ulink>.)
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763

</para>
</listitem>
</itemizedlist>

</para>
</listitem>

<listitem><para> Typing: If <replaceable>exp</replaceable> has type
<replaceable>T1</replaceable> <literal>-></literal>
<replaceable>T2</replaceable> and <replaceable>pat</replaceable> matches
a <replaceable>T2</replaceable>, then the whole view pattern matches a
<replaceable>T1</replaceable>.
</para></listitem>

<listitem><para> Matching: To the equations in Section 3.17.3 of the
<ulink url="http://www.haskell.org/onlinereport/">Haskell 98
Report</ulink>, add the following:
<programlisting>
764
765
case v of { (e -> p) -> e1 ; _ -> e2 }
 =
766
767
768
769
770
771
772
773
case (e v) of { p -> e1 ; _ -> e2 }
</programlisting>
That is, to match a variable <replaceable>v</replaceable> against a pattern
<literal>(</literal> <replaceable>exp</replaceable>
<literal>-></literal> <replaceable>pat</replaceable>
<literal>)</literal>, evaluate <literal>(</literal>
<replaceable>exp</replaceable> <replaceable> v</replaceable>
<literal>)</literal> and match the result against
774
<replaceable>pat</replaceable>.
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
</para></listitem>

<listitem><para> Efficiency: When the same view function is applied in
multiple branches of a function definition or a case expression (e.g.,
in <literal>size</literal> above), GHC makes an attempt to collect these
applications into a single nested case expression, so that the view
function is only applied once.  Pattern compilation in GHC follows the
matrix algorithm described in Chapter 4 of <ulink
url="http://research.microsoft.com/~simonpj/Papers/slpj-book-1987/">The
Implementation of Functional Programming Languages</ulink>.  When the
top rows of the first column of a matrix are all view patterns with the
"same" expression, these patterns are transformed into a single nested
case.  This includes, for example, adjacent view patterns that line up
in a tuple, as in
<programlisting>
f ((view -> A, p1), p2) = e1
f ((view -> B, p3), p4) = e2
</programlisting>
</para>

<para> The current notion of when two view pattern expressions are "the
same" is very restricted: it is not even full syntactic equality.
However, it does include variables, literals, applications, and tuples;
e.g., two instances of <literal>view ("hi", "there")</literal> will be
collected.  However, the current implementation does not compare up to
alpha-equivalence, so two instances of <literal>(x, view x ->
y)</literal> will not be coalesced.
</para>

</listitem>

</itemizedlist>
</para>

809
810
811
812
813
814
</sect2>

    <!-- ===================== n+k patterns ===================  -->

<sect2 id="n-k-patterns">
<title>n+k patterns</title>
815
<indexterm><primary><option>-XNPlusKPatterns</option></primary></indexterm>
816
817

<para>
818
819
<literal>n+k</literal> pattern support is disabled by default. To enable
it, you can use the <option>-XNPlusKPatterns</option> flag.
820
821
</para>

Ian Lynagh's avatar
Ian Lynagh committed
822
823
824
825
826
827
828
829
830
831
832
833
834
</sect2>

    <!-- ===================== Traditional record syntax ===================  -->

<sect2 id="traditional-record-syntax">
<title>Traditional record syntax</title>
<indexterm><primary><option>-XNoTraditionalRecordSyntax</option></primary></indexterm>

<para>
Traditional record syntax, such as <literal>C {f = x}</literal>, is enabled by default.
To disable it, you can use the <option>-XNoTraditionalRecordSyntax</option> flag.
</para>

835
836
837
838
</sect2>

    <!-- ===================== Recursive do-notation ===================  -->

839
<sect2 id="recursive-do-notation">
840
841
842
843
<title>The recursive do-notation
</title>

<para>
844
845
846
    The do-notation of Haskell 98 does not allow <emphasis>recursive bindings</emphasis>,
    that is, the variables bound in a do-expression are visible only in the textually following
    code block. Compare this to a let-expression, where bound variables are visible in the entire binding
thoughtpolice's avatar
thoughtpolice committed
847
848
    group.
</para>
849

thoughtpolice's avatar
thoughtpolice committed
850
<para>
851
852
    It turns out that such recursive bindings do indeed make sense for a variety of monads, but
    not all. In particular, recursion in this sense requires a fixed-point operator for the underlying
Ross Paterson's avatar
Ross Paterson committed
853
854
855
856
857
858
    monad, captured by the <literal>mfix</literal> method of the <literal>MonadFix</literal> class, defined in <literal>Control.Monad.Fix</literal> as follows:
<programlisting>
class Monad m => MonadFix m where
   mfix :: (a -> m a) -> m a
</programlisting>
    Haskell's
859
860
861
    <literal>Maybe</literal>, <literal>[]</literal> (list), <literal>ST</literal> (both strict and lazy versions),
    <literal>IO</literal>, and many other monads have <literal>MonadFix</literal> instances. On the negative
    side, the continuation monad, with the signature <literal>(a -> r) -> r</literal>, does not.
862
</para>
863
864
865
866
867

<para>
    For monads that do belong to the <literal>MonadFix</literal> class, GHC provides
    an extended version of the do-notation that allows recursive bindings.
    The <option>-XRecursiveDo</option> (language pragma: <literal>RecursiveDo</literal>)
Ross Paterson's avatar
Ross Paterson committed
868
869
870
    provides the necessary syntactic support, introducing the keywords <literal>mdo</literal> and
    <literal>rec</literal> for higher and lower levels of the notation respectively. Unlike
    bindings in a <literal>do</literal> expression, those introduced by <literal>mdo</literal> and <literal>rec</literal>
871
872
873
874
    are recursively defined, much like in an ordinary let-expression. Due to the new
    keyword <literal>mdo</literal>, we also call this notation the <emphasis>mdo-notation</emphasis>.
</para>

875
<para>
876
    Here is a simple (albeit contrived) example:
877
<programlisting>
878
879
880
{-# LANGUAGE RecursiveDo #-}
justOnes = mdo { xs &lt;- Just (1:xs)
               ; return (map negate xs) }
881
</programlisting>
Ross Paterson's avatar
Ross Paterson committed
882
883
884
885
886
887
or equivalently
<programlisting>
{-# LANGUAGE RecursiveDo #-}
justOnes = do { rec { xs &lt;- Just (1:xs) }
              ; return (map negate xs) }
</programlisting>
888
889
As you can guess <literal>justOnes</literal> will evaluate to <literal>Just [-1,-1,-1,...</literal>.
</para>
890

thoughtpolice's avatar
thoughtpolice committed
891
<para>
892
893
894
   GHC's implementation the mdo-notation closely follows the original translation as described in the paper
   <ulink url="https://sites.google.com/site/leventerkok/recdo.pdf">A recursive do for Haskell</ulink>, which
   in turn is based on the work <ulink url="http://sites.google.com/site/leventerkok/erkok-thesis.pdf">Value Recursion
Ross Paterson's avatar
Ross Paterson committed
895
896
   in Monadic Computations</ulink>. Furthermore, GHC extends the syntax described in the former paper
   with a lower level syntax flagged by the <literal>rec</literal> keyword, as we describe next.
897
898
</para>

899
<sect3>
900
901
<title>Recursive binding groups</title>

902
<para>
903
904
905
906
    The flag <option>-XRecursiveDo</option> also introduces a new keyword <literal>rec</literal>, which wraps a
    mutually-recursive group of monadic statements inside a <literal>do</literal> expression, producing a single statement.
    Similar to a <literal>let</literal> statement inside a <literal>do</literal>, variables bound in
    the <literal>rec</literal> are visible throughout the <literal>rec</literal> group, and below it.  For example, compare
907
<programlisting>
908
909
910
911
    do { a &lt;- getChar            do { a &lt;- getChar
       ; let { r1 = f a r2          ; rec { r1 &lt;- f a r2
       ;     ; r2 = g r1 }          ;     ; r2 &lt;- g r1 }
       ; return (r1 ++ r2) }        ; return (r1 ++ r2) }
912
</programlisting>
913
914
915
916
    In both cases, <literal>r1</literal> and <literal>r2</literal> are available both throughout
    the <literal>let</literal> or <literal>rec</literal> block, and in the statements that follow it.
    The difference is that <literal>let</literal> is non-monadic, while <literal>rec</literal> is monadic.
    (In Haskell <literal>let</literal> is really <literal>letrec</literal>, of course.)
917
</para>
918

919
<para>
920
921
922
923
    The semantics of <literal>rec</literal> is fairly straightforward. Whenever GHC finds a <literal>rec</literal>
    group, it will compute its set of bound variables, and will introduce an appropriate call
    to the underlying monadic value-recursion operator <literal>mfix</literal>, belonging to the
    <literal>MonadFix</literal> class. Here is an example:
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
924
<programlisting>
925
926
927
rec { b &lt;- f a c     ===>    (b,c) &lt;- mfix (\~(b,c) -> do { b &lt;- f a c
    ; c &lt;- f b a }                                        ; c &lt;- f b a
                                                          ; return (b,c) })
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
928
</programlisting>
929
930
   As usual, the meta-variables <literal>b</literal>, <literal>c</literal> etc., can be arbitrary patterns.
   In general, the statement <literal>rec <replaceable>ss</replaceable></literal> is desugared to the statement
931
<programlisting>
932
<replaceable>vs</replaceable> &lt;- mfix (\~<replaceable>vs</replaceable> -&gt; do { <replaceable>ss</replaceable>; return <replaceable>vs</replaceable> })
933
</programlisting>
934
935
936
937
  where <replaceable>vs</replaceable> is a tuple of the variables bound by <replaceable>ss</replaceable>.
</para>

<para>
Ross Paterson's avatar
Ross Paterson committed
938
939
940
    Note in particular that the translation for a <literal>rec</literal> block only involves wrapping a call
    to <literal>mfix</literal>: it performs no other analysis on the bindings. The latter is the task
    for the <literal>mdo</literal> notation, which is described next.
941
942
943
944
945
946
947
948
</para>
</sect3>

<sect3>
<title>The <literal>mdo</literal> notation</title>

<para>
    A <literal>rec</literal>-block tells the compiler where precisely the recursive knot should be tied. It turns out that
Ross Paterson's avatar
Ross Paterson committed
949
950
    the placement of the recursive knots can be rather delicate: in particular, we would like the knots to be wrapped
    around as minimal groups as possible. This process is known as <emphasis>segmentation</emphasis>, and is described
951
952
    in detail in Secton 3.2 of <ulink url="https://sites.google.com/site/leventerkok/recdo.pdf">A recursive do for
    Haskell</ulink>. Segmentation improves polymorphism and reduces the size of the recursive knot. Most importantly, it avoids
Ross Paterson's avatar
Ross Paterson committed
953
    unnecessary interference caused by a fundamental issue with the so-called <emphasis>right-shrinking</emphasis>
954
955
956
957
958
    axiom for monadic recursion. In brief, most monads of interest (IO, strict state, etc.) do <emphasis>not</emphasis>
    have recursion operators that satisfy this axiom, and thus not performing segmentation can cause unnecessary
    interference, changing the termination behavior of the resulting translation.
    (Details can be found in Sections 3.1 and 7.2.2 of
    <ulink url="http://sites.google.com/site/leventerkok/erkok-thesis.pdf">Value Recursion in Monadic Computations</ulink>.)
959
</para>
960

961
<para>
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
    The <literal>mdo</literal> notation removes the burden of placing
    explicit <literal>rec</literal> blocks in the code.  Unlike an
    ordinary <literal>do</literal> expression, in which variables bound by
    statements are only in scope for later statements, variables bound in
    an <literal>mdo</literal> expression are in scope for all statements
    of the expression.  The compiler then automatically identifies minimal
    mutually recursively dependent segments of statements, treating them as
    if the user had wrapped a <literal>rec</literal> qualifier around them.
</para>

<para>
   The definition is syntactic:
</para>
<itemizedlist>
   <listitem>
       <para>
         A generator <replaceable>g</replaceable>
         <emphasis>depends</emphasis> on a textually following generator
         <replaceable>g'</replaceable>, if
       </para>
       <itemizedlist>
         <listitem>
           <para>
             <replaceable>g'</replaceable> defines a variable that
             is used by <replaceable>g</replaceable>, or
           </para>
         </listitem>
         <listitem>
           <para>
           <replaceable>g'</replaceable> textually appears between
           <replaceable>g</replaceable> and
           <replaceable>g''</replaceable>, where <replaceable>g</replaceable>
           depends on <replaceable>g''</replaceable>.
           </para>
         </listitem>
       </itemizedlist>
   </listitem>
   <listitem>
       <para>
         A <emphasis>segment</emphasis> of a given
         <literal>mdo</literal>-expression is a minimal sequence of generators
         such that no generator of the sequence depends on an outside
         generator. As a special case, although it is not a generator,
         the final expression in an <literal>mdo</literal>-expression is
         considered to form a segment by itself.
       </para>
   </listitem>
</itemizedlist>
<para>
   Segments in this sense are
   related to <emphasis>strongly-connected components</emphasis> analysis,
   with the exception that bindings in a segment cannot be reordered and
   must be contiguous.
1015
1016
1017
</para>

<para>
Ross Paterson's avatar
Ross Paterson committed
1018
    Here is an example <literal>mdo</literal>-expression, and its translation to <literal>rec</literal> blocks:
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
<programlisting>
mdo { a &lt;- getChar      ===> do { a &lt;- getChar
    ; b &lt;- f a c                ; rec { b &lt;- f a c
    ; c &lt;- f b a                ;     ; c &lt;- f b a }
    ; z &lt;- h a b                ; z &lt;- h a b
    ; d &lt;- g d e                ; rec { d &lt;- g d e
    ; e &lt;- g a z                ;     ; e &lt;- g a z }
    ; putChar c }               ; putChar c }
</programlisting>
Note that a given <literal>mdo</literal> expression can cause the creation of multiple <literal>rec</literal> blocks.
If there are no recursive dependencies, <literal>mdo</literal> will introduce no <literal>rec</literal> blocks. In this
latter case an <literal>mdo</literal> expression is precisely the same as a <literal>do</literal> expression, as one
would expect.
</para>

<para>
    In summary, given an <literal>mdo</literal> expression, GHC first performs segmentation, introducing
    <literal>rec</literal> blocks to wrap over minimal recursive groups. Then, each resulting
    <literal>rec</literal> is desugared, using a call to <literal>Control.Monad.Fix.mfix</literal> as described
    in the previous section. The original <literal>mdo</literal>-expression typechecks exactly when the desugared
Ross Paterson's avatar
Ross Paterson committed
1039
    version would do so.
1040
</para>
1041

1042
<para>
1043
Here are some other important points in using the recursive-do notation:
1044

1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
<itemizedlist>
    <listitem>
        <para>
            It is enabled with the flag <literal>-XRecursiveDo</literal>, or the <literal>LANGUAGE RecursiveDo</literal>
            pragma. (The same flag enables both <literal>mdo</literal>-notation, and the use of <literal>rec</literal>
            blocks inside <literal>do</literal> expressions.)
        </para>
    </listitem>
    <listitem>
        <para>
            <literal>rec</literal> blocks can also be used inside <literal>mdo</literal>-expressions, which will be
            treated as a single statement. However, it is good style to either use <literal>mdo</literal> or
            <literal>rec</literal> blocks in a single expression.
        </para>
    </listitem>
    <listitem>
        <para>
            If recursive bindings are required for a monad, then that monad must be declared an instance of
            the <literal>MonadFix</literal> class.
        </para>
    </listitem>
    <listitem>
        <para>
            The following instances of <literal>MonadFix</literal> are automatically provided: List, Maybe, IO.
            Furthermore, the <literal>Control.Monad.ST</literal> and <literal>Control.Monad.ST.Lazy</literal>
            modules provide the instances of the <literal>MonadFix</literal> class for Haskell's internal
            state monad (strict and lazy, respectively).
        </para>
    </listitem>
    <listitem>
        <para>
            Like <literal>let</literal> and <literal>where</literal> bindings, name shadowing is not allowed within
            an <literal>mdo</literal>-expression or a <literal>rec</literal>-block; that is, all the names bound in
            a single <literal>rec</literal> must be distinct. (GHC will complain if this is not the case.)
        </para>
    </listitem>
1081
1082
</itemizedlist>
</para>
1083
1084
</sect3>

1085

1086
1087
1088
</sect2>


1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
   <!-- ===================== PARALLEL LIST COMPREHENSIONS ===================  -->

  <sect2 id="parallel-list-comprehensions">
    <title>Parallel List Comprehensions</title>
    <indexterm><primary>list comprehensions</primary><secondary>parallel</secondary>
    </indexterm>
    <indexterm><primary>parallel list comprehensions</primary>
    </indexterm>

    <para>Parallel list comprehensions are a natural extension to list
    comprehensions.  List comprehensions can be thought of as a nice
    syntax for writing maps and filters.  Parallel comprehensions
    extend this to include the zipWith family.</para>

    <para>A parallel list comprehension has multiple independent
    branches of qualifier lists, each separated by a `|' symbol.  For
    example, the following zips together two lists:</para>

<programlisting>
1108
   [ (x, y) | x &lt;- xs | y &lt;- ys ]
1109
1110
</programlisting>

daniel.is.fischer's avatar
daniel.is.fischer committed
1111
    <para>The behaviour of parallel list comprehensions follows that of
1112
1113
1114
1115
1116
1117
1118
1119
1120
    zip, in that the resulting list will have the same length as the
    shortest branch.</para>

    <para>We can define parallel list comprehensions by translation to
    regular comprehensions.  Here's the basic idea:</para>

    <para>Given a parallel comprehension of the form: </para>

<programlisting>
1121
1122
1123
1124
   [ e | p1 &lt;- e11, p2 &lt;- e12, ...
       | q1 &lt;- e21, q2 &lt;- e22, ...
       ...
   ]
1125
1126
1127
1128
1129
</programlisting>

    <para>This will be translated to: </para>

<programlisting>
1130
1131
1132
1133
   [ e | ((p1,p2), (q1,q2), ...) &lt;- zipN [(p1,p2) | p1 &lt;- e11, p2 &lt;- e12, ...]
                                         [(q1,q2) | q1 &lt;- e21, q2 &lt;- e22, ...]
                                         ...
   ]
1134
1135
1136
1137
1138
</programlisting>

    <para>where `zipN' is the appropriate zip for the given number of
    branches.</para>

1139
  </sect2>
1140

1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
  <!-- ===================== TRANSFORM LIST COMPREHENSIONS ===================  -->

  <sect2 id="generalised-list-comprehensions">
    <title>Generalised (SQL-Like) List Comprehensions</title>
    <indexterm><primary>list comprehensions</primary><secondary>generalised</secondary>
    </indexterm>
    <indexterm><primary>extended list comprehensions</primary>
    </indexterm>
    <indexterm><primary>group</primary></indexterm>
    <indexterm><primary>sql</primary></indexterm>


    <para>Generalised list comprehensions are a further enhancement to the
Ian Lynagh's avatar
Ian Lynagh committed
1154
    list comprehension syntactic sugar to allow operations such as sorting
1155
1156
1157
1158
    and grouping which are familiar from SQL.   They are fully described in the
	paper <ulink url="http://research.microsoft.com/~simonpj/papers/list-comp">
	  Comprehensive comprehensions: comprehensions with "order by" and "group by"</ulink>,
    except that the syntax we use differs slightly from the paper.</para>
1159
<para>The extension is enabled with the flag <option>-XTransformListComp</option>.</para>
1160
<para>Here is an example:
1161
1162
1163
1164
1165
1166
1167
1168
1169
<programlisting>
employees = [ ("Simon", "MS", 80)
, ("Erik", "MS", 100)
, ("Phil", "Ed", 40)
, ("Gordon", "Ed", 45)
, ("Paul", "Yale", 60)]

output = [ (the dept, sum salary)
| (name, dept, salary) &lt;- employees
1170
, then group by dept using groupWith
1171
1172
1173
, then sortWith by (sum salary)
, then take 5 ]
</programlisting>
1174
In this example, the list <literal>output</literal> would take on
1175
    the value:
1176

1177
1178
1179
1180
1181
<programlisting>
[("Yale", 60), ("Ed", 85), ("MS", 180)]
</programlisting>
</para>
<para>There are three new keywords: <literal>group</literal>, <literal>by</literal>, and <literal>using</literal>.
1182
1183
(The functions <literal>sortWith</literal> and <literal>groupWith</literal> are not keywords; they are ordinary
functions that are exported by <literal>GHC.Exts</literal>.)</para>
1184

SamB's avatar
SamB committed
1185
<para>There are five new forms of comprehension qualifier,
1186
1187
1188
all introduced by the (existing) keyword <literal>then</literal>:
    <itemizedlist>
    <listitem>
1189

1190
1191
1192
1193
1194
<programlisting>
then f
</programlisting>

    This statement requires that <literal>f</literal> have the type <literal>
Ian Lynagh's avatar
Ian Lynagh committed
1195
    forall a. [a] -> [a]</literal>. You can see an example of its use in the
1196
    motivating example, as this form is used to apply <literal>take 5</literal>.
1197

1198
    </listitem>
1199
1200


1201
1202
1203
1204
1205
1206
1207
    <listitem>
<para>
<programlisting>
then f by e
</programlisting>

    This form is similar to the previous one, but allows you to create a function
1208
    which will be passed as the first argument to f. As a consequence f must have
1209
    the type <literal>forall a. (a -> t) -> [a] -> [a]</literal>. As you can see
1210
    from the type, this function lets f &quot;project out&quot; some information
1211
1212
    from the elements of the list it is transforming.</para>

1213
1214
    <para>An example is shown in the opening example, where <literal>sortWith</literal>
    is supplied with a function that lets it find out the <literal>sum salary</literal>
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
    for any item in the list comprehension it transforms.</para>

    </listitem>


    <listitem>

<programlisting>
then group by e using f
</programlisting>

    <para>This is the most general of the grouping-type statements. In this form,
    f is required to have type <literal>forall a. (a -> t) -> [a] -> [[a]]</literal>.
    As with the <literal>then f by e</literal> case above, the first argument
    is a function supplied to f by the compiler which lets it compute e on every
    element of the list being transformed. However, unlike the non-grouping case,
    f additionally partitions the list into a number of sublists: this means that
SamB's avatar
SamB committed
1232
    at every point after this statement, binders occurring before it in the comprehension
1233
1234
    refer to <emphasis>lists</emphasis> of possible values, not single values. To help understand
    this, let's look at an example:</para>
1235

1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
<programlisting>
-- This works similarly to groupWith in GHC.Exts, but doesn't sort its input first
groupRuns :: Eq b => (a -> b) -> [a] -> [[a]]
groupRuns f = groupBy (\x y -> f x == f y)

output = [ (the x, y)
| x &lt;- ([1..3] ++ [1..2])
, y &lt;- [4..6]
, then group by x using groupRuns ]
</programlisting>

    <para>This results in the variable <literal>output</literal> taking on the value below:</para>

<programlisting>
[(1, [4, 5, 6]), (2, [4, 5, 6]), (3, [4, 5, 6]), (1, [4, 5, 6]), (2, [4, 5, 6])]
</programlisting>

1253
1254
    <para>Note that we have used the <literal>the</literal> function to change the type
    of x from a list to its original numeric type. The variable y, in contrast, is left
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
    unchanged from the list form introduced by the grouping.</para>

    </listitem>

    <listitem>

<programlisting>
then group using f
</programlisting>

    <para>With this form of the group statement, f is required to simply have the type
    <literal>forall a. [a] -> [[a]]</literal>, which will be used to group up the
    comprehension so far directly. An example of this form is as follows:</para>
1268

1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
<programlisting>
output = [ x
| y &lt;- [1..5]
, x &lt;- "hello"
, then group using inits]
</programlisting>

    <para>This will yield a list containing every prefix of the word "hello" written out 5 times:</para>

<programlisting>
["","h","he","hel","hell","hello","helloh","hellohe","hellohel","hellohell","hellohello","hellohelloh",...]
</programlisting>

    </listitem>
</itemizedlist>
</para>
1285
1286
  </sect2>

1287
1288
1289
1290
1291
1292
1293
   <!-- ===================== MONAD COMPREHENSIONS ===================== -->

<sect2 id="monad-comprehensions">
    <title>Monad comprehensions</title>
    <indexterm><primary>monad comprehensions</primary></indexterm>

    <para>
1294
1295
1296
1297
        Monad comprehensions generalise the list comprehension notation,
        including parallel comprehensions
        (<xref linkend="parallel-list-comprehensions"/>) and
        transform comprehensions (<xref linkend="generalised-list-comprehensions"/>)
1298
        to work for any monad.
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
    </para>

    <para>Monad comprehensions support:</para>

    <itemizedlist>
        <listitem>
            <para>
                Bindings:
            </para>

<programlisting>
[ x + y | x &lt;- Just 1, y &lt;- Just 2 ]
</programlisting>

            <para>
                Bindings are translated with the <literal>(&gt;&gt;=)</literal> and
                <literal>return</literal> functions to the usual do-notation:
            </para>

<programlisting>
do x &lt;- Just 1
   y &lt;- Just 2
   return (x+y)
</programlisting>

        </listitem>
        <listitem>
            <para>
                Guards:
            </para>

<programlisting>
[ x | x &lt;- [1..10], x &lt;= 5 ]
</programlisting>

            <para>
                Guards are translated with the <literal>guard</literal> function,
                which requires a <literal>MonadPlus</literal> instance:
            </para>

<programlisting>
do x &lt;- [1..10]
   guard (x &lt;= 5)
   return x
</programlisting>

        </listitem>
        <listitem>
            <para>
                Transform statements (as with <literal>-XTransformListComp</literal>):
            </para>

<programlisting>
[ x+y | x &lt;- [1..10], y &lt;- [1..x], then take 2 ]
</programlisting>

            <para>
                This translates to:
            </para>

<programlisting>
do (x,y) &lt;- take 2 (do x &lt;- [1..10]
                       y &lt;- [1..x]
                       return (x,y))
   return (x+y)
</programlisting>

        </listitem>
        <listitem>
            <para>
                Group statements (as with <literal>-XTransformListComp</literal>):
            </para>

<programlisting>
[ x | x &lt;- [1,1,2,2,3], then group by x using GHC.Exts.groupWith ]
[ x | x &lt;- [1,1,2,2,3], then group using myGroup ]
</programlisting>

        </listitem>
        <listitem>
            <para>
                Parallel statements (as with <literal>-XParallelListComp</literal>):
            </para>

<programlisting>
[ (x+y) | x &lt;- [1..10]
        | y &lt;- [11..20]
        ]
</programlisting>

            <para>
                Parallel statements are translated using the
                <literal>mzip</literal> function, which requires a
                <literal>MonadZip</literal> instance defined in
                <ulink url="&libraryBaseLocation;/Control-Monad-Zip.html"><literal>Control.Monad.Zip</literal></ulink>:
            </para>

<programlisting>
do (x,y) &lt;- mzip (do x &lt;- [1..10]
                     return x)
                 (do y &lt;- [11..20]
                     return y)
   return (x+y)
</programlisting>

        </listitem>
    </itemizedlist>

    <para>
        All these features are enabled by default if the
        <literal>MonadComprehensions</literal> extension is enabled. The types
        and more detailed examples on how to use comprehensions are explained
        in the previous chapters <xref
            linkend="generalised-list-comprehensions"/> and <xref
            linkend="parallel-list-comprehensions"/>. In general you just have
        to replace the type <literal>[a]</literal> with the type
        <literal>Monad m => m a</literal> for monad comprehensions.
    </para>

    <para>
        Note: Even though most of these examples are using the list monad,
        monad comprehensions work for any monad.
        The <literal>base</literal> package offers all necessary instances for
        lists, which make <literal>MonadComprehensions</literal> backward
        compatible to built-in, transform and parallel list comprehensions.
    </para>
1425
<para> More formally, the desugaring is as follows.  We write <literal>D[ e | Q]</literal>
1426
to mean the desugaring of the monad comprehension <literal>[ e | Q]</literal>:
1427
1428
1429
<programlisting>
Expressions: e
Declarations: d
1430
Lists of qualifiers: Q,R,S
1431
1432
1433

-- Basic forms
D[ e | ]               = return e
1434
D[ e | p &lt;- e, Q ]  = e &gt;&gt;= \p -&gt; D[ e | Q ]
1435
1436
1437
1438
1439
1440
1441
1442
1443
D[ e | e, Q ]          = guard e &gt;&gt; \p -&gt; D[ e | Q ]
D[ e | let d, Q ]      = let d in D[ e | Q ]

-- Parallel comprehensions (iterate for multiple parallel branches)
D[ e | (Q | R), S ]    = mzip D[ Qv | Q ] D[ Rv | R ] &gt;&gt;= \(Qv,Rv) -&gt; D[ e | S ]

-- Transform comprehensions
D[ e | Q then f, R ]                  = f D[ Qv | Q ] &gt;&gt;= \Qv -&gt; D[ e | R ]

1444
D[ e | Q then f by b, R ]             = f (\Qv -&gt; b) D[ Qv | Q ] &gt;&gt;= \Qv -&gt; D[ e | R ]
1445

1446
D[ e | Q then group using f, R ]      = f D[ Qv | Q ] &gt;&gt;= \ys -&gt;
1447
1448
1449
                                        case (fmap selQv1 ys, ..., fmap selQvn ys) of
                                 	     Qv -&gt; D[ e | R ]

1450
D[ e | Q then group by b using f, R ] = f (\Qv -&gt; b) D[ Qv | Q ] &gt;&gt;= \ys -&gt;
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
                                        case (fmap selQv1 ys, ..., fmap selQvn ys) of
                                           Qv -&gt; D[ e | R ]

where  Qv is the tuple of variables bound by Q (and used subsequently)
       selQvi is a selector mapping Qv to the ith component of Qv

Operator     Standard binding       Expected type
--------------------------------------------------------------------
return       GHC.Base               t1 -&gt; m t2
(&gt;&gt;=)        GHC.Base               m1 t1 -&gt; (t2 -&gt; m2 t3) -&gt; m3 t3
(&gt;&gt;)         GHC.Base               m1 t1 -&gt; m2 t2         -&gt; m3 t3
guard        Control.Monad          t1 -&gt; m t2
fmap         GHC.Base               forall a b. (a-&gt;b) -&gt; n a -&gt; n b
mzip         Control.Monad.Zip      forall a b. m a -&gt; m b -&gt; m (a,b)
1465
1466
</programlisting>
The comprehension should typecheck when its desugaring would typecheck.
1467
1468
</para>
<para>
1469
Monad comprehensions support rebindable syntax (<xref linkend="rebindable-syntax"/>).
1470
1471
1472
1473
1474
1475
1476
Without rebindable
syntax, the operators from the "standard binding" module are used; with
rebindable syntax, the operators are looked up in the current lexical scope.
For example, parallel comprehensions will be typechecked and desugared
using whatever "<literal>mzip</literal>" is in scope.
</para>
<para>
1477
The rebindable operators must have the "Expected type" given in the
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
table above.  These types are surprisingly general.  For example, you can
use a bind operator with the type
<programlisting>
(>>=) :: T x y a -> (a -> T y z b) -> T x z b
</programlisting>
In the case of transform comprehensions, notice that the groups are
parameterised over some arbitrary type <literal>n</literal> (provided it
has an <literal>fmap</literal>, as well as
the comprehension being over an arbitrary monad.
</para>
1488
1489
</sect2>

1490
1491
   <!-- ===================== REBINDABLE SYNTAX ===================  -->

1492
<sect2 id="rebindable-syntax">
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
<title>Rebindable syntax and the implicit Prelude import</title>

 <para><indexterm><primary>-XNoImplicitPrelude
 option</primary></indexterm> GHC normally imports
 <filename>Prelude.hi</filename> files for you.  If you'd
 rather it didn't, then give it a
 <option>-XNoImplicitPrelude</option> option.  The idea is
 that you can then import a Prelude of your own.  (But don't
 call it <literal>Prelude</literal>; the Haskell module
 namespace is flat, and you must not conflict with any
 Prelude module.)</para>

            <para>Suppose you are importing a Prelude of your own
	      in order to define your own numeric class
1507
1508
1509
            hierarchy.  It completely defeats that purpose if the
            literal "1" means "<literal>Prelude.fromInteger
            1</literal>", which is what the Haskell Report specifies.
1510
            So the <option>-XRebindableSyntax</option>
1511
	      flag causes
1512
1513
            the following pieces of built-in syntax to refer to
            <emphasis>whatever is in scope</emphasis>, not the Prelude
1514
            versions:
1515
1516
	    <itemizedlist>
	      <listitem>
1517
1518
1519
		<para>An integer literal <literal>368</literal> means
                "<literal>fromInteger (368::Integer)</literal>", rather than
                "<literal>Prelude.fromInteger (368::Integer)</literal>".
1520
</para> </listitem>
1521

1522
      <listitem><para>Fractional literals are handed in just the same way,
1523
	  except that the translation is
1524
	      <literal>fromRational (3.68::Rational)</literal>.
1525
</para> </listitem>
1526
1527
1528

	  <listitem><para>The equality test in an overloaded numeric pattern
	      uses whatever <literal>(==)</literal> is in scope.
1529
</para> </listitem>
1530
1531
1532
1533
1534

	  <listitem><para>The subtraction operation, and the
	  greater-than-or-equal test, in <literal>n+k</literal> patterns
	      use whatever <literal>(-)</literal> and <literal>(>=)</literal> are in scope.
	      </para></listitem>
1535
1536

	      <listitem>
1537
1538
1539
1540
		<para>Negation (e.g. "<literal>- (f x)</literal>")
		means "<literal>negate (f x)</literal>", both in numeric
		patterns, and expressions.
	      </para></listitem>
1541

1542
1543
1544
1545
1546
	      <listitem>
		<para>Conditionals (e.g. "<literal>if</literal> e1 <literal>then</literal> e2 <literal>else</literal> e3")
		means "<literal>ifThenElse</literal> e1 e2 e3".  However <literal>case</literal> expressions are unaffected.
	      </para></listitem>

1547
1548
1549
	      <listitem>
	  <para>"Do" notation is translated using whatever
	      functions <literal>(>>=)</literal>,
1550
1551
	      <literal>(>>)</literal>, and <literal>fail</literal>,
	      are in scope (not the Prelude
1552
	      versions).  List comprehensions, mdo (<xref linkend="recursive-do-notation"/>), and parallel array
1553
	      comprehensions, are unaffected.  </para></listitem>
ross's avatar
ross committed
1554
1555

	      <listitem>
1556
		<para>Arrow
ross's avatar
ross committed
1557
1558
1559
1560
		notation (see <xref linkend="arrow-notation"/>)
		uses whatever <literal>arr</literal>,
		<literal>(>>>)</literal>, <literal>first</literal>,
		<literal>app</literal>, <literal>(|||)</literal> and
1561
1562
1563
1564
1565
		<literal>loop</literal> functions are in scope. But unlike the
		other constructs, the types of these functions must match the
		Prelude types very closely.  Details are in flux; if you want
		to use this, ask!
	      </para></listitem>
1566
	    </itemizedlist>
1567
1568
1569
<option>-XRebindableSyntax</option> implies <option>-XNoImplicitPrelude</option>.
</para>
<para>
1570
In all cases (apart from arrow notation), the static semantics should be that of the desugared form,
1571
even if that is a little unexpected. For example, the
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
static semantics of the literal <literal>368</literal>
is exactly that of <literal>fromInteger (368::Integer)</literal>; it's fine for
<literal>fromInteger</literal> to have any of the types:
<programlisting>
fromInteger :: Integer -> Integer
fromInteger :: forall a. Foo a => Integer -> a
fromInteger :: Num a => a -> Integer
fromInteger :: Integer -> Bool -> Bool
</programlisting>
</para>
1582

ross's avatar
ross committed
1583
1584
1585
1586
	     <para>Be warned: this is an experimental facility, with
	     fewer checks than usual.  Use <literal>-dcore-lint</literal>
	     to typecheck the desugared program.  If Core Lint is happy
	     you should be all right.</para>
1587
1588

</sect2>
1589
1590
1591
1592
1593

<sect2 id="postfix-operators">
<title>Postfix operators</title>

<para>