Chapter 3. Using GHCi

Table of Contents

3.1. Introduction to GHCi
3.2. Loading source files
3.2.1. Modules vs. filenames
3.2.2. Making changes and recompilation
3.3. Loading compiled code
3.4. Interactive evaluation at the prompt
3.4.1. What's really in scope at the prompt?
3.4.1.1. Qualified names
3.4.2. Using do-notation at the prompt
3.4.3. The it variable
3.4.4. Type defaulting in GHCi
3.5. Invoking GHCi
3.5.1. Packages
3.5.2. Extra libraries
3.6. GHCi commands
3.7. The :set command
3.7.1. GHCi options
3.7.2. Setting GHC command-line options in GHCi
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 familiar 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.04, for Haskell 98.
/ /_\\/ __  / /___| |      http://www.haskell.org/ghc/
\____/\/ /_/\____/|_|      Type :? for help.

Loading package base ... linking ... done.
Loading package haskell98 ... linking ... done.
Prelude> 

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>
   :add <filename> ...        add module(s) to the current target set
   :browse [*]<module>        display the names defined by <module>
   :cd <dir>                  change directory to <dir>
   :def <cmd> <expr>          define a command :<cmd>
   :help, :?                  display this list of commands
   :info [<name> ...]         display information about the given names
   :load <filename> ...       load module(s) and their dependents
   :module [+/-] [*]<mod> ... set the context for expression evaluation
   :reload                    reload the current module set

   :set <option> ...          set options
   :set args <arg> ...        set the arguments returned by System.getArgs
   :set prog <progname>       set the value returned by System.getProgName

   :show modules              show the currently loaded modules
   :show bindings             show the current bindings made at the prompt

   :type <expr>               show the type of <expr>
   :kind <type>               show the kind of <type>
   :undef <cmd>               undefine user-defined command :<cmd>
   :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
    -<flags>           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
3
Prelude> let x = 42 in x / 9
4.666666666666667
Prelude> 

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.



[1] The ‘i’ stands for “Interactive”

[2] except foreign export, at the moment