Modula-2 enumerations and subranges
last modified November 26, 2026 Modula-2
An enumeration type lists its values by name, such as the days of the week or the phases of a project. A subrange type takes a part of an existing type, such as the months 1 to 12. Both make a program say what values a variable may hold, and both can be checked by the compiler and at run time.
Enumeration types
An enumeration type is declared by listing its values in parentheses. Each
name becomes a constant of the new type. The values are ordered as they are
listed, so they can be compared with < and
>.
MODULE payday;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
VAR
today, payday: Day;
BEGIN
today := Wed;
payday := Fri;
WriteString("today is day number ");
WriteCard(ORD(today), 0);
WriteLn;
IF today < payday THEN
WriteString("days until payday: ");
WriteCard(ORD(payday) - ORD(today), 0);
WriteLn
END
END payday.
Day has seven values, Mon to Sun.
ORD gives the position of a value in the list, starting from
0, so ORD(Wed) is 2. today < payday compares
the positions. An enumeration value cannot be used as a number without
ORD: payday - today would be a type error.
$ gm2 payday.mod -o payday $ ./payday today is day number 2 days until payday: 2
Writing the names
The names of the values exist only in the source code. The program cannot
print them directly; a procedure maps each value to its text. A
CASE statement over an enumeration is the natural way to do
that.
MODULE names;
FROM InOut IMPORT WriteString, WriteLn;
TYPE
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
VAR
today: Day;
PROCEDURE WriteDay(d: Day);
BEGIN
CASE d OF
Mon: WriteString("Monday") |
Tue: WriteString("Tuesday") |
Wed: WriteString("Wednesday") |
Thu: WriteString("Thursday") |
Fri: WriteString("Friday") |
Sat: WriteString("Saturday") |
Sun: WriteString("Sunday")
END
END WriteDay;
PROCEDURE IsWeekend(d: Day): BOOLEAN;
BEGIN
RETURN (d = Sat) OR (d = Sun)
END IsWeekend;
BEGIN
today := Sat;
WriteDay(today);
IF IsWeekend(today) THEN
WriteString(" is a day off")
ELSE
WriteString(" is a working day")
END;
WriteLn
END names.
WriteDay has one CASE label for each value, so it
needs no ELSE. IsWeekend compares the parameter
with two values. An enumeration is easier to read than numbers 0 to 6: a
call such as IsWeekend(Sat) says what it means.
$ gm2 names.mod -o names $ ./names Saturday is a day off
ORD, VAL, MIN and MAX
MIN and MAX give the first and the last value of
an enumeration type. VAL(T, n) is the inverse of
ORD: it gives the value of type T at position
n.
MODULE week;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
VAR
d: Day;
PROCEDURE Next(day: Day): Day;
BEGIN
IF day = MAX(Day) THEN
RETURN MIN(Day)
END;
RETURN VAL(Day, ORD(day) + 1)
END Next;
BEGIN
WriteString("first and last: ");
WriteCard(ORD(MIN(Day)), 0);
WriteString(" ");
WriteCard(ORD(MAX(Day)), 0);
WriteLn;
FOR d := MIN(Day) TO MAX(Day) DO
WriteCard(ORD(d), 2)
END;
WriteLn;
d := VAL(Day, 4);
WriteString("VAL(Day, 4) has ordinal ");
WriteCard(ORD(d), 0);
WriteLn;
WriteString("after Sun comes day ");
WriteCard(ORD(Next(Sun)), 0);
WriteLn;
WriteString("FALSE and TRUE: ");
WriteCard(ORD(FALSE), 0);
WriteString(" ");
WriteCard(ORD(TRUE), 0);
WriteLn
END week.
A FOR loop from MIN(Day) to MAX(Day)
visits every day without naming the first and the last one.
Next moves to the following day and wraps from
Sun to Mon; it computes the position with
ORD and turns it back into a day with VAL.
BOOLEAN is an enumeration too, with the values
FALSE and TRUE in this order.
$ gm2 week.mod -o week $ ./week first and last: 0 6 0 1 2 3 4 5 6 VAL(Day, 4) has ordinal 4 after Sun comes day 0 FALSE and TRUE: 0 1
INC and DEC
INC and DEC move an enumeration variable to the
next or the previous value. With a second argument they move it by that
many steps.
MODULE steps;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Phase = (Planned, Started, Testing, Review, Done);
VAR
p: Phase;
BEGIN
p := Planned;
WHILE p < Done DO
WriteCard(ORD(p), 2);
INC(p)
END;
WriteLn;
p := Planned;
INC(p, 3);
WriteString("three steps from Planned: ");
WriteCard(ORD(p), 0);
WriteLn;
DEC(p);
WriteString("one step back: ");
WriteCard(ORD(p), 0);
WriteLn
END steps.
The WHILE loop walks through the phases until it reaches
Done. INC(p, 3) moves three phases forward and
DEC(p) one back. The loop tests p < Done
before each step, so p never moves past the last value.
$ gm2 steps.mod -o steps $ ./steps 0 1 2 3 three steps from Planned: 3 one step back: 2
Stepping past the last value
INC on the last value of an enumeration gives a value that
does not exist. GNU Modula-2 does not check this in the default build.
MODULE overrun;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Phase = (Planned, Started, Testing, Review, Done);
VAR
p: Phase;
BEGIN
p := Review;
INC(p);
INC(p);
WriteString("ordinal after two steps: ");
WriteCard(ORD(p), 0);
WriteLn
END overrun.
The second INC moves p past Done,
the value with ordinal 4. The default build prints ordinal 5, a value the
type does not have. With -fsoft-check-all the program stops at
the second INC with exit status 134.
$ gm2 overrun.mod -o overrun $ ./overrun ordinal after two steps: 5 $ gm2 -fsoft-check-all overrun.mod -o overrun $ ./overrun overrun.mod:14:9: In program module overrun overrun.mod:14:9:if the INC is ever executed the expression will cause an overflow error for the designator ‘p’ as it exceeds the type range of ‘Phase’ Aborted (core dumped) $ echo $? 134
Names must be unique
The values of an enumeration are declared in the scope of the type, like constants. Two enumeration types in one scope cannot use the same name for a value.
MODULE lights; TYPE Colour = (Red, Green, Blue); Signal = (Red, Amber, Green); VAR c: Colour; s: Signal; BEGIN c := Blue; s := Amber END lights.
Red and Green would belong to both
Colour and Signal. The compiler rejects the
second declaration of each name. Prefixes such as sigRed or
different words keep the names apart.
$ gm2 lights.mod -o lights
lights.mod:4:13: error: symbol ‘Red’ is already declared in this scope, use a different name or remove the declaration
4 | Colour = (Red, Green, Blue);
| ^~~
lights.mod:5:13: error: symbol ‘Red’ also declared in this module
5 | Signal = (Red, Amber, Green);
| ^~~
lights.mod:4:18: error: symbol ‘Green’ is already declared in this scope, use a different name or remove the declaration
4 | Colour = (Red, Green, Blue);
| ^~~~~
lights.mod:5:25: error: symbol ‘Green’ also declared in this module
5 | Signal = (Red, Amber, Green);
| ^~~~~
Arrays indexed by enumerations
An enumeration type can be the index type of an array.
ARRAY Day OF CARDINAL has one element for each day, selected
by the day itself. An array of names replaces the CASE
statement of WriteDay.
MODULE hours;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
Name = ARRAY [0..9] OF CHAR;
VAR
names: ARRAY Day OF Name;
worked: ARRAY Day OF CARDINAL;
d: Day;
total: CARDINAL;
BEGIN
names[Mon] := "Monday";
names[Tue] := "Tuesday";
names[Wed] := "Wednesday";
names[Thu] := "Thursday";
names[Fri] := "Friday";
names[Sat] := "Saturday";
names[Sun] := "Sunday";
FOR d := MIN(Day) TO MAX(Day) DO
worked[d] := 0
END;
worked[Mon] := 8;
worked[Tue] := 7;
worked[Wed] := 9;
worked[Thu] := 8;
worked[Fri] := 6;
total := 0;
FOR d := MIN(Day) TO MAX(Day) DO
IF worked[d] > 0 THEN
WriteCard(worked[d], 3);
WriteString(" ");
WriteString(names[d]);
WriteLn
END;
INC(total, worked[d])
END;
WriteCard(total, 3);
WriteString(" hours in total");
WriteLn
END hours.
names holds the text of each day and worked the
hours worked on it. Both arrays are indexed by Day, so a
single loop variable selects matching elements in both. Indexing them with
a number such as worked[0] would be a type error.
$ gm2 hours.mod -o hours $ ./hours 8 Monday 7 Tuesday 9 Wednesday 8 Thursday 6 Friday 38 hours in total
Subrange types
A subrange type takes a contiguous part of a whole-number type,
CHAR or an enumeration. It is written as two constants in
square brackets. A subrange variable can be used wherever a value of the
base type is expected.
MODULE ranges;
FROM InOut IMPORT WriteString, WriteLn, WriteCard, WriteInt, Write;
TYPE
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
WorkDay = [Mon..Fri];
Month = [1..12];
Celsius = [-40..50];
Grade = ['A'..'E'];
VAR
w: WorkDay;
d: Day;
m: Month;
t: Celsius;
g: Grade;
BEGIN
m := 10;
WriteString("month ");
WriteCard(m, 0);
WriteString(" of ");
WriteCard(MAX(Month), 0);
WriteLn;
t := -12;
WriteString("temperature ");
WriteInt(t, 0);
WriteString(", lowest allowed ");
WriteInt(MIN(Celsius), 0);
WriteLn;
WriteString("grades: ");
FOR g := MIN(Grade) TO MAX(Grade) DO
Write(g)
END;
WriteLn;
w := Wed;
d := w;
WriteString("working days end at ordinal ");
WriteCard(ORD(MAX(WorkDay)), 0);
WriteString(", d is ");
WriteCard(ORD(d), 0);
WriteLn
END ranges.
WorkDay is a part of Day, Month and
Celsius are parts of the whole numbers, and
Grade is a part of CHAR. MIN and
MAX give the bounds of a subrange. d := w assigns
a working day to a Day variable; the opposite direction is
allowed too, but the value must lie in the subrange.
$ gm2 ranges.mod -o ranges $ ./ranges month 10 of 12 temperature -12, lowest allowed -40 grades: ABCDE working days end at ordinal 4, d is 2
Range checks
A value assigned to a subrange variable must lie within its bounds. For a
constant the compiler checks it: m := 13 draws a warning that
the value exceeds the range of Month. A value computed at run
time is checked only with -fsoft-check-all.
MODULE months;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Month = [1..12];
VAR
m: Month;
n: CARDINAL;
BEGIN
n := 11;
WHILE n <= 13 DO
m := n;
WriteString("month ");
WriteCard(m, 0);
WriteLn;
INC(n)
END
END months.
The loop assigns 11, 12 and 13 to m. The default build
accepts 13 and prints it as a month. With -fsoft-check-all the
program stops at the assignment of 13 with exit status 134. A subrange type
is a cheap way to catch such values while a program is tested.
$ gm2 months.mod -o months $ ./months month 11 month 12 month 13 $ gm2 -fsoft-check-all months.mod -o months $ ./months month 11 month 12 months.mod:15:5: In program module months months.mod:15:5:assignment will cause a range error, as the runtime instance value of ‘Month’ does not overlap with the type ‘CARDINAL’ Aborted (core dumped) $ echo $? 134
Subranges in procedures
Subrange types document the parameters and results of procedures. A
parameter of type Month says that only 1 to 12 make sense,
and a result of type Days says what the caller gets back.
MODULE calendar;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Month = [1..12];
Days = [28..31];
VAR
m: Month;
total: CARDINAL;
PROCEDURE IsLeap(year: CARDINAL): BOOLEAN;
BEGIN
RETURN (year MOD 4 = 0) AND (year MOD 100 # 0) OR (year MOD 400 = 0)
END IsLeap;
PROCEDURE DaysIn(month: Month; year: CARDINAL): Days;
BEGIN
CASE month OF
4, 6, 9, 11: RETURN 30 |
2: IF IsLeap(year) THEN RETURN 29 ELSE RETURN 28 END
ELSE
RETURN 31
END
END DaysIn;
BEGIN
total := 0;
FOR m := MIN(Month) TO MAX(Month) DO
WriteCard(DaysIn(m, 2028), 3);
INC(total, DaysIn(m, 2028))
END;
WriteLn;
WriteString("days in 2028: ");
WriteCard(total, 0);
WriteLn
END calendar.
DaysIn returns the length of a month. The CASE
labels list the short months and ELSE covers the rest. The
FOR loop runs over Month itself, from
MIN(Month) to MAX(Month). 2028 is a leap year,
so February has 29 days.
$ gm2 calendar.mod -o calendar $ ./calendar 31 29 31 30 31 30 31 31 30 31 30 31 days in 2028: 366
The day of the week
The last program combines both kinds of types. It computes the day of the
week for a date with a formula by Tomohiko Sakamoto and returns it as a
Day.
MODULE weekday;
FROM InOut IMPORT WriteString, WriteLn, WriteCard;
TYPE
Day = (Mon, Tue, Wed, Thu, Fri, Sat, Sun);
Month = [1..12];
DayOfMonth = [1..31];
Name = ARRAY [0..9] OF CHAR;
VAR
names: ARRAY Day OF Name;
offset: ARRAY Month OF CARDINAL;
PROCEDURE DayOfWeek(year: CARDINAL; month: Month; day: DayOfMonth): Day;
VAR
y, sunday: CARDINAL;
BEGIN
y := year;
IF month < 3 THEN
DEC(y)
END;
sunday := (y + y DIV 4 - y DIV 100 + y DIV 400 + offset[month] + day) MOD 7;
RETURN VAL(Day, (sunday + 6) MOD 7)
END DayOfWeek;
PROCEDURE Show(year: CARDINAL; month: Month; day: DayOfMonth);
BEGIN
WriteCard(year, 4);
WriteString("-");
WriteCard(month, 0);
WriteString("-");
WriteCard(day, 0);
WriteString(" was a ");
WriteString(names[DayOfWeek(year, month, day)]);
WriteLn
END Show;
BEGIN
names[Mon] := "Monday"; names[Tue] := "Tuesday";
names[Wed] := "Wednesday"; names[Thu] := "Thursday";
names[Fri] := "Friday"; names[Sat] := "Saturday";
names[Sun] := "Sunday";
offset[1] := 0; offset[2] := 3; offset[3] := 2; offset[4] := 5;
offset[5] := 0; offset[6] := 3; offset[7] := 5; offset[8] := 1;
offset[9] := 4; offset[10] := 6; offset[11] := 2; offset[12] := 4;
Show(1978, 3, 1);
Show(1996, 12, 31);
Show(2000, 2, 29);
Show(2026, 11, 26)
END weekday.
The date is passed as a year, a Month and a
DayOfMonth. offset is indexed by
Month and holds a correction for each month. The formula
counts from Sunday, so (sunday + 6) MOD 7 shifts the result
to the order of Day, which starts on Monday;
VAL turns the number into a day and names into
text.
$ gm2 weekday.mod -o weekday $ ./weekday 1978-3-1 was a Wednesday 1996-12-31 was a Tuesday 2000-2-29 was a Tuesday 2026-11-26 was a Thursday
Next steps
A variable of an enumeration type holds one value at a time. The sets article stores any combination of the values in one variable.
Source
Modula-2 language reference at modula2.org
In this article we have declared enumeration types, converted their values
with ORD and VAL, stepped through them with
FOR, INC and DEC, used them as array
indexes, and restricted values with subrange types and range checks. The
conditionals article covers the
CASE statement used to name the values, and the
arrays article the index types.
Author
List all Modula-2 tutorials.