1 A Model for Java with Wildcards Nicholas Cameron Sophia Drossopoulou Erik Ernst
2 Background Wildcards Previous attempts Existential types Subtyping Typing
3 Background
Java Adding Wildcards to the Java Language Torgersen, Ernst, Plesner Hansen, von der Ahé, Bracha, Gafter. SAC 04. Existential types to model wildcards 4
Wild FJ Torgersen, Ernst, Plesner Hansen. FOOL 05. ‘On the fly’ existential types No soundness proof J Cameron, Ernst, Drossopoulou. FtfJP 07. Explicit existential types Explicit open/close expressions Partial model Variant Parametric Types Igarashi, Viroli. ECOOP 02, TOPLAS 06. Partial model No capture conversion 5
On Decidability of Nominal Subtyping with Variance Kennedy, Pierce. FOOL 07. Variance and Generalized Constraints for C# Generics Emir, Kennedy, Russo, Yu. ECOOP 06 Typeless Programming in Java 5.0 Plümicke, Bäuerle. PPPJ 06 A Flow-Based Approach for Variant Parametric Types Chin, Craciun, Khoo, Popeea. OOPSLA 06. Subtyping Existential Types Weir, Theimann. FTfJP 08. 6
Wildcards A very brief description
Java Type Cup 8
Java Generics Type Cup 9
Generics - Invariant Subtyping Cup 10 /
Java Wildcards Type Cup 11
Bounds Cup 12
Generics - Invariant Subtyping Cup 13 /
Wildcards - Variant Subtyping Cup 14 /
Wildcard Capture void test(Cup cx) {...} void m(Cup c) { this.test(c); } 15
Wildcard Capture void test(Cup cx) {...} void m(Cup c) { this.test(c); //this. test(c); //Z fresh } 16
Our Previous Attempts
Naïve Approach Cup 18
Naïve Approach Cup 19 Expressible but not denotable types
'On the fly' Existential Types capture( Cup ) = X.Cup 20
'On the fly' Existential Types capture( Cup ) = X.Cup Too complicated 21
Explicit Existential Types open... close... 22
Explicit Existential Types open... close... Lower Bounds Expressivity 23
Scope Violation void m( X.Cup x) { this. m2(x); } 24
Scope Violation void m( X.Cup x) { this. m2(x); } Alpha Renaming 25
... 26
Modelling Concepts
Tame FJ
Explicit existential types Implicit packing (in subtyping ( XS-ENV )) Implicit unpacking (in type rules) Inferred type parameters this. m(x); Separation of subtyping Subclassing, extended subclassing, subtyping Guarding environment... e : T | Tracks unpacked type variables 29
30 Existential Types
Cup X.Cup 31
Cup X Drink.Cup 32
But Why?
Capture conversion Expressible but not denotable types
Pair make(List x) {...} void test (Pair x) {...} void m(List l, Pair p) { test(p); } 35
36
37
38
X.Pair Type is expressible but not denotable 39
Pair make(List x) {...} void test (Pair x) {...} void m( X.List l, X,Y.Pair p) { test(p); test(make(l)); X.Pair q = make(l); } 40
Pair make(List x) {...} void test (Pair x) {...} void m( X.List l, X,Y.Pair p) { test(p); test(make(l)); X.Pair q = make(l); } 41
Pair make(List x) {...} void test (Pair x) {...} void m( X.List l, X,Y.Pair p) { test(p); test(make(l)); X.Pair q = make(l); } 42
43 Subtyping
Wildcards Subtyping Cup 44
Wildcards Subtyping Cup .Cup Cup .Cup Cup Y Tea.Cup 45
, ’ T <: B ’.[T/X]N <: X B.N (S-E NV ) 46 ┴ ┴
Cup .Cup Tea <: Object .[Tea/X]Cup (S-E NV ) 47 ┴ ┴
Cup .Cup Tea <: Drink .[Tea/X]Cup (S-E NV ) 48 ┴ ┴
Cup Y Tea.Cup Y Tea Y <:Tea <: Drink Y Tea.[Y/X]Cup (S-E NV ) 49 ┴ ┴
3 flavours of subtyping?
Java 4 Subtyping 51 Object CoffeeTea Drink List Vector
Java 5 Subtyping 52 Object CoffeeTea Drink Z Drink.List Z.List List Z Drink.Vector Z.Vector Vector X In class C... Z X.List Z X.Vector
Tame FJ Subclassing 53 Object CoffeeTea Drink List Vector X In class C...
Tame FJ Extended Subtyping 54 Z Drink.List Z.List List Z Drink.Vector Z.Vector Vector In class C... Z X.List Z X.Vector
Tame FJ Subtyping Tea X In class C...
TameFJ Subtyping 56 Object CoffeeTea Drink Z Drink.List Z.List List Z Drink.Vector Z.Vector Vector X In class C... Z X.List Z X.Vector
But Why? Subtyping is too flexible and complicated to use directly in the proofs Subclassing Properties of the class hierarchy – e.g., field/method lookup Extended Subclassing If we need wildcard subtyping, but can't deal with lower bounds E.g., preservation of existential types Subtyping Still required – e.g., result of inversion lemmas, due to T-S UBS 57
58 Typing
59
60
61
class Cup { Mug f; void m(Cup c) { c.f; } 62
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug 63 ┴
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;this:Cup c.f:Mug | Z 64 ┴ ┴
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;this:Cup c.f:Mug | Z 65 ┴ ┴
66
67
68
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;this:Cup c.f:Mug | Z 69 ┴ ┴
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;Z Mug X;this:Cup c.f:Mug | Z 70 ┴ ┴ ┴
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;Z Mug X;this:Cup c.f:Mug | Z X Z.Mug OK 71 ┴ ┴ ┴ ┴
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;Z Mug X;this:Cup c.f:Mug | Z X Z.Mug OK X;this:Cup c.f: Z.Mug | 72 ┴ ┴ ┴ ┴ ┴
class Cup { Mug f; void m(Cup c) { c.f; } X;this:Cup c: Z.Cup | fType( f, Cup ) = Mug X;Z Mug X;this:Cup c.f:Mug | Z X Z.Mug OK X;this:Cup c.f: Z.Mug | 73 ┴ ┴ ┴ ┴ ┴
Full model Proven sound Future Work ‘More full’ - features, translation More expressive? Mechanical proof checking 75
76 Thank You!
class Cup { Cup f; Cup m2(String x) {...} void m(Cup c) { c.f = c.f; c.f = c; c.f = c.m2(“a string”); } 77
Variance in C# and Scala C# is invariant, but variance proposed Declaration site annotations Less complex, but less flexible Vs. use site declaration in Java class CovariantCup... Formalisation and soundness C# - Emir, Kennedy, Russo, Yu. ECOOP 06 Scala - Cremet. PhD Thesis, 06 (Virtual types) 78