3. Languages
Languages are either
monomorphic—every value and variable has only one type or
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 3 / 16
4. Languages
Languages are either
monomorphic—every value and variable has only one type or
polymorphic—values and variables may have multiple types.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 3 / 16
5. Types of Polymorphism
Universal
Parametric
Inclusion
Ad hoc
Overloading
Coercion
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 4 / 16
7. Subsumption Principle
Definition (Subsumption principle)
If σ is a subtype of τ , then a value of type σ may be provided whenever a
value of type τ is required.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 6 / 16
8. Subsumption Principle
Definition (Subsumption principle)
If σ is a subtype of τ , then a value of type σ may be provided whenever a
value of type τ is required.
e : σ σ <: τ
e:τ
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 6 / 16
9. Liskov Substitution Principle
Definition (Liskov substitution principle)
Let q(x) be a property provable about objects x of type τ . Then q(y )
should be true for objects y of type σ where σ is a subtype of τ .
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 7 / 16
10. Liskov Substitution Principle
Definition (Liskov substitution principle)
Let q(x) be a property provable about objects x of type τ . Then q(y )
should be true for objects y of type σ where σ is a subtype of τ .
∀x : τ : q(x) σ <: τ
∀y : σ : q(y )
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 7 / 16
11. Subsumption of Constructed Types
What are the requirements for subsumption of
function type,
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 8 / 16
12. Subsumption of Constructed Types
What are the requirements for subsumption of
function type,
product type,
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 8 / 16
13. Subsumption of Constructed Types
What are the requirements for subsumption of
function type,
product type,
intersection type and
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 8 / 16
14. Subsumption of Constructed Types
What are the requirements for subsumption of
function type,
product type,
intersection type and
union type?
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 8 / 16
15. Record Type
{foo : S, bar : T }
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 9 / 16
16. Universally Qualified Type
∀t.S(t)
where S(t) is a type expression constructed from basic types and type
variable t.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 10 / 16
17. Reference Type
Ref t
Note: references in most languages are actually unions of a reference type
and the unit type.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 11 / 16
18. Array Type
Array t
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 12 / 16
19. Subsumption of Constructed Types (II)
What are the requirements for subsumption of
record type and
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 13 / 16
20. Subsumption of Constructed Types (II)
What are the requirements for subsumption of
record type and
universally qualified type, reference type and array type?
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 13 / 16
21. Subtyping and Objects
Object are records, therefore
method’s return types may change covariantly,
types of method parameters may change contravariantly and
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 14 / 16
22. Subtyping and Objects
Object are records, therefore
method’s return types may change covariantly,
types of method parameters may change contravariantly and
field types are reference types, therefore may not change at all.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 14 / 16
23. Subtyping and Objects
Object are records, therefore
method’s return types may change covariantly,
types of method parameters may change contravariantly and
field types are reference types, therefore may not change at all.
Instances of generic classes are not compatible either.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 14 / 16
24. This Type
this
Always denotes the type of the enclosing class.
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 15 / 16
25. See
Cardelli, L. and Wegner, P. On understanding types, data abstraction,
and polymorphism. ACM Computing Surveys 17, 4 (December 1985),
471-523. http://doi.acm.org/10.1145/6041.6042
Michal P´se (CTU in Prague)
ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 16 / 16