Commit 2feea8cb authored by Tom Bower's avatar Tom Bower
Browse files

Added Brones' unify function

parent d881a0f1
......@@ -117,15 +117,22 @@ unify t t'
= unifyPairs [(t, t')] []
unifyPairs :: [(Type, Type)] -> Sub -> Maybe Sub
unifyPairs [] sub = Just sub
unifyPairs ((TInt _, TInt _):ts) sub = unifyPairs ts sub
unifyPairs ((TBool _, TBool _):ts) sub = unifyPairs ts sub
unifyPairs ((TVar v, TVar v'):ts) sub
| v == v' = unifyPairs ts sub
unifyPairs ((a, b):ts) sub
= unifyPairs (map (\(x,y) -> (applySub (v1,v2) x, applySub (v1,v2) y)) ts) ((v1,v2):sub)
| v == v' = unifyPairs ts sub
unifyPairs ((TVar v, t):ts) sub
| occurs v t = Nothing
| otherwise = unify [(applySub sub' t1, applySub sub' t2) | (t1, t2) <- ts] sub'
where
v1 = if a == TVar v then v else a
v2 = if b == TVar v' then v' else b
sub' = (v, t) : sub
unifyPairs ((t, TVar v) : ts) sub
= unifyPairs ((TVar v, t) : ts) sub
unifyPairs ((Tfun tf1 tx1, TFun tf2 tx2) : ts) sub
= unifyPairs (((tf2, tf2) : (tx1, tx2) : ts) sub
unifyPairs _ _
= Nothing
------------------------------------------------------
-- PART IV
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment