on c79fb21 help sign in
675 lines 29 kB history raw
1{-# LANGUAGE PatternGuards #-}
3module Idris.Core.Unify(match_unify, unify, Fails, FailContext(..), FailAt(..),
4 unrecoverable) where
6import Idris.Core.TT
7import Idris.Core.Evaluate
9import Control.Monad
10import Control.Monad.State.Strict
11import Data.List
12import Debug.Trace
14-- Unification is applied inside the theorem prover. We're looking for holes
15-- which can be filled in, by matching one term's normal form against another.
16-- Returns a list of hole names paired with the term which solves them, and
17-- a list of things which need to be injective.
19-- terms which need to be injective, with the things we're trying to unify
20-- at the time
22data FailAt = Match | Unify
23 deriving (Show, Eq)
25data FailContext = FailContext { fail_sourceloc :: FC,
26 fail_fn :: Name,
27 fail_param :: Name
28 }
29 deriving (Eq, Show)
31type Injs = [(TT Name, TT Name, TT Name)]
32type Fails = [(TT Name, TT Name, Env, Err, [FailContext], FailAt)]
34unrecoverable :: Fails -> Bool
35unrecoverable = any bad
36 where bad (_,_,_, err, _, _) = unrec err
38 unrec (CantUnify r _ _ _ _ _) = not r
39 unrec (At _ e) = unrec e
40 unrec (Elaborating _ _ e) = unrec e
41 unrec (ElaboratingArg _ _ _ e) = unrec e
42 unrec _ = False
44data UInfo = UI Int Fails
45 deriving Show
47data UResult a = UOK a
48 | UPartOK a
49 | UFail Err
51-- | Smart constructor for unification errors that takes into account the FailContext
52cantUnify :: [FailContext] -> Bool -> t -> t -> (Err' t) -> [(Name, t)] -> Int -> Err' t
53cantUnify [] r t1 t2 e ctxt i = CantUnify r t1 t2 e ctxt i
54cantUnify (FailContext fc f x : prev) r t1 t2 e ctxt i =
55 At fc (ElaboratingArg f x
56 (map (\(FailContext _ f' x') -> (f', x')) prev)
57 (CantUnify r t1 t2 e ctxt i))
59-- Solve metavariables by matching terms against each other
60-- Not really unification, of course!
62match_unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] -> [FailContext] ->
63 TC [(Name, TT Name)]
64match_unify ctxt env topx topy inj holes from =
65 case runStateT (un [] (renameBindersTm env topx)
66 (renameBindersTm env topy)) (UI 0 []) of
67 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v))
68 res ->
69 let topxn = renameBindersTm env (normalise ctxt env topx)
70 topyn = renameBindersTm env (normalise ctxt env topy) in
71 case runStateT (un [] topxn topyn)
72 (UI 0 []) of
73 OK (v, UI _ fails) ->
74 return (map (renameBinders env) (trimSolutions v))
75 Error e ->
76 -- just normalise the term we're matching against
77 case runStateT (un [] topxn topy)
78 (UI 0 []) of
79 OK (v, UI _ fails) ->
80 return (map (renameBinders env) (trimSolutions v))
81 _ -> tfail e
82 where
83 -- This rule is highly dubious... it certainly produces a valid answer
84 -- but it scares me. However, matching is never guaranteed to give a unique
85 -- answer, merely a valid one, so perhaps we're okay.
86 -- In other words: it may vanish without warning some day :)
87 un names x tm@(App (P _ f (Bind fn (Pi t) sc)) a)
88 | (P (DCon _ _) _ _, _) <- unApply x,
89 holeIn env f || f `elem` holes
90 = let n' = uniqueName (sMN 0 "mv") (map fst env) in
91 checkCycle names (f, Bind n' (Lam t) x)
92 un names tm@(App (P _ f (Bind fn (Pi t) sc)) a) x
93 | (P (DCon _ _) _ _, _) <- unApply x,
94 holeIn env f || f `elem` holes
95 = let n' = uniqueName fn (map fst env) in
96 checkCycle names (f, Bind n' (Lam t) x)
98 un names tx@(P _ x _) tm
99 | tx /= tm && holeIn env x || x `elem` holes
100 = do sc 1; checkCycle names (x, tm)
101 un names tm ty@(P _ y _)
102 | ty /= tm && holeIn env y || y `elem` holes
103 = do sc 1; checkCycle names (y, tm)
104 un bnames (V i) (P _ x _)
105 | length bnames > i,
106 fst (fst (bnames!!i)) == x ||
107 snd (fst (bnames!!i)) == x = do sc 1; return []
108 un bnames (P _ x _) (V i)
109 | length bnames > i,
110 fst (fst (bnames!!i)) == x ||
111 snd (fst (bnames!!i)) == x = do sc 1; return []
112 un bnames (Bind x bx sx) (Bind y by sy) | notHole bx && notHole by
113 = do h1 <- uB bnames bx by
114 h2 <- un (((x, y), binderTy bx) : bnames) sx sy
115 combine bnames h1 h2
116 un names (App fx ax) (App fy ay)
117 = do hf <- un names fx fy
118 ha <- un names ax ay
119 combine names hf ha
120 un names x y
121 | OK True <- convEq' ctxt holes x y = do sc 1; return []
122 | otherwise = do UI s f <- get
123 let r = recoverable (normalise ctxt env x)
124 (normalise ctxt env y)
125 let err = cantUnify from r
126 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
127 if (not r) then lift $ tfail err
128 else do put (UI s ((x, y, env, err, from, Match) : f))
129 lift $ tfail err
132 uB bnames (Let tx vx) (Let ty vy) = do h1 <- un bnames tx ty
133 h2 <- un bnames vx vy
134 combine bnames h1 h2
135 uB bnames (Lam tx) (Lam ty) = un bnames tx ty
136 uB bnames (Pi tx) (Pi ty) = un bnames tx ty
137 uB bnames x y = do UI s f <- get
138 let r = recoverable (normalise ctxt env (binderTy x))
139 (normalise ctxt env (binderTy y))
140 let err = cantUnify from r topx topy
141 (CantUnify r (binderTy x) (binderTy y) (Msg "") (errEnv env) s)
142 (errEnv env) s
143 put (UI s ((binderTy x, binderTy y, env, err, from, Match) : f))
144 return []
146 notHole (Hole _) = False
147 notHole _ = True
149 -- TODO: there's an annoying amount of repetition between this and the
150 -- main unification function. Consider lifting it out.
152 sc i = do UI s f <- get
153 put (UI (s+i) f)
155 unifyFail x y = do UI s f <- get
156 let r = recoverable (normalise ctxt env x)
157 (normalise ctxt env y)
158 let err = cantUnify from r
159 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
160 put (UI s ((x, y, env, err, from, Match) : f))
161 lift $ tfail err
162 combine bnames as [] = return as
163 combine bnames as ((n, t) : bs)
164 = case lookup n as of
165 Nothing -> combine bnames (as ++ [(n,t)]) bs
166 Just t' -> do ns <- un bnames t t'
167 let ns' = filter (\ (x, _) -> x/=n) ns
168 sc 1
169 combine bnames as (ns' ++ bs)
171-- substN n tm (var, sol) = (var, subst n tm sol)
173 checkCycle ns p@(x, P _ x' _) | x == x' = return []
174 checkCycle ns p@(x, P _ _ _) = return [p]
175 checkCycle ns (x, tm)
176 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
177 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
179 checkScope ns (x, tm) =
180-- case boundVs (envPos x 0 env) tm of
181-- [] -> return [(x, tm)]
182-- (i:_) -> lift $ tfail (UnifyScope x (fst (fst (ns!!i)))
183-- (inst ns tm) (errEnv env))
184 let v = highV (-1) tm in
185 if v >= length ns
186 then lift $ tfail (Msg "SCOPE ERROR")
187 else return [(x, bind v ns tm)]
188 where inst [] tm = tm
189 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
191 bind i ns tm
192 | i < 0 = tm
193 | otherwise = let ((x,y),ty) = ns!!i in
194 App (Bind y (Lam ty) (bind (i-1) ns tm))
195 (P Bound x ty)
197renameBinders env (x, t) = (x, renameBindersTm env t)
199renameBindersTm :: Env -> TT Name -> TT Name
200renameBindersTm env tm = uniqueBinders (map fst env) tm
201 where
202 uniqueBinders env (Bind n b sc)
203 | n `elem` env
204 = let n' = uniqueName n env in
205 explicitHole $ Bind n' (fmap (uniqueBinders env) b)
206 (uniqueBinders (n':env) (rename n n' sc))
207 | otherwise = Bind n (fmap (uniqueBinders (n:env)) b)
208 (uniqueBinders (n:env) sc)
209 uniqueBinders env (App f a) = App (uniqueBinders env f) (uniqueBinders env a)
210 uniqueBinders env t = t
212 rename n n' (P nt x ty) | n == x = P nt n' ty
213 rename n n' (Bind x b sc) = Bind x (fmap (rename n n') b) (rename n n' sc)
214 rename n n' (App f a) = App (rename n n' f) (rename n n' a)
215 rename n n' t = t
217 explicitHole (Bind n (Hole ty) sc)
218 = Bind n (Hole ty) (instantiate (P Bound n ty) sc)
219 explicitHole t = t
221trimSolutions ns = dropPairs ns
222 where dropPairs [] = []
223 dropPairs (n@(x, P _ x' _) : ns)
224 | x == x' = dropPairs ns
225 | otherwise
226 = n : dropPairs
227 (filter (\t -> case t of
228 (n, P _ n' _) -> not (n == x' && n' == x)
229 _ -> True) ns)
230 dropPairs (n : ns) = n : dropPairs ns
232expandLets env (x, tm) = (x, doSubst (reverse env) tm)
233 where
234 doSubst [] tm = tm
235 doSubst ((n, Let v t) : env) tm
236 = doSubst env (subst n v tm)
237 doSubst (_ : env) tm
238 = doSubst env tm
240hasv (V x) = True
241hasv (App f a) = hasv f || hasv a
242hasv (Bind x b sc) = hasv (binderTy b) || hasv sc
243hasv _ = False
245unify :: Context -> Env -> TT Name -> TT Name ->
246 [Name] -> [Name] -> [Name] -> [FailContext] ->
247 TC ([(Name, TT Name)], Fails)
248unify ctxt env topx topy inj holes usersupp from =
249-- traceWhen (hasv topx || hasv topy)
250-- ("Unifying " ++ show topx ++ "\nAND\n" ++ show topy ++ "\n") $
251 -- don't bother if topx and topy are different at the head
252 case runStateT (un False [] (renameBindersTm env topx)
253 (renameBindersTm env topy)) (UI 0 []) of
254 OK (v, UI _ []) -> return (map (renameBinders env) (trimSolutions v),
255 [])
256 res ->
257 let topxn = renameBindersTm env (normalise ctxt env topx)
258 topyn = renameBindersTm env (normalise ctxt env topy) in
259-- trace ("Unifying " ++ show (topx, topy) ++ "\n\n==>\n" ++ show (topxn, topyn) ++ "\n\n" ++ show res ++ "\n\n") $
260 case runStateT (un False [] topxn topyn)
261 (UI 0 []) of
262 OK (v, UI _ fails) ->
263 return (map (renameBinders env) (trimSolutions v),
264 reverse fails)
265-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
266 Error e -> tfail e
267 where
268 headDiff (P (DCon _ _) x _) (P (DCon _ _) y _) = x /= y
269 headDiff (P (TCon _ _) x _) (P (TCon _ _) y _) = x /= y
270 headDiff _ _ = False
272 injective (P (DCon _ _) _ _) = True
273 injective (P (TCon _ _) _ _) = True
274-- injective (App f (P _ _ _)) = injective f
275-- injective (App f (Constant _)) = injective f
276 injective (App f a) = injective f -- && injective a
277 injective _ = False
279-- injectiveVar (P _ (MN _ _) _) = True -- TMP HACK
280 injectiveVar (P _ n _) = n `elem` inj
281 injectiveVar (App f a) = injectiveVar f -- && injective a
282 injectiveVar _ = False
284 injectiveApp x = injective x || injectiveVar x
286 notP (P _ _ _) = False
287 notP _ = True
289 sc i = do UI s f <- get
290 put (UI (s+i) f)
292 errors :: StateT UInfo TC Bool
293 errors = do UI s f <- get
294 return (not (null f))
296 uplus u1 u2 = do UI s f <- get
297 r <- u1
298 UI s f' <- get
299 if (length f == length f')
300 then return r
301 else do put (UI s f); u2
303 un :: Bool -> [((Name, Name), TT Name)] -> TT Name -> TT Name ->
304 StateT UInfo
305 TC [(Name, TT Name)]
306 un = un'
307-- un fn names x y
308-- = let (xf, _) = unApply x
309-- (yf, _) = unApply y in
310-- if headDiff xf yf then unifyFail x y else
311-- uplus (un' fn names x y)
312-- (un' fn names (hnf ctxt env x) (hnf ctxt env y))
314 un' :: Bool -> [((Name, Name), TT Name)] -> TT Name -> TT Name ->
315 StateT UInfo
316 TC [(Name, TT Name)]
317 un' fn names x y | x == y = return [] -- shortcut
318 un' fn names topx@(P (DCon _ _) x _) topy@(P (DCon _ _) y _)
319 | x /= y = unifyFail topx topy
320 un' fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
321 | x /= y = unifyFail topx topy
322 un' fn names topx@(P (DCon _ _) x _) topy@(P (TCon _ _) y _)
323 = unifyFail topx topy
324 un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _) y _)
325 = unifyFail topx topy
326 un' fn names topx@(Constant _) topy@(P (TCon _ _) y _)
327 = unifyFail topx topy
328 un' fn names topx@(P (TCon _ _) x _) topy@(Constant _)
329 = unifyFail topx topy
330 un' fn bnames tx@(P _ x _) ty@(P _ y _)
331 | (x,y) `elem` map fst bnames || x == y = do sc 1; return []
332 | injective tx && not (holeIn env y || y `elem` holes)
333 = unifyTmpFail tx ty
334 | injective ty && not (holeIn env x || x `elem` holes)
335 = unifyTmpFail tx ty
336 -- pick the one bound earliest if both are holes
337 | tx /= ty && (holeIn env x || x `elem` holes)
338 && (holeIn env y || y `elem` holes)
339 = case compare (envPos 0 x env) (envPos 0 y env) of
340 LT -> do sc 1; checkCycle bnames (x, ty)
341 _ -> do sc 1; checkCycle bnames (y, tx)
342 where envPos i n ((n',_):env) | n == n' = i
343 envPos i n (_:env) = envPos (i+1) n env
344 envPos _ _ _ = 100000
345 un' fn bnames xtm@(P _ x _) tm
346 | pureTerm tm, holeIn env x || x `elem` holes
347 = do UI s f <- get
348 -- injectivity check
349 x <- checkCycle bnames (x, tm)
350 if (notP tm && fn)
351-- trace (show (x, tm, normalise ctxt env tm)) $
352-- put (UI s ((tm, topx, topy) : i) f)
353 then unifyTmpFail xtm tm
354 else do sc 1
355 return x
356 | pureTerm tm, not (injective xtm) && injective tm
357 = do checkCycle bnames (x, tm)
358 unifyTmpFail xtm tm
359 un' fn bnames tm ytm@(P _ y _)
360 | pureTerm tm, holeIn env y || y `elem` holes
361 = do UI s f <- get
362 -- injectivity check
363 x <- checkCycle bnames (y, tm)
364 if (notP tm && fn)
365-- trace (show (y, tm, normalise ctxt env tm)) $
366-- put (UI s ((tm, topx, topy) : i) f)
367 then unifyTmpFail tm ytm
368 else do sc 1
369 return x
370 | pureTerm tm, not (injective ytm) && injective tm
371 = do checkCycle bnames (y, tm)
372 unifyTmpFail tm ytm
373 un' fn bnames (V i) (P _ x _)
374 | length bnames > i,
375 fst ((map fst bnames)!!i) == x ||
376 snd ((map fst bnames)!!i) == x = do sc 1; return []
377 un' fn bnames (P _ x _) (V i)
378 | length bnames > i,
379 fst ((map fst bnames)!!i) == x ||
380 snd ((map fst bnames)!!i) == x = do sc 1; return []
382 un' fn names topx@(Bind n (Hole t) sc) y = unifyTmpFail topx y
383 un' fn names x topy@(Bind n (Hole t) sc) = unifyTmpFail x topy
385 un' fn bnames appx@(App _ _) appy@(App _ _)
386 = unApp fn bnames appx appy
387-- = uplus (unApp fn bnames appx appy)
388-- (unifyTmpFail appx appy) -- take the whole lot
390 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
391 | n == n' = un' False bnames x y
392 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
393 | n == n' = un' False bnames x y
394 un' fn bnames x (Bind n (Lam t) (App y (V 0)))
395 = un' False bnames x y
396 un' fn bnames (Bind n (Lam t) (App x (V 0))) y
397 = un' False bnames x y
398-- un' fn bnames (Bind x (PVar _) sx) (Bind y (PVar _) sy)
399-- = un' False ((x,y):bnames) sx sy
400-- un' fn bnames (Bind x (PVTy _) sx) (Bind y (PVTy _) sy)
401-- = un' False ((x,y):bnames) sx sy
403 -- f D unifies with t -> D. This is dubious, but it helps with type
404 -- class resolution for type classes over functions.
406 un' fn bnames (App f x) (Bind n (Pi t) y)
407 | noOccurrence n y && injectiveApp f
408 = do ux <- un' False bnames x y
409 uf <- un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
410 (Bind n (Pi t) (V 1)))
411 combine bnames ux uf
413 un' fn bnames (Bind n (Pi t) y) (App f x)
414 | noOccurrence n y && injectiveApp f
415 = do ux <- un' False bnames y x
416 uf <- un' False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
417 (Bind n (Pi t) (V 1))) f
418 combine bnames ux uf
420 un' fn bnames (Bind x bx sx) (Bind y by sy)
421 | sameBinder bx by
422 = do h1 <- uB bnames bx by
423 h2 <- un' False (((x,y),binderTy bx):bnames) sx sy
424 combine bnames h1 h2
425 where sameBinder (Lam _) (Lam _) = True
426 sameBinder (Pi _) (Pi _) = True
427 sameBinder _ _ = False
428 un' fn bnames x y
429 | OK True <- convEq' ctxt holes x y = do sc 1; return []
430 | otherwise = do UI s f <- get
431 let r = recoverable (normalise ctxt env x)
432 (normalise ctxt env y)
433 let err = cantUnify from r
434 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
435 if (not r) then lift $ tfail err
436 else do put (UI s ((x, y, env, err, from, Unify) : f))
437 return [] -- lift $ tfail err
439 unApp fn bnames appx@(App fx ax) appy@(App fy ay)
440 | (injectiveApp fx && injectiveApp fy)
441 || (injectiveApp fx && metavarApp fy && ax == ay)
442 || (injectiveApp fy && metavarApp fx && ax == ay)
443 = do let (headx, _) = unApply fx
444 let (heady, _) = unApply fy
445 -- fail quickly if the heads are disjoint
446 checkHeads headx heady
447 uplus
448 (do hf <- un' True bnames fx fy
449 let ax' = hnormalise hf ctxt env (substNames hf ax)
450 let ay' = hnormalise hf ctxt env (substNames hf ay)
451 ha <- un' False bnames ax' ay'
452 sc 1
453 combine bnames hf ha)
454 (do ha <- un' False bnames ax ay
455 let fx' = hnormalise ha ctxt env (substNames ha fx)
456 let fy' = hnormalise ha ctxt env (substNames ha fy)
457 hf <- un' False bnames fx' fy'
458 sc 1
459 combine bnames hf ha)
460 | otherwise = unifyTmpFail appx appy
461 where hnormalise [] _ _ t = t
462 hnormalise ns ctxt env t = normalise ctxt env t
463 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
464 | x /= y = unifyFail appx appy
465 checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
466 | x /= y = unifyFail appx appy
467 checkHeads (P (DCon _ _) x _) (P (TCon _ _) y _)
468 = unifyFail appx appy
469 checkHeads (P (TCon _ _) x _) (P (DCon _ _) y _)
470 = unifyFail appx appy
471 checkHeads _ _ = return []
473 unArgs as [] [] = return as
474 unArgs as (x : xs) (y : ys)
475 = do let x' = hnormalise as ctxt env (substNames as x)
476 let y' = hnormalise as ctxt env (substNames as y)
477 as' <- un' False bnames x' y'
478 vs <- combine bnames as as'
479 unArgs vs xs ys
481 numArgs tm = let (f, args) = unApply tm in length args
483 metavarApp tm = let (f, args) = unApply tm in
484 (metavar f &&
485 all (\x -> metavarApp x) args
486 && nub args == args) ||
487 globmetavar tm
488 metavarArgs tm = let (f, args) = unApply tm in
489 all (\x -> metavar x || inenv x) args
490 && nub args == args
491 metavarApp' tm = let (f, args) = unApply tm in
492 all (\x -> pat x || metavar x) (f : args)
493 && nub args == args
495 rigid (P (DCon _ _) _ _) = True
496 rigid (P (TCon _ _) _ _) = True
497 rigid t@(P Ref _ _) = inenv t || globmetavar t
498 rigid (Constant _) = True
499 rigid (App f a) = rigid f && rigid a
500 rigid t = not (metavar t) || globmetavar t
502 globmetavar t = case unApply t of
503 (P _ x _, _) ->
504 case lookupDef x ctxt of
505 [TyDecl _ _] -> True
506 _ -> False
507 _ -> False
509 metavar t = case t of
510 P _ x _ -> (x `notElem` usersupp &&
511 (x `elem` holes || holeIn env x))
512 || globmetavar t
513 _ -> False
514 pat t = case t of
515 P _ x _ -> x `elem` holes || patIn env x
516 _ -> False
517 inenv t = case t of
518 P _ x _ -> x `elem` (map fst env)
519 _ -> False
521 notFn t = injective t || metavar t || inenv t
524 unifyTmpFail :: Term -> Term -> StateT UInfo TC [(Name, TT Name)]
525 unifyTmpFail x y
526 = do UI s f <- get
527 let r = recoverable (normalise ctxt env x) (normalise ctxt env y)
528 let err = cantUnify from r
529 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
530 put (UI s ((topx, topy, env, err, from, Unify) : f))
531 return []
533 -- shortcut failure, if we *know* nothing can fix it
534 unifyFail x y = do UI s f <- get
535 let r = recoverable (normalise ctxt env x) (normalise ctxt env y)
536 let err = cantUnify from r
537 topx topy (CantUnify r x y (Msg "") (errEnv env) s) (errEnv env) s
538 put (UI s ((topx, topy, env, err, from, Unify) : f))
539 lift $ tfail err
542 uB bnames (Let tx vx) (Let ty vy)
543 = do h1 <- un' False bnames tx ty
544 h2 <- un' False bnames vx vy
545 sc 1
546 combine bnames h1 h2
547 uB bnames (Guess tx vx) (Guess ty vy)
548 = do h1 <- un' False bnames tx ty
549 h2 <- un' False bnames vx vy
550 sc 1
551 combine bnames h1 h2
552 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
553 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
554 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
555 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
556 uB bnames x y = do UI s f <- get
557 let r = recoverable (normalise ctxt env (binderTy x))
558 (normalise ctxt env (binderTy y))
559 let err = cantUnify from r topx topy
560 (CantUnify r (binderTy x) (binderTy y) (Msg "") (errEnv env) s)
561 (errEnv env) s
562 put (UI s ((binderTy x, binderTy y, env, err, from, Unify) : f))
563 return [] -- lift $ tfail err
565 checkCycle ns p@(x, P _ _ _) = return [p]
566 checkCycle ns (x, tm)
567 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
568 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
570 checkScope ns (x, tm) | pureTerm tm =
571-- case boundVs (envPos x 0 env) tm of
572-- [] -> return [(x, tm)]
573-- (i:_) -> lift $ tfail (UnifyScope x (fst (fst (ns!!i)))
574-- (inst ns tm) (errEnv env))
575 let v = highV (-1) tm in
576 if v >= length ns
577 then lift $ tfail (Msg "SCOPE ERROR")
578 else return [(x, bind v ns tm)]
579 where inst [] tm = tm
580 inst (((n, _), _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
581 checkScope ns (x, tm) = lift $ tfail (Msg "HOLE ERROR")
583 bind i ns tm
584 | i < 0 = tm
585 | otherwise = let ((x,y),ty) = ns!!i in
586 App (Bind y (Lam ty) (bind (i-1) ns tm))
587 (P Bound x ty)
589 combineArgs bnames args = ca [] args where
590 ca acc [] = return acc
591 ca acc (x : xs) = do x' <- combine bnames acc x
592 ca x' xs
594 combine bnames as [] = return as
595 combine bnames as ((n, t) : bs)
596 = case lookup n as of
597 Nothing -> combine bnames (as ++ [(n,t)]) bs
598 Just t' -> do ns <- un' False bnames t t'
599 -- make sure there's n mapping from n in ns
600 let ns' = filter (\ (x, _) -> x/=n) ns
601 sc 1
602 combine bnames as (ns' ++ bs)
604boundVs :: Int -> Term -> [Int]
605boundVs i (V j) | j < i = []
606 | otherwise = [j]
607boundVs i (Bind n b sc) = boundVs (i + 1) sc
608boundVs i (App f x) = let fs = boundVs i f
609 xs = boundVs i x in
610 nub (fs ++ xs)
611boundVs i _ = []
613highV :: Int -> Term -> Int
614highV i (V j) | j > i = j
615 | otherwise = i
616highV i (Bind n b sc) = maximum [i, highV i (binderTy b), (highV i sc - 1)]
617highV i (App f x) = max (highV i f) (highV i x)
618highV i _ = i
620envPos x i [] = 0
621envPos x i ((y, _) : ys) | x == y = i
622 | otherwise = envPos x (i + 1) ys
625-- If there are any clashes of constructors, deem it unrecoverable, otherwise some
626-- more work may help.
627-- FIXME: Depending on how overloading gets used, this may cause problems. Better
628-- rethink overloading properly...
629-- ASSUMPTION: inputs are in normal form
631recoverable t@(App _ _) _
632 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
633recoverable _ t@(App _ _)
634 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
635recoverable (P (DCon _ _) x _) (P (DCon _ _) y _) = x == y
636recoverable (P (TCon _ _) x _) (P (TCon _ _) y _) = x == y
637recoverable (Constant _) (P (DCon _ _) y _) = False
638recoverable (Constant x) (Constant y) = x == y
639recoverable (P (DCon _ _) x _) (Constant _) = False
640recoverable (Constant _) (P (TCon _ _) y _) = False
641recoverable (P (TCon _ _) x _) (Constant _) = False
642recoverable (P (DCon _ _) x _) (P (TCon _ _) y _) = False
643recoverable (P (TCon _ _) x _) (P (DCon _ _) y _) = False
644recoverable p@(Constant _) (App f a) = recoverable p f
645recoverable (App f a) p@(Constant _) = recoverable f p
646recoverable p@(P _ n _) (App f a) = recoverable p f
647recoverable (App f a) p@(P _ _ _) = recoverable f p
648recoverable (App f a) (App f' a')
649 | f == f' = recoverable a a'
650recoverable (App f a) (App f' a')
651 = recoverable f f' -- && recoverable a a'
652recoverable f (Bind _ (Pi _) sc)
653 | (P (DCon _ _) _ _, _) <- unApply f = False
654 | (P (TCon _ _) _ _, _) <- unApply f = False
655recoverable (Bind _ (Pi _) sc) f
656 | (P (DCon _ _) _ _, _) <- unApply f = False
657 | (P (TCon _ _) _ _, _) <- unApply f = False
658recoverable (Bind _ (Lam _) sc) f = recoverable sc f
659recoverable f (Bind _ (Lam _) sc) = recoverable f sc
660recoverable x y = True
662errEnv = map (\(x, b) -> (x, binderTy b))
664holeIn :: Env -> Name -> Bool
665holeIn env n = case lookup n env of
666 Just (Hole _) -> True
667 Just (Guess _ _) -> True
668 _ -> False
670patIn :: Env -> Name -> Bool
671patIn env n = case lookup n env of
672 Just (PVar _) -> True
673 Just (PVTy _) -> True
674 _ -> False