Successfully reported this slideshow.

Subtyping

638 views

Published on

Published in: Education
  • Be the first to comment

  • Be the first to like this

Subtyping

  1. 1. Polymorphism Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 2 / 16
  2. 2. 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
  3. 3. 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
  4. 4. 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
  5. 5. Subtyping Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 5 / 16
  6. 6. 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
  7. 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. e : σ σ <: τ e:τ Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 6 / 16
  8. 8. 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
  9. 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 τ . ∀x : τ : q(x) σ <: τ ∀y : σ : q(y ) Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 7 / 16
  10. 10. 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
  11. 11. 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
  12. 12. 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
  13. 13. 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
  14. 14. Record Type {foo : S, bar : T } Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 9 / 16
  15. 15. 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
  16. 16. 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
  17. 17. Array Type Array t Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 12 / 16
  18. 18. 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
  19. 19. 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
  20. 20. 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
  21. 21. 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
  22. 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. Instances of generic classes are not compatible either. Michal P´se (CTU in Prague) ıˇ Object Programming Lect. 2: Subtyping October 5, 2010 14 / 16
  23. 23. 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
  24. 24. 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

×