Introduction to Modula-2
last modified November 27, 2026 Modula-2
Modula-2 is a compiled, statically typed, imperative programming language designed by Niklaus Wirth. It keeps the readable syntax of Pascal and adds the module, a unit of code with a separately compiled interface. This article introduces the language and its dialects, installs GNU Modula-2, and compiles the first programs.
What is Modula-2
Wirth designed Pascal at ETH Zurich in 1970 as a language for teaching structured programming. Pascal spread quickly, but it had no standard way to split a large program into separately compiled parts, and it gave no access to the machine. Wirth's next language, Modula, explored modules and concurrency for small systems. Modula-2 followed in 1978 and became the system language of the Lilith workstation, whose operating system and tools were written in it.
The language was described in Wirth's book Programming in Modula-2, usually called PIM. The book went through four editions between 1982 and 1988, and the editions differ in small details of the language. In 1996 the language was standardised as ISO/IEC 10514-1, which also defined a large standard library. Wirth himself moved on to Oberon in 1987, a smaller language that keeps modules and adds type extension.
Modula-2 was used for operating systems, embedded software, compilers, and teaching. Its ideas live on in Oberon and Modula-3, and in the module systems of later languages.
From Pascal to Modula-2
A Pascal programmer reads Modula-2 code without effort, but several rules change:
- Every structured statement ends with
END, soBEGINis no longer needed to group statements insideIF,WHILEorFOR. - Keywords and standard identifiers are upper-case, and all names are
case-sensitive:
countandCountare different variables. - Programs are built from modules. A definition module lists what a module exports; an implementation module contains the code and is compiled separately.
- Input and output are not part of the language. Even
WriteStringis a procedure imported from a library module. - The
SYSTEMmodule gives controlled access to addresses, words, and coroutines for low-level programming.
The porting Pascal to Modula-2 article shows these differences, and a few less obvious ones, on complete programs.
PIM and ISO
Two dialects of the language are in use. PIM is the language of
Wirth's book; it defines a small set of library modules such as
InOut, RealInOut, MathLib0 and
Storage. ISO Modula-2 refines the language, adds
exception handling, complex numbers and a few operators, and replaces the
small PIM modules with a layered library of channels and devices.
This series follows the history of the language. The language core, from the basics to opaque types and local modules, uses PIM and the modules of Wirth's book. The later articles on console and file input, command-line arguments, math, error handling and the system interface use the ISO library, because PIM never standardised these topics. The console output article explains the switch when it happens.
GNU Modula-2
The examples in this series are compiled with GNU Modula-2
(gm2), the Modula-2 front end of the GNU Compiler Collection.
It has been part of GCC since version 13 and produces native executables with
the usual GCC back end and linker. The front end is still young, and each
GCC release fixes bugs in it, so use the newest version available. On Debian
and Ubuntu it comes in versioned packages, which install the compiler as
gm2-15; a symbolic link makes it available as
gm2. Older Ubuntu releases get the package from the
ubuntu-toolchain-r/test PPA. The plain gm2
package follows the distribution's default GCC and may be too old; on
Ubuntu 22.04 it is version 11.
sudo apt install gm2-15
sudo ln -s /usr/bin/gm2-15 /usr/local/bin/gm2
GNU Modula-2 implements PIM and ISO Modula-2. The default dialect is PIM;
the -fiso option selects ISO. The compiler ships three
libraries: the GNU PIM library, the ISO library, and a library compatible with
the Logitech compiler, which provides the InOut and
RealInOut modules described in Wirth's book. The examples were
tested with GNU Modula-2 15.2.0.
The first program
A Modula-2 program is a program module. It starts with
MODULE and a name, imports the procedures it needs, and ends
with END, the same name, and a period. The statements between
BEGIN and END are the body of the module.
MODULE hello;
FROM InOut IMPORT WriteString, WriteLn;
BEGIN
WriteString("Hello there!");
WriteLn
END hello.
The FROM InOut IMPORT line makes two procedures of the
InOut module available. WriteString prints text and
WriteLn ends the line. Statements are separated by semicolons,
so the last statement before END needs none.
$ gm2 hello.mod -o hello $ ./hello Hello there!
Compiling and running
The gm2 command compiles the module and links it with the
library modules it imports into one executable. The
-o option names the result; without it the executable is called
a.out. The program module needs no special file name, but by
convention the file is named after the module.
MODULE greeting;
FROM InOut IMPORT WriteString, WriteLn;
BEGIN
WriteString("Modula-2 was designed by Niklaus Wirth.");
WriteLn;
WriteString("It was published in 1978.");
WriteLn
END greeting.
The program prints two lines. Each WriteLn ends a line; without
the first one both sentences would appear on the same line.
$ gm2 greeting.mod -o greeting $ ./greeting Modula-2 was designed by Niklaus Wirth. It was published in 1978.
Printing numbers
InOut also prints numbers. WriteInt prints an
integer and WriteCard a non-negative cardinal. The second
argument is the minimum field width; the number is padded with spaces on the
left, and a width of 0 prints the number without padding.
MODULE numbers;
FROM InOut IMPORT WriteString, WriteLn, WriteInt, WriteCard;
BEGIN
WriteString("Year: ");
WriteCard(1978, 0);
WriteLn;
WriteString("Editions of PIM: ");
WriteInt(4, 0);
WriteLn;
WriteString("Padded:");
WriteInt(-42, 6);
WriteLn
END numbers.
The last number is printed in a field of six characters, so three spaces
precede -42. Modula-2 has no printf-style
formatting in the language; output is built from calls like these.
$ gm2 numbers.mod -o numbers $ ./numbers Year: 1978 Editions of PIM: 4 Padded: -42
A compiler error
The compiler checks the program before it produces any code. The name after
the final END must repeat the module name, so a mismatch is an
error.
MODULE mismatch;
FROM InOut IMPORT WriteString, WriteLn;
BEGIN
WriteString("Hello there!");
WriteLn
END Mismatch.
Names are case-sensitive, so Mismatch is not the same name as
mismatch. The compiler reports the error at both ends of the
module and produces no executable.
$ gm2 mismatch.mod -o mismatch
mismatch.mod:1:8: error: In program module ‘mismatch’: module name at beginning ‘mismatch’ does not match the name at end ‘Mismatch’
1 | MODULE mismatch;
| ^~~~~~~~
mismatch.mod:8:5: error: module name at end ‘Mismatch’ does not match the name at beginning ‘mismatch’
8 | END Mismatch.
| ^~~~~~~~
Next steps
The Modula-2 basics article continues with variables, constants, assignment, and output. The program structure article describes the parts of a module, and the data types article surveys the basic types of the language.
Source
Modula-2 resources at modula2.org
In this article we have introduced Modula-2, its history and its two
dialects, installed GNU Modula-2, and compiled the first programs with
gm2. The next article, Modula-2
basics, turns these first examples into a working knowledge of variables,
constants, and expressions.
Author
List all Modula-2 tutorials.