on 8063cfd help sign in
Idris-dev/src/Core/Unify.hs
198 lines 8.3 kB history raw
1{-# LANGUAGE PatternGuards #-}
3module Core.Unify(unify, Fails) where
5import Core.TT
6import Core.Evaluate
8import Control.Monad
9import Control.Monad.State
10import Debug.Trace
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
18-- at the time
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)],
26 Injs, Fails)
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, [])
30 _ ->
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)
36 (UI 0 [] []) of
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
39 Error e -> tfail e
40 where
41 notTrivial (x, P _ x' _) = x /= x'
42 notTrivial _ = True
44 injective (P (DCon _ _) _ _) = True
45 injective (P (TCon _ _) _ _) = True
46 injective (App f a) = injective f
47 injective _ = False
49 notP (P _ _ _) = False
50 notP _ = True
52 sc i = do UI s x f <- get
53 put (UI (s+i) x f)
55 uplus u1 u2 = do UI s i f <- get
56 r <- u1
57 UI s _ f' <- get
58 if (length f == length f')
59 then return r
60 else do put (UI s i f); u2
62 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
63 StateT UInfo
64 TC [(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 _)
71 = unifyFail topx topy
72 un' fn names topx@(P (TCon _ _) x _) topy@(P (DCon _ _) y _)
73 = unifyFail topx topy
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)
79 sc 1
80 return [(x, tm)]
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)
84 sc 1
85 return [(y, tm)]
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'
100 sc 1
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'
106 sc 1
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
127 combine bnames h1 h2
128 un' fn bnames x y
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))
143 lift $ tfail err
146 uB bnames (Let tx vx) (Let ty vy)
147 = do h1 <- un' False bnames tx ty
148 h2 <- un' False bnames ty vy
149 sc 1
150 combine bnames h1 h2
151 uB bnames (Guess tx vx) (Guess ty vy)
152 = do h1 <- un' False bnames tx ty
153 h2 <- un' False bnames ty vy
154 sc 1
155 combine bnames h1 h2
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)
164 (errEnv env) 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'
173 sc 1
174 combine bnames as bs
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 _)
182 | x == y = True
183 | otherwise = False
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
197 _ -> False