docs: Update to reflect reality
[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-deprecated-flags</option>,
1038 <option>-fwarn-unrecognised-pragmas</option>,
1039 <option>-fwarn-pointless-pragmas</option>,
1040 <option>-fwarn-duplicate-constraints</option>,
1041 <option>-fwarn-duplicate-exports</option>,
1042 <option>-fwarn-overflowed-literals</option>,
1043 <option>-fwarn-empty-enumerations</option>,
1044 <option>-fwarn-missing-fields</option>,
1045 <option>-fwarn-missing-methods</option>,
1046 <option>-fwarn-wrong-do-bind</option>,
1047 <option>-fwarn-unsupported-calling-conventions</option>,
1048 <option>-fwarn-dodgy-foreign-imports</option>,
1049 <option>-fwarn-inline-rule-shadowing</option>,
1050 <option>-fwarn-unsupported-llvm-version</option>,
1051 <option>-fwarn-context-quantification</option>, and
1052 <option>-fwarn-tabs</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-unused-binds</option>,
1065 <option>-fwarn-unused-matches</option>,
1066 <option>-fwarn-unused-imports</option>,
1067 <option>-fwarn-incomplete-patterns</option>,
1068 <option>-fwarn-dodgy-exports</option>, and
1069 <option>-fwarn-dodgy-imports</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-type-defaults</option>,
1082 <option>-fwarn-name-shadowing</option>,
1083 <option>-fwarn-missing-signatures</option>,
1084 <option>-fwarn-warn-hi-shadowing</option>,
1085 <option>-fwarn-orphans</option>,
1086 <option>-fwarn-unused-do-bind</option>, and
1087 <option>-fwarn-trustworthy-safe</option>.</para>
1088 </listitem>
1089 </varlistentry>
1090
1091 <varlistentry>
1092 <term><option>-w</option>:</term>
1093 <listitem>
1094 <indexterm><primary><option>-w</option></primary></indexterm>
1095 <para>Turns off all warnings, including the standard ones and
1096 those that <literal>-Wall</literal> doesn't enable.</para>
1097 </listitem>
1098 </varlistentry>
1099
1100 <varlistentry>
1101 <term><option>-Werror</option>:</term>
1102 <listitem>
1103 <indexterm><primary><option>-Werror</option></primary></indexterm>
1104 <para>Makes any warning into a fatal error. Useful so that you don't
1105 miss warnings when doing batch compilation. </para>
1106 </listitem>
1107 </varlistentry>
1108
1109 <varlistentry>
1110 <term><option>-Wwarn</option>:</term>
1111 <listitem>
1112 <indexterm><primary><option>-Wwarn</option></primary></indexterm>
1113 <para>Warnings are treated only as warnings, not as errors. This is
1114 the default, but can be useful to negate a
1115 <option>-Werror</option> flag.</para>
1116 </listitem>
1117 </varlistentry>
1118
1119 </variablelist>
1120
1121 <para>The full set of warning options is described below. To turn
1122 off any warning, simply give the corresponding
1123 <option>-fno-warn-...</option> option on the command line.</para>
1124
1125 <variablelist>
1126
1127 <varlistentry>
1128 <term><option>-fwarn-typed-holes</option>:</term>
1129 <listitem>
1130 <indexterm><primary><option>-fwarn-typed-holes</option></primary>
1131 </indexterm>
1132 <indexterm><primary>warnings</primary></indexterm>
1133 <para>
1134 Determines whether the compiler reports typed holes warnings. Has
1135 no effect unless typed holes errors are deferred until runtime.
1136 See <xref linkend="typed-holes"/> and <xref linkend="defer-type-errors"/>
1137 </para>
1138
1139 <para>This warning is on by default.</para>
1140 </listitem>
1141 </varlistentry>
1142
1143
1144 <varlistentry>
1145 <term><option>-fdefer-type-errors</option>:</term>
1146 <listitem>
1147 <indexterm><primary><option>-fdefer-type-errors</option></primary>
1148 </indexterm>
1149 <indexterm><primary>warnings</primary></indexterm>
1150 <para>Defer as many type errors as possible until runtime.
1151 At compile time you get a warning (instead of an error). At
1152 runtime, if you use a value that depends on a type error, you
1153 get a runtime error; but you can run any type-correct parts of your code
1154 just fine. See <xref linkend="defer-type-errors"/></para>
1155 </listitem>
1156 </varlistentry>
1157
1158 <varlistentry>
1159 <term><option>-fdefer-typed-holes</option>:</term>
1160 <listitem>
1161 <indexterm><primary><option>-fdefer-typed-holes</option></primary>
1162 </indexterm>
1163 <indexterm><primary>warnings</primary></indexterm>
1164 <para>
1165 Defer typed holes errors until runtime. This will turn the errors
1166 produced by <link linked="typed-holes">typed holes</link> into
1167 warnings. Using a value that depends on a typed hole produces a
1168 runtime error, the same as <option>-fdefer-type-errors</option>
1169 (which implies this option). See <xref linkend="typed-holes"/>
1170 and <xref linkend="defer-type-errors"/>.
1171 </para>
1172 <para>
1173 Implied by <option>-fdefer-type-errors</option>. See also
1174 <option>-fwarn-typed-holes</option>.
1175 </para>
1176 </listitem>
1177 </varlistentry>
1178
1179 <varlistentry>
1180 <term><option>-fwarn-partial-type-signatures</option>:</term>
1181 <listitem>
1182 <indexterm><primary><option>-fwarn-partial-type-signatures</option></primary>
1183 </indexterm>
1184 <indexterm><primary>warnings</primary></indexterm>
1185 <para>
1186 Determines whether the compiler reports holes in partial type
1187 signatures as warnings. Has no effect unless
1188 <option>-XPartialTypeSignatures</option> is enabled, which
1189 controls whether errors should be generated for holes in types
1190 or not. See <xref linkend="partial-type-signatures"/>.
1191 </para>
1192
1193 <para>This warning is on by default.</para>
1194 </listitem>
1195 </varlistentry>
1196
1197 <varlistentry>
1198 <term><option>-fhelpful-errors</option>:</term>
1199 <listitem>
1200 <indexterm><primary><option>-fhelpful-errors</option></primary>
1201 </indexterm>
1202 <indexterm><primary>warnings</primary></indexterm>
1203 <para>When a name or package is not found in scope, make
1204 suggestions for the name or package you might have meant instead.</para>
1205 <para>This option is on by default.</para>
1206 </listitem>
1207 </varlistentry>
1208
1209 <varlistentry>
1210 <term><option>-fwarn-unrecognised-pragmas</option>:</term>
1211 <listitem>
1212 <indexterm><primary><option>-fwarn-unrecognised-pragmas</option></primary>
1213 </indexterm>
1214 <indexterm><primary>warnings</primary></indexterm>
1215 <indexterm><primary>pragmas</primary></indexterm>
1216 <para>Causes a warning to be emitted when a
1217 pragma that GHC doesn't recognise is used. As well as pragmas
1218 that GHC itself uses, GHC also recognises pragmas known to be used
1219 by other tools, e.g. <literal>OPTIONS_HUGS</literal> and
1220 <literal>DERIVE</literal>.</para>
1221
1222 <para>This option is on by default.</para>
1223 </listitem>
1224 </varlistentry>
1225
1226 <varlistentry>
1227 <term><option>-fwarn-pointless-pragmas</option>:</term>
1228 <listitem>
1229 <indexterm><primary><option>-fwarn-pointless-pragmas</option></primary>
1230 </indexterm>
1231 <indexterm><primary>warnings</primary></indexterm>
1232 <indexterm><primary>pragmas</primary></indexterm>
1233 <para>Causes a warning to be emitted when GHC detects that a
1234 module contains a pragma that has no effect.</para>
1235
1236 <para>This option is on by default.</para>
1237 </listitem>
1238 </varlistentry>
1239
1240 <varlistentry>
1241 <term><option>-fwarn-warnings-deprecations</option>:</term>
1242 <listitem>
1243 <indexterm><primary><option>-fwarn-warnings-deprecations</option></primary>
1244 </indexterm>
1245 <indexterm><primary>warnings</primary></indexterm>
1246 <indexterm><primary>deprecations</primary></indexterm>
1247 <para>Causes a warning to be emitted when a
1248 module, function or type with a WARNING or DEPRECATED pragma
1249 is used. See <xref linkend="warning-deprecated-pragma"/> for more
1250 details on the pragmas.</para>
1251
1252 <para>This option is on by default.</para>
1253 </listitem>
1254 </varlistentry>
1255
1256 <varlistentry>
1257 <term><option>-fwarn-amp</option>:</term>
1258 <listitem>
1259 <indexterm><primary><option>-fwarn-amp</option></primary>
1260 </indexterm>
1261 <indexterm><primary>amp</primary></indexterm>
1262 <indexterm><primary>applicative-monad proposal</primary></indexterm>
1263 <para>Causes a warning to be emitted when a definition
1264 is in conflict with the AMP (Applicative-Monad proosal),
1265 namely:
1266 1. Instance of Monad without Applicative;
1267 2. Instance of MonadPlus without Alternative;
1268 3. Custom definitions of join/pure/&lt;*&gt;</para>
1269
1270 <para>This option is on by default.</para>
1271 </listitem>
1272 </varlistentry>
1273
1274 <varlistentry>
1275 <term><option>-fwarn-deprecated-flags</option>:</term>
1276 <listitem>
1277 <indexterm><primary><option>-fwarn-deprecated-flags</option></primary>
1278 </indexterm>
1279 <indexterm><primary>deprecated-flags</primary></indexterm>
1280 <para>Causes a warning to be emitted when a deprecated
1281 commandline flag is used.</para>
1282
1283 <para>This option is on by default.</para>
1284 </listitem>
1285 </varlistentry>
1286
1287 <varlistentry>
1288 <term><option>-fwarn-unsupported-calling-conventions</option>:</term>
1289 <listitem>
1290 <indexterm><primary><option>-fwarn-unsupported-calling-conventions</option></primary>
1291 </indexterm>
1292 <para>Causes a warning to be emitted for foreign declarations
1293 that use unsupported calling conventions. In particular,
1294 if the <literal>stdcall</literal> calling convention is used
1295 on an architecture other than i386 then it will be treated
1296 as <literal>ccall</literal>.</para>
1297 </listitem>
1298 </varlistentry>
1299
1300 <varlistentry>
1301 <term><option>-fwarn-dodgy-foreign-imports</option>:</term>
1302 <listitem>
1303 <indexterm><primary><option>-fwarn-dodgy-foreign-imports</option></primary>
1304 </indexterm>
1305 <para>Causes a warning to be emitted for foreign imports of
1306 the following form:</para>
1307
1308 <programlisting>
1309 foreign import "f" f :: FunPtr t
1310 </programlisting>
1311
1312 <para>on the grounds that it probably should be</para>
1313
1314 <programlisting>
1315 foreign import "&amp;f" f :: FunPtr t
1316 </programlisting>
1317
1318 <para>The first form declares that `f` is a (pure) C
1319 function that takes no arguments and returns a pointer to a
1320 C function with type `t`, whereas the second form declares
1321 that `f` itself is a C function with type `t`. The first
1322 declaration is usually a mistake, and one that is hard to
1323 debug because it results in a crash, hence this
1324 warning.</para>
1325 </listitem>
1326 </varlistentry>
1327
1328 <varlistentry>
1329 <term><option>-fwarn-dodgy-exports</option>:</term>
1330 <listitem>
1331 <indexterm><primary><option>-fwarn-dodgy-exports</option></primary>
1332 </indexterm>
1333 <para>Causes a warning to be emitted when a datatype
1334 <literal>T</literal> is exported
1335 with all constructors, i.e. <literal>T(..)</literal>, but is it
1336 just a type synonym.</para>
1337 <para>Also causes a warning to be emitted when a module is
1338 re-exported, but that module exports nothing.</para>
1339 </listitem>
1340 </varlistentry>
1341
1342 <varlistentry>
1343 <term><option>-fwarn-dodgy-imports</option>:</term>
1344 <listitem>
1345 <indexterm><primary><option>-fwarn-dodgy-imports</option></primary>
1346 </indexterm>
1347 <para>Causes a warning to be emitted in the following cases:</para>
1348 <itemizedlist>
1349 <listitem>
1350 <para>When a datatype <literal>T</literal> is imported with all
1351 constructors, i.e. <literal>T(..)</literal>, but has been
1352 exported abstractly, i.e. <literal>T</literal>.
1353 </para>
1354 </listitem>
1355 <listitem>
1356 <para>When an <literal>import</literal> statement hides an
1357 entity that is not exported.</para>
1358 </listitem>
1359 </itemizedlist>
1360 </listitem>
1361 </varlistentry>
1362
1363 <varlistentry>
1364 <term><option>-fwarn-overflowed-literals</option>:</term>
1365 <listitem>
1366 <indexterm><primary><option>-fwarn-overflowed-literals</option></primary>
1367 </indexterm>
1368 <para>
1369 Causes a warning to be emitted if a literal will overflow,
1370 e.g. <literal>300 :: Word8</literal>.
1371 </para>
1372 </listitem>
1373 </varlistentry>
1374
1375 <varlistentry>
1376 <term><option>-fwarn-empty-enumerations</option>:</term>
1377 <listitem>
1378 <indexterm><primary><option>-fwarn-empty-enumerations</option></primary>
1379 </indexterm>
1380 <para>
1381 Causes a warning to be emitted if an enumeration is
1382 empty, e.g. <literal>[5 .. 3]</literal>.
1383 </para>
1384 </listitem>
1385 </varlistentry>
1386
1387 <varlistentry>
1388 <term><option>-fwarn-lazy-unlifted-bindings</option>:</term>
1389 <listitem>
1390 <indexterm><primary><option>-fwarn-lazy-unlifted-bindings</option></primary>
1391 </indexterm>
1392 <para>This flag is a no-op, and will be removed in GHC 7.10.</para>
1393 </listitem>
1394 </varlistentry>
1395
1396 <varlistentry>
1397 <term><option>-fwarn-duplicate-constraints</option>:</term>
1398 <listitem>
1399 <indexterm><primary><option>-fwarn-duplicate-constraints</option></primary></indexterm>
1400 <indexterm><primary>duplicate constraints, warning</primary></indexterm>
1401
1402 <para>Have the compiler warn about duplicate constraints in a type signature. For
1403 example
1404 <programlisting>
1405 f :: (Eq a, Show a, Eq a) => a -> a
1406 </programlisting>
1407 The warning will indicate the duplicated <literal>Eq a</literal> constraint.
1408 </para>
1409
1410 <para>This option is on by default.</para>
1411 </listitem>
1412 </varlistentry>
1413
1414 <varlistentry>
1415 <term><option>-fwarn-duplicate-exports</option>:</term>
1416 <listitem>
1417 <indexterm><primary><option>-fwarn-duplicate-exports</option></primary></indexterm>
1418 <indexterm><primary>duplicate exports, warning</primary></indexterm>
1419 <indexterm><primary>export lists, duplicates</primary></indexterm>
1420
1421 <para>Have the compiler warn about duplicate entries in
1422 export lists. This is useful information if you maintain
1423 large export lists, and want to avoid the continued export
1424 of a definition after you've deleted (one) mention of it in
1425 the export list.</para>
1426
1427 <para>This option is on by default.</para>
1428 </listitem>
1429 </varlistentry>
1430
1431 <varlistentry>
1432 <term><option>-fwarn-hi-shadowing</option>:</term>
1433 <listitem>
1434 <indexterm><primary><option>-fwarn-hi-shadowing</option></primary></indexterm>
1435 <indexterm><primary>shadowing</primary>
1436 <secondary>interface files</secondary></indexterm>
1437
1438 <para>Causes the compiler to emit a warning when a module or
1439 interface file in the current directory is shadowing one
1440 with the same module name in a library or other
1441 directory.</para>
1442 </listitem>
1443 </varlistentry>
1444
1445 <varlistentry>
1446 <term><option>-fwarn-identities</option>:</term>
1447 <listitem>
1448 <indexterm><primary><option>-fwarn-identities</option></primary></indexterm>
1449 <para>Causes the compiler to emit a warning when a Prelude numeric
1450 conversion converts a type T to the same type T; such calls
1451 are probably no-ops and can be omitted. The functions checked for
1452 are: <literal>toInteger</literal>,
1453 <literal>toRational</literal>,
1454 <literal>fromIntegral</literal>,
1455 and <literal>realToFrac</literal>.
1456 </para>
1457 </listitem>
1458 </varlistentry>
1459
1460 <varlistentry>
1461 <term><option>-fwarn-implicit-prelude</option>:</term>
1462 <listitem>
1463 <indexterm><primary><option>-fwarn-implicit-prelude</option></primary></indexterm>
1464 <indexterm><primary>implicit prelude, warning</primary></indexterm>
1465 <para>Have the compiler warn if the Prelude is implicitly
1466 imported. This happens unless either the Prelude module is
1467 explicitly imported with an <literal>import ... Prelude ...</literal>
1468 line, or this implicit import is disabled (either by
1469 <option>-XNoImplicitPrelude</option> or a
1470 <literal>LANGUAGE NoImplicitPrelude</literal> pragma).</para>
1471
1472 <para>Note that no warning is given for syntax that implicitly
1473 refers to the Prelude, even if <option>-XNoImplicitPrelude</option>
1474 would change whether it refers to the Prelude.
1475 For example, no warning is given when
1476 <literal>368</literal> means
1477 <literal>Prelude.fromInteger (368::Prelude.Integer)</literal>
1478 (where <literal>Prelude</literal> refers to the actual Prelude module,
1479 regardless of the imports of the module being compiled).</para>
1480
1481 <para>This warning is off by default.</para>
1482 </listitem>
1483 </varlistentry>
1484
1485 <varlistentry>
1486 <term><option>-fwarn-incomplete-patterns</option>,
1487 <option>-fwarn-incomplete-uni-patterns</option>:
1488 </term>
1489 <listitem>
1490 <indexterm><primary><option>-fwarn-incomplete-patterns</option></primary></indexterm>
1491 <indexterm><primary><option>-fwarn-incomplete-uni-patterns</option></primary></indexterm>
1492 <indexterm><primary>incomplete patterns, warning</primary></indexterm>
1493 <indexterm><primary>patterns, incomplete</primary></indexterm>
1494
1495 <para>The option <option>-fwarn-incomplete-patterns</option> warns
1496 about places where
1497 a pattern-match might fail at runtime.
1498 The function
1499 <function>g</function> below will fail when applied to
1500 non-empty lists, so the compiler will emit a warning about
1501 this when <option>-fwarn-incomplete-patterns</option> is
1502 enabled.
1503
1504 <programlisting>
1505 g [] = 2
1506 </programlisting>
1507
1508 This option isn't enabled by default because it can be
1509 a bit noisy, and it doesn't always indicate a bug in the
1510 program. However, it's generally considered good practice
1511 to cover all the cases in your functions, and it is switched
1512 on by <option>-W</option>.</para>
1513
1514 <para>The flag <option>-fwarn-incomplete-uni-patterns</option> is
1515 similar, except that it
1516 applies only to lambda-expressions and pattern bindings, constructs
1517 that only allow a single pattern:
1518
1519 <programlisting>
1520 h = \[] -> 2
1521 Just k = f y
1522 </programlisting>
1523
1524 </para>
1525 </listitem>
1526 </varlistentry>
1527
1528 <varlistentry>
1529 <term><option>-fwarn-incomplete-record-updates</option>:</term>
1530 <listitem>
1531 <indexterm><primary><option>-fwarn-incomplete-record-updates</option></primary></indexterm>
1532 <indexterm><primary>incomplete record updates, warning</primary></indexterm>
1533 <indexterm><primary>record updates, incomplete</primary></indexterm>
1534
1535 <para>The function
1536 <function>f</function> below will fail when applied to
1537 <literal>Bar</literal>, so the compiler will emit a warning about
1538 this when <option>-fwarn-incomplete-record-updates</option> is
1539 enabled.</para>
1540
1541 <programlisting>
1542 data Foo = Foo { x :: Int }
1543 | Bar
1544
1545 f :: Foo -> Foo
1546 f foo = foo { x = 6 }
1547 </programlisting>
1548
1549 <para>This option isn't enabled by default because it can be
1550 very noisy, and it often doesn't indicate a bug in the
1551 program.</para>
1552 </listitem>
1553 </varlistentry>
1554
1555 <varlistentry>
1556 <term>
1557 <option>-fwarn-missing-fields</option>:
1558 <indexterm><primary><option>-fwarn-missing-fields</option></primary></indexterm>
1559 <indexterm><primary>missing fields, warning</primary></indexterm>
1560 <indexterm><primary>fields, missing</primary></indexterm>
1561 </term>
1562 <listitem>
1563
1564 <para>This option is on by default, and warns you whenever
1565 the construction of a labelled field constructor isn't
1566 complete, missing initializers for one or more fields. While
1567 not an error (the missing fields are initialised with
1568 bottoms), it is often an indication of a programmer error.</para>
1569 </listitem>
1570 </varlistentry>
1571
1572 <varlistentry>
1573 <term>
1574 <option>-fwarn-missing-import-lists</option>:
1575 <indexterm><primary><option>-fwarn-import-lists</option></primary></indexterm>
1576 <indexterm><primary>missing import lists, warning</primary></indexterm>
1577 <indexterm><primary>import lists, missing</primary></indexterm>
1578 </term>
1579 <listitem>
1580
1581 <para>This flag warns if you use an unqualified
1582 <literal>import</literal> declaration
1583 that does not explicitly list the entities brought into scope. For
1584 example
1585 </para>
1586
1587 <programlisting>
1588 module M where
1589 import X( f )
1590 import Y
1591 import qualified Z
1592 p x = f x x
1593 </programlisting>
1594
1595 <para>
1596 The <option>-fwarn-import-lists</option> flag will warn about the import
1597 of <literal>Y</literal> but not <literal>X</literal>
1598 If module <literal>Y</literal> is later changed to export (say) <literal>f</literal>,
1599 then the reference to <literal>f</literal> in <literal>M</literal> will become
1600 ambiguous. No warning is produced for the import of <literal>Z</literal>
1601 because extending <literal>Z</literal>'s exports would be unlikely to produce
1602 ambiguity in <literal>M</literal>.
1603 </para>
1604 </listitem>
1605 </varlistentry>
1606
1607 <varlistentry>
1608 <term><option>-fwarn-missing-methods</option>:</term>
1609 <listitem>
1610 <indexterm><primary><option>-fwarn-missing-methods</option></primary></indexterm>
1611 <indexterm><primary>missing methods, warning</primary></indexterm>
1612 <indexterm><primary>methods, missing</primary></indexterm>
1613
1614 <para>This option is on by default, and warns you whenever
1615 an instance declaration is missing one or more methods, and
1616 the corresponding class declaration has no default
1617 declaration for them.</para>
1618 <para>The warning is suppressed if the method name
1619 begins with an underscore. Here's an example where this is useful:
1620 <programlisting>
1621 class C a where
1622 _simpleFn :: a -> String
1623 complexFn :: a -> a -> String
1624 complexFn x y = ... _simpleFn ...
1625 </programlisting>
1626 The idea is that: (a) users of the class will only call <literal>complexFn</literal>;
1627 never <literal>_simpleFn</literal>; and (b)
1628 instance declarations can define either <literal>complexFn</literal> or <literal>_simpleFn</literal>.
1629 </para>
1630 <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>
1631 </listitem>
1632 </varlistentry>
1633
1634 <varlistentry>
1635 <term><option>-fwarn-missing-signatures</option>:</term>
1636 <listitem>
1637 <indexterm><primary><option>-fwarn-missing-signatures</option></primary></indexterm>
1638 <indexterm><primary>type signatures, missing</primary></indexterm>
1639
1640 <para>If you would like GHC to check that every top-level
1641 function/value has a type signature, use the
1642 <option>-fwarn-missing-signatures</option> option. As part of
1643 the warning GHC also reports the inferred type. The
1644 option is off by default.</para>
1645 </listitem>
1646 </varlistentry>
1647
1648 <varlistentry>
1649 <term><option>-fwarn-missing-exported-sigs</option>:</term>
1650 <listitem>
1651 <indexterm><primary><option>-fwarn-missing-exported-sigs</option></primary></indexterm>
1652 <indexterm><primary>type signatures, missing</primary></indexterm>
1653
1654 <para>If you would like GHC to check that every exported top-level
1655 function/value has a type signature, but not check unexported values, use the
1656 <option>-fwarn-missing-exported-sigs</option> option. This option
1657 takes precedence over <option>-fwarn-missing-signatures</option>.
1658 As part of the warning GHC also reports the inferred type. The
1659 option is off by default.</para>
1660 </listitem>
1661 </varlistentry>
1662
1663 <varlistentry>
1664 <term><option>-fwarn-missing-local-sigs</option>:</term>
1665 <listitem>
1666 <indexterm><primary><option>-fwarn-missing-local-sigs</option></primary></indexterm>
1667 <indexterm><primary>type signatures, missing</primary></indexterm>
1668
1669 <para>If you use the
1670 <option>-fwarn-missing-local-sigs</option> flag GHC will warn
1671 you about any polymorphic local bindings. As part of
1672 the warning GHC also reports the inferred type. The
1673 option is off by default.</para>
1674 </listitem>
1675 </varlistentry>
1676
1677 <varlistentry>
1678 <term><option>-fwarn-name-shadowing</option>:</term>
1679 <listitem>
1680 <indexterm><primary><option>-fwarn-name-shadowing</option></primary></indexterm>
1681 <indexterm><primary>shadowing, warning</primary></indexterm>
1682
1683 <para>This option causes a warning to be emitted whenever an
1684 inner-scope value has the same name as an outer-scope value,
1685 i.e. the inner value shadows the outer one. This can catch
1686 typographical errors that turn into hard-to-find bugs, e.g.,
1687 in the inadvertent capture of what would be a recursive call in
1688 <literal>f = ... let f = id in ... f ...</literal>.</para>
1689 <para>The warning is suppressed for names beginning with an underscore. For example
1690 <programlisting>
1691 f x = do { _ignore &lt;- this; _ignore &lt;- that; return (the other) }
1692 </programlisting>
1693 </para>
1694 </listitem>
1695 </varlistentry>
1696
1697 <varlistentry>
1698 <term><option>-fwarn-orphans, -fwarn-auto-orphans</option>:</term>
1699 <listitem>
1700 <indexterm><primary><option>-fwarn-orphans</option></primary></indexterm>
1701 <indexterm><primary><option>-fwarn-auto-orphans</option></primary></indexterm>
1702 <indexterm><primary>orphan instances, warning</primary></indexterm>
1703 <indexterm><primary>orphan rules, warning</primary></indexterm>
1704
1705 <para>These flags cause a warning to be emitted whenever the
1706 module contains an "orphan" instance declaration or rewrite rule.
1707 An instance declaration is an orphan if it appears in a module in
1708 which neither the class nor the type being instanced are declared
1709 in the same module. A rule is an orphan if it is a rule for a
1710 function declared in another module. A module containing any
1711 orphans is called an orphan module.</para>
1712 <para>The trouble with orphans is that GHC must pro-actively read the interface
1713 files for all orphan modules, just in case their instances or rules
1714 play a role, whether or not the module's interface would otherwise
1715 be of any use. See <xref linkend="orphan-modules"/> for details.
1716 </para>
1717 <para>The flag <option>-fwarn-orphans</option> warns about user-written
1718 orphan rules or instances. The flag <option>-fwarn-auto-orphans</option>
1719 warns about automatically-generated orphan rules, notably as a result of
1720 specialising functions, for type classes (<literal>Specialise</literal>)
1721 or argument values (<literal>-fspec-constr</literal>).</para>
1722 </listitem>
1723 </varlistentry>
1724
1725 <varlistentry>
1726 <term>
1727 <option>-fwarn-overlapping-patterns</option>:
1728 <indexterm><primary><option>-fwarn-overlapping-patterns</option></primary></indexterm>
1729 <indexterm><primary>overlapping patterns, warning</primary></indexterm>
1730 <indexterm><primary>patterns, overlapping</primary></indexterm>
1731 </term>
1732 <listitem>
1733 <para>By default, the compiler will warn you if a set of
1734 patterns are overlapping, e.g.,</para>
1735
1736 <programlisting>
1737 f :: String -&#62; Int
1738 f [] = 0
1739 f (_:xs) = 1
1740 f "2" = 2
1741 </programlisting>
1742
1743 <para>where the last pattern match in <function>f</function>
1744 won't ever be reached, as the second pattern overlaps
1745 it. More often than not, redundant patterns is a programmer
1746 mistake/error, so this option is enabled by default.</para>
1747 </listitem>
1748 </varlistentry>
1749
1750 <varlistentry>
1751 <term><option>-fwarn-tabs</option>:</term>
1752 <listitem>
1753 <indexterm><primary><option>-fwarn-tabs</option></primary></indexterm>
1754 <indexterm><primary>tabs, warning</primary></indexterm>
1755 <para>Have the compiler warn if there are tabs in your source
1756 file.</para>
1757
1758 <para>This warning is off by default.</para>
1759 </listitem>
1760 </varlistentry>
1761
1762 <varlistentry>
1763 <term><option>-fwarn-type-defaults</option>:</term>
1764 <listitem>
1765 <indexterm><primary><option>-fwarn-type-defaults</option></primary></indexterm>
1766 <indexterm><primary>defaulting mechanism, warning</primary></indexterm>
1767 <para>Have the compiler warn/inform you where in your source
1768 the Haskell defaulting mechanism for numeric types kicks
1769 in. This is useful information when converting code from a
1770 context that assumed one default into one with another,
1771 e.g., the &lsquo;default default&rsquo; for Haskell 1.4 caused the
1772 otherwise unconstrained value <constant>1</constant> to be
1773 given the type <literal>Int</literal>, whereas Haskell 98
1774 and later
1775 defaults it to <literal>Integer</literal>. This may lead to
1776 differences in performance and behaviour, hence the
1777 usefulness of being non-silent about this.</para>
1778
1779 <para>This warning is off by default.</para>
1780 </listitem>
1781 </varlistentry>
1782
1783 <varlistentry>
1784 <term><option>-fwarn-monomorphism-restriction</option>:</term>
1785 <listitem>
1786 <indexterm><primary><option>-fwarn-monomorphism-restriction</option></primary></indexterm>
1787 <indexterm><primary>monomorphism restriction, warning</primary></indexterm>
1788 <para>Have the compiler warn/inform you where in your source
1789 the Haskell Monomorphism Restriction is applied. If applied silently
1790 the MR can give rise to unexpected behaviour, so it can be helpful
1791 to have an explicit warning that it is being applied.</para>
1792
1793 <para>This warning is off by default.</para>
1794 </listitem>
1795 </varlistentry>
1796
1797 <varlistentry>
1798 <term><option>-fwarn-unticked-promoted-constructors</option>:</term>
1799 <listitem>
1800 <indexterm><primary><option>-fwarn-unticked-promoted-constructors</option></primary></indexterm>
1801 <indexterm><primary>promoted constructor, warning</primary></indexterm>
1802 <para>Warn if a promoted data constructor is used without a tick preceding it's name.
1803 </para>
1804 <para>For example:
1805 </para>
1806 <programlisting>
1807 data Nat = Succ Nat | Zero
1808
1809 data Vec n s where
1810 Nil :: Vec Zero a
1811 Cons :: a -> Vec n a -> Vec (Succ n) a
1812 </programlisting>
1813 <para> Will raise two warnings because <function>Zero</function>
1814 and <function>Succ</function> are not written as <function>'Zero</function> and
1815 <function>'Succ</function>.
1816 </para>
1817 <para>This warning is off by default.</para>
1818 </listitem>
1819 </varlistentry>
1820
1821 <varlistentry>
1822 <term><option>-fwarn-unused-binds</option>:</term>
1823 <listitem>
1824 <indexterm><primary><option>-fwarn-unused-binds</option></primary></indexterm>
1825 <indexterm><primary>unused binds, warning</primary></indexterm>
1826 <indexterm><primary>binds, unused</primary></indexterm>
1827 <para>Report any function definitions (and local bindings)
1828 which are unused. More precisely:
1829
1830 <itemizedlist>
1831 <listitem><para>Warn if a binding brings into scope a variable that is not used,
1832 except if the variable's name starts with an underscore. The "starts-with-underscore"
1833 condition provides a way to selectively disable the warning.
1834 </para>
1835 <para>
1836 A variable is regarded as "used" if
1837 <itemizedlist>
1838 <listitem><para>It is exported, or</para></listitem>
1839 <listitem><para>It appears in the right hand side of a binding that binds at
1840 least one used variable that is used</para></listitem>
1841 </itemizedlist>
1842 For example
1843 <programlisting>
1844 module A (f) where
1845 f = let (p,q) = rhs1 in t p -- Warning about unused q
1846 t = rhs3 -- No warning: f is used, and hence so is t
1847 g = h x -- Warning: g unused
1848 h = rhs2 -- Warning: h is only used in the right-hand side of another unused binding
1849 _w = True -- No warning: _w starts with an underscore
1850 </programlisting>
1851 </para></listitem>
1852
1853 <listitem><para>
1854 Warn if a pattern binding binds no variables at all, unless it is a lone, possibly-banged, wild-card pattern.
1855 For example:
1856 <programlisting>
1857 Just _ = rhs3 -- Warning: unused pattern binding
1858 (_, _) = rhs4 -- Warning: unused pattern binding
1859 _ = rhs3 -- No warning: lone wild-card pattern
1860 !_ = rhs4 -- No warning: banged wild-card pattern; behaves like seq
1861 </programlisting>
1862 The motivation for allowing lone wild-card patterns is they
1863 are not very different from <literal>_v = rhs3</literal>,
1864 which elicits no warning; and they can be useful to add a type
1865 constraint, e.g. <literal>_ = x::Int</literal>. A lone
1866 banged wild-card pattern is is useful as an alternative
1867 (to <literal>seq</literal>) way to force evaluation.
1868 </para>
1869 </listitem>
1870 </itemizedlist>
1871 </para>
1872 </listitem>
1873 </varlistentry>
1874
1875 <varlistentry>
1876 <term><option>-fwarn-unused-imports</option>:</term>
1877 <listitem>
1878 <indexterm><primary><option>-fwarn-unused-imports</option></primary></indexterm>
1879 <indexterm><primary>unused imports, warning</primary></indexterm>
1880 <indexterm><primary>imports, unused</primary></indexterm>
1881
1882 <para>Report any modules that are explicitly imported but
1883 never used. However, the form <literal>import M()</literal> is
1884 never reported as an unused import, because it is a useful idiom
1885 for importing instance declarations, which are anonymous in Haskell.</para>
1886 </listitem>
1887 </varlistentry>
1888
1889 <varlistentry>
1890 <term><option>-fwarn-unused-matches</option>:</term>
1891 <listitem>
1892 <indexterm><primary><option>-fwarn-unused-matches</option></primary></indexterm>
1893 <indexterm><primary>unused matches, warning</primary></indexterm>
1894 <indexterm><primary>matches, unused</primary></indexterm>
1895
1896 <para>Report all unused variables which arise from pattern
1897 matches, including patterns consisting of a single variable.
1898 For instance <literal>f x y = []</literal> would report
1899 <varname>x</varname> and <varname>y</varname> as unused. The
1900 warning is suppressed if the variable name begins with an underscore, thus:
1901 <programlisting>
1902 f _x = True
1903 </programlisting>
1904 </para>
1905 </listitem>
1906 </varlistentry>
1907
1908 <varlistentry>
1909 <term><option>-fwarn-unused-do-bind</option>:</term>
1910 <listitem>
1911 <indexterm><primary><option>-fwarn-unused-do-bind</option></primary></indexterm>
1912 <indexterm><primary>unused do binding, warning</primary></indexterm>
1913 <indexterm><primary>do binding, unused</primary></indexterm>
1914
1915 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1916 that appear to silently throw information away.
1917 For instance <literal>do { mapM popInt xs ; return 10 }</literal> would report
1918 the first statement in the <literal>do</literal> block as suspicious,
1919 as it has the type <literal>StackM [Int]</literal> and not <literal>StackM ()</literal>, but that
1920 <literal>[Int]</literal> value is not bound to anything. The warning is suppressed by
1921 explicitly mentioning in the source code that your program is throwing something away:
1922 <programlisting>
1923 do { _ &lt;- mapM popInt xs ; return 10 }
1924 </programlisting>
1925 Of course, in this particular situation you can do even better:
1926 <programlisting>
1927 do { mapM_ popInt xs ; return 10 }
1928 </programlisting>
1929 </para>
1930 </listitem>
1931 </varlistentry>
1932
1933 <varlistentry>
1934 <term><option>-fwarn-context-quantification</option>:</term>
1935 <listitem>
1936 <indexterm><primary><option>-fwarn-context-quantification</option></primary></indexterm>
1937 <indexterm><primary>implicit context quantification, warning</primary></indexterm>
1938 <indexterm><primary>context, implicit quantification</primary></indexterm>
1939
1940 <para>Report if a variable is quantified only due to its presence
1941 in a context (see <xref linkend="universal-quantification"/>). For example,
1942 <programlisting>
1943 type T a = Monad m => a -> f a
1944 </programlisting>
1945 It is recommended to write this polymorphic type as
1946 <programlisting>
1947 type T a = forall m. Monad m => a -> f a
1948 </programlisting>
1949 instead.
1950 </para>
1951 </listitem>
1952 </varlistentry>
1953
1954 <varlistentry>
1955 <term><option>-fwarn-wrong-do-bind</option>:</term>
1956 <listitem>
1957 <indexterm><primary><option>-fwarn-wrong-do-bind</option></primary></indexterm>
1958 <indexterm><primary>apparently erroneous do binding, warning</primary></indexterm>
1959 <indexterm><primary>do binding, apparently erroneous</primary></indexterm>
1960
1961 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1962 that appear to lack a binding.
1963 For instance <literal>do { return (popInt 10) ; return 10 }</literal> would report
1964 the first statement in the <literal>do</literal> block as suspicious,
1965 as it has the type <literal>StackM (StackM Int)</literal> (which consists of two nested applications
1966 of the same monad constructor), but which is not then &quot;unpacked&quot; by binding the result.
1967 The warning is suppressed by explicitly mentioning in the source code that your program is throwing something away:
1968 <programlisting>
1969 do { _ &lt;- return (popInt 10) ; return 10 }
1970 </programlisting>
1971 For almost all sensible programs this will indicate a bug, and you probably intended to write:
1972 <programlisting>
1973 do { popInt 10 ; return 10 }
1974 </programlisting>
1975 </para>
1976 </listitem>
1977 </varlistentry>
1978
1979 <varlistentry>
1980 <term><option>-fwarn-inline-rule-shadowing</option>:</term>
1981 <listitem>
1982 <indexterm><primary><option>-fwarn-inline-rule-shadowing</option></primary></indexterm>
1983 <para>Warn if a rewrite RULE might fail to fire because the function might be
1984 inlined before the rule has a chance to fire. See <xref linkend="rules-inline"/>.
1985 </para>
1986 </listitem>
1987 </varlistentry>
1988
1989 </variablelist>
1990
1991 <para>If you're feeling really paranoid, the
1992 <option>-dcore-lint</option>
1993 option<indexterm><primary><option>-dcore-lint</option></primary></indexterm>
1994 is a good choice. It turns on heavyweight intra-pass
1995 sanity-checking within GHC. (It checks GHC's sanity, not
1996 yours.)</para>
1997
1998 </sect1>
1999
2000 &packages;
2001
2002 <sect1 id="options-optimise">
2003 <title>Optimisation (code improvement)</title>
2004
2005 <indexterm><primary>optimisation</primary></indexterm>
2006 <indexterm><primary>improvement, code</primary></indexterm>
2007
2008 <para>The <option>-O*</option> options specify convenient
2009 &ldquo;packages&rdquo; of optimisation flags; the
2010 <option>-f*</option> options described later on specify
2011 <emphasis>individual</emphasis> optimisations to be turned on/off;
2012 the <option>-m*</option> options specify
2013 <emphasis>machine-specific</emphasis> optimisations to be turned
2014 on/off.</para>
2015
2016 <sect2 id="optimise-pkgs">
2017 <title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.</title>
2018
2019 <para>There are <emphasis>many</emphasis> options that affect
2020 the quality of code produced by GHC. Most people only have a
2021 general goal, something like &ldquo;Compile quickly&rdquo; or
2022 &ldquo;Make my program run like greased lightning.&rdquo; The
2023 following &ldquo;packages&rdquo; of optimisations (or lack
2024 thereof) should suffice.</para>
2025
2026 <para>Note that higher optimisation levels cause more
2027 cross-module optimisation to be performed, which can have an
2028 impact on how much of your program needs to be recompiled when
2029 you change something. This is one reason to stick to
2030 no-optimisation when developing code.</para>
2031
2032 <variablelist>
2033
2034 <varlistentry>
2035 <term>
2036 No <option>-O*</option>-type option specified:
2037 <indexterm><primary>-O* not specified</primary></indexterm>
2038 </term>
2039 <listitem>
2040 <para>This is taken to mean: &ldquo;Please compile
2041 quickly; I'm not over-bothered about compiled-code
2042 quality.&rdquo; So, for example: <command>ghc -c
2043 Foo.hs</command></para>
2044 </listitem>
2045 </varlistentry>
2046
2047 <varlistentry>
2048 <term>
2049 <option>-O0</option>:
2050 <indexterm><primary><option>-O0</option></primary></indexterm>
2051 </term>
2052 <listitem>
2053 <para>Means &ldquo;turn off all optimisation&rdquo;,
2054 reverting to the same settings as if no
2055 <option>-O</option> options had been specified. Saying
2056 <option>-O0</option> can be useful if
2057 eg. <command>make</command> has inserted a
2058 <option>-O</option> on the command line already.</para>
2059 </listitem>
2060 </varlistentry>
2061
2062 <varlistentry>
2063 <term>
2064 <option>-O</option> or <option>-O1</option>:
2065 <indexterm><primary>-O option</primary></indexterm>
2066 <indexterm><primary>-O1 option</primary></indexterm>
2067 <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
2068 </term>
2069 <listitem>
2070 <para>Means: &ldquo;Generate good-quality code without
2071 taking too long about it.&rdquo; Thus, for example:
2072 <command>ghc -c -O Main.lhs</command></para>
2073 </listitem>
2074 </varlistentry>
2075
2076 <varlistentry>
2077 <term>
2078 <option>-O2</option>:
2079 <indexterm><primary>-O2 option</primary></indexterm>
2080 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
2081 </term>
2082 <listitem>
2083 <para>Means: &ldquo;Apply every non-dangerous
2084 optimisation, even if it means significantly longer
2085 compile times.&rdquo;</para>
2086
2087 <para>The avoided &ldquo;dangerous&rdquo; optimisations
2088 are those that can make runtime or space
2089 <emphasis>worse</emphasis> if you're unlucky. They are
2090 normally turned on or off individually.</para>
2091
2092 <para>At the moment, <option>-O2</option> is
2093 <emphasis>unlikely</emphasis> to produce better code than
2094 <option>-O</option>.</para>
2095 </listitem>
2096 </varlistentry>
2097
2098 <varlistentry>
2099 <term>
2100 <option>-Odph</option>:
2101 <indexterm><primary>-Odph</primary></indexterm>
2102 <indexterm><primary>optimise</primary><secondary>DPH</secondary></indexterm>
2103 </term>
2104 <listitem>
2105 <para>Enables all <option>-O2</option> optimisation, sets
2106 <option>-fmax-simplifier-iterations=20</option>
2107 and <option>-fsimplifier-phases=3</option>. Designed for use with
2108 <link linkend="dph">Data Parallel Haskell (DPH)</link>.</para>
2109 </listitem>
2110 </varlistentry>
2111
2112 </variablelist>
2113
2114 <para>We don't use a <option>-O*</option> flag for day-to-day
2115 work. We use <option>-O</option> to get respectable speed;
2116 e.g., when we want to measure something. When we want to go for
2117 broke, we tend to use <option>-O2</option> (and we go for
2118 lots of coffee breaks).</para>
2119
2120 <para>The easiest way to see what <option>-O</option> (etc.)
2121 &ldquo;really mean&rdquo; is to run with <option>-v</option>,
2122 then stand back in amazement.</para>
2123 </sect2>
2124
2125 <sect2 id="options-f">
2126 <title><option>-f*</option>: platform-independent flags</title>
2127
2128 <indexterm><primary>-f* options (GHC)</primary></indexterm>
2129 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
2130
2131 <para>These flags turn on and off individual optimisations.
2132 Flags marked as <emphasis>Enabled by default</emphasis> are
2133 enabled by <option>-O</option>, and as such you shouldn't
2134 need to set any of them explicitly. A flag <option>-fwombat</option>
2135 can be negated by saying <option>-fno-wombat</option>.
2136 See <xref linkend="options-f-compact"/> for a compact list.
2137 </para>
2138
2139 <variablelist>
2140 <varlistentry>
2141 <term>
2142 <option>-fcase-merge</option>
2143 <indexterm><primary><option></option></primary></indexterm>
2144 </term>
2145 <listitem>
2146 <para><emphasis>On by default.</emphasis>
2147 Merge immediately-nested case expressions that scrutinse the same variable. Example
2148 <programlisting>
2149 case x of
2150 Red -> e1
2151 _ -> case x of
2152 Blue -> e2
2153 Green -> e3
2154 ==>
2155 case x of
2156 Red -> e1
2157 Blue -> e2
2158 Green -> e2
2159 </programlisting>
2160 </para>
2161 </listitem>
2162 </varlistentry>
2163
2164 <varlistentry>
2165 <term>
2166 <option>-fcall-arity</option>
2167 <indexterm><primary><option>-fcall-arity</option></primary></indexterm>
2168 </term>
2169 <listitem>
2170 <para><emphasis>On by default.</emphasis>.
2171 </para>
2172 </listitem>
2173 </varlistentry>
2174
2175 <varlistentry>
2176 <term>
2177 <option>-fcmm-elim-common-blocks</option>
2178 <indexterm><primary><option>-felim-common-blocks</option></primary></indexterm>
2179 </term>
2180 <listitem>
2181 <para><emphasis>On by default.</emphasis>. Enables the common block
2182 elimination optimisation in the code generator. This optimisation
2183 attempts to find identical Cmm blocks and eliminate the duplicates.
2184 </para>
2185 </listitem>
2186 </varlistentry>
2187
2188 <varlistentry>
2189 <term>
2190 <option>-fcmm-sink</option>
2191 <indexterm><primary><option>-fcmm-sink</option></primary></indexterm>
2192 </term>
2193 <listitem>
2194 <para><emphasis>On by default.</emphasis>. Enables the sinking pass
2195 in the code generator. This optimisation
2196 attempts to find identical Cmm blocks and eliminate the duplicates
2197 attempts to move variable bindings closer to their usage sites. It
2198 also inlines simple expressions like literals or registers.
2199 </para>
2200 </listitem>
2201 </varlistentry>
2202
2203 <varlistentry>
2204 <term>
2205 <option>-fcpr-off</option>
2206 <indexterm><primary><option>-fcpr-Off</option></primary></indexterm>
2207 </term>
2208 <listitem>
2209 <para>Switch off CPR analysis in the demand analyser.
2210 </para>
2211 </listitem>
2212 </varlistentry>
2213
2214 <varlistentry>
2215 <term>
2216 <option>-fcse</option>
2217 <indexterm><primary><option>-fcse</option></primary></indexterm>
2218 </term>
2219 <listitem>
2220 <para><emphasis>On by default.</emphasis>. Enables the common-sub-expression
2221 elimination optimisation.
2222 Switching this off can be useful if you have some <literal>unsafePerformIO</literal>
2223 expressions that you don't want commoned-up.</para>
2224 </listitem>
2225 </varlistentry>
2226
2227 <varlistentry>
2228 <term>
2229 <option>-fdicts-cheap</option>
2230 <indexterm><primary><option></option></primary></indexterm>
2231 </term>
2232 <listitem>
2233 <para>A very experimental flag that makes dictionary-valued
2234 expressions seem cheap to the optimiser.
2235 </para>
2236 </listitem>
2237 </varlistentry>
2238
2239 <varlistentry>
2240 <term>
2241 <option>-fdicts-strict</option>
2242 <indexterm><primary><option></option></primary></indexterm>
2243 </term>
2244 <listitem>
2245 <para>Make dictionaries strict.
2246 </para>
2247 </listitem>
2248 </varlistentry>
2249
2250 <varlistentry>
2251 <term>
2252 <option>-fdmd-tx-dict-sel</option>
2253 <indexterm><primary><option>-fdmd-tx-dict-sel</option></primary></indexterm>
2254 </term>
2255 <listitem>
2256 <para><emphasis>On by default for <option>-O0</option>, <option>-O</option>,
2257 <option>-O2</option>.</emphasis>
2258 </para>
2259 <para>Use a special demand transformer for dictionary selectors.
2260 </para>
2261 </listitem>
2262 </varlistentry>
2263
2264 <varlistentry>
2265 <term>
2266 <option>-fdo-eta-reduction</option>
2267 <indexterm><primary><option></option></primary></indexterm>
2268 </term>
2269 <listitem>
2270 <para><emphasis>On by default.</emphasis>
2271 Eta-reduce lambda expressions, if doing so gets rid of a whole
2272 group of lambdas.
2273 </para>
2274 </listitem>
2275 </varlistentry>
2276
2277 <varlistentry>
2278 <term>
2279 <option>-fdo-lambda-eta-expansion</option>
2280 <indexterm><primary><option></option></primary></indexterm>
2281 </term>
2282 <listitem>
2283 <para><emphasis>On by default.</emphasis>
2284 Eta-expand let-bindings to increase their arity.
2285 </para>
2286 </listitem>
2287 </varlistentry>
2288
2289 <varlistentry>
2290 <term>
2291 <option>-feager-blackholing</option>
2292 <indexterm><primary><option></option></primary></indexterm>
2293 </term>
2294 <listitem>
2295 <para>Usually GHC black-holes a thunk only when it switches
2296 threads. This flag makes it do so as soon as the thunk is
2297 entered. See <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/">
2298 Haskell on a shared-memory multiprocessor</ulink>.
2299 </para>
2300 </listitem>
2301 </varlistentry>
2302
2303 <varlistentry>
2304 <term>
2305 <option>-fexcess-precision</option>
2306 <indexterm><primary><option>-fexcess-precision</option></primary></indexterm>
2307 </term>
2308 <listitem>
2309 <para>When this option is given, intermediate floating
2310 point values can have a <emphasis>greater</emphasis>
2311 precision/range than the final type. Generally this is a
2312 good thing, but some programs may rely on the exact
2313 precision/range of
2314 <literal>Float</literal>/<literal>Double</literal> values
2315 and should not use this option for their compilation.</para>
2316
2317 <para>
2318 Note that the 32-bit x86 native code generator only
2319 supports excess-precision mode, so neither
2320 <option>-fexcess-precision</option> nor
2321 <option>-fno-excess-precision</option> has any effect.
2322 This is a known bug, see <xref linkend="bugs-ghc" />.
2323 </para>
2324 </listitem>
2325 </varlistentry>
2326
2327 <varlistentry>
2328 <term>
2329 <option>-fexpose-all-unfoldings</option>
2330 <indexterm><primary><option></option></primary></indexterm>
2331 </term>
2332 <listitem>
2333 <para>An experimental flag to expose all unfoldings, even for very
2334 large or recursive functions. This allows for all functions to be
2335 inlined while usually GHC would avoid inlining larger functions.
2336 </para>
2337 </listitem>
2338 </varlistentry>
2339
2340 <varlistentry>
2341 <term>
2342 <option>-ffloat-in</option>
2343 <indexterm><primary><option></option></primary></indexterm>
2344 </term>
2345 <listitem>
2346 <para><emphasis>On by default.</emphasis>
2347 Float let-bindings inwards, nearer their binding site. See
2348 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">
2349 Let-floating: moving bindings to give faster programs (ICFP'96)</ulink>.
2350 </para>
2351
2352 <para>This optimisation moves let bindings closer to their use
2353 site. The benefit here is that this may avoid unnecessary
2354 allocation if the branch the let is now on is never executed. It
2355 also enables other optimisation passes to work more effectively
2356 as they have more information locally.
2357 </para>
2358
2359 <para>This optimisation isn't always beneficial though (so GHC
2360 applies some heuristics to decide when to apply it). The details
2361 get complicated but a simple example is that it is often beneficial
2362 to move let bindings outwards so that multiple let bindings can be
2363 grouped into a larger single let binding, effectively batching
2364 their allocation and helping the garbage collector and allocator.
2365 </para>
2366 </listitem>
2367 </varlistentry>
2368
2369 <varlistentry>
2370 <term>
2371 <option>-ffull-laziness</option>
2372 <indexterm><primary><option>-ffull-laziness</option></primary></indexterm>
2373 </term>
2374 <listitem>
2375 <para><emphasis>On by default.</emphasis>
2376 Run the full laziness optimisation (also known as let-floating),
2377 which floats let-bindings outside enclosing lambdas, in the hope
2378 they will be thereby be computed less often. See
2379 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">Let-floating:
2380 moving bindings to give faster programs (ICFP'96)</ulink>.
2381 Full laziness increases sharing, which can lead to increased memory
2382 residency.
2383 </para>
2384
2385 <para>NOTE: GHC doesn't implement complete full-laziness.
2386 When optimisation in on, and <option>-fno-full-laziness</option>
2387 is not given, some transformations that increase sharing are
2388 performed, such as extracting repeated computations from a loop.
2389 These are the same transformations that a fully lazy
2390 implementation would do, the difference is that GHC doesn't
2391 consistently apply full-laziness, so don't rely on it.
2392 </para>
2393 </listitem>
2394 </varlistentry>
2395
2396 <varlistentry>
2397 <term>
2398 <option>-ffun-to-thunk</option>
2399 <indexterm><primary><option>-ffun-to-thunk</option></primary></indexterm>
2400 </term>
2401 <listitem>
2402 <para>Worker-wrapper removes unused arguments, but usually we do
2403 not remove them all, lest it turn a function closure into a thunk,
2404 thereby perhaps creating a space leak and/or disrupting inlining.
2405 This flag allows worker/wrapper to remove <emphasis>all</emphasis>
2406 value lambdas. Off by default.
2407 </para>
2408 </listitem>
2409 </varlistentry>
2410
2411 <varlistentry>
2412 <term>
2413 <option>-fignore-asserts</option>
2414 <indexterm><primary><option>-fignore-asserts</option></primary></indexterm>
2415 </term>
2416 <listitem>
2417 <para><emphasis>On by default.</emphasis>.
2418 Causes GHC to ignore uses of the function
2419 <literal>Exception.assert</literal> in source code (in
2420 other words, rewriting <literal>Exception.assert p
2421 e</literal> to <literal>e</literal> (see <xref
2422 linkend="assertions"/>).
2423 </para>
2424 </listitem>
2425 </varlistentry>
2426
2427 <varlistentry>
2428 <term>
2429 <option>-fignore-interface-pragmas</option>
2430 <indexterm><primary><option>-fignore-interface-pragmas</option></primary></indexterm>
2431 </term>
2432 <listitem>
2433 <para>Tells GHC to ignore all inessential information when reading interface files.
2434 That is, even if <filename>M.hi</filename> contains unfolding or strictness information
2435 for a function, GHC will ignore that information.</para>
2436 </listitem>
2437 </varlistentry>
2438
2439 <varlistentry>
2440 <term>
2441 <option>-flate-dmd-anal</option>
2442 <indexterm><primary><option>-flate-dmd-anal</option></primary></indexterm>
2443 </term>
2444 <listitem>
2445 <para>Run demand analysis
2446 again, at the end of the simplification pipeline. We found some opportunities
2447 for discovering strictness that were not visible earlier; and optimisations like
2448 <literal>-fspec-constr</literal> can create functions with unused arguments which
2449 are eliminated by late demand analysis. Improvements are modest, but so is the
2450 cost. See notes on the <ulink url="http://ghc.haskell.org/trac/ghc/wiki/LateDmd">Trac wiki page</ulink>.
2451 </para>
2452 </listitem>
2453 </varlistentry>
2454
2455 <varlistentry>
2456 <term>
2457 <option>-fliberate-case</option>
2458 <indexterm><primary><option>-fliberate-case</option></primary></indexterm>
2459 </term>
2460 <listitem>
2461 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2462 Turn on the liberate-case transformation. This unrolls recursive
2463 function once in its own RHS, to avoid repeated case analysis of
2464 free variables. It's a bit like the call-pattern specialiser
2465 (<option>-fspec-constr</option>) but for free variables rather than
2466 arguments.
2467 </para>
2468 </listitem>
2469 </varlistentry>
2470
2471 <varlistentry>
2472 <term>
2473 <option>-fliberate-case-threshold=<replaceable>n</replaceable></option>
2474 <indexterm><primary><option>-fliberate-case-threshold</option></primary></indexterm>
2475 </term>
2476 <listitem>
2477 <para>Set the size threshold for the liberate-case transformation. Default: 2000
2478 </para>
2479 </listitem>
2480 </varlistentry>
2481
2482 <varlistentry>
2483 <term>
2484 <option>-floopification</option>
2485 <indexterm><primary><option>-floopification</option></primary></indexterm>
2486 </term>
2487 <listitem>
2488 <para><emphasis>On by default.</emphasis>
2489 </para>
2490 <para>When this optimisation is enabled the code generator will turn
2491 all self-recursive saturated tail calls into local jumps rather
2492 than function calls.
2493 </para>
2494 </listitem>
2495 </varlistentry>
2496
2497 <varlistentry>
2498 <term>
2499 <option>-fmax-inline-alloc-size=<replaceable>n</replaceable></option>
2500 <indexterm><primary><option>-fmax-inline-alloc-size</option></primary></indexterm>
2501 </term>
2502 <listitem>
2503 <para>Set the maximum size of inline array allocations to n bytes
2504 (default: 128). GHC will allocate non-pinned arrays of statically
2505 known size in the current nursery block if they're no bigger
2506 than n bytes, ignoring GC overheap. This value should be quite
2507 a bit smaller than the block size (typically: 4096).
2508 </para>
2509 </listitem>
2510 </varlistentry>
2511
2512 <varlistentry>
2513 <term>
2514 <option>-fmax-inline-memcpy-insn=<replaceable>n</replaceable></option>
2515 <indexterm><primary><option>-fmax-inline-memcpy-insn</option></primary></indexterm>
2516 </term>
2517 <listitem>
2518 <para>Inline memcpy calls if they would generate no more than n pseudo instructions (default: 32).
2519 </para>
2520 </listitem>
2521 </varlistentry>
2522
2523 <varlistentry>
2524 <term>
2525 <option>-fmax-inline-memset-insns=<replaceable>n</replaceable></option>
2526 <indexterm><primary><option>-fmax-inline-memset-insns</option></primary></indexterm>
2527 </term>
2528 <listitem>
2529 <para>Inline memset calls if they would generate no more than n pseudo instructions (default: 32).
2530 </para>
2531 </listitem>
2532 </varlistentry>
2533
2534 <varlistentry>
2535 <term>
2536 <option>-fmax-relevant-binds=<replaceable>n</replaceable></option>
2537 <indexterm><primary><option>-fmax-relevant-bindings</option></primary></indexterm>
2538 </term>
2539 <listitem>
2540 <para>The type checker sometimes displays a fragment of the type environment
2541 in error messages, but only up to some maximum number, set by this flag.
2542 The default is 6. Turning it off with <option>-fno-max-relevant-bindings</option>
2543 gives an unlimited number. Syntactically top-level bindings are also
2544 usually excluded (since they may be numerous), but
2545 <option>-fno-max-relevant-bindings</option> includes them too.
2546 </para>
2547 </listitem>
2548 </varlistentry>
2549
2550 <varlistentry>
2551 <term>
2552 <option>-fmax-simplifier-iterations=<replaceable>n</replaceable></option>
2553 <indexterm><primary><option>-fmax-simplifier-iterations</option></primary></indexterm>
2554 </term>
2555 <listitem>
2556 <para>Sets the maximal number of iterations for the simplifier. Defult: 4.
2557 </para>
2558 </listitem>
2559 </varlistentry>
2560
2561 <varlistentry>
2562 <term>
2563 <option>-fmax-worker-args=<replaceable>n</replaceable></option>
2564 <indexterm><primary><option>-fmax-worker-args</option></primary></indexterm>
2565 </term>
2566 <listitem>
2567 <para>If a worker has that many arguments, none will be unpacked anymore (default: 10)
2568 </para>
2569 </listitem>
2570 </varlistentry>
2571
2572 <varlistentry>
2573 <term>
2574 <option>-fno-opt-coercion</option>
2575 <indexterm><primary><option>-fno-opt-coercion</option></primary></indexterm>
2576 </term>
2577 <listitem>
2578 <para>Turn off the coercion optimiser.
2579 </para>
2580 </listitem>
2581 </varlistentry>
2582
2583 <varlistentry>
2584 <term>
2585 <option>-fno-pre-inlining</option>
2586 <indexterm><primary><option>-fno-pre-inlining</option></primary></indexterm>
2587 </term>
2588 <listitem>
2589 <para>Turn off pre-inlining.
2590 </para>
2591 </listitem>
2592 </varlistentry>
2593
2594 <varlistentry>
2595 <term>
2596 <option>-fno-state-hack</option>
2597 <indexterm><primary><option>-fno-state-hack</option></primary></indexterm>
2598 </term>
2599 <listitem>
2600 <para>Turn off the "state hack" whereby any lambda with a
2601 <literal>State#</literal> token as argument is considered to be
2602 single-entry, hence it is considered OK to inline things inside
2603 it. This can improve performance of IO and ST monad code, but it
2604 runs the risk of reducing sharing.
2605 </para>
2606 </listitem>
2607 </varlistentry>
2608
2609 <varlistentry>
2610 <term>
2611 <option>-fomit-interface-pragmas</option>
2612 <indexterm><primary><option>-fomit-interface-pragmas</option></primary></indexterm>
2613 </term>
2614 <listitem>
2615 <para>Tells GHC to omit all inessential information from the
2616 interface file generated for the module being compiled (say M).
2617 This means that a module importing M will see only the
2618 <emphasis>types</emphasis> of the functions that M exports, but
2619 not their unfoldings, strictness info, etc. Hence, for example,
2620 no function exported by M will be inlined into an importing module.
2621 The benefit is that modules that import M will need to be
2622 recompiled less often (only when M's exports change their type, not
2623 when they change their implementation).</para>
2624 </listitem>
2625 </varlistentry>
2626
2627 <varlistentry>
2628 <term>
2629 <option>-fomit-yields</option>
2630 <indexterm><primary><option>-fomit-yields</option></primary></indexterm>
2631 </term>
2632 <listitem>
2633 <para><emphasis>On by default.</emphasis> Tells GHC to omit
2634 heap checks when no allocation is being performed. While this improves
2635 binary sizes by about 5%, it also means that threads run in
2636 tight non-allocating loops will not get preempted in a timely
2637 fashion. If it is important to always be able to interrupt such
2638 threads, you should turn this optimization off. Consider also
2639 recompiling all libraries with this optimization turned off, if you
2640 need to guarantee interruptibility.
2641 </para>
2642 </listitem>
2643 </varlistentry>
2644
2645 <varlistentry>
2646 <term>
2647 <option>-fpedantic-bottoms</option>
2648 <indexterm><primary><option>-fpedantic-bottoms</option></primary></indexterm>
2649 </term>
2650 <listitem>
2651 <para>Make GHC be more precise about its treatment of bottom (but see also
2652 <option>-fno-state-hack</option>). In particular, stop GHC
2653 eta-expanding through a case expression, which is good for
2654 performance, but bad if you are using <literal>seq</literal> on
2655 partial applications.
2656 </para>
2657 </listitem>
2658 </varlistentry>
2659
2660 <varlistentry>
2661 <term>
2662 <option>-fregs-graph</option>
2663 <indexterm><primary><option>-fregs-graph</option></primary></indexterm>
2664 </term>
2665 <listitem>
2666 <para><emphasis>Off by default due to a performance regression bug.
2667 Only applies in combination with the native code generator.</emphasis>
2668 Use the graph colouring register allocator for register allocation
2669 in the native code generator. By default, GHC uses a simpler,
2670 faster linear register allocator. The downside being that the
2671 linear register allocator usually generates worse code.
2672 </para>
2673 </listitem>
2674 </varlistentry>
2675
2676 <varlistentry>
2677 <term>
2678 <option>-fregs-iterative</option>
2679 <indexterm><primary><option>-fregs-iterative</option></primary></indexterm>
2680 </term>
2681 <listitem>
2682 <para><emphasis>Off by default, only applies in combination with
2683 the native code generator.</emphasis>
2684 Use the iterative coalescing graph colouring register allocator for
2685 register allocation in the native code generator. This is the same
2686 register allocator as the <option>-fregs-graph</option> one but also
2687 enables iterative coalescing during register allocation.
2688 </para>
2689 </listitem>
2690 </varlistentry>
2691
2692 <varlistentry>
2693 <term>
2694 <option>-fsimplifier-phases=<replaceable>n</replaceable></option>
2695 <indexterm><primary><option>-fsimplifier-phases</option></primary></indexterm>
2696 </term>
2697 <listitem>
2698 <para>Set the number of phases for the simplifier (default 2). Ignored with -O0.
2699 </para>
2700 </listitem>
2701 </varlistentry>
2702
2703 <varlistentry>
2704 <term>
2705 <option>-fsimpl-tick-factor=<replaceable>n</replaceable></option>
2706 <indexterm><primary><option>-fsimpl-tick-factor</option></primary></indexterm>
2707 </term>
2708 <listitem>
2709 <para>GHC's optimiser can diverge if you write rewrite rules (
2710 <xref linkend="rewrite-rules"/>) that don't terminate, or (less
2711 satisfactorily) if you code up recursion through data types
2712 (<xref linkend="bugs-ghc"/>). To avoid making the compiler fall
2713 into an infinite loop, the optimiser carries a "tick count" and
2714 stops inlining and applying rewrite rules when this count is
2715 exceeded. The limit is set as a multiple of the program size, so
2716 bigger programs get more ticks. The
2717 <option>-fsimpl-tick-factor</option> flag lets you change the
2718 multiplier. The default is 100; numbers larger than 100 give more
2719 ticks, and numbers smaller than 100 give fewer.
2720 </para>
2721
2722 <para>If the tick-count expires, GHC summarises what simplifier
2723 steps it has done; you can use
2724 <option>-fddump-simpl-stats</option> to generate a much more
2725 detailed list. Usually that identifies the loop quite
2726 accurately, because some numbers are very large.
2727 </para>
2728 </listitem>
2729 </varlistentry>
2730
2731 <varlistentry>
2732 <term>
2733 <option>-fspec-constr</option>
2734 <indexterm><primary><option>-fspec-constr</option></primary></indexterm>
2735 </term>
2736 <listitem>
2737 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2738 Turn on call-pattern specialisation; see
2739 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/spec-constr/index.htm">
2740 Call-pattern specialisation for Haskell programs</ulink>.
2741 </para>
2742
2743 <para>This optimisation specializes recursive functions according to
2744 their argument "shapes". This is best explained by example so
2745 consider:
2746 <programlisting>
2747 last :: [a] -> a
2748 last [] = error "last"
2749 last (x : []) = x
2750 last (x : xs) = last xs
2751 </programlisting>
2752 In this code, once we pass the initial check for an empty list we
2753 know that in the recursive case this pattern match is redundant. As
2754 such <option>-fspec-constr</option> will transform the above code
2755 to:
2756 <programlisting>
2757 last :: [a] -> a
2758 last [] = error "last"
2759 last (x : xs) = last' x xs
2760 where
2761 last' x [] = x
2762 last' x (y : ys) = last' y ys
2763 </programlisting>
2764 </para>
2765
2766 <para>As well avoid unnecessary pattern matching it also helps avoid
2767 unnecessary allocation. This applies when a argument is strict in
2768 the recursive call to itself but not on the initial entry. As
2769 strict recursive branch of the function is created similar to the
2770 above example.
2771 </para>
2772
2773 <para>It is also possible for library writers to instruct
2774 GHC to perform call-pattern specialisation extremely
2775 aggressively. This is necessary for some highly optimized
2776 libraries, where we may want to specialize regardless of
2777 the number of specialisations, or the size of the code. As
2778 an example, consider a simplified use-case from the
2779 <literal>vector</literal> library:</para>
2780 <programlisting>
2781 import GHC.Types (SPEC(..))
2782
2783 foldl :: (a -> b -> a) -> a -> Stream b -> a
2784 {-# INLINE foldl #-}
2785 foldl f z (Stream step s _) = foldl_loop SPEC z s
2786 where
2787 foldl_loop !sPEC z s = case step s of
2788 Yield x s' -> foldl_loop sPEC (f z x) s'
2789 Skip -> foldl_loop sPEC z s'
2790 Done -> z
2791 </programlisting>
2792
2793 <para>Here, after GHC inlines the body of
2794 <literal>foldl</literal> to a call site, it will perform
2795 call-pattern specialization very aggressively on
2796 <literal>foldl_loop</literal> due to the use of
2797 <literal>SPEC</literal> in the argument of the loop
2798 body. <literal>SPEC</literal> from
2799 <literal>GHC.Types</literal> is specifically recognized by
2800 the compiler.</para>
2801
2802 <para>(NB: it is extremely important you use
2803 <literal>seq</literal> or a bang pattern on the
2804 <literal>SPEC</literal> argument!)</para>
2805
2806 <para>In particular, after inlining this will
2807 expose <literal>f</literal> to the loop body directly,
2808 allowing heavy specialisation over the recursive
2809 cases.</para>
2810 </listitem>
2811 </varlistentry>
2812
2813 <varlistentry>
2814 <term>
2815 <option>-fspec-constr-count=<replaceable>n</replaceable></option>
2816 <indexterm><primary><option>-fspec-constr-count</option></primary></indexterm>
2817 </term>
2818 <listitem>
2819 <para>Set the maximum number of specialisations
2820 that will be created for any one function by the SpecConstr
2821 transformation (default: 3).
2822 </para>
2823 </listitem>
2824 </varlistentry>
2825
2826 <varlistentry>
2827 <term>
2828 <option>-fspec-constr-threshold=<replaceable>n</replaceable></option>
2829 <indexterm><primary><option>-fspec-constr-threshold</option></primary></indexterm>
2830 </term>
2831 <listitem>
2832 <para>Set the size threshold for the SpecConstr transformation (default: 2000).
2833 </para>
2834 </listitem>
2835 </varlistentry>
2836
2837 <varlistentry>
2838 <term>
2839 <option>-fspecialise</option>
2840 <indexterm><primary><option>-fspecialise</option></primary></indexterm>
2841 </term>
2842 <listitem>
2843 <para><emphasis>On by default.</emphasis>
2844 Specialise each type-class-overloaded function defined in this
2845 module for the types at which it is called in this module. Also
2846 specialise imported functions that have an INLINABLE pragma
2847 (<xref linkend="inlinable-pragma"/>) for the types at which they
2848 are called in this module.
2849 </para>
2850 </listitem>
2851 </varlistentry>
2852
2853 <varlistentry>
2854 <term>
2855 <option>-fstatic-argument-transformation</option>
2856 <indexterm><primary><option>-fstatic-argument-transformation</option></primary></indexterm>
2857 </term>
2858 <listitem>
2859 <para>Turn on the static argument transformation, which turns a
2860 recursive function into a non-recursive one with a local
2861 recursive loop. See Chapter 7 of
2862 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/santos-thesis.ps.gz">
2863 Andre Santos's PhD thesis</ulink>
2864 </para>
2865 </listitem>
2866 </varlistentry>
2867
2868 <varlistentry>
2869 <term>
2870 <option>-fstrictness</option>
2871 <indexterm><primary><option></option></primary></indexterm>
2872 </term>
2873 <listitem>
2874 <para> <emphasis>On by default.</emphasis>.
2875 Switch on the strictness analyser. There is a very old paper about GHC's
2876 strictness analyser, <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/simple-strictnes-analyser.ps.gz">
2877 Measuring the effectiveness of a simple strictness analyser</ulink>,
2878 but the current one is quite a bit different.
2879 </para>
2880
2881 <para>The strictness analyser figures out when arguments and
2882 variables in a function can be treated 'strictly' (that is they
2883 are always evaluated in the function at some point). This allow
2884 GHC to apply certain optimisations such as unboxing that
2885 otherwise don't apply as they change the semantics of the program
2886 when applied to lazy arguments.
2887 </para>
2888 </listitem>
2889 </varlistentry>
2890
2891 <varlistentry>
2892 <term>
2893 <option>-fstrictness-before=<replaceable>n</replaceable></option>
2894 <indexterm><primary><option>-fstrictness-before</option></primary></indexterm>
2895 </term>
2896 <listitem>
2897 <para>Run an additional strictness analysis before simplifier phase n.
2898 </para>
2899 </listitem>
2900 </varlistentry>
2901
2902 <varlistentry>
2903 <term>
2904 <option>-funbox-small-strict-fields</option>:
2905 <indexterm><primary><option>-funbox-small-strict-fields</option></primary></indexterm>
2906 <indexterm><primary>strict constructor fields</primary></indexterm>
2907 <indexterm><primary>constructor fields, strict</primary></indexterm>
2908 </term>
2909 <listitem>
2910 <para><emphasis>On by default.</emphasis>. This option
2911 causes all constructor fields which are marked strict
2912 (i.e. &ldquo;!&rdquo;) and which representation is smaller
2913 or equal to the size of a pointer to be unpacked, if
2914 possible. It is equivalent to adding an
2915 <literal>UNPACK</literal> pragma (see <xref
2916 linkend="unpack-pragma"/>) to every strict constructor
2917 field that fulfils the size restriction.
2918 </para>
2919
2920 <para>For example, the constructor fields in the following
2921 data types
2922 <programlisting>
2923 data A = A !Int
2924 data B = B !A
2925 newtype C = C B
2926 data D = D !C
2927 </programlisting>
2928 would all be represented by a single
2929 <literal>Int#</literal> (see <xref linkend="primitives"/>)
2930 value with
2931 <option>-funbox-small-strict-fields</option> enabled.
2932 </para>
2933
2934 <para>This option is less of a sledgehammer than
2935 <option>-funbox-strict-fields</option>: it should rarely make things
2936 worse. If you use <option>-funbox-small-strict-fields</option>
2937 to turn on unboxing by default you can disable it for certain
2938 constructor fields using the <literal>NOUNPACK</literal> pragma (see
2939 <xref linkend="nounpack-pragma"/>).</para>
2940
2941 <para>
2942 Note that for consistency <literal>Double</literal>,
2943 <literal>Word64</literal>, and <literal>Int64</literal> constructor
2944 fields are unpacked on 32-bit platforms, even though they are
2945 technically larger than a pointer on those platforms.
2946 </para>
2947 </listitem>
2948 </varlistentry>
2949
2950 <varlistentry>
2951 <term>
2952 <option>-funbox-strict-fields</option>:
2953 <indexterm><primary><option>-funbox-strict-fields</option></primary></indexterm>
2954 <indexterm><primary>strict constructor fields</primary></indexterm>
2955 <indexterm><primary>constructor fields, strict</primary></indexterm>
2956 </term>
2957 <listitem>
2958 <para>This option causes all constructor fields which are marked
2959 strict (i.e. &ldquo;!&rdquo;) to be unpacked if possible. It is
2960 equivalent to adding an <literal>UNPACK</literal> pragma to every
2961 strict constructor field (see <xref linkend="unpack-pragma"/>).
2962 </para>
2963
2964 <para>This option is a bit of a sledgehammer: it might sometimes
2965 make things worse. Selectively unboxing fields by using
2966 <literal>UNPACK</literal> pragmas might be better. An alternative
2967 is to use <option>-funbox-strict-fields</option> to turn on
2968 unboxing by default but disable it for certain constructor
2969 fields using the <literal>NOUNPACK</literal> pragma (see
2970 <xref linkend="nounpack-pragma"/>).</para>
2971 </listitem>
2972 </varlistentry>
2973
2974 <varlistentry>
2975 <term>
2976 <option>-funfolding-creation-threshold=<replaceable>n</replaceable></option>:
2977 <indexterm><primary><option>-funfolding-creation-threshold</option></primary></indexterm>
2978 <indexterm><primary>inlining, controlling</primary></indexterm>
2979 <indexterm><primary>unfolding, controlling</primary></indexterm>
2980 </term>
2981 <listitem>
2982 <para>(Default: 750) Governs the maximum size that GHC will allow a
2983 function unfolding to be. (An unfolding has a &ldquo;size&rdquo;
2984 that reflects the cost in terms of &ldquo;code bloat&rdquo; of
2985 expanding (aka inlining) that unfolding at a call site. A bigger
2986 function would be assigned a bigger cost.)
2987 </para>
2988
2989 <para>Consequences: (a) nothing larger than this will be inlined
2990 (unless it has an INLINE pragma); (b) nothing larger than this
2991 will be spewed into an interface file.
2992 </para>
2993
2994 <para>Increasing this figure is more likely to result in longer
2995 compile times than faster code. The
2996 <option>-funfolding-use-threshold</option> is more useful.
2997 </para>
2998 </listitem>
2999 </varlistentry>
3000
3001 <varlistentry>
3002 <term>
3003 <option>-funfolding-dict-discount=<replaceable>n</replaceable></option>:
3004 <indexterm><primary><option>-funfolding-dict-discount</option></primary></indexterm>
3005 <indexterm><primary>inlining, controlling</primary></indexterm>
3006 <indexterm><primary>unfolding, controlling</primary></indexterm>
3007 </term>
3008 <listitem>
3009 <para>Default: 30
3010 </para>
3011 </listitem>
3012 </varlistentry>
3013
3014 <varlistentry>
3015 <term>
3016 <option>-funfolding-fun-discount=<replaceable>n</replaceable></option>:
3017 <indexterm><primary><option>-funfolding-fun-discount</option></primary></indexterm>
3018 <indexterm><primary>inlining, controlling</primary></indexterm>
3019 <indexterm><primary>unfolding, controlling</primary></indexterm>
3020 </term>
3021 <listitem>
3022 <para>Default: 60
3023 </para>
3024 </listitem>
3025 </varlistentry>
3026
3027 <varlistentry>
3028 <term>
3029 <option>-funfolding-keeness-factor=<replaceable>n</replaceable></option>:
3030 <indexterm><primary><option>-funfolding-keeness-factor</option></primary></indexterm>
3031 <indexterm><primary>inlining, controlling</primary></indexterm>
3032 <indexterm><primary>unfolding, controlling</primary></indexterm>
3033 </term>
3034 <listitem>
3035 <para>Default: 1.5
3036 </para>
3037 </listitem>
3038 </varlistentry>
3039
3040 <varlistentry>
3041 <term>
3042 <option>-funfolding-use-threshold=<replaceable>n</replaceable></option>
3043 <indexterm><primary><option>-funfolding-use-threshold</option></primary></indexterm>
3044 <indexterm><primary>inlining, controlling</primary></indexterm>
3045 <indexterm><primary>unfolding, controlling</primary></indexterm>
3046 </term>
3047 <listitem>
3048 <para>(Default: 60) This is the magic cut-off figure for unfolding
3049 (aka inlining): below this size, a function definition will be
3050 unfolded at the call-site, any bigger and it won't. The size
3051 computed for a function depends on two things: the actual size of
3052 the expression minus any discounts that apply depending on the
3053 context into which the expression is to be inlined.
3054 </para>
3055
3056 <para>The difference between this and
3057 <option>-funfolding-creation-threshold</option> is that this one
3058 determines if a function definition will be inlined <emphasis>at
3059 a call site</emphasis>. The other option determines if a
3060 function definition will be kept around at all for potential
3061 inlining.
3062 </para>
3063 </listitem>
3064 </varlistentry>
3065
3066 <varlistentry>
3067 <term>
3068 <option>-fvectorisation-avoidance</option>
3069 <indexterm><primary><option></option></primary></indexterm>
3070 </term>
3071 <listitem>
3072 <para>Part of <link linkend="dph">Data Parallel Haskell
3073 (DPH)</link>.</para>
3074
3075 <para><emphasis>On by default.</emphasis> Enable the
3076 <emphasis>vectorisation</emphasis> avoidance optimisation. This
3077 optimisation only works when used in combination with the
3078 <option>-fvectorise</option> transformation.</para>
3079
3080 <para>While vectorisation of code using DPH is often a big win, it
3081 can also produce worse results for some kinds of code. This
3082 optimisation modifies the vectorisation transformation to try to
3083 determine if a function would be better of unvectorised and if
3084 so, do just that.</para>
3085 </listitem>
3086 </varlistentry>
3087
3088 <varlistentry>
3089 <term>
3090 <option>-fvectorise</option>
3091 <indexterm><primary><option></option></primary></indexterm>
3092 </term>
3093 <listitem>
3094 <para>Part of <link linkend="dph">Data Parallel Haskell
3095 (DPH)</link>.</para>
3096
3097 <para><emphasis>Off by default.</emphasis> Enable the
3098 <emphasis>vectorisation</emphasis> optimisation transformation. This
3099 optimisation transforms the nested data parallelism code of programs
3100 using DPH into flat data parallelism. Flat data parallel programs
3101 should have better load balancing, enable SIMD parallelism and
3102 friendlier cache behaviour.</para>
3103 </listitem>
3104 </varlistentry>
3105
3106 </variablelist>
3107
3108 </sect2>
3109
3110 </sect1>
3111
3112 &code-gens;
3113
3114 &phases;
3115
3116 &shared_libs;
3117
3118 <sect1 id="using-concurrent">
3119 <title>Using Concurrent Haskell</title>
3120 <indexterm><primary>Concurrent Haskell</primary><secondary>using</secondary></indexterm>
3121
3122 <para>GHC supports Concurrent Haskell by default, without requiring a
3123 special option or libraries compiled in a certain way. To get access to
3124 the support libraries for Concurrent Haskell, just import
3125 <ulink
3126 url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>. More information on Concurrent Haskell is provided in the documentation for that module.</para>
3127
3128 <para>
3129 Optionally, the program may be linked with
3130 the <option>-threaded</option> option (see
3131 <xref linkend="options-linker" />. This provides two benefits:
3132
3133 <itemizedlist>
3134 <listitem>
3135 <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
3136 used, which allows threads to run in
3137 parallel<indexterm><primary>parallelism</primary></indexterm>
3138 on a
3139 multiprocessor<indexterm><primary>multiprocessor</primary></indexterm><indexterm><primary>SMP</primary></indexterm>
3140 or
3141 multicore<indexterm><primary>multicore</primary></indexterm>
3142 machine. See <xref linkend="using-smp" />.</para>
3143 </listitem>
3144 <listitem>
3145 <para>If a thread makes a foreign call (and the call is
3146 not marked <literal>unsafe</literal>), then other
3147 Haskell threads in the program will continue to run
3148 while the foreign call is in progress.
3149 Additionally, <literal>foreign export</literal>ed
3150 Haskell functions may be called from multiple OS
3151 threads simultaneously. See
3152 <xref linkend="ffi-threads" />.</para>
3153 </listitem>
3154 </itemizedlist>
3155 </para>
3156
3157 <para>The following RTS option(s) affect the behaviour of Concurrent
3158 Haskell programs:<indexterm><primary>RTS options, concurrent</primary></indexterm></para>
3159
3160 <variablelist>
3161 <varlistentry>
3162 <term><option>-C<replaceable>s</replaceable></option></term>
3163 <listitem>
3164 <para><indexterm><primary><option>-C<replaceable>s</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
3165 Sets the context switch interval to <replaceable>s</replaceable>
3166 seconds. A context switch will occur at the next heap block
3167 allocation after the timer expires (a heap block allocation occurs
3168 every 4k of allocation). With <option>-C0</option> or
3169 <option>-C</option>, context switches will occur as often as
3170 possible (at every heap block allocation). By default, context
3171 switches occur every 20ms.</para>
3172 </listitem>
3173 </varlistentry>
3174 </variablelist>
3175 </sect1>
3176
3177 <sect1 id="using-smp">
3178 <title>Using SMP parallelism</title>
3179 <indexterm><primary>parallelism</primary>
3180 </indexterm>
3181 <indexterm><primary>SMP</primary>
3182 </indexterm>
3183
3184 <para>GHC supports running Haskell programs in parallel on an SMP
3185 (symmetric multiprocessor).</para>
3186
3187 <para>There's a fine distinction between
3188 <emphasis>concurrency</emphasis> and <emphasis>parallelism</emphasis>:
3189 parallelism is all about making your program run
3190 <emphasis>faster</emphasis> by making use of multiple processors
3191 simultaneously. Concurrency, on the other hand, is a means of
3192 abstraction: it is a convenient way to structure a program that must
3193 respond to multiple asynchronous events.</para>
3194
3195 <para>However, the two terms are certainly related. By making use of
3196 multiple CPUs it is possible to run concurrent threads in parallel,
3197 and this is exactly what GHC's SMP parallelism support does. But it
3198 is also possible to obtain performance improvements with parallelism
3199 on programs that do not use concurrency. This section describes how to
3200 use GHC to compile and run parallel programs, in <xref
3201 linkend="lang-parallel" /> we describe the language features that affect
3202 parallelism.</para>
3203
3204 <sect2 id="parallel-compile-options">
3205 <title>Compile-time options for SMP parallelism</title>
3206
3207 <para>In order to make use of multiple CPUs, your program must be
3208 linked with the <option>-threaded</option> option (see <xref
3209 linkend="options-linker" />). Additionally, the following
3210 compiler options affect parallelism:</para>
3211
3212 <variablelist>
3213 <varlistentry>
3214 <term><option>-feager-blackholing</option></term>
3215 <indexterm><primary><option>-feager-blackholing</option></primary></indexterm>
3216 <listitem>
3217 <para>
3218 Blackholing is the act of marking a thunk (lazy
3219 computuation) as being under evaluation. It is useful for
3220 three reasons: firstly it lets us detect certain kinds of
3221 infinite loop (the <literal>NonTermination</literal>
3222 exception), secondly it avoids certain kinds of space
3223 leak, and thirdly it avoids repeating a computation in a
3224 parallel program, because we can tell when a computation
3225 is already in progress.</para>
3226
3227 <para>
3228 The option <option>-feager-blackholing</option> causes
3229 each thunk to be blackholed as soon as evaluation begins.
3230 The default is "lazy blackholing", whereby thunks are only
3231 marked as being under evaluation when a thread is paused
3232 for some reason. Lazy blackholing is typically more
3233 efficient (by 1-2&percnt; or so), because most thunks don't
3234 need to be blackholed. However, eager blackholing can
3235 avoid more repeated computation in a parallel program, and
3236 this often turns out to be important for parallelism.
3237 </para>
3238
3239 <para>
3240 We recommend compiling any code that is intended to be run
3241 in parallel with the <option>-feager-blackholing</option>
3242 flag.
3243 </para>
3244 </listitem>
3245 </varlistentry>
3246 </variablelist>
3247 </sect2>
3248
3249 <sect2 id="parallel-options">
3250 <title>RTS options for SMP parallelism</title>
3251
3252 <para>There are two ways to run a program on multiple
3253 processors:
3254 call <literal>Control.Concurrent.setNumCapabilities</literal> from your
3255 program, or use the RTS <option>-N</option> option.</para>
3256
3257 <variablelist>
3258 <varlistentry>
3259 <term><option>-N<optional><replaceable>x</replaceable></optional></option></term>
3260 <listitem>
3261 <para><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
3262 Use <replaceable>x</replaceable> simultaneous threads when
3263 running the program. Normally <replaceable>x</replaceable>
3264 should be chosen to match the number of CPU cores on the
3265 machine<footnote><para>Whether hyperthreading cores should be counted or not is an
3266 open question; please feel free to experiment and let us know what
3267 results you find.</para></footnote>. For example,
3268 on a dual-core machine we would probably use
3269 <literal>+RTS -N2 -RTS</literal>.</para>
3270
3271 <para>Omitting <replaceable>x</replaceable>,
3272 i.e. <literal>+RTS -N -RTS</literal>, lets the runtime
3273 choose the value of <replaceable>x</replaceable> itself
3274 based on how many processors are in your machine.</para>
3275
3276 <para>Be careful when using all the processors in your
3277 machine: if some of your processors are in use by other
3278 programs, this can actually harm performance rather than
3279 improve it.</para>
3280
3281 <para>Setting <option>-N</option> also has the effect of
3282 enabling the parallel garbage collector (see
3283 <xref linkend="rts-options-gc" />).</para>
3284
3285 <para>The current value of the <option>-N</option> option
3286 is available to the Haskell program
3287 via <literal>Control.Concurrent.getNumCapabilities</literal>, and
3288 it may be changed while the program is running by
3289 calling <literal>Control.Concurrent.setNumCapabilities</literal>.</para>
3290 </listitem>
3291 </varlistentry>
3292 </variablelist>
3293
3294 <para>The following options affect the way the runtime schedules
3295 threads on CPUs:</para>
3296
3297 <variablelist>
3298 <varlistentry>
3299 <term><option>-qa</option></term>
3300 <indexterm><primary><option>-qa</option></primary><secondary>RTS
3301 option</secondary></indexterm>
3302 <listitem>
3303 <para>Use the OS's affinity facilities to try to pin OS
3304 threads to CPU cores. This is an experimental feature,
3305 and may or may not be useful. Please let us know
3306 whether it helps for you!</para>
3307 </listitem>
3308 </varlistentry>
3309 <varlistentry>
3310 <term><option>-qm</option></term>
3311 <indexterm><primary><option>-qm</option></primary><secondary>RTS
3312 option</secondary></indexterm>
3313 <listitem>
3314 <para>Disable automatic migration for load balancing.
3315 Normally the runtime will automatically try to schedule
3316 threads across the available CPUs to make use of idle
3317 CPUs; this option disables that behaviour. Note that
3318 migration only applies to threads; sparks created
3319 by <literal>par</literal> are load-balanced separately
3320 by work-stealing.</para>
3321
3322 <para>
3323 This option is probably only of use for concurrent
3324 programs that explicitly schedule threads onto CPUs
3325 with <literal>Control.Concurrent.forkOn</literal>.
3326 </para>
3327 </listitem>
3328 </varlistentry>
3329 </variablelist>
3330 </sect2>
3331
3332 <sect2>
3333 <title>Hints for using SMP parallelism</title>
3334
3335 <para>Add the <literal>-s</literal> RTS option when
3336 running the program to see timing stats, which will help to tell you
3337 whether your program got faster by using more CPUs or not. If the user
3338 time is greater than
3339 the elapsed time, then the program used more than one CPU. You should
3340 also run the program without <literal>-N</literal> for
3341 comparison.</para>
3342
3343 <para>The output of <literal>+RTS -s</literal> tells you how
3344 many &ldquo;sparks&rdquo; were created and executed during the
3345 run of the program (see <xref linkend="rts-options-gc" />), which
3346 will give you an idea how well your <literal>par</literal>
3347 annotations are working.</para>
3348
3349 <para>GHC's parallelism support has improved in 6.12.1 as a
3350 result of much experimentation and tuning in the runtime
3351 system. We'd still be interested to hear how well it works
3352 for you, and we're also interested in collecting parallel
3353 programs to add to our benchmarking suite.</para>
3354 </sect2>
3355 </sect1>
3356
3357 <sect1 id="options-platform">
3358 <title>Platform-specific Flags</title>
3359
3360 <indexterm><primary>-m* options</primary></indexterm>
3361 <indexterm><primary>platform-specific options</primary></indexterm>
3362 <indexterm><primary>machine-specific options</primary></indexterm>
3363
3364 <para>Some flags only make sense for particular target
3365 platforms.</para>
3366
3367 <variablelist>
3368
3369 <varlistentry>
3370 <term><option>-msse2</option>:</term>
3371 <listitem>
3372 <para>
3373 (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
3374 instruction set to implement floating point operations when using
3375 the <link linkend="native-code-gen">native code generator</link>.
3376 This gives a substantial performance improvement for floating
3377 point, but the resulting compiled code
3378 will only run on processors that support SSE2 (Intel Pentium 4 and
3379 later, or AMD Athlon 64 and later). The
3380 <link linkend="llvm-code-gen">LLVM backend</link> will also use SSE2
3381 if your processor supports it but detects this automatically so no
3382 flag is required.
3383 </para>
3384 <para>
3385 SSE2 is unconditionally used on x86-64 platforms.
3386 </para>
3387 </listitem>
3388 </varlistentry>
3389
3390 <varlistentry>
3391 <term><option>-msse4.2</option>:</term>
3392 <listitem>
3393 <para>
3394 (x86 only, added in GHC 7.4.1) Use the SSE4.2 instruction set to
3395 implement some floating point and bit operations when using the
3396 <link linkend="native-code-gen">native code generator</link>. The
3397 resulting compiled code will only run on processors that
3398 support SSE4.2 (Intel Core i7 and later). The
3399 <link linkend="llvm-code-gen">LLVM backend</link> will also use
3400 SSE4.2 if your processor supports it but detects this automatically
3401 so no flag is required.
3402 </para>
3403 </listitem>
3404 </varlistentry>
3405
3406 </variablelist>
3407
3408 </sect1>
3409
3410 &runtime;
3411 &debug;
3412 &flags;
3413
3414 </chapter>
3415
3416 <!-- Emacs stuff:
3417 ;;; Local Variables: ***
3418 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
3419 ;;; End: ***
3420 -->