Loading Modules and Files
Haskell is a compiled programming language. You write a complete program and compile it into a standalone executable. The purpose of GHCi is not to do any extensive amount of programming but to inspect and experiment with code you have developed, mainly for informal testing and debugging purposes. To this end, GHCi needs to provide the means to load code stored in some file into GHCi. There are two ways to do this.
First, we have the :load
command or, abbreviated, simply :l
. Place the
mergeSort
code from the previous subsection into a file:
mergeSort :: Ord a => [a] -> [a]
mergeSort [] = []
mergeSort xs = mergeInPairs [[x] | x <- xs]
mergeInPairs :: Ord a => [[a]] -> [a]
mergeInPairs [xs] = xs
mergeInPairs xss = mergeInPairs $ mergePairs xss
mergePairs :: Ord a => [[a]] -> [[a]]
mergePairs (xs:ys:xss) = merge xs ys : mergePairs xss
mergePairs xss = xss
merge :: Ord a => [a] -> [a] -> [a]
merge xs [] = xs
merge [] ys = ys
merge xs@(x:xs') ys@(y:ys')
| x <= y = x : merge xs' ys
| otherwise = y : merge xs ys'
Then you can load this file into GHCi and try out all the functions defined in this file:
>>> :load MergeSort.hs
>>> mergeSort [5,8,12,1,3,2,30,1,9]
[1,1,2,3,5,8,9,12,30]
>>> merge [2,8,9] [1,10,12]
[1,2,8,9,10,12]
Note that the path to the file you want to load needs to be an absolute path or
a path relative to the working directory from which you started GHCi. So the
:load MergeSort.hs
command above assumes that the file MergeSort.hs
is
stored in the same directory from which you started GHCi.
There is also the :reload
or :r
command, which reloads the last file you
loaded using :load
or :l
. This is useful if you load a file for testing
and debugging purposes. You try out the functions and maybe detect a bug. You
go and fix the bug in your source code, and then a quick :r
makes sure that
the most recent, fixed version of the code is loaded, and you can play with
this new version of your code to see whether it works correctly now.
What about code that isn't in a source code file you wrote but which is part of
the standard library? For example, we may be interested in using the functions
in the Data.Char
module. You have two options to load this module.
The first is to use the import
statement available also in Haskell source
code files:
>>> import Data.Char
The other is to use the :module
or :m
command:
>>> :m Data.Char
One thing to be aware of is that any subsequent :module
command overrides the
effect of a previous :module
command. So, after
>>> :m Data.List
the functions in Data.Char
would no longer be available. If you want to load
the functions from Data.List
in addition to the functions in Data.Char
,
you need
>>> :m +Data.List
This limitation does not apply when you load additional modules using import
.
You can also load multiple modules at the same time:
>>> :m Data.Char Data.List
The GHCi documentation has the following to say about the :module
command:
Sets or modifies the current context for statements typed at the prompt. The form
import mod
is equivalent to:module +mod
.
After importing the Data.Char
module using one of the methods above, you can
use the functions it defines. For example:
>>> isUpper 'a'
False
>>> toUpper 'a'
'A'