Chapter 2. Using GHCi

Table of Contents

2.1. Introduction to GHCi
2.2. Loading source files
2.2.1. Modules vs. filenames
2.2.2. Making changes and recompilation
2.3. Loading compiled code
2.4. Interactive evaluation at the prompt
2.4.1. I/O actions at the prompt
2.4.2. Using do-notation at the prompt
2.4.3. What's really in scope at the prompt? :module and :load Qualified names The :main and :run commands
2.4.4. The it variable
2.4.5. Type defaulting in GHCi
2.5. The GHCi Debugger
2.5.1. Breakpoints and inspecting variables Setting breakpoints Listing and deleting breakpoints
2.5.2. Single-stepping
2.5.3. Nested breakpoints
2.5.4. The _result variable
2.5.5. Tracing and history
2.5.6. Debugging exceptions
2.5.7. Example: inspecting functions
2.5.8. Limitations
2.6. Invoking GHCi
2.6.1. Packages
2.6.2. Extra libraries
2.7. GHCi commands
2.8. The :set command
2.8.1. GHCi options
2.8.2. Setting GHC command-line options in GHCi
2.9. The .ghci file
2.10. Compiling to object code inside GHCi
2.11. 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. . GHCi also includes an interactive debugger (see Section 2.5, “The GHCi Debugger”).

2.1. Introduction to GHCi

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

$ ghci
GHCi, version 6.12.1:  :? for help
Loading package ghc-prim ... linking ... done.
Loading package integer-gmp ... linking ... done.
Loading package base ... linking ... done.
Loading package ffi-1.0 ... linking ... done.

There may be a short pause while GHCi loads the prelude and standard libraries, after which the prompt is shown. As the banner says, you can type :? to see the list of commands available, and a half line description of each of them.

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.

GHCi also has a multiline mode, , which is terminated by an empty line:

Prelude> :set +m
Prelude> let x = 42 in x / 9

In Haskell, a let expression is followed by in. However, in GHCi, since the expression can also be interpreted in the IO monad, a let binding with no accompanying in statement can be signalled by an empty line, as in the above example.

Multiline mode is useful when entering monadic do statements:

Control.Monad.State> flip evalStateT 0 $ do
Control.Monad.State| i <- get
Control.Monad.State| lift $ do
Control.Monad.State|   putStrLn "Hello World!"
Control.Monad.State|   print i
"Hello World!"

During a multiline interaction, the user can interrupt and return to the top-level prompt.

Prelude> do
Prelude| putStrLn "Hello, World!"
Prelude| ^C

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

[2] except foreign export, at the moment