Java type inference with wildcards - Leiden...
Transcript of Java type inference with wildcards - Leiden...
![Page 1: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/1.jpg)
IntroductionThe Algorithm
Conclusion
Java type inference with wildcards
Martin Plumicke
University of Cooperative EducationStuttgart/Horb
7. November 2006
Martin Plumicke Java type inference with wildcards
![Page 2: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/2.jpg)
IntroductionThe Algorithm
Conclusion
Overview
IntroductionProblemRelated work
The AlgorithmType unificationType-inference-algorithmTool demonstrationPrinciple type
Conclusion
Martin Plumicke Java type inference with wildcards
![Page 3: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/3.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Problem
Extensions of the Java 5.0 type–systemI parametrized types, type variables, type terms, wildcards
e.g.
Vector<? extends AbstractList<? super Integer>>
Complex typingsI Often it is not obvious, which are the best types for methods and
variables
I Sometimes principal types in Java 5.0 are intersection types, whichare not expressible (contradictive of writing re-usable code)
=⇒ Developing a type–inference–system, which determines principal types
Martin Plumicke Java type inference with wildcards
![Page 4: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/4.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Problem
Extensions of the Java 5.0 type–systemI parametrized types, type variables, type terms, wildcards
e.g.
Vector<? extends AbstractList<? super Integer>>
Complex typingsI Often it is not obvious, which are the best types for methods and
variables
I Sometimes principal types in Java 5.0 are intersection types, whichare not expressible (contradictive of writing re-usable code)
=⇒ Developing a type–inference–system, which determines principal types
Martin Plumicke Java type inference with wildcards
![Page 5: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/5.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Problem
Extensions of the Java 5.0 type–systemI parametrized types, type variables, type terms, wildcards
e.g.
Vector<? extends AbstractList<? super Integer>>
Complex typingsI Often it is not obvious, which are the best types for methods and
variables
I Sometimes principal types in Java 5.0 are intersection types, whichare not expressible (contradictive of writing re-usable code)
=⇒ Developing a type–inference–system, which determines principal types
Martin Plumicke Java type inference with wildcards
![Page 6: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/6.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Example: Multiplication of matrices
class Matrix extends Vector<Vector<Integer>> {Matrix mul(Matrix m) {
Matrix ret = new Matrix();
int i = 0;
while(i <size()) {Vector<Integer> v1 = this.elementAt(i);
Vector<Integer> v2 = new Vector<Integer>();
int j = 0;
while(j < v1.size()) {int erg = 0;
int k = 0;
while(k < v1.size()) {erg = erg + v1.elementAt(k)
* m.elementAt(k).elementAt(j); k++; }v2.addElement(new Integer(erg)); j++; }
ret.addElement(v2); i++; }return ret; }}
Martin Plumicke Java type inference with wildcards
![Page 7: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/7.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Alternative Typing
class Matrix extends Vector<Vector<Integer>> {Matrix/Vector<Vector<Integer>> mul(Matrix/Vector<Vector<Integer>> m) {
Matrix/Vector<Vector<Integer>> ret = new Matrix();
int i = 0;
while(i <size()) {Vector<Integer> v1 = this.elementAt(i);
Vector<Integer> v2 = new Vector<Integer>();
int j = 0;
while(j < v1.size()) {int erg = 0;
int k = 0;
while(k < v1.size()) {erg = erg + v1.elementAt(k)
* m.elementAt(k).elementAt(j); k++; }v2.addElement(new Integer(erg)); j++; }
ret.addElement(v2); i++; }return ret; }}
Martin Plumicke Java type inference with wildcards
![Page 8: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/8.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Purpose: Typless
class Matrix extends Vector<Vector<Integer>> {mul(m) {
ret = new Matrix();
i = 0;
while(i <size()) {v1 = this.elementAt(i);
v2 = new Vector<Integer>();
j = 0;
while(j < v1.size()) {erg = 0;
k = 0;
while(k < v1.size()) {erg = erg + v1.elementAt(k)
* m.elementAt(k).elementAt(j); k++; }v2.addElement(new Integer(erg)); j++; }
ret.addElement(v2); i++; }return ret; }}
Martin Plumicke Java type inference with wildcards
![Page 9: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/9.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
System determines the principle typing(s)
mul: Matrix → Matrix &Matrix→ Vector<Vector<Integer>>& . . .&Vector<? extends Vector<? extends Integer>>
→ Vector<? super Vector<Integer>>
Martin Plumicke Java type inference with wildcards
![Page 10: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/10.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Related work
over type expressions)
recursive typeF−bounded polymorphismus
(Cartesian product algorithm
(Data polymorpic Cartesian product algorithm in Java)
type system
S−unification)
[Plevyak, Chien 94]
(Precise concrete TI in OO data polymorphism function polymorphism no principal type)
(inclusion constraints)
[Hindley/Milner et. al.]
[Aiken, Wimmer 93]
[Eifrig, Smith, Trifonov 95a/b][Agesen 95/96]
no data polymorphism) [Agesen, Palsberg, Schwartzbach 93]
[Palsberg, Schwartzbach 91]
(TI in OO−Languages)
(Inheritance)
[Wang, Smith 01]
(Type parameter inference, Generic Libraries
(Type parameter inference, Generic Libraries Extension of [Palsberg, Schwartzbach 94] to parametrized types)
[Fuhrer, Tip Kiezun, Dolby, Keller 05]
[Donovan,Kiezun, Tschantz, Ernst 04]
(copying classes)
diiferentiation of declaration and allocation side
[Oxhoj, Palsberg, Schwartzbach 92]
[Palsberg, Schwartzbach 94]
Martin Plumicke Java type inference with wildcards
![Page 11: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/11.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Related work
over type expressions)
recursive typeF−bounded polymorphismus
(Cartesian product algorithm
(Data polymorpic Cartesian product algorithm in Java)
type system
S−unification)
[Plevyak, Chien 94]
(Precise concrete TI in OO data polymorphism function polymorphism no principal type)
(inclusion constraints)
[Hindley/Milner et. al.]
[Aiken, Wimmer 93]
[Eifrig, Smith, Trifonov 95a/b][Agesen 95/96]
no data polymorphism) [Agesen, Palsberg, Schwartzbach 93]
[Palsberg, Schwartzbach 91]
(TI in OO−Languages)
(Inheritance)
[Wang, Smith 01]
(Type parameter inference, Generic Libraries
(Type parameter inference, Generic Libraries
[Fuhrer, Tip Kiezun, Dolby, Keller 05]
[Donovan,Kiezun, Tschantz, Ernst 04]
(copying classes)
diiferentiation of declaration and allocation side
[Oxhoj, Palsberg, Schwartzbach 92]
[Palsberg, Schwartzbach 94]
precise typesno principle type
Extension of [Palsberg, Schwartzbach 94] to parametrized types)
Martin Plumicke Java type inference with wildcards
![Page 12: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/12.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Related work
recursive type
over type expressions)
F−bounded polymorphismus
(Cartesian product algorithm
(Data polymorpic Cartesian product algorithm in Java)
type system
S−unification)
precise types
(Type parameter inference, Generic Libraries Extension of [Palsberg, Schwartzbach 94] to parametrized types)
[Fuhrer, Tip Kiezun, Dolby, Keller 05]
[Donovan,Kiezun, Tschantz, Ernst 04]
(copying classes)
diiferentiation of declaration and allocation side
[Agesen, Palsberg, Schwartzbach 93]
[Palsberg, Schwartzbach 91]
(Precise concrete TI in OO data polymorphism function polymorphism no principal type)
(inclusion constraints)
(TI in OO−Languages)
(Inheritance)
[Wang, Smith 01]
[Hindley/Milner et. al.]
[Aiken, Wimmer 93]
[Eifrig, Smith, Trifonov 95a/b]
[Plevyak, Chien 94]
no principle type
[Agesen 95/96]
no data polymorphism)
[Oxhoj, Palsberg, Schwartzbach 92]
λ− Terms
[Palsberg, Schwartzbach 94]
(Type parameter inference, Generic Libraries
Martin Plumicke Java type inference with wildcards
![Page 13: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/13.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Our approach
[Hindley/Milner et al]
– function type constructor → (no higher–order functions)
+ function template (ty1 × . . . tyn) → ty0
(first–order functions)
+ data and function polymorphism (overloading)
Martin Plumicke Java type inference with wildcards
![Page 14: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/14.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Our approach
[Hindley/Milner et al]
– function type constructor → (no higher–order functions)
+ function template (ty1 × . . . tyn) → ty0
(first–order functions)
+ data and function polymorphism (overloading)
Martin Plumicke Java type inference with wildcards
![Page 15: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/15.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Our approach
[Hindley/Milner et al]
– function type constructor → (no higher–order functions)
+ function template (ty1 × . . . tyn) → ty0
(first–order functions)
+ data and function polymorphism (overloading)
Martin Plumicke Java type inference with wildcards
![Page 16: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/16.jpg)
IntroductionThe Algorithm
Conclusion
ProblemRelated work
Abbreviation for wildcard–types
Instead of A<? extends B> we write
A<?B>
and instead of C<? super D> we write
C<?D>.
Martin Plumicke Java type inference with wildcards
![Page 17: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/17.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
The algorithm
Type unification
Subtyping relation for type terms: ≤∗
Type Unification problem:
For two type terms θ1 and θ2 a substitution σ is demandedsuch that:
σ( θ1 )≤∗ σ( θ2 ).
Martin Plumicke Java type inference with wildcards
![Page 18: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/18.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Base of Hindley/Milner approach:(Type) unification algorithm [Martelli, Montanari 1982]
(reduce)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ
′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
(erase)Eq ∪ { θ .
= θ′ }Eq
θ = θ′
(swap)Eq ∪ { θ .
= a }Eq ∪ { a
.= θ } a ∈ TV
(subst)Eq ∪ { a
.= θ }
Eq[a 7→ θ] ∪ { a.= θ } a occurs in Eq but not in θ
Martin Plumicke Java type inference with wildcards
![Page 19: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/19.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm for Java 5.0 type terms withoutwildcards [Plumicke 2004, Unif’04, Cork]
(adapt)Eq ∪ {D<θ1, . . . , θn> l D′<θ′1, . . . , θ′m> }Eq ∪ {D′<θ
′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l D′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with
I (D<a1, . . . , an>≤∗ D′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(reduce1)Eq ∪ {C<θ1, . . . , θn> l D<θ′1, . . . , θ′n> }Eq ∪ { θπ( 1 )
.= θ′1, . . . , θπ( n )
.= θ′n }
where
I C<a1, . . . , an>≤∗ D<aπ( 1 ), . . . , aπ( n )>
I { a1, . . . , an } ⊆ TV
I π is a permutation
(reduce2)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
(erase)Eq ∪ { θ
.= θ′ }
Eqθ = θ′
(swap)Eq ∪ { θ
.= a }
Eq ∪ { a.= θ } a ∈ TV
(subst)Eq ∪ { a
.= θ }
Eq[a 7→ θ] ∪ { a.= θ }
where
I a occurs in Eqbut not in θ
Martin Plumicke Java type inference with wildcards
![Page 20: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/20.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm for Java 5.0 type terms withoutwildcards [Plumicke 2004, Unif’04, Cork]
(adapt)Eq ∪ {D<θ1, . . . , θn> l D′<θ′1, . . . , θ′m> }Eq ∪ {D′<θ
′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l D′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with
I (D<a1, . . . , an>≤∗ D′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(reduce1)Eq ∪ {C<θ1, . . . , θn> l D<θ′1, . . . , θ′n> }Eq ∪ { θπ( 1 )
.= θ′1, . . . , θπ( n )
.= θ′n }
where
I C<a1, . . . , an>≤∗ D<aπ( 1 ), . . . , aπ( n )>
I { a1, . . . , an } ⊆ TV
I π is a permutation
(reduce2)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
(erase)Eq ∪ { θ
.= θ′ }
Eqθ = θ′
(swap)Eq ∪ { θ
.= a }
Eq ∪ { a.= θ } a ∈ TV
(subst)Eq ∪ { a
.= θ }
Eq[a 7→ θ] ∪ { a.= θ }
where
I a occurs in Eqbut not in θ
Martin Plumicke Java type inference with wildcards
![Page 21: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/21.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm for Java 5.0 type terms withoutwildcards [Plumicke 2004, Unif’04, Cork]
(adapt)Eq ∪ {D<θ1, . . . , θn> l D′<θ′1, . . . , θ′m> }Eq ∪ {D′<θ
′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l D′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with
I (D<a1, . . . , an>≤∗ D′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(reduce1)Eq ∪ {C<θ1, . . . , θn> l D<θ′1, . . . , θ′n> }Eq ∪ { θπ( 1 )
.= θ′1, . . . , θπ( n )
.= θ′n }
where
I C<a1, . . . , an>≤∗ D<aπ( 1 ), . . . , aπ( n )>
I { a1, . . . , an } ⊆ TV
I π is a permutation
(reduce2)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
(erase)Eq ∪ { θ
.= θ′ }
Eqθ = θ′
(swap)Eq ∪ { θ
.= a }
Eq ∪ { a.= θ } a ∈ TV
(subst)Eq ∪ { a
.= θ }
Eq[a 7→ θ] ∪ { a.= θ }
where
I a occurs in Eqbut not in θ
Martin Plumicke Java type inference with wildcards
![Page 22: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/22.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(redUp)Eq ∪ { θ l ?θ′ }Eq ∪ { θ l θ′ } (redUpLow)
Eq ∪ { ?θ l ?θ′ }Eq ∪ { θ l θ′ } (redLow)
Eq ∪ { ?θ l θ′ }Eq ∪ { θ l θ′ }
Wildcards in outermost position
(red1)Eq ∪ {C<θ1, . . . , θn> l D<θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ) l? θ′1, . . . , θπ( n ) l? θ
′n }
Reduce rule for outermosttype symbol
where– C<a1, . . . , an>≤∗ D<aπ( 1 ), . . . , aπ( n )>
– { a1, . . . , an } ⊆ BTV
– π is a permutation
(redExt)Eq ∪ {X<θ1, . . . , θn>l? ?Y <θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ) l? θ′1, . . . , θπ( n ) l? θ
′n }
Reduce rule forextends wildcards
where– ?Y <aπ( 1 ), . . . , aπ( n )> ∈ gr(X<a1, . . . , an> )– { a1, . . . , an } ⊆ BTV
– π is a permutation
Martin Plumicke Java type inference with wildcards
![Page 23: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/23.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(redUp)Eq ∪ { θ l ?θ′ }Eq ∪ { θ l θ′ } (redUpLow)
Eq ∪ { ?θ l ?θ′ }Eq ∪ { θ l θ′ } (redLow)
Eq ∪ { ?θ l θ′ }Eq ∪ { θ l θ′ }
Wildcards in outermost position
(red1)Eq ∪ {C<θ1, . . . , θn> l D<θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ) l? θ′1, . . . , θπ( n ) l? θ
′n }
Reduce rule for outermosttype symbol
where– C<a1, . . . , an>≤∗ D<aπ( 1 ), . . . , aπ( n )>
– { a1, . . . , an } ⊆ BTV
– π is a permutation
(redExt)Eq ∪ {X<θ1, . . . , θn>l? ?Y <θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ) l? θ′1, . . . , θπ( n ) l? θ
′n }
Reduce rule forextends wildcards
where– ?Y <aπ( 1 ), . . . , aπ( n )> ∈ gr(X<a1, . . . , an> )– { a1, . . . , an } ⊆ BTV
– π is a permutation
Martin Plumicke Java type inference with wildcards
![Page 24: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/24.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(redUp)Eq ∪ { θ l ?θ′ }Eq ∪ { θ l θ′ } (redUpLow)
Eq ∪ { ?θ l ?θ′ }Eq ∪ { θ l θ′ } (redLow)
Eq ∪ { ?θ l θ′ }Eq ∪ { θ l θ′ }
Wildcards in outermost position
(red1)Eq ∪ {C<θ1, . . . , θn> l D<θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ) l? θ′1, . . . , θπ( n ) l? θ
′n }
Reduce rule for outermosttype symbol
where– C<a1, . . . , an>≤∗ D<aπ( 1 ), . . . , aπ( n )>
– { a1, . . . , an } ⊆ BTV
– π is a permutation
(redExt)Eq ∪ {X<θ1, . . . , θn>l? ?Y <θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ) l? θ′1, . . . , θπ( n ) l? θ
′n }
Reduce rule forextends wildcards
where– ?Y <aπ( 1 ), . . . , aπ( n )> ∈ gr(X<a1, . . . , an> )– { a1, . . . , an } ⊆ BTV
– π is a permutation
Martin Plumicke Java type inference with wildcards
![Page 25: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/25.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(redSup)Eq ∪ {X<θ1, . . . , θn>l?
?Y <θ′1, . . . , θ′n> }
Eq ∪ { θ′1 l? θπ( 1 ), . . . , θ′n l? θπ( n ) }
Reduce rule forsuper wildcards
where
– ?Y <aπ( 1 ), . . . , aπ( n )> ∈ gr(X<a1, . . . , an> )– { a1, . . . , an } ⊆ BTV
– π is a permutation
(redEq)Eq ∪ {X<θ1, . . . , θn>l? X<θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ).= θ′1, . . . , θπ( n )
.= θ′n }
Reduce rule forequal type symbols
(reduce2)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ
′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
Original reduce rule
Martin Plumicke Java type inference with wildcards
![Page 26: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/26.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(redSup)Eq ∪ {X<θ1, . . . , θn>l?
?Y <θ′1, . . . , θ′n> }
Eq ∪ { θ′1 l? θπ( 1 ), . . . , θ′n l? θπ( n ) }
Reduce rule forsuper wildcards
where
– ?Y <aπ( 1 ), . . . , aπ( n )> ∈ gr(X<a1, . . . , an> )– { a1, . . . , an } ⊆ BTV
– π is a permutation
(redEq)Eq ∪ {X<θ1, . . . , θn>l? X<θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ).= θ′1, . . . , θπ( n )
.= θ′n }
Reduce rule forequal type symbols
(reduce2)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ
′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
Original reduce rule
Martin Plumicke Java type inference with wildcards
![Page 27: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/27.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(redSup)Eq ∪ {X<θ1, . . . , θn>l?
?Y <θ′1, . . . , θ′n> }
Eq ∪ { θ′1 l? θπ( 1 ), . . . , θ′n l? θπ( n ) }
Reduce rule forsuper wildcards
where
– ?Y <aπ( 1 ), . . . , aπ( n )> ∈ gr(X<a1, . . . , an> )– { a1, . . . , an } ⊆ BTV
– π is a permutation
(redEq)Eq ∪ {X<θ1, . . . , θn>l? X<θ′1, . . . , θ
′n> }
Eq ∪ { θπ( 1 ).= θ′1, . . . , θπ( n )
.= θ′n }
Reduce rule forequal type symbols
(reduce2)Eq ∪ {C<θ1, . . . , θn>
.= C<θ′1, . . . , θ
′n> }
Eq ∪ { θ1.= θ′1, . . . , θn
.= θ′n }
Original reduce rule
Martin Plumicke Java type inference with wildcards
![Page 28: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/28.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(adapt)Eq ∪ {D<θ1, . . . , θn> l D ′<θ′1, . . . , θ
′m> }
Eq ∪ {D ′<θ′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l D ′<θ′1, . . . , θ
′m> }
where there are θ′1, . . . , θ
′m with outermost adapt rule
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(adaptExt)Eq ∪ {D<θ1, . . . , θn>l? ?D
′<θ′1, . . . , θ′m> }
Eq ∪ {D ′<θ′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l? ?D
′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with adapt rule: extends wildcard
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(adaptSup)Eq ∪ {D ′<θ′1, . . . , θ
′m>l?
?D<θ1, . . . , θn> }Eq ∪ {D ′<θ
′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l? ?D
′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with adapt rule: super wildcard
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
Martin Plumicke Java type inference with wildcards
![Page 29: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/29.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(adapt)Eq ∪ {D<θ1, . . . , θn> l D ′<θ′1, . . . , θ
′m> }
Eq ∪ {D ′<θ′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l D ′<θ′1, . . . , θ
′m> }
where there are θ′1, . . . , θ
′m with outermost adapt rule
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(adaptExt)Eq ∪ {D<θ1, . . . , θn>l? ?D
′<θ′1, . . . , θ′m> }
Eq ∪ {D ′<θ′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l? ?D
′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with adapt rule: extends wildcard
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(adaptSup)Eq ∪ {D ′<θ′1, . . . , θ
′m>l?
?D<θ1, . . . , θn> }Eq ∪ {D ′<θ
′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l? ?D
′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with adapt rule: super wildcard
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
Martin Plumicke Java type inference with wildcards
![Page 30: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/30.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(adapt)Eq ∪ {D<θ1, . . . , θn> l D ′<θ′1, . . . , θ
′m> }
Eq ∪ {D ′<θ′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l D ′<θ′1, . . . , θ
′m> }
where there are θ′1, . . . , θ
′m with outermost adapt rule
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(adaptExt)Eq ∪ {D<θ1, . . . , θn>l? ?D
′<θ′1, . . . , θ′m> }
Eq ∪ {D ′<θ′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l? ?D
′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with adapt rule: extends wildcard
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
(adaptSup)Eq ∪ {D ′<θ′1, . . . , θ
′m>l?
?D<θ1, . . . , θn> }Eq ∪ {D ′<θ
′1, . . . , θ
′m>[ai 7→ θi | 16 i 6n] l? ?D
′<θ′1, . . . , θ′m> }
where there are θ′1, . . . , θ
′m with adapt rule: super wildcard
I (D<a1, . . . , an>≤∗ D ′<θ′1, . . . , θ
′m>) ∈ FC( ≤ )
Martin Plumicke Java type inference with wildcards
![Page 31: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/31.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(erase1)Eq ∪ { θ l θ′ }Eq
θ≤∗ θ′
(erase2)Eq ∪ { θl? θ
′ }Eq
θ′ ∈ gr( θ )
(erase3)Eq ∪ { θ .
= θ′ }Eq
θ = θ′
(swap)Eq ∪ { θ .
= a }Eq ∪ { a
.= θ } θ 6∈ BTV , a ∈ BTV
(subst)Eq′ ∪ { a
.= θ }
Eq′[a 7→ θ] ∪ { a.= θ } a occurs in Eq′ but not in θ
Martin Plumicke Java type inference with wildcards
![Page 32: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/32.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type unification algorithm with wildcards
(erase1)Eq ∪ { θ l θ′ }Eq
θ≤∗ θ′
(erase2)Eq ∪ { θl? θ
′ }Eq
θ′ ∈ gr( θ )
(erase3)Eq ∪ { θ .
= θ′ }Eq
θ = θ′
(swap)Eq ∪ { θ .
= a }Eq ∪ { a
.= θ } θ 6∈ BTV , a ∈ BTV
(subst)Eq′ ∪ { a
.= θ }
Eq′[a 7→ θ] ∪ { a.= θ } a occurs in Eq′ but not in θ
Martin Plumicke Java type inference with wildcards
![Page 33: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/33.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Example
Subtyping relation: Matrix<a>≤∗ Vector<Vector<a>>Vector<a>≤∗ AbstractList<a>≤∗ List<a>
Application of the algorithm:{ Matrix<b> l Vector<Vector<?AbstractList<Object>>>, a l b }
(adapt)=⇒ { Vector<Vector<b>> l Vector<Vector<?AbstractList<Object>>>,
a l b }(red1,redEq)
=⇒ { b .= ?AbstractList<Object>,
a l b }(subst)=⇒ { b .
= ?AbstractList<Object>,a l ?AbstractList<Object> }
(new sets)=⇒ {{ b .
= List<Object>, a.= AbstractList<Object> },
{ b .= List<Object>, a
.= ?AbstractList<Object> },
{ b .= List<Object>, a
.= Vector<Object> },
{ b .= List<Object>, a
.= ?Vector<Object> } }
Martin Plumicke Java type inference with wildcards
![Page 34: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/34.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type–inference–algorithm
Type assumptions: For each absent type in the program atype-placeholder (fresh type variable) is assumed.
Run over the abstract syntax tree: During the run over abstract syntaxtree of the coresponding java class the types are calculatedgradually by type unification.
Multiplying the assumptions: If the result of a type unification containsmore than one result or if there is data polymorphism, the set oftype assumptions is multiplied.
Erase type assumptions: If the type unification fails, the correspondingset of type assumptions is erased.
New method type parameters: At the end remained type-placeholdersare replaced by new introduced method type parameters.
Intersection types: At the end each remained set of type assumptionsforms one element of the result’s intersection type.
Martin Plumicke Java type inference with wildcards
![Page 35: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/35.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type–inference rules
(O, τ, τ ′) BExpr e1 : θ′, (O, τ, τ ′) BExpr e2 : θ[Assign]
(O, τ, τ ′) BExpr Assign( e1, e2 ) : θ′θ≤∗ θ′
(O, τ, τ ′) BExpr re : θ∀16 i 6n : (O, τ, τ ′) BExpr ei : θi ,
(θ′1 . . . θ′n, θ) = lub( θ, f , (θ1, . . . , θn) )
[Method-Call
](O, τ, τ ′) BExpr MethodCall( re, f ( e1, . . . , en ) ) : θ
(O, τ, τ ′) BExpr e : θ[Return]
(O, τ, τ ′) BStmt Return( e ) : θ
Martin Plumicke Java type inference with wildcards
![Page 36: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/36.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type–inference rules
(O, τ, τ ′) BExpr e1 : θ′, (O, τ, τ ′) BExpr e2 : θ[Assign]
(O, τ, τ ′) BExpr Assign( e1, e2 ) : θ′θ≤∗ θ′
(O, τ, τ ′) BExpr re : θ∀16 i 6n : (O, τ, τ ′) BExpr ei : θi ,
(θ′1 . . . θ′n, θ) = lub( θ, f , (θ1, . . . , θn) )
[Method-Call
](O, τ, τ ′) BExpr MethodCall( re, f ( e1, . . . , en ) ) : θ
(O, τ, τ ′) BExpr e : θ[Return]
(O, τ, τ ′) BStmt Return( e ) : θ
Martin Plumicke Java type inference with wildcards
![Page 37: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/37.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type–inference rules
(O, τ, τ ′) BExpr e1 : θ′, (O, τ, τ ′) BExpr e2 : θ[Assign]
(O, τ, τ ′) BExpr Assign( e1, e2 ) : θ′θ≤∗ θ′
(O, τ, τ ′) BExpr re : θ∀16 i 6n : (O, τ, τ ′) BExpr ei : θi ,
(θ′1 . . . θ′n, θ) = lub( θ, f , (θ1, . . . , θn) )
[Method-Call
](O, τ, τ ′) BExpr MethodCall( re, f ( e1, . . . , en ) ) : θ
(O, τ, τ ′) BExpr e : θ[Return]
(O, τ, τ ′) BStmt Return( e ) : θ
Martin Plumicke Java type inference with wildcards
![Page 38: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/38.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Example: Multiplication of matrices: Type assumptions
class Matrix extends Vector<Vector<Integer>> {{α } mul({β } m) {
{ γ } ret = new Matrix();
int i = 0;
while(i <size()) {{ ι } v1 = this.elementAt(i);
{κ } v2 = new Vector<Integer>();
int j = 0;
while(j < v1.size()) {{χ } erg = 0;
int k = 0;
while(k < v1.size()) {erg = erg + ({ ξ }({ ι } v1).elementAt(k))
* ({ψ }({φ } ({β } m).elementAt(k)).elementAt(j)); k++;}v2.addElement({χ } erg); j++; }
ret.addElement({µ } v2); i++; }return ret; }}
Martin Plumicke Java type inference with wildcards
![Page 39: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/39.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
ret = new Matrix ()
{α } mul({β } m) {{ γ } ret = { Matrix } new Matrix();. . .return { γ } ret;
}
Unification: Matrix l γ
⇒γ = Matrixγ = Vector<Vector<Integer>>γ = Vector<?Vector<Integer>>γ = Vector<?Vector<?Integer>>γ = Vector<?Vector<
?Integer>>γ = Vector<?Vector<Integer>>
Martin Plumicke Java type inference with wildcards
![Page 40: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/40.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Type assumptions after the first unification
class Matrix extends Vector<Vector<Integer>> {{α, α, α, α, α, α } mul({β, β, β, β, β, β } m) {
{ Matrix, Vector<Vector<Integer>>, Vector<?Vector<Integer>>,Vector<?Vector<?Integer>>, Vector<?Vector<
?Integer>>,Vector<?Vector<Integer>> } ret = new Matrix();
int i = 0; while(i <size()) {{ ι, ι, ι, ι, ι, ι } v1 = this.elementAt(i);
{κ, κ, κ, κ, κ, κ } v2 = new Vector<Integer>();
int j = 0; while(j < v1.size()) {{χ, χ, χ, χ, χ, χ } erg = 0;
int k = 0; while(k < v1.size()) {erg = erg +({ ξ, ξ, ξ, ξ, ξ, ξ }({ ι, ι, ι, ι, ι, ι } v1).elementAt(k))
* ({ψ,ψ, ψ, ψ, ψ, ψ }({φ, φ, φ, φ, φ, φ }({β, β, β, β, β, β } m).elementAt(k)).elementAt(j)); k++;}
v2.addElement({χ, χ, χ, χ, χ, χ } erg); j++; }ret.addElement({µ, µ, µ, µ, µ, µ } v2); i++; }
return ret; }}Martin Plumicke Java type inference with wildcards
![Page 41: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/41.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
v1 = this.elementAt(i);
{α } mul({β } m) {. . .{ ι } v1 = ({ Matrix } this).elementAt(i);. . .
}
Unification: Matrix l Vector<ι>
⇒
ι = Vector<Integer>ι = Vector<?Integer>ι = Vector<?Integer>
Martin Plumicke Java type inference with wildcards
![Page 42: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/42.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
v2 = new Vector<Integer> ();
{α } mul({β } m) {. . .{κ } v2 = { Vector<Integer> } new Vector<Integer>();. . .
}
Unification: Vector<Integer> l κ
⇒
κ = Vector<Integer>κ = Vector<?Integer>κ = Vector<?Integer>
Martin Plumicke Java type inference with wildcards
![Page 43: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/43.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
erg = erg + v1.elementAt(k)
* m.elementAt(k)).elementAt(j)); (I)
{α } mul({β } m) {. . .
{χ } erg = {χ } erg + ({ ξ }({ ι }v1).elementAt(k))* ({ψ }({φ } ({β } m).elementAt(k)).elementAt(j)); k++;}
. . .}
({β } m).elementAt(k): Unification: β l Vector<φ>β = Vector<φ>β = Matrix ⇒ φ = Vector<Integer> or
φ = ?Vector<Integer> orφ = ?Vector<?Integer> orφ = ?Vector<Integer>
Martin Plumicke Java type inference with wildcards
![Page 44: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/44.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
erg = erg + v1.elementAt(k)
* m.elementAt(k)).elementAt(j)); (II)
({φ }({β }m).elementAt(k)).elementAt(j):
Unification: φl Vector<ψ>
β = Vector<φ> ⇒ φ = Vector<ψ> orφ = ?Vector<ψ> orφ = Matrix
β = Matrix⇒ φ = Vector<Integer>⇒ OKφ = . . .
Martin Plumicke Java type inference with wildcards
![Page 45: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/45.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
erg = erg + v1.elementAt(k)
* m.elementAt(k)).elementAt(j)); (III)
({ ξ }({ ι }v1).elementAt(k))*({ψ }({φ }({β }m).elementAt(k)).elementAt(j)):β = Vector<φ> :
From * : (int, int) → int follows
Unification: ψ l Integer
φ = Vector<ψ> ⇒ ψ = Integer⇒ OK⇒ ψ = ?Integer⇒ OK
φ = ?Vector<ψ> ⇒ ψ = Integer⇒ OK⇒ ψ = ?Integer⇒ OK
φ = Matrix⇒ (ψ =)Vector<Integer> l Integer⇒ fail⇒ assumption is erased
Martin Plumicke Java type inference with wildcards
![Page 46: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/46.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
erg = erg + v1.elementAt(k)
* m.elementAt(k)).elementAt(j)); (IV)
{α } mul({β } m) {. . .
}
Result for β:
β = Vector<Vector<Integer>>β = Vector<Vector<?Integer>>β = Vector<?Vector<Integer>>β = Vector<?Vector<?Integer>>β = Matrix
Martin Plumicke Java type inference with wildcards
![Page 47: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/47.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
erg = erg + v1.elementAt(k)
* m.elementAt(k)).elementAt(j)); (V)
{χ } erg = {χ } erg + ({ ξ }({ ι }v1).elementAt(k))*({ψ }({φ }({β }m).elementAt(k)).elementAt(j)):
From +, * : (int, int) → intwith
ξ = Integer l Integerξ = ?Integer l Integer
andψ = Integer l Integerψ = ?Integer l Integer
followsχ = Integer
Martin Plumicke Java type inference with wildcards
![Page 48: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/48.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
v2.addElement(erg);
{α } mul({β } m) {. . .{κ } v2 = new Vector<Integer>();. . .v2.addElement({χ } erg);. . .
}
χ = Integer, κ = Vector<Integer>⇒ Integer l Integer⇒ OK
χ = Integer, κ = Vector<?Integer>⇒ Integer l ?Integer⇒ fail⇒ assumption is erased
χ = Integer, κ = Vector<?Integer>⇒ Integer l ?Integer⇒ OK
Martin Plumicke Java type inference with wildcards
![Page 49: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/49.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
ret.addElement(v2); (I)
{ γ } ret = new Matrix(); . . . ; ret.addElement({κ } v2);
κ = Vector<Integer>, γ = Matrix⇒ Vector<Integer> l Vector<Integer>⇒ OK
κ = Vector<Integer>, γ = Vector<Vector<Integer>>⇒ Vector<Integer> l Vector<Integer>⇒ OK
κ = Vector<Integer>, γ = Vector<?Vector<Integer>>⇒ Vector<Integer> l ?Vector<Integer>⇒ fail
κ = Vector<Integer>, γ = Vector<?Vector<?Integer>>⇒ Vector<Integer> l ?Vector<?Integer>⇒ fail
κ = Vector<Integer>, γ = Vector<?Vector<?Integer>>
⇒ Vector<Integer> l ?Vector<?Integer>⇒ fail
κ = Vector<Integer>, γ = Vector<?Vector<Integer>>⇒ Vector<Integer> l ?Vector<Integer>⇒ OK
Martin Plumicke Java type inference with wildcards
![Page 50: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/50.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
ret.addElement(v2); (II)
{ γ } ret = new Matrix(); . . . ; ret.addElement({κ } v2);
With κ = Vector<?Integer> for all assumptions of γ type unificationfails.
Result:γ = Matrixγ = Vector<Vector<Integer>>γ = Vector<?Vector<Integer>>
Martin Plumicke Java type inference with wildcards
![Page 51: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/51.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
return ret;
{α } mul({β } m) {. . .return { γ } ret;
}Unification: γ l α forγ = Matrixγ = Vector<Vector<Integer>>γ = Vector<?Vector<Integer>>
Result: α = Matrixα = Vector<Vector<Integer>>α = Vector<?Vector<Integer>>α = Vector<?Vector<Integer>>α = Vector<?Vector<?Integer>>α = Vector<?Vector<
?Integer>>Martin Plumicke Java type inference with wildcards
![Page 52: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/52.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Result:
mul: &α,β(α→β),
where
α≤∗ Vector<?Vector<?Integer>>,Matrix≤∗ β
Martin Plumicke Java type inference with wildcards
![Page 53: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/53.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Principal type
Definition [Damas, Milner 1982]:
“A type-scheme for a declaration is a principal type-scheme, if any othertype-scheme for the declaration is a generic instance of it.”
Generalization to the Java 5.0 type system
“An intersection type-scheme for a declaration is a principal type-scheme,if any other type-scheme for the declaration is a generic instance of oneelement of the intersection type-scheme.”
Martin Plumicke Java type inference with wildcards
![Page 54: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/54.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Principal type
Definition [Damas, Milner 1982]:
“A type-scheme for a declaration is a principal type-scheme, if any othertype-scheme for the declaration is a generic instance of it.”
Generalization to the Java 5.0 type system
“An intersection type-scheme for a declaration is a principal type-scheme,if any other type-scheme for the declaration is a generic instance of oneelement of the intersection type-scheme.”
Martin Plumicke Java type inference with wildcards
![Page 55: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/55.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Principal type
DefinitionAn intersection type of a method m in a class C
m : (θ1,1 × . . .× θ1,n → θ1)& . . .&(θm,1 × . . .× θm,n,→ θm)
is called principal if for any correct type annotated method declaration
rty m(ty1 a1 , . . . , tyn an) { . . . }
there is an element (θi ,1 × . . .× θi ,n,→ θi ) of the intersection type andthere is a substitution σ, such that
σ( θi )≤∗ rty , ty1 ≤∗ σ( θi ,1 ), . . . , tyn ≤∗ σ( θi ,n )Martin Plumicke Java type inference with wildcards
![Page 56: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/56.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Reduced principal type:
The inferred type of the example
mul: &α,β(α→β),
where α≤∗ Vector<?Vector<?Integer>> and Matrix≤∗ β.
is a principle type.
But there is also a reduced principle type:
mul: Vector<?Vector<?Integer>>→ Matrix
Martin Plumicke Java type inference with wildcards
![Page 57: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/57.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Reduced principal type:
The inferred type of the example
mul: &α,β(α→β),
where α≤∗ Vector<?Vector<?Integer>> and Matrix≤∗ β.
is a principle type.
But there is also a reduced principle type:
mul: Vector<?Vector<?Integer>>→ Matrix
Martin Plumicke Java type inference with wildcards
![Page 58: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/58.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Principle type property
TheoremThe type inference algorithm calculates a principle type.
Martin Plumicke Java type inference with wildcards
![Page 59: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/59.jpg)
IntroductionThe Algorithm
Conclusion
Type unificationType-inference-algorithmTool demonstrationPrinciple type
Tool demonstration
I Matrix–Example
I Overloading–Example
Martin Plumicke Java type inference with wildcards
![Page 60: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/60.jpg)
IntroductionThe Algorithm
Conclusion
Conclusion and future work
ConclusionI Type-inference-algorithm for Java 5.0
I Type unification
I Principle type property
Future workI Reduce the number of calculated typings.
I Handling of interesection types (adaption of byte-code generation)
Martin Plumicke Java type inference with wildcards
![Page 61: Java type inference with wildcards - Leiden Universityliacs.leidenuniv.nl/~bonsanguemm/fmco/2006/Pluemicke.pdf · 2006-11-05 · I parametrized types, type variables, type terms,](https://reader035.fdocuments.net/reader035/viewer/2022070719/5edea178ad6a402d6669f58b/html5/thumbnails/61.jpg)
IntroductionThe Algorithm
Conclusion
Conclusion and future work
ConclusionI Type-inference-algorithm for Java 5.0
I Type unification
I Principle type property
Future workI Reduce the number of calculated typings.
I Handling of interesection types (adaption of byte-code generation)
Martin Plumicke Java type inference with wildcards