[project @ 1998-12-02 13:17:09 by simonm]
[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-unused-imports@:</tag>
363 <nidx>-fwarn-unused-imports option</nidx>
364 <nidx>unused imports, warning</nidx>
365 <nidx>imports, unused</nidx>
366
367 Report any objects that are explicitly imported but never used.
368
369 <tag>@-fwarn-unused-binds@:</tag>
370 <nidx>-fwarn-unused-binds option</nidx>
371 <nidx>unused binds, warning</nidx>
372 <nidx>binds, unused</nidx>
373
374 Report any function definitions (and local bindings) which are unused.
375 For top-level functions, the warning is only given if the binding is
376 not exported.
377
378 <tag>@-fwarn-unused-matches@:</tag>
379 <nidx>-fwarn-unused-matches option</nidx>
380 <nidx>unused matches, warning</nidx>
381 <nidx>matches, unused</nidx>
382
383 Report all unused variables which arise from pattern matches,
384 including patterns consisting of a single variable.  For instance @f x
385 y = []@ would report @x@ and @y@ as unused.  To eliminate the warning,
386 all unused variables can be replaced with wildcards.
387
388 <tag>@-fwarn-duplicate-exports@:</tag>
389 <nidx>-fwarn-duplicate-exports option</nidx>
390 <nidx>duplicate exports, warning</nidx>
391 <nidx>export lists, duplicates</nidx>
392
393 Have the compiler warn about duplicate entries in export lists. This
394 is useful information if you maintain large export lists, and want to
395 avoid the continued export of a definition after you've deleted (one)
396 mention of it in the export list.
397
398 This option is on by default.
399
400 <tag>@-fwarn-missing-signatures@:</tag>
401 <nidx>-fwarn-missing-signatures option</nidx>
402 <nidx>type signatures, missing</nidx>
403
404 If you would like GHC to check that every top-level function/value has
405 a type signature, use the @-fwarn-missing-signatures@ option.  This
406 option is off by default.
407
408 </descrip>
409
410 If you're feeling really paranoid, the @-dcore-lint@
411 option<nidx>-dcore-lint option</nidx> is a good choice.  It turns on
412 heavyweight intra-pass sanity-checking within GHC.  (It checks GHC's
413 sanity, not yours.)
414
415 %************************************************************************
416 %*                                                                      *
417 <sect1>Separate compilation
418 <label id="separate-compilation">
419 <p>
420 <nidx>separate compilation</nidx>
421 <nidx>recompilation checker</nidx>
422 <nidx>make and recompilation</nidx>
423 %*                                                                      *
424 %************************************************************************
425
426 This section describes how GHC supports separate compilation.
427
428 <sect2>Interface files
429 <label id="hi-files">
430 <p>
431 <nidx>interface files</nidx>
432 <nidx>.hi files</nidx>
433
434 When GHC compiles a source file @F@ which contains a module @A@, say,
435 it generates an object @F.o@, <em>and</em> a companion <em>interface
436 file</em> @A.hi@.  The interface file is not intended for human
437 consumption, as you'll see if you take a look at one.  It's merely
438 there to help the compiler compile other modules in the same program.
439
440 NOTE: Having the name of the interface file follow the module name and
441 not the file name, means that working with tools such as @make(1)@
442 become harder. @make@ implicitly assumes that any output files
443 produced by processing a translation unit will have file names that
444 can be derived from the file name of the translation unit.  For
445 instance, pattern rules becomes unusable.  For this reason, we
446 recommend you stick to using the same file name as the module name.
447
448 The interface file for @A@ contains information needed by the compiler
449 when it compiles any module @B@ that imports @A@, whether directly or
450 indirectly.  When compiling @B@, GHC will read @A.hi@ to find the
451 details that it needs to know about things defined in @A@.
452
453 Furthermore, when compiling module @C@ which imports @B@, GHC may
454 decide that it needs to know something about @A@ --- for example, @B@
455 might export a function that involves a type defined in @A@.  In this
456 case, GHC will go and read @A.hi@ even though @C@ does not explicitly
457 import @A@ at all.
458
459 The interface file may contain all sorts of things that aren't
460 explicitly exported from @A@ by the programmer.  For example, even
461 though a data type is exported abstractly, @A.hi@ will contain the
462 full data type definition.  For small function definitions, @A.hi@
463 will contain the complete definition of the function.  For bigger
464 functions, @A.hi@ will contain strictness information about the
465 function.  And so on.  GHC puts much more information into @.hi@ files
466 when optimisation is turned on with the @-O@ flag.  Without @-O@ it
467 puts in just the minimum; with @-O@ it lobs in a whole pile of stuff.
468 <nidx>optimsation, effect on .hi files</nidx>
469
470 @A.hi@ should really be thought of as a compiler-readable version of
471 @A.o@.  If you use a @.hi@ file that wasn't generated by the same
472 compilation run that generates the @.o@ file the compiler may assume
473 all sorts of incorrect things about @A@, resulting in core dumps and
474 other unpleasant happenings.
475
476 %************************************************************************
477 %*                                                                      *
478 <sect2>Finding interface files
479 <label id="options-finding-imports">
480 <p>
481 <nidx>interface files, finding them</nidx>
482 <nidx>finding interface files</nidx>
483 %*                                                                      *
484 %************************************************************************
485
486 In your program, you import a module @Foo@ by saying
487 @import Foo@.  GHC goes looking for an interface file, @Foo.hi@.
488 It has a builtin list of directories (notably including @.@) where
489 it looks.
490
491 <descrip>
492
493 <tag>@-i<dirs>@</tag><nidx>-i&lt;dirs&gt; option</nidx> This flag
494 prepends a colon-separated list of @dirs@ to the ``import
495 directories'' list.
496
497 <tag>@-i@</tag> resets the ``import directories'' list back to nothing.
498
499 <tag>@-fno-implicit-prelude@</tag>
500 <nidx>-fno-implicit-prelude option</nidx>
501 GHC normally imports @Prelude.hi@ files for you.  If you'd rather it
502 didn't, then give it a @-fno-implicit-prelude@ option.  You are
503 unlikely to get very far without a Prelude, but, hey, it's a free
504 country.
505
506 <tag>@-syslib <lib>@</tag>
507 <nidx>-syslib &lt;lib&gt; option</nidx>
508
509 If you are using a system-supplied non-Prelude library (e.g., the
510 POSIX library), just use a @-syslib posix@ option (for example).  The
511 right interface files should then be available.  Section <ref
512 name="The GHC Prelude and Libraries" id="ghc-prelude"> lists the
513 libraries available by this mechanism.
514
515 <tag>@-I<dir>@</tag>
516 <nidx>-I&lt;dir&gt; option</nidx>
517
518 Once a Haskell module has been compiled to C (@.hc@ file), you may
519 wish to specify where GHC tells the C compiler to look for @.h@ files.
520 (Or, if you are using the @-cpp@ option<nidx>-cpp option</nidx>, where
521 it tells the C pre-processor to look...)  For this purpose, use a @-I@
522 option in the usual C-ish way.
523
524 </descrip>
525
526 %************************************************************************
527 %*                                                                      *
528 <sect2>Other options related to interface files
529 <label id="hi-options">
530 <p>
531 <nidx>interface files, options</nidx>
532 %*                                                                      *
533 %************************************************************************
534
535 The interface output may be directed to another file
536 @bar2/Wurble.iface@ with the option @-ohi bar2/Wurble.iface@<nidx>-ohi
537 &lt;file&gt; option</nidx> (not recommended).
538
539 To avoid generating an interface file at all, use a @-nohi@
540 option.<nidx>-nohi option</nidx>
541
542 The compiler does not overwrite an existing @.hi@ interface file if
543 the new one is byte-for-byte the same as the old one; this is friendly
544 to @make@.  When an interface does change, it is often enlightening to
545 be informed.  The @-hi-diffs@<nidx>-hi-diffs option</nidx> option will
546 make @ghc@ run @diff@ on the old and new @.hi@ files. You can also
547 record the difference in the interface file itself, the
548 @-keep-hi-diffs@<nidx>-keep-hi-diffs</nidx> option takes care of that.
549
550 The @.hi@ files from GHC contain ``usage'' information which changes
551 often and uninterestingly.  If you really want to see these changes
552 reported, you need to use the
553 @-hi-diffs-with-usages@<nidx>-hi-diffs-with-usages option</nidx>
554 option.
555
556 Interface files are normally jammed full of compiler-produced
557 <em>pragmas</em>, which record arities, strictness info, etc.  If you
558 think these pragmas are messing you up (or you are doing some kind of
559 weird experiment), you can tell GHC to ignore them with the
560 @-fignore-interface-pragmas@<nidx>-fignore-interface-pragmas
561 option</nidx> option.
562
563 When compiling without optimisations on, the compiler is extra-careful
564 about not slurping in data constructors and instance declarations that
565 it will not need. If you believe it is getting it wrong and not
566 importing stuff which you think it should, this optimisation can be
567 turned off with @-fno-prune-tydecls@ and @-fno-prune-instdecls@.
568 <nidx>-fno-prune-tydecls option</nidx><nidx>-fno-prune-instdecls
569 option</nidx>
570
571 See also Section <ref name="Linking and consistency-checking"
572 id="options-linker">, which describes how the linker finds standard
573 Haskell libraries.
574
575 %************************************************************************
576 %*                                                                      *
577 <sect2>The recompilation checker
578 <label id="recomp">
579 <p>
580 <nidx>recompilation checker</nidx>
581 %*                                                                      *
582 %************************************************************************
583
584 In the olden days, GHC compared the newly-generated @.hi@ file with
585 the previous version; if they were identical, it left the old one
586 alone and didn't change its modification date.  In consequence,
587 importers of a module with an unchanged output @.hi@ file were not
588 recompiled.
589
590 This doesn't work any more.  In our earlier example, module @C@ does
591 not import module @A@ directly, yet changes to @A.hi@ should force a
592 recompilation of @C@.  And some changes to @A@ (changing the
593 definition of a function that appears in an inlining of a function
594 exported by @B@, say) may conceivably not change @B.hi@ one jot.  So
595 now...
596
597 GHC keeps a version number on each interface file, and on each type
598 signature within the interface file.  It also keeps in every interface
599 file a list of the version numbers of everything it used when it last
600 compiled the file.  If the source file's modification date is earlier
601 than the @.o@ file's date (i.e. the source hasn't changed since the
602 file was last compiled), and you give GHC the @-recomp@<nidx>-recomp
603 option</nidx> flag, then GHC will be clever.  It compares the version
604 numbers on the things it needs this time with the version numbers on
605 the things it needed last time (gleaned from the interface file of the
606 module being compiled); if they are all the same it stops compiling
607 rather early in the process saying ``Compilation IS NOT required''.
608 What a beautiful sight!
609
610 It's still an experimental feature (that's why @-recomp@ is off by
611 default), so tell us if you think it doesn't work.
612
613 Patrick Sansom has a workshop paper about how all this is done.  Ask
614 him (email: <htmlurl name="sansom@@dcs.gla.ac.uk"
615 url="mailto:sansom@@dcs.gla.ac.uk">) if you want a copy.
616
617 %************************************************************************
618 %*                                                                      *
619 <sect2>Using @make@
620 <label id="using-make">
621 <p>
622 <ncdx>make</ncdx>
623 %*                                                                      *
624 %************************************************************************
625
626 It is reasonably straightforward to set up a @Makefile@ to use with
627 GHC, assuming you name your source files the same as your modules.
628 Thus:
629
630 <tscreen><verb>
631 HC      = ghc
632 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
633
634 SRCS = Main.lhs Foo.lhs Bar.lhs
635 OBJS = Main.o   Foo.o   Bar.o
636
637 .SUFFIXES : .o .hi .lhs .hc .s
638
639 cool_pgm : $(OBJS)
640         rm $@
641         $(HC) -o $@ $(HC_OPTS) $(OBJS)
642
643 # Standard suffix rules
644 .o.hi:
645         @:
646
647 .lhs.o:
648         $(HC) -c $< $(HC_OPTS)
649
650 .hs.o:
651         $(HC) -c $< $(HC_OPTS)
652
653 # Inter-module dependencies
654 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
655 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
656 </verb></tscreen>
657
658 (Sophisticated @make@ variants may achieve some of the above more
659 elegantly.  Notably, @gmake@'s pattern rules let you write the more
660 comprehensible:
661
662 <tscreen><verb>
663 %.o : %.lhs
664         $(HC) -c $< $(HC_OPTS)
665 </verb></tscreen>
666
667 What we've shown should work with any @make@.)
668
669 Note the cheesy @.o.hi@ rule: It records the dependency of the
670 interface (@.hi@) file on the source.  The rule says a @.hi@ file can
671 be made from a @.o@ file by doing... nothing.  Which is true.
672
673 Note the inter-module dependencies at the end of the Makefile, which
674 take the form
675
676 <tscreen><verb>
677 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
678 </verb></tscreen>
679
680 They tell @make@ that if any of @Foo.o@, @Foo.hc@ or @Foo.s@ have an
681 earlier modification date than @Baz.hi@, then the out-of-date file
682 must be brought up to date.  To bring it up to date, @make@ looks for
683 a rule to do so; one of the preceding suffix rules does the job
684 nicely.
685
686 Putting inter-dependencies of the form @Foo.o : Bar.hi@ into your
687 @Makefile@ by hand is rather error-prone.  Don't worry---never fear,
688 @mkdependHS@ is here! (and is distributed as part of GHC) Add the
689 following to your @Makefile@:
690
691 <tscreen><verb>
692 depend :
693         mkdependHS -- $(HC_OPTS) -- $(SRCS)
694 </verb></tscreen>
695
696 Now, before you start compiling, and any time you change the @imports@
697 in your program, do @make depend@ before you do @make cool_pgm@.
698 @mkdependHS@ will append the needed dependencies to your @Makefile@.
699 @mkdependHS@ is fully describe in Section <ref name="Makefile
700 dependencies in Haskell: using mkdependHS" id="mkdependHS">.
701
702 A few caveats about this simple scheme:
703
704 <itemize>
705
706 <item> You may need to compile some modules explicitly to create their
707 interfaces in the first place (e.g., @make Bar.o@ to create @Bar.hi@).
708
709 <item> You may have to type @make@ more than once for the dependencies
710 to have full effect.  However, a @make@ run that does nothing
711 <em>does</em> mean ``everything's up-to-date.''
712
713 <item> This scheme will work with mutually-recursive modules but,
714 again, it may take multiple iterations to ``settle.''
715
716 </itemize>
717
718 %************************************************************************
719 %*                                                                      *
720 <sect2>How to compile mutually recursive modules
721 <label id="mutual-recursion">
722 <p>
723 <nidx>module system, recursion</nidx>
724 <nidx>recursion, between modules</nidx>
725 %*                                                                      *
726 %************************************************************************
727
728 Currently, the compiler does not have proper support for dealing with
729 mutually recursive modules:
730
731 <tscreen><verb>
732 module A where
733
734 import B
735
736 newtype A = A Int
737
738 f :: B -> A
739 f (B x) = A x
740 --------
741 module B where
742
743 import A
744
745 data B = B !Int
746
747 g :: A -> B
748 g (A x) = B x
749 </verb></tscreen>
750
751 When compiling either module A and B, the compiler will try (in vain)
752 to look for the interface file of the other. So, to get mutually
753 recursive modules off the ground, you need to hand write an interface
754 file for A or B, so as to break the loop.  These hand-written
755 interface files are called @hi-boot@ files, and are placed in a file
756 called @<module>.hi-boot@.  To import from an @hi-boot@ file instead
757 of the standard @.hi@ file, use the following syntax in the importing module:
758 <nidx>hi-boot files</nidx>
759 <nidx>importing, hi-boot files</nidx>
760
761 <tscreen> <verb>
762 import {-# SOURCE #-} A
763 </verb> </tscreen>
764
765 The hand-written interface need only contain the bare minimum of
766 information needed to get the bootstrapping process started.  For
767 example, it doesn't need to contain declarations for <em/everything/
768 that module @A@ exports, only the things required by the module that
769 imports @A@ recursively.
770
771 For the example at hand, the boot interface file for A would look like
772 the following:
773
774 <tscreen><verb>
775 __interface A 1 where
776 __exports A A f;
777 __import PrelBase Int;
778 1 newtype A = A PrelBase.Int ;
779 1 f :: A -> A ;
780 </verb></tscreen>
781
782 The syntax is essentially the same as a normal @.hi@ file
783 (unfortunately), but you can usually tailor an existing @.hi@ file to
784 make a @.hi-boot@ file.
785
786 Notice that we only put the declaration for the newtype @A@ in the
787 @hi-boot@ file, not the signature for @f@, since @f@ isn't used by
788 @B@.
789
790 The number ``1'' at the beginning of a declaration is the <em>version
791 number</em> of that declaration: for the purposes of @.hi-boot@ files
792 these can all be set to 1.  All names must be fully qualified with the
793 <em/original/ module that an object comes from: for example, the
794 reference to @Int@ in the interface for @A@ comes from @PrelBase@,
795 which is a module internal to GHC's prelude.  It's a pain, but that's
796 the way it is.
797
798 <bf>Note:</bf> This is all a temporary solution, a version of the
799 compiler that handles mutually recursive properly without the manual
800 construction of interface files, is in the works.
801
802 %************************************************************************
803 %*                                                                      *
804 <sect1>Optimisation (code improvement)
805 <label id="options-optimise">
806 <p>
807 <nidx>optimisation (GHC)</nidx>
808 <nidx>improvement, code (GHC)</nidx>
809 %*                                                                      *
810 %************************************************************************
811
812 The @-O*@ options specify convenient ``packages'' of optimisation
813 flags; the @-f*@ options described later on specify
814 <em>individual</em> optimisations to be turned on/off; the @-m*@
815 options specify <em>machine-specific</em> optimisations to be turned
816 on/off.
817
818 %----------------------------------------------------------------------
819 <sect2>@-O*@: convenient ``packages'' of optimisation flags.
820 <label id="optimise-pkgs">
821 <p>
822 <nidx>-O options</nidx>
823
824 There are <em>many</em> options that affect the quality of code
825 produced by GHC.  Most people only have a general goal, something like
826 ``Compile quickly'' or ``Make my program run like greased lightning.''
827 The following ``packages'' of optimisations (or lack thereof) should
828 suffice.
829
830 Once you choose a @-O*@ ``package,'' stick with it---don't chop and
831 change.  Modules' interfaces <em>will</em> change with a shift to a new
832 @-O*@ option, and you may have to recompile a large chunk of all
833 importing modules before your program can again be run
834 safely (see Section <ref name="The recompilation checker" id="recomp">).
835
836 <descrip>
837 <tag>No @-O*@-type option specified:</tag>
838 <nidx>-O* not specified</nidx>
839 This is taken to mean: ``Please compile quickly; I'm not over-bothered
840 about compiled-code quality.''  So, for example: @ghc -c Foo.hs@
841
842 <tag>@-O@ or @-O1@:</tag>
843 <nidx>-O option</nidx>
844 <nidx>-O1 option</nidx>
845 <nidx>optimise normally</nidx>
846 Means: ``Generate good-quality code without taking too long about it.''
847 Thus, for example: @ghc -c -O Main.lhs@
848
849 <tag>@-O2@:</tag>
850 <nidx>-O2 option</nidx>
851 <nidx>optimise aggressively</nidx>
852 Means: ``Apply every non-dangerous optimisation, even if it means
853 significantly longer compile times.''
854
855 The avoided ``dangerous'' optimisations are those that can make
856 runtime or space <em>worse</em> if you're unlucky.  They are
857 normally turned on or off individually.
858
859 At the moment, @-O2@ is <em>unlikely</em> to produce
860 better code than @-O@.
861
862 <tag>@-O2-for-C@:</tag>
863 <nidx>-O2-for-C option</nidx>
864 <nidx>gcc, invoking with -O2</nidx>
865
866 Says to run GCC with @-O2@, which may be worth a few percent in
867 execution speed.  Don't forget @-fvia-C@, lest you use the native-code
868 generator and bypass GCC altogether!
869
870 <tag>@-Onot@:</tag>
871 <nidx>-Onot option</nidx>
872 <nidx>optimising, reset</nidx>
873
874 This option will make GHC ``forget'' any -Oish options it has seen so
875 far.  Sometimes useful; for example: @make all EXTRA_HC_OPTS=-Onot@.
876
877 <tag>@-Ofile <file>@:</tag>
878 <nidx>-Ofile &lt;file&gt; option</nidx>
879 <nidx>optimising, customised</nidx>
880
881 For those who need <em>absolute</em> control over <em>exactly</em>
882 what options are used (e.g., compiler writers, sometimes :-), a list
883 of options can be put in a file and then slurped in with @-Ofile@.
884
885 In that file, comments are of the @#@-to-end-of-line variety; blank
886 lines and most whitespace is ignored.
887
888 Please ask if you are baffled and would like an example of @-Ofile@!
889 </descrip>
890
891 At Glasgow, we don't use a @-O*@ flag for day-to-day work.  We use
892 @-O@ to get respectable speed; e.g., when we want to measure
893 something.  When we want to go for broke, we tend to use @-O -fvia-C
894 -O2-for-C@ (and we go for lots of coffee breaks).
895
896 The easiest way to see what @-O@ (etc) ``really mean'' is to run with
897 @-v@, then stand back in amazement.  Alternatively, just look at the
898 @HsC_minus<blah>@ lists in the @ghc@ driver script.
899
900 %----------------------------------------------------------------------
901 <sect2>@-f*@: platform-independent flags
902 <p>
903 <nidx>-f* options (GHC)</nidx>
904 <nidx>-fno-* options (GHC)</nidx>
905
906 Flags can be turned <em>off</em> individually.  (NB: I hope you have a
907 good reason for doing this....) To turn off the @-ffoo@ flag, just use
908 the @-fno-foo@ flag.<nidx>-fno-&lt;opt&gt; anti-option</nidx> So, for
909 example, you can say @-O2 -fno-strictness@, which will then drop out
910 any running of the strictness analyser.
911
912 The options you are most likely to want to turn off are:
913 @-fno-strictness@<nidx>-fno-strictness option</nidx> (strictness
914 analyser [because it is sometimes slow]),
915 @-fno-specialise@<nidx>-fno-specialise option</nidx> (automatic
916 specialisation of overloaded functions [because it makes your code
917 bigger]) [US spelling also accepted], and
918 @-fno-update-analysis@<nidx>-fno-update-analysis option</nidx> (update
919 analyser, because it sometimes takes a <em>long</em> time).  This one
920 is only enabled with -O2 anyway.
921
922 Should you wish to turn individual flags <em>on</em>, you are advised
923 to use the @-Ofile@ option, described above.  Because the order in
924 which optimisation passes are run is sometimes crucial, it's quite
925 hard to do with command-line options.
926
927 Here are some ``dangerous'' optimisations you <em>might</em> want to try:
928 <descrip>
929 %------------------------------------------------------------------
930 <tag>@-fvia-C@:</tag>
931 <nidx>-fvia-C option</nidx>
932 <nidx>native code generator, turning off</nidx>
933
934 Compile via C, and don't use the native-code generator.  (There are
935 many cases when GHC does this on its own.)  You might pick up a little
936 bit of speed by compiling via C.  If you use @_ccall_gc_@s or
937 @_casm_@s, you probably <em>have to</em> use @-fvia-C@.
938
939 The lower-case incantation, @-fvia-c@, is synonymous.
940
941 Compiling via C will probably be slower (in compilation time) than
942 using GHC's native code generator.
943
944 <tag>@-funfolding-interface-threshold<n>@:</tag>
945 <nidx>-funfolding-interface-threshold option</nidx>
946 <nidx>inlining, controlling</nidx>
947 <nidx>unfolding, controlling</nidx>
948 (Default: 30) By raising or lowering this number, you can raise or
949 lower the amount of pragmatic junk that gets spewed into interface
950 files.  (An unfolding has a ``size'' that reflects the cost in terms
951 of ``code bloat'' of expanding that unfolding in another module.  A
952 bigger function would be assigned a bigger cost.)
953
954 <tag>@-funfolding-creation-threshold<n>@:</tag>
955 <nidx>-funfolding-creation-threshold option</nidx>
956 <nidx>inlining, controlling</nidx>
957 <nidx>unfolding, controlling</nidx>
958 (Default: 30) This option is similar to
959 @-funfolding-interface-threshold@, except that it governs unfoldings
960 within a single module.  Increasing this figure is more likely to
961 result in longer compile times than faster code.  The next option is
962 more useful:
963
964 <tag>@-funfolding-use-threshold<n>@:</tag>
965 <nidx>-funfolding-use-threshold option</nidx>
966 <nidx>inlining, controlling</nidx>
967 <nidx>unfolding, controlling</nidx>
968 (Default: 8) This is the magic cut-off figure for unfolding: below
969 this size, a function definition will be unfolded at the call-site,
970 any bigger and it won't.  The size computed for a function depends on
971 two things: the actual size of the expression minus any discounts that
972 apply (see @-funfolding-con-discount@).
973
974 <tag>@-funfolding-con-discount<n>@:</tag>
975 <nidx>-funfolding-con-discount option</nidx>
976 <nidx>inlining, controlling</nidx>
977 <nidx>unfolding, controlling</nidx>
978 (Default: 2) If the compiler decides that it can eliminate some
979 computation by performing an unfolding, then this is a discount factor
980 that it applies to the funciton size before deciding whether to unfold
981 it or not.
982
983 OK, folks, these magic numbers `30', `8', and '2' are mildly
984 arbitrary; they are of the ``seem to be OK'' variety.  The `8' is the
985 more critical one; it's what determines how eager GHC is about
986 expanding unfoldings.
987
988 <tag>@-fsemi-tagging@:</tag>
989 This option (which <em>does not work</em> with the native-code generator)
990 tells the compiler to add extra code to test for already-evaluated
991 values.  You win if you have lots of such values during a run of your
992 program, you lose otherwise.  (And you pay in extra code space.)
993
994 We have not played with @-fsemi-tagging@ enough to recommend it.
995 (For all we know, it doesn't even work anymore...  Sigh.)
996 </descrip>
997
998 %----------------------------------------------------------------------
999 <sect2>@-m*@: platform-specific flags
1000 <p>
1001 <nidx>-m* options (GHC)</nidx>
1002 <nidx>platform-specific options</nidx>
1003 <nidx>machine-specific options</nidx>
1004
1005 Some flags only make sense for particular target platforms.
1006
1007 <descrip>
1008 <tag>@-mv8@:</tag>
1009 (SPARC machines)<nidx>-mv8 option (SPARC only)</nidx>
1010 Means to pass the like-named option to GCC; it says to use the
1011 Version 8 SPARC instructions, notably integer multiply and divide.
1012 The similiar @-m*@ GCC options for SPARC also work, actually.
1013
1014 <tag>@-mlong-calls@:</tag>
1015 (HPPA machines)<nidx>-mlong-calls option (HPPA only)</nidx>
1016 Means to pass the like-named option to GCC.  Required for Very Big
1017 modules, maybe.  (Probably means you're in trouble...)
1018
1019 <tag>@-monly-[32]-regs@:</tag>
1020 (iX86 machines)<nidx>-monly-N-regs option (iX86 only)</nidx>
1021 GHC tries to ``steal'' four registers from GCC, for performance
1022 reasons; it almost always works.  However, when GCC is compiling some
1023 modules with four stolen registers, it will crash, probably saying:
1024 <tscreen><verb>
1025 Foo.hc:533: fixed or forbidden register was spilled.
1026 This may be due to a compiler bug or to impossible asm
1027 statements or clauses.
1028 </verb></tscreen>
1029 Just give some registers back with @-monly-N-regs@.  Try `3' first,
1030 then `2'.  If `2' doesn't work, please report the bug to us.
1031 </descrip>
1032
1033 %----------------------------------------------------------------------
1034 <sect2>Code improvement by the C compiler.
1035 <label id="optimise-C-compiler">
1036 <p>
1037 <nidx>optimisation by GCC</nidx>
1038 <nidx>GCC optimisation</nidx>
1039
1040 The C~compiler (GCC) is run with @-O@ turned on.  (It has
1041 to be, actually).
1042
1043 If you want to run GCC with @-O2@---which may be worth a few
1044 percent in execution speed---you can give a
1045 @-O2-for-C@<nidx>-O2-for-C option</nidx> option.
1046
1047 %************************************************************************
1048 %*                                                                      *
1049 <sect1>Options related to a particular phase
1050 <label id="options-phases">
1051 <p>
1052 %*                                                                      *
1053 %************************************************************************
1054
1055 <sect2> The C pre-processor
1056 <label id="c-pre-processor">
1057 <p>
1058 <nidx>pre-processing: cpp</nidx>
1059 <nidx>C pre-processor options</nidx>
1060 <nidx>cpp, pre-processing with</nidx>
1061
1062 The C pre-processor @cpp@ is run over your Haskell code only if the
1063 @-cpp@ option <nidx>-cpp option</nidx> is given.  Unless you are
1064 building a large system with significant doses of conditional
1065 compilation, you really shouldn't need it.
1066 <descrip>
1067 <tag>@-D<foo>@:</tag>
1068 <nidx>-D&lt;name&gt; option</nidx>
1069 Define macro @<foo>@ in the usual way.  NB: does <em>not</em> affect
1070 @-D@ macros passed to the C~compiler when compiling via C!  For those,
1071 use the @-optc-Dfoo@ hack... (see Section <ref name="Forcing options
1072 to a particular phase." id="forcing-options-through">).
1073
1074 <tag>@-U<foo>@:</tag>
1075 <nidx>-U&lt;name&gt; option</nidx>
1076 Undefine macro @<foo>@ in the usual way.
1077
1078 <tag>@-I<dir>@:</tag>
1079 <nidx>-I&lt;dir&gt; option</nidx>
1080 Specify a directory in which to look for @#include@ files, in
1081 the usual C way.
1082 </descrip>
1083
1084 The @ghc@ driver pre-defines several macros:
1085 <descrip>
1086 <tag>@__HASKELL1__@:</tag>
1087 <nidx>__HASKELL1__ macro</nidx>
1088 If defined to <em/n/, that means GHC supports the
1089 Haskell language defined in the Haskell report version <em/1.n/.
1090 Currently 4.
1091
1092 NB. This macro is <em/only/ set when pre-processing Haskell source
1093 (ie. @.hs@ or @.lhs@ files).
1094
1095 <tag>@__GLASGOW_HASKELL__@:</tag>
1096 <nidx>__GLASGOW_HASKELL__ macro</nidx>
1097 For version <em/n/ of the GHC system, this will be @#define@d to
1098 <em/100n/.  So, for version 4.00, it is 400.
1099
1100 With any luck, @__GLASGOW_HASKELL__@ will be undefined in all other
1101 implementations that support C-style pre-processing.
1102
1103 (For reference: the comparable symbols for other systems are:
1104 @__HUGS__@ for Hugs and @__HBC__@ for Chalmers.)
1105
1106 NB. This macro is set when pre-processing both Haskell source and C
1107 source, including the C source generated from a Haskell module
1108 (ie. @.hs@, @.lhs@, @.c@ and @.hc@ files).
1109
1110 <tag>@__CONCURRENT_HASKELL__@:</tag>
1111 <nidx>__CONCURRENT_HASKELL__ macro</nidx>
1112 This symbol is defined when pre-processing Haskell (input) and
1113 pre-processing C (GHC output).  Since GHC from verion 4.00 now
1114 supports concurrent haskell by default, this symbol is always defined.
1115
1116 <tag>@__PARALLEL_HASKELL__@:</tag>
1117 <nidx>__PARALLEL_HASKELL__ macro</nidx>
1118 Only defined when @-parallel@ is in use!  This symbol is defined when
1119 pre-processing Haskell (input) and pre-processing C (GHC output).
1120 </descrip>
1121
1122 Options other than the above can be forced through to the C
1123 pre-processor with the @-opt@ flags (see
1124 Section <ref name="Forcing options to a particular phase." id="forcing-options-through">).
1125
1126 A small word of warning: @-cpp@ is not friendly to ``string
1127 gaps''.<nidx>-cpp vs string gaps</nidx><nidx>string gaps vs
1128 -cpp</nidx>.  In other words, strings such as the following:
1129
1130 <tscreen><verb>
1131         strmod = "\
1132         \ p \
1133         \ "
1134 </verb></tscreen>
1135
1136 don't work with @-cpp@; @/usr/bin/cpp@ elides the
1137 backslash-newline pairs.
1138
1139 However, it appears that if you add a space at the end of the line,
1140 then @cpp@ (at least GNU @cpp@ and possibly other @cpp@s)
1141 leaves the backslash-space pairs alone and the string gap works as
1142 expected.
1143
1144 %************************************************************************
1145 %*                                                                      *
1146 <sect2>Options affecting the C compiler (if applicable)
1147 <label id="options-C-compiler">
1148 <p>
1149 <nidx>include-file options</nidx>
1150 <nidx>C compiler options</nidx>
1151 <nidx>GCC options</nidx>
1152 %*                                                                      *
1153 %************************************************************************
1154
1155 At the moment, quite a few common C-compiler options are passed on
1156 quietly to the C compilation of Haskell-compiler-generated C files.
1157 THIS MAY CHANGE.  Meanwhile, options so sent are:
1158
1159 <tabular ca="ll">
1160 @-ansi@      | do ANSI C (not K&amp;R) @@
1161 @-pedantic@  | be so@@
1162 @-dgcc-lint@ | (hack) short for ``make GCC very paranoid''@@
1163 </tabular>
1164 <nidx>-ansi option (for GCC)</nidx>
1165 <nidx>-pedantic option (for GCC)</nidx>
1166 <nidx>-dgcc-lint option (GCC paranoia)</nidx>
1167
1168 If you are compiling with lots of @ccalls@, etc., you may need to
1169 tell the C~compiler about some @#include@ files.  There is no real
1170 pretty way to do this, but you can use this hack from the
1171 command-line:
1172
1173 <tscreen><verb>
1174 % ghc -c '-#include <X/Xlib.h>' Xstuff.lhs
1175 </verb></tscreen>
1176
1177
1178 %************************************************************************
1179 %*                                                                      *
1180 <sect2>Linking and consistency-checking
1181 <label id="options-linker">
1182 <p>
1183 <nidx>linker options</nidx>
1184 <nidx>ld options</nidx>
1185 %*                                                                      *
1186 %************************************************************************
1187
1188 GHC has to link your code with various libraries, possibly including:
1189 user-supplied, GHC-supplied, and system-supplied (@-lm@ math
1190 library, for example).
1191
1192 <descrip>
1193 <tag>@-l<FOO>@:</tag>
1194 <nidx>-l&lt;lib&gt; option</nidx>
1195 Link in a library named @lib<FOO>.a@ which resides somewhere on the
1196 library directories path.
1197
1198 Because of the sad state of most UNIX linkers, the order of such
1199 options does matter.  Thus: @ghc -lbar *.o@ is almost certainly
1200 wrong, because it will search @libbar.a@ <em>before</em> it has
1201 collected unresolved symbols from the @*.o@ files.
1202 @ghc *.o -lbar@ is probably better.
1203
1204 The linker will of course be informed about some GHC-supplied
1205 libraries automatically; these are:
1206
1207 <tabular ca="ll">
1208 <bf>-l equivalent</bf> | <bf>description</bf> @@
1209 @@
1210 @-lHSrts,-lHSclib@ | basic runtime libraries @@
1211 @-lHS@         | standard Prelude library @@
1212 @-lHS_cbits@  | C support code for standard Prelude library @@
1213 @-lgmp@        | GNU multi-precision library (for Integers)@@
1214 </tabular>
1215
1216 <nidx>-lHS library</nidx>
1217 <nidx>-lHS_cbits library</nidx>
1218 <nidx>-lHSrts library</nidx>
1219 <nidx>-lgmp library</nidx>
1220
1221 <tag>@-syslib <name>@:</tag>
1222 <nidx>-syslib &lt;name&gt; option</nidx>
1223
1224 If you are using a Haskell ``system library'' (e.g., the POSIX
1225 library), just use the @-syslib posix@ option, and the correct code
1226 should be linked in.
1227
1228 <tag>@-L<dir>@:</tag>
1229 <nidx>-L&lt;dir&gt; option</nidx>
1230 Where to find user-supplied libraries...  Prepend the directory
1231 @<dir>@ to the library directories path.
1232
1233 <tag>@-static@:</tag>
1234 <nidx>-static option</nidx>
1235 Tell the linker to avoid shared libraries.
1236
1237 <tag>@-no-link-chk@ and @-link-chk@:</tag>
1238 <nidx>-no-link-chk option</nidx>
1239 <nidx>-link-chk option</nidx>
1240 <nidx>consistency checking of executables</nidx>
1241 By default, immediately after linking an executable, GHC verifies that
1242 the pieces that went into it were compiled with compatible flags; a
1243 ``consistency check''.
1244 (This is to avoid mysterious failures caused by non-meshing of
1245 incompatibly-compiled programs; e.g., if one @.o@ file was compiled
1246 for a parallel machine and the others weren't.)  You may turn off this
1247 check with @-no-link-chk@.  You can turn it (back) on with
1248 @-link-chk@ (the default).
1249 </descrip>
1250
1251 %************************************************************************
1252 %*                                                                      *
1253 <sect1>Using Concurrent Haskell
1254 <p>
1255 <nidx>Concurrent Haskell---use</nidx>
1256 %*                                                                      *
1257 %************************************************************************
1258
1259 GHC (as of version 4.00) supports Concurrent Haskell by default,
1260 without requiring a special option or libraries compiled in a certain
1261 way.  To get access to the support libraries for Concurrent Haskell
1262 (ie. @Concurrent@ and friends), use the @-syslib concurrent@ option.
1263
1264 Three RTS options are provided for modifying the behaviour of the
1265 threaded runtime system.  See the descriptions of @-C[<us>]@, @-q@,
1266 and @-t<num>@ in Section <ref name="RTS options for
1267 Concurrent/Parallel Haskell" id="parallel-rts-opts">.
1268
1269 Concurrent Haskell is described in more detail in Section <ref
1270 name="Concurrent and Parallel Haskell" id="concurrent-and-parallel">.
1271
1272 %************************************************************************
1273 %*                                                                      *
1274 <sect2>Potential problems with Concurrent Haskell
1275 <label id="concurrent-problems">
1276 <p>
1277 <nidx>Concurrent Haskell problems</nidx>
1278 <nidx>problems, Concurrent Haskell</nidx>
1279 %*                                                                      *
1280 %************************************************************************
1281
1282 The previous implementation of Concurrent Haskell in GHC had problems
1283 with using signals handlers in concurrent programs.  The current
1284 system, however, provides thread-safe signal handling (see Section
1285 <ref name="Signal Handling" id="signals">).
1286
1287 %************************************************************************
1288 %*                                                                      *
1289 <sect1>Using Parallel Haskell
1290 <p>
1291 <nidx>Parallel Haskell---use</nidx>
1292 %*                                                                      *
1293 %************************************************************************
1294
1295 [You won't be able to execute parallel Haskell programs unless PVM3
1296 (Parallel Virtual Machine, version 3) is installed at your site.]
1297
1298 To compile a Haskell program for parallel execution under PVM, use the
1299 @-parallel@ option,<nidx>-parallel option</nidx> both when compiling
1300 <em>and linking</em>.  You will probably want to @import Parallel@
1301 into your Haskell modules.
1302
1303 To run your parallel program, once PVM is going, just invoke it ``as
1304 normal''.  The main extra RTS option is @-N<n>@, to say how many
1305 PVM ``processors'' your program to run on.  (For more details of
1306 all relevant RTS options, please see Section <ref name="RTS options for Concurrent/Parallel Haskell" id="parallel-rts-opts">.)
1307
1308 In truth, running Parallel Haskell programs and getting information
1309 out of them (e.g., parallelism profiles) is a battle with the vagaries of
1310 PVM, detailed in the following sections.
1311
1312 %************************************************************************
1313 %*                                                                      *
1314 <sect2>Dummy's guide to using PVM
1315 <p>
1316 <nidx>PVM, how to use</nidx>
1317 <nidx>Parallel Haskell---PVM use</nidx>
1318 %*                                                                      *
1319 %************************************************************************
1320
1321 Before you can run a parallel program under PVM, you must set the
1322 required environment variables (PVM's idea, not ours); something like,
1323 probably in your @.cshrc@ or equivalent:
1324 <tscreen><verb>
1325 setenv PVM_ROOT /wherever/you/put/it
1326 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
1327 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
1328 </verb></tscreen>
1329
1330 Creating and/or controlling your ``parallel machine'' is a purely-PVM
1331 business; nothing specific to Parallel Haskell.
1332
1333 You use the @pvm@<nidx>pvm command</nidx> command to start PVM on your
1334 machine.  You can then do various things to control/monitor your
1335 ``parallel machine;'' the most useful being:
1336
1337 \begin{tabular}{ll}
1338 @Control-D@ & exit @pvm@, leaving it running \\
1339 @halt@ & kill off this ``parallel machine'' \& exit \\
1340 @add <host>@ & add @<host>@ as a processor \\
1341 @delete <host>@ & delete @<host>@ \\
1342 @reset@ & kill what's going, but leave PVM up \\
1343 @conf@       & list the current configuration \\
1344 @ps@         & report processes' status \\
1345 @pstat <pid>@ & status of a particular process \\
1346 \end{tabular}
1347
1348 The PVM documentation can tell you much, much more about @pvm@!
1349
1350 %************************************************************************
1351 %*                                                                      *
1352 <sect2>Parallelism profiles
1353 <p>
1354 <nidx>parallelism profiles</nidx>
1355 <nidx>profiles, parallelism</nidx>
1356 <nidx>visualisation tools</nidx>
1357 %*                                                                      *
1358 %************************************************************************
1359
1360 With Parallel Haskell programs, we usually don't care about the
1361 results---only with ``how parallel'' it was!  We want pretty pictures.
1362
1363 Parallelism profiles (\`a la @hbcpp@) can be generated with the
1364 @-q@<nidx>-q RTS option (concurrent, parallel)</nidx> RTS option.  The
1365 per-processor profiling info is dumped into files named
1366 @<full-path><program>.gr@.  These are then munged into a PostScript picture,
1367 which you can then display.  For example, to run your program
1368 @a.out@ on 8 processors, then view the parallelism profile, do:
1369
1370 <tscreen><verb>
1371 % ./a.out +RTS -N8 -q
1372 % grs2gr *.???.gr > temp.gr     # combine the 8 .gr files into one
1373 % gr2ps -O temp.gr              # cvt to .ps; output in temp.ps
1374 % ghostview -seascape temp.ps   # look at it!
1375 </verb></tscreen>
1376
1377 The scripts for processing the parallelism profiles are distributed
1378 in @ghc/utils/parallel/@.
1379
1380 %************************************************************************
1381 %*                                                                      *
1382 <sect2>Other useful info about running parallel programs
1383 <p>
1384 %*                                                                      *
1385 %************************************************************************
1386
1387 The ``garbage-collection statistics'' RTS options can be useful for
1388 seeing what parallel programs are doing.  If you do either
1389 @+RTS -Sstderr@<nidx>-Sstderr RTS option</nidx> or @+RTS -sstderr@, then
1390 you'll get mutator, garbage-collection, etc., times on standard
1391 error. The standard error of all PE's other than the `main thread'
1392 appears in @/tmp/pvml.nnn@, courtesy of PVM.
1393
1394 Whether doing @+RTS -Sstderr@ or not, a handy way to watch
1395 what's happening overall is: @tail -f /tmp/pvml.nnn@.
1396
1397 %************************************************************************
1398 %*                                                                      *
1399 <sect2>RTS options for Concurrent/Parallel Haskell
1400 <label id="parallel-rts-opts">
1401 <p>
1402 <nidx>RTS options, concurrent</nidx>
1403 <nidx>RTS options, parallel</nidx>
1404 <nidx>Concurrent Haskell---RTS options</nidx>
1405 <nidx>Parallel Haskell---RTS options</nidx>
1406 %*                                                                      *
1407 %************************************************************************
1408
1409 Besides the usual runtime system (RTS) options
1410 (Section <ref name="Running a compiled program" id="runtime-control">), there are a few options particularly
1411 for concurrent/parallel execution.
1412
1413 <descrip>
1414 <tag>@-N<N>@:</tag>
1415 <nidx>-N&lt;N&gt; RTS option (parallel)</nidx>
1416 (PARALLEL ONLY) Use @<N>@ PVM processors to run this program;
1417 the default is 2.
1418
1419 <tag>@-C[<us>]@:</tag>
1420 <nidx>-C&lt;us&gt; RTS option</nidx>
1421 Sets the context switch interval to @<us>@ microseconds.  A context
1422 switch will occur at the next heap allocation after the timer expires.
1423 With @-C0@ or @-C@, context switches will occur as often as
1424 possible (at every heap allocation).  By default, context switches
1425 occur every 10 milliseconds.  Note that many interval timers are only
1426 capable of 10 millisecond granularity, so the default setting may be
1427 the finest granularity possible, short of a context switch at every
1428 heap allocation.
1429
1430 [NOTE: this option currently has no effect (version 4.00).  Context
1431 switches happen when the current heap block is full, i.e. every 4k of
1432 allocation].
1433
1434 <tag>@-q[v]@:</tag>
1435 <nidx>-q RTS option</nidx>
1436 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
1437 in the file @<program>.qp@.  In the style of @hbcpp@, this profile
1438 records the movement of threads between the green (runnable) and red
1439 (blocked) queues.  If you specify the verbose suboption (@-qv@), the
1440 green queue is split into green (for the currently running thread
1441 only) and amber (for other runnable threads).  We do not recommend
1442 that you use the verbose suboption if you are planning to use the
1443 @hbcpp@ profiling tools or if you are context switching at every heap
1444 check (with @-C@).
1445
1446 <tag>@-t<num>@:</tag>
1447 <nidx>-t&lt;num&gt; RTS option</nidx>
1448 (PARALLEL ONLY) Limit the number of concurrent threads per processor
1449 to @<num>@.  The default is 32.  Each thread requires slightly over 1K
1450 <em>words</em> in the heap for thread state and stack objects.  (For
1451 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
1452 8K bytes.)
1453
1454 <tag>@-d@:</tag>
1455 <nidx>-d RTS option (parallel)</nidx>
1456 (PARALLEL ONLY) Turn on debugging.  It pops up one xterm (or GDB, or
1457 something...) per PVM processor.  We use the standard @debugger@
1458 script that comes with PVM3, but we sometimes meddle with the
1459 @debugger2@ script.  We include ours in the GHC distribution,
1460 in @ghc/utils/pvm/@.
1461
1462 <tag>@-e<num>@:</tag>
1463 <nidx>-e&lt;num&gt; RTS option (parallel)</nidx>
1464 (PARALLEL ONLY) Limit the number of pending sparks per processor to
1465 @<num>@. The default is 100. A larger number may be appropriate if
1466 your program generates large amounts of parallelism initially.
1467
1468 <tag>@-Q<num>@:</tag>
1469 <nidx>-Q&lt;num&gt; RTS option (parallel)</nidx>
1470 (PARALLEL ONLY) Set the size of packets transmitted between processors
1471 to @<num>@. The default is 1024 words. A larger number may be
1472 appropriate if your machine has a high communication cost relative to
1473 computation speed.
1474 </descrip>