@@ -31,6 +31,7 @@ unify ctxt env topx topy
31 -- trace ("Unifying " ++ show (topx, topy)) $
31 -- trace ("Unifying " ++ show (topx, topy)) $
32 let topxn = normalise ctxt env topx
32 let topxn = normalise ctxt env topx
33 topyn = normalise ctxt env topy in
33 topyn = normalise ctxt env topy in
34+-- trace ("Unifying " ++ show (topxn, topyn)) $
34 case runStateT (un' False [] topxn topyn)
35 case runStateT (un' False [] topxn topyn)
35 (UI 0 [] []) of
36 (UI 0 [] []) of
36 OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
37 OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
@@ -61,6 +62,15 @@ unify ctxt env topx topy
61 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
62 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
62 StateT UInfo
63 StateT UInfo
63 TC [(Name, TT Name)]
64 TC [(Name, TT Name)]
65+ un' fn names x y | x == y = return [] -- shortcut
66+ un' fn names topx@(P (DCon _ _) x _) topy@(P (DCon _ _) y _)
67+ | x /= y = unifyFail topx topy
68+ un' fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
69+ | x /= y = unifyFail topx topy
70+ un' fn names topx@(P (DCon _ _) x _) topy@(P (TCon _ _) y _)
71+ = unifyFail topx topy
72+ un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _) y _)
73+ = unifyFail topx topy
64 un' fn bnames (P Bound x _) (P Bound y _)
74 un' fn bnames (P Bound x _) (P Bound y _)
65 | (x,y) `elem` bnames = do sc 1; return []
75 | (x,y) `elem` bnames = do sc 1; return []
66 un' fn bnames (P Bound x _) tm
76 un' fn bnames (P Bound x _) tm
@@ -78,8 +88,11 @@ unify ctxt env topx topy
78 un' fn bnames (P Bound x _) (V i)
88 un' fn bnames (P Bound x _) (V i)
79 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
89 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
81− un' fn bnames (App fx ax) (App fy ay)
91+ un' fn bnames appx@(App fx ax) appy@(App fy ay)
92+ = do let (headx, _) = unApply fx
93+ let (heady, _) = unApply fy
94+ checkHeads headx heady
82− = do uplus -- do the second one if the first adds any errors
95+ uplus -- do the second one if the first adds any errors
83 (do hf <- un' True bnames fx fy
96 (do hf <- un' True bnames fx fy
84 let ax' = hnormalise hf ctxt env (substNames hf ax)
97 let ax' = hnormalise hf ctxt env (substNames hf ax)
85 let ay' = hnormalise hf ctxt env (substNames hf ay)
98 let ay' = hnormalise hf ctxt env (substNames hf ay)
@@ -94,6 +107,16 @@ unify ctxt env topx topy
94 combine bnames hf ha)
107 combine bnames hf ha)
95 where hnormalise [] _ _ t = t
108 where hnormalise [] _ _ t = t
96 hnormalise ns ctxt env t = normalise ctxt env t
109 hnormalise ns ctxt env t = normalise ctxt env t
110+ checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
111+ | x /= y = unifyFail appx appy
112+ checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
113+ | x /= y = unifyFail appx appy
114+ checkHeads (P (DCon _ _) x _) (P (TCon _ _) y _)
115+ = unifyFail appx appy
116+ checkHeads (P (TCon _ _) x _) (P (DCon _ _) y _)
117+ = unifyFail appx appy
118+ checkHeads _ _ = return []
97 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
120 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
98 | n == n' = un' False bnames x y
121 | n == n' = un' False bnames x y
99 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
122 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
@@ -111,6 +134,15 @@ unify ctxt env topx topy
111 put (UI s i ((x, y, env, err) : f))
134 put (UI s i ((x, y, env, err) : f))
112 return [] -- lift $ tfail err
135 return [] -- lift $ tfail err
137+ -- shortcut failure, if we *know* nothing can fix it
138+ unifyFail x y = do UI s i f <- get
139+ let r = recoverable x y
140+ let err = CantUnify r
141+ topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
142+ put (UI s i ((x, y, env, err) : f))
114 uB bnames (Let tx vx) (Let ty vy)
146 uB bnames (Let tx vx) (Let ty vy)
115 = do h1 <- un' False bnames tx ty
147 = do h1 <- un' False bnames tx ty
116 h2 <- un' False bnames ty vy
148 h2 <- un' False bnames ty vy