1{-# LANGUAGE PatternGuards #-}
3module Core.Unify(match_unify, unify, Fails) where
9import Control.Monad.State
13-- Unification is applied inside the theorem prover. We're looking for holes
14-- which can be filled in, by matching one term's normal form against another.
15-- Returns a list of hole names paired with the term which solves them, and
16-- a list of things which need to be injective.
18-- terms which need to be injective, with the things we're trying to unify
21type Injs = [(TT Name, TT Name, TT Name)]
22type Fails = [(TT Name, TT Name, Env, Err)]
24data UInfo = UI Int Fails
31-- Solve metavariables by matching terms against each other
32-- Not really unification, of course!
34match_unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
36match_unify ctxt env topx topy dont holes =
37 case runStateT (un [] topx topy) (UI 0 []) of
38 OK (v, UI _ []) -> return (filter notTrivial v)
40 let topxn = normalise ctxt env topx
41 topyn = normalise ctxt env topy in
42 case runStateT (un [] topxn topyn)
45 return (filter notTrivial v)
47 -- just normalise the term we're matching against
48 case runStateT (un [] topxn topy)
51 return (filter notTrivial v)
55 | holeIn env x || x `elem` holes
56 = do sc 1; checkCycle names (x, tm)
58 | holeIn env y || y `elem` holes
59 = do sc 1; checkCycle names (y, tm)
60 un bnames (V i) (P _ x _)
61 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
62 un bnames (P _ x _) (V i)
63 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
64 un bnames (Bind x bx sx) (Bind y by sy)
65 = do h1 <- uB bnames bx by
66 h2 <- un ((x, y) : bnames) sx sy
68 un names (App fx ax) (App fy ay)
69 = do hf <- un names fx fy
73 | OK True <- convEq' ctxt x y = do sc 1; return []
74 | otherwise = do UI s f <- get
75 let r = recoverable x y
77 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
78 if (not r) then lift $ tfail err
79 else do put (UI s ((x, y, env, err) : f))
83 uB bnames (Let tx vx) (Let ty vy) = do h1 <- un bnames tx ty
86 uB bnames (Lam tx) (Lam ty) = un bnames tx ty
87 uB bnames (Pi tx) (Pi ty) = un bnames tx ty
88 uB bnames x y = do UI s f <- get
89 let r = recoverable (binderTy x) (binderTy y)
90 let err = CantUnify r topx topy
91 (CantUnify r (binderTy x) (binderTy y) (Msg "") [] s)
93 put (UI s ((binderTy x, binderTy y, env, err) : f))
96 -- TODO: there's an annoying amount of repetition between this and the
97 -- main unification function. Consider lifting it out.
99 sc i = do UI s f <- get
102 unifyFail x y = do UI s f <- get
103 let r = recoverable x y
104 let err = CantUnify r
105 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
106 put (UI s ((x, y, env, err) : f))
108 combine bnames as [] = return as
109 combine bnames as ((n, t) : bs)
110 = case lookup n as of
111 Nothing -> combine bnames (as ++ [(n,t)]) bs
112 Just t' -> do ns <- un bnames t t'
113 -- make sure there's n mapping from n in ns
114 let ns' = filter (\ (x, _) -> x/=n) ns
116 combine bnames as (ns' ++ bs)
118 checkCycle ns p@(x, P _ _ _) = return [p]
119 checkCycle ns (x, tm)
120 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
121 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
123 checkScope ns (x, tm) =
124 case boundVs (envPos x 0 env) tm of
125 [] -> return [(x, tm)]
126 (i:_) -> lift $ tfail (UnifyScope x (fst (ns!!i))
127 (inst ns tm) (errEnv env))
128 where inst [] tm = tm
129 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
131notTrivial (x, P _ x' _) = x /= x'
134expandLets env (x, tm) = (x, doSubst (reverse env) tm)
137 doSubst ((n, Let v t) : env) tm
138 = doSubst env (subst n v tm)
143unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
144 TC ([(Name, TT Name)], Fails)
145unify ctxt env topx topy dont holes =
146-- trace ("Unifying " ++ show (topx, topy)) $
147 -- don't bother if topx and topy are different at the head
148 case runStateT (un False [] topx topy) (UI 0 []) of
149 OK (v, UI _ []) -> return (filter notTrivial v,
152 let topxn = normalise ctxt env topx
153 topyn = normalise ctxt env topy in
154-- trace ("Unifying " ++ show (topx, topy) ++ "\n\n==>\n" ++ show (topxn, topyn) ++ "\n\n" ++ show res ++ "\n\n") $
155 case runStateT (un False [] topxn topyn)
157 OK (v, UI _ fails) ->
158 return (filter notTrivial v, reverse fails)
159-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
162 headDiff (P (DCon _ _) x _) (P (DCon _ _) y _) = x /= y
163 headDiff (P (TCon _ _) x _) (P (TCon _ _) y _) = x /= y
166 injective (P (DCon _ _) _ _) = True
167 injective (P (TCon _ _) _ _) = True
168-- injective (App f (P _ _ _)) = injective f
169-- injective (App f (Constant _)) = injective f
170 injective (App f a) = injective f -- && injective a
173 notP (P _ _ _) = False
176 sc i = do UI s f <- get
179 errors = do UI s f <- get
180 return (not (null f))
182 uplus u1 u2 = do UI s f <- get
185 if (length f == length f')
187 else do put (UI s f); u2
189 un :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
194-- = let (xf, _) = unApply x
195-- (yf, _) = unApply y in
196-- if headDiff xf yf then unifyFail x y else
197-- uplus (un' fn names x y)
198-- (un' fn names (hnf ctxt env x) (hnf ctxt env y))
200 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
203 un' fn names x y | x == y = return [] -- shortcut
204 un' fn names topx@(P (DCon _ _) x _) topy@(P (DCon _ _) y _)
205 | x /= y = unifyFail topx topy
206 un' fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
207 | x /= y = unifyFail topx topy
208 un' fn names topx@(P (DCon _ _) x _) topy@(P (TCon _ _) y _)
209 = unifyFail topx topy
210 un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _) y _)
211 = unifyFail topx topy
212 un' fn names topx@(Constant _) topy@(P (TCon _ _) y _)
213 = unifyFail topx topy
214 un' fn names topx@(P (TCon _ _) x _) topy@(Constant _)
215 = unifyFail topx topy
216 un' fn bnames tx@(P _ x _) ty@(P _ y _)
217 | (x,y) `elem` bnames || x == y = do sc 1; return []
218 | injective tx && not (holeIn env y || y `elem` holes)
220 | injective ty && not (holeIn env x || x `elem` holes)
222 un' fn bnames xtm@(P _ x _) tm
223 | holeIn env x || x `elem` holes
227-- trace (show (x, tm, normalise ctxt env tm)) $
228-- put (UI s ((tm, topx, topy) : i) f)
229 then unifyTmpFail xtm tm
231 checkCycle bnames (x, tm)
232 | not (injective xtm) && injective tm = unifyFail xtm tm
233 un' fn bnames tm ytm@(P _ y _)
234 | holeIn env y || y `elem` holes
238-- trace (show (y, tm, normalise ctxt env tm)) $
239-- put (UI s ((tm, topx, topy) : i) f)
240 then unifyTmpFail tm ytm
242 checkCycle bnames (y, tm)
243 | not (injective ytm) && injective tm = unifyFail ytm tm
244 un' fn bnames (V i) (P _ x _)
245 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
246 un' fn bnames (P _ x _) (V i)
247 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
249 un' fn bnames appx@(App _ _) appy@(App _ _)
250 = unApp fn bnames appx appy
251-- = uplus (unApp fn bnames appx appy)
252-- (unifyTmpFail appx appy) -- take the whole lot
254 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
255 | n == n' = un' False bnames x y
256 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
257 | n == n' = un' False bnames x y
258 un' fn bnames x (Bind n (Lam t) (App y (V 0)))
259 = un' False bnames x y
260 un' fn bnames (Bind n (Lam t) (App x (V 0))) y
261 = un' False bnames x y
262-- un' fn bnames (Bind x (PVar _) sx) (Bind y (PVar _) sy)
263-- = un' False ((x,y):bnames) sx sy
264-- un' fn bnames (Bind x (PVTy _) sx) (Bind y (PVTy _) sy)
265-- = un' False ((x,y):bnames) sx sy
266 un' fn bnames (Bind x bx sx) (Bind y by sy)
267 = do h1 <- uB bnames bx by
268 h2 <- un' False ((x,y):bnames) sx sy
271 | OK True <- convEq' ctxt x y = do sc 1; return []
272 | otherwise = do UI s f <- get
273 let r = recoverable x y
274 let err = CantUnify r
275 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
276 if (not r) then lift $ tfail err
277 else do put (UI s ((x, y, env, err) : f))
278 return [] -- lift $ tfail err
280 unApp fn bnames appx@(App fx ax) appy@(App fy ay)
281 | (injective fx && injective fy)
282 || (injective fx && rigid appx && metavarApp appy)
283 || (injective fy && rigid appy && metavarApp appx)
284 || (injective fx && metavarApp fy && ax == ay)
285 || (injective fy && metavarApp fx && ax == ay)
286 = do let (headx, _) = unApply fx
287 let (heady, _) = unApply fy
288 -- fail quickly if the heads are disjoint
289 checkHeads headx heady
290-- if True then -- (injective fx || injective fy || fx == fy) then
291-- if (injective fx && metavarApp appy) ||
292-- (injective fy && metavarApp appx) ||
293-- (injective fx && injective fy) || fx == fy
295 (do hf <- un' True bnames fx fy
296 let ax' = hnormalise hf ctxt env (substNames hf ax)
297 let ay' = hnormalise hf ctxt env (substNames hf ay)
298 ha <- un' False bnames ax' ay'
300 combine bnames hf ha)
301 (do ha <- un' False bnames ax ay
302 let fx' = hnormalise ha ctxt env (substNames ha fx)
303 let fy' = hnormalise ha ctxt env (substNames ha fy)
304 hf <- un' False bnames fx' fy'
306 combine bnames hf ha)
307 | otherwise = -- trace (show (appx, appy, injective fx, metavarApp appy, sameArgStruct appx appy)) $
308 do let (headx, argsx) = unApply appx
309 let (heady, argsy) = unApply appy
310 -- traceWhen (headx == heady) (show (appx, appy)) $
312 if (length argsx == length argsy &&
313 ((headx == heady && inenv headx) || (argsx == argsy) ||
314 (and (zipWith sameStruct (headx:argsx) (heady:argsy)))))
316-- (notFn headx && notFn heady))) then
317 do uf <- un' True bnames headx heady
319 if (not failed) then unArgs uf argsx argsy
321 else -- trace ("TMPFAIL " ++ show (appx, appy, injective appx, injective appy)) $
322 unifyTmpFail appx appy)
323 (unifyTmpFail appx appy) -- whole application fails
324 where hnormalise [] _ _ t = t
325 hnormalise ns ctxt env t = normalise ctxt env t
326 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
327 | x /= y = unifyFail appx appy
328 checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
329 | x /= y = unifyFail appx appy
330 checkHeads (P (DCon _ _) x _) (P (TCon _ _) y _)
331 = unifyFail appx appy
332 checkHeads (P (TCon _ _) x _) (P (DCon _ _) y _)
333 = unifyFail appx appy
334 checkHeads _ _ = return []
336 unArgs as [] [] = return as
337 unArgs as (x : xs) (y : ys)
338 = do let x' = hnormalise as ctxt env (substNames as x)
339 let y' = hnormalise as ctxt env (substNames as y)
340 as' <- un' False bnames x' y'
341 vs <- combine bnames as as'
344 metavarApp tm = let (f, args) = unApply tm in
346 all (\x -> metavarApp x) args
348 metavarArgs tm = let (f, args) = unApply tm in
349 all (\x -> metavar x || inenv x) args
351 metavarApp' tm = let (f, args) = unApply tm in
352 all (\x -> pat x || metavar x) (f : args)
355 sameArgStruct appx appy
356 = let (_, ax) = unApply appx
357 (_, ay) = unApply appy in
358 length ax == length ay &&
359 and (zipWith sameStruct ax ay)
361 sameStruct fapp@(App f x) gapp@(App g y)
362 = let (f',a') = unApply fapp
363 (g',b') = unApply gapp in
364 (f' == g' && length a' == length b' &&
365 (injective f' || injective g'))
366 || (sameStruct f g && sameStruct x y)
367 sameStruct (P _ x _) (P _ y _) = True
368 sameStruct (V i) (V j) = i == j
369 sameStruct (Constant x) (Constant y) = True
370 sameStruct (P _ _ _) (Constant y) = True
371 sameStruct (Constant x) (P _ _ _) = True
372 sameStruct (Bind n t sc) (P _ _ _) = True
373 sameStruct (P _ _ _) (Bind n t sc) = True
374 sameStruct (Bind n t sc) (Bind n' t' sc') = sameStruct sc sc'
375 sameStruct _ _ = False
377 rigid (P (DCon _ _) _ _) = True
378 rigid (P (TCon _ _) _ _) = True
379 rigid t@(P Ref _ _) = inenv t
380 rigid (Constant _) = True
381 rigid (App f a) = rigid f && rigid a
382 rigid t = not (metavar t)
384 metavar t = case t of
385 P _ x _ -> (x `elem` holes || holeIn env x) &&
389 P _ x _ -> x `elem` holes || patIn env x
392 P _ x _ -> x `elem` (map fst env)
395 notFn t = injective t || metavar t || inenv t
400 let r = recoverable x y
401 let err = CantUnify r
402 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
403 put (UI s ((topx, topy, env, err) : f))
406 -- shortcut failure, if we *know* nothing can fix it
407 unifyFail x y = do UI s f <- get
408 let r = recoverable x y
409 let err = CantUnify r
410 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
411 put (UI s ((topx, topy, env, err) : f))
415 uB bnames (Let tx vx) (Let ty vy)
416 = do h1 <- un' False bnames tx ty
417 h2 <- un' False bnames vx vy
420 uB bnames (Guess tx vx) (Guess ty vy)
421 = do h1 <- un' False bnames tx ty
422 h2 <- un' False bnames vx vy
425 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
426 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
427 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
428 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
429 uB bnames x y = do UI s f <- get
430 let r = recoverable (binderTy x) (binderTy y)
431 let err = CantUnify r topx topy
432 (CantUnify r (binderTy x) (binderTy y) (Msg "") [] s)
434 put (UI s ((binderTy x, binderTy y, env, err) : f))
435 return [] -- lift $ tfail err
437 checkCycle ns p@(x, P _ _ _) = return [p]
438 checkCycle ns (x, tm)
439 | not (x `elem` freeNames tm) = checkScope ns (x, tm)
440 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
442 checkScope ns (x, tm) =
443 case boundVs (envPos x 0 env) tm of
444 [] -> return [(x, tm)]
445 (i:_) -> lift $ tfail (UnifyScope x (fst (ns!!i))
446 (inst ns tm) (errEnv env))
447 where inst [] tm = tm
448 inst ((n, _) : ns) tm = inst ns (substV (P Bound n Erased) tm)
450 combineArgs bnames args = ca [] args where
451 ca acc [] = return acc
452 ca acc (x : xs) = do x' <- combine bnames acc x
455 combine bnames as [] = return as
456 combine bnames as ((n, t) : bs)
457 = case lookup n as of
458 Nothing -> combine bnames (as ++ [(n,t)]) bs
459 Just t' -> do ns <- un' False bnames t t'
460 -- make sure there's n mapping from n in ns
461 let ns' = filter (\ (x, _) -> x/=n) ns
463 combine bnames as (ns' ++ bs)
465boundVs :: Int -> Term -> [Int]
466boundVs i (V j) | j <= i = []
468boundVs i (Bind n b sc) = boundVs (i + 1) sc
469boundVs i (App f x) = let fs = boundVs i f
475envPos x i ((y, _) : ys) | x == y = i
476 | otherwise = envPos x (i + 1) ys
479-- If there are any clashes of constructors, deem it unrecoverable, otherwise some
480-- more work may help.
481-- FIXME: Depending on how overloading gets used, this may cause problems. Better
482-- rethink overloading properly...
484recoverable (P (DCon _ _) x _) (P (DCon _ _) y _)
487recoverable (P (TCon _ _) x _) (P (TCon _ _) y _)
490recoverable (Constant _) (P (DCon _ _) y _) = False
491recoverable (P (DCon _ _) x _) (Constant _) = False
492recoverable (Constant _) (P (TCon _ _) y _) = False
493recoverable (P (TCon _ _) x _) (Constant _) = False
494recoverable (P (DCon _ _) x _) (P (TCon _ _) y _) = False
495recoverable (P (TCon _ _) x _) (P (DCon _ _) y _) = False
496recoverable p@(Constant _) (App f a) = recoverable p f
497recoverable (App f a) p@(Constant _) = recoverable f p
498recoverable p@(P _ n _) (App f a) = recoverable p f
499-- recoverable (App f a) p@(P _ _ _) = recoverable f p
500recoverable (App f a) (App f' a')
501 = recoverable f f' -- && recoverable a a'
502recoverable _ _ = True
504errEnv = map (\(x, b) -> (x, binderTy b))
506holeIn :: Env -> Name -> Bool
507holeIn env n = case lookup n env of
508 Just (Hole _) -> True
509 Just (Guess _ _) -> True
512patIn :: Env -> Name -> Bool
513patIn env n = case lookup n env of
514 Just (PVar _) -> True
515 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