<indexterm><primary>using GHC</primary></indexterm>
<sect1>
+ <title>Getting started: compiling programs</title>
+
+ <para>
+ In this chapter you'll find a complete reference to the GHC
+ command-line syntax, including all 400+ flags. It's a large and
+ complex system, and there are lots of details, so it can be
+ quite hard to figure out how to get started. With that in mind,
+ this introductory section provides a quick introduction to the
+ basic usage of GHC for compiling a Haskell program, before the
+ following sections dive into the full syntax.
+ </para>
+
+ <para>
+ Let's create a Hello World program, and compile and run it.
+ First, create a file <filename>hello.hs</filename> containing
+ the Haskell code:
+ </para>
+
+<programlisting>
+main = putStrLn "Hello, World!"
+</programlisting>
+
+ <para>To compile the program, use GHC like this:</para>
+
+<screen>
+$ ghc hello.hs</screen>
+
+ <para>(where <literal>$</literal> represents the prompt: don't
+ type it). GHC will compile the source
+ file <filename>hello.hs</filename>, producing
+ an <firstterm>object
+ file</firstterm> <filename>hello.o</filename> and
+ an <firstterm>interface
+ file</firstterm> <filename>hello.hi</filename>, and then it
+ will link the object file to the libraries that come with GHC
+ to produce an executable called <filename>hello</filename> on
+ Unix/Linux/Mac, or <filename>hello.exe</filename> on
+ Windows.</para>
+
+ <para>
+ By default GHC will be very quiet about what it is doing, only
+ printing error messages. If you want to see in more detail
+ what's going on behind the scenes, add <option>-v</option> to
+ the command line.
+ </para>
+
+ <para>
+ Then we can run the program like this:
+ </para>
+
+<screen>
+$ ./hello
+Hello World!</screen>
+
+ <para>
+ If your program contains multiple modules, then you only need to
+ tell GHC the name of the source file containing
+ the <filename>Main</filename> module, and GHC will examine
+ the <literal>import</literal> declarations to find the other
+ modules that make up the program and find their source files.
+ This means that, with the exception of
+ the <literal>Main</literal> module, every source file should be
+ named after the module name that it contains (with dots replaced
+ by directory separators). For example, the
+ module <literal>Data.Person</literal> would be in the
+ file <filename>Data/Person.hs</filename> on Unix/Linux/Mac,
+ or <filename>Data\Person.hs</filename> on Windows.
+ </para>
+ </sect1>
+
+ <sect1>
<title>Options overview</title>
<para>GHC's behaviour is controlled by
<varlistentry>
<term>Mode flags</term>
<listitem>
- <para>For example, <option>--make</option> or <option>-E</option>.
+ <para>For example, <option>––make</option> or <option>-E</option>.
There may only be a single mode flag on the command line. The
available modes are listed in <xref linkend="modes"/>.</para>
</listitem>
</varlistentry>
<varlistentry>
+ <term><filename>.ll</filename></term>
+ <listitem>
+ <para>An llvm-intermediate-language source file, usually
+ produced by the compiler.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
+ <term><filename>.bc</filename></term>
+ <listitem>
+ <para>An llvm-intermediate-language bitcode file, usually
+ produced by the compiler.</para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><filename>.s</filename></term>
<listitem>
<para>An assembly-language source file, usually produced by
<sect1 id="modes">
<title>Modes of operation</title>
- <para>GHC's behaviour is firstly controlled by a mode flag. Only
- one of these flags may be given, but it does not necessarily need
- to be the first option on the command-line. The available modes
- are:</para>
+ <para>
+ GHC's behaviour is firstly controlled by a mode flag. Only one
+ of these flags may be given, but it does not necessarily need to
+ be the first option on the command-line.
+ </para>
+
+ <para>
+ If no mode flag is present, then GHC will enter make mode
+ (<xref linkend="make-mode" />) if there are any Haskell source
+ files given on the command line, or else it will link the
+ objects named on the command line to produce an executable.
+ </para>
+
+ <para>The available mode flags are:</para>
<variablelist>
<varlistentry>
<varlistentry>
<term>
- <cmdsynopsis><command>ghc --make</command>
+ <cmdsynopsis><command>ghc ––make</command>
</cmdsynopsis>
<indexterm><primary>make mode</primary></indexterm>
<indexterm><primary><option>––make</option></primary></indexterm>
likely to be much easier, and faster, than using
<command>make</command>. Make mode is described in <xref
linkend="make-mode"/>.</para>
+
+ <para>
+ This mode is the default if there are any Haskell
+ source files mentioned on the command line, and in this case
+ the <option>––make</option> option can be omitted.
+ </para>
</listitem>
</varlistentry>
<varlistentry>
<term>
<cmdsynopsis>
+ <command>ghc --supported-extensions</command>
<command>ghc --supported-languages</command>
</cmdsynopsis>
- <indexterm><primary><option>––supported-languages</option></primary></indexterm>
+ <indexterm><primary><option>––supported-extensions</option></primary><primary><option>––supported-languages</option></primary></indexterm>
</term>
<listitem>
<para>Print the supported language extensions.</para>
<indexterm><primary><option>––make</option></primary></indexterm>
<indexterm><primary>separate compilation</primary></indexterm>
- <para>When given the <option>––make</option> option,
- GHC will build a multi-module Haskell program by following
+ <para>In this mode, GHC will build a multi-module Haskell program by following
dependencies from one or more root modules (usually just
<literal>Main</literal>). For example, if your
<literal>Main</literal> module is in a file called
ghc ––make Main.hs
</screen>
- <para>The command line may contain any number of source file
- names or module names; GHC will figure out all the modules in
- the program by following the imports from these initial modules.
- It will then attempt to compile each module which is out of
- date, and finally, if there is a <literal>Main</literal> module,
- the program will also be linked into an executable.</para>
+ <para>
+ In fact, GHC enters make mode automatically if there are any
+ Haskell source files on the command line and no other mode is
+ specified, so in this case we could just type
+ </para>
+
+<screen>
+ghc Main.hs
+</screen>
+
+ <para>Any number of source file names or module names may be
+ specified; GHC will figure out all the modules in the program by
+ following the imports from these initial modules. It will then
+ attempt to compile each module which is out of date, and
+ finally, if there is a <literal>Main</literal> module, the
+ program will also be linked into an executable.</para>
<para>The main advantages to using <literal>ghc
––make</literal> over traditional
suspicious code. The warnings that are
<emphasis>not</emphasis> enabled by <option>-Wall</option>
are
- <option>-fwarn-simple-patterns</option>,
<option>-fwarn-tabs</option>,
+ <option>-fwarn-incomplete-uni-patterns</option>,
<option>-fwarn-incomplete-record-updates</option>,
<option>-fwarn-monomorphism-restriction</option>,
- <option>-fwarn-unused-do-bind</option>, and
+ <option>-fwarn-unrecognised-pragmas</option>,
+ <option>-fwarn-auto-orphans</option>,
<option>-fwarn-implicit-prelude</option>.</para>
</listitem>
</varlistentry>
is bound in a way that looks lazy, e.g.
<literal>where (I# x) = ...</literal>. Use
<literal>where !(I# x) = ...</literal> instead. This will be an
- error, rather than a warning, in GHC 6.14.
+ error, rather than a warning, in GHC 7.2.
</para>
</listitem>
</varlistentry>
</varlistentry>
<varlistentry>
+ <term><option>-fwarn-identities</option>:</term>
+ <listitem>
+ <indexterm><primary><option>-fwarn-identities</option></primary></indexterm>
+ <para>Causes the compiler to emit a warning when a Prelude numeric
+ conversion converts a type T to the same type T; such calls
+ are probably no-ops and can be omitted. The functions checked for
+ are: <literal>toInteger</literal>,
+ <literal>toRational</literal>,
+ <literal>fromIntegral</literal>,
+ and <literal>realToFrac</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>-fwarn-implicit-prelude</option>:</term>
<listitem>
<indexterm><primary><option>-fwarn-implicit-prelude</option></primary></indexterm>
</varlistentry>
<varlistentry>
- <term><option>-fwarn-incomplete-patterns</option>:</term>
+ <term><option>-fwarn-incomplete-patterns</option>,
+ <option>-fwarn-incomplete-uni-patterns</option>:
+ </term>
<listitem>
<indexterm><primary><option>-fwarn-incomplete-patterns</option></primary></indexterm>
+ <indexterm><primary><option>-fwarn-incomplete-uni-patterns</option></primary></indexterm>
<indexterm><primary>incomplete patterns, warning</primary></indexterm>
<indexterm><primary>patterns, incomplete</primary></indexterm>
- <para>Similarly for incomplete patterns, the function
+ <para>The option <option>-fwarn-incomplete-patterns</option> warns
+ about places where
+ a pattern-match might fail at runtime.
+ The function
<function>g</function> below will fail when applied to
non-empty lists, so the compiler will emit a warning about
this when <option>-fwarn-incomplete-patterns</option> is
- enabled.</para>
-
+ enabled.
<programlisting>
g [] = 2
</programlisting>
-
- <para>This option isn't enabled by default because it can be
+ This option isn't enabled by default because it can be
a bit noisy, and it doesn't always indicate a bug in the
program. However, it's generally considered good practice
- to cover all the cases in your functions.</para>
+ to cover all the cases in your functions, and it is switched
+ on by <option>-W</option>.</para>
+
+ <para>The flag <option>-fwarn-incomplete-uni-patterns</option> is
+ similar, except that it
+ applies only to lambda-expressions and pattern bindings, constructs
+ that only allow a single pattern:
+<programlisting>
+h = \[] -> 2
+Just k = f y
+</programlisting>
+ </para>
</listitem>
</varlistentry>
</varlistentry>
<varlistentry>
+ <term>
+ <option>-fwarn-missing-import-lists</option>:
+ <indexterm><primary><option>-fwarn-import-lists</option></primary></indexterm>
+ <indexterm><primary>missing import lists, warning</primary></indexterm>
+ <indexterm><primary>import lists, missing</primary></indexterm>
+ </term>
+ <listitem>
+
+ <para>This flag warns if you use an unqualified
+ <literal>import</literal> declaration
+ that does not explicitly list the entities brought into scope. For
+ example
+ </para>
+<programlisting>
+module M where
+ import X( f )
+ import Y
+ import qualified Z
+ p x = f x x
+</programlisting>
+ <para>
+ The <option>-fwarn-import-lists</option> flag will warn about the import
+ of <literal>Y</literal> but not <literal>X</literal>
+ If module <literal>Y</literal> is later changed to export (say) <literal>f</literal>,
+ then the reference to <literal>f</literal> in <literal>M</literal> will become
+ ambiguous. No warning is produced for the import of <literal>Z</literal>
+ because extending <literal>Z</literal>'s exports would be unlikely to produce
+ ambiguity in <literal>M</literal>.
+ </para>
+ </listitem>
+ </varlistentry>
+
+ <varlistentry>
<term><option>-fwarn-missing-methods</option>:</term>
<listitem>
<indexterm><primary><option>-fwarn-missing-methods</option></primary></indexterm>
</varlistentry>
<varlistentry>
- <term><option>-fwarn-simple-patterns</option>:</term>
- <listitem>
- <indexterm><primary><option>-fwarn-simple-patterns</option></primary>
- </indexterm>
- <para>Causes the compiler to warn about lambda-bound
- patterns that can fail, eg. <literal>\(x:xs)->...</literal>.
- Normally, these aren't treated as incomplete patterns by
- <option>-fwarn-incomplete-patterns</option>.</para>
- <para>“Lambda-bound patterns” includes all places where there is a single pattern,
- including list comprehensions and do-notation. In these cases, a pattern-match
- failure is quite legitimate, and triggers filtering (list comprehensions) or
- the monad <literal>fail</literal> operation (monads). For example:
- <programlisting>
- f :: [Maybe a] -> [a]
- f xs = [y | Just y <- xs]
- </programlisting>
- Switching on <option>-fwarn-simple-patterns</option> will elicit warnings about
- these probably-innocent cases, which is why the flag is off by default. </para>
- </listitem>
- </varlistentry>
-
- <varlistentry>
<term><option>-fwarn-tabs</option>:</term>
<listitem>
<indexterm><primary><option>-fwarn-tabs</option></primary></indexterm>
e.g., the ‘default default’ for Haskell 1.4 caused the
otherwise unconstrained value <constant>1</constant> to be
given the type <literal>Int</literal>, whereas Haskell 98
+ and later
defaults it to <literal>Integer</literal>. This may lead to
differences in performance and behaviour, hence the
usefulness of being non-silent about this.</para>
<option>-O</option>.</para>
</listitem>
</varlistentry>
-
- <varlistentry>
- <term>
- <option>-Ofile <file></option>:
- <indexterm><primary>-Ofile <file> option</primary></indexterm>
- <indexterm><primary>optimising, customised</primary></indexterm>
- </term>
- <listitem>
- <para>(NOTE: not supported since GHC 4.x. Please ask if
- you're interested in this.)</para>
-
- <para>For those who need <emphasis>absolute</emphasis>
- control over <emphasis>exactly</emphasis> what options are
- used (e.g., compiler writers, sometimes :-), a list of
- options can be put in a file and then slurped in with
- <option>-Ofile</option>.</para>
-
- <para>In that file, comments are of the
- <literal>#</literal>-to-end-of-line variety; blank
- lines and most whitespace is ignored.</para>
-
- <para>Please ask if you are baffled and would like an
- example of <option>-Ofile</option>!</para>
- </listitem>
- </varlistentry>
</variablelist>
<para>We don't use a <option>-O*</option> flag for day-to-day
work. We use <option>-O</option> to get respectable speed;
e.g., when we want to measure something. When we want to go for
- broke, we tend to use <option>-O2 -fvia-C</option> (and we go for
+ broke, we tend to use <option>-O2</option> (and we go for
lots of coffee breaks).</para>
<para>The easiest way to see what <option>-O</option> (etc.)
</para>
</listitem>
</varlistentry>
- <varlistentry>
- <term><option>-qw</option></term>
- <indexterm><primary><option>-qw</option></primary><secondary>RTS
- option</secondary></indexterm>
- <listitem>
- <para>Migrate a thread to the current CPU when it is woken
- up. Normally when a thread is woken up after being
- blocked it will be scheduled on the CPU it was running on
- last; this option allows the thread to immediately migrate
- to the CPU that unblocked it.</para>
-
- <para>The rationale for allowing this eager migration is
- that it tends to move threads that are communicating with
- each other onto the same CPU; however there are
- pathalogical situations where it turns out to be a poor
- strategy. Depending on the communication pattern in your
- program, it may or may not be a good idea.</para>
- </listitem>
- </varlistentry>
</variablelist>
</sect2>
<term><option>-msse2</option>:</term>
<listitem>
<para>
- (x86 only, added in GHC 6.14.1) Use the SSE2 registers and
+ (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
instruction set to implement floating point operations
when using the native code generator. This gives a
substantial performance improvement for floating point,
</listitem>
</varlistentry>
- <varlistentry>
- <term><option>-monly-[32]-regs</option>:</term>
- <listitem>
- <para>(x86 only)<indexterm><primary>-monly-N-regs
- option (iX86 only)</primary></indexterm> GHC tries to
- “steal” four registers from GCC, for performance
- reasons; it almost always works. However, when GCC is
- compiling some modules with four stolen registers, it will
- crash, probably saying:
-
-<screen>
-Foo.hc:533: fixed or forbidden register was spilled.
-This may be due to a compiler bug or to impossible asm
-statements or clauses.
-</screen>
-
- Just give some registers back with
- <option>-monly-N-regs</option>. Try `3' first, then `2'.
- If `2' doesn't work, please report the bug to us.</para>
- </listitem>
- </varlistentry>
</variablelist>
</sect1>
<para>GHC can dump its optimized intermediate code (said to be in “Core” format)
to a file as a side-effect of compilation. Non-GHC back-end tools can read and process Core files; these files have the suffix
- <filename>.hcr</filename>. The Core format is described in <ulink url="../ext-core/core.pdf">
+ <filename>.hcr</filename>. The Core format is described in <ulink url="../../core.pdf">
<citetitle>An External Representation for the GHC Core Language</citetitle></ulink>,
and sample tools
- for manipulating Core files (in Haskell) are in the GHC source distribution
- directory under <literal>utils/ext-core</literal>.
- Note that the format of <literal>.hcr</literal>
+ for manipulating Core files (in Haskell) are available in the
+ <ulink url="http://hackage.haskell.org/package/extcore">extcore package on Hackage</ulink>. Note that the format of <literal>.hcr</literal>
files is <emphasis>different</emphasis> from the Core output format that GHC generates
for debugging purposes (<xref linkend="options-debugging"/>), though the two formats appear somewhat similar.</para>
<!-- Emacs stuff:
;;; Local Variables: ***
- ;;; mode: xml ***
;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
;;; End: ***
-->