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