390 likes | 559 Views
Wildcards, Variance and Virtual Classes. Transfer Talk. Nicholas Cameron. Halfway Already…. Parametric Polymorphism and Subtype Variance Java Wildcards Generics and wildcards Programming with wildcards Formalising wildcards Virtual Classes Virtual Types Virtual Classes Tribe
E N D
Wildcards, Variance and Virtual Classes Transfer Talk Nicholas Cameron ncameron@doc.ic.ac.uk http://www.nicholascameron.co.uk
Halfway Already… • Parametric Polymorphism and Subtype Variance • Java Wildcards • Generics and wildcards • Programming with wildcards • Formalising wildcards • Virtual Classes • Virtual Types • Virtual Classes • Tribe • Parametric types Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Parametric Polymorphism * * Disclaimer: I don’t really write my shopping list in Excel. Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Generics class List<X> { X get(int i)... void add(X x)... } List<Dog> l = new List<Dog>(); l.add(new Dog()); Dog d = l.get(0); Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Virtual Types class List { virtual type X; X get(int i)... void add(X x)... } class DogList extends List { X = Dog; } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Invariance <: Dog <: Animal Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Invariance List<Dog> <: List<Animal> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcards Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
? • ? May be used as an actual type argument • A list of some type (but we don’t know (or care) which type) void m(List<?> aList) ... Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Bounds • Wildcards can have upper and lower bounds • ub is a list of some type that is a subtype of Animal • lb is a list of some type that is a supertype of Dog void m(List<? extends Animal> ub) ... void m(List<? super Dog> lb) ... Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Subtype Variance List<Dog> <: List<?> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Subtype Variance • Covariance • Contravariance List<Dog> <: List<? extends Animal> List<Animal> <: List<? super Dog> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
The trade-off void m1(List<? extends Dog> l) { l.add(new Dog()); //type error Dog d = l.get(0); //ok } void m2(List<? super Dog> l) { l.add(new Dog()); //ok Dog d = l.get(0); //type error } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcard Capture <X>void m1(List<X> x) {…} void m2 (List<?> y) { this.m1(y); } • Note: List<?>List<X> • BUT, we can pass it here • Capture conversion Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Formalisation J Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
J • Existential types List<?> = X.List<X> List<? super Dog> = Y[Dog Object].List<Y> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
J • Capture = open <X>void m1(List<X> x) {…} void m2 (Y.List<Y> y) { open y as z:List<Y> in this.<Y>m1(z); } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
J • Subtyping List<Dog> <: X.List<X> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Back to wildcard capture Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcard Capture • ...is not subtyping: <X>void m1(List<X> x1, List<X> x2) {…} void m2 (List<?> x1, List<?> x2) { m1(x1, x2); //Type error! } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcard Capture • …requires existential types: <X>void m1(Pair<X, X> x) {…} <X>Pair<X, X> m2(List<X> x) {…} void m3 (Pair<?, ?> xx, List<?> x) { m1(xx); //Type error m1(m2(x)); //OK! } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcard Capture <X>void m1(Pair<X, X> x) {…} <X>Pair<X, X> m2(List<X> x) {…} void m3 (Pair<?, ?> xx, List<?> x) { m1(xx); //Type error m1(m2(x)); //OK! } • xx:Y,Z.Pair<Y, Z> • m2(x):Y.Pair<Y, Y> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcard Capture • m2(x):Y.Pair<Y, Y> <X>void m1(Pair<X, X> x) {…} <X>Pair<X, X> m2(List<X> x) {…} void m3 (Y.List<Y> x) { open x as y:List<Y> in this.<Y>m1(this.<Y>m2(y)); } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Type System • Expressible but not denotable types • Caused by wildcard capture • Solved by using existential types • Pack/close vs. subtyping • Modelling capture • Soundness requirement Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Soundness Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Soundness • Challenges caused by: • Type parameters in reduction of open expression • Lower bounds • Complexity of formalism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Lemmas • uBound*(lBound(X)) <:uBound*(X) • C<P0…Pn> <: D<Q0…Qn> subclassing • T <: R T = R’ • Well formed type environments Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Wildcards – further work • Finish proofs • Nearly done! • Formalise translation • Imperative version Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Questions? Thank you! Report: http://www.nicholascameron.co.uk/papers/transfer.pdf Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Tribe Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Tribe • Nested virtual classes class Graph { class Node { } class Edge { Node n1; Node n2; } } class ColourGraph extends Graph { class Node { Colour c; } } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Tribe • Path dependent types this.out.Node n1.out.Node this.out.n1.out.Edge .Graph.Node class Graph { class Node { } class Edge { Node n1; Node n2; } } Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Tribe • Flexible, powerful, elegant • Undecidable subtyping? • Complete subtyping? Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Tribe – further work • Parametric polymorphism and variance • Virtual types? • Extend Tribe with virtual types • Generics? • Orthogonal? • Type variables in paths • Genericity over families Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London
Tribe – further work • Decidable fragment – T9 • Tribal Ownership • Programming with virtual classes Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London