[project @ 2000-06-12 15:47:27 by simonmar]
[ghc-hetmet.git] / ghc / docs / users_guide / using.sgml
1 <Chapter id="using-GHC">
2 <Title>Using GHC
3 </Title>
4
5 <Para>
6 <IndexTerm><Primary>GHC, using</Primary></IndexTerm>
7 <IndexTerm><Primary>using GHC</Primary></IndexTerm>
8 GHC is a command-line compiler: in order to compile a Haskell program,
9 GHC must be invoked on the source file(s) by typing a command to the
10 shell.  The steps involved in compiling a program can be automated
11 using the <Command>make</Command> tool (this is especially useful if the program
12 consists of multiple source files which depend on each other).  This
13 section describes how to use GHC from the command-line.
14 </Para>
15
16 <Sect1 id="command-line-structure">
17 <Title>Overall command-line structure
18 </Title>
19
20 <Para>
21 <IndexTerm><Primary>structure, command-line</Primary></IndexTerm>
22 <IndexTerm><Primary>command-line structure</Primary></IndexTerm>
23 </Para>
24
25 <Para>
26 An invocation of GHC takes the following form:
27 </Para>
28
29 <Para>
30
31 <Screen>
32 ghc [argument...]
33 </Screen>
34
35 </Para>
36
37 <Para>
38 Command-line arguments are either options or file names.
39 </Para>
40
41 <Para>
42 Command-line options begin with <Literal>-</Literal>.  They may <Emphasis>not</Emphasis> be
43 grouped: <Option>-vO</Option> is different from <Option>-v -O</Option>.  Options need not
44 precede filenames: e.g., <Command>ghc *.o -o foo</Command>.  All options are
45 processed and then applied to all files; you cannot, for example, invoke
46 <Command>ghc -c -O1 Foo.hs -O2 Bar.hs</Command> to apply different optimisation
47 levels to the files <Filename>Foo.hs</Filename> and <Filename>Bar.hs</Filename>.  For conflicting
48 options, e.g., <Option>-c -S</Option>, we reserve the right to do anything we
49 want.  (Usually, the last one applies.)
50 </Para>
51
52 </Sect1>
53
54 <Sect1 id="file-suffixes">
55 <Title>Meaningful file suffixes
56 </Title>
57
58 <Para>
59 <IndexTerm><Primary>suffixes, file</Primary></IndexTerm>
60 <IndexTerm><Primary>file suffixes for GHC</Primary></IndexTerm>
61 </Para>
62
63 <Para>
64 File names with &ldquo;meaningful&rdquo; suffixes (e.g., <Filename>.lhs</Filename> or <Filename>.o</Filename>)
65 cause the &ldquo;right thing&rdquo; to happen to those files.
66 </Para>
67
68 <Para>
69 <VariableList>
70
71 <VarListEntry>
72 <Term><Filename>.lhs</Filename>:</Term>
73 <ListItem>
74 <Para>
75 <IndexTerm><Primary>lhs suffix</Primary></IndexTerm>
76 A &ldquo;literate Haskell&rdquo; module.
77 </Para>
78 </ListItem>
79 </VarListEntry>
80 <VarListEntry>
81 <Term><Filename>.hs</Filename>:</Term>
82 <ListItem>
83 <Para>
84 A not-so-literate Haskell module.
85 </Para>
86 </ListItem>
87 </VarListEntry>
88 <VarListEntry>
89 <Term><Filename>.hi</Filename>:</Term>
90 <ListItem>
91 <Para>
92 A Haskell interface file, probably compiler-generated.
93 </Para>
94 </ListItem>
95 </VarListEntry>
96 <VarListEntry>
97 <Term><Filename>.hc</Filename>:</Term>
98 <ListItem>
99 <Para>
100 Intermediate C file produced by the Haskell compiler.
101 </Para>
102 </ListItem>
103 </VarListEntry>
104 <VarListEntry>
105 <Term><Filename>.c</Filename>:</Term>
106 <ListItem>
107 <Para>
108 A C&nbsp;file not produced by the Haskell compiler.
109 </Para>
110 </ListItem>
111 </VarListEntry>
112 <VarListEntry>
113 <Term><Filename>.s</Filename>:</Term>
114 <ListItem>
115 <Para>
116 An assembly-language source file, usually
117 produced by the compiler.
118 </Para>
119 </ListItem>
120 </VarListEntry>
121 <VarListEntry>
122 <Term><Filename>.o</Filename>:</Term>
123 <ListItem>
124 <Para>
125 An object file, produced by an assembler.
126 </Para>
127 </ListItem>
128 </VarListEntry>
129 </VariableList>
130 </Para>
131
132 <Para>
133 Files with other suffixes (or without suffixes) are passed straight
134 to the linker.
135 </Para>
136
137 </Sect1>
138
139 <Sect1 id="options-help">
140 <Title>Help and verbosity options
141 </Title>
142
143 <Para>
144 <IndexTerm><Primary>help options (GHC)</Primary></IndexTerm>
145 <IndexTerm><Primary>verbose option (GHC)</Primary></IndexTerm>
146 </Para>
147
148 <Para>
149 A good option to start with is the <Option>-help</Option> (or <Option>-?</Option>) option.
150 <IndexTerm><Primary>-help option</Primary></IndexTerm>
151 <IndexTerm><Primary>-? option</Primary></IndexTerm>
152 GHC spews a long message to standard output and then exits.
153 </Para>
154
155 <Para>
156 The <Option>-v</Option><IndexTerm><Primary>-v option</Primary></IndexTerm> option makes GHC <Emphasis>verbose</Emphasis>: it
157 reports its version number and shows (on stderr) exactly how it invokes each
158 phase of the compilation system.  Moreover, it passes
159 the <Option>-v</Option> flag to most phases; each reports
160 its version number (and possibly some other information).
161 </Para>
162
163 <Para>
164 Please, oh please, use the <Option>-v</Option> option when reporting bugs!
165 Knowing that you ran the right bits in the right order is always the
166 first thing we want to verify.
167 </Para>
168
169 <Para>
170 If you're just interested in the compiler version number, the
171 <Option>--version</Option><IndexTerm><Primary>--version option</Primary></IndexTerm> option prints out a
172 one-line string containing the requested info.
173 </Para>
174
175 </Sect1>
176
177 <Sect1 id="options-order">
178 <Title>Running the right phases in the right order
179 </Title>
180
181 <Para>
182 <IndexTerm><Primary>order of passes in GHC</Primary></IndexTerm>
183 <IndexTerm><Primary>pass ordering in GHC</Primary></IndexTerm>
184 The basic task of the <Command>ghc</Command> driver is to run each input file
185 through the right phases (compiling, linking, etc.).
186 </Para>
187
188 <Para>
189 The first phase to run is determined by the input-file suffix, and the
190 last phase is determined by a flag.  If no relevant flag is present,
191 then go all the way through linking.  This table summarises:
192 </Para>
193
194 <Para>
195 <InformalTable>
196 <TGroup Cols="4">
197 <ColSpec Align="Left">
198 <ColSpec Align="Left">
199 <ColSpec Align="Left">
200 <ColSpec Align="Left">
201 <TBody>
202
203 <Row>
204 <Entry>Phase of the compilation system</Entry>
205 <Entry>Suffix saying &ldquo;start here&rdquo;</Entry>
206 <Entry>Flag saying &ldquo;stop after&rdquo;</Entry>
207 <Entry>(suffix of) output file</Entry>
208 </Row>
209
210 <Row>
211 <Entry>
212 literate pre-processor </Entry>
213 <Entry> .lhs </Entry>
214 <Entry> - </Entry>
215 <Entry> - </Entry>
216 </Row>
217 <Row>
218 <Entry>
219 C pre-processor (opt.) </Entry>
220 <Entry> - </Entry>
221 <Entry> - </Entry>
222 <Entry> - </Entry>
223 </Row>
224 <Row>
225 <Entry>
226 Haskell compiler </Entry>
227 <Entry> .hs </Entry>
228 <Entry> -C, -S </Entry>
229 <Entry> .hc, .s </Entry>
230 </Row>
231 <Row>
232 <Entry>
233 C compiler (opt.) </Entry>
234 <Entry> .hc or .c </Entry>
235 <Entry> -S </Entry>
236 <Entry> .s </Entry>
237 </Row>
238 <Row>
239 <Entry>
240 assembler </Entry>
241 <Entry> .s </Entry>
242 <Entry> -c </Entry>
243 <Entry> .o </Entry>
244 </Row>
245 <Row>
246 <Entry>
247 linker </Entry>
248 <Entry> other </Entry>
249 <Entry> - </Entry>
250 <Entry> a.out </Entry>
251 </Row>
252 </TBody>
253 </TGroup>
254 </InformalTable>
255
256 <IndexTerm><Primary>-C option</Primary></IndexTerm>
257 <IndexTerm><Primary>-S option</Primary></IndexTerm>
258 <IndexTerm><Primary>-c option</Primary></IndexTerm>
259 </Para>
260
261 <Para>
262 Thus, a common invocation would be: <Command>ghc -c Foo.hs</Command>
263 </Para>
264
265 <Para>
266 Note: What the Haskell compiler proper produces depends on whether a
267 native-code generator is used (producing assembly language) or not
268 (producing C).
269 </Para>
270
271 <Para>
272 The option <Option>-cpp</Option><IndexTerm><Primary>-cpp option</Primary></IndexTerm> must be given for the C
273 pre-processor phase to be run, that is, the pre-processor will be run
274 over your Haskell source file before continuing.
275 </Para>
276
277 <Para>
278 The option <Option>-E</Option><IndexTerm><Primary>-E option</Primary></IndexTerm> runs just the pre-processing
279 passes of the compiler, outputting the result on stdout before
280 stopping. If used in conjunction with -cpp, the output is the
281 code blocks of the original (literal) source after having put it
282 through the grinder that is the C pre-processor. Sans <Option>-cpp</Option>, the
283 output is the de-litted version of the original source.
284 </Para>
285
286 <Para>
287 The option <Option>-optcpp-E</Option><IndexTerm><Primary>-optcpp-E option</Primary></IndexTerm> runs just the
288 pre-processing stage of the C-compiling phase, sending the result to
289 stdout.  (For debugging or obfuscation contests, usually.)
290 </Para>
291
292 </Sect1>
293
294 <Sect1 id="options-output">
295 <Title>Re-directing the compilation output(s)
296 </Title>
297
298 <Para>
299 <IndexTerm><Primary>output-directing options</Primary></IndexTerm>
300 </Para>
301
302 <Para>
303 GHC's compiled output normally goes into a <Filename>.hc</Filename>, <Filename>.o</Filename>, etc., file,
304 depending on the last-run compilation phase.  The option <Option>-o
305 foo</Option><IndexTerm><Primary>-o option</Primary></IndexTerm> re-directs the output of that last-run
306 phase to file <Filename>foo</Filename>.
307 </Para>
308
309 <Para>
310 Note: this &ldquo;feature&rdquo; can be counterintuitive:
311 <Command>ghc -C -o foo.o foo.hs</Command> will put the intermediate C code in the
312 file <Filename>foo.o</Filename>, name notwithstanding!
313 </Para>
314
315 <Para>
316 EXOTICA: But the <Option>-o</Option> option isn't of much use if you have
317 <Emphasis>several</Emphasis> input files&hellip; Non-interface output files are
318 normally put in the same directory as their corresponding input file
319 came from.  You may specify that they be put in another directory
320 using the <Option>-odir &lt;dir&gt;</Option><IndexTerm><Primary>-odir &lt;dir&gt; option</Primary></IndexTerm> (the
321 &ldquo;Oh, dear&rdquo; option).  For example:
322 </Para>
323
324 <Para>
325
326 <Screen>
327 % ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
328 </Screen>
329
330 </Para>
331
332 <Para>
333 The output files, <Filename>Foo.o</Filename>, <Filename>Bar.o</Filename>, and <Filename>Bumble.o</Filename> would be
334 put into a subdirectory named after the architecture of the executing
335 machine (<Filename>sun4</Filename>, <Filename>mips</Filename>, etc).  The directory must already
336 exist; it won't be created.
337 </Para>
338
339 <Para>
340 Note that the <Option>-odir</Option> option does <Emphasis>not</Emphasis> affect where the
341 interface files are put.  In the above example, they would still be
342 put in <Filename>parse/Foo.hi</Filename>, <Filename>parse/Bar.hi</Filename>, and <Filename>gurgle/Bumble.hi</Filename>.
343 </Para>
344
345 <Para>
346 MORE EXOTICA: The <Option>-osuf &lt;suffix&gt;</Option><IndexTerm><Primary>-osuf &lt;suffix&gt;
347 option</Primary></IndexTerm> will change the <Filename>.o</Filename> file suffix for object files to
348 whatever you specify.  (We use this in compiling the prelude.).
349 Similarly, the <Option>-hisuf &lt;suffix&gt;</Option><IndexTerm><Primary>-hisuf &lt;suffix&gt;
350 option</Primary></IndexTerm> will change the <Filename>.hi</Filename> file suffix for non-system
351 interface files (see <XRef LinkEnd="hi-options">).
352 </Para>
353
354 <Para>
355 The <Option>-hisuf</Option>/<Option>-osuf</Option> game is useful if you want to compile a program
356 with both GHC and HBC (say) in the same directory.  Let HBC use the
357 standard <Filename>.hi</Filename>/<Filename>.o</Filename> suffixes; add <Option>-hisuf g&lowbar;hi -osuf g&lowbar;o</Option> to your
358 <Command>make</Command> rule for GHC compiling&hellip;
359 </Para>
360
361 <Para>
362 FURTHER EXOTICA: If you are doing a normal <Filename>.hs</Filename>-to-<Filename>.o</Filename> compilation
363 but would like to hang onto the intermediate <Filename>.hc</Filename> C file, just
364 throw in a <Option>-keep-hc-file-too</Option> option<IndexTerm><Primary>-keep-hc-file-too option</Primary></IndexTerm>.
365 If you would like to look at the assembler output, toss in a
366 <Option>-keep-s-file-too</Option>,<IndexTerm><Primary>-keep-s-file-too option</Primary></IndexTerm> too.
367 </Para>
368
369 <Sect2 id="saving-ghc-stderr">
370 <Title>Saving GHC's standard error output
371 </Title>
372
373 <Para>
374 <IndexTerm><Primary>standard error, saving</Primary></IndexTerm>
375 </Para>
376
377 <Para>
378 Sometimes, you may cause GHC to be rather chatty on standard error;
379 with <Option>-v</Option>, for example.  You can instruct GHC to <Emphasis>append</Emphasis> this
380 output to a particular log file with a <Option>-odump &lt;blah&gt;</Option><IndexTerm><Primary>-odump
381 &lt;blah&gt; option</Primary></IndexTerm> option.
382 </Para>
383
384 </Sect2>
385
386 <Sect2 id="temp-files">
387 <Title>Redirecting temporary files
388 </Title>
389
390 <Para>
391 <IndexTerm><Primary>temporary files, redirecting</Primary></IndexTerm>
392 </Para>
393
394 <Para>
395 If you have trouble because of running out of space in <Filename>/tmp</Filename> (or
396 wherever your installation thinks temporary files should go), you may
397 use the <Option>-tmpdir &lt;dir&gt;</Option><IndexTerm><Primary>-tmpdir &lt;dir&gt; option</Primary></IndexTerm> option
398 to specify an alternate directory.  For example, <Option>-tmpdir .</Option> says to
399 put temporary files in the current working directory.
400 </Para>
401
402 <Para>
403 Alternatively, use your <Constant>TMPDIR</Constant> environment variable.<IndexTerm><Primary>TMPDIR
404 environment variable</Primary></IndexTerm> Set it to the name of the directory where
405 temporary files should be put.  GCC and other programs will honour the
406 <Constant>TMPDIR</Constant> variable as well.
407 </Para>
408
409 <Para>
410 Even better idea: Set the <Constant>TMPDIR</Constant> variable when building GHC, and
411 never worry about <Constant>TMPDIR</Constant> again. (see the build documentation).
412 </Para>
413
414 </Sect2>
415
416 </Sect1>
417
418 <Sect1 id="options-sanity">
419 <Title>Warnings and sanity-checking
420 </Title>
421
422 <Para>
423 <IndexTerm><Primary>sanity-checking options</Primary></IndexTerm>
424 <IndexTerm><Primary>warnings</Primary></IndexTerm>
425 GHC has a number of options that select which types of non-fatal error
426 messages, otherwise known as warnings, can be generated during
427 compilation.  By default, you get a standard set of warnings which are
428 generally likely to indicate bugs in your program.  These are:
429 <Option>-fwarn-overlpapping-patterns</Option>, <Option>-fwarn-duplicate-exports</Option>, and
430 <Option>-fwarn-missing-methods</Option>.  The following flags are simple ways to
431 select standard &ldquo;packages&rdquo; of warnings:
432 </Para>
433
434 <Para>
435 <VariableList>
436
437 <VarListEntry>
438 <Term><Option>-Wnot</Option>:</Term>
439 <ListItem>
440 <Para>
441 <IndexTerm><Primary>-Wnot option</Primary></IndexTerm>
442 Turns off all warnings, including the standard ones.
443 </Para>
444 </ListItem>
445 </VarListEntry>
446
447 <VarListEntry>
448 <Term><Option>-w</Option>:</Term>
449 <ListItem>
450 <Para>
451 <IndexTerm><Primary>-w option</Primary></IndexTerm>
452 Synonym for <Option>-Wnot</Option>.
453 </Para>
454 </ListItem>
455 </VarListEntry>
456
457 <VarListEntry>
458 <Term><Option>-W</Option>:</Term>
459 <ListItem>
460 <Para>
461 <IndexTerm><Primary>-W option</Primary></IndexTerm>
462 Provides the standard warnings plus <Option>-fwarn-incomplete-patterns</Option>,
463 <Option>-fwarn-unused-imports</Option> and <Option>-fwarn-unused-binds</Option>.
464 </Para>
465 </ListItem>
466 </VarListEntry>
467
468 <VarListEntry>
469 <Term><Option>-Wall</Option>:</Term>
470 <ListItem>
471 <Para>
472 <IndexTerm><Primary>-Wall option</Primary></IndexTerm>
473 Turns on all warning options.
474 </Para>
475 </ListItem>
476 </VarListEntry>
477
478 </VariableList>
479 </Para>
480
481 <Para>
482 The full set of warning options is described below.  To turn off any
483 warning, simply give the corresponding <Option>-fno-warn-...</Option> option on
484 the command line.
485 </Para>
486
487 <Para>
488 <VariableList>
489
490 <VarListEntry>
491 <Term><Option>-fwarn-name-shadowing</Option>:</Term>
492 <ListItem>
493 <Para>
494 <IndexTerm><Primary>-fwarn-name-shadowing option</Primary></IndexTerm>
495 <IndexTerm><Primary>shadowing, warning</Primary></IndexTerm>This option causes a warning to be emitted whenever an inner-scope
496 value has the same name as an outer-scope value, i.e. the inner value
497 shadows the outer one.  This can catch typographical errors that turn
498 into hard-to-find bugs, e.g., in the inadvertent cyclic definition
499 <Literal>let x = ... x ... in</Literal>.
500 </Para>
501
502 <Para>
503 Consequently, this option does <Emphasis>not</Emphasis> allow cyclic recursive
504 definitions.
505 </Para>
506 </ListItem>
507 </VarListEntry>
508
509 <VarListEntry>
510 <Term><Option>-fwarn-overlapping-patterns</Option>:</Term>
511 <ListItem>
512 <Para>
513 <IndexTerm><Primary>-fwarn-overlapping-patterns option</Primary></IndexTerm>
514 <IndexTerm><Primary>overlapping patterns, warning</Primary></IndexTerm>
515 <IndexTerm><Primary>patterns, overlapping</Primary></IndexTerm>
516 By default, the compiler will warn you if a set of patterns are
517 overlapping, i.e.,
518 </Para>
519
520 <Para>
521 <ProgramListing>
522 f :: String -&#62; Int
523 f []     = 0
524 f (_:xs) = 1
525 f "2"    = 2
526 </ProgramListing>
527 </Para>
528
529 <Para>
530 where the last pattern match in <Function>f</Function> won't ever be reached, as the
531 second pattern overlaps it. More often than not, redundant patterns
532 is a programmer mistake/error, so this option is enabled by default.
533 </Para>
534 </ListItem>
535 </VarListEntry>
536
537 <VarListEntry>
538 <Term><Option>-fwarn-incomplete-patterns</Option>:</Term>
539 <ListItem>
540 <Para>
541 <IndexTerm><Primary>-fwarn-incomplete-patterns option</Primary></IndexTerm>
542 <IndexTerm><Primary>incomplete patterns, warning</Primary></IndexTerm>
543 <IndexTerm><Primary>patterns, incomplete</Primary></IndexTerm>
544 Similarly for incomplete patterns, the function <Function>g</Function> below will fail
545 when applied to non-empty lists, so the compiler will emit a warning
546 about this when <Option>-fwarn-incomplete-patterns</Option> is enabled.
547 </Para>
548
549 <Para>
550 <ProgramListing>
551 g [] = 2
552 </ProgramListing>
553 </Para>
554
555 <Para>
556 This option isn't enabled be default because it can be a bit noisy,
557 and it doesn't always indicate a bug in the program.  However, it's
558 generally considered good practice to cover all the cases in your
559 functions.
560 </Para>
561 </ListItem>
562 </VarListEntry>
563
564 <VarListEntry>
565 <Term><Option>-fwarn-missing-methods</Option>:</Term>
566 <ListItem>
567 <Para>
568 <IndexTerm><Primary>-fwarn-missing-methods option</Primary></IndexTerm>
569 <IndexTerm><Primary>missing methods, warning</Primary></IndexTerm>
570 <IndexTerm><Primary>methods, missing</Primary></IndexTerm>
571 This option is on by default, and warns you whenever an instance
572 declaration is missing one or more methods, and the corresponding
573 class declaration has no default declaration for them.
574 </Para>
575 </ListItem>
576 </VarListEntry>
577
578 <VarListEntry>
579 <Term><Option>-fwarn-missing-fields</Option>:</Term>
580 <ListItem>
581 <Para>
582 <IndexTerm><Primary>-fwarn-missing-fields option</Primary></IndexTerm>
583 <IndexTerm><Primary>missing fields, warning</Primary></IndexTerm>
584 <IndexTerm><Primary>fields, missing</Primary></IndexTerm>
585 This option is on by default, and warns you whenever the construction
586 of a labelled field constructor isn't complete, missing initializers
587 for one or more fields. While not an error (the missing fields are
588 initialised with bottoms), it is often an indication of a programmer
589 error.
590 </Para>
591 </ListItem>
592 </VarListEntry>
593
594 <VarListEntry>
595 <Term><Option>-fwarn-unused-imports</Option>:</Term>
596 <ListItem>
597 <Para>
598 <IndexTerm><Primary>-fwarn-unused-imports option</Primary></IndexTerm>
599 <IndexTerm><Primary>unused imports, warning</Primary></IndexTerm>
600 <IndexTerm><Primary>imports, unused</Primary></IndexTerm>
601 Report any objects that are explicitly imported but never used.
602 </Para>
603 </ListItem>
604 </VarListEntry>
605
606 <VarListEntry>
607 <Term><Option>-fwarn-unused-binds</Option>:</Term>
608 <ListItem>
609 <Para>
610 <IndexTerm><Primary>-fwarn-unused-binds option</Primary></IndexTerm>
611 <IndexTerm><Primary>unused binds, warning</Primary></IndexTerm>
612 <IndexTerm><Primary>binds, unused</Primary></IndexTerm>
613 Report any function definitions (and local bindings) which are unused.
614 For top-level functions, the warning is only given if the binding is
615 not exported.
616 </Para>
617 </ListItem>
618 </VarListEntry>
619 <VarListEntry>
620 <Term><Option>-fwarn-unused-matches</Option>:</Term>
621 <ListItem>
622 <Para>
623 <IndexTerm><Primary>-fwarn-unused-matches option</Primary></IndexTerm>
624 <IndexTerm><Primary>unused matches, warning</Primary></IndexTerm>
625 <IndexTerm><Primary>matches, unused</Primary></IndexTerm>
626 Report all unused variables which arise from pattern matches,
627 including patterns consisting of a single variable.  For instance <Literal>f x
628 y = []</Literal> would report <VarName>x</VarName> and <VarName>y</VarName> as unused.  To eliminate the warning,
629 all unused variables can be replaced with wildcards.
630 </Para>
631 </ListItem>
632 </VarListEntry>
633
634 <VarListEntry>
635 <Term><Option>-fwarn-duplicate-exports</Option>:</Term>
636 <ListItem>
637 <Para>
638 <IndexTerm><Primary>-fwarn-duplicate-exports option</Primary></IndexTerm>
639 <IndexTerm><Primary>duplicate exports, warning</Primary></IndexTerm>
640 <IndexTerm><Primary>export lists, duplicates</Primary></IndexTerm>
641 Have the compiler warn about duplicate entries in export lists. This
642 is useful information if you maintain large export lists, and want to
643 avoid the continued export of a definition after you've deleted (one)
644 mention of it in the export list.
645 </Para>
646
647 <Para>
648 This option is on by default.
649 </Para>
650 </ListItem>
651 </VarListEntry>
652
653 <VarListEntry>
654 <Term><Option>-fwarn-type-defaults</Option>:</Term>
655 <ListItem>
656 <Para>
657 <IndexTerm><Primary>-fwarn-type-defaults option</Primary></IndexTerm>
658 <IndexTerm><Primary>defaulting mechanism, warning</Primary></IndexTerm>
659 Have the compiler warn/inform you where in your source the Haskell
660 defaulting mechanism for numeric types kicks in. This is useful
661 information when converting code from a context that assumed one
662 default into one with another, e.g., the `default default' for Haskell
663 1.4 caused the otherwise unconstrained value <Constant>1</Constant> to be given
664 the type <Literal>Int</Literal>, whereas Haskell 98 defaults it to
665 <Literal>Integer</Literal>.  This may lead to differences in performance and
666 behaviour, hence the usefulness of being non-silent about this.
667 </Para>
668
669 <Para>
670 This warning is off by default.
671 </Para>
672 </ListItem>
673 </VarListEntry>
674
675 <VarListEntry>
676 <Term><Option>-fwarn-missing-signatures</Option>:</Term>
677 <ListItem>
678 <Para>
679 <IndexTerm><Primary>-fwarn-missing-signatures option</Primary></IndexTerm>
680 <IndexTerm><Primary>type signatures, missing</Primary></IndexTerm>
681 If you would like GHC to check that every top-level function/value has
682 a type signature, use the <Option>-fwarn-missing-signatures</Option> option.  This
683 option is off by default.
684 </Para>
685 </ListItem>
686 </VarListEntry>
687 </VariableList>
688 </Para>
689
690 <Para>
691 If you're feeling really paranoid, the <Option>-dcore-lint</Option>
692 option<IndexTerm><Primary>-dcore-lint option</Primary></IndexTerm> is a good choice.  It turns on
693 heavyweight intra-pass sanity-checking within GHC.  (It checks GHC's
694 sanity, not yours.)
695 </Para>
696
697 </Sect1>
698
699 <Sect1 id="separate-compilation">
700 <Title>Separate compilation
701 </Title>
702
703 <Para>
704 <IndexTerm><Primary>separate compilation</Primary></IndexTerm>
705 <IndexTerm><Primary>recompilation checker</Primary></IndexTerm>
706 <IndexTerm><Primary>make and recompilation</Primary></IndexTerm>
707 This section describes how GHC supports separate compilation.
708 </Para>
709
710 <Sect2 id="hi-files">
711 <Title>Interface files
712 </Title>
713
714 <Para>
715 <IndexTerm><Primary>interface files</Primary></IndexTerm>
716 <IndexTerm><Primary>.hi files</Primary></IndexTerm>
717 </Para>
718
719 <Para>
720 When GHC compiles a source file <Filename>F</Filename> which contains a module <Literal>A</Literal>, say,
721 it generates an object <Filename>F.o</Filename>, <Emphasis>and</Emphasis> a companion <Emphasis>interface
722 file</Emphasis> <Filename>A.hi</Filename>.  The interface file is not intended for human
723 consumption, as you'll see if you take a look at one.  It's merely
724 there to help the compiler compile other modules in the same program.
725 </Para>
726
727 <Para>
728 NOTE: Having the name of the interface file follow the module name and
729 not the file name, means that working with tools such as <Command>make</Command>
730 become harder. <Command>make</Command> implicitly assumes that any output files
731 produced by processing a translation unit will have file names that
732 can be derived from the file name of the translation unit.  For
733 instance, pattern rules becomes unusable.  For this reason, we
734 recommend you stick to using the same file name as the module name.
735 </Para>
736
737 <Para>
738 The interface file for <Literal>A</Literal> contains information needed by the compiler
739 when it compiles any module <Literal>B</Literal> that imports <Literal>A</Literal>, whether directly or
740 indirectly.  When compiling <Literal>B</Literal>, GHC will read <Filename>A.hi</Filename> to find the
741 details that it needs to know about things defined in <Literal>A</Literal>.
742 </Para>
743
744 <Para>
745 Furthermore, when compiling module <Literal>C</Literal> which imports <Literal>B</Literal>, GHC may
746 decide that it needs to know something about <Literal>A</Literal>&mdash;for example, <Literal>B</Literal>
747 might export a function that involves a type defined in <Literal>A</Literal>.  In this
748 case, GHC will go and read <Command>A.hi</Command> even though <Literal>C</Literal> does not explicitly
749 import <Literal>A</Literal> at all.
750 </Para>
751
752 <Para>
753 The interface file may contain all sorts of things that aren't
754 explicitly exported from <Literal>A</Literal> by the programmer.  For example, even
755 though a data type is exported abstractly, <Filename>A.hi</Filename> will contain the
756 full data type definition.  For small function definitions, <Filename>A.hi</Filename>
757 will contain the complete definition of the function.  For bigger
758 functions, <Filename>A.hi</Filename> will contain strictness information about the
759 function.  And so on.  GHC puts much more information into <Filename>.hi</Filename> files
760 when optimisation is turned on with the <Option>-O</Option> flag.  Without <Option>-O</Option> it
761 puts in just the minimum; with <Option>-O</Option> it lobs in a whole pile of stuff.
762 <IndexTerm><Primary>optimsation, effect on .hi files</Primary></IndexTerm>
763 </Para>
764
765 <Para>
766 <Filename>A.hi</Filename> should really be thought of as a compiler-readable version of
767 <Filename>A.o</Filename>.  If you use a <Filename>.hi</Filename> file that wasn't generated by the same
768 compilation run that generates the <Filename>.o</Filename> file the compiler may assume
769 all sorts of incorrect things about <Literal>A</Literal>, resulting in core dumps and
770 other unpleasant happenings.
771 </Para>
772
773 </Sect2>
774
775 <Sect2 id="options-finding-imports">
776 <Title>Finding interface files
777 </Title>
778
779 <Para>
780 <IndexTerm><Primary>interface files, finding them</Primary></IndexTerm>
781 <IndexTerm><Primary>finding interface files</Primary></IndexTerm>
782 </Para>
783
784 <Para>
785 In your program, you import a module <Literal>Foo</Literal> by saying
786 <Literal>import Foo</Literal>.  GHC goes looking for an interface file, <Filename>Foo.hi</Filename>.
787 It has a builtin list of directories (notably including <Filename>.</Filename>) where
788 it looks.
789 </Para>
790
791 <Para>
792 <VariableList>
793
794 <VarListEntry>
795 <Term><Option>-i&lt;dirs&gt;</Option></Term>
796 <ListItem>
797 <Para>
798 <IndexTerm><Primary>-i&lt;dirs&gt; option</Primary></IndexTerm>This flag
799 prepends a colon-separated list of <Filename>dirs</Filename> to the &ldquo;import
800 directories&rdquo; list.
801 See also <XRef LinkEnd="recomp"> for the significance of using
802 relative and absolute pathnames in the <Option>-i</Option> list.
803 </Para>
804 </ListItem>
805 </VarListEntry>
806
807 <VarListEntry>
808 <Term><Option>-i</Option></Term>
809 <ListItem>
810 <Para>
811 resets the &ldquo;import directories&rdquo; list back to nothing.
812 </Para>
813 </ListItem>
814 </VarListEntry>
815
816 <VarListEntry>
817 <Term><Option>-fno-implicit-prelude</Option></Term>
818 <ListItem>
819 <Para>
820 <IndexTerm><Primary>-fno-implicit-prelude option</Primary></IndexTerm>
821 GHC normally imports <Filename>Prelude.hi</Filename> files for you.  If you'd rather it
822 didn't, then give it a <Option>-fno-implicit-prelude</Option> option.  You are
823 unlikely to get very far without a Prelude, but, hey, it's a free
824 country.
825 </Para>
826 </ListItem>
827 </VarListEntry>
828
829 <VarListEntry>
830 <Term><Option>-syslib &lt;lib&gt;</Option></Term>
831 <ListItem>
832 <Para>
833 <IndexTerm><Primary>-syslib &lt;lib&gt; option</Primary></IndexTerm>
834 If you are using a system-supplied non-Prelude library (e.g., the
835 POSIX library), just use a <Option>-syslib posix</Option> option (for
836 example).  The right interface files should then be available.  The
837 accompanying HsLibs document lists the libraries available by this
838 mechanism.
839 </Para>
840 </ListItem>
841 </VarListEntry>
842
843 <VarListEntry>
844 <Term><Option>-I&lt;dir&gt;</Option></Term>
845 <ListItem>
846 <Para>
847 <IndexTerm><Primary>-I&lt;dir&gt; option</Primary></IndexTerm>
848 Once a Haskell module has been compiled to C (<Filename>.hc</Filename> file), you may
849 wish to specify where GHC tells the C compiler to look for <Filename>.h</Filename> files.
850 (Or, if you are using the <Option>-cpp</Option> option<IndexTerm><Primary>-cpp option</Primary></IndexTerm>, where
851 it tells the C pre-processor to look&hellip;)  For this purpose, use a <Option>-I</Option>
852 option in the usual C-ish way.
853 </Para>
854 </ListItem>
855 </VarListEntry>
856
857 </VariableList>
858 </Para>
859
860 </Sect2>
861
862 <Sect2 id="hi-options">
863 <Title>Other options related to interface files
864 </Title>
865
866 <Para>
867 <IndexTerm><Primary>interface files, options</Primary></IndexTerm>
868 The interface output may be directed to another file
869 <Filename>bar2/Wurble.iface</Filename> with the option <Option>-ohi bar2/Wurble.iface</Option><IndexTerm><Primary>-ohi
870 &lt;file&gt; option</Primary></IndexTerm> (not recommended).
871 </Para>
872
873 <Para>
874 To avoid generating an interface file at all, use a <Option>-nohi</Option>
875 option.<IndexTerm><Primary>-nohi option</Primary></IndexTerm>
876 </Para>
877
878 <Para>
879 The compiler does not overwrite an existing <Filename>.hi</Filename> interface file if
880 the new one is byte-for-byte the same as the old one; this is friendly
881 to <Command>make</Command>.  When an interface does change, it is often enlightening to
882 be informed.  The <Option>-hi-diffs</Option><IndexTerm><Primary>-hi-diffs option</Primary></IndexTerm> option will
883 make GHC run <Command>diff</Command> on the old and new <Filename>.hi</Filename> files. You can also
884 record the difference in the interface file itself, the
885 <Option>-keep-hi-diffs</Option><IndexTerm><Primary>-keep-hi-diffs</Primary></IndexTerm> option takes care of that.
886 </Para>
887
888 <Para>
889 The <Filename>.hi</Filename> files from GHC contain &ldquo;usage&rdquo; information which changes
890 often and uninterestingly.  If you really want to see these changes
891 reported, you need to use the
892 <Option>-hi-diffs-with-usages</Option><IndexTerm><Primary>-hi-diffs-with-usages option</Primary></IndexTerm>
893 option.
894 </Para>
895
896 <Para>
897 Interface files are normally jammed full of compiler-produced
898 <Emphasis>pragmas</Emphasis>, which record arities, strictness info, etc.  If you
899 think these pragmas are messing you up (or you are doing some kind of
900 weird experiment), you can tell GHC to ignore them with the
901 <Option>-fignore-interface-pragmas</Option><IndexTerm><Primary>-fignore-interface-pragmas
902 option</Primary></IndexTerm> option.
903 </Para>
904
905 <Para>
906 When compiling without optimisations on, the compiler is extra-careful
907 about not slurping in data constructors and instance declarations that
908 it will not need. If you believe it is getting it wrong and not
909 importing stuff which you think it should, this optimisation can be
910 turned off with <Option>-fno-prune-tydecls</Option> and <Option>-fno-prune-instdecls</Option>.
911 <IndexTerm><Primary>-fno-prune-tydecls option</Primary></IndexTerm><IndexTerm><Primary>-fno-prune-instdecls
912 option</Primary></IndexTerm>
913 </Para>
914
915 <Para>
916 See also <XRef LinkEnd="options-linker">, which describes how the linker finds standard
917 Haskell libraries.
918 </Para>
919
920 </Sect2>
921
922 <Sect2 id="recomp">
923 <Title>The recompilation checker
924 </Title>
925
926 <IndexTerm><Primary>recompilation checker</Primary></IndexTerm>
927
928 <Para>
929 <variablelist>
930 <VarListEntry>
931 <Term><Option>-recomp</Option></Term>
932 <IndexTerm><Primary><option>-recomp</option> option</Primary></IndexTerm>
933 <ListItem>
934 <Para>
935 (On by default) Turn on recompilation checking.  This will stop
936 compilation early, leaving an existing <filename>.o</filename> file in
937 place, if it can be determined that the module does not need to be
938 recompiled.
939 </Para>
940 </ListItem>
941 </VarListEntry>
942 <VarListEntry>
943 <Term><Option>-no-recomp</Option></Term>
944 <IndexTerm><Primary><option>-recomp</option> option</Primary></IndexTerm>
945 <ListItem>
946 <Para>
947 Turn off recompilation checking.
948 </Para>
949 </ListItem>
950 </VarListEntry>
951 </VariableList>
952 </Para>
953
954 <Para>
955 In the olden days, GHC compared the newly-generated
956 <Filename>.hi</Filename> file with the previous version; if they were
957 identical, it left the old one alone and didn't change its
958 modification date.  In consequence, importers of a module with an
959 unchanged output <Filename>.hi</Filename> file were not recompiled.
960 </Para>
961
962 <Para>
963 This doesn't work any more.  In our earlier example, module
964 <Literal>C</Literal> does not import module <Literal>A</Literal>
965 directly, yet changes to <Filename>A.hi</Filename> should force a
966 recompilation of <Literal>C</Literal>.  And some changes to
967 <Literal>A</Literal> (changing the definition of a function that
968 appears in an inlining of a function exported by <Literal>B</Literal>,
969 say) may conceivably not change <Filename>B.hi</Filename> one jot.  So
970 now&hellip;
971 </Para>
972
973 <Para>
974 GHC keeps a version number on each interface file, and on each type
975 signature within the interface file.  It also keeps in every interface
976 file a list of the version numbers of everything it used when it last
977 compiled the file.  If the source file's modification date is earlier
978 than the <Filename>.o</Filename> file's date (i.e. the source hasn't
979 changed since the file was last compiled), and the
980 <option>-recomp</option> is given on the command line, GHC will be
981 clever.  It compares the version numbers on the things it needs this
982 time with the version numbers on the things it needed last time
983 (gleaned from the interface file of the module being compiled); if
984 they are all the same it stops compiling rather early in the process
985 saying &ldquo;Compilation IS NOT required&rdquo;.  What a beautiful
986 sight!
987 </Para>
988
989 <Para>
990 Patrick Sansom had a workshop paper about how all this is done (though
991 the details have changed quite a bit). <ULink URL="mailto:sansom@dcs.gla.ac.uk">Ask him</ULink> if you want a copy.
992 </Para>
993
994 <Sect3 id="packages">
995 <Title>Packages</Title>
996
997 <Para>
998 <IndexTerm><Primary>packages</Primary></IndexTerm>
999 To simplify organisation and compilation, GHC keeps libraries in <Emphasis>packages</Emphasis>. Packages are also compiled into single libraries on Unix, and DLLs on Windows. The term ``package'' can be used pretty much synonymously with ``library'', except that an application also forms a package, the Main package.
1000 </Para>
1001
1002 <ItemizedList>
1003 <ListItem>
1004 <Para>
1005 A package is a group of modules. It may span many directories, or many packages may exist in a single directory. Packages may not be mutually recursive.
1006 </Para>
1007 </ListItem>
1008
1009 <ListItem>
1010 <Para>
1011 A package has a name (e.g. <Filename>std</Filename>)
1012 </Para>
1013 </ListItem>
1014
1015 <ListItem>
1016 <Para>
1017 Each package is built into a single library (Unix; e.g. <Filename>libHSfoo.a</Filename>), or a single DLL (Windows; e.g. <Filename>HSfoo.dll</Filename>)
1018 </Para>
1019 </ListItem>
1020
1021 <ListItem>
1022 <Para>
1023 The <Option>-package-name foo</Option> flag tells GHC that the module being compiled is destined for package <Filename>foo</Filename>. If this is omitted, the default package, <Filename>Main</Filename>, is assumed.
1024 </Para>
1025 </ListItem>
1026
1027 <ListItem>
1028 <Para>
1029 The <Option>-package foo</Option> flag tells GHC to make available modules 
1030 from package <Filename>foo</Filename>.  It replaces <Option>-syslib foo</Option>, which is now deprecated.
1031 </Para>
1032 </ListItem>
1033
1034 <ListItem>
1035 <Para>
1036 GHC does not maintain detailed cross-package dependency information.
1037 It does remember which modules in other packages the current module
1038 depends on, but not which things within those imported things.
1039 </Para>
1040 </ListItem>
1041 </ItemizedList>
1042
1043 <Para>
1044 All of this tidies up the Prelude enormously.  The Prelude and
1045 Standard Libraries are built into a single package called <Filename>std</Filename>.  (This
1046 is a change; the library is now called <Filename>libHSstd.a</Filename> instead of <Filename>libHS.a</Filename>).
1047 </Para>
1048
1049 <Para>
1050 It is worth noting that on Windows, because each package is built as a DLL, and a reference to a DLL costs an extra indirection, intra-package references are cheaper than inter-package references. Of course, this applies to the <Filename>Main</Filename> package as well. This is not normally the case on most Unices.
1051 </Para>
1052
1053 </Sect3>
1054
1055 </Sect2>
1056
1057
1058 <Sect2 id="using-make">
1059 <Title>Using <Command>make</Command>
1060 </Title>
1061
1062 <Para>
1063 <IndexTerm><Primary><literal>make</literal></Primary></IndexTerm>
1064 </Para>
1065
1066 <Para>
1067 It is reasonably straightforward to set up a <Filename>Makefile</Filename> to use with
1068 GHC, assuming you name your source files the same as your modules.
1069 Thus:
1070 </Para>
1071
1072 <Para>
1073
1074 <ProgramListing>
1075 HC      = ghc
1076 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
1077
1078 SRCS = Main.lhs Foo.lhs Bar.lhs
1079 OBJS = Main.o   Foo.o   Bar.o
1080
1081 .SUFFIXES : .o .hi .lhs .hc .s
1082
1083 cool_pgm : $(OBJS)
1084         rm $@
1085         $(HC) -o $@ $(HC_OPTS) $(OBJS)
1086
1087 # Standard suffix rules
1088 .o.hi:
1089         @:
1090
1091 .lhs.o:
1092         $(HC) -c $&#60; $(HC_OPTS)
1093
1094 .hs.o:
1095         $(HC) -c $&#60; $(HC_OPTS)
1096
1097 # Inter-module dependencies
1098 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
1099 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
1100 </ProgramListing>
1101
1102 </Para>
1103
1104 <Para>
1105 (Sophisticated <Command>make</Command> variants may achieve some of the above more
1106 elegantly.  Notably, <Command>gmake</Command>'s pattern rules let you write the more
1107 comprehensible:
1108 </Para>
1109
1110 <Para>
1111
1112 <ProgramListing>
1113 %.o : %.lhs
1114         $(HC) -c $&#60; $(HC_OPTS)
1115 </ProgramListing>
1116
1117 </Para>
1118
1119 <Para>
1120 What we've shown should work with any <Command>make</Command>.)
1121 </Para>
1122
1123 <Para>
1124 Note the cheesy <Literal>.o.hi</Literal> rule: It records the dependency of the
1125 interface (<Filename>.hi</Filename>) file on the source.  The rule says a <Filename>.hi</Filename> file can
1126 be made from a <Filename>.o</Filename> file by doing&hellip;nothing.  Which is true.
1127 </Para>
1128
1129 <Para>
1130 Note the inter-module dependencies at the end of the Makefile, which
1131 take the form
1132 </Para>
1133
1134 <Para>
1135
1136 <ProgramListing>
1137 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
1138 </ProgramListing>
1139
1140 </Para>
1141
1142 <Para>
1143 They tell <Command>make</Command> that if any of <Literal>Foo.o</Literal>, <Literal>Foo.hc</Literal> or <Literal>Foo.s</Literal> have an
1144 earlier modification date than <Literal>Baz.hi</Literal>, then the out-of-date file
1145 must be brought up to date.  To bring it up to date, <Literal>make</Literal> looks for
1146 a rule to do so; one of the preceding suffix rules does the job
1147 nicely.
1148 </Para>
1149
1150     </sect2>
1151
1152     <sect2 id="sec-makefile-dependencies">
1153       <title>Dependency generation</title>
1154       <indexterm><primary>dependencies in Makefiles</primary></indexterm>
1155       <indexterm><primary>Makefile dependencies</primary></indexterm>
1156
1157       <para>Putting inter-dependencies of the form <Literal>Foo.o :
1158       Bar.hi</Literal> into your <Filename>Makefile</Filename> by hand
1159       is rather error-prone.  Don't worry, GHC has support for
1160       automatically generating the required dependencies.  Add the
1161       following to your <Filename>Makefile</Filename>:</para>
1162
1163 <ProgramListing>
1164 depend :
1165         ghc -M $(HC_OPTS) $(SRCS)
1166 </ProgramListing>
1167
1168       <para>Now, before you start compiling, and any time you change
1169       the <Literal>imports</Literal> in your program, do <Command>make
1170       depend</Command> before you do <Command>make
1171       cool&lowbar;pgm</Command>.  <Command>GHC</Command> will append
1172       the needed dependencies to your
1173       <Filename>Makefile</Filename>.</Para>
1174
1175       <para>In general, if module <Literal>A</Literal> contains the
1176       line
1177
1178 <programlisting>
1179 import B ...blah...
1180 </programlisting>
1181
1182        then <command>mkdependHS</command> will generate a dependency
1183        line of the form:
1184
1185 <programlisting>
1186 A.o : B.hi
1187 </programlisting>
1188
1189        If module <literal>A</literal> contains the line 
1190
1191 <programlisting>
1192 import {-# SOURCE #-} B ...blah...
1193 </programlisting>
1194
1195        then <command>mkdependHS</command> will generate a dependency
1196        line of the form:
1197
1198 <programlisting>
1199 A.o : B.hi-boot
1200 </programlisting>
1201
1202        (See <xref linkend="hi-files"> for details of interface files.)
1203        If <literal>A</literal> imports multiple modules, then there
1204        will be multiple lines with <filename>A.o</filename> as the
1205        target.</para>
1206
1207       <para>By default, <Command>GHC</Command> generates all the
1208       dependencies, and then concatenates them onto the end of
1209       <Filename>makefile</Filename> (or <Filename>Makefile</Filename>
1210       if <Filename>makefile</Filename> doesn't exist) bracketed by the
1211       lines "<Literal>&num; DO NOT DELETE: Beginning of Haskell
1212       dependencies</Literal>" and "<Literal>&num; DO NOT DELETE: End
1213       of Haskell dependencies</Literal>".  If these lines already
1214       exist in the <Filename>makefile</Filename>, then the old
1215       dependencies are deleted first.</para>
1216
1217       <para>Internally, GHC uses a script to generate the
1218       dependencies, called <command>mkdependHS</command>.  This script
1219       has some options of its own, which you might find useful.
1220       Options can be passed directly to <command>mkdependHS</command>
1221       with GHC's <literal>-optdep</literal> option.  For example, to
1222       generate the dependencies into a file called
1223       <literal>.depend</literal> instead of
1224       <literal>Makefile</literal>:</para>
1225
1226 <screen>
1227 ghc -M -optdep-f optdep.depend ...
1228 </screen>
1229       
1230       <para>The full list of options accepted by
1231       <command>mkdependHS</command> is:</para>
1232
1233       <variablelist>
1234
1235         <varlistentry>
1236           <term><option>-w</option></term>
1237           <listitem>
1238             <para>Turn off warnings about interface file shadowing.</para>
1239           </listitem>
1240         </varlistentry>
1241
1242         <varlistentry>
1243           <term><option>-f blah</option></term>
1244           <listitem>
1245             <para>Use <Filename>blah</Filename> as the makefile,
1246             rather than <Filename>makefile</Filename> or
1247             <Filename>Makefile</Filename>.  If
1248             <Filename>blah</Filename> doesn't exist,
1249             <Command>mkdependHS</Command> creates it.  We often use
1250             <Option>-f .depend</Option> to put the dependencies in
1251             <Filename>.depend</Filename> and then
1252             <Command>include</Command> the file
1253             <Filename>.depend</Filename> into
1254             <Filename>Makefile</Filename>.</para>
1255           </listitem>
1256         </varlistentry>
1257
1258         <varlistentry>
1259           <term><option>-o &lt;osuf&gt;</option></term>
1260           <listitem>
1261             <para>Use <Filename>.&lt;osuf&gt;</Filename> as the
1262             "target file" suffix ( default: <Literal>o</Literal>).
1263             Multiple <Option>-o</Option> flags are permitted (GHC2.05
1264             onwards).  Thus "<Option>-o hc -o o</Option>" will
1265             generate dependencies for <Filename>.hc</Filename> and
1266             <Filename>.o</Filename> files.</para>
1267           </listitem>
1268         </varlistentry>
1269
1270         <varlistentry>
1271           <term><option>-s &lt;suf&gt;</option></term>
1272           <listitem>
1273             <para>Make extra dependencies that declare that files with
1274             suffix
1275             <Filename>.&lt;suf&gt;&lowbar;&lt;osuf&gt;</Filename>
1276             depend on interface files with suffix
1277             <Filename>.&lt;suf&gt;&lowbar;hi</Filename>, or (for
1278             <Literal>&lcub;-&num; SOURCE &num;-&rcub;</Literal>
1279             imports) on <Filename>.hi-boot</Filename>.  Multiple
1280             <Option>-s</Option> flags are permitted.  For example,
1281             <Option>-o hc -s a -s b</Option> will make dependencies
1282             for <Filename>.hc</Filename> on <Filename>.hi</Filename>,
1283             <Filename>.a&lowbar;hc</Filename> on
1284             <Filename>.a&lowbar;hi</Filename>, and
1285             <Filename>.b&lowbar;hc</Filename> on
1286             <Filename>.b&lowbar;hi</Filename>.  (Useful in conjunction
1287             with NoFib "ways".)</para>
1288           </listitem>
1289         </varlistentry>
1290
1291         <varlistentry>
1292           <term><option>--exclude-module=&lt;file&gt;</option></term>
1293           <listitem>
1294             <para>Regard <Filename>&lt;file&gt;</Filename> as
1295             "stable"; i.e., exclude it from having dependencies on
1296             it.</para>
1297           </listitem>
1298         </varlistentry>
1299
1300         <varlistentry>
1301           <term><option>-x</option></term>
1302           <listitem>
1303             <para>same as <option>--exclude-module</option></para>
1304           </listitem>
1305         </varlistentry>
1306
1307         <varlistentry>
1308           <term><option>--exclude-directory=&lt;dirs&gt;</option></term>
1309           <listitem>
1310             <para>Regard the colon-separated list of directories
1311             <Filename>&lt;dirs&gt;</Filename> as containing stable,
1312             don't generate any dependencies on modules therein.</para>
1313           </listitem>
1314         </varlistentry>
1315
1316         <varlistentry>
1317           <term><option>-xdirs</option></term>
1318           <listitem>
1319             <para>same as <Option>--exclude-directory</Option>.</para>
1320           </listitem>
1321         </varlistentry>
1322
1323         <varlistentry>
1324           <term><option>--include-module=&lt;file&gt;</option></term>
1325           <listitem>
1326             <para>Regard <Filename>&lt;file&gt;</Filename> as not
1327             "stable"; i.e., generate dependencies on it (if any). This
1328             option is normally used in conjunction with the
1329             <Option>--exclude-directory</Option> option.</para>
1330           </listitem>
1331         </varlistentry>
1332
1333         <varlistentry>
1334           <term><option>--include-prelude</option></term>
1335           <listitem>
1336             <para>Regard prelude libraries as unstable, i.e., generate
1337             dependencies on the prelude modules used (including
1338             <Literal>Prelude</Literal>).  This option is normally only
1339             used by the various system libraries. If a
1340             <Option>-syslib</Option> option is used, dependencies will
1341             also be generated on the library's interfaces.</para>
1342           </listitem>
1343         </varlistentry>
1344       </variablelist>
1345
1346 </Sect2>
1347
1348 <Sect2 id="mutual-recursion">
1349 <Title>How to compile mutually recursive modules
1350 </Title>
1351
1352 <Para>
1353 <IndexTerm><Primary>module system, recursion</Primary></IndexTerm>
1354 <IndexTerm><Primary>recursion, between modules</Primary></IndexTerm>
1355 </Para>
1356
1357 <Para>
1358 Currently, the compiler does not have proper support for dealing with
1359 mutually recursive modules:
1360 </Para>
1361
1362 <Para>
1363
1364 <ProgramListing>
1365 module A where
1366
1367 import B
1368
1369 newtype TA = MkTA Int
1370
1371 f :: TB -&#62; TA
1372 f (MkTB x) = MkTA x
1373 --------
1374 module B where
1375
1376 import A
1377
1378 data TB = MkTB !Int
1379
1380 g :: TA -&#62; TB
1381 g (MkTA x) = MkTB x
1382 </ProgramListing>
1383
1384 </Para>
1385
1386 <Para>
1387 When compiling either module A and B, the compiler will try (in vain)
1388 to look for the interface file of the other. So, to get mutually
1389 recursive modules off the ground, you need to hand write an interface
1390 file for A or B, so as to break the loop.  These hand-written
1391 interface files are called <Literal>hi-boot</Literal> files, and are placed in a file
1392 called <Filename>&lt;module&gt;.hi-boot</Filename>.  To import from an <Literal>hi-boot</Literal> file instead
1393 of the standard <Filename>.hi</Filename> file, use the following syntax in the importing module:
1394 <IndexTerm><Primary>hi-boot files</Primary></IndexTerm>
1395 <IndexTerm><Primary>importing, hi-boot files</Primary></IndexTerm>
1396 </Para>
1397
1398 <Para>
1399
1400 <ProgramListing>
1401 import {-# SOURCE #-} A
1402 </ProgramListing>
1403
1404 </Para>
1405
1406 <Para>
1407 The hand-written interface need only contain the bare minimum of
1408 information needed to get the bootstrapping process started.  For
1409 example, it doesn't need to contain declarations for <Emphasis>everything</Emphasis>
1410 that module <Literal>A</Literal> exports, only the things required by the module that
1411 imports <Literal>A</Literal> recursively.
1412 </Para>
1413
1414 <Para>
1415 For the example at hand, the boot interface file for A would look like
1416 the following:
1417 </Para>
1418
1419 <Para>
1420
1421 <ProgramListing>
1422 __interface A 1 404 where
1423 __export A TA{MkTA} ;
1424 1 newtype TA = MkTA PrelBase.Int ;
1425 </ProgramListing>
1426
1427 </Para>
1428
1429 <Para>
1430 The syntax is essentially the same as a normal <Filename>.hi</Filename> file
1431 (unfortunately), but you can usually tailor an existing <Filename>.hi</Filename> file to
1432 make a <Filename>.hi-boot</Filename> file.
1433 </Para>
1434
1435 <Para>
1436 Notice that we only put the declaration for the newtype <Literal>TA</Literal> in the
1437 <Literal>hi-boot</Literal> file, not the signature for <Function>f</Function>, since <Function>f</Function> isn't used by
1438 <Literal>B</Literal>.
1439 </Para>
1440
1441 <Para>
1442 The number &ldquo;1&rdquo; after &ldquo;&lowbar;&lowbar;interface A&rdquo; gives the version number of module A;
1443 it is incremented whenever anything in A's interface file changes.  The &ldquo;404&rdquo; is
1444 the version number of the interface file <Emphasis>syntax</Emphasis>; we change it when
1445 we change the syntax of interface files so that you get a better error message when
1446 you try to read an old-format file with a new-format compiler.
1447 </Para>
1448
1449 <Para>
1450 The number &ldquo;1&rdquo; at the beginning of a declaration is the <Emphasis>version
1451 number</Emphasis> of that declaration: for the purposes of <Filename>.hi-boot</Filename> files
1452 these can all be set to 1.  All names must be fully qualified with the
1453 <Emphasis>original</Emphasis> module that an object comes from: for example, the
1454 reference to <Literal>Int</Literal> in the interface for <Literal>A</Literal> comes from <Literal>PrelBase</Literal>,
1455 which is a module internal to GHC's prelude.  It's a pain, but that's
1456 the way it is.
1457 </Para>
1458
1459 <Para>
1460 If you want an hi-boot file to export a data type, but you don't want to give its constructors
1461 (because the constructors aren't used by the SOURCE-importing module), you can write simply:
1462 </Para>
1463
1464 <Para>
1465
1466 <ProgramListing>
1467 __interface A 1 404 where
1468 __export A TA;
1469 1 data TA
1470 </ProgramListing>
1471
1472 </Para>
1473
1474 <Para>
1475 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
1476 </Para>
1477
1478 <Para>
1479 <Emphasis>Note:</Emphasis> This is all a temporary solution, a version of the
1480 compiler that handles mutually recursive modules properly without the manual
1481 construction of interface files, is (allegedly) in the works.
1482 </Para>
1483
1484 </Sect2>
1485
1486 </Sect1>
1487
1488 <Sect1 id="options-optimise">
1489 <Title>Optimisation (code improvement)
1490 </Title>
1491
1492 <Para>
1493 <IndexTerm><Primary>optimisation (GHC)</Primary></IndexTerm>
1494 <IndexTerm><Primary>improvement, code (GHC)</Primary></IndexTerm>
1495 </Para>
1496
1497 <Para>
1498 The <Option>-O*</Option> options specify convenient &ldquo;packages&rdquo; of optimisation
1499 flags; the <Option>-f*</Option> options described later on specify
1500 <Emphasis>individual</Emphasis> optimisations to be turned on/off; the <Option>-m*</Option>
1501 options specify <Emphasis>machine-specific</Emphasis> optimisations to be turned
1502 on/off.
1503 </Para>
1504
1505 <Sect2 id="optimise-pkgs">
1506 <Title><Option>-O*</Option>: convenient &ldquo;packages&rdquo; of optimisation flags.
1507 </Title>
1508
1509 <Para>
1510 <IndexTerm><Primary>-O options</Primary></IndexTerm>
1511 </Para>
1512
1513 <Para>
1514 There are <Emphasis>many</Emphasis> options that affect the quality of code
1515 produced by GHC.  Most people only have a general goal, something like
1516 &ldquo;Compile quickly&rdquo; or &ldquo;Make my program run like greased lightning.&rdquo;
1517 The following &ldquo;packages&rdquo; of optimisations (or lack thereof) should
1518 suffice.
1519 </Para>
1520
1521 <Para>
1522 Once you choose a <Option>-O*</Option> &ldquo;package,&rdquo; stick with it&mdash;don't chop and
1523 change.  Modules' interfaces <Emphasis>will</Emphasis> change with a shift to a new
1524 <Option>-O*</Option> option, and you may have to recompile a large chunk of all
1525 importing modules before your program can again be run
1526 safely (see <XRef LinkEnd="recomp">).
1527 </Para>
1528
1529 <Para>
1530 <VariableList>
1531
1532 <VarListEntry>
1533 <Term>No <Option>-O*</Option>-type option specified:</Term>
1534 <IndexTerm><Primary>-O* not specified</Primary></IndexTerm>
1535 <ListItem>
1536 <Para>
1537 This is taken to mean: &ldquo;Please compile quickly; I'm not over-bothered
1538 about compiled-code quality.&rdquo;  So, for example: <Command>ghc -c Foo.hs</Command>
1539 </Para>
1540 </ListItem>
1541 </VarListEntry>
1542 <VarListEntry>
1543 <Term><Option>-O</Option> or <Option>-O1</Option>:</Term>
1544 <IndexTerm><Primary>-O option</Primary></IndexTerm>
1545 <IndexTerm><Primary>-O1 option</Primary></IndexTerm>
1546 <IndexTerm><Primary>optimise</Primary><secondary>normally</secondary></IndexTerm>
1547 <ListItem>
1548 <Para>
1549 Means: &ldquo;Generate good-quality code without taking too long about
1550 it.&rdquo; Thus, for example: <Command>ghc -c -O Main.lhs</Command>
1551 </Para>
1552 </ListItem>
1553 </VarListEntry>
1554 <VarListEntry>
1555 <Term><Option>-O2</Option>:</Term>
1556 <IndexTerm><Primary>-O2 option</Primary></IndexTerm>
1557 <IndexTerm><Primary>optimise</Primary><secondary>aggressively</secondary></IndexTerm>
1558 <ListItem>
1559 <Para>
1560 Means: &ldquo;Apply every non-dangerous optimisation, even if it means
1561 significantly longer compile times.&rdquo;
1562 </Para>
1563
1564 <Para>
1565 The avoided &ldquo;dangerous&rdquo; optimisations are those that can make
1566 runtime or space <Emphasis>worse</Emphasis> if you're unlucky.  They are
1567 normally turned on or off individually.
1568 </Para>
1569
1570 <Para>
1571 At the moment, <Option>-O2</Option> is <Emphasis>unlikely</Emphasis> to produce
1572 better code than <Option>-O</Option>.
1573 </Para>
1574 </ListItem>
1575 </VarListEntry>
1576 <VarListEntry>
1577 <Term><Option>-O2-for-C</Option>:</Term>
1578 <IndexTerm><Primary>-O2-for-C option</Primary></IndexTerm>
1579 <IndexTerm><Primary>gcc, invoking with -O2</Primary></IndexTerm>
1580 <ListItem>
1581 <Para>
1582 Says to run GCC with <Option>-O2</Option>, which may be worth a few percent in
1583 execution speed.  Don't forget <Option>-fvia-C</Option>, lest you use the native-code
1584 generator and bypass GCC altogether!
1585 </Para>
1586 </ListItem>
1587 </VarListEntry>
1588 <VarListEntry>
1589 <Term><Option>-Onot</Option>:</Term>
1590 <IndexTerm><Primary>-Onot option</Primary></IndexTerm>
1591 <IndexTerm><Primary>optimising, reset</Primary></IndexTerm>
1592 <ListItem>
1593 <Para>
1594 This option will make GHC &ldquo;forget&rdquo; any
1595 <Option>-O</Option>ish options it has seen so far.  Sometimes useful;
1596 for example: <Command>make all
1597 EXTRA&lowbar;HC&lowbar;OPTS=-Onot</Command>.
1598 </Para>
1599 </ListItem>
1600 </VarListEntry>
1601 <VarListEntry>
1602 <Term><Option>-Ofile &lt;file&gt;</Option>:</Term>
1603 <IndexTerm><Primary>-Ofile &lt;file&gt; option</Primary></IndexTerm>
1604 <IndexTerm><Primary>optimising, customised</Primary></IndexTerm>
1605 <ListItem>
1606 <Para>
1607 For those who need <Emphasis>absolute</Emphasis> control over
1608 <Emphasis>exactly</Emphasis> what options are used (e.g., compiler
1609 writers, sometimes :-), a list of options can be put in a file and
1610 then slurped in with <Option>-Ofile</Option>.
1611 </Para>
1612
1613 <Para>
1614 In that file, comments are of the
1615 <Literal>&num;</Literal>-to-end-of-line variety; blank lines and most
1616 whitespace is ignored.
1617 </Para>
1618
1619 <Para>
1620 Please ask if you are baffled and would like an example of <Option>-Ofile</Option>!
1621 </Para>
1622 </ListItem>
1623 </VarListEntry>
1624 </VariableList>
1625 </Para>
1626
1627 <Para>
1628 At Glasgow, we don't use a <Option>-O*</Option> flag for day-to-day work.  We use
1629 <Option>-O</Option> to get respectable speed; e.g., when we want to measure
1630 something.  When we want to go for broke, we tend to use <Option>-O -fvia-C
1631 -O2-for-C</Option> (and we go for lots of coffee breaks).
1632 </Para>
1633
1634 <Para>
1635 The easiest way to see what <Option>-O</Option> (etc.) &ldquo;really mean&rdquo; is to run with
1636 <Option>-v</Option>, then stand back in amazement.  Alternatively, just look at the
1637 <Literal>HsC&lowbar;minus&lt;blah&gt;</Literal> lists in the GHC driver script.
1638 </Para>
1639
1640 </Sect2>
1641
1642 <Sect2>
1643 <Title><Option>-f*</Option>: platform-independent flags</Title>
1644
1645 <Para>
1646 <IndexTerm><Primary>-f* options (GHC)</Primary></IndexTerm>
1647 <IndexTerm><Primary>-fno-* options (GHC)</Primary></IndexTerm>
1648 </Para>
1649
1650 <Para>
1651 Flags can be turned <Emphasis>off</Emphasis> individually.  (NB: I hope you have a
1652 good reason for doing this&hellip;) To turn off the <Option>-ffoo</Option> flag, just use
1653 the <Option>-fno-foo</Option> flag.<IndexTerm><Primary>-fno-&lt;opt&gt; anti-option</Primary></IndexTerm> So, for
1654 example, you can say <Option>-O2 -fno-strictness</Option>, which will then drop out
1655 any running of the strictness analyser.
1656 </Para>
1657
1658 <Para>
1659 The options you are most likely to want to turn off are:
1660
1661 <ItemizedList>
1662 <ListItem>
1663
1664 <Para>
1665 <Option>-fno-strictness</Option><IndexTerm><Primary>-fno-strictness option</Primary></IndexTerm> (strictness
1666 analyser, because it is sometimes slow),
1667 </Para>
1668 </ListItem>
1669 <ListItem>
1670
1671 <Para>
1672 <Option>-fno-specialise</Option><IndexTerm><Primary>-fno-specialise option</Primary></IndexTerm> (automatic
1673 specialisation of overloaded functions, because it can make your code
1674 bigger) (US spelling also accepted), and
1675 </Para>
1676 </ListItem>
1677 <ListItem>
1678
1679 <Para>
1680 <Option>-fno-cpr-analyse</Option><IndexTerm><Primary>-fno-cpr-analyse option</Primary></IndexTerm> switches off the CPR (constructed product
1681 result) analyser.
1682 </Para>
1683 </ListItem>
1684
1685 </ItemizedList>
1686
1687 </Para>
1688
1689 <Para>
1690 Should you wish to turn individual flags <Emphasis>on</Emphasis>, you are advised
1691 to use the <Option>-Ofile</Option> option, described above.  Because the order in
1692 which optimisation passes are run is sometimes crucial, it's quite
1693 hard to do with command-line options.
1694 </Para>
1695
1696 <Para>
1697 Here are some &ldquo;dangerous&rdquo; optimisations you <Emphasis>might</Emphasis> want to try:
1698 <VariableList>
1699
1700 <VarListEntry>
1701 <Term><Option>-fvia-C</Option>:</Term>
1702 <ListItem>
1703 <Para>
1704 <IndexTerm><Primary>-fvia-C option</Primary></IndexTerm>
1705 <IndexTerm><Primary>native code generator, turning off</Primary></IndexTerm>
1706 </Para>
1707
1708 <Para>
1709 Compile via C, and don't use the native-code generator.  (There are many
1710 cases when GHC does this on its own.) You might pick up a little bit of
1711 speed by compiling via C (e.g. for floating-point intensive code on Intel).
1712 If you use <Function>&lowbar;casm&lowbar;</Function>s (which are utterly
1713 deprecated), you probably <Emphasis>have</Emphasis> to use
1714 <Option>-fvia-C</Option>.
1715 </Para>
1716
1717 <Para>
1718 The lower-case incantation, <Option>-fvia-c</Option>, is synonymous.
1719 </Para>
1720
1721 <Para>
1722 Compiling via C will probably be slower (in compilation time) than
1723 using GHC's native code generator.
1724 </Para>
1725 </ListItem>
1726 </VarListEntry>
1727 <VarListEntry>
1728 <Term><Option>-funfolding-interface-threshold&lt;n&gt;</Option>:</Term>
1729 <ListItem>
1730 <Para>
1731 <IndexTerm><Primary>-funfolding-interface-threshold option</Primary></IndexTerm>
1732 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
1733 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
1734 (Default: 30) By raising or lowering this number, you can raise or
1735 lower the amount of pragmatic junk that gets spewed into interface
1736 files.  (An unfolding has a &ldquo;size&rdquo; that reflects the cost in terms
1737 of &ldquo;code bloat&rdquo; of expanding that unfolding in another module.  A
1738 bigger function would be assigned a bigger cost.)
1739 </Para>
1740 </ListItem>
1741 </VarListEntry>
1742 <VarListEntry>
1743 <Term><Option>-funfolding-creation-threshold&lt;n&gt;</Option>:</Term>
1744 <ListItem>
1745 <Para>
1746 <IndexTerm><Primary>-funfolding-creation-threshold option</Primary></IndexTerm>
1747 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
1748 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
1749 (Default: 30) This option is similar to
1750 <Option>-funfolding-interface-threshold</Option>, except that it governs unfoldings
1751 within a single module.  Increasing this figure is more likely to
1752 result in longer compile times than faster code.  The next option is
1753 more useful:
1754 </Para>
1755 </ListItem>
1756 </VarListEntry>
1757 <VarListEntry>
1758 <Term><Option>-funfolding-use-threshold&lt;n&gt;</Option>:</Term>
1759 <ListItem>
1760 <Para>
1761 <IndexTerm><Primary>-funfolding-use-threshold option</Primary></IndexTerm>
1762 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
1763 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
1764 (Default: 8) This is the magic cut-off figure for unfolding: below
1765 this size, a function definition will be unfolded at the call-site,
1766 any bigger and it won't.  The size computed for a function depends on
1767 two things: the actual size of the expression minus any discounts that
1768 apply (see <Option>-funfolding-con-discount</Option>).
1769 </Para>
1770 </ListItem>
1771 </VarListEntry>
1772 <VarListEntry>
1773 <Term><Option>-funfolding-con-discount&lt;n&gt;</Option>:</Term>
1774 <ListItem>
1775 <Para>
1776 <IndexTerm><Primary>-funfolding-con-discount option</Primary></IndexTerm>
1777 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
1778 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
1779 (Default: 2) If the compiler decides that it can eliminate some
1780 computation by performing an unfolding, then this is a discount factor
1781 that it applies to the funciton size before deciding whether to unfold
1782 it or not.
1783 </Para>
1784
1785 <Para>
1786 OK, folks, these magic numbers `30', `8', and '2' are mildly
1787 arbitrary; they are of the &ldquo;seem to be OK&rdquo; variety.  The `8' is the
1788 more critical one; it's what determines how eager GHC is about
1789 expanding unfoldings.
1790 </Para>
1791 </ListItem>
1792 </VarListEntry>
1793 <VarListEntry>
1794 <Term><Option>-funbox-strict-fields</Option>:</Term>
1795 <ListItem>
1796 <Para>
1797 <IndexTerm><Primary>-funbox-strict-fields option</Primary></IndexTerm>
1798 <IndexTerm><Primary>strict constructor fields</Primary></IndexTerm>
1799 <IndexTerm><Primary>constructor fields, strict</Primary></IndexTerm>
1800 </Para>
1801
1802 <Para>
1803 This option causes all constructor fields which are marked strict
1804 (i.e. &ldquo;!&rdquo;) to be unboxed or unpacked if possible.  For example:
1805 </Para>
1806
1807 <Para>
1808
1809 <ProgramListing>
1810 data T = T !Float !Float
1811 </ProgramListing>
1812
1813 </Para>
1814
1815 <Para>
1816 will create a constructor <Literal>T</Literal> containing two unboxed floats if the
1817 <Option>-funbox-strict-fields</Option> flag is given.  This may not always be an
1818 optimisation: if the <Function>T</Function> constructor is scrutinised and the floats
1819 passed to a non-strict function for example, they will have to be
1820 reboxed (this is done automatically by the compiler).
1821 </Para>
1822
1823 <Para>
1824 This option should only be used in conjunction with <Option>-O</Option>, in order to
1825 expose unfoldings to the compiler so the reboxing can be removed as
1826 often as possible.  For example:
1827 </Para>
1828
1829 <Para>
1830
1831 <ProgramListing>
1832 f :: T -&#62; Float
1833 f (T f1 f2) = f1 + f2
1834 </ProgramListing>
1835
1836 </Para>
1837
1838 <Para>
1839 The compiler will avoid reboxing <Function>f1</Function> and <Function>f2</Function> by inlining <Function>+</Function> on
1840 floats, but only when <Option>-O</Option> is on.
1841 </Para>
1842
1843 <Para>
1844 Any single-constructor data is eligible for unpacking; for example
1845 </Para>
1846
1847 <Para>
1848
1849 <ProgramListing>
1850 data T = T !(Int,Int)
1851 </ProgramListing>
1852
1853 </Para>
1854
1855 <Para>
1856 will store the two <Literal>Int</Literal>s directly in the <Function>T</Function> constructor, by flattening
1857 the pair.  Multi-level unpacking is also supported:
1858 </Para>
1859
1860 <Para>
1861
1862 <ProgramListing>
1863 data T = T !S
1864 data S = S !Int !Int
1865 </ProgramListing>
1866
1867 </Para>
1868
1869 <Para>
1870 will store two unboxed <Literal>Int&num;</Literal>s directly in the <Function>T</Function> constructor.
1871 </Para>
1872 </ListItem>
1873 </VarListEntry>
1874 <VarListEntry>
1875 <Term><Option>-fsemi-tagging</Option>:</Term>
1876 <ListItem>
1877 <Para>
1878 This option (which <Emphasis>does not work</Emphasis> with the native-code generator)
1879 tells the compiler to add extra code to test for already-evaluated
1880 values.  You win if you have lots of such values during a run of your
1881 program, you lose otherwise.  (And you pay in extra code space.)
1882 </Para>
1883
1884 <Para>
1885 We have not played with <Option>-fsemi-tagging</Option> enough to recommend it.
1886 (For all we know, it doesn't even work anymore&hellip; Sigh.)
1887 </Para>
1888 </ListItem>
1889 </VarListEntry>
1890 </VariableList>
1891 </Para>
1892
1893 </Sect2>
1894
1895 <Sect2>
1896 <Title><Option>-m*</Option>: platform-specific flags</Title>
1897
1898 <Para>
1899 <IndexTerm><Primary>-m* options (GHC)</Primary></IndexTerm>
1900 <IndexTerm><Primary>platform-specific options</Primary></IndexTerm>
1901 <IndexTerm><Primary>machine-specific options</Primary></IndexTerm>
1902 </Para>
1903
1904 <Para>
1905 Some flags only make sense for particular target platforms.
1906 </Para>
1907
1908 <Para>
1909 <VariableList>
1910
1911 <VarListEntry>
1912 <Term><Option>-mv8</Option>:</Term>
1913 <ListItem>
1914 <Para>
1915 (SPARC machines)<IndexTerm><Primary>-mv8 option (SPARC only)</Primary></IndexTerm>
1916 Means to pass the like-named option to GCC; it says to use the
1917 Version 8 SPARC instructions, notably integer multiply and divide.
1918 The similiar <Option>-m*</Option> GCC options for SPARC also work, actually.
1919 </Para>
1920 </ListItem>
1921 </VarListEntry>
1922 <VarListEntry>
1923 <Term><Option>-mlong-calls</Option>:</Term>
1924 <ListItem>
1925 <Para>
1926 (HPPA machines)<IndexTerm><Primary>-mlong-calls option (HPPA only)</Primary></IndexTerm>
1927 Means to pass the like-named option to GCC.  Required for Very Big
1928 modules, maybe.  (Probably means you're in trouble&hellip;)
1929 </Para>
1930 </ListItem>
1931 </VarListEntry>
1932 <VarListEntry>
1933 <Term><Option>-monly-[32]-regs</Option>:</Term>
1934 <ListItem>
1935 <Para>
1936 (iX86 machines)<IndexTerm><Primary>-monly-N-regs option (iX86 only)</Primary></IndexTerm>
1937 GHC tries to &ldquo;steal&rdquo; four registers from GCC, for performance
1938 reasons; it almost always works.  However, when GCC is compiling some
1939 modules with four stolen registers, it will crash, probably saying:
1940
1941 <Screen>
1942 Foo.hc:533: fixed or forbidden register was spilled.
1943 This may be due to a compiler bug or to impossible asm
1944 statements or clauses.
1945 </Screen>
1946
1947 Just give some registers back with <Option>-monly-N-regs</Option>.  Try `3' first,
1948 then `2'.  If `2' doesn't work, please report the bug to us.
1949 </Para>
1950 </ListItem>
1951 </VarListEntry>
1952 </VariableList>
1953 </Para>
1954
1955 </Sect2>
1956
1957 <Sect2 id="optimise-C-compiler">
1958 <Title>Code improvement by the C compiler.
1959 </Title>
1960
1961 <Para>
1962 <IndexTerm><Primary>optimisation by GCC</Primary></IndexTerm>
1963 <IndexTerm><Primary>GCC optimisation</Primary></IndexTerm>
1964 </Para>
1965
1966 <Para>
1967 The C&nbsp;compiler (GCC) is run with <Option>-O</Option> turned on.  (It has
1968 to be, actually).
1969 </Para>
1970
1971 <Para>
1972 If you want to run GCC with <Option>-O2</Option>&mdash;which may be worth a few
1973 percent in execution speed&mdash;you can give a
1974 <Option>-O2-for-C</Option><IndexTerm><Primary>-O2-for-C option</Primary></IndexTerm> option.
1975 </Para>
1976
1977 </Sect2>
1978
1979 </Sect1>
1980
1981 <Sect1 id="options-phases">
1982 <Title>Options related to a particular phase
1983 </Title>
1984
1985 <Sect2 id="c-pre-processor">
1986 <Title>The C pre-processor
1987 </Title>
1988
1989 <Para>
1990 <IndexTerm><Primary>pre-processing: cpp</Primary></IndexTerm>
1991 <IndexTerm><Primary>C pre-processor options</Primary></IndexTerm>
1992 <IndexTerm><Primary>cpp, pre-processing with</Primary></IndexTerm>
1993 </Para>
1994
1995 <Para>
1996 The C pre-processor <Command>cpp</Command> is run over your Haskell code only if the
1997 <Option>-cpp</Option> option <IndexTerm><Primary>-cpp option</Primary></IndexTerm> is given.  Unless you are
1998 building a large system with significant doses of conditional
1999 compilation, you really shouldn't need it.
2000 <VariableList>
2001
2002 <VarListEntry>
2003 <Term><Option>-D&lt;foo&gt;</Option>:</Term>
2004 <ListItem>
2005 <Para>
2006 <IndexTerm><Primary>-D&lt;name&gt; option</Primary></IndexTerm>
2007 Define macro <Constant>&lt;foo&gt;</Constant> in the usual way.  NB: does <Emphasis>not</Emphasis> affect
2008 <Option>-D</Option> macros passed to the C&nbsp;compiler when compiling via C!  For those,
2009 use the <Option>-optc-Dfoo</Option> hack&hellip; (see <XRef LinkEnd="forcing-options-through">).
2010 </Para>
2011 </ListItem>
2012 </VarListEntry>
2013 <VarListEntry>
2014 <Term><Option>-U&lt;foo&gt;</Option>:</Term>
2015 <ListItem>
2016 <Para>
2017 <IndexTerm><Primary>-U&lt;name&gt; option</Primary></IndexTerm>
2018 Undefine macro <Command>&lt;foo&gt;</Command> in the usual way.
2019 </Para>
2020 </ListItem>
2021 </VarListEntry>
2022 <VarListEntry>
2023 <Term><Option>-I&lt;dir&gt;</Option>:</Term>
2024 <ListItem>
2025 <Para>
2026 <IndexTerm><Primary>-I&lt;dir&gt; option</Primary></IndexTerm>
2027 Specify a directory in which to look for <Literal>&num;include</Literal> files, in
2028 the usual C way.
2029 </Para>
2030 </ListItem>
2031 </VarListEntry>
2032 </VariableList>
2033 </Para>
2034
2035 <Para>
2036 The GHC driver pre-defines several macros when processing Haskell
2037 source code (<Filename>.hs</Filename> or <Filename>.lhs</Filename> files):
2038 </Para>
2039
2040 <Para>
2041 <VariableList>
2042
2043 <VarListEntry>
2044 <Term><Constant>&lowbar;&lowbar;HASKELL98&lowbar;&lowbar;</Constant>:</Term>
2045 <ListItem>
2046 <Para>
2047 <IndexTerm><Primary>&lowbar;&lowbar;HASKELL98&lowbar;&lowbar;</Primary></IndexTerm>
2048 If defined, this means that GHC supports the language defined by the
2049 Haskell 98 report.
2050 </Para>
2051 </ListItem>
2052 </VarListEntry>
2053 <VarListEntry>
2054 <Term><Constant>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;=98</Constant>:</Term>
2055 <ListItem>
2056 <Para>
2057 <IndexTerm><Primary>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;</Primary></IndexTerm>
2058 In GHC 4.04 and later, the <Constant>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;</Constant> macro is defined as having
2059 the value <Constant>98</Constant>.
2060 </Para>
2061 </ListItem>
2062 </VarListEntry>
2063 <VarListEntry>
2064 <Term><Constant>&lowbar;&lowbar;HASKELL1&lowbar;&lowbar;</Constant>:</Term>
2065 <ListItem>
2066 <Para>
2067 <IndexTerm><Primary>&lowbar;&lowbar;HASKELL1&lowbar;&lowbar; macro</Primary></IndexTerm>
2068 If defined to <Emphasis>n</Emphasis>, that means GHC supports the Haskell language
2069 defined in the Haskell report version <Emphasis>1.n</Emphasis>.  Currently 5.  This
2070 macro is deprecated, and will probably disappear in future versions.
2071 </Para>
2072 </ListItem>
2073 </VarListEntry>
2074 <VarListEntry>
2075 <Term><Constant>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</Term>
2076 <ListItem>
2077 <Para>
2078 <IndexTerm><Primary>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar; macro</Primary></IndexTerm>
2079 For version <Emphasis>n</Emphasis> of the GHC system, this will be <Literal>&num;define</Literal>d to
2080 <Emphasis>100n</Emphasis>.  So, for version 4.00, it is 400.
2081 </Para>
2082
2083 <Para>
2084 With any luck, <Constant>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar;</Constant> will be undefined in all other
2085 implementations that support C-style pre-processing.
2086 </Para>
2087
2088 <Para>
2089 (For reference: the comparable symbols for other systems are:
2090 <Constant>&lowbar;&lowbar;HUGS&lowbar;&lowbar;</Constant> for Hugs and <Constant>&lowbar;&lowbar;HBC&lowbar;&lowbar;</Constant> for Chalmers.)
2091 </Para>
2092
2093 <Para>
2094 NB. This macro is set when pre-processing both Haskell source and C
2095 source, including the C source generated from a Haskell module
2096 (i.e. <Filename>.hs</Filename>, <Filename>.lhs</Filename>, <Filename>.c</Filename> and <Filename>.hc</Filename> files).
2097 </Para>
2098 </ListItem>
2099 </VarListEntry>
2100 <VarListEntry>
2101 <Term><Constant>&lowbar;&lowbar;CONCURRENT&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</Term>
2102 <ListItem>
2103 <Para>
2104 <IndexTerm><Primary>&lowbar;&lowbar;CONCURRENT&lowbar;HASKELL&lowbar;&lowbar; macro</Primary></IndexTerm>
2105 This symbol is defined when pre-processing Haskell (input) and
2106 pre-processing C (GHC output).  Since GHC from verion 4.00 now
2107 supports concurrent haskell by default, this symbol is always defined.
2108 </Para>
2109 </ListItem>
2110 </VarListEntry>
2111 <VarListEntry>
2112 <Term><Constant>&lowbar;&lowbar;PARALLEL&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</Term>
2113 <ListItem>
2114 <Para>
2115 <IndexTerm><Primary>&lowbar;&lowbar;PARALLEL&lowbar;HASKELL&lowbar;&lowbar; macro</Primary></IndexTerm>
2116 Only defined when <Option>-parallel</Option> is in use!  This symbol is defined when
2117 pre-processing Haskell (input) and pre-processing C (GHC output).
2118 </Para>
2119 </ListItem>
2120 </VarListEntry>
2121 </VariableList>
2122 </Para>
2123
2124 <Para>
2125 Options other than the above can be forced through to the C
2126 pre-processor with the <Option>-opt</Option> flags (see
2127 <XRef LinkEnd="forcing-options-through">).
2128 </Para>
2129
2130 <Para>
2131 A small word of warning: <Option>-cpp</Option> is not friendly to &ldquo;string
2132 gaps&rdquo;.<IndexTerm><Primary>-cpp vs string gaps</Primary></IndexTerm><IndexTerm><Primary>string gaps vs
2133 -cpp</Primary></IndexTerm>.  In other words, strings such as the following:
2134 </Para>
2135
2136 <Para>
2137
2138 <ProgramListing>
2139 strmod = "\
2140 \ p \
2141 \ "
2142 </ProgramListing>
2143
2144 </Para>
2145
2146 <Para>
2147 don't work with <Option>-cpp</Option>; <Filename>/usr/bin/cpp</Filename> elides the
2148 backslash-newline pairs.
2149 </Para>
2150
2151 <Para>
2152 However, it appears that if you add a space at the end of the line,
2153 then <Command>cpp</Command> (at least GNU <Command>cpp</Command> and possibly other <Command>cpp</Command>s)
2154 leaves the backslash-space pairs alone and the string gap works as
2155 expected.
2156 </Para>
2157
2158 </Sect2>
2159
2160 <Sect2 id="options-C-compiler">
2161 <Title>Options affecting the C compiler (if applicable)
2162 </Title>
2163
2164 <Para>
2165 <IndexTerm><Primary>include-file options</Primary></IndexTerm>
2166 <IndexTerm><Primary>C compiler options</Primary></IndexTerm>
2167 <IndexTerm><Primary>GCC options</Primary></IndexTerm>
2168 </Para>
2169
2170 <Para>
2171 At the moment, quite a few common C-compiler options are passed on
2172 quietly to the C compilation of Haskell-compiler-generated C files.
2173 THIS MAY CHANGE.  Meanwhile, options so sent are:
2174 </Para>
2175
2176 <Para>
2177
2178 <InformalTable>
2179 <TGroup Cols="2">
2180 <ColSpec Align="Left" Colsep="0">
2181 <ColSpec Align="Left" Colsep="0">
2182 <TBody>
2183 <Row>
2184 <Entry><Option>-ansi</Option> </Entry>
2185 <Entry> do ANSI C (not K&amp;R) </Entry>
2186 </Row>
2187 <Row>
2188 <Entry>
2189 <Option>-pedantic</Option> </Entry>
2190 <Entry> be so</Entry>
2191 </Row>
2192 <Row>
2193 <Entry>
2194 <Option>-dgcc-lint</Option> </Entry>
2195 <Entry> (hack) short for &ldquo;make GCC very paranoid&rdquo;</Entry>
2196 </Row>
2197
2198 </TBody>
2199
2200 </TGroup>
2201 </InformalTable>
2202
2203 <IndexTerm><Primary>-ansi option (for GCC)</Primary></IndexTerm>
2204 <IndexTerm><Primary>-pedantic option (for GCC)</Primary></IndexTerm>
2205 <IndexTerm><Primary>-dgcc-lint option (GCC paranoia)</Primary></IndexTerm>
2206 </Para>
2207
2208 <Para>
2209 If you are compiling with lots of foreign calls, you may need to
2210 tell the C&nbsp;compiler about some <Literal>&num;include</Literal> files.  There is no real
2211 pretty way to do this, but you can use this hack from the
2212 command-line:
2213 </Para>
2214
2215 <Para>
2216
2217 <Screen>
2218 % ghc -c '-#include &#60;X/Xlib.h&#62;' Xstuff.lhs
2219 </Screen>
2220
2221 </Para>
2222
2223 </Sect2>
2224
2225 <Sect2 id="options-linker">
2226 <Title>Linking and consistency-checking
2227 </Title>
2228
2229 <Para>
2230 <IndexTerm><Primary>linker options</Primary></IndexTerm>
2231 <IndexTerm><Primary>ld options</Primary></IndexTerm>
2232 </Para>
2233
2234 <Para>
2235 GHC has to link your code with various libraries, possibly including:
2236 user-supplied, GHC-supplied, and system-supplied (<Option>-lm</Option> math
2237 library, for example).
2238 </Para>
2239
2240 <Para>
2241 <VariableList>
2242
2243 <VarListEntry>
2244 <Term><Option>-l&lt;FOO&gt;</Option>:</Term>
2245 <ListItem>
2246 <Para>
2247 <IndexTerm><Primary>-l&lt;lib&gt; option</Primary></IndexTerm>
2248 Link in a library named <Filename>lib&lt;FOO&gt;.a</Filename> which resides somewhere on the
2249 library directories path.
2250 </Para>
2251
2252 <Para>
2253 Because of the sad state of most UNIX linkers, the order of such
2254 options does matter.  Thus: <Command>ghc -lbar *.o</Command> is almost certainly
2255 wrong, because it will search <Filename>libbar.a</Filename> <Emphasis>before</Emphasis> it has
2256 collected unresolved symbols from the <Filename>*.o</Filename> files.
2257 <Command>ghc *.o -lbar</Command> is probably better.
2258 </Para>
2259
2260 <Para>
2261 The linker will of course be informed about some GHC-supplied
2262 libraries automatically; these are:
2263 </Para>
2264
2265 <Para>
2266
2267 <InformalTable>
2268 <TGroup Cols="2">
2269 <ColSpec Align="Left" Colsep="0">
2270 <ColSpec Align="Left" Colsep="0">
2271 <TBody>
2272 <Row>
2273 <Entry><Emphasis>-l equivalent</Emphasis> </Entry>
2274 <Entry> <Emphasis>description</Emphasis> </Entry>
2275 </Row>
2276
2277 <Row>
2278 <Entry>
2279 <Option>-lHSrts,-lHSclib</Option> </Entry>
2280 <Entry> basic runtime libraries </Entry>
2281 </Row>
2282 <Row>
2283 <Entry>
2284 <Option>-lHS</Option> </Entry>
2285 <Entry> standard Prelude library </Entry>
2286 </Row>
2287 <Row>
2288 <Entry>
2289 <Option>-lHS&lowbar;cbits</Option> </Entry>
2290 <Entry> C support code for standard Prelude library </Entry>
2291 </Row>
2292 <Row>
2293 <Entry>
2294 <Option>-lgmp</Option> </Entry>
2295 <Entry> GNU multi-precision library (for Integers)</Entry>
2296 </Row>
2297
2298 </TBody>
2299
2300 </TGroup>
2301 </InformalTable>
2302
2303 </Para>
2304
2305 <Para>
2306 <IndexTerm><Primary>-lHS library</Primary></IndexTerm>
2307 <IndexTerm><Primary>-lHS&lowbar;cbits library</Primary></IndexTerm>
2308 <IndexTerm><Primary>-lHSrts library</Primary></IndexTerm>
2309 <IndexTerm><Primary>-lgmp library</Primary></IndexTerm>
2310 </Para>
2311 </ListItem>
2312 </VarListEntry>
2313 <VarListEntry>
2314 <Term><Option>-syslib &lt;name&gt;</Option>:</Term>
2315 <ListItem>
2316 <Para>
2317 <IndexTerm><Primary>-syslib &lt;name&gt; option</Primary></IndexTerm>
2318 </Para>
2319
2320 <Para>
2321 If you are using a Haskell &ldquo;system library&rdquo; (e.g., the POSIX
2322 library), just use the <Option>-syslib posix</Option> option, and the correct code
2323 should be linked in.
2324 </Para>
2325 </ListItem>
2326 </VarListEntry>
2327 <VarListEntry>
2328 <Term><Option>-L&lt;dir&gt;</Option>:</Term>
2329 <ListItem>
2330 <Para>
2331 <IndexTerm><Primary>-L&lt;dir&gt; option</Primary></IndexTerm>
2332 Where to find user-supplied libraries&hellip;  Prepend the directory
2333 <Filename>&lt;dir&gt;</Filename> to the library directories path.
2334 </Para>
2335 </ListItem>
2336 </VarListEntry>
2337 <VarListEntry>
2338 <Term><Option>-static</Option>:</Term>
2339 <ListItem>
2340 <Para>
2341 <IndexTerm><Primary>-static option</Primary></IndexTerm>
2342 Tell the linker to avoid shared libraries.
2343 </Para>
2344 </ListItem>
2345 </VarListEntry>
2346 <VarListEntry>
2347 <Term><Option>-no-link-chk</Option> and <Option>-link-chk</Option>:</Term>
2348 <ListItem>
2349 <Para>
2350 <IndexTerm><Primary>-no-link-chk option</Primary></IndexTerm>
2351 <IndexTerm><Primary>-link-chk option</Primary></IndexTerm>
2352 <IndexTerm><Primary>consistency checking of executables</Primary></IndexTerm>
2353 By default, immediately after linking an executable, GHC verifies that
2354 the pieces that went into it were compiled with compatible flags; a
2355 &ldquo;consistency check&rdquo;.
2356 (This is to avoid mysterious failures caused by non-meshing of
2357 incompatibly-compiled programs; e.g., if one <Filename>.o</Filename> file was compiled
2358 for a parallel machine and the others weren't.)  You may turn off this
2359 check with <Option>-no-link-chk</Option>.  You can turn it (back) on with
2360 <Option>-link-chk</Option> (the default).
2361 </Para>
2362 </ListItem>
2363 </VarListEntry>
2364 <VarListEntry>
2365 <Term><Option>-no-hs-main</Option>:</Term>
2366 <ListItem>
2367 <Para>
2368 <IndexTerm><Primary>-no-hs-main option</Primary></IndexTerm>
2369 <IndexTerm><Primary>linking Haskell libraries with foreign code</Primary></IndexTerm>
2370 </Para>
2371
2372 <Para>
2373 In the event you want to include ghc-compiled code as part of another
2374 (non-Haskell) program, the RTS will not be supplying its definition of
2375 <Function>main()</Function> at link-time, you will have to. To signal that to the
2376 driver script when linking, use <Option>-no-hs-main</Option>.
2377 </Para>
2378
2379 <Para>
2380 Notice that since the command-line passed to the linker is rather
2381 involved, you probably want to use the ghc driver script to do the
2382 final link of your `mixed-language' application. This is not a
2383 requirement though, just try linking once with <Option>-v</Option> on to see what
2384 options the driver passes through to the linker.
2385 </Para>
2386 </ListItem>
2387 </VarListEntry>
2388 </VariableList>
2389 </Para>
2390
2391 </Sect2>
2392
2393 </Sect1>
2394
2395 <Sect1 id="sec-using-concurrent">
2396 <Title>Using Concurrent Haskell</Title>
2397
2398 <Para>
2399 <IndexTerm><Primary>Concurrent Haskell&mdash;use</Primary></IndexTerm>
2400 </Para>
2401
2402 <Para>
2403 GHC (as of version 4.00) supports Concurrent Haskell by default,
2404 without requiring a special option or libraries compiled in a certain
2405 way.  To get access to the support libraries for Concurrent Haskell
2406 (i.e. <Literal>Concurrent</Literal> and friends), use the
2407 <Option>-syslib concurrent</Option> option.
2408 </Para>
2409
2410 <Para>
2411 Three RTS options are provided for modifying the behaviour of the
2412 threaded runtime system.  See the descriptions of
2413 <Option>-C[&lt;us&gt;]</Option>, <Option>-q</Option>, and
2414 <Option>-t&lt;num&gt;</Option> in <XRef LinkEnd="parallel-rts-opts">.
2415 </Para>
2416
2417 <Para>
2418 Concurrent Haskell is described in more detail in <XRef
2419 LinkEnd="sec-Concurrent">.
2420 </Para>
2421
2422 </Sect1>
2423
2424 <Sect1 id="sec-using-parallel">
2425 <Title>Using Parallel Haskell</Title>
2426
2427 <Para>
2428 <IndexTerm><Primary>Parallel Haskell&mdash;use</Primary></IndexTerm>
2429 </Para>
2430
2431 <Para>
2432 &lsqb;You won't be able to execute parallel Haskell programs unless PVM3
2433 (Parallel Virtual Machine, version 3) is installed at your site.]
2434 </Para>
2435
2436 <Para>
2437 To compile a Haskell program for parallel execution under PVM, use the
2438 <Option>-parallel</Option> option,<IndexTerm><Primary>-parallel
2439 option</Primary></IndexTerm> both when compiling <Emphasis>and
2440 linking</Emphasis>.  You will probably want to <Literal>import
2441 Parallel</Literal> into your Haskell modules.
2442 </Para>
2443
2444 <Para>
2445 To run your parallel program, once PVM is going, just invoke it
2446 &ldquo;as normal&rdquo;.  The main extra RTS option is
2447 <Option>-N&lt;n&gt;</Option>, to say how many PVM
2448 &ldquo;processors&rdquo; your program to run on.  (For more details of
2449 all relevant RTS options, please see <XRef
2450 LinkEnd="parallel-rts-opts">.)
2451 </Para>
2452
2453 <Para>
2454 In truth, running Parallel Haskell programs and getting information
2455 out of them (e.g., parallelism profiles) is a battle with the vagaries of
2456 PVM, detailed in the following sections.
2457 </Para>
2458
2459 <Sect2>
2460 <Title>Dummy's guide to using PVM</Title>
2461
2462 <Para>
2463 <IndexTerm><Primary>PVM, how to use</Primary></IndexTerm>
2464 <IndexTerm><Primary>Parallel Haskell&mdash;PVM use</Primary></IndexTerm>
2465 Before you can run a parallel program under PVM, you must set the
2466 required environment variables (PVM's idea, not ours); something like,
2467 probably in your <Filename>.cshrc</Filename> or equivalent:
2468
2469 <ProgramListing>
2470 setenv PVM_ROOT /wherever/you/put/it
2471 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
2472 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
2473 </ProgramListing>
2474
2475 </Para>
2476
2477 <Para>
2478 Creating and/or controlling your &ldquo;parallel machine&rdquo; is a purely-PVM
2479 business; nothing specific to Parallel Haskell.
2480 </Para>
2481
2482 <Para>
2483 You use the <Command>pvm</Command><IndexTerm><Primary>pvm command</Primary></IndexTerm> command to start PVM on your
2484 machine.  You can then do various things to control/monitor your
2485 &ldquo;parallel machine;&rdquo; the most useful being:
2486 </Para>
2487
2488 <Para>
2489 <InformalTable>
2490 <TGroup Cols=2>
2491 <ColSpec Align="Left">
2492 <TBody>
2493
2494 <Row>
2495 <Entry><KeyCombo><KeyCap>Control</KeyCap><KeyCap>D</KeyCap></KeyCombo></Entry>
2496 <Entry>exit <Command>pvm</Command>, leaving it running</Entry>
2497 </Row>
2498
2499 <Row>
2500 <Entry><Command>halt</Command></Entry>
2501 <Entry>kill off this &ldquo;parallel machine&rdquo; &amp; exit</Entry>
2502 </Row>
2503
2504 <Row>
2505 <Entry><Command>add &lt;host&gt;</Command></Entry>
2506 <Entry>add <Command>&lt;host&gt;</Command> as a processor</Entry>
2507 </Row>
2508
2509 <Row>
2510 <Entry><Command>delete &lt;host&gt;</Command></Entry>
2511 <Entry>delete <Command>&lt;host&gt;</Command></Entry>
2512 </Row>
2513
2514 <Row>
2515 <Entry><Command>reset</Command></Entry>
2516 <Entry>kill what's going, but leave PVM up</Entry>
2517 </Row>
2518
2519 <Row>
2520 <Entry><Command>conf</Command></Entry>
2521 <Entry>list the current configuration</Entry>
2522 </Row>
2523
2524 <Row>
2525 <Entry><Command>ps</Command></Entry>
2526 <Entry>report processes' status</Entry>
2527 </Row>
2528
2529 <Row>
2530 <Entry><Command>pstat &lt;pid&gt;</Command></Entry>
2531 <Entry>status of a particular process</Entry>
2532 </Row>
2533
2534 </TBody>
2535 </TGroup>
2536 </InformalTable>
2537 </Para>
2538
2539 <Para>
2540 The PVM documentation can tell you much, much more about <Command>pvm</Command>!
2541 </Para>
2542
2543 </Sect2>
2544
2545 <Sect2>
2546 <Title>Parallelism profiles</Title>
2547
2548 <Para>
2549 <IndexTerm><Primary>parallelism profiles</Primary></IndexTerm>
2550 <IndexTerm><Primary>profiles, parallelism</Primary></IndexTerm>
2551 <IndexTerm><Primary>visualisation tools</Primary></IndexTerm>
2552 </Para>
2553
2554 <Para>
2555 With Parallel Haskell programs, we usually don't care about the
2556 results&mdash;only with &ldquo;how parallel&rdquo; it was!  We want pretty pictures.
2557 </Para>
2558
2559 <Para>
2560 Parallelism profiles (&agrave; la <Command>hbcpp</Command>) can be generated with the
2561 <Option>-q</Option><IndexTerm><Primary>-q RTS option (concurrent, parallel)</Primary></IndexTerm> RTS option.  The
2562 per-processor profiling info is dumped into files named
2563 <Filename>&lt;full-path&gt;&lt;program&gt;.gr</Filename>.  These are then munged into a PostScript picture,
2564 which you can then display.  For example, to run your program
2565 <Filename>a.out</Filename> on 8 processors, then view the parallelism profile, do:
2566 </Para>
2567
2568 <Para>
2569
2570 <Screen>
2571 % ./a.out +RTS -N8 -q
2572 % grs2gr *.???.gr &#62; temp.gr     # combine the 8 .gr files into one
2573 % gr2ps -O temp.gr              # cvt to .ps; output in temp.ps
2574 % ghostview -seascape temp.ps   # look at it!
2575 </Screen>
2576
2577 </Para>
2578
2579 <Para>
2580 The scripts for processing the parallelism profiles are distributed
2581 in <Filename>ghc/utils/parallel/</Filename>.
2582 </Para>
2583
2584 </Sect2>
2585
2586 <Sect2>
2587 <Title>Other useful info about running parallel programs</Title>
2588
2589 <Para>
2590 The &ldquo;garbage-collection statistics&rdquo; RTS options can be useful for
2591 seeing what parallel programs are doing.  If you do either
2592 <Option>+RTS -Sstderr</Option><IndexTerm><Primary>-Sstderr RTS option</Primary></IndexTerm> or <Option>+RTS -sstderr</Option>, then
2593 you'll get mutator, garbage-collection, etc., times on standard
2594 error. The standard error of all PE's other than the `main thread'
2595 appears in <Filename>/tmp/pvml.nnn</Filename>, courtesy of PVM.
2596 </Para>
2597
2598 <Para>
2599 Whether doing <Option>+RTS -Sstderr</Option> or not, a handy way to watch
2600 what's happening overall is: <Command>tail -f /tmp/pvml.nnn</Command>.
2601 </Para>
2602
2603 </Sect2>
2604
2605 <Sect2 id="parallel-rts-opts">
2606 <Title>RTS options for Concurrent/Parallel Haskell
2607 </Title>
2608
2609 <Para>
2610 <IndexTerm><Primary>RTS options, concurrent</Primary></IndexTerm>
2611 <IndexTerm><Primary>RTS options, parallel</Primary></IndexTerm>
2612 <IndexTerm><Primary>Concurrent Haskell&mdash;RTS options</Primary></IndexTerm>
2613 <IndexTerm><Primary>Parallel Haskell&mdash;RTS options</Primary></IndexTerm>
2614 </Para>
2615
2616 <Para>
2617 Besides the usual runtime system (RTS) options
2618 (<XRef LinkEnd="runtime-control">), there are a few options particularly
2619 for concurrent/parallel execution.
2620 </Para>
2621
2622 <Para>
2623 <VariableList>
2624
2625 <VarListEntry>
2626 <Term><Option>-N&lt;N&gt;</Option>:</Term>
2627 <ListItem>
2628 <Para>
2629 <IndexTerm><Primary>-N&lt;N&gt; RTS option (parallel)</Primary></IndexTerm>
2630 (PARALLEL ONLY) Use <Literal>&lt;N&gt;</Literal> PVM processors to run this program;
2631 the default is 2.
2632 </Para>
2633 </ListItem>
2634 </VarListEntry>
2635 <VarListEntry>
2636 <Term><Option>-C[&lt;us&gt;]</Option>:</Term>
2637 <ListItem>
2638 <Para>
2639 <IndexTerm><Primary>-C&lt;us&gt; RTS option</Primary></IndexTerm>
2640 Sets the context switch interval to <Literal>&lt;us&gt;</Literal> microseconds.  A context
2641 switch will occur at the next heap allocation after the timer expires.
2642 With <Option>-C0</Option> or <Option>-C</Option>, context switches will occur as often as
2643 possible (at every heap allocation).  By default, context switches
2644 occur every 10 milliseconds.  Note that many interval timers are only
2645 capable of 10 millisecond granularity, so the default setting may be
2646 the finest granularity possible, short of a context switch at every
2647 heap allocation.
2648 </Para>
2649
2650 <Para>
2651 &lsqb;NOTE: this option currently has no effect (version 4.00).  Context
2652 switches happen when the current heap block is full, i.e. every 4k of
2653 allocation].
2654 </Para>
2655 </ListItem>
2656 </VarListEntry>
2657 <VarListEntry>
2658 <Term><Option>-q[v]</Option>:</Term>
2659 <ListItem>
2660 <Para>
2661 <IndexTerm><Primary>-q RTS option</Primary></IndexTerm>
2662 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
2663 in the file <FIlename>&lt;program&gt;.qp</FIlename>.  In the style of <Command>hbcpp</Command>, this profile
2664 records the movement of threads between the green (runnable) and red
2665 (blocked) queues.  If you specify the verbose suboption (<Option>-qv</Option>), the
2666 green queue is split into green (for the currently running thread
2667 only) and amber (for other runnable threads).  We do not recommend
2668 that you use the verbose suboption if you are planning to use the
2669 <Command>hbcpp</Command> profiling tools or if you are context switching at every heap
2670 check (with <Option>-C</Option>).
2671 </Para>
2672 </ListItem>
2673 </VarListEntry>
2674 <VarListEntry>
2675 <Term><Option>-t&lt;num&gt;</Option>:</Term>
2676 <ListItem>
2677 <Para>
2678 <IndexTerm><Primary>-t&lt;num&gt; RTS option</Primary></IndexTerm>
2679 (PARALLEL ONLY) Limit the number of concurrent threads per processor
2680 to <Literal>&lt;num&gt;</Literal>.  The default is 32.  Each thread requires slightly over 1K
2681 <Emphasis>words</Emphasis> in the heap for thread state and stack objects.  (For
2682 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
2683 8K bytes.)
2684 </Para>
2685 </ListItem>
2686 </VarListEntry>
2687 <VarListEntry>
2688 <Term><Option>-d</Option>:</Term>
2689 <ListItem>
2690 <Para>
2691 <IndexTerm><Primary>-d RTS option (parallel)</Primary></IndexTerm>
2692 (PARALLEL ONLY) Turn on debugging.  It pops up one xterm (or GDB, or
2693 something&hellip;) per PVM processor.  We use the standard <Command>debugger</Command>
2694 script that comes with PVM3, but we sometimes meddle with the
2695 <Command>debugger2</Command> script.  We include ours in the GHC distribution,
2696 in <Filename>ghc/utils/pvm/</Filename>.
2697 </Para>
2698 </ListItem>
2699 </VarListEntry>
2700 <VarListEntry>
2701 <Term><Option>-e&lt;num&gt;</Option>:</Term>
2702 <ListItem>
2703 <Para>
2704 <IndexTerm><Primary>-e&lt;num&gt; RTS option (parallel)</Primary></IndexTerm>
2705 (PARALLEL ONLY) Limit the number of pending sparks per processor to
2706 <Literal>&lt;num&gt;</Literal>. The default is 100. A larger number may be appropriate if
2707 your program generates large amounts of parallelism initially.
2708 </Para>
2709 </ListItem>
2710 </VarListEntry>
2711 <VarListEntry>
2712 <Term><Option>-Q&lt;num&gt;</Option>:</Term>
2713 <ListItem>
2714 <Para>
2715 <IndexTerm><Primary>-Q&lt;num&gt; RTS option (parallel)</Primary></IndexTerm>
2716 (PARALLEL ONLY) Set the size of packets transmitted between processors
2717 to <Literal>&lt;num&gt;</Literal>. The default is 1024 words. A larger number may be
2718 appropriate if your machine has a high communication cost relative to
2719 computation speed.
2720 </Para>
2721 </ListItem>
2722 </VarListEntry>
2723 </VariableList>
2724 </Para>
2725
2726 </Sect2>
2727
2728 </Sect1>
2729
2730 &runtime
2731 &debug
2732
2733 </Chapter>
2734
2735 <!-- Emacs stuff:
2736      ;;; Local Variables: ***
2737      ;;; mode: sgml ***
2738      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
2739      ;;; End: ***
2740  -->