on bf4e535 help sign in
732 lines 33 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, -- unification error
33 Bool, -- ready to retry yet
34 Env, Err, [FailContext], FailAt)]
36unrecoverable :: Fails -> Bool
37unrecoverable = any bad
38 where bad (_,_,_,_, err, _, _) = unrec err
40 unrec (CantUnify r _ _ _ _ _) = not r
41 unrec (At _ e) = unrec e
42 unrec (Elaborating _ _ e) = unrec e
43 unrec (ElaboratingArg _ _ _ e) = unrec e
44 unrec _ = False
46data UInfo = UI Int Fails
47 deriving Show
49data UResult a = UOK a
50 | UPartOK a
51 | UFail Err
53-- | Smart constructor for unification errors that takes into account the FailContext
54cantUnify :: [FailContext] -> Bool -> (t, Maybe Provenance) -> (t, Maybe Provenance) -> (Err' t) -> [(Name, t)] -> Int -> Err' t
55cantUnify [] r t1 t2 e ctxt i = CantUnify r t1 t2 e ctxt i
56cantUnify (FailContext fc f x : prev) r t1 t2 e ctxt i =
57 At fc (ElaboratingArg f x
58 (map (\(FailContext _ f' x') -> (f', x')) prev)
59 (CantUnify r t1 t2 e ctxt i))
61-- Solve metavariables by matching terms against each other
62-- Not really unification, of course!
64match_unify :: Context -> Env ->
65 (TT Name, Maybe Provenance) ->
66 (TT Name, Maybe Provenance) -> [Name] -> [Name] -> [FailContext] ->
67 TC [(Name, TT Name)]
68match_unify ctxt env (topx, xfrom) (topy, yfrom) inj holes from =
69 case runStateT (un [] (renameBindersTm env topx)
70 (renameBindersTm env topy)) (UI 0 []) of
71 OK (v, UI _ []) ->
72 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
73 return (map (renameBinders env) v')
74 res ->
75 let topxn = renameBindersTm env (normalise ctxt env topx)
76 topyn = renameBindersTm env (normalise ctxt env topy) in
77 case runStateT (un [] topxn topyn)
78 (UI 0 []) of
79 OK (v, UI _ fails) ->
80 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
81 return (map (renameBinders env) v')
82 Error e ->
83 -- just normalise the term we're matching against
84 case runStateT (un [] topxn topy)
85 (UI 0 []) of
86 OK (v, UI _ fails) ->
87 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
88 return (map (renameBinders env) v')
89 _ -> tfail e
90 where
91 un :: [((Name, Name), TT Name)] -> TT Name -> TT Name ->
92 StateT UInfo
93 TC [(Name, TT Name)]
95 -- This rule is highly dubious... it certainly produces a valid answer
96 -- but it scares me. However, matching is never guaranteed to give a unique
97 -- answer, merely a valid one, so perhaps we're okay.
98 -- In other words: it may vanish without warning some day :)
99 un names x tm@(App (P _ f (Bind fn (Pi _ t _) sc)) a)
100 | (P (TCon _ _) _ _, _) <- unApply x,
101 holeIn env f || f `elem` holes
102 = let n' = uniqueName (sMN 0 "mv") (map fst env) in
103 checkCycle names (f, Bind n' (Lam t) x)
104 un names tm@(App (P _ f (Bind fn (Pi _ t _) sc)) a) x
105 | (P (TCon _ _) _ _, _) <- unApply x,
106 holeIn env f || f `elem` holes
107 = let n' = uniqueName fn (map fst env) in
108 checkCycle names (f, Bind n' (Lam t) x)
109 un names x tm@(App (P _ f (Bind fn (Pi _ t _) sc)) a)
110 | (P (DCon _ _ _) _ _, _) <- unApply x,
111 holeIn env f || f `elem` holes
112 = let n' = uniqueName (sMN 0 "mv") (map fst env) in
113 checkCycle names (f, Bind n' (Lam t) x)
114 un names tm@(App (P _ f (Bind fn (Pi _ t _) sc)) a) x
115 | (P (DCon _ _ _) _ _, _) <- unApply x,
116 holeIn env f || f `elem` holes
117 = let n' = uniqueName fn (map fst env) in
118 checkCycle names (f, Bind n' (Lam t) x)
120 un names tx@(P _ x _) tm
121 | tx /= tm && holeIn env x || x `elem` holes
122 = do sc 1; checkCycle names (x, tm)
123 un names tm ty@(P _ y _)
124 | ty /= tm && holeIn env y || y `elem` holes
125 = do sc 1; checkCycle names (y, tm)
126 un bnames (V i) (P _ x _)
127 | length bnames > i,
128 fst (fst (bnames!!i)) == x ||
129 snd (fst (bnames!!i)) == x = do sc 1; return []
130 un bnames (P _ x _) (V i)
131 | length bnames > i,
132 fst (fst (bnames!!i)) == x ||
133 snd (fst (bnames!!i)) == x = do sc 1; return []
134 un bnames (Bind x bx sx) (Bind y by sy) | notHole bx && notHole by
135 = do h1 <- uB bnames bx by
136 h2 <- un (((x, y), binderTy bx) : bnames) sx sy
137 combine bnames h1 h2
138 un names (App fx ax) (App fy ay)
139 = do hf <- un names fx fy
140 ha <- un names ax ay
141 combine names hf ha
142 un names x y
143 | OK True <- convEq' ctxt holes x y = do sc 1; return []
144 | otherwise = do UI s f <- get
145 let r = recoverable (normalise ctxt env x)
146 (normalise ctxt env y)
147 let err = cantUnify from r
148 (topx, xfrom) (topy, yfrom) (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
149 if (not r) then lift $ tfail err
150 else do put (UI s ((x, y, True, env, err, from, Match) : f))
151 lift $ tfail err
154 uB bnames (Let tx vx) (Let ty vy) = do h1 <- un bnames tx ty
155 h2 <- un bnames vx vy
156 combine bnames h1 h2
157 uB bnames (Lam tx) (Lam ty) = un bnames tx ty
158 uB bnames (Pi i tx _) (Pi i' ty _) = un bnames tx ty
159 uB bnames x y = do UI s f <- get
160 let r = recoverable (normalise ctxt env (binderTy x))
161 (normalise ctxt env (binderTy y))
162 let err = cantUnify from r (topx, xfrom) (topy, yfrom)
163 (CantUnify r (binderTy x, Nothing)
164 (binderTy y, Nothing) (Msg "") (errEnv env) s)
165 (errEnv env) s
166 put (UI s ((binderTy x, binderTy y,
167 False,
168 env, err, from, Match) : f))
169 return []
171 notHole (Hole _) = False
172 notHole _ = True
174 -- TODO: there's an annoying amount of repetition between this and the
175 -- main unification function. Consider lifting it out.
176 -- Issue #1721 on the issue tracker: https://github.com/idris-lang/Idris-dev/issues/1721
178 sc i = do UI s f <- get
179 put (UI (s+i) f)
181 unifyFail x y = do UI s f <- get
182 let r = recoverable (normalise ctxt env x)
183 (normalise ctxt env y)
184 let err = cantUnify from r
185 (topx, xfrom) (topy, yfrom)
186 (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
187 put (UI s ((x, y, True, env, err, from, Match) : f))
188 lift $ tfail err
189 combine bnames as [] = return as
190 combine bnames as ((n, t) : bs)
191 = case lookup n as of
192 Nothing -> combine bnames (as ++ [(n,t)]) bs
193 Just t' -> do ns <- un bnames t t'
194 let ns' = filter (\ (x, _) -> x/=n) ns
195 sc 1
196 combine bnames as (ns' ++ bs)
198-- substN n tm (var, sol) = (var, subst n tm sol)
200 checkCycle ns p@(x, P _ x' _) | x == x' = return []
201 checkCycle ns p@(x, P _ _ _) = return [p]
202 checkCycle ns (x, tm)
203 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
204 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
206 checkScope ns (x, tm) =
207-- case boundVs (envPos x 0 env) tm of
208-- [] -> return [(x, tm)]
209-- (i:_) -> lift $ tfail (UnifyScope x (fst (fst (ns!!i)))
210-- (inst ns tm) (errEnv env))
211 let v = highV (-1) tm in
212 if v >= length ns
213 then lift $ tfail (Msg "SCOPE ERROR")
214 else return [(x, bind v ns tm)]
215 where inst [] tm = tm
216 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
218 bind i ns tm
219 | i < 0 = tm
220 | otherwise = let ((x,y),ty) = ns!!i in
221 App (Bind y (Lam ty) (bind (i-1) ns tm))
222 (P Bound x ty)
224renameBinders env (x, t) = (x, renameBindersTm env t)
226renameBindersTm :: Env -> TT Name -> TT Name
227renameBindersTm env tm = uniqueBinders (map fst env) tm
228 where
229 uniqueBinders env (Bind n b sc)
230 | n `elem` env
231 = let n' = uniqueName n env in
232 explicitHole $ Bind n' (fmap (uniqueBinders env) b)
233 (uniqueBinders (n':env) (rename n n' sc))
234 | otherwise = Bind n (fmap (uniqueBinders (n:env)) b)
235 (uniqueBinders (n:env) sc)
236 uniqueBinders env (App f a) = App (uniqueBinders env f) (uniqueBinders env a)
237 uniqueBinders env t = t
239 rename n n' (P nt x ty) | n == x = P nt n' ty
240 rename n n' (Bind x b sc) = Bind x (fmap (rename n n') b) (rename n n' sc)
241 rename n n' (App f a) = App (rename n n' f) (rename n n' a)
242 rename n n' t = t
244 explicitHole (Bind n (Hole ty) sc)
245 = Bind n (Hole ty) (instantiate (P Bound n ty) sc)
246 explicitHole t = t
248trimSolutions (topx, xfrom) (topy, yfrom) from env topns = followSols [] (dropPairs topns)
249 where dropPairs [] = []
250 dropPairs (n@(x, P _ x' _) : ns)
251 | x == x' = dropPairs ns
252 | otherwise
253 = n : dropPairs
254 (filter (\t -> case t of
255 (n, P _ n' _) -> not (n == x' && n' == x)
256 _ -> True) ns)
257 dropPairs (n : ns) = n : dropPairs ns
259 followSols vs [] = return []
260 followSols vs ((n, P _ t _) : ns)
261 | Just t' <- lookup t ns
262 = do vs' <- case t' of
263 P _ tn _ ->
264 if (n, tn) `elem` vs then -- cycle
265 tfail (cantUnify from False (topx, xfrom) (topy, yfrom)
266 (Msg "") (errEnv env) 0)
267 else return ((n, tn) : vs)
268 _ -> return vs
269 followSols vs' ((n, t') : ns)
270 followSols vs (n : ns) = do ns' <- followSols vs ns
271 return $ n : ns'
273expandLets env (x, tm) = (x, doSubst (reverse env) tm)
274 where
275 doSubst [] tm = tm
276 doSubst ((n, Let v t) : env) tm
277 = doSubst env (subst n v tm)
278 doSubst (_ : env) tm
279 = doSubst env tm
281hasv :: TT Name -> Bool
282hasv (V x) = True
283hasv (App f a) = hasv f || hasv a
284hasv (Bind x b sc) = hasv (binderTy b) || hasv sc
285hasv _ = False
287unify :: Context -> Env ->
288 (TT Name, Maybe Provenance) ->
289 (TT Name, Maybe Provenance) ->
290 [Name] -> [Name] -> [Name] -> [FailContext] ->
291 TC ([(Name, TT Name)], Fails)
292unify ctxt env (topx, xfrom) (topy, yfrom) inj holes usersupp from =
293-- traceWhen (hasv topx || hasv topy)
294-- ("Unifying " ++ show topx ++ "\nAND\n" ++ show topy ++ "\n") $
295 -- don't bother if topx and topy are different at the head
296 case runStateT (un False [] (renameBindersTm env topx)
297 (renameBindersTm env topy)) (UI 0 []) of
298 OK (v, UI _ []) -> do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
299 return (map (renameBinders env) v', [])
300 res ->
301 let topxn = renameBindersTm env (normalise ctxt env topx)
302 topyn = renameBindersTm env (normalise ctxt env topy) in
303-- trace ("Unifying " ++ show (topx, topy) ++ "\n\n==>\n" ++ show (topxn, topyn) ++ "\n\n" ++ show res ++ "\n\n") $
304 case runStateT (un False [] topxn topyn)
305 (UI 0 []) of
306 OK (v, UI _ fails) ->
307 do v' <- trimSolutions (topx, xfrom) (topy, yfrom) from env v
308 return (map (renameBinders env) v', reverse fails)
309-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
310 Error e -> tfail e
311 where
312 headDiff (P (DCon _ _ _) x _) (P (DCon _ _ _) y _) = x /= y
313 headDiff (P (TCon _ _) x _) (P (TCon _ _) y _) = x /= y
314 headDiff _ _ = False
316 injective (P (DCon _ _ _) _ _) = True
317 injective (P (TCon _ _) _ _) = True
318-- injective (App f (P _ _ _)) = injective f
319-- injective (App f (Constant _)) = injective f
320 injective (App f a) = injective f -- && injective a
321 injective _ = False
323-- injectiveVar (P _ (MN _ _) _) = True -- TMP HACK
324 injectiveVar (P _ n _) = n `elem` inj
325 injectiveVar (App f a) = injectiveVar f -- && injective a
326 injectiveVar _ = False
328 injectiveApp x = injective x || injectiveVar x
330 notP (P _ _ _) = False
331 notP _ = True
333 sc i = do UI s f <- get
334 put (UI (s+i) f)
336 errors :: StateT UInfo TC Bool
337 errors = do UI s f <- get
338 return (not (null f))
340 uplus u1 u2 = do UI s f <- get
341 r <- u1
342 UI s f' <- get
343 if (length f == length f')
344 then return r
345 else do put (UI s f); u2
347 un :: Bool -> [((Name, Name), TT Name)] -> TT Name -> TT Name ->
348 StateT UInfo
349 TC [(Name, TT Name)]
350 un = un'
351-- un fn names x y
352-- = let (xf, _) = unApply x
353-- (yf, _) = unApply y in
354-- if headDiff xf yf then unifyFail x y else
355-- uplus (un' fn names x y)
356-- (un' fn names (hnf ctxt env x) (hnf ctxt env y))
358 un' :: Bool -> [((Name, Name), TT Name)] -> TT Name -> TT Name ->
359 StateT UInfo
360 TC [(Name, TT Name)]
361 un' fn names x y | x == y = return [] -- shortcut
362 un' fn names topx@(P (DCon _ _ _) x _) topy@(P (DCon _ _ _) y _)
363 | x /= y = unifyFail topx topy
364 un' fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
365 | x /= y = unifyFail topx topy
366 un' fn names topx@(P (DCon _ _ _) x _) topy@(P (TCon _ _) y _)
367 = unifyFail topx topy
368 un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _ _) y _)
369 = unifyFail topx topy
370 un' fn names topx@(Constant _) topy@(P (TCon _ _) y _)
371 = unifyFail topx topy
372 un' fn names topx@(P (TCon _ _) x _) topy@(Constant _)
373 = unifyFail topx topy
374 un' fn bnames tx@(P _ x _) ty@(P _ y _)
375 | (x,y) `elem` map fst bnames || x == y = do sc 1; return []
376 | injective tx && not (holeIn env y || y `elem` holes)
377 = unifyTmpFail tx ty
378 | injective ty && not (holeIn env x || x `elem` holes)
379 = unifyTmpFail tx ty
380 -- pick the one bound earliest if both are holes
381 | tx /= ty && (holeIn env x || x `elem` holes)
382 && (holeIn env y || y `elem` holes)
383 = case compare (envPos 0 x env) (envPos 0 y env) of
384 LT -> do sc 1; checkCycle bnames (x, ty)
385 _ -> do sc 1; checkCycle bnames (y, tx)
386 where envPos i n ((n',_):env) | n == n' = i
387 envPos i n (_:env) = envPos (i+1) n env
388 envPos _ _ _ = 100000
389 un' fn bnames xtm@(P _ x _) tm
390 | pureTerm tm, holeIn env x || x `elem` holes
391 = do UI s f <- get
392 -- injectivity check
393 x <- checkCycle bnames (x, tm)
394 if (notP tm && fn)
395-- trace (show (x, tm, normalise ctxt env tm)) $
396-- put (UI s ((tm, topx, topy) : i) f)
397 then unifyTmpFail xtm tm
398 else do sc 1
399 return x
400 | pureTerm tm, not (injective xtm) && injective tm
401 = do checkCycle bnames (x, tm)
402 unifyTmpFail xtm tm
403 un' fn bnames tm ytm@(P _ y _)
404 | pureTerm tm, holeIn env y || y `elem` holes
405 = do UI s f <- get
406 -- injectivity check
407 x <- checkCycle bnames (y, tm)
408 if (notP tm && fn)
409-- trace (show (y, tm, normalise ctxt env tm)) $
410-- put (UI s ((tm, topx, topy) : i) f)
411 then unifyTmpFail tm ytm
412 else do sc 1
413 return x
414 | pureTerm tm, not (injective ytm) && injective tm
415 = do checkCycle bnames (y, tm)
416 unifyTmpFail tm ytm
417 un' fn bnames (V i) (P _ x _)
418 | length bnames > i,
419 fst ((map fst bnames)!!i) == x ||
420 snd ((map fst bnames)!!i) == x = do sc 1; return []
421 un' fn bnames (P _ x _) (V i)
422 | length bnames > i,
423 fst ((map fst bnames)!!i) == x ||
424 snd ((map fst bnames)!!i) == x = do sc 1; return []
426 un' fn names topx@(Bind n (Hole t) sc) y = unifyTmpFail topx y
427 un' fn names x topy@(Bind n (Hole t) sc) = unifyTmpFail x topy
429 un' fn bnames appx@(App _ _) appy@(App _ _)
430 = unApp fn bnames appx appy
431-- = uplus (unApp fn bnames appx appy)
432-- (unifyTmpFail appx appy) -- take the whole lot
434 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
435 | n == n' = un' False bnames x y
436 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
437 | n == n' = un' False bnames x y
438 un' fn bnames x (Bind n (Lam t) (App y (V 0)))
439 = un' False bnames x y
440 un' fn bnames (Bind n (Lam t) (App x (V 0))) y
441 = un' False bnames x y
442-- un' fn bnames (Bind x (PVar _) sx) (Bind y (PVar _) sy)
443-- = un' False ((x,y):bnames) sx sy
444-- un' fn bnames (Bind x (PVTy _) sx) (Bind y (PVTy _) sy)
445-- = un' False ((x,y):bnames) sx sy
447 -- f D unifies with t -> D. This is dubious, but it helps with type
448 -- class resolution for type classes over functions.
450 un' fn bnames (App f x) (Bind n (Pi i t k) y)
451 | noOccurrence n y && injectiveApp f
452 = do ux <- un' False bnames x y
453 uf <- un' False bnames f (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
454 (Bind n (Pi i t k) (V 1)))
455 combine bnames ux uf
457 un' fn bnames (Bind n (Pi i t k) y) (App f x)
458 | noOccurrence n y && injectiveApp f
459 = do ux <- un' False bnames y x
460 uf <- un' False bnames (Bind (sMN 0 "uv") (Lam (TType (UVar 0)))
461 (Bind n (Pi i t k) (V 1))) f
462 combine bnames ux uf
464 un' fn bnames (Bind x bx sx) (Bind y by sy)
465 | sameBinder bx by
466 = do h1 <- uB bnames bx by
467 h2 <- un' False (((x,y),binderTy bx):bnames) sx sy
468 combine bnames h1 h2
469 where sameBinder (Lam _) (Lam _) = True
470 sameBinder (Pi i _ _) (Pi i' _ _) = True
471 sameBinder _ _ = False -- never unify holes/guesses/etc
472 un' fn bnames x y
473 | OK True <- convEq' ctxt holes x y = do sc 1; return []
474 | isUniverse x && isUniverse y = do sc 1; return []
475 | otherwise = do UI s f <- get
476 let r = recoverable (normalise ctxt env x)
477 (normalise ctxt env y)
478 let err = cantUnify from r
479 (topx, xfrom) (topy, yfrom) (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
480 if (not r) then lift $ tfail err
481 else do put (UI s ((x, y, True, env, err, from, Unify) : f))
482 return [] -- lift $ tfail err
484 unApp fn bnames appx@(App fx ax) appy@(App fy ay)
485 -- shortcut for the common case where we just want to check the
486 -- arguments are correct
487 | (injectiveApp fx && fx == fy)
488 = un' False bnames ax ay
489 | (injectiveApp fx && injectiveApp fy)
490 || (injectiveApp fx && metavarApp fy && ax == ay)
491 || (injectiveApp fy && metavarApp fx && ax == ay)
492 = do let (headx, _) = unApply fx
493 let (heady, _) = unApply fy
494 -- fail quickly if the heads are disjoint
495 checkHeads headx heady
496 uplus
497 (do hf <- un' True bnames fx fy
498 let ax' = hnormalise hf ctxt env (substNames hf ax)
499 let ay' = hnormalise hf ctxt env (substNames hf ay)
500 ha <- un' False bnames ax' ay'
501 sc 1
502 combine bnames hf ha)
503 (do ha <- un' False bnames ax ay
504 let fx' = hnormalise ha ctxt env (substNames ha fx)
505 let fy' = hnormalise ha ctxt env (substNames ha fy)
506 hf <- un' False bnames fx' fy'
507 sc 1
508 combine bnames hf ha)
509 | otherwise = unifyTmpFail appx appy
510 where hnormalise [] _ _ t = t
511 hnormalise ns ctxt env t = normalise ctxt env t
512 checkHeads (P (DCon _ _ _) x _) (P (DCon _ _ _) y _)
513 | x /= y = unifyFail appx appy
514 checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
515 | x /= y = unifyFail appx appy
516 checkHeads (P (DCon _ _ _) x _) (P (TCon _ _) y _)
517 = unifyFail appx appy
518 checkHeads (P (TCon _ _) x _) (P (DCon _ _ _) y _)
519 = unifyFail appx appy
520 checkHeads _ _ = return []
522 unArgs as [] [] = return as
523 unArgs as (x : xs) (y : ys)
524 = do let x' = hnormalise as ctxt env (substNames as x)
525 let y' = hnormalise as ctxt env (substNames as y)
526 as' <- un' False bnames x' y'
527 vs <- combine bnames as as'
528 unArgs vs xs ys
530 numArgs tm = let (f, args) = unApply tm in length args
532 metavarApp tm = let (f, args) = unApply tm in
533 (metavar f &&
534 all (\x -> metavarApp x) args
535 && nub args == args) ||
536 globmetavar tm
537 metavarArgs tm = let (f, args) = unApply tm in
538 all (\x -> metavar x || inenv x) args
539 && nub args == args
540 metavarApp' tm = let (f, args) = unApply tm in
541 all (\x -> pat x || metavar x) (f : args)
542 && nub args == args
544 rigid (P (DCon _ _ _) _ _) = True
545 rigid (P (TCon _ _) _ _) = True
546 rigid t@(P Ref _ _) = inenv t || globmetavar t
547 rigid (Constant _) = True
548 rigid (App f a) = rigid f && rigid a
549 rigid t = not (metavar t) || globmetavar t
551 globmetavar t = case unApply t of
552 (P _ x _, _) ->
553 case lookupDef x ctxt of
554 [TyDecl _ _] -> True
555 _ -> False
556 _ -> False
558 metavar t = case t of
559 P _ x _ -> (x `notElem` usersupp &&
560 (x `elem` holes || holeIn env x))
561 || globmetavar t
562 _ -> False
563 pat t = case t of
564 P _ x _ -> x `elem` holes || patIn env x
565 _ -> False
566 inenv t = case t of
567 P _ x _ -> x `elem` (map fst env)
568 _ -> False
570 notFn t = injective t || metavar t || inenv t
573 unifyTmpFail :: Term -> Term -> StateT UInfo TC [(Name, TT Name)]
574 unifyTmpFail x y
575 = do UI s f <- get
576 let r = recoverable (normalise ctxt env x) (normalise ctxt env y)
577 let err = cantUnify from r
578 (topx, xfrom) (topy, yfrom)
579 (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
580 put (UI s ((topx, topy, True, env, err, from, Unify) : f))
581 return []
583 -- shortcut failure, if we *know* nothing can fix it
584 unifyFail x y = do UI s f <- get
585 let r = recoverable (normalise ctxt env x) (normalise ctxt env y)
586 let err = cantUnify from r
587 (topx, xfrom) (topy, yfrom)
588 (CantUnify r (x, Nothing) (y, Nothing) (Msg "") (errEnv env) s) (errEnv env) s
589 put (UI s ((topx, topy, True, env, err, from, Unify) : f))
590 lift $ tfail err
593 uB bnames (Let tx vx) (Let ty vy)
594 = do h1 <- un' False bnames tx ty
595 h2 <- un' False bnames vx vy
596 sc 1
597 combine bnames h1 h2
598 uB bnames (Guess tx vx) (Guess ty vy)
599 = do h1 <- un' False bnames tx ty
600 h2 <- un' False bnames vx vy
601 sc 1
602 combine bnames h1 h2
603 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
604 uB bnames (Pi _ tx _) (Pi _ ty _) = do sc 1; un' False bnames tx ty
605 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
606 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
607 uB bnames x y = do UI s f <- get
608 let r = recoverable (normalise ctxt env (binderTy x))
609 (normalise ctxt env (binderTy y))
610 let err = cantUnify from r (topx, xfrom) (topy, yfrom)
611 (CantUnify r (binderTy x, Nothing) (binderTy y, Nothing) (Msg "") (errEnv env) s)
612 (errEnv env) s
613 put (UI s ((binderTy x, binderTy y,
614 False,
615 env, err, from, Unify) : f))
616 return [] -- lift $ tfail err
618 checkCycle ns p@(x, P _ _ _) = return [p]
619 checkCycle ns (x, tm)
620 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
621 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
623 checkScope ns (x, tm) | pureTerm tm =
624-- case boundVs (envPos x 0 env) tm of
625-- [] -> return [(x, tm)]
626-- (i:_) -> lift $ tfail (UnifyScope x (fst (fst (ns!!i)))
627-- (inst ns tm) (errEnv env))
628 let v = highV (-1) tm in
629 if v >= length ns
630 then lift $ tfail (Msg "SCOPE ERROR")
631 else return [(x, bind v ns tm)]
632 where inst [] tm = tm
633 inst (((n, _), _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
634 checkScope ns (x, tm) = lift $ tfail (Msg "HOLE ERROR")
636 bind i ns tm
637 | i < 0 = tm
638 | otherwise = let ((x,y),ty) = ns!!i in
639 App (Bind y (Lam ty) (bind (i-1) ns tm))
640 (P Bound x ty)
642 combineArgs bnames args = ca [] args where
643 ca acc [] = return acc
644 ca acc (x : xs) = do x' <- combine bnames acc x
645 ca x' xs
647 combine bnames as [] = return as
648 combine bnames as ((n, t) : bs)
649 = case lookup n as of
650 Nothing -> combine bnames (as ++ [(n,t)]) bs
651 Just t' -> do ns <- un' False bnames t t'
652 -- make sure there's n mapping from n in ns
653 let ns' = filter (\ (x, _) -> x/=n) ns
654 sc 1
655 combine bnames as (ns' ++ bs)
657boundVs :: Int -> Term -> [Int]
658boundVs i (V j) | j < i = []
659 | otherwise = [j]
660boundVs i (Bind n b sc) = boundVs (i + 1) sc
661boundVs i (App f x) = let fs = boundVs i f
662 xs = boundVs i x in
663 nub (fs ++ xs)
664boundVs i _ = []
666highV :: Int -> Term -> Int
667highV i (V j) | j > i = j
668 | otherwise = i
669highV i (Bind n b sc) = maximum [i, highV i (binderTy b), (highV i sc - 1)]
670highV i (App f x) = max (highV i f) (highV i x)
671highV i _ = i
673envPos x i [] = 0
674envPos x i ((y, _) : ys) | x == y = i
675 | otherwise = envPos x (i + 1) ys
678-- If there are any clashes of constructors, deem it unrecoverable, otherwise some
679-- more work may help.
680-- FIXME: Depending on how overloading gets used, this may cause problems. Better
681-- rethink overloading properly...
682-- ASSUMPTION: inputs are in normal form
683--
684-- Issue #1722 on the issue tracker https://github.com/idris-lang/Idris-dev/issues/1722
685--
686recoverable t@(App _ _) _
687 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
688recoverable _ t@(App _ _)
689 | (P _ (UN l) _, _) <- unApply t, l == txt "Lazy'" = False
690recoverable (P (DCon _ _ _) x _) (P (DCon _ _ _) y _) = x == y
691recoverable (P (TCon _ _) x _) (P (TCon _ _) y _) = x == y
692recoverable (Constant _) (P (DCon _ _ _) y _) = False
693recoverable (Constant x) (Constant y) = x == y
694recoverable (P (DCon _ _ _) x _) (Constant _) = False
695recoverable (Constant _) (P (TCon _ _) y _) = False
696recoverable (P (TCon _ _) x _) (Constant _) = False
697recoverable (P (DCon _ _ _) x _) (P (TCon _ _) y _) = False
698recoverable (P (TCon _ _) x _) (P (DCon _ _ _) y _) = False
699recoverable p@(Constant _) (App f a) = recoverable p f
700recoverable (App f a) p@(Constant _) = recoverable f p
701recoverable p@(P _ n _) (App f a) = recoverable p f
702recoverable (App f a) p@(P _ _ _) = recoverable f p
703recoverable (App f a) (App f' a')
704 | f == f' = recoverable a a'
705recoverable (App f a) (App f' a')
706 = recoverable f f' -- && recoverable a a'
707recoverable f (Bind _ (Pi _ _ _) sc)
708 | (P (DCon _ _ _) _ _, _) <- unApply f = False
709 | (P (TCon _ _) _ _, _) <- unApply f = False
710 | (Constant _) <- f = False
711recoverable (Bind _ (Pi _ _ _) sc) f
712 | (P (DCon _ _ _) _ _, _) <- unApply f = False
713 | (P (TCon _ _) _ _, _) <- unApply f = False
714 | (Constant _) <- f = False
715recoverable (Bind _ (Lam _) sc) f = recoverable sc f
716recoverable f (Bind _ (Lam _) sc) = recoverable f sc
717recoverable x y = True
719errEnv :: [(a, Binder b)] -> [(a, b)]
720errEnv = map (\(x, b) -> (x, binderTy b))
722holeIn :: Env -> Name -> Bool
723holeIn env n = case lookup n env of
724 Just (Hole _) -> True
725 Just (Guess _ _) -> True
726 _ -> False
728patIn :: Env -> Name -> Bool
729patIn env n = case lookup n env of
730 Just (PVar _) -> True
731 Just (PVTy _) -> True
732 _ -> False