Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor John Altidor Taming the Wildcards: Combining Definition- and Use-Site Variance.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Taming Wildcards in Javas Type System Ross Tate Alan Leung Sorin Lerner University of California, San Diego.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Generic programming in Java
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Overview of Java (continue). Announcements You should have access to your repositories and HW0 If you have problems getting HW0, let me know If you’ve.
1 Software Testing and Quality Assurance Lecture 12 - The Testing Perspective (Chapter 2, A Practical Guide to Testing Object-Oriented Software)
Java Generics.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
Java Generics. 2 The Dark Ages: Before Java 5 Java relied only on inclusion polymorphism  A polymorphism code = Using a common superclass Every class.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Covariance & anchored types 2 Covariance? Within the type system of a programming language, a typing rule or a type conversion operator is*:
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
©Ian Sommerville 2000Software Engineering, 6/e, Chapter 91 Formal Specification l Techniques for the unambiguous specification of software.
1 A Short Introduction to (Object-Oriented) Type Systems Kris De Volder.
C++ fundamentals.
Types for Positive and Negative Ints Int = {..., -2, -1, 0, 1, 2,... } Pos = { 1, 2,... } (not including zero) Neg = {..., -2, -1 } (not including zero)
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
Introduction to Object-oriented programming and software development Lecture 1.
GENERAL CONCEPTS OF OOPS INTRODUCTION With rapidly changing world and highly competitive and versatile nature of industry, the operations are becoming.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
1 Concepts: Linguistic Support for Generic Programming in C++ Douglas Gregor Jeremy Siek Gabriel Dos Reis Jaakko Järvi Bjarne Stroustrup Andrew Lumsdaine.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
©Ian Sommerville 2000Software Engineering, 6th edition. Chapter 9 Slide 1 Formal Specification l Techniques for the unambiguous specification of software.
ADSA: Subtypes/ Advanced Data Structures and Algorithms Objective –explain how subtyping/subclassing and generics are combined in Java –introduce.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
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.
Effective Java: Generics Last Updated: Spring 2009.
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.
A Bipartite Graph Model of Information Flow IFIP WG 2.3, May 2014 Gary T. Leavens (with John L. Singleton) University of Central Florida Orlando Florida.
WXGE6103 Software Engineering Process and Practice Formal Specification.
Self Type Constructors Atsushi Igarashi Kyoto University Joint work with Chieri Saito 1.
1 Classes and Objects in C++ 2 Introduction Java is a true OO language and therefore the underlying structure of all Java programs is classes. Anything.
Existential Quantification for Variant Ownership Nicholas Cameron Sophia Drossopoulou Imperial College London (Victoria University of Wellington)‏
CS 106 Introduction to Computer Science I 04 / 23 / 2010 Instructor: Michael Eckmann.
Chapter 12 Support for Object oriented Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
The Scala API. Scala has a reputation of being a difficult language Some people feel that Scala is beyond what the average programmer can master Others.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
Overview of C++ Templates
CMSC 330: Organization of Programming Languages Java Generics.
CSCE 314 Programming Languages Java Generics II Dr. Hyunyoung Lee 1.
Polymorphism (generics) CSE 331 University of Washington.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
Java Software Solutions Lewis and Loftus Chapter 9 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Enhanced Class Design -- Introduction.
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
CS-2852 Data Structures LECTURE 2 Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Soundness of Types Ensuring that a type system is not broken.
Object orientation and Packaging in Java Object Orientation and Packaging Introduction: After completing this chapter, you will be able to identify.
Type soundness In a more formal way. Proving Soundness of Type Systems Goal of a sound type system: –if the program type checks, then it never “crashes”
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
CSE 341 Section 10 Subtyping, Review, and The Future.
1 Aspectual Caml an Aspect-Oriented Functional Language Hideaki Tatsuzawa Hidehiko Masuhara Akinori Yonezawa University of Tokyo.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
Towards a Semantic Model for Java Wildcards Sophia Drossopoulou Mariangiola Dezani-Ciancaglini Imperial College London Università di Torino Italy Nicholas.
Comp1004: Object Oriented Design I Abstract Classes and Interfaces.
On Subtyping, Wildcards, and Existential Types
Interfaces.
03/10/14 Inheritance-2.
INTRODUCTION TO OBJECT-ORIENTED PROGRAMMING (OOP) & CONCEPTS
CSCI 161: Introduction to Programming Function
User-Oriented Language Design
Objects and Aspects: What we’ve seen so far
Generic programming in Java
Verified Subtyping with Traits and Mixins
The Scala API.
Lecture 10 Concepts of Programming Languages
Presentation transcript:

Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor John Altidor Taming the Wildcards: Combining Definition- and Use-Site Variance

2 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Outline  Motivation for Variance.  Existing Approaches. Java only supports use-site variance. C# and Scala only support definition-site variance.  What Is New Here: Combine Definition-Site and Use-Site Variance. Both in a single language, each using the other Case Study – Inferring Def-Site for Java. Insights into Formal Reasoning.  Summary.

3 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Software Reusability  Writing reusable software is hard; many factors: Generality Adaptability Modularity Simplicity of interface / Ease of use …  Programming language support Enable reusable code without introducing bugs. Focus on integrating two flavors of genericity: Generics – Parametric Polymorphism Subtyping – Inclusion Polymorphism

4 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Subtyping – Inclusion Polymorphism  Example: Java inheritance. class Animal { void speak() { } } class Dog extends Animal { void speak() { print(“bark”); } } class Cat extends Animal { void speak() { print(“meow”); } }

5 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Subtyping – Example Client void performSpeak(Animal animal) { animal.speak(); } Can be a Dog, Cat, or any other Animal.

6 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Common Code Structure class ListOfDogs { void add(Dog num) { … } Dog get(int i) { … } int size() { … } }

7 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Common Code Structure (cont.) class ListOfAnimals { void add(Animal num) { … } Animal get(int i) { … } int size() { … } }

8 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Generics – Parametric Polymorphism class List { void add(X x) { … } X get(int i) { … } int size() { … } } type parameter write X read X no X List ≡ List of Animal s List ≡ List of Dog s Customized Lists

9 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Generics and Subtyping  Dog <: Animal (Dog is an Animal).  Cat <: Animal (Cat is an Animal).  List No!  A List can add a Cat to itself. A List cannot.

10 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Variance Introduction  When is C a subtype of C ? class RList { X get(int i) { … } int size() { … } } Can read from but not write to.  It is safe to assume RList.  Why?

11 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Flavors of Variance - Covariance Generic Covariance <:  Assuming Dog <: Animal (Dog is an Animal).

12 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Flavors of Variance - Contravariance Generic Contravariance <:  Assuming Dog <: Animal (Dog is an Animal).

13 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Four Flavors of Variance  How do programmers specify variance? Covariance:T <: U ⇒ C. Contravariance:T <: U ⇒ C. Bivariance: C,for all T and U. Invariance: C,if T <: U and U <: T.

14 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Definition-Site Variance (C#/ Scala)  Programmer specifies variance in definition as in Scala and C#.  Variance of a type position. Return types: covariant. Arguments types: contravariant. class RList { X get(int i) { … } int size() { … } // no method to add } class WList { void add(X x) { … } int size() { … } // no method to get }

15 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Use-Site Variance (Java Wildcards) class List { void add(X x) { … } X get(int i) { … } int size() { … } }

16 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Use-Site Variance (Java Wildcards) class List { void add(X x) { … } X get(int i) { … } int size() { … } } List

17 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Use-Site Variance (Java Wildcards) class List { void add(X x) { … } X get(int i) { … } int size() { … } } List

18 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Use-Site Variance (Java Wildcards) class List { void add(X x) { … } X get(int i) { … } int size() { … } } List

19 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Definition-Site: Pros  Conceptual Simplicity Simpler Type Expressions – no use-site annotations. Subtype policy stated in class definition.  Burden on library designers; not on users. Classes declare variance once and for all uses. class RList {... } class WList {... } class List extends RList, WList {... } covariant contravariant invariant

20 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Definition-Site: Cons class RList {... } class WList {... } class List extends RList, WList {... } covariant contravariant invariant  Redundant Types scala.collection.immutable.Map scala.collection.mutable.Map Generic with n parameters ⇒ 3 n interfaces (or 4 n if bivariance is allowed)

21 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Use-Site: Pros  Flexibility: co-, contra-, bivariant versions on the fly.  Easier on library designers Design classes in natural way No need for fractured classes. class List { void add(X x) { … } X get(int i) { … } int size() { … } }

22 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Use-Site Cons:  Burden shifts to users of generics. Library designers are often users as well.  Type signatures quickly become complicated.  Heavy variance annotation required for subtyping.  From Apache Commons-Collections Library: Iterator > createEntrySetIterator( Iterator >)

23 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Wildcards Criticism Iterator > createEntrySetIterator( Iterator >)  “We simply cannot afford another wildcards” – Joshua Bloch.  “Simplifying Java Generics by Eliminating Wildcards” – Howard Lovatt.

24 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Our Approach: Take Best of Both Worlds  Take advantages. Remove disadvantages. Simpler type expressions than in Java (burden off clients). Less redundant type definitions in C# and Scala.  Inferring definition-site variance from only use-site annotations. [1] Added notion of def-site variance without extending Java. VarLang Calculus: Denotational and language neutral approach. [1] PLDI 2011 (Altidor, Huang, Smaragdakis)

25 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Fewer Wildcard Annotations Iterator > createEntrySetIterator( Iterator >) Iterator > createEntrySetIterator( Iterator >)

26 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor VarLang Calculus  Language neutral approach.  Simplifies and generalizes all previous work.  Sample Applications : Infer definition-site variance for Java. Add use-site variance to C#.

27 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Variance Lattice  Ordering details later

28 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Java to VarLang Calculus class C { X foo(C arg1) {... } void bar(D arg2) {... } } class D { void baz(C arg3) {... } } module C { X+, C -, void+, D - } module D { void+, C - }

29 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Infer Definition-Site Variance  Def-Site Variance of C = c = +. (C is covariant)  Def-Site Variance of D = –. (D is contravariant) module C { X+, C -, void+, D - } module D { void+, C - }

30 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Case Study: Definition-Site Inference for Java  How much benefit if Java’s type system inferred definition-site variance?  Mapped Java classes to VarLang modules.  Applied inference to large, standard libraries e.g., Sun’s JDK 1.6  Example inferences java.util.Iterator is covariant. java.util.Comparator is contravariant.

31 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Sample Results from Case Study  Analysis was conservative (e.g. ignored method bodies). “ foo(List arg) ” could have been “ foo(List arg) ”. majority

32 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Insights into Formal Reasoning  Variance composition: v 1 ⊗ v 2 = v 3  Variance binary predicate: v( T; T’ )  Variance lattice: v 1 ≤ v 2  Variance of a type: var( X ; T )  Relating variance to subtyping: Subtype Lifting Lemma  Variance of a position

33 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Standard Modeling: Variance Lattice  Ordered by subtype constraint binary predicate

34 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Variance of a Type  When is C a subtype of C ?  What about existential types? ∃ X->[ ⊥ -String].Stack  We answer a more general question: When is [U/X]T <: [U’/X]T ?  Key: defined very general predicate: var( X ; T ) = variance of type T with respect to type variable X.

35 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Subtype Lifting Lemma If: (a) v ≤ var( X ; T ) (b) v( U; U’ ) Then: [U/X]T <: [U’/X]T  var( X; Iterator ) = + and + (Dog; Animal) ≡ Dog  We generalize Emir et al.’s subtype lifting lemma.  Goal property of var.

36 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Variance Composition  Variance of variable X in type A >> ?  In general, variance of variable X in type C ?  v 1 ⊗ v 2 = v 3. If: Variance of variable X in type expression E is v 2. The def-site variance of class C is v 1. Then: variance of X in C is v 3. Transform Operator

37 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Deriving Transform Operator Example Case: + ⊗ – = –  Class C is covariant.  Type E is contravariant in X.  Need to show C is contravariant in X.  For any T 1, T 2 :  Hence, C is contravariant in X.

38 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Summary of Transform  Invariance transforms everything into invariance.  Bivariance transforms everything into bivariance.  Covariance preserves a variance.  Contravariance reverses a variance.

39 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Definition of var predicate  Definition-site variance annotations are type checked using var predicate.  For further details: [2] ECOOP 2012 (Altidor, Reichenbach, Smaragdakis)

40 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor  Variance composition: v 1 ⊗ v 2 = v 3  Variance binary predicate: v( T; T’ )  Variance lattice: v 1 ≤ v 2  Variance of a type: var( X ; T )  Relating variance to subtyping: Subtype Lifting Lemma  Variance of a position: See [2] for derivation. General Theory – Template for Adding Variance A >> Subtype Policy Comparing Variances Computing Variances Variance Soundness Check Def-Site Variance

41 Taming the Wildcards: Combining Definition- and Use-Site Variance – Altidor Summary of Contributions  Generics and subtyping coexist fruitfully. Subtyping between two different instantiations.  Combine def-site and use-site variance to reap their advantages and remove disadvantages.  Generalize all previous related work.  Resolve central questions in the design of any language involving parametric polymorphism and subtyping. Variance of a type. Variance of a position.

 To my collaborators: Smaragdakis, Reichenbach, Huang, Palsberg, Tate, Cameron, Kennedy, Urban  To CLC for hosting.