Presentation is loading. Please wait.

Presentation is loading. Please wait.

Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism Principles of Object-Oriented Software Development (Chapter.

Similar presentations


Presentation on theme: "Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism Principles of Object-Oriented Software Development (Chapter."— Presentation transcript:

1 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism Principles of Object-Oriented Software Development (Chapter 9)

2 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 2 Objectives l Present a more systematic treatment of the concept of subtyping l Examine more general notions of polymorphism l Introduce methods for formal analysis of types, polymorphism and inheritance

3 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 3 Inheritance l Uses During analysis: a technique for discovering taxonomies During object design: an opportunity reusing code from existing classes l Inheritance during object design Implementation inheritance When the subclass and superclass relationship does not represent a taxonomy Specification inheritance When the subclass is also a subtype of the superclass l When is it safe to do inheritance?

4 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 4 Liskov Substitution Principle l General principle: If an object of type S can be substituted in all places where an object of type T is expected, then S is a subtype of T l Object-oriented interpretation: A class S, which inherits from a superclass T, is considered a subtype of T if S can be substituted in all places where T is expected. Advantage: new subclasses of T can be written without modifying the methods of T. Specification inheritance follows the Liskov substitution principle. l How can we formally define this substitution principle?

5 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 5 Outline l The subtype relation l Flavors of polymorphism l Type abstraction

6 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 6 Abstract inheritance l In knowledge representation, inheritance hierarchies are used to represent “is-a” relationships l Abstract inheritance: creating declarative relationships l Declarative taxonomic hierarchies represent predicate logic assertions l These relationships may be non-monotonic l Monotonicity: all properties of the ancestor are preserved by the descendants l Knowledge representation is non-monotonic “is-not” relationships in addition to “is-a” relationships l Example:

7 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 7 The subtype relation l Subtypes satisfy the monotinicity property l Types can be represented as sets Union of basic data types and compound types (records, variants, functions) More formally: Subtypes correspond to subsets Subtypes should be compatible with supertypes Subtype can be used anywhere supertype is used – conformance or substitutability property Several refinement relationships preserve this property

8 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 8 Subtype Refinement Relations l Sub-range inclusion l Functions l Records l Variants (contravariance)

9 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 9 Sub-range Inclusion l If n  n’ and m  m’ then the subrange n’..m’ is a subtype of n..m l Example: 3..5  2..6

10 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 10 Functions l If  is a subtype of  and  is a subtype of  then The function  is a subtype of the function  (Function notation:  means function taking parameter of type  and return value of type  ) Subtype can have looser parameter type and/or tighter return type l Examples: float  float is not a subtype of float  int int  float is not a subtype of float  int int  int is a not subtype of float  int float  char is a subtype of float  int double  int is a subtype of float  int

11 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 11 Contravariance Rule for Function Subtyping l Function subtypes can have looser parameter type and/or tighter return type l Intuitively, for a subtype function to be used in place of its supertype: It must be able to support at least the same set of values as the input parameters of the supertype, and, It must return no value more than what its supertype returns l A subtype function is a refinement of its supertype, relaxing the restrictions for its calling function, while strengthening its obligations to the calling function.

12 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 12 Records l If type  i is a subtype of  i, for all i = 1..m, then The record defined by a 1 :  1,…,a n :  n is a subtype of the record defined by a 1 :  1,…,a m :  m Note that n could be greater than m  the subtype can introduce additional fields not in the supertype l Example: {age:int, speed:int, fuel:int}  {age:int, speed:int} l Locations that use the supertype are not aware of the new fields so they are never used in those contexts.

13 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 13 Variants l Variants are also known as unions in C, C++ l If type  i is a subtype of  i, for all i = 1..m, then A variant [a 1 :  1  …  a m :  m ] is a subtype of [a 1 :  1  …  a n :  n ], m  n Subtypes of a variant are created by removing one or more of the available variant fields

14 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 14 Objects as Records l An easy way to understand when a subclass is also a subtype of its superclass is to treat objects as records (whose fields can be data or functions). l As a heuristic, methods and attributes of the superclass can be overridden as long as the replacement methods and attributes are subtypes of the originals. l Exception cases: data hiding, generics, self- references

15 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 15 Object Subtyping Examples l type any = { } l type entity = { age : int } l type vehicle = { age : int, speed : int } l type machine = { age : int, fuel : string } l type car = { age : int, speed : int, fuel : string }

16 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 16 Type Conformance l S provides at least the operations of T l for each operation in T, the corresponding operation in S has the same number of arguments l the type of the result of operations of S conform to those of the operations of T l the types of arguments of operations of T conform to those of the operations of S

17 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 17 Outline l The subtype relation l Flavors of polymorphism Inclusion Parametric Overloading l Type abstraction

18 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 18 l Typing – protection against errors Static typing – type-checking at compile time Strong typing – all expressions are type consistent l Untyped languages – flexibility bitstrings, sets, -calculus l Exceptions to monomorphic typing: overloading, coercion, subranging, value-sharing (nil) The Nature of Types

19 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 19 Polymorphism l The ability to process objects differently depending on their data type l Polymorphic function A function that can evaluate to and be applied to values of different types. l Polymorphic datatype A datatype that contains elements of different types. l Polymorphism is not restricted to object types only.

20 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 20 Flavors of polymorphism l Inclusion polymorphism – to model inheritance l Parametric polymorphism – supports generics (templates) l Intersection types – overloading done systematically l Coercion – typecasting Polymorphism Ad-hoc Universal Parametric Inclusion Overloading Coercion (generics) (inheritance) (intersection)

21 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 21 Inheritance – Incremental Modification l Inclusion polymorphism can be understood by regarding inheritance as a means to define the properties of a subtype incrementally, by adding information l Thus, we can characterize inheritance as R = P + M l Result = Parent + Modifier Example: R = {a1,a2} + {a2,a3} = {a1,a2,a3} l Independent attributes: M disjoint from P l Overlapping attributes: M overrules P l 3 types of attributes: Redefined attributes – redefined by M Virtual attributes – must be defined in M Recursive attributes – defined in P

22 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 22 Outline l The subtype relation l Flavors of polymorphism l Type abstraction Subtype calculus Intersection types Bounded polymorphism Existential types – hiding Self-reference

23 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 23 Type Abstraction l Purpose: to define the valid typing for the various forms of polymorphism, by expressing them in a syntactic way, using type expressions l Based on lambda calculus l Lambda terms Variables Abstraction Application

24 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 24 More on Lambda Calculus l Laws Beta conversion (parameter passing) Extensionality in application Extensionality in abstraction

25 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 25 More on Lambda Calculus l Substitution

26 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 26 Type Calculi l Subtype calculus – models subtypes l Intersection type calculus – models overloading l Calculus for bounded polymorphism – models generics l Existential type calculus – models data hiding l Recursive type calculus – models inheritance

27 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 27 l Elements l Type assignment l Refinement Simple Type Calculus for Subtypes A type  can be a basic type  or a function An expression e can be a variable, a typed function abstraction, or application The expression e has type , assuming that type assignments in  are valid

28 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 28 Example applications l Determining subtypes Since Int is a subtype of Double, D is a subtype of S l It can be shown that C++ does not fully support subtyping by providing a type expression that is valid in this calculus but its corresponding C++ program gets a typechecking error

29 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 29 Intersection Type Calculus l Elements l Type assignment l Refinement

30 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 30 Example Application l This gives us a way to express a limited form of overloading by enumerating all possible types l Defining + as an overloaded operator:

31 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 31 l Elements l Type assignment l Refinement Calculus for Bounded Polymorphism

32 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 32 Example Applications l Parametrized types l Specifying semantics for C++ templates

33 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 33 l Extend from calculus for bounded polymorphism l Elements l Type assignment l Refinement Existential Type Calculus

34 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 34 Existential Types and Data Hiding l Existential types enable modeling of abstract data types and hiding by means of packages and type abstraction Hiding – expressed as existential types Packages – expressing abstract data types

35 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 35 l Extend from calculus for bounded polymorphism l Elements l Type assignment l Refinement Calculus for Recursive Types

36 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 36 Self-References and Inheritance l Calculus employing recursive types Recursive type – defined with itself as one of the components Can be used to characterize the semantics of inheritance more precisely Object semantics determined by unrolling the recursive type Express the semantics of dynamic binding

37 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 37 Summary l Subtype refinement relations determine what types are substitutable l There are many flavors of polymorphism of which inclusion polymorphism due to inheritance is one l Semantics of different flavors of polymorphism can be specified using type calculi derived from lambda calculus l Consistency of a programming language’s type system can be analyzed using these calculi

38 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 38 Beta conversion -- examples

39 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 39 The lambda calculus -- properties

40 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 40 A simple type calculus

41 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 41 The subtype calculus

42 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 42 Subtypes -- examples

43 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 43 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

44 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 44 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; };

45 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 45 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++

46 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 46 Intersection types

47 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 47 The intersection type calculus

48 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 48 Intersection types -- examples

49 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 49 Overloaded function selection rules l [1] no or unavoidable conversions -- array-> pointer, T -> const T l [2] integral promotion -- char->int, short-> int, float->double l [3] standard conversions -- l int->double, double->int, derived* -> base* l [4] user-defined conversions -- constructors and operators l [5] ellipsis in function declaration --...

50 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 50 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++

51 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 51 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

52 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 52 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

53 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 53 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

54 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 54 Bounded polymorphism

55 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 55 The bounded type calculus

56 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 56 Parametrized types -- examples

57 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 57 Bounded quantification -- examples

58 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 58 Point* move(Point* p, int d); require int Point::x Point* move(Point* p, int d) { p.x += d; return p; } example move

59 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 59 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++

60 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 60 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

61 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 61 Int i1, i2; P p1(i1), p2(i2); if ( p1 == p2 ) cout << "OK" << endl; OK Example P

62 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 62 Existential types -- hiding

63 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 63 The existential type calculus

64 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 64 Existential types -- examples

65 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 65 Packages -- examples

66 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 66 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++

67 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 67 Self-reference

68 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 68 A calculus for recursive types

69 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 69 Recursive types -- examples

70 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 70 Inheritance semantics -- self-reference

71 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 71 Object inheritance -- dynamic binding

72 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 72 Object inheritance -- contravariance

73 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 73 Bounded type constraints

74 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 74 Inheritance and constraints

75 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 75 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

76 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 76 p,v:P, c:C v:=c; v.eq(p); error p has no b Example

77 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 77 Summary

78 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 78 Abstract inheritance l abstract inheritance -- declarative relation l inheritance networks -- non-monotonic reasoning l taxonomic structure -- predicate calculus 1

79 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 79 The subtype relation l types -- sets of values l the subtype relation -- refinement rules l functions -- contravariance l objects -- as records 2

80 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 80 Flavors of polymorphism l typing -- protection against errors l flavors -- parametric, inclusion, overloading, coercion l inheritance -- incremental modification mechanism 3

81 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 81 Type abstraction l subtypes -- typed lambda calculus l overloading -- intersection types l bounded polymorphism -- generics and inheritance 4

82 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 82 Existential types l hiding -- existential types l packages -- abstract data types 5

83 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 83 Self reference l self-reference -- recursive types l object semantics -- unrolling inheritance -- dynamic binding l subtyping -- inconsistencies 6

84 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 84 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.

85 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 85 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.

86 Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 86 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 "Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism Principles of Object-Oriented Software Development (Chapter."

Similar presentations


Ads by Google