Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism...
-
date post
19-Dec-2015 -
Category
Documents
-
view
222 -
download
0
Transcript of Harvey SiyPrinciples of Object-Oriented Software Development by Eliens Slide 1 Polymorphism...
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 1
Polymorphism
Principles of Object-Oriented
Software Development
(Chapter 9)
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 2
Objectives
Present a more systematic treatment of the concept of subtyping
Examine more general notions of polymorphism Introduce methods for formal analysis of types,
polymorphism and inheritance
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 3
Inheritance
Uses• During analysis: a technique for discovering
taxonomies• During object design: an opportunity reusing code
from existing classes 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
When is it safe to do inheritance?
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 4
Liskov Substitution Principle
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 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.
How can we formally define this substitution principle?
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 5
Outline
The subtype relation Flavors of polymorphism Type abstraction
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 6
Abstract inheritance
In knowledge representation, inheritance hierarchies are used to represent “is-a” relationships
Abstract inheritance: creating declarative relationships Declarative taxonomic hierarchies represent predicate
logic assertions These relationships may be non-monotonic Monotonicity: all properties of the ancestor are preserved
by the descendants Knowledge representation is non-monotonic
• “is-not” relationships in addition to “is-a” relationships Example:
xCanFlyxBirdxPenguinx .
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 7
The subtype relation
Subtypes satisfy the monotinicity property 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
VVVVIntV
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 8
Subtype Refinement Relations
Sub-range inclusion
Functions
Records
Variants
mnmn
mmnn
.... and
'
and (contravariance)
mmnn
ii
aaaa
nmmi
:,,::,,:
..
1111
1 for
nnmm
ii
aaaa
nmmi
::::
..
1111
1 for
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 9
Sub-range Inclusion
If n n’ and m m’ then the subrange n’..m’ is a subtype of n..m
Example:• 3..5 2..6
mnmn
mmnn
.... and
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 10
Functions
If is a subtype of and is a subtype of thenThe 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
Examples:float float is not a subtype of float intint float is not a subtype of float intint int is a not subtype of float intfloat char is a subtype of float intdouble int is a subtype of float int
'
and
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 11
Contravariance Rule for Function Subtyping
Function subtypes can have looser parameter type and/or tighter return type
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 A subtype function is a refinement of its
supertype, relaxing the restrictions for its calling function, while strengthening its obligations to the calling function.
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 12
Records
If type i is a subtype of i, for all i = 1..m, then
The record defined by a1:1,…,an:n is a subtype of the record defined by a1:1,…,am:m
Note that n could be greater than m the subtype can introduce additional fields not in the supertype
Example:{age:int, speed:int, fuel:int} {age:int, speed:int}
Locations that use the supertype are not aware of the new fields so they are never used in those contexts.
mmnn
ii
aaaa
nmmi
:,,::,,:
..
1111
1 for
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 13
Variants
Variants are also known as unions in C, C++ If type i is a subtype of i, for all i = 1..m, then
A variant [a1: 1 … am:m] is a subtype
of [a1:1 … an:n], m n
Subtypes of a variant are created by removing one or more of the available variant fields
nnmm
ii
aaaa
nmmi
::::
..
1111
1 for
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 14
Objects as Records
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).
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.
Exception cases: data hiding, generics, self-references
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 15
Object 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 }
nnmnmn
ii
aaaa
ni
:,,::,,:
..
1111
1 for
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 16
Type Conformance
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
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 17
Outline
The subtype relation Flavors of polymorphism
• Inclusion• Parametric• Overloading
Type abstraction
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 18
Typing – protection against errors• Static typing – type-checking at compile time • Strong typing – all expressions are type consistent
Untyped languages – flexibility• bitstrings, sets, -calculus
Exceptions to monomorphic typing:• overloading, coercion, subranging, value-sharing
(nil)
The Nature of Types
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 19
Polymorphism
The ability to process objects differently depending on their data type
Polymorphic function • A function that can evaluate to and be applied to
values of different types. Polymorphic datatype
• A datatype that contains elements of different types. Polymorphism is not restricted to object types
only.
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 20
Flavors of polymorphism
Inclusion polymorphism – to model inheritance Parametric polymorphism – supports generics (templates) Intersection types – overloading done systematically Coercion – typecasting
Polymorphism
Ad-hoc
UniversalParametric
Inclusion
Overloading
Coercion
(generics)
(inheritance)
(intersection)
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 21
Inheritance – Incremental Modification
Inclusion polymorphism can be understood by regarding inheritance as a means to define the properties of a subtype incrementally, by adding information
Thus, we can characterize inheritance as R = P + M Result = Parent + Modifier
Example: R = {a1,a2} + {a2,a3} = {a1,a2,a3} Independent attributes: M disjoint from P Overlapping attributes: M overrules P 3 types of attributes:
• Redefined attributes – redefined by M• Virtual attributes – must be defined in M• Recursive attributes – defined in P
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 22
Outline
The subtype relation Flavors of polymorphism Type abstraction
• Subtype calculus• Intersection types• Bounded polymorphism• Existential types – hiding• Self-reference
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 23
Type Abstraction
Purpose: to define the valid typing for the various forms of polymorphism, by expressing them in a syntactic way, using type expressions
Based on lambda calculus Lambda terms
• Variables• Abstraction• Application
MxM .x
MNNM and
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 24
More on Lambda Calculus
Laws
• Beta conversion (parameter passing)
• Extensionality in application
• Extensionality in abstraction
NxMNMx :.
ZNZMNZMZNM and
NxMxNM ..
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 25
More on Lambda Calculus
Substitution
•
•
•
•
NNxx :
yxyNxy if :
NxMyNxMy :.:. NxMNxMNxMM ::: 2121
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 26
Type Calculi
Subtype calculus – models subtypes Intersection type calculus – models overloading Calculus for bounded polymorphism – models
generics Existential type calculus – models data hiding Recursive type calculus – models inheritance
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 27
Elements• •
Type assignment•
•
Refinement•
Simple Type Calculus for Subtypes
21|::
21|.:|:: eeexxe
ex
ex
.:
: :
21
21 :,:
ee
ee
:
:
e
e
A type can be a basic type or a functionAn expression e can be a
variable, a typed function abstraction, or application
The expression e has type , assuming that type assignments in are valid
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 28
Example applications
Determining subtypes•
• Since Int is a subtype of Double, D is a subtype of S 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
IntDoubleD
xDoublexD
IntIntS
xIntxS
:
1.:
:
1.:
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 29
Intersection Type Calculus
Elements•
Type assignment•
Refinement•
•
•
n
i ni
..
..1,
1
2121 ..||::
n
i
e
nie
..:
..1,:
1
in ..1
nn .... 11
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 30
Example Application
This gives us a way to express a limited form of overloading by enumerating all possible types
Defining + as an overloaded operator:
RealRealReal,: IntIntInt
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 31
Elements• •
Type assignment•
•
Refinement•
Calculus for Bounded Polymorphism
2121 .||||:: Top eeeeexxe |.||.:|:: 21
..
: ,
e
e
:
.:,
e
e
..
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 32
Example Applications
Parametrized types
Specifying semantics for C++ templates
33
.:
.:.
Intid
id
xxid
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 33
Extend from calculus for bounded polymorphism Elements
• •
Type assignment•
Refinement•
Existential Type Calculus
21.|:: epacke .in |::
..in
:
epack
e
..
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 34
Existential Types and Data Hiding
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
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 35
Extend from calculus for bounded polymorphism Elements
• •
Type assignment•
Refinement•
Calculus for Recursive Types
.|::
nn eaeaselfe ,,).(|:: 11
nnnn
ii
aaaaself
e
:,,:.,,).(
1..ni :
1111
..
,
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 36
Self-References and Inheritance
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
Harvey Siy Principles of Object-Oriented Software Development by Eliens Slide 37
Summary
Subtype refinement relations determine what types are substitutable
There are many flavors of polymorphism of which inclusion polymorphism due to inheritance is one
Semantics of different flavors of polymorphism can be specified using type calculi derived from lambda calculus
Consistency of a programming language’s type system can be analyzed using these calculi