Docsity

Review, Type Inference - Lecture Notes - Functional Programming | COSC 3015, Study notes of Computer Science

Toggle sidebar
bg1
Lecture 23
Lectured by Prof. Caldwell and scribed by Sunil Kothari
November 13, 2008
1 Review
We looked at unification, substitutions, and various substitution operators.
Remember two types are unifiable if there exists a substitution σsuch that
σt1=σt2.
The terms are defined as:
data Term = V string | Abs String Term | Ap Term Term
The types are defined as:
Type = TyVar String | Arrow Type Type
The goal is a function infer :: Term →T ype where infer treturns the Type of
term t(if there is one).
2 Type Inference
We can make it a MayBe type to handle cases when a term has no type.
data MayBe A = Just A | Nothing
We have a little proof system for the type inference.
But first lets introduce the various concepts
A sequent (a state in the proof) for the type inference Γ `x:twhere,
1. Γ is called a context and is a list of string ×type pairs; the context is
what we know so far.
2. Eis a constraint set and is a list of type [(type, type)].
3. tis a term.
4. αis a type.
1
pf3
pf4

Partial preview of the text

Download Review, Type Inference - Lecture Notes - Functional Programming | COSC 3015 and more Study notes Computer Science in PDF only on Docsity!

Lecture 23

Lectured by Prof. Caldwell and scribed by Sunil Kothari

November 13, 2008

1 Review

We looked at unification, substitutions, and various substitution operators. Remember two types are unifiable if there exists a substitution σ such that σt 1 = σt 2.

The terms are defined as:

data Term = V string | Abs String Term | Ap Term Term

The types are defined as:

Type = TyVar String | Arrow Type Type

The goal is a function infer :: T erm → T ype where infer t returns the Type of term t (if there is one).

2 Type Inference

We can make it a MayBe type to handle cases when a term has no type.

data MayBe A = Just A | Nothing

We have a little proof system for the type inference.

But first lets introduce the various concepts A sequent (a state in the proof) for the type inference Γ ` x : t where,

  1. Γ is called a context and is a list of string × type pairs; the context is what we know so far.
  2. E is a constraint set and is a list of type [(type, type)].
  3. t is a term.
  4. α is a type.

The constraints get synthesized from the proof by propagating back down whereas we construct the proof tree bottom up.

The rule for a type variable is:

Γ, {α = τ } ` x : α where x : τ ∈ Γ (Var)

Here’s an example of Axiom rule: So [x, α → α] , {α → α = τ } ` x : τ.

Γ\x ∪ {x : α} M : β Γ , E ∪ {τ = α → β} λx.M : τ where α and β are fresh (Abs)

The notation means Γ\x - Remove pairs from Γ where x is the first element.

The rule for application is: Γ, E 1 M : α → τ Γ, E 2 N : α Γ, E 1 ∪ E 2 ` M N : τ where α is fresh (App)

So we can now do a proof

(Var) [x : α], β = α x : β (Abs) [], {β = α, τ = α → α} λx.x : τ

Suppose we wanted to find the type of the term (λx.x) y.

(Var) [y : β, x : β′], {τ = β′} x : τ (Abs) [y : β], {τ = β′, α′^ → α = β′^ → τ } λx.x : α′^ → α

(Var) [y : β], {α′^ = β} y : α′ (App) [y : β], {τ = β′, α′^ → α = β′^ → τ, α′^ = β} (λx.x)y : α

The constraint set generated by collecting the constraints from the above proof tree is: τ = β′ α′^ → α = β′^ → τ α′^ = β

The unification algorithm gives the substitution α = β α′^ = β τ = β′

This substitution when applied to the type we assumed earlier i.e. α gives β, which is what we expected.

in subst (unify e) trm where fvars [] = [] fvars ((x,t):xts) = (fv t) ++ fvars xts

Now we can test our code:

*Type_inference> :t infer infer :: [(String, Type)] -> Term -> ([([Char], Type)], [a])

infer [("y", TyVar "b")] (Ap (Abs "x" (V "x")) (V "y")) aaaa *Type_inference> infer_type [("y", TyVar "b")] (Ap (Abs "x" (V "x")) (V "y")) (TyVar "a") [] ([(b,aaaa),((aa -> a),(aaaa -> b)),(aa,b)],["aaaa","b","aa","y","aa","a"]) *Type_inference> let (e,) = infer_type [("y", TyVar "b")] (Ap (Abs "x" (V "x")) (V "y")) (TyVar "a") [] in unify e [a := aaaa,aa := aaaa,b := aaaa] *Type_inference> let (e,) = infer_type [("y", TyVar "b")] (Ap (Abs "x" (V "x")) (V "y")) (TyVar "a") [] in unify [ head e] [b := aaaa] *Type_inference> let (e,_) = infer_type [("y", TyVar "b")] (Ap (Abs "x" (V "x")) (V "y")) (TyVar "a") [] in map2 (subst (unify [ head e])) (tail e) [b := aaaa] *Type_inference> infer_type [("y", TyVar "b")] (Ap (Abs "x" (V "x")) (V "y")) (TyVar "a") [] ([(b,aaaa),((aa -> a),(aaaa -> b)),(aa,b)],["aaaa","b","aa","y","aa","a"])