Primitive Data Types
In many programs integers are used when numerical properties are not involved and when the integer represents a choice from a small number of7 alternatives. In these cases we introduce a new primitive, unstructured data type by enumerating the set of all possible values .
type T = (c₁, c₂, ..., cₙ)(1.1)The cardinality of is .
Examples
type shape = (rectangle, square, ellipse, circle) type color = (red, yellow, green) type sex = (male, female) type Boolean = (false, true) type weekday = (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday) type currency = (franc, mark, pound, dollar, shilling, lira, guilder, krone, ruble, cruzeiro, yen) type destination = (hell, purgatory, heaven) type vehicle = (train, bus, automobile, boat, airplane) type rank = (private, corporal, sergeant, lieutenant, captain, major, colonel, general) type object = (constant, type, variable, procedure, function) type structure = (file, array, record, set) type condition = (manual, unloaded, parity, skew)
The definition of such types introduces not only a new type identifier, but at the same time the set of identifiers denoting the values of the new type. These identifiers may then be used as constants throughout the program, and they enhance its understandability considerably. If, as an example, we introduce variables , , , and
var s: sex var d: weekday var r: rank var b: Boolean
then the following assignment statements are possible:
s := male d := Sunday r := major b := true
Evidently, they are considerably more informative than their counterparts
s := 1 d := 7 r := 6 b := 2
which are based on the assumption that , , , and are defined as type integer and that the constants are mapped onto the natural numbers in the8 order of their enumeration. Furthermore, a compiler can check against the inconsiderate use of arithmetic operators on such non-numeric types as, for example,
s := s+1
If, however, we consider a type as ordered, then it is sensible to introduce functions that generate the successor and predecessor of their argument. These functions are denoted by succ(x) and pred(x). The ordering among values of is defined by the rule