@@ -32,7 +32,7 @@ type Injs = [(TT Name, TT Name, TT Name)]
32type Fails = [(TT Name, TT Name, Env, Err, [FailContext], FailAt)]
32type Fails = [(TT Name, TT Name, Env, Err, [FailContext], FailAt)]
34unrecoverable :: Fails -> Bool
34unrecoverable :: Fails -> Bool
35−unrecoverable = any bad
35+unrecoverable = any bad
36 where bad (_,_,_, err, _, _) = unrec err
36 where bad (_,_,_, err, _, _) = unrec err
38 unrec (CantUnify r _ _ _ _ _) = not r
38 unrec (CantUnify r _ _ _ _ _) = not r
@@ -92,12 +92,12 @@ match_unify ctxt env topx topy inj holes from =
92 | (P (DCon _ _ _) _ _, _) <- unApply x,
92 | (P (DCon _ _ _) _ _, _) <- unApply x,
93 holeIn env f || f `elem` holes
93 holeIn env f || f `elem` holes
94 = let n' = uniqueName (sMN 0 "mv") (map fst env) in
94 = let n' = uniqueName (sMN 0 "mv") (map fst env) in
95− checkCycle names (f, Bind n' (Lam t) x)
95+ checkCycle names (f, Bind n' (Lam t) x)
96 un names tm@(App (P _ f (Bind fn (Pi t _) sc)) a) x
96 un names tm@(App (P _ f (Bind fn (Pi t _) sc)) a) x
97 | (P (DCon _ _ _) _ _, _) <- unApply x,
97 | (P (DCon _ _ _) _ _, _) <- unApply x,
98 holeIn env f || f `elem` holes
98 holeIn env f || f `elem` holes
99 = let n' = uniqueName fn (map fst env) in
99 = let n' = uniqueName fn (map fst env) in
100− checkCycle names (f, Bind n' (Lam t) x)
100+ checkCycle names (f, Bind n' (Lam t) x)
102 un names tx@(P _ x _) tm
102 un names tx@(P _ x _) tm
103 | tx /= tm && holeIn env x || x `elem` holes
103 | tx /= tm && holeIn env x || x `elem` holes
@@ -106,12 +106,12 @@ match_unify ctxt env topx topy inj holes from =
106 | ty /= tm && holeIn env y || y `elem` holes
106 | ty /= tm && holeIn env y || y `elem` holes
107 = do sc 1; checkCycle names (y, tm)
107 = do sc 1; checkCycle names (y, tm)
108 un bnames (V i) (P _ x _)
108 un bnames (V i) (P _ x _)
109− | length bnames > i,
109+ | length bnames > i,
110− fst (fst (bnames!!i)) == x ||
110+ fst (fst (bnames!!i)) == x ||
111 snd (fst (bnames!!i)) == x = do sc 1; return []
111 snd (fst (bnames!!i)) == x = do sc 1; return []
112 un bnames (P _ x _) (V i)
112 un bnames (P _ x _) (V i)
113 | length bnames > i,
113 | length bnames > i,
114− fst (fst (bnames!!i)) == x ||
114+ fst (fst (bnames!!i)) == x ||
115 snd (fst (bnames!!i)) == x = do sc 1; return []
115 snd (fst (bnames!!i)) == x = do sc 1; return []
116 un bnames (Bind x bx sx) (Bind y by sy) | notHole bx && notHole by
116 un bnames (Bind x bx sx) (Bind y by sy) | notHole bx && notHole by
117 = do h1 <- uB bnames bx by
117 = do h1 <- uB bnames bx by
@@ -124,7 +124,7 @@ match_unify ctxt env topx topy inj holes from =
124 un names x y
124 un names x y
125 | OK True <- convEq' ctxt holes x y = do sc 1; return []
125 | OK True <- convEq' ctxt holes x y = do sc 1; return []
126 | otherwise = do UI s f <- get
126 | otherwise = do UI s f <- get
127− let r = recoverable (normalise ctxt env x)
127+ let r = recoverable (normalise ctxt env x)
128 (normalise ctxt env y)
128 (normalise ctxt env y)
129 let err = cantUnify from r
129 let err = cantUnify from r
130 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
130 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
@@ -139,7 +139,7 @@ match_unify ctxt env topx topy inj holes from =
139 uB bnames (Lam tx) (Lam ty) = un bnames tx ty
139 uB bnames (Lam tx) (Lam ty) = un bnames tx ty
140 uB bnames (Pi tx _) (Pi ty _) = un bnames tx ty
140 uB bnames (Pi tx _) (Pi ty _) = un bnames tx ty
141 uB bnames x y = do UI s f <- get
141 uB bnames x y = do UI s f <- get
142− let r = recoverable (normalise ctxt env (binderTy x))
142+ let r = recoverable (normalise ctxt env (binderTy x))
143 (normalise ctxt env (binderTy y))
143 (normalise ctxt env (binderTy y))
144 let err = cantUnify from r topx topy
144 let err = cantUnify from r topx topy
145 (CantUnify r (binderTy x) (binderTy y) (Msg "") (errEnv env) s)
145 (CantUnify r (binderTy x) (binderTy y) (Msg "") (errEnv env) s)
@@ -152,12 +152,13 @@ match_unify ctxt env topx topy inj holes from =
153 -- TODO: there's an annoying amount of repetition between this and the
153 -- TODO: there's an annoying amount of repetition between this and the
154 -- main unification function. Consider lifting it out.
154 -- main unification function. Consider lifting it out.
155+ -- Issue #1721 on the issue tracker: https://github.com/idris-lang/Idris-dev/issues/1721
156 sc i = do UI s f <- get
157 sc i = do UI s f <- get
157 put (UI (s+i) f)
158 put (UI (s+i) f)
159 unifyFail x y = do UI s f <- get
160 unifyFail x y = do UI s f <- get
160− let r = recoverable (normalise ctxt env x)
161+ let r = recoverable (normalise ctxt env x)
161 (normalise ctxt env y)
162 (normalise ctxt env y)
162 let err = cantUnify from r
163 let err = cantUnify from r
163 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
164 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
@@ -192,23 +193,23 @@ match_unify ctxt env topx topy inj holes from =
192 where inst [] tm = tm
193 where inst [] tm = tm
193 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
194 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
195− bind i ns tm
196+ bind i ns tm
196 | i < 0 = tm
197 | i < 0 = tm
197 | otherwise = let ((x,y),ty) = ns!!i in
198 | otherwise = let ((x,y),ty) = ns!!i in
198 App (Bind y (Lam ty) (bind (i-1) ns tm))
199 App (Bind y (Lam ty) (bind (i-1) ns tm))
199 (P Bound x ty)
200 (P Bound x ty)
201renameBinders env (x, t) = (x, renameBindersTm env t)
202renameBinders env (x, t) = (x, renameBindersTm env t)
203renameBindersTm :: Env -> TT Name -> TT Name
204renameBindersTm :: Env -> TT Name -> TT Name
204renameBindersTm env tm = uniqueBinders (map fst env) tm
205renameBindersTm env tm = uniqueBinders (map fst env) tm
206 uniqueBinders env (Bind n b sc)
207 uniqueBinders env (Bind n b sc)
207− | n `elem` env
208+ | n `elem` env
208 = let n' = uniqueName n env in
209 = let n' = uniqueName n env in
209 explicitHole $ Bind n' (fmap (uniqueBinders env) b)
210 explicitHole $ Bind n' (fmap (uniqueBinders env) b)
210 (uniqueBinders (n':env) (rename n n' sc))
211 (uniqueBinders (n':env) (rename n n' sc))
211− | otherwise = Bind n (fmap (uniqueBinders (n:env)) b)
212+ | otherwise = Bind n (fmap (uniqueBinders (n:env)) b)
212 (uniqueBinders (n:env) sc)
213 (uniqueBinders (n:env) sc)
213 uniqueBinders env (App f a) = App (uniqueBinders env f) (uniqueBinders env a)
214 uniqueBinders env (App f a) = App (uniqueBinders env f) (uniqueBinders env a)
214 uniqueBinders env t = t
215 uniqueBinders env t = t
@@ -218,7 +219,7 @@ renameBindersTm env tm = uniqueBinders (map fst env) tm
218 rename n n' (App f a) = App (rename n n' f) (rename n n' a)
219 rename n n' (App f a) = App (rename n n' f) (rename n n' a)
219 rename n n' t = t
220 rename n n' t = t
221− explicitHole (Bind n (Hole ty) sc)
222+ explicitHole (Bind n (Hole ty) sc)
222 = Bind n (Hole ty) (instantiate (P Bound n ty) sc)
223 = Bind n (Hole ty) (instantiate (P Bound n ty) sc)
223 explicitHole t = t
224 explicitHole t = t
@@ -227,7 +228,7 @@ trimSolutions ns = followSols (dropPairs ns)
227 dropPairs (n@(x, P _ x' _) : ns)
228 dropPairs (n@(x, P _ x' _) : ns)
228 | x == x' = dropPairs ns
229 | x == x' = dropPairs ns
229 | otherwise
230 | otherwise
230− = n : dropPairs
231+ = n : dropPairs
231 (filter (\t -> case t of
232 (filter (\t -> case t of
232 (n, P _ n' _) -> not (n == x' && n' == x)
233 (n, P _ n' _) -> not (n == x' && n' == x)
233 _ -> True) ns)
234 _ -> True) ns)
@@ -253,14 +254,14 @@ hasv (App f a) = hasv f || hasv a
253hasv (Bind x b sc) = hasv (binderTy b) || hasv sc
254hasv (Bind x b sc) = hasv (binderTy b) || hasv sc
254hasv _ = False
255hasv _ = False
256−unify :: Context -> Env -> TT Name -> TT Name ->
257+unify :: Context -> Env -> TT Name -> TT Name ->
257 [Name] -> [Name] -> [Name] -> [FailContext] ->
258 [Name] -> [Name] -> [Name] -> [FailContext] ->
258 TC ([(Name, TT Name)], Fails)
259 TC ([(Name, TT Name)], Fails)
259unify ctxt env topx topy inj holes usersupp from =
260unify ctxt env topx topy inj holes usersupp from =
260−-- traceWhen (hasv topx || hasv topy)
261+-- traceWhen (hasv topx || hasv topy)
261-- ("Unifying " ++ show topx ++ "\nAND\n" ++ show topy ++ "\n") $
262-- ("Unifying " ++ show topx ++ "\nAND\n" ++ show topy ++ "\n") $
262 -- don't bother if topx and topy are different at the head
263 -- don't bother if topx and topy are different at the head
263− case runStateT (un False [] (renameBindersTm env topx)
264+ case runStateT (un False [] (renameBindersTm env topx)
264 (renameBindersTm env topy)) (UI 0 []) of
265 (renameBindersTm env topy)) (UI 0 []) of
265 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v),
266 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v),
@@ -271,7 +272,7 @@ unify ctxt env topx topy inj holes usersupp from =
271 case runStateT (un False [] topxn topyn)
272 case runStateT (un False [] topxn topyn)
272 (UI 0 []) of
273 (UI 0 []) of
273 OK (v, UI _ fails) ->
274 OK (v, UI _ fails) ->
274− return (map (renameBinders env) (trimSolutions v),
275+ return (map (renameBinders env) (trimSolutions v),
275 reverse fails)
276 reverse fails)
276-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
277-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
277 Error e -> tfail e
278 Error e -> tfail e
@@ -287,8 +288,8 @@ unify ctxt env topx topy inj holes usersupp from =
287 injective (App f a) = injective f -- && injective a
288 injective (App f a) = injective f -- && injective a
288 injective _ = False
289 injective _ = False
290−-- injectiveVar (P _ (MN _ _) _) = True -- TMP HACK
291+-- injectiveVar (P _ (MN _ _) _) = True -- TMP HACK
291− injectiveVar (P _ n _) = n `elem` inj
292+ injectiveVar (P _ n _) = n `elem` inj
292 injectiveVar (App f a) = injectiveVar f -- && injective a
293 injectiveVar (App f a) = injectiveVar f -- && injective a
293 injectiveVar _ = False
294 injectiveVar _ = False
@@ -364,7 +365,7 @@ unify ctxt env topx topy inj holes usersupp from =
364 then unifyTmpFail xtm tm
365 then unifyTmpFail xtm tm
365 else do sc 1
366 else do sc 1
367− | pureTerm tm, not (injective xtm) && injective tm
368+ | pureTerm tm, not (injective xtm) && injective tm
368 = do checkCycle bnames (x, tm)
369 = do checkCycle bnames (x, tm)
369 unifyTmpFail xtm tm
370 unifyTmpFail xtm tm
370 un' fn bnames tm ytm@(P _ y _)
371 un' fn bnames tm ytm@(P _ y _)
@@ -378,16 +379,16 @@ unify ctxt env topx topy inj holes usersupp from =
378 then unifyTmpFail tm ytm
379 then unifyTmpFail tm ytm
379 else do sc 1
380 else do sc 1
381− | pureTerm tm, not (injective ytm) && injective tm
382+ | pureTerm tm, not (injective ytm) && injective tm
382 = do checkCycle bnames (y, tm)
383 = do checkCycle bnames (y, tm)
383 unifyTmpFail tm ytm
384 unifyTmpFail tm ytm
384 un' fn bnames (V i) (P _ x _)
385 un' fn bnames (V i) (P _ x _)
385 | length bnames > i,
386 | length bnames > i,
386− fst ((map fst bnames)!!i) == x ||
387+ fst ((map fst bnames)!!i) == x ||
387 snd ((map fst bnames)!!i) == x = do sc 1; return []
388 snd ((map fst bnames)!!i) == x = do sc 1; return []
388 un' fn bnames (P _ x _) (V i)
389 un' fn bnames (P _ x _) (V i)
389 | length bnames > i,
390 | length bnames > i,
390− fst ((map fst bnames)!!i) == x ||
391+ fst ((map fst bnames)!!i) == x ||
391 snd ((map fst bnames)!!i) == x = do sc 1; return []
392 snd ((map fst bnames)!!i) == x = do sc 1; return []
393 un' fn names topx@(Bind n (Hole t) sc) y = unifyTmpFail topx y
394 un' fn names topx@(Bind n (Hole t) sc) y = unifyTmpFail topx y
@@ -417,17 +418,17 @@ unify ctxt env topx topy inj holes usersupp from =
417 un' fn bnames (App f x) (Bind n (Pi t k) y)
418 un' fn bnames (App f x) (Bind n (Pi t k) y)
418 | noOccurrence n y && injectiveApp f
419 | noOccurrence n y && injectiveApp f
419 = do ux <- un' False bnames x y
420 = do ux <- un' False bnames x y
420− uf <- un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
421+ uf <- un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
421 (Bind n (Pi t k) (V 1)))
422 (Bind n (Pi t k) (V 1)))
422 combine bnames ux uf
423 combine bnames ux uf
424 un' fn bnames (Bind n (Pi t k) y) (App f x)
425 un' fn bnames (Bind n (Pi t k) y) (App f x)
425 | noOccurrence n y && injectiveApp f
426 | noOccurrence n y && injectiveApp f
426 = do ux <- un' False bnames y x
427 = do ux <- un' False bnames y x
427− uf <- un' False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
428+ uf <- un' False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
428 (Bind n (Pi t k) (V 1))) f
429 (Bind n (Pi t k) (V 1))) f
429 combine bnames ux uf
430 combine bnames ux uf
431 un' fn bnames (Bind x bx sx) (Bind y by sy)
432 un' fn bnames (Bind x bx sx) (Bind y by sy)
432 | sameBinder bx by
433 | sameBinder bx by
433 = do h1 <- uB bnames bx by
434 = do h1 <- uB bnames bx by
@@ -438,9 +439,9 @@ unify ctxt env topx topy inj holes usersupp from =
438 sameBinder _ _ = False
439 sameBinder _ _ = False
439 un' fn bnames x y
440 un' fn bnames x y
440 | OK True <- convEq' ctxt holes x y = do sc 1; return []
441 | OK True <- convEq' ctxt holes x y = do sc 1; return []
441− | isUniverse x && isUniverse y = do sc 1; return []
442+ | isUniverse x && isUniverse y = do sc 1; return []
442 | otherwise = do UI s f <- get
443 | otherwise = do UI s f <- get
443− let r = recoverable (normalise ctxt env x)
444+ let r = recoverable (normalise ctxt env x)
444 (normalise ctxt env y)
445 (normalise ctxt env y)
445 let err = cantUnify from r
446 let err = cantUnify from r
446 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
447 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
@@ -519,7 +520,7 @@ unify ctxt env topx topy inj holes usersupp from =
519 _ -> False
520 _ -> False
521 metavar t = case t of
522 metavar t = case t of
522− P _ x _ -> (x `notElem` usersupp &&
523+ P _ x _ -> (x `notElem` usersupp &&
523 (x `elem` holes || holeIn env x))
524 (x `elem` holes || holeIn env x))
524 || globmetavar t
525 || globmetavar t
525 _ -> False
526 _ -> False
@@ -566,7 +567,7 @@ unify ctxt env topx topy inj holes usersupp from =
566 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
567 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
567 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
568 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
568 uB bnames x y = do UI s f <- get
569 uB bnames x y = do UI s f <- get
569− let r = recoverable (normalise ctxt env (binderTy x))
570+ let r = recoverable (normalise ctxt env (binderTy x))
570 (normalise ctxt env (binderTy y))
571 (normalise ctxt env (binderTy y))
571 let err = cantUnify from r topx topy
572 let err = cantUnify from r topx topy
572 (CantUnify r (binderTy x) (binderTy y) (Msg "") (errEnv env) s)
573 (CantUnify r (binderTy x) (binderTy y) (Msg "") (errEnv env) s)
@@ -590,9 +591,9 @@ unify ctxt env topx topy inj holes usersupp from =
590 else return [(x, bind v ns tm)]
591 else return [(x, bind v ns tm)]
591 where inst [] tm = tm
592 where inst [] tm = tm
592 inst (((n, _), _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
593 inst (((n, _), _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
593− checkScope ns (x, tm) = lift $ tfail (Msg "HOLE ERROR")
594+ checkScope ns (x, tm) = lift $ tfail (Msg "HOLE ERROR")
595− bind i ns tm
596+ bind i ns tm
596 | i < 0 = tm
597 | i < 0 = tm
597 | otherwise = let ((x,y),ty) = ns!!i in
598 | otherwise = let ((x,y),ty) = ns!!i in
598 App (Bind y (Lam ty) (bind (i-1) ns tm))
599 App (Bind y (Lam ty) (bind (i-1) ns tm))
@@ -639,7 +640,9 @@ envPos x i ((y, _) : ys) | x == y = i
639-- FIXME: Depending on how overloading gets used, this may cause problems. Better
640-- FIXME: Depending on how overloading gets used, this may cause problems. Better
640-- rethink overloading properly...
641-- rethink overloading properly...
641-- ASSUMPTION: inputs are in normal form
642-- ASSUMPTION: inputs are in normal form
644+-- Issue #1722 on the issue tracker https://github.com/idris-lang/Idris-dev/issues/1722
643recoverable t@(App _ _) _
646recoverable t@(App _ _) _
644 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
647 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
645recoverable _ t@(App _ _)
648recoverable _ t@(App _ _)
@@ -658,7 +661,7 @@ recoverable (App f a) p@(Constant _) = recoverable f p
658recoverable p@(P _ n _) (App f a) = recoverable p f
661recoverable p@(P _ n _) (App f a) = recoverable p f
659recoverable (App f a) p@(P _ _ _) = recoverable f p
662recoverable (App f a) p@(P _ _ _) = recoverable f p
660recoverable (App f a) (App f' a')
663recoverable (App f a) (App f' a')
661− | f == f' = recoverable a a'
664+ | f == f' = recoverable a a'
662recoverable (App f a) (App f' a')
665recoverable (App f a) (App f' a')
663 = recoverable f f' -- && recoverable a a'
666 = recoverable f f' -- && recoverable a a'
664recoverable f (Bind _ (Pi _ _) sc)
667recoverable f (Bind _ (Pi _ _) sc)
@@ -685,4 +688,3 @@ patIn env n = case lookup n env of
685 Just (PVar _) -> True
688 Just (PVar _) -> True
686 Just (PVTy _) -> True
689 Just (PVTy _) -> True
687 _ -> False
690 _ -> False