Docsity

Unification Algorithm and Type Inference: Lecture Notes by Prof. Caldwell, Assignments of Computer Science

Toggle sidebar
bg1
Lecture 22
Lectured by Prof. Caldwell and scribed by Sunil Kothari
November 11, 2008
1 Review
Remember two types are unifiable if there exists a substitution σsuch that
σt1=σt2.
The types are defined by the following grammar:
Type = TyVar String | Arrow Type Type
The HW was to extend the unification algorithm to product types. Last time,
we couldn’t get the unification completely done. The goal is to use unification
to solves equations to infer a type for a term.
2 Substitution
unify :: type →type →S ubstitution
data Substitution = S [(String, Type)]
subst (S []) t = t
subst (S (x,t1):xts) (TyVar y) = if x == y then t1 else subst (S xts) (TyVar y)
subst s (Arrow t1 t2) = Arrow (subst s t1) (subst s t2)
The subst is a simultaneous substitution or parallel substitution.
We need a couple of operators: subst subst,+++.
1. subst subst
subst_subst s1 (S s2) = S (map_snd (subst s1) s2)
where map_snd f [] = []
map_snd f ((x,t):xts) = (x,f t): map_snd f xts
Our goal is that the substitution should behave as a function.
1
pf3
pf4

Partial preview of the text

Download Unification Algorithm and Type Inference: Lecture Notes by Prof. Caldwell and more Assignments Computer Science in PDF only on Docsity!

Lecture 22

Lectured by Prof. Caldwell and scribed by Sunil Kothari

November 11, 2008

1 Review

Remember two types are unifiable if there exists a substitution σ such that σt 1 = σt 2.

The types are defined by the following grammar:

Type = TyVar String | Arrow Type Type

The HW was to extend the unification algorithm to product types. Last time, we couldn’t get the unification completely done. The goal is to use unification to solves equations to infer a type for a term.

2 Substitution

unif y :: type → type → Substitution

data Substitution = S [(String, Type)]

subst (S []) t = t subst (S (x,t1):xts) (TyVar y) = if x == y then t1 else subst (S xts) (TyVar y) subst s (Arrow t1 t2) = Arrow (subst s t1) (subst s t2)

The subst is a simultaneous substitution or parallel substitution. We need a couple of operators: subst subst, +++.

  1. subst subst

subst_subst s1 (S s2) = S (map_snd (subst s1) s2) where map_snd f [] = [] map_snd f ((x,t):xts) = (x,f t): map_snd f xts

Our goal is that the substitution should behave as a function.

  1. The composition of substitution (s1+++s2) is defined as: subst (s1+++s2) t = subst s1 (subst s 2 t) The mathematical notion for composition is: (f.g) x = f (g x)

(S s2) +++ (S s1) = let (S s1’) = subst_subst (S s2)(S s1) in

But doing like this we have a problem. Suppose s2 = [y := t1] and s1 = [x := V y] then subst subst s 2 s1 = [x := t1]

But when we apply this subst subst (s2 + + + s1) (Arrow (V y) (V x)) ; Arrow t 1 t1. So we change to

(S s2) +++ (S s1) = let (S s1’) = subst_subst (S s2)(S s1) in S (functionalize (s1’ ++ s2)) where functionalize [] = [] functionalize ((x,t):xts) = (x,t):functionalize (filter ((/= x).fst) xts)

The complicated piece of code (f ilter((/ = x).f st)xts) is : apply first to each element of xts and if it’s not equal to x keep it.

But this is a O(n^2 ) algorithm - but we are not concerned since substitu- tions don’t become very big for the terms that we consider here.

3 Unification Algorithm

  1. A variable (say x) unifies with any term t1 as long as x /∈ F V (t). i.e. make a substitution [(x, t)]
  2. t1 and t2 unify if t1 and t2 have the same constructor and their corre- sponding subterms unify.

The code in the HW unifies a list of term-term pairs.

unify (Var x) t = if x ‘elem‘ (fv t) then error "unify:occurs check" else case t of Var y -> if x == y then S [] else S[(x,t)]

4 Type Inference

If {} M : α yields σ then there is a proof in the type system that {} M : σ(α).

The first type inference algorithm was given by Milner in 1978 but Hindley and Seldin came up with the ideas in 1965. Mitchell Wand (1986) gave an algorithm where substitutions are not built as the proof tree is constructed. Instead the equations are generated and substitution is then generated from those equations. Here’s the proof rules for the Wand’s type system We write Γ ` x : t where,

  1. Γ is called a context and is a list of variable × type pairs;
  2. E is a constraint set and is a list of type [(type, type)].
  3. t is a term; and
  4. α is a type.

The terms are given by the following grammar T erm ::= V String | Abs String T erm | Ap T erm T erm

The rule for a type variable is:

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

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

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

Next time we will look at a recursive algorithm which will generate this con- straint set on which we apply the unification algorithm to give a type for the term.