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 case runStateT (un' False [] topxn topyn)
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
40 notTrivial (x, P _ x' _) = x /= x'
43 injective (P (DCon _ _) _ _) = True
44 injective (P (TCon _ _) _ _) = True
45 injective (App f a) = injective f
48 notP (P _ _ _) = False
51 sc i = do UI s x f <- get
54 uplus u1 u2 = do UI s i f <- get
57 if (length f == length f')
59 else do put (UI s i f); u2
61 un' :: Bool -> [(Name, Name)] -> TT 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)
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)
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'
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'
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
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
119 uB bnames (Guess tx vx) (Guess ty vy)
120 = do h1 <- un' False bnames tx ty
121 h2 <- un' False bnames ty vy
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)
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'
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 _)
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
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