Schweitzer Fachinformationen
Wenn es um professionelles Wissen geht, ist Schweitzer Fachinformationen wegweisend. Kunden aus Recht und Beratung sowie Unternehmen, öffentliche Verwaltungen und Bibliotheken erhalten komplette Lösungen zum Beschaffen, Verwalten und Nutzen von digitalen und gedruckten Medien.
The need to split source files, to confine, to reuse fragments of programs to achieve the rational development for a software system arose very early in the history of programming [PAR 72]. This has been studied in Chapter 1 where kits are presented as a model that is a general answer to this need. To implement this model, some programming languages provide a dedicated construct often referred to by the generic name module. We examine them in this chapter.
Modules appeared in the early 1980s, under the name structure in STANDARD ML [MIL 19], module in OCaml [INR 19], package in Ada [AUT 19] or cluster [LIS 81] for instance.
At the same time as module creation, the theory of algebraic abstract data types (ADT) [GOG 78] was under development. It formally models the notion of a data set associated with operations as a term algebra whose properties are stated by equations between terms. This allows one to formally reason about these sets independently from their implementation. Any model consistent with these equations defines a semantics of the ADT. ADTs have been a great source of inspiration for designing the modules of the current languages.
Modules of programming languages are implementations of kits. They all allow us to manipulate a collection of type definitions and values but also, depending on the language, of constants, variables, functions, procedures, classes, exceptions or modules. Thus, we find in this list all the ingredients of a kit. Whatever the language, modules define their own namespace and provide import, export and flattening operations, as presented in Chapter 1. Their semantics is built on that of the imperative and/or functional features of the language, especially on the notions of typing and scope. These features have been studied in Volume 1.
This chapter presents implementations of kits in several languages and answers, in this context, some of the questions raised in Chapter 1. These languages are the following:
Ada is an imperative language with a static scope. Types of components must be declared. It is strongly and statically typed, with type verification. It allows the overloading of functions and constants with different types (see section 5.7.2 of Volume 1). It is compiled into native code (Ans. 1.1). Ada provides constructs for modular but also for real-time, concurrent and distributed programming. Since [ADA 95], the language supports object-oriented features and since [ADA 12], it allows contract-based development: properties are stated by pre- and post-conditions which are checked at runtime (see section 4.7.2 of Volume 1 where pre- and post-conditions are studied). The definition and management of modules were available in the first versions of the language. They were enriched as successive versions progressed. The different evolutions of this language [ADA 95, ADA 05, ADA 12] have always been internationally standardized, a point which guarantees the full compatibility of the object code produced by the various compilers on the market. To get a complete presentation of Ada, the reader may refer to the reference manuals [AUT 19]. In this book, we only present modules.
A module of Ada is an implementation of a kit (Ans. 1.2). It is introduced by the keyword package and defined in two steps, by first providing its specification, then its body. The specification (also called the interface) introduces the module name and the declarations of the fields the developer does not confine. These declarations of functions, procedures, variables, types, constants, modules and exceptions can also be defined at this point. Example 2.1 introduces a module to define a list of integers implemented using pointers. The specification part declares the name of the module, the types used to build values, the operations to manipulate these values and an exception. The mutual recursion between types link and int_l requires a preliminary introduction of the name link. The same can be said for the mutual recursion between functions. There is no dedicated keyword for recursion (Ans. 1.10 and Ans. 1.11).
package
link
int_l
Ada -- Specification part package L_integers is type link ; -- Declaration of an element's type. type int_l is access link ; -- Type pointer to a link. type link is -- Definition of type link. record v: integer; s: int_l ; end record ; function cons (n: integer; l: int_l) return int_l ; function empty return int_l ; function is_empty (l: int_l) return boolean ; function head (l: int_l) return integer ; function tail (l: int_l) return int_l ; specification_error: exception ; end L_integers ;
The body of the module contains the definitions of all the fields only declared in the specification part of the module and these definitions repeat the declarations of the completed fields.
New functions/procedures, variables, types, modules, etc. can be defined in the body of the module, without being declared in the specification part, and then they are confined inside the body.
Example 2.2 extends example 2.1 by defining the body of the module L_integers.
L_integers
Ada package body L_integers is function cons (n: integer; l: int_l) return int_l is ... function empty return int_l is ... function is_empty (l: int_l) return boolean is ... function head (l: int_l) return integer is ... function tail (l: int_l) return int_l is ... procedure proc ( ... raise specification_error ...) end L_integers ;
A module specification can be seen as an incomplete kit, completion being performed in one unique step by the definition of the module's body (Ans. 1.27 and Ans. 1.28). Definition and declaration types must be identical.
As with any kit, a module introduces a namespace in which names are accessible from outside using the dot-notation. In Ada, this namespace is not divided into sub-spaces. Thus, it contains all the names of types, variables and functions, etc. introduced by this module. This implies that types, variables, functions cannot have the same names (except in the case of overloading, which is described later) (Ans. 1.3 and Ans. 1.9).
Thus, the namespace introduced by the module L_integers of example 2.2 is: (link, int_l, cons, empty, ..., specification_error, proc).
(link, int_l, cons, empty, ..., specification_error, proc)
Types and values are defined and computed as indicated for kits (Ans. 1.5 and Ans. 1.6). For instance, the type of L_integers involves the name of the module and the names and types of the fields:
L_integers: link: (v: integer; s: int_l) ; int_l: (access link) ; cons: (integer * int_l -> int_l) ; ...
If we had defined another module M containing the same fields as L_integers, then types M.link and L_integers.link would be different (Ans. 1.7): typing is nominal.
M
M.link
L_integers.link
As for kits, declarations are sequentially evaluated, i.e. they follow the order of the text. Any declaration can make reference to a previous one but never to a future one. This is the reason for the preliminary declaration of link (Ans. 1.10 and Ans. 1.11).
Evaluating an Ada module requires two steps. First, the evaluation of the specification of L_integers in the current environment Env1 binds the name L_integers to the result of the evaluation of the fields declared and defined in this specification. Next comes the evaluation of the module's body in the current environment Env2, which completes the value. Beware, Env1 and Env2 can be different and the memory Mem may have changed between the two steps.
Let us detail the evaluation of L_integers. Let Env be the current environment:
v_L_integers = (cons: ?? ; empty: ?? ; ...; specification_error: exception)
Dateiformat: ePUBKopierschutz: Adobe-DRM (Digital Rights Management)
Systemvoraussetzungen:
Das Dateiformat ePUB ist sehr gut für Romane und Sachbücher geeignet – also für „fließenden” Text ohne komplexes Layout. Bei E-Readern oder Smartphones passt sich der Zeilen- und Seitenumbruch automatisch den kleinen Displays an. Mit Adobe-DRM wird hier ein „harter” Kopierschutz verwendet. Wenn die notwendigen Voraussetzungen nicht vorliegen, können Sie das E-Book leider nicht öffnen. Daher müssen Sie bereits vor dem Download Ihre Lese-Hardware vorbereiten.Bitte beachten Sie: Wir empfehlen Ihnen unbedingt nach Installation der Lese-Software diese mit Ihrer persönlichen Adobe-ID zu autorisieren!
Weitere Informationen finden Sie in unserer E-Book Hilfe.