A program in several modules
Three files, one program
A program of a few hundred lines is easier to read in modules that each answer one question. The example has three files in one folder:
- Config.hs holds the values that the program is configured with.
- Report.hs turns a number into a line and imports Config for the threshold.
- Main.hs is the program: it imports both and prints.
The imports form a chain, Main to Report to Config, and the compiler walks it. A module must not import itself, directly or through another, so a program is a tree and never a cycle; the export lists are what keep the tree small.
Config: a module of values
module Config (threshold, label) where
threshold :: Int
threshold = 60
label :: String
label = "report"
Two constants and an export list: everything else in this module would be private, and there is nothing else. A module of values is a reasonable first module and a common place for a threshold, a table or a list of names; when the values are many, the next lessons keep them in a record and later in a Map.
Report: a module that imports Config
module Report (line) where
import Config (threshold)
line :: Int -> String
line value
| value >= threshold = "ok " ++ show value
| otherwise = "low " ++ show value
The module imports one name and exports one, which is the shape that keeps a dependency honest: a caller of Report does not get threshold from it. Both strings have the same length, so a list of lines lines up when it is printed, which the output below shows.
Main: the program
module Main (main) where
import Config (label)
import Report (line)
main :: IO ()
main = do
putStrLn label
mapM_ (putStrLn . line) [55, 60, 99]
The program is one do block. The interesting part is what is not here: no printing function in Report, no threshold in Main, and no knowledge of each other between Config and the program. Each module can be tested and replaced on its own.
The build and its artefacts
ghc -Wall -O1 Main.hs -o report
./report
[1 of 4] Compiling Config ( Config.hs, Config.o )
[2 of 4] Compiling Report ( Report.hs, Report.o )
[3 of 4] Compiling Main ( Main.hs, Main.o )
[4 of 4] Linking report
report
low 55
ok 60
ok 99
One command named one file and the compiler found the other two. It compiled them in dependency order and linked the executable last. The folder now holds two artefacts per module and one binary:
ls
Config.hi
Config.hs
Config.o
Main.hi
Main.hs
Main.o
Report.hi
Report.hs
Report.o
report
.hi is the interface: the types and the exports the other modules need. .o is the object code. A rebuild after changing Report.hs recompiles Report and then Main, because the interface of Report may have changed, and leaves Config alone. Running the compiler once per module with -c is possible, and it is what a build tool does for you; the next lessons give that job to Cabal.
Exercises
- Add a module Format.hs that pads a line to a width, and use it from Report.
- Move threshold into a record and pass it as an argument instead of importing it.
- Change the export list of Report to export threshold as well, then import it in Main.
- Touch Report.hs and rebuild; list the modules that were compiled.
- Compile each module with ghc -c and link by hand, and compare the commands with the one-line build.
- Explain why a cycle between two modules cannot be compiled.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
How many files does a module need?
Answer
One, named after the module
One source file per module, with the file name matching the module name.
-
What does
ghc Main.hscompile when Main imports Report and Report imports Config?Answer
Config, then Report, then Main, in dependency order
The compiler follows the imports and builds each module after the ones it needs.
-
What is a
.hifile?Answer
The interface of a compiled module
The interface holds the types and exports that other modules need to compile against.
-
What does this program print?
module Main (main) where import Data.Char (toUpper) main :: IO () main = putStrLn (map toUpper "ab")Answer
AB
map toUpper upper-cases the two characters and putStrLn prints them as one line.
-
Why can two modules not import each other?
Answer
The compiler would have to compile each before the other
A cycle has no build order, so the module graph must be a tree or a DAG.
-
What does a module with no export list export?
Answer
Everything it defines
The missing list means all top-level names, which is why libraries write the list.
-
What does this program print?
module Main (main) where import Data.List (intercalate) main :: IO () main = putStrLn (intercalate "-" ["a", "b"])Answer
a-b
intercalate puts the separator between the elements and not around them.
-
Which artefacts does a successful build leave for one module?
Answer
A .hi and a .o beside the source
The interface and the object are written beside the source, and the executable once at the end.
-
Why does changing Report.hs usually recompile Main.hs?
Answer
The interface of Report may have changed, so Main must be checked against it
A module that imports Report is compiled against its interface, so a changed interface means a rebuild.
-
What is the entry module of a program called?
Answer
MainThe runtime looks for Main.main, which is why the program file is module Main.
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