1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <sect1 id="separate-compilation">
3 <title>Filenames and separate compilation</title>
5 <indexterm><primary>separate compilation</primary></indexterm>
6 <indexterm><primary>recompilation checker</primary></indexterm>
7 <indexterm><primary>make and recompilation</primary></indexterm>
9 <para>This section describes what files GHC expects to find, what
10 files it creates, where these files are stored, and what options
11 affect this behaviour.</para>
13 <para>Note that this section is written with
14 <firstterm>hierarchical modules</firstterm> in mind (see <xref
15 linkend="hierarchical-modules"/>); hierarchical modules are an
16 extension to Haskell 98 which extends the lexical syntax of
17 module names to include a dot ‘.’. Non-hierarchical
18 modules are thus a special case in which none of the module names
21 <para>Pathname conventions vary from system to system. In
22 particular, the directory separator is
23 ‘<literal>/</literal>’ on Unix systems and
24 ‘<literal>\</literal>’ on Windows systems. In the
25 sections that follow, we shall consistently use
26 ‘<literal>/</literal>’ as the directory separator;
27 substitute this for the appropriate character for your
30 <sect2 id="source-files">
31 <title>Haskell source files</title>
33 <indexterm><primary>filenames</primary></indexterm>
35 <para>Each Haskell source module should be placed in a file on
38 <para>Usually, the file should be named after the module name,
39 replacing dots in the module name by directory separators. For
40 example, on a Unix system, the module <literal>A.B.C</literal>
41 should be placed in the file <literal>A/B/C.hs</literal>,
42 relative to some base directory. If the module is not going to
43 be imported by another module (<literal>Main</literal>, for
44 example), then you are free to use any filename for it.</para>
46 <indexterm><primary>unicode</primary></indexterm>
48 <para> GHC assumes that source files are
49 ASCII<indexterm><primary>ASCII</primary></indexterm> or
50 UTF-8<indexterm><primary>UTF-8</primary></indexterm> only, other
51 encodings<indexterm><primary>encoding</primary></indexterm> are
52 not recognised. However, invalid UTF-8 sequences will be
53 ignored in comments, so it is possible to use other encodings
55 Latin-1<indexterm><primary>Latin-1</primary></indexterm>, as
56 long as the non-comment source code is ASCII only.</para>
59 <sect2 id="output-files">
60 <title>Output files</title>
62 <indexterm><primary>interface files</primary></indexterm>
63 <indexterm><primary><literal>.hi</literal> files</primary></indexterm>
64 <indexterm><primary>object files</primary></indexterm>
65 <indexterm><primary><literal>.o</literal> files</primary></indexterm>
67 <para>When asked to compile a source file, GHC normally
68 generates two files: an <firstterm>object file</firstterm>, and
69 an <firstterm>interface file</firstterm>. </para>
71 <para>The object file, which normally ends in a
72 <literal>.o</literal> suffix, contains the compiled code for the
75 <para>The interface file,
76 which normally ends in a <literal>.hi</literal> suffix, contains
77 the information that GHC needs in order to compile further
78 modules that depend on this module. It contains things like the
79 types of exported functions, definitions of data types, and so
80 on. It is stored in a binary format, so don't try to read one;
81 use the <option>--show-iface</option> option instead (see <xref
82 linkend="hi-options"/>).</para>
84 <para>You should think of the object file and the interface file as a
85 pair, since the interface file is in a sense a compiler-readable
86 description of the contents of the object file. If the
87 interface file and object file get out of sync for any reason,
88 then the compiler may end up making assumptions about the object
89 file that aren't true; trouble will almost certainly follow.
90 For this reason, we recommend keeping object files and interface
91 files in the same place (GHC does this by default, but it is
92 possible to override the defaults as we'll explain
95 <para>Every module has a <emphasis>module name</emphasis>
96 defined in its source code (<literal>module A.B.C where
97 ...</literal>).</para>
99 <para>The name of the object file generated by GHC is derived
100 according to the following rules, where
101 <replaceable>osuf</replaceable> is the object-file suffix (this
102 can be changed with the <option>-osuf</option> option).</para>
106 <para>If there is no <option>-odir</option> option (the
107 default), then the object filename is derived from the
108 source filename (ignoring the module name) by replacing the
109 suffix with <replaceable>osuf</replaceable>.</para>
113 <option>-odir</option> <replaceable>dir</replaceable>
114 has been specified, then the object filename is
115 <replaceable>dir</replaceable>/<replaceable>mod</replaceable>.<replaceable>osuf</replaceable>,
116 where <replaceable>mod</replaceable> is the module name with
117 dots replaced by slashes. GHC will silently create the necessary directory
118 structure underneath <replaceable>dir</replaceable>, if it does not
119 already exist.</para>
123 <para>The name of the interface file is derived using the same
124 rules, except that the suffix is
125 <replaceable>hisuf</replaceable> (<literal>.hi</literal> by
126 default) instead of <replaceable>osuf</replaceable>, and the
127 relevant options are <option>-hidir</option> and
128 <option>-hisuf</option> instead of <option>-odir</option> and
129 <option>-osuf</option> respectively.</para>
131 <para>For example, if GHC compiles the module
132 <literal>A.B.C</literal> in the file
133 <filename>src/A/B/C.hs</filename>, with no
134 <literal>-odir</literal> or <literal>-hidir</literal> flags, the
135 interface file will be put in <literal>src/A/B/C.hi</literal>
136 and the object file in <literal>src/A/B/C.o</literal>.</para>
138 <para>For any module that is imported, GHC requires that the
139 name of the module in the import statement exactly matches the
140 name of the module in the interface file (or source file) found
141 using the strategy specified in <xref linkend="search-path"/>.
142 This means that for most modules, the source file name should
143 match the module name.</para>
145 <para>However, note that it is reasonable to have a module
146 <literal>Main</literal> in a file named
147 <filename>foo.hs</filename>, but this only works because GHC
148 never needs to search for the interface for module
149 <literal>Main</literal> (because it is never imported). It is
150 therefore possible to have several <literal>Main</literal>
151 modules in separate source files in the same directory, and GHC
152 will not get confused.</para>
154 <para>In batch compilation mode, the name of the object file can
155 also be overridden using the <option>-o</option> option, and the
156 name of the interface file can be specified directly using the
157 <option>-ohi</option> option.</para>
160 <sect2 id="search-path">
161 <title>The search path</title>
163 <indexterm><primary>search path</primary>
165 <indexterm><primary>interface files, finding them</primary></indexterm>
166 <indexterm><primary>finding interface files</primary></indexterm>
168 <para>In your program, you import a module
169 <literal>Foo</literal> by saying <literal>import Foo</literal>.
170 In <option>--make</option> mode or GHCi, GHC will look for a
171 source file for <literal>Foo</literal> and arrange to compile it
172 first. Without <option>--make</option>, GHC will look for the
173 interface file for <literal>Foo</literal>, which should have
174 been created by an earlier compilation of
175 <literal>Foo</literal>. GHC uses the same strategy in each of
176 these cases for finding the appropriate file.</para>
178 <para>This strategy is as follows: GHC keeps a list of
179 directories called the <firstterm>search path</firstterm>. For
180 each of these directories, it tries appending
181 <replaceable>basename</replaceable><literal>.</literal><replaceable>extension</replaceable>
182 to the directory, and checks whether the file exists. The value
183 of <replaceable>basename</replaceable> is the module name with
184 dots replaced by the directory separator ('/' or '\', depending
185 on the system), and <replaceable>extension</replaceable> is a
186 source extension (<literal>hs</literal>, <literal>lhs</literal>)
187 if we are in <option>--make</option> mode or GHCi, or
188 <replaceable>hisuf</replaceable> otherwise.</para>
190 <para>For example, suppose the search path contains directories
191 <literal>d1</literal>, <literal>d2</literal>, and
192 <literal>d3</literal>, and we are in <literal>--make</literal>
193 mode looking for the source file for a module
194 <literal>A.B.C</literal>. GHC will look in
195 <literal>d1/A/B/C.hs</literal>, <literal>d1/A/B/C.lhs</literal>,
196 <literal>d2/A/B/C.hs</literal>, and so on.</para>
198 <para>The search path by default contains a single directory:
199 <quote>.</quote> (i.e. the current directory). The following
200 options can be used to add to or change the contents of the
205 <term><option>-i<replaceable>dirs</replaceable></option></term>
207 <para><indexterm><primary><option>-i<replaceable>dirs</replaceable></option>
208 </primary></indexterm>This flag appends a colon-separated
209 list of <filename>dirs</filename> to the search path.</para>
214 <term><option>-i</option></term>
216 <para>resets the search path back to nothing.</para>
221 <para>This isn't the whole story: GHC also looks for modules in
222 pre-compiled libraries, known as packages. See the section on
223 packages (<xref linkend="packages"/>) for details.</para>
226 <sect2 id="options-output">
227 <title>Redirecting the compilation output(s)</title>
229 <indexterm><primary>output-directing options</primary></indexterm>
230 <indexterm><primary>redirecting compilation output</primary></indexterm>
235 <option>-o</option> <replaceable>file</replaceable>
236 <indexterm><primary><option>-o</option></primary></indexterm>
239 <para>GHC's compiled output normally goes into a
240 <filename>.hc</filename>, <filename>.o</filename>, etc.,
241 file, depending on the last-run compilation phase. The
242 option <option>-o <replaceable>file</replaceable></option>
243 re-directs the output of that last-run phase to
244 <replaceable>file</replaceable>.</para>
246 <para>Note: this “feature” can be
247 counterintuitive: <command>ghc -C -o foo.o
248 foo.hs</command> will put the intermediate C code in the
249 file <filename>foo.o</filename>, name
250 notwithstanding!</para>
252 <para>This option is most often used when creating an
253 executable file, to set the filename of the executable.
255 <screen> ghc -o prog --make Main</screen>
257 will compile the program starting with module
258 <literal>Main</literal> and put the executable in the
259 file <literal>prog</literal>.</para>
261 <para>Note: on Windows, if the result is an executable
262 file, the extension "<filename>.exe</filename>" is added
263 if the specified filename does not already have an
268 will compile and link the module
269 <filename>Main.hs</filename>, and put the resulting
270 executable in <filename>foo.exe</filename> (not
271 <filename>foo</filename>).</para>
273 <para>If you use <command>ghc --make</command> and you don't
274 use the <option>-o</option>, the name GHC will choose
275 for the executable will be based on the name of the file
276 containing the module <literal>Main</literal>.
277 Note that with GHC the <literal>Main</literal> module doesn't
278 have to be put in file <filename>Main.hs</filename>.
287 will produce <filename>Prog</filename> (or
288 <filename>Prog.exe</filename> if you are on Windows).</para>
294 <option>-odir</option> <replaceable>dir</replaceable>
295 <indexterm><primary><option>-odir</option></primary></indexterm>
298 <para>Redirects object files to directory
299 <replaceable>dir</replaceable>. For example:</para>
302 $ ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `uname -m`
305 <para>The object files, <filename>Foo.o</filename>,
306 <filename>Bar.o</filename>, and
307 <filename>Bumble.o</filename> would be put into a
308 subdirectory named after the architecture of the executing
309 machine (<filename>x86</filename>,
310 <filename>mips</filename>, etc).</para>
312 <para>Note that the <option>-odir</option> option does
313 <emphasis>not</emphasis> affect where the interface files
314 are put; use the <option>-hidir</option> option for that.
315 In the above example, they would still be put in
316 <filename>parse/Foo.hi</filename>,
317 <filename>parse/Bar.hi</filename>, and
318 <filename>gurgle/Bumble.hi</filename>.</para>
324 <option>-ohi</option> <replaceable>file</replaceable>
325 <indexterm><primary><option>-ohi</option></primary></indexterm>
328 <para>The interface output may be directed to another file
329 <filename>bar2/Wurble.iface</filename> with the option
330 <option>-ohi bar2/Wurble.iface</option> (not
333 <para>WARNING: if you redirect the interface file
334 somewhere that GHC can't find it, then the recompilation
335 checker may get confused (at the least, you won't get any
336 recompilation avoidance). We recommend using a
337 combination of <option>-hidir</option> and
338 <option>-hisuf</option> options instead, if
341 <para>To avoid generating an interface at all, you could
342 use this option to redirect the interface into the bit
343 bucket: <literal>-ohi /dev/null</literal>, for
350 <option>-hidir</option> <replaceable>dir</replaceable>
351 <indexterm><primary><option>-hidir</option></primary></indexterm>
354 <para>Redirects all generated interface files into
355 <replaceable>dir</replaceable>, instead of the
362 <option>-stubdir</option> <replaceable>dir</replaceable>
363 <indexterm><primary><option>-stubdir</option></primary></indexterm>
366 <para>Redirects all generated FFI stub files into
367 <replaceable>dir</replaceable>. Stub files are generated when the
368 Haskell source contains a <literal>foreign export</literal> or
369 <literal>foreign import "&wrapper"</literal> declaration (see <xref
370 linkend="foreign-export-ghc" />). The <option>-stubdir</option>
371 option behaves in exactly the same way as <option>-odir</option>
372 and <option>-hidir</option> with respect to hierarchical
379 <option>-outputdir</option> <replaceable>dir</replaceable>
380 <indexterm><primary><option>-outputdir</option></primary></indexterm>
383 <para>The <option>-outputdir</option> option is shorthand for
385 of <option>-odir</option>, <option>-hidir</option>,
386 and <option>-stubdir</option>.
393 <option>-osuf</option> <replaceable>suffix</replaceable>
394 <indexterm><primary><option>-osuf</option></primary></indexterm>
397 <option>-hisuf</option> <replaceable>suffix</replaceable>
398 <indexterm><primary><option>-hisuf</option></primary></indexterm>
401 <option>-hcsuf</option> <replaceable>suffix</replaceable>
402 <indexterm><primary><option>-hcsuf</option></primary></indexterm>
405 <para>The <option>-osuf</option>
406 <replaceable>suffix</replaceable> will change the
407 <literal>.o</literal> file suffix for object files to
408 whatever you specify. We use this when compiling
409 libraries, so that objects for the profiling versions of
410 the libraries don't clobber the normal ones.</para>
412 <para>Similarly, the <option>-hisuf</option>
413 <replaceable>suffix</replaceable> will change the
414 <literal>.hi</literal> file suffix for non-system
415 interface files (see <xref linkend="hi-options"/>).</para>
417 <para>Finally, the option <option>-hcsuf</option>
418 <replaceable>suffix</replaceable> will change the
419 <literal>.hc</literal> file suffix for compiler-generated
420 intermediate C files.</para>
422 <para>The <option>-hisuf</option>/<option>-osuf</option>
423 game is particularly useful if you want to compile a
424 program both with and without profiling, in the same
425 directory. You can say:
428 to get the ordinary version, and
430 ghc ... -osuf prof.o -hisuf prof.hi -prof -auto-all</screen>
431 to get the profiled version.</para>
437 <sect2 id="keeping-intermediates">
438 <title>Keeping Intermediate Files</title>
439 <indexterm><primary>intermediate files, saving</primary>
441 <indexterm><primary><literal>.hc</literal> files, saving</primary>
443 <indexterm><primary><literal>.s</literal> files, saving</primary>
446 <para>The following options are useful for keeping certain
447 intermediate files around, when normally GHC would throw these
448 away after compilation:</para>
453 <option>-keep-hc-file</option>,
454 <option>-keep-hc-files</option>
455 <indexterm><primary><option>-keep-hc-file</option></primary></indexterm>
456 <indexterm><primary><option>-keep-hc-files</option></primary></indexterm>
459 <para>Keep intermediate <literal>.hc</literal> files when
460 doing <literal>.hs</literal>-to-<literal>.o</literal>
461 compilations via C (NOTE: <literal>.hc</literal> files
462 aren't generated when using the native code generator, you
463 may need to use <option>-fvia-C</option> to force them
464 to be produced).</para>
470 <option>-keep-s-file</option>,
471 <option>-keep-s-files</option>
472 <indexterm><primary><option>-keep-s-file</option></primary></indexterm>
473 <indexterm><primary><option>-keep-s-files</option></primary></indexterm>
476 <para>Keep intermediate <literal>.s</literal> files.</para>
482 <option>-keep-raw-s-file</option>,
483 <option>-keep-raw-s-files</option>
484 <indexterm><primary><option>-keep-raw-s-file</option></primary></indexterm>
485 <indexterm><primary><option>-keep-raw-s-files</option></primary></indexterm>
488 <para>Keep intermediate <literal>.raw-s</literal> files.
489 These are the direct output from the C compiler, before
490 GHC does “assembly mangling” to produce the
491 <literal>.s</literal> file. Again, these are not produced
492 when using the native code generator.</para>
498 <option>-keep-tmp-files</option>
499 <indexterm><primary><option>-keep-tmp-files</option></primary></indexterm>
500 <indexterm><primary>temporary files</primary><secondary>keeping</secondary></indexterm>
503 <para>Instructs the GHC driver not to delete any of its
504 temporary files, which it normally keeps in
505 <literal>/tmp</literal> (or possibly elsewhere; see <xref
506 linkend="temp-files"/>). Running GHC with
507 <option>-v</option> will show you what temporary files
508 were generated along the way.</para>
514 <sect2 id="temp-files">
515 <title>Redirecting temporary files</title>
518 <primary>temporary files</primary>
519 <secondary>redirecting</secondary>
525 <option>-tmpdir</option>
526 <indexterm><primary><option>-tmpdir</option></primary></indexterm>
529 <para>If you have trouble because of running out of space
530 in <filename>/tmp</filename> (or wherever your
531 installation thinks temporary files should go), you may
532 use the <option>-tmpdir
533 <dir></option><indexterm><primary>-tmpdir
534 <dir> option</primary></indexterm> option to specify
535 an alternate directory. For example, <option>-tmpdir
536 .</option> says to put temporary files in the current
537 working directory.</para>
539 <para>Alternatively, use your <constant>TMPDIR</constant>
540 environment variable.<indexterm><primary>TMPDIR
541 environment variable</primary></indexterm> Set it to the
542 name of the directory where temporary files should be put.
543 GCC and other programs will honour the
544 <constant>TMPDIR</constant> variable as well.</para>
546 <para>Even better idea: Set the
547 <constant>DEFAULT_TMPDIR</constant> make variable when
548 building GHC, and never worry about
549 <constant>TMPDIR</constant> again. (see the build
550 documentation).</para>
556 <sect2 id="hi-options">
557 <title>Other options related to interface files</title>
558 <indexterm><primary>interface files, options</primary></indexterm>
563 <option>-ddump-hi</option>
564 <indexterm><primary><option>-ddump-hi</option></primary></indexterm>
567 <para>Dumps the new interface to standard output.</para>
573 <option>-ddump-hi-diffs</option>
574 <indexterm><primary><option>-ddump-hi-diffs</option></primary></indexterm>
577 <para>The compiler does not overwrite an existing
578 <filename>.hi</filename> interface file if the new one is
579 the same as the old one; this is friendly to
580 <command>make</command>. When an interface does change,
581 it is often enlightening to be informed. The
582 <option>-ddump-hi-diffs</option> option will make GHC
583 report the differences between the old and
584 new <filename>.hi</filename> files.</para>
590 <option>-ddump-minimal-imports</option>
591 <indexterm><primary><option>-ddump-minimal-imports</option></primary></indexterm>
594 <para>Dump to the file "M.imports" (where M is the module
595 being compiled) a "minimal" set of import declarations.
596 You can safely replace all the import declarations in
597 "M.hs" with those found in "M.imports". Why would you
598 want to do that? Because the "minimal" imports (a) import
599 everything explicitly, by name, and (b) import nothing
600 that is not required. It can be quite painful to maintain
601 this property by hand, so this flag is intended to reduce
608 <option>--show-iface</option> <replaceable>file</replaceable>
609 <indexterm><primary><option>--show-iface</option></primary></indexterm>
612 <para>where <replaceable>file</replaceable> is the name of
613 an interface file, dumps the contents of that interface in
614 a human-readable (ish) format. See <xref linkend="modes"/>.</para>
621 <title>The recompilation checker</title>
623 <indexterm><primary>recompilation checker</primary></indexterm>
628 <option>-fforce-recomp</option>
629 <indexterm><primary><option>-fforce-recomp</option></primary></indexterm>
630 <indexterm><primary><option>-fno-force-recomp</option></primary></indexterm>
633 <para>Turn off recompilation checking (which is on by
634 default). Recompilation checking normally stops
635 compilation early, leaving an existing
636 <filename>.o</filename> file in place, if it can be
637 determined that the module does not need to be
643 <para>In the olden days, GHC compared the newly-generated
644 <filename>.hi</filename> file with the previous version; if they
645 were identical, it left the old one alone and didn't change its
646 modification date. In consequence, importers of a module with
647 an unchanged output <filename>.hi</filename> file were not
650 <para>This doesn't work any more. Suppose module
651 <literal>C</literal> imports module <literal>B</literal>, and
652 <literal>B</literal> imports module <literal>A</literal>. So
653 changes to module <literal>A</literal> might require module
654 <literal>C</literal> to be recompiled, and hence when
655 <filename>A.hi</filename> changes we should check whether
656 <literal>C</literal> should be recompiled. However, the
657 dependencies of <literal>C</literal> will only list
658 <literal>B.hi</literal>, not <literal>A.hi</literal>, and some
659 changes to <literal>A</literal> (changing the definition of a
660 function that appears in an inlining of a function exported by
661 <literal>B</literal>, say) may conceivably not change
662 <filename>B.hi</filename> one jot. So now…</para>
664 <para>GHC calculates a fingerprint (in fact an MD5 hash) of each
665 interface file, and of each declaration within the interface
666 file. It also keeps in every interface file a list of the
667 fingerprints of everything it used when it last compiled the
668 file. If the source file's modification date is earlier than
669 the <filename>.o</filename> file's date (i.e. the source hasn't
670 changed since the file was last compiled), and the recompilation
671 checking is on, GHC will be clever. It compares the fingerprints
672 on the things it needs this time with the fingerprints
673 on the things it needed last time (gleaned from the
674 interface file of the module being compiled); if they are all
675 the same it stops compiling early in the process saying
676 “Compilation IS NOT required”. What a beautiful
680 about <ulink url="http://hackage.haskell.org/trac/ghc/wiki/Commentary/Compiler/RecompilationAvoidance">how
681 all this works</ulink> in the GHC commentary.</para>
685 <sect2 id="mutual-recursion">
686 <title>How to compile mutually recursive modules</title>
688 <indexterm><primary>module system, recursion</primary></indexterm>
689 <indexterm><primary>recursion, between modules</primary></indexterm>
691 <para>GHC supports the compilation of mutually recursive modules.
692 This section explains how.</para>
694 <para>Every cycle in the module import graph must be broken by a <filename>hs-boot</filename> file.
695 Suppose that modules <filename>A.hs</filename> and <filename>B.hs</filename> are Haskell source files,
701 newtype TA = MkTA Int
707 import {-# SOURCE #-} A( TA(..) )
714 <indexterm><primary><literal>hs-boot</literal>
715 files</primary></indexterm> <indexterm><primary>importing,
716 <literal>hi-boot</literal> files</primary></indexterm>
717 Here <filename>A</filename> imports <filename>B</filename>, but <filename>B</filename> imports
718 <filename>A</filename> with a <literal>{-# SOURCE #-}</literal> pragma, which breaks the
719 circular dependency. Every loop in the module import graph must be broken by a <literal>{-# SOURCE #-}</literal> import;
720 or, equivalently, the module import graph must be acyclic if <literal>{-# SOURCE #-}</literal> imports are ignored.
722 <para>For every module <filename>A.hs</filename> that is <literal>{-# SOURCE #-}</literal>-imported
723 in this way there must exist a source file <literal>A.hs-boot</literal>. This file contains an abbreviated
724 version of <filename>A.hs</filename>, thus:
727 newtype TA = MkTA Int
730 <para>To compile these three files, issue the following commands:
732 ghc -c A.hs-boot -- Produces A.hi-boot, A.o-boot
733 ghc -c B.hs -- Consumes A.hi-boot, produces B.hi, B.o
734 ghc -c A.hs -- Consumes B.hi, produces A.hi, A.o
735 ghc -o foo A.o B.o -- Linking the program
738 <para>There are several points to note here:
741 <para>The file <filename>A.hs-boot</filename> is a programmer-written source file.
742 It must live in the same directory as its parent source file <filename>A.hs</filename>.
743 Currently, if you use a literate source file <filename>A.lhs</filename> you must
744 also use a literate boot file, <filename>A.lhs-boot</filename>; and vice versa.
748 A <filename>hs-boot</filename> file is compiled by GHC, just like a <filename>hs</filename> file:
752 When a hs-boot file <filename>A.hs-boot</filename>
753 is compiled, it is checked for scope and type errors.
754 When its parent module <filename>A.hs</filename> is compiled, the two are compared, and
755 an error is reported if the two are inconsistent.
759 <para> Just as compiling <filename>A.hs</filename> produces an
760 interface file <filename>A.hi</filename>, and an object file
761 <filename>A.o</filename>, so compiling
762 <filename>A.hs-boot</filename> produces an interface file
763 <filename>A.hi-boot</filename>, and an pseudo-object file
764 <filename>A.o-boot</filename>: </para>
768 <para>The pseudo-object file <filename>A.o-boot</filename> is
769 empty (don't link it!), but it is very useful when using a
770 Makefile, to record when the <filename>A.hi-boot</filename> was
771 last brought up to date (see <xref
772 linkend="using-make"/>).</para>
776 <para>The <filename>hi-boot</filename> generated by compiling a
777 <filename>hs-boot</filename> file is in the same
778 machine-generated binary format as any other GHC-generated
779 interface file (e.g. <filename>B.hi</filename>). You can
780 display its contents with <command>ghc
781 --show-iface</command>. If you specify a directory for
782 interface files, the <option>-ohidir</option> flag, then that
783 affects <filename>hi-boot</filename> files
789 <listitem><para> If hs-boot files are considered distinct from their parent source
790 files, and if a <literal>{-# SOURCE #-}</literal> import is considered to refer to the
791 hs-boot file, then the module import graph must have no cycles. The command
792 <command>ghc -M</command> will report an error if a cycle is found.
795 <listitem><para> A module <literal>M</literal> that is
796 <literal>{-# SOURCE #-}</literal>-imported in a program will usually also be
797 ordinarily imported elsewhere. If not, <command>ghc --make</command>
798 automatically adds <literal>M</literal> to the set of modules it tries to
799 compile and link, to ensure that <literal>M</literal>'s implementation is included in
805 A hs-boot file need only contain the bare
806 minimum of information needed to get the bootstrapping process
807 started. For example, it doesn't need to contain declarations
808 for <emphasis>everything</emphasis> that module
809 <literal>A</literal> exports, only the things required by the
810 module(s) that import <literal>A</literal> recursively.</para>
811 <para>A hs-boot file is written in a subset of Haskell:
813 <listitem><para> The module header (including the export list), and import statements, are exactly as in
814 Haskell, and so are the scoping rules.
815 Hence, to mention a non-Prelude type or class, you must import it.</para></listitem>
817 <listitem><para> There must be no value declarations, but there can be type signatures for
820 double :: Int -> Int
823 <listitem><para> Fixity declarations are exactly as in Haskell.</para></listitem>
824 <listitem><para> Type synonym declarations are exactly as in Haskell.</para></listitem>
825 <listitem><para> A data type declaration can either be given in full, exactly as in Haskell, or it
826 can be given abstractly, by omitting the '=' sign and everything that follows. For example:
830 In a <emphasis>source</emphasis> program
831 this would declare TA to have no constructors (a GHC extension: see <xref linkend="nullary-types"/>),
832 but in an hi-boot file it means "I don't know or care what the constructors are".
833 This is the most common form of data type declaration, because it's easy to get right.
834 You <emphasis>can</emphasis> also write out the constructors but, if you do so, you must write
835 it out precisely as in its real definition.</para>
837 If you do not write out the constructors, you may need to give a kind
838 annotation (<xref linkend="kinding"/>), to tell
839 GHC the kind of the type variable, if it is not "*". (In source files, this is worked out
840 from the way the type variable is used in the constructors.) For example:
842 data R (x :: * -> *) y
844 You cannot use <literal>deriving</literal> on a data type declaration; write an
845 <literal>instance</literal> declaration instead.
847 <listitem><para> Class declarations is exactly as in Haskell, except that you may not put
848 default method declarations. You can also omit all the superclasses and class
849 methods entirely; but you must either omit them all or put them all in.
851 <listitem><para> You can include instance declarations just as in Haskell; but omit the "where" part.
858 <sect2 id="using-make">
859 <title>Using <command>make</command></title>
861 <indexterm><primary><literal>make</literal></primary></indexterm>
863 <para>It is reasonably straightforward to set up a
864 <filename>Makefile</filename> to use with GHC, assuming you name
865 your source files the same as your modules. Thus:</para>
869 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
871 SRCS = Main.lhs Foo.lhs Bar.lhs
872 OBJS = Main.o Foo.o Bar.o
874 .SUFFIXES : .o .hs .hi .lhs .hc .s
878 $(HC) -o $@ $(HC_OPTS) $(OBJS)
880 # Standard suffix rules
885 $(HC) -c $< $(HC_OPTS)
888 $(HC) -c $< $(HC_OPTS)
894 $(HC) -c $< $(HC_OPTS)
897 $(HC) -c $< $(HC_OPTS)
899 # Inter-module dependencies
900 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
901 Main.o Main.hc Main.s : Foo.hi Baz.hi # Main imports Foo and Baz
904 <para>(Sophisticated <command>make</command> variants may
905 achieve some of the above more elegantly. Notably,
906 <command>gmake</command>'s pattern rules let you write the more
907 comprehensible:</para>
911 $(HC) -c $< $(HC_OPTS)
914 <para>What we've shown should work with any
915 <command>make</command>.)</para>
917 <para>Note the cheesy <literal>.o.hi</literal> rule: It records
918 the dependency of the interface (<filename>.hi</filename>) file
919 on the source. The rule says a <filename>.hi</filename> file
920 can be made from a <filename>.o</filename> file by
921 doing…nothing. Which is true.</para>
922 <para> Note that the suffix rules are all repeated twice, once
923 for normal Haskell source files, and once for <filename>hs-boot</filename>
924 files (see <xref linkend="mutual-recursion"/>).</para>
926 <para>Note also the inter-module dependencies at the end of the
927 Makefile, which take the form
930 Foo.o Foo.hc Foo.s : Baz.hi # Foo imports Baz
933 They tell <command>make</command> that if any of
934 <literal>Foo.o</literal>, <literal>Foo.hc</literal> or
935 <literal>Foo.s</literal> have an earlier modification date than
936 <literal>Baz.hi</literal>, then the out-of-date file must be
937 brought up to date. To bring it up to date,
938 <literal>make</literal> looks for a rule to do so; one of the
939 preceding suffix rules does the job nicely. These dependencies
940 can be generated automatically by <command>ghc</command>; see
941 <xref linkend="makefile-dependencies"/></para>
945 <sect2 id="makefile-dependencies">
946 <title>Dependency generation</title>
947 <indexterm><primary>dependencies in Makefiles</primary></indexterm>
948 <indexterm><primary>Makefile dependencies</primary></indexterm>
950 <para>Putting inter-dependencies of the form <literal>Foo.o :
951 Bar.hi</literal> into your <filename>Makefile</filename> by
952 hand is rather error-prone. Don't worry, GHC has support for
953 automatically generating the required dependencies. Add the
954 following to your <filename>Makefile</filename>:</para>
958 ghc -M $(HC_OPTS) $(SRCS)
961 <para>Now, before you start compiling, and any time you change
962 the <literal>imports</literal> in your program, do
963 <command>make depend</command> before you do <command>make
964 cool_pgm</command>. The command <command>ghc -M</command> will
965 append the needed dependencies to your
966 <filename>Makefile</filename>.</para>
968 <para>In general, <command>ghc -M Foo</command> does the following.
969 For each module <literal>M</literal> in the set
970 <literal>Foo</literal> plus all its imports (transitively),
971 it adds to the Makefile:
973 <listitem><para>A line recording the dependence of the object file on the source file.
977 (or <literal>M.lhs</literal> if that is the filename you used).
979 <listitem><para> For each import declaration <literal>import X</literal> in <literal>M</literal>,
980 a line recording the dependence of <literal>M</literal> on <literal>X</literal>:
983 </programlisting></para></listitem>
984 <listitem><para> For each import declaration <literal>import {-# SOURCE #-} X</literal> in <literal>M</literal>,
985 a line recording the dependence of <literal>M</literal> on <literal>X</literal>:
989 (See <xref linkend="mutual-recursion"/> for details of
990 <literal>hi-boot</literal> style interface files.)
993 If <literal>M</literal> imports multiple modules, then there will
994 be multiple lines with <filename>M.o</filename> as the
996 <para>There is no need to list all of the source files as arguments to the <command>ghc -M</command> command;
997 <command>ghc</command> traces the dependencies, just like <command>ghc --make</command>
998 (a new feature in GHC 6.4).</para>
1000 <para>Note that <literal>ghc -M</literal> needs to find a <emphasis>source
1001 file</emphasis> for each module in the dependency graph, so that it can
1002 parse the import declarations and follow dependencies. Any pre-compiled
1003 modules without source files must therefore belong to a
1004 package<footnote><para>This is a change in behaviour relative to 6.2 and
1008 <para>By default, <command>ghc -M</command> generates all the
1009 dependencies, and then concatenates them onto the end of
1010 <filename>makefile</filename> (or
1011 <filename>Makefile</filename> if <filename>makefile</filename>
1012 doesn't exist) bracketed by the lines "<literal># DO NOT
1013 DELETE: Beginning of Haskell dependencies</literal>" and
1014 "<literal># DO NOT DELETE: End of Haskell
1015 dependencies</literal>". If these lines already exist in the
1016 <filename>makefile</filename>, then the old dependencies are
1017 deleted first.</para>
1019 <para>Don't forget to use the same <option>-package</option>
1020 options on the <literal>ghc -M</literal> command line as you
1021 would when compiling; this enables the dependency generator to
1022 locate any imported modules that come from packages. The
1023 package modules won't be included in the dependencies
1024 generated, though (but see the
1025 <option>––include-pkg-deps</option> option below).</para>
1027 <para>The dependency generation phase of GHC can take some
1028 additional options, which you may find useful.
1030 The options which affect dependency generation are:</para>
1034 <term><option>-ddump-mod-cycles</option></term>
1036 <para>Display a list of the cycles in the module graph. This is
1037 useful when trying to eliminate such cycles.</para>
1042 <term><option>-v2</option></term>
1044 <para>Print a full list of the module dependencies to stdout.
1045 (This is the standard verbosity flag, so the list will
1046 also be displayed with <option>-v3</option> and
1047 <option>-v4</option>;
1048 <xref linkend ="options-help"/>.)</para>
1053 <term><option>-dep-makefile</option> <replaceable>file</replaceable></term>
1055 <para>Use <replaceable>file</replaceable> as the makefile,
1056 rather than <filename>makefile</filename> or
1057 <filename>Makefile</filename>. If
1058 <replaceable>file</replaceable> doesn't exist,
1059 <command>mkdependHS</command> creates it. We often use
1060 <option>-dep-makefile .depend</option> to put the dependencies in
1061 <filename>.depend</filename> and then
1062 <command>include</command> the file
1063 <filename>.depend</filename> into
1064 <filename>Makefile</filename>.</para>
1069 <term><option>-dep-suffix <suf></option></term>
1071 <para>Make extra dependencies that declare that files
1073 <filename>.<suf>_<osuf></filename>
1074 depend on interface files with suffix
1075 <filename>.<suf>_hi</filename>, or (for
1076 <literal>{-# SOURCE #-}</literal>
1077 imports) on <filename>.hi-boot</filename>. Multiple
1078 <option>-dep-suffix</option> flags are permitted. For example,
1079 <option>-dep-suffix a -dep-suffix b</option>
1080 will make dependencies
1081 for <filename>.hs</filename> on
1082 <filename>.hi</filename>,
1083 <filename>.a_hs</filename> on
1084 <filename>.a_hi</filename>, and
1085 <filename>.b_hs</filename> on
1086 <filename>.b_hi</filename>. (Useful in
1087 conjunction with NoFib "ways".)</para>
1092 <term><option>––exclude-module=<file></option></term>
1094 <para>Regard <filename><file></filename> as
1095 "stable"; i.e., exclude it from having dependencies on
1101 <term><option>––include-pkg-deps</option></term>
1103 <para>Regard modules imported from packages as unstable,
1104 i.e., generate dependencies on any imported package modules
1105 (including <literal>Prelude</literal>, and all other
1106 standard Haskell libraries). Dependencies are not traced
1107 recursively into packages; dependencies are only generated for
1108 home-package modules on external-package modules directly imported
1109 by the home package module.
1110 This option is normally
1111 only used by the various system libraries.</para>
1118 <sect2 id="orphan-modules">
1119 <title>Orphan modules and instance declarations</title>
1121 <para> Haskell specifies that when compiling module M, any instance
1122 declaration in any module "below" M is visible. (Module A is "below"
1123 M if A is imported directly by M, or if A is below a module that M imports directly.)
1124 In principle, GHC must therefore read the interface files of every module below M,
1125 just in case they contain an instance declaration that matters to M. This would
1126 be a disaster in practice, so GHC tries to be clever. </para>
1128 <para>In particular, if an instance declaration is in the same module as the definition
1129 of any type or class mentioned in the <emphasis>head</emphasis> of the instance declaration
1130 (the part after the “<literal>=></literal>”; see <xref linkend="instance-rules"/>), then
1131 GHC has to visit that interface file anyway. Example:</para>
1134 instance C a => D (T a) where ...
1137 <para> The instance declaration is only relevant if the type T is in use, and if
1138 so, GHC will have visited A's interface file to find T's definition. </para>
1140 <para> The only problem comes when a module contains an instance declaration
1141 and GHC has no other reason for visiting the module. Example:
1144 instance C a => D (T a) where ...
1147 Here, neither D nor T is declared in module Orphan.
1148 We call such modules “orphan modules”.
1149 GHC identifies orphan modules, and visits the interface file of
1150 every orphan module below the module being compiled. This is usually
1151 wasted work, but there is no avoiding it. You should therefore do
1152 your best to have as few orphan modules as possible.
1155 Functional dependencies complicate matters. Suppose we have:
1158 instance E T Int where ...
1161 Is this an orphan module? Apparently not, because <literal>T</literal>
1162 is declared in the same module. But suppose class <literal>E</literal> had a
1163 functional dependency:
1166 class E x y | y -> x where ...
1168 Then in some importing module M, the constraint <literal>(E a Int)</literal> should be "improved" by setting
1169 <literal>a = T</literal>, <emphasis>even though there is no explicit mention
1170 of <literal>T</literal> in M</emphasis>.</para>
1172 These considerations lead to the following definition of an orphan module:
1174 <listitem> <para> An <emphasis>orphan module</emphasis>
1175 <indexterm><primary>orphan module</primary></indexterm>
1176 contains at least one <emphasis>orphan instance</emphasis> or at
1177 least one <emphasis>orphan rule</emphasis>.</para> </listitem>
1179 <listitem><para> An instance declaration in a module M is an <emphasis>orphan instance</emphasis> if
1180 <indexterm><primary>orphan instance</primary></indexterm>
1183 The class of the instance declaration is not declared in M, and
1186 <para> <emphasis>Either</emphasis> the class has no functional dependencies, and none of the type constructors
1187 in the instance head is declared in M; <emphasis>or</emphasis> there
1188 is a functional dependency for which none of the type constructors mentioned
1189 in the <emphasis>non-determined</emphasis> part of the instance head is defined in M.
1193 <para> Only the instance head
1194 counts. In the example above, it is not good enough for C's declaration
1195 to be in module A; it must be the declaration of D or T.</para>
1198 <listitem><para> A rewrite rule in a module M is an <emphasis>orphan rule</emphasis>
1199 <indexterm><primary>orphan rule</primary></indexterm>
1200 if none of the variables, type constructors,
1201 or classes that are free in the left hand side of the rule are declared in M.
1207 <para>If you use the flag <option>-fwarn-orphans</option>, GHC will warn you
1208 if you are creating an orphan module.
1209 Like any warning, you can switch the warning off with <option>-fno-warn-orphans</option>,
1210 and <option>-Werror</option>
1211 will make the compilation fail if the warning is issued.
1214 You can identify an orphan module by looking in its interface
1215 file, <filename>M.hi</filename>, using the
1216 <link linkend="modes"><option>--show-iface</option> mode</link>. If there is a <literal>[orphan module]</literal> on the
1217 first line, GHC considers it an orphan module.
1224 ;;; Local Variables: ***
1226 ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***