@@ -31,6 +31,7 @@ unify ctxt env topx topy
3131 -- trace ("Unifying " ++ show (topx, topy)) $
3232 let topxn = normalise ctxt env topx
3333 topyn = normalise ctxt env topy in
34+-- trace ("Unifying " ++ show (topxn, topyn)) $
3435 case runStateT (un' False [] topxn topyn)
3536 (UI 0 [] []) of
3637 OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
@@ -61,6 +62,15 @@ unify ctxt env topx topy
6162 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
6263 StateT UInfo
6364 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
6474 un' fn bnames (P Bound x _) (P Bound y _)
6575 | (x,y) `elem` bnames = do sc 1; return []
6676 un' fn bnames (P Bound x _) tm
@@ -78,8 +88,11 @@ unify ctxt env topx topy
7888 un' fn bnames (P Bound x _) (V i)
7989 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
8090
81− un' fn bnames (App fx ax) (App fy ay)
82− = do uplus -- do the second one if the first adds any errors
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
95+ uplus -- do the second one if the first adds any errors
8396 (do hf <- un' True bnames fx fy
8497 let ax' = hnormalise hf ctxt env (substNames hf ax)
8598 let ay' = hnormalise hf ctxt env (substNames hf ay)
@@ -94,6 +107,16 @@ unify ctxt env topx topy
94107 combine bnames hf ha)
95108 where hnormalise [] _ _ t = t
96109 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 []
119+
97120 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
98121 | n == n' = un' False bnames x y
99122 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
@@ -111,6 +134,15 @@ unify ctxt env topx topy
111134 put (UI s i ((x, y, env, err) : f))
112135 return [] -- lift $ tfail err
113136
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))
143+ lift $ tfail err
144+
145+
114146 uB bnames (Let tx vx) (Let ty vy)
115147 = do h1 <- un' False bnames tx ty
116148 h2 <- un' False bnames ty vy