carriers T,T∗,N constants ²: →T∗ 0 : →N operations Prefix : T ×T∗ →T∗ | |: T∗ →N +1 : N→N
4.5
Algebras with Booleans and Flags
Looking back on the many examples of algebras in Sections 3.2–3.7, some common features are noticeable. One prominent feature is the special rˆole of the Booleans in defining tests on data in an algebra. Another is the use of special data to flag errors, unknowns, and other exceptional, and usually undesirable, circumstances. We will examine these features in general, and exercise further our new official definition of a Σ-algebra.
4.5.1
Algebras with Booleans
Tests on data are needed to govern the flow of control in computations. Thus, most algebras that model data types will contain the Booleans, as tests are Boolean-valued functions. To define these algebras, we need to
(i) choose a notation for Booleans, and
(ii) require that the notation has a standard interpretation. Definition (Algebras with Booleans)
(i) A signatureΣ is a signature with Booleans if Bool is a sort name in Σ;
true,false :→Bool are constant symbols in Σ; and
not :Bool →Bool
and :Bool ×Bool →Bool are operation symbols in Σ.
(ii) AΣ-algebra A is an algebra with Booleans if Σ
is a signature with Booleans;
ABool =B={tt,ff}
is a carrier of A;
trueBool =tt
falseBool =ff
are constants inA; and
notBool(b) =¬b
andBool(b1,b2) =b1 ∧b2
are the standard operations inB. That is, the sort Bool and its associated constants and operations have their standard interpretation in A.
Once one has the Booleans, two kinds of test may be added to any algebra, namely, equality and conditionals.
Total and Partial Equality
For each sort s, we can add the operation name
eqs :s ×s →Bool
inΣ. We can interpret this with the operation
eqAs :As×As →B
which we define to have the standard interpretation eqAs (x,y) =
(
tt if x =y; ff if x 6=y.
Now, it may not be desirable to add equality with the standard interpretation. Two varia- tions are: eqAs (x,y) = ( tt if x =y; ↑ if x 6=y. eqAs (x,y) = ( ↑ if x =y; ff if x 6=y.
The last interpretation is to be expected when testing infinite data such as real numbers or infinite sequences. Given two infinite sequences
x =x(0),x(1),x(2), . . . and y =y(0),y(1),y(2), . . . it is possible to search for a difference between them, i.e., to
find some n where x(n)6=y(n)
and so x 6=y. But it may not be possible to test they are equal, i.e., check that for all n, x(n) =y(n).
4.5. ALGEBRAS WITH BOOLEANS AND FLAGS 103
Conditional
For each sort s, we can add the operation name
ifs :Bool ×s ×s →s
in Σ. We can interpret this with the operation
ifAs :B×As ×As →As which we define by ifAs(b,x,y) = ( x if b =tt; y if b =ff.
4.5.2
Algebras with an Unspecified Element
There are several reasons why we might add a new element to an existing set of data. For example, we have added:
(i) u to denote unknown or unspecified in the Booleans (Section 3.2.4); and (ii) error to denote an exception or error in data types (Sections 3.3.4 and 3.5.1). And other situations require the addition of:
(iii) over to denote overflow in finite number systems; (iv) +∞,−∞to denote points in infinite number systems;
(v) ↑ or ⊥to denote the undefined value of a function. Let us define one of these processes in general.
LetA be anyΣ-algebra with the Booleans. We make a new signatureΣu by adding a new
constant symbol unspecifieds of each sort. . . ,s, . . . ofΣ, and transforming the operations ofΣ
to accommodate the unspecified elements: signature Σu sorts . . . ,su, . . . constants . . . ,cu : →su, . . . . . . ,unspecifieds : →su, . . . operations . . . ,fu : su 1 × · · · ×snu →su, . . . . . . ,is unspecifieds : su →Bool, . . .
Now we show how to make a Σu-algebra Au from the Σ-algebra A. Let A be a Σ-algebra
and consider the effect of augmenting A with special objects us 6∈As
to represent an undefined orunspecified datum of sort s. We will make a new algebra Au with
carriers
Au
s =As∪ {us}.
The constants of Au
s are those of As together with
us
interpreting unspecifieds.
The operations of Au are derived from those of A as follows: let
F :As(1)× · · · ×As(n)→As
be an operation of A then define the new operation
Fu :Aus(1)× · · · ×Aus(n) →Aus by Fu(x 1, . . . ,xn) = ( F(x1, . . . ,xn) ifx1 ∈As(1), . . . ,xn ∈As(n); us otherwise.
The restriction of operations on Au that makes them return an unspecified value if any of the
input is unspecified is sometimes called a strictness assumption.
We can extract A fromAu by means of the Boolean valued function
IsUnspecified :Au s →B
that interpretsis unspecifieds, which we define for anya ∈Aus by
IsUnspecifieds(a) = ( tt if a =us; ff otherwise. algebra Au carriers . . . ,Au s, . . . constants . . . ,us : →Aus, . . . . . . ,Cu →Au s, . . .
operations . . . ,IsUnspecifieds : Aus →Bool, . . .
. . . ,Fu : Au s(1)× · · · ×Aus(n) →Aus, . . . definitions . . . ,IsUnspecifieds(a) = ( tt if a =us; ff otherwise., . . . . . . ,Fu(x 1, . . . ,xn) = ( fA(x 1, . . . ,xn) if x1 ∈As(1), . . . ,xn ∈As(n); us otherwise. , . . .