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
= /=
< <= > >=
:=
DAY'FIRST -- yields MON DAY'LAST -- yields SUN |
and other attributes that are functions with a single parameter; the latter include
DAY'SUCC -- for example, DAY'SUCC(MON) = TUE DAY'PRED -- for example, DAY'PRED(TUE) = MON DAY'POS -- for example, DAY'POS(MON) = 0 DAY'VAL -- for example, DAY'VAL(0) = MON |
type DIRECTION is (NORTH, EAST, SOUTH, WEST); GOAL : DIRECTION; TODAY : DAY; START : DAY; |
With these declarations, an Ada compiler will accept assignment statements such as
TODAY := MON; GOAL := WEST; START := TODAY; |
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; int GOAL, TODAY, START; |
thereby allowing the same degree of readability as Ada for statements such as
TODAY := MON; GOAL := WEST; START := TODAY; |
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 := WEST; TODAY := 8; TODAY := TODAY + START; START := 2*GOAL - NORTH + SUN*WEST; |
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.