29
1 INM175 Topic 9 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

Embed Size (px)

DESCRIPTION

INM175 Topic 9 3 SOME TERMS OF COLL These terms are not equivalent: pair(unit a, pair(unit b, nil)) a b a b pair(unit a, pair(unit b,unit b))pair(pair(unit a,unit a), unit b) a b nor are these a bb a pair(pair(unit a, nil), unit b)) The symbols a,b etc.are just placeholders for terms of the sort ITEM. We know nothing about them other than they are all different.

Citation preview

Page 1: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

1INM175 Topic 9

Module INM175Discrete Mathematics

Topic 9Abstract Data Types

Page 2: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

2INM175 Topic 9

The theory COLL of ITEM

Sorts COLL, ITEMOperators (the constructors of COLL)

nil: COLLunit: ITEM COLLpair: COLL, COLL COLL

The terms of this theory "look like" binary trees, with a single "root", arbitrary depth, and either singleton terms of the sort ITEM, or "empty" trees at their "leaves".

The sort ITEM is a parameter. We know nothing of its structure.

Page 3: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

3INM175 Topic 9

SOME TERMS OF COLLThese terms are not equivalent:

pair(pair(unit(a), nil), unit(b))pair(unit a, pair(unit b, nil))

a

b a

b

pair(unit a, pair(unit b,unit b)) pair(pair(unit a,unit a), unit b)

a

b

nor are these

a

b b a

pair(pair(unit a, nil), unit b))

The symbols a,b etc.are just placeholders for terms of the sort ITEM.We know nothing about them other than they are all different.

Page 4: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

4INM175 Topic 9

The theory TREE of ITEM

Sorts TREE, ITEMOperators (the constructors of TREE)

empty: TREEone: ITEM TREEjoin: TREE, TREE TREE

(So far, this is identical to the theory COLL except in name).Axioms x:TREE

join(x,empty) = x (empty is thejoin(empty,x) = x identity of join)

Page 5: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

5INM175 Topic 9

SOME TERMS OF TREE

join(one a, join(one b, empty))

a

b

join(join(one a, empty), one b)

a

b

join(one a, join(one b,one b))

a

b

join(join(one a, one b), one b)

a

b

b b

a b

Now these terms are in the same equivalence class, under the congruence induced by the axioms

but these are not

Page 6: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

6INM175 Topic 9

CONSERVATIVE EXTENSIONS OF TREE

We may extend this theory by adding such operators as:

left, right: TREE TREE Note that these are partial.

leaf: TREE ITEM So is this.

not-empty: TREE BOOL These are predicates.

eq-tree: TREE, TREE BOOL This is a relation.

width, depth: TREE N These are interpretations.

Page 7: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

7INM175 Topic 9

Axioms for is-emptyAxioms i:ITEM; x,y:TREE

is-empty empty = Tis-empty unit i = Fis-empty join(x,y) = and(is-empty x, is-empty y)

Here, we have ensured that exactly one axiom is applicable to every term of the sort TREE of ITEM by doing a case analysis over all the constructors.We have also referred to the BOOL operator, or, which we defined earlier.

Page 8: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

8INM175 Topic 9

Axioms for left Axioms i:ITEM; x,y:TREE

left join(x,y) = left x , if is-empty y= left y, if is-empty x= x, otherwise

left is undefined for all but one constructor, join. We could sayleft empty = (the undefined value, called bottom)

left unit i = but it is just as accurate simply to omit these equations.The remaining axiom has three cases. Each is guarded by the predicate that follows the

keyword if. Guards use the values (T and F) returned by BOOL operators as truth values. The operator is undefined for any value not covered by the guards.

Otherwise serves the same purpose as else in imperative programming languages. But this ‘pattern-matching’ style of definition used here is characteristic of the declarative programming languages, such as LISP, Prolog, APL and Haskell.

Page 9: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

9INM175 Topic 9

Axioms for widthAxioms i:ITEM; x,y:TREE

width empty = zerowidth unit i = succ zerowidth join(x,y) = plus(width x, width y)

The result is in N, so we have operators of N, all of which were defined earlier, in the definitions.

This operator is total, so is defined everywhere.

Page 10: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

10INM175 Topic 9

Review of ExtensionAll these operators were defined using:• case analysis, over all the constructors;• operators defined previously, in this or other theories; and• recursion, where the right hand side applies the operator to a term that is

strictly “smaller” (i.e. represented by a shorter string in the term algebra) than the one on the left.

An extension will be conservative if it follows these principles and introduces no confusion.

eq-tree, the predicate that determines whether two terms of TREE are in the same equivalence class, is crucial to the theory but very hard to define. We ease this problem by defining a normal form for TREEs.

Page 11: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

11INM175 Topic 9

A NORMAL FORM OF TREEGiven the partition of the terms induced by the axioms, we select a

representative member of each equivalence class.If we restrict our attention to terms of TREE which are either empty or non-

empty and contain no empty branches, we will have defined a normal form of TREE, that is, a subset of the terms, all of which are distinct, and such that every other term is equivalent, under the axioms, to exactly one term of the subset.

[Of course, we must prove that this is a normal form of TREE, but we will not do that here.]

Each time we identify a normal form, we must define:• a predicate, to detect terms in normal form, and• an operator, to generate the normal form term equivalent to its

argument.

Page 12: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

12INM175 Topic 9

Normal Form Predicate for TREESignature nf-tree: TREE BOOLAxioms i:ITEM; x,y:TREE

nf-tree empty = Tnf-tree unit i = Tnf-tree join(x,y) = and(no-empties x,no-empties y)

This uses the further predicate:Signature no-empties: TREE BOOLAxioms i:ITEM; x,y:TREE

no-empties empty) = Fno-empties unit i = Tno-empties join(x,y) = and(no-empties x, no-empties y)

Page 13: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

13INM175 Topic 9

Normal Form Operator for TREESignature make-nf-tree: TREE TREEAxioms i:ITEM; x,y:TREE

make-nf-tree empty = emptymake-nf-tree unit i = unit imake-nf-tree join(x,y) = make-nf-tree x, if is-empty y

= make-nf-tree y, if is-empty x= join(make-nf-tree x,make-nf-tree y),

otherwise

Page 14: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

14INM175 Topic 9

Equivalence Predicate for TREENow the problem of defining eq-tree is easily solvedSignature eq-tree: TREE, TREE BOOLAxioms x,y:TREE

eq-tree(x,y) = (make-nf x = make-nf y)

Note that an equals sign (=) on the right hand side of an axiom is always syntactic, that is, it responds True only if its arguments are identical terms.

The predicate eq-tree computes the semantic equivalence between terms that may be non-identical but occupy the same equivalence class.

Page 15: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

15INM175 Topic 9

The theory LIST of ITEMSorts LIST, ITEMOperators null: LIST

single: ITEM LISTcat: LIST, LIST LIST

Equations x: LISTcat(x, null) = x (null is thecat(null, x) = x identity of cat)

So far, this is just the theory TREE except in name(s).

We now add the axiom that cat is associativeAxioms x,y,z: LIST

cat(cat(x,y),z) = cat(x,cat(y,z))

Page 16: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

16INM175 Topic 9

SOME TERMS OF LISTThese two terms are in the same equivalence class(because associativity allows a LIST to be thought of as a "flattened" TREE.

cat(single a, cat(single b, single b))

cat(cat(single a, single b), single b)

But these two are notcat(single a, cat(single b, single b))

cat(single a, cat(single b, single b))

a

b ba b b

a

b

b

a

b

a

b

a

a bb

a ba

b

Page 17: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

17INM175 Topic 9

Extensions of LIST

Signaturesfirst , tail: LIST LIST partialhead , last: LIST ITEM partialreverse: LIST LISTtotalis-empty, is-single: LIST BOOL predicateslength: LIST N interpretationeq-list: LIST, LIST BOOL

Again, the equivalence predicate, eq-list, is crucial but hard to define, so we seek a normal form of LIST.

Page 18: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

18INM175 Topic 9

A NORMAL FORM OF LIST

This normal form is defined recursively as follows:every normal form term is either

identically null or the cat of a single LIST with a normal form term.

For example, this is the normal form termrepresenting the LIST [a,b,b,c,a]

a

b

b

c

a

Page 19: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

19INM175 Topic 9

An Alernative Theory of LISTThis normal form suggests the following alternative theory of LISTs:Signatures null: LIST

cons: ITEM LISTwhere the cons operator appends the ITEM to the head of the LIST.This theory, which needs no axioms, is the basic data type of both LISP and Prolog.The constructors of the original theory can be defined as conservative

extensions of this one, thereby proving that both are theories of the same abstract data type.

The theory of LIST presented earlier is usually called MONOID in the mathematical literature.

It also appears frequently in computing, for example, in the design of compilers.

Page 20: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

20INM175 Topic 9

The theory BAG of ITEMSorts BAG, ITEMOperators empty: BAG

one: ITEM BAGmerge: BAG, BAG BAG

Axioms x,y,z: BAGmerge(x, empty) = x (empty is themerge(empty , x) = x identity of merge)merge(merge(x,y),z) = merge(x, merge(y,z)) (merge is associative)

So far, this is just the theory LIST except in name(s).

We now add the axiom that merge is commutativemerge(x,y) = merge(y,x)

Page 21: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

21INM175 Topic 9

SOME TERMS OF BAGmerge(one a, merge(one b, one a))

is equivalent to

merge(merge(one a, one a), one b)

but, still

merge(one a, merge(one b, one b))

is not equivalent to

merge(merge(one a, one a), one b)

a

b

a

b a

a

ba a

a

b

a

ba a

a

b b

ba b

Page 22: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

22INM175 Topic 9

Extensions of BAGSignatures is-empty, is-single: BAG BOOL

count: ITEM, BAG NATremove: ITEM, BAG BAGeq-bag: BAG, BAG BOOL

remove could have several possible meanings The expression remove(i,b) might take just one instance of the ITEM i from

the BAG b, or it might remove all of them.And it might be defined only if there is at least one i in b, or alternatively

whether or not b has any is in it. Try them all.The equivalence predicate is again hard to define, so we seek a Normal Form

for BAG.

Page 23: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

23INM175 Topic 9

A NORMAL FORM OF BAGThe sequence in which ITEMs are added to BAGs is not significant to their

equivalence. So we may pick a particular sequence for our normal form.But we need some ordering relation on ITEMs. Without being specific, we can

denote this relation as : ITEM, ITEM ITEMThen our normal form consists of those terms of BAG that are

identically empty, orthe merge of a singleton BAG (one i for some ITEM i)

with a normal form term, which is itselfeither empty oris a merge whose first argument isa singleton BAG (one j for some ITEM) where i j.

Note that the operation make-nf-bag defines a sorting functionand its axioms implement a sorting algorithm.

Page 24: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

24INM175 Topic 9

The theory SET of ITEMSorts SET, ITEMOperators empty: SET

single: ITEM SETunion: SET, SET SET

Axioms x,y,z: SETunion(x, empty) = x (empty is theunion(empty , x) = x identity of union)union(union(x,y),z) = union(x, union(y,z)) (union is associative)union(x,y) = union(y,x) (union is commutative)

So far, this is just the theory BAG except in name(s). We now add the axiom that union is idempotent

union(x,x) = x

Page 25: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

25INM175 Topic 9

SOME TERMS OF SET

The SET can be thought of as a BAG with no "duplicates".

union(single a, union(single b, single a))

union(union(single a, single b), single b)a

b

b

is equivalent to

a

b a

ba

Page 26: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

26INM175 Topic 9

Extensions of SETThese include the set operations that we merely asserted when we started

this module. Now we have the chance to define them formally.

Signatures intersection: SET, SET SETcardinality: SET NATis-empty, is-single: SET BOOLmember: ITEM, SET BOOLproper-subset, subset: SET, SET BOOLeq-set: SET, SET BOOL

As usual, equivalence is hard to define, so we seek a Normal Form for SET.

Page 27: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

27INM175 Topic 9

A NORMAL FORM OF SETThe number of times that a given ITEM is added to a SET is not

significant. So we may pick a particular number (the obvious candidate is 1) for our normal form.

We still need an ordering relation on ITEMs, but this time it must be a strict order, usually denoted by "<", so that duplicates are omitted.

Then our normal form will compriseempty, andall those terms of the form union(one i,x), where

either x is emptyor x is union (one j,y) , where

i < j and y is in normal form.

Page 28: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

28INM175 Topic 9

SUMMARY OFAlgebraic Specification

A theory presentation consists ofa signature, which contains

• a set of sorts, • a set of operators, each taking zero or more arguments and delivering one

result from these sorts, where some of these operators are the constructors; and

a (possibly empty) set of axioms, quantified over variables ranging over the sorts anddefining each operator in terms of each constructor.

The terms of a theory are the sentences in the language defined by the signature, considered as a grammar.

The equations partition the set of terms into equivalence classes. Terms in different equivalence classes denote distinct values

in all models of the theory.

Page 29: INM175 Topic 9 1 Module INM175 Discrete Mathematics Topic 9 Abstract Data Types

29INM175 Topic 9

And next ....Like LIST, both BAG and SET, can be presented in alternative theories,

based on their Normal Forms.A theory abstracts away from its models, presenting only the properties

that all their models must satisfy. But here we see that an abstract data type can have many alternative theories. So how do we describe the properties that a theory must have?

This question lies at the heart of modern mathematics and leads to abstract algebra, topology and category theory, where many of the most important results in computer science have been found.

But that is for another course. This one is over.