• No results found

3.2

Algebras of Booleans

We begin with what is, perhaps, the simplest and most useful data type: the Booleans.

3.2.1

Standard Booleans

The set B={tt,ff} of truth values or Booleans, where

tt represents true and ff represents false,

has associated with it many useful functions or operations; usually they are called logical or propositional connectives.

For example, we assume the reader recognises the propositional connectives Not : BB And : B×BB Or : B×BB Xor : B×BB Implies : B×BB Equiv : B×BB Nand : B×BB Nor : B×BB

which are normally defined by truth tables. Here are the truth tables for Not and And:

b Not(b) ff tt tt ff b1 b2 And(b1,b2) ff ff ff ff tt ff tt ff ff tt tt tt

It is also worth noting logical implicationImplies and equivalenceEquiv which defines equality on Booleans: b1 b2 Implies(b1,b2) ff ff tt ff tt tt tt ff ff tt tt tt b1 b2 Equiv(b1,b2) ff ff tt ff tt ff tt ff ff tt tt tt

By simply choosing constants and a set of connectives we can make various algebras of Booleans such as (B;tt,ff;Not,And) (B;tt,ff;Not,Or) (B;tt,ff;And,Or) (B;tt,ff;And,Implies) (B;tt,ff;Nand)

(B;tt,ff;Not,And,Or,Implies,Equiv) (B;tt,ff;Not,And,Xor,Nand,Nor)

Most of the functions listed are binary operations, i.e., they have two arguments. There are interesting functions of more arguments.

Lemma (Counting) The number of functions on B with k arguments is 22k

. The number of algebras on B of the form

(B;Fk)

where Fk is a set of k -ary operations, is 22

2 k

.

Proof. Given two sets X and Y with cardinalities|X|=n and |Y|=m, we may deduce that the number of maps X Y is mn.

Now |B| = 2 and take X = Bk and Y = B. Then |X| = 2k and |Y| = 2 and we may

deduce that the number of k–ary functions Bk Bis 22k

.

Given the set X with |X| = n, there are 2n possible subsets of X. Thus, the number of

sets Fk of k–ary operations on B, with which we can make algebras, is22

2 k

.

Over the set B, in the case n = 2, there are 16 possible binary connectives and hence at least 216 algebras to be made from them.

All the usual Boolean connectives can be constructed by composition from the functions Not and And. So, for theoretical purposes, the most useful algebra is the first that we listed, namely: algebra Booleans carriers B constants tt,ff : →B operations Not : BB And : B×BB

Notice that all two elements of these algebras are listed as constants. It is sufficient to pick one, saytt, sinceNot creates the other element, sayff =Not(tt).

There are several common notations for propositional connectives, all of them infix: for example,

Logical Boolean Programming And(x,y) x y x&y x.y x&&y

Or(x,y) x ∨y x +y x||y

Implies(x,y) x y x y x y

Not(x,y) ¬x x !x

3.2.2

Bits

Algebras of Booleans are used everywhere in Computer Science, notably to define and evaluate tests on data in control constructs. They are also used to define and design digital hardware.

3.2. ALGEBRAS OF BOOLEANS 59

In this case of hardware design it is customary to use the set Bit ={1,0}

with

1 representing high voltage and 0 representing low voltage. Many simple designs for digital circuits can be developed from

(Bit;1,0;NotBit,AndBit).

This algebra is equivalent to the algebra on B

(B;tt,ff;NotB,AndB)

where

1 corresponds with tt and 0 with ff.

Then there is an obvious practical sense in which these choices result in equivalent algebras. For instance, the tables defining the operations correspond:

x NotBit(x) 0 1 1 0 x y AndBit(x,y) 0 0 0 0 1 0 1 0 0 1 1 1

3.2.3

Equivalence of Booleans and Bits

It seems clear that Booleans and Bits are essentially the same data, though they are different and have different rˆoles in computing. It is tempting to say that they are the same data in different notations or representations. Other notations for truth and falsity are

{T,F} {t,f} {true,false} in a choice or type faces,

{T,F} {t,f} {true,false} {T,F} {t,f} {true,false} What is going on exactly?

Equivalence of Algebras Can we formulate in what precise theoretical sense these different algebras are equivalent?

The correspondence is made precise by a function φ:BBit

defined by

φ(tt) = 1 and φ(ff) = 0 that converts truth values to bits.

Conversely, we have a function

ψ :Bit →B defined by

ψ(1) = tt and ψ(0) =ff that converts bits to truth values.

We note that these functions express equivalence because ψ(φ(tt)) =tt and ψ(φ(ff)) = ff

φ(ψ(1)) =1 and φ(ψ(0)) =0. Clearly φ is a bijection or one-to-one correspondence with inverse ψ.

However, the equivalence also depends upon relating the operations on truth values and bits. For instance, for any b,b1,b2 ∈B,

φ(NotB(b)) =NotBit(φ(b))

φ(AndB(b1,b2)) =AndBit(φ(b1), φ(b2)).

And, conversely, for any x,x1,x2 ∈Bit,

ψ(NotBit(x)) =NotB(ψ(x))

ψ(AndBit(x1,x2)) =AndB(ψ(x1), ψ(x2)).

These two sets of equations show that the conversion mappings φ and ψ preserve the oper- ations of the algebras. Thus,

equivalence can be made precise by a mapping φ and its inverse ψ, both of which preserve operations.

This precise sense of equivalence is a fundamental notion, and is called the isomorphism

of algebras: two algebras are equivalent when they are isomorphic. We will study isomorphism later (in Chapter 7).

3.2.4

Three-Valued Booleans

We can usefully extend the design of these algebras of Booleans by introducing a special value u to model an unknown truth value in calculations. Let

Bu =B∪ {u}.

3.2. ALGEBRAS OF BOOLEANS 61

Strict Interpretation Consider the simple principle:

if one does not know the truth value of an input to a connective then one does not know the truth value of the output of the connective.

This leads us to extend the definition of the connectives on B to connectives onBu as follows.

Letf :B×BB be a binary connective on the Booleans. We define its extension fu :Bu ×Bu Bu

on known and unknown truth values by fu(b

1,b2) =

(

f(b1,b2) ifb1,b2 ∈B;

u if b1 =u orb2 =u.

By this method the truth tables of Not and And are extended to:

b Notu(b) ff tt tt ff u u b1 b2 Andu(b1,b2) ff ff ff ff tt ff ff u u tt ff ff tt tt tt tt u u u ff u u tt u u u u

This extension of Boolean logic is called Kleene 3-value logic. Applying this principle leads to adaptations of all the algebras given earlier. For example,

algebra Kleene 3 valued logic carriers Bu

constants tt,ff,u : →Bu operations Notu : Bu Bu

Andu : Bu ×Bu Bu

This algebra for evaluating Booleans may be used to raise errors and exceptions in program- ming constructs involving tests. However a case can be made for other methods of extending the operations.

Concurrent Interpretation

Consider the table forAndu. An alternate decision for the definition of this particular connective

is given by the observation thatin processing the conjunction Andu(b1,b2)of two tests b1 and

b2, if b1 is false then for any b2 =tt, ff or u,

Andu(ff,b 2) =ff

(and similarly for b2 false and b1 = tt, ff or u). This results in a connective that ignores the

fact that b2 may be unknown. Here is its truth table:

b1 b2 Candu(b1,b2) ff ff ff ff tt ff ff u ff tt ff ff tt tt tt tt u u u ff ff u tt u u u u

It is well known to implementors of programming languages, and is sometimes called parallel orconcurrent and, or simply Cand, the idea being that, in computingb1 and b2 in parallel, as

soon as one is found to be false, the computation may halt and return And(ff,b2) = ff.

Unlike the concurrent calculation of a truth value, we note that a “sequential” calculation that requires both inputs to be known for the output to be known would be described by the first truth table.