You are on page 1of 34

Type inference

Main seminar, summer term 2011

Sebastian Wiesner

Technische Universität München, Department of Computer Science

June 22nd, 1011

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 1 / 21


Inferring types Implicitly typed local variables in C#

Agenda

1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference

2 Discussion of type inference


Advantages
Disadvantages

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 2 / 21


Inferring types Implicitly typed local variables in C#

Implicitly typed local variables in C#

Inference of variable types from assignment expression

Example

var i = 5;
var j = i + 5;

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 3 / 21


Inferring types Implicitly typed local variables in C#

Implicitly typed local variables in C#

Inference of variable types from assignment expression

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)

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 3 / 21


Inferring types Implicitly typed local variables in C#

Formalization of inference

Γ ` t: τ (C-VarDecl)
Γ, x : τ ` x ← t : τ

Inference rule with:


type context Γ Identifier 7→ type of identifier
term t Representation of expression to type
type variable τ Placeholder for concrete type

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 4 / 21


Inferring types Hindley-Milner type inference

Agenda

1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference

2 Discussion of type inference


Advantages
Disadvantages

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 5 / 21


Inferring types Hindley-Milner type inference

Hindley-Milner type inference

J. Roger Hindley Robin Milner

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 6 / 21


Inferring types Hindley-Milner type inference

Hindley-Milner inference

Type system for simply-typed lambda calculus


Base of type inference in functional languages (e.g. Haskell, ML
family)

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 7 / 21


Inferring types Hindley-Milner type inference

Hindley-Milner inference

Type system for simply-typed lambda calculus


Base of type inference in functional languages (e.g. Haskell, ML
family)

Algorithm
1 Constraint generation ⇒ Set of constraints for types
2 Unification and generalization ⇒ Solve constraints to obtain
principal type

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 7 / 21


Inferring types Hindley-Milner type inference

Basic terms

exp ::= x (<Var>)


| λx.exp (<Abs>)
| exp1 exp2 (<App>)
| let x = exp1 in exp2 (<Let>)
| if exp1 then exp2 else exp3 (<If>)

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 8 / 21


Inferring types Hindley-Milner type inference

Typing abstractions

Γ, x : τ1 ` t2 : τ2 |χ C
(C-Abs)
Γ ` λx.t2 : τ1 → τ2 |χ C

constraint set C Set of equations constraining the inferred types


type variable set χ Set of used (captured) type variables

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 9 / 21


Inferring types Hindley-Milner type inference

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

χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅ Distinct type variables in


subterms t1 and t2

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 10 / 21


Inferring types Hindley-Milner type inference

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

χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅ Distinct type variables in


subterms t1 and t2
α∈
/ χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2 Fresh type variable α

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 10 / 21


Inferring types Hindley-Milner type inference

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

χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅ Distinct type variables in


subterms t1 and t2
α∈
/ χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2 Fresh type variable α
C0 = C1 ∪ C2 ∪ {τ1 = τ2 → α} τ1 constrained to function type τ2 → α

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 10 / 21


Inferring types Hindley-Milner type inference

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

χ1 ∩ χ2 = χ1 ∩ FV (τ2 ) = χ2 ∩ FV (τ1 ) = ∅ Distinct type variables in


subterms t1 and t2
α∈
/ χ1 , χ2 , τ1 , τ2 , C1 , C2 , Γ, t1 , t2 Fresh type variable α
C0 = C1 ∪ C2 ∪ {τ1 = τ2 → α} τ1 constrained to function type τ2 → α
χ1 ∪ χ2 ∪ {α} α captured

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 10 / 21


Inferring types Hindley-Milner type inference

Typing local definitions

Γ ` 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

τg = generalize(Γ, τ1 , C1 ) Generalization of type of bound identifier

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 11 / 21


Inferring types Hindley-Milner type inference

Unification and Generalization

Generalization generalize(Γ, τ, C)

1 Unify constraint set C to a principal unifier σ :


I Type substitution
I Most general solution to the constraint equations

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 12 / 21


Inferring types Hindley-Milner type inference

Unification and Generalization

Generalization generalize(Γ, τ, C)

1 Unify constraint set C to a principal unifier σ :


I Type substitution
I Most general solution to the constraint equations
2 Principal type τp = στ

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 12 / 21


Inferring types Hindley-Milner type inference

Unification and Generalization

Generalization generalize(Γ, τ, C)

1 Unify constraint set C to a principal unifier σ :


I Type substitution
I Most general solution to the constraint equations
2 Principal type τp = στ
3 Generalized principal type τg = ∀α1 , . . . , αn : τp (type scheme)

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 12 / 21


Inferring types Hindley-Milner type inference

Unification and Generalization

Generalization generalize(Γ, τ, C)

1 Unify constraint set C to a principal unifier σ :


I Type substitution
I Most general solution to the constraint equations
2 Principal type τp = στ
3 Generalized principal type τg = ∀α1 , . . . , αn : τp (type scheme)

Instantiation of type scheme with fresh type variables on each


occurrence

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 12 / 21


Inferring types Hindley-Milner type inference

Let-Polymorphism

let id = \ x > x in ( id 10, id True)

Generalized principal type of id: ∀α : α → α

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 13 / 21


Inferring types Hindley-Milner type inference

Let-Polymorphism

let id = \ x > x in ( id 10, id True)

Generalized principal type of id: ∀α : α → α


Instantiation to:
1 Integer → Integer
2 Bool → Bool

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 13 / 21


Inferring types Hindley-Milner type inference

Let-Polymorphism

let id = \ x > x in ( id 10, id True)

Generalized principal type of id: ∀α : α → α


Instantiation to:
1 Integer → Integer
2 Bool → Bool

⇒ Parametric polymorphism

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 13 / 21


Inferring types Hindley-Milner type inference

Inference with other kinds of polymorphism

Ad-hoc polymorphism
Types grouped into sets according to supported interface
Type classes: sort : Ord α ⇒ [α] → [α]

⇒ Established, wide-spread usage

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 14 / 21


Inferring types Hindley-Milner type inference

Inference with other kinds of polymorphism

Ad-hoc polymorphism
Types grouped into sets according to supported interface
Type classes: sort : Ord α ⇒ [α] → [α]

⇒ Established, wide-spread usage

Subtype polymorphism
Ordering over types according to subtype relationships ⇒
Inequalities as constraints
Cubic complexity of unification
Principal unifier not guaranteed to exist

⇒ No language with subtyping and complete inference

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 14 / 21


Inferring types Local type inference

Agenda

1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference

2 Discussion of type inference


Advantages
Disadvantages

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 15 / 21


Inferring types Local type inference

Local type inference

Inference only in critical cases:


Type arguments of polymorphic functions
Parameter types of anonymous functions
Local bindings

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 16 / 21


Inferring types Local type inference

Local type inference

Inference only in critical cases:


Type arguments of polymorphic functions
Parameter types of anonymous functions
Local bindings

Algorithm
Standard inference of type arguments (with respect to subtype
constraints)
Propagation of inferred types down the syntax tree (colored local
type inference)

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 16 / 21


Discussion of type inference Advantages

Agenda

1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference

2 Discussion of type inference


Advantages
Disadvantages

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 17 / 21


Discussion of type inference Advantages

Advantages

Avoidance of needless and superfluous annotations ⇒ focus on


algorithm instead of pleasing the type-checker
Sort of program analysis, drives compiler optimizations
Higher “quality” of type system, especially in case of complete
inference

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 18 / 21


Discussion of type inference Disadvantages

Agenda

1 Inferring types
Implicitly typed local variables in C#
Hindley-Milner type inference
Local type inference

2 Discussion of type inference


Advantages
Disadvantages

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 19 / 21


Discussion of type inference Disadvantages

Error messages

Type errors caught by unification failure


Error message probably unrelated to actual type error

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 20 / 21


Discussion of type inference Disadvantages

Error messages

Type errors caught by unification failure


Error message probably unrelated to actual type error

Example
Erroneous function to sum elements of a list:

sum : : [ Integer ] > Integer


sum xs = foldr (+) xs

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

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 20 / 21


Discussion of type inference Disadvantages

Questions?

Sebastian Wiesner (TUM CS) Type inference June 22nd, 1011 21 / 21

You might also like