1 - 10.3 - Covariance & anchored types 2 Covariance? Within the type system of a programming language, a typing rule or a type conversion operator is*:

Slides:



Advertisements
Similar presentations
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Advertisements

Building Bug-Free O-O Software: An Introduction to Design By Contract A presentation about Design By Contract and the Eiffel software development tool.
SCOOP: Simple Concurrent Object-Oriented Programming Extend the pure, strongly typed, object-oriented language Eiffel with a general and powerful concurrency.
Eiffel: Analysis, Design and Programming Bertrand Meyer (Nadia Polikarpova) Chair of Software Engineering.
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
An Abstract Interpretation Framework for Refactoring P. Cousot, NYU, ENS, CNRS, INRIA R. Cousot, ENS, CNRS, INRIA F. Logozzo, M. Barnett, Microsoft Research.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Chair of Software Engineering CAT calls (Changed Availability or Type) these slides contain advanced material and are optional.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 7: SCOOP Type System (based on work with Piotr Nienaltowski)
Chair of Software Engineering Object-Oriented Software Construction Bertrand Meyer Lesson 21 Last update: 25 May 2004 Type issues, covariance and catcalls.
1 Advanced Material The following slides contain advanced material and are optional.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Principles of Object-Oriented Software Development The language Eiffel.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering OOSC - Lecture 21 1 Object-Oriented Software Construction Bertrand Meyer.
Chair of Software Engineering Avoid a void Bertrand Meyer ©Bertrand Meyer, 2008.
Chair of Software Engineering Concurrent Object-Oriented Programming Prof. Dr. Bertrand Meyer Lecture 9: Contracts and Inheritance (based on work with.
Eiffel: Analysis, Design and Programming Bertrand Meyer Chair of Software Engineering.
Managing Reuse Presented by: Aisha Al-Hammadi. Outline Introduction History. The technical and managerial advantages of Reusing Solutions. The main challenges.
Chair of Software Engineering ATOT - Lecture 23, 23 June Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering OOSC - Summer Semester Object-Oriented Software Construction Bertrand Meyer Lecture 6: Genericity.
Chapter 1 Principles of Programming and Software Engineering.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
Chair of Software Engineering ATOT - Lecture 7, 23 April Advanced Topics in Object Technology Bertrand Meyer.
Chair of Software Engineering ATOT - Lecture 22, 18 June Advanced Topics in Object Technology Bertrand Meyer.
Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor John Altidor Taming the Wildcards: Combining Definition- and Use-Site Variance.
Subclasses and Subtypes CMPS Subclasses and Subtypes A class is a subclass if it has been built using inheritance. ▫ It says nothing about the meaning.
Types(2). 2 Recursive Problems  One or more simple cases of the problem have a straightforward, nonrecusive solution  The other cases can be redefined.
Comparison of OO Programming Languages © Jason Voegele, 2003.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Chapter 8 More Object Concepts
CISC6795: Spring Object-Oriented Programming: Polymorphism.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
10 Conversion. Let’s start with conformance Conformance determines when a type may be used in lieu of another. Conformance relies on inheritance. The.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
C++ for Engineers and Scientists Second Edition Chapter 6 Modularity Using Functions.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
© Bertrand Meyer and Yishai Feldman Notice Some of the material is taken from Object-Oriented Software Construction, 2nd edition, by Bertrand Meyer (Prentice.
Generics and Collections. Introduction Generics New feature of J2SE 5.0 Provide compile-time type safety Catch invalid types at compile time Generic methods.
Introduction CS 3358 Data Structures. What is Computer Science? Computer Science is the study of algorithms, including their  Formal and mathematical.
Contracts for Concurrency - Contracts & Inheritance Aryabrata Basu University of Georgia.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 6 Using Methods.
Chapter 12 Support for Object oriented Programming.
Type == Class  reference type -- store only the object’s address  simple (primitive) type -- for storing small data values Other options Wrapper classes.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 14: Substitution Principle.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
© 2006 Pearson Addison-Wesley. All rights reserved 2-1 Chapter 2 Principles of Programming & Software Engineering.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 16: Smalltalking about Objects.
Conformance Object-Oriented Programming Spring
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
Ceg860 (Prasad)L17IT1 Inheritance Techniques Subcontracting Anchored Types.
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Programming Logic and Design Seventh Edition
Design by Contract Jim Fawcett CSE784 – Software Studio
Design by Contract Jim Fawcett CSE784 – Software Studio
Principles of Programming and Software Engineering
Polymorphism.
C++ for Engineers and Scientists Second Edition
Algorithms Chapter 3 With Question/Answer Animations
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Abstract Types Defined as Classes of Variables
Presentation transcript:

Covariance & anchored types

2 Covariance? Within the type system of a programming language, a typing rule or a type conversion operator is*: covariant if it preserves the ordering, ≤, of types, which orders types from more specific to more generic: animal := dog (animal: ANIMAL, dog: DOG) list := string_list (list: LIST [ ANY ], string_list: LIST [ STRING ]) contravariant if it reverses this ordering, which orders types from more generic to more specific dog := animal string_list := list novariant if neither of these apply. *

3 The need for covariance CUSTOMERMINORBEVERAGE SOFT_ DRINK ALCOHOL drink : BEVERAGE serve (b : BEVERAGE ) do drink := b ensure drink = b end drink : SOFT_DRINK serve (b : SOFT_DRINK ) do drink := b end Client Inherit

4 Defeating covariance (1) CUSTOMERMINORBEVERAGE SOFT_ DRINK ALCOHOL Pimms : ALCOHOL c : CUSTOMER Shiloh : MINOR c. serve ( Pimms) c := Shiloh c. serve (b ) b := Pimms b : BEVERAGE drink : BEVERAGE serve (b : BEVERAGE ) do drink := b end drink : SOFT_DRINK serve (b : SOFT_DRINK ) do drink := b end c. serve ( Pimms) c := Shiloh

5 Defeating covariance (2) CUSTOMERMINORBEVERAGE SOFT_ DRINK ALCOHOL drink : BEVERAGE serve (b : BEVERAGE ) do drink := b end drink : SOFT_DRINK serve (b : SOFT_DRINK ) do drink := b end bus. item. serve ( Pimms) bus : LIST [CUSTOMER] school_bus : LIST [MINOR] Client Inherit Pimms : ALCOHOL c : CUSTOMER Shiloh : MINOR bus := school_bus Generic conformance

6 Terminology Catcall: incorrect application of a feature to an object as a result of  Incorrect argument type  Information hiding violation (CAT: Changed Availability or Type) BIRDOSTRICH fly ??

7 Status Problem known since late 80s “Covariance” term coined by Luca Cardelli Criticism of initial Eiffel design by W. Cook et al., 1989* Numerous proposals since then *A Proposal for Making Eiffel Type-Safe, ECOOP 1989

8 Mitigating mechanism 1: non-conforming inheritance class C inherit {NONE } B feature... end No polymorphism permitted: b1 := c1 -- Invalid BC b1 : B c1 : C

9 class CUSTOMER feature drink : BEVERAGE serve (b : like drink ) do drink := b end Mitigating mechanism 2: anchored types class CUSTOMER feature drink : BEVERAGE serve (b : BEVERAGE ) do drink := b end class MINOR inherit CUSTOMER redefine drink, serve end feature drink : SOFT_DRINK serve (b : SOFT_DRINK ) do drink := b end class MINOR inherit CUSTOMER redefine drink end feature drink : SOFT_DRINK end In practice: anchored types

10 Anchoring to Current Also possible, in a class C : x : like Current In any descendant D of C (including C itself), x has type D

11 Mitigating mechanism 3: flat type checking An assignment x := y may be valid in a routine r of a class B but not necessarily in a descendant C which only redefines x (covariantly) The Eiffel type system now specifies that every class must be independently valid “Flat type checking” BC r do x := y e nd x : T y : T x : U TU

12 Unrealistic approach: contravariance Results specialized, arguments generalized Solves the problem Hard to reconcile with practice. The world does seem to be covariant. CUSTOMERMINORBEVERAGE SOFT_ DRINK ALCOHOL drink : BEVERAGE serve (b : BEVERAGE ) do drink := b end drink : ???? serve (b :????) do drink := b end

13 Novariance (argument passing) C++, Java,.NET languages Eliminates the problem (obviously) Forces programmers to do the adaptations themselves May result in brittle/unnecessary code

14 Previous solutions: genericity* class CUSTOMER [DRINK_TYPE] *Franz Weber: Getting Class Correctness and System Correctness Equivalent (How to get covariance right), TOOLS EUROPE 1992

15 Previous solutions: system-level validity* Considering all assignments, compute dynamic type set (DTS) of any variable x. If there is an assignment x := y, or argument passing, all elements of DTS of y are also in the DTS of x. Advantages:  No attempt at control flow analysis  Fixpoint algorithm  Helps with optimization Disadvantages:  Pessimistic  Not incremental  Difficulty of giving precise diagnostics *B. Meyer: Eiffel: The Language, Prentice Hall, 1991

16 Type intervals CUSTOMERMINOR drink : BEVERAGE serve (b : BEVERAGE ) do drink := b end drink : SOFT_DRINK serve (b : SOFT_DRINK ) do drink := b end Pimms : ALCOHOL c. serve ( Pimms) c := Shiloh c : CUSTOMER.. MINOR Shiloh : MINOR -- Now invalid

17 Type intervals c. serve ( Pimms) c := Shiloh c : CUSTOMER.. MINOR -- Now invalid Rule: a call x. f (a), with x : T.. U, must be valid when x is given any type in T.. U Abbreviations: x : T means x : T.. NONE LIST [T ] means LIST [T.. T ] x : CUSTOMER.. CUSTOMER x. serve ( Pimms) x := Shiloh -- Now invalid

18 The retained solution: a simplified version c. serve ( Pimms) c := Shiloh c : CUSTOMER x: frozen CUSTOMER OK Rule: a call x. f (a), with x: T not frozen, must be valid when x is given any descendant type of T x. serve ( Pimms) x := Shiloh Invalid OK Invalid

19 Genericity rule vbus. extend (Shiloh) vbus := school_bus bus : LIST [CUSTOMER ] vbus : LIST [variant CUSTOMER ] school_bus : LIST [MINOR] OK Rule:  An assignment with a different actual generic parameter requires the “variant” mark.  The variant mark precludes the use of a routine with an argument of a formal generic parameter type Invalid OK Invalid bus. extend (Shiloh) bus := school_bus

20 By the way! vbus. extend (Shiloh) vbus := school_bus bus : LIST [CUSTOMER ] vbus : LIST [variant CUSTOMER ] school_bus : LIST [MINOR] OK Invalid bus := school_bus OK bus. extend (Shiloh) Invalid since, in LIST [G], item : G is not frozen bus. item. serve (Pimms) ???

21 Anchored (“ like ”) declarations New results:  “Flat type checking” guarantees that like Current declarations are safe  b : like a, with a of type T, may be considered an abbreviation not for b : T as now, but for Then only explicit (non-anchored) covariance remains! b : like a b : frozen T

22 Status Implemented and currently being experimented Criterion: must not break code unless there is a real catcall risk Need for formal proof Proposal for a more flexible mechanism (“forget”)

23 Covariance: summary Reconcile modeling power with safety