@@ -16,17 +16,19 @@ import Debug.Trace
16-- at the time
16-- at the time
18type Injs = [(TT Name, TT Name, TT Name)]
18type Injs = [(TT Name, TT Name, TT Name)]
19+type Fails = [(TT Name, TT Name, Err)]
20−data UInfo = UI Int Injs
21+data UInfo = UI Int Injs Fails
22unify :: Context -> Env -> TT Name -> TT Name -> TC ([(Name, TT Name)],
23unify :: Context -> Env -> TT Name -> TT Name -> TC ([(Name, TT Name)],
23− Injs)
24+ Injs, Fails)
24unify ctxt env topx topy
25unify ctxt env topx topy
25 = case runStateT
26 = case runStateT
26 (un' False [] (normalise ctxt env topx) (normalise ctxt env topy))
27 (un' False [] (normalise ctxt env topx) (normalise ctxt env topy))
27− (UI 0 []) of
28+ (UI 0 [] []) of
28− OK (v, (UI _ inj)) -> return (filter notTrivial v, inj)
29+ OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
29− Error e -> tfail $ CantUnify topx topy e 0
30+-- OK (_, UI s _ ((_,_,f):fs)) -> tfail $ CantUnify topx topy f s
31 notTrivial (x, P _ x' _) = x /= x'
33 notTrivial (x, P _ x' _) = x /= x'
32 notTrivial _ = True
34 notTrivial _ = True
@@ -39,8 +41,15 @@ unify ctxt env topx topy
39 notP (P _ _ _) = False
41 notP (P _ _ _) = False
40 notP _ = True
42 notP _ = True
42− sc i = do UI s x <- get
44+ sc i = do UI s x f <- get
43− put (UI (s+i) x)
45+ put (UI (s+i) x f)
47+ uplus u1 u2 = do UI s i f <- get
50+ if (length f == length f')
52+ else do put (UI s i f); u2
45 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
54 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
46 StateT UInfo
55 StateT UInfo
@@ -48,13 +57,13 @@ unify ctxt env topx topy
48 un' fn bnames (P Bound x _) (P Bound y _)
57 un' fn bnames (P Bound x _) (P Bound y _)
49 | (x,y) `elem` bnames = do sc 1; return []
58 | (x,y) `elem` bnames = do sc 1; return []
50 un' fn bnames (P Bound x _) tm
59 un' fn bnames (P Bound x _) tm
51− | holeIn env x = do UI s i <- get
60+ | holeIn env x = do UI s i f <- get
52− when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i))
61+ when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
54 return [(x, tm)]
63 return [(x, tm)]
55 un' fn bnames tm (P Bound y _)
64 un' fn bnames tm (P Bound y _)
56− | holeIn env y = do UI s i <- get
65+ | holeIn env y = do UI s i f <- get
57− when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i))
66+ when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
59 return [(y, tm)]
68 return [(y, tm)]
60 un' fn bnames (V i) (P Bound x _)
69 un' fn bnames (V i) (P Bound x _)
@@ -63,7 +72,8 @@ unify ctxt env topx topy
63 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
72 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
65 un' fn bnames (App fx ax) (App fy ay)
74 un' fn bnames (App fx ax) (App fy ay)
75+ = do uplus -- do the second one if the first adds any errors
66− = mplus (do hf <- un' True bnames fx fy
76+ (do hf <- un' True bnames fx fy
67 let ax' = normalise ctxt env (substNames hf ax)
77 let ax' = normalise ctxt env (substNames hf ax)
68 let ay' = normalise ctxt env (substNames hf ay)
78 let ay' = normalise ctxt env (substNames hf ay)
69 ha <- un' False bnames ax' ay'
79 ha <- un' False bnames ax' ay'
@@ -86,8 +96,10 @@ unify ctxt env topx topy
86 combine bnames h1 h2
96 combine bnames h1 h2
87 un' fn bnames x y
97 un' fn bnames x y
88 | x == y = do sc 1; return []
98 | x == y = do sc 1; return []
89− | otherwise = do UI s _ <- get
99+ | otherwise = do UI s i f <- get
100+ let err = CantUnify topx topy (CantUnify x y (Msg "") s) s
101+ put (UI s i ((x, y, err) : f))
90− lift $ tfail (CantUnify x y (Msg "") s)
102+ return [] -- lift $ tfail err
92 uB bnames (Let tx vx) (Let ty vy)
104 uB bnames (Let tx vx) (Let ty vy)
93 = do h1 <- un' False bnames tx ty
105 = do h1 <- un' False bnames tx ty
@@ -103,8 +115,12 @@ unify ctxt env topx topy
103 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
115 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
104 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
116 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
105 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
117 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
106− uB bnames x y = do UI s _ <- get
118+ uB bnames x y = do UI s i f <- get
107− lift $ tfail (CantUnify (binderTy x) (binderTy y) (Msg "") s)
119+ let err = CantUnify topx topy
120+ (CantUnify (binderTy x) (binderTy y) (Msg "") s)
122+ put (UI s i ((binderTy x, binderTy y, err) : f))
123+ return [] -- lift $ tfail err
109 combine bnames as [] = return as
125 combine bnames as [] = return as
110 combine bnames as ((n, t) : bs)
126 combine bnames as ((n, t) : bs)