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