5de6c1a5612e473452bc6adae87cdd6705ce4408
[ghc-hetmet.git] / ghc / docs / users_guide / using.vsgml
1 <sect> Using GHC
2 <label id="using-GHC">
3 <p>
4 <nidx>GHC, using</nidx>
5 <nidx>using GHC</nidx>
6
7 GHC is a command-line compiler: in order to compile a Haskell program,
8 GHC must be invoked on the source file(s) by typing a command to the
9 shell.  The steps involved in compiling a program can be automated
10 using the @make@ tool (this is especially useful if the program
11 consists of multiple source files which depend on each other).  This
12 section describes how to use GHC from the command-line.
13
14 %************************************************************************
15 %*                                                                      *
16 <sect1> Overall command-line structure
17 <label id="command-line-structure">
18 <p>
19 <nidx>structure, command-line</nidx>
20 <nidx>command-line structure</nidx>
21 %*                                                                      *
22 %************************************************************************
23
24 An invocation of GHC takes the following form:
25
26 <tscreen> <verb>
27 ghc [argument...]
28 </verb> </tscreen>
29
30 Command-line arguments are either options or file names.
31
32 Command-line options begin with @-@.  They may <em>not</em> be
33 grouped: @-vO@ is different from @-v -O@.  Options need not
34 precede filenames: e.g., @ghc *.o -o foo@.  All options are
35 processed and then applied to all files; you cannot, for example, invoke
36 @ghc -c -O1 Foo.hs -O2 Bar.hs@ to apply different optimisation
37 levels to the files @Foo.hs@ and @Bar.hs@.  For conflicting
38 options, e.g., @-c -S@, we reserve the right to do anything we
39 want.  (Usually, the last one applies.)
40
41 %************************************************************************
42 %*                                                                      *
43 <sect1>Meaningful file suffixes
44 <label id="file-suffixes">
45 <p>
46 <nidx>suffixes, file</nidx>
47 <nidx>file suffixes for GHC</nidx>
48 %*                                                                      *
49 %************************************************************************
50
51 File names with ``meaningful'' suffixes (e.g., @.lhs@ or @.o@)
52 cause the ``right thing'' to happen to those files.
53
54 <descrip>
55 <tag>@.lhs@:</tag>
56 <nidx>lhs suffix</nidx>
57 A ``literate Haskell'' module.
58
59 <tag>@.hs@:</tag> 
60 A not-so-literate Haskell module.
61
62 <tag>@.hi@:</tag>
63 A Haskell interface file, probably compiler-generated.
64
65 <tag>@.hc@:</tag>
66 Intermediate C file produced by the Haskell compiler.
67
68 <tag>@.c@:</tag>
69 A C~file not produced by the Haskell compiler.
70
71 % <tag>@.i@:</tag>
72 % C code after it has be preprocessed by the C compiler (using the
73 % @-E@ flag).
74
75 <tag>@.s@:</tag>
76 An assembly-language source file, usually
77 produced by the compiler.
78
79 <tag>@.o@:</tag>
80 An object file, produced by an assembler.
81 </descrip>
82
83 Files with other suffixes (or without suffixes) are passed straight
84 to the linker.
85
86 %************************************************************************
87 %*                                                                      *
88 <sect1>Help and verbosity options
89 <label id="options-help">
90 <p>
91 <nidx>help options (GHC)</nidx>
92 <nidx>verbose option (GHC)</nidx>
93 %*                                                                      *
94 %************************************************************************
95
96 A good option to start with is the @-help@ (or @-?@) option.
97 <nidx>-help option</nidx>
98 <nidx>-? option</nidx>
99 GHC spews a long message to standard output and then exits.
100
101 The @-v@<nidx>-v option</nidx> option makes GHC <em>verbose</em>: it
102 reports its version number and shows (on stderr) exactly how it invokes each 
103 phase of the compilation system.  Moreover, it passes
104 the @-v@ flag to most phases; each reports
105 its version number (and possibly some other information).
106
107 Please, oh please, use the @-v@ option when reporting bugs!
108 Knowing that you ran the right bits in the right order is always the
109 first thing we want to verify.
110
111 If you're just interested in the compiler version number, the
112 @--version@<nidx>--version option</nidx> option prints out a
113 one-line string containing the requested info.
114
115 %************************************************************************
116 %*                                                                      *
117 <sect1>Running the right phases in the right order
118 <label id="options-order">
119 <p>
120 <nidx>order of passes in GHC</nidx>
121 <nidx>pass ordering in GHC</nidx>
122 %*                                                                      *
123 %************************************************************************
124
125 The basic task of the @ghc@ driver is to run each input file
126 through the right phases (compiling, linking, etc.).
127
128 The first phase to run is determined by the input-file suffix, and the
129 last phase is determined by a flag.  If no relevant flag is present,
130 then go all the way through linking.  This table summarises:
131
132 <tabular ca="llll">
133 Phase of the           | Suffix saying | Flag saying   | (suffix of) @@
134 compilation system     | ``start here''| ``stop after''| output file @@
135 @@
136 literate pre-processor | .lhs          | -             | - @@
137 C pre-processor (opt.) | -             | -             | - @@
138 Haskell compiler       | .hs           | -C, -S        | .hc, .s @@
139 C compiler (opt.)      | .hc or .c     | -S            | .s  @@
140 assembler              | .s            | -c            | .o  @@
141 linker                 | other         | -             | a.out @@
142 </tabular>
143 <nidx>-C option</nidx>
144 <nidx>-S option</nidx>
145 <nidx>-c option</nidx>
146
147 Thus, a common invocation would be: @ghc -c Foo.hs@
148
149 Note: What the Haskell compiler proper produces depends on whether a
150 native-code generator is used (producing assembly language) or not
151 (producing C).
152
153 The option @-cpp@<nidx>-cpp option</nidx> must be given for the C
154 pre-processor phase to be run, that is, the pre-processor will be run
155 over your Haskell source file before continuing.
156
157 The option @-E@<nidx>-E option</nidx> runs just the pre-processing
158 passes of the compiler, outputting the result on stdout before
159 stopping. If used in conjunction with -cpp, the output is the
160 code blocks of the original (literal) source after having put it
161 through the grinder that is the C pre-processor. Sans @-cpp@, the
162 output is the de-litted version of the original source.
163
164 The option @-optcpp-E@<nidx>-optcpp-E option</nidx> runs just the
165 pre-processing stage of the C-compiling phase, sending the result to
166 stdout.  (For debugging or obfuscation contests, usually.)
167
168 %************************************************************************
169 %*                                                                      *
170 <sect1>Re-directing the compilation output(s)
171 <label id="options-output">
172 <p>
173 <nidx>output-directing options</nidx>
174 %*                                                                      *
175 %************************************************************************
176
177 GHC's compiled output normally goes into a @.hc@, @.o@, etc., file,
178 depending on the last-run compilation phase.  The option @-o
179 foo@<nidx>-o option</nidx> re-directs the output of that last-run
180 phase to file @foo@.
181
182 Note: this ``feature'' can be counterintuitive:
183 @ghc -C -o foo.o foo.hs@ will put the intermediate C code in the
184 file @foo.o@, name notwithstanding!
185
186 EXOTICA: But the @-o@ option isn't of much use if you have
187 <em>several</em> input files... Non-interface output files are
188 normally put in the same directory as their corresponding input file
189 came from.  You may specify that they be put in another directory
190 using the @-odir <dir>@<nidx>-odir &lt;dir&gt; option</nidx> (the
191 ``Oh, dear'' option).  For example:
192
193 <tscreen><verb>
194 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
195 </verb></tscreen>
196
197 The output files, @Foo.o@, @Bar.o@, and @Bumble.o@ would be
198 put into a subdirectory named after the architecture of the executing
199 machine (@sun4@, @mips@, etc).  The directory must already
200 exist; it won't be created.
201
202 Note that the @-odir@ option does <em>not</em> affect where the
203 interface files are put.  In the above example, they would still be
204 put in @parse/Foo.hi@, @parse/Bar.hi@, and @gurgle/Bumble.hi@.
205
206 MORE EXOTICA: The @-osuf <suffix>@<nidx>-osuf &lt;suffix&gt;
207 option</nidx> will change the @.o@ file suffix for object files to
208 whatever you specify.  (We use this in compiling the prelude.).
209 Similarly, the @-hisuf <suffix>@<nidx>-hisuf &lt;suffix&gt;
210 option</nidx> will change the @.hi@ file suffix for non-system
211 interface files (see Section <ref name="Other options related to
212 interface files" id="hi-options">).
213
214 The @-hisuf@/@-osuf@ game is useful if you want to compile a program
215 with both GHC and HBC (say) in the same directory.  Let HBC use the
216 standard @.hi@/@.o@ suffixes; add @-hisuf g_hi -osuf g_o@ to your
217 @make@ rule for GHC compiling...
218
219 FURTHER EXOTICA: If you are doing a normal @.hs@-to-@.o@ compilation
220 but would like to hang onto the intermediate @.hc@ C file, just
221 throw in a @-keep-hc-file-too@ option<nidx>-keep-hc-file-too option</nidx>.
222 If you would like to look at the assembler output, toss in a
223 @-keep-s-file-too@,<nidx>-keep-s-file-too option</nidx> too.
224
225 <sect2> Saving GHC's standard error output
226 <label id="saving-ghc-stderr">
227 <p>
228 <nidx>standard error, saving</nidx>
229
230 Sometimes, you may cause GHC to be rather chatty on standard error;
231 with @-v@, for example.  You can instruct GHC to <em>append</em> this
232 output to a particular log file with a @-odump <blah>@<nidx>-odump
233 &lt;blah&gt; option</nidx> option.
234
235 <sect2> Redirecting temporary files
236 <label id="temp-files">
237 <p>
238 <nidx>temporary files, redirecting</nidx>
239
240 If you have trouble because of running out of space in @/tmp@ (or
241 wherever your installation thinks temporary files should go), you may
242 use the @-tmpdir <dir>@<nidx>-tmpdir &lt;dir&gt; option</nidx> option
243 to specify an alternate directory.  For example, @-tmpdir .@ says to
244 put temporary files in the current working directory.
245
246 Alternatively, use your @TMPDIR@ environment variable.<nidx>TMPDIR
247 environment variable</nidx> Set it to the name of the directory where
248 temporary files should be put.  GCC and other programs will honour the
249 @TMPDIR@ variable as well.
250
251 Even better idea: Set the @TMPDIR@ variable when building GHC, and
252 never worry about @TMPDIR@ again. (see the build documentation).
253
254 %************************************************************************
255 %*                                                                      *
256 <sect1>Warnings and sanity-checking
257 <label id="options-sanity">
258 <p>
259 <nidx>sanity-checking options</nidx>
260 <nidx>warnings</nidx>
261 %*                                                                      *
262 %************************************************************************
263
264 GHC has a number of options that select which types of non-fatal error
265 messages, otherwise known as warnings, can be generated during
266 compilation.  By default, you get a standard set of warnings which are
267 generally likely to indicate bugs in your program.  These are:
268 @-fwarn-overlpapping-patterns@, @-fwarn-duplicate-exports@, and
269 @-fwarn-missing-methods@.  The following flags are simple ways to
270 select standard ``packages'' of warnings:
271
272 <descrip>
273
274 <tag>@-Wnot@:</tag>
275 <nidx>-Wnot option</nidx>
276
277 Turns off all warnings, including the standard ones.
278
279 <tag>@-w@:</tag>
280 <nidx>-w option</nidx>
281
282 Synonym for @-Wnot@.
283
284 <tag>@-W@:</tag>
285 <nidx>-W option</nidx>
286
287 Provides the standard warnings plus @-fwarn-incomplete-patterns@,
288 @-fwarn-unused-imports@ and @-fwarn-unused-binds@.
289
290 <tag>@-Wall@:</tag>
291 <nidx>-Wall option</nidx>
292
293 Turns on all warning options.
294
295 </descrip>
296
297 The full set of warning options is described below.  To turn off any
298 warning, simply give the corresponding @-fno-warn-...@ option on
299 the command line.
300
301 <descrip>
302
303 <tag>@-fwarn-name-shadowing@:</tag> 
304 <nidx>-fwarn-name-shadowing option</nidx>
305 <nidx>shadowing, warning</nidx>
306
307 This option causes a warning to be emitted whenever an inner-scope
308 value has the same name as an outer-scope value, i.e. the inner value
309 shadows the outer one.  This can catch typographical errors that turn
310 into hard-to-find bugs, e.g., in the inadvertent cyclic definition
311 @let x = ... x ... in@.
312
313 Consequently, this option does <em>not</em> allow cyclic recursive
314 definitions.
315
316 <tag>@-fwarn-overlapping-patterns@:</tag>
317 <nidx>-fwarn-overlapping-patterns option</nidx>
318 <nidx>overlapping patterns, warning</nidx>
319 <nidx>patterns, overlapping</nidx>
320
321 By default, the compiler will warn you if a set of patterns are
322 overlapping, i.e.,
323
324 <tscreen><verb>
325 f :: String -> Int
326 f []     = 0
327 f (_:xs) = 1
328 f "2"    = 2
329 </verb></tscreen>
330
331 where the last pattern match in @f@ won't ever be reached, as the
332 second pattern overlaps it. More often than not, redundant patterns
333 is a programmer mistake/error, so this option is enabled by default.
334
335 <tag>@-fwarn-incomplete-patterns@:</tag>
336 <nidx>-fwarn-incomplete-patterns option</nidx>
337 <nidx>incomplete patterns, warning</nidx>
338 <nidx>patterns, incomplete</nidx>
339
340 Similarly for incomplete patterns, the function @g@ below will fail
341 when applied to non-empty lists, so the compiler will emit a warning
342 about this when @-fwarn-incomplete-patterns@ is enabled.
343
344 <tscreen><verb>
345 g [] = 2
346 </verb></tscreen>
347
348 This option isn't enabled be default because it can be a bit noisy,
349 and it doesn't always indicate a bug in the program.  However, it's
350 generally considered good practice to cover all the cases in your
351 functions.
352
353 <tag>@-fwarn-missing-methods@:</tag>
354 <nidx>-fwarn-missing-methods option</nidx>
355 <nidx>missing methods, warning</nidx>
356 <nidx>methods, missing</nidx>
357
358 This option is on by default, and warns you whenever an instance
359 declaration is missing one or more methods, and the corresponding
360 class declaration has no default declaration for them.
361
362 <tag>@-fwarn-missing-fields@:</tag>
363 <nidx>-fwarn-missing-fields option</nidx>
364 <nidx>missing fields, warning</nidx>
365 <nidx>fields, missing</nidx>
366
367 This option is on by default, and warns you whenever the construction
368 of a labelled field constructor isn't complete, missing initializers
369 for one or more fields. While not an error (the missing fields are
370 initialised with bottoms), it is often an indication of a programmer
371 error.
372
373 <tag>@-fwarn-unused-imports@:</tag>
374 <nidx>-fwarn-unused-imports option</nidx>
375 <nidx>unused imports, warning</nidx>
376 <nidx>imports, unused</nidx>
377
378 Report any objects that are explicitly imported but never used.
379
380 <tag>@-fwarn-unused-binds@:</tag>
381 <nidx>-fwarn-unused-binds option</nidx>
382 <nidx>unused binds, warning</nidx>
383 <nidx>binds, unused</nidx>
384
385 Report any function definitions (and local bindings) which are unused.
386 For top-level functions, the warning is only given if the binding is
387 not exported.
388
389 <tag>@-fwarn-unused-matches@:</tag>
390 <nidx>-fwarn-unused-matches option</nidx>
391 <nidx>unused matches, warning</nidx>
392 <nidx>matches, unused</nidx>
393
394 Report all unused variables which arise from pattern matches,
395 including patterns consisting of a single variable.  For instance @f x
396 y = []@ would report @x@ and @y@ as unused.  To eliminate the warning,
397 all unused variables can be replaced with wildcards.
398
399 <tag>@-fwarn-duplicate-exports@:</tag>
400 <nidx>-fwarn-duplicate-exports option</nidx>
401 <nidx>duplicate exports, warning</nidx>
402 <nidx>export lists, duplicates</nidx>
403
404 Have the compiler warn about duplicate entries in export lists. This
405 is useful information if you maintain large export lists, and want to
406 avoid the continued export of a definition after you've deleted (one)
407 mention of it in the export list.
408
409 This option is on by default.
410
411 <tag><tt>-fwarn-type-defaults</tt>:</tag>
412 <nidx>-fwarn-type-defaults option</nidx>
413 <nidx>defaulting mechanism, warning</nidx>
414
415 Have the compiler warn/inform you where in your source the Haskell
416 defaulting mechanism for numeric types kicks in. This is useful
417 information when converting code from a context that assumed one
418 default into one with another, e.g., the 'default default' for Haskell
419 1.4 caused the otherwise unconstrained value <tt>1</tt> to be given
420 the type <tt>Int</tt>, whereas Haskell 98 defaults it to
421 <tt>Integer</tt>.  This may lead to differences in performance and
422 behaviour, hence the usefulness of being non-silent about this.
423
424 This warning is off by default.
425
426 <tag>@-fwarn-missing-signatures@:</tag>
427 <nidx>-fwarn-missing-signatures option</nidx>
428 <nidx>type signatures, missing</nidx>
429
430 If you would like GHC to check that every top-level function/value has
431 a type signature, use the @-fwarn-missing-signatures@ option.  This
432 option is off by default.
433
434 </descrip>
435
436 If you're feeling really paranoid, the @-dcore-lint@
437 option<nidx>-dcore-lint option</nidx> is a good choice.  It turns on
438 heavyweight intra-pass sanity-checking within GHC.  (It checks GHC's
439 sanity, not yours.)
440
441 %************************************************************************
442 %*                                                                      *
443 <sect1>Separate compilation
444 <label id="separate-compilation">
445 <p>
446 <nidx>separate compilation</nidx>
447 <nidx>recompilation checker</nidx>
448 <nidx>make and recompilation</nidx>
449 %*                                                                      *
450 %************************************************************************
451
452 This section describes how GHC supports separate compilation.
453
454 <sect2>Interface files
455 <label id="hi-files">
456 <p>
457 <nidx>interface files</nidx>
458 <nidx>.hi files</nidx>
459
460 When GHC compiles a source file @F@ which contains a module @A@, say,
461 it generates an object @F.o@, <em>and</em> a companion <em>interface
462 file</em> @A.hi@.  The interface file is not intended for human
463 consumption, as you'll see if you take a look at one.  It's merely
464 there to help the compiler compile other modules in the same program.
465
466 NOTE: Having the name of the interface file follow the module name and
467 not the file name, means that working with tools such as @make(1)@
468 become harder. @make@ implicitly assumes that any output files
469 produced by processing a translation unit will have file names that
470 can be derived from the file name of the translation unit.  For
471 instance, pattern rules becomes unusable.  For this reason, we
472 recommend you stick to using the same file name as the module name.
473
474 The interface file for @A@ contains information needed by the compiler
475 when it compiles any module @B@ that imports @A@, whether directly or
476 indirectly.  When compiling @B@, GHC will read @A.hi@ to find the
477 details that it needs to know about things defined in @A@.
478
479 Furthermore, when compiling module @C@ which imports @B@, GHC may
480 decide that it needs to know something about @A@ --- for example, @B@
481 might export a function that involves a type defined in @A@.  In this
482 case, GHC will go and read @A.hi@ even though @C@ does not explicitly
483 import @A@ at all.
484
485 The interface file may contain all sorts of things that aren't
486 explicitly exported from @A@ by the programmer.  For example, even
487 though a data type is exported abstractly, @A.hi@ will contain the
488 full data type definition.  For small function definitions, @A.hi@
489 will contain the complete definition of the function.  For bigger
490 functions, @A.hi@ will contain strictness information about the
491 function.  And so on.  GHC puts much more information into @.hi@ files
492 when optimisation is turned on with the @-O@ flag.  Without @-O@ it
493 puts in just the minimum; with @-O@ it lobs in a whole pile of stuff.
494 <nidx>optimsation, effect on .hi files</nidx>
495
496 @A.hi@ should really be thought of as a compiler-readable version of
497 @A.o@.  If you use a @.hi@ file that wasn't generated by the same
498 compilation run that generates the @.o@ file the compiler may assume
499 all sorts of incorrect things about @A@, resulting in core dumps and
500 other unpleasant happenings.
501
502 %************************************************************************
503 %*                                                                      *
504 <sect2>File names and module names
505 <label id="files-and-modules">
506 <p>
507 %*                                                                      *
508 %************************************************************************
509
510 Typically, a module @Foo@ will be contained in a file called @Foo.hs@
511 or @Foo.lhs@.  But GHC does not require that to be the case.  You can put a module
512 named @Foo@ in a file named @SomethingElse@.  In this case, <em>GHC will still
513 write an interface file @Foo.hi@</em>, but it will write an object fild
514 @SomethingElse.o@.  Any module that imports @Foo@ will
515 of course look for @Foo.hi@, and it will find it.
516
517 A useful consequence is that you can have many files, @A.hs@, @B.hs@, etc, containing
518 the module @Main@.  This is useful if you want to build distinct programs
519 in the same directory.
520
521
522 %************************************************************************
523 %*                                                                      *
524 <sect2>Finding interface files
525 <label id="options-finding-imports">
526 <p>
527 <nidx>interface files, finding them</nidx>
528 <nidx>finding interface files</nidx>
529 %*                                                                      *
530 %************************************************************************
531
532 In your program, you import a module @Foo@ by saying
533 @import Foo@.  GHC goes looking for an interface file, @Foo.hi@.
534 It has a builtin list of directories (notably including @.@) where
535 it looks.
536
537 <descrip>
538
539 <tag>@-i<dirs>@</tag><nidx>-i&lt;dirs&gt; option</nidx> This flag
540 prepends a colon-separated list of @dirs@ to the ``import
541 directories'' list.
542 See also Section <ref id="recomp"> for the significance of using
543 relative and absolute pathnames in the @-i@ list.
544
545 <tag>@-i@</tag> resets the ``import directories'' list back to nothing.
546
547 <tag>@-fno-implicit-prelude@</tag>
548 <nidx>-fno-implicit-prelude option</nidx>
549 GHC normally imports @Prelude.hi@ files for you.  If you'd rather it
550 didn't, then give it a @-fno-implicit-prelude@ option.  You are
551 unlikely to get very far without a Prelude, but, hey, it's a free
552 country.
553
554 <tag>@-syslib <lib>@</tag>
555 <nidx>-syslib &lt;lib&gt; option</nidx>
556
557 If you are using a system-supplied non-Prelude library (e.g., the
558 POSIX library), just use a @-syslib posix@ option (for example).  The
559 right interface files should then be available.  Section <ref
560 name="The GHC Prelude and Libraries" id="ghc-prelude"> lists the
561 libraries available by this mechanism.
562
563 <tag>@-I<dir>@</tag>
564 <nidx>-I&lt;dir&gt; option</nidx>
565
566 Once a Haskell module has been compiled to C (@.hc@ file), you may
567 wish to specify where GHC tells the C compiler to look for @.h@ files.
568 (Or, if you are using the @-cpp@ option<nidx>-cpp option</nidx>, where
569 it tells the C pre-processor to look...)  For this purpose, use a @-I@
570 option in the usual C-ish way.
571
572 </descrip>
573
574 %************************************************************************
575 %*                                                                      *
576 <sect2>Other options related to interface files
577 <label id="hi-options">
578 <p>
579 <nidx>interface files, options</nidx>
580 %*                                                                      *
581 %************************************************************************
582
583 GHC supports some other more exotic command-line options related to interface files.
584 Most programmers should never need to use any of them.
585
586 <descrip>
587
588 <tag>@-ohi@ <filename></tag> 
589 <nidx>-ohi &lt;file&gt; option</nidx> 
590
591 The interface output may be directed to another file
592 @bar2/Wurble.iface@ with the option @-ohi bar2/Wurble.iface@
593 (not recommended).
594
595 <tag>@-nohi@</tag>
596 <nidx>-nohi option</nidx>
597
598 Don't generate an interface file at all.
599
600 <tag>@-hi-diffs@, @-hi-diffs-with-usages@, @-keep-hi-diffs@</tag>
601 <nidx>-hi-diffs option</nidx> 
602 <nidx>-hi-diffs-with-usages option</nidx> 
603 <nidx>-keep-hi-diffs option</nidx> 
604 The compiler does not overwrite an existing @.hi@ interface file if
605 the new one is byte-for-byte the same as the old one; this is friendly
606 to @make@.  When an interface does change, it is often enlightening to
607 be informed.  The @-hi-diffs@ option will
608 make @ghc@ run @diff@ on the old and new @.hi@ files. You can also
609 record the difference in the interface file itself, the
610 @-keep-hi-diffs@<nidx>-keep-hi-diffs</nidx> option takes care of that.
611
612 The @.hi@ files from GHC contain ``usage'' information which changes
613 often and uninterestingly.  If you really want to see these changes
614 reported, you need to use the
615 @-hi-diffs-with-usages@<nidx>-hi-diffs-with-usages option</nidx>
616 option.
617
618 <tag>@-fignore-interface-pragmas@</tag>
619 <nidx>-fignore-interface-pragmas option</nidx>
620
621 Interface files are normally jammed full of compiler-produced
622 <em>pragmas</em>, which record arities, strictness info, etc.  If you
623 think these pragmas are messing you up (or you are doing some kind of
624 weird experiment), you can tell GHC to ignore them with the
625 @-fignore-interface-pragmas@ option.
626
627 <tag>@-fno-prune-tydecls@</tag>
628 <nidx>-fno-prune-tydecls option</nidx>
629 When compiling without optimisations on, the compiler is extra-careful
630 about not slurping in data constructors that it will not need. 
631 The @-fno-prune-tydecls@ flag lets you turn this cleverness off; the reason
632 is to allow us to measure the effect of the cleverness.   (In earlier versions
633 of GHC there was a bug that meant you <em>had</em> to turn it off sometimes,
634 but that is no longer true.)
635 </descrip>
636
637 See also Section <ref name="Linking and consistency-checking"
638 id="options-linker">, which describes how the linker finds standard
639 Haskell libraries.
640
641 %************************************************************************
642 %*                                                                      *
643 <sect2>The recompilation checker
644 <label id="recomp">
645 <p>
646 <nidx>recompilation checker</nidx>
647 %*                                                                      *
648 %************************************************************************
649
650 In the olden days, GHC compared the newly-generated @.hi@ file with
651 the previous version; if they were identical, it left the old one
652 alone and didn't change its modification date.  In consequence,
653 importers of a module with an unchanged output @.hi@ file were not
654 recompiled.
655
656 This doesn't work any more.  In our earlier example, module @C@ does
657 not import module @A@ directly, yet changes to @A.hi@ should force a
658 recompilation of @C@.  And some changes to @A@ (changing the
659 definition of a function that appears in an inlining of a function
660 exported by @B@, say) may conceivably not change @B.hi@ one jot.  So
661 now...
662
663 GHC keeps a version number on each interface file, and on each type
664 signature within the interface file.  It also keeps in every interface
665 file a list of the version numbers of everything it used when it last
666 compiled the file.  If the source file's modification date is earlier
667 than the @.o@ file's date (i.e. the source hasn't changed since the
668 file was last compiled), GHC will be clever.  It compares the version
669 numbers on the things it needs this time with the version numbers on
670 the things it needed last time (gleaned from the interface file of the
671 module being compiled); if they are all the same it stops compiling
672 rather early in the process saying ``Compilation IS NOT required''.
673 What a beautiful sight!
674
675 GHC <em>only</em> keeps detailed dependency information for ``user'' modules,
676 not for ``library'' modules.  It distinguishes the two by a hack: a module
677 whose @.hi@ file has an absolute path name is considered a library module,
678 while a relative path name indicates a user module.  So if you have a 
679 multi-directory application, use <em>relative</em> path names in your
680 @-i@ path, to force GHC to record detailed dependency information.
681 Use absolute path names only for directories containing slowly-changing
682 library modules.
683
684 A path is considered ``absolute'' if it starts with ``@/@'', or
685 ``@A:/@'', or ``@A:\@'' (or ``@B:/@'', ``@B:\@'' etc).
686
687 Patrick Sansom had a workshop paper about how all this is done (though
688 the details have changed quite a bit).  Ask
689 him (email: <htmlurl name="sansom@@dcs.gla.ac.uk"
690 url="mailto:sansom@@dcs.gla.ac.uk">) if you want a copy.
691
692 %************************************************************************
693 %*                                                                      *
694 <sect2>Using @make@
695 <label id="using-make">
696 <p>
697 <ncdx>make</ncdx>
698 %*                                                                      *
699 %************************************************************************
700
701 It is reasonably straightforward to set up a @Makefile@ to use with
702 GHC, assuming you name your source files the same as your modules.
703 Thus:
704
705 <tscreen><verb>
706 HC      = ghc
707 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
708
709 SRCS = Main.lhs Foo.lhs Bar.lhs
710 OBJS = Main.o   Foo.o   Bar.o
711
712 .SUFFIXES : .o .hi .lhs .hc .s
713
714 cool_pgm : $(OBJS)
715         rm $@
716         $(HC) -o $@ $(HC_OPTS) $(OBJS)
717
718 # Standard suffix rules
719 .o.hi:
720         @:
721
722 .lhs.o:
723         $(HC) -c $< $(HC_OPTS)
724
725 .hs.o:
726         $(HC) -c $< $(HC_OPTS)
727
728 # Inter-module dependencies
729 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
730 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
731 </verb></tscreen>
732
733 (Sophisticated @make@ variants may achieve some of the above more
734 elegantly.  Notably, @gmake@'s pattern rules let you write the more
735 comprehensible:
736
737 <tscreen><verb>
738 %.o : %.lhs
739         $(HC) -c $< $(HC_OPTS)
740 </verb></tscreen>
741
742 What we've shown should work with any @make@.)
743
744 Note the cheesy @.o.hi@ rule: It records the dependency of the
745 interface (@.hi@) file on the source.  The rule says a @.hi@ file can
746 be made from a @.o@ file by doing... nothing.  Which is true.
747
748 Note the inter-module dependencies at the end of the Makefile, which
749 take the form
750
751 <tscreen><verb>
752 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
753 </verb></tscreen>
754
755 They tell @make@ that if any of @Foo.o@, @Foo.hc@ or @Foo.s@ have an
756 earlier modification date than @Baz.hi@, then the out-of-date file
757 must be brought up to date.  To bring it up to date, @make@ looks for
758 a rule to do so; one of the preceding suffix rules does the job
759 nicely.
760
761 Putting inter-dependencies of the form @Foo.o : Bar.hi@ into your
762 @Makefile@ by hand is rather error-prone.  Don't worry---never fear,
763 @mkdependHS@ is here! (and is distributed as part of GHC) Add the
764 following to your @Makefile@:
765
766 <tscreen><verb>
767 depend :
768         mkdependHS -- $(HC_OPTS) -- $(SRCS)
769 </verb></tscreen>
770
771 Now, before you start compiling, and any time you change the @imports@
772 in your program, do @make depend@ before you do @make cool_pgm@.
773 @mkdependHS@ will append the needed dependencies to your @Makefile@.
774 @mkdependHS@ is fully describe in Section <ref name="Makefile
775 dependencies in Haskell: using mkdependHS" id="mkdependHS">.
776
777 A few caveats about this simple scheme:
778
779 <itemize>
780
781 <item> You may need to compile some modules explicitly to create their
782 interfaces in the first place (e.g., @make Bar.o@ to create @Bar.hi@).
783
784 <item> You may have to type @make@ more than once for the dependencies
785 to have full effect.  However, a @make@ run that does nothing
786 <em>does</em> mean ``everything's up-to-date.''
787
788 <item> This scheme will work with mutually-recursive modules but,
789 again, it may take multiple iterations to ``settle.''
790
791 </itemize>
792
793 %************************************************************************
794 %*                                                                      *
795 <sect2>How to compile mutually recursive modules
796 <label id="mutual-recursion">
797 <p>
798 <nidx>module system, recursion</nidx>
799 <nidx>recursion, between modules</nidx>
800 %*                                                                      *
801 %************************************************************************
802
803 Currently, the compiler does not have proper support for dealing with
804 mutually recursive modules:
805
806 <tscreen><verb>
807 module A where
808
809 import B
810
811 newtype TA = MkTA Int
812
813 f :: TB -> TA
814 f (MkTB x) = MkTA x
815 --------
816 module B where
817
818 import A
819
820 data TB = MkTB !Int
821
822 g :: TA -> TB
823 g (MkTA x) = MkTB x
824 </verb></tscreen>
825
826 When compiling either module A and B, the compiler will try (in vain)
827 to look for the interface file of the other. So, to get mutually
828 recursive modules off the ground, you need to hand write an interface
829 file for A or B, so as to break the loop.  These hand-written
830 interface files are called @hi-boot@ files, and are placed in a file
831 called @<module>.hi-boot@.  To import from an @hi-boot@ file instead
832 of the standard @.hi@ file, use the following syntax in the importing module:
833 <nidx>hi-boot files</nidx>
834 <nidx>importing, hi-boot files</nidx>
835
836 <tscreen> <verb>
837 import {-# SOURCE #-} A
838 </verb> </tscreen>
839
840 The hand-written interface need only contain the bare minimum of
841 information needed to get the bootstrapping process started.  For
842 example, it doesn't need to contain declarations for <em/everything/
843 that module @A@ exports, only the things required by the module that
844 imports @A@ recursively.
845
846 For the example at hand, the boot interface file for A would look like
847 the following:
848
849 <tscreen><verb>
850 __interface A 1 404 where
851 __export A TA{MkTA} ;
852 1 newtype TA = MkTA PrelBase.Int ;
853 </verb></tscreen>
854
855 The syntax is essentially the same as a normal @.hi@ file
856 (unfortunately), but you can usually tailor an existing @.hi@ file to
857 make a @.hi-boot@ file.
858
859 Notice that we only put the declaration for the newtype @TA@ in the
860 @hi-boot@ file, not the signature for @f@, since @f@ isn't used by
861 @B@.
862
863 The number ``1'' after ``__interface A'' gives the version number of module A;
864 it is incremented whenever anything in A's interface file changes.  The ``404'' is
865 the version number of the interface file <em>syntax</em>; we change it when
866 we change the syntax of interface files so that you get a better error message when
867 you try to read an old-format file with a new-format compiler.
868
869 The number ``1'' at the beginning of a declaration is the <em>version
870 number</em> of that declaration: for the purposes of @.hi-boot@ files
871 these can all be set to 1.  All names must be fully qualified with the
872 <em/original/ module that an object comes from: for example, the
873 reference to @Int@ in the interface for @A@ comes from @PrelBase@,
874 which is a module internal to GHC's prelude.  It's a pain, but that's
875 the way it is.
876
877 If you want an hi-boot file to export a data type, but you don't want to give its constructors
878 (because the constructors aren't used by the SOURCE-importing module), you can write simply:
879
880 <tscreen><verb>
881 __interface A 1 404 where
882 __export A TA;
883 1 data TA
884 </verb></tscreen>
885
886 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
887
888 <bf>Note:</bf> This is all a temporary solution, a version of the
889 compiler that handles mutually recursive properly without the manual
890 construction of interface files, is (allegedly) in the works.
891
892 %************************************************************************
893 %*                                                                      *
894 <sect1>Optimisation (code improvement)
895 <label id="options-optimise">
896 <p>
897 <nidx>optimisation (GHC)</nidx>
898 <nidx>improvement, code (GHC)</nidx>
899 %*                                                                      *
900 %************************************************************************
901
902 The @-O*@ options specify convenient ``packages'' of optimisation
903 flags; the @-f*@ options described later on specify
904 <em>individual</em> optimisations to be turned on/off; the @-m*@
905 options specify <em>machine-specific</em> optimisations to be turned
906 on/off.
907
908 %----------------------------------------------------------------------
909 <sect2>@-O*@: convenient ``packages'' of optimisation flags.
910 <label id="optimise-pkgs">
911 <p>
912 <nidx>-O options</nidx>
913
914 There are <em>many</em> options that affect the quality of code
915 produced by GHC.  Most people only have a general goal, something like
916 ``Compile quickly'' or ``Make my program run like greased lightning.''
917 The following ``packages'' of optimisations (or lack thereof) should
918 suffice.
919
920 Once you choose a @-O*@ ``package,'' stick with it---don't chop and
921 change.  Modules' interfaces <em>will</em> change with a shift to a new
922 @-O*@ option, and you may have to recompile a large chunk of all
923 importing modules before your program can again be run
924 safely (see Section <ref name="The recompilation checker" id="recomp">).
925
926 <descrip>
927 <tag>No @-O*@-type option specified:</tag>
928 <nidx>-O* not specified</nidx>
929 This is taken to mean: ``Please compile quickly; I'm not over-bothered
930 about compiled-code quality.''  So, for example: @ghc -c Foo.hs@
931
932 <tag>@-O@ or @-O1@:</tag>
933 <nidx>-O option</nidx>
934 <nidx>-O1 option</nidx>
935 <nidx>optimise normally</nidx>
936 Means: ``Generate good-quality code without taking too long about it.''
937 Thus, for example: @ghc -c -O Main.lhs@
938
939 <tag>@-O2@:</tag>
940 <nidx>-O2 option</nidx>
941 <nidx>optimise aggressively</nidx>
942 Means: ``Apply every non-dangerous optimisation, even if it means
943 significantly longer compile times.''
944
945 The avoided ``dangerous'' optimisations are those that can make
946 runtime or space <em>worse</em> if you're unlucky.  They are
947 normally turned on or off individually.
948
949 At the moment, @-O2@ is <em>unlikely</em> to produce
950 better code than @-O@.
951
952 <tag>@-O2-for-C@:</tag>
953 <nidx>-O2-for-C option</nidx>
954 <nidx>gcc, invoking with -O2</nidx>
955
956 Says to run GCC with @-O2@, which may be worth a few percent in
957 execution speed.  Don't forget @-fvia-C@, lest you use the native-code
958 generator and bypass GCC altogether!
959
960 <tag>@-Onot@:</tag>
961 <nidx>-Onot option</nidx>
962 <nidx>optimising, reset</nidx>
963
964 This option will make GHC ``forget'' any @-O@ish options it has seen so
965 far.  Sometimes useful; for example: @make all EXTRA_HC_OPTS=-Onot@.
966
967 <tag>@-Ofile <file>@:</tag>
968 <nidx>-Ofile &lt;file&gt; option</nidx>
969 <nidx>optimising, customised</nidx>
970
971 For those who need <em>absolute</em> control over <em>exactly</em>
972 what options are used (e.g., compiler writers, sometimes :-), a list
973 of options can be put in a file and then slurped in with @-Ofile@.
974
975 In that file, comments are of the @#@-to-end-of-line variety; blank
976 lines and most whitespace is ignored.
977
978 Please ask if you are baffled and would like an example of @-Ofile@!
979 </descrip>
980
981 At Glasgow, we don't use a @-O*@ flag for day-to-day work.  We use
982 @-O@ to get respectable speed; e.g., when we want to measure
983 something.  When we want to go for broke, we tend to use @-O -fvia-C
984 -O2-for-C@ (and we go for lots of coffee breaks).
985
986 The easiest way to see what @-O@ (etc.) ``really mean'' is to run with
987 @-v@, then stand back in amazement.  Alternatively, just look at the
988 @HsC_minus<blah>@ lists in the @ghc@ driver script.
989
990 %----------------------------------------------------------------------
991 <sect2>@-f*@: platform-independent flags
992 <p>
993 <nidx>-f* options (GHC)</nidx>
994 <nidx>-fno-* options (GHC)</nidx>
995
996 Flags can be turned <em>off</em> individually.  (NB: I hope you have a
997 good reason for doing this....) To turn off the @-ffoo@ flag, just use
998 the @-fno-foo@ flag.<nidx>-fno-&lt;opt&gt; anti-option</nidx> So, for
999 example, you can say @-O2 -fno-strictness@, which will then drop out
1000 any running of the strictness analyser.
1001
1002 The options you are most likely to want to turn off are:
1003 <itemize>
1004 <item>
1005 @-fno-strictness@<nidx>-fno-strictness option</nidx> (strictness
1006 analyser, because it is sometimes slow),
1007 <item>
1008 @-fno-specialise@<nidx>-fno-specialise option</nidx> (automatic
1009 specialisation of overloaded functions, because it can make your code
1010 bigger) (US spelling also accepted), and
1011 <item>
1012 @-fno-cpr-analyse@<nidx>-fno-cpr-analyse option</nidx> switches off the CPR (constructed product
1013 result) analyser.
1014 </itemize>
1015
1016 Should you wish to turn individual flags <em>on</em>, you are advised
1017 to use the @-Ofile@ option, described above.  Because the order in
1018 which optimisation passes are run is sometimes crucial, it's quite
1019 hard to do with command-line options.
1020
1021 Here are some ``dangerous'' optimisations you <em>might</em> want to try:
1022 <descrip>
1023 %------------------------------------------------------------------
1024 <tag>@-fvia-C@:</tag>
1025 <nidx>-fvia-C option</nidx>
1026 <nidx>native code generator, turning off</nidx>
1027
1028 Compile via C, and don't use the native-code generator.  (There are
1029 many cases when GHC does this on its own.)  You might pick up a little
1030 bit of speed by compiling via C.  If you use @_ccall_gc_@s or
1031 @_casm_@s, you probably <em>have to</em> use @-fvia-C@.
1032
1033 The lower-case incantation, @-fvia-c@, is synonymous.
1034
1035 Compiling via C will probably be slower (in compilation time) than
1036 using GHC's native code generator.
1037
1038 <tag>@-funfolding-interface-threshold<n>@:</tag>
1039 <nidx>-funfolding-interface-threshold option</nidx>
1040 <nidx>inlining, controlling</nidx>
1041 <nidx>unfolding, controlling</nidx>
1042 (Default: 30) By raising or lowering this number, you can raise or
1043 lower the amount of pragmatic junk that gets spewed into interface
1044 files.  (An unfolding has a ``size'' that reflects the cost in terms
1045 of ``code bloat'' of expanding that unfolding in another module.  A
1046 bigger function would be assigned a bigger cost.)
1047
1048 <tag>@-funfolding-creation-threshold<n>@:</tag>
1049 <nidx>-funfolding-creation-threshold option</nidx>
1050 <nidx>inlining, controlling</nidx>
1051 <nidx>unfolding, controlling</nidx>
1052 (Default: 30) This option is similar to
1053 @-funfolding-interface-threshold@, except that it governs unfoldings
1054 within a single module.  Increasing this figure is more likely to
1055 result in longer compile times than faster code.  The next option is
1056 more useful:
1057
1058 <tag>@-funfolding-use-threshold<n>@:</tag>
1059 <nidx>-funfolding-use-threshold option</nidx>
1060 <nidx>inlining, controlling</nidx>
1061 <nidx>unfolding, controlling</nidx>
1062 (Default: 8) This is the magic cut-off figure for unfolding: below
1063 this size, a function definition will be unfolded at the call-site,
1064 any bigger and it won't.  The size computed for a function depends on
1065 two things: the actual size of the expression minus any discounts that
1066 apply (see @-funfolding-con-discount@).
1067
1068 <tag>@-funfolding-con-discount<n>@:</tag>
1069 <nidx>-funfolding-con-discount option</nidx>
1070 <nidx>inlining, controlling</nidx>
1071 <nidx>unfolding, controlling</nidx>
1072 (Default: 2) If the compiler decides that it can eliminate some
1073 computation by performing an unfolding, then this is a discount factor
1074 that it applies to the funciton size before deciding whether to unfold
1075 it or not.
1076
1077 OK, folks, these magic numbers `30', `8', and '2' are mildly
1078 arbitrary; they are of the ``seem to be OK'' variety.  The `8' is the
1079 more critical one; it's what determines how eager GHC is about
1080 expanding unfoldings.
1081
1082 <tag>@-funbox-strict-fields@:</tag>
1083 <nidx>-funbox-strict-fields option</nidx>
1084 <nidx>strict constructor fields</nidx>
1085 <nidx>constructor fields, strict</nidx>
1086
1087 This option causes all constructor fields which are marked strict
1088 (i.e. ``!'') to be unboxed or unpacked if possible.  For example:
1089
1090 <tscreen><verb>
1091 data T = T !Float !Float
1092 </verb></tscreen>
1093
1094 will create a constructor @T@ containing two unboxed floats if the
1095 @-funbox-strict-fields@ flag is given.  This may not always be an
1096 optimisation: if the @T@ constructor is scrutinised and the floats
1097 passed to a non-strict function for example, they will have to be
1098 reboxed (this is done automatically by the compiler).
1099
1100 This option should only be used in conjunction with @-O@, in order to
1101 expose unfoldings to the compiler so the reboxing can be removed as
1102 often as possible.  For example:
1103
1104 <tscreen><verb>
1105 f :: T -> Float
1106 f (T f1 f2) = f1 + f2
1107 </verb></tscreen>
1108
1109 The compiler will avoid reboxing @f1@ and @f2@ by inlining @+@ on
1110 floats, but only when @-O@ is on.
1111
1112 Any single-constructor data is eligible for unpacking; for example
1113
1114 <tscreen><verb>
1115 data T = T !(Int,Int)
1116 </verb></tscreen>
1117
1118 will store the two @Int@s directly in the @T@ constructor, by flattening
1119 the pair.  Multi-level unpacking is also supported:
1120
1121 <tscreen><verb>
1122 data T = T !S
1123 data S = S !Int !Int
1124 </verb></tscreen>
1125
1126 will store two unboxed @Int#@s directly in the @T@ constructor.
1127
1128 <tag>@-fsemi-tagging@:</tag>
1129 This option (which <em>does not work</em> with the native-code generator)
1130 tells the compiler to add extra code to test for already-evaluated
1131 values.  You win if you have lots of such values during a run of your
1132 program, you lose otherwise.  (And you pay in extra code space.)
1133
1134 We have not played with @-fsemi-tagging@ enough to recommend it.
1135 (For all we know, it doesn't even work anymore...  Sigh.)
1136 </descrip>
1137
1138 %----------------------------------------------------------------------
1139 <sect2>@-m*@: platform-specific flags
1140 <p>
1141 <nidx>-m* options (GHC)</nidx>
1142 <nidx>platform-specific options</nidx>
1143 <nidx>machine-specific options</nidx>
1144
1145 Some flags only make sense for particular target platforms.
1146
1147 <descrip>
1148 <tag>@-mv8@:</tag>
1149 (SPARC machines)<nidx>-mv8 option (SPARC only)</nidx>
1150 Means to pass the like-named option to GCC; it says to use the
1151 Version 8 SPARC instructions, notably integer multiply and divide.
1152 The similiar @-m*@ GCC options for SPARC also work, actually.
1153
1154 <tag>@-mlong-calls@:</tag>
1155 (HPPA machines)<nidx>-mlong-calls option (HPPA only)</nidx>
1156 Means to pass the like-named option to GCC.  Required for Very Big
1157 modules, maybe.  (Probably means you're in trouble...)
1158
1159 <tag>@-monly-[32]-regs@:</tag>
1160 (iX86 machines)<nidx>-monly-N-regs option (iX86 only)</nidx>
1161 GHC tries to ``steal'' four registers from GCC, for performance
1162 reasons; it almost always works.  However, when GCC is compiling some
1163 modules with four stolen registers, it will crash, probably saying:
1164 <tscreen><verb>
1165 Foo.hc:533: fixed or forbidden register was spilled.
1166 This may be due to a compiler bug or to impossible asm
1167 statements or clauses.
1168 </verb></tscreen>
1169 Just give some registers back with @-monly-N-regs@.  Try `3' first,
1170 then `2'.  If `2' doesn't work, please report the bug to us.
1171 </descrip>
1172
1173 %----------------------------------------------------------------------
1174 <sect2>Code improvement by the C compiler.
1175 <label id="optimise-C-compiler">
1176 <p>
1177 <nidx>optimisation by GCC</nidx>
1178 <nidx>GCC optimisation</nidx>
1179
1180 The C~compiler (GCC) is run with @-O@ turned on.  (It has
1181 to be, actually).
1182
1183 If you want to run GCC with @-O2@---which may be worth a few
1184 percent in execution speed---you can give a
1185 @-O2-for-C@<nidx>-O2-for-C option</nidx> option.
1186
1187 %************************************************************************
1188 %*                                                                      *
1189 <sect1>Options related to a particular phase
1190 <label id="options-phases">
1191 <p>
1192 %*                                                                      *
1193 %************************************************************************
1194
1195 <sect2> The C pre-processor
1196 <label id="c-pre-processor">
1197 <p>
1198 <nidx>pre-processing: cpp</nidx>
1199 <nidx>C pre-processor options</nidx>
1200 <nidx>cpp, pre-processing with</nidx>
1201
1202 The C pre-processor @cpp@ is run over your Haskell code only if the
1203 @-cpp@ option <nidx>-cpp option</nidx> is given.  Unless you are
1204 building a large system with significant doses of conditional
1205 compilation, you really shouldn't need it.
1206 <descrip>
1207 <tag>@-D<foo>@:</tag>
1208 <nidx>-D&lt;name&gt; option</nidx>
1209 Define macro @<foo>@ in the usual way.  NB: does <em>not</em> affect
1210 @-D@ macros passed to the C~compiler when compiling via C!  For those,
1211 use the @-optc-Dfoo@ hack... (see Section <ref name="Forcing options
1212 to a particular phase." id="forcing-options-through">).
1213
1214 <tag>@-U<foo>@:</tag>
1215 <nidx>-U&lt;name&gt; option</nidx>
1216 Undefine macro @<foo>@ in the usual way.
1217
1218 <tag>@-I<dir>@:</tag>
1219 <nidx>-I&lt;dir&gt; option</nidx>
1220 Specify a directory in which to look for @#include@ files, in
1221 the usual C way.
1222 </descrip>
1223
1224 The @ghc@ driver pre-defines several macros when processing Haskell
1225 source code (@.hs@ or @.lhs@ files):
1226
1227 <descrip>
1228 <tag>@__HASKELL98__@:</tag>
1229 <nidx>__HASKELL98__</nidx>
1230 If defined, this means that GHC supports the language defined by the
1231 Haskell 98 report.
1232
1233 <tag>@__HASKELL__=98@:</tag>
1234 <nidx>__HASKELL__</nidx>
1235 In GHC 4.04 and later, the @__HASKELL__@ macro is defined as having
1236 the value @98@.
1237
1238 <tag>@__HASKELL1__@:</tag>
1239 <nidx>__HASKELL1__ macro</nidx>
1240 If defined to <em/n/, that means GHC supports the Haskell language
1241 defined in the Haskell report version <em/1.n/.  Currently 5.  This
1242 macro is deprecated, and will probably disappear in future versions.
1243
1244 <tag>@__GLASGOW_HASKELL__@:</tag>
1245 <nidx>__GLASGOW_HASKELL__ macro</nidx>
1246 For version <em/n/ of the GHC system, this will be @#define@d to
1247 <em/100n/.  So, for version 4.00, it is 400.
1248
1249 With any luck, @__GLASGOW_HASKELL__@ will be undefined in all other
1250 implementations that support C-style pre-processing.
1251
1252 (For reference: the comparable symbols for other systems are:
1253 @__HUGS__@ for Hugs and @__HBC__@ for Chalmers.)
1254
1255 NB. This macro is set when pre-processing both Haskell source and C
1256 source, including the C source generated from a Haskell module
1257 (ie. @.hs@, @.lhs@, @.c@ and @.hc@ files).
1258
1259 <tag>@__CONCURRENT_HASKELL__@:</tag>
1260 <nidx>__CONCURRENT_HASKELL__ macro</nidx>
1261 This symbol is defined when pre-processing Haskell (input) and
1262 pre-processing C (GHC output).  Since GHC from verion 4.00 now
1263 supports concurrent haskell by default, this symbol is always defined.
1264
1265 <tag>@__PARALLEL_HASKELL__@:</tag>
1266 <nidx>__PARALLEL_HASKELL__ macro</nidx>
1267 Only defined when @-parallel@ is in use!  This symbol is defined when
1268 pre-processing Haskell (input) and pre-processing C (GHC output).
1269 </descrip>
1270
1271 Options other than the above can be forced through to the C
1272 pre-processor with the @-opt@ flags (see
1273 Section <ref name="Forcing options to a particular phase." id="forcing-options-through">).
1274
1275 A small word of warning: @-cpp@ is not friendly to ``string
1276 gaps''.<nidx>-cpp vs string gaps</nidx><nidx>string gaps vs
1277 -cpp</nidx>.  In other words, strings such as the following:
1278
1279 <tscreen><verb>
1280         strmod = "\
1281         \ p \
1282         \ "
1283 </verb></tscreen>
1284
1285 don't work with @-cpp@; @/usr/bin/cpp@ elides the
1286 backslash-newline pairs.
1287
1288 However, it appears that if you add a space at the end of the line,
1289 then @cpp@ (at least GNU @cpp@ and possibly other @cpp@s)
1290 leaves the backslash-space pairs alone and the string gap works as
1291 expected.
1292
1293 %************************************************************************
1294 %*                                                                      *
1295 <sect2>Options affecting the C compiler (if applicable)
1296 <label id="options-C-compiler">
1297 <p>
1298 <nidx>include-file options</nidx>
1299 <nidx>C compiler options</nidx>
1300 <nidx>GCC options</nidx>
1301 %*                                                                      *
1302 %************************************************************************
1303
1304 At the moment, quite a few common C-compiler options are passed on
1305 quietly to the C compilation of Haskell-compiler-generated C files.
1306 THIS MAY CHANGE.  Meanwhile, options so sent are:
1307
1308 <tabular ca="ll">
1309 @-ansi@      | do ANSI C (not K&amp;R) @@
1310 @-pedantic@  | be so@@
1311 @-dgcc-lint@ | (hack) short for ``make GCC very paranoid''@@
1312 </tabular>
1313 <nidx>-ansi option (for GCC)</nidx>
1314 <nidx>-pedantic option (for GCC)</nidx>
1315 <nidx>-dgcc-lint option (GCC paranoia)</nidx>
1316
1317 If you are compiling with lots of @ccalls@, etc., you may need to
1318 tell the C~compiler about some @#include@ files.  There is no real
1319 pretty way to do this, but you can use this hack from the
1320 command-line:
1321
1322 <tscreen><verb>
1323 % ghc -c '-#include <X/Xlib.h>' Xstuff.lhs
1324 </verb></tscreen>
1325
1326
1327 %************************************************************************
1328 %*                                                                      *
1329 <sect2>Linking and consistency-checking
1330 <label id="options-linker">
1331 <p>
1332 <nidx>linker options</nidx>
1333 <nidx>ld options</nidx>
1334 %*                                                                      *
1335 %************************************************************************
1336
1337 GHC has to link your code with various libraries, possibly including:
1338 user-supplied, GHC-supplied, and system-supplied (@-lm@ math
1339 library, for example).
1340
1341 <descrip>
1342 <tag>@-l<FOO>@:</tag>
1343 <nidx>-l&lt;lib&gt; option</nidx>
1344 Link in a library named @lib<FOO>.a@ which resides somewhere on the
1345 library directories path.
1346
1347 Because of the sad state of most UNIX linkers, the order of such
1348 options does matter.  Thus: @ghc -lbar *.o@ is almost certainly
1349 wrong, because it will search @libbar.a@ <em>before</em> it has
1350 collected unresolved symbols from the @*.o@ files.
1351 @ghc *.o -lbar@ is probably better.
1352
1353 The linker will of course be informed about some GHC-supplied
1354 libraries automatically; these are:
1355
1356 <tabular ca="ll">
1357 <bf>-l equivalent</bf> | <bf>description</bf> @@
1358 @@
1359 @-lHSrts,-lHSclib@ | basic runtime libraries @@
1360 @-lHS@         | standard Prelude library @@
1361 @-lHS_cbits@  | C support code for standard Prelude library @@
1362 @-lgmp@        | GNU multi-precision library (for Integers)@@
1363 </tabular>
1364
1365 <nidx>-lHS library</nidx>
1366 <nidx>-lHS_cbits library</nidx>
1367 <nidx>-lHSrts library</nidx>
1368 <nidx>-lgmp library</nidx>
1369
1370 <tag>@-syslib <name>@:</tag>
1371 <nidx>-syslib &lt;name&gt; option</nidx>
1372
1373 If you are using a Haskell ``system library'' (e.g., the POSIX
1374 library), just use the @-syslib posix@ option, and the correct code
1375 should be linked in.
1376
1377 <tag>@-L<dir>@:</tag>
1378 <nidx>-L&lt;dir&gt; option</nidx>
1379 Where to find user-supplied libraries...  Prepend the directory
1380 @<dir>@ to the library directories path.
1381
1382 <tag>@-static@:</tag>
1383 <nidx>-static option</nidx>
1384 Tell the linker to avoid shared libraries.
1385
1386 <tag>@-no-link-chk@ and @-link-chk@:</tag>
1387 <nidx>-no-link-chk option</nidx>
1388 <nidx>-link-chk option</nidx>
1389 <nidx>consistency checking of executables</nidx>
1390 By default, immediately after linking an executable, GHC verifies that
1391 the pieces that went into it were compiled with compatible flags; a
1392 ``consistency check''.
1393 (This is to avoid mysterious failures caused by non-meshing of
1394 incompatibly-compiled programs; e.g., if one @.o@ file was compiled
1395 for a parallel machine and the others weren't.)  You may turn off this
1396 check with @-no-link-chk@.  You can turn it (back) on with
1397 @-link-chk@ (the default).
1398
1399 <tag><tt>-no-hs-main</tt>:</tag>
1400 <nidx>-no-hs-main option</nidx>
1401 <nidx>linking Haskell libraries with foreign code</nidx>
1402
1403 In the event you want to include ghc-compiled code as part of another
1404 (non-Haskell) program, the RTS will not be supplying its definition of
1405 <tt/main()/ at link-time, you will have to. To signal that to the
1406 driver script when linking, use <tt/-no-hs-main/.
1407
1408 Notice that since the command-line passed to the linker is rather
1409 involved, you probably want to use the ghc driver script to do the
1410 final link of your `mixed-language' application. This is not a
1411 requirement though, just try linking once with <tt/-v/ on to see what
1412 options the driver passes through to the linker.
1413
1414 </descrip>
1415
1416 %************************************************************************
1417 %*                                                                      *
1418 <sect1>Using Concurrent Haskell
1419 <p>
1420 <nidx>Concurrent Haskell---use</nidx>
1421 %*                                                                      *
1422 %************************************************************************
1423
1424 GHC (as of version 4.00) supports Concurrent Haskell by default,
1425 without requiring a special option or libraries compiled in a certain
1426 way.  To get access to the support libraries for Concurrent Haskell
1427 (ie. @Concurrent@ and friends), use the @-syslib concurrent@ option.
1428
1429 Three RTS options are provided for modifying the behaviour of the
1430 threaded runtime system.  See the descriptions of @-C[<us>]@, @-q@,
1431 and @-t<num>@ in Section <ref name="RTS options for
1432 Concurrent/Parallel Haskell" id="parallel-rts-opts">.
1433
1434 Concurrent Haskell is described in more detail in Section <ref
1435 name="Concurrent and Parallel Haskell" id="concurrent-and-parallel">.
1436
1437 %************************************************************************
1438 %*                                                                      *
1439 <sect1>Using Parallel Haskell
1440 <p>
1441 <nidx>Parallel Haskell---use</nidx>
1442 %*                                                                      *
1443 %************************************************************************
1444
1445 [You won't be able to execute parallel Haskell programs unless PVM3
1446 (Parallel Virtual Machine, version 3) is installed at your site.]
1447
1448 To compile a Haskell program for parallel execution under PVM, use the
1449 @-parallel@ option,<nidx>-parallel option</nidx> both when compiling
1450 <em>and linking</em>.  You will probably want to @import Parallel@
1451 into your Haskell modules.
1452
1453 To run your parallel program, once PVM is going, just invoke it ``as
1454 normal''.  The main extra RTS option is @-N<n>@, to say how many
1455 PVM ``processors'' your program to run on.  (For more details of
1456 all relevant RTS options, please see Section <ref name="RTS options for Concurrent/Parallel Haskell" id="parallel-rts-opts">.)
1457
1458 In truth, running Parallel Haskell programs and getting information
1459 out of them (e.g., parallelism profiles) is a battle with the vagaries of
1460 PVM, detailed in the following sections.
1461
1462 %************************************************************************
1463 %*                                                                      *
1464 <sect2>Dummy's guide to using PVM
1465 <p>
1466 <nidx>PVM, how to use</nidx>
1467 <nidx>Parallel Haskell---PVM use</nidx>
1468 %*                                                                      *
1469 %************************************************************************
1470
1471 Before you can run a parallel program under PVM, you must set the
1472 required environment variables (PVM's idea, not ours); something like,
1473 probably in your @.cshrc@ or equivalent:
1474 <tscreen><verb>
1475 setenv PVM_ROOT /wherever/you/put/it
1476 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
1477 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
1478 </verb></tscreen>
1479
1480 Creating and/or controlling your ``parallel machine'' is a purely-PVM
1481 business; nothing specific to Parallel Haskell.
1482
1483 You use the @pvm@<nidx>pvm command</nidx> command to start PVM on your
1484 machine.  You can then do various things to control/monitor your
1485 ``parallel machine;'' the most useful being:
1486
1487 \begin{tabular}{ll}
1488 @Control-D@ & exit @pvm@, leaving it running \\
1489 @halt@ & kill off this ``parallel machine'' \& exit \\
1490 @add <host>@ & add @<host>@ as a processor \\
1491 @delete <host>@ & delete @<host>@ \\
1492 @reset@ & kill what's going, but leave PVM up \\
1493 @conf@       & list the current configuration \\
1494 @ps@         & report processes' status \\
1495 @pstat <pid>@ & status of a particular process \\
1496 \end{tabular}
1497
1498 The PVM documentation can tell you much, much more about @pvm@!
1499
1500 %************************************************************************
1501 %*                                                                      *
1502 <sect2>Parallelism profiles
1503 <p>
1504 <nidx>parallelism profiles</nidx>
1505 <nidx>profiles, parallelism</nidx>
1506 <nidx>visualisation tools</nidx>
1507 %*                                                                      *
1508 %************************************************************************
1509
1510 With Parallel Haskell programs, we usually don't care about the
1511 results---only with ``how parallel'' it was!  We want pretty pictures.
1512
1513 Parallelism profiles (\`a la @hbcpp@) can be generated with the
1514 @-q@<nidx>-q RTS option (concurrent, parallel)</nidx> RTS option.  The
1515 per-processor profiling info is dumped into files named
1516 @<full-path><program>.gr@.  These are then munged into a PostScript picture,
1517 which you can then display.  For example, to run your program
1518 @a.out@ on 8 processors, then view the parallelism profile, do:
1519
1520 <tscreen><verb>
1521 % ./a.out +RTS -N8 -q
1522 % grs2gr *.???.gr > temp.gr     # combine the 8 .gr files into one
1523 % gr2ps -O temp.gr              # cvt to .ps; output in temp.ps
1524 % ghostview -seascape temp.ps   # look at it!
1525 </verb></tscreen>
1526
1527 The scripts for processing the parallelism profiles are distributed
1528 in @ghc/utils/parallel/@.
1529
1530 %************************************************************************
1531 %*                                                                      *
1532 <sect2>Other useful info about running parallel programs
1533 <p>
1534 %*                                                                      *
1535 %************************************************************************
1536
1537 The ``garbage-collection statistics'' RTS options can be useful for
1538 seeing what parallel programs are doing.  If you do either
1539 @+RTS -Sstderr@<nidx>-Sstderr RTS option</nidx> or @+RTS -sstderr@, then
1540 you'll get mutator, garbage-collection, etc., times on standard
1541 error. The standard error of all PE's other than the `main thread'
1542 appears in @/tmp/pvml.nnn@, courtesy of PVM.
1543
1544 Whether doing @+RTS -Sstderr@ or not, a handy way to watch
1545 what's happening overall is: @tail -f /tmp/pvml.nnn@.
1546
1547 %************************************************************************
1548 %*                                                                      *
1549 <sect2>RTS options for Concurrent/Parallel Haskell
1550 <label id="parallel-rts-opts">
1551 <p>
1552 <nidx>RTS options, concurrent</nidx>
1553 <nidx>RTS options, parallel</nidx>
1554 <nidx>Concurrent Haskell---RTS options</nidx>
1555 <nidx>Parallel Haskell---RTS options</nidx>
1556 %*                                                                      *
1557 %************************************************************************
1558
1559 Besides the usual runtime system (RTS) options
1560 (Section <ref name="Running a compiled program" id="runtime-control">), there are a few options particularly
1561 for concurrent/parallel execution.
1562
1563 <descrip>
1564 <tag>@-N<N>@:</tag>
1565 <nidx>-N&lt;N&gt; RTS option (parallel)</nidx>
1566 (PARALLEL ONLY) Use @<N>@ PVM processors to run this program;
1567 the default is 2.
1568
1569 <tag>@-C[<us>]@:</tag>
1570 <nidx>-C&lt;us&gt; RTS option</nidx>
1571 Sets the context switch interval to @<us>@ microseconds.  A context
1572 switch will occur at the next heap allocation after the timer expires.
1573 With @-C0@ or @-C@, context switches will occur as often as
1574 possible (at every heap allocation).  By default, context switches
1575 occur every 10 milliseconds.  Note that many interval timers are only
1576 capable of 10 millisecond granularity, so the default setting may be
1577 the finest granularity possible, short of a context switch at every
1578 heap allocation.
1579
1580 [NOTE: this option currently has no effect (version 4.00).  Context
1581 switches happen when the current heap block is full, i.e. every 4k of
1582 allocation].
1583
1584 <tag>@-q[v]@:</tag>
1585 <nidx>-q RTS option</nidx>
1586 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
1587 in the file @<program>.qp@.  In the style of @hbcpp@, this profile
1588 records the movement of threads between the green (runnable) and red
1589 (blocked) queues.  If you specify the verbose suboption (@-qv@), the
1590 green queue is split into green (for the currently running thread
1591 only) and amber (for other runnable threads).  We do not recommend
1592 that you use the verbose suboption if you are planning to use the
1593 @hbcpp@ profiling tools or if you are context switching at every heap
1594 check (with @-C@).
1595
1596 <tag>@-t<num>@:</tag>
1597 <nidx>-t&lt;num&gt; RTS option</nidx>
1598 (PARALLEL ONLY) Limit the number of concurrent threads per processor
1599 to @<num>@.  The default is 32.  Each thread requires slightly over 1K
1600 <em>words</em> in the heap for thread state and stack objects.  (For
1601 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
1602 8K bytes.)
1603
1604 <tag>@-d@:</tag>
1605 <nidx>-d RTS option (parallel)</nidx>
1606 (PARALLEL ONLY) Turn on debugging.  It pops up one xterm (or GDB, or
1607 something...) per PVM processor.  We use the standard @debugger@
1608 script that comes with PVM3, but we sometimes meddle with the
1609 @debugger2@ script.  We include ours in the GHC distribution,
1610 in @ghc/utils/pvm/@.
1611
1612 <tag>@-e<num>@:</tag>
1613 <nidx>-e&lt;num&gt; RTS option (parallel)</nidx>
1614 (PARALLEL ONLY) Limit the number of pending sparks per processor to
1615 @<num>@. The default is 100. A larger number may be appropriate if
1616 your program generates large amounts of parallelism initially.
1617
1618 <tag>@-Q<num>@:</tag>
1619 <nidx>-Q&lt;num&gt; RTS option (parallel)</nidx>
1620 (PARALLEL ONLY) Set the size of packets transmitted between processors
1621 to @<num>@. The default is 1024 words. A larger number may be
1622 appropriate if your machine has a high communication cost relative to
1623 computation speed.
1624 </descrip>