Sorry for the very basic question: In GHCi, is there a difference between import Library.Name
and :m +Library.Name
? They seem equivalent, but I ass
You would include import
in the source code, which is more general, whereas :m
is a ghci - specific command (for convenience).
You can use ghci> :m + Module1 ... ModuleN
to load multiple modules. Use -
instead of +
to unload the module. Because ghci
is interactive, I'd stick to :m
, unless your workflow is rather: edit your .hs
file, save it, and reload it. Then the import
would be more suitable (and has more features e.g. qualified imports).
The import
directive would also work if you later decide to compile the program using for example ghc
. You can selectively import only specific functions: import Data.List (sort)
would import only sort, so is pollutes the namespace less.
You're right that import Module
and :module + Module
are identical, but there are a few reasons for the :module
(henceforth abbreviated :m
) syntax.
It's older. GHCi really used to just be the inside of an IO
do
block; now it supports every part of the language, so we can do import
s. (It looks like GHCi 6.6.x didn't support import, but GHCi 6.8.1 did; we didn't get full support for top-level declarations until GHCi 7.4.1.)
It lets you import multiple modules at once. :m + M1 M2 M3
is the same as writing import M1
, import M2
, and import M3
each on a new line.
It lets you un-import modules: :m - M
will remove M
's contents from what's currently in scope at the prompt.
It lets you import an interpreted module so that you can see the whole scope. This is what happens when you :load File.hs
; you find that you're in the module *File
, and can e.g. see everything that File
imports and even everything that it doesn't export. If you have an interpreted module MI
loaded, then you can use :m + M1 *MI M3
to bring M1
and M2
into scope the ordinary way, and MI
into scope the special way.
For completeness, though, import
syntax does offer us three things that :m
doesn't:
Qualified imports. It's much nicer to be able to do import qualified Data.Map as M
and use M.lookup
than to have to do Data.Map.lookup
or suffer ambiguity errors. (Note that every package that's installed is available fully qualified in GHCi, so import qualified Module
alone buys you nothing.)
Import lists. Even if I have the functions from Data.Map
qualified with M
, I can still do import Data.Map (Map)
to just bring the type into scope unqualified.
Import hiding
. The reverse of the above; maybe I'm writing my own sorting routine, so I can do import Data.List hiding (sort)
.
You can also check out §2.4.5, "What's really in scope at the prompt?", in the GHC (7.6) user's guide.