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 assume there's a reason for the alternative syntax.
The syntax for importing modules in a Haskell script is import <module name>. This must be done before defining any functions, so imports are usually done at the top of the file. One script can, of course, import several modules. Just put each import statement into a separate line.
A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.
Prelude is a module that contains a small set of standard definitions and is included automatically into all Haskell modules.
Haskell modules are a useful way to group a set of related functionalities into a single package and manage different functions that may have the same names. The module definition is the first thing that goes in your Haskell file. the name of the module begins with a capital letter; each file contains only one module.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With