• No results found

4.9 Importing Algebras

4.9.3 Example

Consider the following signature for computing with real numbers: signature Reals with Integer Rounding

import Booleans,Integers,Reals sorts

constants

operations less : real ×real bool include : int real

round up : real →int Given the specific signatures

ΣBooleans, ΣIntegers and ΣReals this defines a new signature

ΣReals with Integer Rounding

by combining all the sorts, constants and operations, and adding the new ones declared. Flattening the notation for ΣReals with Integer Rounding, we get the following expansion

ΣReals with Integer Rounding0

4.9. IMPORTING ALGEBRAS 121

signature Reals with Integer Rounding0 sorts bool

constants true,false : →bool zero,one : int zeroreal,onereal,pi,e : →real

operations not : bool bool

and : bool×bool →bool add : int ×int int minus : int int times : int ×int →int addreal : real ×real →real

minusreal : real →real

timesreal : real ×real →real

invert : real real

exp : real ×real real log : real ×real →real sqrt : real real abs : real real sin : real →real cos : real real tan : real real in : int →real round up : real int Similarly, given algebras

ABooleans, AIntegers and AReals

interpreting the three imported signatures, the new ΣReals with Integer Rounding-algebra

AReals with Integer Rounding0

is defined by combining all the carriers, constants and operations, and adding the order relation, the sort conversion of integer to real, and the ceiling function.

This construction has a simple architecture as shown in Figure 4.3: AIntegers

ABooleans AReals

AReals with Integer Rounding

Exercises for Chapter 4

1. List all the product types and operation types over the two sorts in S = {nat,Bool}. Why do we assume

nat×Bool and Bool ×nat

are different product types? Devise formulae that count the number of two-sorted product and operation types of arity n.

2. Devise formulae that count the number of m-sorted product types and operation types of arity n.

3. Using the provisional definition of an algebra from Section 3.1, are the algebras (N;0;n +1) and (N;0;n+1,n+1)

the same algebras or not? Recast the definitions of these algebras using the formal definition of an algebra in Section 4.1. How do signatures affect the Counting Lemma in Section3.2.1?

4. Let Σ be a single-sorted signature with sort s and setsΣsk,s of k-ary operation symbols,

where k = 0,1,2, . . . ,K and K is the maximum arity. Show that the number of Σ- algebras definable on a carrier set X with cardinality |X|=n is

ΠK k=0n

nk.|Σ sk,s|.

The signature ΣRing of a ring contains two constants 0 and 1, one unary operation −,

and two binary operations + and. How many ΣRing-algebras are definable on a carrier

set X with n-elements? Estimate how many ΣRing-algebras satisfy the axioms for a

commutative ring.

5. Let Σ be a two-sorted signature with sorts s(1) and s(2). Construct a formula for the number of Σ-algebras definable on sets X1 and X2 of cardinality |X1| = n1 and

|X2| = n2. Hence, give a formula for the special case s(1) = s, s(2) = Bool, |X1| = n

and X2 ={tt,ff}.

6. Write down signatures to model interfaces for the following algebras of bits: (a) bits;

(b) bytes; and (c) n-bit words.

7. Write down signatures to model interfaces for the following algebras of numbers: (a) rational numbers; and

(b) complex numbers.

8. Write down a signature to model the interface for the data of Babbage’s 1835 design for Analytical Engine.

4.9. IMPORTING ALGEBRAS 123

9. Write down signatures to model interfaces for the following data structures: (a) the array;

(b) the list; and (c) the stack.

10. A data type interface is modelled by a signature. Model an idea of ”linking” two different interfaces by formulating conditions on mappings between signatures.

11. Use algebras to model the design of some of the data types in: (i) a pocket calculator; and

(ii) a programming system of your choice.

12. Let A, B and C be any S-sorted Σ-algebras. Prove that if B A, C A and B C then B C.

13. We expand the algebra of Section 4.7.2 for integer addition with multiplication. Let Σ be a signature for the algebra

A= (Z;0Z; +Z,−Z

, .Z). Show that for any n 1,

B = (nZ;0Z; +Z,

−Z, .Z)

is a Σ-subalgebra of A.

14. Show that the operations of A = (Z;0Z; +Z,Z) are not constructors. What must be

added to Ato equip it with a set of constructors? 15. Show that the algebra

(N;0;n+1) has no proper subalgebras. What are the subalgebras of

(N;0;n . 1)?

16. Let A = (R;0; +,) be the algebra of real number addition and subtraction. What are the carriers of the following subalgebras:

(a) h0i; (b) h1i; (c) h1,2i; (d) h√2i; and (e) hπi?

17. Let A = (R;0,1; +,−, .,−1) be the algebra of real number addition, subtraction, multi-

(a) h0,1i; (b) h2i; (c) h1 2i; (d) h√2i; and (e) hπi?

18. LetA = ({a,b,aa,ab,ba,bb, . . .};²;◦) be the algebra of all strings overa,b with concate- nation. Which of the following sets form subalgebras of A:

(a) {an |n 0}; (b) {bn |n 0}; (c) {(ab)n |n 0}; (d) {a2n |n 0}; (e) {b3n |n 0}; 19. Let GL(2,R) ={ µ a b c d ¶ |ad −bc 6= 0}

be the set of non-singular2×2-matrices with real number coefficients. This set is closed under matrix multiplication and matrix inversion, so forms an algebra

A= (GL(2,R);1;.,−1) where 1 = µ 1 0 0 1 ¶

is the identity matrix. Which of the following sets form subalgebras: (a) { µ 1 b 0 1 ¶ |b Z}; (b) { µ a b 0 d ¶ |ad 6=0}?

20. When working with a particular class K of algebras, it is often important that if AK and B is a subalgebra of A, then B K.

Definition A classK ofΣ-algebras is said to beclosed under the formation of subalgebras if, and only if, wheneverA∈K and B ≤A then B ∈K.

(a) Are the classes of semigroups, groups, rings and fields closed under the formation of subalgebras?

(b) Is any class of algebras defined by equations closed under the formation of subalge- bras?

4.9. IMPORTING ALGEBRAS 125

(c) Is the class of all finite structures of any signature Σ closed under the formation of subalgebras?

21. Using the import notation, re-define the following signatures and algebras:

(a) the standard model of Peano arithmetic with the Booleans (see Section 3.3.1); (b) the real numbers with the Booleans (see Section 3.5); and

(c) strings with length (see Section 3.7).

22. In the general account of import how restrictive is the assumption that the sorts, con- stants and operation symbols must be new? Give an interpretation of import without the assumption, illustrating your answer with examples.

Chapter 5

Specifications and Axioms

We are developing the idea of a data type in several stages. In this chapter we reach the third stage. We will add a new component to the concept of both data type and algebra, namely, the programming idea of a

specification and the corresponding mathematical idea of an

axiomatic theory

to model it. In the context of data types, the term axiomatic theory is renamed axiomatic specification

In the second stage of developing our idea of a data type, we revised our idea of a data type by introducing these two aspects,

Data Type = Interface + Implementation

Whilst the names of the data and the operations can be fixed by declaring an interface for a data type, there will always be considerable variation in the details of how the data and operations are implemented.

In the third stage, we reflect on this variation of implementations. We need precise criteria for data type implementations to be either acceptable or unacceptable. We answer the following question:

Specification Problem

How can we specify the properties of a data type for its users?

The user communicates with the data type via its interface which consists of operations. One solution is to list some of the algebraic properties of the operations in the interface that any acceptable implementation of the data type must possess. The algebraic criteria for acceptable data type implementations form a specification and we propose that for a data type:

Specification = Interface + Properties of Operations.

For example, given names for operations on integers, we can require that any implementation must satisfy the basic laws of arithmetic, like associativity and commutativity, and perhaps some conditions on overflow.

In the mathematical model, the signature fixes a notation, and the interpretation models a choice for the data representation and the input-output behaviour of the algorithms implement- ing the operations of the data type. To analyse mathematically the diversity of representations and implementations we will postulate a list of algebraic properties of the constants and oper- ations in the signature. We model data type specifications using the mathematical idea of an axiomatic theory which has the form,

Axiomatic Theory = Signature + Axioms.

An algebra satisfies a theory if the axioms are true of its operations named in the signature. The specification of data types is a deep subject with huge scope. In this book we are merely pointing out its existence; for some information and guidance, see the Further Reading. In Section 5.1, we take up the idea that interfaces have many useful implementations that form classes of algebras of common signatures defined by axiomatic specifications. We reflect on how an axiomatic specification provides a foundation for

reasoning about classes of implementations.

We examine the fact that some properties can be proved true of all implementations of a spec- ification, whilst others cannot, and must be added to a specification, if desired. In Section 5.2, we begin with a simple example of a class of implementations of the integers.

For the rest of the chapter, in Sections 5.3, 5.4, 5.5 and 5.7, we meet some axiomatic specifications of data types and examine some of their uses. To begin with, we are interested in specifying and reasoning with the data types of the integer, rational and real numbers. These systems have been studied in great depth by mathematicians over the centuries. Indeed, it is through the study of number systems, ideas and methods have emerged that can be applied to any data. Specifically, we look at the mathematical ideas of the

commutative ring, field and group

from the point of view of data type specifications, and at equation-solving in these kinds of algebras.

5.1

Classes of Algebras

It is usual in using or designing a data type that we end up with not one algebra, but a whole class of algebras that satisfy a range of design criteria. The class of algebras has a common interface with computations, namely its signature. It ought to have some standard properties that all algebras possess. The signature plays a fundamental rˆole in

(i) making precise the concept of an algebra as a model of an implementation; (ii) defining classes of algebras with common properties;

(iii) proving or disproving further properties of operations of algebras; and (iv) comparing algebras.

5.1. CLASSES OF ALGEBRAS 129