Improve documentation for -fwarn-unused-binds
[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. More precisely:
1740
1741 <itemizedlist>
1742 <listitem><para>Warn if a binding brings into scope a variable that is not used,
1743 except if the variable's name starts with an underscore. The "starts-with-underscore"
1744 condition provides a way to selectively disable the warning.
1745 </para>
1746 <para>
1747 A variable is regarded as "used" if
1748 <itemizedlist>
1749 <listitem><para>It is exported, or</para></listitem>
1750 <listitem><para>It appears in the right hand side of a binding that binds at
1751 least one used variable that is used</para></listitem>
1752 </itemizedlist>
1753 For example
1754 <programlisting>
1755 module A (f) where
1756 f = let (p,q) = rhs1 in t p -- Warning about unused q
1757 t = rhs3 -- No warning: f is used, and hence so is t
1758 g = h x -- Warning: g unused
1759 h = rhs2 -- Warning: h is only used in the right-hand side of another unused binding
1760 _w = True -- No warning: _w starts with an underscore
1761 </programlisting>
1762 </para></listitem>
1763
1764 <listitem><para>
1765 Warn if a pattern binding binds no variables at all, unless it is a lone, possibly-banged, wild-card pattern.
1766 For example:
1767 <programlisting>
1768 Just _ = rhs3 -- Warning: unused pattern binding
1769 (_, _) = rhs4 -- Warning: unused pattern binding
1770 _ = rhs3 -- No warning: lone wild-card pattern
1771 !_ = rhs4 -- No warning: banged wild-card pattern; behaves like seq
1772 </programlisting>
1773 The motivation for allowing lone wild-card patterns is they
1774 are not very different from <literal>_v = rhs3</literal>,
1775 which elicits no warning; and they can be useful to add a type
1776 constraint, e.g. <literal>_ = x::Int</literal>. A lone
1777 banged wild-card pattern is is useful as an alternative
1778 (to <literal>seq</literal>) way to force evaluation.
1779 </para>
1780 </listitem>
1781 </itemizedlist>
1782 </para>
1783 </listitem>
1784 </varlistentry>
1785
1786 <varlistentry>
1787 <term><option>-fwarn-unused-imports</option>:</term>
1788 <listitem>
1789 <indexterm><primary><option>-fwarn-unused-imports</option></primary></indexterm>
1790 <indexterm><primary>unused imports, warning</primary></indexterm>
1791 <indexterm><primary>imports, unused</primary></indexterm>
1792
1793 <para>Report any modules that are explicitly imported but
1794 never used. However, the form <literal>import M()</literal> is
1795 never reported as an unused import, because it is a useful idiom
1796 for importing instance declarations, which are anonymous in Haskell.</para>
1797 </listitem>
1798 </varlistentry>
1799
1800 <varlistentry>
1801 <term><option>-fwarn-unused-matches</option>:</term>
1802 <listitem>
1803 <indexterm><primary><option>-fwarn-unused-matches</option></primary></indexterm>
1804 <indexterm><primary>unused matches, warning</primary></indexterm>
1805 <indexterm><primary>matches, unused</primary></indexterm>
1806
1807 <para>Report all unused variables which arise from pattern
1808 matches, including patterns consisting of a single variable.
1809 For instance <literal>f x y = []</literal> would report
1810 <varname>x</varname> and <varname>y</varname> as unused. The
1811 warning is suppressed if the variable name begins with an underscore, thus:
1812 <programlisting>
1813 f _x = True
1814 </programlisting>
1815 </para>
1816 </listitem>
1817 </varlistentry>
1818
1819 <varlistentry>
1820 <term><option>-fwarn-unused-do-bind</option>:</term>
1821 <listitem>
1822 <indexterm><primary><option>-fwarn-unused-do-bind</option></primary></indexterm>
1823 <indexterm><primary>unused do binding, warning</primary></indexterm>
1824 <indexterm><primary>do binding, unused</primary></indexterm>
1825
1826 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1827 that appear to silently throw information away.
1828 For instance <literal>do { mapM popInt xs ; return 10 }</literal> would report
1829 the first statement in the <literal>do</literal> block as suspicious,
1830 as it has the type <literal>StackM [Int]</literal> and not <literal>StackM ()</literal>, but that
1831 <literal>[Int]</literal> value is not bound to anything. The warning is suppressed by
1832 explicitly mentioning in the source code that your program is throwing something away:
1833 <programlisting>
1834 do { _ &lt;- mapM popInt xs ; return 10 }
1835 </programlisting>
1836 Of course, in this particular situation you can do even better:
1837 <programlisting>
1838 do { mapM_ popInt xs ; return 10 }
1839 </programlisting>
1840 </para>
1841 </listitem>
1842 </varlistentry>
1843
1844 <varlistentry>
1845 <term><option>-fwarn-wrong-do-bind</option>:</term>
1846 <listitem>
1847 <indexterm><primary><option>-fwarn-wrong-do-bind</option></primary></indexterm>
1848 <indexterm><primary>apparently erroneous do binding, warning</primary></indexterm>
1849 <indexterm><primary>do binding, apparently erroneous</primary></indexterm>
1850
1851 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1852 that appear to lack a binding.
1853 For instance <literal>do { return (popInt 10) ; return 10 }</literal> would report
1854 the first statement in the <literal>do</literal> block as suspicious,
1855 as it has the type <literal>StackM (StackM Int)</literal> (which consists of two nested applications
1856 of the same monad constructor), but which is not then &quot;unpacked&quot; by binding the result.
1857 The warning is suppressed by explicitly mentioning in the source code that your program is throwing something away:
1858 <programlisting>
1859 do { _ &lt;- return (popInt 10) ; return 10 }
1860 </programlisting>
1861 For almost all sensible programs this will indicate a bug, and you probably intended to write:
1862 <programlisting>
1863 do { popInt 10 ; return 10 }
1864 </programlisting>
1865 </para>
1866 </listitem>
1867 </varlistentry>
1868
1869 </variablelist>
1870
1871 <para>If you're feeling really paranoid, the
1872 <option>-dcore-lint</option>
1873 option<indexterm><primary><option>-dcore-lint</option></primary></indexterm>
1874 is a good choice. It turns on heavyweight intra-pass
1875 sanity-checking within GHC. (It checks GHC's sanity, not
1876 yours.)</para>
1877
1878 </sect1>
1879
1880 &packages;
1881
1882 <sect1 id="options-optimise">
1883 <title>Optimisation (code improvement)</title>
1884
1885 <indexterm><primary>optimisation</primary></indexterm>
1886 <indexterm><primary>improvement, code</primary></indexterm>
1887
1888 <para>The <option>-O*</option> options specify convenient
1889 &ldquo;packages&rdquo; of optimisation flags; the
1890 <option>-f*</option> options described later on specify
1891 <emphasis>individual</emphasis> optimisations to be turned on/off;
1892 the <option>-m*</option> options specify
1893 <emphasis>machine-specific</emphasis> optimisations to be turned
1894 on/off.</para>
1895
1896 <sect2 id="optimise-pkgs">
1897 <title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.</title>
1898
1899 <para>There are <emphasis>many</emphasis> options that affect
1900 the quality of code produced by GHC. Most people only have a
1901 general goal, something like &ldquo;Compile quickly&rdquo; or
1902 &ldquo;Make my program run like greased lightning.&rdquo; The
1903 following &ldquo;packages&rdquo; of optimisations (or lack
1904 thereof) should suffice.</para>
1905
1906 <para>Note that higher optimisation levels cause more
1907 cross-module optimisation to be performed, which can have an
1908 impact on how much of your program needs to be recompiled when
1909 you change something. This is one reason to stick to
1910 no-optimisation when developing code.</para>
1911
1912 <variablelist>
1913
1914 <varlistentry>
1915 <term>
1916 No <option>-O*</option>-type option specified:
1917 <indexterm><primary>-O* not specified</primary></indexterm>
1918 </term>
1919 <listitem>
1920 <para>This is taken to mean: &ldquo;Please compile
1921 quickly; I'm not over-bothered about compiled-code
1922 quality.&rdquo; So, for example: <command>ghc -c
1923 Foo.hs</command></para>
1924 </listitem>
1925 </varlistentry>
1926
1927 <varlistentry>
1928 <term>
1929 <option>-O0</option>:
1930 <indexterm><primary><option>-O0</option></primary></indexterm>
1931 </term>
1932 <listitem>
1933 <para>Means &ldquo;turn off all optimisation&rdquo;,
1934 reverting to the same settings as if no
1935 <option>-O</option> options had been specified. Saying
1936 <option>-O0</option> can be useful if
1937 eg. <command>make</command> has inserted a
1938 <option>-O</option> on the command line already.</para>
1939 </listitem>
1940 </varlistentry>
1941
1942 <varlistentry>
1943 <term>
1944 <option>-O</option> or <option>-O1</option>:
1945 <indexterm><primary>-O option</primary></indexterm>
1946 <indexterm><primary>-O1 option</primary></indexterm>
1947 <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
1948 </term>
1949 <listitem>
1950 <para>Means: &ldquo;Generate good-quality code without
1951 taking too long about it.&rdquo; Thus, for example:
1952 <command>ghc -c -O Main.lhs</command></para>
1953 </listitem>
1954 </varlistentry>
1955
1956 <varlistentry>
1957 <term>
1958 <option>-O2</option>:
1959 <indexterm><primary>-O2 option</primary></indexterm>
1960 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
1961 </term>
1962 <listitem>
1963 <para>Means: &ldquo;Apply every non-dangerous
1964 optimisation, even if it means significantly longer
1965 compile times.&rdquo;</para>
1966
1967 <para>The avoided &ldquo;dangerous&rdquo; optimisations
1968 are those that can make runtime or space
1969 <emphasis>worse</emphasis> if you're unlucky. They are
1970 normally turned on or off individually.</para>
1971
1972 <para>At the moment, <option>-O2</option> is
1973 <emphasis>unlikely</emphasis> to produce better code than
1974 <option>-O</option>.</para>
1975 </listitem>
1976 </varlistentry>
1977 </variablelist>
1978
1979 <para>We don't use a <option>-O*</option> flag for day-to-day
1980 work. We use <option>-O</option> to get respectable speed;
1981 e.g., when we want to measure something. When we want to go for
1982 broke, we tend to use <option>-O2</option> (and we go for
1983 lots of coffee breaks).</para>
1984
1985 <para>The easiest way to see what <option>-O</option> (etc.)
1986 &ldquo;really mean&rdquo; is to run with <option>-v</option>,
1987 then stand back in amazement.</para>
1988 </sect2>
1989
1990 <sect2 id="options-f">
1991 <title><option>-f*</option>: platform-independent flags</title>
1992
1993 <indexterm><primary>-f* options (GHC)</primary></indexterm>
1994 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
1995
1996 <para>These flags turn on and off individual optimisations.
1997 They are normally set via the <option>-O</option> options
1998 described above, and as such, you shouldn't need to set any of
1999 them explicitly (indeed, doing so could lead to unexpected
2000 results). A flag <option>-fwombat</option> can be negated by
2001 saying <option>-fno-wombat</option>. The flags below are off
2002 by default, except where noted below. See <xref linkend="options-f-compact"/>
2003 for a compact list.
2004 </para>
2005
2006 <variablelist>
2007 <varlistentry>
2008 <term>
2009 <option>-favoid-vect</option>
2010 <indexterm><primary><option></option></primary></indexterm>
2011 </term>
2012 <listitem>
2013 <para>Part of <link linkend="dph">Data Parallel Haskell
2014 (DPH)</link>.</para>
2015
2016 <para><emphasis>Off by default.</emphasis> Enable the
2017 <emphasis>vectorisation</emphasis> avoidance optimisation. This
2018 optimisation only works when used in combination with the
2019 <option>-fvectorise</option> transformation.</para>
2020
2021 <para>While vectorisation of code using DPH is often a big win, it
2022 can also produce worse results for some kinds of code. This
2023 optimisation modifies the vectorisation transformation to try to
2024 determine if a function would be better of unvectorised and if
2025 so, do just that.</para>
2026 </listitem>
2027 </varlistentry>
2028
2029 <varlistentry>
2030 <term>
2031 <option>-fcase-merge</option>
2032 <indexterm><primary><option></option></primary></indexterm>
2033 </term>
2034 <listitem>
2035 <para><emphasis>On by default.</emphasis>
2036 Merge immediately-nested case expressions that scrutinse the same variable. Example
2037 <programlisting>
2038 case x of
2039 Red -> e1
2040 _ -> case x of
2041 Blue -> e2
2042 Green -> e3
2043 ==>
2044 case x of
2045 Red -> e1
2046 Blue -> e2
2047 Green -> e2
2048 </programlisting>
2049 </para>
2050 </listitem>
2051 </varlistentry>
2052
2053 <varlistentry>
2054 <term>
2055 <option>-fcse</option>
2056 <indexterm><primary><option>-fcse</option></primary></indexterm>
2057 </term>
2058 <listitem>
2059 <para><emphasis>On by default.</emphasis>. Enables the common-sub-expression
2060 elimination optimisation.
2061 Switching this off can be useful if you have some <literal>unsafePerformIO</literal>
2062 expressions that you don't want commoned-up.</para>
2063 </listitem>
2064 </varlistentry>
2065
2066 <varlistentry>
2067 <term>
2068 <option>-fdicts-cheap</option>
2069 <indexterm><primary><option></option></primary></indexterm>
2070 </term>
2071 <listitem>
2072 <para>A very experimental flag that makes dictionary-valued
2073 expressions seem cheap to the optimiser.
2074 </para>
2075 </listitem>
2076 </varlistentry>
2077
2078 <varlistentry>
2079 <term>
2080 <option>-fdo-lambda-eta-expansion</option>
2081 <indexterm><primary><option></option></primary></indexterm>
2082 </term>
2083 <listitem>
2084 <para><emphasis>On by default.</emphasis>
2085 Eta-expand let-bindings to increase their arity.
2086 </para>
2087 </listitem>
2088 </varlistentry>
2089
2090 <varlistentry>
2091 <term>
2092 <option>-fdo-eta-reduction</option>
2093 <indexterm><primary><option></option></primary></indexterm>
2094 </term>
2095 <listitem>
2096 <para><emphasis>On by default.</emphasis>
2097 Eta-reduce lambda expressions, if doing so gets rid of a whole
2098 group of lambdas.
2099 </para>
2100 </listitem>
2101 </varlistentry>
2102
2103 <varlistentry>
2104 <term>
2105 <option>-feager-blackholing</option>
2106 <indexterm><primary><option></option></primary></indexterm>
2107 </term>
2108 <listitem>
2109 <para>Usually GHC black-holes a thunk only when it switches
2110 threads. This flag makes it do so as soon as the thunk is
2111 entered. See <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/parallel/">
2112 Haskell on a shared-memory multiprocessor</ulink>.
2113 </para>
2114 </listitem>
2115 </varlistentry>
2116
2117 <varlistentry>
2118 <term>
2119 <option>-fexcess-precision</option>
2120 <indexterm><primary><option>-fexcess-precision</option></primary></indexterm>
2121 </term>
2122 <listitem>
2123 <para>When this option is given, intermediate floating
2124 point values can have a <emphasis>greater</emphasis>
2125 precision/range than the final type. Generally this is a
2126 good thing, but some programs may rely on the exact
2127 precision/range of
2128 <literal>Float</literal>/<literal>Double</literal> values
2129 and should not use this option for their compilation.</para>
2130
2131 <para>
2132 Note that the 32-bit x86 native code generator only
2133 supports excess-precision mode, so neither
2134 <option>-fexcess-precision</option> nor
2135 <option>-fno-excess-precision</option> has any effect.
2136 This is a known bug, see <xref linkend="bugs-ghc" />.
2137 </para>
2138 </listitem>
2139 </varlistentry>
2140
2141 <varlistentry>
2142 <term>
2143 <option>-fexpose-all-unfoldings</option>
2144 <indexterm><primary><option></option></primary></indexterm>
2145 </term>
2146 <listitem>
2147 <para>An experimental flag to expose all unfoldings, even for very
2148 large or recursive functions. This allows for all functions to be
2149 inlined while usually GHC would avoid inlining larger functions.
2150 </para>
2151 </listitem>
2152 </varlistentry>
2153
2154 <varlistentry>
2155 <term>
2156 <option>-ffloat-in</option>
2157 <indexterm><primary><option></option></primary></indexterm>
2158 </term>
2159 <listitem>
2160 <para><emphasis>On by default.</emphasis>
2161 Float let-bindings inwards, nearer their binding site. See
2162 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">
2163 Let-floating: moving bindings to give faster programs (ICFP'96)</ulink>.
2164 </para>
2165
2166 <para>This optimisation moves let bindings closer to their use
2167 site. The benefit here is that this may avoid unnecessary
2168 allocation if the branch the let is now on is never executed. It
2169 also enables other optimisation passes to work more effectively
2170 as they have more information locally.
2171 </para>
2172
2173 <para>This optimisation isn't always beneficial though (so GHC
2174 applies some heuristics to decide when to apply it). The details
2175 get complicated but a simple example is that it is often beneficial
2176 to move let bindings outwards so that multiple let bindings can be
2177 grouped into a larger single let binding, effectively batching
2178 their allocation and helping the garbage collector and allocator.
2179 </para>
2180 </listitem>
2181 </varlistentry>
2182
2183 <varlistentry>
2184 <term>
2185 <option>-ffull-laziness</option>
2186 <indexterm><primary><option>-ffull-laziness</option></primary></indexterm>
2187 </term>
2188 <listitem>
2189 <para><emphasis>On by default.</emphasis>
2190 Run the full laziness optimisation (also known as let-floating),
2191 which floats let-bindings outside enclosing lambdas, in the hope
2192 they will be thereby be computed less often. See
2193 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/float.ps.gz">Let-floating:
2194 moving bindings to give faster programs (ICFP'96)</ulink>.
2195 Full laziness increases sharing, which can lead to increased memory
2196 residency.
2197 </para>
2198
2199 <para>NOTE: GHC doesn't implement complete full-laziness.
2200 When optimisation in on, and <option>-fno-full-laziness</option>
2201 is not given, some transformations that increase sharing are
2202 performed, such as extracting repeated computations from a loop.
2203 These are the same transformations that a fully lazy
2204 implementation would do, the difference is that GHC doesn't
2205 consistently apply full-laziness, so don't rely on it.
2206 </para>
2207 </listitem>
2208 </varlistentry>
2209
2210 <varlistentry>
2211 <term>
2212 <option>-ffun-to-thunk</option>
2213 <indexterm><primary><option>-ffun-to-thunk</option></primary></indexterm>
2214 </term>
2215 <listitem>
2216 <para>Worker-wrapper removes unused arguments, but usually we do
2217 not remove them all, lest it turn a function closure into a thunk,
2218 thereby perhaps creating a space leak and/or disrupting inlining.
2219 This flag allows worker/wrapper to remove <emphasis>all</emphasis>
2220 value lambdas. Off by default.
2221 </para>
2222 </listitem>
2223 </varlistentry>
2224
2225 <varlistentry>
2226 <term>
2227 <option>-fignore-asserts</option>
2228 <indexterm><primary><option>-fignore-asserts</option></primary></indexterm>
2229 </term>
2230 <listitem>
2231 <para>Causes GHC to ignore uses of the function
2232 <literal>Exception.assert</literal> in source code (in
2233 other words, rewriting <literal>Exception.assert p
2234 e</literal> to <literal>e</literal> (see <xref
2235 linkend="assertions"/>). This flag is turned on by
2236 <option>-O</option>.
2237 </para>
2238 </listitem>
2239 </varlistentry>
2240
2241 <varlistentry>
2242 <term>
2243 <option>-fignore-interface-pragmas</option>
2244 <indexterm><primary><option>-fignore-interface-pragmas</option></primary></indexterm>
2245 </term>
2246 <listitem>
2247 <para>Tells GHC to ignore all inessential information when reading interface files.
2248 That is, even if <filename>M.hi</filename> contains unfolding or strictness information
2249 for a function, GHC will ignore that information.</para>
2250 </listitem>
2251 </varlistentry>
2252
2253 <varlistentry>
2254 <term>
2255 <option>-flate-dmd-anal</option>
2256 <indexterm><primary><option>-flate-dmd-anal</option></primary></indexterm>
2257 </term>
2258 <listitem>
2259 <para><emphasis>Off by default.</emphasis>Run demand analysis
2260 again, at the end of the simplification pipeline. We found some opportunities
2261 for discovering strictness that were not visible earlier; and optimisations like
2262 <literal>-fspec-constr</literal> can create functions with unused arguments which
2263 are eliminated by late demand analysis. Improvements are modest, but so is the
2264 cost. See notes on the <ulink href="http://ghc.haskell.org/trac/ghc/wiki/LateDmd">Trac wiki page</ulink>.
2265 </para>
2266 </listitem>
2267 </varlistentry>
2268
2269 <varlistentry>
2270 <term>
2271 <option>-fliberate-case</option>
2272 <indexterm><primary><option>-fliberate-case</option></primary></indexterm>
2273 </term>
2274 <listitem>
2275 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2276 Turn on the liberate-case transformation. This unrolls recursive
2277 function once in its own RHS, to avoid repeated case analysis of
2278 free variables. It's a bit like the call-pattern specialiser
2279 (<option>-fspec-constr</option>) but for free variables rather than
2280 arguments.
2281 </para>
2282 </listitem>
2283 </varlistentry>
2284
2285 <varlistentry>
2286 <term>
2287 <option>-fliberate-case-threshold=N</option>
2288 <indexterm><primary><option>-fliberate-case-threshold</option></primary></indexterm>
2289 </term>
2290 <listitem>
2291 <para>Set the size threshold for the liberate-case transformation.
2292 </para>
2293 </listitem>
2294 </varlistentry>
2295
2296 <varlistentry>
2297 <term>
2298 <option>-fmax-relevant-bindings=N</option>
2299 <indexterm><primary><option>-fmax-relevant-bindings</option></primary></indexterm>
2300 </term>
2301 <listitem>
2302 <para>The type checker sometimes displays a fragment of the type environment
2303 in error messages, but only up to some maximum number, set by this flag.
2304 The default is 6. Turning it off with <option>-fno-max-relevant-bindings</option>
2305 gives an unlimited number. Syntactically top-level bindings are also
2306 usually excluded (since they may be numerous), but
2307 <option>-fno-max-relevant-bindings</option> includes them too.
2308 </para>
2309 </listitem>
2310 </varlistentry>
2311
2312 <varlistentry>
2313 <term>
2314 <option>-fno-state-hack</option>
2315 <indexterm><primary><option>-fno-state-hack</option></primary></indexterm>
2316 </term>
2317 <listitem>
2318 <para>Turn off the "state hack" whereby any lambda with a
2319 <literal>State#</literal> token as argument is considered to be
2320 single-entry, hence it is considered OK to inline things inside
2321 it. This can improve performance of IO and ST monad code, but it
2322 runs the risk of reducing sharing.
2323 </para>
2324 </listitem>
2325 </varlistentry>
2326
2327 <varlistentry>
2328 <term>
2329 <option>-fomit-interface-pragmas</option>
2330 <indexterm><primary><option>-fomit-interface-pragmas</option></primary></indexterm>
2331 </term>
2332 <listitem>
2333 <para>Tells GHC to omit all inessential information from the
2334 interface file generated for the module being compiled (say M).
2335 This means that a module importing M will see only the
2336 <emphasis>types</emphasis> of the functions that M exports, but
2337 not their unfoldings, strictness info, etc. Hence, for example,
2338 no function exported by M will be inlined into an importing module.
2339 The benefit is that modules that import M will need to be
2340 recompiled less often (only when M's exports change their type, not
2341 when they change their implementation).</para>
2342 </listitem>
2343 </varlistentry>
2344
2345 <varlistentry>
2346 <term>
2347 <option>-fomit-yields</option>
2348 <indexterm><primary><option>-fomit-yields</option></primary></indexterm>
2349 </term>
2350 <listitem>
2351 <para><emphasis>On by default.</emphasis> Tells GHC to omit
2352 heap checks when no allocation is being performed. While this improves
2353 binary sizes by about 5%, it also means that threads run in
2354 tight non-allocating loops will not get preempted in a timely
2355 fashion. If it is important to always be able to interrupt such
2356 threads, you should turn this optimization off. Consider also
2357 recompiling all libraries with this optimization turned off, if you
2358 need to guarantee interruptibility.
2359 </para>
2360 </listitem>
2361 </varlistentry>
2362
2363 <varlistentry>
2364 <term>
2365 <option>-fpedantic-bottoms</option>
2366 <indexterm><primary><option>-fpedantic-bottoms</option></primary></indexterm>
2367 </term>
2368 <listitem>
2369 <para>Make GHC be more precise about its treatment of bottom (but see also
2370 <option>-fno-state-hack</option>). In particular, stop GHC
2371 eta-expanding through a case expression, which is good for
2372 performance, but bad if you are using <literal>seq</literal> on
2373 partial applications.
2374 </para>
2375 </listitem>
2376 </varlistentry>
2377
2378 <varlistentry>
2379 <term>
2380 <option>-fregs-graph</option>
2381 <indexterm><primary><option></option></primary></indexterm>
2382 </term>
2383 <listitem>
2384 <para><emphasis>Off by default, but enabled by -O2. Only applies in
2385 combination with the native code generator.</emphasis>
2386 Use the graph colouring register allocator for register allocation
2387 in the native code generator. By default, GHC uses a simpler,
2388 faster linear register allocator. The downside being that the
2389 linear register allocator usually generates worse code.
2390 </para>
2391 </listitem>
2392 </varlistentry>
2393
2394 <varlistentry>
2395 <term>
2396 <option>-fregs-iterative</option>
2397 <indexterm><primary><option></option></primary></indexterm>
2398 </term>
2399 <listitem>
2400 <para><emphasis>Off by default, only applies in combination with
2401 the native code generator.</emphasis>
2402 Use the iterative coalescing graph colouring register allocator for
2403 register allocation in the native code generator. This is the same
2404 register allocator as the <option>-freg-graph</option> one but also
2405 enables iterative coalescing during register allocation.
2406 </para>
2407 </listitem>
2408 </varlistentry>
2409
2410 <varlistentry>
2411 <term>
2412 <option>-fsimpl-tick-factor=<replaceable>n</replaceable></option>
2413 <indexterm><primary><option>-fsimpl-tick-factor</option></primary></indexterm>
2414 </term>
2415 <listitem>
2416 <para>GHC's optimiser can diverge if you write rewrite rules (
2417 <xref linkend="rewrite-rules"/>) that don't terminate, or (less
2418 satisfactorily) if you code up recursion through data types
2419 (<xref linkend="bugs-ghc"/>). To avoid making the compiler fall
2420 into an infinite loop, the optimiser carries a "tick count" and
2421 stops inlining and applying rewrite rules when this count is
2422 exceeded. The limit is set as a multiple of the program size, so
2423 bigger programs get more ticks. The
2424 <option>-fsimpl-tick-factor</option> flag lets you change the
2425 multiplier. The default is 100; numbers larger than 100 give more
2426 ticks, and numbers smaller than 100 give fewer.
2427 </para>
2428
2429 <para>If the tick-count expires, GHC summarises what simplifier
2430 steps it has done; you can use
2431 <option>-fddump-simpl-stats</option> to generate a much more
2432 detailed list. Usually that identifies the loop quite
2433 accurately, because some numbers are very large.
2434 </para>
2435 </listitem>
2436 </varlistentry>
2437
2438 <varlistentry>
2439 <term>
2440 <option>-funfolding-creation-threshold=<replaceable>n</replaceable></option>:
2441 <indexterm><primary><option>-funfolding-creation-threshold</option></primary></indexterm>
2442 <indexterm><primary>inlining, controlling</primary></indexterm>
2443 <indexterm><primary>unfolding, controlling</primary></indexterm>
2444 </term>
2445 <listitem>
2446 <para>(Default: 45) Governs the maximum size that GHC will allow a
2447 function unfolding to be. (An unfolding has a &ldquo;size&rdquo;
2448 that reflects the cost in terms of &ldquo;code bloat&rdquo; of
2449 expanding (aka inlining) that unfolding at a call site. A bigger
2450 function would be assigned a bigger cost.)
2451 </para>
2452
2453 <para>Consequences: (a) nothing larger than this will be inlined
2454 (unless it has an INLINE pragma); (b) nothing larger than this
2455 will be spewed into an interface file.
2456 </para>
2457
2458 <para>Increasing this figure is more likely to result in longer
2459 compile times than faster code. The
2460 <option>-funfolding-use-threshold</option> is more useful.
2461 </para>
2462 </listitem>
2463 </varlistentry>
2464
2465 <varlistentry>
2466 <term>
2467 <option>-funfolding-use-threshold=<replaceable>n</replaceable></option>
2468 <indexterm><primary><option>-funfolding-use-threshold</option></primary></indexterm>
2469 <indexterm><primary>inlining, controlling</primary></indexterm>
2470 <indexterm><primary>unfolding, controlling</primary></indexterm>
2471 </term>
2472 <listitem>
2473 <para>(Default: 8) This is the magic cut-off figure for unfolding
2474 (aka inlining): below this size, a function definition will be
2475 unfolded at the call-site, any bigger and it won't. The size
2476 computed for a function depends on two things: the actual size of
2477 the expression minus any discounts that
2478 apply (see <option>-funfolding-con-discount</option>).
2479 </para>
2480
2481 <para>The difference between this and
2482 <option>-funfolding-creation-threshold</option> is that this one
2483 determines if a function definition will be inlined <emphasis>at
2484 a call site</emphasis>. The other option determines if a
2485 function definition will be kept around at all for potential
2486 inlining.
2487 </para>
2488 </listitem>
2489 </varlistentry>
2490
2491 <varlistentry>
2492 <term>
2493 <option>-fvectorise</option>
2494 <indexterm><primary><option></option></primary></indexterm>
2495 </term>
2496 <listitem>
2497 <para>Part of <link linkend="dph">Data Parallel Haskell
2498 (DPH)</link>.</para>
2499
2500 <para><emphasis>Off by default.</emphasis> Enable the
2501 <emphasis>vectorisation</emphasis> optimisation transformation. This
2502 optimisation transforms the nested data parallelism code of programs
2503 using DPH into flat data parallelism. Flat data parallel programs
2504 should have better load balancing, enable SIMD parallelism and
2505 friendlier cache behaviour.</para>
2506 </listitem>
2507 </varlistentry>
2508
2509 <varlistentry>
2510 <term>
2511 <option>-fspec-constr</option>
2512 <indexterm><primary><option>-fspec-constr</option></primary></indexterm>
2513 </term>
2514 <listitem>
2515 <para><emphasis>Off by default, but enabled by -O2.</emphasis>
2516 Turn on call-pattern specialisation; see
2517 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/spec-constr/index.htm">
2518 Call-pattern specialisation for Haskell programs</ulink>.
2519 </para>
2520
2521 <para>This optimisation specializes recursive functions according to
2522 their argument "shapes". This is best explained by example so
2523 consider:
2524 <programlisting>
2525 last :: [a] -> a
2526 last [] = error "last"
2527 last (x : []) = x
2528 last (x : xs) = last xs
2529 </programlisting>
2530 In this code, once we pass the initial check for an empty list we
2531 know that in the recursive case this pattern match is redundant. As
2532 such <option>-fspec-constr</option> will transform the above code
2533 to:
2534 <programlisting>
2535 last :: [a] -> a
2536 last [] = error "last"
2537 last (x : xs) = last' x xs
2538 where
2539 last' x [] = x
2540 last' x (y : ys) = last' y ys
2541 </programlisting>
2542 </para>
2543
2544 <para>As well avoid unnecessary pattern matching it also helps avoid
2545 unnecessary allocation. This applies when a argument is strict in
2546 the recursive call to itself but not on the initial entry. As
2547 strict recursive branch of the function is created similar to the
2548 above example.
2549 </para>
2550
2551 <para>It is also possible for library writers to instruct
2552 GHC to perform call-pattern specialisation extremely
2553 aggressively. This is necessary for some highly optimized
2554 libraries, where we may want to specialize regardless of
2555 the number of specialisations, or the size of the code. As
2556 an example, consider a simplified use-case from the
2557 <literal>vector</literal> library:</para>
2558 <programlisting>
2559 import GHC.Types (SPEC(..))
2560
2561 foldl :: (a -> b -> a) -> a -> Stream b -> a
2562 {-# INLINE foldl #-}
2563 foldl f z (Stream step s _) = foldl_loop SPEC z s
2564 where
2565 foldl_loop !sPEC z s = case step s of
2566 Yield x s' -> foldl_loop sPEC (f z x) s'
2567 Skip -> foldl_loop sPEC z s'
2568 Done -> z
2569 </programlisting>
2570
2571 <para>Here, after GHC inlines the body of
2572 <literal>foldl</literal> to a call site, it will perform
2573 call-pattern specialization very aggressively on
2574 <literal>foldl_loop</literal> due to the use of
2575 <literal>SPEC</literal> in the argument of the loop
2576 body. <literal>SPEC</literal> from
2577 <literal>GHC.Types</literal> is specifically recognized by
2578 the compiler.</para>
2579
2580 <para>(NB: it is extremely important you use
2581 <literal>seq</literal> or a bang pattern on the
2582 <literal>SPEC</literal> argument!)</para>
2583
2584 <para>In particular, after inlining this will
2585 expose <literal>f</literal> to the loop body directly,
2586 allowing heavy specialisation over the recursive
2587 cases.</para>
2588 </listitem>
2589 </varlistentry>
2590
2591 <varlistentry>
2592 <term>
2593 <option>-fspecialise</option>
2594 <indexterm><primary><option>-fspecialise</option></primary></indexterm>
2595 </term>
2596 <listitem>
2597 <para><emphasis>On by default.</emphasis>
2598 Specialise each type-class-overloaded function defined in this
2599 module for the types at which it is called in this module. Also
2600 specialise imported functions that have an INLINABLE pragma
2601 (<xref linkend="inlinable-pragma"/>) for the types at which they
2602 are called in this module.
2603 </para>
2604 </listitem>
2605 </varlistentry>
2606
2607 <varlistentry>
2608 <term>
2609 <option>-fstatic-argument-transformation</option>
2610 <indexterm><primary><option>-fstatic-argument-transformation</option></primary></indexterm>
2611 </term>
2612 <listitem>
2613 <para>Turn on the static argument transformation, which turns a
2614 recursive function into a non-recursive one with a local
2615 recursive loop. See Chapter 7 of
2616 <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/santos-thesis.ps.gz">
2617 Andre Santos's PhD thesis</ulink>
2618 </para>
2619 </listitem>
2620 </varlistentry>
2621
2622 <varlistentry>
2623 <term>
2624 <option>-fstrictness</option>
2625 <indexterm><primary><option></option></primary></indexterm>
2626 </term>
2627 <listitem>
2628 <para> <emphasis>On by default.</emphasis>.
2629 Switch on the strictness analyser. There is a very old paper about GHC's
2630 strictness analyser, <ulink url="http://research.microsoft.com/en-us/um/people/simonpj/papers/simple-strictnes-analyser.ps.gz">
2631 Measuring the effectiveness of a simple strictness analyser</ulink>,
2632 but the current one is quite a bit different.
2633 </para>
2634
2635 <para>The strictness analyser figures out when arguments and
2636 variables in a function can be treated 'strictly' (that is they
2637 are always evaluated in the function at some point). This allow
2638 GHC to apply certain optimisations such as unboxing that
2639 otherwise don't apply as they change the semantics of the program
2640 when applied to lazy arguments.
2641 </para>
2642 </listitem>
2643 </varlistentry>
2644
2645 <varlistentry>
2646 <term>
2647 <option>-funbox-strict-fields</option>:
2648 <indexterm><primary><option>-funbox-strict-fields</option></primary></indexterm>
2649 <indexterm><primary>strict constructor fields</primary></indexterm>
2650 <indexterm><primary>constructor fields, strict</primary></indexterm>
2651 </term>
2652 <listitem>
2653 <para>This option causes all constructor fields which are marked
2654 strict (i.e. &ldquo;!&rdquo;) to be unpacked if possible. It is
2655 equivalent to adding an <literal>UNPACK</literal> pragma to every
2656 strict constructor field (see <xref linkend="unpack-pragma"/>).
2657 </para>
2658
2659 <para>This option is a bit of a sledgehammer: it might sometimes
2660 make things worse. Selectively unboxing fields by using
2661 <literal>UNPACK</literal> pragmas might be better. An alternative
2662 is to use <option>-funbox-strict-fields</option> to turn on
2663 unboxing by default but disable it for certain constructor
2664 fields using the <literal>NOUNPACK</literal> pragma (see
2665 <xref linkend="nounpack-pragma"/>).</para>
2666 </listitem>
2667 </varlistentry>
2668
2669 <varlistentry>
2670 <term>
2671 <option>-funbox-small-strict-fields</option>:
2672 <indexterm><primary><option>-funbox-small-strict-fields</option></primary></indexterm>
2673 <indexterm><primary>strict constructor fields</primary></indexterm>
2674 <indexterm><primary>constructor fields, strict</primary></indexterm>
2675 </term>
2676 <listitem>
2677 <para><emphasis>On by default.</emphasis>. This option
2678 causes all constructor fields which are marked strict
2679 (i.e. &ldquo;!&rdquo;) and which representation is smaller
2680 or equal to the size of a pointer to be unpacked, if
2681 possible. It is equivalent to adding an
2682 <literal>UNPACK</literal> pragma (see <xref
2683 linkend="unpack-pragma"/>) to every strict constructor
2684 field that fulfils the size restriction.
2685 </para>
2686
2687 <para>For example, the constructor fields in the following
2688 data types
2689 <programlisting>
2690 data A = A !Int
2691 data B = B !A
2692 newtype C = C B
2693 data D = D !C
2694 </programlisting>
2695 would all be represented by a single
2696 <literal>Int#</literal> (see <xref linkend="primitives"/>)
2697 value with
2698 <option>-funbox-small-strict-fields</option> enabled.
2699 </para>
2700
2701 <para>This option is less of a sledgehammer than
2702 <option>-funbox-strict-fields</option>: it should rarely make things
2703 worse. If you use <option>-funbox-small-strict-fields</option>
2704 to turn on unboxing by default you can disable it for certain
2705 constructor fields using the <literal>NOUNPACK</literal> pragma (see
2706 <xref linkend="nounpack-pragma"/>).</para>
2707
2708 <para>
2709 Note that for consistency <literal>Double</literal>,
2710 <literal>Word64</literal>, and <literal>Int64</literal> constructor
2711 fields are unpacked on 32-bit platforms, even though they are
2712 technically larger than a pointer on those platforms.
2713 </para>
2714 </listitem>
2715 </varlistentry>
2716
2717 </variablelist>
2718
2719 </sect2>
2720
2721 </sect1>
2722
2723 &code-gens;
2724
2725 &phases;
2726
2727 &shared_libs;
2728
2729 <sect1 id="using-concurrent">
2730 <title>Using Concurrent Haskell</title>
2731 <indexterm><primary>Concurrent Haskell</primary><secondary>using</secondary></indexterm>
2732
2733 <para>GHC supports Concurrent Haskell by default, without requiring a
2734 special option or libraries compiled in a certain way. To get access to
2735 the support libraries for Concurrent Haskell, just import
2736 <ulink
2737 url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>. More information on Concurrent Haskell is provided in the documentation for that module.</para>
2738
2739 <para>
2740 Optionally, the program may be linked with
2741 the <option>-threaded</option> option (see
2742 <xref linkend="options-linker" />. This provides two benefits:
2743
2744 <itemizedlist>
2745 <listitem>
2746 <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
2747 used, which allows threads to run in
2748 parallel<indexterm><primary>parallelism</primary></indexterm>
2749 on a
2750 multiprocessor<indexterm><primary>multiprocessor</primary></indexterm><indexterm><primary>SMP</primary></indexterm>
2751 or
2752 multicore<indexterm><primary>multicore</primary></indexterm>
2753 machine. See <xref linkend="using-smp" />.</para>
2754 </listitem>
2755 <listitem>
2756 <para>If a thread makes a foreign call (and the call is
2757 not marked <literal>unsafe</literal>), then other
2758 Haskell threads in the program will continue to run
2759 while the foreign call is in progress.
2760 Additionally, <literal>foreign export</literal>ed
2761 Haskell functions may be called from multiple OS
2762 threads simultaneously. See
2763 <xref linkend="ffi-threads" />.</para>
2764 </listitem>
2765 </itemizedlist>
2766 </para>
2767
2768 <para>The following RTS option(s) affect the behaviour of Concurrent
2769 Haskell programs:<indexterm><primary>RTS options, concurrent</primary></indexterm></para>
2770
2771 <variablelist>
2772 <varlistentry>
2773 <term><option>-C<replaceable>s</replaceable></option></term>
2774 <listitem>
2775 <para><indexterm><primary><option>-C<replaceable>s</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
2776 Sets the context switch interval to <replaceable>s</replaceable>
2777 seconds. A context switch will occur at the next heap block
2778 allocation after the timer expires (a heap block allocation occurs
2779 every 4k of allocation). With <option>-C0</option> or
2780 <option>-C</option>, context switches will occur as often as
2781 possible (at every heap block allocation). By default, context
2782 switches occur every 20ms.</para>
2783 </listitem>
2784 </varlistentry>
2785 </variablelist>
2786 </sect1>
2787
2788 <sect1 id="using-smp">
2789 <title>Using SMP parallelism</title>
2790 <indexterm><primary>parallelism</primary>
2791 </indexterm>
2792 <indexterm><primary>SMP</primary>
2793 </indexterm>
2794
2795 <para>GHC supports running Haskell programs in parallel on an SMP
2796 (symmetric multiprocessor).</para>
2797
2798 <para>There's a fine distinction between
2799 <emphasis>concurrency</emphasis> and <emphasis>parallelism</emphasis>:
2800 parallelism is all about making your program run
2801 <emphasis>faster</emphasis> by making use of multiple processors
2802 simultaneously. Concurrency, on the other hand, is a means of
2803 abstraction: it is a convenient way to structure a program that must
2804 respond to multiple asynchronous events.</para>
2805
2806 <para>However, the two terms are certainly related. By making use of
2807 multiple CPUs it is possible to run concurrent threads in parallel,
2808 and this is exactly what GHC's SMP parallelism support does. But it
2809 is also possible to obtain performance improvements with parallelism
2810 on programs that do not use concurrency. This section describes how to
2811 use GHC to compile and run parallel programs, in <xref
2812 linkend="lang-parallel" /> we describe the language features that affect
2813 parallelism.</para>
2814
2815 <sect2 id="parallel-compile-options">
2816 <title>Compile-time options for SMP parallelism</title>
2817
2818 <para>In order to make use of multiple CPUs, your program must be
2819 linked with the <option>-threaded</option> option (see <xref
2820 linkend="options-linker" />). Additionally, the following
2821 compiler options affect parallelism:</para>
2822
2823 <variablelist>
2824 <varlistentry>
2825 <term><option>-feager-blackholing</option></term>
2826 <indexterm><primary><option>-feager-blackholing</option></primary></indexterm>
2827 <listitem>
2828 <para>
2829 Blackholing is the act of marking a thunk (lazy
2830 computuation) as being under evaluation. It is useful for
2831 three reasons: firstly it lets us detect certain kinds of
2832 infinite loop (the <literal>NonTermination</literal>
2833 exception), secondly it avoids certain kinds of space
2834 leak, and thirdly it avoids repeating a computation in a
2835 parallel program, because we can tell when a computation
2836 is already in progress.</para>
2837
2838 <para>
2839 The option <option>-feager-blackholing</option> causes
2840 each thunk to be blackholed as soon as evaluation begins.
2841 The default is "lazy blackholing", whereby thunks are only
2842 marked as being under evaluation when a thread is paused
2843 for some reason. Lazy blackholing is typically more
2844 efficient (by 1-2&percnt; or so), because most thunks don't
2845 need to be blackholed. However, eager blackholing can
2846 avoid more repeated computation in a parallel program, and
2847 this often turns out to be important for parallelism.
2848 </para>
2849
2850 <para>
2851 We recommend compiling any code that is intended to be run
2852 in parallel with the <option>-feager-blackholing</option>
2853 flag.
2854 </para>
2855 </listitem>
2856 </varlistentry>
2857 </variablelist>
2858 </sect2>
2859
2860 <sect2 id="parallel-options">
2861 <title>RTS options for SMP parallelism</title>
2862
2863 <para>There are two ways to run a program on multiple
2864 processors:
2865 call <literal>Control.Concurrent.setNumCapabilities</literal> from your
2866 program, or use the RTS <option>-N</option> option.</para>
2867
2868 <variablelist>
2869 <varlistentry>
2870 <term><option>-N<optional><replaceable>x</replaceable></optional></option></term>
2871 <listitem>
2872 <para><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
2873 Use <replaceable>x</replaceable> simultaneous threads when
2874 running the program. Normally <replaceable>x</replaceable>
2875 should be chosen to match the number of CPU cores on the
2876 machine<footnote><para>Whether hyperthreading cores should be counted or not is an
2877 open question; please feel free to experiment and let us know what
2878 results you find.</para></footnote>. For example,
2879 on a dual-core machine we would probably use
2880 <literal>+RTS -N2 -RTS</literal>.</para>
2881
2882 <para>Omitting <replaceable>x</replaceable>,
2883 i.e. <literal>+RTS -N -RTS</literal>, lets the runtime
2884 choose the value of <replaceable>x</replaceable> itself
2885 based on how many processors are in your machine.</para>
2886
2887 <para>Be careful when using all the processors in your
2888 machine: if some of your processors are in use by other
2889 programs, this can actually harm performance rather than
2890 improve it.</para>
2891
2892 <para>Setting <option>-N</option> also has the effect of
2893 enabling the parallel garbage collector (see
2894 <xref linkend="rts-options-gc" />).</para>
2895
2896 <para>The current value of the <option>-N</option> option
2897 is available to the Haskell program
2898 via <literal>Control.Concurrent.getNumCapabilities</literal>, and
2899 it may be changed while the program is running by
2900 calling <literal>Control.Concurrent.setNumCapabilities</literal>.</para>
2901 </listitem>
2902 </varlistentry>
2903 </variablelist>
2904
2905 <para>The following options affect the way the runtime schedules
2906 threads on CPUs:</para>
2907
2908 <variablelist>
2909 <varlistentry>
2910 <term><option>-qa</option></term>
2911 <indexterm><primary><option>-qa</option></primary><secondary>RTS
2912 option</secondary></indexterm>
2913 <listitem>
2914 <para>Use the OS's affinity facilities to try to pin OS
2915 threads to CPU cores. This is an experimental feature,
2916 and may or may not be useful. Please let us know
2917 whether it helps for you!</para>
2918 </listitem>
2919 </varlistentry>
2920 <varlistentry>
2921 <term><option>-qm</option></term>
2922 <indexterm><primary><option>-qm</option></primary><secondary>RTS
2923 option</secondary></indexterm>
2924 <listitem>
2925 <para>Disable automatic migration for load balancing.
2926 Normally the runtime will automatically try to schedule
2927 threads across the available CPUs to make use of idle
2928 CPUs; this option disables that behaviour. Note that
2929 migration only applies to threads; sparks created
2930 by <literal>par</literal> are load-balanced separately
2931 by work-stealing.</para>
2932
2933 <para>
2934 This option is probably only of use for concurrent
2935 programs that explicitly schedule threads onto CPUs
2936 with <literal>Control.Concurrent.forkOn</literal>.
2937 </para>
2938 </listitem>
2939 </varlistentry>
2940 </variablelist>
2941 </sect2>
2942
2943 <sect2>
2944 <title>Hints for using SMP parallelism</title>
2945
2946 <para>Add the <literal>-s</literal> RTS option when
2947 running the program to see timing stats, which will help to tell you
2948 whether your program got faster by using more CPUs or not. If the user
2949 time is greater than
2950 the elapsed time, then the program used more than one CPU. You should
2951 also run the program without <literal>-N</literal> for
2952 comparison.</para>
2953
2954 <para>The output of <literal>+RTS -s</literal> tells you how
2955 many &ldquo;sparks&rdquo; were created and executed during the
2956 run of the program (see <xref linkend="rts-options-gc" />), which
2957 will give you an idea how well your <literal>par</literal>
2958 annotations are working.</para>
2959
2960 <para>GHC's parallelism support has improved in 6.12.1 as a
2961 result of much experimentation and tuning in the runtime
2962 system. We'd still be interested to hear how well it works
2963 for you, and we're also interested in collecting parallel
2964 programs to add to our benchmarking suite.</para>
2965 </sect2>
2966 </sect1>
2967
2968 <sect1 id="options-platform">
2969 <title>Platform-specific Flags</title>
2970
2971 <indexterm><primary>-m* options</primary></indexterm>
2972 <indexterm><primary>platform-specific options</primary></indexterm>
2973 <indexterm><primary>machine-specific options</primary></indexterm>
2974
2975 <para>Some flags only make sense for particular target
2976 platforms.</para>
2977
2978 <variablelist>
2979
2980 <varlistentry>
2981 <term><option>-msse2</option>:</term>
2982 <listitem>
2983 <para>
2984 (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
2985 instruction set to implement floating point operations when using
2986 the <link linkend="native-code-gen">native code generator</link>.
2987 This gives a substantial performance improvement for floating
2988 point, but the resulting compiled code
2989 will only run on processors that support SSE2 (Intel Pentium 4 and
2990 later, or AMD Athlon 64 and later). The
2991 <link linkend="llvm-code-gen">LLVM backend</link> will also use SSE2
2992 if your processor supports it but detects this automatically so no
2993 flag is required.
2994 </para>
2995 <para>
2996 SSE2 is unconditionally used on x86-64 platforms.
2997 </para>
2998 </listitem>
2999 </varlistentry>
3000
3001 <varlistentry>
3002 <term><option>-msse4.2</option>:</term>
3003 <listitem>
3004 <para>
3005 (x86 only, added in GHC 7.4.1) Use the SSE4.2 instruction set to
3006 implement some floating point and bit operations when using the
3007 <link linkend="native-code-gen">native code generator</link>. The
3008 resulting compiled code will only run on processors that
3009 support SSE4.2 (Intel Core i7 and later). The
3010 <link linkend="llvm-code-gen">LLVM backend</link> will also use
3011 SSE4.2 if your processor supports it but detects this automatically
3012 so no flag is required.
3013 </para>
3014 </listitem>
3015 </varlistentry>
3016
3017 </variablelist>
3018
3019 </sect1>
3020
3021 &runtime;
3022 &debug;
3023 &flags;
3024
3025 </chapter>
3026
3027 <!-- Emacs stuff:
3028 ;;; Local Variables: ***
3029 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
3030 ;;; End: ***
3031 -->