2d281607d158109d1d9fc62770dffa89cb16764d
[ghc-hetmet.git] / ghc / docs / users_guide / separate_compilation.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2   <sect1 id="separate-compilation">
3     <title>Filenames and separate compilation</title>
4
5     <indexterm><primary>separate compilation</primary></indexterm>
6     <indexterm><primary>recompilation checker</primary></indexterm>
7     <indexterm><primary>make and recompilation</primary></indexterm>
8
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>
12
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 &lsquo;.&rsquo;.  Non-hierarchical
18     modules are thus a special case in which none of the module names
19     contain dots.</para>
20
21     <para>Pathname conventions vary from system to system.  In
22     particular, the directory separator is
23     &lsquo;<literal>/</literal>&rsquo; on Unix systems and
24     &lsquo;<literal>\</literal>&rsquo; on Windows systems.  In the
25     sections that follow, we shall consistently use
26     &lsquo;<literal>/</literal>&rsquo; as the directory separator;
27     substitute this for the appropriate character for your
28     system.</para>
29
30     <sect2 id="source-files">
31       <title>Haskell source files</title>
32
33       <para>Each Haskell source module should be placed in a file on
34       its own.</para>
35
36       <para>The file should usually be named after the module name, by
37       replacing dots in the module name by directory separators.  For
38       example, on a Unix system, the module <literal>A.B.C</literal>
39       should be placed in the file <literal>A/B/C.hs</literal>,
40       relative to some base directory.  GHC's behaviour if this rule
41       is not followed is fully defined by the following section (<xref
42       linkend="output-files"/>).</para>
43     </sect2>
44
45     <sect2 id="output-files">
46       <title>Output files</title>
47
48       <indexterm><primary>interface files</primary></indexterm>
49       <indexterm><primary><literal>.hi</literal> files</primary></indexterm>
50       <indexterm><primary>object files</primary></indexterm>
51       <indexterm><primary><literal>.o</literal> files</primary></indexterm>
52
53       <para>When asked to compile a source file, GHC normally
54       generates two files: an <firstterm>object file</firstterm>, and
55       an <firstterm>interface file</firstterm>. </para>
56
57       <para>The object file, which normally ends in a
58       <literal>.o</literal> suffix (or <literal>.obj</literal> if
59       you're on Windows), contains the compiled code for the module.</para>
60
61       <para>The interface file,
62       which normally ends in a <literal>.hi</literal> suffix, contains
63       the information that GHC needs in order to compile further
64       modules that depend on this module.  It contains things like the
65       types of exported functions, definitions of data types, and so
66       on.  It is stored in a binary format, so don't try to read one;
67       use the <option>--show-iface</option> option instead (see <xref
68       linkend="hi-options"/>).</para>
69
70       <para>You should think of the object file and the interface file as a
71       pair, since the interface file is in a sense a compiler-readable
72       description of the contents of the object file.  If the
73       interface file and object file get out of sync for any reason,
74       then the compiler may end up making assumptions about the object
75       file that aren't true; trouble will almost certainly follow.
76       For this reason, we recommend keeping object files and interface
77       files in the same place (GHC does this by default, but it is
78       possible to override the defaults as we'll explain
79       shortly).</para>
80
81       <para>Every module has a <emphasis>module name</emphasis>
82       defined in its source code (<literal>module A.B.C where
83       ...</literal>).</para>
84
85       <para>The name of the object file generated by GHC is derived
86       according to the following rules, where
87       <replaceable>osuf</replaceable> is the object-file suffix (this
88       can be changed with the <option>-osuf</option> option).</para>
89
90       <itemizedlist>
91         <listitem>
92           <para>If there is no <option>-odir</option> option (the
93           default), then the object filename is derived from the
94           source filename (ignoring the module name) by replacing the
95           suffix with <replaceable>osuf</replaceable>.</para>
96         </listitem>
97         <listitem>
98           <para>If
99           <option>-odir</option>&nbsp;<replaceable>dir</replaceable>
100           has been specified, then the object filename is
101           <replaceable>dir</replaceable>/<replaceable>mod</replaceable>.<replaceable>osuf</replaceable>,
102           where <replaceable>mod</replaceable> is the module name with
103           dots replaced by slashes.</para>
104         </listitem>
105       </itemizedlist>
106
107       <para>The name of the interface file is derived using the same
108       rules, except that the suffix is
109       <replaceable>hisuf</replaceable> (<literal>.hi</literal> by
110       default) instead of <replaceable>osuf</replaceable>, and the
111       relevant options are <option>-hidir</option> and
112       <option>-hisuf</option> instead of <option>-odir</option> and
113       <option>-osuf</option> respectively.</para>
114
115       <para>For example, if GHC compiles the module
116       <literal>A.B.C</literal> in the file
117       <filename>src/A/B/C.hs</filename>, with no
118       <literal>-odir</literal> or <literal>-hidir</literal> flags, the
119       interface file will be put in <literal>src/A/B/C.hi</literal>
120       and the object file in <literal>src/A/B/C.o</literal>.</para>
121
122       <para>For any module that is imported, GHC requires that the
123       name of the module in the import statement exactly matches the
124       name of the module in the interface file (or source file) found
125       using the strategy specified in <xref linkend="search-path"/>.
126       This means that for most modules, the source file name should
127       match the module name.</para>
128
129       <para>However, note that it is reasonable to have a module
130       <literal>Main</literal> in a file named
131       <filename>foo.hs</filename>, but this only works because GHC
132       never needs to search for the interface for module
133       <literal>Main</literal> (because it is never imported).  It is
134       therefore possible to have several <literal>Main</literal>
135       modules in separate source files in the same directory, and GHC
136       will not get confused.</para>
137
138       <para>In batch compilation mode, the name of the object file can
139       also be overridden using the <option>-o</option> option, and the
140       name of the interface file can be specified directly using the
141       <option>-ohi</option> option.</para>
142     </sect2>
143
144     <sect2 id="search-path">
145       <title>The search path</title>
146
147       <indexterm><primary>search path</primary>
148       </indexterm>
149       <indexterm><primary>interface files, finding them</primary></indexterm>
150       <indexterm><primary>finding interface files</primary></indexterm>
151
152       <para>In your program, you import a module
153       <literal>Foo</literal> by saying <literal>import Foo</literal>.
154       In <option>--make</option> mode or GHCi, GHC will look for a
155       source file for <literal>Foo</literal> and arrange to compile it
156       first.  Without <option>--make</option>, GHC will look for the
157       interface file for <literal>Foo</literal>, which should have
158       been created by an earlier compilation of
159       <literal>Foo</literal>.  GHC uses the same strategy in each of
160       these cases for finding the appropriate file.</para>
161
162       <para>This strategy is as follows: GHC keeps a list of
163       directories called the <firstterm>search path</firstterm>.  For
164       each of these directories, it tries appending
165       <replaceable>basename</replaceable><literal>.</literal><replaceable>extension</replaceable>
166       to the directory, and checks whether the file exists.  The value
167       of <replaceable>basename</replaceable> is the module name with
168       dots replaced by the directory separator ('/' or '\', depending
169       on the system), and <replaceable>extension</replaceable> is a
170       source extension (<literal>hs</literal>, <literal>lhs</literal>)
171       if we are in <option>--make</option> mode and GHCi, or
172       <replaceable>hisuf</replaceable> otherwise.</para>
173
174       <para>For example, suppose the search path contains directories
175       <literal>d1</literal>, <literal>d2</literal>, and
176       <literal>d3</literal>, and we are in <literal>--make</literal>
177       mode looking for the source file for a module
178       <literal>A.B.C</literal>.  GHC will look in
179       <literal>d1/A/B/C.hs</literal>, <literal>d1/A/B/C.lhs</literal>,
180       <literal>d2/A/B/C.hs</literal>, and so on.</para>
181
182       <para>The search path by default contains a single directory:
183       <quote>.</quote> (i.e. the current directory).  The following
184       options can be used to add to or change the contents of the
185       search path:</para>
186
187       <variablelist>
188         <varlistentry>
189           <term><option>-i<replaceable>dirs</replaceable></option></term>
190           <listitem>
191             <para><indexterm><primary><option>-i<replaceable>dirs</replaceable></option>
192             </primary></indexterm>This flag appends a colon-separated
193             list of <filename>dirs</filename> to the search path.</para>
194           </listitem>
195         </varlistentry>
196
197         <varlistentry>
198           <term><option>-i</option></term>
199           <listitem>
200             <para>resets the search path back to nothing.</para>
201           </listitem>
202         </varlistentry>
203       </variablelist>
204
205       <para>This isn't the whole story: GHC also looks for modules in
206       pre-compiled libraries, known as packages.  See the section on
207       packages (<xref linkend="packages"/>), for details.</para>
208     </sect2>
209
210     <sect2 id="options-output">
211       <title>Redirecting the compilation output(s)</title>
212
213       <indexterm><primary>output-directing options</primary></indexterm>
214       <indexterm><primary>redirecting compilation output</primary></indexterm>
215
216       <variablelist>
217         <varlistentry>
218           <term>
219             <option>-o</option> <replaceable>file</replaceable>
220             <indexterm><primary><option>-o</option></primary></indexterm>
221           </term>
222           <listitem>
223             <para>GHC's compiled output normally goes into a
224             <filename>.hc</filename>, <filename>.o</filename>, etc.,
225             file, depending on the last-run compilation phase.  The
226             option <option>-o <replaceable>file</replaceable></option>
227             re-directs the output of that last-run phase to
228             <replaceable>file</replaceable>.</para>
229
230             <para>Note: this &ldquo;feature&rdquo; can be
231             counterintuitive: <command>ghc -C -o foo.o
232             foo.hs</command> will put the intermediate C code in the
233             file <filename>foo.o</filename>, name
234             notwithstanding!</para>
235
236             <para>This option is most often used when creating an
237             executable file, to set the filename of the executable.
238             For example:
239 <screen>   ghc -o prog --make Main</screen>
240
241             will compile the program starting with module
242             <literal>Main</literal>  and put the executable in the
243             file <literal>prog</literal>.</para>
244
245             <para>Note: on Windows, if the result is an executable
246             file, the extension "<filename>.exe</filename>" is added
247             if the specified filename does not already have an
248             extension.  Thus
249 <programlisting>
250    ghc -o foo Main.hs
251 </programlisting>
252           will compile and link the module
253           <filename>Main.hs</filename>, and put the resulting
254           executable in <filename>foo.exe</filename> (not
255           <filename>foo</filename>).</para>
256           </listitem>
257         </varlistentry>
258
259         <varlistentry>
260           <term>
261             <option>-odir</option> <replaceable>dir</replaceable>
262             <indexterm><primary><option>-odir</option></primary></indexterm>
263           </term>
264           <listitem>
265             <para>Redirects object files to directory
266             <replaceable>dir</replaceable>.  For example:</para>
267
268 <screen>
269 $ ghc -c parse/Foo.hs parse/Bar.hs gurgle/Bumble.hs -odir `arch`
270 </screen>
271
272             <para>The object files, <filename>Foo.o</filename>,
273             <filename>Bar.o</filename>, and
274             <filename>Bumble.o</filename> would be put into a
275             subdirectory named after the architecture of the executing
276             machine (<filename>x86</filename>,
277             <filename>mips</filename>, etc).</para>
278
279             <para>Note that the <option>-odir</option> option does
280             <emphasis>not</emphasis> affect where the interface files
281             are put; use the <option>-hidir</option> option for that.
282             In the above example, they would still be put in
283             <filename>parse/Foo.hi</filename>,
284             <filename>parse/Bar.hi</filename>, and
285             <filename>gurgle/Bumble.hi</filename>.</para>
286           </listitem>
287         </varlistentry>
288
289         <varlistentry>
290           <term>
291             <option>-ohi</option>  <replaceable>file</replaceable>
292             <indexterm><primary><option>-ohi</option></primary></indexterm>
293           </term>
294           <listitem>
295             <para>The interface output may be directed to another file
296             <filename>bar2/Wurble.iface</filename> with the option
297             <option>-ohi bar2/Wurble.iface</option> (not
298             recommended).</para>
299
300             <para>WARNING: if you redirect the interface file
301             somewhere that GHC can't find it, then the recompilation
302             checker may get confused (at the least, you won't get any
303             recompilation avoidance).  We recommend using a
304             combination of <option>-hidir</option> and
305             <option>-hisuf</option> options instead, if
306             possible.</para>
307
308             <para>To avoid generating an interface at all, you could
309             use this option to redirect the interface into the bit
310             bucket: <literal>-ohi /dev/null</literal>, for
311             example.</para>
312           </listitem>
313         </varlistentry>
314
315         <varlistentry>
316           <term>
317             <option>-hidir</option>  <replaceable>dir</replaceable>
318             <indexterm><primary><option>-hidir</option></primary></indexterm>
319           </term>
320           <listitem>
321             <para>Redirects all generated interface files into
322             <replaceable>dir</replaceable>, instead of the
323             default.</para>
324           </listitem>
325         </varlistentry>
326
327         <varlistentry>
328           <term>
329             <option>-osuf</option> <replaceable>suffix</replaceable>
330             <indexterm><primary><option>-osuf</option></primary></indexterm>
331           </term>
332           <term>
333             <option>-hisuf</option> <replaceable>suffix</replaceable>
334             <indexterm><primary><option>-hisuf</option></primary></indexterm>
335           </term>
336           <term>
337             <option>-hcsuf</option> <replaceable>suffix</replaceable>
338             <indexterm><primary><option>-hcsuf</option></primary></indexterm>
339           </term>
340           <listitem>
341             <para>The <option>-osuf</option>
342             <replaceable>suffix</replaceable> will change the
343             <literal>.o</literal> file suffix for object files to
344             whatever you specify.  We use this when compiling
345             libraries, so that objects for the profiling versions of
346             the libraries don't clobber the normal ones.</para>
347
348             <para>Similarly, the <option>-hisuf</option>
349             <replaceable>suffix</replaceable> will change the
350             <literal>.hi</literal> file suffix for non-system
351             interface files (see <xref linkend="hi-options"/>).</para>
352
353             <para>Finally, the option <option>-hcsuf</option>
354             <replaceable>suffix</replaceable> will change the
355             <literal>.hc</literal> file suffix for compiler-generated
356             intermediate C files.</para>
357
358             <para>The <option>-hisuf</option>/<option>-osuf</option>
359             game is particularly useful if you want to compile a
360             program both with and without profiling, in the same
361             directory.  You can say:
362             <screen>
363               ghc ...</screen>
364             to get the ordinary version, and
365             <screen>
366               ghc ... -osuf prof.o -hisuf prof.hi -prof -auto-all</screen>
367             to get the profiled version.</para>
368           </listitem>
369         </varlistentry>
370       </variablelist>
371     </sect2>
372
373     <sect2 id="keeping-intermediates">
374       <title>Keeping Intermediate Files</title>
375       <indexterm><primary>intermediate files, saving</primary>
376       </indexterm>
377       <indexterm><primary><literal>.hc</literal> files, saving</primary>
378       </indexterm>
379       <indexterm><primary><literal>.s</literal> files, saving</primary>
380       </indexterm>
381
382       <para>The following options are useful for keeping certain
383       intermediate files around, when normally GHC would throw these
384       away after compilation:</para>
385
386       <variablelist>
387         <varlistentry>
388           <term>
389             <option>-keep-hc-files</option>
390             <indexterm><primary><option>-keep-hc-files</option></primary></indexterm>
391           </term>
392           <listitem>
393             <para>Keep intermediate <literal>.hc</literal> files when
394             doing <literal>.hs</literal>-to-<literal>.o</literal>
395             compilations via C (NOTE: <literal>.hc</literal> files
396             aren't generated when using the native code generator, you
397             may need to use <option>-fvia-C</option> to force them
398             to be produced).</para>
399           </listitem>
400         </varlistentry>
401
402         <varlistentry>
403           <term>
404             <option>-keep-s-files</option>
405             <indexterm><primary><option>-keep-s-files</option></primary></indexterm>
406           </term>
407           <listitem>
408             <para>Keep intermediate <literal>.s</literal> files.</para>
409           </listitem>
410         </varlistentry>
411
412         <varlistentry>
413           <term>
414             <option>-keep-raw-s-files</option>
415             <indexterm><primary><option>-keep-raw-s-files</option></primary></indexterm>
416           </term>
417           <listitem>
418             <para>Keep intermediate <literal>.raw-s</literal> files.
419             These are the direct output from the C compiler, before
420             GHC does &ldquo;assembly mangling&rdquo; to produce the
421             <literal>.s</literal> file.  Again, these are not produced
422             when using the native code generator.</para>
423           </listitem>
424         </varlistentry>
425
426         <varlistentry>
427           <term>
428             <option>-keep-tmp-files</option>
429             <indexterm><primary><option>-keep-tmp-files</option></primary></indexterm>
430             <indexterm><primary>temporary files</primary><secondary>keeping</secondary></indexterm>
431           </term>
432           <listitem>
433             <para>Instructs the GHC driver not to delete any of its
434             temporary files, which it normally keeps in
435             <literal>/tmp</literal> (or possibly elsewhere; see <xref
436             linkend="temp-files"/>).  Running GHC with
437             <option>-v</option> will show you what temporary files
438             were generated along the way.</para>
439           </listitem>
440         </varlistentry>
441       </variablelist>
442     </sect2>
443
444     <sect2 id="temp-files">
445       <title>Redirecting temporary files</title>
446
447       <indexterm>
448         <primary>temporary files</primary>
449         <secondary>redirecting</secondary>
450       </indexterm>
451
452       <variablelist>
453         <varlistentry>
454           <term>
455             <option>-tmpdir</option>
456             <indexterm><primary><option>-tmpdir</option></primary></indexterm>
457           </term>
458           <listitem>
459             <para>If you have trouble because of running out of space
460             in <filename>/tmp</filename> (or wherever your
461             installation thinks temporary files should go), you may
462             use the <option>-tmpdir
463             &lt;dir&gt;</option><indexterm><primary>-tmpdir
464             &lt;dir&gt; option</primary></indexterm> option to specify
465             an alternate directory.  For example, <option>-tmpdir
466             .</option> says to put temporary files in the current
467             working directory.</para>
468
469             <para>Alternatively, use your <constant>TMPDIR</constant>
470             environment variable.<indexterm><primary>TMPDIR
471             environment variable</primary></indexterm> Set it to the
472             name of the directory where temporary files should be put.
473             GCC and other programs will honour the
474             <constant>TMPDIR</constant> variable as well.</para>
475
476             <para>Even better idea: Set the
477             <constant>DEFAULT_TMPDIR</constant> make variable when
478             building GHC, and never worry about
479             <constant>TMPDIR</constant> again. (see the build
480             documentation).</para>
481           </listitem>
482         </varlistentry>
483       </variablelist>
484     </sect2>
485
486     <sect2 id="hi-options">
487       <title>Other options related to interface files</title>
488       <indexterm><primary>interface files, options</primary></indexterm>
489
490       <variablelist>
491         <varlistentry>
492           <term>
493             <option>-ddump-hi</option>
494             <indexterm><primary><option>-ddump-hi</option></primary></indexterm>
495           </term>
496           <listitem>
497             <para>Dumps the new interface to standard output.</para>
498           </listitem>
499         </varlistentry>
500
501         <varlistentry>
502           <term>
503             <option>-ddump-hi-diffs</option>
504             <indexterm><primary><option>-ddump-hi-diffs</option></primary></indexterm>
505           </term>
506           <listitem>
507             <para>The compiler does not overwrite an existing
508             <filename>.hi</filename> interface file if the new one is
509             the same as the old one; this is friendly to
510             <command>make</command>.  When an interface does change,
511             it is often enlightening to be informed.  The
512             <option>-ddump-hi-diffs</option> option will make GHC run
513             <command>diff</command> on the old and new
514             <filename>.hi</filename> files.</para>
515           </listitem>
516         </varlistentry>
517
518         <varlistentry>
519           <term>
520             <option>-ddump-minimal-imports</option>
521             <indexterm><primary><option>-ddump-minimal-imports</option></primary></indexterm>
522           </term>
523           <listitem>
524             <para>Dump to the file "M.imports" (where M is the module
525             being compiled) a "minimal" set of import declarations.
526             You can safely replace all the import declarations in
527             "M.hs" with those found in "M.imports".  Why would you
528             want to do that?  Because the "minimal" imports (a) import
529             everything explicitly, by name, and (b) import nothing
530             that is not required.  It can be quite painful to maintain
531             this property by hand, so this flag is intended to reduce
532             the labour.</para>
533           </listitem>
534         </varlistentry>
535
536         <varlistentry>
537           <term>
538             <option>--show-iface</option> <replaceable>file</replaceable>
539             <indexterm><primary><option>--show-iface</option></primary></indexterm>
540           </term>
541           <listitem>
542             <para>Where <replaceable>file</replaceable> is the name of
543             an interface file, dumps the contents of that interface in
544             a human-readable (ish) format.</para>
545           </listitem>
546         </varlistentry>
547       </variablelist>
548     </sect2>
549
550     <sect2 id="recomp">
551       <title>The recompilation checker</title>
552
553       <indexterm><primary>recompilation checker</primary></indexterm>
554
555       <variablelist>
556         <varlistentry>
557           <term>
558             <option>-no-recomp</option>
559             <indexterm><primary><option>-recomp</option></primary></indexterm>
560             <indexterm><primary><option>-no-recomp</option></primary></indexterm>
561           </term>
562           <listitem>
563             <para>Turn off recompilation checking (which is on by
564             default).  Recompilation checking normally stops
565             compilation early, leaving an existing
566             <filename>.o</filename> file in place, if it can be
567             determined that the module does not need to be
568             recompiled.</para>
569           </listitem>
570         </varlistentry>
571       </variablelist>
572
573       <para>In the olden days, GHC compared the newly-generated
574       <filename>.hi</filename> file with the previous version; if they
575       were identical, it left the old one alone and didn't change its
576       modification date.  In consequence, importers of a module with
577       an unchanged output <filename>.hi</filename> file were not
578       recompiled.</para>
579
580       <para>This doesn't work any more.  Suppose module
581       <literal>C</literal> imports module <literal>B</literal>, and
582       <literal>B</literal> imports module <literal>A</literal>.  So
583       changes to module <literal>A</literal> might require module
584       <literal>C</literal> to be recompiled, and hence when
585       <filename>A.hi</filename> changes we should check whether
586       <literal>C</literal> should be recompiled.  However, the
587       dependencies of <literal>C</literal> will only list
588       <literal>B.hi</literal>, not <literal>A.hi</literal>, and some
589       changes to <literal>A</literal> (changing the definition of a
590       function that appears in an inlining of a function exported by
591       <literal>B</literal>, say) may conceivably not change
592       <filename>B.hi</filename> one jot.  So now&hellip;</para>
593
594       <para>GHC keeps a version number on each interface file, and on
595       each type signature within the interface file.  It also keeps in
596       every interface file a list of the version numbers of everything
597       it used when it last compiled the file.  If the source file's
598       modification date is earlier than the <filename>.o</filename>
599       file's date (i.e. the source hasn't changed since the file was
600       last compiled), and the recompilation checking is on, GHC will be
601       clever.  It compares the version numbers on the things it needs
602       this time with the version numbers on the things it needed last
603       time (gleaned from the interface file of the module being
604       compiled); if they are all the same it stops compiling rather
605       early in the process saying &ldquo;Compilation IS NOT
606       required&rdquo;.  What a beautiful sight!</para>
607
608       <para>Patrick Sansom had a workshop paper about how all this is
609       done (though the details have changed quite a bit). <ulink
610       url="mailto:sansom@dcs.gla.ac.uk">Ask him</ulink> if you want a
611       copy.</para>
612
613     </sect2>
614
615     <sect2 id="mutual-recursion">
616       <title>How to compile mutually recursive modules</title>
617
618       <indexterm><primary>module system, recursion</primary></indexterm>
619       <indexterm><primary>recursion, between modules</primary></indexterm>
620
621       <para>GHC supports the compilation of mutually recursive modules.
622       This section explains how.</para>
623
624       <para>Every cycle in the module import graph must be broken by a <filename>hs-boot</filename> file.
625       Suppose that modules <filename>A.hs</filename> and <filename>B.hs</filename> are Haskell source files, 
626       thus:
627 <programlisting>
628 module A where
629     import B( TB(..) )
630     
631     newtype TA = MkTA Int
632     
633     f :: TB -&#62; TA
634     f (MkTB x) = MkTA x
635
636 module B where
637     import {-# SOURCE #-} A( TA(..) )
638     
639     data TB = MkTB !Int
640     
641     g :: TA -&#62; TB
642     g (MkTA x) = MkTB x
643 </programlisting>
644 <indexterm><primary><literal>hs-boot</literal>
645       files</primary></indexterm> <indexterm><primary>importing,
646       <literal>hi-boot</literal> files</primary></indexterm>
647 Here <filename>A</filename> imports <filename>B</filename>, but <filename>B</filename> imports
648 <filename>A</filename> with a <literal>{-# SOURCE #-}</literal> pragma, which breaks the
649 circular dependency.  For every module <filename>A.hs</filename> that is <literal>{-# SOURCE #-}</literal>-imported
650 in this way there must exist a souce file <literal>A.hs-boot</literal>.  This file contains an abbreviated
651 version of <filename>A.hs</filename>, thus:
652 <programlisting>
653 module A where
654     newtype TA = MkTA Int
655 </programlisting>
656 </para>
657 <para>To compile these three files, issue the following commands:
658 <programlisting>
659   ghc -c A.hs-boot    -- Poduces A.hi-boot, A.o-boot
660   ghc -c B.hs         -- Consumes A.hi-boot, produces B.hi, B.o
661   ghc -c A.hs         -- Consumes B.hi, produces A.hi, A.o
662   ghc -o foo A.o B.o  -- Linking the program
663 </programlisting>
664 </para>
665 <para>There are several points to note here:
666 <itemizedlist>
667 <listitem>
668   <para>The file <filename>A.hs-boot</filename> is a programmer-written source file.
669   It must live in the same directory as its parent source file <filename>A.hs</filename>.
670   Currently, if you use a literate source file <filename>A.lhs</filename> you must
671   also use a literate boot file, <filename>A.lhs-boot</filename>; and vice versa.
672   </para></listitem>
673
674 <listitem><para>
675   A <filename>hs-boot</filename> file is compiled by GHC, just like a <filename>hs</filename> file:
676 <programlisting>
677   ghc -c A.hs-boot
678 </programlisting>
679 When a hs-boot file <filename>A.hs-boot</filename> 
680    is compiled, it is checked for scope and type errors.
681    When its parent module <filename>A.hs</filename> is compiled, the two are compared, and
682    an error is reported if the two are inconsistent.
683    </para></listitem>
684    
685 <listitem><para> Just as compiling <filename>A.hs</filename> produces an
686 interface file <filename>A.hi</filename>, and an object file
687 <filename>A.o</filename>, so compiling <filename>A.hs-boot</filename>
688 produces an interface file
689 <filename>A.hi-boot</filename>, and an pseudo-object file
690 <filename>A.o-boot</filename>:  
691 <itemizedlist>
692 <listitem><para>
693 The pseudo-object file <filename>A.o-boot</filename> is empty (don't link it!), but it is
694 very useful when using a Makefile, to record when the <filename>A.hi-boot</filename> was
695 last brought up to date (see <xref linkend="using-make"/>).
696 </para>
697 </listitem>
698
699   <listitem><para> The <filename>hi-boot</filename> generated by compiling a <filename>hs-boot</filename>
700   file is in the same machine-generated binary format as any other
701   GHC-generated interface file (e.g. <filename>B.hi</filename>).
702   You can display its contents with <command>ghc --show-iface</command>. If you 
703   specify a directory for interface files, the <option>-ohidir</option> flag, then that affects
704   <filename>hi-boot</filename> files too.</para></listitem>b
705 </itemizedlist>
706 </para></listitem>
707
708    <listitem><para> If hs-boot files are considered distinct from their parent source
709    files, and if a <literal>{-# SOURCE #-}</literal> import is considered to refer to the
710    hs-boot file, then the module import graph must have no cycles.  The <command>ghc -M</command>
711    will report an error if a cycle is found.
712    </para></listitem>
713 </itemizedlist>
714 </para>
715 <para>
716 A hs-boot file need only contain the bare
717       minimum of information needed to get the bootstrapping process
718       started.  For example, it doesn't need to contain declarations
719       for <emphasis>everything</emphasis> that module
720       <literal>A</literal> exports, only the things required by the
721       module that imports <literal>A</literal> recursively.</para>
722 <para>A hs-boot file is written in a subset of Haskell:
723 <itemizedlist>
724 <listitem><para> The module header (including the export list), and import statements, are exactly as in
725 Haskell, and so are the scoping rules.  
726    Hence, to mention a non-Prelude type or class, you must import it.</para></listitem>
727    
728 <listitem><para> There must be no value declarations, but there can be type signatures for
729 values.  For example:
730 <programlisting>
731   double :: Int -&#62; Int
732 </programlisting>
733 </para></listitem>
734 <listitem><para> Fixity declarations are exactly as in Haskell.</para></listitem>
735 <listitem><para> Type synonym declarations are exactly as in Haskell.</para></listitem>
736 <listitem><para> A data type declaration can either be given in full, exactly as in Haskell, or it 
737 can be given abstractly, by omitting the '=' sign and everything that follows.  For example:
738 <programlisting>
739   data T a b
740 </programlisting>
741             In a <emphasis>source</emphasis> program
742           this would declare TA to have no constructors (a GHC extension: see <xref linkend="nullary-types"/>),
743           but in an hi-boot file it means "I don't know or care what the constructors are".
744             This is the most common form of data type declaration, because it's easy to get right.
745           You <emphasis>can</emphasis> also write out the constructors but, if you do so, you must write
746           it out precisely as in its real definition.</para>
747           <para>
748             If you do not write out the constructors, you may need to give a kind 
749             annotation (<xref linkend="sec-kinding"/>), to tell
750             GHC the kind of the type variable, if it is not "*".  (In source files, this is worked out
751             from the way the type variable is used in the constructors.)  For example:
752 <programlisting>
753   data R (x :: * -&#62; *) y
754 </programlisting>
755 </para></listitem>
756 <listitem><para> Class declarations is exactly as in Haskell, except that you may not put
757 default method declarations.  You can also omit all the class methods entirely.
758 </para></listitem>
759 <listitem><para> Do not include instance declarations. There is a complication to do with
760 how the dictionary functions are named.  It may well work, but it's not a well-tested feature.
761  </para></listitem>
762 </itemizedlist>
763 </para>
764     </sect2>
765
766
767     <sect2 id="using-make">
768       <title>Using <command>make</command></title>
769
770       <indexterm><primary><literal>make</literal></primary></indexterm>
771
772       <para>It is reasonably straightforward to set up a
773       <filename>Makefile</filename> to use with GHC, assuming you name
774       your source files the same as your modules.  Thus:</para>
775
776 <programlisting>
777 HC      = ghc
778 HC_OPTS = -cpp $(EXTRA_HC_OPTS)
779
780 SRCS = Main.lhs Foo.lhs Bar.lhs
781 OBJS = Main.o   Foo.o   Bar.o
782
783 .SUFFIXES : .o .hs .hi .lhs .hc .s
784
785 cool_pgm : $(OBJS)
786         rm -f $@
787         $(HC) -o $@ $(HC_OPTS) $(OBJS)
788
789 # Standard suffix rules
790 .o.hi:
791         @:
792
793 .lhs.o:
794         $(HC) -c $&#60; $(HC_OPTS)
795
796 .hs.o:
797         $(HC) -c $&#60; $(HC_OPTS)
798
799 .o-boot.hi-boot:
800         @:
801
802 .lhs-boot.o-boot:
803         $(HC) -c $&#60; $(HC_OPTS)
804
805 .hs-boot.o-boot:
806         $(HC) -c $&#60; $(HC_OPTS)
807
808 # Inter-module dependencies
809 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
810 Main.o Main.hc Main.s : Foo.hi Baz.hi   # Main imports Foo and Baz
811 </programlisting>
812
813       <para>(Sophisticated <command>make</command> variants may
814       achieve some of the above more elegantly.  Notably,
815       <command>gmake</command>'s pattern rules let you write the more
816       comprehensible:</para>
817
818 <programlisting>
819 %.o : %.lhs
820         $(HC) -c $&#60; $(HC_OPTS)
821 </programlisting>
822
823       <para>What we've shown should work with any
824       <command>make</command>.)</para>
825
826       <para>Note the cheesy <literal>.o.hi</literal> rule: It records
827       the dependency of the interface (<filename>.hi</filename>) file
828       on the source.  The rule says a <filename>.hi</filename> file
829       can be made from a <filename>.o</filename> file by
830       doing&hellip;nothing.  Which is true.</para>
831       <para> Note that the suffix rules are all repeated twice, once
832       for normal Haskell source files, and once for <filename>hs-boot</filename>
833       files (see <xref linkend="mutual-recursion"/>).</para>
834
835       <para>Note also the inter-module dependencies at the end of the
836       Makefile, which take the form
837
838 <programlisting>
839 Foo.o Foo.hc Foo.s    : Baz.hi          # Foo imports Baz
840 </programlisting>
841
842       They tell <command>make</command> that if any of
843       <literal>Foo.o</literal>, <literal>Foo.hc</literal> or
844       <literal>Foo.s</literal> have an earlier modification date than
845       <literal>Baz.hi</literal>, then the out-of-date file must be
846       brought up to date.  To bring it up to date,
847       <literal>make</literal> looks for a rule to do so; one of the
848       preceding suffix rules does the job nicely.  These dependencies
849       can be generated automatically by <command>ghc</command>; see 
850       <xref linkend="sec-makefile-dependencies"/></para>
851
852  </sect2>
853       <sect2 id="sec-makefile-dependencies">
854         <title>Dependency generation</title>
855         <indexterm><primary>dependencies in Makefiles</primary></indexterm>
856         <indexterm><primary>Makefile dependencies</primary></indexterm>
857
858         <para>Putting inter-dependencies of the form <literal>Foo.o :
859         Bar.hi</literal> into your <filename>Makefile</filename> by
860         hand is rather error-prone.  Don't worry, GHC has support for
861         automatically generating the required dependencies.  Add the
862         following to your <filename>Makefile</filename>:</para>
863
864 <programlisting>
865 depend :
866         ghc -M $(HC_OPTS) $(SRCS)
867 </programlisting>
868
869         <para>Now, before you start compiling, and any time you change
870         the <literal>imports</literal> in your program, do
871         <command>make depend</command> before you do <command>make
872         cool&lowbar;pgm</command>.  The command <command>ghc -M</command> will
873         append the needed dependencies to your
874         <filename>Makefile</filename>.</para>
875
876         <para>In general, <command>ghc -M Foo</command> does the following.
877         For each module <literal>M</literal> in the set 
878         <literal>Foo</literal> plus all its imports (transitively),
879         it adds to the Makefile:
880         <itemizedlist>
881         <listitem><para>A line recording the dependence of the object file on the source file.
882 <programlisting>
883 M.o : M.hs
884 </programlisting>
885 (or <literal>M.lhs</literal> if that is the filename you used).
886        </para></listitem>
887        <listitem><para> For each import declaration <literal>import X</literal> in <literal>M</literal>,
888        a line recording the dependence of <literal>M</literal> on <literal>X</literal>:
889 <programlisting>
890 M.o : X.hi
891 </programlisting></para></listitem>
892        <listitem><para> For each import declaration <literal>import {-# SOURCE #-} X</literal> in <literal>M</literal>,
893        a line recording the dependence of <literal>M</literal> on <literal>X</literal>:
894 <programlisting>
895 M.o : X.hi-boot
896 </programlisting>
897        (See <xref linkend="mutual-recursion"/> for details of
898        <literal>hi-boot</literal> style interface files.)
899       </para></listitem>
900         </itemizedlist> 
901         If <literal>M</literal> imports multiple modules, then there will
902        be multiple lines with <filename>M.o</filename> as the
903        target.</para>
904        <para>There is no need to list all of the source files as arguments to the <command>ghc -M</command> command;
905        <command>ghc</command> traces the dependencies, just like <command>ghc --make</command>
906        (a new feature in GHC 6.4).</para>
907
908         <para>By default, <command>ghc -M</command> generates all the
909         dependencies, and then concatenates them onto the end of
910         <filename>makefile</filename> (or
911         <filename>Makefile</filename> if <filename>makefile</filename>
912         doesn't exist) bracketed by the lines "<literal>&num; DO NOT
913         DELETE: Beginning of Haskell dependencies</literal>" and
914         "<literal>&num; DO NOT DELETE: End of Haskell
915         dependencies</literal>".  If these lines already exist in the
916         <filename>makefile</filename>, then the old dependencies are
917         deleted first.</para>
918
919         <para>Don't forget to use the same <option>-package</option>
920         options on the <literal>ghc -M</literal> command line as you
921         would when compiling; this enables the dependency generator to
922         locate any imported modules that come from packages.  The
923         package modules won't be included in the dependencies
924         generated, though (but see the
925         <option>&ndash;&ndash;include-pkg-deps</option> option below).</para>
926
927         <para>The dependency generation phase of GHC can take some
928         additional options, which you may find useful.  For historical
929         reasons, each option passed to the dependency generator from
930         the GHC command line must be preceded by
931         <literal>-optdep</literal>.  For example, to pass <literal>-f
932         .depend</literal> to the dependency generator, you say
933
934 <screen>
935 ghc -M -optdep-f -optdep.depend ...
936 </screen>
937
938         The options which affect dependency generation are:</para>
939
940         <variablelist>
941           <varlistentry>
942             <term><option>-w</option></term>
943             <listitem>
944               <para>Turn off warnings about interface file shadowing.</para>
945             </listitem>
946           </varlistentry>
947
948           <varlistentry>
949             <term><option>-v2</option></term>
950             <listitem>
951               <para>Print a full list of the module depenencies to stdout.
952                     (This is the standard verbosity flag, so the list will
953               also be displayed with <option>-v3</option> and
954               <option>-v4</option>;
955               <xref linkend ="options-help"/>.)</para>
956             </listitem>
957           </varlistentry>
958
959           <varlistentry>
960             <term><option>-f</option> <replaceable>file</replaceable></term>
961             <listitem>
962               <para>Use <replaceable>file</replaceable> as the makefile,
963               rather than <filename>makefile</filename> or
964               <filename>Makefile</filename>.  If
965               <replaceable>file</replaceable> doesn't exist,
966               <command>mkdependHS</command> creates it.  We often use
967               <option>-f .depend</option> to put the dependencies in
968               <filename>.depend</filename> and then
969               <command>include</command> the file
970               <filename>.depend</filename> into
971               <filename>Makefile</filename>.</para>
972             </listitem>
973           </varlistentry>
974
975 <!-- Retired with the move away from 'mkdependHS'.
976           <varlistentry>
977             <term><option>-o &lt;osuf&gt;</option></term>
978             <listitem>
979               <para>Use <filename>.&lt;osuf&gt;</filename> as the
980               "target file" suffix ( default: <literal>o</literal>).
981               Multiple <option>-o</option> flags are permitted
982               (GHC2.05 onwards).  Thus "<option>-o hc -o o</option>"
983               will generate dependencies for <filename>.hc</filename>
984               and <filename>.o</filename> files.</para>
985             </listitem>
986           </varlistentry>
987 -->
988           <varlistentry>
989             <term><option>-s &lt;suf&gt;</option></term>
990             <listitem>
991               <para>Make extra dependencies that declare that files
992               with suffix
993               <filename>.&lt;suf&gt;&lowbar;&lt;osuf&gt;</filename>
994               depend on interface files with suffix
995               <filename>.&lt;suf&gt;&lowbar;hi</filename>, or (for
996               <literal>&lcub;-&num; SOURCE &num;-&rcub;</literal>
997               imports) on <filename>.hi-boot</filename>.  Multiple
998               <option>-s</option> flags are permitted.  For example,
999               <option>-o hc -s a -s b</option> will make dependencies
1000               for <filename>.hc</filename> on
1001               <filename>.hi</filename>,
1002               <filename>.a&lowbar;hc</filename> on
1003               <filename>.a&lowbar;hi</filename>, and
1004               <filename>.b&lowbar;hc</filename> on
1005               <filename>.b&lowbar;hi</filename>.  (Useful in
1006               conjunction with NoFib "ways".)</para>
1007             </listitem>
1008           </varlistentry>
1009
1010           <varlistentry>
1011             <term><option>&ndash;&ndash;exclude-module=&lt;file&gt;</option></term>
1012             <listitem>
1013               <para>Regard <filename>&lt;file&gt;</filename> as
1014               "stable"; i.e., exclude it from having dependencies on
1015               it.</para>
1016             </listitem>
1017           </varlistentry>
1018
1019           <varlistentry>
1020             <term><option>-x</option></term>
1021             <listitem>
1022               <para>same as <option>&ndash;&ndash;exclude-module</option></para>
1023             </listitem>
1024           </varlistentry>
1025
1026           <varlistentry>
1027             <term><option>&ndash;&ndash;exclude-directory=&lt;dirs&gt;</option></term>
1028             <listitem>
1029               <para>Regard the colon-separated list of directories
1030               <filename>&lt;dirs&gt;</filename> as containing stable,
1031               don't generate any dependencies on modules
1032               therein.</para>
1033             </listitem>
1034           </varlistentry>
1035
1036           <varlistentry>
1037             <term><option>&ndash;&ndash;include-module=&lt;file&gt;</option></term>
1038             <listitem>
1039               <para>Regard <filename>&lt;file&gt;</filename> as not
1040               "stable"; i.e., generate dependencies on it (if
1041               any). This option is normally used in conjunction with
1042               the <option>&ndash;&ndash;exclude-directory</option> option.</para>
1043             </listitem>
1044           </varlistentry>
1045
1046           <varlistentry>
1047             <term><option>&ndash;&ndash;include-pkg-deps</option></term>
1048             <listitem>
1049               <para>Regard modules imported from packages as unstable,
1050               i.e., generate dependencies on any imported package modules
1051               (including <literal>Prelude</literal>, and all other
1052               standard Haskell libraries).  Dependencies are not traced
1053               recursively into packages; dependencies are only generated for
1054               home-package modules on external-package modules directly imported
1055               by the home package module.
1056               This option is normally
1057               only used by the various system libraries.</para>
1058             </listitem>
1059           </varlistentry>
1060         </variablelist>
1061
1062     </sect2>
1063
1064     <sect2 id="orphan-modules">
1065       <title>Orphan modules and instance declarations</title>
1066
1067 <para> Haskell specifies that when compiling module M, any instance
1068 declaration in any module "below" M is visible.  (Module A is "below"
1069 M if A is imported directly by M, or if A is below a module that M imports directly.)
1070 In principle, GHC must therefore read the interface files of every module below M,
1071 just in case they contain an instance declaration that matters to M.  This would
1072 be a disaster in practice, so GHC tries to be clever. </para>
1073
1074 <para>In particular, if an instance declaration is in the same module as the definition
1075 of any type or class mentioned in the head of the instance declaration, then
1076 GHC has to visit that interface file anyway.  Example:</para>
1077 <programlisting>
1078   module A where
1079     instance C a =&gt; D (T a) where ...
1080     data T a = ...
1081 </programlisting>
1082 <para> The instance declaration is only relevant if the type T is in use, and if
1083 so, GHC will have visited A's interface file to find T's definition. </para>
1084
1085 <para> The only problem comes when a module contains an instance declaration
1086 and GHC has no other reason for visiting the module.  Example:
1087 <programlisting>
1088   module Orphan where
1089     instance C a =&gt; D (T a) where ...
1090     class C a where ...
1091 </programlisting>
1092 Here, neither D nor T is declared in module Orphan.
1093 We call such modules ``orphan modules'',
1094 defined thus:</para>
1095 <itemizedlist>
1096   <listitem> <para> An <emphasis>orphan module</emphasis>
1097   <indexterm><primary>orphan module</primary></indexterm>
1098   contains at least one <emphasis>orphan instance</emphasis> or at
1099   least one <emphasis>orphan rule</emphasis>.</para> </listitem>
1100
1101   <listitem><para> An instance declaration in a module M is an <emphasis>orphan instance</emphasis> if
1102   <indexterm><primary>orphan instance</primary></indexterm>
1103   none of the type constructors
1104   or classes mentioned in the instance head (the part after the ``<literal>=&gt;</literal>'') are declared
1105   in M.</para>
1106
1107   <para> Only the instance head counts.  In the example above, it is not good enough for C's declaration
1108   to be in module A; it must be the declaration of D or T.</para>
1109   </listitem>
1110
1111   <listitem><para> A rewrite rule in a module M is an <emphasis>orphan rule</emphasis>
1112   <indexterm><primary>orphan rule</primary></indexterm>
1113   if none of the variables, type constructors,
1114   or classes that are free in the left hand side of the rule are declared in M.
1115   </para> </listitem>
1116  </itemizedlist>
1117
1118
1119 <para> GHC identifies orphan modules, and visits the interface file of
1120 every orphan module below the module being compiled.  This is usually
1121 wasted work, but there is no avoiding it.  You should therefore do
1122 your best to have as few orphan modules as possible.
1123
1124 </para>
1125
1126 <para> You can identify an orphan module by looking in its interface
1127 file, <filename>M.hi</filename>, using the
1128 <option>--show-iface</option>.  If there is a ``!'' on the first line,
1129 GHC considers it an orphan module.
1130 </para>
1131 </sect2>
1132
1133   </sect1>
1134
1135 <!-- Emacs stuff:
1136      ;;; Local Variables: ***
1137      ;;; mode: xml ***
1138      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter" "sect1") ***
1139      ;;; End: ***
1140  -->