using.rst 35.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
.. _using-ghc:

Using GHC
=========

.. index::
   single: GHC, using
   single: using GHC

Getting started: compiling programs
-----------------------------------

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.

Let's create a Hello World program, and compile and run it. First,
21
create a file :file:`hello.hs` containing the Haskell code: ::
22 23 24

    main = putStrLn "Hello, World!"

25 26 27
To compile the program, use GHC like this:

.. code-block:: sh
28 29 30 31

    $ ghc hello.hs

(where ``$`` represents the prompt: don't type it). GHC will compile the
32 33
source file :file:`hello.hs`, producing an object file :file:`hello.o` and an
interface file :file:`hello.hi`, and then it will link the object file to
34
the libraries that come with GHC to produce an executable called
35
:file:`hello` on Unix/Linux/Mac, or :file:`hello.exe` on Windows.
36 37 38

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
39 40 41
the scenes, add :ghc-flag:`-v` to the command line.

Then we can run the program like this:
42

43
.. code-block:: sh
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

    $ ./hello
    Hello World!

If your program contains multiple modules, then you only need to tell
GHC the name of the source file containing the ``Main`` module, and GHC
will examine the ``import`` declarations to find the other modules that
make up the program and find their source files. This means that, with
the exception of the ``Main`` module, every source file should be named
after the module name that it contains (with dots replaced by directory
separators). For example, the module ``Data.Person`` would be in the
file ``Data/Person.hs`` on Unix/Linux/Mac, or ``Data\Person.hs`` on
Windows.

Options overview
----------------

GHC's behaviour is controlled by options, which for historical reasons
are also sometimes referred to as command-line flags or arguments.
Options can be specified in three ways:

Command-line arguments
~~~~~~~~~~~~~~~~~~~~~~

.. index::
   single: structure, command-line
   single: command-line; arguments
   single: arguments; command-line

73 74 75
An invocation of GHC takes the following form:

.. code-block:: none
76 77 78 79 80 81 82 83 84 85 86 87

    ghc [argument...]

Command-line arguments are either options or file names.

Command-line options begin with ``-``. They may *not* be grouped:
``-vO`` is different from ``-v -O``. Options need not precede filenames:
e.g., ``ghc *.o -o foo``. All options are processed and then applied to
all files; you cannot, for example, invoke
``ghc -c -O1 Foo.hs -O2 Bar.hs`` to apply different optimisation levels
to the files ``Foo.hs`` and ``Bar.hs``.

88 89 90 91 92 93 94 95
.. note::

    .. index::
       single: command-line; order of arguments

    Note that command-line options are *order-dependent*, with arguments being
    evaluated from left-to-right. This can have seemingly strange effects in the
    presence of flag implication. For instance, consider
Ben Gamari's avatar
Ben Gamari committed
96
    :ghc-flag:`-fno-specialise <-fspecialise>` and :ghc-flag:`-O1` (which implies
97 98 99 100 101 102 103 104 105 106 107 108 109
    :ghc-flag:`-fspecialise`). These two command lines mean very different
    things:

    ``-fno-specialise -O1``

        ``-fspecialise`` will be enabled as the ``-fno-specialise`` is overriden
        by the ``-O1``.

    ``-O1 -fno-specialise``

        ``-fspecialise`` will not be enabled, since the ``-fno-specialise``
        overrides the ``-fspecialise`` implied by ``-O1``.

110 111 112 113 114 115 116 117 118 119 120
.. _source-file-options:

Command line options in source files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. index::
   single: source-file options

Sometimes it is useful to make the connection between a source file and
the command-line options it requires quite tight. For instance, if a
Haskell source file deliberately uses name shadowing, it should be
121
compiled with the ``-Wno-name-shadowing`` option. Rather than
122 123
maintaining the list of per-file options in a ``Makefile``, it is
possible to do this directly in the source file using the
124
``OPTIONS_GHC`` :ref:`pragma <options-pragma>` ::
125

126
    {-# OPTIONS_GHC -Wno-name-shadowing #-}
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    module X where
    ...

``OPTIONS_GHC`` is a *file-header pragma* (see :ref:`options-pragma`).

Only *dynamic* flags can be used in an ``OPTIONS_GHC`` pragma (see
:ref:`static-dynamic-flags`).

Note that your command shell does not get to the source file options,
they are just included literally in the array of command-line arguments
the compiler maintains internally, so you'll be desperately disappointed
if you try to glob etc. inside ``OPTIONS_GHC``.

.. note::
   The contents of ``OPTIONS_GHC`` are appended to the command-line
   options, so options given in the source file override those given on the
   command-line.

It is not recommended to move all the contents of your Makefiles into
your source files, but in some circumstances, the ``OPTIONS_GHC`` pragma
147
is the Right Thing. (If you use :ghc-flag:`-keep-hc-file` and have ``OPTION`` flags in
148 149 150 151 152
your module, the ``OPTIONS_GHC`` will get put into the generated ``.hc`` file).

Setting options in GHCi
~~~~~~~~~~~~~~~~~~~~~~~

153 154
Options may also be modified from within GHCi, using the :ghci-cmd:`:set`
command.
155 156 157 158 159 160 161 162 163 164 165 166 167 168

.. _static-dynamic-flags:

Static, Dynamic, and Mode options
---------------------------------

.. index::
   single: static; options
   single: dynamic; options
   single: mode; options

Each of GHC's command line options is classified as static, dynamic or
mode:

169
    For example, :ghc-flag:`--make` or :ghc-flag:`-E`. There may only be a single mode
170 171 172 173 174
    flag on the command line. The available modes are listed in
    :ref:`modes`.

    Most non-mode flags fall into this category. A dynamic flag may be
    used on the command line, in a ``OPTIONS_GHC`` pragma in a source
175
    file, or set using :ghci-cmd:`:set` in GHCi.
176 177 178 179 180 181 182 183

    A few flags are "static", which means they can only be used on the
    command-line, and remain in force over the entire GHC/GHCi run.

The flag reference tables (:ref:`flag-reference`) lists the status of
each flag.

There are a few flags that are static except that they can also be used
184
with GHCi's :ghci-cmd:`:set` command; these are listed as “static/\ ``:set``\ ”
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
in the table.

.. _file-suffixes:

Meaningful file suffixes
------------------------

.. index::
   single: suffixes, file
   single: file suffixes for GHC

File names with "meaningful" suffixes (e.g., ``.lhs`` or ``.o``) cause
the "right thing" to happen to those files.

``.hs``
    A Haskell module.

``.lhs``
    .. index::
       single: lhs file extension

    A “literate Haskell” module.

``.hspp``
    A file created by the preprocessor.

``.hi``
    A Haskell interface file, probably compiler-generated.

``.hc``
    Intermediate C file produced by the Haskell compiler.

``.c``
    A C file not produced by the Haskell compiler.

``.ll``
    An llvm-intermediate-language source file, usually produced by the
    compiler.

``.bc``
    An llvm-intermediate-language bitcode file, usually produced by the
    compiler.

``.s``
    An assembly-language source file, usually produced by the compiler.

``.o``
    An object file, produced by an assembler.

Files with other suffixes (or without suffixes) are passed straight to
the linker.

.. _modes:

Modes of operation
------------------

.. index::
   single: help options

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
247 248 249
option on the command-line. For instance,

.. code-block:: none
250

251 252 253
    $ ghc Main.hs --make -o my-application

If no mode flag is present, then GHC will enter :ghc-flag:`--make` mode
254 255 256 257 258 259
(:ref:`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.

The available mode flags are:

260
.. ghc-flag:: --interactive
261 262 263 264
    :shortdesc: Interactive mode - normally used by just running ``ghci``;
        see :ref:`ghci` for details.
    :type: mode
    :category: modes
265

266 267 268 269
    .. index::
       single: interactive mode
       single: GHCi

270
    Interactive mode, which is also available as :program:`ghci`. Interactive
271 272
    mode is described in more detail in :ref:`ghci`.

273
.. ghc-flag:: --make
274 275 276 277 278
    :shortdesc: Build a multi-module Haskell program, automatically figuring out
        dependencies. Likely to be much easier, and faster, than using
        ``make``; see :ref:`make-mode` for details.
    :type: mode
    :category: modes
279

280 281 282 283 284 285
    .. index::
       single: make mode; of GHC

    In this mode, GHC will build a multi-module Haskell program
    automatically, figuring out dependencies for itself. If you have a
    straightforward Haskell program, this is likely to be much easier,
286
    and faster, than using :command:`make`. Make mode is described in
287 288 289
    :ref:`make-mode`.

    This mode is the default if there are any Haskell source files
290
    mentioned on the command line, and in this case the :ghc-flag:`--make`
291 292
    option can be omitted.

293
.. ghc-flag:: -e expr
294 295 296
    :shortdesc: Evaluate ``expr``; see :ref:`eval-mode` for details.
    :type: mode
    :category: modes
297

298 299 300 301 302 303 304 305
    .. index::
       single: eval mode; of GHC

    Expression-evaluation mode. This is very similar to interactive
    mode, except that there is a single expression to evaluate (expr)
    which is given on the command line. See :ref:`eval-mode` for more
    details.

306
.. ghc-flag:: -E
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
    :shortdesc: Stop after preprocessing (``.hspp`` file)
    :type: mode
    :category: phases

    Stop after preprocessing (``.hspp`` file)

.. ghc-flag:: -C
    :shortdesc: Stop after generating C (``.hc`` file)
    :type: mode
    :category: phases
    
    Stop after generating C (``.hc`` file)

.. ghc-flag:: -S
    :shortdesc: Stop after generating assembly (``.s`` file)
    :type: mode
    :category: phases

    Stop after generating assembly (``.s`` file)

.. ghc-flag:: -c
    :shortdesc: Stop after generating object (``.o``) file
    :type: mode
    :category: phases
    
    Stop after generating object (``.o``) file
333 334 335 336 337

    This is the traditional batch-compiler mode, in which GHC can
    compile source files one at a time, or link objects together into an
    executable. See :ref:`options-order`.

338
.. ghc-flag:: -M
339 340 341 342
    :shortdesc: generate dependency information suitable for use in a
        ``Makefile``; see :ref:`makefile-dependencies` for details.
    :type: mode
    :category: modes
343

344
    .. index::
345
        single: dependency-generation mode; of GHC
346 347 348 349 350

    Dependency-generation mode. In this mode, GHC can be used to
    generate dependency information suitable for use in a ``Makefile``.
    See :ref:`makefile-dependencies`.

351
.. ghc-flag:: --frontend module
352 353 354 355
    :shortdesc: run GHC with the given frontend plugin; see
        :ref:`frontend_plugins` for details.
    :type: mode
    :category: modes
356 357 358 359 360 361 362

    .. index::
        single: frontend plugins; using

    Run GHC using the given frontend plugin. See :ref:`frontend_plugins` for
    details.

363
.. ghc-flag:: --mk-dll
364 365 366
    :shortdesc: DLL-creation mode (Windows only)
    :type: mode
    :category: modes
367

368 369 370 371 372
    .. index::
       single: DLL-creation mode

    DLL-creation mode (Windows only). See :ref:`win32-dlls-create`.

373 374
.. ghc-flag:: --help
              -?
375 376 377
    :shortdesc: Display help
    :type: mode
    :category: modes
378 379 380 381

    Cause GHC to spew a long usage message to standard output and then
    exit.

382
.. ghc-flag:: --show-iface file
383 384 385
    :shortdesc: display the contents of an interface file.
    :type: mode
    :category: modes
386 387 388 389

    Read the interface in file and dump it as text to ``stdout``. For
    example ``ghc --show-iface M.hi``.

390 391
.. ghc-flag:: --supported-extensions
              --supported-languages
392 393 394
    :shortdesc: display the supported language extensions
    :type: mode
    :category: modes
395 396 397

    Print the supported language extensions.

398
.. ghc-flag:: --show-options
399 400 401
    :shortdesc: display the supported command line options
    :type: mode
    :category: modes
402 403 404 405

    Print the supported command line options. This flag can be used for
    autocompletion in a shell.

406
.. ghc-flag:: --info
407 408 409
    :shortdesc: display information about the compiler
    :type: mode
    :category: modes
410 411 412

    Print information about the compiler.

413 414
.. ghc-flag:: --version
              -V
415 416 417
    :shortdesc: display GHC version
    :type: mode
    :category: modes
418 419 420

    Print a one-line string including GHC's version number.

421
.. ghc-flag:: --numeric-version
422 423 424
    :shortdesc: display GHC version (numeric only)
    :type: mode
    :category: modes
425 426 427

    Print GHC's numeric version number only.

428
.. ghc-flag:: --print-libdir
429 430 431
    :shortdesc: display GHC library directory
    :type: mode
    :category: modes
432

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
    .. index::
       single: libdir

    Print the path to GHC's library directory. This is the top of the
    directory tree containing GHC's libraries, interfaces, and include
    files (usually something like ``/usr/local/lib/ghc-5.04`` on Unix).
    This is the value of ``$libdir`` in the package
    configuration file (see :ref:`packages`).

.. _make-mode:

Using ``ghc`` ``--make``
~~~~~~~~~~~~~~~~~~~~~~~~

.. index::
   single: --make; mode of GHC
   single: separate compilation

In this mode, GHC will build a multi-module Haskell program by following
dependencies from one or more root modules (usually just ``Main``). For
453 454 455 456
example, if your ``Main`` module is in a file called :file:`Main.hs`, you
could compile and link the program like this:

.. code-block:: none
457 458 459 460 461

    ghc --make Main.hs

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
462 463 464
this case we could just type

.. code-block:: none
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489

    ghc Main.hs

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 ``Main`` module, the
program will also be linked into an executable.

The main advantages to using ``ghc --make`` over traditional
``Makefile``\s are:

-  GHC doesn't have to be restarted for each compilation, which means it
   can cache information between compilations. Compiling a multi-module
   program with ``ghc --make`` can be up to twice as fast as running
   ``ghc`` individually on each source file.

-  You don't have to write a ``Makefile``.

   .. index::
      single: Makefiles; avoiding

-  GHC re-calculates the dependencies each time it is invoked, so the
   dependencies never get out of sync with the source.

490 491 492
-  Using the :ghc-flag:`-j[n]` flag, you can compile modules in parallel.
   Specify ``-j n`` to compile n jobs in parallel. If n is omitted, then
   it defaults to the number of processors.
493 494 495 496 497 498 499 500 501 502 503 504

Any of the command-line options described in the rest of this chapter
can be used with ``--make``, but note that any options you give on the
command line will apply to all the source files compiled, so if you want
any options to apply to a single source file only, you'll need to use an
``OPTIONS_GHC`` pragma (see :ref:`source-file-options`).

If the program needs to be linked with additional objects (say, some
auxiliary C code), then the object files can be given on the command
line and GHC will include them when linking the executable.

For backward compatibility with existing make scripts, when used in
505
combination with :ghc-flag:`-c`, the linking phase is omitted (same as
506 507 508 509 510 511 512 513 514
``--make -no-link``).

Note that GHC can only follow dependencies if it has the source file
available, so if your program includes a module for which there is no
source file, even if you have an object and an interface file for the
module, then GHC will complain. The exception to this rule is for
package modules, which may or may not have source files.

The source files for the program don't all need to be in the same
515
directory; the :ghc-flag:`-i` option can be used to add directories to the
516 517
search path (see :ref:`search-path`).

518
.. ghc-flag:: -j[n]
519 520 521 522
    :shortdesc: When compiling with :ghc-flag:`--make`, compile n modules
        in parallel.
    :type: dynamic
    :category: misc
523 524

    Perform compilation in parallel when possible. GHC will use up to N
525 526 527
    threads during compilation. If N is omitted, then it defaults to the
    number of processors. Note that compilation of a module may not begin
    until its dependencies have been built.
528

529 530 531 532 533 534 535
.. _eval-mode:

Expression evaluation mode
~~~~~~~~~~~~~~~~~~~~~~~~~~

This mode is very similar to interactive mode, except that there is a
single expression to evaluate which is specified on the command line as
536 537 538
an argument to the ``-e`` option:

.. code-block:: none
539 540 541 542 543 544 545 546

    ghc -e expr

Haskell source files may be named on the command line, and they will be
loaded exactly as in interactive mode. The expression is evaluated in
the context of the loaded modules.

For example, to load and run a Haskell program containing a module
547 548 549
``Main``, we might say:

.. code-block:: none
550 551 552 553

    ghc -e Main.main Main.hs

or we can just use this mode to evaluate expressions in the context of
554 555 556
the ``Prelude``:

.. code-block:: none
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

    $ ghc -e "interact (unlines.map reverse.lines)"
    hello
    olleh

.. _options-order:

Batch compiler mode
~~~~~~~~~~~~~~~~~~~

In *batch mode*, GHC will compile one or more source files given on the
command line.

The first phase to run is determined by each input-file suffix, and the
last phase is determined by a flag. If no relevant flag is present, then
go all the way through to linking. This table summarises:

+-----------------------------------+------------------------------+----------------------------+---------------------------+
| Phase of the compilation system   | Suffix saying start here   | Flag saying stop after   | (suffix of) output file   |
+===================================+==============================+============================+===========================+
| literate pre-processor            | ``.lhs``                     |                            | ``.hs``                   |
+-----------------------------------+------------------------------+----------------------------+---------------------------+
| C pre-processor (opt.)            | ``.hs`` (with ``-cpp``)      | ``-E``                     | ``.hspp``                 |
+-----------------------------------+------------------------------+----------------------------+---------------------------+
| Haskell compiler                  | ``.hs``                      | ``-C``, ``-S``             | ``.hc``, ``.s``           |
+-----------------------------------+------------------------------+----------------------------+---------------------------+
| C compiler (opt.)                 | ``.hc`` or ``.c``            | ``-S``                     | ``.s``                    |
+-----------------------------------+------------------------------+----------------------------+---------------------------+
| assembler                         | ``.s``                       | ``-c``                     | ``.o``                    |
+-----------------------------------+------------------------------+----------------------------+---------------------------+
| linker                            | other                      |                            | ``a.out``                 |
+-----------------------------------+------------------------------+----------------------------+---------------------------+

.. index::
   single: -C
   single: -E
   single: -S
   single: -c

Thus, a common invocation would be:

598
.. code-block:: none
599 600 601 602 603 604 605 606 607 608 609

    ghc -c Foo.hs

to compile the Haskell source file ``Foo.hs`` to an object file
``Foo.o``.

.. note::
   What the Haskell compiler proper produces depends on what backend
   code generator is used. See :ref:`code-generators` for more details.

.. note::
610
   Pre-processing is optional, the :ghc-flag:`-cpp` flag turns it
611 612 613
   on. See :ref:`c-pre-processor` for more details.

.. note::
614
   The option :ghc-flag:`-E` runs just the pre-processing passes of
615 616 617
   the compiler, dumping the result in a file.

.. note::
618
   The option :ghc-flag:`-C` is only available when GHC is built in
619 620 621 622 623 624 625
   unregisterised mode. See :ref:`unreg` for more details.

.. _overriding-suffixes:

Overriding the default behaviour for a file
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

626 627 628
As described above, the way in which a file is processed by GHC depends on its
suffix. This behaviour can be overridden using the :ghc-flag:`-x suffix`
option:
629

630
.. ghc-flag:: -x suffix
631 632 633
    :shortdesc: Override default behaviour for source files
    :type: dynamic
    :category: phases
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650

    Causes all files following this option on the command line to be
    processed as if they had the suffix suffix. For example, to
    compile a Haskell module in the file ``M.my-hs``, use
    ``ghc -c -x hs M.my-hs``.

.. _options-help:

Verbosity options
-----------------

.. index::
   single: verbosity options

See also the ``--help``, ``--version``, ``--numeric-version``, and
``--print-libdir`` modes in :ref:`modes`.

651
.. ghc-flag:: -v
652 653 654
    :shortdesc: verbose mode (equivalent to ``-v3``)
    :type: dynamic
    :category: verbosity
655

656
    The :ghc-flag:`-v` option makes GHC *verbose*: it reports its version number
657 658 659 660 661 662 663 664 665
    and shows (on stderr) exactly how it invokes each phase of the
    compilation system. Moreover, it passes the ``-v`` flag to most
    phases; each reports its version number (and possibly some other
    information).

    Please, oh please, use the ``-v`` option when reporting bugs!
    Knowing that you ran the right bits in the right order is always the
    first thing we want to verify.

666 667 668 669
.. ghc-flag:: -vn
    :shortdesc: set verbosity level
    :type: dynamic
    :category: verbosity
670 671 672 673 674 675 676 677 678 679 680

    To provide more control over the compiler's verbosity, the ``-v``
    flag takes an optional numeric argument. Specifying ``-v`` on its
    own is equivalent to ``-v3``, and the other levels have the
    following meanings:

    ``-v0``
        Disable all non-essential messages (this is the default).

    ``-v1``
        Minimal verbosity: print one line per compilation (this is the
681
        default when :ghc-flag:`--make` or :ghc-flag:`--interactive` is on).
682 683 684

    ``-v2``
        Print the name of each compilation phase as it is executed.
685
        (equivalent to :ghc-flag:`-dshow-passes`).
686 687 688 689 690 691 692 693 694 695 696

    ``-v3``
        The same as ``-v2``, except that in addition the full command
        line (if appropriate) for each compilation phase is also
        printed.

    ``-v4``
        The same as ``-v3`` except that the intermediate program
        representation after each compilation phase is also printed
        (excluding preprocessed and C/assembly files).

697
.. ghc-flag:: -fprint-potential-instances
698 699 700 701
    :shortdesc: display all available instances in type error messages
    :type: dynamic
    :reverse: -fno-print-potential-instances
    :category: verbosity
702 703 704 705 706

    When GHC can't find an instance for a class, it displays a short
    list of some in the instances it knows about. With this flag it
    prints *all* the instances it knows about.

707
.. ghc-flag:: -fhide-source-paths
708 709 710
    :shortdesc: hide module source and object paths
    :type: dynamic
    :category: verbosity
711

Ben Gamari's avatar
Ben Gamari committed
712 713 714 715
    Starting with minimal verbosity (``-v1``, see :ghc-flag:`-v`), GHC
    displays the name, the source path and the target path of each compiled
    module. This flag can be used to reduce GHC's output by hiding source paths
    and target paths.
716 717 718 719 720

The following flags control the way in which GHC displays types in error
messages and in GHCi:

.. ghc-flag:: -fprint-unicode-syntax
721 722 723 724 725
    :shortdesc: Use unicode syntax when printing expressions, types and kinds.
        See also :ghc-flag:`-XUnicodeSyntax`
    :type: dynamic
    :reverse: -fno-print-unicode-syntax
    :category: verbosity
726 727 728 729 730 731 732

    When enabled GHC prints type signatures using the unicode symbols from the
    :ghc-flag:`-XUnicodeSyntax` extension. For instance,

    .. code-block:: none

        ghci> :set -fprint-unicode-syntax
733 734
        ghci> :t +v (>>)
        (>>) ∷ Monad m ⇒ ∀ a b. m a → m b → m b
735

736
.. _pretty-printing-types:
Rufflewind's avatar
Rufflewind committed
737

738
.. ghc-flag:: -fprint-explicit-foralls
739 740 741 742 743
    :shortdesc: Print explicit ``forall`` quantification in types.
        See also :ghc-flag:`-XExplicitForAll`
    :type: dynamic
    :reverse: -fno-print-explicit-foralls
    :category: verbosity
744

745
    Using :ghc-flag:`-fprint-explicit-foralls` makes
746
    GHC print explicit ``forall`` quantification at the top level of a
747 748 749
    type; normally this is suppressed. For example, in GHCi:

    .. code-block:: none
750 751 752 753 754 755 756 757 758 759 760

        ghci> let f x = x
        ghci> :t f
        f :: a -> a
        ghci> :set -fprint-explicit-foralls
        ghci> :t f
        f :: forall a. a -> a

    However, regardless of the flag setting, the quantifiers are printed
    under these circumstances:

761 762 763
    -  For nested ``foralls``, e.g.

       .. code-block:: none
764 765 766 767 768

           ghci> :t GHC.ST.runST
           GHC.ST.runST :: (forall s. GHC.ST.ST s a) -> a

    -  If any of the quantified type variables has a kind that mentions
769 770 771
       a kind variable, e.g.

       .. code-block:: none
772 773 774 775 776 777 778

           ghci> :i Data.Type.Equality.sym
           Data.Type.Equality.sym ::
             forall (k :: BOX) (a :: k) (b :: k).
             (a Data.Type.Equality.:~: b) -> b Data.Type.Equality.:~: a
                   -- Defined in Data.Type.Equality

779
.. ghc-flag:: -fprint-explicit-kinds
780 781 782 783 784
    :shortdesc: Print explicit kind foralls and kind arguments in types.
        See also :ghc-flag:`-XKindSignatures`
    :type: dynamic
    :reverse: -fno-print-explicit-kinds
    :category: verbosity
785

786
    Using :ghc-flag:`-fprint-explicit-kinds` makes GHC print kind arguments in
787
    types, which are normally suppressed. This can be important when you
788 789 790
    are using kind polymorphism. For example:

    .. code-block:: none
791 792 793 794 795 796 797 798 799

        ghci> :set -XPolyKinds
        ghci> data T a = MkT
        ghci> :t MkT
        MkT :: forall (k :: BOX) (a :: k). T a
        ghci> :set -fprint-explicit-foralls
        ghci> :t MkT
        MkT :: forall (k :: BOX) (a :: k). T k a

800
.. ghc-flag:: -fprint-explicit-runtime-reps
801 802 803 804 805
    :shortdesc: Print ``RuntimeRep`` variables in types which are
        runtime-representation polymorphic.
    :type: dynamic
    :reverse: -fno-print-explicit-runtime-reps
    :category: verbosity
806 807

    When :ghc-flag:`-fprint-explicit-runtime-reps` is enabled, GHC prints
Richard Eisenberg's avatar
Richard Eisenberg committed
808
    ``RuntimeRep`` type variables for levity-polymorphic types.
809
    Otherwise GHC will default these to ``LiftedRep``. For example,
810 811 812 813 814 815 816 817 818 819 820

    .. code-block:: none

        ghci> :t ($)
        ($) :: (a -> b) -> a -> b
        ghci> :set -fprint-explicit-runtime-reps
        ghci> :t ($)
        ($)
          :: forall (r :: GHC.Types.RuntimeRep) a (b :: TYPE r).
             (a -> b) -> a -> b

821
.. ghc-flag:: -fprint-explicit-coercions
822 823 824 825
    :shortdesc: Print coercions in types
    :type: dynamic
    :reverse: -fno-print-explicit-coercions
    :category: verbosity
826

827
    Using :ghc-flag:`-fprint-explicit-coercions` makes GHC print coercions in
828 829 830 831
    types. When trying to prove the equality between types of different
    kinds, GHC uses type-level coercions. Users will rarely need to
    see these, as they are meant to be internal.

832
.. ghc-flag:: -fprint-equality-relations
833 834 835 836
    :shortdesc: Distinguish between equality relations when printing
    :type: dynamic
    :reverse: -fno-print-equality-relations
    :category: verbosity
837

838
    Using :ghc-flag:`-fprint-equality-relations` tells GHC to distinguish between
839 840 841 842 843 844
    its equality relations when printing. For example, ``~`` is homogeneous
    lifted equality (the kinds of its arguments are the same) while
    ``~~`` is heterogeneous lifted equality (the kinds of its arguments
    might be different) and ``~#`` is heterogeneous unlifted equality,
    the internal equality relation used in GHC's solver. Generally,
    users should not need to worry about the subtleties here; ``~`` is
845
    probably what you want. Without :ghc-flag:`-fprint-equality-relations`, GHC
846
    prints all of these as ``~``. See also :ref:`equality-constraints`.
847

848
.. ghc-flag:: -fprint-expanded-synonyms
849 850 851 852
    :shortdesc: In type errors, also print type-synonym-expanded types.
    :type: dynamic
    :reverse: -fno-print-expanded-synonyms
    :category: verbosity
853 854

    When enabled, GHC also prints type-synonym-expanded types in type
855
    errors. For example, with this type synonyms: ::
856 857 858 859 860

        type Foo = Int
        type Bar = Bool
        type MyBarST s = ST s Bar

861 862 863
    This error message:

    .. code-block:: none
864 865 866 867 868

        Couldn't match type 'Int' with 'Bool'
        Expected type: ST s Foo
          Actual type: MyBarST s

869 870 871
    Becomes this:

    .. code-block:: none
872 873 874 875 876 877 878 879

        Couldn't match type 'Int' with 'Bool'
        Expected type: ST s Foo
          Actual type: MyBarST s
        Type synonyms expanded:
        Expected type: ST s Int
          Actual type: ST s Bool

880
.. ghc-flag:: -fprint-typechecker-elaboration
881 882 883 884
    :shortdesc: Print extra information from typechecker.
    :type: dynamic
    :reverse: -fno-print-typechecker-elaboration
    :category: verbosity
885 886

    When enabled, GHC also prints extra information from the typechecker in
887
    warnings. For example: ::
888 889 890 891 892 893

        main :: IO ()
        main = do
          return $ let a = "hello" in a
          return ()

894 895 896
    This warning message:

    .. code-block:: none
897 898 899 900 901 902

        A do-notation statement discarded a result of type [Char]
        Suppress this warning by saying
          _ <- ($) return let a = "hello" in a
        or by using the flag -fno-warn-unused-do-bind

903 904 905
    Becomes this:

    .. code-block:: none
906 907 908 909 910 911 912 913 914 915 916 917 918 919 920

        A do-notation statement discarded a result of type [Char]
        Suppress this warning by saying
          _ <- ($)
                  return
                  let
                    AbsBinds [] []
                      {Exports: [a <= a
                                   <>]
                       Exported types: a :: [Char]
                                       [LclId, Str=DmdType]
                       Binds: a = "hello"}
                  in a
        or by using the flag -fno-warn-unused-do-bind

921
.. ghc-flag:: -fdiagnostics-color=always|auto|never
922 923 924
    :shortdesc: Use colors in error messages
    :type: dynamic
    :category: verbosity
925 926 927

    Causes GHC to display error messages with colors.  To do this, the
    terminal must have support for ANSI color codes, or else garbled text will
Rufflewind's avatar
Rufflewind committed
928 929 930 931 932 933 934 935 936
    appear.  The default value is ``auto``, which means GHC will make an
    attempt to detect whether terminal supports colors and choose accordingly.

    The precise color scheme is controlled by the environment variable
    ``GHC_COLORS`` (or ``GHC_COLOURS``).  This can be set to colon-separated
    list of ``key=value`` pairs.  These are the default settings:

    .. code-block:: none

Rufflewind's avatar
Rufflewind committed
937
        header=:message=1:warning=1;35:error=1;31:fatal=1;31:margin=1;34
Rufflewind's avatar
Rufflewind committed
938 939

    Each value is expected to be a `Select Graphic Rendition (SGR) substring
Rufflewind's avatar
Rufflewind committed
940 941 942 943 944 945 946 947 948 949
    <https://en.wikipedia.org/wiki/ANSI_escape_code#graphics>`_.  The
    formatting of each element can inherit from parent elements.  For example,
    if ``header`` is left empty, it will inherit the formatting of
    ``message``.  Alternatively if ``header`` is set to ``1`` (bold), it will
    be bolded but still inherits the color of ``message``.

    Currently, in the primary message, the following inheritance tree is in
    place:

    - ``message``
Ben Gamari's avatar
Ben Gamari committed
950

Rufflewind's avatar
Rufflewind committed
951
      - ``header``
Ben Gamari's avatar
Ben Gamari committed
952

Rufflewind's avatar
Rufflewind committed
953 954 955 956 957 958
        - ``warning``
        - ``error``
        - ``fatal``

    In the caret diagnostics, there is currently no inheritance at all between
    ``margin``, ``warning``, ``error``, and ``fatal``.
Rufflewind's avatar
Rufflewind committed
959 960 961

    The environment variable can also be set to the magical values ``never``
    or ``always``, which is equivalent to setting the corresponding
Rufflewind's avatar
Rufflewind committed
962
    ``-fdiagnostics-color`` flag but with lower precedence.
963

964 965 966 967 968
.. ghc-flag:: -fdiagnostics-show-caret
    :shortdesc: Whether to show snippets of original source code
    :type: dynamic
    :reverse: -fno-diagnostics-show-caret
    :category: verbosity
Rufflewind's avatar
Rufflewind committed
969 970 971 972 973

    Controls whether GHC displays a line of the original source code where the
    error was detected.  This also affects the associated caret symbol that
    points at the region of code at fault.  The flag is on by default.

974
.. ghc-flag:: -ferror-spans
975 976 977
    :shortdesc: Output full span in error messages
    :type: dynamic
    :category: verbosity
978 979 980 981 982

    Causes GHC to emit the full source span of the syntactic entity
    relating to an error message. Normally, GHC emits the source
    location of the start of the syntactic entity only.

983 984 985
    For example:

    .. code-block:: none
986 987 988

        test.hs:3:6: parse error on input `where'

989 990 991
    becomes:

    .. code-block:: none
992 993 994

        test296.hs:3:6-10: parse error on input `where'

995 996 997
    And multi-line spans are possible too:

    .. code-block:: none
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

        test.hs:(5,4)-(6,7):
            Conflicting definitions for `a'
            Bound at: test.hs:5:4
                      test.hs:6:7
            In the binding group for: a, b, a

    Note that line numbers start counting at one, but column numbers
    start at zero. This choice was made to follow existing convention
    (i.e. this is how Emacs does it).

1009
.. ghc-flag:: -H ⟨size⟩
1010 1011 1012
    :shortdesc: Set the minimum size of the heap to ⟨size⟩
    :type: dynamic
    :category: misc
1013 1014 1015 1016

    Set the minimum size of the heap to ⟨size⟩. This option is
    equivalent to ``+RTS -Hsize``, see :ref:`rts-options-gc`.

1017
.. ghc-flag:: -Rghc-timing
1018 1019 1020
    :shortdesc: Summarise timing stats for GHC (same as ``+RTS -tstderr``).
    :type: dynamic
    :category: verbosity
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037

    Prints a one-line summary of timing statistics for the GHC run. This
    option is equivalent to ``+RTS -tstderr``, see
    :ref:`rts-options-gc`.

.. _options-platform:

Platform-specific Flags
-----------------------

.. index::
   single: -m\* options
   single: platform-specific options
   single: machine-specific options

Some flags only make sense for particular target platforms.

1038
.. ghc-flag:: -msse2
1039 1040 1041
    :shortdesc: (x86 only) Use SSE2 for floating-point operations
    :type: dynamic
    :category: platform-options
1042

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    (x86 only, added in GHC 7.0.1) Use the SSE2 registers and
    instruction set to implement floating point operations when using
    the :ref:`native code generator <native-code-gen>`. This gives a
    substantial performance improvement for floating point, but the
    resulting compiled code will only run on processors that support
    SSE2 (Intel Pentium 4 and later, or AMD Athlon 64 and later). The
    :ref:`LLVM backend <llvm-code-gen>` will also use SSE2 if your
    processor supports it but detects this automatically so no flag is
    required.

    SSE2 is unconditionally used on x86-64 platforms.

1055
.. ghc-flag:: -msse4.2
1056 1057 1058
    :shortdesc: (x86 only) Use SSE4.2 for floating-point operations
    :type: dynamic
    :category: platform-options
1059

1060 1061 1062 1063 1064 1065 1066
    (x86 only, added in GHC 7.4.1) Use the SSE4.2 instruction set to
    implement some floating point and bit operations when using the
    :ref:`native code generator <native-code-gen>`. The resulting compiled
    code will only run on processors that support SSE4.2 (Intel Core i7
    and later). The :ref:`LLVM backend <llvm-code-gen>` will also use
    SSE4.2 if your processor supports it but detects this automatically
    so no flag is required.
1067 1068 1069 1070 1071 1072 1073 1074 1075

Miscellaneous flags
-------------------

.. index::
   single: miscellaneous flags

Some flags only make sense for a particular use case.

1076
.. ghc-flag:: -ghcversion-file ⟨path to ghcversion.h⟩
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
    :shortdesc: (GHC as a C compiler only) Use this ``ghcversion.h`` file
    :type: dynamic
    :category: misc

    When GHC is used to compile C files, GHC adds package include paths and
    includes ``ghcversion.h`` directly. The compiler will lookup the path for
    the ``ghcversion.h`` file from the ``rts`` package in the package database.
    In some cases, the compiler's package database does not contain the ``rts``
    package, or one wants to specify a specific ``ghcversions.h`` to be
    included. This option can be used to specify the path to the
    ``ghcversions.h`` file to be included. This is primarily intended to be
    used by GHC's build system.