Professional Documents
Culture Documents
Type Inference: Main Seminar, Summer Term 2011
Type Inference: Main Seminar, Summer Term 2011
Sebastian Wiesner
Agenda
1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference
Example
var i = 5;
var j = i + 5;
Example
var i = 5;
var j = i + 5;
Rules
Declaration with assignment
Single expression on right hand side
Typable expression (no “typeless” values like null)
Formalization of inference
Γ ` t: τ (C-VarDecl)
Γ, x : τ ` x ← t : τ
Agenda
1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference
Hindley-Milner inference
Hindley-Milner inference
Algorithm
1 Constraint generation ⇒ Set of constraints for types
2 Unification and generalization ⇒ Solve constraints to obtain
principal type
Basic terms
Typing abstractions
Γ, x : τ1 ` t2 : τ2 |χ C
(C-Abs)
Γ ` λx.t2 : τ1 → τ2 |χ C
Typing applications
Γ ` t1 : τ1 |χ1 C1 Γ ` t2 : τ2 |χ2 C2
χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅
α∈ / χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2
C0 = C1 ∪ C2 ∪ {τ1 = τ2 → α}
(C-App)
Γ ` t1 t2 : α |χ1 ∪χ2 ∪{α} C0
Typing applications
Γ ` t1 : τ1 |χ1 C1 Γ ` t2 : τ2 |χ2 C2
χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅
α∈ / χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2
C0 = C1 ∪ C2 ∪ {τ1 = τ2 → α}
(C-App)
Γ ` t1 t2 : α |χ1 ∪χ2 ∪{α} C0
Typing applications
Γ ` t1 : τ1 |χ1 C1 Γ ` t2 : τ2 |χ2 C2
χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅
α∈ / χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2
C0 = C1 ∪ C2 ∪ {τ1 = τ2 → α}
(C-App)
Γ ` t1 t2 : α |χ1 ∪χ2 ∪{α} C0
Typing applications
Γ ` t1 : τ1 |χ1 C1 Γ ` t2 : τ2 |χ2 C2
χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅
α∈ / χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2
C0 = C1 ∪ C2 ∪ {τ1 = τ2 → α}
(C-App)
Γ ` t1 t2 : α |χ1 ∪χ2 ∪{α} C0
Γ ` t1 : τ1 |χ1 C1 Γ, x : τg ` t2 : τ2 |χ2 C2
τg = generalize(Γ, τ1 , C1 )
χ1 ∩ χ2 = ∅ C‘ = C1 ∪ C2
(C-Let)
Γ ` let x = t1 in t2 |χ C0
Generalization generalize(Γ, τ, C)
Generalization generalize(Γ, τ, C)
Generalization generalize(Γ, τ, C)
Generalization generalize(Γ, τ, C)
Let-Polymorphism
Let-Polymorphism
Let-Polymorphism
⇒ Parametric polymorphism
Ad-hoc polymorphism
Types grouped into sets according to supported interface
Type classes: sort : Ord α ⇒ [α] → [α]
Ad-hoc polymorphism
Types grouped into sets according to supported interface
Type classes: sort : Ord α ⇒ [α] → [α]
Subtype polymorphism
Ordering over types according to subtype relationships ⇒
Inequalities as constraints
Cubic complexity of unification
Principal unifier not guaranteed to exist
Agenda
1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference
Algorithm
Standard inference of type arguments (with respect to subtype
constraints)
Propagation of inferred types down the syntax tree (colored local
type inference)
Agenda
1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference
Advantages
Agenda
1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference
Error messages
Error messages
Example
Erroneous function to sum elements of a list:
Error message:
Couldn’t match expected type ‘Integer’
with actual type ‘[a0] -> b0’
In the return type of a call of ‘foldr’
In the expression: foldr (+) xs
Questions?