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