Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
More type inference in Java 8 Martin Pl¨ umicke Baden-Wuerttemberg Cooperative State University Stuttgart/Horb
24. June 2014
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Overview
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Type inference in Java 8 Type inference for lambda expressions: (ty1 a1 , ..., ty1 a1 ) -> expr Type inference for generic parameters (Diamond operator): Vector v = new Vector (); Type instances: T id(T x) { return x; } Integer i = id(1); T 7→ Integer Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
No type inference for lambda expressions
interface Fun1 { R apply(T arg); } interface Fun2 { R apply(T1 arg1, T2 arg2); } class Matrix extends Vector { Fun1>, Matrix> op = (m) -> (f) -> f.apply(this, m); }
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Our goal
class Matrix extends Vector { op = (m) -> (f) -> f.apply(this, m); }
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
The approach 1. Collecting all correct deducible functional interfaces as type for a lambda expression in one equivalence class. 2. Canonical functional interface FunN as a representative of each equivalence class interface FunN {
R apply(T1 arg1, ..., TN argN ); } (cp. (T1 , . . . , TN ) → R) 3. Type inference algorithm that determines the respective canonical interfaces. Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
The type inference algorithm TI: TypeAssumptions × Class→ { (Constraints, TClass) } TI( Ass, Class( τ, extends( τ 0 ), fdecls ) ) = let (Class( τ, extends( τ 0 ), fdeclst ), ConS) = TYPE( Ass, Class( τ, extends( τ 0 ), fdecls ) ) { (cs1 , σ1 ), . . . , (csn , σn ) } = SOLVE( ConS ) in { (csi , σi ( Class( τ, extends( τ 0 ), fdeclst ) ))| 1 6 i 6 n }
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
The type inference algorithm TI: TypeAssumptions × Class→ { (Constraints, TClass) } TI( Ass, Class( τ, extends( τ 0 ), fdecls ) ) = let (Class( τ, extends( τ 0 ), fdeclst ), ConS) = TYPE( Ass, Class( τ, extends( τ 0 ), fdecls ) ) { (cs1 , σ1 ), . . . , (csn , σn ) } = SOLVE( ConS ) in { (csi , σi ( Class( τ, extends( τ 0 ), fdeclst ) ))| 1 6 i 6 n } TYPE: I
I Inserts type annotations, widely type variables as placeholders (cp. [Fuh/Mishra 88: Type Inference with Subtypes]) Determines the set of type constraints
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
The type inference algorithm TI: TypeAssumptions × Class→ { (Constraints, TClass) } TI( Ass, Class( τ, extends( τ 0 ), fdecls ) ) = let (Class( τ, extends( τ 0 ), fdeclst ), ConS) = TYPE( Ass, Class( τ, extends( τ 0 ), fdecls ) ) { (cs1 , σ1 ), . . . , (csn , σn ) } = SOLVE( ConS ) in { (csi , σi ( Class( τ, extends( τ 0 ), fdeclst ) ))| 1 6 i 6 n } TYPE:
I Inserts type annotations, widely type variables as placeholders (cp. [Fuh/Mishra 88: Type Inference with Subtypes]) I Determines the set of type constraints SOLVE: Solves the constraints set by type unification [Pl¨ umicke 07] Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Example: op = (m) -> (f) -> f.apply(this, m); TYPE: Annotated expression: Lam( m : am , Lam( f : af , MCall( LoVar( f ) : af , apply, ( this : Matrix, LoVar( m ) : am ) ) : a3 ) : Fun1 ) : Fun1
Set of constraints: . {(Fun1 l aop ), (Fun1 l aλf ), (af = Fun2), (Matrix l a1 ), (am l a2 ), (a3 l aapp ) }.
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Example: op = (m) -> (f) -> f.apply(this, m);
SOLVE: { ({ am l a2 , a3 l aapp }, . { aop = Fun1, . . aλf = Fun1, af = Fun2, . a1 = X }) } | Matrix is a subtype of X }
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Example: op = (m) -> (f) -> f.apply(this, m); TI: { class Matrix extends Vector { 1 Fun1 op = (m) -> (f) -> f.apply(this, m); } | Matrix is a subtype of X }
1
The constraints are here given as bounded type variables for fields, which is in original Java only allowed for methods. Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Eclipse plugin: User type selection
Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Summary and Future work Summary: I Introduction of functional interfaces FunN as representatives of the types of lambda expressions. I Type inference for lambda expressions and recursive functions I Results are sets of typed programs
2
[Pluemicke 08: Intersection Types in Java] Martin Pl¨ umicke
More type inference in Java 8
Introduction The type inference algorithm Example Ongoing work: Eclipse plugin Summary and Future work
Summary and Future work Summary: I Introduction of functional interfaces FunN as representatives of the types of lambda expressions. I Type inference for lambda expressions and recursive functions I Results are sets of typed programs Future work: Introduction of Intersection types I Intersection type buliding from set of results (cp. [Pluemicke 08]2 ) I Byte-code generation for functions with intersection types =⇒ User type selection is not any longer necessary 2
[Pluemicke 08: Intersection Types in Java] Martin Pl¨ umicke
More type inference in Java 8