Modula-2 modules
last modified November 26, 2026 Modula-2
Modules are what gave Modula-2 its name. A library module has a definition module, which states what the module offers, and an implementation module, which holds the code. The two parts are compiled separately, and every program that imports the module is checked against the definition. The program structure article compiled a first library module with one procedure; this article exports constants and types, imports in two ways, hides data inside a module and follows the order in which modules start.
Exporting constants, types and procedures
A definition module may declare constants, types, variables and procedure headings. Every name it declares is exported: a program that imports the module can use it.
DEFINITION MODULE Geometry;
CONST
Pi = 3.14159265;
TYPE
Point = RECORD
x, y: REAL
END;
PROCEDURE SetPoint(VAR p: Point; x, y: REAL);
PROCEDURE Distance(a, b: Point): REAL;
PROCEDURE CircleArea(r: REAL): REAL;
END Geometry.
Geometry exports the constant Pi, the record type
Point and three procedures. The definition module contains no
code, only the headings of the procedures.
IMPLEMENTATION MODULE Geometry; FROM MathLib0 IMPORT sqrt; PROCEDURE SetPoint(VAR p: Point; x, y: REAL); BEGIN p.x := x; p.y := y END SetPoint; PROCEDURE Distance(a, b: Point): REAL; VAR dx, dy: REAL; BEGIN dx := b.x - a.x; dy := b.y - a.y; RETURN sqrt(dx * dx + dy * dy) END Distance; PROCEDURE CircleArea(r: REAL): REAL; BEGIN RETURN Pi * r * r END CircleArea; END Geometry.
The implementation module repeats each procedure heading and adds its body.
It does not repeat Pi or Point: the declarations
of the definition module are visible in the implementation module. It
imports sqrt for its own use; the import is not passed on to
the clients of Geometry.
MODULE distance;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
FROM Geometry IMPORT Point, SetPoint, Distance, CircleArea;
VAR
a, b: Point;
d: REAL;
BEGIN
SetPoint(a, 0.0, 0.0);
SetPoint(b, 3.0, 4.0);
d := Distance(a, b);
WriteString("distance:"); WriteReal(d, 12); WriteLn;
WriteString("area: "); WriteReal(CircleArea(d), 12); WriteLn
END distance.
The program imports the type and the procedures it needs. The library
module is compiled first with -c, which writes
Geometry.o, and the program is compiled and linked with that
object file.
$ gm2 -c Geometry.mod $ gm2 distance.mod Geometry.o -o distance $ ./distance distance: 5.000000 area: 78.539816
Qualified import
IMPORT Geometry imports the module itself instead of single
names. Every exported name is then written with the module name in front,
as Geometry.Point.
MODULE qualified;
IMPORT Geometry;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
VAR
p, q: Geometry.Point;
BEGIN
Geometry.SetPoint(p, 1.0, 1.0);
Geometry.SetPoint(q, 7.0, 9.0);
WriteString("distance:"); WriteReal(Geometry.Distance(p, q), 12); WriteLn;
WriteString("pi: "); WriteReal(Geometry.Pi, 12); WriteLn
END qualified.
The qualified names are longer, but they show at each use where a name
comes from, and the import list does not have to change when the program
starts using another procedure of the module. FROM … IMPORT
suits a few names used often, such as the output procedures of
InOut.
$ gm2 qualified.mod Geometry.o -o qualified $ ./qualified distance: 10.000000 pi: 3.141593
Name clashes
Two modules may export the same name. Circles and
Squares each export Area and
Perimeter.
DEFINITION MODULE Circles; PROCEDURE Area(radius: REAL): REAL; PROCEDURE Perimeter(radius: REAL): REAL; END Circles.
The procedures of Circles take a radius.
DEFINITION MODULE Squares; PROCEDURE Area(side: REAL): REAL; PROCEDURE Perimeter(side: REAL): REAL; END Squares.
The procedures of Squares take the length of a side.
MODULE clash; FROM InOut IMPORT WriteLn; FROM RealInOut IMPORT WriteReal; FROM Circles IMPORT Area; FROM Squares IMPORT Area; BEGIN WriteReal(Area(2.0), 10); WriteLn END clash.
A program cannot import Area from both modules with
FROM … IMPORT: the second import would declare a name that
already exists in the program. The compiler needs only the definition
modules to find the error, so -c is enough here.
$ gm2 -c clash.mod
clash.mod:7:1: error: In program module ‘clash’: name clash when trying to import (Area)
7 | BEGIN
| ^~~~~
Qualified names avoid clashes
With qualified import the clash disappears, because the names are written
Circles.Area and Squares.Area.
IMPLEMENTATION MODULE Circles; FROM Geometry IMPORT Pi; PROCEDURE Area(radius: REAL): REAL; BEGIN RETURN Pi * radius * radius END Area; PROCEDURE Perimeter(radius: REAL): REAL; BEGIN RETURN 2.0 * Pi * radius END Perimeter; END Circles.
Circles imports Pi from Geometry, so
one library module uses another.
IMPLEMENTATION MODULE Squares; PROCEDURE Area(side: REAL): REAL; BEGIN RETURN side * side END Area; PROCEDURE Perimeter(side: REAL): REAL; BEGIN RETURN 4.0 * side END Perimeter; END Squares.
Squares needs no imports.
MODULE areas;
IMPORT Circles, Squares;
FROM InOut IMPORT WriteString, WriteLn;
FROM RealInOut IMPORT WriteReal;
VAR
size: REAL;
BEGIN
size := 3.0;
WriteString("circle area: "); WriteReal(Circles.Area(size), 10); WriteLn;
WriteString("circle perimeter:"); WriteReal(Circles.Perimeter(size), 10); WriteLn;
WriteString("square area: "); WriteReal(Squares.Area(size), 10); WriteLn;
WriteString("square perimeter:"); WriteReal(Squares.Perimeter(size), 10); WriteLn
END areas.
The program imports both modules whole and calls all four procedures. The
link step lists Geometry.o too, because Circles
uses it; every module that the program needs, directly or through another
module, must be linked.
$ gm2 -c Circles.mod $ gm2 -c Squares.mod $ gm2 areas.mod Circles.o Squares.o Geometry.o -o areas $ ./areas circle area: 28.274334 circle perimeter: 18.849556 square area: 9.000000 square perimeter: 12.000000
Module state
Variables declared in an implementation module keep their values for the whole run of the program. They belong to the module, not to any procedure, and are initialised in the module body.
DEFINITION MODULE Tickets; PROCEDURE Next(): CARDINAL; PROCEDURE Issued(): CARDINAL; END Tickets.
Tickets hands out numbered tickets. Its definition module
exports two procedures and nothing else.
IMPLEMENTATION MODULE Tickets; CONST First = 100; VAR last: CARDINAL; PROCEDURE Next(): CARDINAL; BEGIN INC(last); RETURN last END Next; PROCEDURE Issued(): CARDINAL; BEGIN RETURN last - First + 1 END Issued; BEGIN last := First - 1 END Tickets.
The constant First and the variable last are
declared in the implementation module, so only the module itself can use
them. The body sets last before the program starts, and
Next increments it on each call.
MODULE tickets;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
IMPORT Tickets;
VAR
i: CARDINAL;
BEGIN
FOR i := 1 TO 3 DO
WriteString("ticket "); WriteCard(Tickets.Next(), 0); WriteLn
END;
WriteString("issued: "); WriteCard(Tickets.Issued(), 0); WriteLn
END tickets.
The program sees only Next and Issued. It cannot
tell how the numbers are kept, and it cannot change them except through the
two procedures.
$ gm2 -c Tickets.mod $ gm2 tickets.mod Tickets.o -o tickets $ ./tickets ticket 100 ticket 101 ticket 102 issued: 3
Hidden names
A name declared only in the implementation module is hidden from clients. The next program tries to reset the ticket counter directly.
MODULE peek; FROM InOut IMPORT WriteCard, WriteLn; IMPORT Tickets; BEGIN Tickets.last := 99; WriteCard(Tickets.Next(), 0); WriteLn END peek.
The compiler looks up last in the definition module of
Tickets, does not find it, and rejects the program. A
definition module may also export a variable, but then every client can
change it; exporting procedures keeps the variable under the control of the
module.
$ gm2 -c peek.mod
peek.mod:6:11: error: In definition module ‘Tickets’: unknown symbol ‘last’
6 | Tickets.last := 99;
| ^~~~
peek.mod:6:16: error: In program module ‘peek’: symbol last is not exported from definition module Tickets
6 | Tickets.last := 99;
| ^~
./Tickets.def:6:12: error: In definition module ‘Tickets’: the following unknown symbols in module ‘Tickets’ were unresolved: last
6 | END Tickets.
| ^
EXPORT QUALIFIED
In the first edition of Wirth's book, a definition module listed its
exports after the heading, with EXPORT QUALIFIED. Later
editions export every name of the definition module automatically, but
older code and some libraries keep the list. The Logitech
Strings module of GNU Modula-2 is one of them.
DEFINITION MODULE Convert; EXPORT QUALIFIED CToF, FToC; PROCEDURE CToF(c: REAL): REAL; PROCEDURE FToC(f: REAL): REAL; END Convert.
The export list names both procedures. QUALIFIED means that a
client which imports the module whole writes the names as
Convert.CToF; with FROM Convert IMPORT it uses
them unqualified, as with any other module.
IMPLEMENTATION MODULE Convert; PROCEDURE CToF(c: REAL): REAL; BEGIN RETURN c * 9.0 / 5.0 + 32.0 END CToF; PROCEDURE FToC(f: REAL): REAL; BEGIN RETURN (f - 32.0) * 5.0 / 9.0 END FToC; END Convert.
The implementation module is the same as without the export list.
MODULE temps;
FROM InOut IMPORT WriteString, WriteInt, WriteLn;
FROM RealInOut IMPORT WriteReal;
FROM Convert IMPORT CToF, FToC;
VAR
c: INTEGER;
BEGIN
FOR c := -10 TO 30 BY 10 DO
WriteInt(c, 4); WriteString(" C ="); WriteReal(CToF(FLOAT(c)), 12);
WriteString(" F"); WriteLn
END;
WriteString("100 F ="); WriteReal(FToC(100.0), 11); WriteString(" C"); WriteLn
END temps.
GNU Modula-2 accepts the export list in every PIM dialect and in ISO mode, and the program imports the procedures as usual. New code leaves the list out.
$ gm2 -c Convert.mod $ gm2 temps.mod Convert.o -o temps $ ./temps -10 C = 14.000000 F 0 C = 32.000000 F 10 C = 50.000000 F 20 C = 68.000000 F 30 C = 86.000000 F 100 F = 37.777778 C
Initialisation order
The body of every imported module runs once before the body of the program. When modules import each other, a module's body runs after the bodies of the modules it imports.
DEFINITION MODULE Log; PROCEDURE Note(msg: ARRAY OF CHAR); END Log.
Log prints numbered messages.
IMPLEMENTATION MODULE Log;
FROM InOut IMPORT WriteString, WriteCard, WriteLn;
VAR
line: CARDINAL;
PROCEDURE Note(msg: ARRAY OF CHAR);
BEGIN
INC(line);
WriteCard(line, 2); WriteString(" "); WriteString(msg); WriteLn
END Note;
BEGIN
line := 0;
Note("Log initialised")
END Log.
The body of Log sets the line counter to zero and writes the
first message.
DEFINITION MODULE Store; PROCEDURE Save(item: ARRAY OF CHAR); END Store.
Store saves items; here it only logs them.
IMPLEMENTATION MODULE Store;
IMPORT Log;
PROCEDURE Save(item: ARRAY OF CHAR);
BEGIN
Log.Note(item)
END Save;
BEGIN
Log.Note("Store initialised")
END Store.
Store imports Log and uses it in its own body.
For that to work, Log must be initialised first.
MODULE startup;
IMPORT Store, Log;
BEGIN
Log.Note("program starts");
Store.Save("first item");
Store.Save("second item")
END startup.
The program imports Store before Log, yet the
Log body runs first, because Store depends on it.
The order of the object files on the command line does not matter either.
Modules that import each other in a cycle have no such order, and their
bodies should not rely on one another.
$ gm2 -c Log.mod $ gm2 -c Store.mod $ gm2 startup.mod Store.o Log.o -o startup $ ./startup 1 Log initialised 2 Store initialised 3 program starts 4 first item 5 second item
A module for fractions
The last module stores fractions as records of a numerator and a
denominator. Make always reduces a fraction and keeps the sign
in the numerator, so every fraction has one form.
DEFINITION MODULE Fractions;
TYPE
Fraction = RECORD
num, den: INTEGER
END;
PROCEDURE Make(VAR f: Fraction; n, d: INTEGER);
PROCEDURE Add(a, b: Fraction; VAR sum: Fraction);
PROCEDURE Write(f: Fraction);
END Fractions.
The definition module exports the record type with its fields and three
procedures. Following the records article,
results are returned through VAR parameters.
IMPLEMENTATION MODULE Fractions;
FROM InOut IMPORT WriteString, WriteInt;
PROCEDURE Gcd(a, b: INTEGER): INTEGER;
VAR
t: INTEGER;
BEGIN
a := ABS(a);
b := ABS(b);
WHILE b # 0 DO
t := a MOD b; a := b; b := t
END;
RETURN a
END Gcd;
PROCEDURE Make(VAR f: Fraction; n, d: INTEGER);
VAR
g: INTEGER;
BEGIN
IF d < 0 THEN n := -n; d := -d END;
g := Gcd(n, d);
f.num := n DIV g;
f.den := d DIV g
END Make;
PROCEDURE Add(a, b: Fraction; VAR sum: Fraction);
BEGIN
Make(sum, a.num * b.den + b.num * a.den, a.den * b.den)
END Add;
PROCEDURE Write(f: Fraction);
BEGIN
WriteInt(f.num, 0); WriteString("/"); WriteInt(f.den, 0)
END Write;
END Fractions.
Gcd is a helper that is not exported. Make moves
a negative sign from the denominator to the numerator and divides both by
their greatest common divisor; Add builds the sum with
Make, so the sum is reduced too.
MODULE fractions;
FROM InOut IMPORT WriteString, WriteLn;
FROM Fractions IMPORT Fraction, Make, Add, Write;
VAR
a, b, sum, bad: Fraction;
BEGIN
Make(a, 1, 2);
Make(b, 6, -9);
Add(a, b, sum);
Write(a); WriteString(" + "); Write(b);
WriteString(" = "); Write(sum); WriteLn;
bad.num := 3;
bad.den := 0;
Write(bad); WriteLn
END fractions.
6/-9 is stored as -2/3, and the sum is reduced to -1/6. The last lines show
the weakness of the module: because the definition module exports the
fields, the program can bypass Make and store a fraction with
a zero denominator. The opaque types
article hides the fields so that this cannot happen.
$ gm2 -c Fractions.mod $ gm2 fractions.mod Fractions.o -o fractions $ ./fractions 1/2 + -2/3 = -1/6 3/0
Source
Modula-2 language reference at modula2.org
In this article we have written definition and implementation modules that
export constants, types and procedures, imported them by name and whole,
resolved a name clash with qualified names, kept state hidden inside a
module, used an EXPORT QUALIFIED list, and followed the order
in which module bodies run. The
program structure article
introduces the three kinds of module, the
opaque types article hides a type's
fields, and the local modules article
nests modules inside a program.
Author
List all Modula-2 tutorials.