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