1{-# LANGUAGE PatternGuards #-}
3module Idris.Core.Unify(match_unify, unify, Fails, FailContext(..), FailAt(..),
7import Idris.Core.Evaluate
10import Control.Monad.State.Strict
15-- Unification is applied inside the theorem prover. We're looking for holes
16-- which can be filled in, by matching one term's normal form against another.
17-- Returns a list of hole names paired with the term which solves them, and
18-- a list of things which need to be injective.
20-- terms which need to be injective, with the things we're trying to unify
23data FailAt = Match | Unify
26data FailContext = FailContext { fail_sourceloc :: FC,
32type Injs = [(TT Name, TT Name, TT Name)]
33type Fails = [(TT Name, TT Name, -- unification error
34 Bool, -- ready to retry yet
35 Env, Err, [FailContext], FailAt)]
37unrecoverable :: Fails -> Bool
38unrecoverable = any bad
39 where bad (_,_,_,_, err, _, _) = unrec err
41 unrec (CantUnify r _ _ _ _ _) = not r
42 unrec (At _ e) = unrec e
43 unrec (Elaborating _ _ _ e) = unrec e
44 unrec (ElaboratingArg _ _ _ e) = unrec e
47data UInfo = UI Int Fails
54-- | Smart constructor for unification errors that takes into account the FailContext
55cantUnify :: [FailContext] -> Bool -> (t, Maybe Provenance) -> (t, Maybe Provenance) -> (Err' t) -> [(Name, t)] -> Int -> Err' t
56cantUnify [] r t1 t2 e ctxt i = CantUnify r t1 t2 e ctxt i
57cantUnify (FailContext fc f x : prev) r t1 t2 e ctxt i =
58 At fc (ElaboratingArg f x
59 (map (\(FailContext _ f' x') -> (f', x')) prev)
60 (CantUnify r t1 t2 e ctxt i))
62-- Solve metavariables by matching terms against each other
63-- Not really unification, of course!
65match_unify :: Context -> Env ->
66 (TT Name, Maybe Provenance) ->
67 (TT Name, Maybe Provenance) -> [Name] -> [Name] -> [FailContext] ->
69match_unify ctxt env (topx, xfrom) (topy, yfrom) inj holes from =
70 case runStateT (un [] (renameBindersTm env topx)
71 (renameBindersTm env topy)) (UI 0 []) of
73 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
74 return (map (renameBinders env) v')
76 let topxn = renameBindersTm env (normalise ctxt env topx)
77 topyn = renameBindersTm env (normalise ctxt env topy) in
78 case runStateT (un [] topxn topyn)
81 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
82 return (map (renameBinders env) v')
84 -- just normalise the term we're matching against
85 case runStateT (un [] topxn topy)
88 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
89 return (map (renameBinders env) v')
92 un :: [((Name, Name), TT Name)] -> TT Name -> TT Name ->
96 un names tx@(P _ x _) tm
97 | tx /= tm && holeIn env x || x `elem` holes
98 = do sc 1; checkCycle names (x, tm)
99 un names tm ty@(P _ y _)
100 | ty /= tm && holeIn env y || y `elem` holes
101 = do sc 1; checkCycle names (y, tm)
102 un bnames (V i) (P _ x _)
104 fst (fst (bnames!!i)) == x ||
105 snd (fst (bnames!!i)) == x = do sc 1; return []
106 un bnames (P _ x _) (V i)
108 fst (fst (bnames!!i)) == x ||
109 snd (fst (bnames!!i)) == x = do sc 1; return []
110 un bnames (Bind x bx sx) (Bind y by sy) | notHole bx && notHole by
111 = do h1 <- uB bnames bx by
112 h2 <- un (((x, y), binderTy bx) : bnames) sx sy
114 un names (App _ fx ax) (App _ fy ay)
115 = do hf <- un names fx fy
119 | OK True <- convEq' ctxt holes x y = do sc 1; return []
120 | otherwise = do UI s f <- get
121 let r = recoverable (normalise ctxt env x)
122 (normalise ctxt env y)
123 let err = cantUnify from r
124 (topx, xfrom) (topy, yfrom) (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
125 if (not r) then lift $ tfail err
126 else do put (UI s ((x, y, True, env, err, from, Match) : f))
130 uB bnames (Let tx vx) (Let ty vy) = do h1 <- un bnames tx ty
131 h2 <- un bnames vx vy
133 uB bnames (Lam tx) (Lam ty) = un bnames tx ty
134 uB bnames (Pi i tx _) (Pi i' ty _) = un bnames tx ty
135 uB bnames x y = do UI s f <- get
136 let r = recoverable (normalise ctxt env (binderTy x))
137 (normalise ctxt env (binderTy y))
138 let err = cantUnify from r (topx, xfrom) (topy, yfrom)
139 (CantUnify r (binderTy x, Nothing)
140 (binderTy y, Nothing) (Msg "") (errEnv env) s)
142 put (UI s ((binderTy x, binderTy y,
144 env, err, from, Match) : f))
147 notHole (Hole _) = False
150 -- TODO: there's an annoying amount of repetition between this and the
151 -- main unification function. Consider lifting it out.
152 -- Issue #1721 on the issue tracker: https://github.com/idris-lang/Idris-dev/issues/1721
154 sc i = do UI s f <- get
157 unifyFail x y = do UI s f <- get
158 let r = recoverable (normalise ctxt env x)
159 (normalise ctxt env y)
160 let err = cantUnify from r
161 (topx, xfrom) (topy, yfrom)
162 (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
163 put (UI s ((x, y, True, env, err, from, Match) : f))
165 combine bnames as [] = return as
166 combine bnames as ((n, t) : bs)
167 = case lookup n as of
168 Nothing -> combine bnames (as ++ [(n,t)]) bs
169 Just t' -> do ns <- un bnames t t'
170 let ns' = filter (\ (x, _) -> x/=n) ns
172 combine bnames as (ns' ++ bs)
174-- substN n tm (var, sol) = (var, subst n tm sol)
176 checkCycle ns p@(x, P _ x' _) | x == x' = return []
177 checkCycle ns p@(x, P _ _ _) = return [p]
178 checkCycle ns (x, tm)
179 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
180 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
182 checkScope ns (x, tm) =
183-- case boundVs (envPos x 0 env) tm of
184-- [] -> return [(x, tm)]
185-- (i:_) -> lift $ tfail (UnifyScope x (fst (fst (ns!!i)))
186-- (inst ns tm) (errEnv env))
187 let v = highV (-1) tm in
189 then lift $ tfail (Msg "SCOPE ERROR")
190 else return [(x, bind v ns tm)]
191 where inst [] tm = tm
192 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
196 | otherwise = let ((x,y),ty) = ns!!i in
197 App MaybeHoles (Bind y (Lam ty) (bind (i-1) ns tm))
200renameBinders env (x, t) = (x, renameBindersTm env t)
202renameBindersTm :: Env -> TT Name -> TT Name
203renameBindersTm env tm = uniqueBinders (map fst env) tm
205 uniqueBinders env (Bind n b sc)
207 = let n' = uniqueName n env in
208 explicitHole $ Bind n' (fmap (uniqueBinders env) b)
209 (uniqueBinders (n':env) (rename n n' sc))
210 | otherwise = Bind n (fmap (uniqueBinders (n:env)) b)
211 (uniqueBinders (n:env) sc)
212 uniqueBinders env (App s f a) = App s (uniqueBinders env f) (uniqueBinders env a)
213 uniqueBinders env t = t
215 rename n n' (P nt x ty) | n == x = P nt n' ty
216 rename n n' (Bind x b sc) = Bind x (fmap (rename n n') b) (rename n n' sc)
217 rename n n' (App s f a) = App s (rename n n' f) (rename n n' a)
220 explicitHole (Bind n (Hole ty) sc)
221 = Bind n (Hole ty) (instantiate (P Bound n ty) sc)
224trimSolutions (topx, xfrom) (topy, yfrom) from env topns = followSols [] (dropPairs topns)
225 where dropPairs [] = []
226 dropPairs (n@(x, P _ x' _) : ns)
227 | x == x' = dropPairs ns
230 (filter (\t -> case t of
231 (n, P _ n' _) -> not (n == x' && n' == x)
233 dropPairs (n : ns) = n : dropPairs ns
235 followSols vs [] = return []
236 followSols vs ((n, P _ t _) : ns)
237 | Just t' <- lookup t ns
238 = do vs' <- case t' of
240 if (n, tn) `elem` vs then -- cycle
241 tfail (cantUnify from False (topx, xfrom) (topy, yfrom)
242 (Msg "") (errEnv env) 0)
243 else return ((n, tn) : vs)
245 followSols vs' ((n, t') : ns)
246 followSols vs (n : ns) = do ns' <- followSols vs ns
249expandLets env (x, tm) = (x, doSubst (reverse env) tm)
252 doSubst ((n, Let v t) : env) tm
253 = doSubst env (subst n v tm)
257hasv :: TT Name -> Bool
259hasv (App _ f a) = hasv f || hasv a
260hasv (Bind x b sc) = hasv (binderTy b) || hasv sc
263unify :: Context -> Env ->
264 (TT Name, Maybe Provenance) ->
265 (TT Name, Maybe Provenance) ->
266 [Name] -> [Name] -> [Name] -> [FailContext] ->
267 TC ([(Name, TT Name)], Fails)
268unify ctxt env (topx, xfrom) (topy, yfrom) inj holes usersupp from =
269-- traceWhen (hasv topx || hasv topy)
270-- ("Unifying " ++ show topx ++ "\nAND\n" ++ show topy ++ "\n") $
271 -- don't bother if topx and topy are different at the head
272 case runStateT (un False [] (renameBindersTm env topx)
273 (renameBindersTm env topy)) (UI 0 []) of
274 OK (v, UI _ []) -> do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
275 return (map (renameBinders env) v', [])
277 let topxn = renameBindersTm env (normalise ctxt env topx)
278 topyn = renameBindersTm env (normalise ctxt env topy) in
279-- trace ("Unifying " ++ show (topx, topy) ++ "\n\n==>\n" ++ show (topxn, topyn) ++ "\n\n") $
280 case runStateT (un False [] topxn topyn)
282 OK (v, UI _ fails) ->
283 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
284-- trace ("OK " ++ show (topxn, topyn, v, holes)) $
285 return (map (renameBinders env) v', reverse fails)
286-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
289 headDiff (P (DCon _ _ _) x _) (P (DCon _ _ _) y _) = x /= y
290 headDiff (P (TCon _ _) x _) (P (TCon _ _) y _) = x /= y
293 injective (P (DCon _ _ _) _ _) = True
294 injective (P (TCon _ _) _ _) = True
295 injective (App _ f a) = injective f -- && injective a
298-- injectiveVar (P _ (MN _ _) _) = True -- TMP HACK
299 injectiveVar (P _ n _) = n `elem` inj
300 injectiveVar (App _ f a) = injectiveVar f -- && injective a
301 injectiveVar _ = False
303 injectiveApp x = injective x || injectiveVar x
305 notP (P _ _ _) = False
308 sc i = do UI s f <- get
311 errors :: StateT UInfo TC Bool
312 errors = do UI s f <- get
313 return (not (null f))
315 uplus u1 u2 = do UI s f <- get
318 if (length f == length f')
320 else do put (UI s f); u2
322 un :: Bool -> [((Name, Name), TT Name)] -> TT Name -> TT Name ->
327-- = let (xf, _) = unApply x
328-- (yf, _) = unApply y in
329-- if headDiff xf yf then unifyFail x y else
330-- uplus (un' fn names x y)
331-- (un' fn names (hnf ctxt env x) (hnf ctxt env y))
333 un' :: Env -> Bool -> [((Name, Name), TT Name)] -> TT Name -> TT Name ->
336 un' env fn names x y | x == y = return [] -- shortcut
337 un' env fn names topx@(P (DCon _ _ _) x _) topy@(P (DCon _ _ _) y _)
338 | x /= y = unifyFail topx topy
339 un' env fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
340 | x /= y = unifyFail topx topy
341 un' env fn names topx@(P (DCon _ _ _) x _) topy@(P (TCon _ _) y _)
342 = unifyFail topx topy
343 un' env fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _ _) y _)
344 = unifyFail topx topy
345 un' env fn names topx@(Constant _) topy@(P (TCon _ _) y _)
346 = unifyFail topx topy
347 un' env fn names topx@(P (TCon _ _) x _) topy@(Constant _)
348 = unifyFail topx topy
349 un' env fn bnames tx@(P _ x _) ty@(P _ y _)
350 | (x,y) `elem` map fst bnames || x == y = do sc 1; return []
351 | injective tx && not (holeIn env y || y `elem` holes)
353 | injective ty && not (holeIn env x || x `elem` holes)
355 -- pick the one bound earliest if both are holes
356 | tx /= ty && (holeIn env x || x `elem` holes)
357 && (holeIn env y || y `elem` holes)
358 = case compare (envPos 0 x env) (envPos 0 y env) of
359 LT -> do sc 1; checkCycle bnames (x, ty)
360 _ -> do sc 1; checkCycle bnames (y, tx)
361 where envPos i n ((n',_):env) | n == n' = i
362 envPos i n (_:env) = envPos (i+1) n env
363 envPos _ _ _ = 100000
364 un' env fn bnames xtm@(P _ x _) tm
365 | pureTerm tm, holeIn env x || x `elem` holes
368 x <- checkCycle bnames (x, tm)
370-- trace (show (x, tm, normalise ctxt env tm)) $
371-- put (UI s ((tm, topx, topy) : i) f)
372 then unifyTmpFail xtm tm
375 | pureTerm tm, not (injective xtm) && injective tm
376 = do checkCycle bnames (x, tm)
378 un' env fn bnames tm ytm@(P _ y _)
379 | pureTerm tm, holeIn env y || y `elem` holes
382 x <- checkCycle bnames (y, tm)
384-- trace (show (y, tm, normalise ctxt env tm)) $
385-- put (UI s ((tm, topx, topy) : i) f)
386 then unifyTmpFail tm ytm
389 | pureTerm tm, not (injective ytm) && injective tm
390 = do checkCycle bnames (y, tm)
392 un' env fn bnames (V i) (P _ x _)
394 fst ((map fst bnames)!!i) == x ||
395 snd ((map fst bnames)!!i) == x = do sc 1; return []
396 un' env fn bnames (P _ x _) (V i)
398 fst ((map fst bnames)!!i) == x ||
399 snd ((map fst bnames)!!i) == x = do sc 1; return []
401 un' env fn names topx@(Bind n (Hole t) sc) y = unifyTmpFail topx y
402 un' env fn names x topy@(Bind n (Hole t) sc) = unifyTmpFail x topy
404-- Pattern unification rule
405 un' env fn bnames tm app@(App _ _ _)
406 | (P _ mv _, args) <- unApply app,
407 holeIn env mv || mv `elem` holes,
409 containsOnly (mapMaybe getname args) (mapMaybe getV args) tm
410 -- && TODO: tm does not refer to any variables other than those
412 = -- trace ("PATTERN RULE SOLVE: " ++ show (mv, tm, env, bindLams args (substEnv env tm))) $
413 checkCycle bnames (mv, eta [] $ bindLams args (substEnv env tm))
414 where rigid (V i) = True
415 rigid (P _ t _) = t `elem` map fst env &&
416 not (holeIn env t || t `elem` holes)
422 getname (P _ n _) = Just n
425 containsOnly args vs (V i) = i `elem` vs
426 containsOnly args vs (P Bound n ty)
427 = n `elem` args && containsOnly args vs ty
428 containsOnly args vs (P _ n ty)
429 = not (holeIn env n || n `elem` holes)
430 && containsOnly args vs ty
431 containsOnly args vs (App _ f a)
432 = containsOnly args vs f && containsOnly args vs a
433 containsOnly args vs (Bind _ b sc)
434 = containsOnly args vs (binderTy b) &&
435 containsOnly args (0 : map (+1) vs) sc
436 containsOnly args vs _ = True
439 bindLams (a : as) tm = bindLam a (bindLams as tm)
441 bindLam (V i) tm = Bind (fst (env !! i))
442 (Lam (binderTy (snd (env !! i))))
444 bindLam (P _ n ty) tm = Bind n (Lam ty) tm
445 bindLam _ tm = error "Can't happen [non rigid bindLam]"
448 substEnv ((n, t) : env) tm
449 = substEnv env (substV (P Bound n (binderTy t)) tm)
451 -- remove any unnecessary lambdas (helps with type class
452 -- resolution later).
453 eta ks (Bind n (Lam ty) sc) = eta ((n, ty) : ks) sc
454 eta ks t = rebind ks t
456 rebind ((n, ty) : ks) (App _ f (P _ n' _))
458 rebind ((n, ty) : ks) t = rebind ks (Bind n (Lam ty) t)
461 un' env fn bnames appx@(App _ _ _) appy@(App _ _ _)
462 = unApp env fn bnames appx appy
463-- = uplus (unApp fn bnames appx appy)
464-- (unifyTmpFail appx appy) -- take the whole lot
466 un' env fn bnames x (Bind n (Lam t) (App _ y (P Bound n' _)))
467 | n == n' = un' env False bnames x y
468 un' env fn bnames (Bind n (Lam t) (App _ x (P Bound n' _))) y
469 | n == n' = un' env False bnames x y
470 un' env fn bnames x (Bind n (Lam t) (App _ y (V 0)))
471 = un' env False bnames x y
472 un' env fn bnames (Bind n (Lam t) (App _ x (V 0))) y
473 = un' env False bnames x y
474-- un' env fn bnames (Bind x (PVar _) sx) (Bind y (PVar _) sy)
475-- = un' env False ((x,y):bnames) sx sy
476-- un' env fn bnames (Bind x (PVTy _) sx) (Bind y (PVTy _) sy)
477-- = un' env False ((x,y):bnames) sx sy
479 -- f D unifies with t -> D. This is dubious, but it helps with type
480 -- class resolution for type classes over functions.
482 un' env fn bnames (App _ f x) (Bind n (Pi i t k) y)
483 | noOccurrence n y && injectiveApp f
484 = do ux <- un' env False bnames x y
485 uf <- un' env False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
486 (Bind n (Pi i t k) (V 1)))
487 combine env bnames ux uf
489 un' env fn bnames (Bind n (Pi i t k) y) (App _ f x)
490 | noOccurrence n y && injectiveApp f
491 = do ux <- un' env False bnames y x
492 uf <- un' env False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
493 (Bind n (Pi i t k) (V 1))) f
494 combine env bnames ux uf
496 un' env fn bnames (Bind x bx sx) (Bind y by sy)
498 = do h1 <- uB env bnames bx by
499 h2 <- un' ((x, bx) : env) False (((x,y),binderTy bx):bnames) sx sy
500 combine env bnames h1 h2
501 where sameBinder (Lam _) (Lam _) = True
502 sameBinder (Pi i _ _) (Pi i' _ _) = True
503 sameBinder _ _ = False -- never unify holes/guesses/etc
504 un' env fn bnames x y
505 | OK True <- convEq' ctxt holes x y = do sc 1; return []
506 | isUniverse x && isUniverse y = do sc 1; return []
507 | otherwise = do UI s f <- get
508 let r = recoverable (normalise ctxt env x)
509 (normalise ctxt env y)
510 let err = cantUnify from r
511 (topx, xfrom) (topy, yfrom) (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
512 if (not r) then lift $ tfail err
513 else do put (UI s ((x, y, True, env, err, from, Unify) : f))
514 return [] -- lift $ tfail err
516 unApp env fn bnames appx@(App _ fx ax) appy@(App _ fy ay)
517 -- shortcut for the common case where we just want to check the
518 -- arguments are correct
519 | (injectiveApp fx && fx == fy)
520 = un' env False bnames ax ay
521 | (injectiveApp fx && injectiveApp fy)
522 || (injectiveApp fx && metavarApp fy && ax == ay)
523 || (injectiveApp fy && metavarApp fx && ax == ay)
524 = do let (headx, _) = unApply fx
525 let (heady, _) = unApply fy
526 -- fail quickly if the heads are disjoint
527 checkHeads headx heady
529 (do hf <- un' env True bnames fx fy
530 let ax' = hnormalise hf ctxt env (substNames hf ax)
531 let ay' = hnormalise hf ctxt env (substNames hf ay)
532 ha <- un' env False bnames ax' ay'
534 combine env bnames hf ha)
535 (do ha <- un' env False bnames ax ay
536 let fx' = hnormalise ha ctxt env (substNames ha fx)
537 let fy' = hnormalise ha ctxt env (substNames ha fy)
538 hf <- un' env False bnames fx' fy'
540 combine env bnames hf ha)
541 | otherwise = unifyTmpFail appx appy
542 where hnormalise [] _ _ t = t
543 hnormalise ns ctxt env t = normalise ctxt env t
544 checkHeads (P (DCon _ _ _) x _) (P (DCon _ _ _) y _)
545 | x /= y = unifyFail appx appy
546 checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
547 | x /= y = unifyFail appx appy
548 checkHeads (P (DCon _ _ _) x _) (P (TCon _ _) y _)
549 = unifyFail appx appy
550 checkHeads (P (TCon _ _) x _) (P (DCon _ _ _) y _)
551 = unifyFail appx appy
552 checkHeads _ _ = return []
554 unArgs as [] [] = return as
555 unArgs as (x : xs) (y : ys)
556 = do let x' = hnormalise as ctxt env (substNames as x)
557 let y' = hnormalise as ctxt env (substNames as y)
558 as' <- un' env False bnames x' y'
559 vs <- combine env bnames as as'
562 numArgs tm = let (f, args) = unApply tm in length args
564 metavarApp tm = let (f, args) = unApply tm in
566 all (\x -> metavarApp x) args
567 && nub args == args) ||
569 metavarArgs tm = let (f, args) = unApply tm in
570 all (\x -> metavar x || inenv x) args
572 metavarApp' tm = let (f, args) = unApply tm in
573 all (\x -> pat x || metavar x) (f : args)
576 rigid (P (DCon _ _ _) _ _) = True
577 rigid (P (TCon _ _) _ _) = True
578 rigid t@(P Ref _ _) = inenv t || globmetavar t
579 rigid (Constant _) = True
580 rigid (App _ f a) = rigid f && rigid a
581 rigid t = not (metavar t) || globmetavar t
583 globmetavar t = case unApply t of
585 case lookupDef x ctxt of
590 metavar t = case t of
591 P _ x _ -> (x `notElem` usersupp &&
592 (x `elem` holes || holeIn env x))
596 P _ x _ -> x `elem` holes || patIn env x
599 P _ x _ -> x `elem` (map fst env)
602 notFn t = injective t || metavar t || inenv t
605 unifyTmpFail :: Term -> Term -> StateT UInfo TC [(Name, TT Name)]
608 let r = recoverable (normalise ctxt env x) (normalise ctxt env y)
609 let err = cantUnify from r
610 (topx, xfrom) (topy, yfrom)
611 (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
612 put (UI s ((topx, topy, True, env, err, from, Unify) : f))
615 -- shortcut failure, if we *know* nothing can fix it
616 unifyFail x y = do UI s f <- get
617 let r = recoverable (normalise ctxt env x) (normalise ctxt env y)
618 let err = cantUnify from r
619 (topx, xfrom) (topy, yfrom)
620 (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
621 put (UI s ((topx, topy, True, env, err, from, Unify) : f))
625 uB env bnames (Let tx vx) (Let ty vy)
626 = do h1 <- un' env False bnames tx ty
627 h2 <- un' env False bnames vx vy
629 combine env bnames h1 h2
630 uB env bnames (Guess tx vx) (Guess ty vy)
631 = do h1 <- un' env False bnames tx ty
632 h2 <- un' env False bnames vx vy
634 combine env bnames h1 h2
635 uB env bnames (Lam tx) (Lam ty) = do sc 1; un' env False bnames tx ty
636 uB env bnames (Pi _ tx _) (Pi _ ty _) = do sc 1; un' env False bnames tx ty
637 uB env bnames (Hole tx) (Hole ty) = un' env False bnames tx ty
638 uB env bnames (PVar tx) (PVar ty) = un' env False bnames tx ty
641 let r = recoverable (normalise ctxt env (binderTy x))
642 (normalise ctxt env (binderTy y))
643 let err = cantUnify from r (topx, xfrom) (topy, yfrom)
644 (CantUnify r (binderTy x, Nothing) (binderTy y, Nothing) (Msg "") (errEnv env) s)
646 put (UI s ((binderTy x, binderTy y,
648 env, err, from, Unify) : f))
649 return [] -- lift $ tfail err
651 checkCycle ns p@(x, P _ _ _) = return [p]
652 checkCycle ns (x, tm)
653 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
654 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
656 checkScope ns (x, tm) | pureTerm tm =
657-- case boundVs (envPos x 0 env) tm of
658-- [] -> return [(x, tm)]
659-- (i:_) -> lift $ tfail (UnifyScope x (fst (fst (ns!!i)))
660-- (inst ns tm) (errEnv env))
661 let v = highV (-1) tm in
663 then lift $ tfail (Msg "SCOPE ERROR")
664 else return [(x, bind v ns tm)]
665 where inst [] tm = tm
666 inst (((n, _), _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
667 checkScope ns (x, tm) = lift $ tfail (Msg "HOLE ERROR")
671 | otherwise = let ((x,y),ty) = ns!!i in
672 App MaybeHoles (Bind y (Lam ty) (bind (i-1) ns tm))
675 combine env bnames as [] = return as
676 combine env bnames as ((n, t) : bs)
677 = case lookup n as of
678 Nothing -> combine env bnames (as ++ [(n,t)]) bs
679 Just t' -> do ns <- un' env False bnames t t'
680 -- make sure there's n mapping from n in ns
681 let ns' = filter (\ (x, _) -> x/=n) ns
683 combine env bnames as (ns' ++ bs)
685boundVs :: Int -> Term -> [Int]
686boundVs i (V j) | j < i = []
688boundVs i (Bind n b sc) = boundVs (i + 1) sc
689boundVs i (App _ f x) = let fs = boundVs i f
694highV :: Int -> Term -> Int
695highV i (V j) | j > i = j
697highV i (Bind n b sc) = maximum [i, highV i (binderTy b), (highV i sc - 1)]
698highV i (App _ f x) = max (highV i f) (highV i x)
702envPos x i ((y, _) : ys) | x == y = i
703 | otherwise = envPos x (i + 1) ys
706-- If there are any clashes of constructors, deem it unrecoverable, otherwise some
707-- more work may help.
708-- FIXME: Depending on how overloading gets used, this may cause problems. Better
709-- rethink overloading properly...
710-- ASSUMPTION: inputs are in normal form
712-- Issue #1722 on the issue tracker https://github.com/idris-lang/Idris-dev/issues/1722
714recoverable t@(App _ _ _) _
715 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
716recoverable _ t@(App _ _ _)
717 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
718recoverable (P (DCon _ _ _) x _) (P (DCon _ _ _) y _) = x == y
719recoverable (P (TCon _ _) x _) (P (TCon _ _) y _) = x == y
720recoverable (TType _) (P (DCon _ _ _) y _) = False
721recoverable (UType _) (P (DCon _ _ _) y _) = False
722recoverable (Constant _) (P (DCon _ _ _) y _) = False
723recoverable (Constant x) (Constant y) = x == y
724recoverable (P (DCon _ _ _) x _) (TType _) = False
725recoverable (P (DCon _ _ _) x _) (UType _) = False
726recoverable (P (DCon _ _ _) x _) (Constant _) = False
727recoverable (TType _) (P (TCon _ _) y _) = False
728recoverable (UType _) (P (TCon _ _) y _) = False
729recoverable (Constant _) (P (TCon _ _) y _) = False
730recoverable (P (TCon _ _) x _) (TType _) = False
731recoverable (P (TCon _ _) x _) (UType _) = False
732recoverable (P (TCon _ _) x _) (Constant _) = False
733recoverable (P (DCon _ _ _) x _) (P (TCon _ _) y _) = False
734recoverable (P (TCon _ _) x _) (P (DCon _ _ _) y _) = False
735recoverable p@(TType _) (App _ f a) = recoverable p f
736recoverable p@(UType _) (App _ f a) = recoverable p f
737recoverable p@(Constant _) (App _ f a) = recoverable p f
738recoverable (App _ f a) p@(TType _) = recoverable f p
739recoverable (App _ f a) p@(UType _) = recoverable f p
740recoverable (App _ f a) p@(Constant _) = recoverable f p
741recoverable p@(P _ n _) (App _ f a) = recoverable p f
742recoverable (App _ f a) p@(P _ _ _) = recoverable f p
743recoverable (App _ f a) (App _ f' a')
744 | f == f' = recoverable a a'
745recoverable (App _ f a) (App _ f' a')
746 = recoverable f f' -- && recoverable a a'
747recoverable f (Bind _ (Pi _ _ _) sc)
748 | (P (DCon _ _ _) _ _, _) <- unApply f = False
749 | (P (TCon _ _) _ _, _) <- unApply f = False
750 | (Constant _) <- f = False
751 | TType _ <- f = False
752 | UType _ <- f = False
753recoverable (Bind _ (Pi _ _ _) sc) f
754 | (P (DCon _ _ _) _ _, _) <- unApply f = False
755 | (P (TCon _ _) _ _, _) <- unApply f = False
756 | (Constant _) <- f = False
757 | TType _ <- f = False
758 | UType _ <- f = False
759recoverable (Bind _ (Lam _) sc) f = recoverable sc f
760recoverable f (Bind _ (Lam _) sc) = recoverable f sc
761recoverable x y = True
763errEnv :: [(a, Binder b)] -> [(a, b)]
764errEnv = map (\(x, b) -> (x, binderTy b))
766holeIn :: Env -> Name -> Bool
767holeIn env n = case lookup n env of
768 Just (Hole _) -> True
769 Just (Guess _ _) -> True
772patIn :: Env -> Name -> Bool
773patIn env n = case lookup n env of
774 Just (PVar _) -> True
775 Just (PVTy _) -> True
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list