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 bx sx) (Bind y by sy)
125 = do h1 <- uB bnames bx by
126 h2 <- un' False ((x,y):bnames) sx sy
129 | OK True <- convEq' ctxt x y = do sc 1; return []
130 | otherwise = do UI s i f <- get
131 let r = recoverable x y
132 let err = CantUnify r
133 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
134 put (UI s i ((x, y, env, err) : f))
135 return [] -- lift $ tfail err
137 -- shortcut failure, if we *know* nothing can fix it
138 unifyFail x y = do UI s i f <- get
139 let r = recoverable x y
140 let err = CantUnify r
141 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
142 put (UI s i ((x, y, env, err) : f))
146 uB bnames (Let tx vx) (Let ty vy)
147 = do h1 <- un' False bnames tx ty
148 h2 <- un' False bnames ty vy
151 uB bnames (Guess tx vx) (Guess ty vy)
152 = do h1 <- un' False bnames tx ty
153 h2 <- un' False bnames ty vy
156 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
157 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
158 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
159 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
160 uB bnames x y = do UI s i f <- get
161 let r = recoverable (binderTy x) (binderTy y)
162 let err = CantUnify r topx topy
163 (CantUnify r (binderTy x) (binderTy y) (Msg "") [] s)
165 put (UI s i ((binderTy x, binderTy y, env, err) : f))
166 return [] -- lift $ tfail err
168 combine bnames as [] = return as
169 combine bnames as ((n, t) : bs)
170 = case lookup n as of
171 Nothing -> combine bnames (as ++ [(n,t)]) bs
172 Just t' -> do un' False bnames t t'
176 -- If there are any clashes of constructors, deem it unrecoverable, otherwise some
177 -- more work may help.
178 -- FIXME: Depending on how overloading gets used, this may cause problems. Better
179 -- rethink overloading properly...
181 recoverable (P (DCon _ _) x _) (P (DCon _ _) y _)
184-- recoverable (P (DCon _ _) x _) (P (TCon _ _) y _) = False
185-- recoverable (P (TCon _ _) x _) (P (DCon _ _) y _) = False
186 recoverable p@(P _ _ _) (App f a) = recoverable p f
187 recoverable (App f a) p@(P _ _ _) = recoverable f p
188 recoverable (App f a) (App f' a')
189 = recoverable f f' && recoverable a a'
190 recoverable _ _ = True
192errEnv = map (\(x, b) -> (x, binderTy b))
194holeIn :: Env -> Name -> Bool
195holeIn env n = case lookup n env of
196 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