Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell library import syntax

Tags:

haskell

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.

like image 359
planarian Avatar asked Oct 20 '13 21:10

planarian


People also ask

How do I import libraries into Haskell?

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.

What is a qualified import?

A qualified import allows using functions with the same name imported from several modules, e.g. map from the Prelude and map from Data.

What is Prelude in Ghci?

Prelude is a module that contains a small set of standard definitions and is included automatically into all Haskell modules.

What are the Haskell modules for?

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.


2 Answers

You're right that import Module and :module + Module are identical, but there are a few reasons for the :module (henceforth abbreviated :m) syntax.

  1. 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 imports. (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.)

  2. 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.

  3. It lets you un-import modules: :m - M will remove M's contents from what's currently in scope at the prompt.

  4. 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:

  1. 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.)

  2. 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.

  3. 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.

like image 51
Antal Spector-Zabusky Avatar answered Sep 27 '22 18:09

Antal Spector-Zabusky


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.

like image 45
jev Avatar answered Sep 27 '22 18:09

jev