8006fffc184bcd93583f1d57cbfae1b0c22f035c
[ghc.git] / docs / users_guide / using.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="using-ghc">
3 <title>Using GHC</title>
4
5 <indexterm><primary>GHC, using</primary></indexterm>
6 <indexterm><primary>using GHC</primary></indexterm>
7
8 <sect1>
9 <title>Getting started: compiling programs</title>
10
11 <para>
12 In this chapter you'll find a complete reference to the GHC
13 command-line syntax, including all 400+ flags. It's a large and
14 complex system, and there are lots of details, so it can be
15 quite hard to figure out how to get started. With that in mind,
16 this introductory section provides a quick introduction to the
17 basic usage of GHC for compiling a Haskell program, before the
18 following sections dive into the full syntax.
19 </para>
20
21 <para>
22 Let's create a Hello World program, and compile and run it.
23 First, create a file <filename>hello.hs</filename> containing
24 the Haskell code:
25 </para>
26
27 <programlisting>
28 main = putStrLn "Hello, World!"
29 </programlisting>
30
31 <para>To compile the program, use GHC like this:</para>
32
33 <screen>
34 $ ghc hello.hs
35 </screen>
36
37 <para>(where <literal>$</literal> represents the prompt: don't
38 type it). GHC will compile the source
39 file <filename>hello.hs</filename>, producing
40 an <firstterm>object
41 file</firstterm> <filename>hello.o</filename> and
42 an <firstterm>interface
43 file</firstterm> <filename>hello.hi</filename>, and then it
44 will link the object file to the libraries that come with GHC
45 to produce an executable called <filename>hello</filename> on
46 Unix/Linux/Mac, or <filename>hello.exe</filename> on
47 Windows.</para>
48
49 <para>
50 By default GHC will be very quiet about what it is doing, only
51 printing error messages. If you want to see in more detail
52 what's going on behind the scenes, add <option>-v</option> to
53 the command line.
54 </para>
55
56 <para>
57 Then we can run the program like this:
58 </para>
59
60 <screen>
61 $ ./hello
62 Hello World!
63 </screen>
64
65 <para>
66 If your program contains multiple modules, then you only need to
67 tell GHC the name of the source file containing
68 the <filename>Main</filename> module, and GHC will examine
69 the <literal>import</literal> declarations to find the other
70 modules that make up the program and find their source files.
71 This means that, with the exception of
72 the <literal>Main</literal> module, every source file should be
73 named after the module name that it contains (with dots replaced
74 by directory separators). For example, the
75 module <literal>Data.Person</literal> would be in the
76 file <filename>Data/Person.hs</filename> on Unix/Linux/Mac,
77 or <filename>Data\Person.hs</filename> on Windows.
78 </para>
79 </sect1>
80
81 <sect1>
82 <title>Options overview</title>
83
84 <para>GHC's behaviour is controlled by
85 <firstterm>options</firstterm>, which for historical reasons are
86 also sometimes referred to as command-line flags or arguments.
87 Options can be specified in three ways:</para>
88
89 <sect2>
90 <title>Command-line arguments</title>
91
92 <indexterm><primary>structure, command-line</primary></indexterm>
93 <indexterm><primary>command-line</primary><secondary>arguments</secondary></indexterm>
94 <indexterm><primary>arguments</primary><secondary>command-line</secondary></indexterm>
95
96 <para>An invocation of GHC takes the following form:</para>
97
98 <screen>
99 ghc [argument...]
100 </screen>
101
102 <para>Command-line arguments are either options or file names.</para>
103
104 <para>Command-line options begin with <literal>-</literal>.
105 They may <emphasis>not</emphasis> be grouped:
106 <option>-vO</option> is different from <option>-v -O</option>.
107 Options need not precede filenames: e.g., <literal>ghc *.o -o
108 foo</literal>. All options are processed and then applied to
109 all files; you cannot, for example, invoke <literal>ghc -c -O1
110 Foo.hs -O2 Bar.hs</literal> to apply different optimisation
111 levels to the files <filename>Foo.hs</filename> and
112 <filename>Bar.hs</filename>.</para>
113 </sect2>
114
115 <sect2 id="source-file-options">
116 <title>Command line options in source files</title>
117
118 <indexterm><primary>source-file options</primary></indexterm>
119
120 <para>Sometimes it is useful to make the connection between a
121 source file and the command-line options it requires quite
122 tight. For instance, if a Haskell source file deliberately
123 uses name shadowing, it should be compiled with the
124 <option>-fno-warn-name-shadowing</option> option. Rather than maintaining
125 the list of per-file options in a <filename>Makefile</filename>,
126 it is possible to do this directly in the source file using the
127 <literal>OPTIONS_GHC</literal> pragma <indexterm><primary>OPTIONS_GHC
128 pragma</primary></indexterm>:</para>
129
130 <programlisting>
131 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
132 module X where
133 ...
134 </programlisting>
135
136 <para><literal>OPTIONS_GHC</literal> is a <emphasis>file-header pragma</emphasis>
137 (see <xref linkend="pragmas"/>).</para>
138
139 <para>Only <emphasis>dynamic</emphasis> flags can be used in an <literal>OPTIONS_GHC</literal> pragma
140 (see <xref linkend="static-dynamic-flags"/>).</para>
141
142 <para>Note that your command shell does not
143 get to the source file options, they are just included literally
144 in the array of command-line arguments the compiler
145 maintains internally, so you'll be desperately disappointed if
146 you try to glob etc. inside <literal>OPTIONS_GHC</literal>.</para>
147
148 <para>NOTE: the contents of OPTIONS_GHC are appended to the
149 command-line options, so options given in the source file
150 override those given on the command-line.</para>
151
152 <para>It is not recommended to move all the contents of your
153 Makefiles into your source files, but in some circumstances, the
154 <literal>OPTIONS_GHC</literal> pragma is the Right Thing. (If you
155 use <option>-keep-hc-file</option> and have OPTION flags in
156 your module, the OPTIONS_GHC will get put into the generated .hc
157 file).</para>
158 </sect2>
159
160 <sect2>
161 <title>Setting options in GHCi</title>
162
163 <para>Options may also be modified from within GHCi, using the
164 <literal>:set</literal> command. See <xref linkend="ghci-set"/>
165 for more details.</para>
166 </sect2>
167 </sect1>
168
169 <sect1 id="static-dynamic-flags">
170 <title>Static, Dynamic, and Mode options</title>
171 <indexterm><primary>static</primary><secondary>options</secondary>
172 </indexterm>
173 <indexterm><primary>dynamic</primary><secondary>options</secondary>
174 </indexterm>
175 <indexterm><primary>mode</primary><secondary>options</secondary>
176 </indexterm>
177
178 <para>Each of GHC's command line options is classified as
179 <firstterm>static</firstterm>, <firstterm>dynamic</firstterm> or
180 <firstterm>mode</firstterm>:</para>
181
182 <variablelist>
183 <varlistentry>
184 <term>Mode flags</term>
185 <listitem>
186 <para>For example, <option>--make</option> or <option>-E</option>.
187 There may only be a single mode flag on the command line. The
188 available modes are listed in <xref linkend="modes"/>.</para>
189 </listitem>
190 </varlistentry>
191 <varlistentry>
192 <term>Dynamic Flags</term>
193 <listitem>
194 <para>Most non-mode flags fall into this category. A dynamic flag
195 may be used on the command line, in a
196 <literal>OPTIONS_GHC</literal> pragma in a source file, or set
197 using <literal>:set</literal> in GHCi.</para>
198 </listitem>
199 </varlistentry>
200 <varlistentry>
201 <term>Static Flags</term>
202 <listitem>
203 <para>A few flags are "static", which means they can only be used on
204 the command-line, and remain in force over the entire GHC/GHCi
205 run.</para>
206 </listitem>
207 </varlistentry>
208 </variablelist>
209
210 <para>The flag reference tables (<xref
211 linkend="flag-reference"/>) lists the status of each flag.</para>
212
213 <para>There are a few flags that are static except that they can
214 also be used with GHCi's <literal>:set</literal> command; these
215 are listed as &ldquo;static/<literal>:set</literal>&rdquo; in the
216 table.</para>
217 </sect1>
218
219 <sect1 id="file-suffixes">
220 <title>Meaningful file suffixes</title>
221
222 <indexterm><primary>suffixes, file</primary></indexterm>
223 <indexterm><primary>file suffixes for GHC</primary></indexterm>
224
225 <para>File names with &ldquo;meaningful&rdquo; suffixes (e.g.,
226 <filename>.lhs</filename> or <filename>.o</filename>) cause the
227 &ldquo;right thing&rdquo; to happen to those files.</para>
228
229 <variablelist>
230
231 <varlistentry>
232 <term><filename>.hs</filename></term>
233 <listitem>
234 <para>A Haskell module.</para>
235 </listitem>
236 </varlistentry>
237
238 <varlistentry>
239 <term>
240 <filename>.lhs</filename>
241 <indexterm><primary><literal>lhs</literal> suffix</primary></indexterm>
242 </term>
243 <listitem>
244 <para>A &ldquo;literate Haskell&rdquo; module.</para>
245 </listitem>
246 </varlistentry>
247
248 <varlistentry>
249 <term><filename>.hspp</filename></term>
250 <listitem>
251 <para>A file created by the preprocessor.</para>
252 </listitem>
253 </varlistentry>
254
255 <varlistentry>
256 <term><filename>.hi</filename></term>
257 <listitem>
258 <para>A Haskell interface file, probably
259 compiler-generated.</para>
260 </listitem>
261 </varlistentry>
262
263 <varlistentry>
264 <term><filename>.hc</filename></term>
265 <listitem>
266 <para>Intermediate C file produced by the Haskell
267 compiler.</para>
268 </listitem>
269 </varlistentry>
270
271 <varlistentry>
272 <term><filename>.c</filename></term>
273 <listitem>
274 <para>A C&nbsp;file not produced by the Haskell
275 compiler.</para>
276 </listitem>
277 </varlistentry>
278
279 <varlistentry>
280 <term><filename>.ll</filename></term>
281 <listitem>
282 <para>An llvm-intermediate-language source file, usually
283 produced by the compiler.</para>
284 </listitem>
285 </varlistentry>
286
287 <varlistentry>
288 <term><filename>.bc</filename></term>
289 <listitem>
290 <para>An llvm-intermediate-language bitcode file, usually
291 produced by the compiler.</para>
292 </listitem>
293 </varlistentry>
294
295 <varlistentry>
296 <term><filename>.s</filename></term>
297 <listitem>
298 <para>An assembly-language source file, usually produced by
299 the compiler.</para>
300 </listitem>
301 </varlistentry>
302
303 <varlistentry>
304 <term><filename>.o</filename></term>
305 <listitem>
306 <para>An object file, produced by an assembler.</para>
307 </listitem>
308 </varlistentry>
309 </variablelist>
310
311 <para>Files with other suffixes (or without suffixes) are passed
312 straight to the linker.</para>
313
314 </sect1>
315
316 <sect1 id="modes">
317 <title>Modes of operation</title>
318 <indexterm><primary>help options</primary></indexterm>
319
320 <para>
321 GHC's behaviour is firstly controlled by a mode flag. Only one
322 of these flags may be given, but it does not necessarily need to
323 be the first option on the command-line.
324 </para>
325
326 <para>
327 If no mode flag is present, then GHC will enter make mode
328 (<xref linkend="make-mode" />) if there are any Haskell source
329 files given on the command line, or else it will link the
330 objects named on the command line to produce an executable.
331 </para>
332
333 <para>The available mode flags are:</para>
334
335 <variablelist>
336 <varlistentry>
337 <term>
338 <cmdsynopsis><command>ghc --interactive</command>
339 </cmdsynopsis>
340 <indexterm><primary>interactive mode</primary></indexterm>
341 <indexterm><primary>ghci</primary></indexterm>
342 </term>
343 <listitem>
344 <para>Interactive mode, which is also available as
345 <command>ghci</command>. Interactive mode is described in
346 more detail in <xref linkend="ghci"/>.</para>
347 </listitem>
348 </varlistentry>
349
350 <varlistentry>
351 <term>
352 <cmdsynopsis><command>ghc --make</command>
353 </cmdsynopsis>
354 <indexterm><primary>make mode</primary></indexterm>
355 <indexterm><primary><option>--make</option></primary></indexterm>
356 </term>
357 <listitem>
358 <para>In this mode, GHC will build a multi-module Haskell
359 program automatically, figuring out dependencies for itself.
360 If you have a straightforward Haskell program, this is
361 likely to be much easier, and faster, than using
362 <command>make</command>. Make mode is described in <xref
363 linkend="make-mode"/>.</para>
364
365 <para>
366 This mode is the default if there are any Haskell
367 source files mentioned on the command line, and in this case
368 the <option>--make</option> option can be omitted.
369 </para>
370 </listitem>
371 </varlistentry>
372
373 <varlistentry>
374 <term>
375 <cmdsynopsis><command>ghc -e</command>
376 <arg choice='plain'><replaceable>expr</replaceable></arg>
377 </cmdsynopsis>
378 <indexterm><primary>eval mode</primary></indexterm>
379 </term>
380 <listitem>
381 <para>Expression-evaluation mode. This is very similar to
382 interactive mode, except that there is a single expression
383 to evaluate (<replaceable>expr</replaceable>) which is given
384 on the command line. See <xref linkend="eval-mode"/> for
385 more details.</para>
386 </listitem>
387 </varlistentry>
388
389 <varlistentry>
390 <term>
391 <cmdsynopsis>
392 <command>ghc -E</command>
393 <command>ghc -C</command>
394 <command>ghc -S</command>
395 <command>ghc -c</command>
396 </cmdsynopsis>
397 <indexterm><primary><option>-E</option></primary></indexterm>
398 <indexterm><primary><option>-C</option></primary></indexterm>
399 <indexterm><primary><option>-S</option></primary></indexterm>
400 <indexterm><primary><option>-c</option></primary></indexterm>
401 </term>
402 <listitem>
403 <para>This is the traditional batch-compiler mode, in which
404 GHC can compile source files one at a time, or link objects
405 together into an executable. See <xref
406 linkend="options-order"/>.</para>
407 </listitem>
408 </varlistentry>
409
410 <varlistentry>
411 <term>
412 <cmdsynopsis>
413 <command>ghc -M</command>
414 </cmdsynopsis>
415 <indexterm><primary>dependency-generation mode</primary></indexterm>
416 </term>
417 <listitem>
418 <para>Dependency-generation mode. In this mode, GHC can be
419 used to generate dependency information suitable for use in
420 a <literal>Makefile</literal>. See <xref
421 linkend="makefile-dependencies"/>.</para>
422 </listitem>
423 </varlistentry>
424
425 <varlistentry>
426 <term>
427 <cmdsynopsis>
428 <command>ghc --mk-dll</command>
429 </cmdsynopsis>
430 <indexterm><primary>DLL-creation mode</primary></indexterm>
431 </term>
432 <listitem>
433 <para>DLL-creation mode (Windows only). See <xref
434 linkend="win32-dlls-create"/>.</para>
435 </listitem>
436 </varlistentry>
437
438 <varlistentry>
439 <term>
440 <cmdsynopsis>
441 <command>ghc --help</command> <command>ghc -?</command>
442 </cmdsynopsis>
443 <indexterm><primary><option>--help</option></primary></indexterm>
444 </term>
445 <listitem>
446 <para>Cause GHC to spew a long usage message to standard
447 output and then exit.</para>
448 </listitem>
449 </varlistentry>
450
451 <varlistentry>
452 <term>
453 <cmdsynopsis>
454 <command>ghc --show-iface <replaceable>file</replaceable></command>
455 </cmdsynopsis>
456 <indexterm><primary><option>--show-iface</option></primary></indexterm>
457 </term>
458 <listitem>
459 <para>Read the interface in
460 <replaceable>file</replaceable> and dump it as text to
461 <literal>stdout</literal>. For example <literal>ghc --show-iface M.hi</literal>.</para>
462 </listitem>
463 </varlistentry>
464
465 <varlistentry>
466 <term>
467 <cmdsynopsis>
468 <command>ghc --supported-extensions</command>
469 <command>ghc --supported-languages</command>
470 </cmdsynopsis>
471 <indexterm><primary><option>--supported-extensions</option></primary><primary><option>--supported-languages</option></primary></indexterm>
472 </term>
473 <listitem>
474 <para>Print the supported language extensions.</para>
475 </listitem>
476 </varlistentry>
477
478 <varlistentry>
479 <term>
480 <cmdsynopsis>
481 <command>ghc --show-options</command>
482 </cmdsynopsis>
483 <indexterm><primary><option>--show-options</option></primary></indexterm>
484 </term>
485 <listitem>
486 <para>Print the supported command line options. This flag can be used for autocompletion in a shell.</para>
487 </listitem>
488 </varlistentry>
489
490 <varlistentry>
491 <term>
492 <cmdsynopsis>
493 <command>ghc --info</command>
494 </cmdsynopsis>
495 <indexterm><primary><option>--info</option></primary></indexterm>
496 </term>
497 <listitem>
498 <para>Print information about the compiler.</para>
499 </listitem>
500 </varlistentry>
501
502 <varlistentry>
503 <term>
504 <cmdsynopsis>
505 <command>ghc --version</command>
506 <command>ghc -V</command>
507 </cmdsynopsis>
508 <indexterm><primary><option>-V</option></primary></indexterm>
509 <indexterm><primary><option>--version</option></primary></indexterm>
510 </term>
511 <listitem>
512 <para>Print a one-line string including GHC's version number.</para>
513 </listitem>
514 </varlistentry>
515
516 <varlistentry>
517 <term>
518 <cmdsynopsis>
519 <command>ghc --numeric-version</command>
520 </cmdsynopsis>
521 <indexterm><primary><option>--numeric-version</option></primary></indexterm>
522 </term>
523 <listitem>
524 <para>Print GHC's numeric version number only.</para>
525 </listitem>
526 </varlistentry>
527
528 <varlistentry>
529 <term>
530 <cmdsynopsis>
531 <command>ghc --print-libdir</command>
532 </cmdsynopsis>
533 <indexterm><primary><option>--print-libdir</option></primary></indexterm>
534 </term>
535 <listitem>
536 <para>Print the path to GHC's library directory. This is
537 the top of the directory tree containing GHC's libraries,
538 interfaces, and include files (usually something like
539 <literal>/usr/local/lib/ghc-5.04</literal> on Unix). This
540 is the value of
541 <literal>$libdir</literal><indexterm><primary><literal>libdir</literal></primary></indexterm>
542 in the package configuration file
543 (see <xref linkend="packages"/>).</para>
544 </listitem>
545 </varlistentry>
546
547 </variablelist>
548
549 <sect2 id="make-mode">
550 <title>Using <command>ghc</command> <option>--make</option></title>
551 <indexterm><primary><option>--make</option></primary></indexterm>
552 <indexterm><primary>separate compilation</primary></indexterm>
553
554 <para>In this mode, GHC will build a multi-module Haskell program by following
555 dependencies from one or more root modules (usually just
556 <literal>Main</literal>). For example, if your
557 <literal>Main</literal> module is in a file called
558 <filename>Main.hs</filename>, you could compile and link the
559 program like this:</para>
560
561 <screen>
562 ghc --make Main.hs
563 </screen>
564
565 <para>
566 In fact, GHC enters make mode automatically if there are any
567 Haskell source files on the command line and no other mode is
568 specified, so in this case we could just type
569 </para>
570
571 <screen>
572 ghc Main.hs
573 </screen>
574
575 <para>Any number of source file names or module names may be
576 specified; GHC will figure out all the modules in the program by
577 following the imports from these initial modules. It will then
578 attempt to compile each module which is out of date, and
579 finally, if there is a <literal>Main</literal> module, the
580 program will also be linked into an executable.</para>
581
582 <para>The main advantages to using <literal>ghc
583 --make</literal> over traditional
584 <literal>Makefile</literal>s are:</para>
585
586 <itemizedlist>
587 <listitem>
588 <para>GHC doesn't have to be restarted for each compilation,
589 which means it can cache information between compilations.
590 Compiling a multi-module program with <literal>ghc
591 --make</literal> can be up to twice as fast as
592 running <literal>ghc</literal> individually on each source
593 file.</para>
594 </listitem>
595 <listitem>
596 <para>You don't have to write a <literal>Makefile</literal>.</para>
597 <indexterm><primary><literal>Makefile</literal>s</primary><secondary>avoiding</secondary></indexterm>
598 </listitem>
599 <listitem>
600 <para>GHC re-calculates the dependencies each time it is
601 invoked, so the dependencies never get out of sync with the
602 source.</para>
603 </listitem>
604 <listitem>
605 <para>Using the <literal>-j</literal> flag, you can compile
606 modules in parallel. Specify <literal>-jN</literal> to
607 compile <replaceable>N</replaceable> jobs in parallel.</para>
608 </listitem>
609 </itemizedlist>
610
611 <para>Any of the command-line options described in the rest of
612 this chapter can be used with
613 <option>--make</option>, but note that any options
614 you give on the command line will apply to all the source files
615 compiled, so if you want any options to apply to a single source
616 file only, you'll need to use an <literal>OPTIONS_GHC</literal>
617 pragma (see <xref linkend="source-file-options"/>).</para>
618
619 <para>If the program needs to be linked with additional objects
620 (say, some auxiliary C code), then the object files can be
621 given on the command line and GHC will include them when linking
622 the executable.</para>
623
624 <para>For backward compatibility with existing make scripts, when
625 used in combination with <option>-c</option>, the linking phase
626 is omitted (same as <option>--make</option>
627 <option>-no-link</option>).</para>
628
629 <para>Note that GHC can only follow dependencies if it has the
630 source file available, so if your program includes a module for
631 which there is no source file, even if you have an object and an
632 interface file for the module, then GHC will complain. The
633 exception to this rule is for package modules, which may or may
634 not have source files.</para>
635
636 <para>The source files for the program don't all need to be in
637 the same directory; the <option>-i</option> option can be used
638 to add directories to the search path (see <xref
639 linkend="search-path"/>).</para>
640 </sect2>
641
642 <sect2 id="eval-mode">
643 <title>Expression evaluation mode</title>
644
645 <para>This mode is very similar to interactive mode, except that
646 there is a single expression to evaluate which is specified on
647 the command line as an argument to the <option>-e</option>
648 option:</para>
649
650 <screen>
651 ghc -e <replaceable>expr</replaceable>
652 </screen>
653
654 <para>Haskell source files may be named on the command line, and
655 they will be loaded exactly as in interactive mode. The
656 expression is evaluated in the context of the loaded
657 modules.</para>
658
659 <para>For example, to load and run a Haskell program containing
660 a module <literal>Main</literal>, we might say</para>
661
662 <screen>
663 ghc -e Main.main Main.hs
664 </screen>
665
666 <para>or we can just use this mode to evaluate expressions in
667 the context of the <literal>Prelude</literal>:</para>
668
669 <screen>
670 $ ghc -e "interact (unlines.map reverse.lines)"
671 hello
672 olleh
673 </screen>
674 </sect2>
675
676 <sect2 id="options-order">
677 <title>Batch compiler mode</title>
678
679 <para>In <emphasis>batch mode</emphasis>, GHC will compile one or more source files
680 given on the command line.</para>
681
682 <para>The first phase to run is determined by each input-file
683 suffix, and the last phase is determined by a flag. If no
684 relevant flag is present, then go all the way through to linking.
685 This table summarises:</para>
686
687 <informaltable>
688 <tgroup cols="4">
689 <colspec align="left"/>
690 <colspec align="left"/>
691 <colspec align="left"/>
692 <colspec align="left"/>
693
694 <thead>
695 <row>
696 <entry>Phase of the compilation system</entry>
697 <entry>Suffix saying &ldquo;start here&rdquo;</entry>
698 <entry>Flag saying &ldquo;stop after&rdquo;</entry>
699 <entry>(suffix of) output file</entry>
700 </row>
701 </thead>
702 <tbody>
703 <row>
704 <entry>literate pre-processor</entry>
705 <entry><literal>.lhs</literal></entry>
706 <entry>-</entry>
707 <entry><literal>.hs</literal></entry>
708 </row>
709
710 <row>
711 <entry>C pre-processor (opt.) </entry>
712 <entry><literal>.hs</literal> (with
713 <option>-cpp</option>)</entry>
714 <entry><option>-E</option></entry>
715 <entry><literal>.hspp</literal></entry>
716 </row>
717
718 <row>
719 <entry>Haskell compiler</entry>
720 <entry><literal>.hs</literal></entry>
721 <entry><option>-C</option>, <option>-S</option></entry>
722 <entry><literal>.hc</literal>, <literal>.s</literal></entry>
723 </row>
724
725 <row>
726 <entry>C compiler (opt.)</entry>
727 <entry><literal>.hc</literal> or <literal>.c</literal></entry>
728 <entry><option>-S</option></entry>
729 <entry><literal>.s</literal></entry>
730 </row>
731
732 <row>
733 <entry>assembler</entry>
734 <entry><literal>.s</literal></entry>
735 <entry><option>-c</option></entry>
736 <entry><literal>.o</literal></entry>
737 </row>
738
739 <row>
740 <entry>linker</entry>
741 <entry><replaceable>other</replaceable></entry>
742 <entry>-</entry>
743 <entry><filename>a.out</filename></entry>
744 </row>
745 </tbody>
746 </tgroup>
747 </informaltable>
748
749 <indexterm><primary><option>-C</option></primary></indexterm>
750 <indexterm><primary><option>-E</option></primary></indexterm>
751 <indexterm><primary><option>-S</option></primary></indexterm>
752 <indexterm><primary><option>-c</option></primary></indexterm>
753
754 <para>Thus, a common invocation would be: </para>
755
756 <screen>
757 ghc -c Foo.hs
758 </screen>
759
760 <para>to compile the Haskell source file
761 <filename>Foo.hs</filename> to an object file
762 <filename>Foo.o</filename>.</para>
763
764 <para>Note: What the Haskell compiler proper produces depends on what
765 backend code generator is used. See <xref linkend="code-generators"/>
766 for more details.</para>
767
768 <para>Note: C pre-processing is optional, the
769 <option>-cpp</option><indexterm><primary><option>-cpp</option></primary></indexterm>
770 flag turns it on. See <xref linkend="c-pre-processor"/> for more
771 details.</para>
772
773 <para>Note: The option <option>-E</option><indexterm><primary>-E
774 option</primary></indexterm> runs just the pre-processing passes
775 of the compiler, dumping the result in a file.</para>
776
777 <para>Note: The option <option>-C</option> is only available when
778 GHC is built in unregisterised mode. See <xref linkend="unreg"/>
779 for more details.</para>
780
781 <sect3 id="overriding-suffixes">
782 <title>Overriding the default behaviour for a file</title>
783
784 <para>As described above, the way in which a file is processed by GHC
785 depends on its suffix. This behaviour can be overridden using the
786 <option>-x</option> option:</para>
787
788 <variablelist>
789 <varlistentry>
790 <term><option>-x</option> <replaceable>suffix</replaceable>
791 <indexterm><primary><option>-x</option></primary>
792 </indexterm></term>
793 <listitem>
794 <para>Causes all files following this option on the command
795 line to be processed as if they had the suffix
796 <replaceable>suffix</replaceable>. For example, to compile a
797 Haskell module in the file <literal>M.my-hs</literal>,
798 use <literal>ghc -c -x hs M.my-hs</literal>.</para>
799 </listitem>
800 </varlistentry>
801 </variablelist>
802 </sect3>
803
804 </sect2>
805 </sect1>
806
807 <sect1 id="options-help">
808 <title>Verbosity options</title>
809
810 <indexterm><primary>verbosity options</primary></indexterm>
811
812 <para>See also the <option>--help</option>, <option>--version</option>, <option>--numeric-version</option>,
813 and <option>--print-libdir</option> modes in <xref linkend="modes"/>.</para>
814 <variablelist>
815 <varlistentry>
816 <term>
817 <option>-v</option>
818 <indexterm><primary><option>-v</option></primary></indexterm>
819 </term>
820 <listitem>
821 <para>The <option>-v</option> option makes GHC
822 <emphasis>verbose</emphasis>: it reports its version number
823 and shows (on stderr) exactly how it invokes each phase of
824 the compilation system. Moreover, it passes the
825 <option>-v</option> flag to most phases; each reports its
826 version number (and possibly some other information).</para>
827
828 <para>Please, oh please, use the <option>-v</option> option
829 when reporting bugs! Knowing that you ran the right bits in
830 the right order is always the first thing we want to
831 verify.</para>
832 </listitem>
833 </varlistentry>
834
835 <varlistentry>
836 <term>
837 <option>-v</option><replaceable>n</replaceable>
838 <indexterm><primary><option>-v</option></primary></indexterm>
839 </term>
840 <listitem>
841 <para>To provide more control over the compiler's verbosity,
842 the <option>-v</option> flag takes an optional numeric
843 argument. Specifying <option>-v</option> on its own is
844 equivalent to <option>-v3</option>, and the other levels
845 have the following meanings:</para>
846
847 <variablelist>
848 <varlistentry>
849 <term><option>-v0</option></term>
850 <listitem>
851 <para>Disable all non-essential messages (this is the
852 default).</para>
853 </listitem>
854 </varlistentry>
855
856 <varlistentry>
857 <term><option>-v1</option></term>
858 <listitem>
859 <para>Minimal verbosity: print one line per
860 compilation (this is the default when
861 <option>--make</option> or
862 <option>--interactive</option> is on).</para>
863 </listitem>
864 </varlistentry>
865
866 <varlistentry>
867 <term><option>-v2</option></term>
868 <listitem>
869 <para>Print the name of each compilation phase as it
870 is executed. (equivalent to
871 <option>-dshow-passes</option>).</para>
872 </listitem>
873 </varlistentry>
874
875 <varlistentry>
876 <term><option>-v3</option></term>
877 <listitem>
878 <para>The same as <option>-v2</option>, except that in
879 addition the full command line (if appropriate) for
880 each compilation phase is also printed.</para>
881 </listitem>
882 </varlistentry>
883
884 <varlistentry>
885 <term><option>-v4</option></term>
886 <listitem>
887 <para>The same as <option>-v3</option> except that the
888 intermediate program representation after each
889 compilation phase is also printed (excluding
890 preprocessed and C/assembly files).</para>
891 </listitem>
892 </varlistentry>
893 </variablelist>
894 </listitem>
895 </varlistentry>
896
897
898 <varlistentry>
899 <term><option>--fprint-explicit-foralls, -fprint-explicit-kinds</option>
900 <indexterm><primary><option>-fprint-explicit-foralls</option></primary></indexterm>
901 <indexterm><primary><option>-fprint-explicit-kinds</option></primary></indexterm>
902 </term>
903 <listitem>
904 <para>These two flags control the way in which GHC displays types, in error messages and in GHCi.
905 Using <option>-fprint-explicit-foralls</option> makes GHC print explicit <literal>forall</literal>
906 quantification at the top level of a type; normally this is suppressed. For example, in GHCi:
907 <screen>
908 ghci> let f x = x
909 ghci> :t f
910 f :: a -> a
911 ghci> :set -fprint-explicit-foralls
912 ghci> :t f
913 f :: forall a. a -> a
914 </screen>
915 However, regardless of the flag setting, the quantifiers are printed under these circumstances:
916 <itemizedlist>
917 <listitem><para>For nested <literal>foralls</literal>, e.g.
918 <screen>
919 ghci> :t GHC.ST.runST
920 GHC.ST.runST :: (forall s. GHC.ST.ST s a) -> a
921 </screen>
922 </para></listitem>
923 <listitem><para>If any of the quantified type variables has a kind
924 that mentions a kind variable, e.g.
925 <screen>
926 ghci> :i Data.Type.Equality.sym
927 Data.Type.Equality.sym ::
928 forall (k :: BOX) (a :: k) (b :: k).
929 (a Data.Type.Equality.:~: b) -> b Data.Type.Equality.:~: a
930 -- Defined in Data.Type.Equality
931 </screen>
932 </para></listitem>
933 </itemizedlist>
934 </para>
935 <para>
936 Using <option>-fprint-explicit-kinds</option> makes GHC print kind arguments
937 in types, which are normally suppressed. This can be important when you are using kind polymorphism.
938 For example:
939 <screen>
940 ghci> :set -XPolyKinds
941 ghci> data T a = MkT
942 ghci> :t MkT
943 MkT :: forall (k :: BOX) (a :: k). T a
944 ghci> :set -fprint-explicit-foralls
945 ghci> :t MkT
946 MkT :: forall (k :: BOX) (a :: k). T k a
947 </screen>
948 </para>
949 </listitem>
950 </varlistentry>
951
952 <varlistentry>
953 <term><option>-ferror-spans</option>
954 <indexterm><primary><option>-ferror-spans</option></primary>
955 </indexterm>
956 </term>
957 <listitem>
958 <para>Causes GHC to emit the full source span of the
959 syntactic entity relating to an error message. Normally, GHC
960 emits the source location of the start of the syntactic
961 entity only.</para>
962
963 <para>For example:</para>
964
965 <screen>
966 test.hs:3:6: parse error on input `where'
967 </screen>
968
969 <para>becomes:</para>
970
971 <screen>
972 test296.hs:3:6-10: parse error on input `where'
973 </screen>
974
975 <para>And multi-line spans are possible too:</para>
976
977 <screen>
978 test.hs:(5,4)-(6,7):
979 Conflicting definitions for `a'
980 Bound at: test.hs:5:4
981 test.hs:6:7
982 In the binding group for: a, b, a
983 </screen>
984
985 <para>Note that line numbers start counting at one, but
986 column numbers start at zero. This choice was made to
987 follow existing convention (i.e. this is how Emacs does
988 it).</para>
989 </listitem>
990 </varlistentry>
991
992 <varlistentry>
993 <term><option>-H</option><replaceable>size</replaceable>
994 <indexterm><primary><option>-H</option></primary></indexterm>
995 </term>
996 <listitem>
997 <para>Set the minimum size of the heap to
998 <replaceable>size</replaceable>.
999 This option is equivalent to
1000 <literal>+RTS&nbsp;-H<replaceable>size</replaceable></literal>,
1001 see <xref linkend="rts-options-gc" />.
1002 </para>
1003 </listitem>
1004 </varlistentry>
1005
1006 <varlistentry>
1007 <term><option>-Rghc-timing</option>
1008 <indexterm><primary><option>-Rghc-timing</option></primary></indexterm>
1009 </term>
1010 <listitem>
1011 <para>Prints a one-line summary of timing statistics for the
1012 GHC run. This option is equivalent to
1013 <literal>+RTS&nbsp;-tstderr</literal>, see <xref
1014 linkend="rts-options-gc" />.
1015 </para>
1016 </listitem>
1017 </varlistentry>
1018 </variablelist>
1019 </sect1>
1020
1021 &separate;
1022
1023 <sect1 id="options-sanity">
1024 <title>Warnings and sanity-checking</title>
1025
1026 <indexterm><primary>sanity-checking options</primary></indexterm>
1027 <indexterm><primary>warnings</primary></indexterm>
1028
1029
1030 <para>GHC has a number of options that select which types of
1031 non-fatal error messages, otherwise known as warnings, can be
1032 generated during compilation. By default, you get a standard set
1033 of warnings which are generally likely to indicate bugs in your
1034 program. These are:
1035 <option>-fwarn-overlapping-patterns</option>,
1036 <option>-fwarn-warnings-deprecations</option>,
1037 <option>-fwarn-amp</option>,
1038 <option>-fwarn-deprecated-flags</option>,
1039 <option>-fwarn-unrecognised-pragmas</option>,
1040 <option>-fwarn-pointless-pragmas</option>,
1041 <option>-fwarn-duplicate-constraints</option>,
1042 <option>-fwarn-duplicate-exports</option>,
1043 <option>-fwarn-overflowed-literals</option>,
1044 <option>-fwarn-empty-enumerations</option>,
1045 <option>-fwarn-missing-fields</option>,
1046 <option>-fwarn-missing-methods</option>,
1047 <option>-fwarn-wrong-do-bind</option>,
1048 <option>-fwarn-unsupported-calling-conventions</option>,
1049 <option>-fwarn-dodgy-foreign-imports</option>,
1050 <option>-fwarn-inline-rule-shadowing</option>,
1051 <option>-fwarn-unsupported-llvm-version</option>, and
1052 <option>-fwarn-context-quantification</option>.
1053 The following flags are simple ways to select standard
1054 &ldquo;packages&rdquo; of warnings:
1055 </para>
1056
1057 <variablelist>
1058
1059 <varlistentry>
1060 <term><option>-W</option>:</term>
1061 <listitem>
1062 <indexterm><primary>-W option</primary></indexterm>
1063 <para>Provides the standard warnings plus
1064 <option>-fwarn-incomplete-patterns</option>,
1065 <option>-fwarn-dodgy-exports</option>,
1066 <option>-fwarn-dodgy-imports</option>,
1067 <option>-fwarn-unused-matches</option>,
1068 <option>-fwarn-unused-imports</option>, and
1069 <option>-fwarn-unused-binds</option>.</para>
1070 </listitem>
1071 </varlistentry>
1072
1073 <varlistentry>
1074 <term><option>-Wall</option>:</term>
1075 <listitem>
1076 <indexterm><primary><option>-Wall</option></primary></indexterm>
1077 <para>Turns on all warning options that indicate potentially
1078 suspicious code. The warnings that are
1079 <emphasis>not</emphasis> enabled by <option>-Wall</option>
1080 are
1081 <option>-fwarn-tabs</option>,
1082 <option>-fwarn-incomplete-uni-patterns</option>,
1083 <option>-fwarn-incomplete-record-updates</option>,
1084 <option>-fwarn-monomorphism-restriction</option>,
1085 <option>-fwarn-auto-orphans</option>,
1086 <option>-fwarn-implicit-prelude</option>,
1087 <option>-fwarn-missing-local-sigs</option>,
1088 <option>-fwarn-missing-import-lists</option>.</para>
1089 </listitem>
1090 </varlistentry>
1091
1092 <varlistentry>
1093 <term><option>-w</option>:</term>
1094 <listitem>
1095 <indexterm><primary><option>-w</option></primary></indexterm>
1096 <para>Turns off all warnings, including the standard ones and
1097 those that <literal>-Wall</literal> doesn't enable.</para>
1098 </listitem>
1099 </varlistentry>
1100
1101 <varlistentry>
1102 <term><option>-Werror</option>:</term>
1103 <listitem>
1104 <indexterm><primary><option>-Werror</option></primary></indexterm>
1105 <para>Makes any warning into a fatal error. Useful so that you don't
1106 miss warnings when doing batch compilation. </para>
1107 </listitem>
1108 </varlistentry>
1109
1110 <varlistentry>
1111 <term><option>-Wwarn</option>:</term>
1112 <listitem>
1113 <indexterm><primary><option>-Wwarn</option></primary></indexterm>
1114 <para>Warnings are treated only as warnings, not as errors. This is
1115 the default, but can be useful to negate a
1116 <option>-Werror</option> flag.</para>
1117 </listitem>
1118 </varlistentry>
1119
1120 </variablelist>
1121
1122 <para>The full set of warning options is described below. To turn
1123 off any warning, simply give the corresponding
1124 <option>-fno-warn-...</option> option on the command line.</para>
1125
1126 <variablelist>
1127
1128 <varlistentry>
1129 <term><option>-fwarn-typed-holes</option>:</term>
1130 <listitem>
1131 <indexterm><primary><option>-fwarn-typed-holes</option></primary>
1132 </indexterm>
1133 <indexterm><primary>warnings</primary></indexterm>
1134 <para>
1135 Determines whether the compiler reports typed holes warnings. Has
1136 no effect unless typed holes errors are deferred until runtime.
1137 See <xref linkend="typed-holes"/> and <xref linkend="defer-type-errors"/>
1138 </para>
1139
1140 <para>This warning is on by default.</para>
1141 </listitem>
1142 </varlistentry>
1143
1144
1145 <varlistentry>
1146 <term><option>-fdefer-type-errors</option>:</term>
1147 <listitem>
1148 <indexterm><primary><option>-fdefer-type-errors</option></primary>
1149 </indexterm>
1150 <indexterm><primary>warnings</primary></indexterm>
1151 <para>Defer as many type errors as possible until runtime.
1152 At compile time you get a warning (instead of an error). At
1153 runtime, if you use a value that depends on a type error, you
1154 get a runtime error; but you can run any type-correct parts of your code
1155 just fine. See <xref linkend="defer-type-errors"/></para>
1156 </listitem>
1157 </varlistentry>
1158
1159 <varlistentry>
1160 <term><option>-fdefer-typed-holes</option>:</term>
1161 <listitem>
1162 <indexterm><primary><option>-fdefer-typed-holes</option></primary>
1163 </indexterm>
1164 <indexterm><primary>warnings</primary></indexterm>
1165 <para>
1166 Defer typed holes errors until runtime. This will turn the errors
1167 produced by <link linked="typed-holes">typed holes</link> into
1168 warnings. Using a value that depends on a typed hole produces a
1169 runtime error, the same as <option>-fdefer-type-errors</option>
1170 (which implies this option). See <xref linkend="typed-holes"/>
1171 and <xref linkend="defer-type-errors"/>.
1172 </para>
1173 <para>
1174 Implied by <option>-fdefer-type-errors</option>. See also
1175 <option>-fwarn-typed-holes</option>.
1176 </para>
1177 </listitem>
1178 </varlistentry>
1179
1180 <varlistentry>
1181 <term><option>-fwarn-partial-type-signatures</option>:</term>
1182 <listitem>
1183 <indexterm><primary><option>-fwarn-partial-type-signatures</option></primary>
1184 </indexterm>
1185 <indexterm><primary>warnings</primary></indexterm>
1186 <para>
1187 Determines whether the compiler reports holes in partial type
1188 signatures as warnings. Has no effect unless
1189 <option>-XPartialTypeSignatures</option> is enabled, which
1190 controls whether errors should be generated for holes in types
1191 or not. See <xref linkend="partial-type-signatures"/>.
1192 </para>
1193
1194 <para>This warning is on by default.</para>
1195 </listitem>
1196 </varlistentry>
1197
1198 <varlistentry>
1199 <term><option>-fhelpful-errors</option>:</term>
1200 <listitem>
1201 <indexterm><primary><option>-fhelpful-errors</option></primary>
1202 </indexterm>
1203 <indexterm><primary>warnings</primary></indexterm>
1204 <para>When a name or package is not found in scope, make
1205 suggestions for the name or package you might have meant instead.</para>
1206 <para>This option is on by default.</para>
1207 </listitem>
1208 </varlistentry>
1209
1210 <varlistentry>
1211 <term><option>-fwarn-unrecognised-pragmas</option>:</term>
1212 <listitem>
1213 <indexterm><primary><option>-fwarn-unrecognised-pragmas</option></primary>
1214 </indexterm>
1215 <indexterm><primary>warnings</primary></indexterm>
1216 <indexterm><primary>pragmas</primary></indexterm>
1217 <para>Causes a warning to be emitted when a
1218 pragma that GHC doesn't recognise is used. As well as pragmas
1219 that GHC itself uses, GHC also recognises pragmas known to be used
1220 by other tools, e.g. <literal>OPTIONS_HUGS</literal> and
1221 <literal>DERIVE</literal>.</para>
1222
1223 <para>This option is on by default.</para>
1224 </listitem>
1225 </varlistentry>
1226
1227 <varlistentry>
1228 <term><option>-fwarn-pointless-pragmas</option>:</term>
1229 <listitem>
1230 <indexterm><primary><option>-fwarn-pointless-pragmas</option></primary>
1231 </indexterm>
1232 <indexterm><primary>warnings</primary></indexterm>
1233 <indexterm><primary>pragmas</primary></indexterm>
1234 <para>Causes a warning to be emitted when GHC detects that a
1235 module contains a pragma that has no effect.</para>
1236
1237 <para>This option is on by default.</para>
1238 </listitem>
1239 </varlistentry>
1240
1241 <varlistentry>
1242 <term><option>-fwarn-warnings-deprecations</option>:</term>
1243 <listitem>
1244 <indexterm><primary><option>-fwarn-warnings-deprecations</option></primary>
1245 </indexterm>
1246 <indexterm><primary>warnings</primary></indexterm>
1247 <indexterm><primary>deprecations</primary></indexterm>
1248 <para>Causes a warning to be emitted when a
1249 module, function or type with a WARNING or DEPRECATED pragma
1250 is used. See <xref linkend="warning-deprecated-pragma"/> for more
1251 details on the pragmas.</para>
1252
1253 <para>This option is on by default.</para>
1254 </listitem>
1255 </varlistentry>
1256
1257 <varlistentry>
1258 <term><option>-fwarn-amp</option>:</term>
1259 <listitem>
1260 <indexterm><primary><option>-fwarn-amp</option></primary>
1261 </indexterm>
1262 <indexterm><primary>amp</primary></indexterm>
1263 <indexterm><primary>applicative-monad proposal</primary></indexterm>
1264 <para>Causes a warning to be emitted when a definition
1265 is in conflict with the AMP (Applicative-Monad proosal),
1266 namely:
1267 1. Instance of Monad without Applicative;
1268 2. Instance of MonadPlus without Alternative;
1269 3. Custom definitions of join/pure/&lt;*&gt;</para>
1270
1271 <para>This option is on by default.</para>
1272 </listitem>
1273 </varlistentry>
1274
1275 <varlistentry>
1276 <term><option>-fwarn-deprecated-flags</option>:</term>
1277 <listitem>
1278 <indexterm><primary><option>-fwarn-deprecated-flags</option></primary>
1279 </indexterm>
1280 <indexterm><primary>deprecated-flags</primary></indexterm>
1281 <para>Causes a warning to be emitted when a deprecated
1282 commandline flag is used.</para>
1283
1284 <para>This option is on by default.</para>
1285 </listitem>
1286 </varlistentry>
1287
1288 <varlistentry>
1289 <term><option>-fwarn-unsupported-calling-conventions</option>:</term>
1290 <listitem>
1291 <indexterm><primary><option>-fwarn-unsupported-calling-conventions</option></primary>
1292 </indexterm>
1293 <para>Causes a warning to be emitted for foreign declarations
1294 that use unsupported calling conventions. In particular,
1295 if the <literal>stdcall</literal> calling convention is used
1296 on an architecture other than i386 then it will be treated
1297 as <literal>ccall</literal>.</para>
1298 </listitem>
1299 </varlistentry>
1300
1301 <varlistentry>
1302 <term><option>-fwarn-dodgy-foreign-imports</option>:</term>
1303 <listitem>
1304 <indexterm><primary><option>-fwarn-dodgy-foreign-imports</option></primary>
1305 </indexterm>
1306 <para>Causes a warning to be emitted for foreign imports of
1307 the following form:</para>
1308
1309 <programlisting>
1310 foreign import "f" f :: FunPtr t
1311 </programlisting>
1312
1313 <para>on the grounds that it probably should be</para>
1314
1315 <programlisting>
1316 foreign import "&amp;f" f :: FunPtr t
1317 </programlisting>
1318
1319 <para>The first form declares that `f` is a (pure) C
1320 function that takes no arguments and returns a pointer to a
1321 C function with type `t`, whereas the second form declares
1322 that `f` itself is a C function with type `t`. The first
1323 declaration is usually a mistake, and one that is hard to
1324 debug because it results in a crash, hence this
1325 warning.</para>
1326 </listitem>
1327 </varlistentry>
1328
1329 <varlistentry>
1330 <term><option>-fwarn-dodgy-exports</option>:</term>
1331 <listitem>
1332 <indexterm><primary><option>-fwarn-dodgy-exports</option></primary>
1333 </indexterm>
1334 <para>Causes a warning to be emitted when a datatype
1335 <literal>T</literal> is exported
1336 with all constructors, i.e. <literal>T(..)</literal>, but is it
1337 just a type synonym.</para>
1338 <para>Also causes a warning to be emitted when a module is
1339 re-exported, but that module exports nothing.</para>
1340 </listitem>
1341 </varlistentry>
1342
1343 <varlistentry>
1344 <term><option>-fwarn-dodgy-imports</option>:</term>
1345 <listitem>
1346 <indexterm><primary><option>-fwarn-dodgy-imports</option></primary>
1347 </indexterm>
1348 <para>Causes a warning to be emitted in the following cases:</para>
1349 <itemizedlist>
1350 <listitem>
1351 <para>When a datatype <literal>T</literal> is imported with all
1352 constructors, i.e. <literal>T(..)</literal>, but has been
1353 exported abstractly, i.e. <literal>T</literal>.
1354 </para>
1355 </listitem>
1356 <listitem>
1357 <para>When an <literal>import</literal> statement hides an
1358 entity that is not exported.</para>
1359 </listitem>
1360 </itemizedlist>
1361 </listitem>
1362 </varlistentry>
1363
1364 <varlistentry>
1365 <term><option>-fwarn-overflowed-literals</option>:</term>
1366 <listitem>
1367 <indexterm><primary><option>-fwarn-overflowed-literals</option></primary>
1368 </indexterm>
1369 <para>
1370 Causes a warning to be emitted if a literal will overflow,
1371 e.g. <literal>300 :: Word8</literal>.
1372 </para>
1373 </listitem>
1374 </varlistentry>
1375
1376 <varlistentry>
1377 <term><option>-fwarn-empty-enumerations</option>:</term>
1378 <listitem>
1379 <indexterm><primary><option>-fwarn-empty-enumerations</option></primary>
1380 </indexterm>
1381 <para>
1382 Causes a warning to be emitted if an enumeration is
1383 empty, e.g. <literal>[5 .. 3]</literal>.
1384 </para>
1385 </listitem>
1386 </varlistentry>
1387
1388 <varlistentry>
1389 <term><option>-fwarn-lazy-unlifted-bindings</option>:</term>
1390 <listitem>
1391 <indexterm><primary><option>-fwarn-lazy-unlifted-bindings</option></primary>
1392 </indexterm>
1393 <para>This flag is a no-op, and will be removed in GHC 7.10.</para>
1394 </listitem>
1395 </varlistentry>
1396
1397 <varlistentry>
1398 <term><option>-fwarn-duplicate-constraints</option>:</term>
1399 <listitem>
1400 <indexterm><primary><option>-fwarn-duplicate-constraints</option></primary></indexterm>
1401 <indexterm><primary>duplicate constraints, warning</primary></indexterm>
1402
1403 <para>Have the compiler warn about duplicate constraints in a type signature. For
1404 example
1405 <programlisting>
1406 f :: (Eq a, Show a, Eq a) => a -> a
1407 </programlisting>
1408 The warning will indicate the duplicated <literal>Eq a</literal> constraint.
1409 </para>
1410
1411 <para>This option is on by default.</para>
1412 </listitem>
1413 </varlistentry>
1414
1415 <varlistentry>
1416 <term><option>-fwarn-duplicate-exports</option>:</term>
1417 <listitem>
1418 <indexterm><primary><option>-fwarn-duplicate-exports</option></primary></indexterm>
1419 <indexterm><primary>duplicate exports, warning</primary></indexterm>
1420 <indexterm><primary>export lists, duplicates</primary></indexterm>
1421
1422 <para>Have the compiler warn about duplicate entries in
1423 export lists. This is useful information if you maintain
1424 large export lists, and want to avoid the continued export
1425 of a definition after you've deleted (one) mention of it in
1426 the export list.</para>
1427
1428 <para>This option is on by default.</para>
1429 </listitem>
1430 </varlistentry>
1431
1432 <varlistentry>
1433 <term><option>-fwarn-hi-shadowing</option>:</term>
1434 <listitem>
1435 <indexterm><primary><option>-fwarn-hi-shadowing</option></primary></indexterm>
1436 <indexterm><primary>shadowing</primary>
1437 <secondary>interface files</secondary></indexterm>
1438
1439 <para>Causes the compiler to emit a warning when a module or
1440 interface file in the current directory is shadowing one
1441 with the same module name in a library or other
1442 directory.</para>
1443 </listitem>
1444 </varlistentry>
1445
1446 <varlistentry>
1447 <term><option>-fwarn-identities</option>:</term>
1448 <listitem>
1449 <indexterm><primary><option>-fwarn-identities</option></primary></indexterm>
1450 <para>Causes the compiler to emit a warning when a Prelude numeric
1451 conversion converts a type T to the same type T; such calls
1452 are probably no-ops and can be omitted. The functions checked for
1453 are: <literal>toInteger</literal>,
1454 <literal>toRational</literal>,
1455 <literal>fromIntegral</literal>,
1456 and <literal>realToFrac</literal>.
1457 </para>
1458 </listitem>
1459 </varlistentry>
1460
1461 <varlistentry>
1462 <term><option>-fwarn-implicit-prelude</option>:</term>
1463 <listitem>
1464 <indexterm><primary><option>-fwarn-implicit-prelude</option></primary></indexterm>
1465 <indexterm><primary>implicit prelude, warning</primary></indexterm>
1466 <para>Have the compiler warn if the Prelude is implicitly
1467 imported. This happens unless either the Prelude module is
1468 explicitly imported with an <literal>import ... Prelude ...</literal>
1469 line, or this implicit import is disabled (either by
1470 <option>-XNoImplicitPrelude</option> or a
1471 <literal>LANGUAGE NoImplicitPrelude</literal> pragma).</para>
1472
1473 <para>Note that no warning is given for syntax that implicitly
1474 refers to the Prelude, even if <option>-XNoImplicitPrelude</option>
1475 would change whether it refers to the Prelude.
1476 For example, no warning is given when
1477 <literal>368</literal> means
1478 <literal>Prelude.fromInteger (368::Prelude.Integer)</literal>
1479 (where <literal>Prelude</literal> refers to the actual Prelude module,
1480 regardless of the imports of the module being compiled).</para>
1481
1482 <para>This warning is off by default.</para>
1483 </listitem>
1484 </varlistentry>
1485
1486 <varlistentry>
1487 <term><option>-fwarn-incomplete-patterns</option>,
1488 <option>-fwarn-incomplete-uni-patterns</option>:
1489 </term>
1490 <listitem>
1491 <indexterm><primary><option>-fwarn-incomplete-patterns</option></primary></indexterm>
1492 <indexterm><primary><option>-fwarn-incomplete-uni-patterns</option></primary></indexterm>
1493 <indexterm><primary>incomplete patterns, warning</primary></indexterm>
1494 <indexterm><primary>patterns, incomplete</primary></indexterm>
1495
1496 <para>The option <option>-fwarn-incomplete-patterns</option> warns
1497 about places where
1498 a pattern-match might fail at runtime.
1499 The function
1500 <function>g</function> below will fail when applied to
1501 non-empty lists, so the compiler will emit a warning about
1502 this when <option>-fwarn-incomplete-patterns</option> is
1503 enabled.
1504
1505 <programlisting>
1506 g [] = 2
1507 </programlisting>
1508
1509 This option isn't enabled by default because it can be
1510 a bit noisy, and it doesn't always indicate a bug in the
1511 program. However, it's generally considered good practice
1512 to cover all the cases in your functions, and it is switched
1513 on by <option>-W</option>.</para>
1514
1515 <para>The flag <option>-fwarn-incomplete-uni-patterns</option> is
1516 similar, except that it
1517 applies only to lambda-expressions and pattern bindings, constructs
1518 that only allow a single pattern:
1519
1520 <programlisting>
1521 h = \[] -> 2
1522 Just k = f y
1523 </programlisting>
1524
1525 </para>
1526 </listitem>
1527 </varlistentry>
1528
1529 <varlistentry>
1530 <term><option>-fwarn-incomplete-record-updates</option>:</term>
1531 <listitem>
1532 <indexterm><primary><option>-fwarn-incomplete-record-updates</option></primary></indexterm>
1533 <indexterm><primary>incomplete record updates, warning</primary></indexterm>
1534 <indexterm><primary>record updates, incomplete</primary></indexterm>
1535
1536 <para>The function
1537 <function>f</function> below will fail when applied to
1538 <literal>Bar</literal>, so the compiler will emit a warning about
1539 this when <option>-fwarn-incomplete-record-updates</option> is
1540 enabled.</para>
1541
1542 <programlisting>
1543 data Foo = Foo { x :: Int }
1544 | Bar
1545
1546 f :: Foo -> Foo
1547 f foo = foo { x = 6 }
1548 </programlisting>
1549
1550 <para>This option isn't enabled by default because it can be
1551 very noisy, and it often doesn't indicate a bug in the
1552 program.</para>
1553 </listitem>
1554 </varlistentry>
1555
1556 <varlistentry>
1557 <term>
1558 <option>-fwarn-missing-fields</option>:
1559 <indexterm><primary><option>-fwarn-missing-fields</option></primary></indexterm>
1560 <indexterm><primary>missing fields, warning</primary></indexterm>
1561 <indexterm><primary>fields, missing</primary></indexterm>
1562 </term>
1563 <listitem>
1564
1565 <para>This option is on by default, and warns you whenever
1566 the construction of a labelled field constructor isn't
1567 complete, missing initializers for one or more fields. While
1568 not an error (the missing fields are initialised with
1569 bottoms), it is often an indication of a programmer error.</para>
1570 </listitem>
1571 </varlistentry>
1572
1573 <varlistentry>
1574 <term>
1575 <option>-fwarn-missing-import-lists</option>:
1576 <indexterm><primary><option>-fwarn-import-lists</option></primary></indexterm>
1577 <indexterm><primary>missing import lists, warning</primary></indexterm>
1578 <indexterm><primary>import lists, missing</primary></indexterm>
1579 </term>
1580 <listitem>
1581
1582 <para>This flag warns if you use an unqualified
1583 <literal>import</literal> declaration
1584 that does not explicitly list the entities brought into scope. For
1585 example
1586 </para>
1587
1588 <programlisting>
1589 module M where
1590 import X( f )
1591 import Y
1592 import qualified Z
1593 p x = f x x
1594 </programlisting>
1595
1596 <para>
1597 The <option>-fwarn-import-lists</option> flag will warn about the import
1598 of <literal>Y</literal> but not <literal>X</literal>
1599 If module <literal>Y</literal> is later changed to export (say) <literal>f</literal>,
1600 then the reference to <literal>f</literal> in <literal>M</literal> will become
1601 ambiguous. No warning is produced for the import of <literal>Z</literal>
1602 because extending <literal>Z</literal>'s exports would be unlikely to produce
1603 ambiguity in <literal>M</literal>.
1604 </para>
1605 </listitem>
1606 </varlistentry>
1607
1608 <varlistentry>
1609 <term><option>-fwarn-missing-methods</option>:</term>
1610 <listitem>
1611 <indexterm><primary><option>-fwarn-missing-methods</option></primary></indexterm>
1612 <indexterm><primary>missing methods, warning</primary></indexterm>
1613 <indexterm><primary>methods, missing</primary></indexterm>
1614
1615 <para>This option is on by default, and warns you whenever
1616 an instance declaration is missing one or more methods, and
1617 the corresponding class declaration has no default
1618 declaration for them.</para>
1619 <para>The warning is suppressed if the method name
1620 begins with an underscore. Here's an example where this is useful:
1621 <programlisting>
1622 class C a where
1623 _simpleFn :: a -> String
1624 complexFn :: a -> a -> String
1625 complexFn x y = ... _simpleFn ...
1626 </programlisting>
1627 The idea is that: (a) users of the class will only call <literal>complexFn</literal>;
1628 never <literal>_simpleFn</literal>; and (b)
1629 instance declarations can define either <literal>complexFn</literal> or <literal>_simpleFn</literal>.
1630 </para>
1631 <para>The MINIMAL pragma can be used to change which combination of methods will be required for instances of a particular class. See <xref linkend="minimal-pragma"/>.</para>
1632 </listitem>
1633 </varlistentry>
1634
1635 <varlistentry>
1636 <term><option>-fwarn-missing-signatures</option>:</term>
1637 <listitem>
1638 <indexterm><primary><option>-fwarn-missing-signatures</option></primary></indexterm>
1639 <indexterm><primary>type signatures, missing</primary></indexterm>
1640
1641 <para>If you would like GHC to check that every top-level
1642 function/value has a type signature, use the
1643 <option>-fwarn-missing-signatures</option> option. As part of
1644 the warning GHC also reports the inferred type. The
1645 option is off by default.</para>
1646 </listitem>
1647 </varlistentry>
1648
1649 <varlistentry>
1650 <term><option>-fwarn-missing-exported-sigs</option>:</term>
1651 <listitem>
1652 <indexterm><primary><option>-fwarn-missing-exported-sigs</option></primary></indexterm>
1653 <indexterm><primary>type signatures, missing</primary></indexterm>
1654
1655 <para>If you would like GHC to check that every exported top-level
1656 function/value has a type signature, but not check unexported values, use the
1657 <option>-fwarn-missing-exported-sigs</option> option. This option
1658 takes precedence over <option>-fwarn-missing-signatures</option>.
1659 As part of the warning GHC also reports the inferred type. The
1660 option is off by default.</para>
1661 </listitem>
1662 </varlistentry>
1663
1664 <varlistentry>
1665 <term><option>-fwarn-missing-local-sigs</option>:</term>
1666 <listitem>
1667 <indexterm><primary><option>-fwarn-missing-local-sigs</option></primary></indexterm>
1668 <indexterm><primary>type signatures, missing</primary></indexterm>
1669
1670 <para>If you use the
1671 <option>-fwarn-missing-local-sigs</option> flag GHC will warn
1672 you about any polymorphic local bindings. As part of
1673 the warning GHC also reports the inferred type. The
1674 option is off by default.</para>
1675 </listitem>
1676 </varlistentry>
1677
1678 <varlistentry>
1679 <term><option>-fwarn-name-shadowing</option>:</term>
1680 <listitem>
1681 <indexterm><primary><option>-fwarn-name-shadowing</option></primary></indexterm>
1682 <indexterm><primary>shadowing, warning</primary></indexterm>
1683
1684 <para>This option causes a warning to be emitted whenever an
1685 inner-scope value has the same name as an outer-scope value,
1686 i.e. the inner value shadows the outer one. This can catch
1687 typographical errors that turn into hard-to-find bugs, e.g.,
1688 in the inadvertent capture of what would be a recursive call in
1689 <literal>f = ... let f = id in ... f ...</literal>.</para>
1690 <para>The warning is suppressed for names beginning with an underscore. For example
1691 <programlisting>
1692 f x = do { _ignore &lt;- this; _ignore &lt;- that; return (the other) }
1693 </programlisting>
1694 </para>
1695 </listitem>
1696 </varlistentry>
1697
1698 <varlistentry>
1699 <term><option>-fwarn-orphans, -fwarn-auto-orphans</option>:</term>
1700 <listitem>
1701 <indexterm><primary><option>-fwarn-orphans</option></primary></indexterm>
1702 <indexterm><primary><option>-fwarn-auto-orphans</option></primary></indexterm>
1703 <indexterm><primary>orphan instances, warning</primary></indexterm>
1704 <indexterm><primary>orphan rules, warning</primary></indexterm>
1705
1706 <para>These flags cause a warning to be emitted whenever the
1707 module contains an "orphan" instance declaration or rewrite rule.
1708 An instance declaration is an orphan if it appears in a module in
1709 which neither the class nor the type being instanced are declared
1710 in the same module. A rule is an orphan if it is a rule for a
1711 function declared in another module. A module containing any
1712 orphans is called an orphan module.</para>
1713 <para>The trouble with orphans is that GHC must pro-actively read the interface
1714 files for all orphan modules, just in case their instances or rules
1715 play a role, whether or not the module's interface would otherwise
1716 be of any use. See <xref linkend="orphan-modules"/> for details.
1717 </para>
1718 <para>The flag <option>-fwarn-orphans</option> warns about user-written
1719 orphan rules or instances. The flag <option>-fwarn-auto-orphans</option>
1720 warns about automatically-generated orphan rules, notably as a result of
1721 specialising functions, for type classes (<literal>Specialise</literal>)
1722 or argument values (<literal>-fspec-constr</literal>).</para>
1723 </listitem>
1724 </varlistentry>
1725
1726 <varlistentry>
1727 <term>
1728 <option>-fwarn-overlapping-patterns</option>:
1729 <indexterm><primary><option>-fwarn-overlapping-patterns</option></primary></indexterm>
1730 <indexterm><primary>overlapping patterns, warning</primary></indexterm>
1731 <indexterm><primary>patterns, overlapping</primary></indexterm>
1732 </term>
1733 <listitem>
1734 <para>By default, the compiler will warn you if a set of
1735 patterns are overlapping, e.g.,</para>
1736
1737 <programlisting>
1738 f :: String -&#62; Int
1739 f [] = 0
1740 f (_:xs) = 1
1741 f "2" = 2
1742 </programlisting>
1743
1744 <para>where the last pattern match in <function>f</function>
1745 won't ever be reached, as the second pattern overlaps
1746 it. More often than not, redundant patterns is a programmer
1747 mistake/error, so this option is enabled by default.</para>
1748 </listitem>
1749 </varlistentry>
1750
1751 <varlistentry>
1752 <term><option>-fwarn-tabs</option>:</term>
1753 <listitem>
1754 <indexterm><primary><option>-fwarn-tabs</option></primary></indexterm>
1755 <indexterm><primary>tabs, warning</primary></indexterm>
1756 <para>Have the compiler warn if there are tabs in your source
1757 file.</para>
1758
1759 <para>This warning is off by default.</para>
1760 </listitem>
1761 </varlistentry>
1762
1763 <varlistentry>
1764 <term><option>-fwarn-type-defaults</option>:</term>
1765 <listitem>
1766 <indexterm><primary><option>-fwarn-type-defaults</option></primary></indexterm>
1767 <indexterm><primary>defaulting mechanism, warning</primary></indexterm>
1768 <para>Have the compiler warn/inform you where in your source
1769 the Haskell defaulting mechanism for numeric types kicks
1770 in. This is useful information when converting code from a
1771 context that assumed one default into one with another,
1772 e.g., the &lsquo;default default&rsquo; for Haskell 1.4 caused the
1773 otherwise unconstrained value <constant>1</constant> to be
1774 given the type <literal>Int</literal>, whereas Haskell 98
1775 and later
1776 defaults it to <literal>Integer</literal>. This may lead to
1777 differences in performance and behaviour, hence the
1778 usefulness of being non-silent about this.</para>
1779
1780 <para>This warning is off by default.</para>
1781 </listitem>
1782 </varlistentry>
1783
1784 <varlistentry>
1785 <term><option>-fwarn-monomorphism-restriction</option>:</term>
1786 <listitem>
1787 <indexterm><primary><option>-fwarn-monomorphism-restriction</option></primary></indexterm>
1788 <indexterm><primary>monomorphism restriction, warning</primary></indexterm>
1789 <para>Have the compiler warn/inform you where in your source
1790 the Haskell Monomorphism Restriction is applied. If applied silently
1791 the MR can give rise to unexpected behaviour, so it can be helpful
1792 to have an explicit warning that it is being applied.</para>
1793
1794 <para>This warning is off by default.</para>
1795 </listitem>
1796 </varlistentry>
1797
1798 <varlistentry>
1799 <term><option>-fwarn-unticked-promoted-constructors</option>:</term>
1800 <listitem>
1801 <indexterm><primary><option>-fwarn-unticked-promoted-constructors</option></primary></indexterm>
1802 <indexterm><primary>promoted constructor, warning</primary></indexterm>
1803 <para>Warn if a promoted data constructor is used without a tick preceding it's name.
1804 </para>
1805 <para>For example:
1806 </para>
1807 <programlisting>
1808 data Nat = Succ Nat | Zero
1809
1810 data Vec n s where
1811 Nil :: Vec Zero a
1812 Cons :: a -> Vec n a -> Vec (Succ n) a
1813 </programlisting>
1814 <para> Will raise two warnings because <function>Zero</function>
1815 and <function>Succ</function> are not written as <function>'Zero</function> and
1816 <function>'Succ</function>.
1817 </para>
1818 <para>This warning is off by default.</para>
1819 </listitem>
1820 </varlistentry>
1821
1822 <varlistentry>
1823 <term><option>-fwarn-unused-binds</option>:</term>
1824 <listitem>
1825 <indexterm><primary><option>-fwarn-unused-binds</option></primary></indexterm>
1826 <indexterm><primary>unused binds, warning</primary></indexterm>
1827 <indexterm><primary>binds, unused</primary></indexterm>
1828 <para>Report any function definitions (and local bindings)
1829 which are unused. More precisely:
1830
1831 <itemizedlist>
1832 <listitem><para>Warn if a binding brings into scope a variable that is not used,
1833 except if the variable's name starts with an underscore. The "starts-with-underscore"
1834 condition provides a way to selectively disable the warning.
1835 </para>
1836 <para>
1837 A variable is regarded as "used" if
1838 <itemizedlist>
1839 <listitem><para>It is exported, or</para></listitem>
1840 <listitem><para>It appears in the right hand side of a binding that binds at
1841 least one used variable that is used</para></listitem>
1842 </itemizedlist>
1843 For example
1844 <programlisting>
1845 module A (f) where
1846 f = let (p,q) = rhs1 in t p -- Warning about unused q
1847 t = rhs3 -- No warning: f is used, and hence so is t
1848 g = h x -- Warning: g unused
1849 h = rhs2 -- Warning: h is only used in the right-hand side of another unused binding
1850 _w = True -- No warning: _w starts with an underscore
1851 </programlisting>
1852 </para></listitem>
1853
1854 <listitem><para>
1855 Warn if a pattern binding binds no variables at all, unless it is a lone, possibly-banged, wild-card pattern.
1856 For example:
1857 <programlisting>
1858 Just _ = rhs3 -- Warning: unused pattern binding
1859 (_, _) = rhs4 -- Warning: unused pattern binding
1860 _ = rhs3 -- No warning: lone wild-card pattern
1861 !_ = rhs4 -- No warning: banged wild-card pattern; behaves like seq
1862 </programlisting>
1863 The motivation for allowing lone wild-card patterns is they
1864 are not very different from <literal>_v = rhs3</literal>,
1865 which elicits no warning; and they can be useful to add a type
1866 constraint, e.g. <literal>_ = x::Int</literal>. A lone
1867 banged wild-card pattern is is useful as an alternative
1868 (to <literal>seq</literal>) way to force evaluation.
1869 </para>
1870 </listitem>
1871 </itemizedlist>
1872 </para>
1873 </listitem>
1874 </varlistentry>
1875
1876 <varlistentry>
1877 <term><option>-fwarn-unused-imports</option>:</term>
1878 <listitem>
1879 <indexterm><primary><option>-fwarn-unused-imports</option></primary></indexterm>
1880 <indexterm><primary>unused imports, warning</primary></indexterm>
1881 <indexterm><primary>imports, unused</primary></indexterm>
1882
1883 <para>Report any modules that are explicitly imported but
1884 never used. However, the form <literal>import M()</literal> is
1885 never reported as an unused import, because it is a useful idiom
1886 for importing instance declarations, which are anonymous in Haskell.</para>
1887 </listitem>
1888 </varlistentry>
1889
1890 <varlistentry>
1891 <term><option>-fwarn-unused-matches</option>:</term>
1892 <listitem>
1893 <indexterm><primary><option>-fwarn-unused-matches</option></primary></indexterm>
1894 <indexterm><primary>unused matches, warning</primary></indexterm>
1895 <indexterm><primary>matches, unused</primary></indexterm>
1896
1897 <para>Report all unused variables which arise from pattern
1898 matches, including patterns consisting of a single variable.
1899 For instance <literal>f x y = []</literal> would report
1900 <varname>x</varname> and <varname>y</varname> as unused. The
1901 warning is suppressed if the variable name begins with an underscore, thus:
1902 <programlisting>
1903 f _x = True
1904 </programlisting>
1905 </para>
1906 </listitem>
1907 </varlistentry>
1908
1909 <varlistentry>
1910 <term><option>-fwarn-unused-do-bind</option>:</term>
1911 <listitem>
1912 <indexterm><primary><option>-fwarn-unused-do-bind</option></primary></indexterm>
1913 <indexterm><primary>unused do binding, warning</primary></indexterm>
1914 <indexterm><primary>do binding, unused</primary></indexterm>
1915
1916 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1917 that appear to silently throw information away.
1918 For instance <literal>do { mapM popInt xs ; return 10 }</literal> would report
1919 the first statement in the <literal>do</literal> block as suspicious,
1920 as it has the type <literal>StackM [Int]</literal> and not <literal>StackM ()</literal>, but that
1921 <literal>[Int]</literal> value is not bound to anything. The warning is suppressed by
1922 explicitly mentioning in the source code that your program is throwing something away:
1923 <programlisting>
1924 do { _ &lt;- mapM popInt xs ; return 10 }
1925 </programlisting>
1926 Of course, in this particular situation you can do even better:
1927 <programlisting>
1928 do { mapM_ popInt xs ; return 10 }
1929 </programlisting>
1930 </para>
1931 </listitem>
1932 </varlistentry>
1933
1934 <varlistentry>
1935 <term><option>-fwarn-context-quantification</option>:</term>
1936 <listitem>
1937 <indexterm><primary><option>-fwarn-context-quantification</option></primary></indexterm>
1938 <indexterm><primary>implicit context quantification, warning</primary></indexterm>
1939 <indexterm><primary>context, implicit quantification</primary></indexterm>
1940
1941 <para>Report if a variable is quantified only due to its presence
1942 in a context (see <xref linkend="universal-quantification"/>). For example,
1943 <programlisting>
1944 type T a = Monad m => a -> f a
1945 </programlisting>
1946 It is recommended to write this polymorphic type as
1947 <programlisting>
1948 type T a = forall m. Monad m => a -> f a
1949 </programlisting>
1950 instead.
1951 </para>
1952 </listitem>
1953 </varlistentry>
1954
1955 <varlistentry>
1956 <term><option>-fwarn-wrong-do-bind</option>:</term>
1957 <listitem>
1958 <indexterm><primary><option>-fwarn-wrong-do-bind</option></primary></indexterm>
1959 <indexterm><primary>apparently erroneous do binding, warning</primary></indexterm>
1960 <indexterm><primary>do binding, apparently erroneous</primary></indexterm>
1961
1962 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1963 that appear to lack a binding.
1964 For instance <literal>do { return (popInt 10) ; return 10 }</literal> would report
1965 the first statement in the <literal>do</literal> block as suspicious,
1966 as it has the type <literal>StackM (StackM Int)</literal> (which consists of two nested applications
1967 of the same monad constructor), but which is not then &quot;unpacked&quot; by binding the result.
1968 The warning is suppressed by explicitly mentioning in the source code that your program is throwing something away:
1969 <programlisting>
1970 do { _ &lt;- return (popInt 10) ; return 10 }
1971 </programlisting>
1972 For almost all sensible programs this will indicate a bug, and you probably intended to write:
1973 <programlisting>
1974 do { popInt 10 ; return 10 }
1975 </programlisting>
1976 </para>
1977 </listitem>
1978 </varlistentry>
1979
1980 <varlistentry>
1981 <term><option>-fwarn-inline-rule-shadowing</option>:</term>
1982 <listitem>
1983 <indexterm><primary><option>-fwarn-inline-rule-shadowing</option></primary></indexterm>
1984 <para>Warn if a rewrite RULE might fail to fire because the function might be
1985 inlined before the rule has a chance to fire. See <xref linkend="rules-inline"/>.
1986 </para>
1987 </listitem>
1988 </varlistentry>
1989
1990 </variablelist>
1991
1992 <para>If you're feeling really paranoid, the
1993 <option>-dcore-lint</option>
1994 option<indexterm><primary><option>-dcore-lint</option></primary></indexterm>
1995 is a good choice. It turns on heavyweight intra-pass
1996 sanity-checking within GHC. (It checks GHC's sanity, not
1997 yours.)</para>
1998
1999 </sect1>
2000
2001 &packages;
2002
2003 <sect1 id="options-optimise">
2004 <title>Optimisation (code improvement)</title>
2005
2006 <indexterm><primary>optimisation</primary></indexterm>
2007 <indexterm><primary>improvement, code</primary></indexterm>
2008
2009 <para>The <option>-O*</option> options specify convenient
2010 &ldquo;packages&rdquo; of optimisation flags; the
2011 <option>-f*</option> options described later on specify
2012 <emphasis>individual</emphasis> optimisations to be turned on/off;
2013 the <option>-m*</option> options specify
2014 <emphasis>machine-specific</emphasis> optimisations to be turned
2015 on/off.</para>
2016
2017 <sect2 id="optimise-pkgs">
2018 <title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.</title>
2019
2020 <para>There are <emphasis>many</emphasis> options that affect
2021 the quality of code produced by GHC. Most people only have a
2022 general goal, something like &ldquo;Compile quickly&rdquo; or
2023 &ldquo;Make my program run like greased lightning.&rdquo; The
2024 following &ldquo;packages&rdquo; of optimisations (or lack
2025 thereof) should suffice.</para>
2026
2027 <para>Note that higher optimisation levels cause more
2028 cross-module optimisation to be performed, which can have an
2029 impact on how much of your program needs to be recompiled when
2030 you change something. This is one reason to stick to
2031 no-optimisation when developing code.</para>
2032
2033 <variablelist>
2034
2035 <varlistentry>
2036 <term>
2037 No <option>-O*</option>-type option specified:
2038 <indexterm><primary>-O* not specified</primary></indexterm>
2039 </term>
2040 <listitem>
2041 <para>This is taken to mean: &ldquo;Please compile
2042 quickly; I'm not over-bothered about compiled-code
2043 quality.&rdquo; So, for example: <command>ghc -c
2044 Foo.hs</command></para>
2045 </listitem>
2046 </varlistentry>
2047
2048 <varlistentry>
2049 <term>
2050 <option>-O0</option>:
2051 <indexterm><primary><option>-O0</option></primary></indexterm>
2052 </term>
2053 <listitem>
2054 <para>Means &ldquo;turn off all optimisation&rdquo;,
2055 reverting to the same settings as if no
2056 <option>-O</option> options had been specified. Saying
2057 <option>-O0</option> can be useful if
2058 eg. <command>make</command> has inserted a
2059 <option>-O</option> on the command line already.</para>
2060 </listitem>
2061 </varlistentry>
2062
2063 <varlistentry>
2064 <term>
2065 <option>-O</option> or <option>-O1</option>:
2066 <indexterm><primary>-O option</primary></indexterm>
2067 <indexterm><primary>-O1 option</primary></indexterm>
2068 <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
2069 </term>
2070 <listitem>
2071 <para>Means: &ldquo;Generate good-quality code without
2072 taking too long about it.&rdquo; Thus, for example:
2073 <command>ghc -c -O Main.lhs</command></para>
2074 </listitem>
2075 </varlistentry>
2076
2077 <varlistentry>
2078 <term>
2079 <option>-O2</option>:
2080 <indexterm><primary>-O2 option</primary></indexterm>
2081 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
2082 </term>
2083 <listitem>
2084 <para>Means: &ldquo;Apply every non-dangerous
2085 optimisation, even if it means significantly longer
2086 compile times.&rdquo;</para>
2087
2088 <para>The avoided &ldquo;dangerous&rdquo; optimisations
2089 are those that can make runtime or space
2090 <emphasis>worse</emphasis> if you're unlucky. They are
2091 normally turned on or off individually.</para>
2092
2093 <para>At the moment, <option>-O2</option> is
2094 <emphasis>unlikely</emphasis> to produce better code than
2095 <option>-O</option>.</para>
2096 </listitem>
2097 </varlistentry>
2098
2099 <varlistentry>
2100 <term>
2101 <option>-Odph</option>:
2102 <indexterm><primary>-Odph</primary></indexterm>
2103 <indexterm><primary>optimise</primary><secondary>DPH</secondary></indexterm>
2104 </term>
2105 <listitem>
2106 <para>Enables all <option>-O2</option> optimisation, sets
2107 <option>-fmax-simplifier-iterations=20</option>
2108 and <option>-fsimplifier-phases=3</option>. Designed for use with
2109 <link linkend="dph">Data Parallel Haskell (DPH)</link>.</para>
2110 </listitem>
2111 </varlistentry>
2112
2113 </variablelist>
2114
2115 <para>We don't use a <option>-O*</option> flag for day-to-day
2116 work. We use <option>-O</option> to get respectable speed;
2117 e.g., when we want to measure something. When we want to go for
2118 broke, we tend to use <option>-O2</option> (and we go for
2119 lots of coffee breaks).</para>
2120
2121 <para>The easiest way to see what <option>-O</option> (etc.)
2122 &ldquo;really mean&rdquo; is to run with <option>-v</option>,
2123 then stand back in amazement.</para>
2124 </sect2>
2125
2126 <sect2 id="options-f">
2127 <title><option>-f*</option>: platform-independent flags</title>
2128
2129 <indexterm><primary>-f* options (GHC)</primary></indexterm>
2130 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
2131
2132 <para>These flags turn on and off individual optimisations.
2133 Flags marked as <emphasis>Enabled by default</emphasis> are
2134 enabled by <option>-O</option>, and as such you shouldn't
2135 need to set any of them explicitly. A flag <option>-fwombat</option>
2136 can be negated by saying <option>-fno-wombat</option>.
2137 See <xref linkend="options-f-compact"/> for a compact list.
2138 </para>
2139
2140 <variablelist>
2141 <varlistentry>
2142 <term>
2143 <option>-fcase-merge</option>
2144 <indexterm><primary><option></option></primary></indexterm>
2145 </term>
2146 <listitem>
2147 <para><emphasis>On by default.</emphasis>
2148 Merge immediately-nested case expressions that scrutinse the same variable. Example
2149 <programlisting>
2150 case x of
2151 Red -> e1
2152 _ -> case x of
2153 Blue -> e2
2154 Green -> e3
2155 ==>
2156 case x of
2157 Red -> e1
2158 Blue -> e2
2159 Green -> e2
2160 </programlisting>
2161 </para>
2162 </listitem>
2163 </varlistentry>
2164
2165 <varlistentry>
2166 <term>
2167 <option>-fcall-arity</option>
2168 <indexterm><primary><option>-fcall-arity</option></primary></indexterm>
2169 </term>
2170 <listitem>
2171 <para><emphasis>On by default.</emphasis>.
2172 </para>
2173 </listitem>
2174 </varlistentry>
2175
2176 <varlistentry>
2177 <term>
2178 <option>-fcmm-elim-common-blocks</option>
2179 <indexterm><primary><option>-felim-common-blocks</option></primary></indexterm>
2180 </term>
2181 <listitem>
2182 <para><emphasis>On by default.</emphasis>. Enables the common block
2183 elimination optimisation in the code generator. This optimisation
2184 attempts to find identical Cmm blocks and eliminate the duplicates.
2185 </para>
2186 </listitem>
2187 </varlistentry>
2188
2189 <varlistentry>
2190 <term>
2191 <option>-fcmm-sink</option>
2192 <indexterm><primary><option>-fcmm-sink</option></primary></indexterm>
2193 </term>
2194 <listitem>
2195 <para><emphasis>On by default.</emphasis>. Enables the sinking pass
2196 in the code generator. This optimisation
2197 attempts to find identical Cmm blocks and eliminate the duplicates
2198 attempts to move variable bindings closer to their usage sites. It
2199 also inlines simple expressions like literals or registers.
2200 </para>
2201 </listitem>
2202 </varlistentry>
2203
2204 <varlistentry>
2205 <term>
2206 <option>-fcpr-off</option>
2207 <indexterm><primary><option>-fcpr-Off</option></primary></indexterm>
2208 </term>
2209 <listitem>
2210 <para>Switch off CPR analysis in the demand analyser.
2211 </para>
2212 </listitem>
2213 </varlistentry>
2214
2215 <varlistentry>
2216 <term>
2217 <option>-fcse</option>
2218 <indexterm><primary><option>-fcse</option></primary></indexterm>
2219 </term>
2220 <listitem>
2221 <para><emphasis>On by default.</emphasis>. Enables the common-sub-expression
2222 elimination optimisation.
2223 Switching this off can be useful if you have some <literal>unsafePerformIO</literal>
2224 expressions that you don't want commoned-up.</para>
2225 </listitem>
2226 </varlistentry>
2227
2228 <varlistentry>
2229 <term>
2230 <option>-fdicts-cheap</option>
2231 <indexterm><primary><option></option></primary></indexterm>
2232 </term>
2233 <listitem>
2234 <para>A very experimental flag that makes dictionary-valued
2235 expressions seem cheap to the optimiser.
2236 </para>
2237 </listitem>
2238 </varlistentry>
2239
2240 <varlistentry>
2241 <term>
2242 <option>-fdicts-strict</option>
2243 <indexterm><primary><option></option></primary></indexterm>
2244 </term>
2245 <listitem>
2246 <para>Make dictionaries strict.
2247 </para>
2248 </listitem>
2249 </varlistentry>
2250
2251 <varlistentry>
2252 <term>
2253 <option>-fdmd-tx-dict-sel</option>
2254 <indexterm><primary><option>-fdmd-tx-dict-sel</option></primary></indexterm>
2255 </term>
2256 <listitem>
2257 <para><emphasis>On by default for <option>-O0</option>, <option>-O</option>,
2258 <option>-O2</option>.</emphasis>
2259 </para>
2260 <para>Use a special demand transformer for dictionary selectors.
2261 </para>
2262 </listitem>
2263 </varlistentry>
2264
2265 <varlistentry>
2266 <term>
2267 <option>-fdo-eta-reduction</option>
2268 <indexterm><primary><option></option></primary></indexterm>
2269 </term>
2270 <listitem>
2271 <para><emphasis>On by default.</emphasis>
2272 Eta-reduce lambda expressions, if doing so gets rid of a whole
2273 group of lambdas.
2274 </para>
2275 </listitem>
2276 </varlistentry>
2277
2278 <varlistentry>
2279 <term>
2280 <option>-fdo-lambda-eta-expansion</option>
2281 <indexterm><primary><option></option></primary></indexterm>
2282 </term>
2283 <listitem>
2284 <para><emphasis>On by default.</emphasis>
2285 Eta-expand let-bindings to increase their arity.
2286 </para>
2287 </listitem>
2288 </varlistentry>
2289
2290 <varlistentry>
2291 <term>
2292 <option>-feager-blackholing</option>
2293 <indexterm><primary><option></option></primary></indexterm>
2294 </term>
2295 <listitem>
2296 <para>Usually GHC black-holes a thunk only when it switches
2297 threads. This flag makes it do so as soon as the thunk is
2298 entered. See <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/">
2299 Haskell on a shared-memory multiprocessor</ulink>.
2300 </para>
2301 </listitem>
2302 </varlistentry>
2303
2304 <varlistentry>
2305 <term>
2306 <option>-fexcess-precision</option>
2307 <indexterm><primary><option>-fexcess-precision</option></primary></indexterm>
2308 </term>
2309 <listitem>
2310 <para>When this option is given, intermediate floating
2311 point values can have a <emphasis>greater</emphasis>
2312 precision/range than the final type. Generally this is a
2313 good thing, but some programs may rely on the exact
2314 precision/range of
2315 <literal>Float</literal>/<literal>Double</literal> values
2316 and should not use this option for their compilation.</para>
2317
2318 <para>
2319 Note that the 32-bit x86 native code generator only
2320 supports excess-precision mode, so neither
2321 <option>-fexcess-precision</option> nor
2322 <option>-fno-excess-precision</option> has any effect.
2323 This is a known bug, see <xref linkend="bugs-ghc" />.
2324 </para>
2325 </listitem>
2326 </varlistentry>
2327
2328 <varlistentry>
2329 <term>
2330 <option>-fexpose-all-unfoldings</option>
2331 <indexterm><primary><option></option></primary></indexterm>
2332 </term>
2333 <listitem>
2334 <para>An experimental flag to expose all unfoldings, even for very
2335 large or recursive functions. This allows for all functions to be
2336 inlined while usually GHC would avoid inlining larger functions.
2337 </para>
2338 </listitem>
2339 </varlistentry>
2340
2341 <varlistentry>
2342 <term>
2343 <option>-ffloat-in</option>
2344 <indexterm><primary><option></option></primary></indexterm>
2345 </term>
2346 <listitem>
2347 <para><emphasis>On by default.</emphasis>
2348 Float let-bindings inwards, nearer their binding site. See
2349 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">
2350 Let-floating: moving bindings to give faster programs (ICFP'96)</ulink>.
2351 </para>
2352
2353 <para>This optimisation moves let bindings closer to their use
2354 site. The benefit here is that this may avoid unnecessary
2355 allocation if the branch the let is now on is never executed. It
2356 also enables other optimisation passes to work more effectively
2357 as they have more information locally.
2358 </para>
2359
2360 <para>This optimisation isn't always beneficial though (so GHC
2361 applies some heuristics to decide when to apply it). The details
2362 get complicated but a simple example is that it is often beneficial
2363 to move let bindings outwards so that multiple let bindings can be
2364 grouped into a larger single let binding, effectively batching
2365 their allocation and helping the garbage collector and allocator.
2366 </para>
2367 </listitem>
2368 </varlistentry>
2369
2370 <varlistentry>
2371 <term>
2372 <option>-ffull-laziness</option>
2373 <indexterm><primary><option>-ffull-laziness</option></primary></indexterm>
2374 </term>
2375 <listitem>
2376 <para><emphasis>On by default.</emphasis>
2377 Run the full laziness optimisation (also known as let-floating),
2378 which floats let-bindings outside enclosing lambdas, in the hope
2379 they will be thereby be computed less often. See
2380 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">Let-floating:
2381 moving bindings to give faster programs (ICFP'96)</ulink>.
2382 Full laziness increases sharing, which can lead to increased memory
2383 residency.
2384 </para>
2385
2386 <para>NOTE: GHC doesn't implement complete full-laziness.
2387 When optimisation in on, and <option>-fno-full-laziness</option>
2388 is not given, some transformations that increase sharing are
2389 performed, such as extracting repeated computations from a loop.
2390 These are the same transformations that a fully lazy
2391 implementation would do, the difference is that GHC doesn't
2392 consistently apply full-laziness, so don't rely on it.
2393 </para>
2394 </listitem>
2395 </varlistentry>
2396
2397 <varlistentry>
2398 <term>
2399 <option>-ffun-to-thunk</option>
2400 <indexterm><primary><option>-ffun-to-thunk</option></primary></indexterm>
2401 </term>
2402 <listitem>
2403 <para>Worker-wrapper removes unused arguments, but usually we do
2404 not remove them all, lest it turn a function closure into a thunk,
2405 thereby perhaps creating a space leak and/or disrupting inlining.
2406 This flag allows worker/wrapper to remove <emphasis>all</emphasis>
2407 value lambdas. Off by default.
2408 </para>
2409 </listitem>
2410 </varlistentry>
2411
2412 <varlistentry>
2413 <term>
2414 <option>-fignore-asserts</option>
2415 <indexterm><primary><option>-fignore-asserts</option></primary></indexterm>
2416 </term>
2417 <listitem>
2418 <para><emphasis>On by default.</emphasis>.
2419 Causes GHC to ignore uses of the function
2420 <literal>Exception.assert</literal> in source code (in
2421 other words, rewriting <literal>Exception.assert p
2422 e</literal> to <literal>e</literal> (see <xref
2423 linkend="assertions"/>).
2424 </para>
2425 </listitem>
2426 </varlistentry>
2427
2428 <varlistentry>
2429 <term>
2430 <option>-fignore-interface-pragmas</option>
2431 <indexterm><primary><option>-fignore-interface-pragmas</option></primary></indexterm>
2432 </term>
2433 <listitem>
2434 <para>Tells GHC to ignore all inessential information when reading interface files.
2435 That is, even if <filename>M.hi</filename> contains unfolding or strictness information
2436 for a function, GHC will ignore that information.</para>
2437 </listitem>
2438 </varlistentry>
2439
2440 <varlistentry>
2441 <term>
2442 <option>-flate-dmd-anal</option>
2443 <indexterm><primary><option>-flate-dmd-anal</option></primary></indexterm>
2444 </term>
2445 <listitem>
2446 <para>Run demand analysis
2447 again, at the end of the simplification pipeline. We found some opportunities
2448 for discovering strictness that were not visible earlier; and optimisations like
2449 <literal>-fspec-constr</literal> can create functions with unused arguments which
2450 are eliminated by late demand analysis. Improvements are modest, but so is the
2451 cost. See notes on the <ulink url="http://ghc.haskell.org/trac/ghc/wiki/LateDmd">Trac wiki page</ulink>.
2452 </para>
2453 </listitem>
2454 </varlistentry>
2455
2456 <varlistentry>
2457 <term>
2458 <option>-fliberate-case</option>
2459 <indexterm><primary><option>-fliberate-case</option></primary></indexterm>
2460 </term>
2461 <listitem>
2462 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2463 Turn on the liberate-case transformation. This unrolls recursive
2464 function once in its own RHS, to avoid repeated case analysis of
2465 free variables. It's a bit like the call-pattern specialiser
2466 (<option>-fspec-constr</option>) but for free variables rather than
2467 arguments.
2468 </para>
2469 </listitem>
2470 </varlistentry>
2471
2472 <varlistentry>
2473 <term>
2474 <option>-fliberate-case-threshold=<replaceable>n</replaceable></option>
2475 <indexterm><primary><option>-fliberate-case-threshold</option></primary></indexterm>
2476 </term>
2477 <listitem>
2478 <para>Set the size threshold for the liberate-case transformation. Default: 2000
2479 </para>
2480 </listitem>
2481 </varlistentry>
2482
2483 <varlistentry>
2484 <term>
2485 <option>-floopification</option>
2486 <indexterm><primary><option>-floopification</option></primary></indexterm>
2487 </term>
2488 <listitem>
2489 <para><emphasis>On by default.</emphasis>
2490 </para>
2491 <para>When this optimisation is enabled the code generator will turn
2492 all self-recursive saturated tail calls into local jumps rather
2493 than function calls.
2494 </para>
2495 </listitem>
2496 </varlistentry>
2497
2498 <varlistentry>
2499 <term>
2500 <option>-fmax-inline-alloc-size=<replaceable>n</replaceable></option>
2501 <indexterm><primary><option>-fmax-inline-alloc-size</option></primary></indexterm>
2502 </term>
2503 <listitem>
2504 <para>Set the maximum size of inline array allocations to n bytes
2505 (default: 128). GHC will allocate non-pinned arrays of statically
2506 known size in the current nursery block if they're no bigger
2507 than n bytes, ignoring GC overheap. This value should be quite
2508 a bit smaller than the block size (typically: 4096).
2509 </para>
2510 </listitem>
2511 </varlistentry>
2512
2513 <varlistentry>
2514 <term>
2515 <option>-fmax-inline-memcpy-insn=<replaceable>n</replaceable></option>
2516 <indexterm><primary><option>-fmax-inline-memcpy-insn</option></primary></indexterm>
2517 </term>
2518 <listitem>
2519 <para>Inline memcpy calls if they would generate no more than n pseudo instructions (default: 32).
2520 </para>
2521 </listitem>
2522 </varlistentry>
2523
2524 <varlistentry>
2525 <term>
2526 <option>-fmax-inline-memset-insns=<replaceable>n</replaceable></option>
2527 <indexterm><primary><option>-fmax-inline-memset-insns</option></primary></indexterm>
2528 </term>
2529 <listitem>
2530 <para>Inline memset calls if they would generate no more than n pseudo instructions (default: 32).
2531 </para>
2532 </listitem>
2533 </varlistentry>
2534
2535 <varlistentry>
2536 <term>
2537 <option>-fmax-relevant-binds=<replaceable>n</replaceable></option>
2538 <indexterm><primary><option>-fmax-relevant-bindings</option></primary></indexterm>
2539 </term>
2540 <listitem>
2541 <para>The type checker sometimes displays a fragment of the type environment
2542 in error messages, but only up to some maximum number, set by this flag.
2543 The default is 6. Turning it off with <option>-fno-max-relevant-bindings</option>
2544 gives an unlimited number. Syntactically top-level bindings are also
2545 usually excluded (since they may be numerous), but
2546 <option>-fno-max-relevant-bindings</option> includes them too.
2547 </para>
2548 </listitem>
2549 </varlistentry>
2550
2551 <varlistentry>
2552 <term>
2553 <option>-fmax-simplifier-iterations=<replaceable>n</replaceable></option>
2554 <indexterm><primary><option>-fmax-simplifier-iterations</option></primary></indexterm>
2555 </term>
2556 <listitem>
2557 <para>Sets the maximal number of iterations for the simplifier. Defult: 4.
2558 </para>
2559 </listitem>
2560 </varlistentry>
2561
2562 <varlistentry>
2563 <term>
2564 <option>-fmax-worker-args=<replaceable>n</replaceable></option>
2565 <indexterm><primary><option>-fmax-worker-args</option></primary></indexterm>
2566 </term>
2567 <listitem>
2568 <para>If a worker has that many arguments, none will be unpacked anymore (default: 10)
2569 </para>
2570 </listitem>
2571 </varlistentry>
2572
2573 <varlistentry>
2574 <term>
2575 <option>-fno-opt-coercion</option>
2576 <indexterm><primary><option>-fno-opt-coercion</option></primary></indexterm>
2577 </term>
2578 <listitem>
2579 <para>Turn off the coercion optimiser.
2580 </para>
2581 </listitem>
2582 </varlistentry>
2583
2584 <varlistentry>
2585 <term>
2586 <option>-fno-pre-inlining</option>
2587 <indexterm><primary><option>-fno-pre-inlining</option></primary></indexterm>
2588 </term>
2589 <listitem>
2590 <para>Turn off pre-inlining.
2591 </para>
2592 </listitem>
2593 </varlistentry>
2594
2595 <varlistentry>
2596 <term>
2597 <option>-fno-state-hack</option>
2598 <indexterm><primary><option>-fno-state-hack</option></primary></indexterm>
2599 </term>
2600 <listitem>
2601 <para>Turn off the "state hack" whereby any lambda with a
2602 <literal>State#</literal> token as argument is considered to be
2603 single-entry, hence it is considered OK to inline things inside
2604 it. This can improve performance of IO and ST monad code, but it
2605 runs the risk of reducing sharing.
2606 </para>
2607 </listitem>
2608 </varlistentry>
2609
2610 <varlistentry>
2611 <term>
2612 <option>-fomit-interface-pragmas</option>
2613 <indexterm><primary><option>-fomit-interface-pragmas</option></primary></indexterm>
2614 </term>
2615 <listitem>
2616 <para>Tells GHC to omit all inessential information from the
2617 interface file generated for the module being compiled (say M).
2618 This means that a module importing M will see only the
2619 <emphasis>types</emphasis> of the functions that M exports, but
2620 not their unfoldings, strictness info, etc. Hence, for example,
2621 no function exported by M will be inlined into an importing module.
2622 The benefit is that modules that import M will need to be
2623 recompiled less often (only when M's exports change their type, not
2624 when they change their implementation).</para>
2625 </listitem>
2626 </varlistentry>
2627
2628 <varlistentry>
2629 <term>
2630 <option>-fomit-yields</option>
2631 <indexterm><primary><option>-fomit-yields</option></primary></indexterm>
2632 </term>
2633 <listitem>
2634 <para><emphasis>On by default.</emphasis> Tells GHC to omit
2635 heap checks when no allocation is being performed. While this improves
2636 binary sizes by about 5%, it also means that threads run in
2637 tight non-allocating loops will not get preempted in a timely
2638 fashion. If it is important to always be able to interrupt such
2639 threads, you should turn this optimization off. Consider also
2640 recompiling all libraries with this optimization turned off, if you
2641 need to guarantee interruptibility.
2642 </para>
2643 </listitem>
2644 </varlistentry>
2645
2646 <varlistentry>
2647 <term>
2648 <option>-fpedantic-bottoms</option>
2649 <indexterm><primary><option>-fpedantic-bottoms</option></primary></indexterm>
2650 </term>
2651 <listitem>
2652 <para>Make GHC be more precise about its treatment of bottom (but see also
2653 <option>-fno-state-hack</option>). In particular, stop GHC
2654 eta-expanding through a case expression, which is good for
2655 performance, but bad if you are using <literal>seq</literal> on
2656 partial applications.
2657 </para>
2658 </listitem>
2659 </varlistentry>
2660
2661 <varlistentry>
2662 <term>
2663 <option>-fregs-graph</option>
2664 <indexterm><primary><option>-fregs-graph</option></primary></indexterm>
2665 </term>
2666 <listitem>
2667 <para><emphasis>Off by default due to a performance regression bug.
2668 Only applies in combination with the native code generator.</emphasis>
2669 Use the graph colouring register allocator for register allocation
2670 in the native code generator. By default, GHC uses a simpler,
2671 faster linear register allocator. The downside being that the
2672 linear register allocator usually generates worse code.
2673 </para>
2674 </listitem>
2675 </varlistentry>
2676
2677 <varlistentry>
2678 <term>
2679 <option>-fregs-iterative</option>
2680 <indexterm><primary><option>-fregs-iterative</option></primary></indexterm>
2681 </term>
2682 <listitem>
2683 <para><emphasis>Off by default, only applies in combination with
2684 the native code generator.</emphasis>
2685 Use the iterative coalescing graph colouring register allocator for
2686 register allocation in the native code generator. This is the same
2687 register allocator as the <option>-fregs-graph</option> one but also
2688 enables iterative coalescing during register allocation.
2689 </para>
2690 </listitem>
2691 </varlistentry>
2692
2693 <varlistentry>
2694 <term>
2695 <option>-fsimplifier-phases=<replaceable>n</replaceable></option>
2696 <indexterm><primary><option>-fsimplifier-phases</option></primary></indexterm>
2697 </term>
2698 <listitem>
2699 <para>Set the number of phases for the simplifier (default 2). Ignored with -O0.
2700 </para>
2701 </listitem>
2702 </varlistentry>
2703
2704 <varlistentry>
2705 <term>
2706 <option>-fsimpl-tick-factor=<replaceable>n</replaceable></option>
2707 <indexterm><primary><option>-fsimpl-tick-factor</option></primary></indexterm>
2708 </term>
2709 <listitem>
2710 <para>GHC's optimiser can diverge if you write rewrite rules (
2711 <xref linkend="rewrite-rules"/>) that don't terminate, or (less
2712 satisfactorily) if you code up recursion through data types
2713 (<xref linkend="bugs-ghc"/>). To avoid making the compiler fall
2714 into an infinite loop, the optimiser carries a "tick count" and
2715 stops inlining and applying rewrite rules when this count is
2716 exceeded. The limit is set as a multiple of the program size, so
2717 bigger programs get more ticks. The
2718 <option>-fsimpl-tick-factor</option> flag lets you change the
2719 multiplier. The default is 100; numbers larger than 100 give more
2720 ticks, and numbers smaller than 100 give fewer.
2721 </para>
2722
2723 <para>If the tick-count expires, GHC summarises what simplifier
2724 steps it has done; you can use
2725 <option>-fddump-simpl-stats</option> to generate a much more
2726 detailed list. Usually that identifies the loop quite
2727 accurately, because some numbers are very large.
2728 </para>
2729 </listitem>
2730 </varlistentry>
2731
2732 <varlistentry>
2733 <term>
2734 <option>-fspec-constr</option>
2735 <indexterm><primary><option>-fspec-constr</option></primary></indexterm>
2736 </term>
2737 <listitem>
2738 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2739 Turn on call-pattern specialisation; see
2740 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/spec-constr/index.htm">
2741 Call-pattern specialisation for Haskell programs</ulink>.
2742 </para>
2743
2744 <para>This optimisation specializes recursive functions according to
2745 their argument "shapes". This is best explained by example so
2746 consider:
2747 <programlisting>
2748 last :: [a] -> a
2749 last [] = error "last"
2750 last (x : []) = x
2751 last (x : xs) = last xs
2752 </programlisting>
2753 In this code, once we pass the initial check for an empty list we
2754 know that in the recursive case this pattern match is redundant. As
2755 such <option>-fspec-constr</option> will transform the above code
2756 to:
2757 <programlisting>
2758 last :: [a] -> a
2759 last [] = error "last"
2760 last (x : xs) = last' x xs
2761 where
2762 last' x [] = x
2763 last' x (y : ys) = last' y ys
2764 </programlisting>
2765 </para>
2766
2767 <para>As well avoid unnecessary pattern matching it also helps avoid
2768 unnecessary allocation. This applies when a argument is strict in
2769 the recursive call to itself but not on the initial entry. As
2770 strict recursive branch of the function is created similar to the
2771 above example.
2772 </para>
2773
2774 <para>It is also possible for library writers to instruct
2775 GHC to perform call-pattern specialisation extremely
2776 aggressively. This is necessary for some highly optimized
2777 libraries, where we may want to specialize regardless of
2778 the number of specialisations, or the size of the code. As
2779 an example, consider a simplified use-case from the
2780 <literal>vector</literal> library:</para>
2781 <programlisting>
2782 import GHC.Types (SPEC(..))
2783
2784 foldl :: (a -> b -> a) -> a -> Stream b -> a
2785 {-# INLINE foldl #-}
2786 foldl f z (Stream step s _) = foldl_loop SPEC z s
2787 where
2788 foldl_loop !sPEC z s = case step s of
2789 Yield x s' -> foldl_loop sPEC (f z x) s'
2790 Skip -> foldl_loop sPEC z s'
2791 Done -> z
2792 </programlisting>
2793
2794 <para>Here, after GHC inlines the body of
2795 <literal>foldl</literal> to a call site, it will perform
2796 call-pattern specialization very aggressively on
2797 <literal>foldl_loop</literal> due to the use of
2798 <literal>SPEC</literal> in the argument of the loop
2799 body. <literal>SPEC</literal> from
2800 <literal>GHC.Types</literal> is specifically recognized by
2801 the compiler.</para>
2802
2803 <para>(NB: it is extremely important you use
2804 <literal>seq</literal> or a bang pattern on the
2805 <literal>SPEC</literal> argument!)</para>
2806
2807 <para>In particular, after inlining this will
2808 expose <literal>f</literal> to the loop body directly,
2809 allowing heavy specialisation over the recursive
2810 cases.</para>
2811 </listitem>
2812 </varlistentry>
2813
2814 <varlistentry>
2815 <term>
2816 <option>-fspec-constr-count=<replaceable>n</replaceable></option>
2817 <indexterm><primary><option>-fspec-constr-count</option></primary></indexterm>
2818 </term>
2819 <listitem>
2820 <para>Set the maximum number of specialisations
2821 that will be created for any one function by the SpecConstr
2822 transformation (default: 3).
2823 </para>
2824 </listitem>
2825 </varlistentry>
2826
2827 <varlistentry>
2828 <term>
2829 <option>-fspec-constr-threshold=<replaceable>n</replaceable></option>
2830 <indexterm><primary><option>-fspec-constr-threshold</option></primary></indexterm>
2831 </term>
2832 <listitem>
2833 <para>Set the size threshold for the SpecConstr transformation (default: 2000).
2834 </para>
2835 </listitem>
2836 </varlistentry>
2837
2838 <varlistentry>
2839 <term>
2840 <option>-fspecialise</option>
2841 <indexterm><primary><option>-fspecialise</option></primary></indexterm>
2842 </term>
2843 <listitem>
2844 <para><emphasis>On by default.</emphasis>
2845 Specialise each type-class-overloaded function defined in this
2846 module for the types at which it is called in this module. Also
2847 specialise imported functions that have an INLINABLE pragma
2848 (<xref linkend="inlinable-pragma"/>) for the types at which they
2849 are called in this module.
2850 </para>
2851 </listitem>
2852 </varlistentry>
2853
2854 <varlistentry>
2855 <term>
2856 <option>-fstatic-argument-transformation</option>
2857 <indexterm><primary><option>-fstatic-argument-transformation</option></primary></indexterm>
2858 </term>
2859 <listitem>
2860 <para>Turn on the static argument transformation, which turns a
2861 recursive function into a non-recursive one with a local
2862 recursive loop. See Chapter 7 of
2863 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/santos-thesis.ps.gz">
2864 Andre Santos's PhD thesis</ulink>
2865 </para>
2866 </listitem>
2867 </varlistentry>
2868
2869 <varlistentry>
2870 <term>
2871 <option>-fstrictness</option>
2872 <indexterm><primary><option></option></primary></indexterm>
2873 </term>
2874 <listitem>
2875 <para> <emphasis>On by default.</emphasis>.
2876 Switch on the strictness analyser. There is a very old paper about GHC's
2877 strictness analyser, <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/simple-strictnes-analyser.ps.gz">
2878 Measuring the effectiveness of a simple strictness analyser</ulink>,
2879 but the current one is quite a bit different.
2880 </para>
2881
2882 <para>The strictness analyser figures out when arguments and
2883 variables in a function can be treated 'strictly' (that is they
2884 are always evaluated in the function at some point). This allow
2885 GHC to apply certain optimisations such as unboxing that
2886 otherwise don't apply as they change the semantics of the program
2887 when applied to lazy arguments.
2888 </para>
2889 </listitem>
2890 </varlistentry>
2891
2892 <varlistentry>
2893 <term>
2894 <option>-fstrictness-before=<replaceable>n</replaceable></option>
2895 <indexterm><primary><option>-fstrictness-before</option></primary></indexterm>
2896 </term>
2897 <listitem>
2898 <para>Run an additional strictness analysis before simplifier phase n.
2899 </para>
2900 </listitem>
2901 </varlistentry>
2902
2903 <varlistentry>
2904 <term>
2905 <option>-funbox-small-strict-fields</option>:
2906 <indexterm><primary><option>-funbox-small-strict-fields</option></primary></indexterm>
2907 <indexterm><primary>strict constructor fields</primary></indexterm>
2908 <indexterm><primary>constructor fields, strict</primary></indexterm>
2909 </term>
2910 <listitem>
2911 <para><emphasis>On by default.</emphasis>. This option
2912 causes all constructor fields which are marked strict
2913 (i.e. &ldquo;!&rdquo;) and which representation is smaller
2914 or equal to the size of a pointer to be unpacked, if
2915 possible. It is equivalent to adding an
2916 <literal>UNPACK</literal> pragma (see <xref
2917 linkend="unpack-pragma"/>) to every strict constructor
2918 field that fulfils the size restriction.
2919 </para>
2920
2921 <para>For example, the constructor fields in the following
2922 data types
2923 <programlisting>
2924 data A = A !Int
2925 data B = B !A
2926 newtype C = C B
2927 data D = D !C
2928 </programlisting>
2929 would all be represented by a single
2930 <literal>Int#</literal> (see <xref linkend="primitives"/>)
2931 value with
2932 <option>-funbox-small-strict-fields</option> enabled.
2933 </para>
2934
2935 <para>This option is less of a sledgehammer than
2936 <option>-funbox-strict-fields</option>: it should rarely make things
2937 worse. If you use <option>-funbox-small-strict-fields</option>
2938 to turn on unboxing by default you can disable it for certain
2939 constructor fields using the <literal>NOUNPACK</literal> pragma (see
2940 <xref linkend="nounpack-pragma"/>).</para>
2941
2942 <para>
2943 Note that for consistency <literal>Double</literal>,
2944 <literal>Word64</literal>, and <literal>Int64</literal> constructor
2945 fields are unpacked on 32-bit platforms, even though they are
2946 technically larger than a pointer on those platforms.
2947 </para>
2948 </listitem>
2949 </varlistentry>
2950
2951 <varlistentry>
2952 <term>
2953 <option>-funbox-strict-fields</option>:
2954 <indexterm><primary><option>-funbox-strict-fields</option></primary></indexterm>
2955 <indexterm><primary>strict constructor fields</primary></indexterm>
2956 <indexterm><primary>constructor fields, strict</primary></indexterm>
2957 </term>
2958 <listitem>
2959 <para>This option causes all constructor fields which are marked
2960 strict (i.e. &ldquo;!&rdquo;) to be unpacked if possible. It is
2961 equivalent to adding an <literal>UNPACK</literal> pragma to every
2962 strict constructor field (see <xref linkend="unpack-pragma"/>).
2963 </para>
2964
2965 <para>This option is a bit of a sledgehammer: it might sometimes
2966 make things worse. Selectively unboxing fields by using
2967 <literal>UNPACK</literal> pragmas might be better. An alternative
2968 is to use <option>-funbox-strict-fields</option> to turn on
2969 unboxing by default but disable it for certain constructor
2970 fields using the <literal>NOUNPACK</literal> pragma (see
2971 <xref linkend="nounpack-pragma"/>).</para>
2972 </listitem>
2973 </varlistentry>
2974
2975 <varlistentry>
2976 <term>
2977 <option>-funfolding-creation-threshold=<replaceable>n</replaceable></option>:
2978 <indexterm><primary><option>-funfolding-creation-threshold</option></primary></indexterm>
2979 <indexterm><primary>inlining, controlling</primary></indexterm>
2980 <indexterm><primary>unfolding, controlling</primary></indexterm>
2981 </term>
2982 <listitem>
2983 <para>(Default: 750) Governs the maximum size that GHC will allow a
2984 function unfolding to be. (An unfolding has a &ldquo;size&rdquo;
2985 that reflects the cost in terms of &ldquo;code bloat&rdquo; of
2986 expanding (aka inlining) that unfolding at a call site. A bigger
2987 function would be assigned a bigger cost.)
2988 </para>
2989
2990 <para>Consequences: (a) nothing larger than this will be inlined
2991 (unless it has an INLINE pragma); (b) nothing larger than this
2992 will be spewed into an interface file.
2993 </para>
2994
2995 <para>Increasing this figure is more likely to result in longer
2996 compile times than faster code. The
2997 <option>-funfolding-use-threshold</option> is more useful.
2998 </para>
2999 </listitem>
3000 </varlistentry>
3001
3002 <varlistentry>
3003 <term>
3004 <option>-funfolding-dict-discount=<replaceable>n</replaceable></option>:
3005 <indexterm><primary><option>-funfolding-dict-discount</option></primary></indexterm>
3006 <indexterm><primary>inlining, controlling</primary></indexterm>
3007 <indexterm><primary>unfolding, controlling</primary></indexterm>
3008 </term>
3009 <listitem>
3010 <para>Default: 30
3011 </para>
3012 </listitem>
3013 </varlistentry>
3014
3015 <varlistentry>
3016 <term>
3017 <option>-funfolding-fun-discount=<replaceable>n</replaceable></option>:
3018 <indexterm><primary><option>-funfolding-fun-discount</option></primary></indexterm>
3019 <indexterm><primary>inlining, controlling</primary></indexterm>
3020 <indexterm><primary>unfolding, controlling</primary></indexterm>
3021 </term>
3022 <listitem>
3023 <para>Default: 60
3024 </para>
3025 </listitem>
3026 </varlistentry>
3027
3028 <varlistentry>
3029 <term>
3030 <option>-funfolding-keeness-factor=<replaceable>n</replaceable></option>:
3031 <indexterm><primary><option>-funfolding-keeness-factor</option></primary></indexterm>
3032 <indexterm><primary>inlining, controlling</primary></indexterm>
3033 <indexterm><primary>unfolding, controlling</primary></indexterm>
3034 </term>
3035 <listitem>
3036 <para>Default: 1.5
3037 </para>
3038 </listitem>
3039 </varlistentry>
3040
3041 <varlistentry>
3042 <term>
3043 <option>-funfolding-use-threshold=<replaceable>n</replaceable></option>
3044 <indexterm><primary><option>-funfolding-use-threshold</option></primary></indexterm>
3045 <indexterm><primary>inlining, controlling</primary></indexterm>
3046 <indexterm><primary>unfolding, controlling</primary></indexterm>
3047 </term>
3048 <listitem>
3049 <para>(Default: 60) This is the magic cut-off figure for unfolding
3050 (aka inlining): below this size, a function definition will be
3051 unfolded at the call-site, any bigger and it won't. The size
3052 computed for a function depends on two things: the actual size of
3053 the expression minus any discounts that apply depending on the
3054 context into which the expression is to be inlined.
3055 </para>
3056
3057 <para>The difference between this and
3058 <option>-funfolding-creation-threshold</option> is that this one
3059 determines if a function definition will be inlined <emphasis>at
3060 a call site</emphasis>. The other option determines if a
3061 function definition will be kept around at all for potential
3062 inlining.
3063 </para>
3064 </listitem>
3065 </varlistentry>
3066
3067 <varlistentry>
3068 <term>
3069 <option>-fvectorisation-avoidance</option>
3070 <indexterm><primary><option></option></primary></indexterm>
3071 </term>
3072 <listitem>
3073 <para>Part of <link linkend="dph">Data Parallel Haskell
3074 (DPH)</link>.</para>
3075
3076 <para><emphasis>On by default.</emphasis> Enable the
3077 <emphasis>vectorisation</emphasis> avoidance optimisation. This
3078 optimisation only works when used in combination with the
3079 <option>-fvectorise</option> transformation.</para>
3080
3081 <para>While vectorisation of code using DPH is often a big win, it
3082 can also produce worse results for some kinds of code. This
3083 optimisation modifies the vectorisation transformation to try to
3084 determine if a function would be better of unvectorised and if
3085 so, do just that.</para>
3086 </listitem>
3087 </varlistentry>
3088
3089 <varlistentry>
3090 <term>
3091 <option>-fvectorise</option>
3092 <indexterm><primary><option></option></primary></indexterm>
3093 </term>
3094 <listitem>
3095 <para>Part of <link linkend="dph">Data Parallel Haskell
3096 (DPH)</link>.</para>
3097
3098 <para><emphasis>Off by default.</emphasis> Enable the
3099 <emphasis>vectorisation</emphasis> optimisation transformation. This
3100 optimisation transforms the nested data parallelism code of programs
3101 using DPH into flat data parallelism. Flat data parallel programs
3102 should have better load balancing, enable SIMD parallelism and
3103 friendlier cache behaviour.</para>
3104 </listitem>
3105 </varlistentry>
3106
3107 </variablelist>
3108
3109 </sect2>
3110
3111 </sect1>
3112
3113 &code-gens;
3114
3115 &phases;
3116
3117 &shared_libs;
3118
3119 <sect1 id="using-concurrent">
3120 <title>Using Concurrent Haskell</title>
3121 <indexterm><primary>Concurrent Haskell</primary><secondary>using</secondary></indexterm>
3122
3123 <para>GHC supports Concurrent Haskell by default, without requiring a
3124 special option or libraries compiled in a certain way. To get access to
3125 the support libraries for Concurrent Haskell, just import
3126 <ulink
3127 url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>. More information on Concurrent Haskell is provided in the documentation for that module.</para>
3128
3129 <para>
3130 Optionally, the program may be linked with
3131 the <option>-threaded</option> option (see
3132 <xref linkend="options-linker" />. This provides two benefits:
3133
3134 <itemizedlist>
3135 <listitem>
3136 <para>It enables the <option>-N</option><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm> RTS option to be
3137 used, which allows threads to run in
3138 parallel<indexterm><primary>parallelism</primary></indexterm>
3139 on a
3140 multiprocessor<indexterm><primary>multiprocessor</primary></indexterm><indexterm><primary>SMP</primary></indexterm>
3141 or
3142 multicore<indexterm><primary>multicore</primary></indexterm>
3143 machine. See <xref linkend="using-smp" />.</para>
3144 </listitem>
3145 <listitem>
3146 <para>If a thread makes a foreign call (and the call is
3147 not marked <literal>unsafe</literal>), then other
3148 Haskell threads in the program will continue to run
3149 while the foreign call is in progress.
3150 Additionally, <literal>foreign export</literal>ed
3151 Haskell functions may be called from multiple OS
3152 threads simultaneously. See
3153 <xref linkend="ffi-threads" />.</para>
3154 </listitem>
3155 </itemizedlist>
3156 </para>
3157
3158 <para>The following RTS option(s) affect the behaviour of Concurrent
3159 Haskell programs:<indexterm><primary>RTS options, concurrent</primary></indexterm></para>
3160
3161 <variablelist>
3162 <varlistentry>
3163 <term><option>-C<replaceable>s</replaceable></option></term>
3164 <listitem>
3165 <para><indexterm><primary><option>-C<replaceable>s</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
3166 Sets the context switch interval to <replaceable>s</replaceable>
3167 seconds. A context switch will occur at the next heap block
3168 allocation after the timer expires (a heap block allocation occurs
3169 every 4k of allocation). With <option>-C0</option> or
3170 <option>-C</option>, context switches will occur as often as
3171 possible (at every heap block allocation). By default, context
3172 switches occur every 20ms.</para>
3173 </listitem>
3174 </varlistentry>
3175 </variablelist>
3176 </sect1>
3177
3178 <sect1 id="using-smp">
3179 <title>Using SMP parallelism</title>
3180 <indexterm><primary>parallelism</primary>
3181 </indexterm>
3182 <indexterm><primary>SMP</primary>
3183 </indexterm>
3184
3185 <para>GHC supports running Haskell programs in parallel on an SMP
3186 (symmetric multiprocessor).</para>
3187
3188 <para>There's a fine distinction between
3189 <emphasis>concurrency</emphasis> and <emphasis>parallelism</emphasis>:
3190 parallelism is all about making your program run
3191 <emphasis>faster</emphasis> by making use of multiple processors
3192 simultaneously. Concurrency, on the other hand, is a means of
3193 abstraction: it is a convenient way to structure a program that must
3194 respond to multiple asynchronous events.</para>
3195
3196 <para>However, the two terms are certainly related. By making use of
3197 multiple CPUs it is possible to run concurrent threads in parallel,
3198 and this is exactly what GHC's SMP parallelism support does. But it
3199 is also possible to obtain performance improvements with parallelism
3200 on programs that do not use concurrency. This section describes how to
3201 use GHC to compile and run parallel programs, in <xref
3202 linkend="lang-parallel" /> we describe the language features that affect
3203 parallelism.</para>
3204
3205 <sect2 id="parallel-compile-options">
3206 <title>Compile-time options for SMP parallelism</title>
3207
3208 <para>In order to make use of multiple CPUs, your program must be
3209 linked with the <option>-threaded</option> option (see <xref
3210 linkend="options-linker" />). Additionally, the following
3211 compiler options affect parallelism:</para>
3212
3213 <variablelist>
3214 <varlistentry>
3215 <term><option>-feager-blackholing</option></term>
3216 <indexterm><primary><option>-feager-blackholing</option></primary></indexterm>
3217 <listitem>
3218 <para>
3219 Blackholing is the act of marking a thunk (lazy
3220 computuation) as being under evaluation. It is useful for
3221 three reasons: firstly it lets us detect certain kinds of
3222 infinite loop (the <literal>NonTermination</literal>
3223 exception), secondly it avoids certain kinds of space
3224 leak, and thirdly it avoids repeating a computation in a
3225 parallel program, because we can tell when a computation
3226 is already in progress.</para>
3227
3228 <para>
3229 The option <option>-feager-blackholing</option> causes
3230 each thunk to be blackholed as soon as evaluation begins.
3231 The default is "lazy blackholing", whereby thunks are only
3232 marked as being under evaluation when a thread is paused
3233 for some reason. Lazy blackholing is typically more
3234 efficient (by 1-2&percnt; or so), because most thunks don't
3235 need to be blackholed. However, eager blackholing can
3236 avoid more repeated computation in a parallel program, and
3237 this often turns out to be important for parallelism.
3238 </para>
3239
3240 <para>
3241 We recommend compiling any code that is intended to be run
3242 in parallel with the <option>-feager-blackholing</option>
3243 flag.
3244 </para>
3245 </listitem>
3246 </varlistentry>
3247 </variablelist>
3248 </sect2>
3249
3250 <sect2 id="parallel-options">
3251 <title>RTS options for SMP parallelism</title>
3252
3253 <para>There are two ways to run a program on multiple
3254 processors:
3255 call <literal>Control.Concurrent.setNumCapabilities</literal> from your
3256 program, or use the RTS <option>-N</option> option.</para>
3257
3258 <variablelist>
3259 <varlistentry>
3260 <term><option>-N<optional><replaceable>x</replaceable></optional></option></term>
3261 <listitem>
3262 <para><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
3263 Use <replaceable>x</replaceable> simultaneous threads when
3264 running the program. Normally <replaceable>x</replaceable>
3265 should be chosen to match the number of CPU cores on the
3266 machine<footnote><para>Whether hyperthreading cores should be counted or not is an
3267 open question; please feel free to experiment and let us know what
3268 results you find.</para></footnote>. For example,
3269 on a dual-core machine we would probably use
3270 <literal>+RTS -N2 -RTS</literal>.</para>
3271
3272 <para>Omitting <replaceable>x</replaceable>,
3273 i.e. <literal>+RTS -N -RTS</literal>, lets the runtime
3274 choose the value of <replaceable>x</replaceable> itself
3275 based on how many processors are in your machine.</para>
3276
3277 <para>Be careful when using all the processors in your
3278 machine: if some of your processors are in use by other
3279 programs, this can actually harm performance rather than
3280 improve it.</para>
3281
3282 <para>Setting <option>-N</option> also has the effect of
3283 enabling the parallel garbage collector (see
3284 <xref linkend="rts-options-gc" />).</para>
3285
3286 <para>The current value of the <option>-N</option> option
3287 is available to the Haskell program
3288 via <literal>Control.Concurrent.getNumCapabilities</literal>, and
3289 it may be changed while the program is running by
3290 calling <literal>Control.Concurrent.setNumCapabilities</literal>.</para>
3291 </listitem>
3292 </varlistentry>
3293 </variablelist>
3294
3295 <para>The following options affect the way the runtime schedules
3296 threads on CPUs:</para>
3297
3298 <variablelist>
3299 <varlistentry>
3300 <term><option>-qa</option></term>
3301 <indexterm><primary><option>-qa</option></primary><secondary>RTS
3302 option</secondary></indexterm>
3303 <listitem>
3304 <para>Use the OS's affinity facilities to try to pin OS
3305 threads to CPU cores. This is an experimental feature,
3306 and may or may not be useful. Please let us know
3307 whether it helps for you!</para>
3308 </listitem>
3309 </varlistentry>
3310 <varlistentry>
3311 <term><option>-qm</option></term>
3312 <indexterm><primary><option>-qm</option></primary><secondary>RTS
3313 option</secondary></indexterm>
3314 <listitem>
3315 <para>Disable automatic migration for load balancing.
3316 Normally the runtime will automatically try to schedule
3317 threads across the available CPUs to make use of idle
3318 CPUs; this option disables that behaviour. Note that
3319 migration only applies to threads; sparks created
3320 by <literal>par</literal> are load-balanced separately
3321 by work-stealing.</para>
3322
3323 <para>
3324 This option is probably only of use for concurrent
3325 programs that explicitly schedule threads onto CPUs
3326 with <literal>Control.Concurrent.forkOn</literal>.
3327 </para>
3328 </listitem>
3329 </varlistentry>
3330 </variablelist>
3331 </sect2>
3332
3333 <sect2>
3334 <title>Hints for using SMP parallelism</title>
3335
3336 <para>Add the <literal>-s</literal> RTS option when
3337 running the program to see timing stats, which will help to tell you
3338 whether your program got faster by using more CPUs or not. If the user
3339 time is greater than
3340 the elapsed time, then the program used more than one CPU. You should
3341 also run the program without <literal>-N</literal> for
3342 comparison.</para>
3343
3344 <para>The output of <literal>+RTS -s</literal> tells you how
3345 many &ldquo;sparks&rdquo; were created and executed during the
3346 run of the program (see <xref linkend="rts-options-gc" />), which
3347 will give you an idea how well your <literal>par</literal>
3348 annotations are working.</para>
3349
3350 <para>GHC's parallelism support has improved in 6.12.1 as a
3351 result of much experimentation and tuning in the runtime
3352 system. We'd still be interested to hear how well it works
3353 for you, and we're also interested in collecting parallel
3354 programs to add to our benchmarking suite.</para>
3355 </sect2>
3356 </sect1>
3357
3358 <sect1 id="options-platform">
3359 <title>Platform-specific Flags</title>
3360
3361 <indexterm><primary>-m* options</primary></indexterm>
3362 <indexterm><primary>platform-specific options</primary></indexterm>
3363 <indexterm><primary>machine-specific options</primary></indexterm>
3364
3365 <para>Some flags only make sense for particular target
3366 platforms.</para>
3367
3368 <variablelist>
3369
3370 <varlistentry>
3371 <term><option>-msse2</option>:</term>
3372 <listitem>
3373 <para>
3374 (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
3375 instruction set to implement floating point operations when using
3376 the <link linkend="native-code-gen">native code generator</link>.
3377 This gives a substantial performance improvement for floating
3378 point, but the resulting compiled code
3379 will only run on processors that support SSE2 (Intel Pentium 4 and
3380 later, or AMD Athlon 64 and later). The
3381 <link linkend="llvm-code-gen">LLVM backend</link> will also use SSE2
3382 if your processor supports it but detects this automatically so no
3383 flag is required.
3384 </para>
3385 <para>
3386 SSE2 is unconditionally used on x86-64 platforms.
3387 </para>
3388 </listitem>
3389 </varlistentry>
3390
3391 <varlistentry>
3392 <term><option>-msse4.2</option>:</term>
3393 <listitem>
3394 <para>
3395 (x86 only, added in GHC 7.4.1) Use the SSE4.2 instruction set to
3396 implement some floating point and bit operations when using the
3397 <link linkend="native-code-gen">native code generator</link>. The
3398 resulting compiled code will only run on processors that
3399 support SSE4.2 (Intel Core i7 and later). The
3400 <link linkend="llvm-code-gen">LLVM backend</link> will also use
3401 SSE4.2 if your processor supports it but detects this automatically
3402 so no flag is required.
3403 </para>
3404 </listitem>
3405 </varlistentry>
3406
3407 </variablelist>
3408
3409 </sect1>
3410
3411 &runtime;
3412 &debug;
3413 &flags;
3414
3415 </chapter>
3416
3417 <!-- Emacs stuff:
3418 ;;; Local Variables: ***
3419 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
3420 ;;; End: ***
3421 -->