Don't document '-fregs-liveness' or '-fllvm-tbaa', both hidden flags
[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>, and
958 <option>-fwarn-dodgy-foreign-imports</option>. The following
959 flags are
960 simple ways to select standard &ldquo;packages&rdquo; of warnings:
961 </para>
962
963 <variablelist>
964
965 <varlistentry>
966 <term><option>-W</option>:</term>
967 <listitem>
968 <indexterm><primary>-W option</primary></indexterm>
969 <para>Provides the standard warnings plus
970 <option>-fwarn-incomplete-patterns</option>,
971 <option>-fwarn-dodgy-exports</option>,
972 <option>-fwarn-dodgy-imports</option>,
973 <option>-fwarn-unused-matches</option>,
974 <option>-fwarn-unused-imports</option>, and
975 <option>-fwarn-unused-binds</option>.</para>
976 </listitem>
977 </varlistentry>
978
979 <varlistentry>
980 <term><option>-Wall</option>:</term>
981 <listitem>
982 <indexterm><primary><option>-Wall</option></primary></indexterm>
983 <para>Turns on all warning options that indicate potentially
984 suspicious code. The warnings that are
985 <emphasis>not</emphasis> enabled by <option>-Wall</option>
986 are
987 <option>-fwarn-tabs</option>,
988 <option>-fwarn-incomplete-uni-patterns</option>,
989 <option>-fwarn-incomplete-record-updates</option>,
990 <option>-fwarn-monomorphism-restriction</option>,
991 <option>-fwarn-unrecognised-pragmas</option>,
992 <option>-fwarn-auto-orphans</option>,
993 <option>-fwarn-implicit-prelude</option>.</para>
994 </listitem>
995 </varlistentry>
996
997 <varlistentry>
998 <term><option>-w</option>:</term>
999 <listitem>
1000 <indexterm><primary><option>-w</option></primary></indexterm>
1001 <para>Turns off all warnings, including the standard ones and
1002 those that <literal>-Wall</literal> doesn't enable.</para>
1003 </listitem>
1004 </varlistentry>
1005
1006 <varlistentry>
1007 <term><option>-Werror</option>:</term>
1008 <listitem>
1009 <indexterm><primary><option>-Werror</option></primary></indexterm>
1010 <para>Makes any warning into a fatal error. Useful so that you don't
1011 miss warnings when doing batch compilation. </para>
1012 </listitem>
1013 </varlistentry>
1014
1015 <varlistentry>
1016 <term><option>-Wwarn</option>:</term>
1017 <listitem>
1018 <indexterm><primary><option>-Wwarn</option></primary></indexterm>
1019 <para>Warnings are treated only as warnings, not as errors. This is
1020 the default, but can be useful to negate a
1021 <option>-Werror</option> flag.</para>
1022 </listitem>
1023 </varlistentry>
1024
1025 </variablelist>
1026
1027 <para>The full set of warning options is described below. To turn
1028 off any warning, simply give the corresponding
1029 <option>-fno-warn-...</option> option on the command line.</para>
1030
1031 <variablelist>
1032
1033 <varlistentry>
1034 <term><option>-fdefer-type-errors</option>:</term>
1035 <listitem>
1036 <indexterm><primary><option>-fdefer-type-errors</option></primary>
1037 </indexterm>
1038 <indexterm><primary>warnings</primary></indexterm>
1039 <para>Defer as many type errors as possible until runtime.
1040 At compile time you get a warning (instead of an error). At
1041 runtime, if you use a value that depends on a type error, you
1042 get a runtime error; but you can run any type-correct parts of your code
1043 just fine.</para>
1044 </listitem>
1045 </varlistentry>
1046
1047 <varlistentry>
1048 <term><option>-fhelpful-errors</option>:</term>
1049 <listitem>
1050 <indexterm><primary><option>-fhelpful-errors</option></primary>
1051 </indexterm>
1052 <indexterm><primary>warnings</primary></indexterm>
1053 <para>When a name or package is not found in scope, make
1054 suggestions for the name or package you might have meant instead.</para>
1055 <para>This option is on by default.</para>
1056 </listitem>
1057 </varlistentry>
1058
1059 <varlistentry>
1060 <term><option>-fwarn-unrecognised-pragmas</option>:</term>
1061 <listitem>
1062 <indexterm><primary><option>-fwarn-unrecognised-pragmas</option></primary>
1063 </indexterm>
1064 <indexterm><primary>warnings</primary></indexterm>
1065 <indexterm><primary>pragmas</primary></indexterm>
1066 <para>Causes a warning to be emitted when a
1067 pragma that GHC doesn't recognise is used. As well as pragmas
1068 that GHC itself uses, GHC also recognises pragmas known to be used
1069 by other tools, e.g. <literal>OPTIONS_HUGS</literal> and
1070 <literal>DERIVE</literal>.</para>
1071
1072 <para>This option is on by default.</para>
1073 </listitem>
1074 </varlistentry>
1075
1076 <varlistentry>
1077 <term><option>-fwarn-warnings-deprecations</option>:</term>
1078 <listitem>
1079 <indexterm><primary><option>-fwarn-warnings-deprecations</option></primary>
1080 </indexterm>
1081 <indexterm><primary>warnings</primary></indexterm>
1082 <indexterm><primary>deprecations</primary></indexterm>
1083 <para>Causes a warning to be emitted when a
1084 module, function or type with a WARNING or DEPRECATED pragma
1085 is used. See <xref linkend="warning-deprecated-pragma"/> for more
1086 details on the pragmas.</para>
1087
1088 <para>This option is on by default.</para>
1089 </listitem>
1090 </varlistentry>
1091
1092 <varlistentry>
1093 <term><option>-fwarn-deprecated-flags</option>:</term>
1094 <listitem>
1095 <indexterm><primary><option>-fwarn-deprecated-flags</option></primary>
1096 </indexterm>
1097 <indexterm><primary>deprecated-flags</primary></indexterm>
1098 <para>Causes a warning to be emitted when a deprecated
1099 commandline flag is used.</para>
1100
1101 <para>This option is on by default.</para>
1102 </listitem>
1103 </varlistentry>
1104
1105 <varlistentry>
1106 <term><option>-fwarn-dodgy-foreign-imports</option>:</term>
1107 <listitem>
1108 <indexterm><primary><option>-fwarn-dodgy-foreign-imports</option></primary>
1109 </indexterm>
1110 <para>Causes a warning to be emitted for foreign imports of
1111 the following form:</para>
1112
1113 <programlisting>
1114 foreign import "f" f :: FunPtr t
1115 </programlisting>
1116
1117 <para>on the grounds that it probably should be</para>
1118
1119 <programlisting>
1120 foreign import "&amp;f" f :: FunPtr t
1121 </programlisting>
1122
1123 <para>The first form declares that `f` is a (pure) C
1124 function that takes no arguments and returns a pointer to a
1125 C function with type `t`, whereas the second form declares
1126 that `f` itself is a C function with type `t`. The first
1127 declaration is usually a mistake, and one that is hard to
1128 debug because it results in a crash, hence this
1129 warning.</para>
1130 </listitem>
1131 </varlistentry>
1132
1133 <varlistentry>
1134 <term><option>-fwarn-dodgy-exports</option>:</term>
1135 <listitem>
1136 <indexterm><primary><option>-fwarn-dodgy-exports</option></primary>
1137 </indexterm>
1138 <para>Causes a warning to be emitted when a datatype
1139 <literal>T</literal> is exported
1140 with all constructors, i.e. <literal>T(..)</literal>, but is it
1141 just a type synonym.</para>
1142 <para>Also causes a warning to be emitted when a module is
1143 re-exported, but that module exports nothing.</para>
1144 </listitem>
1145 </varlistentry>
1146
1147 <varlistentry>
1148 <term><option>-fwarn-dodgy-imports</option>:</term>
1149 <listitem>
1150 <indexterm><primary><option>-fwarn-dodgy-imports</option></primary>
1151 </indexterm>
1152 <para>Causes a warning to be emitted when a datatype
1153 <literal>T</literal> is imported
1154 with all constructors, i.e. <literal>T(..)</literal>, but has been
1155 exported abstractly, i.e. <literal>T</literal>.</para>
1156 </listitem>
1157 </varlistentry>
1158
1159 <varlistentry>
1160 <term><option>-fwarn-lazy-unlifted-bindings</option>:</term>
1161 <listitem>
1162 <indexterm><primary><option>-fwarn-lazy-unlifted-bindings</option></primary>
1163 </indexterm>
1164 <para>Causes a warning to be emitted when an unlifted type
1165 is bound in a way that looks lazy, e.g.
1166 <literal>where (I# x) = ...</literal>. Use
1167 <literal>where !(I# x) = ...</literal> instead. This will be an
1168 error, rather than a warning, in GHC 7.2.
1169 </para>
1170 </listitem>
1171 </varlistentry>
1172
1173 <varlistentry>
1174 <term><option>-fwarn-duplicate-exports</option>:</term>
1175 <listitem>
1176 <indexterm><primary><option>-fwarn-duplicate-exports</option></primary></indexterm>
1177 <indexterm><primary>duplicate exports, warning</primary></indexterm>
1178 <indexterm><primary>export lists, duplicates</primary></indexterm>
1179
1180 <para>Have the compiler warn about duplicate entries in
1181 export lists. This is useful information if you maintain
1182 large export lists, and want to avoid the continued export
1183 of a definition after you've deleted (one) mention of it in
1184 the export list.</para>
1185
1186 <para>This option is on by default.</para>
1187 </listitem>
1188 </varlistentry>
1189
1190 <varlistentry>
1191 <term><option>-fwarn-hi-shadowing</option>:</term>
1192 <listitem>
1193 <indexterm><primary><option>-fwarn-hi-shadowing</option></primary></indexterm>
1194 <indexterm><primary>shadowing</primary>
1195 <secondary>interface files</secondary></indexterm>
1196
1197 <para>Causes the compiler to emit a warning when a module or
1198 interface file in the current directory is shadowing one
1199 with the same module name in a library or other
1200 directory.</para>
1201 </listitem>
1202 </varlistentry>
1203
1204 <varlistentry>
1205 <term><option>-fwarn-identities</option>:</term>
1206 <listitem>
1207 <indexterm><primary><option>-fwarn-identities</option></primary></indexterm>
1208 <para>Causes the compiler to emit a warning when a Prelude numeric
1209 conversion converts a type T to the same type T; such calls
1210 are probably no-ops and can be omitted. The functions checked for
1211 are: <literal>toInteger</literal>,
1212 <literal>toRational</literal>,
1213 <literal>fromIntegral</literal>,
1214 and <literal>realToFrac</literal>.
1215 </para>
1216 </listitem>
1217 </varlistentry>
1218
1219 <varlistentry>
1220 <term><option>-fwarn-implicit-prelude</option>:</term>
1221 <listitem>
1222 <indexterm><primary><option>-fwarn-implicit-prelude</option></primary></indexterm>
1223 <indexterm><primary>implicit prelude, warning</primary></indexterm>
1224 <para>Have the compiler warn if the Prelude is implicitly
1225 imported. This happens unless either the Prelude module is
1226 explicitly imported with an <literal>import ... Prelude ...</literal>
1227 line, or this implicit import is disabled (either by
1228 <option>-XNoImplicitPrelude</option> or a
1229 <literal>LANGUAGE NoImplicitPrelude</literal> pragma).</para>
1230
1231 <para>Note that no warning is given for syntax that implicitly
1232 refers to the Prelude, even if <option>-XNoImplicitPrelude</option>
1233 would change whether it refers to the Prelude.
1234 For example, no warning is given when
1235 <literal>368</literal> means
1236 <literal>Prelude.fromInteger (368::Prelude.Integer)</literal>
1237 (where <literal>Prelude</literal> refers to the actual Prelude module,
1238 regardless of the imports of the module being compiled).</para>
1239
1240 <para>This warning is off by default.</para>
1241 </listitem>
1242 </varlistentry>
1243
1244 <varlistentry>
1245 <term><option>-fwarn-incomplete-patterns</option>,
1246 <option>-fwarn-incomplete-uni-patterns</option>:
1247 </term>
1248 <listitem>
1249 <indexterm><primary><option>-fwarn-incomplete-patterns</option></primary></indexterm>
1250 <indexterm><primary><option>-fwarn-incomplete-uni-patterns</option></primary></indexterm>
1251 <indexterm><primary>incomplete patterns, warning</primary></indexterm>
1252 <indexterm><primary>patterns, incomplete</primary></indexterm>
1253
1254 <para>The option <option>-fwarn-incomplete-patterns</option> warns
1255 about places where
1256 a pattern-match might fail at runtime.
1257 The function
1258 <function>g</function> below will fail when applied to
1259 non-empty lists, so the compiler will emit a warning about
1260 this when <option>-fwarn-incomplete-patterns</option> is
1261 enabled.
1262
1263 <programlisting>
1264 g [] = 2
1265 </programlisting>
1266
1267 This option isn't enabled by default because it can be
1268 a bit noisy, and it doesn't always indicate a bug in the
1269 program. However, it's generally considered good practice
1270 to cover all the cases in your functions, and it is switched
1271 on by <option>-W</option>.</para>
1272
1273 <para>The flag <option>-fwarn-incomplete-uni-patterns</option> is
1274 similar, except that it
1275 applies only to lambda-expressions and pattern bindings, constructs
1276 that only allow a single pattern:
1277
1278 <programlisting>
1279 h = \[] -> 2
1280 Just k = f y
1281 </programlisting>
1282
1283 </para>
1284 </listitem>
1285 </varlistentry>
1286
1287 <varlistentry>
1288 <term><option>-fwarn-incomplete-record-updates</option>:</term>
1289 <listitem>
1290 <indexterm><primary><option>-fwarn-incomplete-record-updates</option></primary></indexterm>
1291 <indexterm><primary>incomplete record updates, warning</primary></indexterm>
1292 <indexterm><primary>record updates, incomplete</primary></indexterm>
1293
1294 <para>The function
1295 <function>f</function> below will fail when applied to
1296 <literal>Bar</literal>, so the compiler will emit a warning about
1297 this when <option>-fwarn-incomplete-record-updates</option> is
1298 enabled.</para>
1299
1300 <programlisting>
1301 data Foo = Foo { x :: Int }
1302 | Bar
1303
1304 f :: Foo -> Foo
1305 f foo = foo { x = 6 }
1306 </programlisting>
1307
1308 <para>This option isn't enabled by default because it can be
1309 very noisy, and it often doesn't indicate a bug in the
1310 program.</para>
1311 </listitem>
1312 </varlistentry>
1313
1314 <varlistentry>
1315 <term>
1316 <option>-fwarn-missing-fields</option>:
1317 <indexterm><primary><option>-fwarn-missing-fields</option></primary></indexterm>
1318 <indexterm><primary>missing fields, warning</primary></indexterm>
1319 <indexterm><primary>fields, missing</primary></indexterm>
1320 </term>
1321 <listitem>
1322
1323 <para>This option is on by default, and warns you whenever
1324 the construction of a labelled field constructor isn't
1325 complete, missing initializers for one or more fields. While
1326 not an error (the missing fields are initialised with
1327 bottoms), it is often an indication of a programmer error.</para>
1328 </listitem>
1329 </varlistentry>
1330
1331 <varlistentry>
1332 <term>
1333 <option>-fwarn-missing-import-lists</option>:
1334 <indexterm><primary><option>-fwarn-import-lists</option></primary></indexterm>
1335 <indexterm><primary>missing import lists, warning</primary></indexterm>
1336 <indexterm><primary>import lists, missing</primary></indexterm>
1337 </term>
1338 <listitem>
1339
1340 <para>This flag warns if you use an unqualified
1341 <literal>import</literal> declaration
1342 that does not explicitly list the entities brought into scope. For
1343 example
1344 </para>
1345
1346 <programlisting>
1347 module M where
1348 import X( f )
1349 import Y
1350 import qualified Z
1351 p x = f x x
1352 </programlisting>
1353
1354 <para>
1355 The <option>-fwarn-import-lists</option> flag will warn about the import
1356 of <literal>Y</literal> but not <literal>X</literal>
1357 If module <literal>Y</literal> is later changed to export (say) <literal>f</literal>,
1358 then the reference to <literal>f</literal> in <literal>M</literal> will become
1359 ambiguous. No warning is produced for the import of <literal>Z</literal>
1360 because extending <literal>Z</literal>'s exports would be unlikely to produce
1361 ambiguity in <literal>M</literal>.
1362 </para>
1363 </listitem>
1364 </varlistentry>
1365
1366 <varlistentry>
1367 <term><option>-fwarn-missing-methods</option>:</term>
1368 <listitem>
1369 <indexterm><primary><option>-fwarn-missing-methods</option></primary></indexterm>
1370 <indexterm><primary>missing methods, warning</primary></indexterm>
1371 <indexterm><primary>methods, missing</primary></indexterm>
1372
1373 <para>This option is on by default, and warns you whenever
1374 an instance declaration is missing one or more methods, and
1375 the corresponding class declaration has no default
1376 declaration for them.</para>
1377 <para>The warning is suppressed if the method name
1378 begins with an underscore. Here's an example where this is useful:
1379 <programlisting>
1380 class C a where
1381 _simpleFn :: a -> String
1382 complexFn :: a -> a -> String
1383 complexFn x y = ... _simpleFn ...
1384 </programlisting>
1385 The idea is that: (a) users of the class will only call <literal>complexFn</literal>;
1386 never <literal>_simpleFn</literal>; and (b)
1387 instance declarations can define either <literal>complexFn</literal> or <literal>_simpleFn</literal>.
1388 </para>
1389 </listitem>
1390 </varlistentry>
1391
1392 <varlistentry>
1393 <term><option>-fwarn-missing-signatures</option>:</term>
1394 <listitem>
1395 <indexterm><primary><option>-fwarn-missing-signatures</option></primary></indexterm>
1396 <indexterm><primary>type signatures, missing</primary></indexterm>
1397
1398 <para>If you would like GHC to check that every top-level
1399 function/value has a type signature, use the
1400 <option>-fwarn-missing-signatures</option> option. As part of
1401 the warning GHC also reports the inferred type. The
1402 option is off by default.</para>
1403 </listitem>
1404 </varlistentry>
1405
1406 <varlistentry>
1407 <term><option>-fwarn-missing-local-sigs</option>:</term>
1408 <listitem>
1409 <indexterm><primary><option>-fwarn-missing-local-sigs</option></primary></indexterm>
1410 <indexterm><primary>type signatures, missing</primary></indexterm>
1411
1412 <para>If you use the
1413 <option>-fwarn-missing-local-sigs</option> flag GHC will warn
1414 you about any polymorphic local bindings. As part of
1415 the warning GHC also reports the inferred type. The
1416 option is off by default.</para>
1417 </listitem>
1418 </varlistentry>
1419
1420 <varlistentry>
1421 <term><option>-fwarn-name-shadowing</option>:</term>
1422 <listitem>
1423 <indexterm><primary><option>-fwarn-name-shadowing</option></primary></indexterm>
1424 <indexterm><primary>shadowing, warning</primary></indexterm>
1425
1426 <para>This option causes a warning to be emitted whenever an
1427 inner-scope value has the same name as an outer-scope value,
1428 i.e. the inner value shadows the outer one. This can catch
1429 typographical errors that turn into hard-to-find bugs, e.g.,
1430 in the inadvertent capture of what would be a recursive call in
1431 <literal>f = ... let f = id in ... f ...</literal>.</para>
1432 <para>The warning is suppressed for names beginning with an underscore. For example
1433 <programlisting>
1434 f x = do { _ignore &lt;- this; _ignore &lt;- that; return (the other) }
1435 </programlisting>
1436 </para>
1437 </listitem>
1438 </varlistentry>
1439
1440 <varlistentry>
1441 <term><option>-fwarn-orphans, -fwarn-auto-orphans</option>:</term>
1442 <listitem>
1443 <indexterm><primary><option>-fwarn-orphans</option></primary></indexterm>
1444 <indexterm><primary><option>-fwarn-auto-orphans</option></primary></indexterm>
1445 <indexterm><primary>orphan instances, warning</primary></indexterm>
1446 <indexterm><primary>orphan rules, warning</primary></indexterm>
1447
1448 <para>These flags cause a warning to be emitted whenever the
1449 module contains an "orphan" instance declaration or rewrite rule.
1450 An instance declaration is an orphan if it appears in a module in
1451 which neither the class nor the type being instanced are declared
1452 in the same module. A rule is an orphan if it is a rule for a
1453 function declared in another module. A module containing any
1454 orphans is called an orphan module.</para>
1455 <para>The trouble with orphans is that GHC must pro-actively read the interface
1456 files for all orphan modules, just in case their instances or rules
1457 play a role, whether or not the module's interface would otherwise
1458 be of any use. See <xref linkend="orphan-modules"/> for details.
1459 </para>
1460 <para>The flag <option>-fwarn-orphans</option> warns about user-written
1461 orphan rules or instances. The flag <option>-fwarn-auto-orphans</option>
1462 warns about automatically-generated orphan rules, notably as a result of
1463 specialising functions, for type classes (<literal>Specialise</literal>)
1464 or argument values (<literal>SpecConstr</literal>).</para>
1465 </listitem>
1466 </varlistentry>
1467
1468 <varlistentry>
1469 <term>
1470 <option>-fwarn-overlapping-patterns</option>:
1471 <indexterm><primary><option>-fwarn-overlapping-patterns</option></primary></indexterm>
1472 <indexterm><primary>overlapping patterns, warning</primary></indexterm>
1473 <indexterm><primary>patterns, overlapping</primary></indexterm>
1474 </term>
1475 <listitem>
1476 <para>By default, the compiler will warn you if a set of
1477 patterns are overlapping, e.g.,</para>
1478
1479 <programlisting>
1480 f :: String -&#62; Int
1481 f [] = 0
1482 f (_:xs) = 1
1483 f "2" = 2
1484 </programlisting>
1485
1486 <para>where the last pattern match in <function>f</function>
1487 won't ever be reached, as the second pattern overlaps
1488 it. More often than not, redundant patterns is a programmer
1489 mistake/error, so this option is enabled by default.</para>
1490 </listitem>
1491 </varlistentry>
1492
1493 <varlistentry>
1494 <term><option>-fwarn-tabs</option>:</term>
1495 <listitem>
1496 <indexterm><primary><option>-fwarn-tabs</option></primary></indexterm>
1497 <indexterm><primary>tabs, warning</primary></indexterm>
1498 <para>Have the compiler warn if there are tabs in your source
1499 file.</para>
1500
1501 <para>This warning is off by default.</para>
1502 </listitem>
1503 </varlistentry>
1504
1505 <varlistentry>
1506 <term><option>-fwarn-type-defaults</option>:</term>
1507 <listitem>
1508 <indexterm><primary><option>-fwarn-type-defaults</option></primary></indexterm>
1509 <indexterm><primary>defaulting mechanism, warning</primary></indexterm>
1510 <para>Have the compiler warn/inform you where in your source
1511 the Haskell defaulting mechanism for numeric types kicks
1512 in. This is useful information when converting code from a
1513 context that assumed one default into one with another,
1514 e.g., the &lsquo;default default&rsquo; for Haskell 1.4 caused the
1515 otherwise unconstrained value <constant>1</constant> to be
1516 given the type <literal>Int</literal>, whereas Haskell 98
1517 and later
1518 defaults it to <literal>Integer</literal>. This may lead to
1519 differences in performance and behaviour, hence the
1520 usefulness of being non-silent about this.</para>
1521
1522 <para>This warning is off by default.</para>
1523 </listitem>
1524 </varlistentry>
1525
1526 <varlistentry>
1527 <term><option>-fwarn-monomorphism-restriction</option>:</term>
1528 <listitem>
1529 <indexterm><primary><option>-fwarn-monomorphism-restriction</option></primary></indexterm>
1530 <indexterm><primary>monomorphism restriction, warning</primary></indexterm>
1531 <para>Have the compiler warn/inform you where in your source
1532 the Haskell Monomorphism Restriction is applied. If applied silently
1533 the MR can give rise to unexpected behaviour, so it can be helpful
1534 to have an explicit warning that it is being applied.</para>
1535
1536 <para>This warning is off by default.</para>
1537 </listitem>
1538 </varlistentry>
1539
1540 <varlistentry>
1541 <term><option>-fwarn-unused-binds</option>:</term>
1542 <listitem>
1543 <indexterm><primary><option>-fwarn-unused-binds</option></primary></indexterm>
1544 <indexterm><primary>unused binds, warning</primary></indexterm>
1545 <indexterm><primary>binds, unused</primary></indexterm>
1546 <para>Report any function definitions (and local bindings)
1547 which are unused. For top-level functions, the warning is
1548 only given if the binding is not exported.</para>
1549 <para>A definition is regarded as "used" if (a) it is exported, or (b) it is
1550 mentioned in the right hand side of another definition that is used, or (c) the
1551 function it defines begins with an underscore. The last case provides a
1552 way to suppress unused-binding warnings selectively. </para>
1553 <para> Notice that a variable
1554 is reported as unused even if it appears in the right-hand side of another
1555 unused binding. </para>
1556 </listitem>
1557 </varlistentry>
1558
1559 <varlistentry>
1560 <term><option>-fwarn-unused-imports</option>:</term>
1561 <listitem>
1562 <indexterm><primary><option>-fwarn-unused-imports</option></primary></indexterm>
1563 <indexterm><primary>unused imports, warning</primary></indexterm>
1564 <indexterm><primary>imports, unused</primary></indexterm>
1565
1566 <para>Report any modules that are explicitly imported but
1567 never used. However, the form <literal>import M()</literal> is
1568 never reported as an unused import, because it is a useful idiom
1569 for importing instance declarations, which are anonymous in Haskell.</para>
1570 </listitem>
1571 </varlistentry>
1572
1573 <varlistentry>
1574 <term><option>-fwarn-unused-matches</option>:</term>
1575 <listitem>
1576 <indexterm><primary><option>-fwarn-unused-matches</option></primary></indexterm>
1577 <indexterm><primary>unused matches, warning</primary></indexterm>
1578 <indexterm><primary>matches, unused</primary></indexterm>
1579
1580 <para>Report all unused variables which arise from pattern
1581 matches, including patterns consisting of a single variable.
1582 For instance <literal>f x y = []</literal> would report
1583 <varname>x</varname> and <varname>y</varname> as unused. The
1584 warning is suppressed if the variable name begins with an underscore, thus:
1585 <programlisting>
1586 f _x = True
1587 </programlisting>
1588 </para>
1589 </listitem>
1590 </varlistentry>
1591
1592 <varlistentry>
1593 <term><option>-fwarn-unused-do-bind</option>:</term>
1594 <listitem>
1595 <indexterm><primary><option>-fwarn-unused-do-bind</option></primary></indexterm>
1596 <indexterm><primary>unused do binding, warning</primary></indexterm>
1597 <indexterm><primary>do binding, unused</primary></indexterm>
1598
1599 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1600 that appear to silently throw information away.
1601 For instance <literal>do { mapM popInt xs ; return 10 }</literal> would report
1602 the first statement in the <literal>do</literal> block as suspicious,
1603 as it has the type <literal>StackM [Int]</literal> and not <literal>StackM ()</literal>, but that
1604 <literal>[Int]</literal> value is not bound to anything. The warning is suppressed by
1605 explicitly mentioning in the source code that your program is throwing something away:
1606 <programlisting>
1607 do { _ &lt;- mapM popInt xs ; return 10 }
1608 </programlisting>
1609 Of course, in this particular situation you can do even better:
1610 <programlisting>
1611 do { mapM_ popInt xs ; return 10 }
1612 </programlisting>
1613 </para>
1614 </listitem>
1615 </varlistentry>
1616
1617 <varlistentry>
1618 <term><option>-fwarn-wrong-do-bind</option>:</term>
1619 <listitem>
1620 <indexterm><primary><option>-fwarn-wrong-do-bind</option></primary></indexterm>
1621 <indexterm><primary>apparently erroneous do binding, warning</primary></indexterm>
1622 <indexterm><primary>do binding, apparently erroneous</primary></indexterm>
1623
1624 <para>Report expressions occurring in <literal>do</literal> and <literal>mdo</literal> blocks
1625 that appear to lack a binding.
1626 For instance <literal>do { return (popInt 10) ; return 10 }</literal> would report
1627 the first statement in the <literal>do</literal> block as suspicious,
1628 as it has the type <literal>StackM (StackM Int)</literal> (which consists of two nested applications
1629 of the same monad constructor), but which is not then &quot;unpacked&quot; by binding the result.
1630 The warning is suppressed by explicitly mentioning in the source code that your program is throwing something away:
1631 <programlisting>
1632 do { _ &lt;- return (popInt 10) ; return 10 }
1633 </programlisting>
1634 For almost all sensible programs this will indicate a bug, and you probably intended to write:
1635 <programlisting>
1636 do { popInt 10 ; return 10 }
1637 </programlisting>
1638 </para>
1639 </listitem>
1640 </varlistentry>
1641
1642 </variablelist>
1643
1644 <para>If you're feeling really paranoid, the
1645 <option>-dcore-lint</option>
1646 option<indexterm><primary><option>-dcore-lint</option></primary></indexterm>
1647 is a good choice. It turns on heavyweight intra-pass
1648 sanity-checking within GHC. (It checks GHC's sanity, not
1649 yours.)</para>
1650
1651 </sect1>
1652
1653 &packages;
1654
1655 <sect1 id="options-optimise">
1656 <title>Optimisation (code improvement)</title>
1657
1658 <indexterm><primary>optimisation</primary></indexterm>
1659 <indexterm><primary>improvement, code</primary></indexterm>
1660
1661 <para>The <option>-O*</option> options specify convenient
1662 &ldquo;packages&rdquo; of optimisation flags; the
1663 <option>-f*</option> options described later on specify
1664 <emphasis>individual</emphasis> optimisations to be turned on/off;
1665 the <option>-m*</option> options specify
1666 <emphasis>machine-specific</emphasis> optimisations to be turned
1667 on/off.</para>
1668
1669 <sect2 id="optimise-pkgs">
1670 <title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.</title>
1671
1672 <para>There are <emphasis>many</emphasis> options that affect
1673 the quality of code produced by GHC. Most people only have a
1674 general goal, something like &ldquo;Compile quickly&rdquo; or
1675 &ldquo;Make my program run like greased lightning.&rdquo; The
1676 following &ldquo;packages&rdquo; of optimisations (or lack
1677 thereof) should suffice.</para>
1678
1679 <para>Note that higher optimisation levels cause more
1680 cross-module optimisation to be performed, which can have an
1681 impact on how much of your program needs to be recompiled when
1682 you change something. This is one reason to stick to
1683 no-optimisation when developing code.</para>
1684
1685 <variablelist>
1686
1687 <varlistentry>
1688 <term>
1689 No <option>-O*</option>-type option specified:
1690 <indexterm><primary>-O* not specified</primary></indexterm>
1691 </term>
1692 <listitem>
1693 <para>This is taken to mean: &ldquo;Please compile
1694 quickly; I'm not over-bothered about compiled-code
1695 quality.&rdquo; So, for example: <command>ghc -c
1696 Foo.hs</command></para>
1697 </listitem>
1698 </varlistentry>
1699
1700 <varlistentry>
1701 <term>
1702 <option>-O0</option>:
1703 <indexterm><primary><option>-O0</option></primary></indexterm>
1704 </term>
1705 <listitem>
1706 <para>Means &ldquo;turn off all optimisation&rdquo;,
1707 reverting to the same settings as if no
1708 <option>-O</option> options had been specified. Saying
1709 <option>-O0</option> can be useful if
1710 eg. <command>make</command> has inserted a
1711 <option>-O</option> on the command line already.</para>
1712 </listitem>
1713 </varlistentry>
1714
1715 <varlistentry>
1716 <term>
1717 <option>-O</option> or <option>-O1</option>:
1718 <indexterm><primary>-O option</primary></indexterm>
1719 <indexterm><primary>-O1 option</primary></indexterm>
1720 <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
1721 </term>
1722 <listitem>
1723 <para>Means: &ldquo;Generate good-quality code without
1724 taking too long about it.&rdquo; Thus, for example:
1725 <command>ghc -c -O Main.lhs</command></para>
1726 </listitem>
1727 </varlistentry>
1728
1729 <varlistentry>
1730 <term>
1731 <option>-O2</option>:
1732 <indexterm><primary>-O2 option</primary></indexterm>
1733 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
1734 </term>
1735 <listitem>
1736 <para>Means: &ldquo;Apply every non-dangerous
1737 optimisation, even if it means significantly longer
1738 compile times.&rdquo;</para>
1739
1740 <para>The avoided &ldquo;dangerous&rdquo; optimisations
1741 are those that can make runtime or space
1742 <emphasis>worse</emphasis> if you're unlucky. They are
1743 normally turned on or off individually.</para>
1744
1745 <para>At the moment, <option>-O2</option> is
1746 <emphasis>unlikely</emphasis> to produce better code than
1747 <option>-O</option>.</para>
1748 </listitem>
1749 </varlistentry>
1750 </variablelist>
1751
1752 <para>We don't use a <option>-O*</option> flag for day-to-day
1753 work. We use <option>-O</option> to get respectable speed;
1754 e.g., when we want to measure something. When we want to go for
1755 broke, we tend to use <option>-O2</option> (and we go for
1756 lots of coffee breaks).</para>
1757
1758 <para>The easiest way to see what <option>-O</option> (etc.)
1759 &ldquo;really mean&rdquo; is to run with <option>-v</option>,
1760 then stand back in amazement.</para>
1761 </sect2>
1762
1763 <sect2 id="options-f">
1764 <title><option>-f*</option>: platform-independent flags</title>
1765
1766 <indexterm><primary>-f* options (GHC)</primary></indexterm>
1767 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
1768
1769 <para>These flags turn on and off individual optimisations.
1770 They are normally set via the <option>-O</option> options
1771 described above, and as such, you shouldn't need to set any of
1772 them explicitly (indeed, doing so could lead to unexpected
1773 results). However, there are one or two that may be of
1774 interest:</para>
1775
1776 <variablelist>
1777 <varlistentry>
1778 <term>
1779 <option>-fno-cse</option>
1780 <indexterm><primary><option>-fno-cse</option></primary></indexterm>
1781 </term>
1782 <listitem>
1783 <para>Turns off the common-sub-expression elimination optimisation.
1784 Can be useful if you have some <literal>unsafePerformIO</literal>
1785 expressions that you don't want commoned-up.</para>
1786 </listitem>
1787 </varlistentry>
1788
1789 <varlistentry>
1790 <term>
1791 <option>-fstrictness</option>
1792 <indexterm><primary><option></option></primary></indexterm>
1793 </term>
1794 <listitem>
1795 <para>
1796 </para>
1797 TODO: Document optimisation
1798 </listitem>
1799 </varlistentry>
1800
1801 <varlistentry>
1802 <term>
1803 <option>-fno-strictness</option>
1804 <indexterm><primary><option>-fno-strictness</option></primary></indexterm>
1805 </term>
1806 <listitem>
1807 <para>Turns off the strictness analyser; sometimes it eats
1808 too many cycles.</para>
1809 TODO: Maybe document positive cases and remove all negative cases?
1810 </listitem>
1811 </varlistentry>
1812
1813 <varlistentry>
1814 <term>
1815 <option>-funbox-strict-fields</option>:
1816 <indexterm><primary><option>-funbox-strict-fields</option></primary></indexterm>
1817 <indexterm><primary>strict constructor fields</primary></indexterm>
1818 <indexterm><primary>constructor fields, strict</primary></indexterm>
1819 </term>
1820 <listitem>
1821 <para>This option causes all constructor fields which are marked
1822 strict (i.e. &ldquo;!&rdquo;) to be unpacked if possible. It is
1823 equivalent to adding an <literal>UNPACK</literal> pragma to every
1824 strict constructor field (see <xref linkend="unpack-pragma"/>).
1825 </para>
1826
1827 <para>This option is a bit of a sledgehammer: it might sometimes
1828 make things worse. Selectively unboxing fields by using
1829 <literal>UNPACK</literal> pragmas might be better. An alternative
1830 is to use <option>-funbox-strict-fields</option> to turn on
1831 unboxing by default but disable it for certain constructor
1832 fields using the <literal>NOUNPACK</literal> pragma (see
1833 <xref linkend="nounpack-pragma"/>).</para>
1834 </listitem>
1835 </varlistentry>
1836
1837 <varlistentry>
1838 <term>
1839 <option>-fspec-constr</option>
1840 <indexterm><primary><option>-fspec-constr</option></primary></indexterm>
1841 </term>
1842 <listitem>
1843 <para>Turn on call-pattern specialisation.</para>
1844 TODO: Document optimisation
1845 </listitem>
1846 </varlistentry>
1847
1848 <varlistentry>
1849 <term>
1850 <option>-fno-specialise</option>
1851 <indexterm><primary><option>-fno-specialise</option></primary></indexterm>
1852 </term>
1853 <listitem>
1854 <para>Turns off the automatic specialisation of overloaded functions.</para>
1855 TODO: Document optimisation, probably change to a positive, not a -fno-* description.
1856 </listitem>
1857 </varlistentry>
1858
1859 <varlistentry>
1860 <term>
1861 <option>-fstatic-argument-transformation</option>
1862 <indexterm><primary><option>-fstatic-argument-transformation</option></primary></indexterm>
1863 </term>
1864 <listitem>
1865 <para>Turn on the static argument transformation.</para>
1866 TODO: Document optimisation
1867 </listitem>
1868 </varlistentry>
1869
1870 <varlistentry>
1871 <term>
1872 <option>-ffloat-in</option>
1873 <indexterm><primary><option></option></primary></indexterm>
1874 </term>
1875 <listitem>
1876 <para>
1877 </para>
1878 TODO: Document optimisation
1879 </listitem>
1880 </varlistentry>
1881
1882 <varlistentry>
1883 <term>
1884 <option>-fno-float-in</option>
1885 <indexterm><primary><option>-fno-float-in</option></primary></indexterm>
1886 </term>
1887 <listitem>
1888 <para>Turns off the float-in transformation.</para>
1889 TODO: Maybe document positive cases and remove all negative cases?
1890 </listitem>
1891 </varlistentry>
1892
1893 <varlistentry>
1894 <term>
1895 <option>-fno-full-laziness</option>
1896 <indexterm><primary><option>-fno-full-laziness</option></primary></indexterm>
1897 </term>
1898 <listitem>
1899 <para>Turns off the full laziness optimisation (also known as
1900 let-floating). Full laziness increases sharing, which can lead
1901 to increased memory residency.</para>
1902
1903 TODO: Document optimisation
1904
1905 <para>NOTE: GHC doesn't implement complete full-laziness.
1906 When optimisation in on, and
1907 <option>-fno-full-laziness</option> is not given, some
1908 transformations that increase sharing are performed, such
1909 as extracting repeated computations from a loop. These
1910 are the same transformations that a fully lazy
1911 implementation would do, the difference is that GHC
1912 doesn't consistently apply full-laziness, so don't rely on
1913 it.</para>
1914 </listitem>
1915 </varlistentry>
1916
1917 <varlistentry>
1918 <term>
1919 <option>-fdo-lambda-eta-expansion</option>
1920 <indexterm><primary><option></option></primary></indexterm>
1921 </term>
1922 <listitem>
1923 <para>
1924 </para>
1925 TODO: Document optimisation
1926 </listitem>
1927 </varlistentry>
1928
1929 <varlistentry>
1930 <term>
1931 <option>-fdo-eta-reduction</option>
1932 <indexterm><primary><option></option></primary></indexterm>
1933 </term>
1934 <listitem>
1935 <para>
1936 </para>
1937 TODO: Document optimisation
1938 </listitem>
1939 </varlistentry>
1940
1941 <varlistentry>
1942 <term>
1943 <option>-fcase-merge</option>
1944 <indexterm><primary><option></option></primary></indexterm>
1945 </term>
1946 <listitem>
1947 <para>
1948 </para>
1949 TODO: Document optimisation
1950 </listitem>
1951 </varlistentry>
1952
1953 <varlistentry>
1954 <term>
1955 <option>-fliberate-case</option>
1956 <indexterm><primary><option>-fliberate-case</option></primary></indexterm>
1957 </term>
1958 <listitem>
1959 <para>Turn on the liberate-case transformation.</para>
1960 TODO: Document optimisation
1961 </listitem>
1962 </varlistentry>
1963
1964 <varlistentry>
1965 <term>
1966 <option>-fdicts-cheap</option>
1967 <indexterm><primary><option></option></primary></indexterm>
1968 </term>
1969 <listitem>
1970 <para>
1971 </para>
1972 TODO: Document optimisation
1973 </listitem>
1974 </varlistentry>
1975
1976 <varlistentry>
1977 <term>
1978 <option>-feager-blackholing</option>
1979 <indexterm><primary><option></option></primary></indexterm>
1980 </term>
1981 <listitem>
1982 <para>
1983 </para>
1984 TODO: Document optimisation
1985 </listitem>
1986 </varlistentry>
1987
1988 <varlistentry>
1989 <term>
1990 <option>-fno-state-hack</option>
1991 <indexterm><primary><option>-fno-state-hack</option></primary></indexterm>
1992 </term>
1993 <listitem>
1994 <para>Turn off the "state hack" whereby any lambda with a
1995 <literal>State#</literal> token as argument is considered to be
1996 single-entry, hence it is considered OK to inline things inside
1997 it. This can improve performance of IO and ST monad code, but it
1998 runs the risk of reducing sharing.</para>
1999 </listitem>
2000 </varlistentry>
2001
2002 <varlistentry>
2003 <term>
2004 <option>-fpedantic-bottoms</option>
2005 <indexterm><primary><option>-fpedantic-bottoms</option></primary></indexterm>
2006 </term>
2007 <listitem>
2008 <para>Make GHC be more precise about its treatment of bottom (but see also
2009 <option>-fno-state-hack</option>). In particular, stop GHC
2010 eta-expanding through a case expression, which is good for
2011 performance, but bad if you are using <literal>seq</literal> on
2012 partial applications.</para>
2013 </listitem>
2014 </varlistentry>
2015
2016 <varlistentry>
2017 <term>
2018 <option>-fsimpl-tick-factor=<replaceable>n</replaceable></option>
2019 <indexterm><primary><option>-fsimpl-tick-factor</option></primary></indexterm>
2020 </term>
2021 <listitem>
2022 <para>GHC's optimiser can diverge if you write rewrite rules (<xref linkend="rewrite-rules"/>)
2023 that don't terminate, or (less satisfactorily) if you
2024 code up recursion through data types
2025 (<xref linkend="bugs-ghc"/>). To avoid making the compiler fall into an infinite
2026 loop, the optimiser carries a "tick count" and stops inlining and applying rewrite rules
2027 when this count is exceeded. The limit is set as a multiple of the program size, so
2028 bigger programs get more ticks. The <option>-fsimpl-tick-factor</option> flag lets
2029 you change the multiplier. The default is 100; numbers larger than 100 give more ticks,
2030 and numbers smaller than 100 give fewer.</para>
2031
2032 <para>If the tick-count expires, GHC summarises what simplifier steps it has done;
2033 you can use <option>-fddump-simpl-stats</option> to generate a much more detailed list.
2034 Usually that identifies the loop quite accurately, because some numbers are very large.
2035 </para>
2036 </listitem>
2037 </varlistentry>
2038
2039 <varlistentry>
2040 <term>
2041 <option>-funfolding-creation-threshold=<replaceable>n</replaceable></option>:
2042 <indexterm><primary><option>-funfolding-creation-threshold</option></primary></indexterm>
2043 <indexterm><primary>inlining, controlling</primary></indexterm>
2044 <indexterm><primary>unfolding, controlling</primary></indexterm>
2045 </term>
2046 <listitem>
2047 <para>(Default: 45) Governs the maximum size that GHC will allow a
2048 function unfolding to be. (An unfolding has a &ldquo;size&rdquo;
2049 that reflects the cost in terms of &ldquo;code bloat&rdquo; of
2050 expanding (aka inlining) that unfolding at a call site. A bigger
2051 function would be assigned a bigger cost.) </para>
2052
2053 <para>Consequences: (a) nothing larger than this will be
2054 inlined (unless it has an INLINE pragma); (b) nothing
2055 larger than this will be spewed into an interface
2056 file. </para>
2057
2058 <para>Increasing this figure is more likely to result in longer
2059 compile times than faster code. The
2060 <option>-funfolding-use-threshold</option> is more useful.
2061 </para>
2062 </listitem>
2063 </varlistentry>
2064
2065 <varlistentry>
2066 <term>
2067 <option>-funfolding-use-threshold=<replaceable>n</replaceable></option>
2068 <indexterm><primary><option>-funfolding-use-threshold</option></primary></indexterm>
2069 <indexterm><primary>inlining, controlling</primary></indexterm>
2070 <indexterm><primary>unfolding, controlling</primary></indexterm>
2071 </term>
2072 <listitem>
2073 <para>(Default: 8) This is the magic cut-off figure for unfolding
2074 (aka inlining): below this size, a function definition will be
2075 unfolded at the call-site, any bigger and it won't. The size
2076 computed for a function depends on two things: the actual size of
2077 the expression minus any discounts that
2078 apply (see <option>-funfolding-con-discount</option>).</para>
2079
2080 <para>The difference between this and
2081 <option>-funfolding-creation-threshold</option> is that this one
2082 determines if a function definition will be inlined at a call
2083 site. The other option determines if a function definition will
2084 be kept around to begin with for potential inlining.</para>
2085
2086 TODO: Is above para correct?
2087 </listitem>
2088 </varlistentry>
2089
2090 <varlistentry>
2091 <term>
2092 <option>-fexpose-all-unfoldings</option>
2093 <indexterm><primary><option></option></primary></indexterm>
2094 </term>
2095 <listitem>
2096 <para>
2097 </para>
2098 TODO: Document optimisation
2099 </listitem>
2100 </varlistentry>
2101
2102 <varlistentry>
2103 <term>
2104 <option>-fvectorise</option>
2105 <indexterm><primary><option></option></primary></indexterm>
2106 </term>
2107 <listitem>
2108 <para>
2109 </para>
2110 TODO: Document optimisation
2111 </listitem>
2112 </varlistentry>
2113
2114 <varlistentry>
2115 <term>
2116 <option>-favoid-vect</option>
2117 <indexterm><primary><option></option></primary></indexterm>
2118 </term>
2119 <listitem>
2120 <para>
2121 </para>
2122 TODO: Document optimisation
2123 </listitem>
2124 </varlistentry>
2125
2126 <varlistentry>
2127 <term>
2128 <option>-fregs-graph</option>
2129 <indexterm><primary><option></option></primary></indexterm>
2130 </term>
2131 <listitem>
2132 <para>
2133 </para>
2134 TODO: Document optimisation
2135 </listitem>
2136 </varlistentry>
2137
2138 <varlistentry>
2139 <term>
2140 <option>-fregs-iterative</option>
2141 <indexterm><primary><option></option></primary></indexterm>
2142 </term>
2143 <listitem>
2144 <para>
2145 </para>
2146 TODO: Document optimisation
2147 </listitem>
2148 </varlistentry>
2149
2150 <varlistentry>
2151 <term>
2152 <option>-fexcess-precision</option>
2153 <indexterm><primary><option>-fexcess-precision</option></primary></indexterm>
2154 </term>
2155 <listitem>
2156 <para>When this option is given, intermediate floating
2157 point values can have a <emphasis>greater</emphasis>
2158 precision/range than the final type. Generally this is a
2159 good thing, but some programs may rely on the exact
2160 precision/range of
2161 <literal>Float</literal>/<literal>Double</literal> values
2162 and should not use this option for their compilation.</para>
2163 </listitem>
2164 </varlistentry>
2165
2166 <varlistentry>
2167 <term>
2168 <option>-fignore-asserts</option>
2169 <indexterm><primary><option>-fignore-asserts</option></primary></indexterm>
2170 </term>
2171 <listitem>
2172 <para>Causes GHC to ignore uses of the function
2173 <literal>Exception.assert</literal> in source code (in
2174 other words, rewriting <literal>Exception.assert p
2175 e</literal> to <literal>e</literal> (see <xref
2176 linkend="assertions"/>). This flag is turned on by
2177 <option>-O</option>.
2178 </para>
2179 </listitem>
2180 </varlistentry>
2181
2182 <varlistentry>
2183 <term>
2184 <option>-fignore-interface-pragmas</option>
2185 <indexterm><primary><option>-fignore-interface-pragmas</option></primary></indexterm>
2186 </term>
2187 <listitem>
2188 <para>Tells GHC to ignore all inessential information when reading interface files.
2189 That is, even if <filename>M.hi</filename> contains unfolding or strictness information
2190 for a function, GHC will ignore that information.</para>
2191 </listitem>
2192 </varlistentry>
2193
2194 <varlistentry>
2195 <term>
2196 <option>-fomit-interface-pragmas</option>
2197 <indexterm><primary><option>-fomit-interface-pragmas</option></primary></indexterm>
2198 </term>
2199 <listitem>
2200 <para>Tells GHC to omit all inessential information from the
2201 interface file generated for the module being compiled (say M).
2202 This means that a module importing M will see only the
2203 <emphasis>types</emphasis> of the functions that M exports, but
2204 not their unfoldings, strictness info, etc. Hence, for example,
2205 no function exported by M will be inlined into an importing module.
2206 The benefit is that modules that import M will need to be
2207 recompiled less often (only when M's exports change their type, not
2208 when they change their implementation).</para>
2209 </listitem>
2210 </varlistentry>
2211
2212 </variablelist>
2213
2214 </sect2>
2215
2216 </sect1>
2217
2218 &code-gens;
2219
2220 &phases;
2221
2222 &shared_libs;
2223
2224 <sect1 id="using-concurrent">
2225 <title>Using Concurrent Haskell</title>
2226 <indexterm><primary>Concurrent Haskell</primary><secondary>using</secondary></indexterm>
2227
2228 <para>GHC supports Concurrent Haskell by default, without requiring a
2229 special option or libraries compiled in a certain way. To get access to
2230 the support libraries for Concurrent Haskell, just import
2231 <ulink
2232 url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>. More information on Concurrent Haskell is provided in the documentation for that module.</para>
2233
2234 <para>
2235 Optionally, the program may be linked with
2236 the <option>-threaded</option> option (see
2237 <xref linkend="options-linker" />. This provides two benefits:
2238
2239 <itemizedlist>
2240 <listitem>
2241 <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
2242 used, which allows threads to run in
2243 parallel<indexterm><primary>parallelism</primary></indexterm>
2244 on a
2245 multiprocessor<indexterm><primary>multiprocessor</primary></indexterm><indexterm><primary>SMP</primary></indexterm>
2246 or
2247 multicore<indexterm><primary>multicore</primary></indexterm>
2248 machine. See <xref linkend="using-smp" />.</para>
2249 </listitem>
2250 <listitem>
2251 <para>If a thread makes a foreign call (and the call is
2252 not marked <literal>unsafe</literal>), then other
2253 Haskell threads in the program will continue to run
2254 while the foreign call is in progress.
2255 Additionally, <literal>foreign export</literal>ed
2256 Haskell functions may be called from multiple OS
2257 threads simultaneously. See
2258 <xref linkend="ffi-threads" />.</para>
2259 </listitem>
2260 </itemizedlist>
2261 </para>
2262
2263 <para>The following RTS option(s) affect the behaviour of Concurrent
2264 Haskell programs:<indexterm><primary>RTS options, concurrent</primary></indexterm></para>
2265
2266 <variablelist>
2267 <varlistentry>
2268 <term><option>-C<replaceable>s</replaceable></option></term>
2269 <listitem>
2270 <para><indexterm><primary><option>-C<replaceable>s</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
2271 Sets the context switch interval to <replaceable>s</replaceable>
2272 seconds. A context switch will occur at the next heap block
2273 allocation after the timer expires (a heap block allocation occurs
2274 every 4k of allocation). With <option>-C0</option> or
2275 <option>-C</option>, context switches will occur as often as
2276 possible (at every heap block allocation). By default, context
2277 switches occur every 20ms.</para>
2278 </listitem>
2279 </varlistentry>
2280 </variablelist>
2281 </sect1>
2282
2283 <sect1 id="using-smp">
2284 <title>Using SMP parallelism</title>
2285 <indexterm><primary>parallelism</primary>
2286 </indexterm>
2287 <indexterm><primary>SMP</primary>
2288 </indexterm>
2289
2290 <para>GHC supports running Haskell programs in parallel on an SMP
2291 (symmetric multiprocessor).</para>
2292
2293 <para>There's a fine distinction between
2294 <emphasis>concurrency</emphasis> and <emphasis>parallelism</emphasis>:
2295 parallelism is all about making your program run
2296 <emphasis>faster</emphasis> by making use of multiple processors
2297 simultaneously. Concurrency, on the other hand, is a means of
2298 abstraction: it is a convenient way to structure a program that must
2299 respond to multiple asynchronous events.</para>
2300
2301 <para>However, the two terms are certainly related. By making use of
2302 multiple CPUs it is possible to run concurrent threads in parallel,
2303 and this is exactly what GHC's SMP parallelism support does. But it
2304 is also possible to obtain performance improvements with parallelism
2305 on programs that do not use concurrency. This section describes how to
2306 use GHC to compile and run parallel programs, in <xref
2307 linkend="lang-parallel" /> we describe the language features that affect
2308 parallelism.</para>
2309
2310 <sect2 id="parallel-compile-options">
2311 <title>Compile-time options for SMP parallelism</title>
2312
2313 <para>In order to make use of multiple CPUs, your program must be
2314 linked with the <option>-threaded</option> option (see <xref
2315 linkend="options-linker" />). Additionally, the following
2316 compiler options affect parallelism:</para>
2317
2318 <variablelist>
2319 <varlistentry>
2320 <term><option>-feager-blackholing</option></term>
2321 <indexterm><primary><option>-feager-blackholing</option></primary></indexterm>
2322 <listitem>
2323 <para>
2324 Blackholing is the act of marking a thunk (lazy
2325 computuation) as being under evaluation. It is useful for
2326 three reasons: firstly it lets us detect certain kinds of
2327 infinite loop (the <literal>NonTermination</literal>
2328 exception), secondly it avoids certain kinds of space
2329 leak, and thirdly it avoids repeating a computation in a
2330 parallel program, because we can tell when a computation
2331 is already in progress.</para>
2332
2333 <para>
2334 The option <option>-feager-blackholing</option> causes
2335 each thunk to be blackholed as soon as evaluation begins.
2336 The default is "lazy blackholing", whereby thunks are only
2337 marked as being under evaluation when a thread is paused
2338 for some reason. Lazy blackholing is typically more
2339 efficient (by 1-2&percnt; or so), because most thunks don't
2340 need to be blackholed. However, eager blackholing can
2341 avoid more repeated computation in a parallel program, and
2342 this often turns out to be important for parallelism.
2343 </para>
2344
2345 <para>
2346 We recommend compiling any code that is intended to be run
2347 in parallel with the <option>-feager-blackholing</option>
2348 flag.
2349 </para>
2350 </listitem>
2351 </varlistentry>
2352 </variablelist>
2353 </sect2>
2354
2355 <sect2 id="parallel-options">
2356 <title>RTS options for SMP parallelism</title>
2357
2358 <para>There are two ways to run a program on multiple
2359 processors:
2360 call <literal>Control.Concurrent.setNumCapabilities</literal> from your
2361 program, or use the RTS <option>-N</option> option.</para>
2362
2363 <variablelist>
2364 <varlistentry>
2365 <term><option>-N<optional><replaceable>x</replaceable></optional></option></term>
2366 <listitem>
2367 <para><indexterm><primary><option>-N<replaceable>x</replaceable></option></primary><secondary>RTS option</secondary></indexterm>
2368 Use <replaceable>x</replaceable> simultaneous threads when
2369 running the program. Normally <replaceable>x</replaceable>
2370 should be chosen to match the number of CPU cores on the
2371 machine<footnote><para>Whether hyperthreading cores should be counted or not is an
2372 open question; please feel free to experiment and let us know what
2373 results you find.</para></footnote>. For example,
2374 on a dual-core machine we would probably use
2375 <literal>+RTS -N2 -RTS</literal>.</para>
2376
2377 <para>Omitting <replaceable>x</replaceable>,
2378 i.e. <literal>+RTS -N -RTS</literal>, lets the runtime
2379 choose the value of <replaceable>x</replaceable> itself
2380 based on how many processors are in your machine.</para>
2381
2382 <para>Be careful when using all the processors in your
2383 machine: if some of your processors are in use by other
2384 programs, this can actually harm performance rather than
2385 improve it.</para>
2386
2387 <para>Setting <option>-N</option> also has the effect of
2388 enabling the parallel garbage collector (see
2389 <xref linkend="rts-options-gc" />).</para>
2390
2391 <para>The current value of the <option>-N</option> option
2392 is available to the Haskell program
2393 via <literal>Control.Concurrent.getNumCapabilities</literal>, and
2394 it may be changed while the program is running by
2395 calling <literal>Control.Concurrent.setNumCapabilities</literal>.</para>
2396 </listitem>
2397 </varlistentry>
2398 </variablelist>
2399
2400 <para>The following options affect the way the runtime schedules
2401 threads on CPUs:</para>
2402
2403 <variablelist>
2404 <varlistentry>
2405 <term><option>-qa</option></term>
2406 <indexterm><primary><option>-qa</option></primary><secondary>RTS
2407 option</secondary></indexterm>
2408 <listitem>
2409 <para>Use the OS's affinity facilities to try to pin OS
2410 threads to CPU cores. This is an experimental feature,
2411 and may or may not be useful. Please let us know
2412 whether it helps for you!</para>
2413 </listitem>
2414 </varlistentry>
2415 <varlistentry>
2416 <term><option>-qm</option></term>
2417 <indexterm><primary><option>-qm</option></primary><secondary>RTS
2418 option</secondary></indexterm>
2419 <listitem>
2420 <para>Disable automatic migration for load balancing.
2421 Normally the runtime will automatically try to schedule
2422 threads across the available CPUs to make use of idle
2423 CPUs; this option disables that behaviour. Note that
2424 migration only applies to threads; sparks created
2425 by <literal>par</literal> are load-balanced separately
2426 by work-stealing.</para>
2427
2428 <para>
2429 This option is probably only of use for concurrent
2430 programs that explicitly schedule threads onto CPUs
2431 with <literal>Control.Concurrent.forkOn</literal>.
2432 </para>
2433 </listitem>
2434 </varlistentry>
2435 </variablelist>
2436 </sect2>
2437
2438 <sect2>
2439 <title>Hints for using SMP parallelism</title>
2440
2441 <para>Add the <literal>-s</literal> RTS option when
2442 running the program to see timing stats, which will help to tell you
2443 whether your program got faster by using more CPUs or not. If the user
2444 time is greater than
2445 the elapsed time, then the program used more than one CPU. You should
2446 also run the program without <literal>-N</literal> for
2447 comparison.</para>
2448
2449 <para>The output of <literal>+RTS -s</literal> tells you how
2450 many &ldquo;sparks&rdquo; were created and executed during the
2451 run of the program (see <xref linkend="rts-options-gc" />), which
2452 will give you an idea how well your <literal>par</literal>
2453 annotations are working.</para>
2454
2455 <para>GHC's parallelism support has improved in 6.12.1 as a
2456 result of much experimentation and tuning in the runtime
2457 system. We'd still be interested to hear how well it works
2458 for you, and we're also interested in collecting parallel
2459 programs to add to our benchmarking suite.</para>
2460 </sect2>
2461 </sect1>
2462
2463 <sect1 id="options-platform">
2464 <title>Platform-specific Flags</title>
2465
2466 <indexterm><primary>-m* options</primary></indexterm>
2467 <indexterm><primary>platform-specific options</primary></indexterm>
2468 <indexterm><primary>machine-specific options</primary></indexterm>
2469
2470 <para>Some flags only make sense for particular target
2471 platforms.</para>
2472
2473 <variablelist>
2474
2475 <varlistentry>
2476 <term><option>-msse2</option>:</term>
2477 <listitem>
2478 <para>
2479 (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
2480 instruction set to implement floating point operations when using
2481 the <link linkend="native-code-gen">native code generator</link>.
2482 This gives a substantial performance improvement for floating
2483 point, but the resulting compiled code
2484 will only run on processors that support SSE2 (Intel Pentium 4 and
2485 later, or AMD Athlon 64 and later). The
2486 <link linkend="llvm-code-gen">LLVM backend</link> will also use SSE2
2487 if your processor supports it but detects this automatically so no
2488 flag is required.
2489 </para>
2490 <para>
2491 SSE2 is unconditionally used on x86-64 platforms.
2492 </para>
2493 </listitem>
2494 </varlistentry>
2495
2496 <varlistentry>
2497 <term><option>-msse4.2</option>:</term>
2498 <listitem>
2499 <para>
2500 (x86 only, added in GHC 7.4.1) Use the SSE4.2 instruction set to
2501 implement some floating point and bit operations when using the
2502 <link linkend="native-code-gen">native code generator</link>. The
2503 resulting compiled code will only run on processors that
2504 support SSE4.2 (Intel Core i7 and later). The
2505 <link linkend="llvm-code-gen">LLVM backend</link> will also use
2506 SSE4.2 if your processor supports it but detects this automatically
2507 so no flag is required.
2508 </para>
2509 </listitem>
2510 </varlistentry>
2511
2512 </variablelist>
2513
2514 </sect1>
2515
2516 &runtime;
2517
2518 <sect1 id="ext-core">
2519 <title>Generating and compiling External Core Files</title>
2520
2521 <indexterm><primary>intermediate code generation</primary></indexterm>
2522
2523 <para>GHC can dump its optimized intermediate code (said to be in &ldquo;Core&rdquo; format)
2524 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
2525 <filename>.hcr</filename>. The Core format is described in <ulink url="../../core.pdf">
2526 <citetitle>An External Representation for the GHC Core Language</citetitle></ulink>,
2527 and sample tools
2528 for manipulating Core files (in Haskell) are available in the
2529 <ulink url="http://hackage.haskell.org/package/extcore">extcore package on Hackage</ulink>. Note that the format of <literal>.hcr</literal>
2530 files is <emphasis>different</emphasis> from the Core output format that GHC generates
2531 for debugging purposes (<xref linkend="options-debugging"/>), though the two formats appear somewhat similar.</para>
2532
2533 <para>The Core format natively supports notes which you can add to
2534 your source code using the <literal>CORE</literal> pragma (see <xref
2535 linkend="pragmas"/>).</para>
2536
2537 <variablelist>
2538
2539 <varlistentry>
2540 <term>
2541 <option>-fext-core</option>
2542 <indexterm><primary><option>-fext-core</option></primary></indexterm>
2543 </term>
2544 <listitem>
2545 <para>Generate <literal>.hcr</literal> files.</para>
2546 </listitem>
2547 </varlistentry>
2548
2549 </variablelist>
2550
2551 <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>
2552
2553 </sect1>
2554
2555 &debug;
2556 &flags;
2557
2558 </chapter>
2559
2560 <!-- Emacs stuff:
2561 ;;; Local Variables: ***
2562 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
2563 ;;; End: ***
2564 -->