Using GHCi
Breakpoints with GHCi
Section titled “Breakpoints with GHCi”GHCi supports imperative-style breakpoints out of the box with interpreted code (code that’s been :loaded).
With the following program:
-- mySum.hsdoSum n = do putStrLn ("Counting to " ++ (show n)) let v = sum [1..n] putStrLn ("sum to " ++ (show n) ++ " = " ++ (show v))loaded into GHCi:
Prelude> :load mySum.hs[1 of 1] Compiling Main ( mySum.hs, interpreted )Ok, modules loaded: Main.*Main>We can now set breakpoints using line numbers:
*Main> :break 2Breakpoint 0 activated at mySum.hs:2:3-39and GHCi will stop at the relevant line when we run the function:
*Main> doSum 12Stopped at mySum.hs:2:3-39_result :: IO () = _n :: Integer = 12[mySum.hs:2:3-39] *Main>It might be confusing where we are in the program, so we can use :list to clarify:
[mySum.hs:2:3-39] *Main> :list1 doSum n = do2 putStrLn ("Counting to " ++ (show n)) -- GHCi will emphasise this line, as that's where we've stopped3 let v = sum [1..n]We can print variables, and continue execution too:
[mySum.hs:2:3-39] *Main> n12:continueCounting to 12sum to 12 = 78*Main>Starting GHCi
Section titled “Starting GHCi”Type ghci at a shell prompt to start GHCI.
$ ghciGHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for helpPrelude>Changing the GHCi default prompt
Section titled “Changing the GHCi default prompt”By default, GHCI’s prompt shows all the modules you have loaded into your interactive session. If you have many modules loaded this can get long:
Prelude Data.List Control.Monad> -- etcThe :set prompt command changes the prompt for this interactive session.
Prelude Data.List Control.Monad> :set prompt "foo> "foo>To change the prompt permanently, add :set prompt "foo> " to the GHCi config file.
The GHCi configuration file
Section titled “The GHCi configuration file”GHCi uses a configuration file in ~/.ghci. A configuration file consists of a sequence of commands which GHCi will execute on startup.
$ echo ":set prompt \"foo> \"" > ~/.ghci$ ghciGHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for helpLoaded GHCi configuration from ~/.ghcifoo>Loading a file
Section titled “Loading a file”The :l or :load command type-checks and loads a file.
$ echo "f = putStrLn \"example\"" > example.hs$ ghciGHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for helpghci> :l example.hs[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> fexampleQuitting GHCi
Section titled “Quitting GHCi”You can quit GHCi simply with :q or :quit
ghci> :qLeaving GHCi.
ghci> :quitLeaving GHCi.Alternatively, the shortcut CTRL+D (Cmd+D for OSX) has the same effect as :q.
Reloading a already loaded file
Section titled “Reloading a already loaded file”If you have loaded a file into GHCi (e.g. using :l filename.hs) and you have changed the file in an editor outside of GHCi you must reload the file with :r or :reload in order to make use of the changes, hence you don’t need to type again the filename.
ghci> :rOK, modules loaded: Main.
ghci> :reloadOK, modules loaded: Main.Multi-line statements
Section titled “Multi-line statements”The :{ instruction begins multi-line mode and :} ends it. In multi-line mode GHCi will interpret newlines as semicolons, not as the end of an instruction.
ghci> :{ghci| myFoldr f z [] = zghci| myFoldr f z (y:ys) = f y (myFoldr f z ys)ghci| :}ghci> :t myFoldrmyFoldr :: (a -> b -> b) -> b -> [a] -> bRemarks
Section titled “Remarks”GHCI is the interactive REPL that comes bundled with GHC.