1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, PatternGuards #-}
3{- | Implements a proof state, some primitive tactics for manipulating
4proofs, and some high level commands for introducing new theorems,
5evaluation/checking inside the proof system, etc.
8module Idris.Core.ProofTerm(ProofTerm, Goal(..), mkProofTerm, getProofTerm,
9 updateSolved, updateSolvedTerm, updateSolvedTerm',
10 bound_in, bound_in_term, refocus, updsubst,
14import Idris.Core.Typecheck
15import Idris.Core.Evaluate
18import Control.Monad.State.Strict
22-- | A zipper over terms, in order to efficiently update proof terms.
24 | AppL (AppStatus Name) TermPath Term
25 | AppR (AppStatus Name) Term TermPath
26 | InBind Name BinderPath Term
27 | InScope Name (Binder Term) TermPath
30-- | A zipper over binders, because terms and binders are mutually defined.
31data BinderPath = Binder (Binder TermPath)
34 | GuessT TermPath Term
35 | GuessV Term TermPath
38-- | Replace the top of a term path with another term path. In other
39-- words, "graft" one term path into another.
40replaceTop :: TermPath -> TermPath -> TermPath
42replaceTop p (AppL s l t) = AppL s (replaceTop p l) t
43replaceTop p (AppR s t r) = AppR s t (replaceTop p r)
44replaceTop p (InBind n bp sc) = InBind n (replaceTopB p bp) sc
46 replaceTopB p (Binder b) = Binder (fmap (replaceTop p) b)
47 replaceTopB p (LetT t v) = LetT (replaceTop p t) v
48 replaceTopB p (LetV t v) = LetV t (replaceTop p v)
49 replaceTopB p (GuessT t v) = GuessT (replaceTop p t) v
50 replaceTopB p (GuessV t v) = GuessV t (replaceTop p v)
51replaceTop p (InScope n b sc) = InScope n b (replaceTop p sc)
53-- | Build a term from a zipper, given something to put in the hole.
54rebuildTerm :: Term -> TermPath -> Term
55rebuildTerm tm Top = tm
56rebuildTerm tm (AppL s p a) = App s (rebuildTerm tm p) a
57rebuildTerm tm (AppR s f p) = App s f (rebuildTerm tm p)
58rebuildTerm tm (InScope n b p) = Bind n b (rebuildTerm tm p)
59rebuildTerm tm (InBind n bp sc) = Bind n (rebuildBinder tm bp) sc
61-- | Build a binder from a zipper, given something to put in the hole.
62rebuildBinder :: Term -> BinderPath -> Binder Term
63rebuildBinder tm (Binder p) = fmap (rebuildTerm tm) p
64rebuildBinder tm (LetT p t) = Let (rebuildTerm tm p) t
65rebuildBinder tm (LetV v p) = Let v (rebuildTerm tm p)
66rebuildBinder tm (GuessT p t) = Guess (rebuildTerm tm p) t
67rebuildBinder tm (GuessV v p) = Guess v (rebuildTerm tm p)
69-- | Find the binding of a hole in a term. If present, return the path
70-- to the hole's binding, the environment extended by the binders that
71-- are crossed, and the actual binding term.
72findHole :: Name -> Env -> Term -> Maybe (TermPath, Env, Term)
73findHole n env t = fh' env Top t where
74 fh' env path tm@(Bind x h sc)
75 | hole h && n == x = Just (path, env, tm)
76 fh' env path (App Complete _ _) = Nothing
77 fh' env path (App s f a)
78 | Just (p, env', tm) <- fh' env path a = Just (AppR s f p, env', tm)
79 | Just (p, env', tm) <- fh' env path f = Just (AppL s p a, env', tm)
80 fh' env path (Bind x b sc)
81 | Just (bp, env', tm) <- fhB env path b = Just (InBind x bp sc, env', tm)
82 | Just (p, env', tm) <- fh' ((x,b):env) path sc = Just (InScope x b p, env', tm)
85 fhB env path (Let t v)
86 | Just (p, env', tm) <- fh' env path t = Just (LetT p v, env', tm)
87 | Just (p, env', tm) <- fh' env path v = Just (LetV t p, env', tm)
88 fhB env path (Guess t v)
89 | Just (p, env', tm) <- fh' env path t = Just (GuessT p v, env', tm)
90 | Just (p, env', tm) <- fh' env path v = Just (GuessV t p, env', tm)
92 | Just (p, env', tm) <- fh' env path (binderTy b)
93 = Just (Binder (fmap (\_ -> p) b), env', tm)
96data ProofTerm = PT { -- wholeterm :: Term,
100 updates :: [(Name, Term)] }
103type RunTactic' a = Context -> Env -> Term -> StateT a TC Term
104type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
106-- | Refocus the proof term zipper on a particular hole, if it
107-- exists. If not, return the original proof term.
108refocus :: Hole -> ProofTerm -> ProofTerm
109refocus h t = let res = refocus' h t in res
110-- trace ("OLD: " ++ show t ++ "\n" ++
111-- "REFOCUSSED " ++ show h ++ ": " ++ show res) res
112refocus' (Just n) pt@(PT path env tm ups)
113 -- First look for the hole in the proof term as-is
114 | Just (p', env', tm') <- findHole n env tm
115 = PT (replaceTop p' path) env' tm' ups
116 -- If it's not there, rebuild and look from the top
117 | Just (p', env', tm') <- findHole n [] (rebuildTerm tm (updateSolvedPath ups path))
122data Goal = GD { premises :: Env,
123 goalType :: Binder Term
126mkProofTerm :: Term -> ProofTerm
127mkProofTerm tm = PT Top [] tm []
129getProofTerm :: ProofTerm -> Term
130getProofTerm (PT path _ sub ups) = rebuildTerm sub (updateSolvedPath ups path)
132same :: Eq a => Maybe a -> a -> Bool
134same (Just x) n = x == n
136-- | Is a particular binder a hole or a guess?
137hole :: Binder b -> Bool
139hole (Guess _ _) = True
142-- | Given a list of solved holes, fill out the solutions in a term
143updateSolvedTerm :: [(Name, Term)] -> Term -> Term
144updateSolvedTerm xs x = fst $ updateSolvedTerm' xs x
146-- | Given a list of solved holes, fill out the solutions in a
147-- term. Return whether updates were performed, to facilitate sharing
148-- when there are no updates.
149updateSolvedTerm' :: [(Name, Term)] -> Term -> (Term, Bool)
150updateSolvedTerm' [] x = (x, False)
151updateSolvedTerm' xs x = updateSolved' xs x where
152 -- This version below saves allocations, because it doesn't need to reallocate
153 -- the term if there are no updates to do.
154 -- The Bool is ugly, and probably 'Maybe' would be less ugly, but >>= is
155 -- the wrong combinator. Feel free to tidy up as long as it's still as cheap :).
156 updateSolved' [] x = (x, False)
157 updateSolved' xs (Bind n (Hole ty) t)
158 | Just v <- lookup n xs
160 [_] -> (updsubst n v t, True) -- some may be Vs! Can't assume
162 _ -> let (t', _) = updateSolved' xs t in
163 (updsubst n v t', True)
164 updateSolved' xs tm@(Bind n b t)
165 | otherwise = let (t', ut) = updateSolved' xs t
166 (b', ub) = updateSolvedB' xs b in
167 if ut || ub then (Bind n b' t', True)
169 updateSolved' xs t@(App Complete f a) = (t, False)
170 updateSolved' xs t@(App s f a)
171 = let (f', uf) = updateSolved' xs f
172 (a', ua) = updateSolved' xs a in
173 if uf || ua then (App s f' a', True)
175 updateSolved' xs t@(P _ n@(MN _ _) _)
176 | Just v <- lookup n xs = (v, True)
177 updateSolved' xs t@(P nt n ty)
178 = let (ty', ut) = updateSolved' xs ty in
179 if ut then (P nt n ty', True) else (t, False)
180 updateSolved' xs t = (t, False)
182 updateSolvedB' xs b@(Let t v) = let (t', ut) = updateSolved' xs t
183 (v', uv) = updateSolved' xs v in
184 if ut || uv then (Let t' v', True)
186 updateSolvedB' xs b@(Guess t v) = let (t', ut) = updateSolved' xs t
187 (v', uv) = updateSolved' xs v in
188 if ut || uv then (Guess t' v', True)
190 updateSolvedB' xs b = let (ty', u) = updateSolved' xs (binderTy b) in
191 if u then (b { binderTy = ty' }, u) else (b, False)
193 noneOf ns (P _ n _) | n `elem` ns = False
194 noneOf ns (App s f a) = noneOf ns a && noneOf ns f
195 noneOf ns (Bind n (Hole ty) t) = n `notElem` ns && noneOf ns ty && noneOf ns t
196 noneOf ns (Bind n b t) = noneOf ns t && noneOfB ns b
198 noneOfB ns (Let t v) = noneOf ns t && noneOf ns v
199 noneOfB ns (Guess t v) = noneOf ns t && noneOf ns v
200 noneOfB ns b = noneOf ns (binderTy b)
203-- | As 'subst', in TT, but takes advantage of knowing not to substitute
204-- under Complete applications.
205updsubst :: Eq n => n {-^ The id to replace -} ->
206 TT n {-^ The replacement term -} ->
207 TT n {-^ The term to replace in -} ->
209-- subst n v tm = instantiate v (pToV n tm)
210updsubst n v tm = fst $ subst' 0 tm
212 -- keep track of updates to save allocations - this is a big win on
213 -- large terms in particular
214 -- ('Maybe' would be neater here, but >>= is not the right combinator.
215 -- Feel free to tidy up, as long as it still saves allocating when no
216 -- substitution happens...)
217 subst' i (V x) | i == x = (v, True)
218 subst' i (P _ x _) | n == x = (v, True)
219 subst' i t@(P nt x ty)
220 = let (ty', ut) = subst' i ty in
221 if ut then (P nt x ty', True) else (t, False)
222 subst' i t@(Bind x b sc) | x /= n
223 = let (b', ub) = substB' i b
224 (sc', usc) = subst' (i+1) sc in
225 if ub || usc then (Bind x b' sc', True) else (t, False)
226 subst' i t@(App Complete f a) = (t, False)
227 subst' i t@(App s f a) = let (f', uf) = subst' i f
228 (a', ua) = subst' i a in
229 if uf || ua then (App s f' a', True) else (t, False)
230 subst' i t@(Proj x idx) = let (x', u) = subst' i x in
231 if u then (Proj x' idx, u) else (t, False)
232 subst' i t = (t, False)
234 substB' i b@(Let t v) = let (t', ut) = subst' i t
235 (v', uv) = subst' i v in
236 if ut || uv then (Let t' v', True)
238 substB' i b@(Guess t v) = let (t', ut) = subst' i t
239 (v', uv) = subst' i v in
240 if ut || uv then (Guess t' v', True)
242 substB' i b = let (ty', u) = subst' i (binderTy b) in
243 if u then (b { binderTy = ty' }, u) else (b, False)
246-- | Apply solutions to an environment.
247updateEnv :: [(Name, Term)] -> Env -> Env
250updateEnv ns ((n, b) : env) = (n, fmap (updateSolvedTerm ns) b) : updateEnv ns env
252-- | Fill out solved holes in a term zipper.
253updateSolvedPath :: [(Name, Term)] -> TermPath -> TermPath
254updateSolvedPath [] t = t
255updateSolvedPath ns Top = Top
256updateSolvedPath ns t@(AppL Complete _ _) = t
257updateSolvedPath ns t@(AppR Complete _ _) = t
258updateSolvedPath ns (AppL s p r) = AppL s (updateSolvedPath ns p) (updateSolvedTerm ns r)
259updateSolvedPath ns (AppR s l p) = AppR s (updateSolvedTerm ns l) (updateSolvedPath ns p)
260updateSolvedPath ns (InBind n b sc)
261 = InBind n (updateSolvedPathB b) (updateSolvedTerm ns sc)
263 updateSolvedPathB (Binder b) = Binder (fmap (updateSolvedPath ns) b)
264 updateSolvedPathB (LetT p v) = LetT (updateSolvedPath ns p) (updateSolvedTerm ns v)
265 updateSolvedPathB (LetV v p) = LetV (updateSolvedTerm ns v) (updateSolvedPath ns p)
266 updateSolvedPathB (GuessT p v) = GuessT (updateSolvedPath ns p) (updateSolvedTerm ns v)
267 updateSolvedPathB (GuessV v p) = GuessV (updateSolvedTerm ns v) (updateSolvedPath ns p)
268updateSolvedPath ns (InScope n (Hole ty) t)
269 | Just v <- lookup n ns = case ns of
270 [_] -> updateSolvedPath [(n,v)] t
271 _ -> updateSolvedPath ns $
272 updateSolvedPath [(n,v)] t
273updateSolvedPath ns (InScope n b sc)
274 = InScope n (fmap (updateSolvedTerm ns) b) (updateSolvedPath ns sc)
276updateSolved :: [(Name, Term)] -> ProofTerm -> ProofTerm
277updateSolved xs pt@(PT path env sub ups)
278 = PT path -- (updateSolvedPath xs path)
279 (updateEnv xs (filter (\(n, t) -> n `notElem` map fst xs) env))
280 (updateSolvedTerm xs sub)
283goal :: Hole -> ProofTerm -> TC Goal
284goal h pt@(PT path env sub ups)
285-- | OK ginf <- g env sub = return ginf
286 | otherwise = g [] (rebuildTerm sub (updateSolvedPath ups path))
288 g :: Env -> Term -> TC Goal
289 g env (Bind n b@(Guess _ _) sc)
290 | same h n = return $ GD env b
292 = gb env b `mplus` g ((n, b):env) sc
293 g env (Bind n b sc) | hole b && same h n = return $ GD env b
295 = g ((n, b):env) sc `mplus` gb env b
296 g env (App Complete f a) = fail "Can't find hole"
297 g env (App _ f a) = g env a `mplus` g env f
298 g env t = fail "Can't find hole"
300 gb env (Let t v) = g env v `mplus` g env t
301 gb env (Guess t v) = g env v `mplus` g env t
302 gb env t = g env (binderTy t)
304atHole :: Hole -> RunTactic' a -> Context -> Env -> ProofTerm ->
305 StateT a TC (ProofTerm, Bool)
306atHole h f c e pt -- @(PT path env sub)
307 = do let PT path env sub ups = refocus h pt
308 (tm, u) <- atH f c env sub
309 return (PT path env tm ups, u)
310-- if u then return (PT path env tm ups, u)
311-- else do let PT path env sub ups = refocus h pt
312-- (tm, u) <- atH f c env sub
313-- return (PT path env tm ups, u)
315 updated o = do o' <- o
318 ulift2 f c env op a b
319 = do (b', u) <- atH f c env b
320 if u then return (op a b', True)
321 else do (a', u) <- atH f c env a
324 -- Search the things most likely to contain the binding first!
326 atH :: RunTactic' a -> Context -> Env -> Term -> StateT a TC (Term, Bool)
327 atH f c env binder@(Bind n b@(Guess t v) sc)
328 | same h n = updated (f c env binder)
331 (b', u) <- ulift2 f c env Guess t v
332 if u then return (Bind n b' sc, True)
333 else do (sc', u) <- atH f c ((n, b) : env) sc
334 return (Bind n b' sc', u)
335 atH f c env binder@(Bind n b sc)
336 | hole b && same h n = updated (f c env binder)
337 | otherwise -- scope first
338 = do (sc', u) <- atH f c ((n, b) : env) sc
339 if u then return (Bind n b sc', True)
340 else do (b', u) <- atHb f c env b
341 return (Bind n b' sc', u)
342 atH tac c env (App s f a) = ulift2 tac c env (App s) f a
343 atH tac c env t = return (t, False)
345 atHb f c env (Let t v) = ulift2 f c env Let t v
346 atHb f c env (Guess t v) = ulift2 f c env Guess t v
347 atHb f c env t = do (ty', u) <- atH f c env (binderTy t)
348 return (t { binderTy = ty' }, u)
350bound_in :: ProofTerm -> [Name]
351bound_in (PT path _ tm ups) = bound_in_term (rebuildTerm tm (updateSolvedPath ups path))
353bound_in_term :: Term -> [Name]
354bound_in_term (Bind n b sc) = n : bi b ++ bound_in_term sc
356 bi (Let t v) = bound_in_term t ++ bound_in_term v
357 bi (Guess t v) = bound_in_term t ++ bound_in_term v
358 bi b = bound_in_term (binderTy b)
359bound_in_term (App _ f a) = bound_in_term f ++ bound_in_term a
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