on e88a135 help sign in
Idris-dev/src/Core/Unify.hs
164 lines 6.7 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 case runStateT (un' False [] topxn topyn)
35 (UI 0 [] []) of
36 OK (v, UI _ inj fails) -> return (filter notTrivial v, inj, reverse fails)
37-- OK (_, UI s _ ((_,_,f):fs)) -> tfail $ CantUnify topx topy f s
38 Error e -> tfail e
39 where
40 notTrivial (x, P _ x' _) = x /= x'
41 notTrivial _ = True
43 injective (P (DCon _ _) _ _) = True
44 injective (P (TCon _ _) _ _) = True
45 injective (App f a) = injective f
46 injective _ = False
48 notP (P _ _ _) = False
49 notP _ = True
51 sc i = do UI s x f <- get
52 put (UI (s+i) x f)
54 uplus u1 u2 = do UI s i f <- get
55 r <- u1
56 UI s _ f' <- get
57 if (length f == length f')
58 then return r
59 else do put (UI s i f); u2
61 un' :: Bool -> [(Name, Name)] -> TT Name -> TT Name ->
62 StateT UInfo
63 TC [(Name, TT Name)]
64 un' fn bnames (P Bound x _) (P Bound y _)
65 | (x,y) `elem` bnames = do sc 1; return []
66 un' fn bnames (P Bound x _) tm
67 | holeIn env x = do UI s i f <- get
68 when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
69 sc 1
70 return [(x, tm)]
71 un' fn bnames tm (P Bound y _)
72 | holeIn env y = do UI s i f <- get
73 when (notP tm && fn) $ put (UI s ((tm, topx, topy) : i) f)
74 sc 1
75 return [(y, tm)]
76 un' fn bnames (V i) (P Bound x _)
77 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
78 un' fn bnames (P Bound x _) (V i)
79 | fst (bnames!!i) == x || snd (bnames!!i) == x = do sc 1; return []
81 un' fn bnames (App fx ax) (App fy ay)
82 = do uplus -- do the second one if the first adds any errors
83 (do hf <- un' True bnames fx fy
84 let ax' = hnormalise hf ctxt env (substNames hf ax)
85 let ay' = hnormalise hf ctxt env (substNames hf ay)
86 ha <- un' False bnames ax' ay'
87 sc 1
88 combine bnames hf ha)
89 (do ha <- un' False bnames ax ay
90 let fx' = hnormalise ha ctxt env (substNames ha fx)
91 let fy' = hnormalise ha ctxt env (substNames ha fy)
92 hf <- un' False bnames fx' fy'
93 sc 1
94 combine bnames hf ha)
95 where hnormalise [] _ _ t = t
96 hnormalise ns ctxt env t = normalise ctxt env t
97 un' fn bnames x (Bind n (Lam t) (App y (P Bound n' _)))
98 | n == n' = un' False bnames x y
99 un' fn bnames (Bind n (Lam t) (App x (P Bound n' _))) y
100 | n == n' = un' False bnames x y
101 un' fn bnames (Bind x bx sx) (Bind y by sy)
102 = do h1 <- uB bnames bx by
103 h2 <- un' False ((x,y):bnames) sx sy
104 combine bnames h1 h2
105 un' fn bnames x y
106 | OK True <- convEq' ctxt x y = do sc 1; return []
107 | otherwise = do UI s i f <- get
108 let r = recoverable x y
109 let err = CantUnify r
110 topx topy (CantUnify r x y (Msg "") [] s) (errEnv env) s
111 put (UI s i ((x, y, env, err) : f))
112 return [] -- lift $ tfail err
114 uB bnames (Let tx vx) (Let ty vy)
115 = do h1 <- un' False bnames tx ty
116 h2 <- un' False bnames ty vy
117 sc 1
118 combine bnames h1 h2
119 uB bnames (Guess tx vx) (Guess ty vy)
120 = do h1 <- un' False bnames tx ty
121 h2 <- un' False bnames ty vy
122 sc 1
123 combine bnames h1 h2
124 uB bnames (Lam tx) (Lam ty) = do sc 1; un' False bnames tx ty
125 uB bnames (Pi tx) (Pi ty) = do sc 1; un' False bnames tx ty
126 uB bnames (Hole tx) (Hole ty) = un' False bnames tx ty
127 uB bnames (PVar tx) (PVar ty) = un' False bnames tx ty
128 uB bnames x y = do UI s i f <- get
129 let r = recoverable (binderTy x) (binderTy y)
130 let err = CantUnify r topx topy
131 (CantUnify r (binderTy x) (binderTy y) (Msg "") [] s)
132 (errEnv env) s
133 put (UI s i ((binderTy x, binderTy y, env, err) : f))
134 return [] -- lift $ tfail err
136 combine bnames as [] = return as
137 combine bnames as ((n, t) : bs)
138 = case lookup n as of
139 Nothing -> combine bnames (as ++ [(n,t)]) bs
140 Just t' -> do un' False bnames t t'
141 sc 1
142 combine bnames as bs
144 -- If there are any clashes of constructors, deem it unrecoverable, otherwise some
145 -- more work may help.
147 recoverable (P (DCon _ _) x _) (P (DCon _ _) y _)
148 | x == y = True
149 | otherwise = False
150 recoverable (P (DCon _ _) x _) (P (TCon _ _) y _) = False
151 recoverable (P (TCon _ _) x _) (P (DCon _ _) y _) = False
152 recoverable p@(P _ _ _) (App f a) = recoverable p f
153 recoverable (App f a) p@(P _ _ _) = recoverable f p
154 recoverable (App f a) (App f' a')
155 = recoverable f f' && recoverable a a'
156 recoverable _ _ = True
158errEnv = map (\(x, b) -> (x, binderTy b))
160holeIn :: Env -> Name -> Bool
161holeIn env n = case lookup n env of
162 Just (Hole _) -> True
163 _ -> False