GHCi is invoked with the command ghci
or
ghc ––interactive
. One or more modules or
filenames can also be specified on the command line; this
instructs GHCi to load the specified modules or filenames (and all
the modules they depend on), just as if you had said
:load
at the
GHCi prompt (see Section 3.6, “GHCi commands”). For example, to
start GHCi and load the program whose topmost module is in the
file modules
Main.hs
, we could say:
$ ghci Main.hs
Most of the command-line options accepted by GHC (see Chapter 4, Using GHC) also make sense in interactive mode. The ones that don't make sense are mostly obvious; for example, GHCi doesn't generate interface files, so options related to interface file generation won't have any effect.
Most packages (see Section 4.8.1, “Using Packages ”) are available without needing to specify any extra flags at all: they will be automatically loaded the first time they are needed.
For non-auto packages, however, you need to request the
package be loaded by using the -package
flag:
$ ghci -package data ___ ___ _ / _ \ /\ /\/ __(_) / /_\// /_/ / / | | GHC Interactive, version 5.05, for Haskell 98. / /_\\/ __ / /___| | http://www.haskell.org/ghc/ \____/\/ /_/\____/|_| Type :? for help. Loading package base ... linking ... done. Loading package haskell98 ... linking ... done. Loading package lang ... linking ... done. Loading package concurrent ... linking ... done. Loading package readline ... linking ... done. Loading package unix ... linking ... done. Loading package posix ... linking ... done. Loading package util ... linking ... done. Loading package data ... linking ... done. Prelude>
The following command works to load new packages into a running GHCi:
Prelude> :set -package name
But note that doing this will cause all currently loaded
modules to be unloaded, and you'll be dumped back into the
Prelude
.
Extra libraries may be specified on the command line using
the normal -l
option. (The term library here refers to
libraries of foreign object code; for using libraries of Haskell
source code, see Section 3.2.1, “Modules vs. filenames”.) For
example, to load the “m” library:lib
$ ghci -lm
On systems with .so
-style shared
libraries, the actual library loaded will the
lib
. GHCi
searches the following places for libraries, in this order:lib
.so
Paths specified using the
-L
command-line option,path
the standard library search path for your system,
which on some systems may be overridden by setting the
LD_LIBRARY_PATH
environment
variable.
On systems with .dll
-style shared
libraries, the actual library loaded will be
. Again,
GHCi will signal an error if it can't find the library.lib
.dll
GHCi can also load plain object files
(.o
or .obj
depending on
your platform) from the command-line. Just add the name the
object file to the command line.
Ordering of -l
options matters: a library
should be mentioned before the libraries it
depends on (see Section 4.10.7, “Options affecting linking”).