1.3

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 TT by enumerating the set of all possible values c1,c2…cnc_1, c_2 \ldots c_n.

type T = (c₁, c₂, ..., cₙ)
(1.1)

The cardinality of TT is card(T)=n\mathit{card}(T) = n.

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 ss, dd, rr, and bb

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 cc, dd, rr, and bb 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 TT is defined by the rule

(ci<cj)≡(i<j)(1.2)(c_i < c_j) \equiv (i < j) \tag{1.2}
↑ Up