[Ada Information Clearinghouse]
Ada '83 Rationale, Sec 4.2: The Concept of Type

"Rationale for the Design of the
Ada® Programming Language"

[Ada '83 Rationale, HTML Version]

Copyright ©1986 owned by the United States Government. All rights reserved.
Direct inquiries to the Ada Information Clearinghouse at adainfo@sw-eng.falls-church.va.us.

CHAPTER 4: Types

4.2 The Concept of Type

Ada provides a capability to define new types. The language construct used to declare a new type is called a type declaration. Examples of type declarations appear below:

type INT is range -2**24 .. 2**24;                     -- integer type
type SCALE is (LOW, MEDIUM, HIGH);                     -- enumeration type
type MASS is digits 8 range 0.0 .. 1.0E9;              -- floating point type
type VOLT is delta 0.01 range 0.0 .. 1_000.0;          -- fixed point type
type LINE is array (1 .. 128) of CHARACTER;            -- array type
type PAIR is record X, Y :  INTEGER; end record;       -- record type
type MY_INT is new INTEGER;                            -- derived type
type TEXT is access STRING;                            -- access type
type FILE is limited private;                          -- private type

As stated in the introduction, a type is characterized by a set of values and a set of operations. To illustrate this we can use enumeration types: in many ways they are the simplest form of type, yet they are sufficient to discuss the most important aspects. Consider for example:

    type DAY is (MON, TUE, WED, THU, FRI, SAT, SUN);

Each of the identifiers thus enumerated is called an enumeration literal and can be viewed as a (parameterless) function that always delivers the same value. Hence we have a distinct value for each enumeration literal, and so we have seven values for the type DAY.

Consider now the set of operations that is - implictly - defined by this type declaration. This set includes

Thus the declaration of the type DAY has implicitly defined the above set of values and operations, and thereby what we are allowed to do with objects and values of type DAY. To appreciate the contribution of this concept to program reliability consider the interactions of three important rules in typed languages such as Pascal and Ada:
  1. All objects (variables and constants) must be declared.

  2. The declaration of an object must specify its type.

  3. Any operation on an object must preserve its type.
It results from the above rules that the type of an object is invariant during program execution: it is the type given in the object declaration. All properties characterized by the type are therefore static and must be checked at compilation time by Ada compilers. To illustrate this point consider the additional declarations:


With these declarations, an Ada compiler will accept assignment statements such as

GOAL   :=  WEST;

Consider for example the first one: TODAY is a variable declared to be of type DAY; there is an assignment operation (:=) defined for this type; assignment to a variable is allowed, but it requires a value of the same type: and there is actually a literal MON that yields a value of type DAY. Using similar simple rules, an Ada compiler must reject each of the following illegal assignment statements:

TODAY :=  WEST;           -- Illegal: WEST is not a DAY value
TODAY :=  5;              -- Illegal: 5 is not a DAY value
TODAY :=  TODAY + START;  -- Illegal: "+" is not defined for DAYS

In the last case, TODAY and START are both of type DAY but the operation "+" is not defined for this type and this knowledge allows rejection of the statement.

This example demonstrates that the contribution of enumeration types to the quality of programs goes far beyond increased readability. We could actually achieve a comparable degree of readability in languages such as Algol 68, which do not provide enumeration types (or even in Fortran, using data or parameter statements). The set of Algol 68 declarations could be as follows

ø days of the week: ø
int MON=1, TUE=2, WED=3, THU=4, FRI=5, SAT=6, SUN=7;
ø directions: ø
int NORTH=1, EAST=2, SOUTH=3, WEST=4;

thereby allowing the same degree of readability as Ada for statements such as

GOAL   :=  WEST;

The real difference is one of reliability. The following statements would all be accepted by an Algol 68 compiler, whereas they would all be rejected by an Ada compiler in the Ada formulation:

TODAY  :=  8;

By declaring DAY as an enumeration type we expressed the intent that there be seven distinct values with well-defined operations. This intent was expressed in a form that permits a compiler to verify that further uses of days are consistent. Furthermore, in declaring DIRECTION to be a different type (instead of having a single enumeration type with eleven values), we have conveyed our intent that days and directions should not be mixed; and again, we have done so in a form that allows verification at compilation time by an Ada compiler.

In all cases to be examined in later sections, we will find that types allow the explicit formulation of certain logical requirements of programs. Explicit formulation allows these logical requirements to be verified by a mechanical tool - the Ada compiler - thereby contributing to program reliability.

Address any questions or comments to adainfo@sw-eng.falls-church.va.us.