Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook
Intersection Types in Java
Martin Plumicke¨
University of Cooperative Education Stuttgart/Horb
September 11, 2008
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Overview
Motivation/Tooldemonstration
Method calls with intersection types Semantics First approach The algorithm
Principal Typing
Conclusion and Outlook
Martin Plumicke¨ Intersection Types in Java Purpuse: Byte-code generation for methods with intersection types
Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook
Purpuse: Byte-code generation for methods with intersection types
Martin Plumicke¨ Intersection Types in Java ... m ( ... ) { ... receiver.method(t1, ..., tn); ... }
0 0 0 I method: ty1 × ... × tyn → ty0 & ... & ty1 × ... × tyn → ty0 has an intersection type
I t1 , ... , tn have unambiguous types during execution
I by the argument types and the result type the typing of method is determined
I method is executed with the determined typing.
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Semantics of type–inferred Java programs
I control-structures have the same semantics as in standard Java
I method calls differ, as there are intersection types
Martin Plumicke¨ Intersection Types in Java I t1 , ... , tn have unambiguous types during execution
I by the argument types and the result type the typing of method is determined
I method is executed with the determined typing.
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Semantics of type–inferred Java programs
I control-structures have the same semantics as in standard Java
I method calls differ, as there are intersection types
... m ( ... ) { ... receiver.method(t1, ..., tn); ... }
0 0 0 I method: ty1 × ... × tyn → ty0 & ... & ty1 × ... × tyn → ty0 has an intersection type
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Semantics of type–inferred Java programs
I control-structures have the same semantics as in standard Java
I method calls differ, as there are intersection types
... m ( ... ) { ... receiver.method(t1, ..., tn); ... }
0 0 0 I method: ty1 × ... × tyn → ty0 & ... & ty1 × ... × tyn → ty0 has an intersection type
I t1 , ... , tn have unambiguous types during execution
I by the argument types and the result type the typing of method is determined
I method is executed with the determined typing.
Martin Plumicke¨ Intersection Types in Java main:Integer → Integer is determined
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Semantics example
class OL { Integer m(x) { return x + x; } //Integer → Integer Boolean m(x) { return x || x; } //Boolean → Boolean }
class Main { main(x) { // Integer → Integer & Boolean → Boolean ol; ol = new OL(); return ol.m(x); } } ... Main rec = new Main(); Integer r = rec.main(x);
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Semantics example
class OL { Integer m(x) { return x + x; } //Integer → Integer Boolean m(x) { return x || x; } //Boolean → Boolean }
class Main { main(x) { // Integer → Integer & Boolean → Boolean ol; ol = new OL(); return ol.m(x); } } ... Main rec = new Main(); Integer r = rec.main(x); main:Integer → Integer is determined
Martin Plumicke¨ Intersection Types in Java Result for Main: class Main { Integer main(Integer x) { OL ol; ol = new OL(); return ol.m(x); }
Boolean main(Boolean x) { OL ol; ol = new OL(); return ol.m(x); }}
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Code generation for method with intersection types
I Byte-code allows no intersection types I First approach: generate for each element of the intersection type an own method
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Code generation for method with intersection types
I Byte-code allows no intersection types I First approach: generate for each element of the intersection type an own method Result for Main: class Main { Integer main(Integer x) { OL ol; ol = new OL(); return ol.m(x); }
Boolean main(Boolean x) { OL ol; ol = new OL(); return ol.m(x); }} Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example: Multiplication of matrices I
class Matrix extends Vector
Martin Plumicke¨ Intersection Types in Java class Matrix extends Vector
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example: Multiplication of matrices II
mul: &β,α(β→α), where β ≤∗ Vector>, Matrix ≤∗ α
Martin Plumicke¨ Intersection Types in Java Not a correct Java program
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example: Multiplication of matrices II
mul: &β,α(β→α), where β ≤∗ Vector>, Matrix ≤∗ α
class Matrix extends Vector
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example: Multiplication of matrices II
mul: &β,α(β→α), where β ≤∗ Vector>, Matrix ≤∗ α
class Matrix extends Vector
Martin Plumicke¨ Intersection Types in Java Code–execution: Callgraph of the method declarations
CG( cl.m : τ ) Callgraph of the method m in the class cl with the typing τ. Supertype of function types: Subtyping ordering ∗ 0 ∗ 0 θi ≤ θi , θ ≤ θ ⇒ 0 ∗ 0 0 θ1 × ... × θn → θ ≤ θ1 × ... × θn → θ
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Group elements of the intersection type
Idea: 1. Group all elements which I executes the same code I have a common supertype 2. Generate new methods only for the groups
Martin Plumicke¨ Intersection Types in Java Supertype of function types: Subtyping ordering ∗ 0 ∗ 0 θi ≤ θi , θ ≤ θ ⇒ 0 ∗ 0 0 θ1 × ... × θn → θ ≤ θ1 × ... × θn → θ
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Group elements of the intersection type
Idea: 1. Group all elements which I executes the same code I have a common supertype 2. Generate new methods only for the groups
Code–execution: Callgraph of the method declarations
CG( cl.m : τ ) Callgraph of the method m in the class cl with the typing τ.
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Group elements of the intersection type
Idea: 1. Group all elements which I executes the same code I have a common supertype 2. Generate new methods only for the groups
Code–execution: Callgraph of the method declarations
CG( cl.m : τ ) Callgraph of the method m in the class cl with the typing τ. Supertype of function types: Subtyping ordering ∗ 0 ∗ 0 θi ≤ θi , θ ≤ θ ⇒ 0 ∗ 0 0 θ1 × ... × θn → θ ≤ θ1 × ... × θn → θ
Martin Plumicke¨ Intersection Types in Java Supertype Integer → Integer Boolean → Boolean
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example class OL I
Callgraph CG( Main.main : Integer → Integer ) CG( Main.main : Boolean → Boolean ) = = Main.main: Integer−>Integer Main.main: Integer−>Integer & Boolean −> Boolean & Boolean −> Boolean
OL.m: Integer −> Integer Ol.m: Boolean −> Boolean
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example class OL I
Callgraph CG( Main.main : Integer → Integer ) CG( Main.main : Boolean → Boolean ) = = Main.main: Integer−>Integer Main.main: Integer−>Integer & Boolean −> Boolean & Boolean −> Boolean
OL.m: Integer −> Integer Ol.m: Boolean −> Boolean
Supertype Integer → Integer Boolean → Boolean
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example class OL II
Code generation
class Main { Integer main(Integer x) { OL ol; ol = new OL(); return ol.m(x); }
Boolean main(Boolean x) { OL ol; ol = new OL(); return ol.m(x); }}
Code is unchanged in comparison to the first approach
Martin Plumicke¨ Intersection Types in Java Supertype:
Vector> → Matrix
Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example class Matrix I
Callgraph CG( Matrix.mul : τ ) for all τ
Matrix.mul: Vector< Vector>? −> Matrix & Vector< Vector
Matrix −> Vector< Vector>
Vector
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example class Matrix I
Callgraph CG( Matrix.mul : τ ) for all τ
Matrix.mul: Vector< Vector>? −> Matrix & Vector< Vector
Matrix −> Vector< Vector>
Vector
Supertype:
Vector> → Matrix
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Semantics Method calls with intersection types First approach Principal Typing The algorithm Conclusion and Outlook Example class Matrix II
Code generation (only one method!) Matrix mul(Vector> m) { Matrix ret = new Matrix(); int i = 0; while(i
Input: A Java program p with inferred (intersection) types. Output: A Java program p0, where the methods have standard Java types. The semantics of p and p0 are equal. 1. Step: For every class cl in p consider for each method m the intersection type tym:
I Build the callgraph CG( cl.m : τ ) for each function type τ of the intersection type tym. I Group all elements τ of tym, where CG( cl.m : τ ) is the same graph and there is a supertype. 2. Step: Determine the supertype of the respective group. 3. Step: Generate for each group of function types the corresponding Java code with the supertype as standard typing in p0.
Martin Plumicke¨ Intersection Types in Java Generalization to the Java type system [Plumicke¨ 2007, PPPJ07]
“An intersection type-scheme for a declaration is a principal type-scheme, if any (non–intersection) type-scheme for the declaration is a subtype of a generic instance of one element of the intersection type-scheme.”
Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Principal Typing
Definition [Damas, Milner 1982]:
“A type-scheme for a declaration is a principal type-scheme, if any other type-scheme for the declaration is a generic instance of it.”
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Principal Typing
Definition [Damas, Milner 1982]:
“A type-scheme for a declaration is a principal type-scheme, if any other type-scheme for the declaration is a generic instance of it.”
Generalization to the Java type system [Plumicke¨ 2007, PPPJ07]
“An intersection type-scheme for a declaration is a principal type-scheme, if any (non–intersection) type-scheme for the declaration is a subtype of a generic instance of one element of the intersection type-scheme.”
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Example principal typing I
import java.util.Vector; import java.util.Stack;
class Put {
main(ele, x) { putElement(ele, x); } } Martin Plumicke¨ Intersection Types in Java But there is another principal type:
main : T × Vector
as Stack
Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Example principal typing II
The inferred intersection type:
main : T × Vector
is a principal type.
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Example principal typing II
The inferred intersection type:
main : T × Vector
is a principal type.
But there is another principal type:
main : T × Vector
as Stack
Martin Plumicke¨ Intersection Types in Java The principal type main : T × Vector
Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Example principal typing III
class Put {
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Example principal typing III
class Put {
The principal type main : T × Vector
Martin Plumicke¨ Intersection Types in Java This refined definition guarantees, that for each method, which is generated by the resolving algorithm, at least one typing is contained in the principal type.
Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Refined definition of Principal typing
“An intersection type-scheme for a declaration is a principal type-scheme, if any (non–intersection) type-scheme θ for the declaration is a subtype of a generic instance of one element of the intersection type-scheme τ and θ and τ have the same callgraph.”
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Refined definition of Principal typing
“An intersection type-scheme for a declaration is a principal type-scheme, if any (non–intersection) type-scheme θ for the declaration is a subtype of a generic instance of one element of the intersection type-scheme τ and θ and τ have the same callgraph.”
This refined definition guarantees, that for each method, which is generated by the resolving algorithm, at least one typing is contained in the principal type.
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Example Put (cont.)
CG( Put.main : T × Vector
Put.putElement: T x Vector
main : T × Vector
Martin Plumicke¨ Intersection Types in Java Outlook At the moment: Type inference algorithm infers typings, which are later erased as subtypes by the resolving algorithm. Purpuse: Type inference algorithm infers only supertypes, such that no typings are erased in the resolving algorithm.
Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Conclusion and Outlook
Conclusion
I Semantics for Java methods with intersection types
I Resolving algorithm of intersection types
I Code generation for methods with intersection types possible ⇒ type selection is not longer necessary in the Eclipse plugin.
I (Redefined) Principal type property
Martin Plumicke¨ Intersection Types in Java Motivation/Tooldemonstration Method calls with intersection types Principal Typing Conclusion and Outlook Conclusion and Outlook
Conclusion
I Semantics for Java methods with intersection types
I Resolving algorithm of intersection types
I Code generation for methods with intersection types possible ⇒ type selection is not longer necessary in the Eclipse plugin.
I (Redefined) Principal type property
Outlook At the moment: Type inference algorithm infers typings, which are later erased as subtypes by the resolving algorithm. Purpuse: Type inference algorithm infers only supertypes, such that no typings are erased in the resolving algorithm.
Martin Plumicke¨ Intersection Types in Java