Table of Contents
GHCi 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 the language extensions that GHC provides. . GHCi also includes an interactive debugger (see Section 2.5, “The GHCi Debugger”).
Let's start with an example GHCi session. You can fire up
GHCi with the command
$ ghci GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? 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. Prelude>
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 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.
Prelude> :set +m Prelude> let x = 42 in x / 9 Prelude| 4.666666666666667 Prelude>
In Haskell, a
let expression is followed
in. However, in GHCi, since the expression
can also be interpreted in the
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
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 Control.Monad.State| "Hello World!" 0 Control.Monad.State>
During a multiline interaction, the user can interrupt and return to the top-level prompt.
Prelude> do Prelude| putStrLn "Hello, World!" Prelude| ^C Prelude>