Chapter 3. Using GHCi

Table of Contents
3.1. Introduction to GHCi
3.2. Loading source files
3.3. Loading compiled code
3.4. Interactive evaluation at the prompt
3.5. Invoking GHCi
3.6. GHCi commands
3.7. The :set command
3.8. The .ghci file
3.9. FAQ and Things To Watch Out For

GHCi[1] is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted. If you're famililar with Hugs, then you'll be right at home with GHCi. However, GHCi also has support for interactively loading compiled code, as well as supporting all[2]the language extensions that GHC provides.

3.1. Introduction to GHCi

Let's start with an example GHCi session. You can fire up GHCi with the command ghci:

$ ghci
   ___         ___ _
  / _ \ /\  /\/ __(_)
 / /_\// /_/ / /  | |      GHC Interactive, version 5.00, For Haskell 98.
/ /_\\/ __  / /___| |
\____/\/ /_/\____/|_|      Type :? for help.

Loading package std ... linking ... done.

There may be a short pause while GHCi loads the prelude and standard libraries, after which the prompt is shown. If we follow the instructions and type :? for help, we get:

 Commands available from the prompt:
   <stmt>              evaluate/run <stmt>
   :cd <dir>           change directory to <dir>
   :def <cmd> <expr>   define a macro :<cmd>
   :help, :?           display this list of commands
   :load <filename>    load a module (and it dependents)
   :module <mod>       set the context for expression evaluation to <mod>
   :reload             reload the current module set
   :set <option> ...   set options
   :type <expr>        show the type of <expr>
   :unset <option> ... unset options
   :quit               exit GHCi
   :!<command>         run the shell command <command>
 Options for `:set' and `:unset':
    +r                 revert top-level expressions after each evaluation
    +s                 print timing/memory stats after each evaluation
    +t                 print type after evaluation
    -<flag>            most GHC command line flags can also be set here
                         (eg. -v2, -fglasgow-exts, etc.)

We'll explain most of these commands as we go along. For Hugs users: many things work the same as in Hugs, so you should be able to get going straight away.

Haskell expressions can be typed at the prompt:

Prelude> 1+2
Prelude> let x = 42 in x / 9

GHCi interprets the whole line as an expression to evaluate. The expression may not span several lines - as soon as you press enter, GHCi will attempt to evaluate it.



The ‘i’ stands for “Interactive”


except the FFI, at the moment