Difference between revisions of "Import"

From HaskellWiki
Jump to navigation Jump to search
(glossary)
(neutral formulation in the introduction)
Line 1: Line 1:
 
[[Category:Language]]
 
[[Category:Language]]
 
[[Category:Glossary]]
 
[[Category:Glossary]]
The <hask>import</hask> statement is used to import functions and other definitions from another module. In the simplest case, you just write
+
The <hask>import</hask> statement is used to import functions and other definitions from another module. The shortest form of the import statement is
 
<haskell>
 
<haskell>
 
import Data.Maybe
 
import Data.Maybe
 
</haskell>
 
</haskell>
to import the named module (in this case <hask>Data.Maybe</hask>).
+
that imports the named module (in this case <hask>Data.Maybe</hask>) and brings all of its identifiers into scope in their plain form.
   
However, in more complicated cases, the module can be imported qualified, with or without hiding, and with or without renaming. Getting all of this straight in your head is quite tricky, so here is a table (lifted directly from the language reference manual) that roughly summarises the various possibilities:
+
However, there are more options: The module can be imported qualified, with or without hiding, and with or without renaming. Getting all of this straight in your head is quite tricky, so here is a table (lifted directly from the language reference manual) that roughly summarises the various possibilities:
   
 
Suppose that module <hask>Mod</hask> exports three functions named <hask>x</hask>, <hask>y</hask> and <hask>z</hask>. In that case:
 
Suppose that module <hask>Mod</hask> exports three functions named <hask>x</hask>, <hask>y</hask> and <hask>z</hask>. In that case:
Line 80: Line 80:
 
== See also ==
 
== See also ==
   
* [[Import modules properly]]
+
* [[Import modules properly]] - Some thoughts that may help to decide what form of the import statement to use.

Revision as of 10:39, 28 January 2011

The import statement is used to import functions and other definitions from another module. The shortest form of the import statement is

import Data.Maybe

that imports the named module (in this case Data.Maybe) and brings all of its identifiers into scope in their plain form.

However, there are more options: The module can be imported qualified, with or without hiding, and with or without renaming. Getting all of this straight in your head is quite tricky, so here is a table (lifted directly from the language reference manual) that roughly summarises the various possibilities:

Suppose that module Mod exports three functions named x, y and z. In that case:

Import command What is brought into scope Notes
import Mod x, y, z, Mod.x, Mod.y, Mod.z (By default, qualified and unqualified names.)
import Mod () (Nothing!) (Useful for only importing instances of typeclasses and nothing else)
import Mod (x,y) x, y, Mod.x, Mod.y (Only x and y, no z.)
import qualified Mod Mod.x, Mod.y, Mod.z (Only qualified versions; no unqualified versions.)
import qualified Mod (x,y) Mod.x, Mod.y (Only x and y, only qualified.)
import Mod hiding (x,y) z, Mod.z (x and y are hidden.)
import qualified Mod hiding (x,y) Mod.z (x and y are hidden.)
import Mod as Foo x, y, z, Foo.x, Foo.y, Foo.z (Unqualified names as before. Qualified names use Foo instead of Mod.)
import Mod as Foo (x,y) x, y, Foo.x, Foo.y (Only import x and y.)
import qualified Mod as Foo Foo.x, Foo.y, Foo.z (Only qualified names, using new qualifier.)

Note also that, by default, every module implicitly imports Prelude. However, if you add an explicit import declaration for the prelude, this turns off the implicit one. Thus, if you wanted (for example) to write a module that redefines zip you could do

module Mod where

import Prelude hiding (zip)

zip = {- ... -}

Without the import statement, you could receive a compile-time error about an 'ambiguous use of zip'. A slightly more messy alternative is to do

module Mod where

import qualified Prelude as P

zip = {- ... -}

This has the disadvantage that (say) 'P.show (2 P.+ 3 P.* 3) P.++ "abc"' is very messy to read. Typically a module only redefines a few prelude functions, and it's simpler to just hide the ones you don't want to clash with.

Note that any module using a module that redefines prelude functions will need to import either the prelude or the other module (or maybe both) qualified and/or with hiding for the same reason.

See also