Modules and exports
A file is a module
Every Haskell file is a module with a name, and the file name has to match it: Geometry.hs holds module Geometry where. The module name may be dotted, and then the folders follow the dots: Shop/Price.hs holds module Shop.Price where, which is what the Cabal lessons use.
module Geometry (Shape (..), area, perimeter) where
module Geometry where
Without an export list the module exports everything it defines, which is fine while a file is small and a promise you cannot keep in a library. The list is the interface: it says what a caller may use and, just as importantly, what it may not.
The export list
The shape of a list entry follows what is exported:
- area exports one function.
- Shape (..) exports the type and every constructor.
- Shape (Circle) exports the type but only one constructor, so the others cannot be built by a caller.
- Shape alone exports the type and no constructor, which is an abstract type: a caller can pass one around and use the functions of the module, and cannot look inside.
The module below exports the type with its constructors, because a program that builds shapes needs them, and the two functions that work on any shape:
module Geometry
( Shape (..)
, area
, perimeter
) where
data Shape
= Circle Double
| Rectangle Double Double
area :: Shape -> Double
area (Circle r) = pi * r * r
area (Rectangle w h) = w * h
perimeter :: Shape -> Double
perimeter (Circle r) = 2 * pi * r
perimeter (Rectangle w h) = 2 * (w + h)
A module file that is not the program has no main and no module Main. It compiles on its own with ghc -c, which is what the next section does when it builds the program.
Import lists and qualified names
An import brings names into scope. The list after the module name limits what comes in, and qualified makes the caller write the module name as a prefix, which is how two functions with the same name live in one file:
import Geometry (Shape (..), area, perimeter) -- the names, unqualified
import Data.List (sort) -- one name only
import qualified Data.List as L -- everything, behind L.
import Data.Map hiding (map) -- everything except map
The program uses both forms of the same module. The qualified name is also the newer spelling import Data.List qualified as L, which is in GHC2021 and not in Haskell 2010; the lesson on the two language versions shows the difference.
module Main (main) where
import Data.List (sort)
import qualified Data.List as L
main :: IO ()
main = do
print (L.sort [3, 1, 2 :: Int])
print (sort ["pear", "apple"])
print (L.group [1, 1, 2, 2, 3 :: Int])
ghc -Wall -O1 -o lists lists.hs
./lists
[1,2,3]
["apple","pear"]
[[1,1],[2,2],[3]]
Two imports of one module are allowed and do not compile it twice: sort came in unqualified and group was reached through L.. The warning flag -Wall reports an import whose names are all unused, which is why the list is worth keeping short.
Two files in one program
The program that uses the geometry module is a second file, and ghc Main.hs finds Geometry.hs beside it, compiles both and links one executable. The main file imports the names it needs and nothing else:
module Main (main) where
import Geometry (Shape (..), area, perimeter)
import Text.Printf (printf)
shapes :: [Shape]
shapes = [Circle 1, Rectangle 2 3]
main :: IO ()
main = mapM_ report shapes
where
report shape =
printf "area %.2f perimeter %.2f\n" (area shape) (perimeter shape)
ghc -Wall -O1 Geometry.hs Main.hs -o shapes
./shapes
area 3.14 perimeter 6.28
area 6.00 perimeter 10.00
The command names both files, and the order does not matter: the compiler reads Main.hs, sees the import of Geometry, looks for Geometry.hs in the same folder and compiles it first. It writes Geometry.hi and Geometry.o beside the source, then the same two for the main module, then the executable. A missing file is a compile error that names the module, not the path.
hiding, re-exporting and the name clash
Three rules cover most of the confusion:
- import M hiding (x) brings everything except x. It is the tool for a module that clashes with the Prelude, such as Data.List.nub or Data.Map.map.
- Two imports that bring the same name make that name ambiguous at its use, not at the import; the fix is to qualify one of them.
- A module can re-export another with module Geometry in its export list, which is how a library offers one front door for several modules.
module Shapes (module Geometry, describe) where
import Geometry
describe :: Shape -> String
describe (Circle r) = "circle of " ++ show r
describe (Rectangle w h) = show w ++ " by " ++ show h
A re-exporting module with no main is a normal library module. The next lesson takes this further: three modules, one program, and the build command that produces it.
Exercises
- Add a Triangle constructor to Geometry and the two functions that handle it.
- Change the export list to Shape (Circle) and explain the error the main file gets.
- Import Data.List qualified and unqualified in one program and use both.
- Write a module that re-exports Geometry and adds one function of its own.
- Explain why the file name has to match the module name.
- Build the two files with ghc -c separately and then link, and compare the artefacts with the one-command build.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is a Haskell file?
Answer
A module whose name matches the file name
The module header names the module and the compiler finds it by file name.
-
What does
Shape (..)in an export list export?Answer
The type and all its constructors
The (..) means every constructor, while the bare name would export the type abstractly.
-
What does
import qualified Data.Map as Mchange?Answer
The names are only reachable with the M. prefix
A qualified import puts the names behind the alias and avoids clashes.
-
What does this program print?
module Main (main) where import Data.List (sort) main :: IO () main = print (sort [2, 1, 3 :: Int])Answer
[1,2,3]
sort returns the ascending order of the input.
-
What happens when two imports bring the same name unqualified?
Answer
The name is ambiguous where it is used
The ambiguity is reported at the use of the name, and qualifying one import fixes it.
-
What does
import Data.List hiding (sort)do?Answer
Imports everything except sort
hiding removes the listed names from an otherwise complete import.
-
What does
ghc Main.hsdo when Main.hs imports Geometry?Answer
Compiles Geometry.hs and Main.hs and links one executable
The compiler follows the imports and compiles the modules it finds beside the main file.
-
What is exported by
module Geometry wherewith no list?Answer
Every top-level name the module defines
The missing list means everything, which is why a library writes the list explicitly.
-
Where does the compiler look for
import Shop.Price?Answer
In Shop/Price.hs relative to the search path
A dotted module name maps to a folder path, which is why the Cabal lesson uses src/Shop/Price.hs.
-
What does a re-exporting module put in its export list?
Answer
module Geometryto pass its exports throughThe module keyword in an export list re-exports everything that module exports.
The code on this page was built with GHC 9.10.3 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev