principles of object-oriented software development abstract data types
Post on 20-Jan-2016
217 views
TRANSCRIPT
![Page 1: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/1.jpg)
Principles of Object-Oriented Software Development
Abstract data types
![Page 2: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/2.jpg)
Abstract data types
Introduction
Abstraction and types Algebraic specification Decomposition -- modules versus objects Types versus classes
Summary Q/A Literature
![Page 3: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/3.jpg)
Abstract data types
• abstraction and types
• algebraic specification
• modules versus classes
• types as constraints
Additional keywords and phrases:control abstractions, data abstractions, compiler support, description systems, behavioral specification, implementation specification
![Page 4: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/4.jpg)
Abstraction and types
Subsections:
Abstraction in programming languages Foundational perspectives -- types as constraints Objectives of typed OOP
![Page 5: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/5.jpg)
Abstraction in programming languages
![Page 6: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/6.jpg)
Abstraction
• control abstractions -- structured programming
• data abstraction -- information hiding
programming methodology
The kind of abstraction provided by ADTs can be supported by any language with a procedure call mechanism (given that appropriate protocols are developed and observed by the programmer). [DT88]
![Page 7: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/7.jpg)
A foundational perspective types as constraints
Abstract Data Types -- foundational perspective
unambiguous values in some semantic domain
Mathematical models -- types as constraints
algebra -- set oriented second order lambda calculus -- polymorphic types constructive mathematics -- formulas as types
Mathematical models for types
![Page 8: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/8.jpg)
Objectives of typed OOP
• packaging in a coherent manner
• flexible style of associating operations with objects
• inheritance of description components -- reuse, understanding
• separation of specification and implementation
• explicit typing to guide binding decisions
system description
![Page 9: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/9.jpg)
Algebraic specification
Subsections:
Signatures -- generators and observers Equations -- specifying constraints Initial algebra semantics Objects as algebras
![Page 10: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/10.jpg)
Algebraic specification -- ADT
Booladt bool is functions true : bool false : bool and, or : bool * bool -> bool not : bool -> bool axioms [B1] and(true,x) = x [B2] and(false,x) = false [B3] not(true) = false [B4] not(false) = true [B5] or(x,y) = not(and(not(x),not(y))) end
![Page 11: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/11.jpg)
Signatures -- generators and observers
Algebraic specification
![Page 12: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/12.jpg)
Generators -- basis and universe
![Page 13: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/13.jpg)
The ADT Seq
![Page 14: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/14.jpg)
Equations -- specifying constraints
![Page 15: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/15.jpg)
Natural numbers
Nat
functions 0 : Nat S : Nat -> Nat mul : Nat * Nat -> Nat plus : Nat * Nat -> Nataxioms [1] plus(x,0) = x [2] plus(x,Sy) = S(plus(x,y)) [3] mul(x,0) = 0 [4] mul(x,Sy) = plus(mul(x,y),x)end
![Page 16: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/16.jpg)
mul(plus(S 0,S 0),S 0) -[2]-> mul(S(plus(S 0,0)), S 0) -[1]-> mul(SS 0,S 0) -[4]-> plus(mul(SS0,0),SS0) -[3]-> plus(0,SS0) -[2*]-> SS0
Symbolic evaluation
![Page 17: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/17.jpg)
Sets
Set
Axioms
The ADT Set
![Page 18: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/18.jpg)
Equivalence classes for Set
![Page 19: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/19.jpg)
Initial algebra semantics
Interpretations and models
![Page 20: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/20.jpg)
Interpretations of Bool and Nat
![Page 21: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/21.jpg)
Initial models
![Page 22: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/22.jpg)
Structure and interpretation
![Page 23: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/23.jpg)
Objects as algebras
![Page 24: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/24.jpg)
Abstract Data Type -- applicative
stackfunctions new : stack; push : element * stack -> stack; empty : stack -> boolean; pop : stack -> stack; top : stack -> element; axioms empty( new ) = true empty( push(x,s) ) = false top( push(x,s) ) = x pop( push(x,s) ) = s preconditions pre: pop( s : stack ) = not empty(s) pre: top( s : stack ) = not empty(s) end
![Page 25: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/25.jpg)
Dynamic state changes -- objects
accountobject account is functions bal : account -> money methods credit : account * money -> account debit : account * money -> account error overdraw : money -> money axioms bal(new(A)) = 0 bal(credit(A,M)) = bal(A) + M bal(debit(A,M)) = bal(A) - M if bal(A) >= M error-axioms bal(debit(A,M)) = overdraw(M) if bal(A) < M end
![Page 26: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/26.jpg)
The interpretation of change
![Page 27: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/27.jpg)
Example - a counter object
object ctr is ctr
function n : ctr -> nat method incr : ctr -> ctr axioms n(new(C)) = 0 n(incr(C)) = n(C) + 1 end
The object ctr
![Page 28: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/28.jpg)
Abstract evaluation
<n(incr(incr(new(C)))),{ C }> -[new]-> <n(incr(incr(C))),{ C[n:=0] }> -[incr]-> <n(incr(C)),{ C[n:=1] }> -[incr]-> <n(C), { C[n:=2] }> -[n]-> <2, { C[n:=2] }>
An example of abstract evaluation
![Page 29: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/29.jpg)
Decomposition -- modules versus objects
Subsections:
Abstract interfaces Representation and implementation Adding new generators Adding new observers
![Page 30: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/30.jpg)
Decomposition -- matrix
data abstraction
Modules -- operation oriented ADT
organized around observers -- representation hiding
Objects -- data oriented OOP
organized around generators -- method interface
![Page 31: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/31.jpg)
Abstract interfaces
![Page 32: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/32.jpg)
Modules -- a functional interface
ADT
typedef int element; struct list; extern list* nil(); extern list* cons(element e, list* l); extern element head(list* l); extern list* tail(list* l); extern bool equal(list* l, list* m);
Modules -- a functional interface
![Page 33: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/33.jpg)
Objects -- a method interfaceOOP
template< class E > class list { public: list() { } virtual ~list() { } virtual bool empty() = 0; virtual E head() = 0; virtual list<E>* tail() = 0; virtual bool operator==(list<E>* m) = 0; };
Objects -- a method interface
![Page 34: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/34.jpg)
Representation and implementation
![Page 35: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/35.jpg)
Modules -- representation hidingADT
typedef int element; enum { NIL, CONS }; struct list { int tag; element e; list* next; };
![Page 36: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/36.jpg)
Generators
list* nil() { nil
list* l = new list; l->tag = NIL; return l; } list* cons( element e, list* l) { cons
list* x = new list; x->tag = CONS; x->e = e; x->next = l; return x; }
Data abstraction and modules
![Page 37: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/37.jpg)
Modules -- observers
ADT
![Page 38: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/38.jpg)
int empty(list* lst) { return !lst || lst->tag == NIL; } element head(list* l) { head require( ! empty(l) ); return l->e; } list* tail(list* l) { tail require( ! empty(l) ); return l->next; } bool equal(list* l, list* m) { equal switch( l->tag) { case NIL: return empty(m); case CONS: return !empty(m) && head(l) == head(m) && tail(l) == tail(m); } }
![Page 39: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/39.jpg)
Method interface -- listOOP
template< class E > class nil : public list< E > { nil
public: nil() {} bool empty() { return 1; } E head() { require( false ); return E(); } list< E >* tail() { require( 0 ); return 0; } bool operator==(list<E>* m) { return m->empty(); } };
![Page 40: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/40.jpg)
template< class E > class cons : public list< E > { cons
public: cons(E e, list<E>* l) : _e(e), next(l) {} ~cons() { delete next; } bool empty() { return 0; } E head() { return _e; } list<E>* tail() { return next; } bool operator==(list<E>* m); protected: E _e; list<E>* next; };
Data abstraction and objects
![Page 41: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/41.jpg)
Adding new generators
![Page 42: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/42.jpg)
Adding new generators --representation
ADT
typedef int element; enum { NIL, CONS, INTERVAL }; struct list { int tag; element e; union { element z; list* next; }; };
![Page 43: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/43.jpg)
Generator
list* interval( element x, element y ) { list* l = new list; if ( x <= y ) { l->tag = INTERVAL; l->e = x; l->z = y; } else l->tag = NIL; return l; }
Modules and generators
![Page 44: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/44.jpg)
Modifying the observersADT
element head(list* l) { head require( ! empty(l) ); return l->e; for both CONS and INTERVAL } list* tail(list* l) { tail require( ! empty(l) ); switch( l->tag ) { case CONS: return l->next; case INTERVAL: return interval((l->e)+1,l->z); } }
![Page 45: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/45.jpg)
Adding new generators
OOP
![Page 46: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/46.jpg)
class interval : public list<int> { interval public: interval(int x, int y) : _x(x), _y(y) { require( x <= y ); } bool empty() { return 0; } int head() { return _x; } list< int >* tail() { return (_x+1 <= _y)? new interval(_x+1,_y): new nil<int>; } bool operator==(list@lt;int>* m) { return !m->empty() && _x == m->head() && tail() == m->tail(); } protected: int _x; int _y; };
![Page 47: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/47.jpg)
Adding new observers
![Page 48: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/48.jpg)
Adding new observersADT
int length( list* l ) { length
switch( l->tag ) { case NIL: return 0; case CONS: return 1 + length(l->next); case INTERVAL: return l->z - l->e + 1; }; }
Modules and observers
![Page 49: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/49.jpg)
Adding new observersOOP
template< class E > int length(list< E >* l) { length
return l->empty() ? 0 : 1 + length( l->tail() ); } template< class E > class listWL : public list<E> { listWL
public: int length() { return ::length( this ); } };
Objects and observers
![Page 50: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/50.jpg)
Types versus classes
![Page 51: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/51.jpg)
Types versus classes
• types -- type checking predicates
• classes -- templates for object creation
Type specification
syntactically -- signature (under) semantically -- behavior (right) pragmatically -- implementation (over)
![Page 52: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/52.jpg)
Modifications
types -(predicate constraints)-> subtypes classes -(template modification)-> subclasses
Varieties of (compatible) modifications
behaviorally -- algebraic, axiomatic signature -- type checking (signature) name -- method search algorithm (classes)
![Page 53: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/53.jpg)
Signature compatible modifications behavior is approximated by signatureSemantics preserving extensions horizontal -- Person = Citizen + { age : 0..120 } vertical -- Retiree = Person + { age : 65..120 }
Principle of substitutability an instance of a subtype can always be used in any context in which an instance of a supertype can be used
Read-only substitutability
subset subtypes, isomorphically embedded subtypes
![Page 54: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/54.jpg)
Name compatible modifications
• operational semantics -- no extra compile/run-time checks
procedure search(name, module) if name = action then do action elsif inherited = nil then undefined else search(name, inherited)
The inheritance search algorithm
![Page 55: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/55.jpg)
Summary
![Page 56: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/56.jpg)
Abstraction and types
• abstraction -- control and data
• abstract data types -- values in a semantic domain
• types as constraints -- mathematical models
1
![Page 57: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/57.jpg)
Algebraic specification
• signature -- producers and observers
• generator universe -- equivalence classes
• initial model -- no junk, no confusion
• objects -- multiple world semantics
2
![Page 58: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/58.jpg)
Decomposition -- modulesversus objects
• data abstraction -- generators/observers matrix
• modules -- operation oriented
• objects -- data oriented
3
![Page 59: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/59.jpg)
Types versus classes
• types -- syntactically, semantically, pragmatically
• compatible modifications -- type, signature, class
4
![Page 60: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/60.jpg)
Questions1. Characterize the differences between control abstractions and data abstractions. Explain how these two kinds of abstractions may be embodied in programming language constructs. 2. How can you model the meaning of abstract data types in a mathematical way? Do you know any alternative ways? 3. Explain how types may affect object-oriented programming. 4. Explain how you may characterize an abstract data type by means of a matrix with generator columns and observer rows. What benefits does such an organization have? 5. How would you characterize the differences between the realization of abstract data types by modules and by objects? Discuss the trade-offs involved. 6. How would you characterize the distinction between types and classes? Mention three ways of specifying types. How are these kinds related to each other? 7. How would you characterize behavior compatible modifications? What alternatives can you think of?
![Page 61: Principles of Object-Oriented Software Development Abstract data types](https://reader036.vdocument.in/reader036/viewer/2022062423/56649d595503460f94a391a8/html5/thumbnails/61.jpg)
Further reading
There is a vast amount of literature on the algebraic specification of abstract data types. You may consult, for example, [Dahl92].