Difference between revisions of "Import"
(neutral formulation in the introduction) 
(fix link to ghc user guide) 

(5 intermediate revisions by 5 users not shown)  
Line 5:  Line 5:  
import Data.Maybe 
import Data.Maybe 

</haskell> 
</haskell> 

−  that imports the named module (in this case <hask>Data.Maybe</hask>) 
+  that imports the named module (in this case <hask>Data.Maybe</hask>). 
⚫  
+  However, there are more options: 

⚫  
+  # Modules can be imported '''qualified''' (forcing an obligatory namespace qualifier to imported identifiers). 

+  # Some identifiers can be skipped via the '''hiding''' clause. 

+  # The module namespace can be renamed, with an '''as''' clause. 

+  
⚫  
+  
⚫  
{ 
{ 

Line 18:  Line 24:  
 
 

 <hask>import Mod</hask> 
 <hask>import Mod</hask> 

−   <hask>x, y, z, Mod.x, Mod.y, Mod.z</hask> 
+   <hask>x, y, z, (+++), Mod.x, Mod.y, Mod.z, (Mod.+++)</hask> 
 (By default, qualified ''and'' unqualified names.) 
 (By default, qualified ''and'' unqualified names.) 

 
 

Line 25:  Line 31:  
 (Useful for only importing instances of typeclasses and nothing else) 
 (Useful for only importing instances of typeclasses and nothing else) 

 
 

−   <hask>import Mod (x,y)</hask> 
+   <hask>import Mod (x,y, (+++))</hask> 
−   <hask>x, y, Mod.x, Mod.y</hask> 
+   <hask>x, y, (+++), Mod.x, Mod.y, (Mod.+++)</hask> 
−   (Only <hask>x</hask> and <hask> 
+   (Only <hask>x</hask>, <hask>y</hask>, and <hask>(+++)</hask>, no <hask>z</hask>.) 
 
 

 <hask>import qualified Mod</hask> 
 <hask>import qualified Mod</hask> 

−   <hask>Mod.x, Mod.y, Mod.z</hask> 
+   <hask>Mod.x, Mod.y, Mod.z, (Mod.+++)</hask> 
 (Only qualified versions; no unqualified versions.) 
 (Only qualified versions; no unqualified versions.) 

 
 

Line 37:  Line 43:  
 (Only <hask>x</hask> and <hask>y</hask>, only qualified.) 
 (Only <hask>x</hask> and <hask>y</hask>, only qualified.) 

 
 

−   <hask>import Mod hiding (x,y)</hask> 
+   <hask>import Mod hiding (x,y,(+++))</hask> 
 <hask>z, Mod.z</hask> 
 <hask>z, Mod.z</hask> 

 (<hask>x</hask> and <hask>y</hask> are hidden.) 
 (<hask>x</hask> and <hask>y</hask> are hidden.) 

 
 

 <hask>import qualified Mod hiding (x,y)</hask> 
 <hask>import qualified Mod hiding (x,y)</hask> 

−   <hask>Mod.z</hask> 
+   <hask>Mod.z, (Mod.+++)</hask> 
 (<hask>x</hask> and <hask>y</hask> are hidden.) 
 (<hask>x</hask> and <hask>y</hask> are hidden.) 

 
 

 <hask>import Mod as Foo</hask> 
 <hask>import Mod as Foo</hask> 

−   <hask>x, y, z, Foo.x, Foo.y, Foo.z</hask> 
+   <hask>x, y, z, (+++), Foo.x, Foo.y, Foo.z, (Foo.+++)</hask> 
 (Unqualified names as before. Qualified names use <hask>Foo</hask> instead of <hask>Mod</hask>.) 
 (Unqualified names as before. Qualified names use <hask>Foo</hask> instead of <hask>Mod</hask>.) 

 
 

Line 54:  Line 60:  
 
 

 <hask>import qualified Mod as Foo</hask> 
 <hask>import qualified Mod as Foo</hask> 

−   <hask>Foo.x, Foo.y, Foo.z</hask> 
+   <hask>Foo.x, Foo.y, Foo.z, (Foo.+++)</hask> 
 (Only qualified names, using new qualifier.) 
 (Only qualified names, using new qualifier.) 

+   

+   <hask>import qualified Mod as Foo (x,y)</hask> 

+   <hask>Foo.x, Foo.y</hask> 

+   (Only qualified versions of <hask>x</hask> and <hask>y</hask>, using new qualifier) 

} 
} 

⚫  
+  Note that multiple import statements for the same module are also allowed, so it is possible to mix and match styles if its so desired (for example, importing operators directly and functions qualified) 

+  
+  ===Hiding Prelude=== 

+  
⚫  
<haskell> 
<haskell> 

module Mod where 
module Mod where 

Line 81:  Line 95:  
* [[Import modules properly]]  Some thoughts that may help to decide what form of the import statement to use. 
* [[Import modules properly]]  Some thoughts that may help to decide what form of the import statement to use. 

+  
+  * {{GHCUsersGuideexts/package_qualified_importsa seciton about packagequalified import syntax}}. 
Latest revision as of 22:23, 24 July 2021
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
).
However, there are more options:
 Modules can be imported qualified (forcing an obligatory namespace qualifier to imported identifiers).
 Some identifiers can be skipped via the hiding clause.
 The module namespace can be renamed, with an as clause.
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:
Supposing that the module Mod
exports four functions named x
, y
, z
, and (+++)
...
Import command  What is brought into scope  Notes 

import Mod

x, y, z, (+++), Mod.x, Mod.y, Mod.z, (Mod.+++)

(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, (Mod.+++)

(Only x , y , and (+++) , no z .)

import qualified Mod

Mod.x, Mod.y, Mod.z, (Mod.+++)

(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, (Mod.+++)

(x and y are hidden.)

import Mod as Foo

x, y, z, (+++), Foo.x, Foo.y, Foo.z, (Foo.+++)

(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, (Foo.+++)

(Only qualified names, using new qualifier.) 
import qualified Mod as Foo (x,y)

Foo.x, Foo.y

(Only qualified versions of x and y , using new qualifier)

Note that multiple import statements for the same module are also allowed, so it is possible to mix and match styles if its so desired (for example, importing operators directly and functions qualified)
Hiding Prelude
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 compiletime 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
 Import modules properly  Some thoughts that may help to decide what form of the import statement to use.