Java type inference with wildcards - Leiden...

Preview:

Citation preview

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

IntroductionThe Algorithm

Conclusion

Overview

IntroductionProblemRelated work

The AlgorithmType unificationType-inference-algorithmTool demonstrationPrinciple type

Conclusion

Martin Plumicke Java type inference with wildcards

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

IntroductionThe Algorithm

Conclusion

Type unificationType-inference-algorithmTool demonstrationPrinciple type

Result:

mul: &α,β(α→β),

where

α≤∗ Vector<?Vector<?Integer>>,Matrix≤∗ β

Martin Plumicke Java type inference with wildcards

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

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

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

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

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

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

IntroductionThe Algorithm

Conclusion

Type unificationType-inference-algorithmTool demonstrationPrinciple type

Tool demonstration

I Matrix–Example

I Overloading–Example

Martin Plumicke Java type inference with wildcards

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

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

Recommended