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 Injs Fails
25unify :: Context -> Env -> TT Name -> TT Name -> TC ([(Name, TT Name)],
27unify ctxt env topx topy
28 = -- case runStateT (un' False [] topx topy) (UI 0 [] []) of
29 -- OK (v, UI _ inj []) -> return (filter notTrivial v, inj, [])
31-- trace ("Unifying " ++ show (topx, topy)) $
32 let topxn = normalise ctxt env topx
33 topyn = normalise ctxt env topy in
34-- trace ("Unifying " ++ show (topxn, topyn)) $
35 case runStateT (un' False [] topxn topyn)
37 OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
38-- OK (_, UI s _ ((_,_,f):fs)) -> tfail $ CantUnify topx topy f s
41 notTrivial (x, P _ x' _) = x /= x'
44 injective (P (DCon _ _) _ _) = True
45 injective (P (TCon _ _) _ _) = True
46 injective (App f a) = injective f
49 notP (P _ _ _) = False
52 sc i = do UI s x f <- get
55 uplus u1 u2 = do UI s i f <- get
58 if (length f == length f')
60 else do put (UI s i f); u2
62 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
65 un' fn names x y | x == y = return [] -- shortcut
66 un' fn names topx@(P (DCon _ _) x _) topy@(P (DCon _ _) y _)
67 | x /= y = unifyFail topx topy
68 un' fn names topx@(P (TCon _ _) x _) topy@(P (TCon _ _) y _)
69 | x /= y = unifyFail topx topy
70 un' fn names topx@(P (DCon _ _) x _) topy@(P (TCon _ _) y _)
72 un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _) y _)
74 un' fn bnames (P Bound x _) (P Bound y _)
75 | (x,y) `elem` bnames = do sc 1; return []
76 un' fn bnames (P Bound x _) tm
77 | holeIn env x = do UI s i f <- get
78 when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
81 un' fn bnames tm (P Bound y _)
82 | holeIn env y = do UI s i f <- get
83 when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
86 un' fn bnames (V i) (P Bound x _)
87 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
88 un' fn bnames (P Bound x _) (V i)
89 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
91 un' fn bnames appx@(App fx ax) appy@(App fy ay)
92 = do let (headx, _) = unApply fx
93 let (heady, _) = unApply fy
94 checkHeads headx heady
95 uplus -- do the second one if the first adds any errors
96 (do hf <- un' True bnames fx fy
97 let ax' = hnormalise hf ctxt env (substNames hf ax)
98 let ay' = hnormalise hf ctxt env (substNames hf ay)
99 ha <- un' False bnames ax' ay'
101 combine bnames hf ha)
102 (do ha <- un' False bnames ax ay
103 let fx' = hnormalise ha ctxt env (substNames ha fx)
104 let fy' = hnormalise ha ctxt env (substNames ha fy)
105 hf <- un' False bnames fx' fy'
107 combine bnames hf ha)
108 where hnormalise [] _ _ t = t
109 hnormalise ns ctxt env t = normalise ctxt env t
110 checkHeads (P (DCon _ _) x _) (P (DCon _ _) y _)
111 | x /= y = unifyFail appx appy
112 checkHeads (P (TCon _ _) x _) (P (TCon _ _) y _)
113 | x /= y = unifyFail appx appy
114 checkHeads (P (DCon _ _) x _) (P (TCon _ _) y _)
115 = unifyFail appx appy
116 checkHeads (P (TCon _ _) x _) (P (DCon _ _) y _)
117 = unifyFail appx appy
118 checkHeads _ _ = return []
120 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
121 | n == n' = un' False bnames x y
122 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
123 | n == n' = un' False bnames x y
124-- un' fn bnames (Bind x (PVar _) sx) (Bind y (PVar _) sy)
125-- = un' False ((x,y):bnames) sx sy
126-- un' fn bnames (Bind x (PVTy _) sx) (Bind y (PVTy _) sy)
127-- = un' False ((x,y):bnames) sx sy
128 un' fn bnames (Bind x bx sx) (Bind y by sy)
129 = do h1 <- uB bnames bx by
130 h2 <- un' False ((x,y):bnames) sx sy
133 | OK True <- convEq' ctxt x y = do sc 1; return []
134 | otherwise = do UI s i f <- get
135 let r = recoverable x y
136 let err = CantUnify r
137 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
138 put (UI s i ((x, y, env, err) : f))
139 return [] -- lift $ tfail err
141 -- shortcut failure, if we *know* nothing can fix it
142 unifyFail x y = do UI s i f <- get
143 let r = recoverable x y
144 let err = CantUnify r
145 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
146 put (UI s i ((x, y, env, err) : f))
150 uB bnames (Let tx vx) (Let ty vy)
151 = do h1 <- un' False bnames tx ty
152 h2 <- un' False bnames ty vy
155 uB bnames (Guess tx vx) (Guess ty vy)
156 = do h1 <- un' False bnames tx ty
157 h2 <- un' False bnames ty vy
160 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
161 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
162 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
163 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
164 uB bnames x y = do UI s i f <- get
165 let r = recoverable (binderTy x) (binderTy y)
166 let err = CantUnify r topx topy
167 (CantUnify r (binderTy x) (binderTy y) (Msg "") [] s)
169 put (UI s i ((binderTy x, binderTy y, env, err) : f))
170 return [] -- lift $ tfail err
172 checkCycle p@(x, P _ _ _) = return [p]
174 | not (x `elem` freeNames tm) = return [(x, tm)]
175 | otherwise = lift $ tfail (InfiniteUnify x tm (errEnv env))
177 combine bnames as [] = return as
178 combine bnames as ((n, t) : bs)
179 = case lookup n as of
180 Nothing -> combine bnames (as ++ [(n,t)]) bs
181 Just t' -> do ns <- un' False bnames t t'
182 -- make sure there's n mapping from n in ns
183 let ns' = filter (\ (x, _) -> x/=n) ns
185 combine bnames as (ns' ++ bs)
187 -- If there are any clashes of constructors, deem it unrecoverable, otherwise some
188 -- more work may help.
189 -- FIXME: Depending on how overloading gets used, this may cause problems. Better
190 -- rethink overloading properly...
192 recoverable (P (DCon _ _) x _) (P (DCon _ _) y _)
195-- recoverable (P (DCon _ _) x _) (P (TCon _ _) y _) = False
196-- recoverable (P (TCon _ _) x _) (P (DCon _ _) y _) = False
197 recoverable p@(P _ _ _) (App f a) = recoverable p f
198 recoverable (App f a) p@(P _ _ _) = recoverable f p
199 recoverable (App f a) (App f' a')
200 = recoverable f f' && recoverable a a'
201 recoverable _ _ = True
203errEnv = map (\(x, b) -> (x, binderTy b))
205holeIn :: Env -> Name -> Bool
206holeIn env n = case lookup n env of
207 Just (Hole _) -> 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