On Subtyping, Wildcards, and Existential Types

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

1 A Model for Java with Wildcards Nicholas Cameron Sophia Drossopoulou Erik Ernst.
1 Variant Ownership with Existential Types Nicholas Cameron Sophia Drossopoulou Imperial College London.
Supervised by: Dr Sophia Drossopoulou, Dr Nobuko Yoshida Wildcards, Variance and Virtual Classes.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
OOP: Inheritance By: Lamiaa Said.
C12, Polymorphism “many forms” (greek: poly = many, morphos = form)
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
Session 07: C# OOP 4 Review of: Inheritance and Polymorphism. Static and dynamic type of an object. Abstract Classes. Interfaces. FEN AK - IT:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor John Altidor Taming the Wildcards: Combining Definition- and Use-Site Variance.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
OOPs Object oriented programming. Based on ADT principles  Representation of type and operations in a single unit  Available for other units to create.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Often categorize concepts into hierarchies: Inheritance Hierarchies Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved.
Self Type Constructors Atsushi Igarashi Kyoto University Joint work with Chieri Saito 1.
Inheritance and Polymorphism Daniel Liang, Introduction to Java Programming.
Existential Quantification for Variant Ownership Nicholas Cameron Sophia Drossopoulou Imperial College London (Victoria University of Wellington)‏
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Multiple Ownership Nicholas Cameron Sophia Drossopoulou James Noble Matthew Smith.
Multiple Ownership Nicholas Cameron Sophia Drossopoulou James Noble Matthew Smith.
Featherweight Generic Ownership Alex Potanin, James Noble Victoria University of Wellington Dave Clarke CWI, Netherlands Robert Biddle Carlton University.
OOPs Object oriented programming. Abstract data types  Representationof type and operations in a single unit  Available for other units to create variables.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 9 Java Fundamentals Objects/ClassesMethods Mon.
Inheritance 2 Mehdi Einali Advanced Programming in Java 1.
Encoding Ownership Types in Java Nicholas Cameron James Noble Victoria University of Wellington, New Zealand.
FEN 2014UCN Teknologi/act2learn1 Object-Oriented Programming “ The Three Pillars of OOP”: Encapsulation Inheritance Polymorphism The Substitution Principle.
Class Relationships Lecture Oo08 Polymorphism. References n Booch, et al, The Unified Modeling Language User Guide, Chapt 10 p.125 n Fowler & Scott, UML.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
SUBCLASSES - JAVA. The Purpose of Subclasses Class Farm String getOwner() void setOwner(String s) int getSize() void setSize(int s) Class DairyFarm String.
Towards a Semantic Model for Java Wildcards Sophia Drossopoulou Mariangiola Dezani-Ciancaglini Imperial College London Università di Torino Italy Nicholas.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Comparing Universes and Existential Ownership Types Nicholas Cameron Werner Dietl ETH Zurich Victoria University of Wellington.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Advanced Programming in Java
Advanced Programming in Java
Nicholas Cameron James Noble Victoria University of Wellington
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
6.001 SICP Object Oriented Programming
Inheritance and Polymorphism
CS 326 Programming Languages, Concepts and Implementation
Object Oriented Concepts -II
PRINCIPALES OF OBJECT ORIENTED PROGRAMMING
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance B.Ramamurthy 11/7/2018 B.Ramamurthy.
Names, Binding, and Scope
Inheritance Basics Programming with Inheritance
Formal Models for Programming Languages
Inheritance, Polymorphism, and Interfaces. Oh My
Advanced Programming in Java
Computer Programming with JAVA
Inheritance and Polymorphism
Java Programming Course
More About Inheritance & Interfaces
Java Inheritance.
Advanced Programming in Java
CMSC 202 Generics.
Review of Previous Lesson
C++ Programming CLASS This pointer Static Class Friend Class
Final and Abstract Classes
Lecture 10 Concepts of Programming Languages
Extending Classes Through Inheritance
A Considerate Specification of the Composite Pattern
Presentation transcript:

On Subtyping, Wildcards, and Existential Types Nicholas Cameron Sophia Drossopoulou Victoria University of Wellington Imperial College London

Polymorphism An expression may have many types An expression with a given type may be treated as if it had a different type

Polymorphism Inclusion polymorphism – subtyping Parametric polymorphism – generics Ad-hoc polymorphism – coercions and overloading

Subclassing Reuse code and encapsulate implementation class Dog {...} class Poodle extends Dog {...} class Husky extends Dog {...}

Subclassing Gives rise to subtyping Poodle <: Dog Husky <: Dog Which gives inclusion polymorphism Dog d = new Husky(); d = new Poodle();

Subclassing Inclusion polymorphism can be represented using existential types Igarashi, Viroli; TOPLAS '06 Bruce, Petersen, Fiech; ECOOP '97 Saito, Igarashi; SAC '09 Abadi, Cardelli, Vishwanathan; POPL '96 .... ƎX<:Dog.X The existential quantification makes explicit the partial knowledge about values with such types vs exact types: @Dog

Generics Parametric polymorphism in Java List<Dog> Invariant List<Poodle> </: List<Dog>

Wildcards Allow for variant generic types List<? extends Poodle> <: List<? extends Dog>

Wildcards Can be represented using existential types Torgersen et al; SAC '04 Torgersen, Ernst, Plesner Hansen; FOOL '05 Cameron, Drossopoulou, Ernst; ECOOP '08 Cameron, Ernst, Drossopoulou; FTfJP '07 ƎX<:Dog.List<X> The existential quantification makes explicit the partial knowledge about values with such types

Is there a connection? Ǝ Ǝ Ǝ Ǝ Ǝ Ǝ Ǝ

Is there a connection? Two different uses for existential types Are they the same type of types? Can they be combined cleanly or are they orthogonal? How do we combine the two uses?

A Story FJ - ƎFJ C ƎX<:C.X FGJ - ƎFGJ C<D> ƎX<:C<D>.X Wildcards - ƎWFJ C<? extends D> ƎX<:(ƎY<:D.C<Y>). X

Types Break the automatic connection from subclassing to subtyping to polymorphism and classes to types Give a clearer and more precise account of typing Class names give brands, which cannot be used as types by the programmer Malayeri, Aldrich; ECOOP '08 Brands correspond to class names, what is declared, not how they can be used as types E.g., Dog, Poodle Subclassing relates brands

Types Existential types may be bounded by brands All polymorphism is explicit and given by the existential types ƎX<:Dog.X Existential types are the bridge from subclassing to polymorphism Subclassing between brands: Poodle <: Dog Subtyping between types: ƎX<:Poodle.X <: ƎX<:Dog.X

Types A brand can be used to make a type, it is not a type by itself, in particular the subtyping properties are not specified We could also have exact types, eg @Dog which is the type of Dog without subclassing @Poodle </: @Dog

Types BUT we treat brands as types and sub-branding as subtyping

ƎFJ Model for Java 1.4 Uses existential types to make polymorphism explicit Corresponds to FJ N ::= C P ::= N | X T ::= ƎX<:P.X Brands Parameters Types

ƎFGJ

ƎFGJ Model for Java with generics (but not wildcards) Simply adds type parameters to ƎFJ eg List<Dog> in Java ƎX<:List<Dog>.X in ƎFGJ Note: List<Dog> is a brand, types are parameterised by brands, not types N ::= C<P> P ::= N | X T ::= ƎX<:P.X Brands Parameters Types

Wildcards This very nearly gives us wildcards for free! That is, wildcards emerge naturally from the combination of a calculus which makes polymorphism explicit and type parameters Example: List<? extends Dog> ƎX<:(ƎY<:Dog.List<Y>).X This is how we can combine existential types

Wildcards We generalise the syntax very slightly Bound type variables may be used as bounds and parameters Allow quantification of brands as well as types ƎX<:(ƎY<:Dog.List<Y>).X And change the definition of well-formed types accordingly The rest of the system just works! Not orthogonal

Wildcards N ::= C<P> Brands P ::= ƎX<:P.N | X | X Parameters T ::= ƎX<:P.X Brands Parameters Types

Understanding Wildcards

Understanding Wildcards Wildcards are difficult to understand Confusion between the polymorphic behaviour of class types and the invariance of class names when used as type parameters Polymorphic behaviour of type parameters is complex We try to make the variance explicit

Understanding Wildcards Type parameters are not types Variance denoted by existential quantification `at the top'

Understanding Wildcards Dog List<Dog> List<? extends Dog> ƎX<:Dog.X ƎX<:List<Dog>.X ƎX<:(ƎY<:Dog.List<Y>).X

Thank you! Questions? ncameron@ecs.vuw.ac.nz

Understanding Wildcards class Box<X> { X get() {...} void set(X x) {...} } Box<Dog> b = new Box<Dog>(); b.set(new Poodle()); Dog d = b.get(); Box<? extends Dog> b = new Box<Dog>(); b.set(null); Dog d = b.get() Box<? super Dog> b = new Box<Dog>(); b.set(new Poodle()); Object d = b.get()

Understanding Wildcards class Box<X> { ƎY<:X.Y get() {...} void set(ƎY<:X.Y x) {...} } ƎX<:Box<Dog>.X b = new Box<Dog>(); b.set(new Poodle()); ƎX<:Dog.X d = b.get(); ƎX<:(ƎY<:Dog.List<Y>).X b = new Box<Dog>(); b.set(null); ƎX<:Dog.X d = b.get() ƎX<:(ƎY>:Dog.List<Y>).X b = new Box<Dog>(); b.set(new Poodle()); ƎX<:Object.X d = b.get()