[project @ 2005-07-28 15:41:47 by simonmar]
[ghc-hetmet.git] / ghc / docs / users_guide / using.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <chapter id="using-ghc">
3   <title>Using GHC</title>
4
5   <indexterm><primary>GHC, using</primary></indexterm>
6   <indexterm><primary>using GHC</primary></indexterm>
7
8   <sect1>
9     <title>Options overview</title>
10     
11     <para>GHC's behaviour is controlled by
12     <firstterm>options</firstterm>, which for historical reasons are
13     also sometimes referred to as command-line flags or arguments.
14     Options can be specified in three ways:</para>
15
16     <sect2>
17       <title>command-line arguments</title>
18       
19       <indexterm><primary>structure, command-line</primary></indexterm>
20       <indexterm><primary>command-line</primary><secondary>arguments</secondary></indexterm>
21       <indexterm><primary>arguments</primary><secondary>command-line</secondary></indexterm>
22       
23       <para>An invocation of GHC takes the following form:</para>
24
25 <screen>
26 ghc [argument...]
27 </screen>
28
29       <para>command-line arguments are either options or file names.</para>
30
31       <para>command-line options begin with <literal>-</literal>.
32       They may <emphasis>not</emphasis> be grouped:
33       <option>-vO</option> is different from <option>-v -O</option>.
34       Options need not precede filenames: e.g., <literal>ghc *.o -o
35       foo</literal>.  All options are processed and then applied to
36       all files; you cannot, for example, invoke <literal>ghc -c -O1
37       Foo.hs -O2 Bar.hs</literal> to apply different optimisation
38       levels to the files <filename>Foo.hs</filename> and
39       <filename>Bar.hs</filename>.</para>
40     </sect2>
41
42     <sect2 id="source-file-options">
43       <title>command line options in source files</title>
44     
45       <indexterm><primary>source-file options</primary></indexterm>
46
47       <para>Sometimes it is useful to make the connection between a
48       source file and the command-line options it requires quite
49       tight. For instance, if a Haskell source file uses GHC
50       extensions, it will always need to be compiled with the
51       <option>-fglasgow-exts</option> option.  Rather than maintaining
52       the list of per-file options in a <filename>Makefile</filename>,
53       it is possible to do this directly in the source file using the
54       <literal>OPTIONS_GHC</literal> pragma <indexterm><primary>OPTIONS_GHC
55       pragma</primary></indexterm>:</para>
56
57 <programlisting>
58 {-# OPTIONS_GHC -fglasgow-exts #-}
59 module X where
60 ...
61 </programlisting>
62       
63       <para><literal>OPTIONS_GHC</literal> pragmas are only looked for at
64       the top of your source files, upto the first
65       (non-literate,non-empty) line not containing
66       <literal>OPTIONS_GHC</literal>. Multiple <literal>OPTIONS_GHC</literal>
67       pragmas are recognised.  Do not put comments before, or on the same line
68         as, the <literal>OPTIONS_GHC</literal> pragma.</para>
69
70       <para>Note that your command shell does not
71       get to the source file options, they are just included literally
72       in the array of command-line arguments the compiler
73       maintains internally, so you'll be desperately disappointed if
74       you try to glob etc. inside <literal>OPTIONS_GHC</literal>.</para>
75
76       <para>NOTE: the contents of OPTIONS_GHC are prepended to the
77       command-line options, so you <emphasis>do</emphasis> have the
78       ability to override OPTIONS_GHC settings via the command
79       line.</para>
80
81       <para>It is not recommended to move all the contents of your
82       Makefiles into your source files, but in some circumstances, the
83       <literal>OPTIONS_GHC</literal> pragma is the Right Thing. (If you
84       use <option>-keep-hc-file-too</option> and have OPTION flags in
85       your module, the OPTIONS_GHC will get put into the generated .hc
86       file).</para>
87     </sect2>
88
89     <sect2>
90       <title>Setting options in GHCi</title>
91
92       <para>Options may also be modified from within GHCi, using the
93       <literal>:set</literal> command.  See <xref linkend="ghci-set"/>
94       for more details.</para>
95     </sect2>
96   </sect1>
97     
98   <sect1 id="static-dynamic-flags">
99     <title>Static vs. Dynamic options</title>
100     <indexterm><primary>static</primary><secondary>options</secondary>
101     </indexterm>
102     <indexterm><primary>dynamic</primary><secondary>options</secondary>
103     </indexterm>
104
105     <para>Each of GHC's command line options is classified as either
106     <firstterm>static</firstterm> or <firstterm>dynamic</firstterm>.
107     A static flag may only be specified on the command line, whereas a
108     dynamic flag may also be given in an <literal>OPTIONS_GHC</literal>
109     pragma in a source file or set from the GHCi command-line with
110     <literal>:set</literal>.</para>
111
112     <para>As a rule of thumb, options which relate to filenames are
113     static, and the rest are dynamic. The flag reference tables (<xref
114     linkend="flag-reference"/>) lists the status of each flag.</para>
115   </sect1>
116
117   <sect1 id="file-suffixes">
118     <title>Meaningful file suffixes</title>
119
120     <indexterm><primary>suffixes, file</primary></indexterm>
121     <indexterm><primary>file suffixes for GHC</primary></indexterm>
122
123     <para>File names with &ldquo;meaningful&rdquo; suffixes (e.g.,
124     <filename>.lhs</filename> or <filename>.o</filename>) cause the
125     &ldquo;right thing&rdquo; to happen to those files.</para>
126
127     <variablelist>
128
129       <varlistentry>
130         <term>
131           <filename>.lhs</filename>
132           <indexterm><primary><literal>lhs</literal> suffix</primary></indexterm>
133         </term>
134         <listitem>
135           <para>A &ldquo;literate Haskell&rdquo; module.</para>
136         </listitem>
137       </varlistentry>
138
139       <varlistentry>
140         <term><filename>.hs</filename></term>
141         <listitem>
142           <para>A not-so-literate Haskell module.</para>
143         </listitem>
144       </varlistentry>
145
146       <varlistentry>
147         <term><filename>.hi</filename></term>
148         <listitem>
149           <para>A Haskell interface file, probably
150           compiler-generated.</para>
151         </listitem>
152       </varlistentry>
153
154       <varlistentry>
155         <term><filename>.hc</filename></term>
156         <listitem>
157           <para>Intermediate C file produced by the Haskell
158           compiler.</para>
159         </listitem>
160       </varlistentry>
161
162       <varlistentry>
163         <term><filename>.c</filename></term>
164         <listitem>
165           <para>A C&nbsp;file not produced by the Haskell
166           compiler.</para>
167         </listitem>
168       </varlistentry>
169       
170       <varlistentry>
171         <term><filename>.s</filename></term>
172         <listitem>
173           <para>An assembly-language source file, usually produced by
174           the compiler.</para>
175         </listitem>
176       </varlistentry>
177
178       <varlistentry>
179         <term><filename>.o</filename></term>
180         <listitem>
181           <para>An object file, produced by an assembler.</para>
182         </listitem>
183       </varlistentry>
184     </variablelist>
185
186     <para>Files with other suffixes (or without suffixes) are passed
187     straight to the linker.</para>
188
189   </sect1>
190
191   <sect1 id="modes">
192     <title>Modes of operation</title>
193
194     <para>GHC's behaviour is firstly controlled by a mode flag.  Only
195     one of these flags may be given, but it does not necessarily need
196     to be the first option on the command-line.  The available modes
197     are:</para>
198
199     <variablelist>
200       <varlistentry>
201         <term>
202           <cmdsynopsis><command>ghc</command>
203             <arg choice='plain'>&ndash;&ndash;interactive</arg>
204           </cmdsynopsis>
205           <indexterm><primary>interactive mode</primary></indexterm>
206           <indexterm><primary>ghci</primary></indexterm>
207         </term>
208         <listitem>
209           <para>Interactive mode, which is also available as
210           <command>ghci</command>.  Interactive mode is described in
211           more detail in <xref linkend="ghci"/>.</para>
212         </listitem>
213       </varlistentry>
214       
215       <varlistentry>
216         <term>
217           <cmdsynopsis><command>ghc</command>
218             <arg choice='plain'>&ndash;&ndash;make</arg>
219           </cmdsynopsis>
220           <indexterm><primary>make mode</primary></indexterm>
221           <indexterm><primary><option>&ndash;&ndash;make</option></primary></indexterm>
222         </term>
223         <listitem>
224           <para>In this mode, GHC will build a multi-module Haskell
225           program automatically, figuring out dependencies for itself.
226           If you have a straightforward Haskell program, this is
227           likely to be much easier, and faster, than using
228           <command>make</command>.  Make mode is described in <xref
229           linkend="make-mode"/>.</para>
230         </listitem>
231       </varlistentry>
232
233       <varlistentry>
234         <term>
235           <cmdsynopsis><command>ghc</command>
236             <arg choice='plain'>&ndash;e</arg> <arg choice='plain'><replaceable>expr</replaceable></arg>
237           </cmdsynopsis>
238           <indexterm><primary>eval mode</primary></indexterm>
239         </term>
240         <listitem>
241           <para>Expression-evaluation mode.  This is very similar to
242           interactive mode, except that there is a single expression
243           to evaluate (<replaceable>expr</replaceable>) which is given
244           on the command line.  See <xref linkend="eval-mode"/> for
245           more details.</para>
246         </listitem>
247       </varlistentry>
248       
249       <varlistentry>
250         <term>
251           <cmdsynopsis>
252             <command>ghc</command>
253             <group>
254               <arg>-E</arg>
255               <arg>-C</arg>
256               <arg>-S</arg>
257               <arg>-c</arg>
258             </group>
259           </cmdsynopsis>
260           <indexterm><primary><option>-E</option></primary></indexterm>
261           <indexterm><primary><option>-C</option></primary></indexterm>
262           <indexterm><primary><option>-S</option></primary></indexterm>
263           <indexterm><primary><option>-c</option></primary></indexterm>
264         </term>
265         <listitem>
266           <para>This is the traditional batch-compiler mode, in which
267           GHC can compile source files one at a time, or link objects
268           together into an executable.  This mode also applies if
269           there is no other mode flag specified on the command line,
270           in which case it means that the specified files should be
271           compiled and then linked to form a program. See <xref
272           linkend="options-order"/>.</para>
273         </listitem>
274       </varlistentry>
275
276       <varlistentry>
277         <term>
278           <cmdsynopsis>
279             <command>ghc</command>
280             <arg choice='plain'>&ndash;M</arg>
281           </cmdsynopsis>
282           <indexterm><primary>dependency-generation mode</primary></indexterm>
283         </term>
284         <listitem>
285           <para>Dependency-generation mode.  In this mode, GHC can be
286           used to generate dependency information suitable for use in
287           a <literal>Makefile</literal>.  See <xref
288           linkend="sec-makefile-dependencies"/>.</para>
289         </listitem>
290       </varlistentry>
291
292       <varlistentry>
293         <term>
294           <cmdsynopsis>
295             <command>ghc</command>
296             <arg choice='plain'>&ndash;&ndash;mk-dll</arg>
297           </cmdsynopsis>
298           <indexterm><primary>dependency-generation mode</primary></indexterm>
299         </term>
300         <listitem>
301           <para>DLL-creation mode (Windows only).  See <xref
302           linkend="win32-dlls-create"/>.</para>
303         </listitem>
304       </varlistentry>
305     </variablelist>
306
307     <sect2 id="make-mode">
308       <title>Using <command>ghc</command> <option>&ndash;&ndash;make</option></title>
309       <indexterm><primary><option>&ndash;&ndash;make</option></primary></indexterm>
310       <indexterm><primary>separate compilation</primary></indexterm>
311       
312       <para>When given the <option>&ndash;&ndash;make</option> option,
313       GHC will build a multi-module Haskell program by following
314       dependencies from a single root module (usually
315       <literal>Main</literal>).  For example, if your
316       <literal>Main</literal> module is in a file called
317       <filename>Main.hs</filename>, you could compile and link the
318       program like this:</para>
319
320 <screen>
321 ghc &ndash;&ndash;make Main.hs
322 </screen>
323
324       <para>The command line may contain any number of source file
325       names or module names; GHC will figure out all the modules in
326       the program by following the imports from these initial modules.
327       It will then attempt to compile each module which is out of
328       date, and finally if there is a <literal>Main</literal> module,
329       the program will also be linked into an executable.</para>
330
331       <para>The main advantages to using <literal>ghc
332       &ndash;&ndash;make</literal> over traditional
333       <literal>Makefile</literal>s are:</para>
334
335       <itemizedlist>
336         <listitem>
337           <para>GHC doesn't have to be restarted for each compilation,
338           which means it can cache information between compilations.
339           Compiling a multi-module program with <literal>ghc
340           &ndash;&ndash;make</literal> can be up to twice as fast as
341           running <literal>ghc</literal> individually on each source
342           file.</para>
343         </listitem>
344         <listitem>
345           <para>You don't have to write a<literal>Makefile</literal>.</para>
346           <indexterm><primary><literal>Makefile</literal>s</primary><secondary>avoiding</secondary></indexterm>
347         </listitem>
348         <listitem>
349           <para>GHC re-calculates the dependencies each time it is
350           invoked, so the dependencies never get out of sync with the
351           source.</para>
352         </listitem>
353       </itemizedlist>
354       
355       <para>Any of the command-line options described in the rest of
356       this chapter can be used with
357       <option>&ndash;&ndash;make</option>, but note that any options
358       you give on the command line will apply to all the source files
359       compiled, so if you want any options to apply to a single source
360       file only, you'll need to use an <literal>OPTIONS_GHC</literal>
361       pragma (see <xref linkend="source-file-options"/>).</para>
362
363       <para>If the program needs to be linked with additional objects
364       (say, some auxiliary C code), then the object files can be
365       given on the command line and GHC will include them when linking
366       the executable.</para>
367       
368       <para>Note that GHC can only follow dependencies if it has the
369       source file available, so if your program includes a module for
370       which there is no source file, even if you have an object and an
371       interface file for the module, then GHC will complain.  The
372       exception to this rule is for package modules, which may or may
373       not have source files.</para>
374
375       <para>The source files for the program don't all need to be in
376       the same directory; the <option>-i</option> option can be used
377       to add directories to the search path (see <xref
378       linkend="search-path"/>).</para>
379     </sect2>
380   
381     <sect2 id="eval-mode">
382       <title>Expression evaluation mode</title>
383
384       <para>This mode is very similar to interactive mode, except that
385       there is a single expression to evaluate which is specified on
386       the command line as an argument to the <option>-e</option>
387       option:</para>
388
389 <screen>
390 ghc -e <replaceable>expr</replaceable>
391 </screen>
392
393       <para>Haskell source files may be named on the command line, and
394       they will be loaded exactly as in interactive mode.  The
395       expression is evaluated in the context of the loaded
396       modules.</para>
397
398       <para>For example, to load and run a Haskell program containing
399       a module <literal>Main</literal>, we might say</para>
400
401 <screen>
402 ghc -e Main.main Main.hs
403 </screen>
404       
405       <para>or we can just use this mode to evaluate expressions in
406       the context of the <literal>Prelude</literal>:</para>
407
408 <screen>
409 $ ghc -e "interact (unlines.map reverse.lines)"
410 hello
411 olleh
412 </screen>
413     </sect2>
414
415     <sect2 id="options-order">
416       <title>Batch compiler mode</title>
417       
418       <para>In <emphasis>batch mode</emphasis>, GHC will compile one or more source files
419       given on the command line.</para>
420       
421       <para>The first phase to run is determined by each input-file
422       suffix, and the last phase is determined by a flag.  If no
423       relevant flag is present, then go all the way through linking.
424       This table summarises:</para>
425       
426       <informaltable>
427         <tgroup cols="4">
428           <colspec align="left"/>
429           <colspec align="left"/>
430           <colspec align="left"/>
431           <colspec align="left"/>
432           
433           <thead>
434             <row>
435               <entry>Phase of the compilation system</entry>
436               <entry>Suffix saying &ldquo;start here&rdquo;</entry>
437               <entry>Flag saying &ldquo;stop after&rdquo;</entry>
438               <entry>(suffix of) output file</entry>
439             </row>
440           </thead>
441           <tbody>
442             <row>
443               <entry>literate pre-processor</entry>
444               <entry><literal>.lhs</literal></entry>
445               <entry>-</entry>
446               <entry><literal>.hs</literal></entry>
447             </row>
448             
449             <row>
450               <entry>C pre-processor (opt.) </entry>
451               <entry><literal>.hs</literal> (with
452               <option>-cpp</option>)</entry>
453               <entry><option>-E</option></entry>
454               <entry><literal>.hspp</literal></entry>
455             </row>
456             
457             <row>
458               <entry>Haskell compiler</entry>
459               <entry><literal>.hs</literal></entry>
460               <entry><option>-C</option>, <option>-S</option></entry>
461               <entry><literal>.hc</literal>, <literal>.s</literal></entry>
462             </row>
463             
464             <row>
465               <entry>C compiler (opt.)</entry>
466               <entry><literal>.hc</literal> or <literal>.c</literal></entry>
467               <entry><option>-S</option></entry>
468               <entry><literal>.s</literal></entry>
469             </row>
470             
471             <row>
472               <entry>assembler</entry>
473               <entry><literal>.s</literal></entry>
474               <entry><option>-c</option></entry>
475               <entry><literal>.o</literal></entry>
476             </row>
477             
478             <row>
479               <entry>linker</entry>
480               <entry><replaceable>other</replaceable></entry>
481               <entry>-</entry>
482               <entry><filename>a.out</filename></entry>
483             </row>
484           </tbody>
485         </tgroup>
486       </informaltable>
487       
488       <indexterm><primary><option>-C</option></primary></indexterm>
489       <indexterm><primary><option>-E</option></primary></indexterm>
490       <indexterm><primary><option>-S</option></primary></indexterm>
491       <indexterm><primary><option>-c</option></primary></indexterm>
492       
493       <para>Thus, a common invocation would be: </para>
494
495 <screen>
496 ghc -c Foo.hs</screen>
497       
498       <para>to compile the Haskell source file
499       <filename>Foo.hs</filename> to an object file
500       <filename>Foo.o</filename>.</para>
501
502       <para>Note: What the Haskell compiler proper produces depends on
503       whether a native-code generator<indexterm><primary>native-code
504       generator</primary></indexterm> is used (producing assembly
505       language) or not (producing C).  See <xref
506       linkend="options-codegen"/> for more details.</para>
507
508       <para>Note: C pre-processing is optional, the
509       <option>-cpp</option><indexterm><primary><option>-cpp</option></primary></indexterm>
510       flag turns it on.  See <xref linkend="c-pre-processor"/> for more
511       details.</para>
512       
513       <para>Note: The option <option>-E</option><indexterm><primary>-E
514       option</primary></indexterm> runs just the pre-processing passes
515       of the compiler, dumping the result in a file.  Note that this
516       differs from the previous behaviour of dumping the file to
517       standard output.</para>
518     </sect2>
519   </sect1>
520
521   <sect1 id="options-help">
522     <title>Help and verbosity options</title>
523
524     <indexterm><primary>help options</primary></indexterm>
525     <indexterm><primary>verbosity options</primary></indexterm>
526
527     <variablelist>
528       <varlistentry>
529         <term>
530           <option>&ndash;&ndash;help</option>
531           <indexterm><primary><option>&ndash;&ndash;help</option></primary></indexterm>
532         </term>
533         <term>
534           <option>-?</option>
535           <indexterm><primary><option>-?</option></primary></indexterm>
536         </term>
537         <listitem>
538           <para>Cause GHC to spew a long usage message to standard
539           output and then exit.</para>
540         </listitem>
541       </varlistentry>
542
543       <varlistentry>
544         <term>
545           <option>-v</option>
546           <indexterm><primary><option>-v</option></primary></indexterm>
547         </term>
548         <listitem>
549           <para>The <option>-v</option> option makes GHC
550           <emphasis>verbose</emphasis>: it reports its version number
551           and shows (on stderr) exactly how it invokes each phase of
552           the compilation system.  Moreover, it passes the
553           <option>-v</option> flag to most phases; each reports its
554           version number (and possibly some other information).</para>
555
556           <para>Please, oh please, use the <option>-v</option> option
557           when reporting bugs!  Knowing that you ran the right bits in
558           the right order is always the first thing we want to
559           verify.</para>
560         </listitem>
561       </varlistentry>
562         
563       <varlistentry>
564         <term>
565           <option>-v</option><replaceable>n</replaceable>
566           <indexterm><primary><option>-v</option></primary></indexterm>
567         </term>
568         <listitem>
569           <para>To provide more control over the compiler's verbosity,
570           the <option>-v</option> flag takes an optional numeric
571           argument.  Specifying <option>-v</option> on its own is
572           equivalent to <option>-v3</option>, and the other levels
573           have the following meanings:</para>
574           
575           <variablelist>
576             <varlistentry>
577               <term><option>-v0</option></term>
578               <listitem>
579                 <para>Disable all non-essential messages (this is the
580                 default).</para>
581               </listitem>
582             </varlistentry>
583
584             <varlistentry>
585               <term><option>-v1</option></term>
586               <listitem>
587                 <para>Minimal verbosity: print one line per
588                 compilation (this is the default when
589                 <option>&ndash;&ndash;make</option> or
590                 <option>&ndash;&ndash;interactive</option> is on).</para>
591               </listitem>
592             </varlistentry>
593
594             <varlistentry>
595               <term><option>-v2</option></term>
596               <listitem>
597                 <para>Print the name of each compilation phase as it
598                 is executed. (equivalent to
599                 <option>-dshow-passes</option>).</para>
600               </listitem>
601             </varlistentry>
602
603             <varlistentry>
604               <term><option>-v3</option></term>
605               <listitem>
606                 <para>The same as <option>-v2</option>, except that in
607                 addition the full command line (if appropriate) for
608                 each compilation phase is also printed.</para>
609               </listitem>
610             </varlistentry>
611
612             <varlistentry>
613               <term><option>-v4</option></term>
614               <listitem>
615                 <para>The same as <option>-v3</option> except that the
616                 intermediate program representation after each
617                 compilation phase is also printed (excluding
618                 preprocessed and C/assembly files).</para>
619               </listitem>
620             </varlistentry>
621           </variablelist>
622         </listitem>
623       </varlistentry>
624       
625       <varlistentry>
626         <term>
627           <option>-V</option>
628           <indexterm><primary><option>-V</option></primary></indexterm>
629         </term>
630         <term>
631           <option>&ndash;&ndash;version</option>
632           <indexterm><primary><option>&ndash;&ndash;version</option></primary></indexterm>
633         </term>
634         <listitem>
635           <para>Print a one-line string including GHC's version number.</para>
636         </listitem>
637       </varlistentry>
638
639       <varlistentry>
640         <term>
641           <option>&ndash;&ndash;numeric-version</option>
642           <indexterm><primary><option>&ndash;&ndash;numeric-version</option></primary></indexterm>
643         </term>
644         <listitem>
645           <para>Print GHC's numeric version number only.</para>
646         </listitem>
647       </varlistentry>
648
649       <varlistentry>
650         <term>
651           <option>&ndash;&ndash;print-libdir</option>
652           <indexterm><primary><option>&ndash;&ndash;print-libdir</option></primary></indexterm>
653         </term>
654         <listitem>
655           <para>Print the path to GHC's library directory.  This is
656           the top of the directory tree containing GHC's libraries,
657           interfaces, and include files (usually something like
658           <literal>/usr/local/lib/ghc-5.04</literal> on Unix).  This
659           is the value of
660           <literal>$libdir</literal><indexterm><primary><literal>libdir</literal></primary>
661           </indexterm>in the package configuration file (see <xref
662           linkend="packages"/>).</para>
663         </listitem>
664       </varlistentry>
665
666       <varlistentry>
667         <term><option>-ferror-spans</option>
668           <indexterm><primary><option>-ferror-spans</option></primary>
669           </indexterm>
670         </term>
671         <listitem>
672           <para>Causes GHC to emit the full source span of the
673           syntactic entity relating to an error message.  Normally, GHC
674           emits the source location of the start of the syntactic
675           entity only.</para>
676
677           <para>For example:</para>
678
679 <screen>test.hs:3:6: parse error on input `where'</screen>
680
681           <para>becomes:</para>
682
683 <screen>test296.hs:3:6-10: parse error on input `where'</screen>
684
685           <para>And multi-line spans are possible too:</para>
686
687 <screen>test.hs:(5,4)-(6,7):
688     Conflicting definitions for `a'
689     Bound at: test.hs:5:4
690               test.hs:6:7
691     In the binding group for: a, b, a</screen>
692
693           <para>Note that line numbers start counting at one, but
694           column numbers start at zero.  This choice was made to
695           follow existing convention (i.e. this is how Emacs does
696           it).</para>
697         </listitem>
698       </varlistentry>
699     </variablelist>
700   </sect1>
701
702   &separate;
703
704   <sect1 id="options-sanity">
705     <title>Warnings and sanity-checking</title>
706
707     <indexterm><primary>sanity-checking options</primary></indexterm>
708     <indexterm><primary>warnings</primary></indexterm>
709
710
711     <para>GHC has a number of options that select which types of
712     non-fatal error messages, otherwise known as warnings, can be
713     generated during compilation.  By default, you get a standard set
714     of warnings which are generally likely to indicate bugs in your
715     program.  These are:
716     <option>-fwarn-overlapping-patterns</option>,
717     <option>-fwarn-deprecations</option>,
718     <option>-fwarn-duplicate-exports</option>,
719     <option>-fwarn-missing-fields</option>, and
720     <option>-fwarn-missing-methods</option>.  The following flags are
721     simple ways to select standard &ldquo;packages&rdquo; of warnings:
722     </para>
723
724     <variablelist>
725
726       <varlistentry>
727         <term><option>-W</option>:</term>
728         <listitem>
729           <indexterm><primary>-W option</primary></indexterm>
730           <para>Provides the standard warnings plus
731           <option>-fwarn-incomplete-patterns</option>,
732           <option>-fwarn-unused-matches</option>,
733           <option>-fwarn-unused-imports</option>,
734           <option>-fwarn-misc</option>, and
735           <option>-fwarn-unused-binds</option>.</para>
736         </listitem>
737       </varlistentry>
738
739       <varlistentry>
740         <term><option>-w</option>:</term>
741         <listitem>
742           <indexterm><primary><option>-w</option></primary></indexterm>
743           <para>Turns off all warnings, including the standard ones.</para>
744         </listitem>
745       </varlistentry>
746
747       <varlistentry>
748         <term><option>-Wall</option>:</term>
749         <listitem>
750           <indexterm><primary><option>-Wall</option></primary></indexterm>
751           <para>Turns on all warning options.</para>
752         </listitem>
753       </varlistentry>
754
755       <varlistentry>
756         <term><option>-Werror</option>:</term>
757         <listitem>
758           <indexterm><primary><option>-Werror</option></primary></indexterm>
759           <para>Makes any warning into a fatal error. Useful so that you don't 
760             miss warnings when doing batch compilation. </para>
761         </listitem>
762       </varlistentry>
763
764     </variablelist>
765
766     <para>The full set of warning options is described below.  To turn
767     off any warning, simply give the corresponding
768     <option>-fno-warn-...</option> option on the command line.</para>
769
770     <variablelist>
771
772       <varlistentry>
773         <term><option>-fwarn-deprecations</option>:</term>
774         <listitem>
775           <indexterm><primary><option>-fwarn-deprecations</option></primary>
776           </indexterm>
777           <indexterm><primary>deprecations</primary></indexterm>
778           <para>Causes a warning to be emitted when a deprecated
779           function or type is used.  Entities can be marked as
780           deprecated using a pragma, see <xref
781           linkend="deprecated-pragma"/>.</para>
782         </listitem>
783       </varlistentry>
784
785       <varlistentry>
786         <term><option>-fwarn-duplicate-exports</option>:</term>
787         <listitem>
788           <indexterm><primary><option>-fwarn-duplicate-exports</option></primary></indexterm>
789           <indexterm><primary>duplicate exports, warning</primary></indexterm>
790           <indexterm><primary>export lists, duplicates</primary></indexterm>
791
792           <para>Have the compiler warn about duplicate entries in
793           export lists. This is useful information if you maintain
794           large export lists, and want to avoid the continued export
795           of a definition after you've deleted (one) mention of it in
796           the export list.</para>
797
798           <para>This option is on by default.</para>
799         </listitem>
800       </varlistentry>
801
802       <varlistentry>
803         <term><option>-fwarn-hi-shadowing</option>:</term>
804         <listitem>
805           <indexterm><primary><option>-fwarn-hi-shadowing</option></primary></indexterm>
806           <indexterm><primary>shadowing</primary>
807             <secondary>interface files</secondary></indexterm>
808
809           <para>Causes the compiler to emit a warning when a module or
810           interface file in the current directory is shadowing one
811           with the same module name in a library or other
812           directory.</para>
813         </listitem>
814       </varlistentry>
815
816       <varlistentry>
817         <term><option>-fwarn-incomplete-patterns</option>:</term>
818         <listitem>
819           <indexterm><primary><option>-fwarn-incomplete-patterns</option></primary></indexterm>
820           <indexterm><primary>incomplete patterns, warning</primary></indexterm>
821           <indexterm><primary>patterns, incomplete</primary></indexterm>
822
823           <para>Similarly for incomplete patterns, the function
824           <function>g</function> below will fail when applied to
825           non-empty lists, so the compiler will emit a warning about
826           this when <option>-fwarn-incomplete-patterns</option> is
827           enabled.</para>
828
829 <programlisting>
830 g [] = 2
831 </programlisting>
832
833           <para>This option isn't enabled be default because it can be
834           a bit noisy, and it doesn't always indicate a bug in the
835           program.  However, it's generally considered good practice
836           to cover all the cases in your functions.</para>
837         </listitem>
838       </varlistentry>
839
840       <varlistentry>
841         <term><option>-fwarn-incomplete-record-updates</option>:</term>
842         <listitem>
843           <indexterm><primary><option>-fwarn-incomplete-record-updates</option></primary></indexterm>
844           <indexterm><primary>incomplete record updates, warning</primary></indexterm>
845           <indexterm><primary>record updates, incomplete</primary></indexterm>
846
847           <para>The function
848           <function>f</function> below will fail when applied to
849           <literal>Bar</literal>, so the compiler will emit a warning about
850           this when <option>-fwarn-incomplete-record-updates</option> is
851           enabled.</para>
852
853 <programlisting>
854 data Foo = Foo { x :: Int }
855          | Bar
856
857 f :: Foo -> Foo
858 f foo = foo { x = 6 }
859 </programlisting>
860
861           <para>This option isn't enabled be default because it can be
862           very noisy, and it often doesn't indicate a bug in the
863           program.</para>
864         </listitem>
865       </varlistentry>
866
867       <varlistentry>
868         <term>
869           <option>-fwarn-misc</option>:
870           <indexterm><primary><option>-fwarn-misc</option></primary></indexterm>
871         </term>
872         <listitem>
873           <para>Turns on warnings for various harmless but untidy
874           things.  This currently includes: importing a type with
875           <literal>(..)</literal> when the export is abstract, and
876           listing duplicate class assertions in a qualified type.</para>
877         </listitem>
878       </varlistentry>
879
880       <varlistentry>
881         <term>
882           <option>-fwarn-missing-fields</option>:
883           <indexterm><primary><option>-fwarn-missing-fields</option></primary></indexterm>
884           <indexterm><primary>missing fields, warning</primary></indexterm>
885           <indexterm><primary>fields, missing</primary></indexterm>
886         </term>
887         <listitem>
888
889           <para>This option is on by default, and warns you whenever
890           the construction of a labelled field constructor isn't
891           complete, missing initializers for one or more fields. While
892           not an error (the missing fields are initialised with
893           bottoms), it is often an indication of a programmer error.</para>
894         </listitem>
895       </varlistentry>
896
897       <varlistentry>
898         <term><option>-fwarn-missing-methods</option>:</term>
899         <listitem>
900           <indexterm><primary><option>-fwarn-missing-methods</option></primary></indexterm>
901           <indexterm><primary>missing methods, warning</primary></indexterm>
902           <indexterm><primary>methods, missing</primary></indexterm>
903
904           <para>This option is on by default, and warns you whenever
905           an instance declaration is missing one or more methods, and
906           the corresponding class declaration has no default
907           declaration for them.</para>
908           <para>The warning is suppressed if the method name
909           begins with an underscore.  Here's an example where this is useful:
910             <programlisting>
911               class C a where
912                 _simpleFn :: a -> String
913                 complexFn :: a -> a -> String
914                 complexFn x y = ... _simpleFn ...
915               </programlisting>
916             The idea is that: (a) users of the class will only call <literal>complexFn</literal>; 
917             never <literal>_simpleFn</literal>; and (b)
918             instance declarations can define either <literal>complexFn</literal> or <literal>_simpleFn</literal>.
919             </para>
920         </listitem>
921       </varlistentry>
922
923       <varlistentry>
924         <term><option>-fwarn-missing-signatures</option>:</term>
925         <listitem>
926           <indexterm><primary><option>-fwarn-missing-signatures</option></primary></indexterm>
927           <indexterm><primary>type signatures, missing</primary></indexterm>
928
929           <para>If you would like GHC to check that every top-level
930           function/value has a type signature, use the
931           <option>-fwarn-missing-signatures</option> option.  This
932           option is off by default.</para>
933         </listitem>
934       </varlistentry>
935
936       <varlistentry>
937         <term><option>-fwarn-name-shadowing</option>:</term>
938         <listitem>
939           <indexterm><primary><option>-fwarn-name-shadowing</option></primary></indexterm>
940           <indexterm><primary>shadowing, warning</primary></indexterm>
941           
942           <para>This option causes a warning to be emitted whenever an
943           inner-scope value has the same name as an outer-scope value,
944           i.e. the inner value shadows the outer one.  This can catch
945           typographical errors that turn into hard-to-find bugs, e.g.,
946           in the inadvertent cyclic definition <literal>let x = ... x
947           ... in</literal>.</para>
948
949           <para>Consequently, this option does
950           <emphasis>will</emphasis> complain about cyclic recursive
951           definitions.</para>
952         </listitem>
953       </varlistentry>
954
955       <varlistentry>
956         <term><option>-fwarn-orphans</option>:</term>
957         <listitem>
958           <indexterm><primary><option>-fwarn-orphans</option></primary></indexterm>
959           <indexterm><primary>orphan instances, warning</primary></indexterm>
960           <indexterm><primary>orphan rules, warning</primary></indexterm>
961           
962           <para>This option causes a warning to be emitted whenever the 
963             module contains an "orphan" instance declaration or rewrite rule.
964             An instance declartion is an orphan if it appears in a module in
965             which neither the class nor the type being instanced are declared
966             in the same module.  A rule is an orphan if it is a rule for a
967             function declared in another module.  A module containing any
968           orphans is called an orphan module.</para>
969           <para>The trouble with orphans is that GHC must pro-actively read the interface
970             files for all orphan modules, just in case their instances or rules
971             play a role, whether or not the module's interface would otherwise 
972             be of any use.  Other things being equal, avoid orphan modules.</para>
973         </listitem>
974       </varlistentry>
975
976       <varlistentry>
977         <term>
978           <option>-fwarn-overlapping-patterns</option>:
979           <indexterm><primary><option>-fwarn-overlapping-patterns</option></primary></indexterm>
980           <indexterm><primary>overlapping patterns, warning</primary></indexterm>
981           <indexterm><primary>patterns, overlapping</primary></indexterm>
982         </term>
983         <listitem>
984           <para>By default, the compiler will warn you if a set of
985           patterns are overlapping, i.e.,</para>
986
987 <programlisting>
988 f :: String -&#62; Int
989 f []     = 0
990 f (_:xs) = 1
991 f "2"    = 2
992 </programlisting>
993
994           <para>where the last pattern match in <function>f</function>
995           won't ever be reached, as the second pattern overlaps
996           it. More often than not, redundant patterns is a programmer
997           mistake/error, so this option is enabled by default.</para>
998         </listitem>
999       </varlistentry>
1000
1001       <varlistentry>
1002         <term><option>-fwarn-simple-patterns</option>:</term>
1003         <listitem>
1004           <indexterm><primary><option>-fwarn-simple-patterns</option></primary>
1005           </indexterm>
1006           <para>Causes the compiler to warn about lambda-bound
1007           patterns that can fail, eg. <literal>\(x:xs)->...</literal>.
1008           Normally, these aren't treated as incomplete patterns by
1009           <option>-fwarn-incomplete-patterns</option>.</para>
1010           <para>``Lambda-bound patterns'' includes all places where there is a single pattern,
1011             including list comprehensions and do-notation.  In these cases, a pattern-match 
1012             failure is quite legitimate, and triggers filtering (list comprehensions) or
1013             the monad <literal>fail</literal> operation (monads). For example:
1014             <programlisting>
1015               f :: [Maybe a] -> [a]
1016               f xs = [y | Just y &lt;- xs]
1017               </programlisting>
1018             Switching on <option>-fwarn-simple-patterns</option> will elicit warnings about
1019             these probably-innocent cases, which is why the flag is off by default. </para>
1020           <para> The <literal>deriving( Read )</literal> mechanism produces monadic code with
1021             pattern matches, so you will also get misleading warnings about the compiler-generated
1022             code.  (This is arguably a Bad Thing, but it's awkward to fix.)</para>
1023
1024         </listitem>
1025       </varlistentry>
1026
1027       <varlistentry>
1028         <term><option>-fwarn-type-defaults</option>:</term>
1029         <listitem>
1030           <indexterm><primary><option>-fwarn-type-defaults</option></primary></indexterm>
1031           <indexterm><primary>defaulting mechanism, warning</primary></indexterm>
1032           <para>Have the compiler warn/inform you where in your source
1033           the Haskell defaulting mechanism for numeric types kicks
1034           in. This is useful information when converting code from a
1035           context that assumed one default into one with another,
1036           e.g., the `default default' for Haskell 1.4 caused the
1037           otherwise unconstrained value <constant>1</constant> to be
1038           given the type <literal>Int</literal>, whereas Haskell 98
1039           defaults it to <literal>Integer</literal>.  This may lead to
1040           differences in performance and behaviour, hence the
1041           usefulness of being non-silent about this.</para>
1042
1043           <para>This warning is off by default.</para>
1044         </listitem>
1045       </varlistentry>
1046
1047       <varlistentry>
1048         <term><option>-fwarn-unused-binds</option>:</term>
1049         <listitem>
1050           <indexterm><primary><option>-fwarn-unused-binds</option></primary></indexterm>
1051           <indexterm><primary>unused binds, warning</primary></indexterm>
1052           <indexterm><primary>binds, unused</primary></indexterm>
1053           <para>Report any function definitions (and local bindings)
1054           which are unused.  For top-level functions, the warning is
1055           only given if the binding is not exported.</para>
1056           <para>A definition is regarded as "used" if (a) it is exported, or (b) it is
1057             mentioned in the right hand side of another definition that is used, or (c) the 
1058             function it defines begins with an underscore.  The last case provides a 
1059             way to suppress unused-binding warnings selectively.  </para>
1060           <para> Notice that a variable
1061             is reported as unused even if it appears in the right-hand side of another
1062             unused binding. </para>
1063         </listitem>
1064       </varlistentry>
1065
1066       <varlistentry>
1067         <term><option>-fwarn-unused-imports</option>:</term>
1068         <listitem>
1069           <indexterm><primary><option>-fwarn-unused-imports</option></primary></indexterm>
1070           <indexterm><primary>unused imports, warning</primary></indexterm>
1071           <indexterm><primary>imports, unused</primary></indexterm>
1072
1073           <para>Report any modules that are explicitly imported but
1074           never used.  However, the form <literal>import M()</literal> is
1075           never reported as an unused import, because it is a useful idiom
1076           for importing instance declarations, which are anonymous in Haskell.</para>
1077         </listitem>
1078       </varlistentry>
1079
1080       <varlistentry>
1081         <term><option>-fwarn-unused-matches</option>:</term>
1082         <listitem>
1083           <indexterm><primary><option>-fwarn-unused-matches</option></primary></indexterm>
1084           <indexterm><primary>unused matches, warning</primary></indexterm>
1085           <indexterm><primary>matches, unused</primary></indexterm>
1086
1087           <para>Report all unused variables which arise from pattern
1088           matches, including patterns consisting of a single variable.
1089           For instance <literal>f x y = []</literal> would report
1090           <varname>x</varname> and <varname>y</varname> as unused.  The
1091           warning is suppressed if the variable name begins with an underscore, thus:
1092             <programlisting>
1093                f _x = True
1094             </programlisting>
1095           </para>
1096         </listitem>
1097       </varlistentry>
1098
1099     </variablelist>
1100
1101     <para>If you're feeling really paranoid, the
1102     <option>-dcore-lint</option>
1103     option<indexterm><primary><option>-dcore-lint</option></primary></indexterm>
1104     is a good choice.  It turns on heavyweight intra-pass
1105     sanity-checking within GHC.  (It checks GHC's sanity, not
1106     yours.)</para>
1107
1108   </sect1>
1109
1110   &packages;
1111
1112   <sect1 id="options-optimise">
1113     <title>Optimisation (code improvement)</title>
1114
1115     <indexterm><primary>optimisation</primary></indexterm>
1116     <indexterm><primary>improvement, code</primary></indexterm>
1117
1118     <para>The <option>-O*</option> options specify convenient
1119     &ldquo;packages&rdquo; of optimisation flags; the
1120     <option>-f*</option> options described later on specify
1121     <emphasis>individual</emphasis> optimisations to be turned on/off;
1122     the <option>-m*</option> options specify
1123     <emphasis>machine-specific</emphasis> optimisations to be turned
1124     on/off.</para>
1125
1126     <sect2 id="optimise-pkgs">
1127       <title><option>-O*</option>: convenient &ldquo;packages&rdquo; of optimisation flags.</title>
1128
1129       <para>There are <emphasis>many</emphasis> options that affect
1130       the quality of code produced by GHC.  Most people only have a
1131       general goal, something like &ldquo;Compile quickly&rdquo; or
1132       &ldquo;Make my program run like greased lightning.&rdquo; The
1133       following &ldquo;packages&rdquo; of optimisations (or lack
1134       thereof) should suffice.</para>
1135
1136       <para>Note that higher optimisation levels cause more
1137       cross-module optimisation to be performed, which can have an
1138       impact on how much of your program needs to be recompiled when
1139       you change something.  This is one reaosn to stick to
1140       no-optimisation when developing code.</para>
1141
1142       <variablelist>
1143
1144         <varlistentry>
1145           <term>
1146             No <option>-O*</option>-type option specified:
1147             <indexterm><primary>-O* not specified</primary></indexterm>
1148           </term>
1149           <listitem>
1150             <para>This is taken to mean: &ldquo;Please compile
1151             quickly; I'm not over-bothered about compiled-code
1152             quality.&rdquo; So, for example: <command>ghc -c
1153             Foo.hs</command></para>
1154           </listitem>
1155         </varlistentry>
1156
1157         <varlistentry>
1158           <term>
1159             <option>-O0</option>:
1160             <indexterm><primary><option>-O0</option></primary></indexterm>
1161           </term>
1162           <listitem>
1163             <para>Means &ldquo;turn off all optimisation&rdquo;,
1164             reverting to the same settings as if no
1165             <option>-O</option> options had been specified.  Saying
1166             <option>-O0</option> can be useful if
1167             eg. <command>make</command> has inserted a
1168             <option>-O</option> on the command line already.</para>
1169           </listitem>
1170         </varlistentry>
1171
1172         <varlistentry>
1173           <term>
1174             <option>-O</option> or <option>-O1</option>:
1175             <indexterm><primary>-O option</primary></indexterm>
1176             <indexterm><primary>-O1 option</primary></indexterm>
1177             <indexterm><primary>optimise</primary><secondary>normally</secondary></indexterm>
1178           </term>
1179           <listitem>
1180             <para>Means: &ldquo;Generate good-quality code without
1181             taking too long about it.&rdquo; Thus, for example:
1182             <command>ghc -c -O Main.lhs</command></para>
1183
1184             <para><option>-O</option> currently also implies
1185             <option>-fvia-C</option>.  This may change in the
1186             future.</para>
1187           </listitem>
1188         </varlistentry>
1189
1190         <varlistentry>
1191           <term>
1192             <option>-O2</option>:
1193             <indexterm><primary>-O2 option</primary></indexterm>
1194             <indexterm><primary>optimise</primary><secondary>aggressively</secondary></indexterm>
1195           </term>
1196           <listitem>
1197             <para>Means: &ldquo;Apply every non-dangerous
1198             optimisation, even if it means significantly longer
1199             compile times.&rdquo;</para>
1200
1201             <para>The avoided &ldquo;dangerous&rdquo; optimisations
1202             are those that can make runtime or space
1203             <emphasis>worse</emphasis> if you're unlucky.  They are
1204             normally turned on or off individually.</para>
1205
1206             <para>At the moment, <option>-O2</option> is
1207             <emphasis>unlikely</emphasis> to produce better code than
1208             <option>-O</option>.</para>
1209           </listitem>
1210         </varlistentry>
1211
1212         <varlistentry>
1213           <term>
1214             <option>-Ofile &lt;file&gt;</option>:
1215             <indexterm><primary>-Ofile &lt;file&gt; option</primary></indexterm>
1216             <indexterm><primary>optimising, customised</primary></indexterm>
1217           </term>
1218           <listitem>
1219             <para>(NOTE: not supported since GHC 4.x.  Please ask if
1220             you're interested in this.)</para>
1221             
1222             <para>For those who need <emphasis>absolute</emphasis>
1223             control over <emphasis>exactly</emphasis> what options are
1224             used (e.g., compiler writers, sometimes :-), a list of
1225             options can be put in a file and then slurped in with
1226             <option>-Ofile</option>.</para>
1227
1228             <para>In that file, comments are of the
1229             <literal>&num;</literal>-to-end-of-line variety; blank
1230             lines and most whitespace is ignored.</para>
1231
1232             <para>Please ask if you are baffled and would like an
1233             example of <option>-Ofile</option>!</para>
1234           </listitem>
1235         </varlistentry>
1236       </variablelist>
1237
1238       <para>We don't use a <option>-O*</option> flag for day-to-day
1239       work.  We use <option>-O</option> to get respectable speed;
1240       e.g., when we want to measure something.  When we want to go for
1241       broke, we tend to use <option>-O2 -fvia-C</option> (and we go for
1242       lots of coffee breaks).</para>
1243
1244       <para>The easiest way to see what <option>-O</option> (etc.)
1245       &ldquo;really mean&rdquo; is to run with <option>-v</option>,
1246       then stand back in amazement.</para>
1247     </sect2>
1248
1249     <sect2 id="options-f">
1250       <title><option>-f*</option>: platform-independent flags</title>
1251
1252       <indexterm><primary>-f* options (GHC)</primary></indexterm>
1253       <indexterm><primary>-fno-* options (GHC)</primary></indexterm>
1254
1255       <para>These flags turn on and off individual optimisations.
1256       They are normally set via the <option>-O</option> options
1257       described above, and as such, you shouldn't need to set any of
1258       them explicitly (indeed, doing so could lead to unexpected
1259       results).  However, there are one or two that may be of
1260       interest:</para>
1261
1262       <variablelist>
1263         <varlistentry>
1264           <term><option>-fexcess-precision</option>:</term>
1265           <listitem>
1266             <indexterm><primary><option>-fexcess-precision</option></primary></indexterm>
1267             <para>When this option is given, intermediate floating
1268             point values can have a <emphasis>greater</emphasis>
1269             precision/range than the final type.  Generally this is a
1270             good thing, but some programs may rely on the exact
1271             precision/range of
1272             <literal>Float</literal>/<literal>Double</literal> values
1273             and should not use this option for their compilation.</para>
1274           </listitem>
1275         </varlistentry>
1276
1277         <varlistentry>
1278           <term><option>-fignore-asserts</option>:</term>
1279           <listitem>
1280             <indexterm><primary><option>-fignore-asserts</option></primary></indexterm>
1281             <para>Causes GHC to ignore uses of the function
1282             <literal>Exception.assert</literal> in source code (in
1283             other words, rewriting <literal>Exception.assert p
1284             e</literal> to <literal>e</literal> (see <xref
1285             linkend="sec-assertions"/>).  This flag is turned on by
1286             <option>-O</option>.
1287             </para>
1288           </listitem>
1289         </varlistentry>
1290
1291         <varlistentry>
1292           <term>
1293             <option>-fno-cse</option>
1294             <indexterm><primary><option>-fno-cse</option></primary></indexterm>
1295           </term>
1296           <listitem>
1297             <para>Turns off the common-sub-expression elimination optimisation.
1298               Can be useful if you have some <literal>unsafePerformIO</literal>
1299             expressions that you don't want commoned-up.</para>
1300           </listitem>
1301         </varlistentry>
1302
1303         <varlistentry>
1304           <term>
1305             <option>-fno-strictness</option>
1306             <indexterm><primary><option>-fno-strictness</option></primary></indexterm>
1307           </term>
1308           <listitem>
1309             <para>Turns off the strictness analyser; sometimes it eats
1310             too many cycles.</para>
1311           </listitem>
1312         </varlistentry>
1313
1314         <varlistentry>
1315           <term>
1316             <option>-fno-full-laziness</option>
1317             <indexterm><primary><option>-fno-full-laziness</option></primary></indexterm>
1318           </term>
1319           <listitem>
1320             <para>Turns off the full laziness optimisation (also known as
1321               let-floating).  Full laziness increases sharing, which can lead
1322               to increased memory residency.</para>
1323           </listitem>
1324         </varlistentry>
1325
1326         <varlistentry>
1327           <term>
1328             <option>-fno-state-hack</option>
1329             <indexterm><primary><option>-fno-state-hack</option></primary></indexterm>
1330           </term>
1331           <listitem>
1332             <para>Turn off the "state hack" whereby any lambda with a
1333               <literal>State#</literal> token as argument is considered to be
1334               single-entry, hence it is considered OK to inline things inside
1335               it.  This can improve performance of IO and ST monad code, but it
1336             runs the risk of reducing sharing.</para> 
1337           </listitem>
1338         </varlistentry>
1339
1340         <varlistentry>
1341           <term>
1342             <option>-funbox-strict-fields</option>:
1343             <indexterm><primary><option>-funbox-strict-fields</option></primary></indexterm>
1344             <indexterm><primary>strict constructor fields</primary></indexterm>
1345             <indexterm><primary>constructor fields, strict</primary></indexterm>
1346           </term>
1347           <listitem>
1348             <para>This option causes all constructor fields which are
1349             marked strict (i.e. &ldquo;!&rdquo;) to be unboxed or
1350             unpacked if possible.  It is equivalent to adding an
1351             <literal>UNPACK</literal> pragma to every strict
1352             constructor field (see <xref
1353             linkend="unpack-pragma"/>).</para>
1354
1355             <para>This option is a bit of a sledgehammer: it might
1356             sometimes make things worse.  Selectively unboxing fields
1357             by using <literal>UNPACK</literal> pragmas might be
1358             better.</para>
1359           </listitem>
1360         </varlistentry>
1361
1362         <varlistentry>
1363           <term>
1364             <option>-funfolding-update-in-place&lt;n&gt;</option>
1365             <indexterm><primary><option>-funfolding-update-in-place</option></primary></indexterm>
1366           </term>
1367           <listitem>
1368             <para>Switches on an experimental "optimisation".
1369             Switching it on makes the compiler a little keener to
1370             inline a function that returns a constructor, if the
1371             context is that of a thunk.
1372 <programlisting>
1373    x = plusInt a b
1374 </programlisting>
1375             If we inlined plusInt we might get an opportunity to use
1376             update-in-place for the thunk 'x'.</para>
1377           </listitem>
1378         </varlistentry>
1379
1380         <varlistentry>
1381           <term>
1382             <option>-funfolding-creation-threshold&lt;n&gt;</option>:
1383             <indexterm><primary><option>-funfolding-creation-threshold</option></primary></indexterm>
1384             <indexterm><primary>inlining, controlling</primary></indexterm>
1385             <indexterm><primary>unfolding, controlling</primary></indexterm>
1386           </term>
1387           <listitem>
1388             <para>(Default: 45) Governs the maximum size that GHC will 
1389             allow a function unfolding to be.   (An unfolding has a
1390             &ldquo;size&rdquo; that reflects the cost in terms of
1391             &ldquo;code bloat&rdquo; of expanding that unfolding at
1392             at a call site. A bigger function would be assigned a
1393             bigger cost.) </para>
1394
1395             <para> Consequences: (a) nothing larger than this will be
1396             inlined (unless it has an INLINE pragma); (b) nothing
1397             larger than this will be spewed into an interface
1398             file. </para>
1399
1400
1401             <para> Increasing this figure is more likely to result in longer
1402             compile times than faster code.  The next option is more
1403             useful:</para>
1404           </listitem>
1405         </varlistentry>
1406
1407         <varlistentry>
1408           <term><option>-funfolding-use-threshold&lt;n&gt;</option>:</term>
1409           <listitem>
1410             <indexterm><primary><option>-funfolding-use-threshold</option></primary></indexterm>
1411             <indexterm><primary>inlining, controlling</primary></indexterm>
1412             <indexterm><primary>unfolding, controlling</primary></indexterm>
1413
1414             <para>(Default: 8) This is the magic cut-off figure for
1415             unfolding: below this size, a function definition will be
1416             unfolded at the call-site, any bigger and it won't.  The
1417             size computed for a function depends on two things: the
1418             actual size of the expression minus any discounts that
1419             apply (see <option>-funfolding-con-discount</option>).</para>
1420           </listitem>
1421         </varlistentry>
1422       </variablelist>
1423
1424     </sect2>
1425     
1426   </sect1>
1427   
1428   &phases;  
1429   
1430   <sect1 id="sec-using-concurrent">
1431 <title>Using Concurrent Haskell</title>
1432
1433              <indexterm><primary>Concurrent Haskell&mdash;use</primary></indexterm>
1434
1435 <para>
1436 GHC supports Concurrent Haskell by default, without requiring a
1437 special option or libraries compiled in a certain way.  To get access
1438 to the support libraries for Concurrent Haskell, just import
1439 <literal>Control.Concurrent</literal> (details are in the accompanying
1440 library documentation).</para>
1441
1442 <para>
1443 RTS options are provided for modifying the behaviour of the threaded
1444 runtime system.  See <xref linkend="parallel-rts-opts"/>.
1445 </para>
1446
1447 <para>
1448 Concurrent Haskell is described in more detail in the documentation
1449 for the <literal>Control.Concurrent</literal> module.
1450 </para>
1451
1452 </sect1>
1453
1454 <sect1 id="sec-using-parallel">
1455 <title>Using parallel Haskell</title>
1456
1457 <para>
1458 <indexterm><primary>parallel Haskell&mdash;use</primary></indexterm>
1459 </para>
1460
1461 <para>
1462 &lsqb;You won't be able to execute parallel Haskell programs unless PVM3
1463 (parallel Virtual Machine, version 3) is installed at your site.&rsqb;
1464 </para>
1465
1466 <para>
1467 To compile a Haskell program for parallel execution under PVM, use the
1468 <option>-parallel</option> option,<indexterm><primary>-parallel
1469 option</primary></indexterm> both when compiling <emphasis>and
1470 linking</emphasis>.  You will probably want to <literal>import
1471 parallel</literal> into your Haskell modules.
1472 </para>
1473
1474 <para>
1475 To run your parallel program, once PVM is going, just invoke it
1476 &ldquo;as normal&rdquo;.  The main extra RTS option is
1477 <option>-qp&lt;n&gt;</option>, to say how many PVM
1478 &ldquo;processors&rdquo; your program to run on.  (For more details of
1479 all relevant RTS options, please see <xref
1480 linkend="parallel-rts-opts"/>.)
1481 </para>
1482
1483 <para>
1484 In truth, running parallel Haskell programs and getting information
1485 out of them (e.g., parallelism profiles) is a battle with the vagaries of
1486 PVM, detailed in the following sections.
1487 </para>
1488
1489 <sect2 id="pvm-dummies">
1490 <title>Dummy's guide to using PVM</title>
1491
1492 <para>
1493 <indexterm><primary>PVM, how to use</primary></indexterm>
1494 <indexterm><primary>parallel Haskell&mdash;PVM use</primary></indexterm>
1495 Before you can run a parallel program under PVM, you must set the
1496 required environment variables (PVM's idea, not ours); something like,
1497 probably in your <filename>.cshrc</filename> or equivalent:
1498
1499 <programlisting>
1500 setenv PVM_ROOT /wherever/you/put/it
1501 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
1502 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
1503 </programlisting>
1504
1505 </para>
1506
1507 <para>
1508 Creating and/or controlling your &ldquo;parallel machine&rdquo; is a purely-PVM
1509 business; nothing specific to parallel Haskell. The following paragraphs
1510 describe how to configure your parallel machine interactively.
1511 </para>
1512
1513 <para>
1514 If you use parallel Haskell regularly on the same machine configuration it
1515 is a good idea to maintain a file with all machine names and to make the
1516 environment variable PVM_HOST_FILE point to this file. Then you can avoid
1517 the interactive operations described below by just saying
1518 </para>
1519
1520 <programlisting>
1521 pvm $PVM_HOST_FILE
1522 </programlisting>
1523
1524 <para>
1525 You use the <command>pvm</command><indexterm><primary>pvm command</primary></indexterm> command to start PVM on your
1526 machine.  You can then do various things to control/monitor your
1527 &ldquo;parallel machine;&rdquo; the most useful being:
1528 </para>
1529
1530 <para>
1531 <informaltable>
1532 <tgroup cols="2">
1533 <colspec align="left"/>
1534 <tbody>
1535
1536 <row>
1537 <entry><keycombo><keycap>Control</keycap><keycap>D</keycap></keycombo></entry>
1538 <entry>exit <command>pvm</command>, leaving it running</entry>
1539 </row>
1540
1541 <row>
1542 <entry><command>halt</command></entry>
1543 <entry>kill off this &ldquo;parallel machine&rdquo; &amp; exit</entry>
1544 </row>
1545
1546 <row>
1547 <entry><command>add &lt;host&gt;</command></entry>
1548 <entry>add <command>&lt;host&gt;</command> as a processor</entry>
1549 </row>
1550
1551 <row>
1552 <entry><command>delete &lt;host&gt;</command></entry>
1553 <entry>delete <command>&lt;host&gt;</command></entry>
1554 </row>
1555
1556 <row>
1557 <entry><command>reset</command></entry>
1558 <entry>kill what's going, but leave PVM up</entry>
1559 </row>
1560
1561 <row>
1562 <entry><command>conf</command></entry>
1563 <entry>list the current configuration</entry>
1564 </row>
1565
1566 <row>
1567 <entry><command>ps</command></entry>
1568 <entry>report processes' status</entry>
1569 </row>
1570
1571 <row>
1572 <entry><command>pstat &lt;pid&gt;</command></entry>
1573 <entry>status of a particular process</entry>
1574 </row>
1575
1576 </tbody>
1577 </tgroup>
1578 </informaltable>
1579 </para>
1580
1581 <para>
1582 The PVM documentation can tell you much, much more about <command>pvm</command>!
1583 </para>
1584
1585 </sect2>
1586
1587 <sect2 id="par-profiles">
1588 <title>parallelism profiles</title>
1589
1590 <para>
1591 <indexterm><primary>parallelism profiles</primary></indexterm>
1592 <indexterm><primary>profiles, parallelism</primary></indexterm>
1593 <indexterm><primary>visualisation tools</primary></indexterm>
1594 </para>
1595
1596 <para>
1597 With parallel Haskell programs, we usually don't care about the
1598 results&mdash;only with &ldquo;how parallel&rdquo; it was!  We want pretty pictures.
1599 </para>
1600
1601 <para>
1602 parallelism profiles (&agrave; la <command>hbcpp</command>) can be generated with the
1603 <option>-qP</option><indexterm><primary>-qP RTS option (concurrent, parallel)</primary></indexterm> RTS option.  The
1604 per-processor profiling info is dumped into files named
1605 <filename>&lt;full-path&gt;&lt;program&gt;.gr</filename>.  These are then munged into a PostScript picture,
1606 which you can then display.  For example, to run your program
1607 <filename>a.out</filename> on 8 processors, then view the parallelism profile, do:
1608 </para>
1609
1610 <para>
1611
1612 <screen>
1613 <prompt>&dollar;</prompt> ./a.out +RTS -qP -qp8
1614 <prompt>&dollar;</prompt> grs2gr *.???.gr &#62; temp.gr # combine the 8 .gr files into one
1615 <prompt>&dollar;</prompt> gr2ps -O temp.gr              # cvt to .ps; output in temp.ps
1616 <prompt>&dollar;</prompt> ghostview -seascape temp.ps   # look at it!
1617 </screen>
1618
1619 </para>
1620
1621 <para>
1622 The scripts for processing the parallelism profiles are distributed
1623 in <filename>ghc/utils/parallel/</filename>.
1624 </para>
1625
1626 </sect2>
1627
1628 <sect2>
1629 <title>Other useful info about running parallel programs</title>
1630
1631 <para>
1632 The &ldquo;garbage-collection statistics&rdquo; RTS options can be useful for
1633 seeing what parallel programs are doing.  If you do either
1634 <option>+RTS -Sstderr</option><indexterm><primary>-Sstderr RTS option</primary></indexterm> or <option>+RTS -sstderr</option>, then
1635 you'll get mutator, garbage-collection, etc., times on standard
1636 error. The standard error of all PE's other than the `main thread'
1637 appears in <filename>/tmp/pvml.nnn</filename>, courtesy of PVM.
1638 </para>
1639
1640 <para>
1641 Whether doing <option>+RTS -Sstderr</option> or not, a handy way to watch
1642 what's happening overall is: <command>tail -f /tmp/pvml.nnn</command>.
1643 </para>
1644
1645 </sect2>
1646
1647 <sect2 id="parallel-rts-opts">
1648 <title>RTS options for Concurrent/parallel Haskell
1649 </title>
1650
1651 <para>
1652 <indexterm><primary>RTS options, concurrent</primary></indexterm>
1653 <indexterm><primary>RTS options, parallel</primary></indexterm>
1654 <indexterm><primary>Concurrent Haskell&mdash;RTS options</primary></indexterm>
1655 <indexterm><primary>parallel Haskell&mdash;RTS options</primary></indexterm>
1656 </para>
1657
1658 <para>
1659 Besides the usual runtime system (RTS) options
1660 (<xref linkend="runtime-control"/>), there are a few options particularly
1661 for concurrent/parallel execution.
1662 </para>
1663
1664 <para>
1665 <variablelist>
1666
1667 <varlistentry>
1668 <term><option>-qp&lt;N&gt;</option>:</term>
1669 <listitem>
1670 <para>
1671 <indexterm><primary>-qp&lt;N&gt; RTS option</primary></indexterm>
1672 (paraLLEL ONLY) Use <literal>&lt;N&gt;</literal> PVM processors to run this program;
1673 the default is 2.
1674 </para>
1675 </listitem>
1676 </varlistentry>
1677 <varlistentry>
1678 <term><option>-C[&lt;us&gt;]</option>:</term>
1679 <listitem>
1680 <para>
1681 <indexterm><primary>-C&lt;us&gt; RTS option</primary></indexterm> Sets
1682 the context switch interval to <literal>&lt;s&gt;</literal> seconds.
1683 A context switch will occur at the next heap block allocation after
1684 the timer expires (a heap block allocation occurs every 4k of
1685 allocation).  With <option>-C0</option> or <option>-C</option>,
1686 context switches will occur as often as possible (at every heap block
1687 allocation).  By default, context switches occur every 20ms
1688 milliseconds.  Note that GHC's internal timer ticks every 20ms, and
1689 the context switch timer is always a multiple of this timer, so 20ms
1690 is the maximum granularity available for timed context switches.
1691 </para>
1692 </listitem>
1693 </varlistentry>
1694 <varlistentry>
1695 <term><option>-q[v]</option>:</term>
1696 <listitem>
1697 <para>
1698 <indexterm><primary>-q RTS option</primary></indexterm>
1699 (paraLLEL ONLY) Produce a quasi-parallel profile of thread activity,
1700 in the file <filename>&lt;program&gt;.qp</filename>.  In the style of <command>hbcpp</command>, this profile
1701 records the movement of threads between the green (runnable) and red
1702 (blocked) queues.  If you specify the verbose suboption (<option>-qv</option>), the
1703 green queue is split into green (for the currently running thread
1704 only) and amber (for other runnable threads).  We do not recommend
1705 that you use the verbose suboption if you are planning to use the
1706 <command>hbcpp</command> profiling tools or if you are context switching at every heap
1707 check (with <option>-C</option>).
1708 -->
1709 </para>
1710 </listitem>
1711 </varlistentry>
1712 <varlistentry>
1713 <term><option>-qt&lt;num&gt;</option>:</term>
1714 <listitem>
1715 <para>
1716 <indexterm><primary>-qt&lt;num&gt; RTS option</primary></indexterm>
1717 (paraLLEL ONLY) Limit the thread pool size, i.e. the number of concurrent
1718 threads per processor to <literal>&lt;num&gt;</literal>.  The default is
1719 32.  Each thread requires slightly over 1K <emphasis>words</emphasis> in
1720 the heap for thread state and stack objects.  (For 32-bit machines, this
1721 translates to 4K bytes, and for 64-bit machines, 8K bytes.)
1722 </para>
1723 </listitem>
1724 </varlistentry>
1725 <!-- no more -HWL
1726 <varlistentry>
1727 <term><option>-d</option>:</term>
1728 <listitem>
1729 <para>
1730 <indexterm><primary>-d RTS option (parallel)</primary></indexterm>
1731 (paraLLEL ONLY) Turn on debugging.  It pops up one xterm (or GDB, or
1732 something&hellip;) per PVM processor.  We use the standard <command>debugger</command>
1733 script that comes with PVM3, but we sometimes meddle with the
1734 <command>debugger2</command> script.  We include ours in the GHC distribution,
1735 in <filename>ghc/utils/pvm/</filename>.
1736 </para>
1737 </listitem>
1738 </varlistentry>
1739 -->
1740 <varlistentry>
1741 <term><option>-qe&lt;num&gt;</option>:</term>
1742 <listitem>
1743 <para>
1744 <indexterm><primary>-qe&lt;num&gt; RTS option
1745 (parallel)</primary></indexterm> (paraLLEL ONLY) Limit the spark pool size
1746 i.e. the number of pending sparks per processor to
1747 <literal>&lt;num&gt;</literal>. The default is 100. A larger number may be
1748 appropriate if your program generates large amounts of parallelism
1749 initially.
1750 </para>
1751 </listitem>
1752 </varlistentry>
1753 <varlistentry>
1754 <term><option>-qQ&lt;num&gt;</option>:</term>
1755 <listitem>
1756 <para>
1757 <indexterm><primary>-qQ&lt;num&gt; RTS option (parallel)</primary></indexterm>
1758 (paraLLEL ONLY) Set the size of packets transmitted between processors
1759 to <literal>&lt;num&gt;</literal>. The default is 1024 words. A larger number may be
1760 appropriate if your machine has a high communication cost relative to
1761 computation speed.
1762 </para>
1763 </listitem>
1764 </varlistentry>
1765 <varlistentry>
1766 <term><option>-qh&lt;num&gt;</option>:</term>
1767 <listitem>
1768 <para>
1769 <indexterm><primary>-qh&lt;num&gt; RTS option (parallel)</primary></indexterm>
1770 (paraLLEL ONLY) Select a packing scheme. Set the number of non-root thunks to pack in one packet to
1771 &lt;num&gt;-1 (0 means infinity). By default GUM uses full-subgraph
1772 packing, i.e. the entire subgraph with the requested closure as root is
1773 transmitted (provided it fits into one packet). Choosing a smaller value
1774 reduces the amount of pre-fetching of work done in GUM. This can be
1775 advantageous for improving data locality but it can also worsen the balance
1776 of the load in the system. 
1777 </para>
1778 </listitem>
1779 </varlistentry>
1780 <varlistentry>
1781 <term><option>-qg&lt;num&gt;</option>:</term>
1782 <listitem>
1783 <para>
1784 <indexterm><primary>-qg&lt;num&gt; RTS option
1785 (parallel)</primary></indexterm> (paraLLEL ONLY) Select a globalisation
1786 scheme. This option affects the
1787 generation of global addresses when transferring data. Global addresses are
1788 globally unique identifiers required to maintain sharing in the distributed
1789 graph structure. Currently this is a binary option. With &lt;num&gt;=0 full globalisation is used
1790 (default). This means a global address is generated for every closure that
1791 is transmitted. With &lt;num&gt;=1 a thunk-only globalisation scheme is
1792 used, which generated global address only for thunks. The latter case may
1793 lose sharing of data but has a reduced overhead in packing graph structures
1794 and maintaining internal tables of global addresses.
1795 </para>
1796 </listitem>
1797 </varlistentry>
1798 </variablelist>
1799 </para>
1800
1801 </sect2>
1802
1803 </sect1>
1804
1805   <sect1 id="options-platform">
1806     <title>Platform-specific Flags</title>
1807
1808     <indexterm><primary>-m* options</primary></indexterm>
1809     <indexterm><primary>platform-specific options</primary></indexterm>
1810     <indexterm><primary>machine-specific options</primary></indexterm>
1811
1812     <para>Some flags only make sense for particular target
1813     platforms.</para>
1814
1815     <variablelist>
1816
1817       <varlistentry>
1818         <term><option>-mv8</option>:</term>
1819         <listitem>
1820           <para>(SPARC machines)<indexterm><primary>-mv8 option (SPARC
1821           only)</primary></indexterm> Means to pass the like-named
1822           option to GCC; it says to use the Version 8 SPARC
1823           instructions, notably integer multiply and divide.  The
1824           similar <option>-m*</option> GCC options for SPARC also
1825           work, actually.</para>
1826         </listitem>
1827       </varlistentry>
1828
1829       <varlistentry>
1830         <term><option>-monly-[32]-regs</option>:</term>
1831         <listitem>
1832           <para>(iX86 machines)<indexterm><primary>-monly-N-regs
1833           option (iX86 only)</primary></indexterm> GHC tries to
1834           &ldquo;steal&rdquo; four registers from GCC, for performance
1835           reasons; it almost always works.  However, when GCC is
1836           compiling some modules with four stolen registers, it will
1837           crash, probably saying:
1838
1839 <screen>
1840 Foo.hc:533: fixed or forbidden register was spilled.
1841 This may be due to a compiler bug or to impossible asm
1842 statements or clauses.
1843 </screen>
1844
1845           Just give some registers back with
1846           <option>-monly-N-regs</option>.  Try `3' first, then `2'.
1847           If `2' doesn't work, please report the bug to us.</para>
1848         </listitem>
1849       </varlistentry>
1850     </variablelist>
1851
1852   </sect1>
1853
1854 &runtime;
1855
1856 <sect1 id="ext-core">
1857   <title>Generating and compiling External Core Files</title>
1858
1859   <indexterm><primary>intermediate code generation</primary></indexterm>
1860
1861   <para>GHC can dump its optimized intermediate code (said to be in &ldquo;Core&rdquo; format) 
1862   to a file as a side-effect of compilation. Core files, which are given the suffix
1863   <filename>.hcr</filename>, can be read and processed by non-GHC back-end
1864   tools.  The Core format is formally described in <ulink url="http://www.haskell.org/ghc/docs/papers/core.ps.gz">
1865   <citetitle>An External Representation for the GHC Core Language</citetitle></ulink>, 
1866   and sample tools (in Haskell)
1867   for manipulating Core files are available in the GHC source distribution 
1868   directory <literal>/fptools/ghc/utils/ext-core</literal>.  
1869   Note that the format of <literal>.hcr</literal> 
1870   files is <emphasis>different</emphasis> (though similar) to the Core output format generated 
1871   for debugging purposes (<xref linkend="options-debugging"/>).</para>
1872
1873   <para>The Core format natively supports notes which you can add to
1874   your source code using the <literal>CORE</literal> pragma (see <xref
1875   linkend="pragmas"/>).</para>
1876
1877     <variablelist>
1878
1879         <varlistentry>
1880           <term>
1881             <option>-fext-core</option>
1882             <indexterm><primary><option>-fext-core</option></primary></indexterm>
1883           </term>
1884           <listitem>
1885             <para>Generate <literal>.hcr</literal> files.</para>
1886           </listitem>
1887         </varlistentry>
1888
1889     </variablelist>
1890
1891 <para>GHC can also read in External Core files as source; just give the <literal>.hcr</literal> file on
1892 the command line, instead of the <literal>.hs</literal> or <literal>.lhs</literal> Haskell source.
1893 A current infelicity is that you need to give the <literal>-fglasgow-exts</literal> flag too, because
1894 ordinary Haskell 98, when translated to External Core, uses things like rank-2 types.</para>
1895 </sect1>
1896
1897 &debug;
1898 &flags;
1899
1900 </chapter>
1901
1902 <!-- Emacs stuff:
1903      ;;; Local Variables: ***
1904      ;;; mode: xml ***
1905      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
1906      ;;; End: ***
1907  -->