Haskell’s repl can be run use ghci or cabal repl. The repl using : to prefix commands, so for example :? will list the help.
I’m not going to go through all commands, but just the one’s I’ve been using regularly, check out Cabal for the full list of commands and features.
Quitting the repl
Let’s start by looking at how to quitting the repl (gracefully)
Prelude> :q
Writing code
Obviously we want to be able to execute code in the repl, in such cases we write code such as
x = 1 + 2 -- press enter x -- press enter
The output from this will obviously be 3.
To handle multi line input we create a block using :{ and :}, for example
:{ data Expr = Lit Integer | Div Expr Expr :}
Don’t forget indention is required!
Loading existing code
In some cases we might want to load existing code into the repl, such a data definitions etc. Let’s say we have a Main.hs then run
:load Main
Display information
Assuming we added the Expr data type, to the repl, we might want to show the definition at some point, just use
:i Expr
Showing the type of an expression
Let’s assume we have something like the following in a module
eval :: Expr -> Integer
We can use the following to return the type (obviously in this instance we should see the above, but this command can ofcourse be executed against types where we want to find the type annotations/decalaractions
:t Expr
Now try
:t print
and you’ll see the following
print :: Show a => a -> IO ()
References