1 <Chapter id="using-GHC">
6 <IndexTerm><Primary>GHC, using</Primary></IndexTerm>
7 <IndexTerm><Primary>using GHC</Primary></IndexTerm>
8 GHC is a command-line compiler: in order to compile a Haskell program,
9 GHC must be invoked on the source file(s) by typing a command to the
10 shell. The steps involved in compiling a program can be automated
11 using the <Command>make</Command> tool (this is especially useful if the program
12 consists of multiple source files which depend on each other). This
13 section describes how to use GHC from the command-line.
16 <Sect1 id="command-line-structure">
17 <Title>Overall command-line structure
21 <IndexTerm><Primary>structure, command-line</Primary></IndexTerm>
22 <IndexTerm><Primary>command-line structure</Primary></IndexTerm>
26 An invocation of GHC takes the following form:
38 Command-line arguments are either options or file names.
42 Command-line options begin with <literal>-</literal>. They may <Emphasis>not</Emphasis> be
43 grouped: <option>-vO</option> is different from <option>-v -O</option>. Options need not
44 precede filenames: e.g., <Command>ghc *.o -o foo</Command>. All options are
45 processed and then applied to all files; you cannot, for example, invoke
46 <Command>ghc -c -O1 Foo.hs -O2 Bar.hs</Command> to apply different optimisation
47 levels to the files <Filename>Foo.hs</Filename> and <Filename>Bar.hs</Filename>. For conflicting
48 options, e.g., <option>-c -S</option>, we reserve the right to do anything we
49 want. (Usually, the last one applies.)
54 <Sect1 id="file-suffixes">
55 <Title>Meaningful file suffixes
59 <IndexTerm><Primary>suffixes, file</Primary></IndexTerm>
60 <IndexTerm><Primary>file suffixes for GHC</Primary></IndexTerm>
64 File names with “meaningful” suffixes (e.g., <Filename>.lhs</Filename> or <Filename>.o</Filename>)
65 cause the “right thing” to happen to those files.
72 <Term><Filename>.lhs</Filename>:</Term>
75 <IndexTerm><Primary>lhs suffix</Primary></IndexTerm>
76 A “literate Haskell” module.
81 <Term><Filename>.hs</Filename>:</Term>
84 A not-so-literate Haskell module.
89 <Term><Filename>.hi</Filename>:</Term>
92 A Haskell interface file, probably compiler-generated.
97 <Term><Filename>.hc</Filename>:</Term>
100 Intermediate C file produced by the Haskell compiler.
105 <Term><Filename>.c</Filename>:</Term>
108 A C file not produced by the Haskell compiler.
113 <Term><Filename>.s</Filename>:</Term>
116 An assembly-language source file, usually
117 produced by the compiler.
122 <Term><Filename>.o</Filename>:</Term>
125 An object file, produced by an assembler.
133 Files with other suffixes (or without suffixes) are passed straight
139 <Sect1 id="options-help">
140 <Title>Help and verbosity options
144 <IndexTerm><Primary>help options (GHC)</Primary></IndexTerm>
145 <IndexTerm><Primary>verbose option (GHC)</Primary></IndexTerm>
149 <tgroup cols=3 align=left colsep=1 rowsep=1>
153 <entry>Description</entry>
154 <entry>Static/Dynamic</entry>
155 <entry>Reverse</entry>
160 <entry><literal>-?</literal></entry>
162 <entry>static</entry>
166 <entry><literal>-help</literal></entry>
168 <entry>static</entry>
172 <entry><literal>-v</literal></entry>
173 <entry>verbose mode (equivalent to <literal>-v3</literal>)</entry>
174 <entry>dynamic</entry>
178 <entry><literal>-v</literal><emphasis>n</emphasis></entry>
179 <entry>set verbosity level</entry>
180 <entry>dynamic</entry>
184 <entry><literal>--version</literal></entry>
185 <entry>display GHC version</entry>
186 <entry>static</entry>
190 <entry><literal>--numeric-version</literal></entry>
191 <entry>display GHC version (numeric only)</entry>
192 <entry>static</entry>
202 <term><literal>-help</literal></term>
203 <term><literal>-?</literal></term>
204 <indexterm><primary><literal>-?</literal></primary></indexterm>
205 <indexterm><primary><literal>-help</literal></primary></indexterm>
207 <para>Cause GHC to spew a long usage message to standard
208 output and then exit.</para>
213 <term><literal>-v</literal></term>
214 <indexterm><primary><literal>-v</literal></primary></indexterm>
216 <para>The <option>-v</option> option makes GHC
217 <Emphasis>verbose</Emphasis>: it reports its version number
218 and shows (on stderr) exactly how it invokes each phase of
219 the compilation system. Moreover, it passes the
220 <option>-v</option> flag to most phases; each reports its
221 version number (and possibly some other information).</para>
223 <para>Please, oh please, use the <option>-v</option> option
224 when reporting bugs! Knowing that you ran the right bits in
225 the right order is always the first thing we want to
231 <term><literal>-v</literal><emphasis>n</emphasis></term>
232 <indexterm><primary><literal>-v</literal><emphasis>n</emphasis></primary></indexterm>
234 <para>To provide more control over the compiler's verbosity,
235 the <option>-v</option> flag takes an optional numeric
236 argument. Specifying <option>-v</option> on its own is
237 equivalent to <option>-v3</option>, and the other levels
238 have the following meanings:</para>
242 <term><literal>-v0</literal></term>
244 <para>Disable all non-essential messages (this is the
250 <term><literal>-v1</literal></term>
252 <para>Minimal verbosity: print one line per
253 compilation (this is the default when
254 <option>--make</option> or
255 <option>--interactive</option> is on).</para>
260 <term><literal>-v2</literal></term>
262 <para>Print the name of each compilation phase as it
263 is executed. (equivalent to
264 <option>-dshow-passes</option>).</para>
269 <term><literal>-v3</literal></term>
271 <para>The same as <option>-v2</option>, except that in
272 addition the full command line (if appropriate) for
273 each compilation phase is also printed.</para>
278 <term><literal>-v4</literal></term>
280 <para>The same as <option>-v3</option> except that the
281 intermediate program representation after each
282 compilation phase is also printed (excluding
283 preprocessed and C/assembly files).</para>
291 <term><literal>--version</literal></term>
292 <indexterm><primary><literal>--version</literal></primary></indexterm>
294 <para>Print a one-line string including GHC's version number.</para>
299 <term><literal>--numeric-version</literal></term>
300 <indexterm><primary><literal>--numeric-version</literal></primary></indexterm>
302 <para>Print GHC's numeric version number only.</para>
308 <Sect1 id="options-order">
309 <title>Running the right phases in the right order</title>
311 <indexterm><primary>order of passes in GHC</primary></indexterm>
312 <indexterm><primary>pass ordering in GHC</primary></indexterm>
315 <tgroup cols=3 align=left colsep=1 rowsep=1>
319 <entry>Description</entry>
320 <entry>Static/Dynamic</entry>
321 <entry>Reverse</entry>
326 <entry><literal>-cpp</literal></entry>
327 <entry>Run the C pre-processor on the Haskell source</entry>
328 <entry>dynamic</entry>
332 <entry><literal>-E</literal></entry>
333 <entry>Stop after preprocessing (<literal>.hspp</literal> file)</entry>
334 <entry>static</entry>
338 <entry><literal>-C</literal></entry>
339 <entry>Stop after generating C (<literal>.hc</literal> file)</entry>
340 <entry>static</entry>
344 <entry><literal>-S</literal></entry>
345 <entry>Stop after generating assembly (<literal>.s</literal> file)</entry>
346 <entry>static</entry>
350 <entry><literal>-c</literal></entry>
351 <entry>Stop after compiling to object code (<literal>.o</literal> file)</entry>
352 <entry>static</entry>
359 <para>The basic task of the <Command>ghc</Command> driver is to
360 run each input file through the right phases (compiling, linking,
363 <para>The first phase to run is determined by the input-file
364 suffix, and the last phase is determined by a flag. If no
365 relevant flag is present, then go all the way through linking.
366 This table summarises:</para>
370 <ColSpec Align="Left">
371 <ColSpec Align="Left">
372 <ColSpec Align="Left">
373 <ColSpec Align="Left">
377 <Entry>Phase of the compilation system</Entry>
378 <Entry>Suffix saying “start here”</Entry>
379 <Entry>Flag saying “stop after”</Entry>
380 <Entry>(suffix of) output file</Entry>
385 literate pre-processor </Entry>
386 <Entry> .lhs </Entry>
392 C pre-processor (opt.) </Entry>
393 <Entry> .hs (with <literal>-cpp</literal>) </Entry>
395 <Entry> .hspp </Entry>
399 Haskell compiler </Entry>
401 <Entry> -C, -S </Entry>
402 <Entry> .hc, .s </Entry>
406 C compiler (opt.) </Entry>
407 <Entry> .hc or .c </Entry>
421 <Entry> other </Entry>
423 <Entry> a.out </Entry>
429 <IndexTerm><Primary>-C option</Primary></IndexTerm>
430 <IndexTerm><Primary>-S option</Primary></IndexTerm>
431 <IndexTerm><Primary>-c option</Primary></IndexTerm>
434 Thus, a common invocation would be: <Command>ghc -c Foo.hs</Command>
438 Note: What the Haskell compiler proper produces depends on whether a
439 native-code generator is used (producing assembly language) or not
443 <para>NOTE: the option <option>-E</option><IndexTerm><Primary>-E
444 option</Primary></IndexTerm> runs just the pre-processing passes
445 of the compiler, dumping the result in a file. Note that this
446 differs from all GHCs prior to version 4.11, in which the result
447 was dumped to the standard output. If used in conjunction with
448 -cpp, the output is the code blocks of the original (literal)
449 source after having put it through the grinder that is the C
450 pre-processor. Sans <option>-cpp</option>, the output is the
451 de-litted version of the original source.</para>
455 <sect1 id="options-output">
456 <title>Re-directing the compilation output(s)</title>
458 <indexterm><primary>output-directing options</primary></indexterm>
459 <indexterm><primary>redirecting compilation output</primary></indexterm>
462 <tgroup cols=3 align=left colsep=1 rowsep=1>
466 <entry>Description</entry>
467 <entry>Static/Dynamic</entry>
468 <entry>Reverse</entry>
473 <entry><literal>-hisuf</literal></entry>
474 <entry>set the suffix to use for interface files</entry>
475 <entry>static</entry>
479 <entry><literal>-o</literal></entry>
480 <entry>set output filename</entry>
481 <entry>static</entry>
485 <entry><literal>-odir</literal></entry>
486 <entry>set output directory</entry>
487 <entry>static</entry>
491 <entry><literal>-ohi</literal></entry>
492 <entry>set the filename in which to put the interface</entry>
493 <entry>static</entry>
497 <entry><literal>-osuf</literal></entry>
498 <entry>set the output file suffix</entry>
499 <entry>static</entry>
508 <term><literal>-o</literal></term>
509 <indexterm><primary><literal>-o</literal></primary></indexterm>
511 <para>GHC's compiled output normally goes into a
512 <Filename>.hc</Filename>, <Filename>.o</Filename>, etc.,
513 file, depending on the last-run compilation phase. The
514 option <option>-o foo</option><IndexTerm><Primary>-o
515 option</Primary></IndexTerm> re-directs the output of that
516 last-run phase to file <Filename>foo</Filename>.</para>
518 <para>Note: this “feature” can be
519 counterintuitive: <Command>ghc -C -o foo.o foo.hs</Command>
520 will put the intermediate C code in the file
521 <Filename>foo.o</Filename>, name notwithstanding!</para>
526 <term><literal>-odir</literal></term>
527 <indexterm><primary><literal>-odir</literal></primary></indexterm>
529 <para>The <option>-o</option> option isn't of much use if
530 you have <Emphasis>several</Emphasis> input files…
531 Non-interface output files are normally put in the same
532 directory as their corresponding input file came from. You
533 may specify that they be put in another directory using the
534 <option>-odir <dir></option><IndexTerm><Primary>-odir
535 <dir> option</Primary></IndexTerm> (the “Oh,
536 dear” option). For example:</para>
539 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
542 <para>The output files, <Filename>Foo.o</Filename>,
543 <Filename>Bar.o</Filename>, and
544 <Filename>Bumble.o</Filename> would be put into a
545 subdirectory named after the architecture of the executing
546 machine (<Filename>sun4</Filename>,
547 <Filename>mips</Filename>, etc). The directory must already
548 exist; it won't be created.</para>
550 <para>Note that the <option>-odir</option> option does
551 <Emphasis>not</Emphasis> affect where the interface files
552 are put. In the above example, they would still be put in
553 <Filename>parse/Foo.hi</Filename>,
554 <Filename>parse/Bar.hi</Filename>, and
555 <Filename>gurgle/Bumble.hi</Filename>.</para>
560 <term><literal>-ohi</literal></term>
561 <indexterm><primary><literal>-ohi</literal></primary></indexterm>
568 <term><literal>-osuf</literal></term>
569 <term><literal>-hisuf</literal></term>
570 <indexterm><primary><literal>-osuf</literal></primary></indexterm>
571 <indexterm><primary><literal>-hisuf</literal></primary></indexterm>
573 <para>EXOTICA: The <option>-osuf
574 <suffix></option><IndexTerm><Primary>-osuf
575 <suffix> option</Primary></IndexTerm> will change the
576 <Filename>.o</Filename> file suffix for object files to
577 whatever you specify. (We use this in compiling the
580 <para>Similarly, the <option>-hisuf
581 <suffix></option><IndexTerm><Primary>-hisuf
582 <suffix> option</Primary></IndexTerm> will change the
583 <Filename>.hi</Filename> file suffix for non-system
584 interface files (see <XRef LinkEnd="hi-options">).</para>
586 <para>The <option>-hisuf</option>/<option>-osuf</option>
587 game is useful if you want to compile a program with both
588 GHC and HBC (say) in the same directory. Let HBC use the
589 standard <Filename>.hi</Filename>/<Filename>.o</Filename>
590 suffixes; add <option>-hisuf g_hi -osuf
591 g_o</option> to your <Command>make</Command> rule for
592 GHC compiling…</para>
597 <sect2 id="keeping-intermediates">
598 <title>Keeping Intermediate Files</title>
599 <indexterm><primary>intermediate files, saving</primary>
601 <indexterm><primary><literal>.hc</literal> files, saving</primary>
603 <indexterm><primary><literal>.s</literal> files, saving</primary>
607 <tgroup cols=3 align=left colsep=1 rowsep=1>
611 <entry>Description</entry>
612 <entry>Static/Dynamic</entry>
613 <entry>Reverse</entry>
618 <entry><literal>-keep-hc-file</literal></entry>
619 <entry>retain intermediate <literal>.hc</literal> files</entry>
620 <entry>static</entry>
624 <entry><literal>-keep-s-file</literal></entry>
625 <entry>retain intermediate <literal>.s</literal> files</entry>
626 <entry>static</entry>
630 <entry><literal>-keep-raw-s-file</literal></entry>
631 <entry>retain intermediate <literal>.raw_s</literal> files</entry>
632 <entry>static</entry>
636 <entry><literal>-keep-tmp-files</literal></entry>
637 <entry>retain all intermediate temporary files</entry>
638 <entry>static</entry>
645 <para>The following options are useful for keeping certain
646 intermediate files around, when normally GHC would throw these
647 away after compilation:</para>
651 <term><literal>-keep-hc-files</literal></term>
653 <primary><literal>-keep-hc-files</literal></primary>
656 <para>Keep intermediate <literal>.hc</literal> files when
657 doing <literal>.hs</literal>-to-<literal>.o</literal>
658 compilations via C (NOTE: <literal>.hc</literal> files
659 aren't generated when using the native code generator, you
660 may need to use <literal>-fvia-C</literal> to force them
661 to be produced).</para>
666 <term><literal>-keep-s-files</literal></term>
668 <primary><literal>-keep-s-files</literal></primary>
671 <para>Keep intermediate <literal>.s</literal> files.</para>
676 <term><literal>-keep-raw-s-files</literal></term>
678 <primary><literal>-keep-raw-s-files</literal></primary>
681 <para>Keep intermediate <literal>.raw-s</literal> files.
682 These are the direct output from the C compiler, before
683 GHC does “assembly mangling” to produce the
684 <literal>.s</literal> file. Again, these are not produced
685 when using the native code generator.</para>
690 <term><literal>-keep-tmp-files</literal></term>
692 <primary><literal>-keep-tmp-files</literal></primary>
695 <primary>temporary files</primary>
696 <secondary>keeping</secondary>
699 <para>Instructs the GHC driver not to delete any of its
700 temporary files, which it normally keeps in
701 <literal>/tmp</literal> (or possibly elsewhere; see <xref
702 linkend="temp-files">). Running GHC with
703 <literal>-v</literal> will show you what temporary files
704 were generated along the way.</para>
710 <sect2 id="temp-files">
711 <title>Redirecting temporary files</title>
714 <primary>temporary files</primary>
715 <secondary>redirecting</secondary>
719 <tgroup cols=3 align=left colsep=1 rowsep=1>
723 <entry>Description</entry>
724 <entry>Static/Dynamic</entry>
725 <entry>Reverse</entry>
730 <entry><literal>-tmpdir</literal></entry>
731 <entry>set the directory for temporary files</entry>
732 <entry>static</entry>
741 <term><literal>-tmpdir</literal></term>
742 <indexterm><primary><literal>-tmpdir</literal></primary></indexterm>
744 <para>If you have trouble because of running out of space
745 in <Filename>/tmp</Filename> (or wherever your
746 installation thinks temporary files should go), you may
747 use the <option>-tmpdir
748 <dir></option><IndexTerm><Primary>-tmpdir
749 <dir> option</Primary></IndexTerm> option to specify
750 an alternate directory. For example, <option>-tmpdir
751 .</option> says to put temporary files in the current
752 working directory.</para>
754 <para>Alternatively, use your <Constant>TMPDIR</Constant>
755 environment variable.<IndexTerm><Primary>TMPDIR
756 environment variable</Primary></IndexTerm> Set it to the
757 name of the directory where temporary files should be put.
758 GCC and other programs will honour the
759 <Constant>TMPDIR</Constant> variable as well.</para>
761 <para>Even better idea: Set the
762 <Constant>DEFAULT_TMPDIR</Constant> make variable when
763 building GHC, and never worry about
764 <Constant>TMPDIR</Constant> again. (see the build
765 documentation).</para>
773 <sect1 id="options-sanity">
774 <title>Warnings and sanity-checking</title>
776 <indexterm><primary>sanity-checking options</primary></indexterm>
777 <indexterm><primary>warnings</primary></indexterm>
780 <tgroup cols=3 align=left colsep=1 rowsep=1>
784 <entry>Description</entry>
785 <entry>Static/Dynamic</entry>
786 <entry>Reverse</entry>
791 <entry><literal>-W</literal></entry>
792 <entry>enable normal warnings</entry>
793 <entry>static</entry>
794 <entry><literal>-w</literal></entry>
797 <entry><literal>-w</literal></entry>
798 <entry>disable all warnings</entry>
799 <entry>static</entry>
803 <entry><literal>-Wall</literal></entry>
804 <entry>enable all warnings</entry>
805 <entry>static</entry>
806 <entry><literal>-w</literal></entry>
810 <entry><literal>-fwarn-deprecations</literal></entry>
811 <entry>warn about uses of functions & types that are deprecated</entry>
812 <entry>dynamic</entry>
813 <entry><literal>-fno-warn-deprecations</literal></entry>
817 <entry><literal>-fwarn-duplicate-exports</literal></entry>
818 <entry>warn when an entity is exported multiple times</entry>
819 <entry>dynamic</entry>
820 <entry><literal>-fno-warn-duplicate-exports</literal></entry>
824 <entry><literal>-fwarn-hi-shadowing</literal></entry>
825 <entry>warn when a <literal>.hi</literal> file in the
826 current directory shadows a library</entry>
827 <entry>dynamic</entry>
828 <entry><literal>-fno-warn-hi-shadowing</literal></entry>
832 <entry><literal>-fwarn-incomplete-patterns</literal></entry>
833 <entry>warn when a pattern match could fail</entry>
834 <entry>dynamic</entry>
835 <entry><literal>-fno-warn-incomplete-patterns</literal></entry>
839 <entry><literal>-fwarn-missing-fields</literal></entry>
840 <entry>warn when fields of a record are uninitialised</entry>
841 <entry>dynamic</entry>
842 <entry><literal>-fno-warn-missing-fields</literal></entry>
846 <entry><literal>-fwarn-missing-methods</literal></entry>
847 <entry>warn when class methods are undefined</entry>
848 <entry>dynamic</entry>
849 <entry><literal>-fno-warn-missing-methods</literal></entry>
853 <entry><literal>-fwarn-missing-signatures</literal></entry>
854 <entry>warn about top-level functions without signatures</entry>
855 <entry>dynamic</entry>
856 <entry><literal>-fno-warn-missing-signatures</literal></entry>
860 <entry><literal>-fwarn-name-shadowing</literal></entry>
861 <entry>warn when names are shadowed</entry>
862 <entry>dynamic</entry>
863 <entry><literal>-fno-warn-name-shadowing</literal></entry>
867 <entry><literal>-fwarn-overlapping-patterns</literal></entry>
868 <entry>warn about overlapping patterns</entry>
869 <entry>dynamic</entry>
870 <entry><literal>-fno-warn-overlapping-patterns</literal></entry>
874 <entry><literal>-fwarn-simple-patterns</literal></entry>
875 <entry>warn about lambda-patterns that can fail</entry>
876 <entry>dynamic</entry>
877 <entry><literal>-fno-warn-simple-patterns</literal></entry>
881 <entry><literal>-fwarn-type-defaults</literal></entry>
882 <entry>warn when defaulting happens</entry>
883 <entry>dynamic</entry>
884 <entry><literal>-fno-warn-type-defaults</literal></entry>
888 <entry><literal>-fwarn-unused-binds</literal></entry>
889 <entry>warn about bindings that are unused</entry>
890 <entry>dynamic</entry>
891 <entry><literal>-fno-warn-unused-binds</literal></entry>
895 <entry><literal>-fwarn-unused-imports</literal></entry>
896 <entry>warn about unnecessary imports</entry>
897 <entry>dynamic</entry>
898 <entry><literal>-fno-warn-unused-imports</literal></entry>
902 <entry><literal>-fwarn-unused-matches</literal></entry>
903 <entry>warn about variables in patterns that aren't used</entry>
904 <entry>dynamic</entry>
905 <entry><literal>-fno-warn-unused-matches</literal></entry>
912 <para>GHC has a number of options that select which types of
913 non-fatal error messages, otherwise known as warnings, can be
914 generated during compilation. By default, you get a standard set
915 of warnings which are generally likely to indicate bugs in your
917 <option>-fwarn-overlpapping-patterns</option>,
918 <option>-fwarn-deprecations</option>,
919 <option>-fwarn-duplicate-exports</option>,
920 <option>-fwarn-missing-fields</option>, and
921 <option>-fwarn-missing-methods</option>. The following flags are
922 simple ways to select standard “packages” of warnings:
928 <term><option>-W</option>:</term>
930 <IndexTerm><Primary>-W option</Primary></IndexTerm>
931 <para>Provides the standard warnings plus
932 <option>-fwarn-incomplete-patterns</option>,
933 <option>-fwarn-unused-matches</option>,
934 <option>-fwarn-unused-imports</option> and
935 <option>-fwarn-unused-binds</option>.</para>
940 <term><option>-w</option>:</term>
942 <IndexTerm><Primary>-w option</Primary></IndexTerm>
943 <para>Turns off all warnings, including the standard ones.</para>
948 <term><option>-Wall</option>:</term>
950 <indexterm><primary>-Wall option</primary></indexterm>
951 <para>Turns on all warning options.</para>
957 <para>The full set of warning options is described below. To turn
958 off any warning, simply give the corresponding
959 <option>-fno-warn-...</option> option on the command line.</para>
964 <term><option>-fwarn-deprecations</option>:</term>
966 <indexterm><primary><option>-fwarn-deprecations</option></primary>
968 <indexterm><primary>deprecations</primary></indexterm>
969 <para>Causes a warning to be emitted when a deprecated
970 function or type is used. Entities can be marked as
971 deprecated using a pragma, see <xref
972 linkend="deprecated-pragma">.</para>
977 <term><option>-fwarn-duplicate-exports</option>:</term>
979 <indexterm><primary>-fwarn-duplicate-exports option</primary></indexterm>
980 <indexterm><primary>duplicate exports, warning</primary></indexterm>
981 <indexterm><primary>export lists, duplicates</primary></indexterm>
983 <para>Have the compiler warn about duplicate entries in
984 export lists. This is useful information if you maintain
985 large export lists, and want to avoid the continued export
986 of a definition after you've deleted (one) mention of it in
987 the export list.</para>
989 <para>This option is on by default.</para>
994 <term><option>-fwarn-hi-shadowing</option>:</term>
996 <indexterm><primary>-fwarn-hi-shadowing option</primary></indexterm>
997 <indexterm><primary>shadowing</primary>
998 <secondary>interface files</secondary></indexterm>
1000 <para>Causes the compiler to emit a warning when a module or
1001 interface file in the current directory is shadowing one
1002 with the same module name in a library or other
1008 <term><option>-fwarn-incomplete-patterns</option>:</term>
1010 <indexterm><primary>-fwarn-incomplete-patterns option</primary></indexterm>
1011 <indexterm><primary>incomplete patterns, warning</primary></indexterm>
1012 <indexterm><primary>patterns, incomplete</primary></indexterm>
1014 <para>Similarly for incomplete patterns, the function
1015 <function>g</function> below will fail when applied to
1016 non-empty lists, so the compiler will emit a warning about
1017 this when <option>-fwarn-incomplete-patterns</option> is
1024 <para>This option isn't enabled be default because it can be
1025 a bit noisy, and it doesn't always indicate a bug in the
1026 program. However, it's generally considered good practice
1027 to cover all the cases in your functions.</para>
1032 <term><option>-fwarn-missing-fields</option>:</term>
1034 <indexterm><primary>-fwarn-missing-fields option</primary></indexterm>
1035 <indexterm><primary>missing fields, warning</primary></indexterm>
1036 <indexterm><primary>fields, missing</primary></indexterm>
1038 <para>This option is on by default, and warns you whenever
1039 the construction of a labelled field constructor isn't
1040 complete, missing initializers for one or more fields. While
1041 not an error (the missing fields are initialised with
1042 bottoms), it is often an indication of a programmer error.</para>
1047 <term><option>-fwarn-missing-methods</option>:</term>
1049 <indexterm><primary>-fwarn-missing-methods option</primary></indexterm>
1050 <indexterm><primary>missing methods, warning</primary></indexterm>
1051 <indexterm><primary>methods, missing</primary></indexterm>
1053 <para>This option is on by default, and warns you whenever
1054 an instance declaration is missing one or more methods, and
1055 the corresponding class declaration has no default
1056 declaration for them.</para>
1061 <term><option>-fwarn-missing-signatures</option>:</term>
1063 <indexterm><primary>-fwarn-missing-signatures option</primary></indexterm>
1064 <indexterm><primary>type signatures, missing</primary></indexterm>
1066 <para>If you would like GHC to check that every top-level
1067 function/value has a type signature, use the
1068 <option>-fwarn-missing-signatures</option> option. This
1069 option is off by default.</para>
1074 <term><option>-fwarn-name-shadowing</option>:</term>
1076 <indexterm><primary>-fwarn-name-shadowing option</primary></indexterm>
1077 <indexterm><primary>shadowing, warning</primary></indexterm>
1079 <para>This option causes a warning to be emitted whenever an
1080 inner-scope value has the same name as an outer-scope value,
1081 i.e. the inner value shadows the outer one. This can catch
1082 typographical errors that turn into hard-to-find bugs, e.g.,
1083 in the inadvertent cyclic definition <literal>let x = ... x
1084 ... in</literal>.</para>
1086 <para>Consequently, this option does
1087 <emphasis>will</emphasis> complain about cyclic recursive
1093 <term><option>-fwarn-overlapping-patterns</option>:</term>
1094 <indexterm><primary>-fwarn-overlapping-patterns option</primary></indexterm>
1095 <indexterm><primary>overlapping patterns, warning</primary></indexterm>
1096 <indexterm><primary>patterns, overlapping</primary></indexterm>
1098 <para>By default, the compiler will warn you if a set of
1099 patterns are overlapping, i.e.,</para>
1102 f :: String -> Int
1108 <para>where the last pattern match in <Function>f</Function>
1109 won't ever be reached, as the second pattern overlaps
1110 it. More often than not, redundant patterns is a programmer
1111 mistake/error, so this option is enabled by default.</para>
1116 <term><option>-fwarn-simple-patterns</option>:</term>
1118 <indexterm><primary><option>-fwarn-simple-patterns</option></primary>
1120 <para>Causes the compiler to warn about lambda-bound
1121 patterns that can fail, eg. <literal>\(x:xs)->...</literal>.
1122 Normally, these aren't treated as incomplete patterns by
1123 <option>-fwarn-incomplete-patterns</option>.</para>
1128 <term><option>-fwarn-type-defaults</option>:</term>
1130 <indexterm><primary>-fwarn-type-defaults option</primary></indexterm>
1131 <indexterm><primary>defaulting mechanism, warning</primary></indexterm>
1132 <para>Have the compiler warn/inform you where in your source
1133 the Haskell defaulting mechanism for numeric types kicks
1134 in. This is useful information when converting code from a
1135 context that assumed one default into one with another,
1136 e.g., the `default default' for Haskell 1.4 caused the
1137 otherwise unconstrained value <Constant>1</Constant> to be
1138 given the type <literal>Int</literal>, whereas Haskell 98
1139 defaults it to <literal>Integer</literal>. This may lead to
1140 differences in performance and behaviour, hence the
1141 usefulness of being non-silent about this.</para>
1143 <para>This warning is off by default.</para>
1148 <term><option>-fwarn-unused-binds</option>:</term>
1150 <indexterm><primary>-fwarn-unused-binds option</primary></indexterm>
1151 <indexterm><primary>unused binds, warning</primary></indexterm>
1152 <indexterm><primary>binds, unused</primary></indexterm>
1153 <para>Report any function definitions (and local bindings)
1154 which are unused. For top-level functions, the warning is
1155 only given if the binding is not exported.</para>
1160 <term><option>-fwarn-unused-imports</option>:</term>
1162 <indexterm><primary>-fwarn-unused-imports option</primary></indexterm>
1163 <indexterm><primary>unused imports, warning</primary></indexterm>
1164 <indexterm><primary>imports, unused</primary></indexterm>
1166 <para>Report any objects that are explicitly imported but
1172 <term><option>-fwarn-unused-matches</option>:</term>
1174 <indexterm><primary>-fwarn-unused-matches option</primary></indexterm>
1175 <indexterm><primary>unused matches, warning</primary></indexterm>
1176 <indexterm><primary>matches, unused</primary></indexterm>
1178 <para>Report all unused variables which arise from pattern
1179 matches, including patterns consisting of a single variable.
1180 For instance <literal>f x y = []</literal> would report
1181 <VarName>x</VarName> and <VarName>y</VarName> as unused. To
1182 eliminate the warning, all unused variables can be replaced
1183 with wildcards.</para>
1189 <para>If you're feeling really paranoid, the
1190 <option>-dcore-lint</option> option<indexterm><primary>-dcore-lint
1191 option</primary></indexterm> is a good choice. It turns on
1192 heavyweight intra-pass sanity-checking within GHC. (It checks
1193 GHC's sanity, not yours.)</para>
1197 <Sect1 id="separate-compilation">
1198 <Title>Separate compilation
1202 <indexterm><primary>separate compilation</primary></indexterm>
1203 <indexterm><primary>recompilation checker</primary></indexterm>
1204 <indexterm><primary>make and recompilation</primary></indexterm>
1205 This section describes how GHC supports separate compilation.
1208 <Sect2 id="hi-files">
1209 <Title>Interface files
1213 <indexterm><primary>interface files</primary></indexterm>
1214 <indexterm><primary>.hi files</primary></indexterm>
1218 When GHC compiles a source file <Filename>F</Filename> which contains a module <literal>A</literal>, say,
1219 it generates an object <Filename>F.o</Filename>, <Emphasis>and</Emphasis> a companion <Emphasis>interface
1220 file</Emphasis> <Filename>A.hi</Filename>. The interface file is not intended for human
1221 consumption, as you'll see if you take a look at one. It's merely
1222 there to help the compiler compile other modules in the same program.
1226 NOTE: Having the name of the interface file follow the module name and
1227 not the file name, means that working with tools such as <Command>make</Command>
1228 become harder. <Command>make</Command> implicitly assumes that any output files
1229 produced by processing a translation unit will have file names that
1230 can be derived from the file name of the translation unit. For
1231 instance, pattern rules becomes unusable. For this reason, we
1232 recommend you stick to using the same file name as the module name.
1236 The interface file for <literal>A</literal> contains information needed by the compiler
1237 when it compiles any module <literal>B</literal> that imports <literal>A</literal>, whether directly or
1238 indirectly. When compiling <literal>B</literal>, GHC will read <Filename>A.hi</Filename> to find the
1239 details that it needs to know about things defined in <literal>A</literal>.
1243 Furthermore, when compiling module <literal>C</literal> which imports <literal>B</literal>, GHC may
1244 decide that it needs to know something about <literal>A</literal>—for example, <literal>B</literal>
1245 might export a function that involves a type defined in <literal>A</literal>. In this
1246 case, GHC will go and read <Command>A.hi</Command> even though <literal>C</literal> does not explicitly
1247 import <literal>A</literal> at all.
1251 The interface file may contain all sorts of things that aren't
1252 explicitly exported from <literal>A</literal> by the programmer. For example, even
1253 though a data type is exported abstractly, <Filename>A.hi</Filename> will contain the
1254 full data type definition. For small function definitions, <Filename>A.hi</Filename>
1255 will contain the complete definition of the function. For bigger
1256 functions, <Filename>A.hi</Filename> will contain strictness information about the
1257 function. And so on. GHC puts much more information into <Filename>.hi</Filename> files
1258 when optimisation is turned on with the <option>-O</option> flag. Without <option>-O</option> it
1259 puts in just the minimum; with <option>-O</option> it lobs in a whole pile of stuff.
1260 <indexterm><primary>optimsation, effect on .hi files</primary></indexterm>
1264 <Filename>A.hi</Filename> should really be thought of as a compiler-readable version of
1265 <Filename>A.o</Filename>. If you use a <Filename>.hi</Filename> file that wasn't generated by the same
1266 compilation run that generates the <Filename>.o</Filename> file the compiler may assume
1267 all sorts of incorrect things about <literal>A</literal>, resulting in core dumps and
1268 other unpleasant happenings.
1273 <Sect2 id="options-finding-imports">
1274 <Title>Finding interface files
1278 <indexterm><primary>interface files, finding them</primary></indexterm>
1279 <indexterm><primary>finding interface files</primary></indexterm>
1283 In your program, you import a module <literal>Foo</literal> by saying
1284 <literal>import Foo</literal>. GHC goes looking for an interface file, <Filename>Foo.hi</Filename>.
1285 It has a builtin list of directories (notably including <Filename>.</Filename>) where
1293 <term><option>-i<dirs></option></term>
1296 <indexterm><primary>-i<dirs> option</primary></indexterm>This flag
1297 prepends a colon-separated list of <Filename>dirs</Filename> to the “import
1298 directories” list.
1299 See also <XRef LinkEnd="recomp"> for the significance of using
1300 relative and absolute pathnames in the <option>-i</option> list.
1306 <term><option>-i</option></term>
1309 resets the “import directories” list back to nothing.
1315 <term><option>-fno-implicit-prelude</option></term>
1318 <indexterm><primary>-fno-implicit-prelude option</primary></indexterm>
1319 GHC normally imports <Filename>Prelude.hi</Filename> files for you. If you'd rather it
1320 didn't, then give it a <option>-fno-implicit-prelude</option> option.
1321 The idea is that you can then import a Prelude of your own. (But don't call it <literal>Prelude</literal>;
1322 the Haskell module namespace is flat, and you must not conflict with any Prelude module.)
1325 Even though you have not imported the Prelude, all the built-in syntax still refers to
1326 the built-in Haskell Prelude types and values, as specified by the Haskell Report.
1327 For example, the type <literal>[Int]</literal>
1328 still means <literal>Prelude.[] Int</literal>; tuples continue to refer to the standard Prelude
1329 tuples; the translation for list comprehensions continues to use <literal>Prelude.map</literal> etc.
1331 <para> With one group of exceptions! You may want to define your own numeric class hierarchy.
1332 It completely defeats that purpose if the literal "1" means "<literal>Prelude.fromInteger 1</literal>",
1333 which is what the Haskell Report specifies. So the <option>-fno-implicit-prelude</option> flag causes
1334 the following pieces of built-in syntax to refer to whatever is in scope, not the Prelude versions:
1338 Integer and fractional literals mean "<literal>fromInteger 1</literal>" and "<literal>fromRational 3.2</literal>",
1339 not the Prelude-qualified versions; both in expressions and in patterns.
1344 Negation (e.g. "<literal>- (f x)</literal>") means "<literal>negate (f x)</literal>" (not <literal>Prelude.negate</literal>).
1349 In an n+k pattern, the standard Prelude <literal>Ord</literal> class is used for comparison, but the
1350 necessary subtraction uses
1351 whatever "<literal>(-)</literal>" is in scope (not "<literal>Prelude.(-)</literal>").
1360 <term><option>-I<dir></option></term>
1363 <indexterm><primary>-I<dir> option</primary></indexterm>
1364 Once a Haskell module has been compiled to C (<Filename>.hc</Filename> file), you may
1365 wish to specify where GHC tells the C compiler to look for <Filename>.h</Filename> files.
1366 (Or, if you are using the <option>-cpp</option> option<indexterm><primary>-cpp option</primary></indexterm>, where
1367 it tells the C pre-processor to look…) For this purpose, use a <option>-I</option>
1368 option in the usual C-ish way.
1378 <Sect2 id="hi-options">
1379 <Title>Other options related to interface files
1383 <indexterm><primary>interface files, options</primary></indexterm>
1384 The interface output may be directed to another file
1385 <Filename>bar2/Wurble.iface</Filename> with the option <option>-ohi bar2/Wurble.iface</option><indexterm><primary>-ohi
1386 <file> option</primary></indexterm> (not recommended).
1390 To avoid generating an interface file at all, use a <option>-nohi</option>
1391 option.<indexterm><primary>-nohi option</primary></indexterm>
1395 The compiler does not overwrite an existing <Filename>.hi</Filename> interface file if
1396 the new one is byte-for-byte the same as the old one; this is friendly
1397 to <Command>make</Command>. When an interface does change, it is often enlightening to
1398 be informed. The <option>-hi-diffs</option><indexterm><primary>-hi-diffs option</primary></indexterm> option will
1399 make GHC run <Command>diff</Command> on the old and new <Filename>.hi</Filename> files. You can also
1400 record the difference in the interface file itself, the
1401 <option>-keep-hi-diffs</option><indexterm><primary>-keep-hi-diffs</primary></indexterm> option takes care of that.
1405 The <Filename>.hi</Filename> files from GHC contain “usage” information which changes
1406 often and uninterestingly. If you really want to see these changes
1407 reported, you need to use the
1408 <option>-hi-diffs-with-usages</option><indexterm><primary>-hi-diffs-with-usages option</primary></indexterm>
1413 Interface files are normally jammed full of compiler-produced
1414 <Emphasis>pragmas</Emphasis>, which record arities, strictness info, etc. If you
1415 think these pragmas are messing you up (or you are doing some kind of
1416 weird experiment), you can tell GHC to ignore them with the
1417 <option>-fignore-interface-pragmas</option><indexterm><primary>-fignore-interface-pragmas
1418 option</primary></indexterm> option.
1422 When compiling without optimisations on, the compiler is extra-careful
1423 about not slurping in data constructors and instance declarations that
1424 it will not need. If you believe it is getting it wrong and not
1425 importing stuff which you think it should, this optimisation can be
1426 turned off with <option>-fno-prune-tydecls</option> and <option>-fno-prune-instdecls</option>.
1427 <indexterm><primary>-fno-prune-tydecls option</primary></indexterm><indexterm><primary>-fno-prune-instdecls
1428 option</primary></indexterm>
1432 See also <XRef LinkEnd="options-linker">, which describes how the linker finds standard
1439 <Title>The recompilation checker
1442 <indexterm><primary>recompilation checker</primary></indexterm>
1447 <term><option>-recomp</option></term>
1448 <indexterm><primary><option>-recomp</option> option</primary></indexterm>
1451 (On by default) Turn on recompilation checking. This will stop
1452 compilation early, leaving an existing <filename>.o</filename> file in
1453 place, if it can be determined that the module does not need to be
1459 <term><option>-no-recomp</option></term>
1460 <indexterm><primary><option>-recomp</option> option</primary></indexterm>
1463 Turn off recompilation checking.
1471 In the olden days, GHC compared the newly-generated
1472 <Filename>.hi</Filename> file with the previous version; if they were
1473 identical, it left the old one alone and didn't change its
1474 modification date. In consequence, importers of a module with an
1475 unchanged output <Filename>.hi</Filename> file were not recompiled.
1479 This doesn't work any more. In our earlier example, module
1480 <literal>C</literal> does not import module <literal>A</literal>
1481 directly, yet changes to <Filename>A.hi</Filename> should force a
1482 recompilation of <literal>C</literal>. And some changes to
1483 <literal>A</literal> (changing the definition of a function that
1484 appears in an inlining of a function exported by <literal>B</literal>,
1485 say) may conceivably not change <Filename>B.hi</Filename> one jot. So
1490 GHC keeps a version number on each interface file, and on each type
1491 signature within the interface file. It also keeps in every interface
1492 file a list of the version numbers of everything it used when it last
1493 compiled the file. If the source file's modification date is earlier
1494 than the <Filename>.o</Filename> file's date (i.e. the source hasn't
1495 changed since the file was last compiled), and the
1496 <option>-recomp</option> is given on the command line, GHC will be
1497 clever. It compares the version numbers on the things it needs this
1498 time with the version numbers on the things it needed last time
1499 (gleaned from the interface file of the module being compiled); if
1500 they are all the same it stops compiling rather early in the process
1501 saying “Compilation IS NOT required”. What a beautiful
1506 Patrick Sansom had a workshop paper about how all this is done (though
1507 the details have changed quite a bit). <ULink URL="mailto:sansom@dcs.gla.ac.uk">Ask him</ULink> if you want a copy.
1513 <Sect2 id="using-make">
1514 <Title>Using <Command>make</Command>
1518 <indexterm><primary><literal>make</literal></primary></indexterm>
1522 It is reasonably straightforward to set up a <Filename>Makefile</Filename> to use with GHC, assuming you name your source files the same as your modules.
1530 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
1532 SRCS = Main.lhs Foo.lhs Bar.lhs
1533 OBJS = Main.o Foo.o Bar.o
1535 .SUFFIXES : .o .hs .hi .lhs .hc .s
1539 $(HC) -o $@ $(HC_OPTS) $(OBJS)
1541 # Standard suffix rules
1546 $(HC) -c $< $(HC_OPTS)
1549 $(HC) -c $< $(HC_OPTS)
1551 # Inter-module dependencies
1552 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
1553 Main.o Main.hc Main.s : Foo.hi Baz.hi # Main imports Foo and Baz
1559 (Sophisticated <Command>make</Command> variants may achieve some of the above more
1560 elegantly. Notably, <Command>gmake</Command>'s pattern rules let you write the more
1568 $(HC) -c $< $(HC_OPTS)
1574 What we've shown should work with any <Command>make</Command>.)
1578 Note the cheesy <literal>.o.hi</literal> rule: It records the dependency of the
1579 interface (<Filename>.hi</Filename>) file on the source. The rule says a <Filename>.hi</Filename> file can
1580 be made from a <Filename>.o</Filename> file by doing…nothing. Which is true.
1584 Note the inter-module dependencies at the end of the Makefile, which
1591 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
1597 They tell <Command>make</Command> that if any of <literal>Foo.o</literal>, <literal>Foo.hc</literal> or <literal>Foo.s</literal> have an
1598 earlier modification date than <literal>Baz.hi</literal>, then the out-of-date file
1599 must be brought up to date. To bring it up to date, <literal>make</literal> looks for
1600 a rule to do so; one of the preceding suffix rules does the job
1606 <sect2 id="sec-makefile-dependencies">
1607 <title>Dependency generation</title>
1608 <indexterm><primary>dependencies in Makefiles</primary></indexterm>
1609 <indexterm><primary>Makefile dependencies</primary></indexterm>
1611 <para>Putting inter-dependencies of the form <literal>Foo.o :
1612 Bar.hi</literal> into your <Filename>Makefile</Filename> by hand
1613 is rather error-prone. Don't worry, GHC has support for
1614 automatically generating the required dependencies. Add the
1615 following to your <Filename>Makefile</Filename>:</para>
1619 ghc -M $(HC_OPTS) $(SRCS)
1622 <para>Now, before you start compiling, and any time you change
1623 the <literal>imports</literal> in your program, do <Command>make
1624 depend</Command> before you do <Command>make
1625 cool_pgm</Command>. <Command>ghc -M</Command> will append
1626 the needed dependencies to your
1627 <Filename>Makefile</Filename>.</para>
1629 <para>In general, if module <literal>A</literal> contains the
1636 then <command>ghc -M</command> will generate a dependency
1643 If module <literal>A</literal> contains the line
1646 import {-# SOURCE #-} B ...blah...
1649 then <command>ghc -M</command> will generate a dependency
1656 (See <xref linkend="hi-files"> for details of interface files.)
1657 If <literal>A</literal> imports multiple modules, then there
1658 will be multiple lines with <filename>A.o</filename> as the
1661 <para>By default, <Command>ghc -M</Command> generates all the
1662 dependencies, and then concatenates them onto the end of
1663 <Filename>makefile</Filename> (or <Filename>Makefile</Filename>
1664 if <Filename>makefile</Filename> doesn't exist) bracketed by the
1665 lines "<literal># DO NOT DELETE: Beginning of Haskell
1666 dependencies</literal>" and "<literal># DO NOT DELETE: End
1667 of Haskell dependencies</literal>". If these lines already
1668 exist in the <Filename>makefile</Filename>, then the old
1669 dependencies are deleted first.</para>
1671 <para>Internally, GHC uses a script to generate the
1672 dependencies, called <command>mkdependHS</command>. This script
1673 has some options of its own, which you might find useful.
1674 Options can be passed directly to <command>mkdependHS</command>
1675 with GHC's <literal>-optdep</literal> option. For example, to
1676 generate the dependencies into a file called
1677 <literal>.depend</literal> instead of
1678 <literal>Makefile</literal>:</para>
1681 ghc -M -optdep-f optdep.depend ...
1684 <para>The full list of options accepted by
1685 <command>mkdependHS</command> is:</para>
1690 <term><option>-w</option></term>
1692 <para>Turn off warnings about interface file shadowing.</para>
1697 <term><option>-f blah</option></term>
1699 <para>Use <Filename>blah</Filename> as the makefile,
1700 rather than <Filename>makefile</Filename> or
1701 <Filename>Makefile</Filename>. If
1702 <Filename>blah</Filename> doesn't exist,
1703 <Command>mkdependHS</Command> creates it. We often use
1704 <option>-f .depend</option> to put the dependencies in
1705 <Filename>.depend</Filename> and then
1706 <Command>include</Command> the file
1707 <Filename>.depend</Filename> into
1708 <Filename>Makefile</Filename>.</para>
1713 <term><option>-o <osuf></option></term>
1715 <para>Use <Filename>.<osuf></Filename> as the
1716 "target file" suffix ( default: <literal>o</literal>).
1717 Multiple <option>-o</option> flags are permitted (GHC2.05
1718 onwards). Thus "<option>-o hc -o o</option>" will
1719 generate dependencies for <Filename>.hc</Filename> and
1720 <Filename>.o</Filename> files.</para>
1725 <term><option>-s <suf></option></term>
1727 <para>Make extra dependencies that declare that files with
1729 <Filename>.<suf>_<osuf></Filename>
1730 depend on interface files with suffix
1731 <Filename>.<suf>_hi</Filename>, or (for
1732 <literal>{-# SOURCE #-}</literal>
1733 imports) on <Filename>.hi-boot</Filename>. Multiple
1734 <option>-s</option> flags are permitted. For example,
1735 <option>-o hc -s a -s b</option> will make dependencies
1736 for <Filename>.hc</Filename> on <Filename>.hi</Filename>,
1737 <Filename>.a_hc</Filename> on
1738 <Filename>.a_hi</Filename>, and
1739 <Filename>.b_hc</Filename> on
1740 <Filename>.b_hi</Filename>. (Useful in conjunction
1741 with NoFib "ways".)</para>
1746 <term><option>--exclude-module=<file></option></term>
1748 <para>Regard <Filename><file></Filename> as
1749 "stable"; i.e., exclude it from having dependencies on
1755 <term><option>-x</option></term>
1757 <para>same as <option>--exclude-module</option></para>
1762 <term><option>--exclude-directory=<dirs></option></term>
1764 <para>Regard the colon-separated list of directories
1765 <Filename><dirs></Filename> as containing stable,
1766 don't generate any dependencies on modules therein.</para>
1771 <term><option>-xdirs</option></term>
1773 <para>same as <option>--exclude-directory</option>.</para>
1778 <term><option>--include-module=<file></option></term>
1780 <para>Regard <Filename><file></Filename> as not
1781 "stable"; i.e., generate dependencies on it (if any). This
1782 option is normally used in conjunction with the
1783 <option>--exclude-directory</option> option.</para>
1788 <term><option>--include-prelude</option></term>
1790 <para>Regard prelude libraries as unstable, i.e., generate
1791 dependencies on the prelude modules used (including
1792 <literal>Prelude</literal>). This option is normally only
1793 used by the various system libraries. If a
1794 <option>-package</option> option is used, dependencies will
1795 also be generated on the library's interfaces.</para>
1802 <Sect2 id="mutual-recursion">
1803 <Title>How to compile mutually recursive modules
1807 <indexterm><primary>module system, recursion</primary></indexterm>
1808 <indexterm><primary>recursion, between modules</primary></indexterm>
1812 Currently, the compiler does not have proper support for dealing with
1813 mutually recursive modules:
1823 newtype TA = MkTA Int
1841 When compiling either module A and B, the compiler will try (in vain)
1842 to look for the interface file of the other. So, to get mutually
1843 recursive modules off the ground, you need to hand write an interface
1844 file for A or B, so as to break the loop. These hand-written
1845 interface files are called <literal>hi-boot</literal> files, and are placed in a file
1846 called <Filename><module>.hi-boot</Filename>. To import from an <literal>hi-boot</literal> file instead
1847 of the standard <Filename>.hi</Filename> file, use the following syntax in the importing module:
1848 <indexterm><primary>hi-boot files</primary></indexterm>
1849 <indexterm><primary>importing, hi-boot files</primary></indexterm>
1855 import {-# SOURCE #-} A
1861 The hand-written interface need only contain the bare minimum of
1862 information needed to get the bootstrapping process started. For
1863 example, it doesn't need to contain declarations for <Emphasis>everything</Emphasis>
1864 that module <literal>A</literal> exports, only the things required by the module that
1865 imports <literal>A</literal> recursively.
1869 For the example at hand, the boot interface file for A would look like
1876 __interface A 1 404 where
1877 __export A TA{MkTA} ;
1878 1 newtype TA = MkTA PrelBase.Int ;
1884 The syntax is essentially the same as a normal <Filename>.hi</Filename> file
1885 (unfortunately), but you can usually tailor an existing <Filename>.hi</Filename> file to
1886 make a <Filename>.hi-boot</Filename> file.
1890 Notice that we only put the declaration for the newtype <literal>TA</literal> in the
1891 <literal>hi-boot</literal> file, not the signature for <Function>f</Function>, since <Function>f</Function> isn't used by
1892 <literal>B</literal>.
1896 The number “1” after “__interface A” gives the version number of module A;
1897 it is incremented whenever anything in A's interface file changes. The “404” is
1898 the version number of the interface file <Emphasis>syntax</Emphasis>; we change it when
1899 we change the syntax of interface files so that you get a better error message when
1900 you try to read an old-format file with a new-format compiler.
1904 The number “1” at the beginning of a declaration is the <Emphasis>version
1905 number</Emphasis> of that declaration: for the purposes of <Filename>.hi-boot</Filename> files
1906 these can all be set to 1. All names must be fully qualified with the
1907 <Emphasis>original</Emphasis> module that an object comes from: for example, the
1908 reference to <literal>Int</literal> in the interface for <literal>A</literal> comes from <literal>PrelBase</literal>,
1909 which is a module internal to GHC's prelude. It's a pain, but that's
1914 If you want an hi-boot file to export a data type, but you don't want to give its constructors
1915 (because the constructors aren't used by the SOURCE-importing module), you can write simply:
1921 __interface A 1 404 where
1929 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
1933 <Emphasis>Note:</Emphasis> This is all a temporary solution, a version of the
1934 compiler that handles mutually recursive modules properly without the manual
1935 construction of interface files, is (allegedly) in the works.
1942 <sect1 id="packages">
1943 <title>Packages</title>
1944 <indexterm><primary>packages</primary></indexterm>
1946 <para>Packages are collections of libraries, conveniently grouped
1947 together as a single entity. The package system is flexible: a
1948 package may consist of Haskell code, foreign language code (eg. C
1949 libraries), or a mixture of the two. A package is a good way to
1950 group together related Haskell modules, and is essential if you
1951 intend to make the modules into a Windows DLL (see below).</para>
1953 <para>Because packages can contain both Haskell and C libraries, they
1954 are also a good way to provide convenient access to a Haskell
1955 layer over a C library.</para>
1957 <para>GHC comes with several packages (see <xref
1958 linkend="book-hslibs">), and packages can be added/removed from an
1959 existing GHC installation.</para>
1961 <sect2 id="listing-packages">
1962 <title>Listing the available packages</title>
1963 <indexterm><primary>packages</primary>
1964 <secondary>listing</secondary></indexterm>
1966 <para>To see what packages are currently installed, use the
1967 <literal>--list-packages</literal> option:</para>
1968 <indexterm><primary><literal>--list-packages</literal></primary>
1972 $ ghc --list-packages
1973 gmp, rts, std, lang, concurrent, data, net, posix, text, util
1976 <para>Note that your GHC installation might have a slightly
1977 different set of packages installed.</para>
1979 <para>The <literal>gmp</literal> and <literal>rts</literal>
1980 packages are always present, and represent the multi-precision
1981 integer and runtime system libraries respectively. The
1982 <literal>std</literal> package contains the Haskell prelude.
1983 The rest of the packages are optional libraries.</para>
1987 <sect2 id="using-packages">
1988 <title>Using a package</title>
1989 <indexterm><primary>packages</primary>
1990 <secondary>using</secondary></indexterm>
1992 <para>To use a package, add the <literal>-package</literal> flag
1993 to the command line:</para>
1997 <term><option>-package <lib></option></term>
1998 <indexterm><primary>-package <lib> option</primary></indexterm>
2000 <para>This option brings into scope all the modules from
2001 package <literal><lib></literal> (they still have to
2002 be imported in your Haskell source, however). It also
2003 causes the relevant libraries to be linked when linking is
2009 <para>Some packages depend on other packages, for example the
2010 <literal>text</literal> package makes use of some of the modules
2011 in the <literal>lang</literal> package. The package system
2012 takes care of all these dependencies, so that when you say
2013 <literal>-package text</literal> on the command line, you
2014 automatically get <literal>-package lang</literal> too.</para>
2017 <sect2 id="building-packages">
2018 <title>Building a package from Haskell source</title>
2019 <indexterm><primary>packages</primary>
2020 <secondary>building</secondary></indexterm>
2022 <para>It takes some special considerations to build a new
2027 <para>A package may contain several Haskell modules. A
2028 package may span many directories, or many packages may
2029 exist in a single directory. Packages may not be mutually
2034 <para>A package has a name
2035 (e.g. <filename>std</filename>)</para>
2039 <para>The Haskell code in a package may be built into one or
2040 more Unix libraries (e.g. <Filename>libHSfoo.a</Filename>),
2041 or a single DLL on Windows
2042 (e.g. <Filename>HSfoo.dll</Filename>). The restriction to a
2043 single DLL on Windows is that the package system is used to
2044 tell the compiler when it should make an inter-DLL call
2045 rather than an intra-DLL call (inter-DLL calls require an
2046 extra indirection).</para>
2050 <para>GHC does not maintain detailed cross-package
2051 dependency information. It does remember which modules in
2052 other packages the current module depends on, but not which
2053 things within those imported things.</para>
2057 <para>To compile a module which is to be part of a new package,
2058 use the <literal>-package-name</literal> option:</para>
2062 <term><option>-package-name <foo></option></term>
2063 <indexterm><primary><literal>-package-name</literal></primary>
2064 <secondary>option</secondary></indexterm>
2066 <para>This option is added to the command line when
2067 compiling a module that is destined to be part of package
2068 <literal>foo</literal>. If this flag is omitted then the
2069 default package <literal>Main</literal> is assumed.</para>
2074 <para>Failure to use the <literal>-package-name</literal> option
2075 when compiling a package will result in disaster on Windows, but
2076 is relatively harmless on Unix at the moment (it will just cause
2077 a few extra dependencies in some interface files). However,
2078 bear in mind that we might add support for Unix shared libraries
2079 at some point in the future.</para>
2081 <para>It is worth noting that on Windows, because each package
2082 is built as a DLL, and a reference to a DLL costs an extra
2083 indirection, intra-package references are cheaper than
2084 inter-package references. Of course, this applies to the
2085 <Filename>Main</Filename> package as well.</para>
2088 <sect2 id="package-management">
2089 <title>Package management</title>
2090 <indexterm><primary>packages</primary>
2091 <secondary>management</secondary></indexterm>
2093 <para>GHC uses a package configuration file, called
2094 <literal>packages.conf</literal>, which can be found in your GHC
2095 install directory. This file isn't intended to be edited
2096 directly, instead GHC provides options for adding & removing
2101 <term><option>--add-package</option></term>
2102 <indexterm><primary><literal>--add-package</literal></primary>
2103 <secondary>option</secondary></indexterm>
2105 <para>Reads a package specification (see below) on stdin,
2106 and adds it to the database of installed packages. The
2107 package specification must be a package that isn't already
2113 <term><option>--delete-package <foo></option></term>
2114 <indexterm><primary><literal>--delete-package</literal></primary>
2115 <secondary>option</secondary></indexterm>
2117 <para>Removes the specified package from the installed
2118 configuration.</para>
2123 <para>In both cases, the old package configuration file is saved
2124 in <literal>packages.conf.old</literal> in your GHC install
2125 directory, so in an emergency you can always copy this file into
2126 <literal>package.conf</literal> to restore the old
2129 <para>A package specification looks like this:</para>
2134 import_dirs = ["/usr/local/lib/imports/mypkg"],
2135 library_dirs = ["/usr/local/lib"],
2136 hs_libraries = ["HSmypkg" ],
2137 extra_libraries = ["HSmypkg_cbits"],
2139 c_includes = ["HsMyPkg.h"],
2140 package_deps = ["text", "data"],
2141 extra_ghc_opts = [],
2143 extra_ld_opts = ["-lmy_clib"]
2147 <para>Components of a package specification may be specified in
2148 any order, and are:</para>
2152 <term><literal>name</literal></term>
2153 <indexterm><primary><literal>name</literal></primary>
2154 <secondary>package specification</secondary></indexterm>
2156 <para>The package's name, for use with
2157 the <literal>-package</literal> flag and as listed in the
2158 <literal>--list-packages</literal> list.
2164 <term><literal>import_dirs</literal></term>
2165 <indexterm><primary><literal>import_dirs</literal></primary>
2166 <secondary>package specification</secondary></indexterm>
2168 <para>A list of directories containing interface files
2169 (<literal>.hi</literal> files) for this package.</para>
2174 <term><literal>library_dirs</literal></term>
2175 <indexterm><primary><literal>library_dirs</literal></primary>
2176 <secondary>package specification</secondary></indexterm>
2178 <para>A list of directories containing libraries for this
2184 <term><literal>hs_libraries</literal></term>
2185 <indexterm><primary><literal>hs_libraries</literal></primary>
2186 <secondary>package specification</secondary></indexterm>
2188 <para>A list of libraries containing Haskell code for this
2189 package, with the <literal>.a</literal> or
2190 <literal>.dll</literal> suffix omitted. On Unix, the
2191 <literal>lib</literal> prefix is also omitted.</para>
2196 <term><literal>extra_libraries</literal></term>
2197 <indexterm><primary><literal>extra_libraries</literal></primary>
2198 <secondary>package specification</secondary></indexterm>
2200 <para>A list of extra libraries for this package. The
2201 difference between <literal>hs_libraries</literal> and
2202 <literal>extra_libraries</literal> is that
2203 <literal>hs_libraries</literal> normally have several
2204 versions, to support profiling, parallel and other build
2205 options. The various versions are given different
2206 suffixes to distinguish them, for example the profiling
2207 version of the standard prelude library is named
2208 <filename>libHSstd_p.a</filename>, with the
2209 <literal>_p</literal> indicating that this is a profiling
2210 version. The suffix is added automatically by GHC for
2211 <literal>hs_libraries</literal> only, no suffix is added
2213 <literal>extra_libraries</literal>.</para>
2215 <para>Also, <literal>extra_libraries</literal> are placed
2216 on the linker command line before the
2217 <literal>hs_libraries</literal> for the same package. If
2218 your package has dependencies in the other direction, you
2219 might need to make two separate packages.</para>
2224 <term><literal>include_dirs</literal></term>
2225 <indexterm><primary><literal>include_dirs</literal></primary>
2226 <secondary>package specification</secondary></indexterm>
2228 <para>A list of directories containing C includes for this
2229 package (maybe the empty list).</para>
2234 <term><literal>c_includes</literal></term>
2235 <indexterm><primary><literal>c_includes</literal></primary>
2236 <secondary>package specification</secondary></indexterm>
2238 <para>A list of files to include for via-C compilations
2239 using this package. Typically this include file will
2240 contain function prototypes for any C functions used in
2241 the package, in case they end up being called as a result
2242 of Haskell functions from the package being
2248 <term><literal>package_deps</literal></term>
2249 <indexterm><primary><literal>package_deps</literal></primary>
2250 <secondary>package specification</secondary></indexterm>
2252 <para>A list of packages which this package depends
2258 <term><literal>extra_ghc_opts</literal></term>
2259 <indexterm><primary><literal>extra_ghc_opts</literal></primary>
2260 <secondary>package specification</secondary></indexterm>
2262 <para>Extra arguments to be added to the GHC command line
2263 when this package is being used.</para>
2268 <term><literal>extra_cc_opts</literal></term>
2269 <indexterm><primary><literal>extra_cc_opts</literal></primary>
2270 <secondary>package specification</secondary></indexterm>
2272 <para>Extra arguments to be added to the gcc command line
2273 when this package is being used (only for via-C
2274 compilations).</para>
2279 <term><literal>extra_ld_opts</literal></term>
2280 <indexterm><primary><literal>extra_ld_opts</literal></primary>
2281 <secondary>package specification</secondary></indexterm>
2283 <para>Extra arguments to be added to the gcc command line
2284 (for linking) when this package is being used.</para>
2289 <para>For examples of more package specifications, take a look
2290 at the <literal>package.conf</literal> in your GHC
2291 installation.</para>
2296 <Sect1 id="options-optimise">
2297 <Title>Optimisation (code improvement)
2301 <indexterm><primary>optimisation (GHC)</primary></indexterm>
2302 <indexterm><primary>improvement, code (GHC)</primary></indexterm>
2306 The <option>-O*</option> options specify convenient “packages” of optimisation
2307 flags; the <option>-f*</option> options described later on specify
2308 <Emphasis>individual</Emphasis> optimisations to be turned on/off; the <option>-m*</option>
2309 options specify <Emphasis>machine-specific</Emphasis> optimisations to be turned
2313 <Sect2 id="optimise-pkgs">
2314 <Title><option>-O*</option>: convenient “packages” of optimisation flags.
2318 <indexterm><primary>-O options</primary></indexterm>
2322 There are <Emphasis>many</Emphasis> options that affect the quality of code
2323 produced by GHC. Most people only have a general goal, something like
2324 “Compile quickly” or “Make my program run like greased lightning.”
2325 The following “packages” of optimisations (or lack thereof) should
2330 Once you choose a <option>-O*</option> “package,” stick with it—don't chop and
2331 change. Modules' interfaces <Emphasis>will</Emphasis> change with a shift to a new
2332 <option>-O*</option> option, and you may have to recompile a large chunk of all
2333 importing modules before your program can again be run
2334 safely (see <XRef LinkEnd="recomp">).
2341 <term>No <option>-O*</option>-type option specified:</term>
2342 <indexterm><primary>-O* not specified</primary></indexterm>
2345 This is taken to mean: “Please compile quickly; I'm not over-bothered
2346 about compiled-code quality.” So, for example: <Command>ghc -c Foo.hs</Command>
2351 <term><option>-O</option> or <option>-O1</option>:</term>
2352 <indexterm><primary>-O option</primary></indexterm>
2353 <indexterm><primary>-O1 option</primary></indexterm>
2354 <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
2357 Means: “Generate good-quality code without taking too long about
2358 it.” Thus, for example: <Command>ghc -c -O Main.lhs</Command>
2363 <term><option>-O2</option>:</term>
2364 <indexterm><primary>-O2 option</primary></indexterm>
2365 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
2368 Means: “Apply every non-dangerous optimisation, even if it means
2369 significantly longer compile times.”
2373 The avoided “dangerous” optimisations are those that can make
2374 runtime or space <Emphasis>worse</Emphasis> if you're unlucky. They are
2375 normally turned on or off individually.
2379 At the moment, <option>-O2</option> is <Emphasis>unlikely</Emphasis> to produce
2380 better code than <option>-O</option>.
2385 <term><option>-O2-for-C</option>:</term>
2386 <indexterm><primary>-O2-for-C option</primary></indexterm>
2387 <indexterm><primary>gcc, invoking with -O2</primary></indexterm>
2390 Says to run GCC with <option>-O2</option>, which may be worth a few percent in
2391 execution speed. Don't forget <option>-fvia-C</option>, lest you use the native-code
2392 generator and bypass GCC altogether!
2397 <term><option>-Onot</option>:</term>
2398 <indexterm><primary>-Onot option</primary></indexterm>
2399 <indexterm><primary>optimising, reset</primary></indexterm>
2402 This option will make GHC “forget” any
2403 <option>-O</option>ish options it has seen so far. Sometimes useful;
2404 for example: <Command>make all
2405 EXTRA_HC_OPTS=-Onot</Command>.
2410 <term><option>-Ofile <file></option>:</term>
2411 <indexterm><primary>-Ofile <file> option</primary></indexterm>
2412 <indexterm><primary>optimising, customised</primary></indexterm>
2415 For those who need <Emphasis>absolute</Emphasis> control over
2416 <Emphasis>exactly</Emphasis> what options are used (e.g., compiler
2417 writers, sometimes :-), a list of options can be put in a file and
2418 then slurped in with <option>-Ofile</option>.
2422 In that file, comments are of the
2423 <literal>#</literal>-to-end-of-line variety; blank lines and most
2424 whitespace is ignored.
2428 Please ask if you are baffled and would like an example of <option>-Ofile</option>!
2436 At Glasgow, we don't use a <option>-O*</option> flag for day-to-day work. We use
2437 <option>-O</option> to get respectable speed; e.g., when we want to measure
2438 something. When we want to go for broke, we tend to use <option>-O -fvia-C
2439 -O2-for-C</option> (and we go for lots of coffee breaks).
2443 The easiest way to see what <option>-O</option> (etc.) “really mean” is to run with
2444 <option>-v</option>, then stand back in amazement. Alternatively, just look at the
2445 <literal>HsC_minus<blah></literal> lists in the GHC driver script.
2451 <Title><option>-f*</option>: platform-independent flags</Title>
2454 <indexterm><primary>-f* options (GHC)</primary></indexterm>
2455 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
2459 Flags can be turned <Emphasis>off</Emphasis> individually. (NB: I hope you have a
2460 good reason for doing this…) To turn off the <option>-ffoo</option> flag, just use
2461 the <option>-fno-foo</option> flag.<indexterm><primary>-fno-<opt> anti-option</primary></indexterm> So, for
2462 example, you can say <option>-O2 -fno-strictness</option>, which will then drop out
2463 any running of the strictness analyser.
2467 The options you are most likely to want to turn off are:
2473 <option>-fno-strictness</option><indexterm><primary>-fno-strictness option</primary></indexterm> (strictness
2474 analyser, because it is sometimes slow),
2480 <option>-fno-specialise</option><indexterm><primary>-fno-specialise option</primary></indexterm> (automatic
2481 specialisation of overloaded functions, because it can make your code
2482 bigger) (US spelling also accepted), and
2488 <option>-fno-cpr-analyse</option><indexterm><primary>-fno-cpr-analyse option</primary></indexterm> switches off the CPR (constructed product
2498 Should you wish to turn individual flags <Emphasis>on</Emphasis>, you are advised
2499 to use the <option>-Ofile</option> option, described above. Because the order in
2500 which optimisation passes are run is sometimes crucial, it's quite
2501 hard to do with command-line options.
2505 Here are some “dangerous” optimisations you <Emphasis>might</Emphasis> want to try:
2509 <term><option>-fvia-C</option>:</term>
2512 <indexterm><primary>-fvia-C option</primary></indexterm>
2513 <indexterm><primary>native code generator, turning off</primary></indexterm>
2517 Compile via C, and don't use the native-code generator. (There are many
2518 cases when GHC does this on its own.) You might pick up a little bit of
2519 speed by compiling via C (e.g. for floating-point intensive code on Intel).
2520 If you use <Function>_casm_</Function>s (which are utterly
2521 deprecated), you probably <Emphasis>have</Emphasis> to use
2522 <option>-fvia-C</option>.
2526 The lower-case incantation, <option>-fvia-c</option>, is synonymous.
2530 Compiling via C will probably be slower (in compilation time) than
2531 using GHC's native code generator.
2536 <term><option>-funfolding-interface-threshold<n></option>:</term>
2539 <indexterm><primary>-funfolding-interface-threshold option</primary></indexterm>
2540 <indexterm><primary>inlining, controlling</primary></indexterm>
2541 <indexterm><primary>unfolding, controlling</primary></indexterm>
2542 (Default: 30) By raising or lowering this number, you can raise or
2543 lower the amount of pragmatic junk that gets spewed into interface
2544 files. (An unfolding has a “size” that reflects the cost in terms
2545 of “code bloat” of expanding that unfolding in another module. A
2546 bigger function would be assigned a bigger cost.)
2551 <term><option>-funfolding-creation-threshold<n></option>:</term>
2554 <indexterm><primary>-funfolding-creation-threshold option</primary></indexterm>
2555 <indexterm><primary>inlining, controlling</primary></indexterm>
2556 <indexterm><primary>unfolding, controlling</primary></indexterm>
2557 (Default: 30) This option is similar to
2558 <option>-funfolding-interface-threshold</option>, except that it governs unfoldings
2559 within a single module. Increasing this figure is more likely to
2560 result in longer compile times than faster code. The next option is
2566 <term><option>-funfolding-use-threshold<n></option>:</term>
2569 <indexterm><primary>-funfolding-use-threshold option</primary></indexterm>
2570 <indexterm><primary>inlining, controlling</primary></indexterm>
2571 <indexterm><primary>unfolding, controlling</primary></indexterm>
2572 (Default: 8) This is the magic cut-off figure for unfolding: below
2573 this size, a function definition will be unfolded at the call-site,
2574 any bigger and it won't. The size computed for a function depends on
2575 two things: the actual size of the expression minus any discounts that
2576 apply (see <option>-funfolding-con-discount</option>).
2582 <term><option>-funfolding-update-in-place<n></option>:</term>
2585 Switches on an experimental "optimisation". Switching it on makes the compiler
2586 a little keener to inline a function that returns a constructor, if the context is
2591 If we inlined plusInt we might get an opportunity to use update-in-place for
2598 <term><option>-funbox-strict-fields</option>:</term>
2601 <indexterm><primary>-funbox-strict-fields option</primary></indexterm>
2602 <indexterm><primary>strict constructor fields</primary></indexterm>
2603 <indexterm><primary>constructor fields, strict</primary></indexterm>
2607 This option causes all constructor fields which are marked strict
2608 (i.e. “!”) to be unboxed or unpacked if possible. For example:
2614 data T = T !Float !Float
2620 will create a constructor <literal>T</literal> containing two unboxed floats if the
2621 <option>-funbox-strict-fields</option> flag is given. This may not always be an
2622 optimisation: if the <Function>T</Function> constructor is scrutinised and the floats
2623 passed to a non-strict function for example, they will have to be
2624 reboxed (this is done automatically by the compiler).
2628 This option should only be used in conjunction with <option>-O</option>, in order to
2629 expose unfoldings to the compiler so the reboxing can be removed as
2630 often as possible. For example:
2637 f (T f1 f2) = f1 + f2
2643 The compiler will avoid reboxing <Function>f1</Function> and <Function>f2</Function> by inlining <Function>+</Function> on
2644 floats, but only when <option>-O</option> is on.
2648 Any single-constructor data is eligible for unpacking; for example
2654 data T = T !(Int,Int)
2660 will store the two <literal>Int</literal>s directly in the <Function>T</Function> constructor, by flattening
2661 the pair. Multi-level unpacking is also supported:
2668 data S = S !Int !Int
2674 will store two unboxed <literal>Int#</literal>s directly in the <Function>T</Function> constructor.
2679 <term><option>-fsemi-tagging</option>:</term>
2682 This option (which <Emphasis>does not work</Emphasis> with the native-code generator)
2683 tells the compiler to add extra code to test for already-evaluated
2684 values. You win if you have lots of such values during a run of your
2685 program, you lose otherwise. (And you pay in extra code space.)
2689 We have not played with <option>-fsemi-tagging</option> enough to recommend it.
2690 (For all we know, it doesn't even work anymore… Sigh.)
2695 <term><option>-fexcess-precision</option>:</term>
2698 When this option is given, intermediate floating point values can have
2699 a <Emphasis>greater</Emphasis> precision/range than the final type.
2700 Generally this is a good thing, but some programs may rely on the
2701 exact precision/range of <literal>Float</literal>/<literal>Double</literal>
2702 values and should not use this option for their compilation.
2712 <Title><option>-m*</option>: platform-specific flags</Title>
2715 <indexterm><primary>-m* options (GHC)</primary></indexterm>
2716 <indexterm><primary>platform-specific options</primary></indexterm>
2717 <indexterm><primary>machine-specific options</primary></indexterm>
2721 Some flags only make sense for particular target platforms.
2728 <term><option>-mv8</option>:</term>
2731 (SPARC machines)<indexterm><primary>-mv8 option (SPARC only)</primary></indexterm>
2732 Means to pass the like-named option to GCC; it says to use the
2733 Version 8 SPARC instructions, notably integer multiply and divide.
2734 The similiar <option>-m*</option> GCC options for SPARC also work, actually.
2739 <term><option>-mlong-calls</option>:</term>
2742 (HPPA machines)<indexterm><primary>-mlong-calls option (HPPA only)</primary></indexterm>
2743 Means to pass the like-named option to GCC. Required for Very Big
2744 modules, maybe. (Probably means you're in trouble…)
2749 <term><option>-monly-[32]-regs</option>:</term>
2752 (iX86 machines)<indexterm><primary>-monly-N-regs option (iX86 only)</primary></indexterm>
2753 GHC tries to “steal” four registers from GCC, for performance
2754 reasons; it almost always works. However, when GCC is compiling some
2755 modules with four stolen registers, it will crash, probably saying:
2758 Foo.hc:533: fixed or forbidden register was spilled.
2759 This may be due to a compiler bug or to impossible asm
2760 statements or clauses.
2763 Just give some registers back with <option>-monly-N-regs</option>. Try `3' first,
2764 then `2'. If `2' doesn't work, please report the bug to us.
2773 <Sect2 id="optimise-C-compiler">
2774 <Title>Code improvement by the C compiler.
2778 <indexterm><primary>optimisation by GCC</primary></indexterm>
2779 <indexterm><primary>GCC optimisation</primary></indexterm>
2783 The C compiler (GCC) is run with <option>-O</option> turned on. (It has
2788 If you want to run GCC with <option>-O2</option>—which may be worth a few
2789 percent in execution speed—you can give a
2790 <option>-O2-for-C</option><indexterm><primary>-O2-for-C option</primary></indexterm> option.
2797 <Sect1 id="options-phases">
2798 <Title>Options related to a particular phase
2801 <Sect2 id="c-pre-processor">
2802 <Title>The C pre-processor
2806 <indexterm><primary>pre-processing: cpp</primary></indexterm>
2807 <indexterm><primary>C pre-processor options</primary></indexterm>
2808 <indexterm><primary>cpp, pre-processing with</primary></indexterm>
2812 The C pre-processor <Command>cpp</Command> is run over your Haskell code only if the
2813 <option>-cpp</option> option <indexterm><primary>-cpp option</primary></indexterm> is given. Unless you are
2814 building a large system with significant doses of conditional
2815 compilation, you really shouldn't need it.
2819 <term><option>-D<foo></option>:</term>
2822 <indexterm><primary>-D<name> option</primary></indexterm>
2823 Define macro <Constant><foo></Constant> in the usual way. NB: does <Emphasis>not</Emphasis> affect
2824 <option>-D</option> macros passed to the C compiler when compiling via C! For those,
2825 use the <option>-optc-Dfoo</option> hack… (see <XRef LinkEnd="forcing-options-through">).
2830 <term><option>-U<foo></option>:</term>
2833 <indexterm><primary>-U<name> option</primary></indexterm>
2834 Undefine macro <Command><foo></Command> in the usual way.
2839 <term><option>-I<dir></option>:</term>
2842 <indexterm><primary>-I<dir> option</primary></indexterm>
2843 Specify a directory in which to look for <literal>#include</literal> files, in
2852 The GHC driver pre-defines several macros when processing Haskell
2853 source code (<Filename>.hs</Filename> or <Filename>.lhs</Filename> files):
2860 <term><Constant>__HASKELL98__</Constant>:</term>
2863 <indexterm><primary>__HASKELL98__</primary></indexterm>
2864 If defined, this means that GHC supports the language defined by the
2870 <term><Constant>__HASKELL__=98</Constant>:</term>
2873 <indexterm><primary>__HASKELL__</primary></indexterm>
2874 In GHC 4.04 and later, the <Constant>__HASKELL__</Constant> macro is defined as having
2875 the value <Constant>98</Constant>.
2880 <term><Constant>__HASKELL1__</Constant>:</term>
2883 <indexterm><primary>__HASKELL1__ macro</primary></indexterm>
2884 If defined to <Emphasis>n</Emphasis>, that means GHC supports the Haskell language
2885 defined in the Haskell report version <Emphasis>1.n</Emphasis>. Currently 5. This
2886 macro is deprecated, and will probably disappear in future versions.
2891 <term><Constant>__GLASGOW_HASKELL__</Constant>:</term>
2894 <indexterm><primary>__GLASGOW_HASKELL__ macro</primary></indexterm>
2895 For version <Emphasis>n</Emphasis> of the GHC system, this will be <literal>#define</literal>d to
2896 <Emphasis>100n</Emphasis>. So, for version 4.00, it is 400.
2900 With any luck, <Constant>__GLASGOW_HASKELL__</Constant> will be undefined in all other
2901 implementations that support C-style pre-processing.
2905 (For reference: the comparable symbols for other systems are:
2906 <Constant>__HUGS__</Constant> for Hugs and <Constant>__HBC__</Constant> for Chalmers.)
2910 NB. This macro is set when pre-processing both Haskell source and C
2911 source, including the C source generated from a Haskell module
2912 (i.e. <Filename>.hs</Filename>, <Filename>.lhs</Filename>, <Filename>.c</Filename> and <Filename>.hc</Filename> files).
2917 <term><Constant>__CONCURRENT_HASKELL__</Constant>:</term>
2920 <indexterm><primary>__CONCURRENT_HASKELL__ macro</primary></indexterm>
2921 This symbol is defined when pre-processing Haskell (input) and
2922 pre-processing C (GHC output). Since GHC from verion 4.00 now
2923 supports concurrent haskell by default, this symbol is always defined.
2928 <term><Constant>__PARALLEL_HASKELL__</Constant>:</term>
2931 <indexterm><primary>__PARALLEL_HASKELL__ macro</primary></indexterm>
2932 Only defined when <option>-parallel</option> is in use! This symbol is defined when
2933 pre-processing Haskell (input) and pre-processing C (GHC output).
2941 Options other than the above can be forced through to the C
2942 pre-processor with the <option>-opt</option> flags (see
2943 <XRef LinkEnd="forcing-options-through">).
2947 A small word of warning: <option>-cpp</option> is not friendly to “string
2948 gaps”.<indexterm><primary>-cpp vs string gaps</primary></indexterm><indexterm><primary>string gaps vs
2949 -cpp</primary></indexterm>. In other words, strings such as the following:
2963 don't work with <option>-cpp</option>; <Filename>/usr/bin/cpp</Filename> elides the
2964 backslash-newline pairs.
2968 However, it appears that if you add a space at the end of the line,
2969 then <Command>cpp</Command> (at least GNU <Command>cpp</Command> and possibly other <Command>cpp</Command>s)
2970 leaves the backslash-space pairs alone and the string gap works as
2976 <Sect2 id="options-C-compiler">
2977 <Title>Options affecting the C compiler (if applicable)
2981 <indexterm><primary>include-file options</primary></indexterm>
2982 <indexterm><primary>C compiler options</primary></indexterm>
2983 <indexterm><primary>GCC options</primary></indexterm>
2987 At the moment, quite a few common C-compiler options are passed on
2988 quietly to the C compilation of Haskell-compiler-generated C files.
2989 THIS MAY CHANGE. Meanwhile, options so sent are:
2996 <ColSpec Align="Left" Colsep="0">
2997 <ColSpec Align="Left" Colsep="0">
3000 <Entry><option>-ansi</option> </Entry>
3001 <Entry> do ANSI C (not K&R) </Entry>
3005 <option>-pedantic</option> </Entry>
3006 <Entry> be so</Entry>
3010 <option>-dgcc-lint</option> </Entry>
3011 <Entry> (hack) short for “make GCC very paranoid”</Entry>
3019 <indexterm><primary>-ansi option (for GCC)</primary></indexterm>
3020 <indexterm><primary>-pedantic option (for GCC)</primary></indexterm>
3021 <indexterm><primary>-dgcc-lint option (GCC paranoia)</primary></indexterm>
3025 If you are compiling with lots of foreign calls, you may need to
3026 tell the C compiler about some <literal>#include</literal> files. There is no real
3027 pretty way to do this, but you can use this hack from the
3034 % ghc -c '-#include <X/Xlib.h>' Xstuff.lhs
3041 <Sect2 id="options-linker">
3042 <Title>Linking and consistency-checking
3046 <indexterm><primary>linker options</primary></indexterm>
3047 <indexterm><primary>ld options</primary></indexterm>
3051 GHC has to link your code with various libraries, possibly including:
3052 user-supplied, GHC-supplied, and system-supplied (<option>-lm</option> math
3053 library, for example).
3060 <term><option>-l<FOO></option>:</term>
3063 <indexterm><primary>-l<lib> option</primary></indexterm>
3064 Link in a library named <Filename>lib<FOO>.a</Filename> which resides somewhere on the
3065 library directories path.
3069 Because of the sad state of most UNIX linkers, the order of such
3070 options does matter. Thus: <Command>ghc -lbar *.o</Command> is almost certainly
3071 wrong, because it will search <Filename>libbar.a</Filename> <Emphasis>before</Emphasis> it has
3072 collected unresolved symbols from the <Filename>*.o</Filename> files.
3073 <Command>ghc *.o -lbar</Command> is probably better.
3077 The linker will of course be informed about some GHC-supplied
3078 libraries automatically; these are:
3085 <ColSpec Align="Left" Colsep="0">
3086 <ColSpec Align="Left" Colsep="0">
3089 <Entry><Emphasis>-l equivalent</Emphasis> </Entry>
3090 <Entry> <Emphasis>description</Emphasis> </Entry>
3095 <option>-lHSrts,-lHSclib</option> </Entry>
3096 <Entry> basic runtime libraries </Entry>
3100 <option>-lHS</option> </Entry>
3101 <Entry> standard Prelude library </Entry>
3105 <option>-lHS_cbits</option> </Entry>
3106 <Entry> C support code for standard Prelude library </Entry>
3110 <option>-lgmp</option> </Entry>
3111 <Entry> GNU multi-precision library (for Integers)</Entry>
3122 <indexterm><primary>-lHS library</primary></indexterm>
3123 <indexterm><primary>-lHS_cbits library</primary></indexterm>
3124 <indexterm><primary>-lHSrts library</primary></indexterm>
3125 <indexterm><primary>-lgmp library</primary></indexterm>
3130 <term><option>-package <name></option>:</term>
3133 <indexterm><primary>-package <name> option</primary></indexterm>
3137 If you are using a Haskell “package” (e.g., the POSIX
3138 library), just use the <option>-package posix</option> option, and the
3139 correct code should be linked in. See <xref linkend="packages"> for
3145 <term><option>-L<dir></option>:</term>
3148 <indexterm><primary>-L<dir> option</primary></indexterm>
3149 Where to find user-supplied libraries… Prepend the directory
3150 <Filename><dir></Filename> to the library directories path.
3155 <term><option>-static</option>:</term>
3158 <indexterm><primary>-static option</primary></indexterm>
3159 Tell the linker to avoid shared libraries.
3164 <term><option>-no-link-chk</option> and <option>-link-chk</option>:</term>
3167 <indexterm><primary>-no-link-chk option</primary></indexterm>
3168 <indexterm><primary>-link-chk option</primary></indexterm>
3169 <indexterm><primary>consistency checking of executables</primary></indexterm>
3170 By default, immediately after linking an executable, GHC verifies that
3171 the pieces that went into it were compiled with compatible flags; a
3172 “consistency check”.
3173 (This is to avoid mysterious failures caused by non-meshing of
3174 incompatibly-compiled programs; e.g., if one <Filename>.o</Filename> file was compiled
3175 for a parallel machine and the others weren't.) You may turn off this
3176 check with <option>-no-link-chk</option>. You can turn it (back) on with
3177 <option>-link-chk</option> (the default).
3182 <term><option>-no-hs-main</option>:</term>
3185 <indexterm><primary>-no-hs-main option</primary></indexterm>
3186 <indexterm><primary>linking Haskell libraries with foreign code</primary></indexterm>
3190 In the event you want to include ghc-compiled code as part of another
3191 (non-Haskell) program, the RTS will not be supplying its definition of
3192 <Function>main()</Function> at link-time, you will have to. To signal that to the
3193 driver script when linking, use <option>-no-hs-main</option>.
3197 Notice that since the command-line passed to the linker is rather
3198 involved, you probably want to use the ghc driver script to do the
3199 final link of your `mixed-language' application. This is not a
3200 requirement though, just try linking once with <option>-v</option> on to see what
3201 options the driver passes through to the linker.
3212 <Sect1 id="sec-using-concurrent">
3213 <Title>Using Concurrent Haskell</Title>
3216 <indexterm><primary>Concurrent Haskell—use</primary></indexterm>
3220 GHC (as of version 4.00) supports Concurrent Haskell by default,
3221 without requiring a special option or libraries compiled in a certain
3222 way. To get access to the support libraries for Concurrent Haskell
3223 (i.e. <literal>Concurrent</literal> and friends), use the
3224 <option>-package concurrent</option> option.
3228 Three RTS options are provided for modifying the behaviour of the
3229 threaded runtime system. See the descriptions of
3230 <option>-C[<us>]</option>, <option>-q</option>, and
3231 <option>-t<num></option> in <XRef LinkEnd="parallel-rts-opts">.
3235 Concurrent Haskell is described in more detail in <XRef
3236 LinkEnd="sec-Concurrent">.
3241 <Sect1 id="sec-using-parallel">
3242 <Title>Using Parallel Haskell</Title>
3245 <indexterm><primary>Parallel Haskell—use</primary></indexterm>
3249 [You won't be able to execute parallel Haskell programs unless PVM3
3250 (Parallel Virtual Machine, version 3) is installed at your site.]
3254 To compile a Haskell program for parallel execution under PVM, use the
3255 <option>-parallel</option> option,<indexterm><primary>-parallel
3256 option</primary></indexterm> both when compiling <Emphasis>and
3257 linking</Emphasis>. You will probably want to <literal>import
3258 Parallel</literal> into your Haskell modules.
3262 To run your parallel program, once PVM is going, just invoke it
3263 “as normal”. The main extra RTS option is
3264 <option>-N<n></option>, to say how many PVM
3265 “processors” your program to run on. (For more details of
3266 all relevant RTS options, please see <XRef
3267 LinkEnd="parallel-rts-opts">.)
3271 In truth, running Parallel Haskell programs and getting information
3272 out of them (e.g., parallelism profiles) is a battle with the vagaries of
3273 PVM, detailed in the following sections.
3277 <Title>Dummy's guide to using PVM</Title>
3280 <indexterm><primary>PVM, how to use</primary></indexterm>
3281 <indexterm><primary>Parallel Haskell—PVM use</primary></indexterm>
3282 Before you can run a parallel program under PVM, you must set the
3283 required environment variables (PVM's idea, not ours); something like,
3284 probably in your <Filename>.cshrc</Filename> or equivalent:
3287 setenv PVM_ROOT /wherever/you/put/it
3288 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
3289 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
3295 Creating and/or controlling your “parallel machine” is a purely-PVM
3296 business; nothing specific to Parallel Haskell.
3300 You use the <Command>pvm</Command><indexterm><primary>pvm command</primary></indexterm> command to start PVM on your
3301 machine. You can then do various things to control/monitor your
3302 “parallel machine;” the most useful being:
3308 <ColSpec Align="Left">
3312 <Entry><KeyCombo><KeyCap>Control</KeyCap><KeyCap>D</KeyCap></KeyCombo></Entry>
3313 <Entry>exit <Command>pvm</Command>, leaving it running</Entry>
3317 <Entry><Command>halt</Command></Entry>
3318 <Entry>kill off this “parallel machine” & exit</Entry>
3322 <Entry><Command>add <host></Command></Entry>
3323 <Entry>add <Command><host></Command> as a processor</Entry>
3327 <Entry><Command>delete <host></Command></Entry>
3328 <Entry>delete <Command><host></Command></Entry>
3332 <Entry><Command>reset</Command></Entry>
3333 <Entry>kill what's going, but leave PVM up</Entry>
3337 <Entry><Command>conf</Command></Entry>
3338 <Entry>list the current configuration</Entry>
3342 <Entry><Command>ps</Command></Entry>
3343 <Entry>report processes' status</Entry>
3347 <Entry><Command>pstat <pid></Command></Entry>
3348 <Entry>status of a particular process</Entry>
3357 The PVM documentation can tell you much, much more about <Command>pvm</Command>!
3363 <Title>Parallelism profiles</Title>
3366 <indexterm><primary>parallelism profiles</primary></indexterm>
3367 <indexterm><primary>profiles, parallelism</primary></indexterm>
3368 <indexterm><primary>visualisation tools</primary></indexterm>
3372 With Parallel Haskell programs, we usually don't care about the
3373 results—only with “how parallel” it was! We want pretty pictures.
3377 Parallelism profiles (à la <Command>hbcpp</Command>) can be generated with the
3378 <option>-q</option><indexterm><primary>-q RTS option (concurrent, parallel)</primary></indexterm> RTS option. The
3379 per-processor profiling info is dumped into files named
3380 <Filename><full-path><program>.gr</Filename>. These are then munged into a PostScript picture,
3381 which you can then display. For example, to run your program
3382 <Filename>a.out</Filename> on 8 processors, then view the parallelism profile, do:
3388 % ./a.out +RTS -N8 -q
3389 % grs2gr *.???.gr > temp.gr # combine the 8 .gr files into one
3390 % gr2ps -O temp.gr # cvt to .ps; output in temp.ps
3391 % ghostview -seascape temp.ps # look at it!
3397 The scripts for processing the parallelism profiles are distributed
3398 in <Filename>ghc/utils/parallel/</Filename>.
3404 <Title>Other useful info about running parallel programs</Title>
3407 The “garbage-collection statistics” RTS options can be useful for
3408 seeing what parallel programs are doing. If you do either
3409 <option>+RTS -Sstderr</option><indexterm><primary>-Sstderr RTS option</primary></indexterm> or <option>+RTS -sstderr</option>, then
3410 you'll get mutator, garbage-collection, etc., times on standard
3411 error. The standard error of all PE's other than the `main thread'
3412 appears in <Filename>/tmp/pvml.nnn</Filename>, courtesy of PVM.
3416 Whether doing <option>+RTS -Sstderr</option> or not, a handy way to watch
3417 what's happening overall is: <Command>tail -f /tmp/pvml.nnn</Command>.
3422 <Sect2 id="parallel-rts-opts">
3423 <Title>RTS options for Concurrent/Parallel Haskell
3427 <indexterm><primary>RTS options, concurrent</primary></indexterm>
3428 <indexterm><primary>RTS options, parallel</primary></indexterm>
3429 <indexterm><primary>Concurrent Haskell—RTS options</primary></indexterm>
3430 <indexterm><primary>Parallel Haskell—RTS options</primary></indexterm>
3434 Besides the usual runtime system (RTS) options
3435 (<XRef LinkEnd="runtime-control">), there are a few options particularly
3436 for concurrent/parallel execution.
3443 <term><option>-N<N></option>:</term>
3446 <indexterm><primary>-N<N> RTS option (parallel)</primary></indexterm>
3447 (PARALLEL ONLY) Use <literal><N></literal> PVM processors to run this program;
3453 <term><option>-C[<us>]</option>:</term>
3456 <indexterm><primary>-C<us> RTS option</primary></indexterm> Sets
3457 the context switch interval to <literal><s></literal> seconds.
3458 A context switch will occur at the next heap block allocation after
3459 the timer expires (a heap block allocation occurs every 4k of
3460 allocation). With <option>-C0</option> or <option>-C</option>,
3461 context switches will occur as often as possible (at every heap block
3462 allocation). By default, context switches occur every 20ms
3463 milliseconds. Note that GHC's internal timer ticks every 20ms, and
3464 the context switch timer is always a multiple of this timer, so 20ms
3465 is the maximum granularity available for timed context switches.
3470 <term><option>-q[v]</option>:</term>
3473 <indexterm><primary>-q RTS option</primary></indexterm>
3474 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
3475 in the file <FIlename><program>.qp</FIlename>. In the style of <Command>hbcpp</Command>, this profile
3476 records the movement of threads between the green (runnable) and red
3477 (blocked) queues. If you specify the verbose suboption (<option>-qv</option>), the
3478 green queue is split into green (for the currently running thread
3479 only) and amber (for other runnable threads). We do not recommend
3480 that you use the verbose suboption if you are planning to use the
3481 <Command>hbcpp</Command> profiling tools or if you are context switching at every heap
3482 check (with <option>-C</option>).
3487 <term><option>-t<num></option>:</term>
3490 <indexterm><primary>-t<num> RTS option</primary></indexterm>
3491 (PARALLEL ONLY) Limit the number of concurrent threads per processor
3492 to <literal><num></literal>. The default is 32. Each thread requires slightly over 1K
3493 <Emphasis>words</Emphasis> in the heap for thread state and stack objects. (For
3494 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
3500 <term><option>-d</option>:</term>
3503 <indexterm><primary>-d RTS option (parallel)</primary></indexterm>
3504 (PARALLEL ONLY) Turn on debugging. It pops up one xterm (or GDB, or
3505 something…) per PVM processor. We use the standard <Command>debugger</Command>
3506 script that comes with PVM3, but we sometimes meddle with the
3507 <Command>debugger2</Command> script. We include ours in the GHC distribution,
3508 in <Filename>ghc/utils/pvm/</Filename>.
3513 <term><option>-e<num></option>:</term>
3516 <indexterm><primary>-e<num> RTS option (parallel)</primary></indexterm>
3517 (PARALLEL ONLY) Limit the number of pending sparks per processor to
3518 <literal><num></literal>. The default is 100. A larger number may be appropriate if
3519 your program generates large amounts of parallelism initially.
3524 <term><option>-Q<num></option>:</term>
3527 <indexterm><primary>-Q<num> RTS option (parallel)</primary></indexterm>
3528 (PARALLEL ONLY) Set the size of packets transmitted between processors
3529 to <literal><num></literal>. The default is 1024 words. A larger number may be
3530 appropriate if your machine has a high communication cost relative to
3548 ;;; Local Variables: ***
3550 ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***