9957c9b73e3622b847294e07cd201d636da22de2
[ghc-hetmet.git] / ghc / docs / users_guide / using.sgml
1 <Chapter id="using-GHC">
2 <Title>Using GHC
3 </Title>
4
5 <para>
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.
14 </para>
15
16 <Sect1 id="command-line-structure">
17 <Title>Overall command-line structure
18 </Title>
19
20 <para>
21 <IndexTerm><Primary>structure, command-line</Primary></IndexTerm>
22 <IndexTerm><Primary>command-line structure</Primary></IndexTerm>
23 </para>
24
25 <para>
26 An invocation of GHC takes the following form:
27 </para>
28
29 <para>
30
31 <Screen>
32 ghc [argument...]
33 </Screen>
34
35 </para>
36
37 <para>
38 Command-line arguments are either options or file names.
39 </para>
40
41 <para>
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.)
50 </para>
51
52 </Sect1>
53
54 <Sect1 id="file-suffixes">
55 <Title>Meaningful file suffixes
56 </Title>
57
58 <para>
59 <IndexTerm><Primary>suffixes, file</Primary></IndexTerm>
60 <IndexTerm><Primary>file suffixes for GHC</Primary></IndexTerm>
61 </para>
62
63 <para>
64 File names with &ldquo;meaningful&rdquo; suffixes (e.g., <Filename>.lhs</Filename> or <Filename>.o</Filename>)
65 cause the &ldquo;right thing&rdquo; to happen to those files.
66 </para>
67
68 <para>
69 <VariableList>
70
71 <VarListEntry>
72 <Term><Filename>.lhs</Filename>:</Term>
73 <ListItem>
74 <para>
75 <IndexTerm><Primary>lhs suffix</Primary></IndexTerm>
76 A &ldquo;literate Haskell&rdquo; module.
77 </para>
78 </ListItem>
79 </VarListEntry>
80 <VarListEntry>
81 <Term><Filename>.hs</Filename>:</Term>
82 <ListItem>
83 <para>
84 A not-so-literate Haskell module.
85 </para>
86 </ListItem>
87 </VarListEntry>
88 <VarListEntry>
89 <Term><Filename>.hi</Filename>:</Term>
90 <ListItem>
91 <para>
92 A Haskell interface file, probably compiler-generated.
93 </para>
94 </ListItem>
95 </VarListEntry>
96 <VarListEntry>
97 <Term><Filename>.hc</Filename>:</Term>
98 <ListItem>
99 <para>
100 Intermediate C file produced by the Haskell compiler.
101 </para>
102 </ListItem>
103 </VarListEntry>
104 <VarListEntry>
105 <Term><Filename>.c</Filename>:</Term>
106 <ListItem>
107 <para>
108 A C&nbsp;file not produced by the Haskell compiler.
109 </para>
110 </ListItem>
111 </VarListEntry>
112 <VarListEntry>
113 <Term><Filename>.s</Filename>:</Term>
114 <ListItem>
115 <para>
116 An assembly-language source file, usually
117 produced by the compiler.
118 </para>
119 </ListItem>
120 </VarListEntry>
121 <VarListEntry>
122 <Term><Filename>.o</Filename>:</Term>
123 <ListItem>
124 <para>
125 An object file, produced by an assembler.
126 </para>
127 </ListItem>
128 </VarListEntry>
129 </VariableList>
130 </para>
131
132 <para>
133 Files with other suffixes (or without suffixes) are passed straight
134 to the linker.
135 </para>
136
137 </Sect1>
138
139 <Sect1 id="options-help">
140 <Title>Help and verbosity options
141 </Title>
142
143 <para>
144 <IndexTerm><Primary>help options (GHC)</Primary></IndexTerm>
145 <IndexTerm><Primary>verbose option (GHC)</Primary></IndexTerm>
146 </para>
147
148     <informaltable>
149       <tgroup cols=3 align=left colsep=1 rowsep=1>
150         <thead>
151           <row>
152             <entry>Flag</entry>
153             <entry>Description</entry>
154             <entry>Static/Dynamic</entry>
155             <entry>Reverse</entry>
156           </row>
157         </thead>
158         <tbody>
159         <row>
160           <entry><literal>-?</literal></entry>
161           <entry>help</entry>
162           <entry>static</entry>
163           <entry>-</entry>
164         </row>
165         <row>
166           <entry><literal>-help</literal></entry>
167           <entry>help</entry>
168           <entry>static</entry>
169           <entry>-</entry>
170         </row>
171         <row>
172           <entry><literal>-v</literal></entry>
173           <entry>verbose mode (equivalent to <literal>-v3</literal>)</entry>
174           <entry>dynamic</entry>
175           <entry>-</entry>
176         </row>
177         <row>
178           <entry><literal>-v</literal><emphasis>n</emphasis></entry>
179           <entry>set verbosity level</entry>
180           <entry>dynamic</entry>
181           <entry>-</entry>
182         </row>
183         <row>
184           <entry><literal>--version</literal></entry>
185           <entry>display GHC version</entry>
186           <entry>static</entry>
187           <entry>-</entry>
188         </row>
189         <row>
190           <entry><literal>--numeric-version</literal></entry>
191           <entry>display GHC version (numeric only)</entry>
192           <entry>static</entry>
193           <entry>-</entry>
194         </row>
195         </tbody>
196       </tgroup>
197     </informaltable>
198
199
200     <variablelist>
201       <varlistentry>
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>
206         <listitem>
207           <para>Cause GHC to spew a long usage message to standard
208           output and then exit.</para>
209         </listitem>
210       </varlistentry>
211
212       <varlistentry>
213         <term><literal>-v</literal></term>
214         <indexterm><primary><literal>-v</literal></primary></indexterm>
215         <listitem>
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>
222
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
226           verify.</para>
227         </listitem>
228       </varlistentry>
229         
230       <varlistentry>
231         <term><literal>-v</literal><emphasis>n</emphasis></term>
232         <indexterm><primary><literal>-v</literal><emphasis>n</emphasis></primary></indexterm>
233         <listitem>
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>
239           
240           <variablelist>
241             <varlistentry>
242               <term><literal>-v0</literal></term>
243               <listitem>
244                 <para>Disable all non-essential messages (this is the
245                 default).</para>
246               </listitem>
247             </varlistentry>
248
249             <varlistentry>
250               <term><literal>-v1</literal></term>
251               <listitem>
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>
256               </listitem>
257             </varlistentry>
258
259             <varlistentry>
260               <term><literal>-v2</literal></term>
261               <listitem>
262                 <para>Print the name of each compilation phase as it
263                 is executed. (equivalent to
264                 <option>-dshow-passes</option>).</para>
265               </listitem>
266             </varlistentry>
267
268             <varlistentry>
269               <term><literal>-v3</literal></term>
270               <listitem>
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>
274               </listitem>
275             </varlistentry>
276
277             <varlistentry>
278               <term><literal>-v4</literal></term>
279               <listitem>
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>
284               </listitem>
285             </varlistentry>
286           </variablelist>
287         </listitem>
288       </varlistentry>
289       
290       <varlistentry>
291         <term><literal>--version</literal></term>
292         <indexterm><primary><literal>--version</literal></primary></indexterm>
293         <listitem>
294           <para>Print a one-line string including GHC's version number.</para>
295         </listitem>
296       </varlistentry>
297
298       <varlistentry>
299         <term><literal>--numeric-version</literal></term>
300         <indexterm><primary><literal>--numeric-version</literal></primary></indexterm>
301         <listitem>
302           <para>Print GHC's numeric version number only.</para>
303         </listitem>
304       </varlistentry>
305     </variablelist>
306   </sect1>
307     
308   <Sect1 id="options-order">
309     <title>Running the right phases in the right order</title>
310
311     <indexterm><primary>order of passes in GHC</primary></indexterm>
312     <indexterm><primary>pass ordering in GHC</primary></indexterm>
313
314     <informaltable>
315       <tgroup cols=3 align=left colsep=1 rowsep=1>
316         <thead>
317           <row>
318             <entry>Flag</entry>
319             <entry>Description</entry>
320             <entry>Static/Dynamic</entry>
321             <entry>Reverse</entry>
322           </row>
323         </thead>
324         <tbody>
325           <row>
326             <entry><literal>-cpp</literal></entry>
327             <entry>Run the C pre-processor on the Haskell source</entry>
328             <entry>dynamic</entry>
329             <entry>-</entry>
330           </row>
331           <row>
332             <entry><literal>-E</literal></entry>
333             <entry>Stop after preprocessing (<literal>.hspp</literal> file)</entry>
334             <entry>static</entry>
335             <entry>-</entry>
336           </row>
337           <row>
338             <entry><literal>-C</literal></entry>
339             <entry>Stop after generating C (<literal>.hc</literal> file)</entry>
340             <entry>static</entry>
341             <entry>-</entry>
342           </row>
343           <row>
344             <entry><literal>-S</literal></entry>
345             <entry>Stop after generating assembly (<literal>.s</literal> file)</entry>
346             <entry>static</entry>
347             <entry>-</entry>
348           </row>
349           <row>
350             <entry><literal>-c</literal></entry>
351             <entry>Stop after compiling to object code (<literal>.o</literal> file)</entry>
352             <entry>static</entry>
353             <entry>-</entry>
354           </row>
355         </tbody>
356       </tgroup>
357     </informaltable>
358
359     <para>The basic task of the <Command>ghc</Command> driver is to
360     run each input file through the right phases (compiling, linking,
361     etc.).</para>
362
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>
367
368 <InformalTable>
369 <TGroup Cols="4">
370 <ColSpec Align="Left">
371 <ColSpec Align="Left">
372 <ColSpec Align="Left">
373 <ColSpec Align="Left">
374 <TBody>
375
376 <Row>
377 <Entry>Phase of the compilation system</Entry>
378 <Entry>Suffix saying &ldquo;start here&rdquo;</Entry>
379 <Entry>Flag saying &ldquo;stop after&rdquo;</Entry>
380 <Entry>(suffix of) output file</Entry>
381 </Row>
382
383 <Row>
384 <Entry>
385 literate pre-processor </Entry>
386 <Entry> .lhs </Entry>
387 <Entry> - </Entry>
388 <Entry> .hs </Entry>
389 </Row>
390 <Row>
391 <Entry>
392 C pre-processor (opt.) </Entry>
393 <Entry> .hs (with <literal>-cpp</literal>) </Entry>
394 <Entry> -E </Entry>
395 <Entry> .hspp </Entry>
396 </Row>
397 <Row>
398 <Entry>
399 Haskell compiler </Entry>
400 <Entry> .hs </Entry>
401 <Entry> -C, -S </Entry>
402 <Entry> .hc, .s </Entry>
403 </Row>
404 <Row>
405 <Entry>
406 C compiler (opt.) </Entry>
407 <Entry> .hc or .c </Entry>
408 <Entry> -S </Entry>
409 <Entry> .s </Entry>
410 </Row>
411 <Row>
412 <Entry>
413 assembler </Entry>
414 <Entry> .s </Entry>
415 <Entry> -c </Entry>
416 <Entry> .o </Entry>
417 </Row>
418 <Row>
419 <Entry>
420 linker </Entry>
421 <Entry> other </Entry>
422 <Entry> - </Entry>
423 <Entry> a.out </Entry>
424 </Row>
425 </TBody>
426 </TGroup>
427 </InformalTable>
428
429 <IndexTerm><Primary>-C option</Primary></IndexTerm>
430 <IndexTerm><Primary>-S option</Primary></IndexTerm>
431 <IndexTerm><Primary>-c option</Primary></IndexTerm>
432
433 <para>
434 Thus, a common invocation would be: <Command>ghc -c Foo.hs</Command>
435 </para>
436
437 <para>
438 Note: What the Haskell compiler proper produces depends on whether a
439 native-code generator is used (producing assembly language) or not
440 (producing C).
441 </para>
442     
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>
452
453   </sect1>
454
455   <sect1 id="options-output">
456     <title>Re-directing the compilation output(s)</title>
457
458     <indexterm><primary>output-directing options</primary></indexterm>
459     <indexterm><primary>redirecting compilation output</primary></indexterm>
460
461     <informaltable>
462       <tgroup cols=3 align=left colsep=1 rowsep=1>
463         <thead>
464           <row>
465             <entry>Flag</entry>
466             <entry>Description</entry>
467             <entry>Static/Dynamic</entry>
468             <entry>Reverse</entry>
469           </row>
470         </thead>
471         <tbody>
472         <row>
473           <entry><literal>-hisuf</literal></entry>
474           <entry>set the suffix to use for interface files</entry>
475           <entry>static</entry>
476           <entry>-</entry>
477         </row>
478         <row>
479           <entry><literal>-o</literal></entry>
480           <entry>set output filename</entry>
481           <entry>static</entry>
482           <entry>-</entry>
483         </row>
484         <row>
485           <entry><literal>-odir</literal></entry>
486           <entry>set output directory</entry>
487           <entry>static</entry>
488           <entry>-</entry>
489         </row>
490         <row>
491           <entry><literal>-ohi</literal></entry>
492           <entry>set the filename in which to put the interface</entry>
493           <entry>static</entry>
494           <entry></entry>
495         </row>
496         <row>
497           <entry><literal>-osuf</literal></entry>
498           <entry>set the output file suffix</entry>
499           <entry>static</entry>
500           <entry>-</entry>
501         </row>
502         </tbody>
503       </tgroup>
504     </informaltable>
505
506     <variablelist>
507       <varlistentry>
508         <term><literal>-o</literal></term>
509         <indexterm><primary><literal>-o</literal></primary></indexterm>
510         <listitem>
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>
517
518           <para>Note: this &ldquo;feature&rdquo; 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>
522         </listitem>
523       </varlistentry>
524
525       <varlistentry>
526         <term><literal>-odir</literal></term>
527         <indexterm><primary><literal>-odir</literal></primary></indexterm>
528         <listitem>
529           <para>The <option>-o</option> option isn't of much use if
530           you have <Emphasis>several</Emphasis> input files&hellip;
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 &lt;dir&gt;</option><IndexTerm><Primary>-odir
535           &lt;dir&gt; option</Primary></IndexTerm> (the &ldquo;Oh,
536           dear&rdquo; option).  For example:</para>
537
538 <Screen>
539 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
540 </Screen>
541
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>
549
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>
556         </listitem>
557       </varlistentry>
558
559       <varlistentry>
560         <term><literal>-ohi</literal></term>
561         <indexterm><primary><literal>-ohi</literal></primary></indexterm>
562         <listitem>
563           <para></para>
564         </listitem>
565       </varlistentry>
566
567       <varlistentry>
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>
572         <listitem>
573           <para>EXOTICA: The <option>-osuf
574           &lt;suffix&gt;</option><IndexTerm><Primary>-osuf
575           &lt;suffix&gt; 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
578           prelude.).</para>
579
580           <para>Similarly, the <option>-hisuf
581           &lt;suffix&gt;</option><IndexTerm><Primary>-hisuf
582           &lt;suffix&gt; option</Primary></IndexTerm> will change the
583           <Filename>.hi</Filename> file suffix for non-system
584           interface files (see <XRef LinkEnd="hi-options">).</para>
585
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&lowbar;hi -osuf
591           g&lowbar;o</option> to your <Command>make</Command> rule for
592           GHC compiling&hellip;</para>
593         </listitem>
594       </varlistentry>
595     </variablelist>
596         
597     <sect2 id="keeping-intermediates">
598       <title>Keeping Intermediate Files</title>
599       <indexterm><primary>intermediate files, saving</primary>
600       </indexterm>
601       <indexterm><primary><literal>.hc</literal> files, saving</primary>
602       </indexterm>
603       <indexterm><primary><literal>.s</literal> files, saving</primary>
604       </indexterm>
605
606       <informaltable>
607         <tgroup cols=3 align=left colsep=1 rowsep=1>
608           <thead>
609             <row>
610               <entry>Flag</entry>
611               <entry>Description</entry>
612               <entry>Static/Dynamic</entry>
613               <entry>Reverse</entry>
614             </row>
615           </thead>
616           <tbody>
617             <row>
618               <entry><literal>-keep-hc-file</literal></entry>
619               <entry>retain intermediate <literal>.hc</literal> files</entry>
620               <entry>static</entry>
621               <entry>-</entry>
622             </row>
623             <row>
624               <entry><literal>-keep-s-file</literal></entry>
625               <entry>retain intermediate <literal>.s</literal> files</entry>
626               <entry>static</entry>
627               <entry>-</entry>
628             </row>
629             <row>
630               <entry><literal>-keep-raw-s-file</literal></entry>
631               <entry>retain intermediate <literal>.raw_s</literal> files</entry>
632               <entry>static</entry>
633               <entry>-</entry>
634             </row>
635             <row>
636               <entry><literal>-keep-tmp-files</literal></entry>
637               <entry>retain all intermediate temporary files</entry>
638               <entry>static</entry>
639               <entry>-</entry>
640             </row>
641           </tbody>
642         </tgroup>
643       </informaltable>
644
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>
648
649       <variablelist>
650         <varlistentry>
651           <term><literal>-keep-hc-files</literal></term>
652           <indexterm>
653             <primary><literal>-keep-hc-files</literal></primary>
654           </indexterm>
655           <listitem>
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>
662           </listitem>
663         </varlistentry>
664
665         <varlistentry>
666           <term><literal>-keep-s-files</literal></term>
667           <indexterm>
668             <primary><literal>-keep-s-files</literal></primary>
669           </indexterm>
670           <listitem>
671             <para>Keep intermediate <literal>.s</literal> files.</para>
672           </listitem>
673         </varlistentry>
674
675         <varlistentry>
676           <term><literal>-keep-raw-s-files</literal></term>
677           <indexterm>
678             <primary><literal>-keep-raw-s-files</literal></primary>
679           </indexterm>
680           <listitem>
681             <para>Keep intermediate <literal>.raw-s</literal> files.
682             These are the direct output from the C compiler, before
683             GHC does &ldquo;assembly mangling&rdquo; to produce the
684             <literal>.s</literal> file.  Again, these are not produced
685             when using the native code generator.</para>
686           </listitem>
687         </varlistentry>
688
689         <varlistentry>
690           <term><literal>-keep-tmp-files</literal></term>
691           <indexterm>
692             <primary><literal>-keep-tmp-files</literal></primary>
693           </indexterm>
694           <indexterm>
695             <primary>temporary files</primary>
696             <secondary>keeping</secondary>
697           </indexterm>
698           <listitem>
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>
705           </listitem>
706         </varlistentry>
707       </variablelist>
708     </sect2>
709
710     <sect2 id="temp-files">
711       <title>Redirecting temporary files</title>
712
713       <indexterm>
714         <primary>temporary files</primary>
715         <secondary>redirecting</secondary>
716       </indexterm>
717
718       <informaltable>
719         <tgroup cols=3 align=left colsep=1 rowsep=1>
720           <thead>
721             <row>
722               <entry>Flag</entry>
723               <entry>Description</entry>
724               <entry>Static/Dynamic</entry>
725               <entry>Reverse</entry>
726             </row>
727           </thead>
728           <tbody>
729             <row>
730               <entry><literal>-tmpdir</literal></entry>
731               <entry>set the directory for temporary files</entry>
732               <entry>static</entry>
733               <entry>-</entry>
734             </row>
735           </tbody>
736         </tgroup>
737       </informaltable>
738
739       <variablelist>
740         <varlistentry>
741           <term><literal>-tmpdir</literal></term>
742           <indexterm><primary><literal>-tmpdir</literal></primary></indexterm>
743           <listitem>
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             &lt;dir&gt;</option><IndexTerm><Primary>-tmpdir
749             &lt;dir&gt; 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>
753
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>
760
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>
766           </listitem>
767         </varlistentry>
768       </variablelist>
769     </sect2>
770
771   </sect1>
772
773   <sect1 id="options-sanity">
774     <title>Warnings and sanity-checking</title>
775
776     <indexterm><primary>sanity-checking options</primary></indexterm>
777     <indexterm><primary>warnings</primary></indexterm>
778
779     <informaltable>
780       <tgroup cols=3 align=left colsep=1 rowsep=1>
781         <thead>
782           <row>
783             <entry>Flag</entry>
784             <entry>Description</entry>
785             <entry>Static/Dynamic</entry>
786             <entry>Reverse</entry>
787           </row>
788         </thead>
789         <tbody>
790           <row>
791             <entry><literal>-W</literal></entry>
792             <entry>enable normal warnings</entry>
793             <entry>static</entry>
794             <entry><literal>-w</literal></entry>
795           </row>
796           <row>
797             <entry><literal>-w</literal></entry>
798             <entry>disable all warnings</entry>
799             <entry>static</entry>
800             <entry>-</entry>
801           </row>
802           <row>
803             <entry><literal>-Wall</literal></entry>
804             <entry>enable all warnings</entry>
805             <entry>static</entry>
806             <entry><literal>-w</literal></entry>
807           </row>
808
809           <row>
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>
814           </row>
815
816           <row>
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>
821           </row>
822
823           <row>
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>
829           </row>
830
831           <row>
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>
836           </row>
837
838           <row>
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>
843           </row>
844
845           <row>
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>
850           </row>
851
852           <row>
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>
857           </row>
858
859           <row>
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>
864           </row>
865
866           <row>
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>
871           </row>
872
873           <row>
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>
878           </row>
879
880           <row>
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>
885           </row>
886
887           <row>
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>
892           </row>
893
894           <row>
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>
899           </row>
900
901           <row>
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>
906           </row>
907
908         </tbody>
909       </tgroup>
910     </informaltable>
911
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
916     program.  These are:
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 &ldquo;packages&rdquo; of warnings:
923     </para>
924
925     <VariableList>
926
927       <varlistentry>
928         <term><option>-W</option>:</term>
929         <listitem>
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>
936         </listitem>
937       </varlistentry>
938
939       <varlistentry>
940         <term><option>-w</option>:</term>
941         <listitem>
942           <IndexTerm><Primary>-w option</Primary></IndexTerm>
943           <para>Turns off all warnings, including the standard ones.</para>
944         </listitem>
945       </varlistentry>
946
947       <varlistentry>
948         <term><option>-Wall</option>:</term>
949         <listitem>
950           <indexterm><primary>-Wall option</primary></indexterm>
951           <para>Turns on all warning options.</para>
952         </listitem>
953       </varlistentry>
954
955     </variablelist>
956
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>
960
961     <variablelist>
962
963       <varlistentry>
964         <term><option>-fwarn-deprecations</option>:</term>
965         <listitem>
966           <indexterm><primary><option>-fwarn-deprecations</option></primary>
967           </indexterm>
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>
973         </listitem>
974       </varlistentry>
975
976       <varlistentry>
977         <term><option>-fwarn-duplicate-exports</option>:</term>
978         <listitem>
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>
982
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>
988
989           <para>This option is on by default.</para>
990         </listitem>
991       </varlistentry>
992
993       <varlistentry>
994         <term><option>-fwarn-hi-shadowing</option>:</term>
995         <listitem>
996           <indexterm><primary>-fwarn-hi-shadowing option</primary></indexterm>
997           <indexterm><primary>shadowing</primary>
998             <secondary>interface files</secondary></indexterm>
999
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
1003           directory.</para>
1004         </listitem>
1005       </varlistentry>
1006
1007       <varlistentry>
1008         <term><option>-fwarn-incomplete-patterns</option>:</term>
1009         <listitem>
1010           <indexterm><primary>-fwarn-incomplete-patterns option</primary></indexterm>
1011           <indexterm><primary>incomplete patterns, warning</primary></indexterm>
1012           <indexterm><primary>patterns, incomplete</primary></indexterm>
1013
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
1018           enabled.</para>
1019
1020 <programlisting>
1021 g [] = 2
1022 </programlisting>
1023
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>
1028         </listitem>
1029       </varlistentry>
1030
1031       <varlistentry>
1032         <term><option>-fwarn-missing-fields</option>:</term>
1033         <listitem>
1034           <indexterm><primary>-fwarn-missing-fields option</primary></indexterm>
1035           <indexterm><primary>missing fields, warning</primary></indexterm>
1036           <indexterm><primary>fields, missing</primary></indexterm>
1037
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>
1043         </listitem>
1044       </varlistentry>
1045
1046       <varlistentry>
1047         <term><option>-fwarn-missing-methods</option>:</term>
1048         <listitem>
1049           <indexterm><primary>-fwarn-missing-methods option</primary></indexterm>
1050           <indexterm><primary>missing methods, warning</primary></indexterm>
1051           <indexterm><primary>methods, missing</primary></indexterm>
1052
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>
1057         </listitem>
1058       </varlistentry>
1059
1060       <varlistentry>
1061         <term><option>-fwarn-missing-signatures</option>:</term>
1062         <listitem>
1063           <indexterm><primary>-fwarn-missing-signatures option</primary></indexterm>
1064           <indexterm><primary>type signatures, missing</primary></indexterm>
1065
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>
1070         </listitem>
1071       </varlistentry>
1072
1073       <varlistentry>
1074         <term><option>-fwarn-name-shadowing</option>:</term>
1075         <listitem>
1076           <indexterm><primary>-fwarn-name-shadowing option</primary></indexterm>
1077           <indexterm><primary>shadowing, warning</primary></indexterm>
1078           
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>
1085
1086           <para>Consequently, this option does
1087           <emphasis>will</emphasis> complain about cyclic recursive
1088           definitions.</para>
1089         </listitem>
1090       </varlistentry>
1091
1092       <varlistentry>
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>
1097         <listitem>
1098           <para>By default, the compiler will warn you if a set of
1099           patterns are overlapping, i.e.,</para>
1100
1101 <programlisting>
1102 f :: String -&#62; Int
1103 f []     = 0
1104 f (_:xs) = 1
1105 f "2"    = 2
1106 </programlisting>
1107
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>
1112         </listitem>
1113       </varlistentry>
1114
1115       <varlistentry>
1116         <term><option>-fwarn-simple-patterns</option>:</term>
1117         <listitem>
1118           <indexterm><primary><option>-fwarn-simple-patterns</option></primary>
1119           </indexterm>
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>
1124         </listitem>
1125       </varlistentry>
1126
1127       <varlistentry>
1128         <term><option>-fwarn-type-defaults</option>:</term>
1129         <listitem>
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>
1142
1143           <para>This warning is off by default.</para>
1144         </listitem>
1145       </varlistentry>
1146
1147       <varlistentry>
1148         <term><option>-fwarn-unused-binds</option>:</term>
1149         <listitem>
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>
1156         </listitem>
1157       </varlistentry>
1158
1159       <varlistentry>
1160         <term><option>-fwarn-unused-imports</option>:</term>
1161         <listitem>
1162           <indexterm><primary>-fwarn-unused-imports option</primary></indexterm>
1163           <indexterm><primary>unused imports, warning</primary></indexterm>
1164           <indexterm><primary>imports, unused</primary></indexterm>
1165
1166           <para>Report any objects that are explicitly imported but
1167           never used.</para>
1168         </listitem>
1169       </varlistentry>
1170
1171       <varlistentry>
1172         <term><option>-fwarn-unused-matches</option>:</term>
1173         <listitem>
1174           <indexterm><primary>-fwarn-unused-matches option</primary></indexterm>
1175           <indexterm><primary>unused matches, warning</primary></indexterm>
1176           <indexterm><primary>matches, unused</primary></indexterm>
1177
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>
1184         </listitem>
1185       </varlistentry>
1186
1187     </VariableList>
1188
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>
1194
1195   </sect1>
1196
1197 <Sect1 id="separate-compilation">
1198 <Title>Separate compilation
1199 </Title>
1200
1201 <para>
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.
1206 </para>
1207
1208 <Sect2 id="hi-files">
1209 <Title>Interface files
1210 </Title>
1211
1212 <para>
1213 <indexterm><primary>interface files</primary></indexterm>
1214 <indexterm><primary>.hi files</primary></indexterm>
1215 </para>
1216
1217 <para>
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.
1223 </para>
1224
1225 <para>
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.
1233 </para>
1234
1235 <para>
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>.
1240 </para>
1241
1242 <para>
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>&mdash;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.
1248 </para>
1249
1250 <para>
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>
1261 </para>
1262
1263 <para>
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.
1269 </para>
1270
1271 </sect2>
1272
1273 <Sect2 id="options-finding-imports">
1274 <Title>Finding interface files
1275 </Title>
1276
1277 <para>
1278 <indexterm><primary>interface files, finding them</primary></indexterm>
1279 <indexterm><primary>finding interface files</primary></indexterm>
1280 </para>
1281
1282 <para>
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
1286 it looks.
1287 </para>
1288
1289 <para>
1290 <VariableList>
1291
1292 <varlistentry>
1293 <term><option>-i&lt;dirs&gt;</option></term>
1294 <listitem>
1295 <para>
1296 <indexterm><primary>-i&lt;dirs&gt; option</primary></indexterm>This flag
1297 prepends a colon-separated list of <Filename>dirs</Filename> to the &ldquo;import
1298 directories&rdquo; list.
1299 See also <XRef LinkEnd="recomp"> for the significance of using
1300 relative and absolute pathnames in the <option>-i</option> list.
1301 </para>
1302 </listitem>
1303 </varlistentry>
1304
1305 <varlistentry>
1306 <term><option>-i</option></term>
1307 <listitem>
1308 <para>
1309 resets the &ldquo;import directories&rdquo; list back to nothing.
1310 </para>
1311 </listitem>
1312 </varlistentry>
1313
1314 <varlistentry>
1315 <term><option>-fno-implicit-prelude</option></term>
1316 <listitem>
1317 <para>
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.)
1323 </para>
1324 <para>
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.
1330 </para>
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:
1335 <ItemizedList>
1336 <listitem>
1337 <para>
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.
1340 </para>
1341 </listitem>
1342 <listitem>
1343 <para>
1344 Negation (e.g. "<literal>- (f x)</literal>") means "<literal>negate (f x)</literal>" (not <literal>Prelude.negate</literal>).
1345 </para>
1346 </listitem>
1347 <listitem>
1348 <para>
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>").
1352 </para>
1353 </listitem>
1354 </ItemizedList>
1355 </para>
1356 </listitem>
1357 </varlistentry>
1358
1359 <varlistentry>
1360 <term><option>-I&lt;dir&gt;</option></term>
1361 <listitem>
1362 <para>
1363 <indexterm><primary>-I&lt;dir&gt; 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&hellip;)  For this purpose, use a <option>-I</option>
1368 option in the usual C-ish way.
1369 </para>
1370 </listitem>
1371 </varlistentry>
1372
1373 </VariableList>
1374 </para>
1375
1376 </sect2>
1377
1378 <Sect2 id="hi-options">
1379 <Title>Other options related to interface files
1380 </Title>
1381
1382 <para>
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 &lt;file&gt; option</primary></indexterm> (not recommended).
1387 </para>
1388
1389 <para>
1390 To avoid generating an interface file at all, use a <option>-nohi</option>
1391 option.<indexterm><primary>-nohi option</primary></indexterm>
1392 </para>
1393
1394 <para>
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.
1402 </para>
1403
1404 <para>
1405 The <Filename>.hi</Filename> files from GHC contain &ldquo;usage&rdquo; 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>
1409 option.
1410 </para>
1411
1412 <para>
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.
1419 </para>
1420
1421 <para>
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>
1429 </para>
1430
1431 <para>
1432 See also <XRef LinkEnd="options-linker">, which describes how the linker finds standard
1433 Haskell libraries.
1434 </para>
1435
1436 </sect2>
1437
1438 <Sect2 id="recomp">
1439 <Title>The recompilation checker
1440 </Title>
1441
1442 <indexterm><primary>recompilation checker</primary></indexterm>
1443
1444 <para>
1445 <variablelist>
1446 <varlistentry>
1447 <term><option>-recomp</option></term>
1448 <indexterm><primary><option>-recomp</option> option</primary></indexterm>
1449 <listitem>
1450 <para>
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
1454 recompiled.
1455 </para>
1456 </listitem>
1457 </varlistentry>
1458 <varlistentry>
1459 <term><option>-no-recomp</option></term>
1460 <indexterm><primary><option>-recomp</option> option</primary></indexterm>
1461 <listitem>
1462 <para>
1463 Turn off recompilation checking.
1464 </para>
1465 </listitem>
1466 </varlistentry>
1467 </VariableList>
1468 </para>
1469
1470 <para>
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.
1476 </para>
1477
1478 <para>
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
1486 now&hellip;
1487 </para>
1488
1489 <para>
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 &ldquo;Compilation IS NOT required&rdquo;.  What a beautiful
1502 sight!
1503 </para>
1504
1505 <para>
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.
1508 </para>
1509
1510 </sect2>
1511
1512
1513 <Sect2 id="using-make">
1514 <Title>Using <Command>make</Command>
1515 </Title>
1516
1517 <para>
1518 <indexterm><primary><literal>make</literal></primary></indexterm>
1519 </para>
1520
1521 <para>
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.
1523 Thus:
1524 </para>
1525
1526 <para>
1527
1528 <ProgramListing>
1529 HC      = ghc
1530 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
1531
1532 SRCS = Main.lhs Foo.lhs Bar.lhs
1533 OBJS = Main.o   Foo.o   Bar.o
1534
1535 .SUFFIXES : .o .hs .hi .lhs .hc .s
1536
1537 cool_pgm : $(OBJS)
1538         rm $@
1539         $(HC) -o $@ $(HC_OPTS) $(OBJS)
1540
1541 # Standard suffix rules
1542 .o.hi:
1543         @:
1544
1545 .lhs.o:
1546         $(HC) -c $&#60; $(HC_OPTS)
1547
1548 .hs.o:
1549         $(HC) -c $&#60; $(HC_OPTS)
1550
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
1554 </ProgramListing>
1555
1556 </para>
1557
1558 <para>
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
1561 comprehensible:
1562 </para>
1563
1564 <para>
1565
1566 <ProgramListing>
1567 %.o : %.lhs
1568         $(HC) -c $&#60; $(HC_OPTS)
1569 </ProgramListing>
1570
1571 </para>
1572
1573 <para>
1574 What we've shown should work with any <Command>make</Command>.)
1575 </para>
1576
1577 <para>
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&hellip;nothing.  Which is true.
1581 </para>
1582
1583 <para>
1584 Note the inter-module dependencies at the end of the Makefile, which
1585 take the form
1586 </para>
1587
1588 <para>
1589
1590 <ProgramListing>
1591 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
1592 </ProgramListing>
1593
1594 </para>
1595
1596 <para>
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
1601 nicely.
1602 </para>
1603
1604     </sect2>
1605
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>
1610
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>
1616
1617 <ProgramListing>
1618 depend :
1619         ghc -M $(HC_OPTS) $(SRCS)
1620 </ProgramListing>
1621
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&lowbar;pgm</Command>.  <Command>ghc -M</Command> will append
1626       the needed dependencies to your
1627       <Filename>Makefile</Filename>.</para>
1628
1629       <para>In general, if module <literal>A</literal> contains the
1630       line
1631
1632 <programlisting>
1633 import B ...blah...
1634 </programlisting>
1635
1636        then <command>ghc -M</command> will generate a dependency
1637        line of the form:
1638
1639 <programlisting>
1640 A.o : B.hi
1641 </programlisting>
1642
1643        If module <literal>A</literal> contains the line 
1644
1645 <programlisting>
1646 import {-# SOURCE #-} B ...blah...
1647 </programlisting>
1648
1649        then <command>ghc -M</command> will generate a dependency
1650        line of the form:
1651
1652 <programlisting>
1653 A.o : B.hi-boot
1654 </programlisting>
1655
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
1659        target.</para>
1660
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>&num; DO NOT DELETE: Beginning of Haskell
1666       dependencies</literal>" and "<literal>&num; 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>
1670
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>
1679
1680 <screen>
1681 ghc -M -optdep-f optdep.depend ...
1682 </screen>
1683       
1684       <para>The full list of options accepted by
1685       <command>mkdependHS</command> is:</para>
1686
1687       <variablelist>
1688
1689         <varlistentry>
1690           <term><option>-w</option></term>
1691           <listitem>
1692             <para>Turn off warnings about interface file shadowing.</para>
1693           </listitem>
1694         </varlistentry>
1695
1696         <varlistentry>
1697           <term><option>-f blah</option></term>
1698           <listitem>
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>
1709           </listitem>
1710         </varlistentry>
1711
1712         <varlistentry>
1713           <term><option>-o &lt;osuf&gt;</option></term>
1714           <listitem>
1715             <para>Use <Filename>.&lt;osuf&gt;</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>
1721           </listitem>
1722         </varlistentry>
1723
1724         <varlistentry>
1725           <term><option>-s &lt;suf&gt;</option></term>
1726           <listitem>
1727             <para>Make extra dependencies that declare that files with
1728             suffix
1729             <Filename>.&lt;suf&gt;&lowbar;&lt;osuf&gt;</Filename>
1730             depend on interface files with suffix
1731             <Filename>.&lt;suf&gt;&lowbar;hi</Filename>, or (for
1732             <literal>&lcub;-&num; SOURCE &num;-&rcub;</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&lowbar;hc</Filename> on
1738             <Filename>.a&lowbar;hi</Filename>, and
1739             <Filename>.b&lowbar;hc</Filename> on
1740             <Filename>.b&lowbar;hi</Filename>.  (Useful in conjunction
1741             with NoFib "ways".)</para>
1742           </listitem>
1743         </varlistentry>
1744
1745         <varlistentry>
1746           <term><option>--exclude-module=&lt;file&gt;</option></term>
1747           <listitem>
1748             <para>Regard <Filename>&lt;file&gt;</Filename> as
1749             "stable"; i.e., exclude it from having dependencies on
1750             it.</para>
1751           </listitem>
1752         </varlistentry>
1753
1754         <varlistentry>
1755           <term><option>-x</option></term>
1756           <listitem>
1757             <para>same as <option>--exclude-module</option></para>
1758           </listitem>
1759         </varlistentry>
1760
1761         <varlistentry>
1762           <term><option>--exclude-directory=&lt;dirs&gt;</option></term>
1763           <listitem>
1764             <para>Regard the colon-separated list of directories
1765             <Filename>&lt;dirs&gt;</Filename> as containing stable,
1766             don't generate any dependencies on modules therein.</para>
1767           </listitem>
1768         </varlistentry>
1769
1770         <varlistentry>
1771           <term><option>-xdirs</option></term>
1772           <listitem>
1773             <para>same as <option>--exclude-directory</option>.</para>
1774           </listitem>
1775         </varlistentry>
1776
1777         <varlistentry>
1778           <term><option>--include-module=&lt;file&gt;</option></term>
1779           <listitem>
1780             <para>Regard <Filename>&lt;file&gt;</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>
1784           </listitem>
1785         </varlistentry>
1786
1787         <varlistentry>
1788           <term><option>--include-prelude</option></term>
1789           <listitem>
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>
1796           </listitem>
1797         </varlistentry>
1798       </variablelist>
1799
1800 </sect2>
1801
1802 <Sect2 id="mutual-recursion">
1803 <Title>How to compile mutually recursive modules
1804 </Title>
1805
1806 <para>
1807 <indexterm><primary>module system, recursion</primary></indexterm>
1808 <indexterm><primary>recursion, between modules</primary></indexterm>
1809 </para>
1810
1811 <para>
1812 Currently, the compiler does not have proper support for dealing with
1813 mutually recursive modules:
1814 </para>
1815
1816 <para>
1817
1818 <ProgramListing>
1819 module A where
1820
1821 import B
1822
1823 newtype TA = MkTA Int
1824
1825 f :: TB -&#62; TA
1826 f (MkTB x) = MkTA x
1827 --------
1828 module B where
1829
1830 import A
1831
1832 data TB = MkTB !Int
1833
1834 g :: TA -&#62; TB
1835 g (MkTA x) = MkTB x
1836 </ProgramListing>
1837
1838 </para>
1839
1840 <para>
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>&lt;module&gt;.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>
1850 </para>
1851
1852 <para>
1853
1854 <ProgramListing>
1855 import {-# SOURCE #-} A
1856 </ProgramListing>
1857
1858 </para>
1859
1860 <para>
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.
1866 </para>
1867
1868 <para>
1869 For the example at hand, the boot interface file for A would look like
1870 the following:
1871 </para>
1872
1873 <para>
1874
1875 <ProgramListing>
1876 __interface A 1 404 where
1877 __export A TA{MkTA} ;
1878 1 newtype TA = MkTA PrelBase.Int ;
1879 </ProgramListing>
1880
1881 </para>
1882
1883 <para>
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.
1887 </para>
1888
1889 <para>
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>.
1893 </para>
1894
1895 <para>
1896 The number &ldquo;1&rdquo; after &ldquo;&lowbar;&lowbar;interface A&rdquo; gives the version number of module A;
1897 it is incremented whenever anything in A's interface file changes.  The &ldquo;404&rdquo; 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.
1901 </para>
1902
1903 <para>
1904 The number &ldquo;1&rdquo; 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
1910 the way it is.
1911 </para>
1912
1913 <para>
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:
1916 </para>
1917
1918 <para>
1919
1920 <ProgramListing>
1921 __interface A 1 404 where
1922 __export A TA;
1923 1 data TA
1924 </ProgramListing>
1925
1926 </para>
1927
1928 <para>
1929 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
1930 </para>
1931
1932 <para>
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.
1936 </para>
1937
1938 </sect2>
1939
1940 </Sect1>
1941
1942   <sect1 id="packages">
1943     <title>Packages</title>
1944     <indexterm><primary>packages</primary></indexterm>
1945
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>
1952
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>
1956
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>
1960
1961     <sect2 id="listing-packages">
1962       <title>Listing the available packages</title>
1963       <indexterm><primary>packages</primary>
1964         <secondary>listing</secondary></indexterm>
1965
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>
1969       </indexterm>
1970
1971 <screen>
1972   $ ghc --list-packages
1973   gmp, rts, std, lang, concurrent, data, net, posix, text, util
1974 </screen>
1975
1976       <para>Note that your GHC installation might have a slightly
1977       different set of packages installed.</para>
1978
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>
1984
1985     </sect2>
1986
1987     <sect2 id="using-packages">
1988       <title>Using a package</title>
1989       <indexterm><primary>packages</primary>
1990         <secondary>using</secondary></indexterm>
1991       
1992       <para>To use a package, add the <literal>-package</literal> flag
1993       to the command line:</para>
1994
1995       <variablelist>
1996         <varlistentry>
1997           <term><option>-package &lt;lib&gt;</option></term>
1998           <indexterm><primary>-package &lt;lib&gt; option</primary></indexterm>
1999           <listitem>
2000             <para>This option brings into scope all the modules from
2001             package <literal>&lt;lib&gt;</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
2004             being done.</para>
2005           </listitem>
2006         </varlistentry>
2007       </variablelist>
2008
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>
2015     </sect2>
2016
2017     <sect2 id="building-packages">
2018       <title>Building a package from Haskell source</title>
2019       <indexterm><primary>packages</primary>
2020         <secondary>building</secondary></indexterm>
2021
2022       <para>It takes some special considerations to build a new
2023       package:</para>
2024
2025       <itemizedlist>
2026         <listitem>
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
2030           recursive.</para>
2031         </listitem>
2032
2033         <listitem>
2034           <para>A package has a name
2035           (e.g. <filename>std</filename>)</para>
2036         </listitem>
2037
2038         <listitem>
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>
2047         </listitem>
2048
2049         <listitem>
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>
2054         </listitem>
2055       </itemizedlist>
2056
2057       <para>To compile a module which is to be part of a new package,
2058       use the <literal>-package-name</literal> option:</para>
2059
2060       <variablelist>
2061         <varlistentry>
2062           <term><option>-package-name &lt;foo&gt;</option></term>
2063           <indexterm><primary><literal>-package-name</literal></primary>
2064             <secondary>option</secondary></indexterm>
2065           <listitem>
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>
2070           </listitem>
2071         </varlistentry>
2072       </variablelist>
2073
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>
2080
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>
2086
2087     </sect2>
2088     <sect2 id="package-management">
2089       <title>Package management</title>
2090       <indexterm><primary>packages</primary>
2091         <secondary>management</secondary></indexterm>
2092       
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
2097       packages:</para>
2098
2099       <variablelist>
2100         <varlistentry>
2101           <term><option>--add-package</option></term>
2102           <indexterm><primary><literal>--add-package</literal></primary>
2103               <secondary>option</secondary></indexterm>
2104           <listitem>
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
2108             installed.</para>
2109           </listitem>
2110         </varlistentry>
2111
2112         <varlistentry>
2113           <term><option>--delete-package &lt;foo&gt;</option></term>
2114           <indexterm><primary><literal>--delete-package</literal></primary>
2115               <secondary>option</secondary></indexterm>
2116           <listitem>
2117             <para>Removes the specified package from the installed
2118             configuration.</para>
2119           </listitem>
2120         </varlistentry>
2121       </variablelist>
2122
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
2127       settings.</para>
2128
2129       <para>A package specification looks like this:</para>
2130
2131 <screen>
2132   Package {
2133      name            = "mypkg",
2134      import_dirs     = ["/usr/local/lib/imports/mypkg"],
2135      library_dirs    = ["/usr/local/lib"],
2136      hs_libraries    = ["HSmypkg" ],
2137      extra_libraries = ["HSmypkg_cbits"],
2138      include_dirs    = [],
2139      c_includes      = ["HsMyPkg.h"],
2140      package_deps    = ["text", "data"],
2141      extra_ghc_opts  = [],
2142      extra_cc_opts   = [],
2143      extra_ld_opts   = ["-lmy_clib"]
2144   }
2145 </screen>
2146
2147       <para>Components of a package specification may be specified in
2148       any order, and are:</para>
2149
2150       <variablelist>
2151         <varlistentry>
2152           <term><literal>name</literal></term>
2153           <indexterm><primary><literal>name</literal></primary>
2154             <secondary>package specification</secondary></indexterm>
2155           <listitem>
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. 
2159             </para>
2160           </listitem>
2161         </varlistentry>
2162
2163         <varlistentry>
2164           <term><literal>import_dirs</literal></term>
2165           <indexterm><primary><literal>import_dirs</literal></primary>
2166             <secondary>package specification</secondary></indexterm>
2167           <listitem>
2168             <para>A list of directories containing interface files
2169             (<literal>.hi</literal> files) for this package.</para>
2170           </listitem>
2171         </varlistentry>
2172
2173         <varlistentry>
2174           <term><literal>library_dirs</literal></term>
2175           <indexterm><primary><literal>library_dirs</literal></primary>
2176             <secondary>package specification</secondary></indexterm>
2177           <listitem>
2178             <para>A list of directories containing libraries for this
2179             package.</para>
2180           </listitem>
2181         </varlistentry>
2182
2183         <varlistentry>
2184           <term><literal>hs_libraries</literal></term>
2185           <indexterm><primary><literal>hs_libraries</literal></primary>
2186             <secondary>package specification</secondary></indexterm>
2187           <listitem>
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>
2192           </listitem>
2193         </varlistentry>
2194
2195         <varlistentry>
2196           <term><literal>extra_libraries</literal></term>
2197           <indexterm><primary><literal>extra_libraries</literal></primary>
2198             <secondary>package specification</secondary></indexterm>
2199           <listitem>
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
2212             for libraries in
2213             <literal>extra_libraries</literal>.</para>
2214
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>
2220           </listitem>
2221         </varlistentry>
2222
2223         <varlistentry>
2224           <term><literal>include_dirs</literal></term>
2225           <indexterm><primary><literal>include_dirs</literal></primary>
2226             <secondary>package specification</secondary></indexterm>
2227           <listitem>
2228             <para>A list of directories containing C includes for this
2229             package (maybe the empty list).</para>
2230           </listitem>
2231         </varlistentry>
2232
2233         <varlistentry>
2234           <term><literal>c_includes</literal></term>
2235           <indexterm><primary><literal>c_includes</literal></primary>
2236             <secondary>package specification</secondary></indexterm>
2237           <listitem>
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
2243             inlined.</para>
2244           </listitem>
2245         </varlistentry>
2246
2247         <varlistentry>
2248           <term><literal>package_deps</literal></term>
2249           <indexterm><primary><literal>package_deps</literal></primary>
2250             <secondary>package specification</secondary></indexterm>
2251           <listitem>
2252             <para>A list of packages which this package depends
2253             on.</para>
2254           </listitem>
2255         </varlistentry>
2256
2257         <varlistentry>
2258           <term><literal>extra_ghc_opts</literal></term>
2259           <indexterm><primary><literal>extra_ghc_opts</literal></primary>
2260             <secondary>package specification</secondary></indexterm>
2261           <listitem>
2262             <para>Extra arguments to be added to the GHC command line
2263             when this package is being used.</para>
2264           </listitem>
2265         </varlistentry>
2266
2267         <varlistentry>
2268           <term><literal>extra_cc_opts</literal></term>
2269           <indexterm><primary><literal>extra_cc_opts</literal></primary>
2270             <secondary>package specification</secondary></indexterm>
2271           <listitem>
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>
2275           </listitem>
2276         </varlistentry>
2277
2278         <varlistentry>
2279           <term><literal>extra_ld_opts</literal></term>
2280           <indexterm><primary><literal>extra_ld_opts</literal></primary>
2281             <secondary>package specification</secondary></indexterm>
2282           <listitem>
2283             <para>Extra arguments to be added to the gcc command line
2284             (for linking) when this package is being used.</para>
2285           </listitem>
2286         </varlistentry>
2287       </variablelist>
2288
2289       <para>For examples of more package specifications, take a look
2290       at the <literal>package.conf</literal> in your GHC
2291       installation.</para>
2292     </sect2>
2293   </sect1>
2294
2295
2296 <Sect1 id="options-optimise">
2297 <Title>Optimisation (code improvement)
2298 </Title>
2299
2300 <para>
2301 <indexterm><primary>optimisation (GHC)</primary></indexterm>
2302 <indexterm><primary>improvement, code (GHC)</primary></indexterm>
2303 </para>
2304
2305 <para>
2306 The <option>-O*</option> options specify convenient &ldquo;packages&rdquo; 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
2310 on/off.
2311 </para>
2312
2313 <Sect2 id="optimise-pkgs">
2314 <Title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.
2315 </Title>
2316
2317 <para>
2318 <indexterm><primary>-O options</primary></indexterm>
2319 </para>
2320
2321 <para>
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 &ldquo;Compile quickly&rdquo; or &ldquo;Make my program run like greased lightning.&rdquo;
2325 The following &ldquo;packages&rdquo; of optimisations (or lack thereof) should
2326 suffice.
2327 </para>
2328
2329 <para>
2330 Once you choose a <option>-O*</option> &ldquo;package,&rdquo; stick with it&mdash;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">).
2335 </para>
2336
2337 <para>
2338 <VariableList>
2339
2340 <varlistentry>
2341 <term>No <option>-O*</option>-type option specified:</term>
2342 <indexterm><primary>-O* not specified</primary></indexterm>
2343 <listitem>
2344 <para>
2345 This is taken to mean: &ldquo;Please compile quickly; I'm not over-bothered
2346 about compiled-code quality.&rdquo;  So, for example: <Command>ghc -c Foo.hs</Command>
2347 </para>
2348 </listitem>
2349 </varlistentry>
2350 <varlistentry>
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>
2355 <listitem>
2356 <para>
2357 Means: &ldquo;Generate good-quality code without taking too long about
2358 it.&rdquo; Thus, for example: <Command>ghc -c -O Main.lhs</Command>
2359 </para>
2360 </listitem>
2361 </varlistentry>
2362 <varlistentry>
2363 <term><option>-O2</option>:</term>
2364 <indexterm><primary>-O2 option</primary></indexterm>
2365 <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
2366 <listitem>
2367 <para>
2368 Means: &ldquo;Apply every non-dangerous optimisation, even if it means
2369 significantly longer compile times.&rdquo;
2370 </para>
2371
2372 <para>
2373 The avoided &ldquo;dangerous&rdquo; 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.
2376 </para>
2377
2378 <para>
2379 At the moment, <option>-O2</option> is <Emphasis>unlikely</Emphasis> to produce
2380 better code than <option>-O</option>.
2381 </para>
2382 </listitem>
2383 </varlistentry>
2384 <varlistentry>
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>
2388 <listitem>
2389 <para>
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!
2393 </para>
2394 </listitem>
2395 </varlistentry>
2396 <varlistentry>
2397 <term><option>-Onot</option>:</term>
2398 <indexterm><primary>-Onot option</primary></indexterm>
2399 <indexterm><primary>optimising, reset</primary></indexterm>
2400 <listitem>
2401 <para>
2402 This option will make GHC &ldquo;forget&rdquo; any
2403 <option>-O</option>ish options it has seen so far.  Sometimes useful;
2404 for example: <Command>make all
2405 EXTRA&lowbar;HC&lowbar;OPTS=-Onot</Command>.
2406 </para>
2407 </listitem>
2408 </varlistentry>
2409 <varlistentry>
2410 <term><option>-Ofile &lt;file&gt;</option>:</term>
2411 <indexterm><primary>-Ofile &lt;file&gt; option</primary></indexterm>
2412 <indexterm><primary>optimising, customised</primary></indexterm>
2413 <listitem>
2414 <para>
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>.
2419 </para>
2420
2421 <para>
2422 In that file, comments are of the
2423 <literal>&num;</literal>-to-end-of-line variety; blank lines and most
2424 whitespace is ignored.
2425 </para>
2426
2427 <para>
2428 Please ask if you are baffled and would like an example of <option>-Ofile</option>!
2429 </para>
2430 </listitem>
2431 </varlistentry>
2432 </VariableList>
2433 </para>
2434
2435 <para>
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).
2440 </para>
2441
2442 <para>
2443 The easiest way to see what <option>-O</option> (etc.) &ldquo;really mean&rdquo; is to run with
2444 <option>-v</option>, then stand back in amazement.  Alternatively, just look at the
2445 <literal>HsC&lowbar;minus&lt;blah&gt;</literal> lists in the GHC driver script.
2446 </para>
2447
2448 </sect2>
2449
2450 <sect2>
2451 <Title><option>-f*</option>: platform-independent flags</Title>
2452
2453 <para>
2454 <indexterm><primary>-f* options (GHC)</primary></indexterm>
2455 <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
2456 </para>
2457
2458 <para>
2459 Flags can be turned <Emphasis>off</Emphasis> individually.  (NB: I hope you have a
2460 good reason for doing this&hellip;) To turn off the <option>-ffoo</option> flag, just use
2461 the <option>-fno-foo</option> flag.<indexterm><primary>-fno-&lt;opt&gt; 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.
2464 </para>
2465
2466 <para>
2467 The options you are most likely to want to turn off are:
2468
2469 <ItemizedList>
2470 <listitem>
2471
2472 <para>
2473 <option>-fno-strictness</option><indexterm><primary>-fno-strictness option</primary></indexterm> (strictness
2474 analyser, because it is sometimes slow),
2475 </para>
2476 </listitem>
2477 <listitem>
2478
2479 <para>
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
2483 </para>
2484 </listitem>
2485 <listitem>
2486
2487 <para>
2488 <option>-fno-cpr-analyse</option><indexterm><primary>-fno-cpr-analyse option</primary></indexterm> switches off the CPR (constructed product
2489 result) analyser.
2490 </para>
2491 </listitem>
2492
2493 </ItemizedList>
2494
2495 </para>
2496
2497 <para>
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.
2502 </para>
2503
2504 <para>
2505 Here are some &ldquo;dangerous&rdquo; optimisations you <Emphasis>might</Emphasis> want to try:
2506 <VariableList>
2507
2508 <varlistentry>
2509 <term><option>-fvia-C</option>:</term>
2510 <listitem>
2511 <para>
2512 <indexterm><primary>-fvia-C option</primary></indexterm>
2513 <indexterm><primary>native code generator, turning off</primary></indexterm>
2514 </para>
2515
2516 <para>
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>&lowbar;casm&lowbar;</Function>s (which are utterly
2521 deprecated), you probably <Emphasis>have</Emphasis> to use
2522 <option>-fvia-C</option>.
2523 </para>
2524
2525 <para>
2526 The lower-case incantation, <option>-fvia-c</option>, is synonymous.
2527 </para>
2528
2529 <para>
2530 Compiling via C will probably be slower (in compilation time) than
2531 using GHC's native code generator.
2532 </para>
2533 </listitem>
2534 </varlistentry>
2535 <varlistentry>
2536 <term><option>-funfolding-interface-threshold&lt;n&gt;</option>:</term>
2537 <listitem>
2538 <para>
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 &ldquo;size&rdquo; that reflects the cost in terms
2545 of &ldquo;code bloat&rdquo; of expanding that unfolding in another module.  A
2546 bigger function would be assigned a bigger cost.)
2547 </para>
2548 </listitem>
2549 </varlistentry>
2550 <varlistentry>
2551 <term><option>-funfolding-creation-threshold&lt;n&gt;</option>:</term>
2552 <listitem>
2553 <para>
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
2561 more useful:
2562 </para>
2563 </listitem>
2564 </varlistentry>
2565 <varlistentry>
2566 <term><option>-funfolding-use-threshold&lt;n&gt;</option>:</term>
2567 <listitem>
2568 <para>
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>).
2577 </para>
2578 </listitem>
2579 </varlistentry>
2580
2581 <varlistentry>
2582 <term><option>-funfolding-update-in-place&lt;n&gt;</option>:</term>
2583 <listitem>
2584 <para>
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
2587 that of a thunk.
2588 <ProgramListing>
2589    x = plusInt a b
2590 </ProgramListing>
2591 If we inlined plusInt we might get an opportunity to use update-in-place for
2592 the thunk 'x'.
2593 </para>
2594 </listitem>
2595 </varlistentry>
2596
2597 <varlistentry>
2598 <term><option>-funbox-strict-fields</option>:</term>
2599 <listitem>
2600 <para>
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>
2604 </para>
2605
2606 <para>
2607 This option causes all constructor fields which are marked strict
2608 (i.e. &ldquo;!&rdquo;) to be unboxed or unpacked if possible.  For example:
2609 </para>
2610
2611 <para>
2612
2613 <ProgramListing>
2614 data T = T !Float !Float
2615 </ProgramListing>
2616
2617 </para>
2618
2619 <para>
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).
2625 </para>
2626
2627 <para>
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:
2631 </para>
2632
2633 <para>
2634
2635 <ProgramListing>
2636 f :: T -&#62; Float
2637 f (T f1 f2) = f1 + f2
2638 </ProgramListing>
2639
2640 </para>
2641
2642 <para>
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.
2645 </para>
2646
2647 <para>
2648 Any single-constructor data is eligible for unpacking; for example
2649 </para>
2650
2651 <para>
2652
2653 <ProgramListing>
2654 data T = T !(Int,Int)
2655 </ProgramListing>
2656
2657 </para>
2658
2659 <para>
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:
2662 </para>
2663
2664 <para>
2665
2666 <ProgramListing>
2667 data T = T !S
2668 data S = S !Int !Int
2669 </ProgramListing>
2670
2671 </para>
2672
2673 <para>
2674 will store two unboxed <literal>Int&num;</literal>s directly in the <Function>T</Function> constructor.
2675 </para>
2676 </listitem>
2677 </varlistentry>
2678 <varlistentry>
2679 <term><option>-fsemi-tagging</option>:</term>
2680 <listitem>
2681 <para>
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.)
2686 </para>
2687
2688 <para>
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&hellip; Sigh.)
2691 </para>
2692 </listitem>
2693 </varlistentry>
2694 <varlistentry>
2695 <term><option>-fexcess-precision</option>:</term>
2696 <listitem>
2697 <para>
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.
2703 </para>
2704 </listitem>
2705 </varlistentry>
2706 </VariableList>
2707 </para>
2708
2709 </sect2>
2710
2711 <sect2>
2712 <Title><option>-m*</option>: platform-specific flags</Title>
2713
2714 <para>
2715 <indexterm><primary>-m* options (GHC)</primary></indexterm>
2716 <indexterm><primary>platform-specific options</primary></indexterm>
2717 <indexterm><primary>machine-specific options</primary></indexterm>
2718 </para>
2719
2720 <para>
2721 Some flags only make sense for particular target platforms.
2722 </para>
2723
2724 <para>
2725 <VariableList>
2726
2727 <varlistentry>
2728 <term><option>-mv8</option>:</term>
2729 <listitem>
2730 <para>
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.
2735 </para>
2736 </listitem>
2737 </varlistentry>
2738 <varlistentry>
2739 <term><option>-mlong-calls</option>:</term>
2740 <listitem>
2741 <para>
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&hellip;)
2745 </para>
2746 </listitem>
2747 </varlistentry>
2748 <varlistentry>
2749 <term><option>-monly-[32]-regs</option>:</term>
2750 <listitem>
2751 <para>
2752 (iX86 machines)<indexterm><primary>-monly-N-regs option (iX86 only)</primary></indexterm>
2753 GHC tries to &ldquo;steal&rdquo; 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:
2756
2757 <Screen>
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.
2761 </Screen>
2762
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.
2765 </para>
2766 </listitem>
2767 </varlistentry>
2768 </VariableList>
2769 </para>
2770
2771 </sect2>
2772
2773 <Sect2 id="optimise-C-compiler">
2774 <Title>Code improvement by the C compiler.
2775 </Title>
2776
2777 <para>
2778 <indexterm><primary>optimisation by GCC</primary></indexterm>
2779 <indexterm><primary>GCC optimisation</primary></indexterm>
2780 </para>
2781
2782 <para>
2783 The C&nbsp;compiler (GCC) is run with <option>-O</option> turned on.  (It has
2784 to be, actually).
2785 </para>
2786
2787 <para>
2788 If you want to run GCC with <option>-O2</option>&mdash;which may be worth a few
2789 percent in execution speed&mdash;you can give a
2790 <option>-O2-for-C</option><indexterm><primary>-O2-for-C option</primary></indexterm> option.
2791 </para>
2792
2793 </sect2>
2794
2795 </Sect1>
2796
2797 <Sect1 id="options-phases">
2798 <Title>Options related to a particular phase
2799 </Title>
2800
2801 <Sect2 id="c-pre-processor">
2802 <Title>The C pre-processor
2803 </Title>
2804
2805 <para>
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>
2809 </para>
2810
2811 <para>
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.
2816 <VariableList>
2817
2818 <varlistentry>
2819 <term><option>-D&lt;foo&gt;</option>:</term>
2820 <listitem>
2821 <para>
2822 <indexterm><primary>-D&lt;name&gt; option</primary></indexterm>
2823 Define macro <Constant>&lt;foo&gt;</Constant> in the usual way.  NB: does <Emphasis>not</Emphasis> affect
2824 <option>-D</option> macros passed to the C&nbsp;compiler when compiling via C!  For those,
2825 use the <option>-optc-Dfoo</option> hack&hellip; (see <XRef LinkEnd="forcing-options-through">).
2826 </para>
2827 </listitem>
2828 </varlistentry>
2829 <varlistentry>
2830 <term><option>-U&lt;foo&gt;</option>:</term>
2831 <listitem>
2832 <para>
2833 <indexterm><primary>-U&lt;name&gt; option</primary></indexterm>
2834 Undefine macro <Command>&lt;foo&gt;</Command> in the usual way.
2835 </para>
2836 </listitem>
2837 </varlistentry>
2838 <varlistentry>
2839 <term><option>-I&lt;dir&gt;</option>:</term>
2840 <listitem>
2841 <para>
2842 <indexterm><primary>-I&lt;dir&gt; option</primary></indexterm>
2843 Specify a directory in which to look for <literal>&num;include</literal> files, in
2844 the usual C way.
2845 </para>
2846 </listitem>
2847 </varlistentry>
2848 </VariableList>
2849 </para>
2850
2851 <para>
2852 The GHC driver pre-defines several macros when processing Haskell
2853 source code (<Filename>.hs</Filename> or <Filename>.lhs</Filename> files):
2854 </para>
2855
2856 <para>
2857 <VariableList>
2858
2859 <varlistentry>
2860 <term><Constant>&lowbar;&lowbar;HASKELL98&lowbar;&lowbar;</Constant>:</term>
2861 <listitem>
2862 <para>
2863 <indexterm><primary>&lowbar;&lowbar;HASKELL98&lowbar;&lowbar;</primary></indexterm>
2864 If defined, this means that GHC supports the language defined by the
2865 Haskell 98 report.
2866 </para>
2867 </listitem>
2868 </varlistentry>
2869 <varlistentry>
2870 <term><Constant>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;=98</Constant>:</term>
2871 <listitem>
2872 <para>
2873 <indexterm><primary>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;</primary></indexterm>
2874 In GHC 4.04 and later, the <Constant>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;</Constant> macro is defined as having
2875 the value <Constant>98</Constant>.
2876 </para>
2877 </listitem>
2878 </varlistentry>
2879 <varlistentry>
2880 <term><Constant>&lowbar;&lowbar;HASKELL1&lowbar;&lowbar;</Constant>:</term>
2881 <listitem>
2882 <para>
2883 <indexterm><primary>&lowbar;&lowbar;HASKELL1&lowbar;&lowbar; 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.
2887 </para>
2888 </listitem>
2889 </varlistentry>
2890 <varlistentry>
2891 <term><Constant>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</term>
2892 <listitem>
2893 <para>
2894 <indexterm><primary>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar; macro</primary></indexterm>
2895 For version <Emphasis>n</Emphasis> of the GHC system, this will be <literal>&num;define</literal>d to
2896 <Emphasis>100n</Emphasis>.  So, for version 4.00, it is 400.
2897 </para>
2898
2899 <para>
2900 With any luck, <Constant>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar;</Constant> will be undefined in all other
2901 implementations that support C-style pre-processing.
2902 </para>
2903
2904 <para>
2905 (For reference: the comparable symbols for other systems are:
2906 <Constant>&lowbar;&lowbar;HUGS&lowbar;&lowbar;</Constant> for Hugs and <Constant>&lowbar;&lowbar;HBC&lowbar;&lowbar;</Constant> for Chalmers.)
2907 </para>
2908
2909 <para>
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).
2913 </para>
2914 </listitem>
2915 </varlistentry>
2916 <varlistentry>
2917 <term><Constant>&lowbar;&lowbar;CONCURRENT&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</term>
2918 <listitem>
2919 <para>
2920 <indexterm><primary>&lowbar;&lowbar;CONCURRENT&lowbar;HASKELL&lowbar;&lowbar; 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.
2924 </para>
2925 </listitem>
2926 </varlistentry>
2927 <varlistentry>
2928 <term><Constant>&lowbar;&lowbar;PARALLEL&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</term>
2929 <listitem>
2930 <para>
2931 <indexterm><primary>&lowbar;&lowbar;PARALLEL&lowbar;HASKELL&lowbar;&lowbar; 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).
2934 </para>
2935 </listitem>
2936 </varlistentry>
2937 </VariableList>
2938 </para>
2939
2940 <para>
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">).
2944 </para>
2945
2946 <para>
2947 A small word of warning: <option>-cpp</option> is not friendly to &ldquo;string
2948 gaps&rdquo;.<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:
2950 </para>
2951
2952 <para>
2953
2954 <ProgramListing>
2955 strmod = "\
2956 \ p \
2957 \ "
2958 </ProgramListing>
2959
2960 </para>
2961
2962 <para>
2963 don't work with <option>-cpp</option>; <Filename>/usr/bin/cpp</Filename> elides the
2964 backslash-newline pairs.
2965 </para>
2966
2967 <para>
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
2971 expected.
2972 </para>
2973
2974 </sect2>
2975
2976 <Sect2 id="options-C-compiler">
2977 <Title>Options affecting the C compiler (if applicable)
2978 </Title>
2979
2980 <para>
2981 <indexterm><primary>include-file options</primary></indexterm>
2982 <indexterm><primary>C compiler options</primary></indexterm>
2983 <indexterm><primary>GCC options</primary></indexterm>
2984 </para>
2985
2986 <para>
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:
2990 </para>
2991
2992 <para>
2993
2994 <InformalTable>
2995 <TGroup Cols="2">
2996 <ColSpec Align="Left" Colsep="0">
2997 <ColSpec Align="Left" Colsep="0">
2998 <TBody>
2999 <Row>
3000 <Entry><option>-ansi</option> </Entry>
3001 <Entry> do ANSI C (not K&amp;R) </Entry>
3002 </Row>
3003 <Row>
3004 <Entry>
3005 <option>-pedantic</option> </Entry>
3006 <Entry> be so</Entry>
3007 </Row>
3008 <Row>
3009 <Entry>
3010 <option>-dgcc-lint</option> </Entry>
3011 <Entry> (hack) short for &ldquo;make GCC very paranoid&rdquo;</Entry>
3012 </Row>
3013
3014 </TBody>
3015
3016 </TGroup>
3017 </InformalTable>
3018
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>
3022 </para>
3023
3024 <para>
3025 If you are compiling with lots of foreign calls, you may need to
3026 tell the C&nbsp;compiler about some <literal>&num;include</literal> files.  There is no real
3027 pretty way to do this, but you can use this hack from the
3028 command-line:
3029 </para>
3030
3031 <para>
3032
3033 <Screen>
3034 % ghc -c '-#include &#60;X/Xlib.h&#62;' Xstuff.lhs
3035 </Screen>
3036
3037 </para>
3038
3039 </sect2>
3040
3041 <Sect2 id="options-linker">
3042 <Title>Linking and consistency-checking
3043 </Title>
3044
3045 <para>
3046 <indexterm><primary>linker options</primary></indexterm>
3047 <indexterm><primary>ld options</primary></indexterm>
3048 </para>
3049
3050 <para>
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).
3054 </para>
3055
3056 <para>
3057 <VariableList>
3058
3059 <varlistentry>
3060 <term><option>-l&lt;FOO&gt;</option>:</term>
3061 <listitem>
3062 <para>
3063 <indexterm><primary>-l&lt;lib&gt; option</primary></indexterm>
3064 Link in a library named <Filename>lib&lt;FOO&gt;.a</Filename> which resides somewhere on the
3065 library directories path.
3066 </para>
3067
3068 <para>
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.
3074 </para>
3075
3076 <para>
3077 The linker will of course be informed about some GHC-supplied
3078 libraries automatically; these are:
3079 </para>
3080
3081 <para>
3082
3083 <InformalTable>
3084 <TGroup Cols="2">
3085 <ColSpec Align="Left" Colsep="0">
3086 <ColSpec Align="Left" Colsep="0">
3087 <TBody>
3088 <Row>
3089 <Entry><Emphasis>-l equivalent</Emphasis> </Entry>
3090 <Entry> <Emphasis>description</Emphasis> </Entry>
3091 </Row>
3092
3093 <Row>
3094 <Entry>
3095 <option>-lHSrts,-lHSclib</option> </Entry>
3096 <Entry> basic runtime libraries </Entry>
3097 </Row>
3098 <Row>
3099 <Entry>
3100 <option>-lHS</option> </Entry>
3101 <Entry> standard Prelude library </Entry>
3102 </Row>
3103 <Row>
3104 <Entry>
3105 <option>-lHS&lowbar;cbits</option> </Entry>
3106 <Entry> C support code for standard Prelude library </Entry>
3107 </Row>
3108 <Row>
3109 <Entry>
3110 <option>-lgmp</option> </Entry>
3111 <Entry> GNU multi-precision library (for Integers)</Entry>
3112 </Row>
3113
3114 </TBody>
3115
3116 </TGroup>
3117 </InformalTable>
3118
3119 </para>
3120
3121 <para>
3122 <indexterm><primary>-lHS library</primary></indexterm>
3123 <indexterm><primary>-lHS&lowbar;cbits library</primary></indexterm>
3124 <indexterm><primary>-lHSrts library</primary></indexterm>
3125 <indexterm><primary>-lgmp library</primary></indexterm>
3126 </para>
3127 </listitem>
3128 </varlistentry>
3129 <varlistentry>
3130 <term><option>-package &lt;name&gt;</option>:</term>
3131 <listitem>
3132 <para>
3133 <indexterm><primary>-package &lt;name&gt; option</primary></indexterm>
3134 </para>
3135
3136 <para>
3137 If you are using a Haskell &ldquo;package&rdquo; (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
3140 more details.
3141 </para>
3142 </listitem>
3143 </varlistentry>
3144 <varlistentry>
3145 <term><option>-L&lt;dir&gt;</option>:</term>
3146 <listitem>
3147 <para>
3148 <indexterm><primary>-L&lt;dir&gt; option</primary></indexterm>
3149 Where to find user-supplied libraries&hellip;  Prepend the directory
3150 <Filename>&lt;dir&gt;</Filename> to the library directories path.
3151 </para>
3152 </listitem>
3153 </varlistentry>
3154 <varlistentry>
3155 <term><option>-static</option>:</term>
3156 <listitem>
3157 <para>
3158 <indexterm><primary>-static option</primary></indexterm>
3159 Tell the linker to avoid shared libraries.
3160 </para>
3161 </listitem>
3162 </varlistentry>
3163 <varlistentry>
3164 <term><option>-no-link-chk</option> and <option>-link-chk</option>:</term>
3165 <listitem>
3166 <para>
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 &ldquo;consistency check&rdquo;.
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).
3178 </para>
3179 </listitem>
3180 </varlistentry>
3181 <varlistentry>
3182 <term><option>-no-hs-main</option>:</term>
3183 <listitem>
3184 <para>
3185 <indexterm><primary>-no-hs-main option</primary></indexterm>
3186 <indexterm><primary>linking Haskell libraries with foreign code</primary></indexterm>
3187 </para>
3188
3189 <para>
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>.
3194 </para>
3195
3196 <para>
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.
3202 </para>
3203 </listitem>
3204 </varlistentry>
3205 </VariableList>
3206 </para>
3207
3208 </sect2>
3209
3210 </Sect1>
3211
3212 <Sect1 id="sec-using-concurrent">
3213 <Title>Using Concurrent Haskell</Title>
3214
3215 <para>
3216 <indexterm><primary>Concurrent Haskell&mdash;use</primary></indexterm>
3217 </para>
3218
3219 <para>
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.
3225 </para>
3226
3227 <para>
3228 Three RTS options are provided for modifying the behaviour of the
3229 threaded runtime system.  See the descriptions of
3230 <option>-C[&lt;us&gt;]</option>, <option>-q</option>, and
3231 <option>-t&lt;num&gt;</option> in <XRef LinkEnd="parallel-rts-opts">.
3232 </para>
3233
3234 <para>
3235 Concurrent Haskell is described in more detail in <XRef
3236 LinkEnd="sec-Concurrent">.
3237 </para>
3238
3239 </Sect1>
3240
3241 <Sect1 id="sec-using-parallel">
3242 <Title>Using Parallel Haskell</Title>
3243
3244 <para>
3245 <indexterm><primary>Parallel Haskell&mdash;use</primary></indexterm>
3246 </para>
3247
3248 <para>
3249 &lsqb;You won't be able to execute parallel Haskell programs unless PVM3
3250 (Parallel Virtual Machine, version 3) is installed at your site.]
3251 </para>
3252
3253 <para>
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.
3259 </para>
3260
3261 <para>
3262 To run your parallel program, once PVM is going, just invoke it
3263 &ldquo;as normal&rdquo;.  The main extra RTS option is
3264 <option>-N&lt;n&gt;</option>, to say how many PVM
3265 &ldquo;processors&rdquo; your program to run on.  (For more details of
3266 all relevant RTS options, please see <XRef
3267 LinkEnd="parallel-rts-opts">.)
3268 </para>
3269
3270 <para>
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.
3274 </para>
3275
3276 <sect2>
3277 <Title>Dummy's guide to using PVM</Title>
3278
3279 <para>
3280 <indexterm><primary>PVM, how to use</primary></indexterm>
3281 <indexterm><primary>Parallel Haskell&mdash;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:
3285
3286 <ProgramListing>
3287 setenv PVM_ROOT /wherever/you/put/it
3288 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
3289 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
3290 </ProgramListing>
3291
3292 </para>
3293
3294 <para>
3295 Creating and/or controlling your &ldquo;parallel machine&rdquo; is a purely-PVM
3296 business; nothing specific to Parallel Haskell.
3297 </para>
3298
3299 <para>
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 &ldquo;parallel machine;&rdquo; the most useful being:
3303 </para>
3304
3305 <para>
3306 <InformalTable>
3307 <TGroup Cols=2>
3308 <ColSpec Align="Left">
3309 <TBody>
3310
3311 <Row>
3312 <Entry><KeyCombo><KeyCap>Control</KeyCap><KeyCap>D</KeyCap></KeyCombo></Entry>
3313 <Entry>exit <Command>pvm</Command>, leaving it running</Entry>
3314 </Row>
3315
3316 <Row>
3317 <Entry><Command>halt</Command></Entry>
3318 <Entry>kill off this &ldquo;parallel machine&rdquo; &amp; exit</Entry>
3319 </Row>
3320
3321 <Row>
3322 <Entry><Command>add &lt;host&gt;</Command></Entry>
3323 <Entry>add <Command>&lt;host&gt;</Command> as a processor</Entry>
3324 </Row>
3325
3326 <Row>
3327 <Entry><Command>delete &lt;host&gt;</Command></Entry>
3328 <Entry>delete <Command>&lt;host&gt;</Command></Entry>
3329 </Row>
3330
3331 <Row>
3332 <Entry><Command>reset</Command></Entry>
3333 <Entry>kill what's going, but leave PVM up</Entry>
3334 </Row>
3335
3336 <Row>
3337 <Entry><Command>conf</Command></Entry>
3338 <Entry>list the current configuration</Entry>
3339 </Row>
3340
3341 <Row>
3342 <Entry><Command>ps</Command></Entry>
3343 <Entry>report processes' status</Entry>
3344 </Row>
3345
3346 <Row>
3347 <Entry><Command>pstat &lt;pid&gt;</Command></Entry>
3348 <Entry>status of a particular process</Entry>
3349 </Row>
3350
3351 </TBody>
3352 </TGroup>
3353 </InformalTable>
3354 </para>
3355
3356 <para>
3357 The PVM documentation can tell you much, much more about <Command>pvm</Command>!
3358 </para>
3359
3360 </sect2>
3361
3362 <sect2>
3363 <Title>Parallelism profiles</Title>
3364
3365 <para>
3366 <indexterm><primary>parallelism profiles</primary></indexterm>
3367 <indexterm><primary>profiles, parallelism</primary></indexterm>
3368 <indexterm><primary>visualisation tools</primary></indexterm>
3369 </para>
3370
3371 <para>
3372 With Parallel Haskell programs, we usually don't care about the
3373 results&mdash;only with &ldquo;how parallel&rdquo; it was!  We want pretty pictures.
3374 </para>
3375
3376 <para>
3377 Parallelism profiles (&agrave; 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>&lt;full-path&gt;&lt;program&gt;.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:
3383 </para>
3384
3385 <para>
3386
3387 <Screen>
3388 % ./a.out +RTS -N8 -q
3389 % grs2gr *.???.gr &#62; 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!
3392 </Screen>
3393
3394 </para>
3395
3396 <para>
3397 The scripts for processing the parallelism profiles are distributed
3398 in <Filename>ghc/utils/parallel/</Filename>.
3399 </para>
3400
3401 </sect2>
3402
3403 <sect2>
3404 <Title>Other useful info about running parallel programs</Title>
3405
3406 <para>
3407 The &ldquo;garbage-collection statistics&rdquo; 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.
3413 </para>
3414
3415 <para>
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>.
3418 </para>
3419
3420 </sect2>
3421
3422 <Sect2 id="parallel-rts-opts">
3423 <Title>RTS options for Concurrent/Parallel Haskell
3424 </Title>
3425
3426 <para>
3427 <indexterm><primary>RTS options, concurrent</primary></indexterm>
3428 <indexterm><primary>RTS options, parallel</primary></indexterm>
3429 <indexterm><primary>Concurrent Haskell&mdash;RTS options</primary></indexterm>
3430 <indexterm><primary>Parallel Haskell&mdash;RTS options</primary></indexterm>
3431 </para>
3432
3433 <para>
3434 Besides the usual runtime system (RTS) options
3435 (<XRef LinkEnd="runtime-control">), there are a few options particularly
3436 for concurrent/parallel execution.
3437 </para>
3438
3439 <para>
3440 <VariableList>
3441
3442 <varlistentry>
3443 <term><option>-N&lt;N&gt;</option>:</term>
3444 <listitem>
3445 <para>
3446 <indexterm><primary>-N&lt;N&gt; RTS option (parallel)</primary></indexterm>
3447 (PARALLEL ONLY) Use <literal>&lt;N&gt;</literal> PVM processors to run this program;
3448 the default is 2.
3449 </para>
3450 </listitem>
3451 </varlistentry>
3452 <varlistentry>
3453 <term><option>-C[&lt;us&gt;]</option>:</term>
3454 <listitem>
3455 <para>
3456 <indexterm><primary>-C&lt;us&gt; RTS option</primary></indexterm> Sets
3457 the context switch interval to <literal>&lt;s&gt;</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.
3466 </para>
3467 </listitem>
3468 </varlistentry>
3469 <varlistentry>
3470 <term><option>-q[v]</option>:</term>
3471 <listitem>
3472 <para>
3473 <indexterm><primary>-q RTS option</primary></indexterm>
3474 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
3475 in the file <FIlename>&lt;program&gt;.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>).
3483 </para>
3484 </listitem>
3485 </varlistentry>
3486 <varlistentry>
3487 <term><option>-t&lt;num&gt;</option>:</term>
3488 <listitem>
3489 <para>
3490 <indexterm><primary>-t&lt;num&gt; RTS option</primary></indexterm>
3491 (PARALLEL ONLY) Limit the number of concurrent threads per processor
3492 to <literal>&lt;num&gt;</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,
3495 8K bytes.)
3496 </para>
3497 </listitem>
3498 </varlistentry>
3499 <varlistentry>
3500 <term><option>-d</option>:</term>
3501 <listitem>
3502 <para>
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&hellip;) 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>.
3509 </para>
3510 </listitem>
3511 </varlistentry>
3512 <varlistentry>
3513 <term><option>-e&lt;num&gt;</option>:</term>
3514 <listitem>
3515 <para>
3516 <indexterm><primary>-e&lt;num&gt; RTS option (parallel)</primary></indexterm>
3517 (PARALLEL ONLY) Limit the number of pending sparks per processor to
3518 <literal>&lt;num&gt;</literal>. The default is 100. A larger number may be appropriate if
3519 your program generates large amounts of parallelism initially.
3520 </para>
3521 </listitem>
3522 </varlistentry>
3523 <varlistentry>
3524 <term><option>-Q&lt;num&gt;</option>:</term>
3525 <listitem>
3526 <para>
3527 <indexterm><primary>-Q&lt;num&gt; RTS option (parallel)</primary></indexterm>
3528 (PARALLEL ONLY) Set the size of packets transmitted between processors
3529 to <literal>&lt;num&gt;</literal>. The default is 1024 words. A larger number may be
3530 appropriate if your machine has a high communication cost relative to
3531 computation speed.
3532 </para>
3533 </listitem>
3534 </varlistentry>
3535 </VariableList>
3536 </para>
3537
3538 </sect2>
3539
3540 </Sect1>
3541
3542 &runtime
3543 &debug
3544
3545 </Chapter>
3546
3547 <!-- Emacs stuff:
3548      ;;; Local Variables: ***
3549      ;;; mode: sgml ***
3550      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
3551      ;;; End: ***
3552  -->