liveBook · Manning

Import statement

As explained in Learn Haskell by Example

An import declaration is a Haskell declaration that makes definitions from another module available in the importing module.

An import declaration makes definitions from another Haskell module available to the importing module. A basic import brings in everything named by the imported module’s export list, or everything when no export list exists. section 2.1.3

Use imports to control how modules share code:

  • Import a module directly when its exported names do not conflict with names already in scope. section 2.1.3
  • Qualify a name with its module, such as ModuleName.x, when different modules provide definitions with the same name. section 2.1.3 section 5.3
  • Group related modules behind a top-level module that re-exports them, allowing clients to use one import instead of several. section 7.4.2

Importing Lib into Main

A module can export someFunc, and Main can import Lib before using it as its entry point: module Main where import Lib main :: IO () main = someFunc For larger projects, a Csv module can instead re-export Csv.Conversion and Csv.Types, so client modules import Csv once.

Listing 7.15 A re-export of modules
module Csv
  ( module Csv.Conversion,  #1
    module Csv.Types,
  )
where

import Csv.Conversion     #2
import Csv.Types

Inspect the re-export pattern that combines related CSV modules behind one public import. section 7.4.2

How Learn Haskell by Example covers it

How deep it goes
Explains exported definitions, qualified access, name conflicts, cyclical imports, and module re-export structure.
Examples
Uses Lib and Main, a conflicting Graph lookup, and a Csv facade re-exporting Conversion and Types.
What it assumes
Assumes basic Haskell module files and declarations while postponing detailed treatment of IO actions.
What you can do after
Apply it by importing modules, qualifying conflicting names, or exposing grouped submodules through a re-export.
How it connects
Connects Haskell modules with export lists, qualified names, Prelude, name conflicts, cyclical imports, and re-exporting modules.

Also explained in

Compare how 13 books explain import statement →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.