on 417f040 help sign in
Idris-dev/src/Core/ProofState.hs
772 lines 32 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) <- -- trace ("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 "\n" ++ show u ++ "\n" ++ qshow fails ++ "\nCurrent problems:\n"
185 ++ qshow (problems ps) ++ "\n" ++ 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 sc) | hole b && same h n = return $ GD env b
242 | otherwise
243 = gb env b `mplus` g ((n, b):env) sc
244 g env (App f a) = g env f `mplus` g env a
245 g env t = fail "Can't find hole"
247 gb env (Let t v) = g env t `mplus` g env v
248 gb env (Guess t v) = g env t `mplus` g env v
249 gb env t = g env (binderTy t)
251tactic :: Hole -> RunTactic -> StateT TState TC ()
252tactic h f = do ps <- get
253 tm' <- atH (context ps) [] (pterm ps)
254 ps <- get -- might have changed while processing
255 put (ps { pterm = tm' })
256 where
257 atH c env binder@(Bind n b sc)
258 | hole b && same h n = f c env binder
259 | otherwise
260 = liftM2 (Bind n) (atHb c env b) (atH c ((n, b) : env) sc)
261 atH c env (App f a) = liftM2 App (atH c env f) (atH c env a)
262 atH c env t = return t
264 atHb c env (Let t v) = liftM2 Let (atH c env t) (atH c env v)
265 atHb c env (Guess t v) = liftM2 Guess (atH c env t) (atH c env v)
266 atHb c env t = do ty' <- atH c env (binderTy t)
267 return $ t { binderTy = ty' }
269computeLet :: Context -> Name -> Term -> Term
270computeLet ctxt n tm = cl [] tm where
271 cl env (Bind n' (Let t v) sc)
272 | n' == n = let v' = normalise ctxt env v in
273 Bind n' (Let t v') sc
274 cl env (Bind n' b sc) = Bind n' (fmap (cl env) b) (cl ((n, b):env) sc)
275 cl env (App f a) = App (cl env f) (cl env a)
276 cl env t = t
278attack :: RunTactic
279attack ctxt env (Bind x (Hole t) sc)
280 = do h <- getName "hole"
281 action (\ps -> ps { holes = h : holes ps })
282 return $ Bind x (Guess t (newtm h)) sc
283 where
284 newtm h = Bind h (Hole t) (P Bound h t)
285attack ctxt env _ = fail "Not an attackable hole"
287claim :: Name -> Raw -> RunTactic
288claim n ty ctxt env t =
289 do (tyv, tyt) <- lift $ check ctxt env ty
290 lift $ isType ctxt env tyt
291 action (\ps -> let (g:gs) = holes ps in
292 ps { holes = g : n : gs } )
293 return $ Bind n (Hole tyv) t -- (weakenTm 1 t)
295reorder_claims :: RunTactic
296reorder_claims ctxt env t
297 = -- trace (showSep "\n" (map show (scvs t))) $
298 let (bs, sc) = scvs t []
299 newbs = reverse (sortB (reverse bs)) in
300 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
301 return (bindAll newbs sc)
302 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
303 scvs sc acc = (reverse acc, sc)
305 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
306 sortB [] = []
307 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
308 | otherwise = sortB (insertB x xs)
310 insertB x [] = [x]
311 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
312 | otherwise = y : insertB x ys
314 noOcc (n, _) (_, Let t v) = noOccurrence n t && noOccurrence n v
315 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
316 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
318focus :: Name -> RunTactic
319focus n ctxt env t = do action (\ps -> let hs = holes ps in
320 if n `elem` hs
321 then ps { holes = n : (hs \\ [n]) }
322 else ps)
323 ps <- get
324 return t
326movelast :: Name -> RunTactic
327movelast n ctxt env t = do action (\ps -> let hs = holes ps in
328 if n `elem` hs
329 then ps { holes = (hs \\ [n]) ++ [n] }
330 else ps)
331 return t
333instanceArg :: Name -> RunTactic
334instanceArg n ctxt env (Bind x (Hole t) sc)
335 = do action (\ps -> let hs = holes ps
336 is = instances ps in
337 ps { holes = (hs \\ [x]) ++ [x],
338 instances = x:is })
339 return (Bind x (Hole t) sc)
341setinj :: Name -> RunTactic
342setinj n ctxt env (Bind x (Hole t) sc)
343 = do action (\ps -> let is = injective ps in
344 ps { injective = n : is })
345 return (Bind x (Hole t) sc)
347defer :: Name -> RunTactic
348defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
349 do action (\ps -> let hs = holes ps in
350 ps { holes = hs \\ [x] })
351 return (Bind n (GHole (mkTy (reverse env) t))
352 (mkApp (P Ref n ty) (map getP (reverse env))))
353 where
354 mkTy [] t = t
355 mkTy ((n,b) : bs) t = Bind n (Pi (binderTy b)) (mkTy bs t)
357 getP (n, b) = P Bound n (binderTy b)
359-- as defer, but build the type and application explicitly
360deferType :: Name -> Raw -> [Name] -> RunTactic
361deferType n fty_in args ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
362 do (fty, _) <- lift $ check ctxt env fty_in
363 action (\ps -> let hs = holes ps
364 ds = deferred ps in
365 ps { holes = hs \\ [x],
366 deferred = n : ds })
367 return (Bind n (GHole fty)
368 (mkApp (P Ref n ty) (map getP args)))
369 where
370 getP n = case lookup n env of
371 Just b -> P Bound n (binderTy b)
372 Nothing -> error ("deferType can't find " ++ show n)
374regret :: RunTactic
375regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
376 do action (\ps -> let hs = holes ps in
377 ps { holes = hs \\ [x] })
378 return sc
379regret ctxt env (Bind x (Hole t) _)
380 = fail $ show x ++ " : " ++ show t ++ " is not solved..."
382exact :: Raw -> RunTactic
383exact guess ctxt env (Bind x (Hole ty) sc) =
384 do (val, valty) <- lift $ check ctxt env guess
385 lift $ converts ctxt env valty ty
386 return $ Bind x (Guess ty val) sc
387exact _ _ _ _ = fail "Can't fill here."
389-- As exact, but attempts to solve other goals by unification
391fill :: Raw -> RunTactic
392fill guess ctxt env (Bind x (Hole ty) sc) =
393 do (val, valty) <- lift $ check ctxt env guess
394-- let valtyn = normalise ctxt env valty
395-- let tyn = normalise ctxt env ty
396 ns <- unify' ctxt env valty ty
397 ps <- get
398 let (uh, uns) = unified ps
399-- put (ps { unified = (uh, uns ++ ns) })
400-- addLog (show (uh, uns ++ ns))
401 return $ Bind x (Guess ty val) sc
402fill _ _ _ _ = fail "Can't fill here."
404-- As fill, but attempts to solve other goals by matching
406match_fill :: Raw -> RunTactic
407match_fill guess ctxt env (Bind x (Hole ty) sc) =
408 do (val, valty) <- lift $ check ctxt env guess
409-- let valtyn = normalise ctxt env valty
410-- let tyn = normalise ctxt env ty
411 ns <- match_unify' ctxt env valty ty
412 ps <- get
413 let (uh, uns) = unified ps
414-- put (ps { unified = (uh, uns ++ ns) })
415-- addLog (show (uh, uns ++ ns))
416 return $ Bind x (Guess ty val) sc
417match_fill _ _ _ _ = fail "Can't fill here."
419prep_fill :: Name -> [Name] -> RunTactic
420prep_fill f as ctxt env (Bind x (Hole ty) sc) =
421 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
422 return $ Bind x (Guess ty val) sc
423prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
425complete_fill :: RunTactic
426complete_fill ctxt env (Bind x (Guess ty val) sc) =
427 do let guess = forget val
428 (val', valty) <- lift $ check ctxt env guess
429 ns <- unify' ctxt env valty ty
430 ps <- get
431 let (uh, uns) = unified ps
432-- put (ps { unified = (uh, uns ++ ns) })
433 return $ Bind x (Guess ty val) sc
434complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
436-- When solving something in the 'dont unify' set, we should check
437-- that the guess we are solving it with unifies with the thing unification
438-- found for it, if anything.
440solve :: RunTactic
441solve ctxt env (Bind x (Guess ty val) sc)
442 | True = do ps <- get
443 let (uh, uns) = unified ps
444 case lookup x (notunified ps) of
445 Just tm -> do -- trace ("AVOIDED " ++ show (x, tm, val)) $
446 -- return []
447 unify' ctxt env tm val
448 _ -> return []
449 action (\ps -> ps { holes = holes ps \\ [x],
450 solved = Just (x, val),
451-- problems = solveInProblems
452-- x val (problems ps),
453 -- dontunify = dontunify ps \\ [x],
454 -- unified = (uh, uns ++ [(x, val)]),
455 instances = instances ps \\ [x] })
456 return $ {- Bind x (Let ty val) sc -}
457 instantiate val (pToV x sc)
458 | otherwise = lift $ tfail $ IncompleteTerm val
459solve _ _ h@(Bind x t sc)
460 = do ps <- get
461 fail $ "Not a guess " ++ show h ++ "\n" ++ show (holes ps, pterm ps)
463introTy :: Raw -> Maybe Name -> RunTactic
464introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
465 do let n = case mn of
466 Just name -> name
467 Nothing -> x
468 let t' = case t of
469 x@(Bind y (Pi s) _) -> x
470 _ -> hnf ctxt env t
471 (tyv, tyt) <- lift $ check ctxt env ty
472-- ns <- lift $ unify ctxt env tyv t'
473 case t' of
474 Bind y (Pi s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
475 do ns <- unify' ctxt env s tyv
476 ps <- get
477 let (uh, uns) = unified ps
478-- put (ps { unified = (uh, uns ++ ns) })
479 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
480 _ -> lift $ tfail $ CantIntroduce t'
481introTy ty n ctxt env _ = fail "Can't introduce here."
483intro :: Maybe Name -> RunTactic
484intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
485 do let n = case mn of
486 Just name -> name
487 Nothing -> x
488 let t' = case t of
489 x@(Bind y (Pi s) _) -> x
490 _ -> hnf ctxt env t
491 case t' of
492 Bind y (Pi s) t -> -- trace ("in type " ++ show t') $
493 let t' = instantiate (P Bound n s) (pToV y t) in
494 return $ Bind n (Lam s) (Bind x (Hole t') (P Bound x t'))
495 _ -> lift $ tfail $ CantIntroduce t'
496intro n ctxt env _ = fail "Can't introduce here."
498forall :: Name -> Raw -> RunTactic
499forall n ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
500 do (tyv, tyt) <- lift $ check ctxt env ty
501 unify' ctxt env tyt (TType (UVar 0))
502 return $ Bind n (Pi tyv) (Bind x (Hole t) (P Bound x t))
503forall n ty ctxt env _ = fail "Can't pi bind here"
505patvar :: Name -> RunTactic
506patvar n ctxt env (Bind x (Hole t) sc) =
507 do action (\ps -> ps { holes = holes ps \\ [x] })
508 return $ Bind n (PVar t) (instantiate (P Bound n t) (pToV x sc))
509patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
511letbind :: Name -> Raw -> Raw -> RunTactic
512letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
513 do (tyv, tyt) <- lift $ check ctxt env ty
514 (valv, valt) <- lift $ check ctxt env val
515 lift $ isType ctxt env tyt
516 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
517letbind n ty val ctxt env _ = fail "Can't let bind here"
519expandLet :: Name -> Term -> RunTactic
520expandLet n v ctxt env tm =
521 return $ subst n v tm
523rewrite :: Raw -> RunTactic
524rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
525 do (tmv, tmt) <- lift $ check ctxt env tm
526 let tmt' = normalise ctxt env tmt
527 case unApply tmt' of
528 (P _ (UN "=") _, [lt,rt,l,r]) ->
529 do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
530 let newt = mkP l r l t
531 let sc = forget $ (Bind x (Hole newt)
532 (mkApp (P Ref (UN "replace") (TType (UVal 0)))
533 [lt, l, r, p, tmv, xp]))
534 (scv, sct) <- lift $ check ctxt env sc
535 return scv
536 _ -> fail "Not an equality type"
537 where
538 -- to make the P for rewrite, replace syntactic occurrences of l in ty with
539 -- an x, and put \x : lt in front
540 mkP lt l r ty | l == ty = lt
541 mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
542 a' = if (r /= a) then mkP lt l r a else a in
543 App f' a'
544 mkP lt l r (Bind n b sc)
545 = let b' = mkPB b
546 sc' = if (r /= sc) then mkP lt l r sc else sc in
547 Bind n b' sc'
548 where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
549 v' = if (r /= v) then mkP lt l r v else v in
550 Let t' v'
551 mkPB b = let ty = binderTy b
552 ty' = if (r /= ty) then mkP lt l r ty else ty in
553 b { binderTy = ty' }
554 mkP lt l r x = x
556 rname = MN 0 "replaced"
557rewrite _ _ _ _ = fail "Can't rewrite here"
559equiv :: Raw -> RunTactic
560equiv tm ctxt env (Bind x (Hole t) sc) =
561 do (tmv, tmt) <- lift $ check ctxt env tm
562 lift $ converts ctxt env tmv t
563 return $ Bind x (Hole tmv) sc
564equiv tm ctxt env _ = fail "Can't equiv here"
566patbind :: Name -> RunTactic
567patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
568 do let t' = case t of
569 x@(Bind y (PVTy s) t) -> x
570 _ -> hnf ctxt env t
571 case t' of
572 Bind y (PVTy s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
573 return $ Bind n (PVar s) (Bind x (Hole t') (P Bound x t'))
574 _ -> fail "Nothing to pattern bind"
575patbind n ctxt env _ = fail "Can't pattern bind here"
577compute :: RunTactic
578compute ctxt env (Bind x (Hole ty) sc) =
579 do return $ Bind x (Hole (normalise ctxt env ty)) sc
580compute ctxt env t = return t
582hnf_compute :: RunTactic
583hnf_compute ctxt env (Bind x (Hole ty) sc) =
584 do let ty' = hnf ctxt env ty in
585-- trace ("HNF " ++ show (ty, ty')) $
586 return $ Bind x (Hole ty') sc
587hnf_compute ctxt env t = return t
589-- reduce let bindings only
590simplify :: RunTactic
591simplify ctxt env (Bind x (Hole ty) sc) =
592 do return $ Bind x (Hole (specialise ctxt env [] ty)) sc
593simplify ctxt env t = return t
595check_in :: Raw -> RunTactic
596check_in t ctxt env tm =
597 do (val, valty) <- lift $ check ctxt env t
598 addLog (showEnv env val ++ " : " ++ showEnv env valty)
599 return tm
601eval_in :: Raw -> RunTactic
602eval_in t ctxt env tm =
603 do (val, valty) <- lift $ check ctxt env t
604 let val' = normalise ctxt env val
605 let valty' = normalise ctxt env valty
606 addLog (showEnv env val ++ " : " ++
607 showEnv env valty ++
608-- " in " ++ show env ++
609 " ==>\n " ++
610 showEnv env val' ++ " : " ++
611 showEnv env valty')
612 return tm
614start_unify :: Name -> RunTactic
615start_unify n ctxt env tm = do -- action (\ps -> ps { unified = (n, []) })
616 return tm
618tmap f (a, b, c) = (f a, b, c)
620solve_unified :: RunTactic
621solve_unified ctxt env tm =
622 do ps <- get
623 let (_, ns) = unified ps
624 let unify = dropGiven (dontunify ps) ns (holes ps)
625 action (\ps -> ps { holes = holes ps \\ map fst unify })
626 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
627 return (updateSolved unify tm)
628 where
630dropGiven du [] hs = []
631dropGiven du ((n, P Bound t ty) : us) hs
632 | n `elem` du && not (t `elem` du)
633 && n `elem` hs && t `elem` hs
634 = (t, P Bound n ty) : dropGiven du us hs
635dropGiven du (u@(n, _) : us) hs
636 | n `elem` du = dropGiven du us hs
637-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
638dropGiven du (u : us) hs = u : dropGiven du us hs
640keepGiven du [] hs = []
641keepGiven du ((n, P Bound t ty) : us) hs
642 | n `elem` du && not (t `elem` du)
643 && n `elem` hs && t `elem` hs
644 = keepGiven du us hs
645keepGiven du (u@(n, _) : us) hs
646 | n `elem` du = u : keepGiven du us hs
647keepGiven du (u : us) hs = keepGiven du us hs
649updateSolved xs x = -- trace ("Updating " ++ show xs ++ " in " ++ show x) $
650 updateSolved' xs x
651updateSolved' xs (Bind n (Hole ty) t)
652 | Just v <- lookup n xs = instantiate v (pToV n (updateSolved' xs t))
653updateSolved' xs (Bind n b t)
654 | otherwise = Bind n (fmap (updateSolved' xs) b) (updateSolved' xs t)
655updateSolved' xs (App f a) = App (updateSolved' xs f) (updateSolved' xs a)
656updateSolved' xs (P _ n _)
657 | Just v <- lookup n xs = v
658updateSolved' xs t = t
660updateEnv ns [] = []
661updateEnv ns ((n, b) : env) = (n, fmap (updateSolved ns) b) : updateEnv ns env
663updateError ns (CantUnify b l r e xs sc)
664 = CantUnify b (updateSolved ns l) (updateSolved ns r) (updateError ns e) xs sc
665updateError ns e = e
667solveInProblems x val [] = []
668solveInProblems x val ((l, r, env, err) : ps)
669 = ((instantiate val (pToV x l), instantiate val (pToV x r),
670 updateEnv [(x, val)] env, err) : solveInProblems x val ps)
672updateProblems ctxt ns ps inj holes = up ns ps where
673 up ns [] = (ns, [])
674 up ns ((x, y, env, err) : ps) =
675 let x' = updateSolved ns x
676 y' = updateSolved ns y
677 err' = updateError ns err
678 env' = updateEnv ns env in
679 case unify ctxt env' x' y' inj holes of
680 OK (v, []) -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
681 up (ns ++ v) ps
682 _ -> let (ns', ps') = up ns ps in
683 (ns', (x',y',env',err') : ps')
685processTactic :: Tactic -> ProofState -> TC (ProofState, String)
686processTactic QED ps = case holes ps of
687 [] -> do let tm = {- normalise (context ps) [] -} (pterm ps)
688 (tm', ty', _) <- recheck (context ps) [] (forget tm) tm
689 return (ps { done = True, pterm = tm' },
690 "Proof complete: " ++ showEnv [] tm')
691 _ -> fail "Still holes to fill."
692processTactic ProofState ps = return (ps, showEnv [] (pterm ps))
693processTactic Undo ps = case previous ps of
694 Nothing -> fail "Nothing to undo."
695 Just pold -> return (pold, "")
696processTactic EndUnify ps
697 = let (h, ns_in) = unified ps
698 ns = dropGiven (dontunify ps) ns_in (holes ps)
699 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
700 (ns'', probs') = updateProblems (context ps) ns' (problems ps)
701 (injective ps) (holes ps)
702 tm' = -- trace ("Updating " ++ show ns_in ++ "\n" ++ show ns'') $ -- ++ " in " ++ show (pterm ps)) $
703 updateSolved ns'' (pterm ps) in
704 return (ps { pterm = tm',
705 unified = (h, []),
706 problems = probs',
707 holes = holes ps \\ map fst ns'' }, "")
708processTactic (Reorder n) ps
709 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
710 return (ps' { previous = Just ps, plog = "" }, plog ps')
711processTactic (ComputeLet n) ps
712 = return (ps { pterm = computeLet (context ps) n (pterm ps) }, "")
713processTactic t ps
714 = case holes ps of
715 [] -> fail "Nothing to fill in."
716 (h:_) -> do ps' <- execStateT (process t h) ps
717 let pterm' = case solved ps' of
718 Just s -> updateSolved [s] (pterm ps')
719 _ -> pterm ps'
720 let (ns', probs')
721 = case solved ps' of
722 Just s -> updateProblems (context ps')
723 [s] (problems ps')
724 (injective ps')
725 (holes ps')
726 _ -> ([], problems ps')
727 -- rechecking problems may find more solutions, so
728 -- apply them here
729 let pterm'' = updateSolved ns' pterm'
730 return (ps' { pterm = pterm'',
731 solved = Nothing,
732 problems = probs',
733 previous = Just ps, plog = "",
734 holes = holes ps' \\ (map fst ns')}, plog ps')
736process :: Tactic -> Name -> StateT TState TC ()
737process EndUnify _
738 = do ps <- get
739 let (h, _) = unified ps
740 tactic (Just h) solve_unified
741process t h = tactic (Just h) (mktac t)
742 where mktac Attack = attack
743 mktac (Claim n r) = claim n r
744 mktac (Exact r) = exact r
745 mktac (Fill r) = fill r
746 mktac (MatchFill r) = match_fill r
747 mktac (PrepFill n ns) = prep_fill n ns
748 mktac CompleteFill = complete_fill
749 mktac Regret = regret
750 mktac Solve = solve
751 mktac (StartUnify n) = start_unify n
752 mktac Compute = compute
753 mktac Simplify = Core.ProofState.simplify
754 mktac HNF_Compute = hnf_compute
755 mktac (Intro n) = intro n
756 mktac (IntroTy ty n) = introTy ty n
757 mktac (Forall n t) = forall n t
758 mktac (LetBind n t v) = letbind n t v
759 mktac (ExpandLet n b) = expandLet n b
760 mktac (Rewrite t) = rewrite t
761 mktac (Equiv t) = equiv t
762 mktac (PatVar n) = patvar n
763 mktac (PatBind n) = patbind n
764 mktac (CheckIn r) = check_in r
765 mktac (EvalIn r) = eval_in r
766 mktac (Focus n) = focus n
767 mktac (Defer n) = defer n
768 mktac (DeferType n t a) = deferType n t a
769 mktac (Instance n) = instanceArg n
770 mktac (SetInjective n) = setinj n
771 mktac (MoveLast n) = movelast n