Supervised by: Dr Sophia Drossopoulou, Dr Nobuko Yoshida [email protected] Wildcards, Variance...

39
[email protected] http:// www.nicholascameron.co. Supervised by: Dr Sophia Drossopoulou, Dr Nobuko Yoshida Wildcards, Variance and Virtual Classes Nicholas Cameron Transfer Talk

Transcript of Supervised by: Dr Sophia Drossopoulou, Dr Nobuko Yoshida [email protected] Wildcards, Variance...

[email protected]://

www.nicholascameron.co.uk

Supervised by: Dr Sophia Drossopoulou,Dr Nobuko Yoshida

Wildcards, Variance and Virtual Classes

Nicholas Cameron

Transfer Talk

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Halfway Already…o Parametric Polymorphism and Subtype Variance

o Java Wildcardso Generics and wildcardso Programming with wildcardso Formalising wildcards

o Virtual Classeso Virtual Typeso Virtual Classeso Tribeo 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

Genericsclass 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 Typesclass 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

?o ? May be used as an actual type

argument

o 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

Boundso Wildcards can have upper and lower

bounds

o ub is a list of some type that is a subtype of Animal

o 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 Varianceo Covariance

o Contravariance

List<Dog> <: List<? extends Animal>

List<Animal> <: List<? super Dog>

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

The trade-offvoid 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

o Note: List<?> List<X>o BUT, we can pass it here

o Capture conversion

<X>void m1(List<X> x) {…}

void m2 (List<?> y){ this.m1(y);}

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Formalisation

J

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Jo Existential types

List<?> = X.List<X>

List<? super Dog> =

Y[Dog Object].List<Y>

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Jo 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

o 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 Captureo ...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 Captureo …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

o xx:Y,Z.Pair<Y, Z>o m2(x): Y.Pair<Y, Y>

<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

o 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 Systemo Expressible but not denotable types

o Caused by wildcard captureo Solved by using existential types

o Pack/close vs. subtyping

o Modelling capture

o Soundness requirement

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Soundness

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Soundnesso Challenges caused by:

o Type parameters in reduction of open expression

o Lower bounds

o Complexity of formalism

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Lemmaso uBound*(lBound(X)) <: uBound*(X)

o C<P0…Pn> <: D<Q0…Qn> subclassing

o T <: R T = R’

o Well formed type environments

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Wildcards – further worko Finish proofs

o Nearly done!

o Formalise translation

o 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

Tribeo 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

Tribeo 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

Tribeo Flexible, powerful, elegant

o Undecidable subtyping?

o Complete subtyping?

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Tribe – further worko Parametric polymorphism and variance

o Virtual types?o Extend Tribe with virtual types

o Generics?o Orthogonal?o Type variables in pathso Genericity over families

Wildcards, Variance and Virtual ClassesNick Cameron, Imperial College London

Tribe – further worko Decidable fragment – T9

o Tribal Ownership

o Programming with virtual classes