on 385dcbf help sign in
Idris-dev/src/Idris/Core/ProofTerm.hs
368 lines 16 kB history raw
1{-|
2Module : Idris.Core.ProofTerm
3Description : Proof term. implementation and utilities.
4Copyright :
5License : BSD3
6Maintainer : The Idris Community.
7-}
9{-# LANGUAGE FlexibleInstances, MultiParamTypeClasses, PatternGuards #-}
10module Idris.Core.ProofTerm(
11 ProofTerm, Goal(..), mkProofTerm, getProofTerm
12 , resetProofTerm
13 , updateSolved, updateSolvedTerm, updateSolvedTerm'
14 , bound_in, bound_in_term, refocus, updsubst
15 , Hole, RunTactic'
16 , goal, atHole
17 ) where
19import Idris.Core.Evaluate
20import Idris.Core.TT
21import Idris.Core.Typecheck
23import Control.Monad.State.Strict
24import Data.List
25import Debug.Trace
27-- | A zipper over terms, in order to efficiently update proof terms.
28data TermPath = Top
29 | AppL (AppStatus Name) TermPath Term
30 | AppR (AppStatus Name) Term TermPath
31 | InBind Name BinderPath Term
32 | InScope Name (Binder Term) TermPath
33 deriving Show
35-- | A zipper over binders, because terms and binders are mutually defined.
36data BinderPath = Binder (Binder TermPath)
37 | LetT TermPath Term
38 | LetV Term TermPath
39 | GuessT TermPath Term
40 | GuessV Term TermPath
41 deriving Show
43-- | Replace the top of a term path with another term path. In other
44-- words, "graft" one term path into another.
45replaceTop :: TermPath -> TermPath -> TermPath
46replaceTop p Top = p
47replaceTop p (AppL s l t) = AppL s (replaceTop p l) t
48replaceTop p (AppR s t r) = AppR s t (replaceTop p r)
49replaceTop p (InBind n bp sc) = InBind n (replaceTopB p bp) sc
50 where
51 replaceTopB p (Binder b) = Binder (fmap (replaceTop p) b)
52 replaceTopB p (LetT t v) = LetT (replaceTop p t) v
53 replaceTopB p (LetV t v) = LetV t (replaceTop p v)
54 replaceTopB p (GuessT t v) = GuessT (replaceTop p t) v
55 replaceTopB p (GuessV t v) = GuessV t (replaceTop p v)
56replaceTop p (InScope n b sc) = InScope n b (replaceTop p sc)
58-- | Build a term from a zipper, given something to put in the hole.
59rebuildTerm :: Term -> TermPath -> Term
60rebuildTerm tm Top = tm
61rebuildTerm tm (AppL s p a) = App s (rebuildTerm tm p) a
62rebuildTerm tm (AppR s f p) = App s f (rebuildTerm tm p)
63rebuildTerm tm (InScope n b p) = Bind n b (rebuildTerm tm p)
64rebuildTerm tm (InBind n bp sc) = Bind n (rebuildBinder tm bp) sc
66-- | Build a binder from a zipper, given something to put in the hole.
67rebuildBinder :: Term -> BinderPath -> Binder Term
68rebuildBinder tm (Binder p) = fmap (rebuildTerm tm) p
69rebuildBinder tm (LetT p t) = Let (rebuildTerm tm p) t
70rebuildBinder tm (LetV v p) = Let v (rebuildTerm tm p)
71rebuildBinder tm (GuessT p t) = Guess (rebuildTerm tm p) t
72rebuildBinder tm (GuessV v p) = Guess v (rebuildTerm tm p)
74-- | Find the binding of a hole in a term. If present, return the path
75-- to the hole's binding, the environment extended by the binders that
76-- are crossed, and the actual binding term.
77findHole :: Name -> Env -> Term -> Maybe (TermPath, Env, Term)
78findHole n env t = fh' env Top t where
79 fh' env path tm@(Bind x h sc)
80 | hole h && n == x = Just (path, env, tm)
81 fh' env path (App Complete _ _) = Nothing
82 fh' env path (App s f a)
83 | Just (p, env', tm) <- fh' env path a = Just (AppR s f p, env', tm)
84 | Just (p, env', tm) <- fh' env path f = Just (AppL s p a, env', tm)
85 fh' env path (Bind x b sc)
86 | Just (bp, env', tm) <- fhB env path b = Just (InBind x bp sc, env', tm)
87 | Just (p, env', tm) <- fh' ((x,RigW,b):env) path sc = Just (InScope x b p, env', tm)
88 fh' _ _ _ = Nothing
90 fhB env path (Let t v)
91 | Just (p, env', tm) <- fh' env path t = Just (LetT p v, env', tm)
92 | Just (p, env', tm) <- fh' env path v = Just (LetV t p, env', tm)
93 fhB env path (Guess t v)
94 | Just (p, env', tm) <- fh' env path t = Just (GuessT p v, env', tm)
95 | Just (p, env', tm) <- fh' env path v = Just (GuessV t p, env', tm)
96 fhB env path b
97 | Just (p, env', tm) <- fh' env path (binderTy b)
98 = Just (Binder (fmap (\_ -> p) b), env', tm)
99 fhB _ _ _ = Nothing
101data ProofTerm = PT { -- wholeterm :: Term,
102 path :: TermPath,
103 subterm_env :: Env,
104 subterm :: Term,
105 updates :: [(Name, Term)] }
106 deriving Show
108type RunTactic' a = Context -> Env -> Term -> StateT a TC Term
109type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
111-- | Refocus the proof term zipper on a particular hole, if it
112-- exists. If not, return the original proof term.
113refocus :: Hole -> ProofTerm -> ProofTerm
114refocus h t = let res = refocus' h t in res
115-- trace ("OLD: " ++ show t ++ "\n" ++
116-- "REFOCUSSED " ++ show h ++ ": " ++ show res) res
117refocus' (Just n) pt@(PT path env tm ups)
118 -- First look for the hole in the proof term as-is
119 | Just (p', env', tm') <- findHole n env tm
120 = PT (replaceTop p' path) env' tm' ups
121 -- If it's not there, rebuild and look from the top
122 | Just (p', env', tm') <- findHole n [] (rebuildTerm tm (updateSolvedPath ups path))
123 = PT p' env' tm' []
124 | otherwise = pt
125refocus' _ pt = pt
127data Goal = GD { premises :: Env,
128 goalType :: Binder Term
129 }
131mkProofTerm :: Term -> ProofTerm
132mkProofTerm tm = PT Top [] tm []
134getProofTerm :: ProofTerm -> Term
135getProofTerm (PT path _ sub ups) = rebuildTerm sub (updateSolvedPath ups path)
137resetProofTerm :: ProofTerm -> ProofTerm
138resetProofTerm = mkProofTerm . getProofTerm
140same :: Eq a => Maybe a -> a -> Bool
141same Nothing n = True
142same (Just x) n = x == n
144-- | Is a particular binder a hole or a guess?
145hole :: Binder b -> Bool
146hole (Hole _) = True
147hole (Guess _ _) = True
148hole _ = False
150-- | Given a list of solved holes, fill out the solutions in a term
151updateSolvedTerm :: [(Name, Term)] -> Term -> Term
152updateSolvedTerm xs x = fst $ updateSolvedTerm' xs x
154-- | Given a list of solved holes, fill out the solutions in a
155-- term. Return whether updates were performed, to facilitate sharing
156-- when there are no updates.
157updateSolvedTerm' :: [(Name, Term)] -> Term -> (Term, Bool)
158updateSolvedTerm' [] x = (x, False)
159updateSolvedTerm' xs x = updateSolved' xs x where
160 -- This version below saves allocations, because it doesn't need to reallocate
161 -- the term if there are no updates to do.
162 -- The Bool is ugly, and probably 'Maybe' would be less ugly, but >>= is
163 -- the wrong combinator. Feel free to tidy up as long as it's still as cheap :).
164 updateSolved' [] x = (x, False)
165 updateSolved' xs (Bind n (Hole ty) t)
166 | Just v <- lookup n xs
167 = case xs of
168 [_] -> (updsubst n v t, True) -- some may be Vs! Can't assume
169 -- explicit names
170 _ -> let (t', _) = updateSolved' xs t in
171 (updsubst n v t', True)
172 updateSolved' xs tm@(Bind n b t)
173 = let (t', ut) = updateSolved' xs t
174 (b', ub) = updateSolvedB' xs b in
175 if ut || ub then (Bind n b' t', True)
176 else (tm, False)
177 updateSolved' xs t@(App Complete f a) = (t, False)
178 updateSolved' xs t@(App s f a)
179 = let (f', uf) = updateSolved' xs f
180 (a', ua) = updateSolved' xs a in
181 if uf || ua then (App s f' a', True)
182 else (t, False)
183 updateSolved' xs t@(P _ n@(MN _ _) _)
184 | Just v <- lookup n xs = (v, True)
185 updateSolved' xs t@(P nt n ty)
186 = let (ty', ut) = updateSolved' xs ty in
187 if ut then (P nt n ty', True) else (t, False)
188 updateSolved' xs t = (t, False)
190 updateSolvedB' xs b@(Let t v) = let (t', ut) = updateSolved' xs t
191 (v', uv) = updateSolved' xs v in
192 if ut || uv then (Let t' v', True)
193 else (b, False)
194 updateSolvedB' xs b@(Guess t v) = let (t', ut) = updateSolved' xs t
195 (v', uv) = updateSolved' xs v in
196 if ut || uv then (Guess t' v', True)
197 else (b, False)
198 updateSolvedB' xs b = let (ty', u) = updateSolved' xs (binderTy b) in
199 if u then (b { binderTy = ty' }, u) else (b, False)
201 noneOf ns (P _ n _) | n `elem` ns = False
202 noneOf ns (App s f a) = noneOf ns a && noneOf ns f
203 noneOf ns (Bind n (Hole ty) t) = n `notElem` ns && noneOf ns ty && noneOf ns t
204 noneOf ns (Bind n b t) = noneOf ns t && noneOfB ns b
205 where
206 noneOfB ns (Let t v) = noneOf ns t && noneOf ns v
207 noneOfB ns (Guess t v) = noneOf ns t && noneOf ns v
208 noneOfB ns b = noneOf ns (binderTy b)
209 noneOf ns _ = True
211-- | As 'subst', in TT, but takes advantage of knowing not to substitute
212-- under Complete applications.
213updsubst :: Eq n => n {-^ The id to replace -} ->
214 TT n {-^ The replacement term -} ->
215 TT n {-^ The term to replace in -} ->
216 TT n
217-- subst n v tm = instantiate v (pToV n tm)
218updsubst n v tm = fst $ subst' 0 tm
219 where
220 -- keep track of updates to save allocations - this is a big win on
221 -- large terms in particular
222 -- ('Maybe' would be neater here, but >>= is not the right combinator.
223 -- Feel free to tidy up, as long as it still saves allocating when no
224 -- substitution happens...)
225 subst' i (V x) | i == x = (v, True)
226 subst' i (P _ x _) | n == x = (v, True)
227 subst' i t@(P nt x ty)
228 = let (ty', ut) = subst' i ty in
229 if ut then (P nt x ty', True) else (t, False)
230 subst' i t@(Bind x b sc) | x /= n
231 = let (b', ub) = substB' i b
232 (sc', usc) = subst' (i+1) sc in
233 if ub || usc then (Bind x b' sc', True) else (t, False)
234 subst' i t@(App Complete f a) = (t, False)
235 subst' i t@(App s f a) = let (f', uf) = subst' i f
236 (a', ua) = subst' i a in
237 if uf || ua then (App s f' a', True) else (t, False)
238 subst' i t@(Proj x idx) = let (x', u) = subst' i x in
239 if u then (Proj x' idx, u) else (t, False)
240 subst' i t = (t, False)
242 substB' i b@(Let t v) = let (t', ut) = subst' i t
243 (v', uv) = subst' i v in
244 if ut || uv then (Let t' v', True)
245 else (b, False)
246 substB' i b@(Guess t v) = let (t', ut) = subst' i t
247 (v', uv) = subst' i v in
248 if ut || uv then (Guess t' v', True)
249 else (b, False)
250 substB' i b = let (ty', u) = subst' i (binderTy b) in
251 if u then (b { binderTy = ty' }, u) else (b, False)
254-- | Apply solutions to an environment.
255updateEnv :: [(Name, Term)] -> Env -> Env
256updateEnv [] e = e
257updateEnv ns [] = []
258updateEnv ns ((n, r, b) : env) = (n, r, fmap (updateSolvedTerm ns) b) : updateEnv ns env
260-- | Fill out solved holes in a term zipper.
261updateSolvedPath :: [(Name, Term)] -> TermPath -> TermPath
262updateSolvedPath [] t = t
263updateSolvedPath ns Top = Top
264updateSolvedPath ns t@(AppL Complete _ _) = t
265updateSolvedPath ns t@(AppR Complete _ _) = t
266updateSolvedPath ns (AppL s p r) = AppL s (updateSolvedPath ns p) (updateSolvedTerm ns r)
267updateSolvedPath ns (AppR s l p) = AppR s (updateSolvedTerm ns l) (updateSolvedPath ns p)
268updateSolvedPath ns (InBind n b sc)
269 = InBind n (updateSolvedPathB b) (updateSolvedTerm ns sc)
270 where
271 updateSolvedPathB (Binder b) = Binder (fmap (updateSolvedPath ns) b)
272 updateSolvedPathB (LetT p v) = LetT (updateSolvedPath ns p) (updateSolvedTerm ns v)
273 updateSolvedPathB (LetV v p) = LetV (updateSolvedTerm ns v) (updateSolvedPath ns p)
274 updateSolvedPathB (GuessT p v) = GuessT (updateSolvedPath ns p) (updateSolvedTerm ns v)
275 updateSolvedPathB (GuessV v p) = GuessV (updateSolvedTerm ns v) (updateSolvedPath ns p)
276updateSolvedPath ns (InScope n (Hole ty) t)
277 | Just v <- lookup n ns = case ns of
278 [_] -> updateSolvedPath [(n,v)] t
279 _ -> updateSolvedPath ns $
280 updateSolvedPath [(n,v)] t
281updateSolvedPath ns (InScope n b sc)
282 = InScope n (fmap (updateSolvedTerm ns) b) (updateSolvedPath ns sc)
284updateSolved :: [(Name, Term)] -> ProofTerm -> ProofTerm
285updateSolved xs pt@(PT path env sub ups)
286 = PT path -- (updateSolvedPath xs path)
287 (updateEnv xs (filter (\(n, r, t) -> n `notElem` map fst xs) env))
288 (updateSolvedTerm xs sub)
289 (ups ++ xs)
291goal :: Hole -> ProofTerm -> TC Goal
292goal h pt@(PT path env sub ups)
293-- | OK ginf <- g env sub = return ginf
294 | otherwise = g [] (rebuildTerm sub (updateSolvedPath ups path))
295 where
296 g :: Env -> Term -> TC Goal
297 g env (Bind n b@(Guess _ _) sc)
298 | same h n = return $ GD env b
299 | otherwise
300 = gb env b `mplus` g ((n, RigW, b):env) sc
301 g env (Bind n b sc) | hole b && same h n = return $ GD env b
302 | otherwise
303 = g ((n, RigW, b):env) sc `mplus` gb env b
304 g env (App Complete f a) = fail "Can't find hole"
305 g env (App _ f a) = g env a `mplus` g env f
306 g env t = fail "Can't find hole"
308 gb env (Let t v) = g env v `mplus` g env t
309 gb env (Guess t v) = g env v `mplus` g env t
310 gb env t = g env (binderTy t)
312atHole :: Hole -> RunTactic' a -> Context -> Env -> ProofTerm ->
313 StateT a TC (ProofTerm, Bool)
314atHole h f c e pt -- @(PT path env sub)
315 = do let PT path env sub ups = refocus h pt
316 (tm, u) <- atH f c env sub
317 return (PT path env tm ups, u)
318-- if u then return (PT path env tm ups, u)
319-- else do let PT path env sub ups = refocus h pt
320-- (tm, u) <- atH f c env sub
321-- return (PT path env tm ups, u)
322 where
323 updated o = do o' <- o
324 return (o', True)
326 ulift2 f c env op a b
327 = do (b', u) <- atH f c env b
328 if u then return (op a b', True)
329 else do (a', u) <- atH f c env a
330 return (op a' b', u)
332 -- Search the things most likely to contain the binding first!
334 atH :: RunTactic' a -> Context -> Env -> Term -> StateT a TC (Term, Bool)
335 atH f c env binder@(Bind n b@(Guess t v) sc)
336 | same h n = updated (f c env binder)
337 | otherwise
338 = do -- binder first
339 (b', u) <- ulift2 f c env Guess t v
340 if u then return (Bind n b' sc, True)
341 else do (sc', u) <- atH f c ((n, RigW, b) : env) sc
342 return (Bind n b' sc', u)
343 atH f c env binder@(Bind n b sc)
344 | hole b && same h n = updated (f c env binder)
345 | otherwise -- scope first
346 = do (sc', u) <- atH f c ((n, RigW, b) : env) sc
347 if u then return (Bind n b sc', True)
348 else do (b', u) <- atHb f c env b
349 return (Bind n b' sc', u)
350 atH tac c env (App s f a) = ulift2 tac c env (App s) f a
351 atH tac c env t = return (t, False)
353 atHb f c env (Let t v) = ulift2 f c env Let t v
354 atHb f c env (Guess t v) = ulift2 f c env Guess t v
355 atHb f c env t = do (ty', u) <- atH f c env (binderTy t)
356 return (t { binderTy = ty' }, u)
358bound_in :: ProofTerm -> [Name]
359bound_in (PT path _ tm ups) = bound_in_term (rebuildTerm tm (updateSolvedPath ups path))
361bound_in_term :: Term -> [Name]
362bound_in_term (Bind n b sc) = n : bi b ++ bound_in_term sc
363 where
364 bi (Let t v) = bound_in_term t ++ bound_in_term v
365 bi (Guess t v) = bound_in_term t ++ bound_in_term v
366 bi b = bound_in_term (binderTy b)
367bound_in_term (App _ f a) = bound_in_term f ++ bound_in_term a
368bound_in_term _ = []