on 53376fc help sign in
Idris-dev/src/Core/ProofState.hs
796 lines 33 kB history raw
1{-# LANGUAGE MultiParamTypeClasses, FlexibleInstances, PatternGuards #-}
3{- Implements a proof state, some primitive tactics for manipulating
4 proofs, and some high level commands for introducing new theorems,
5 evaluation/checking inside the proof system, etc. --}
7module Core.ProofState(ProofState(..), newProof, envAtFocus, goalAtFocus,
8 Tactic(..), Goal(..), processTactic,
9 dropGiven, keepGiven) where
11import Core.Typecheck
12import Core.Evaluate
13import Core.TT
14import Core.Unify
16import Control.Monad.State
17import Control.Applicative hiding (empty)
18import Data.List
19import Debug.Trace
21import Util.Pretty
23data ProofState = PS { thname :: Name,
24 holes :: [Name], -- holes still to be solved
25 usedns :: [Name], -- used names, don't use again
26 nextname :: Int, -- name supply
27 pterm :: Term, -- current proof term
28 ptype :: Type, -- original goal
29 dontunify :: [Name], -- explicitly given by programmer, leave it
30 unified :: (Name, [(Name, Term)]),
31 notunified :: [(Name, Term)],
32 solved :: Maybe (Name, Term),
33 problems :: Fails,
34 injective :: [Name],
35 deferred :: [Name], -- names we'll need to define
36 instances :: [Name], -- instance arguments (for type classes)
37 previous :: Maybe ProofState, -- for undo
38 context :: Context,
39 plog :: String,
40 unifylog :: Bool,
41 done :: Bool
42 }
44data Goal = GD { premises :: Env,
45 goalType :: Binder Term
46 }
48data Tactic = Attack
49 | Claim Name Raw
50 | Reorder Name
51 | Exact Raw
52 | Fill Raw
53 | MatchFill Raw
54 | PrepFill Name [Name]
55 | CompleteFill
56 | Regret
57 | Solve
58 | StartUnify Name
59 | EndUnify
60 | Compute
61 | ComputeLet Name
62 | Simplify
63 | HNF_Compute
64 | EvalIn Raw
65 | CheckIn Raw
66 | Intro (Maybe Name)
67 | IntroTy Raw (Maybe Name)
68 | Forall Name Raw
69 | LetBind Name Raw Raw
70 | ExpandLet Name Term
71 | Rewrite Raw
72 | Equiv Raw
73 | PatVar Name
74 | PatBind Name
75 | Focus Name
76 | Defer Name
77 | DeferType Name Raw [Name]
78 | Instance Name
79 | SetInjective Name
80 | MoveLast Name
81 | ProofState
82 | Undo
83 | QED
84 deriving Show
86-- Some utilites on proof and tactic states
88instance Show ProofState where
89 show (PS nm [] _ _ tm _ _ _ _ _ _ _ _ _ _ _ _ _ _)
90 = show nm ++ ": no more goals"
91 show (PS nm (h:hs) _ _ tm _ _ _ _ _ _ _ i _ _ ctxt _ _ _)
92 = let OK g = goal (Just h) tm
93 wkenv = premises g in
94 "Other goals: " ++ show hs ++ "\n" ++
95 showPs wkenv (reverse wkenv) ++ "\n" ++
96 "-------------------------------- (" ++ show nm ++
97 ") -------\n " ++
98 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
99 where showPs env [] = ""
100 showPs env ((n, Let t v):bs)
101 = " " ++ show n ++ " : " ++
102 showEnv env ({- normalise ctxt env -} t) ++ " = " ++
103 showEnv env ({- normalise ctxt env -} v) ++
104 "\n" ++ showPs env bs
105 showPs env ((n, b):bs)
106 = " " ++ show n ++ " : " ++
107 showEnv env ({- normalise ctxt env -} (binderTy b)) ++
108 "\n" ++ showPs env bs
109 showG ps (Guess t v) = showEnv ps ({- normalise ctxt ps -} t) ++
110 " =?= " ++ showEnv ps v
111 showG ps b = showEnv ps (binderTy b)
113instance Pretty ProofState where
114 pretty (PS nm [] _ _ trm _ _ _ _ _ _ _ _ _ _ _ _ _ _) =
115 if size nm > breakingSize then
116 pretty nm <> colon $$ nest nestingSize (text " no more goals.")
117 else
118 pretty nm <> colon <+> text " no more goals."
119 pretty p@(PS nm (h:hs) _ _ tm _ _ _ _ _ _ _ i _ _ ctxt _ _ _) =
120 let OK g = goal (Just h) tm in
121 let wkEnv = premises g in
122 text "Other goals" <+> colon <+> pretty hs $$
123 prettyPs wkEnv (reverse wkEnv) $$
124 text "---------- " <+> text "Focussing on" <> colon <+> pretty nm <+> text " ----------" $$
125 pretty h <+> colon <+> prettyGoal wkEnv (goalType g)
126 where
127 prettyGoal ps (Guess t v) =
128 if size v > breakingSize then
129 prettyEnv ps t <+> text "=?=" $$
130 nest nestingSize (prettyEnv ps v)
131 else
132 prettyEnv ps t <+> text "=?=" <+> prettyEnv ps v
133 prettyGoal ps b = prettyEnv ps $ binderTy b
135 prettyPs env [] = empty
136 prettyPs env ((n, Let t v):bs) =
137 nest nestingSize (pretty n <+> colon <+>
138 if size v > breakingSize then
139 prettyEnv env t <+> text "=" $$
140 nest nestingSize (prettyEnv env v) $$
141 nest nestingSize (prettyPs env bs)
142 else
143 prettyEnv env t <+> text "=" <+> prettyEnv env v $$
144 nest nestingSize (prettyPs env bs))
145 prettyPs env ((n, b):bs) =
146 if size (binderTy b) > breakingSize then
147 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+> prettyPs env bs)
148 else
149 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) $$
150 nest nestingSize (prettyPs env bs))
152same Nothing n = True
153same (Just x) n = x == n
155hole (Hole _) = True
156hole (Guess _ _) = True
157hole _ = False
159holeName i = MN i "hole"
161qshow :: Fails -> String
162qshow fs = show (map (\ (x, y, _, _) -> (x, y)) fs)
164match_unify' :: Context -> Env -> TT Name -> TT Name ->
165 StateT TState TC [(Name, TT Name)]
166match_unify' ctxt env topx topy =
167 do ps <- get
168 let dont = dontunify ps
169 u <- lift $ match_unify ctxt env topx topy dont (holes ps)
170 let (h, ns) = unified ps
171 put (ps { unified = (h, u ++ ns) })
172 return u
174unify' :: Context -> Env -> TT Name -> TT Name ->
175 StateT TState TC [(Name, TT Name)]
176unify' ctxt env topx topy =
177 do ps <- get
178 let dont = dontunify ps
179 (u, fails) <- traceWhen (unifylog ps) ("Trying " ++ show (topx, topy)) $
180 lift $ unify ctxt env topx topy dont (holes ps)
181 traceWhen (unifylog ps)
182 ("Unified " ++ show (topx, topy) ++ " without " ++ show dont ++
183 " in " ++ show env ++
184 "\nSolved: " ++ show u ++ "\nNew problems: " ++ qshow fails ++ "\nCurrent problems:\n"
185 ++ qshow (problems ps) ++ "\nHoles: " ++ show (holes ps) ++ "\n"
186-- ++ show (pterm ps)
187 ++ "\n----------") $
188 case fails of
189-- [] -> return u
190 err ->
191 do ps <- get
192 let (h, ns) = unified ps
193 let (ns', probs') = updateProblems (context ps) (u ++ ns)
194 (err ++ problems ps)
195 (injective ps)
196 (holes ps)
197 put (ps { problems = probs',
198 unified = (h, ns') })
199 return u
201getName :: Monad m => String -> StateT TState m Name
202getName tag = do ps <- get
203 let n = nextname ps
204 put (ps { nextname = n+1 })
205 return $ MN n tag
207action :: Monad m => (ProofState -> ProofState) -> StateT TState m ()
208action a = do ps <- get
209 put (a ps)
211addLog :: Monad m => String -> StateT TState m ()
212addLog str = action (\ps -> ps { plog = plog ps ++ str ++ "\n" })
214newProof :: Name -> Context -> Type -> ProofState
215newProof n ctxt ty = let h = holeName 0
216 ty' = vToP ty in
217 PS n [h] [] 1 (Bind h (Hole ty')
218 (P Bound h ty')) ty [] (h, []) []
219 Nothing [] []
220 [] []
221 Nothing ctxt "" False False
223type TState = ProofState -- [TacticAction])
224type RunTactic = Context -> Env -> Term -> StateT TState TC Term
225type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
227envAtFocus :: ProofState -> TC Env
228envAtFocus ps
229 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
230 return (premises g)
231 | otherwise = fail "No holes"
233goalAtFocus :: ProofState -> TC (Binder Type)
234goalAtFocus ps
235 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
236 return (goalType g)
237 | otherwise = error $ "No goal in " ++ show (holes ps) ++ show (pterm ps)
239goal :: Hole -> Term -> TC Goal
240goal h tm = g [] tm where
241 g env (Bind n b@(Guess _ _) sc)
242 | same h n = return $ GD env b
243 | otherwise
244 = gb env b `mplus` g ((n, b):env) sc
245 g env (Bind n b sc) | hole b && same h n = return $ GD env b
246 | otherwise
247 = g ((n, b):env) sc `mplus` gb env b
248 g env (App f a) = g env f `mplus` g env a
249 g env t = fail "Can't find hole"
251 gb env (Let t v) = g env v `mplus` g env t
252 gb env (Guess t v) = g env v `mplus` g env t
253 gb env t = g env (binderTy t)
255tactic :: Hole -> RunTactic -> StateT TState TC ()
256tactic h f = do ps <- get
257 (tm', _) <- atH (context ps) [] (pterm ps)
258 ps <- get -- might have changed while processing
259 put (ps { pterm = tm' })
260 where
261 updated o = do o' <- o
262 return (o', True)
264 ulift2 c env op a b
265 = do (b', u) <- atH c env b
266 if u then return (op a b', True)
267 else do (a', u) <- atH c env a
268 return (op a' b', u)
270 -- Search the things most likely to contain the binding first!
272 atH :: Context -> Env -> Term -> StateT TState TC (Term, Bool)
273 atH c env binder@(Bind n b@(Guess t v) sc)
274 | same h n = updated (f c env binder)
275 | otherwise
276 = do -- binder first
277 (b', u) <- ulift2 c env Guess t v
278 if u then return (Bind n b' sc, True)
279 else do (sc', u) <- atH c ((n, b) : env) sc
280 return (Bind n b' sc', u)
281 atH c env binder@(Bind n b sc)
282 | hole b && same h n = updated (f c env binder)
283 | otherwise -- scope first
284 = do (sc', u) <- atH c ((n, b) : env) sc
285 if u then return (Bind n b sc', True)
286 else do (b', u) <- atHb c env b
287 return (Bind n b' sc', u)
288 atH c env (App f a) = ulift2 c env App f a
289 atH c env t = return (t, False)
291 atHb c env (Let t v) = ulift2 c env Let t v
292 atHb c env (Guess t v) = ulift2 c env Guess t v
293 atHb c env t = do (ty', u) <- atH c env (binderTy t)
294 return (t { binderTy = ty' }, u)
296computeLet :: Context -> Name -> Term -> Term
297computeLet ctxt n tm = cl [] tm where
298 cl env (Bind n' (Let t v) sc)
299 | n' == n = let v' = normalise ctxt env v in
300 Bind n' (Let t v') sc
301 cl env (Bind n' b sc) = Bind n' (fmap (cl env) b) (cl ((n, b):env) sc)
302 cl env (App f a) = App (cl env f) (cl env a)
303 cl env t = t
305attack :: RunTactic
306attack ctxt env (Bind x (Hole t) sc)
307 = do h <- getName "hole"
308 action (\ps -> ps { holes = h : holes ps })
309 return $ Bind x (Guess t (newtm h)) sc
310 where
311 newtm h = Bind h (Hole t) (P Bound h t)
312attack ctxt env _ = fail "Not an attackable hole"
314claim :: Name -> Raw -> RunTactic
315claim n ty ctxt env t =
316 do (tyv, tyt) <- lift $ check ctxt env ty
317 lift $ isType ctxt env tyt
318 action (\ps -> let (g:gs) = holes ps in
319 ps { holes = g : n : gs } )
320 return $ Bind n (Hole tyv) t -- (weakenTm 1 t)
322reorder_claims :: RunTactic
323reorder_claims ctxt env t
324 = -- trace (showSep "\n" (map show (scvs t))) $
325 let (bs, sc) = scvs t []
326 newbs = reverse (sortB (reverse bs)) in
327 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
328 return (bindAll newbs sc)
329 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
330 scvs sc acc = (reverse acc, sc)
332 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
333 sortB [] = []
334 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
335 | otherwise = sortB (insertB x xs)
337 insertB x [] = [x]
338 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
339 | otherwise = y : insertB x ys
341 noOcc (n, _) (_, Let t v) = noOccurrence n t && noOccurrence n v
342 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
343 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
345focus :: Name -> RunTactic
346focus n ctxt env t = do action (\ps -> let hs = holes ps in
347 if n `elem` hs
348 then ps { holes = n : (hs \\ [n]) }
349 else ps)
350 ps <- get
351 return t
353movelast :: Name -> RunTactic
354movelast n ctxt env t = do action (\ps -> let hs = holes ps in
355 if n `elem` hs
356 then ps { holes = (hs \\ [n]) ++ [n] }
357 else ps)
358 return t
360instanceArg :: Name -> RunTactic
361instanceArg n ctxt env (Bind x (Hole t) sc)
362 = do action (\ps -> let hs = holes ps
363 is = instances ps in
364 ps { holes = (hs \\ [x]) ++ [x],
365 instances = x:is })
366 return (Bind x (Hole t) sc)
368setinj :: Name -> RunTactic
369setinj n ctxt env (Bind x (Hole t) sc)
370 = do action (\ps -> let is = injective ps in
371 ps { injective = n : is })
372 return (Bind x (Hole t) sc)
374defer :: Name -> RunTactic
375defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
376 do action (\ps -> let hs = holes ps in
377 ps { holes = hs \\ [x] })
378 return (Bind n (GHole (mkTy (reverse env) t))
379 (mkApp (P Ref n ty) (map getP (reverse env))))
380 where
381 mkTy [] t = t
382 mkTy ((n,b) : bs) t = Bind n (Pi (binderTy b)) (mkTy bs t)
384 getP (n, b) = P Bound n (binderTy b)
386-- as defer, but build the type and application explicitly
387deferType :: Name -> Raw -> [Name] -> RunTactic
388deferType n fty_in args ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
389 do (fty, _) <- lift $ check ctxt env fty_in
390 action (\ps -> let hs = holes ps
391 ds = deferred ps in
392 ps { holes = hs \\ [x],
393 deferred = n : ds })
394 return (Bind n (GHole fty)
395 (mkApp (P Ref n ty) (map getP args)))
396 where
397 getP n = case lookup n env of
398 Just b -> P Bound n (binderTy b)
399 Nothing -> error ("deferType can't find " ++ show n)
401regret :: RunTactic
402regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
403 do action (\ps -> let hs = holes ps in
404 ps { holes = hs \\ [x] })
405 return sc
406regret ctxt env (Bind x (Hole t) _)
407 = fail $ show x ++ " : " ++ show t ++ " is not solved..."
409exact :: Raw -> RunTactic
410exact guess ctxt env (Bind x (Hole ty) sc) =
411 do (val, valty) <- lift $ check ctxt env guess
412 lift $ converts ctxt env valty ty
413 return $ Bind x (Guess ty val) sc
414exact _ _ _ _ = fail "Can't fill here."
416-- As exact, but attempts to solve other goals by unification
418fill :: Raw -> RunTactic
419fill guess ctxt env (Bind x (Hole ty) sc) =
420 do (val, valty) <- lift $ check ctxt env guess
421-- let valtyn = normalise ctxt env valty
422-- let tyn = normalise ctxt env ty
423 ns <- unify' ctxt env valty ty
424 ps <- get
425 let (uh, uns) = unified ps
426-- put (ps { unified = (uh, uns ++ ns) })
427-- addLog (show (uh, uns ++ ns))
428 return $ Bind x (Guess ty val) sc
429fill _ _ _ _ = fail "Can't fill here."
431-- As fill, but attempts to solve other goals by matching
433match_fill :: Raw -> RunTactic
434match_fill guess ctxt env (Bind x (Hole ty) sc) =
435 do (val, valty) <- lift $ check ctxt env guess
436-- let valtyn = normalise ctxt env valty
437-- let tyn = normalise ctxt env ty
438 ns <- match_unify' ctxt env valty ty
439 ps <- get
440 let (uh, uns) = unified ps
441-- put (ps { unified = (uh, uns ++ ns) })
442-- addLog (show (uh, uns ++ ns))
443 return $ Bind x (Guess ty val) sc
444match_fill _ _ _ _ = fail "Can't fill here."
446prep_fill :: Name -> [Name] -> RunTactic
447prep_fill f as ctxt env (Bind x (Hole ty) sc) =
448 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
449 return $ Bind x (Guess ty val) sc
450prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
452complete_fill :: RunTactic
453complete_fill ctxt env (Bind x (Guess ty val) sc) =
454 do let guess = forget val
455 (val', valty) <- lift $ check ctxt env guess
456 ns <- unify' ctxt env valty ty
457 ps <- get
458 let (uh, uns) = unified ps
459-- put (ps { unified = (uh, uns ++ ns) })
460 return $ Bind x (Guess ty val) sc
461complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
463-- When solving something in the 'dont unify' set, we should check
464-- that the guess we are solving it with unifies with the thing unification
465-- found for it, if anything.
467solve :: RunTactic
468solve ctxt env (Bind x (Guess ty val) sc)
469 | True = do ps <- get
470 let (uh, uns) = unified ps
471 case lookup x (notunified ps) of
472 Just tm -> do -- trace ("AVOIDED " ++ show (x, tm, val)) $
473 -- return []
474 unify' ctxt env tm val
475 _ -> return []
476 action (\ps -> ps { holes = holes ps \\ [x],
477 solved = Just (x, val),
478-- problems = solveInProblems
479-- x val (problems ps),
480 -- dontunify = dontunify ps \\ [x],
481 -- unified = (uh, uns ++ [(x, val)]),
482 instances = instances ps \\ [x] })
483 let tm' = instantiate val (pToV x sc) in
484 return tm'
485 | otherwise = lift $ tfail $ IncompleteTerm val
486solve _ _ h@(Bind x t sc)
487 = do ps <- get
488 fail $ "Not a guess " ++ show h ++ "\n" ++ show (holes ps, pterm ps)
490introTy :: Raw -> Maybe Name -> RunTactic
491introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
492 do let n = case mn of
493 Just name -> name
494 Nothing -> x
495 let t' = case t of
496 x@(Bind y (Pi s) _) -> x
497 _ -> hnf ctxt env t
498 (tyv, tyt) <- lift $ check ctxt env ty
499-- ns <- lift $ unify ctxt env tyv t'
500 case t' of
501 Bind y (Pi s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
502 do ns <- unify' ctxt env s tyv
503 ps <- get
504 let (uh, uns) = unified ps
505-- put (ps { unified = (uh, uns ++ ns) })
506 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
507 _ -> lift $ tfail $ CantIntroduce t'
508introTy ty n ctxt env _ = fail "Can't introduce here."
510intro :: Maybe Name -> RunTactic
511intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
512 do let n = case mn of
513 Just name -> name
514 Nothing -> x
515 let t' = case t of
516 x@(Bind y (Pi s) _) -> x
517 _ -> hnf ctxt env t
518 case t' of
519 Bind y (Pi s) t -> -- trace ("in type " ++ show t') $
520 let t' = instantiate (P Bound n s) (pToV y t) in
521 return $ Bind n (Lam s) (Bind x (Hole t') (P Bound x t'))
522 _ -> lift $ tfail $ CantIntroduce t'
523intro n ctxt env _ = fail "Can't introduce here."
525forall :: Name -> Raw -> RunTactic
526forall n ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
527 do (tyv, tyt) <- lift $ check ctxt env ty
528 unify' ctxt env tyt (TType (UVar 0))
529 return $ Bind n (Pi tyv) (Bind x (Hole t) (P Bound x t))
530forall n ty ctxt env _ = fail "Can't pi bind here"
532patvar :: Name -> RunTactic
533patvar n ctxt env (Bind x (Hole t) sc) =
534 do action (\ps -> ps { holes = holes ps \\ [x] })
535 return $ Bind n (PVar t) (instantiate (P Bound n t) (pToV x sc))
536patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
538letbind :: Name -> Raw -> Raw -> RunTactic
539letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
540 do (tyv, tyt) <- lift $ check ctxt env ty
541 (valv, valt) <- lift $ check ctxt env val
542 lift $ isType ctxt env tyt
543 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
544letbind n ty val ctxt env _ = fail "Can't let bind here"
546expandLet :: Name -> Term -> RunTactic
547expandLet n v ctxt env tm =
548 return $ subst n v tm
550rewrite :: Raw -> RunTactic
551rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
552 do (tmv, tmt) <- lift $ check ctxt env tm
553 let tmt' = normalise ctxt env tmt
554 case unApply tmt' of
555 (P _ (UN "=") _, [lt,rt,l,r]) ->
556 do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
557 let newt = mkP l r l t
558 let sc = forget $ (Bind x (Hole newt)
559 (mkApp (P Ref (UN "replace") (TType (UVal 0)))
560 [lt, l, r, p, tmv, xp]))
561 (scv, sct) <- lift $ check ctxt env sc
562 return scv
563 _ -> fail "Not an equality type"
564 where
565 -- to make the P for rewrite, replace syntactic occurrences of l in ty with
566 -- an x, and put \x : lt in front
567 mkP lt l r ty | l == ty = lt
568 mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
569 a' = if (r /= a) then mkP lt l r a else a in
570 App f' a'
571 mkP lt l r (Bind n b sc)
572 = let b' = mkPB b
573 sc' = if (r /= sc) then mkP lt l r sc else sc in
574 Bind n b' sc'
575 where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
576 v' = if (r /= v) then mkP lt l r v else v in
577 Let t' v'
578 mkPB b = let ty = binderTy b
579 ty' = if (r /= ty) then mkP lt l r ty else ty in
580 b { binderTy = ty' }
581 mkP lt l r x = x
583 rname = MN 0 "replaced"
584rewrite _ _ _ _ = fail "Can't rewrite here"
586equiv :: Raw -> RunTactic
587equiv tm ctxt env (Bind x (Hole t) sc) =
588 do (tmv, tmt) <- lift $ check ctxt env tm
589 lift $ converts ctxt env tmv t
590 return $ Bind x (Hole tmv) sc
591equiv tm ctxt env _ = fail "Can't equiv here"
593patbind :: Name -> RunTactic
594patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
595 do let t' = case t of
596 x@(Bind y (PVTy s) t) -> x
597 _ -> hnf ctxt env t
598 case t' of
599 Bind y (PVTy s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
600 return $ Bind n (PVar s) (Bind x (Hole t') (P Bound x t'))
601 _ -> fail "Nothing to pattern bind"
602patbind n ctxt env _ = fail "Can't pattern bind here"
604compute :: RunTactic
605compute ctxt env (Bind x (Hole ty) sc) =
606 do return $ Bind x (Hole (normalise ctxt env ty)) sc
607compute ctxt env t = return t
609hnf_compute :: RunTactic
610hnf_compute ctxt env (Bind x (Hole ty) sc) =
611 do let ty' = hnf ctxt env ty in
612-- trace ("HNF " ++ show (ty, ty')) $
613 return $ Bind x (Hole ty') sc
614hnf_compute ctxt env t = return t
616-- reduce let bindings only
617simplify :: RunTactic
618simplify ctxt env (Bind x (Hole ty) sc) =
619 do return $ Bind x (Hole (specialise ctxt env [] ty)) sc
620simplify ctxt env t = return t
622check_in :: Raw -> RunTactic
623check_in t ctxt env tm =
624 do (val, valty) <- lift $ check ctxt env t
625 addLog (showEnv env val ++ " : " ++ showEnv env valty)
626 return tm
628eval_in :: Raw -> RunTactic
629eval_in t ctxt env tm =
630 do (val, valty) <- lift $ check ctxt env t
631 let val' = normalise ctxt env val
632 let valty' = normalise ctxt env valty
633 addLog (showEnv env val ++ " : " ++
634 showEnv env valty ++
635-- " in " ++ show env ++
636 " ==>\n " ++
637 showEnv env val' ++ " : " ++
638 showEnv env valty')
639 return tm
641start_unify :: Name -> RunTactic
642start_unify n ctxt env tm = do -- action (\ps -> ps { unified = (n, []) })
643 return tm
645tmap f (a, b, c) = (f a, b, c)
647solve_unified :: RunTactic
648solve_unified ctxt env tm =
649 do ps <- get
650 let (_, ns) = unified ps
651 let unify = dropGiven (dontunify ps) ns (holes ps)
652 action (\ps -> ps { holes = holes ps \\ map fst unify })
653 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
654 return (updateSolved unify tm)
655 where
657dropGiven du [] hs = []
658dropGiven du ((n, P Bound t ty) : us) hs
659 | n `elem` du && not (t `elem` du)
660 && n `elem` hs && t `elem` hs
661 = (t, P Bound n ty) : dropGiven du us hs
662dropGiven du (u@(n, _) : us) hs
663 | n `elem` du = dropGiven du us hs
664-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
665dropGiven du (u : us) hs = u : dropGiven du us hs
667keepGiven du [] hs = []
668keepGiven du ((n, P Bound t ty) : us) hs
669 | n `elem` du && not (t `elem` du)
670 && n `elem` hs && t `elem` hs
671 = keepGiven du us hs
672keepGiven du (u@(n, _) : us) hs
673 | n `elem` du = u : keepGiven du us hs
674keepGiven du (u : us) hs = keepGiven du us hs
676updateSolved xs x = -- trace ("Updating " ++ show xs ++ " in " ++ show x) $
677 updateSolved' xs x
678updateSolved' xs (Bind n (Hole ty) t)
679 | Just v <- lookup n xs = instantiate v (pToV n (updateSolved' xs t))
680updateSolved' xs (Bind n b t)
681 | otherwise = Bind n (fmap (updateSolved' xs) b) (updateSolved' xs t)
682updateSolved' xs (App f a) = App (updateSolved' xs f) (updateSolved' xs a)
683updateSolved' xs (P _ n _)
684 | Just v <- lookup n xs = v
685updateSolved' xs t = t
687updateEnv ns [] = []
688updateEnv ns ((n, b) : env) = (n, fmap (updateSolved ns) b) : updateEnv ns env
690updateError ns (CantUnify b l r e xs sc)
691 = CantUnify b (updateSolved ns l) (updateSolved ns r) (updateError ns e) xs sc
692updateError ns e = e
694solveInProblems x val [] = []
695solveInProblems x val ((l, r, env, err) : ps)
696 = ((instantiate val (pToV x l), instantiate val (pToV x r),
697 updateEnv [(x, val)] env, err) : solveInProblems x val ps)
699updateProblems ctxt ns ps inj holes = up ns ps where
700 up ns [] = (ns, [])
701 up ns ((x, y, env, err) : ps) =
702 let x' = updateSolved ns x
703 y' = updateSolved ns y
704 err' = updateError ns err
705 env' = updateEnv ns env in
706 case unify ctxt env' x' y' inj holes of
707 OK (v, []) -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
708 up (ns ++ v) ps
709 _ -> let (ns', ps') = up ns ps in
710 (ns', (x',y',env',err') : ps')
712processTactic :: Tactic -> ProofState -> TC (ProofState, String)
713processTactic QED ps = case holes ps of
714 [] -> do let tm = {- normalise (context ps) [] -} (pterm ps)
715 (tm', ty', _) <- recheck (context ps) [] (forget tm) tm
716 return (ps { done = True, pterm = tm' },
717 "Proof complete: " ++ showEnv [] tm')
718 _ -> fail "Still holes to fill."
719processTactic ProofState ps = return (ps, showEnv [] (pterm ps))
720processTactic Undo ps = case previous ps of
721 Nothing -> fail "Nothing to undo."
722 Just pold -> return (pold, "")
723processTactic EndUnify ps
724 = let (h, ns_in) = unified ps
725 ns = dropGiven (dontunify ps) ns_in (holes ps)
726 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
727 (ns'', probs') = updateProblems (context ps) ns' (problems ps)
728 (injective ps) (holes ps)
729 tm' = -- trace ("Updating " ++ show ns_in ++ "\n" ++ show ns'') $ -- ++ " in " ++ show (pterm ps)) $
730 updateSolved ns'' (pterm ps) in
731 return (ps { pterm = tm',
732 unified = (h, []),
733 problems = probs',
734 holes = holes ps \\ map fst ns'' }, "")
735processTactic (Reorder n) ps
736 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
737 return (ps' { previous = Just ps, plog = "" }, plog ps')
738processTactic (ComputeLet n) ps
739 = return (ps { pterm = computeLet (context ps) n (pterm ps) }, "")
740processTactic t ps
741 = case holes ps of
742 [] -> fail "Nothing to fill in."
743 (h:_) -> do ps' <- execStateT (process t h) ps
744 let (ns', probs')
745 = case solved ps' of
746 Just s -> updateProblems (context ps')
747 [s] (problems ps')
748 (injective ps')
749 (holes ps')
750 _ -> ([], problems ps')
751 -- rechecking problems may find more solutions, so
752 -- apply them here
753 let pterm'' = updateSolved ns' (pterm ps')
754 return (ps' { pterm = pterm'',
755 solved = Nothing,
756 problems = probs',
757 previous = Just ps, plog = "",
758 holes = holes ps' \\ (map fst ns')}, plog ps')
760process :: Tactic -> Name -> StateT TState TC ()
761process EndUnify _
762 = do ps <- get
763 let (h, _) = unified ps
764 tactic (Just h) solve_unified
765process t h = tactic (Just h) (mktac t)
766 where mktac Attack = attack
767 mktac (Claim n r) = claim n r
768 mktac (Exact r) = exact r
769 mktac (Fill r) = fill r
770 mktac (MatchFill r) = match_fill r
771 mktac (PrepFill n ns) = prep_fill n ns
772 mktac CompleteFill = complete_fill
773 mktac Regret = regret
774 mktac Solve = solve
775 mktac (StartUnify n) = start_unify n
776 mktac Compute = compute
777 mktac Simplify = Core.ProofState.simplify
778 mktac HNF_Compute = hnf_compute
779 mktac (Intro n) = intro n
780 mktac (IntroTy ty n) = introTy ty n
781 mktac (Forall n t) = forall n t
782 mktac (LetBind n t v) = letbind n t v
783 mktac (ExpandLet n b) = expandLet n b
784 mktac (Rewrite t) = rewrite t
785 mktac (Equiv t) = equiv t
786 mktac (PatVar n) = patvar n
787 mktac (PatBind n) = patbind n
788 mktac (CheckIn r) = check_in r
789 mktac (EvalIn r) = eval_in r
790 mktac (Focus n) = focus n
791 mktac (Defer n) = defer n
792 mktac (DeferType n t a) = deferType n t a
793 mktac (Instance n) = instanceArg n
794 mktac (SetInjective n) = setinj n
795 mktac (MoveLast n) = movelast n