[email protected] 1 A Model for Java with Wildcards Nicholas Cameron Sophia Drossopoulou Erik...
-
Upload
bruno-walford -
Category
Documents
-
view
214 -
download
0
Transcript of [email protected] 1 A Model for Java with Wildcards Nicholas Cameron Sophia Drossopoulou Erik...
• Background• Wildcards• Previous attempts• Existential types• Subtyping• Typing
Background
• Java 5.0• 2004
• Adding Wildcards to the Java LanguageTorgersen, Ernst, Plesner Hansen, von der Ahé, Bracha, Gafter. SAC 04.
• Existential types to model wildcards
• Wild FJTorgersen, Ernst, Plesner Hansen. FOOL 05.
• ‘On the fly’ existential types• No soundness proof
• JCameron, Ernst, Drossopoulou. FtfJP 07.
• Explicit existential types• Explicit open/close expressions• Partial model
• Variant Parametric TypesIgarashi, Viroli. ECOOP 02, TOPLAS 06.
• Partial model• No capture conversion
• 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.0Plümicke, Bäuerle. PPPJ 06
• A Flow-Based Approach for Variant Parametric Types
Chin, Craciun, Khoo, Popeea. OOPSLA 06.
• Subtyping Existential TypesWeir, Theimann. FTfJP 08.
Java Type
Java Generics Type
Cup<Tea>[email protected] 9
Java Wildcards Type
Cup<?>[email protected] 11
Wildcards - Variant Subtyping
Cup<Tea> <: Cup<Object>
Cup<Tea> <: Cup<?>
Cup<Tea> <:Cup<? extends Drink>
Cup<? extends Tea> <:Cup<? extends Drink>
/
Wildcard Capture
void <X> test(Cup<X> cx) {...}
void m(Cup<?> c) {
this.test(c);
}
Wildcard Capture
void <X> test(Cup<X> cx) {...}
void m(Cup<?> c) {
this.test(c);
//this.<Z>test(c);
//Z fresh
}
Our Previous Attempts
...
Modelling Concepts
Tame FJ
• Explicit existential typesImplicit packing (in subtyping (XS-ENV))
Implicit unpacking (in type rules)
• Inferred type parametersthis.<>m(x);
• Separation of subtypingSubclassing, extended subclassing, subtyping
• Guarding environment... e : T |
Tracks unpacked type variables
Existential Types
But Why?
But Why?
Capture conversion
Expressible but not denotable types
<X> Pair<X,X> make(List<X> x) {...}
<X> void test (Pair<X, X> x) {...}
void m(List<?> l, Pair<?, ?> p) {
test(p);
}
<X> Pair<X,X> make(List<X> x) {...}
<X> void test (Pair<X, X> x) {...}
void m(X.List<X> l, X,Y.Pair<X, Y> p) {
test(p);
test(make(l));
X.Pair<X, X> q = make(l);}
<X> Pair<X,X> make(List<X> x) {...}
<X> void test (Pair<X, X> x) {...}
void m(X.List<X> l, X,Y.Pair<X, Y> p) {
test(p);
test(make(l));
X.Pair<X, X> q = make(l);}
<X> Pair<X,X> make(List<X> x) {...}
<X> void test (Pair<X, X> x) {...}
void m(X.List<X> l, X,Y.Pair<X, Y> p) {
test(p);
test(make(l));
X.Pair<X, X> q = make(l);}
Subtyping
Wildcards Subtyping
Cup<Tea> <: Cup<?>
Cup<Tea> <:Cup<? extends Drink>
Cup<? extends Tea> <:Cup<? extends Drink>
Wildcards Subtyping
Cup<Tea> <: Cup<?>
.Cup<Tea> <: X.Cup<X>
Cup<Tea> <:Cup<? extends Drink>
.Cup<Tea> <: XDrink.Cup<X>
Cup<? extends Tea> <:Cup<? extends Drink>
YTea.Cup<Y> <: XDrink.Cup<X>
Cup<Tea> <: Cup<?>
.Cup<Tea> <: X.Cup<X>
Tea <: Object
.[Tea/X]Cup<X> <:X.Cup<X>(S-ENV)
┴
┴
Cup<Tea> <:Cup<? extends Drink>
.Cup<Tea> <: XDrink.Cup<X>
Tea <: Drink
.[Tea/X]Cup<X> <:XDrink.Cup<X>(S-ENV)
┴
┴
Cup<? extends Tea> <:Cup<? extends Drink>
YTea.Cup<Y> <: XDrink.Cup<X>
YTea Y <:Tea <: Drink
YTea.[Y/X]Cup<X> <:XDrink.Cup<X>(S-ENV)
┴
┴
3 flavours of subtyping?
Java 5 Subtyping
Object
CoffeeTea
Drink
ZDrink.List<Z>
Z.List<Z>
List<Tea>
ZDrink.Vector<Z>
Z.Vector<Z>
Vector<Tea>
X
In class C<X extends Tea> ...
ZX.List<Z>
ZX.Vector<Z>
Tame FJ Subclassing
Object
CoffeeTea
Drink List<Z>
List<Tea>
Vector<Z>
Vector<Tea>
X
In class C<X extends Tea> ...
Tame FJ Extended Subtyping
ZDrink.List<Z>
Z.List<Z>
List<Tea>
ZDrink.Vector<Z>
Z.Vector<Z>
Vector<Tea>
In class C<X extends Tea> ...
ZX.List<Z> ZX.Vector<Z>
Tame FJ Subtyping
Tea
X
In class C<X extends Tea> ...
TameFJ Subtyping
Object
CoffeeTea
Drink
ZDrink.List<Z>
Z.List<Z>
List<Tea>
ZDrink.Vector<Z>
Z.Vector<Z>
Vector<Tea>
X
In class C<X extends Tea> ...
ZX.List<Z>
ZX.Vector<Z>
But Why?
• Subtyping is too flexible and complicated to use directly in the proofs
• SubclassingProperties of the class hierarchy – e.g., field/method lookup
• Extended SubclassingIf we need wildcard subtyping, but can't deal with lower bounds
E.g., preservation of existential types
• SubtypingStill required – e.g., result of inversion lemmas, due to T-SUBS
Typing
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> | fType(f, Cup<Z>) = Mug<Z>
┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> | fType(f, Cup<Z>) = Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z
┴┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> | fType(f, Cup<Z>) = Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z
┴┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> |
fType(f, Cup<Z>) = Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z
┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> |
fType(f, Cup<Z>) = Mug<Z> X;Z Mug<Z> <:Z.Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z
┴
┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> |
fType(f, Cup<Z>) = Mug<Z> X;Z Mug<Z> <:Z.Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z X Z.Mug<Z> OK
┴
┴
┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> |
fType(f, Cup<Z>) = Mug<Z> X;Z Mug<Z> <:Z.Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z X Z.Mug<Z> OK
X;this:Cup<X> c.f:Z.Mug<Z> |
┴
┴
┴
┴
class Cup<X> {
Mug<X> f;
void m(Cup<?> c) {
c.f;
}
}
X;this:Cup<X> c:Z.Cup<Z> |
fType(f, Cup<Z>) = Mug<Z> X;Z Mug<Z> <:Z.Mug<Z>
X;this:Cup<X> c.f:Mug<Z> | Z X Z.Mug<Z> OK
X;this:Cup<X> c.f:Z.Mug<Z> |
┴
┴
┴
┴
• Full model
• Proven sound
• Future Work• ‘More full’ - features, translation• More expressive?• Mechanical proof checking
class Cup<X> {
Cup<X> f;
Cup<X> m2(String x) {...}
void m(Cup<?> c) {
c.f = c.f;
c.f = c;
c.f = c.m2(“a string”);
}
}
Variance in C# and Scala
• C# is invariant, but variance proposed
• Declaration site annotationsLess complex, but less flexible
Vs. use site declaration in Java
class CovariantCup<+X> ...
• Formalisation and soundnessC# - Emir, Kennedy, Russo, Yu. ECOOP 06
Scala - Cremet. PhD Thesis, 06 (Virtual types)