COMP302: Programming Languages and Paradigms: Jacob Errington Prof. Brigitte Pientka
COMP302: Programming Languages and Paradigms: Jacob Errington Prof. Brigitte Pientka
Γ ` e1 : int Γ ` e2 : int
t-num t-plus
Γ ` n : int Γ ` e1 + e2 : int
Γ ` e : bool Γ ` e1 : T Γ ` e2 : T Γ(x) = T
t-if t-var
Γ ` if e then e1 else e2 : T Γ`x :T
Γ ` e1 : T1 Γ, x:T1 ` e2 : T
t-let x must be new
Γ ` let x = e1 in e2 end : T
COMP302: Programming Languages and Paradigms 4 / 20
Generalizing to functions and function application
Γ, x:T1 ` e : T2 Γ ` e1 : T2 → T Γ ` e2 : T2
t-fn t-app
Γ ` fn x => e : T1 → T2 Γ ` e1 e2 : T
Γ, x:T1 ` e : T2 Γ ` e1 : T2 → T Γ ` e2 : T2
t-fn t-app
Γ ` fn x => e : T1 → T2 Γ ` e1 e2 : T
Γ, x:T1 ` e : T2 Γ ` e1 : T2 → T Γ ` e2 : T2
t-fn t-app
Γ ` fn x => e : T1 → T2 Γ ` e1 e2 : T
Examples for A.
Examples for A.
fn x => x has type α→α
Examples for A.
fn x => x has type α→α
fn f => fn x => f (f (x)) has type (α → α) → α → α
Examples for A.
fn x => x has type α→α
fn f => fn x => f (f (x)) has type (α → α) → α → α
fn f => f x has type (α → β) → β
Examples for A.
` fn x => x has type α→α
fn f => fn x => f (f (x)) has type (α → α) → α → α
fn f => f x has type (α → β) → β
Examples for A.
` fn x => x has type α→α
` fn f => fn x => f (f (x)) has type (α → α) → α → α
fn f => f x has type (α → β) → β
Examples for A.
` fn x => x has type α→α
` fn f => fn x => f (f (x)) has type (α → α) → α → α
x : α ` fn f => f x has type (α → β) → β
Examples for B.
Examples for B.
Examples for B.
Examples for B.
Examples for B.
Examples for B.
Examples for B.
x: α , f : α → β ` f : α → β x: α , f : α → β ` x : α
x: α , f : α → β ` f x : β
x: α ` fn f => f x : ( α → β) → β
` fn x => fn f => f x : α → ( α → β) → β
Therefore ....
fn x => fn y => if x then y else 2 + 2 has type bool → int → int
COMP302: Programming Languages and Paradigms 13 / 20
Type Inference (more formally)
For example:
Γ ` e ⇒ T /C Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-if
Γ ` if e then e1 else e2 ⇒ T1 /C ∪ C1 ∪ C2 ∪ {T = bool, T1 = T2 }
Γ ` e ⇒ T /C Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-if
Γ ` if e then e1 else e2 ⇒ T1 /C ∪ C1 ∪ C2 ∪ {T = bool, T1 = T2 }
Read t-if rule as :
Given the typing context Γ and expression if e then e1 else e2 , infer
a type T1 and a set of constraints C ∪ C1 ∪ C2 ∪ {T = bool, T1 = T2 }
as follows
• given the typing context Γ and expression e,
infer the type T together with the constraint C
• given the typing context Γ and expression e1 ,
infer the type T1 together with the constraint C1
• given the typing context Γ and expression e2 ,
infer the type T2 together with the constraint C2
Inputs are green – outputs are blue
COMP302: Programming Languages and Paradigms 15 / 20
Inferring Types ...
Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C
Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C
Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}
Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C
Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}
Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C
Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}
Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C
Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}
Γ, x:α ` e ⇒ T /C
t-fn where α is new
Γ ` fn x => e ⇒ α → T /C
Γ ` e1 ⇒ T1 /C1 Γ ` e2 ⇒ T2 /C2
t-app where α is new
Γ ` e1 e2 ⇒ α/C1 ∪ C2 ∪ {T1 = (T2 → α)}
Given as set of constraints C try to simplify the set until we derive the
empty set.
Given as set of constraints C try to simplify the set until we derive the
empty set.
{C , int = int} =⇒ {C }
Given as set of constraints C try to simplify the set until we derive the
empty set.
{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
Given as set of constraints C try to simplify the set until we derive the
empty set.
{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
{C , α = α} =⇒ {C }
Given as set of constraints C try to simplify the set until we derive the
empty set.
{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
{C , α = α} =⇒ {C }
{C , (T1 → T2 ) = (S1 → S2 )} =⇒ {C , T1 = S1 , T2 = S2 }
Given as set of constraints C try to simplify the set until we derive the
empty set.
{C , int = int} =⇒ {C }
{C , bool = bool} =⇒ {C }
{C , α = α} =⇒ {C }
{C , (T1 → T2 ) = (S1 → S2 )} =⇒ {C , T1 = S1 , T2 = S2 }
{C , α = T} =⇒ {[T /α]C } provided that α 6∈ FV(T )
{C , T = α} =⇒ {[T /α]C } provided that α ∈6 FV(T )
Example : α = β → γ, β = α =⇒ ?
1 let double f x = f ( f x )
Γ ` e1 : T1 Γ, x:T1 ` e2 : T
t-let x must be new
Γ ` let x = e1 in e2 end : T