Presentation is loading. Please wait.

Presentation is loading. Please wait.

Principles of Object-Oriented Software Development Polymorphism.

Similar presentations


Presentation on theme: "Principles of Object-Oriented Software Development Polymorphism."— Presentation transcript:

1 Principles of Object-Oriented Software Development Polymorphism

2 Introduction Abstract inheritance The subtype relation Flavors of polymorphism Type abstraction Existential types -- hiding Self-reference Summary Q/A Literature

3 Polymorphism abstract inheritance subtypes type abstraction self-reference Additional keywords and phrases: exceptions, type calculi, parametric types, coercion, ad hoc polymorphism, universal types, existential types, unfolding, intersection types

4 Abstract inheritance declarative relation among entities Inheritance networks isa-trees -- partial ordering isa/is-not -- bipolar, is-not inference Non-monotonic reasoning Nixon is-a Quaker Nixon is-a Republican Quakers are Pacifists Republicans are not Pacifists Incremental system evolution is in practice non-monotonic!

5 Taxonomies and predicate logic

6 The subtype relation Subsections: Types as sets The subtype refinement relation Objects as records

7 Types as sets

8 The subtype refinement relation

9

10 The function subtype relation

11 Objects as records

12 The object subtype relation

13 Subtyping -- examples type any = { } type entity = { age : int } type vehicle = { age : int, speed : int } type machine = { age : int, fuel : string } type car = { age : int, speed : int, fuel : string } Subtyping rules

14 Subtyping in Emerald S provides at least the operations of T for each operation in T, the corresponding operation in S has the same number of arguments the type of the result of operations of S conform to those of the operations of T the types of arguments of operations of T conform to those of the operations of S S conforms to T

15 Flavors of polymorphism

16 Typing -- protection against errors static -- type checking at compile time strong -- all expressions are type consistent Untyped -- flexibility bitstrings, sets, -calculus Exceptions to monomorphic typing: overloading, coercion, subranging, value-sharing (nil) The nature of types

17 Flavors of polymorphism inclusion polymorphism -- to model subtypes and inheritance parametric polymorphism -- uniformly on a range of types intersection types -- coherent overloading

18 Inheritance -- incremental modification Result = Parent + Modifier Independent attributes: M disjoint from P Overlapping attributes: M overrules P Dynamic binding

19 Type abstraction Subsections: A simple type calculus Intersection types Bounded polymorphism

20 The lambda calculus -- terms

21 The lambda calculus -- laws

22 The lambda calculus -- substitution

23 Beta conversion -- examples

24 The lambda calculus -- properties

25 A simple type calculus

26 The subtype calculus

27 Subtypes -- examples

28 int S(int x) { return x+1; } int twice(int f(int), int y) { return f(f(y)); } int twice_S(int y) { return twice(S,y); } Types in C++ int SD(double x) { return x+1; } SD example

29 class P { P public: P() { _self = 0; } virtual P* self() { return _self?_self->self():this; } virtual void attach(C* p) { _self = p; } private: P* _self; };

30 class C : public P { C <= P public: C() : P(this) { } C* self() { ANSI/ISO return _self?_self->self():this; } void attach(P* p) { rejected p->attach(self()); } void redirect(C* c) { _self = c; } private: C* _self; }; Subtyping in C++

31 Intersection types

32 The intersection type calculus

33 Intersection types -- examples

34 Overloaded function selection rules [1] no or unavoidable conversions -- array-> pointer, T -> const T [2] integral promotion -- char->int, short-> int, float->double [3] standard conversions -- int->double, double->int, derived* -> base* [4] user-defined conversions -- constructors and operators [5] ellipsis in function declaration --...

35 Multiple arguments -- intersect rule better match for at least one argument and at least as good a match for every other argument Overloading in C++

36 void f(int, double); void f(double, int); f(1,2.0); f(int, double); f(2.0,1); f(double,int); f(1,1); error: ambiguous example

37 class P; class C; void f(P* p) { cout << "f(P*)"; } (1) void f(C* c) { cout << "f(C*)"; } (2) class P { public: virtual void f() { cout << "P::f"; } (3) }; class C : public P { public: virtual void f() { cout << "C::f"; } (4) }; Static versus dynamic selection

38 P* p = new P static and dynamic P* C* c = new C; static and dynamic C* P* pc = new C; static P*, dynamic C* f(p); f(P*) f(c); f(C*) f(pc); f(P*) p->f(); P::f c->f(); C::f pc->f(); C::f

39 Bounded polymorphism

40 The bounded type calculus

41 Parametrized types -- examples

42 Bounded quantification -- examples

43 Point* move(Point* p, int d); require int Point::x Point* move(Point* p, int d) { p.x += d; return p; } example move

44 template requires T::value() class P { public: P(T& r) : t(r) {} int operator==( P & p) { return t.value() == p.t.value(); } private: T& t; }; Type abstraction in C++

45 template class A { A public: virtual T value() = 0; }; class Int : public A { Int public: Int(int n = 0) : _n(n) {} int value() { return _n; } private: int _n; }; Type instantiation

46 Int i1, i2; P p1(i1), p2(i2); if ( p1 == p2 ) cout << "OK" << endl; OK Example P

47 Existential types -- hiding

48 The existential type calculus

49 Existential types -- examples

50 Packages -- examples

51 class event { event protected: event(event* x) : ev(x) {} public: int type() { return ev->type(); } void* rawevent() { return ev; } private: event* ev; }; class xevent : public event { X public: int type() { return X->type(); } private: struct XEvent* X; }; Hiding in C++

52 Self-reference

53 A calculus for recursive types

54 Recursive types -- examples

55 Inheritance semantics -- self-reference

56 Object inheritance -- dynamic binding

57 Object inheritance -- contravariance

58 Bounded type constraints

59 Inheritance and constraints

60 Inheritance != subtyping Eiffel class C inherit P redefine eq feature b : Boolean is true; eq( other : like Current ) : Boolean is begin Result := (other.i = Current.i) and (other.b = Current.b) end end C Inheritance and subtyping in Eiffel

61 p,v:P, c:C v:=c; v.eq(p); error p has no b Example

62 Summary

63 Abstract inheritance abstract inheritance -- declarative relation inheritance networks -- non-monotonic reasoning taxonomic structure -- predicate calculus 1

64 The subtype relation types -- sets of values the subtype relation -- refinement rules functions -- contravariance objects -- as records 2

65 Flavors of polymorphism typing -- protection against errors flavors -- parametric, inclusion, overloading, coercion inheritance -- incremental modification mechanism 3

66 Type abstraction subtypes -- typed lambda calculus overloading -- intersection types bounded polymorphism -- generics and inheritance 4

67 Existential types hiding -- existential types packages -- abstract data types 5

68 Self reference self-reference -- recursive types object semantics -- unrolling inheritance -- dynamic binding subtyping -- inconsistencies 6

69 Questions 1. How would you characterize inheritance as applied in knowledge representation? Discuss the problems that arise due to non-monotony. 2. How would you render the meaning of an inheritance lattice? Give some examples. 3. What is the meaning of a type? How would you characterize the relation between a type and its subtypes? 4. Characterize the subtyping rules for ranges, functions, records and variant records. Give some examples. 5. What is the intuition underlying the function subtyping rule? 6. What is understood by the notion of objects as records? Explain the subtyping rule for objects. 7. Discuss the relative merits of typed formalisms and untyped formalisms. 8. What flavors of polymorphism can you think of? Explain how the various flavors are related to programming language constructs.

70 9. Discuss how inheritance may be understood as an incremental modification mechanism. 10. Characterize the simple type calculus, that is the syntax, type assignment and refinement rules. Do the same for and. 11. Type the following expressions: (a), (b) and (c) 12. Verify whether: (a), (b) (c) 13. Explain how you may model abstract data types as existential types. 14. What realizations of the type can you think of? Give at least two examples. 15.Prove that. 16.Prove that, for.

71 Further reading As further reading I recommend [CW85] and [Pierce93]. As another source of material and exercises consult [Palsberg94]. An exhaustive overview of the semantics of object systems, in both first order and second order calculi, is further given in [ObjectCalculus].


Download ppt "Principles of Object-Oriented Software Development Polymorphism."

Similar presentations


Ads by Google