[project @ 2000-07-08 23:08:02 by panne]
[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>-I&lt;dir&gt;</Option></Term>
831 <ListItem>
832 <Para>
833 <IndexTerm><Primary>-I&lt;dir&gt; option</Primary></IndexTerm>
834 Once a Haskell module has been compiled to C (<Filename>.hc</Filename> file), you may
835 wish to specify where GHC tells the C compiler to look for <Filename>.h</Filename> files.
836 (Or, if you are using the <Option>-cpp</Option> option<IndexTerm><Primary>-cpp option</Primary></IndexTerm>, where
837 it tells the C pre-processor to look&hellip;)  For this purpose, use a <Option>-I</Option>
838 option in the usual C-ish way.
839 </Para>
840 </ListItem>
841 </VarListEntry>
842
843 </VariableList>
844 </Para>
845
846 </Sect2>
847
848 <Sect2 id="hi-options">
849 <Title>Other options related to interface files
850 </Title>
851
852 <Para>
853 <IndexTerm><Primary>interface files, options</Primary></IndexTerm>
854 The interface output may be directed to another file
855 <Filename>bar2/Wurble.iface</Filename> with the option <Option>-ohi bar2/Wurble.iface</Option><IndexTerm><Primary>-ohi
856 &lt;file&gt; option</Primary></IndexTerm> (not recommended).
857 </Para>
858
859 <Para>
860 To avoid generating an interface file at all, use a <Option>-nohi</Option>
861 option.<IndexTerm><Primary>-nohi option</Primary></IndexTerm>
862 </Para>
863
864 <Para>
865 The compiler does not overwrite an existing <Filename>.hi</Filename> interface file if
866 the new one is byte-for-byte the same as the old one; this is friendly
867 to <Command>make</Command>.  When an interface does change, it is often enlightening to
868 be informed.  The <Option>-hi-diffs</Option><IndexTerm><Primary>-hi-diffs option</Primary></IndexTerm> option will
869 make GHC run <Command>diff</Command> on the old and new <Filename>.hi</Filename> files. You can also
870 record the difference in the interface file itself, the
871 <Option>-keep-hi-diffs</Option><IndexTerm><Primary>-keep-hi-diffs</Primary></IndexTerm> option takes care of that.
872 </Para>
873
874 <Para>
875 The <Filename>.hi</Filename> files from GHC contain &ldquo;usage&rdquo; information which changes
876 often and uninterestingly.  If you really want to see these changes
877 reported, you need to use the
878 <Option>-hi-diffs-with-usages</Option><IndexTerm><Primary>-hi-diffs-with-usages option</Primary></IndexTerm>
879 option.
880 </Para>
881
882 <Para>
883 Interface files are normally jammed full of compiler-produced
884 <Emphasis>pragmas</Emphasis>, which record arities, strictness info, etc.  If you
885 think these pragmas are messing you up (or you are doing some kind of
886 weird experiment), you can tell GHC to ignore them with the
887 <Option>-fignore-interface-pragmas</Option><IndexTerm><Primary>-fignore-interface-pragmas
888 option</Primary></IndexTerm> option.
889 </Para>
890
891 <Para>
892 When compiling without optimisations on, the compiler is extra-careful
893 about not slurping in data constructors and instance declarations that
894 it will not need. If you believe it is getting it wrong and not
895 importing stuff which you think it should, this optimisation can be
896 turned off with <Option>-fno-prune-tydecls</Option> and <Option>-fno-prune-instdecls</Option>.
897 <IndexTerm><Primary>-fno-prune-tydecls option</Primary></IndexTerm><IndexTerm><Primary>-fno-prune-instdecls
898 option</Primary></IndexTerm>
899 </Para>
900
901 <Para>
902 See also <XRef LinkEnd="options-linker">, which describes how the linker finds standard
903 Haskell libraries.
904 </Para>
905
906 </Sect2>
907
908 <Sect2 id="recomp">
909 <Title>The recompilation checker
910 </Title>
911
912 <IndexTerm><Primary>recompilation checker</Primary></IndexTerm>
913
914 <Para>
915 <variablelist>
916 <VarListEntry>
917 <Term><Option>-recomp</Option></Term>
918 <IndexTerm><Primary><option>-recomp</option> option</Primary></IndexTerm>
919 <ListItem>
920 <Para>
921 (On by default) Turn on recompilation checking.  This will stop
922 compilation early, leaving an existing <filename>.o</filename> file in
923 place, if it can be determined that the module does not need to be
924 recompiled.
925 </Para>
926 </ListItem>
927 </VarListEntry>
928 <VarListEntry>
929 <Term><Option>-no-recomp</Option></Term>
930 <IndexTerm><Primary><option>-recomp</option> option</Primary></IndexTerm>
931 <ListItem>
932 <Para>
933 Turn off recompilation checking.
934 </Para>
935 </ListItem>
936 </VarListEntry>
937 </VariableList>
938 </Para>
939
940 <Para>
941 In the olden days, GHC compared the newly-generated
942 <Filename>.hi</Filename> file with the previous version; if they were
943 identical, it left the old one alone and didn't change its
944 modification date.  In consequence, importers of a module with an
945 unchanged output <Filename>.hi</Filename> file were not recompiled.
946 </Para>
947
948 <Para>
949 This doesn't work any more.  In our earlier example, module
950 <Literal>C</Literal> does not import module <Literal>A</Literal>
951 directly, yet changes to <Filename>A.hi</Filename> should force a
952 recompilation of <Literal>C</Literal>.  And some changes to
953 <Literal>A</Literal> (changing the definition of a function that
954 appears in an inlining of a function exported by <Literal>B</Literal>,
955 say) may conceivably not change <Filename>B.hi</Filename> one jot.  So
956 now&hellip;
957 </Para>
958
959 <Para>
960 GHC keeps a version number on each interface file, and on each type
961 signature within the interface file.  It also keeps in every interface
962 file a list of the version numbers of everything it used when it last
963 compiled the file.  If the source file's modification date is earlier
964 than the <Filename>.o</Filename> file's date (i.e. the source hasn't
965 changed since the file was last compiled), and the
966 <option>-recomp</option> is given on the command line, GHC will be
967 clever.  It compares the version numbers on the things it needs this
968 time with the version numbers on the things it needed last time
969 (gleaned from the interface file of the module being compiled); if
970 they are all the same it stops compiling rather early in the process
971 saying &ldquo;Compilation IS NOT required&rdquo;.  What a beautiful
972 sight!
973 </Para>
974
975 <Para>
976 Patrick Sansom had a workshop paper about how all this is done (though
977 the details have changed quite a bit). <ULink URL="mailto:sansom@dcs.gla.ac.uk">Ask him</ULink> if you want a copy.
978 </Para>
979
980 </Sect2>
981
982
983 <Sect2 id="using-make">
984 <Title>Using <Command>make</Command>
985 </Title>
986
987 <Para>
988 <IndexTerm><Primary><literal>make</literal></Primary></IndexTerm>
989 </Para>
990
991 <Para>
992 It is reasonably straightforward to set up a <Filename>Makefile</Filename> to use with GHC, assuming you name your source files the same as your modules.
993 Thus:
994 </Para>
995
996 <Para>
997
998 <ProgramListing>
999 HC      = ghc
1000 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
1001
1002 SRCS = Main.lhs Foo.lhs Bar.lhs
1003 OBJS = Main.o   Foo.o   Bar.o
1004
1005 .SUFFIXES : .o .hs .hi .lhs .hc .s
1006
1007 cool_pgm : $(OBJS)
1008         rm $@
1009         $(HC) -o $@ $(HC_OPTS) $(OBJS)
1010
1011 # Standard suffix rules
1012 .o.hi:
1013         @:
1014
1015 .lhs.o:
1016         $(HC) -c $&#60; $(HC_OPTS)
1017
1018 .hs.o:
1019         $(HC) -c $&#60; $(HC_OPTS)
1020
1021 # Inter-module dependencies
1022 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
1023 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
1024 </ProgramListing>
1025
1026 </Para>
1027
1028 <Para>
1029 (Sophisticated <Command>make</Command> variants may achieve some of the above more
1030 elegantly.  Notably, <Command>gmake</Command>'s pattern rules let you write the more
1031 comprehensible:
1032 </Para>
1033
1034 <Para>
1035
1036 <ProgramListing>
1037 %.o : %.lhs
1038         $(HC) -c $&#60; $(HC_OPTS)
1039 </ProgramListing>
1040
1041 </Para>
1042
1043 <Para>
1044 What we've shown should work with any <Command>make</Command>.)
1045 </Para>
1046
1047 <Para>
1048 Note the cheesy <Literal>.o.hi</Literal> rule: It records the dependency of the
1049 interface (<Filename>.hi</Filename>) file on the source.  The rule says a <Filename>.hi</Filename> file can
1050 be made from a <Filename>.o</Filename> file by doing&hellip;nothing.  Which is true.
1051 </Para>
1052
1053 <Para>
1054 Note the inter-module dependencies at the end of the Makefile, which
1055 take the form
1056 </Para>
1057
1058 <Para>
1059
1060 <ProgramListing>
1061 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
1062 </ProgramListing>
1063
1064 </Para>
1065
1066 <Para>
1067 They tell <Command>make</Command> that if any of <Literal>Foo.o</Literal>, <Literal>Foo.hc</Literal> or <Literal>Foo.s</Literal> have an
1068 earlier modification date than <Literal>Baz.hi</Literal>, then the out-of-date file
1069 must be brought up to date.  To bring it up to date, <Literal>make</Literal> looks for
1070 a rule to do so; one of the preceding suffix rules does the job
1071 nicely.
1072 </Para>
1073
1074     </sect2>
1075
1076     <sect2 id="sec-makefile-dependencies">
1077       <title>Dependency generation</title>
1078       <indexterm><primary>dependencies in Makefiles</primary></indexterm>
1079       <indexterm><primary>Makefile dependencies</primary></indexterm>
1080
1081       <para>Putting inter-dependencies of the form <Literal>Foo.o :
1082       Bar.hi</Literal> into your <Filename>Makefile</Filename> by hand
1083       is rather error-prone.  Don't worry, GHC has support for
1084       automatically generating the required dependencies.  Add the
1085       following to your <Filename>Makefile</Filename>:</para>
1086
1087 <ProgramListing>
1088 depend :
1089         ghc -M $(HC_OPTS) $(SRCS)
1090 </ProgramListing>
1091
1092       <para>Now, before you start compiling, and any time you change
1093       the <Literal>imports</Literal> in your program, do <Command>make
1094       depend</Command> before you do <Command>make
1095       cool&lowbar;pgm</Command>.  <Command>ghc -M</Command> will append
1096       the needed dependencies to your
1097       <Filename>Makefile</Filename>.</Para>
1098
1099       <para>In general, if module <Literal>A</Literal> contains the
1100       line
1101
1102 <programlisting>
1103 import B ...blah...
1104 </programlisting>
1105
1106        then <command>ghc -M</command> will generate a dependency
1107        line of the form:
1108
1109 <programlisting>
1110 A.o : B.hi
1111 </programlisting>
1112
1113        If module <literal>A</literal> contains the line 
1114
1115 <programlisting>
1116 import {-# SOURCE #-} B ...blah...
1117 </programlisting>
1118
1119        then <command>ghc -M</command> will generate a dependency
1120        line of the form:
1121
1122 <programlisting>
1123 A.o : B.hi-boot
1124 </programlisting>
1125
1126        (See <xref linkend="hi-files"> for details of interface files.)
1127        If <literal>A</literal> imports multiple modules, then there
1128        will be multiple lines with <filename>A.o</filename> as the
1129        target.</para>
1130
1131       <para>By default, <Command>ghc -M</Command> generates all the
1132       dependencies, and then concatenates them onto the end of
1133       <Filename>makefile</Filename> (or <Filename>Makefile</Filename>
1134       if <Filename>makefile</Filename> doesn't exist) bracketed by the
1135       lines "<Literal>&num; DO NOT DELETE: Beginning of Haskell
1136       dependencies</Literal>" and "<Literal>&num; DO NOT DELETE: End
1137       of Haskell dependencies</Literal>".  If these lines already
1138       exist in the <Filename>makefile</Filename>, then the old
1139       dependencies are deleted first.</para>
1140
1141       <para>Internally, GHC uses a script to generate the
1142       dependencies, called <command>mkdependHS</command>.  This script
1143       has some options of its own, which you might find useful.
1144       Options can be passed directly to <command>mkdependHS</command>
1145       with GHC's <literal>-optdep</literal> option.  For example, to
1146       generate the dependencies into a file called
1147       <literal>.depend</literal> instead of
1148       <literal>Makefile</literal>:</para>
1149
1150 <screen>
1151 ghc -M -optdep-f optdep.depend ...
1152 </screen>
1153       
1154       <para>The full list of options accepted by
1155       <command>mkdependHS</command> is:</para>
1156
1157       <variablelist>
1158
1159         <varlistentry>
1160           <term><option>-w</option></term>
1161           <listitem>
1162             <para>Turn off warnings about interface file shadowing.</para>
1163           </listitem>
1164         </varlistentry>
1165
1166         <varlistentry>
1167           <term><option>-f blah</option></term>
1168           <listitem>
1169             <para>Use <Filename>blah</Filename> as the makefile,
1170             rather than <Filename>makefile</Filename> or
1171             <Filename>Makefile</Filename>.  If
1172             <Filename>blah</Filename> doesn't exist,
1173             <Command>mkdependHS</Command> creates it.  We often use
1174             <Option>-f .depend</Option> to put the dependencies in
1175             <Filename>.depend</Filename> and then
1176             <Command>include</Command> the file
1177             <Filename>.depend</Filename> into
1178             <Filename>Makefile</Filename>.</para>
1179           </listitem>
1180         </varlistentry>
1181
1182         <varlistentry>
1183           <term><option>-o &lt;osuf&gt;</option></term>
1184           <listitem>
1185             <para>Use <Filename>.&lt;osuf&gt;</Filename> as the
1186             "target file" suffix ( default: <Literal>o</Literal>).
1187             Multiple <Option>-o</Option> flags are permitted (GHC2.05
1188             onwards).  Thus "<Option>-o hc -o o</Option>" will
1189             generate dependencies for <Filename>.hc</Filename> and
1190             <Filename>.o</Filename> files.</para>
1191           </listitem>
1192         </varlistentry>
1193
1194         <varlistentry>
1195           <term><option>-s &lt;suf&gt;</option></term>
1196           <listitem>
1197             <para>Make extra dependencies that declare that files with
1198             suffix
1199             <Filename>.&lt;suf&gt;&lowbar;&lt;osuf&gt;</Filename>
1200             depend on interface files with suffix
1201             <Filename>.&lt;suf&gt;&lowbar;hi</Filename>, or (for
1202             <Literal>&lcub;-&num; SOURCE &num;-&rcub;</Literal>
1203             imports) on <Filename>.hi-boot</Filename>.  Multiple
1204             <Option>-s</Option> flags are permitted.  For example,
1205             <Option>-o hc -s a -s b</Option> will make dependencies
1206             for <Filename>.hc</Filename> on <Filename>.hi</Filename>,
1207             <Filename>.a&lowbar;hc</Filename> on
1208             <Filename>.a&lowbar;hi</Filename>, and
1209             <Filename>.b&lowbar;hc</Filename> on
1210             <Filename>.b&lowbar;hi</Filename>.  (Useful in conjunction
1211             with NoFib "ways".)</para>
1212           </listitem>
1213         </varlistentry>
1214
1215         <varlistentry>
1216           <term><option>--exclude-module=&lt;file&gt;</option></term>
1217           <listitem>
1218             <para>Regard <Filename>&lt;file&gt;</Filename> as
1219             "stable"; i.e., exclude it from having dependencies on
1220             it.</para>
1221           </listitem>
1222         </varlistentry>
1223
1224         <varlistentry>
1225           <term><option>-x</option></term>
1226           <listitem>
1227             <para>same as <option>--exclude-module</option></para>
1228           </listitem>
1229         </varlistentry>
1230
1231         <varlistentry>
1232           <term><option>--exclude-directory=&lt;dirs&gt;</option></term>
1233           <listitem>
1234             <para>Regard the colon-separated list of directories
1235             <Filename>&lt;dirs&gt;</Filename> as containing stable,
1236             don't generate any dependencies on modules therein.</para>
1237           </listitem>
1238         </varlistentry>
1239
1240         <varlistentry>
1241           <term><option>-xdirs</option></term>
1242           <listitem>
1243             <para>same as <Option>--exclude-directory</Option>.</para>
1244           </listitem>
1245         </varlistentry>
1246
1247         <varlistentry>
1248           <term><option>--include-module=&lt;file&gt;</option></term>
1249           <listitem>
1250             <para>Regard <Filename>&lt;file&gt;</Filename> as not
1251             "stable"; i.e., generate dependencies on it (if any). This
1252             option is normally used in conjunction with the
1253             <Option>--exclude-directory</Option> option.</para>
1254           </listitem>
1255         </varlistentry>
1256
1257         <varlistentry>
1258           <term><option>--include-prelude</option></term>
1259           <listitem>
1260             <para>Regard prelude libraries as unstable, i.e., generate
1261             dependencies on the prelude modules used (including
1262             <Literal>Prelude</Literal>).  This option is normally only
1263             used by the various system libraries. If a
1264             <Option>-package</Option> option is used, dependencies will
1265             also be generated on the library's interfaces.</para>
1266           </listitem>
1267         </varlistentry>
1268       </variablelist>
1269
1270 </Sect2>
1271
1272 <Sect2 id="mutual-recursion">
1273 <Title>How to compile mutually recursive modules
1274 </Title>
1275
1276 <Para>
1277 <IndexTerm><Primary>module system, recursion</Primary></IndexTerm>
1278 <IndexTerm><Primary>recursion, between modules</Primary></IndexTerm>
1279 </Para>
1280
1281 <Para>
1282 Currently, the compiler does not have proper support for dealing with
1283 mutually recursive modules:
1284 </Para>
1285
1286 <Para>
1287
1288 <ProgramListing>
1289 module A where
1290
1291 import B
1292
1293 newtype TA = MkTA Int
1294
1295 f :: TB -&#62; TA
1296 f (MkTB x) = MkTA x
1297 --------
1298 module B where
1299
1300 import A
1301
1302 data TB = MkTB !Int
1303
1304 g :: TA -&#62; TB
1305 g (MkTA x) = MkTB x
1306 </ProgramListing>
1307
1308 </Para>
1309
1310 <Para>
1311 When compiling either module A and B, the compiler will try (in vain)
1312 to look for the interface file of the other. So, to get mutually
1313 recursive modules off the ground, you need to hand write an interface
1314 file for A or B, so as to break the loop.  These hand-written
1315 interface files are called <Literal>hi-boot</Literal> files, and are placed in a file
1316 called <Filename>&lt;module&gt;.hi-boot</Filename>.  To import from an <Literal>hi-boot</Literal> file instead
1317 of the standard <Filename>.hi</Filename> file, use the following syntax in the importing module:
1318 <IndexTerm><Primary>hi-boot files</Primary></IndexTerm>
1319 <IndexTerm><Primary>importing, hi-boot files</Primary></IndexTerm>
1320 </Para>
1321
1322 <Para>
1323
1324 <ProgramListing>
1325 import {-# SOURCE #-} A
1326 </ProgramListing>
1327
1328 </Para>
1329
1330 <Para>
1331 The hand-written interface need only contain the bare minimum of
1332 information needed to get the bootstrapping process started.  For
1333 example, it doesn't need to contain declarations for <Emphasis>everything</Emphasis>
1334 that module <Literal>A</Literal> exports, only the things required by the module that
1335 imports <Literal>A</Literal> recursively.
1336 </Para>
1337
1338 <Para>
1339 For the example at hand, the boot interface file for A would look like
1340 the following:
1341 </Para>
1342
1343 <Para>
1344
1345 <ProgramListing>
1346 __interface A 1 404 where
1347 __export A TA{MkTA} ;
1348 1 newtype TA = MkTA PrelBase.Int ;
1349 </ProgramListing>
1350
1351 </Para>
1352
1353 <Para>
1354 The syntax is essentially the same as a normal <Filename>.hi</Filename> file
1355 (unfortunately), but you can usually tailor an existing <Filename>.hi</Filename> file to
1356 make a <Filename>.hi-boot</Filename> file.
1357 </Para>
1358
1359 <Para>
1360 Notice that we only put the declaration for the newtype <Literal>TA</Literal> in the
1361 <Literal>hi-boot</Literal> file, not the signature for <Function>f</Function>, since <Function>f</Function> isn't used by
1362 <Literal>B</Literal>.
1363 </Para>
1364
1365 <Para>
1366 The number &ldquo;1&rdquo; after &ldquo;&lowbar;&lowbar;interface A&rdquo; gives the version number of module A;
1367 it is incremented whenever anything in A's interface file changes.  The &ldquo;404&rdquo; is
1368 the version number of the interface file <Emphasis>syntax</Emphasis>; we change it when
1369 we change the syntax of interface files so that you get a better error message when
1370 you try to read an old-format file with a new-format compiler.
1371 </Para>
1372
1373 <Para>
1374 The number &ldquo;1&rdquo; at the beginning of a declaration is the <Emphasis>version
1375 number</Emphasis> of that declaration: for the purposes of <Filename>.hi-boot</Filename> files
1376 these can all be set to 1.  All names must be fully qualified with the
1377 <Emphasis>original</Emphasis> module that an object comes from: for example, the
1378 reference to <Literal>Int</Literal> in the interface for <Literal>A</Literal> comes from <Literal>PrelBase</Literal>,
1379 which is a module internal to GHC's prelude.  It's a pain, but that's
1380 the way it is.
1381 </Para>
1382
1383 <Para>
1384 If you want an hi-boot file to export a data type, but you don't want to give its constructors
1385 (because the constructors aren't used by the SOURCE-importing module), you can write simply:
1386 </Para>
1387
1388 <Para>
1389
1390 <ProgramListing>
1391 __interface A 1 404 where
1392 __export A TA;
1393 1 data TA
1394 </ProgramListing>
1395
1396 </Para>
1397
1398 <Para>
1399 (You must write all the type parameters, but leave out the '=' and everything that follows it.)
1400 </Para>
1401
1402 <Para>
1403 <Emphasis>Note:</Emphasis> This is all a temporary solution, a version of the
1404 compiler that handles mutually recursive modules properly without the manual
1405 construction of interface files, is (allegedly) in the works.
1406 </Para>
1407
1408 </Sect2>
1409
1410 </Sect1>
1411
1412   <sect1 id="packages">
1413     <title>Packages</title>
1414     <indexterm><primary>packages</primary></indexterm>
1415
1416     <para>Packages are collections of libraries, conveniently grouped
1417     together as a single entity.  The package system is flexible: a
1418     package may consist of Haskell code, foreign language code (eg. C
1419     libraries), or a mixture of the two.  A package is a good way to
1420     group together related Haskell modules, and is essential if you
1421     intend to make the modules into a Windows DLL (see below).</para>
1422
1423     <para>Because packages can contain both Haskell and C libraries, they
1424     are also a good way to provide convenient access to a Haskell
1425     layer over a C library.</para>
1426
1427     <para>GHC comes with several packages (see <xref
1428     linkend="book-hslibs">), and packages can be added/removed from an
1429     existing GHC installation.</para>
1430
1431     <sect2 id="listing-packages">
1432       <title>Listing the available packages</title>
1433       <indexterm><primary>packages</primary>
1434         <secondary>listing</secondary></indexterm>
1435
1436       <para>To see what packages are currently installed, use the
1437       <literal>--list-packages</literal> option:</para>
1438       <indexterm><primary><literal>--list-packages</literal></primary>
1439       </indexterm>
1440
1441 <screen>
1442   $ ghc --list-packages
1443   gmp, rts, std, lang, concurrent, data, net, posix, text, util
1444 </screen>
1445
1446       <para>Note that your GHC installation might have a slightly
1447       different set of packages installed.</para>
1448
1449       <para>The <literal>gmp</literal> and <literal>rts</literal>
1450       packages are always present, and represent the multi-precision
1451       integer and runtime system libraries respectively.  The
1452       <literal>std</literal> package contains the Haskell prelude.
1453       The rest of the packages are optional libraries.</para>
1454
1455     </sect2>
1456
1457     <sect2 id="using-packages">
1458       <title>Using a package</title>
1459       <indexterm><primary>packages</primary>
1460         <secondary>using</secondary></indexterm>
1461       
1462       <para>To use a package, add the <literal>-package</literal> flag
1463       to the command line:</para>
1464
1465       <variablelist>
1466         <varlistentry>
1467           <term><option>-package &lt;lib&gt;</option></term>
1468           <indexterm><primary>-package &lt;lib&gt; option</primary></indexterm>
1469           <listitem>
1470             <para>This option brings into scope all the modules from
1471             package <literal>&lt;lib&gt;</literal> (they still have to
1472             be imported in your Haskell source, however).  It also
1473             causes the relevant libraries to be linked when linking is
1474             being done.</para>
1475           </listitem>
1476         </varlistentry>
1477       </variablelist>
1478
1479       <para>Some packages depend on other packages, for example the
1480       <literal>text</literal> package makes use of some of the modules
1481       in the <literal>lang</literal> package.  The package system
1482       takes care of all these dependencies, so that when you say
1483       <literal>-package text</literal> on the command line, you
1484       automatically get <literal>-package lang</literal> too.</para>
1485     </sect2>
1486
1487     <sect2 id="building-packages">
1488       <title>Building a package from Haskell source</title>
1489       <indexterm><primary>packages</primary>
1490         <secondary>building</secondary></indexterm>
1491
1492       <para>It takes some special considerations to build a new
1493       package:</para>
1494
1495       <itemizedlist>
1496         <listitem>
1497           <para>A package may contain several Haskell modules. A
1498           package may span many directories, or many packages may
1499           exist in a single directory. Packages may not be mutually
1500           recursive.</para>
1501         </listitem>
1502
1503         <listitem>
1504           <para>A package has a name
1505           (e.g. <filename>std</filename>)</para>
1506         </listitem>
1507
1508         <listitem>
1509           <para>The Haskell code in a package may be built into one or
1510           more Unix libraries (e.g. <Filename>libHSfoo.a</Filename>),
1511           or a single DLL on Windows
1512           (e.g. <Filename>HSfoo.dll</Filename>).  The restriction to a
1513           single DLL on Windows is that the package system is used to
1514           tell the compiler when it should make an inter-DLL call
1515           rather than an intra-DLL call (inter-DLL calls require an
1516           extra indirection).</para>
1517         </listitem>
1518
1519         <listitem>
1520           <para>GHC does not maintain detailed cross-package
1521           dependency information.  It does remember which modules in
1522           other packages the current module depends on, but not which
1523           things within those imported things.</para>
1524         </listitem>
1525       </itemizedlist>
1526
1527       <para>To compile a module which is to be part of a new package,
1528       use the <literal>-package-name</literal> option:</para>
1529
1530       <variablelist>
1531         <varlistentry>
1532           <term><option>-package-name &lt;foo&gt;</option></term>
1533           <indexterm><primary><literal>-package-name</literal></primary>
1534             <secondary>option</secondary></indexterm>
1535           <listitem>
1536             <para>This option is added to the command line when
1537             compiling a module that is destined to be part of package
1538             <literal>foo</literal>.  If this flag is omitted then the
1539             default package <literal>Main</literal> is assumed.</para>
1540           </listitem>
1541         </varlistentry>
1542       </variablelist>
1543
1544       <para>Failure to use the <literal>-package-name</literal> option
1545       when compiling a package will result in disaster on Windows, but
1546       is relatively harmless on Unix at the moment (it will just cause
1547       a few extra dependencies in some interface files).  However,
1548       bear in mind that we might add support for Unix shared libraries
1549       at some point in the future.</para>
1550
1551       <para>It is worth noting that on Windows, because each package
1552       is built as a DLL, and a reference to a DLL costs an extra
1553       indirection, intra-package references are cheaper than
1554       inter-package references. Of course, this applies to the
1555       <Filename>Main</Filename> package as well.</para>
1556
1557     </sect2>
1558     <sect2 id="package-management">
1559       <title>Package management</title>
1560       <indexterm><primary>packages</primary>
1561         <secondary>management</secondary></indexterm>
1562       
1563       <para>GHC uses a package configuration file, called
1564       <literal>packages.conf</literal>, which can be found in your GHC
1565       install directory.  This file isn't intended to be edited
1566       directly, instead GHC provides options for adding & removing
1567       packages:</para>
1568
1569       <variablelist>
1570         <varlistentry>
1571           <term><option>--add-package</option></term>
1572           <indexterm><primary><literal>--add-package</literal></primary>
1573               <secondary>option</secondary></indexterm>
1574           <listitem>
1575             <para>Reads a package specification (see below) on stdin,
1576             and adds it to the database of installed packages.  The
1577             package specification must be a package that isn't already
1578             installed.</para>
1579           </listitem>
1580         </varlistentry>
1581
1582         <varlistentry>
1583           <term><option>--delete-package &lt;foo&gt;</option></term>
1584           <indexterm><primary><literal>--delete-package</literal></primary>
1585               <secondary>option</secondary></indexterm>
1586           <listitem>
1587             <para>Removes the specified package from the installed
1588             configuration.</para>
1589           </listitem>
1590         </varlistentry>
1591       </variablelist>
1592
1593       <para>In both cases, the old package configuration file is saved
1594       in <literal>packages.conf.old</literal> in your GHC install
1595       directory, so in an emergency you can always copy this file into
1596       <literal>package.conf</literal> to restore the old
1597       settings.</para>
1598
1599       <para>A package specification looks like this:</para>
1600
1601 <screen>
1602  ("mypkg",
1603   "4.08",
1604   Package
1605        {
1606         import_dirs    =  ["/usr/local/lib/imports/mypkg"],
1607         library_dirs   =  ["/usr/local/lib"],
1608         libraries      =  ["HSmypkg", "HSmypkg_cbits"],
1609         include_dirs   =  [],
1610         c_includes     =  ["HsMyPkg.h"],
1611         package_deps   =  ["text", "data"],
1612         extra_ghc_opts =  [],
1613         extra_cc_opts  =  [],
1614         extra_ld_opts  =  ["-lmy_clib"]
1615        }
1616  )
1617 </screen>
1618
1619       <para>The first line is the name of the package, for use with
1620       the <literal>-package</literal> flag and as listed in the
1621       <literal>--list-packages</literal> list.  The second line is the
1622       version of GHC that was used to compile any Haskell code in this
1623       package (GHC will refuse to add the package if its version
1624       number differs from this one).  The rest of the components of
1625       the package specification may be specified in any order, and
1626       are:</para>
1627
1628       <variablelist>
1629         <varlistentry>
1630           <term><literal>import_dirs</literal></term>
1631           <indexterm><primary><literal>import_dirs</literal></primary>
1632             <secondary>package specification</secondary></indexterm>
1633           <listitem>
1634             <para>A list of directories containing interface files
1635             (<literal>.hi</literal> files) for this package.</para>
1636           </listitem>
1637         </varlistentry>
1638
1639         <varlistentry>
1640           <term><literal>library_dirs</literal></term>
1641           <indexterm><primary><literal>library_dirs</literal></primary>
1642             <secondary>package specification</secondary></indexterm>
1643           <listitem>
1644             <para>A list of directories containing libraries for this
1645             package.</para>
1646           </listitem>
1647         </varlistentry>
1648
1649         <varlistentry>
1650           <term><literal>libraries</literal></term>
1651           <indexterm><primary><literal>libraries</literal></primary>
1652             <secondary>package specification</secondary></indexterm>
1653           <listitem>
1654             <para>A list of libraries for this package, with the
1655             <literal>.a</literal> or <literal>.dll</literal> suffix
1656             omitted.  On Unix, the <literal>lib</literal> prefix is
1657             also omitted.</para>
1658           </listitem>
1659         </varlistentry>
1660
1661         <varlistentry>
1662           <term><literal>include_dirs</literal></term>
1663           <indexterm><primary><literal>include_dirs</literal></primary>
1664             <secondary>package specification</secondary></indexterm>
1665           <listitem>
1666             <para>A list of directories containing C includes for this
1667             package (maybe the empty list).</para>
1668           </listitem>
1669         </varlistentry>
1670
1671         <varlistentry>
1672           <term><literal>c_includes</literal></term>
1673           <indexterm><primary><literal>c_includes</literal></primary>
1674             <secondary>package specification</secondary></indexterm>
1675           <listitem>
1676             <para>A list of files to include for via-C compilations
1677             using this package.  Typically this include file will
1678             contain function prototypes for any C functions used in
1679             the package, in case they end up being called as a result
1680             of Haskell functions from the package being
1681             inlined.</para>
1682           </listitem>
1683         </varlistentry>
1684
1685         <varlistentry>
1686           <term><literal>package_deps</literal></term>
1687           <indexterm><primary><literal>package_deps</literal></primary>
1688             <secondary>package specification</secondary></indexterm>
1689           <listitem>
1690             <para>A list of packages which this package depends
1691             on.</para>
1692           </listitem>
1693         </varlistentry>
1694
1695         <varlistentry>
1696           <term><literal>extra_ghc_opts</literal></term>
1697           <indexterm><primary><literal>extra_ghc_opts</literal></primary>
1698             <secondary>package specification</secondary></indexterm>
1699           <listitem>
1700             <para>Extra arguments to be added to the GHC command line
1701             when this package is being used.</para>
1702           </listitem>
1703         </varlistentry>
1704
1705         <varlistentry>
1706           <term><literal>extra_cc_opts</literal></term>
1707           <indexterm><primary><literal>extra_cc_opts</literal></primary>
1708             <secondary>package specification</secondary></indexterm>
1709           <listitem>
1710             <para>Extra arguments to be added to the gcc command line
1711             when this package is being used (only for via-C
1712             compilations).</para>
1713           </listitem>
1714         </varlistentry>
1715
1716         <varlistentry>
1717           <term><literal>extra_ld_opts</literal></term>
1718           <indexterm><primary><literal>extra_ld_opts</literal></primary>
1719             <secondary>package specification</secondary></indexterm>
1720           <listitem>
1721             <para>Extra arguments to be added to the gcc command line
1722             (for linking) when this package is being used.</para>
1723           </listitem>
1724         </varlistentry>
1725       </variablelist>
1726
1727       <para>For examples of more package specifications, take a look
1728       at the <literal>package.conf</literal> in your GHC
1729       installation.</para>
1730     </sect2>
1731   </sect1>
1732
1733
1734 <Sect1 id="options-optimise">
1735 <Title>Optimisation (code improvement)
1736 </Title>
1737
1738 <Para>
1739 <IndexTerm><Primary>optimisation (GHC)</Primary></IndexTerm>
1740 <IndexTerm><Primary>improvement, code (GHC)</Primary></IndexTerm>
1741 </Para>
1742
1743 <Para>
1744 The <Option>-O*</Option> options specify convenient &ldquo;packages&rdquo; of optimisation
1745 flags; the <Option>-f*</Option> options described later on specify
1746 <Emphasis>individual</Emphasis> optimisations to be turned on/off; the <Option>-m*</Option>
1747 options specify <Emphasis>machine-specific</Emphasis> optimisations to be turned
1748 on/off.
1749 </Para>
1750
1751 <Sect2 id="optimise-pkgs">
1752 <Title><Option>-O*</Option>: convenient &ldquo;packages&rdquo; of optimisation flags.
1753 </Title>
1754
1755 <Para>
1756 <IndexTerm><Primary>-O options</Primary></IndexTerm>
1757 </Para>
1758
1759 <Para>
1760 There are <Emphasis>many</Emphasis> options that affect the quality of code
1761 produced by GHC.  Most people only have a general goal, something like
1762 &ldquo;Compile quickly&rdquo; or &ldquo;Make my program run like greased lightning.&rdquo;
1763 The following &ldquo;packages&rdquo; of optimisations (or lack thereof) should
1764 suffice.
1765 </Para>
1766
1767 <Para>
1768 Once you choose a <Option>-O*</Option> &ldquo;package,&rdquo; stick with it&mdash;don't chop and
1769 change.  Modules' interfaces <Emphasis>will</Emphasis> change with a shift to a new
1770 <Option>-O*</Option> option, and you may have to recompile a large chunk of all
1771 importing modules before your program can again be run
1772 safely (see <XRef LinkEnd="recomp">).
1773 </Para>
1774
1775 <Para>
1776 <VariableList>
1777
1778 <VarListEntry>
1779 <Term>No <Option>-O*</Option>-type option specified:</Term>
1780 <IndexTerm><Primary>-O* not specified</Primary></IndexTerm>
1781 <ListItem>
1782 <Para>
1783 This is taken to mean: &ldquo;Please compile quickly; I'm not over-bothered
1784 about compiled-code quality.&rdquo;  So, for example: <Command>ghc -c Foo.hs</Command>
1785 </Para>
1786 </ListItem>
1787 </VarListEntry>
1788 <VarListEntry>
1789 <Term><Option>-O</Option> or <Option>-O1</Option>:</Term>
1790 <IndexTerm><Primary>-O option</Primary></IndexTerm>
1791 <IndexTerm><Primary>-O1 option</Primary></IndexTerm>
1792 <IndexTerm><Primary>optimise</Primary><secondary>normally</secondary></IndexTerm>
1793 <ListItem>
1794 <Para>
1795 Means: &ldquo;Generate good-quality code without taking too long about
1796 it.&rdquo; Thus, for example: <Command>ghc -c -O Main.lhs</Command>
1797 </Para>
1798 </ListItem>
1799 </VarListEntry>
1800 <VarListEntry>
1801 <Term><Option>-O2</Option>:</Term>
1802 <IndexTerm><Primary>-O2 option</Primary></IndexTerm>
1803 <IndexTerm><Primary>optimise</Primary><secondary>aggressively</secondary></IndexTerm>
1804 <ListItem>
1805 <Para>
1806 Means: &ldquo;Apply every non-dangerous optimisation, even if it means
1807 significantly longer compile times.&rdquo;
1808 </Para>
1809
1810 <Para>
1811 The avoided &ldquo;dangerous&rdquo; optimisations are those that can make
1812 runtime or space <Emphasis>worse</Emphasis> if you're unlucky.  They are
1813 normally turned on or off individually.
1814 </Para>
1815
1816 <Para>
1817 At the moment, <Option>-O2</Option> is <Emphasis>unlikely</Emphasis> to produce
1818 better code than <Option>-O</Option>.
1819 </Para>
1820 </ListItem>
1821 </VarListEntry>
1822 <VarListEntry>
1823 <Term><Option>-O2-for-C</Option>:</Term>
1824 <IndexTerm><Primary>-O2-for-C option</Primary></IndexTerm>
1825 <IndexTerm><Primary>gcc, invoking with -O2</Primary></IndexTerm>
1826 <ListItem>
1827 <Para>
1828 Says to run GCC with <Option>-O2</Option>, which may be worth a few percent in
1829 execution speed.  Don't forget <Option>-fvia-C</Option>, lest you use the native-code
1830 generator and bypass GCC altogether!
1831 </Para>
1832 </ListItem>
1833 </VarListEntry>
1834 <VarListEntry>
1835 <Term><Option>-Onot</Option>:</Term>
1836 <IndexTerm><Primary>-Onot option</Primary></IndexTerm>
1837 <IndexTerm><Primary>optimising, reset</Primary></IndexTerm>
1838 <ListItem>
1839 <Para>
1840 This option will make GHC &ldquo;forget&rdquo; any
1841 <Option>-O</Option>ish options it has seen so far.  Sometimes useful;
1842 for example: <Command>make all
1843 EXTRA&lowbar;HC&lowbar;OPTS=-Onot</Command>.
1844 </Para>
1845 </ListItem>
1846 </VarListEntry>
1847 <VarListEntry>
1848 <Term><Option>-Ofile &lt;file&gt;</Option>:</Term>
1849 <IndexTerm><Primary>-Ofile &lt;file&gt; option</Primary></IndexTerm>
1850 <IndexTerm><Primary>optimising, customised</Primary></IndexTerm>
1851 <ListItem>
1852 <Para>
1853 For those who need <Emphasis>absolute</Emphasis> control over
1854 <Emphasis>exactly</Emphasis> what options are used (e.g., compiler
1855 writers, sometimes :-), a list of options can be put in a file and
1856 then slurped in with <Option>-Ofile</Option>.
1857 </Para>
1858
1859 <Para>
1860 In that file, comments are of the
1861 <Literal>&num;</Literal>-to-end-of-line variety; blank lines and most
1862 whitespace is ignored.
1863 </Para>
1864
1865 <Para>
1866 Please ask if you are baffled and would like an example of <Option>-Ofile</Option>!
1867 </Para>
1868 </ListItem>
1869 </VarListEntry>
1870 </VariableList>
1871 </Para>
1872
1873 <Para>
1874 At Glasgow, we don't use a <Option>-O*</Option> flag for day-to-day work.  We use
1875 <Option>-O</Option> to get respectable speed; e.g., when we want to measure
1876 something.  When we want to go for broke, we tend to use <Option>-O -fvia-C
1877 -O2-for-C</Option> (and we go for lots of coffee breaks).
1878 </Para>
1879
1880 <Para>
1881 The easiest way to see what <Option>-O</Option> (etc.) &ldquo;really mean&rdquo; is to run with
1882 <Option>-v</Option>, then stand back in amazement.  Alternatively, just look at the
1883 <Literal>HsC&lowbar;minus&lt;blah&gt;</Literal> lists in the GHC driver script.
1884 </Para>
1885
1886 </Sect2>
1887
1888 <Sect2>
1889 <Title><Option>-f*</Option>: platform-independent flags</Title>
1890
1891 <Para>
1892 <IndexTerm><Primary>-f* options (GHC)</Primary></IndexTerm>
1893 <IndexTerm><Primary>-fno-* options (GHC)</Primary></IndexTerm>
1894 </Para>
1895
1896 <Para>
1897 Flags can be turned <Emphasis>off</Emphasis> individually.  (NB: I hope you have a
1898 good reason for doing this&hellip;) To turn off the <Option>-ffoo</Option> flag, just use
1899 the <Option>-fno-foo</Option> flag.<IndexTerm><Primary>-fno-&lt;opt&gt; anti-option</Primary></IndexTerm> So, for
1900 example, you can say <Option>-O2 -fno-strictness</Option>, which will then drop out
1901 any running of the strictness analyser.
1902 </Para>
1903
1904 <Para>
1905 The options you are most likely to want to turn off are:
1906
1907 <ItemizedList>
1908 <ListItem>
1909
1910 <Para>
1911 <Option>-fno-strictness</Option><IndexTerm><Primary>-fno-strictness option</Primary></IndexTerm> (strictness
1912 analyser, because it is sometimes slow),
1913 </Para>
1914 </ListItem>
1915 <ListItem>
1916
1917 <Para>
1918 <Option>-fno-specialise</Option><IndexTerm><Primary>-fno-specialise option</Primary></IndexTerm> (automatic
1919 specialisation of overloaded functions, because it can make your code
1920 bigger) (US spelling also accepted), and
1921 </Para>
1922 </ListItem>
1923 <ListItem>
1924
1925 <Para>
1926 <Option>-fno-cpr-analyse</Option><IndexTerm><Primary>-fno-cpr-analyse option</Primary></IndexTerm> switches off the CPR (constructed product
1927 result) analyser.
1928 </Para>
1929 </ListItem>
1930
1931 </ItemizedList>
1932
1933 </Para>
1934
1935 <Para>
1936 Should you wish to turn individual flags <Emphasis>on</Emphasis>, you are advised
1937 to use the <Option>-Ofile</Option> option, described above.  Because the order in
1938 which optimisation passes are run is sometimes crucial, it's quite
1939 hard to do with command-line options.
1940 </Para>
1941
1942 <Para>
1943 Here are some &ldquo;dangerous&rdquo; optimisations you <Emphasis>might</Emphasis> want to try:
1944 <VariableList>
1945
1946 <VarListEntry>
1947 <Term><Option>-fvia-C</Option>:</Term>
1948 <ListItem>
1949 <Para>
1950 <IndexTerm><Primary>-fvia-C option</Primary></IndexTerm>
1951 <IndexTerm><Primary>native code generator, turning off</Primary></IndexTerm>
1952 </Para>
1953
1954 <Para>
1955 Compile via C, and don't use the native-code generator.  (There are many
1956 cases when GHC does this on its own.) You might pick up a little bit of
1957 speed by compiling via C (e.g. for floating-point intensive code on Intel).
1958 If you use <Function>&lowbar;casm&lowbar;</Function>s (which are utterly
1959 deprecated), you probably <Emphasis>have</Emphasis> to use
1960 <Option>-fvia-C</Option>.
1961 </Para>
1962
1963 <Para>
1964 The lower-case incantation, <Option>-fvia-c</Option>, is synonymous.
1965 </Para>
1966
1967 <Para>
1968 Compiling via C will probably be slower (in compilation time) than
1969 using GHC's native code generator.
1970 </Para>
1971 </ListItem>
1972 </VarListEntry>
1973 <VarListEntry>
1974 <Term><Option>-funfolding-interface-threshold&lt;n&gt;</Option>:</Term>
1975 <ListItem>
1976 <Para>
1977 <IndexTerm><Primary>-funfolding-interface-threshold option</Primary></IndexTerm>
1978 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
1979 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
1980 (Default: 30) By raising or lowering this number, you can raise or
1981 lower the amount of pragmatic junk that gets spewed into interface
1982 files.  (An unfolding has a &ldquo;size&rdquo; that reflects the cost in terms
1983 of &ldquo;code bloat&rdquo; of expanding that unfolding in another module.  A
1984 bigger function would be assigned a bigger cost.)
1985 </Para>
1986 </ListItem>
1987 </VarListEntry>
1988 <VarListEntry>
1989 <Term><Option>-funfolding-creation-threshold&lt;n&gt;</Option>:</Term>
1990 <ListItem>
1991 <Para>
1992 <IndexTerm><Primary>-funfolding-creation-threshold option</Primary></IndexTerm>
1993 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
1994 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
1995 (Default: 30) This option is similar to
1996 <Option>-funfolding-interface-threshold</Option>, except that it governs unfoldings
1997 within a single module.  Increasing this figure is more likely to
1998 result in longer compile times than faster code.  The next option is
1999 more useful:
2000 </Para>
2001 </ListItem>
2002 </VarListEntry>
2003 <VarListEntry>
2004 <Term><Option>-funfolding-use-threshold&lt;n&gt;</Option>:</Term>
2005 <ListItem>
2006 <Para>
2007 <IndexTerm><Primary>-funfolding-use-threshold option</Primary></IndexTerm>
2008 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
2009 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
2010 (Default: 8) This is the magic cut-off figure for unfolding: below
2011 this size, a function definition will be unfolded at the call-site,
2012 any bigger and it won't.  The size computed for a function depends on
2013 two things: the actual size of the expression minus any discounts that
2014 apply (see <Option>-funfolding-con-discount</Option>).
2015 </Para>
2016 </ListItem>
2017 </VarListEntry>
2018 <VarListEntry>
2019 <Term><Option>-funfolding-con-discount&lt;n&gt;</Option>:</Term>
2020 <ListItem>
2021 <Para>
2022 <IndexTerm><Primary>-funfolding-con-discount option</Primary></IndexTerm>
2023 <IndexTerm><Primary>inlining, controlling</Primary></IndexTerm>
2024 <IndexTerm><Primary>unfolding, controlling</Primary></IndexTerm>
2025 (Default: 2) If the compiler decides that it can eliminate some
2026 computation by performing an unfolding, then this is a discount factor
2027 that it applies to the funciton size before deciding whether to unfold
2028 it or not.
2029 </Para>
2030
2031 <Para>
2032 OK, folks, these magic numbers `30', `8', and '2' are mildly
2033 arbitrary; they are of the &ldquo;seem to be OK&rdquo; variety.  The `8' is the
2034 more critical one; it's what determines how eager GHC is about
2035 expanding unfoldings.
2036 </Para>
2037 </ListItem>
2038 </VarListEntry>
2039 <VarListEntry>
2040 <Term><Option>-funbox-strict-fields</Option>:</Term>
2041 <ListItem>
2042 <Para>
2043 <IndexTerm><Primary>-funbox-strict-fields option</Primary></IndexTerm>
2044 <IndexTerm><Primary>strict constructor fields</Primary></IndexTerm>
2045 <IndexTerm><Primary>constructor fields, strict</Primary></IndexTerm>
2046 </Para>
2047
2048 <Para>
2049 This option causes all constructor fields which are marked strict
2050 (i.e. &ldquo;!&rdquo;) to be unboxed or unpacked if possible.  For example:
2051 </Para>
2052
2053 <Para>
2054
2055 <ProgramListing>
2056 data T = T !Float !Float
2057 </ProgramListing>
2058
2059 </Para>
2060
2061 <Para>
2062 will create a constructor <Literal>T</Literal> containing two unboxed floats if the
2063 <Option>-funbox-strict-fields</Option> flag is given.  This may not always be an
2064 optimisation: if the <Function>T</Function> constructor is scrutinised and the floats
2065 passed to a non-strict function for example, they will have to be
2066 reboxed (this is done automatically by the compiler).
2067 </Para>
2068
2069 <Para>
2070 This option should only be used in conjunction with <Option>-O</Option>, in order to
2071 expose unfoldings to the compiler so the reboxing can be removed as
2072 often as possible.  For example:
2073 </Para>
2074
2075 <Para>
2076
2077 <ProgramListing>
2078 f :: T -&#62; Float
2079 f (T f1 f2) = f1 + f2
2080 </ProgramListing>
2081
2082 </Para>
2083
2084 <Para>
2085 The compiler will avoid reboxing <Function>f1</Function> and <Function>f2</Function> by inlining <Function>+</Function> on
2086 floats, but only when <Option>-O</Option> is on.
2087 </Para>
2088
2089 <Para>
2090 Any single-constructor data is eligible for unpacking; for example
2091 </Para>
2092
2093 <Para>
2094
2095 <ProgramListing>
2096 data T = T !(Int,Int)
2097 </ProgramListing>
2098
2099 </Para>
2100
2101 <Para>
2102 will store the two <Literal>Int</Literal>s directly in the <Function>T</Function> constructor, by flattening
2103 the pair.  Multi-level unpacking is also supported:
2104 </Para>
2105
2106 <Para>
2107
2108 <ProgramListing>
2109 data T = T !S
2110 data S = S !Int !Int
2111 </ProgramListing>
2112
2113 </Para>
2114
2115 <Para>
2116 will store two unboxed <Literal>Int&num;</Literal>s directly in the <Function>T</Function> constructor.
2117 </Para>
2118 </ListItem>
2119 </VarListEntry>
2120 <VarListEntry>
2121 <Term><Option>-fsemi-tagging</Option>:</Term>
2122 <ListItem>
2123 <Para>
2124 This option (which <Emphasis>does not work</Emphasis> with the native-code generator)
2125 tells the compiler to add extra code to test for already-evaluated
2126 values.  You win if you have lots of such values during a run of your
2127 program, you lose otherwise.  (And you pay in extra code space.)
2128 </Para>
2129
2130 <Para>
2131 We have not played with <Option>-fsemi-tagging</Option> enough to recommend it.
2132 (For all we know, it doesn't even work anymore&hellip; Sigh.)
2133 </Para>
2134 </ListItem>
2135 </VarListEntry>
2136 </VariableList>
2137 </Para>
2138
2139 </Sect2>
2140
2141 <Sect2>
2142 <Title><Option>-m*</Option>: platform-specific flags</Title>
2143
2144 <Para>
2145 <IndexTerm><Primary>-m* options (GHC)</Primary></IndexTerm>
2146 <IndexTerm><Primary>platform-specific options</Primary></IndexTerm>
2147 <IndexTerm><Primary>machine-specific options</Primary></IndexTerm>
2148 </Para>
2149
2150 <Para>
2151 Some flags only make sense for particular target platforms.
2152 </Para>
2153
2154 <Para>
2155 <VariableList>
2156
2157 <VarListEntry>
2158 <Term><Option>-mv8</Option>:</Term>
2159 <ListItem>
2160 <Para>
2161 (SPARC machines)<IndexTerm><Primary>-mv8 option (SPARC only)</Primary></IndexTerm>
2162 Means to pass the like-named option to GCC; it says to use the
2163 Version 8 SPARC instructions, notably integer multiply and divide.
2164 The similiar <Option>-m*</Option> GCC options for SPARC also work, actually.
2165 </Para>
2166 </ListItem>
2167 </VarListEntry>
2168 <VarListEntry>
2169 <Term><Option>-mlong-calls</Option>:</Term>
2170 <ListItem>
2171 <Para>
2172 (HPPA machines)<IndexTerm><Primary>-mlong-calls option (HPPA only)</Primary></IndexTerm>
2173 Means to pass the like-named option to GCC.  Required for Very Big
2174 modules, maybe.  (Probably means you're in trouble&hellip;)
2175 </Para>
2176 </ListItem>
2177 </VarListEntry>
2178 <VarListEntry>
2179 <Term><Option>-monly-[32]-regs</Option>:</Term>
2180 <ListItem>
2181 <Para>
2182 (iX86 machines)<IndexTerm><Primary>-monly-N-regs option (iX86 only)</Primary></IndexTerm>
2183 GHC tries to &ldquo;steal&rdquo; four registers from GCC, for performance
2184 reasons; it almost always works.  However, when GCC is compiling some
2185 modules with four stolen registers, it will crash, probably saying:
2186
2187 <Screen>
2188 Foo.hc:533: fixed or forbidden register was spilled.
2189 This may be due to a compiler bug or to impossible asm
2190 statements or clauses.
2191 </Screen>
2192
2193 Just give some registers back with <Option>-monly-N-regs</Option>.  Try `3' first,
2194 then `2'.  If `2' doesn't work, please report the bug to us.
2195 </Para>
2196 </ListItem>
2197 </VarListEntry>
2198 </VariableList>
2199 </Para>
2200
2201 </Sect2>
2202
2203 <Sect2 id="optimise-C-compiler">
2204 <Title>Code improvement by the C compiler.
2205 </Title>
2206
2207 <Para>
2208 <IndexTerm><Primary>optimisation by GCC</Primary></IndexTerm>
2209 <IndexTerm><Primary>GCC optimisation</Primary></IndexTerm>
2210 </Para>
2211
2212 <Para>
2213 The C&nbsp;compiler (GCC) is run with <Option>-O</Option> turned on.  (It has
2214 to be, actually).
2215 </Para>
2216
2217 <Para>
2218 If you want to run GCC with <Option>-O2</Option>&mdash;which may be worth a few
2219 percent in execution speed&mdash;you can give a
2220 <Option>-O2-for-C</Option><IndexTerm><Primary>-O2-for-C option</Primary></IndexTerm> option.
2221 </Para>
2222
2223 </Sect2>
2224
2225 </Sect1>
2226
2227 <Sect1 id="options-phases">
2228 <Title>Options related to a particular phase
2229 </Title>
2230
2231 <Sect2 id="c-pre-processor">
2232 <Title>The C pre-processor
2233 </Title>
2234
2235 <Para>
2236 <IndexTerm><Primary>pre-processing: cpp</Primary></IndexTerm>
2237 <IndexTerm><Primary>C pre-processor options</Primary></IndexTerm>
2238 <IndexTerm><Primary>cpp, pre-processing with</Primary></IndexTerm>
2239 </Para>
2240
2241 <Para>
2242 The C pre-processor <Command>cpp</Command> is run over your Haskell code only if the
2243 <Option>-cpp</Option> option <IndexTerm><Primary>-cpp option</Primary></IndexTerm> is given.  Unless you are
2244 building a large system with significant doses of conditional
2245 compilation, you really shouldn't need it.
2246 <VariableList>
2247
2248 <VarListEntry>
2249 <Term><Option>-D&lt;foo&gt;</Option>:</Term>
2250 <ListItem>
2251 <Para>
2252 <IndexTerm><Primary>-D&lt;name&gt; option</Primary></IndexTerm>
2253 Define macro <Constant>&lt;foo&gt;</Constant> in the usual way.  NB: does <Emphasis>not</Emphasis> affect
2254 <Option>-D</Option> macros passed to the C&nbsp;compiler when compiling via C!  For those,
2255 use the <Option>-optc-Dfoo</Option> hack&hellip; (see <XRef LinkEnd="forcing-options-through">).
2256 </Para>
2257 </ListItem>
2258 </VarListEntry>
2259 <VarListEntry>
2260 <Term><Option>-U&lt;foo&gt;</Option>:</Term>
2261 <ListItem>
2262 <Para>
2263 <IndexTerm><Primary>-U&lt;name&gt; option</Primary></IndexTerm>
2264 Undefine macro <Command>&lt;foo&gt;</Command> in the usual way.
2265 </Para>
2266 </ListItem>
2267 </VarListEntry>
2268 <VarListEntry>
2269 <Term><Option>-I&lt;dir&gt;</Option>:</Term>
2270 <ListItem>
2271 <Para>
2272 <IndexTerm><Primary>-I&lt;dir&gt; option</Primary></IndexTerm>
2273 Specify a directory in which to look for <Literal>&num;include</Literal> files, in
2274 the usual C way.
2275 </Para>
2276 </ListItem>
2277 </VarListEntry>
2278 </VariableList>
2279 </Para>
2280
2281 <Para>
2282 The GHC driver pre-defines several macros when processing Haskell
2283 source code (<Filename>.hs</Filename> or <Filename>.lhs</Filename> files):
2284 </Para>
2285
2286 <Para>
2287 <VariableList>
2288
2289 <VarListEntry>
2290 <Term><Constant>&lowbar;&lowbar;HASKELL98&lowbar;&lowbar;</Constant>:</Term>
2291 <ListItem>
2292 <Para>
2293 <IndexTerm><Primary>&lowbar;&lowbar;HASKELL98&lowbar;&lowbar;</Primary></IndexTerm>
2294 If defined, this means that GHC supports the language defined by the
2295 Haskell 98 report.
2296 </Para>
2297 </ListItem>
2298 </VarListEntry>
2299 <VarListEntry>
2300 <Term><Constant>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;=98</Constant>:</Term>
2301 <ListItem>
2302 <Para>
2303 <IndexTerm><Primary>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;</Primary></IndexTerm>
2304 In GHC 4.04 and later, the <Constant>&lowbar;&lowbar;HASKELL&lowbar;&lowbar;</Constant> macro is defined as having
2305 the value <Constant>98</Constant>.
2306 </Para>
2307 </ListItem>
2308 </VarListEntry>
2309 <VarListEntry>
2310 <Term><Constant>&lowbar;&lowbar;HASKELL1&lowbar;&lowbar;</Constant>:</Term>
2311 <ListItem>
2312 <Para>
2313 <IndexTerm><Primary>&lowbar;&lowbar;HASKELL1&lowbar;&lowbar; macro</Primary></IndexTerm>
2314 If defined to <Emphasis>n</Emphasis>, that means GHC supports the Haskell language
2315 defined in the Haskell report version <Emphasis>1.n</Emphasis>.  Currently 5.  This
2316 macro is deprecated, and will probably disappear in future versions.
2317 </Para>
2318 </ListItem>
2319 </VarListEntry>
2320 <VarListEntry>
2321 <Term><Constant>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</Term>
2322 <ListItem>
2323 <Para>
2324 <IndexTerm><Primary>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar; macro</Primary></IndexTerm>
2325 For version <Emphasis>n</Emphasis> of the GHC system, this will be <Literal>&num;define</Literal>d to
2326 <Emphasis>100n</Emphasis>.  So, for version 4.00, it is 400.
2327 </Para>
2328
2329 <Para>
2330 With any luck, <Constant>&lowbar;&lowbar;GLASGOW&lowbar;HASKELL&lowbar;&lowbar;</Constant> will be undefined in all other
2331 implementations that support C-style pre-processing.
2332 </Para>
2333
2334 <Para>
2335 (For reference: the comparable symbols for other systems are:
2336 <Constant>&lowbar;&lowbar;HUGS&lowbar;&lowbar;</Constant> for Hugs and <Constant>&lowbar;&lowbar;HBC&lowbar;&lowbar;</Constant> for Chalmers.)
2337 </Para>
2338
2339 <Para>
2340 NB. This macro is set when pre-processing both Haskell source and C
2341 source, including the C source generated from a Haskell module
2342 (i.e. <Filename>.hs</Filename>, <Filename>.lhs</Filename>, <Filename>.c</Filename> and <Filename>.hc</Filename> files).
2343 </Para>
2344 </ListItem>
2345 </VarListEntry>
2346 <VarListEntry>
2347 <Term><Constant>&lowbar;&lowbar;CONCURRENT&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</Term>
2348 <ListItem>
2349 <Para>
2350 <IndexTerm><Primary>&lowbar;&lowbar;CONCURRENT&lowbar;HASKELL&lowbar;&lowbar; macro</Primary></IndexTerm>
2351 This symbol is defined when pre-processing Haskell (input) and
2352 pre-processing C (GHC output).  Since GHC from verion 4.00 now
2353 supports concurrent haskell by default, this symbol is always defined.
2354 </Para>
2355 </ListItem>
2356 </VarListEntry>
2357 <VarListEntry>
2358 <Term><Constant>&lowbar;&lowbar;PARALLEL&lowbar;HASKELL&lowbar;&lowbar;</Constant>:</Term>
2359 <ListItem>
2360 <Para>
2361 <IndexTerm><Primary>&lowbar;&lowbar;PARALLEL&lowbar;HASKELL&lowbar;&lowbar; macro</Primary></IndexTerm>
2362 Only defined when <Option>-parallel</Option> is in use!  This symbol is defined when
2363 pre-processing Haskell (input) and pre-processing C (GHC output).
2364 </Para>
2365 </ListItem>
2366 </VarListEntry>
2367 </VariableList>
2368 </Para>
2369
2370 <Para>
2371 Options other than the above can be forced through to the C
2372 pre-processor with the <Option>-opt</Option> flags (see
2373 <XRef LinkEnd="forcing-options-through">).
2374 </Para>
2375
2376 <Para>
2377 A small word of warning: <Option>-cpp</Option> is not friendly to &ldquo;string
2378 gaps&rdquo;.<IndexTerm><Primary>-cpp vs string gaps</Primary></IndexTerm><IndexTerm><Primary>string gaps vs
2379 -cpp</Primary></IndexTerm>.  In other words, strings such as the following:
2380 </Para>
2381
2382 <Para>
2383
2384 <ProgramListing>
2385 strmod = "\
2386 \ p \
2387 \ "
2388 </ProgramListing>
2389
2390 </Para>
2391
2392 <Para>
2393 don't work with <Option>-cpp</Option>; <Filename>/usr/bin/cpp</Filename> elides the
2394 backslash-newline pairs.
2395 </Para>
2396
2397 <Para>
2398 However, it appears that if you add a space at the end of the line,
2399 then <Command>cpp</Command> (at least GNU <Command>cpp</Command> and possibly other <Command>cpp</Command>s)
2400 leaves the backslash-space pairs alone and the string gap works as
2401 expected.
2402 </Para>
2403
2404 </Sect2>
2405
2406 <Sect2 id="options-C-compiler">
2407 <Title>Options affecting the C compiler (if applicable)
2408 </Title>
2409
2410 <Para>
2411 <IndexTerm><Primary>include-file options</Primary></IndexTerm>
2412 <IndexTerm><Primary>C compiler options</Primary></IndexTerm>
2413 <IndexTerm><Primary>GCC options</Primary></IndexTerm>
2414 </Para>
2415
2416 <Para>
2417 At the moment, quite a few common C-compiler options are passed on
2418 quietly to the C compilation of Haskell-compiler-generated C files.
2419 THIS MAY CHANGE.  Meanwhile, options so sent are:
2420 </Para>
2421
2422 <Para>
2423
2424 <InformalTable>
2425 <TGroup Cols="2">
2426 <ColSpec Align="Left" Colsep="0">
2427 <ColSpec Align="Left" Colsep="0">
2428 <TBody>
2429 <Row>
2430 <Entry><Option>-ansi</Option> </Entry>
2431 <Entry> do ANSI C (not K&amp;R) </Entry>
2432 </Row>
2433 <Row>
2434 <Entry>
2435 <Option>-pedantic</Option> </Entry>
2436 <Entry> be so</Entry>
2437 </Row>
2438 <Row>
2439 <Entry>
2440 <Option>-dgcc-lint</Option> </Entry>
2441 <Entry> (hack) short for &ldquo;make GCC very paranoid&rdquo;</Entry>
2442 </Row>
2443
2444 </TBody>
2445
2446 </TGroup>
2447 </InformalTable>
2448
2449 <IndexTerm><Primary>-ansi option (for GCC)</Primary></IndexTerm>
2450 <IndexTerm><Primary>-pedantic option (for GCC)</Primary></IndexTerm>
2451 <IndexTerm><Primary>-dgcc-lint option (GCC paranoia)</Primary></IndexTerm>
2452 </Para>
2453
2454 <Para>
2455 If you are compiling with lots of foreign calls, you may need to
2456 tell the C&nbsp;compiler about some <Literal>&num;include</Literal> files.  There is no real
2457 pretty way to do this, but you can use this hack from the
2458 command-line:
2459 </Para>
2460
2461 <Para>
2462
2463 <Screen>
2464 % ghc -c '-#include &#60;X/Xlib.h&#62;' Xstuff.lhs
2465 </Screen>
2466
2467 </Para>
2468
2469 </Sect2>
2470
2471 <Sect2 id="options-linker">
2472 <Title>Linking and consistency-checking
2473 </Title>
2474
2475 <Para>
2476 <IndexTerm><Primary>linker options</Primary></IndexTerm>
2477 <IndexTerm><Primary>ld options</Primary></IndexTerm>
2478 </Para>
2479
2480 <Para>
2481 GHC has to link your code with various libraries, possibly including:
2482 user-supplied, GHC-supplied, and system-supplied (<Option>-lm</Option> math
2483 library, for example).
2484 </Para>
2485
2486 <Para>
2487 <VariableList>
2488
2489 <VarListEntry>
2490 <Term><Option>-l&lt;FOO&gt;</Option>:</Term>
2491 <ListItem>
2492 <Para>
2493 <IndexTerm><Primary>-l&lt;lib&gt; option</Primary></IndexTerm>
2494 Link in a library named <Filename>lib&lt;FOO&gt;.a</Filename> which resides somewhere on the
2495 library directories path.
2496 </Para>
2497
2498 <Para>
2499 Because of the sad state of most UNIX linkers, the order of such
2500 options does matter.  Thus: <Command>ghc -lbar *.o</Command> is almost certainly
2501 wrong, because it will search <Filename>libbar.a</Filename> <Emphasis>before</Emphasis> it has
2502 collected unresolved symbols from the <Filename>*.o</Filename> files.
2503 <Command>ghc *.o -lbar</Command> is probably better.
2504 </Para>
2505
2506 <Para>
2507 The linker will of course be informed about some GHC-supplied
2508 libraries automatically; these are:
2509 </Para>
2510
2511 <Para>
2512
2513 <InformalTable>
2514 <TGroup Cols="2">
2515 <ColSpec Align="Left" Colsep="0">
2516 <ColSpec Align="Left" Colsep="0">
2517 <TBody>
2518 <Row>
2519 <Entry><Emphasis>-l equivalent</Emphasis> </Entry>
2520 <Entry> <Emphasis>description</Emphasis> </Entry>
2521 </Row>
2522
2523 <Row>
2524 <Entry>
2525 <Option>-lHSrts,-lHSclib</Option> </Entry>
2526 <Entry> basic runtime libraries </Entry>
2527 </Row>
2528 <Row>
2529 <Entry>
2530 <Option>-lHS</Option> </Entry>
2531 <Entry> standard Prelude library </Entry>
2532 </Row>
2533 <Row>
2534 <Entry>
2535 <Option>-lHS&lowbar;cbits</Option> </Entry>
2536 <Entry> C support code for standard Prelude library </Entry>
2537 </Row>
2538 <Row>
2539 <Entry>
2540 <Option>-lgmp</Option> </Entry>
2541 <Entry> GNU multi-precision library (for Integers)</Entry>
2542 </Row>
2543
2544 </TBody>
2545
2546 </TGroup>
2547 </InformalTable>
2548
2549 </Para>
2550
2551 <Para>
2552 <IndexTerm><Primary>-lHS library</Primary></IndexTerm>
2553 <IndexTerm><Primary>-lHS&lowbar;cbits library</Primary></IndexTerm>
2554 <IndexTerm><Primary>-lHSrts library</Primary></IndexTerm>
2555 <IndexTerm><Primary>-lgmp library</Primary></IndexTerm>
2556 </Para>
2557 </ListItem>
2558 </VarListEntry>
2559 <VarListEntry>
2560 <Term><Option>-package &lt;name&gt;</Option>:</Term>
2561 <ListItem>
2562 <Para>
2563 <IndexTerm><Primary>-package &lt;name&gt; option</Primary></IndexTerm>
2564 </Para>
2565
2566 <Para>
2567 If you are using a Haskell &ldquo;system library&rdquo; (e.g., the POSIX
2568 library), just use the <Option>-package posix</Option> option, and the correct code
2569 should be linked in.
2570 </Para>
2571 </ListItem>
2572 </VarListEntry>
2573 <VarListEntry>
2574 <Term><Option>-L&lt;dir&gt;</Option>:</Term>
2575 <ListItem>
2576 <Para>
2577 <IndexTerm><Primary>-L&lt;dir&gt; option</Primary></IndexTerm>
2578 Where to find user-supplied libraries&hellip;  Prepend the directory
2579 <Filename>&lt;dir&gt;</Filename> to the library directories path.
2580 </Para>
2581 </ListItem>
2582 </VarListEntry>
2583 <VarListEntry>
2584 <Term><Option>-static</Option>:</Term>
2585 <ListItem>
2586 <Para>
2587 <IndexTerm><Primary>-static option</Primary></IndexTerm>
2588 Tell the linker to avoid shared libraries.
2589 </Para>
2590 </ListItem>
2591 </VarListEntry>
2592 <VarListEntry>
2593 <Term><Option>-no-link-chk</Option> and <Option>-link-chk</Option>:</Term>
2594 <ListItem>
2595 <Para>
2596 <IndexTerm><Primary>-no-link-chk option</Primary></IndexTerm>
2597 <IndexTerm><Primary>-link-chk option</Primary></IndexTerm>
2598 <IndexTerm><Primary>consistency checking of executables</Primary></IndexTerm>
2599 By default, immediately after linking an executable, GHC verifies that
2600 the pieces that went into it were compiled with compatible flags; a
2601 &ldquo;consistency check&rdquo;.
2602 (This is to avoid mysterious failures caused by non-meshing of
2603 incompatibly-compiled programs; e.g., if one <Filename>.o</Filename> file was compiled
2604 for a parallel machine and the others weren't.)  You may turn off this
2605 check with <Option>-no-link-chk</Option>.  You can turn it (back) on with
2606 <Option>-link-chk</Option> (the default).
2607 </Para>
2608 </ListItem>
2609 </VarListEntry>
2610 <VarListEntry>
2611 <Term><Option>-no-hs-main</Option>:</Term>
2612 <ListItem>
2613 <Para>
2614 <IndexTerm><Primary>-no-hs-main option</Primary></IndexTerm>
2615 <IndexTerm><Primary>linking Haskell libraries with foreign code</Primary></IndexTerm>
2616 </Para>
2617
2618 <Para>
2619 In the event you want to include ghc-compiled code as part of another
2620 (non-Haskell) program, the RTS will not be supplying its definition of
2621 <Function>main()</Function> at link-time, you will have to. To signal that to the
2622 driver script when linking, use <Option>-no-hs-main</Option>.
2623 </Para>
2624
2625 <Para>
2626 Notice that since the command-line passed to the linker is rather
2627 involved, you probably want to use the ghc driver script to do the
2628 final link of your `mixed-language' application. This is not a
2629 requirement though, just try linking once with <Option>-v</Option> on to see what
2630 options the driver passes through to the linker.
2631 </Para>
2632 </ListItem>
2633 </VarListEntry>
2634 </VariableList>
2635 </Para>
2636
2637 </Sect2>
2638
2639 </Sect1>
2640
2641 <Sect1 id="sec-using-concurrent">
2642 <Title>Using Concurrent Haskell</Title>
2643
2644 <Para>
2645 <IndexTerm><Primary>Concurrent Haskell&mdash;use</Primary></IndexTerm>
2646 </Para>
2647
2648 <Para>
2649 GHC (as of version 4.00) supports Concurrent Haskell by default,
2650 without requiring a special option or libraries compiled in a certain
2651 way.  To get access to the support libraries for Concurrent Haskell
2652 (i.e. <Literal>Concurrent</Literal> and friends), use the
2653 <Option>-package concurrent</Option> option.
2654 </Para>
2655
2656 <Para>
2657 Three RTS options are provided for modifying the behaviour of the
2658 threaded runtime system.  See the descriptions of
2659 <Option>-C[&lt;us&gt;]</Option>, <Option>-q</Option>, and
2660 <Option>-t&lt;num&gt;</Option> in <XRef LinkEnd="parallel-rts-opts">.
2661 </Para>
2662
2663 <Para>
2664 Concurrent Haskell is described in more detail in <XRef
2665 LinkEnd="sec-Concurrent">.
2666 </Para>
2667
2668 </Sect1>
2669
2670 <Sect1 id="sec-using-parallel">
2671 <Title>Using Parallel Haskell</Title>
2672
2673 <Para>
2674 <IndexTerm><Primary>Parallel Haskell&mdash;use</Primary></IndexTerm>
2675 </Para>
2676
2677 <Para>
2678 &lsqb;You won't be able to execute parallel Haskell programs unless PVM3
2679 (Parallel Virtual Machine, version 3) is installed at your site.]
2680 </Para>
2681
2682 <Para>
2683 To compile a Haskell program for parallel execution under PVM, use the
2684 <Option>-parallel</Option> option,<IndexTerm><Primary>-parallel
2685 option</Primary></IndexTerm> both when compiling <Emphasis>and
2686 linking</Emphasis>.  You will probably want to <Literal>import
2687 Parallel</Literal> into your Haskell modules.
2688 </Para>
2689
2690 <Para>
2691 To run your parallel program, once PVM is going, just invoke it
2692 &ldquo;as normal&rdquo;.  The main extra RTS option is
2693 <Option>-N&lt;n&gt;</Option>, to say how many PVM
2694 &ldquo;processors&rdquo; your program to run on.  (For more details of
2695 all relevant RTS options, please see <XRef
2696 LinkEnd="parallel-rts-opts">.)
2697 </Para>
2698
2699 <Para>
2700 In truth, running Parallel Haskell programs and getting information
2701 out of them (e.g., parallelism profiles) is a battle with the vagaries of
2702 PVM, detailed in the following sections.
2703 </Para>
2704
2705 <Sect2>
2706 <Title>Dummy's guide to using PVM</Title>
2707
2708 <Para>
2709 <IndexTerm><Primary>PVM, how to use</Primary></IndexTerm>
2710 <IndexTerm><Primary>Parallel Haskell&mdash;PVM use</Primary></IndexTerm>
2711 Before you can run a parallel program under PVM, you must set the
2712 required environment variables (PVM's idea, not ours); something like,
2713 probably in your <Filename>.cshrc</Filename> or equivalent:
2714
2715 <ProgramListing>
2716 setenv PVM_ROOT /wherever/you/put/it
2717 setenv PVM_ARCH `$PVM_ROOT/lib/pvmgetarch`
2718 setenv PVM_DPATH $PVM_ROOT/lib/pvmd
2719 </ProgramListing>
2720
2721 </Para>
2722
2723 <Para>
2724 Creating and/or controlling your &ldquo;parallel machine&rdquo; is a purely-PVM
2725 business; nothing specific to Parallel Haskell.
2726 </Para>
2727
2728 <Para>
2729 You use the <Command>pvm</Command><IndexTerm><Primary>pvm command</Primary></IndexTerm> command to start PVM on your
2730 machine.  You can then do various things to control/monitor your
2731 &ldquo;parallel machine;&rdquo; the most useful being:
2732 </Para>
2733
2734 <Para>
2735 <InformalTable>
2736 <TGroup Cols=2>
2737 <ColSpec Align="Left">
2738 <TBody>
2739
2740 <Row>
2741 <Entry><KeyCombo><KeyCap>Control</KeyCap><KeyCap>D</KeyCap></KeyCombo></Entry>
2742 <Entry>exit <Command>pvm</Command>, leaving it running</Entry>
2743 </Row>
2744
2745 <Row>
2746 <Entry><Command>halt</Command></Entry>
2747 <Entry>kill off this &ldquo;parallel machine&rdquo; &amp; exit</Entry>
2748 </Row>
2749
2750 <Row>
2751 <Entry><Command>add &lt;host&gt;</Command></Entry>
2752 <Entry>add <Command>&lt;host&gt;</Command> as a processor</Entry>
2753 </Row>
2754
2755 <Row>
2756 <Entry><Command>delete &lt;host&gt;</Command></Entry>
2757 <Entry>delete <Command>&lt;host&gt;</Command></Entry>
2758 </Row>
2759
2760 <Row>
2761 <Entry><Command>reset</Command></Entry>
2762 <Entry>kill what's going, but leave PVM up</Entry>
2763 </Row>
2764
2765 <Row>
2766 <Entry><Command>conf</Command></Entry>
2767 <Entry>list the current configuration</Entry>
2768 </Row>
2769
2770 <Row>
2771 <Entry><Command>ps</Command></Entry>
2772 <Entry>report processes' status</Entry>
2773 </Row>
2774
2775 <Row>
2776 <Entry><Command>pstat &lt;pid&gt;</Command></Entry>
2777 <Entry>status of a particular process</Entry>
2778 </Row>
2779
2780 </TBody>
2781 </TGroup>
2782 </InformalTable>
2783 </Para>
2784
2785 <Para>
2786 The PVM documentation can tell you much, much more about <Command>pvm</Command>!
2787 </Para>
2788
2789 </Sect2>
2790
2791 <Sect2>
2792 <Title>Parallelism profiles</Title>
2793
2794 <Para>
2795 <IndexTerm><Primary>parallelism profiles</Primary></IndexTerm>
2796 <IndexTerm><Primary>profiles, parallelism</Primary></IndexTerm>
2797 <IndexTerm><Primary>visualisation tools</Primary></IndexTerm>
2798 </Para>
2799
2800 <Para>
2801 With Parallel Haskell programs, we usually don't care about the
2802 results&mdash;only with &ldquo;how parallel&rdquo; it was!  We want pretty pictures.
2803 </Para>
2804
2805 <Para>
2806 Parallelism profiles (&agrave; la <Command>hbcpp</Command>) can be generated with the
2807 <Option>-q</Option><IndexTerm><Primary>-q RTS option (concurrent, parallel)</Primary></IndexTerm> RTS option.  The
2808 per-processor profiling info is dumped into files named
2809 <Filename>&lt;full-path&gt;&lt;program&gt;.gr</Filename>.  These are then munged into a PostScript picture,
2810 which you can then display.  For example, to run your program
2811 <Filename>a.out</Filename> on 8 processors, then view the parallelism profile, do:
2812 </Para>
2813
2814 <Para>
2815
2816 <Screen>
2817 % ./a.out +RTS -N8 -q
2818 % grs2gr *.???.gr &#62; temp.gr     # combine the 8 .gr files into one
2819 % gr2ps -O temp.gr              # cvt to .ps; output in temp.ps
2820 % ghostview -seascape temp.ps   # look at it!
2821 </Screen>
2822
2823 </Para>
2824
2825 <Para>
2826 The scripts for processing the parallelism profiles are distributed
2827 in <Filename>ghc/utils/parallel/</Filename>.
2828 </Para>
2829
2830 </Sect2>
2831
2832 <Sect2>
2833 <Title>Other useful info about running parallel programs</Title>
2834
2835 <Para>
2836 The &ldquo;garbage-collection statistics&rdquo; RTS options can be useful for
2837 seeing what parallel programs are doing.  If you do either
2838 <Option>+RTS -Sstderr</Option><IndexTerm><Primary>-Sstderr RTS option</Primary></IndexTerm> or <Option>+RTS -sstderr</Option>, then
2839 you'll get mutator, garbage-collection, etc., times on standard
2840 error. The standard error of all PE's other than the `main thread'
2841 appears in <Filename>/tmp/pvml.nnn</Filename>, courtesy of PVM.
2842 </Para>
2843
2844 <Para>
2845 Whether doing <Option>+RTS -Sstderr</Option> or not, a handy way to watch
2846 what's happening overall is: <Command>tail -f /tmp/pvml.nnn</Command>.
2847 </Para>
2848
2849 </Sect2>
2850
2851 <Sect2 id="parallel-rts-opts">
2852 <Title>RTS options for Concurrent/Parallel Haskell
2853 </Title>
2854
2855 <Para>
2856 <IndexTerm><Primary>RTS options, concurrent</Primary></IndexTerm>
2857 <IndexTerm><Primary>RTS options, parallel</Primary></IndexTerm>
2858 <IndexTerm><Primary>Concurrent Haskell&mdash;RTS options</Primary></IndexTerm>
2859 <IndexTerm><Primary>Parallel Haskell&mdash;RTS options</Primary></IndexTerm>
2860 </Para>
2861
2862 <Para>
2863 Besides the usual runtime system (RTS) options
2864 (<XRef LinkEnd="runtime-control">), there are a few options particularly
2865 for concurrent/parallel execution.
2866 </Para>
2867
2868 <Para>
2869 <VariableList>
2870
2871 <VarListEntry>
2872 <Term><Option>-N&lt;N&gt;</Option>:</Term>
2873 <ListItem>
2874 <Para>
2875 <IndexTerm><Primary>-N&lt;N&gt; RTS option (parallel)</Primary></IndexTerm>
2876 (PARALLEL ONLY) Use <Literal>&lt;N&gt;</Literal> PVM processors to run this program;
2877 the default is 2.
2878 </Para>
2879 </ListItem>
2880 </VarListEntry>
2881 <VarListEntry>
2882 <Term><Option>-C[&lt;us&gt;]</Option>:</Term>
2883 <ListItem>
2884 <Para>
2885 <IndexTerm><Primary>-C&lt;us&gt; RTS option</Primary></IndexTerm>
2886 Sets the context switch interval to <Literal>&lt;us&gt;</Literal> microseconds.  A context
2887 switch will occur at the next heap allocation after the timer expires.
2888 With <Option>-C0</Option> or <Option>-C</Option>, context switches will occur as often as
2889 possible (at every heap allocation).  By default, context switches
2890 occur every 10 milliseconds.  Note that many interval timers are only
2891 capable of 10 millisecond granularity, so the default setting may be
2892 the finest granularity possible, short of a context switch at every
2893 heap allocation.
2894 </Para>
2895
2896 <Para>
2897 &lsqb;NOTE: this option currently has no effect (version 4.00).  Context
2898 switches happen when the current heap block is full, i.e. every 4k of
2899 allocation].
2900 </Para>
2901 </ListItem>
2902 </VarListEntry>
2903 <VarListEntry>
2904 <Term><Option>-q[v]</Option>:</Term>
2905 <ListItem>
2906 <Para>
2907 <IndexTerm><Primary>-q RTS option</Primary></IndexTerm>
2908 (PARALLEL ONLY) Produce a quasi-parallel profile of thread activity,
2909 in the file <FIlename>&lt;program&gt;.qp</FIlename>.  In the style of <Command>hbcpp</Command>, this profile
2910 records the movement of threads between the green (runnable) and red
2911 (blocked) queues.  If you specify the verbose suboption (<Option>-qv</Option>), the
2912 green queue is split into green (for the currently running thread
2913 only) and amber (for other runnable threads).  We do not recommend
2914 that you use the verbose suboption if you are planning to use the
2915 <Command>hbcpp</Command> profiling tools or if you are context switching at every heap
2916 check (with <Option>-C</Option>).
2917 </Para>
2918 </ListItem>
2919 </VarListEntry>
2920 <VarListEntry>
2921 <Term><Option>-t&lt;num&gt;</Option>:</Term>
2922 <ListItem>
2923 <Para>
2924 <IndexTerm><Primary>-t&lt;num&gt; RTS option</Primary></IndexTerm>
2925 (PARALLEL ONLY) Limit the number of concurrent threads per processor
2926 to <Literal>&lt;num&gt;</Literal>.  The default is 32.  Each thread requires slightly over 1K
2927 <Emphasis>words</Emphasis> in the heap for thread state and stack objects.  (For
2928 32-bit machines, this translates to 4K bytes, and for 64-bit machines,
2929 8K bytes.)
2930 </Para>
2931 </ListItem>
2932 </VarListEntry>
2933 <VarListEntry>
2934 <Term><Option>-d</Option>:</Term>
2935 <ListItem>
2936 <Para>
2937 <IndexTerm><Primary>-d RTS option (parallel)</Primary></IndexTerm>
2938 (PARALLEL ONLY) Turn on debugging.  It pops up one xterm (or GDB, or
2939 something&hellip;) per PVM processor.  We use the standard <Command>debugger</Command>
2940 script that comes with PVM3, but we sometimes meddle with the
2941 <Command>debugger2</Command> script.  We include ours in the GHC distribution,
2942 in <Filename>ghc/utils/pvm/</Filename>.
2943 </Para>
2944 </ListItem>
2945 </VarListEntry>
2946 <VarListEntry>
2947 <Term><Option>-e&lt;num&gt;</Option>:</Term>
2948 <ListItem>
2949 <Para>
2950 <IndexTerm><Primary>-e&lt;num&gt; RTS option (parallel)</Primary></IndexTerm>
2951 (PARALLEL ONLY) Limit the number of pending sparks per processor to
2952 <Literal>&lt;num&gt;</Literal>. The default is 100. A larger number may be appropriate if
2953 your program generates large amounts of parallelism initially.
2954 </Para>
2955 </ListItem>
2956 </VarListEntry>
2957 <VarListEntry>
2958 <Term><Option>-Q&lt;num&gt;</Option>:</Term>
2959 <ListItem>
2960 <Para>
2961 <IndexTerm><Primary>-Q&lt;num&gt; RTS option (parallel)</Primary></IndexTerm>
2962 (PARALLEL ONLY) Set the size of packets transmitted between processors
2963 to <Literal>&lt;num&gt;</Literal>. The default is 1024 words. A larger number may be
2964 appropriate if your machine has a high communication cost relative to
2965 computation speed.
2966 </Para>
2967 </ListItem>
2968 </VarListEntry>
2969 </VariableList>
2970 </Para>
2971
2972 </Sect2>
2973
2974 </Sect1>
2975
2976 &runtime
2977 &debug
2978
2979 </Chapter>
2980
2981 <!-- Emacs stuff:
2982      ;;; Local Variables: ***
2983      ;;; mode: sgml ***
2984      ;;; sgml-parent-document: ("users_guide.sgml" "book" "chapter") ***
2985      ;;; End: ***
2986  -->