


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!



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