1 / 39

Wildcards, Variance and Virtual Classes

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

conway
Download Presentation

Wildcards, Variance and Virtual Classes

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Wildcards, Variance and Virtual Classes Transfer Talk Nicholas Cameron ncameron@doc.ic.ac.uk http://www.nicholascameron.co.uk

  2. 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

  3. Parametric Polymorphism * * Disclaimer: I don’t really write my shopping list in Excel. Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  4. Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  5. Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  6. Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  7. Parametric Polymorphism Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  8. 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

  9. 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

  10. Invariance <: Dog <: Animal Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  11. Invariance List<Dog> <: List<Animal> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  12. Wildcards Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  13. ? • ? 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

  14. 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

  15. Subtype Variance List<Dog> <: List<?> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  16. Subtype Variance • Covariance • Contravariance List<Dog> <: List<? extends Animal> List<Animal> <: List<? super Dog> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  17. 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

  18. 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

  19. Formalisation J Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  20. J • Existential types List<?> = X.List<X> List<? super Dog> = Y[Dog Object].List<Y> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  21. 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

  22. J • Subtyping List<Dog> <: X.List<X> Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  23. Back to wildcard capture Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. Soundness Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  30. 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

  31. 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

  32. Wildcards – further work • Finish proofs • Nearly done! • Formalise translation • Imperative version Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  33. Questions? Thank you! Report: http://www.nicholascameron.co.uk/papers/transfer.pdf Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  34. Tribe Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  35. 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

  36. 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

  37. Tribe • Flexible, powerful, elegant • Undecidable subtyping? • Complete subtyping? Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

  38. 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

  39. Tribe – further work • Decidable fragment – T9 • Tribal Ownership • Programming with virtual classes Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

More Related