25.10.2015 Views

Write You a Haskell Stephen Diehl

1kEcQTb

1kEcQTb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

[s]τ = [s]τ ′<br />

Two identical terms are trivially unifiable by the empty unifier.<br />

c ∼ c : [ ]<br />

e unification rules for our little HM language are as follows:<br />

c ∼ c : []<br />

α ∼ α : []<br />

α /∈ ftv(τ)<br />

α ∼ τ : [α/τ]<br />

α /∈ ftv(τ)<br />

τ ∼ α : [α/τ]<br />

τ 1 ∼ τ ′ 1 : θ 1 [θ 1 ]τ 2 ∼ [θ 1 ]τ ′ 2 : θ 2<br />

τ 1 τ 2 ∼ τ ′ 1 τ ′ 2 : θ 2 ◦ θ 1<br />

τ 1 ∼ τ ′ 1 : θ 1 [θ 1 ]τ 2 ∼ [θ 1 ]τ ′ 2 : θ 2<br />

τ 1 → τ 2 ∼ τ ′ 1 → τ ′ 2 : θ 2 ◦ θ 1<br />

(Uni-Const)<br />

(Uni-Var)<br />

(Uni-VarLeft)<br />

(Uni-VarRight)<br />

(Uni-Con)<br />

(Uni-Arrow)<br />

ere is a precondition for unifying two variables known as the occurs check which asserts that if we are<br />

applying a substitution of variable x to an expression e, the variable x cannot be free in e. Otherwise<br />

the rewrite would diverge, constantly rewriting itself. For example the following substitution would not<br />

pass the occurs check and would generate an infinitely long function type.<br />

[x/x → x]<br />

e occurs check will forbid the existence of many of the pathological livering terms we discussed when<br />

covering the untyped lambda calculus, including the omega combinator.<br />

occursCheck :: Substitutable a => TVar -> a -> Bool<br />

occursCheck a t = a ‘Set.member‘ ftv t<br />

e unify function lives in the Infer monad and yields a subsitution:<br />

unify :: Type -> Type -> Infer Subst<br />

unify (l ‘TArr‘ r) (l’ ‘TArr‘ r’) = do<br />

s1

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!