ArticleslgStudy

science

Modula-2

Modula-2 is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Modula-2 rather than just read about it. In short: Modula-2 is a structured, procedural programming language developed between 1977 and 1985/8 by Niklaus Wirth at ETH Zurich. It was created as the language for the operating system and application software of the Lilith personal workstation.

Key takeaways

  • Modula-2 belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Modula-2 to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Modula-2 from memory before moving on to harder problems.

Reference excerpt

Modula-2 is a structured, procedural programming language developed between 1977 and 1985/8 by Niklaus Wirth at ETH Zurich. It was created as the language for the operating system and application software of the Lilith personal workstation. It was later used for programming outside the context of the Lilith. Wirth viewed Modula-2 as a successor to his earlier programming languages Pascal and Modula. The main concepts are:

The module as a compiling unit for separate compiling The coroutine as the basic building block for concurrent processes Types and procedures that allow access to machine-specific data The language design was influenced by the Mesa language and the Xerox Alto, both from Xerox PARC, that Wirth saw during his 1976 sabbatical year there. The computer magazine Byte devoted the August 1984 issue to the language and its surrounding environment. Wirth created the Oberon series of languages as the successor to Modula-2, while others (particularly at Digital Equipment Corporation and Acorn Computers, later Olivetti) developed Modula-2 into Modula-2+ and later Modula-3.

Description Modula-2 is a general-purpose procedural language suitable for both systems programming and applications programming. The syntax is based on Wirth's earlier language, Pascal, with some elements and syntactic ambiguities removed. The module concept, designed to support separate compilation and data abstraction; and direct language support for multiprogramming were added. The language allows the use of one-pass compilers. Such a compiler by Gutknecht and Wirth was about four times faster than earlier multi-pass compilers. Here is an example of the source code for the "Hello world" program:

A Modula-2 module may be used to encapsulate a set of related subprograms and data structures, and restrict their visibility from other parts of the program. Modula-2 programs are composed of modules, each of which is made up of two parts: a definition module, the interface portion, which contains only those parts of the subsystem that are exported (visible to other modules), and an implementation module, which contains the working code that is internal to the module. The language has strict scope control. Except for standard identifiers, no object from the outside is visible inside a module unless explicitly imported; no internal module object is visible from the outside unless explicitly exported. Suppose module M1 exports objects a, b, c, and P by enumerating its identifiers in an explicit export list

Then the objects a, b, c, and P from module M1 are known outside module M1 as M1.a, M1.b, M1.c, and M1.P. They are exported in a qualified manner to the outside (assuming module M1 is global). The exporting module's name, i.e. M1, is used as a qualifier followed by the object's name. Suppose module M2 contains the following IMPORT declaration

Then this means that the objects exported by module M1 to the outside of its enclosing program can now be used inside module M2. They are referenced in a qualified manner: M1.a, M1.b, M1.c, and M1.P. Example:

Qualified export avoids name clashes. For example, if another module M3 exports an object called P, then the two objects can be distinguished since M1.P differs from M3.P. It does not matter that both objects are called P inside their exporting modules M1 and M3. An alternative method exists. Suppose module M4 is formulated as this:

This means that objects exported by module M1 to the outside can again be used inside module M4, but now by mere references to the exported identifiers in an unqualified manner as: a, b, c, and P. Example:

This method of import is usable if there are no name clashes. It allows variables and other objects to be used outside their exporting module in the same unqualified manner as inside the exporting module. The export and import rules not only safeguard objects against unwanted access, but also allow a cross-reference of the definition of every identifier in a program to be created. This property helps with the maintenance of large programs containing many modules. The language provides for single-processor concurrency (monitors, coroutines and explicit transfer of control) and for hardware access (absolute addresses, bit manipulation, and interrupts). It uses a nominal type system.

Dialects There are two major dialects of Modula-2. The first is PIM, named for the book Programming in Modula-2 by Niklaus Wirth. There were three major editions of PIM: the second, third (corrected), and fourth. Each describes slight variants of the language. The second major dialect is ISO, named for the standardization effort by the International Organization for Standardization. Here are a few of the differences among them.

PIM2 (1983) Required explicit EXPORT clause in definition modules. Function SIZE needs to be imported from module SYSTEM PIM3 (1985) Removed the EXPORT clause from definition modules following the observation that everything within a definition module defines the interface to that module, hence the EXPORT clause was redundant. Function SIZE is pervasive (visible in any scope without import) PIM4 (1988) Specified the behaviour of the MOD operator when the operands are negative. Required all ARRAY OF CHAR strings to be terminated by ASCII NUL, even if the string fits exactly into its array. ISO (1996, 1998) ISO Modula-2 resolved most of the ambiguities in PIM Modula-2. It added the data types COMPLEX and LONGCOMPLEX, exceptions, module termination (FINALLY clause) and a complete standard input/output (I/O) library. There are many minor differences and clarifications.

Supersets There are several supersets of Modula-2 with language extensions for specific application domains:

PIM supersets Canterbury Modula-2, extended with Oberon-like extensible records [This has been withdrawn and is no longer available anywhere] Modula-2+, extended with preemptive threads, exceptions, and garbage collection Modula-2*, parallel extension Modula-P, another parallel extension Modula–Prolog, adds a Prolog layer Modula/R, adds relational database extensions Modula-GM, adds embedded system extensions ISO supersets ISO10514-2, adds an object-oriented programming layer ISO10514-3, adds a generic programming (generics) layer IEC supersets Mod51, extended with IEC 1131 constructs for embedded development

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Modula-2

Start with the simplest possible case. Write down what Modula-2 claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Modula-2 before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Modula-2 ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Modula-2

In research
Modula-2 appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Modula-2 in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Modula-2 is common in secondary-school and first-year university syllabi. It links to neighbouring topics Compiled programming languages, High-level programming languages, Modula programming language family, so understanding it makes those chapters shorter.
In everyday life
Look for Modula-2 outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Modula-2” →

How to study Modula-2 in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Modula-2 means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Modula-2 out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Modula-2 in simple terms?

Modula-2 is a structured, procedural programming language developed between 1977 and 1985/8 by Niklaus Wirth at ETH Zurich. It was created as the language for the operating system and application software of the Lilith personal workstation.

Why does Modula-2 matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Modula-2?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Modula-2.

Tags

  • Compiled programming languages
  • High-level programming languages
  • Modula programming language family
  • Programming languages created in 1978
  • Programming languages with an ISO/IEC standard
  • Statically typed programming languages
  • Systems programming languages

Keep exploring