liveBook · Manning

Import/export syntax

As explained in Learn Haskell by Example

A module export is a definition made visible to code that imports the module containing it.

Haskell modules group related definitions and control which names other modules can see. An export list follows the module name and names the definitions made available to importers. section 2.1.3

Use imports and exports to connect modules without exposing every definition:

  • Write an export list such as `(someFunc)` after the module name to expose selected definitions. section 2.1.3
  • Use `import Lib` to bring in definitions listed by `Lib`; without an export list, the module imports everything. section 2.1.3
  • Resolve clashing names by qualifying them with the module name, as in `ModuleName.x`. section 2.1.3
  • Use module re-exports to present several related modules through one import. section 7.4.2

Expose a function from Lib

Define `someFunc` in `Lib` and export it with `module Lib (someFunc) where`. Then write `import Lib` in `Main` and call `someFunc` from `main`. The export list makes the function available to the importing module.

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

A top-level `Csv` module re-exports `Csv.Conversion` and `Csv.Types`, allowing clients to import one module for related CSV definitions. section 7.4.2

How Learn Haskell by Example covers it

How deep it goes
Defines basic module, import, qualification, and re-export mechanics through examples, without explaining IO-action details.
Examples
Uses `Lib` and `Main` modules, an exported `someFunc`, qualified names, and a bundled `Csv` module.
What it assumes
Starts with module files, paths, export lists, and imports before requiring understanding of IO actions.
What you can do after
Apply Haskell module syntax to expose definitions, import them elsewhere, qualify clashes, and bundle related exports.
How it connects
Connects Haskell modules, imports, name clashes, re-exporting modules, data types, and smart constructors.

Also explained in

Compare how 5 books explain import/export syntax →

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.