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:

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:

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

  1. Add a Triangle constructor to Geometry and the two functions that handle it.
  2. Change the export list to Shape (Circle) and explain the error the main file gets.
  3. Import Data.List qualified and unqualified in one program and use both.
  4. Write a module that re-exports Geometry and adds one function of its own.
  5. Explain why the file name has to match the module name.
  6. 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.

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

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

  3. What does import qualified Data.Map as M change?

    Answer

    The names are only reachable with the M. prefix

    A qualified import puts the names behind the alias and avoids clashes.

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

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

  6. What does import Data.List hiding (sort) do?

    Answer

    Imports everything except sort

    hiding removes the listed names from an otherwise complete import.

  7. What does ghc Main.hs do 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.

  8. What is exported by module Geometry where with no list?

    Answer

    Every top-level name the module defines

    The missing list means everything, which is why a library writes the list explicitly.

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

  10. What does a re-exporting module put in its export list?

    Answer

    module Geometry to pass its exports through

    The 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