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