Modula-3
From Wikipedia, the free encyclopedia
| Paradigm | imperative, structured, modular |
|---|---|
| Appeared in | 1980s |
| Designed by | DEC and Olivetti |
| Typing discipline | strong, static |
| Major implementations | CM3, PM3, EZM3, HM3 |
| Influenced by | Modula-2+, Modula-2, Pascal, ALGOL |
| Influenced | Java, Python[1], Caml, C# |
In Computer science, Modula-3 is a programming language conceived as a successor to an upgraded version of Modula-2. While it has been influential in research circles (influencing the designs of languages such as Java, C#, and Python) it has not been adopted widely in industry. It was designed by Luca Cardelli, Jim Donahue, Mick Jordan, Bill Kalsow and Greg Nelson at the Digital Equipment Corporation (DEC) Systems Research Center (SRC) and Olivetti in the late 1980s. Its design was heavily influenced by work on the Modula-2+ language in use at SRC at the time, which was the language in which the operating system for the DEC Firefly multiprocessor VAX workstation was written. As the revised Modula-3 Report states, the language was also influenced by other languages such as Mesa, Cedar, Object Pascal, Oberon and Euclid.[2]
Modula-3's main features are simplicity and safety while preserving the power of a systems-programming language. Modula-3 aimed to continue the Pascal tradition of type safety, while introducing new constructs for practical real-world programming. In particular Modula-3 added support for generic programming (similar to templates), multithreading, exception handling, garbage collection, object-oriented programming, partial revelation and encapsulation of unsafe code. The design goal of Modula-3 was a language that implements the most important features of modern imperative languages in quite basic forms. Thus allegedly dangerous and complicating features like multiple inheritance and operator overloading were omitted.
Contents |
[edit] Historical development
The Modula-3 project started in November 1986 when Maurice Wilkes wrote to Niklaus Wirth with some ideas for a new version of Modula. Wilkes had been working at DEC just prior to this point, and had returned to England and joined Olivetti's Research Strategy Board. Wirth had already moved on to Oberon, but had no problems with Wilkes's team continuing development under the Modula name. The language definition was completed in August 1988, and an updated version in January 1989. Compilers from DEC and Olivetti soon followed, and 3rd party implementations after that.
During the 1990s, Modula-3 gained considerable currency as a teaching language, but it was never widely adopted for industrial use. Contributing to this may have been the demise of DEC, a key Modula-3 supporter. In any case, in spite of Modula-3's simplicity and power, it appears that there was little demand for a procedural compiled language with restricted implementation of object-oriented programming. For a time, a commercial compiler called CM3 and integrated development environment called Reactor were offered by Critical Mass, Inc., but that company ceased active operations in 2000. Modula-3 is now taught in universities mostly in comparative programming language courses, and its textbooks are out of print. Essentially the only corporate supporter of Modula-3 is elego Software Solutions GmbH, which inherited the complete sources from Critical Mass and has since made several releases of the CM3 system in source and binary form. The Reactor IDE has been open source released after several years it had not, with the new name CM3-IDE. In March 2002 elego also took over the repository of the last other active Modula-3 distribution, PM3, till then maintained at the École Polytechnique de Montréal.
[edit] Language features
Exception handling is based on a TRY...EXCEPT block system, which has since become common. One feature that has not been adopted in other languages, with the notable exceptions of Delphi (programming language), Python[1], Scala[2] and Visual Basic.NET, is that the EXCEPT construct defined a pseudo-CASE with each possible exception as a case in one EXCEPT clause. Modula-3 also supports a LOOP...EXIT...END construct that loops until an EXIT occurs, a structure equivalent to a simple loop inside of a TRY...EXCEPT clause.
Object support is intentionally kept to its simplest terms. An object type (class) is introduced with the OBJECT declaration, which has essentially the same syntax as a RECORD declaration, although the type so declared is a reference type, whereas RECORDs in Modula-3 are not (similar to structs in C). For instance:
A = OBJECT a: INTEGER; METHODS p() := AP; END;
defines a new object type A, which contains a single field a and method p. The procedure AP that implements p must be defined elsewhere:
PROCEDURE AP(self: A) = BEGIN ... END;
Method calls are accomplished with o.p();, where o is a variable of type A.
Modula-3's REVEAL construct provides a conceptually simple and clean yet very powerful mechanism for hiding implementation details from clients, with arbitrarily many levels of "friendliness." A typical use of REVEAL is to provide private method and fields:
INTERFACE A;
TYPE
T <: Public; (* T is a subtype of Public *)
Public = OBJECT METHODS
m();
END
END A.
In this interface, the word "Public" is not a keyword, but rather the name of a type, A.Public. The declaration TYPE T <: Public indicates that T is some subtype of Public. (By convention, the "main" type exported by an interface is called T, something used to great advantage by the generics (templates) distributed with Modula-3.)
In the implementation module, the full A is REVEALed:
MODULE A;
REVEAL
T = Public BRANDED OBJECT
f : INTEGER; (* a private field *)
METHODS
n(); (* a private method, invisible from outside this MODULE *)
OVERRIDES
m := PrivateM;
END;
PROCEDURE PrivateM(t : T) = BEGIN ... END PrivateM;
BEGIN END A.
In this way, Modula-3's information hiding rule can be kept extremely simple: in a code MODULE, apart from language keywords, exactly those names are visible that are declared in the current code module or the interface it exports (these names are unqualified) and those names that are mentioned in those interfaces that are imported (these names are qualified unless they are imported using the FROM Interface IMPORT Name syntax).
Modula-3 is one of the few programming languages that requires that external references from a module be strictly qualified. That is, a reference in module A to the object x exported from module B must take the form B.x. It is not possible in Modula-3 to import "all exported names" from a module. Modula-3 also requires the programmer to distinguish between declaring a method signature (METHODS) and a method override in a subtype (OVERRIDES). For example, if we wanted to define a new object type B that subtypes A but overrides its p method, we could write as follows:
B = A OBJECT OVERRIDES p := BP END;
But we could also define a subtype with a new method p that shadows the p from A; let's call this type C:
C = A OBJECT METHODS p() := CP END;
After these declarations, objects of type B would have only a single method p in their method suites. Objects of type C, on the other hand, have two different ps, and which gets called depends on the revealed type of the object in the scope where the method is called. For example:
c := NEW(C);
b := NEW(B);
VAR
x : C := c;
y : A := c;
u : B := b;
v : A := b;
BEGIN
x.p(); (* CP called, shadowed method *)
y.p(); (* AP called, original method *)
u.p(); (* BP called, overriden method *)
v.p() (* BP called, overriden method *)
END
Because of the language's requirements on name qualification and method overriding, it is impossible to break a working program simply by adding new declarations to an interface (any interface). This makes it possible for large programs to be edited concurrently by many programmers without any worries about naming conflicts; and it also makes it possible to edit core language libraries with the firm knowledge that no existing programs will be "broken" in the process.
In summary, the language features:
- Modules and interfaces
- Explicit marking of unsafe code
- Automatic garbage collection
- Strong typing, structural equivalence of types
- Objects
- Exceptions
- Threads
- Generics
Modula-3 is one of the rare languages whose evolution of features is documented.
In Systems Programming with Modula-3 four essential points of the language design are intensively discussed. These topics are: Structural vs. name equivalence, subtyping rules, generic modules, parameter modes like READONLY.
[edit] Syntax
| This section requires expansion. |
A common example of a language's syntax is the Hello world program.
MODULE Main;
IMPORT IO;
BEGIN
IO.Put ("Hello World\n")
END Main.
A different coding would use Writer Modules taken from [3]
MODULE Hello EXPORTS Main; IMPORT Wr, Stdio; BEGIN Wr.PutText(Stdio.stdout, "Hello, World!\n"); Wr.Close(Stdio.stdout); END Hello.
[edit] Standard library features
Continuing a trend started with the C programming language, many of the features required to write real programs were left out of the language definition itself and instead provided via a number of standard libraries. Standard libraries provide the following features.
- Text: Operations on immutable string references, called TEXTs
- Thread: Operations relating to threading, including MUTEX, condition variable, and thread pausing. The threading library provides pre-emptive threads switching
- Word: Bitwise operations on unsigned integers (or machine words). Normally implemented directly by the compiler
- Floating-point interfaces
- Fmt: Formatting various datatypes for printing
- Pkl (or Pickle): Object serialization of any reference types reachable by the garbage collector
- Table: Generic modules for maps
As in C, I/O is also provided via libraries, in Modula-3 called Rd and Wr. The object-oriented design of the Rd and Wr (readers and writers respectively) libraries is covered in detail in the book by Greg Nelson. An interesting aspect of Modula-3 is that it is one of few programming languages whose standard libraries have been formally verified not to contain various types of bugs, including locking bugs. This was done under the auspices of the Extended Static Checking [4] project at DEC Systems Research Center.
[edit] Implementations
Several compilers are available, most of them open source.
- DEC-SRC M3, the original
- Olivetti Research Center (ORC) Modula-3 toolkit, originally a compiler, now a library for constructing Modula-3 parsers.
- Critical Mass CM3, a different successor of DEC-SRC M3
- Polytechnique Montreal Modula-3 PM3, a successor of DEC-SRC M3, currently merging with CM3
- EzM3, an independent lightweight and easily portable implementation, developed in connection with CVSup
- HM3, a successor of the pm3-1.1.15 release of PM3, with support of native threading using NPTL
Since the only aspect of C data structures that is missing from Modula-3 is the union type, all existing Modula-3 implementations are able to provide good binary compatibility with C language type declarations of arrays and structs.
[edit] Books
None of these books are still in print, although used copies are obtainable and some are digitized or partially digitized.
- Greg Nelson, ed. Systems Programming with Modula-3 The definitive reference on the Modula-3 language with interesting articles on object-oriented systems software construction and a documentation of the discussion leading to the final features of the language.
- Samuel P. Harbison, Modula-3 Easy to use class textbook.
- Robert Sedgewick, Algorithms in Modula-3
- Laszlo Boszormenyi & Carsten Weich, Programming in Modula-3: An Introduction in Programming with Style
- Renzo Orsini, Agostino Cortesi Programmare in Modula-3 : introduzione alla programmazione imperativa e a oggetti an italian book of the language explaining its main features.
[edit] Projects using Modula-3
- The SPIN operating system was implemented using Modula-3 as its programming language.
- The CVSup repository synchronization program was implemented in Modula-3.
- The m3w collection of (mostly) free (GPL) software with various levels of dedication and usability for web application programming.
[edit] References
- ^ http://www.python.org/doc/essays/foreword/ Foreword for "Programming Python" (1st ed.)
- ^ SRC-RR-52 Modula-3 report (revised). - Cardelli, Luca; Donahue, James; Glassman, Lucille
- ^ Safe programming with Modula-3 Sam Harbison. Dr. Dobb's Journal. Volume 17 , Issue 10 (October 1992).
- ^ Extended Static Checking David L. Detlefs, K. Rustan M. Leino, Greg Nelson, James B. Saxe. Compaq SRC Research Report 159 (December 1998)
[edit] External links
- Modula-3 Resource Page
- Modula-3 Home Page (now long dead, mirror)
- Modula-3: Language definition
- elego Software Solutions
- Modula-3 newsgroup
- Notes from Caltech's CS2 class, taught in Modula-3 in 2002 and 2003.
- Caltech's CS3 class 2009.
- mirror Programming in Modula-3: program examples
- Building Distributed OO Applications: Modula-3 Objects at Work. Michel R. Dagenais. Draft Version (January 1997)
- Modula-3: Language, Libraries and Tools. Presentation on Modula-3 over 120 slides. Michael R. Dagenais
- Object-Oriented Data Abstraction in Modula-3. Joseph Bergin (1997)
- Computerworld Interview with Luca Cardelli on Modula-3

