GHCi: the interactive shell

Starting the shell

ghci is the interactive shell that comes with the compiler. Type an expression, press Enter, read the value: no file, no build, no main. It is the fastest way to ask the compiler a question, and the way to test a function before it goes into a program.

Start it with no arguments and it prints its version and a prompt:

ghci

GHCi, version 9.10.3: https://www.haskell.org/ghc/ :? for help
ghci>

The prompt is ghci>. Everything typed there is either an expression, which is evaluated and printed, or a command that starts with a colon. :quit leaves, and so does Ctrl-D. :? prints the list of commands, which is worth reading once.

The banner line also says where the shell came from: the install lesson checks the same version with ghc --version.

:load and the prompt

A session becomes useful when a file is loaded into it. Save this as Doubler.hs:

module Main (main) where double :: Int -> Int double x = x * 2 main :: IO () main = print (double 21)

Then start the shell and load it:

ghci Doubler.hs

ghci Doubler.hs is a shortcut for :load Doubler.hs after the shell starts. The transcript below is one session, run on this machine:

GHCi, version 9.10.3: https://www.haskell.org/ghc/ :? for help
ghci> :load Doubler.hs
[1 of 2] Compiling Main ( Doubler.hs, interpreted )
Ok, one module loaded.
ghci> double 21
42
ghci> :type double
double :: Int -> Int
ghci> :type map
map :: (a -> b) -> [a] -> [b]
ghci> :quit
Leaving GHCi.

Three things are worth noticing. interpreted means the shell did not produce an executable: it read the module into memory. The file has a main, and the shell ignores it until you ask for it with main. And :type prints the type of anything, not only of the names in your file: map comes from Prelude and has a type with two type variables.

:type can be shortened to :t, :load to :l, :reload to :r. The shell needs the name of a module or a file, not a URL: the path is relative to the folder where ghci started, unless :cd moved it.

Edit, then :reload

The loop a programmer actually uses is: change the file in an editor, type :reload, try again. Below the same session loads the file, calls the function, and then the file is changed from x * 2 to x * 3 between two commands:

ghci> :load Doubler.hs
[1 of 2] Compiling Main ( Doubler.hs, interpreted )
Ok, one module loaded.
ghci> double 21
42
ghci> :reload
[1 of 2] Compiling Main ( Doubler.hs, interpreted ) [Source file changed]
Ok, one module reloaded.
ghci> double 21
63
ghci> :quit
Leaving GHCi.

[Source file changed] is the shell telling you why it recompiled. Nothing was typed twice: the new definition replaced the old one in memory, and the second call returns 63 because the file now says so.

A reload reports errors the way the compiler does, and keeps the last good version loaded, so a typo in the middle of a session costs one line and not the session. That is the main reason to keep ghci open in a terminal next to the editor.

:info

:type answers "what is this", and :info answers "what is this made of". For a type it prints the definition and every instance the loaded packages provide:

ghci> :info Bool
type Bool :: *
data Bool = False | True
-- Defined in ‘GHC.Types’
instance Bounded Bool -- Defined in ‘GHC.Internal.Enum’
instance Read Bool -- Defined in ‘GHC.Internal.Read’
instance Enum Bool -- Defined in ‘GHC.Internal.Enum’
instance Show Bool -- Defined in ‘GHC.Internal.Show’
instance Eq Bool -- Defined in ‘GHC.Classes’
instance Ord Bool -- Defined in ‘GHC.Classes’

The first line is the kind, the second the declaration, and the rest the type classes Bool belongs to: it can be compared (Eq, Ord), printed (Show), read (Read), counted through (Enum) and has a smallest and a largest value (Bounded). Those names are the subject of the type classes lesson; here the point is that the answer comes from the compiler, not from memory.

:info also takes a function, a class or a constructor, and prints where it is defined and what its type is.

:set and warnings

:set changes the flags of the session. The one to turn on first is -Wall: from then on, every module loaded in this session is compiled with the warnings the compiler offers, including the missing type signature. The file below has no signature on purpose:

module Twice (twice) where twice x = x * 2

ghci> :set -Wall
ghci> :load Twice.hs
[1 of 1] Compiling Twice ( Twice.hs, interpreted )
Twice.hs:3:1: warning: [GHC-38417] [-Wmissing-signatures]
Top-level binding with no type signature: twice :: Num a => a -> a
|
3 | twice x = x * 2
| ^^^^^

Ok, one module loaded.

The flag stays on for the rest of the session, and :set -Wall -Werror turns the same warnings into errors, which is how a file is checked before a commit. The lesson on warnings reads each line of that message, and :set +s prints the time and the allocation after every evaluation, which is the cheapest profile a program can have.

Exercises

  1. Start ghci and ask for :type 1, :type "abc" and :type 'a'. Which two answers had you not expected?
  2. Load Doubler.hs, then call main in the shell. What is printed, and why does the shell allow it?
  3. Change the type signature of double to Double -> Double and run :reload. Read the error before you undo the change.
  4. Ask :info Maybe and find the two constructors and the instances in the answer.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does GHCi do with the expression you type at the prompt?

    Answer

    It evaluates it and prints the value

    The prompt evaluates an expression in the loaded modules and prints its value; a command that starts with a colon does something else.

  2. Which command loads a file into a running session?

    Answer

    :load Doubler.hs

    :load (or :l) reads a file into the session; :import adds a module that is already installed, and the other two names do not exist.

  3. The file changed on disk. Which command compiles it again in the session?

    Answer

    :reload

    :reload (:r) recompiles the modules that are already loaded, and the shell prints [Source file changed] when the file is newer.

  4. What does the prompt line double :: Int -> Int in a session come from?

    Answer

    The answer to :type, which asks the compiler

    :type asks the compiler for the type of a name; the signature in the file is one input to that answer, not the answer itself.

  5. What does :set -Wall change?

    Answer

    It turns on every warning for the modules loaded after it

    :set passes flags to the compiler of the session, and -Wall turns on the warning group; the file on disk is not touched.

  6. What does this program print? module Main (main) where double :: Int -> Int double x = x * 2 main :: IO () main = print (double 21)

    Answer

    42

    main is called by the runtime when the program starts, and double 21 is 42.

  7. What does this program print? module Main (main) where double :: Int -> Int double x = x * 2 main :: IO () main = print (map double [1, 2, 3])

    Answer

    [2, 4, 6]

    map applies double to each element of the list, so the result is [2, 4, 6].

  8. What does this program print? module Main (main) where main :: IO () main = print (length (show (42 :: Int)))

    Answer

    2

    show turns 42 into the two-character string "42", and length counts its characters.

  9. A file has twice x = x * 2 and no signature. What does :set -Wall followed by :load print?

    Answer

    warning: [GHC-38417] [-Wmissing-signatures]

    -Wmissing-signatures is part of -Wall, and a top-level binding without a signature is reported with GHC-38417.

  10. Why is the shell called interactive, while ghc is not?

    Answer

    Because ghci keeps the compiled modules in memory and answers one expression at a time, while ghc builds an executable

    ghci compiles to memory and evaluates at the prompt; ghc compiles a file to an executable that runs on its own.

The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev