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