1{-# LANGUAGE PatternGuards #-}
3module Core.Unify(unify, Fails) where
9import Control.Monad.State
12-- Unification is applied inside the theorem prover. We're looking for holes
13-- which can be filled in, by matching one term's normal form against another.
14-- Returns a list of hole names paired with the term which solves them, and
15-- a list of things which need to be injective.
17-- terms which need to be injective, with the things we're trying to unify
20type Injs = [(TT Name, TT Name, TT Name)]
21type Fails = [(TT Name, TT Name, Env, Err)]
23data UInfo = UI Int Fails
30unify :: Context -> Env -> TT Name -> TT Name -> [Name] -> [Name] ->
31 TC ([(Name, TT Name)], Fails)
32unify ctxt env topx topy dont holes =
33-- trace ("Unifying " ++ show (topx, topy)) $
34 -- don't bother if topx and topy are different at the head
35 case runStateT (un False [] topx topy) (UI 0 []) of
36 OK (v, UI _ []) -> return (filter notTrivial v, [])
38 let topxn = normalise ctxt env topx
39 topyn = normalise ctxt env topy in
40-- trace ("Unifying " ++ show (topx, topy) ++ "\n\n==>\n" ++ show (topxn, topyn) ++ "\n\n" ++ show res ++ "\n\n") $
41 case runStateT (un False [] topxn topyn)
44 return (filter notTrivial v, reverse fails)
45-- Error e@(CantUnify False _ _ _ _ _) -> tfail e
48 notTrivial (x, P _ x' _) = x /= x'
51 headDiff (P (DCon _ _) x _) (P (DCon _ _) y _) = x /= y
52 headDiff (P (TCon _ _) x _) (P (TCon _ _) y _) = x /= y
55 injective (P (DCon _ _) _ _) = True
56 injective (P (TCon _ _) _ _) = True
57 injective (App f (P _ _ _)) = injective f
58 injective (App f (Constant _)) = injective f
59 injective (App f a) = injective f && injective a
62 notP (P _ _ _) = False
65 sc i = do UI s f <- get
68 uplus u1 u2 = do UI s f <- get
71 if (length f == length f')
73 else do put (UI s f); u2
75 un :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
80-- = let (xf, _) = unApply x
81-- (yf, _) = unApply y in
82-- if headDiff xf yf then unifyFail x y else
83-- uplus (un' fn names x y)
84-- (un' fn names (hnf ctxt env x) (hnf ctxt env y))
86 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
89 un' fn names x y | x == y = return [] -- shortcut
90 un' fn names topx@(P (DCon _ _) x _) topy@(P (DCon _ _) y _)
91 | x /= y = unifyFail topx topy
92 un' fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
93 | x /= y = unifyFail topx topy
94 un' fn names topx@(P (DCon _ _) x _) topy@(P (TCon _ _) y _)
96 un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _) y _)
98 un' fn names topx@(Constant _) topy@(P (TCon _ _) y _)
100 un' fn names topx@(P (TCon _ _) x _) topy@(Constant _)
101 = unifyFail topx topy
102 un' fn bnames tx@(P _ x _) ty@(P _ y _)
103 | (x,y) `elem` bnames || x == y = do sc 1; return []
104 | injective tx && not (holeIn env y || y `elem` holes)
106 | injective ty && not (holeIn env x || x `elem` holes)
108 un' fn bnames xtm@(P _ x _) tm
109 | holeIn env x || x `elem` holes
113-- trace (show (x, tm, normalise ctxt env tm)) $
114-- put (UI s ((tm, topx, topy) : i) f)
115 then unifyTmpFail xtm tm
118 | not (injective xtm) && injective tm = unifyFail xtm tm
119 un' fn bnames tm ytm@(P _ y _)
120 | holeIn env y || y `elem` holes
124-- trace (show (y, tm, normalise ctxt env tm)) $
125-- put (UI s ((tm, topx, topy) : i) f)
126 then unifyTmpFail tm ytm
129 | not (injective ytm) && injective tm = unifyFail ytm tm
130 un' fn bnames (V i) (P _ x _)
131 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
132 un' fn bnames (P _ x _) (V i)
133 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
135 un' fn bnames appx@(App fx ax) appy@(App fy ay)
136 | injective fx && sameArgStruct appx appy && metavarApp appy
137 || injective fy && sameArgStruct appx appy && metavarApp appx
138 || injective fx && injective fy
140 = do let (headx, _) = unApply fx
141 let (heady, _) = unApply fy
142 -- fail quickly if the heads are disjoint
143 checkHeads headx heady
144-- if True then -- (injective fx || injective fy || fx == fy) then
145-- if (injective fx && metavarApp appy) ||
146-- (injective fy && metavarApp appx) ||
147-- (injective fx && injective fy) || fx == fy
149 (do hf <- un' True bnames fx fy
150 let ax' = hnormalise hf ctxt env (substNames hf ax)
151 let ay' = hnormalise hf ctxt env (substNames hf ay)
152 ha <- un' False bnames ax' ay'
154 combine bnames hf ha)
155 (do ha <- un' False bnames ax ay
156 let fx' = hnormalise ha ctxt env (substNames ha fx)
157 let fy' = hnormalise ha ctxt env (substNames ha fy)
158 hf <- un' False bnames fx' fy'
160 combine bnames hf ha)
162 = do let (headx, argsx) = unApply appx
163 let (heady, argsy) = unApply appy
164 if (length argsx == length argsy &&
165 ((headx == heady) || (argsx == argsy) ||
166 (and (zipWith sameStruct (headx:argsx) (heady:argsy)))))
168-- (notFn headx && notFn heady))) then
169 do uf <- un' True bnames headx heady
170 unArgs uf argsx argsy
171 else unifyTmpFail appx appy
172 where hnormalise [] _ _ t = t
173 hnormalise ns ctxt env t = normalise ctxt env t
174 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
175 | x /= y = unifyFail appx appy
176 checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
177 | x /= y = unifyFail appx appy
178 checkHeads (P (DCon _ _) x _) (P (TCon _ _) y _)
179 = unifyFail appx appy
180 checkHeads (P (TCon _ _) x _) (P (DCon _ _) y _)
181 = unifyFail appx appy
182 checkHeads _ _ = return []
184 unArgs as [] [] = return as
185 unArgs as (x : xs) (y : ys)
186 = do let x' = hnormalise as ctxt env (substNames as x)
187 let y' = hnormalise as ctxt env (substNames as y)
188 as' <- un' False bnames x' y'
189 vs <- combine bnames as as'
192 metavarApp tm = let (f, args) = unApply tm in
193 all (\x -> metavar x || inenv x) (f : args)
194 metavarArgs tm = let (f, args) = unApply tm in
195 all (\x -> metavar x || inenv x) args
196 metavarApp' tm = let (f, args) = unApply tm in
197 all (\x -> pat x || metavar x) (f : args)
199 sameArgStruct appx appy = let (_, ax) = unApply appx
200 (_, ay) = unApply appy in
201 and (zipWith sameStruct ax ay)
203 sameStruct fapp@(App f x) gapp@(App g y)
204 = let (f',a') = unApply fapp
205 (g',b') = unApply gapp in
206 (f' == g' && length a' == length b' &&
207 (injective f' || injective g'))
208 || (sameStruct f g && sameStruct x y)
209 sameStruct (P _ x _) (P _ y _) = True
210 sameStruct (V i) (V j) = i == j
211 sameStruct (Constant x) (Constant y) = True
212 sameStruct (P _ _ _) (Constant y) = True
213 sameStruct (Constant x) (P _ _ _) = True
214 sameStruct (Bind n t sc) (P _ _ _) = True
215 sameStruct (P _ _ _) (Bind n t sc) = True
216 sameStruct (Bind n t sc) (Bind n' t' sc') = sameStruct sc sc'
217 sameStruct _ _ = False
219 metavar t = case t of
220 P _ x _ -> (x `elem` holes || holeIn env x) &&
224 P _ x _ -> x `elem` holes || patIn env x
227 P _ x _ -> x `elem` (map fst env)
230 notFn t = injective t || metavar t || inenv t
232 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
233 | n == n' = un' False bnames x y
234 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
235 | n == n' = un' False bnames x y
236 un' fn bnames x (Bind n (Lam t) (App y (V 0)))
237 = un' False bnames x y
238 un' fn bnames (Bind n (Lam t) (App x (V 0))) y
239 = un' False bnames x y
240-- un' fn bnames (Bind x (PVar _) sx) (Bind y (PVar _) sy)
241-- = un' False ((x,y):bnames) sx sy
242-- un' fn bnames (Bind x (PVTy _) sx) (Bind y (PVTy _) sy)
243-- = un' False ((x,y):bnames) sx sy
244 un' fn bnames (Bind x bx sx) (Bind y by sy)
245 = do h1 <- uB bnames bx by
246 h2 <- un' False ((x,y):bnames) sx sy
249 | OK True <- convEq' ctxt x y = do sc 1; return []
250 | otherwise = do UI s f <- get
251 let r = recoverable x y
252 let err = CantUnify r
253 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
254 if (not r) then lift $ tfail err
255 else do put (UI s ((x, y, env, err) : f))
256 return [] -- lift $ tfail err
260 let r = recoverable x y
261 let err = CantUnify r
262 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
263 put (UI s ((x, y, env, err) : f))
266 -- shortcut failure, if we *know* nothing can fix it
267 unifyFail x y = do UI s f <- get
268 let r = recoverable x y
269 let err = CantUnify r
270 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
271 put (UI s ((x, y, env, err) : f))
275 uB bnames (Let tx vx) (Let ty vy)
276 = do h1 <- un' False bnames tx ty
277 h2 <- un' False bnames ty vy
280 uB bnames (Guess tx vx) (Guess ty vy)
281 = do h1 <- un' False bnames tx ty
282 h2 <- un' False bnames ty vy
285 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
286 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
287 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
288 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
289 uB bnames x y = do UI s f <- get
290 let r = recoverable (binderTy x) (binderTy y)
291 let err = CantUnify r topx topy
292 (CantUnify r (binderTy x) (binderTy y) (Msg "") [] s)
294 put (UI s ((binderTy x, binderTy y, env, err) : f))
295 return [] -- lift $ tfail err
297 checkCycle p@(x, P _ _ _) = return [p]
299 | not (x `elem` freeNames tm) = return [(x, tm)]
300 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
302 combineArgs bnames args = ca [] args where
303 ca acc [] = return acc
304 ca acc (x : xs) = do x' <- combine bnames acc x
307 combine bnames as [] = return as
308 combine bnames as ((n, t) : bs)
309 = case lookup n as of
310 Nothing -> combine bnames (as ++ [(n,t)]) bs
311 Just t' -> do ns <- un' False bnames t t'
312 -- make sure there's n mapping from n in ns
313 let ns' = filter (\ (x, _) -> x/=n) ns
315 combine bnames as (ns' ++ bs)
317 -- If there are any clashes of constructors, deem it unrecoverable, otherwise some
318 -- more work may help.
319 -- FIXME: Depending on how overloading gets used, this may cause problems. Better
320 -- rethink overloading properly...
322 recoverable (P (DCon _ _) x _) (P (DCon _ _) y _)
325 recoverable (P (TCon _ _) x _) (P (TCon _ _) y _)
328 recoverable (Constant _) (P (DCon _ _) y _) = False
329 recoverable (P (DCon _ _) x _) (Constant _) = False
330 recoverable (Constant _) (P (TCon _ _) y _) = False
331 recoverable (P (TCon _ _) x _) (Constant _) = False
332 recoverable (P (DCon _ _) x _) (P (TCon _ _) y _) = False
333 recoverable (P (TCon _ _) x _) (P (DCon _ _) y _) = False
334 recoverable p@(Constant _) (App f a) = recoverable p f
335 recoverable (App f a) p@(Constant _) = recoverable f p
336 recoverable p@(P _ n _) (App f a) = recoverable p f
337-- recoverable (App f a) p@(P _ _ _) = recoverable f p
338 recoverable (App f a) (App f' a')
339 = recoverable f f' -- && recoverable a a'
340 recoverable _ _ = True
342errEnv = map (\(x, b) -> (x, binderTy b))
344holeIn :: Env -> Name -> Bool
345holeIn env n = case lookup n env of
346 Just (Hole _) -> True
347 Just (Guess _ _) -> True
350patIn :: Env -> Name -> Bool
351patIn env n = case lookup n env of
352 Just (PVar _) -> True
353 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