


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



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).
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,
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"])