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, dropGiven) where
15import Control.Monad.State
16import Control.Applicative hiding (empty)
22data ProofState = PS { thname :: Name,
23 holes :: [Name], -- holes still to be solved
24 usedns :: [Name], -- used names, don't use again
25 nextname :: Int, -- name supply
26 pterm :: Term, -- current proof term
27 ptype :: Type, -- original goal
28 dontunify :: [Name], -- explicitly given by programmer, leave it
29 unified :: (Name, [(Name, Term)]),
30 solved :: Maybe (Name, Term),
32 injective :: [(Term, Term, Term)],
33 deferred :: [Name], -- names we'll need to define
34 instances :: [Name], -- instance arguments (for type classes)
35 previous :: Maybe ProofState, -- for undo
41data Goal = GD { premises :: Env,
42 goalType :: Binder Term
50 | PrepFill Name [Name]
60 | IntroTy Raw (Maybe Name)
62 | LetBind Name Raw Raw
68 | DeferType Name Raw [Name]
76-- Some utilites on proof and tactic states
78instance Show ProofState where
79 show (PS nm [] _ _ tm _ _ _ _ _ _ _ _ _ _ _ _) = show nm ++ ": no more goals"
80 show (PS nm (h:hs) _ _ tm _ _ _ _ _ _ i _ _ ctxt _ _)
81 = let OK g = goal (Just h) tm
83 "Other goals: " ++ show hs ++ "\n" ++
84 showPs wkenv (reverse wkenv) ++ "\n" ++
85 "-------------------------------- (" ++ show nm ++
87 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
88 where showPs env [] = ""
89 showPs env ((n, Let t v):bs)
90 = " " ++ show n ++ " : " ++
91 showEnv env ({- normalise ctxt env -} t) ++ " = " ++
92 showEnv env ({- normalise ctxt env -} v) ++
94 showPs env ((n, b):bs)
95 = " " ++ show n ++ " : " ++
96 showEnv env ({- normalise ctxt env -} (binderTy b)) ++
98 showG ps (Guess t v) = showEnv ps ({- normalise ctxt ps -} t) ++
99 " =?= " ++ showEnv ps v
100 showG ps b = showEnv ps (binderTy b)
102instance Pretty ProofState where
103 pretty (PS nm [] _ _ trm _ _ _ _ _ _ _ _ _ _ _ _) =
104 if size nm > breakingSize then
105 pretty nm <> colon $$ nest nestingSize (text " no more goals.")
107 pretty nm <> colon <+> text " no more goals."
108 pretty p@(PS nm (h:hs) _ _ tm _ _ _ _ _ _ i _ _ ctxt _ _) =
109 let OK g = goal (Just h) tm in
110 let wkEnv = premises g in
111 text "Other goals" <+> colon <+> pretty hs $$
112 prettyPs wkEnv (reverse wkEnv) $$
113 text "---------- " <+> text "Focussing on" <> colon <+> pretty nm <+> text " ----------" $$
114 pretty h <+> colon <+> prettyGoal wkEnv (goalType g)
116 prettyGoal ps (Guess t v) =
117 if size v > breakingSize then
118 prettyEnv ps t <+> text "=?=" $$
119 nest nestingSize (prettyEnv ps v)
121 prettyEnv ps t <+> text "=?=" <+> prettyEnv ps v
122 prettyGoal ps b = prettyEnv ps $ binderTy b
124 prettyPs env [] = empty
125 prettyPs env ((n, Let t v):bs) =
126 nest nestingSize (pretty n <+> colon <+>
127 if size v > breakingSize then
128 prettyEnv env t <+> text "=" $$
129 nest nestingSize (prettyEnv env v) $$
130 nest nestingSize (prettyPs env bs)
132 prettyEnv env t <+> text "=" <+> prettyEnv env v $$
133 nest nestingSize (prettyPs env bs))
134 prettyPs env ((n, b):bs) =
135 if size (binderTy b) > breakingSize then
136 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+> prettyPs env bs)
138 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) $$
139 nest nestingSize (prettyPs env bs))
142same (Just x) n = x == n
145hole (Guess _ _) = True
148holeName i = MN i "hole"
150unify' :: Context -> Env -> TT Name -> TT Name -> StateT TState TC [(Name, TT Name)]
151unify' ctxt env topx topy = do (u, inj, fails) <- lift $ unify ctxt env topx topy
157 put (ps { problems = err ++ problems ps })
160getName :: Monad m => String -> StateT TState m Name
161getName tag = do ps <- get
163 put (ps { nextname = n+1 })
166action :: Monad m => (ProofState -> ProofState) -> StateT TState m ()
167action a = do ps <- get
170addLog :: Monad m => String -> StateT TState m ()
171addLog str = action (\ps -> ps { plog = plog ps ++ str ++ "\n" })
173newProof :: Name -> Context -> Type -> ProofState
174newProof n ctxt ty = let h = holeName 0
176 PS n [h] [] 1 (Bind h (Hole ty')
177 (P Bound h ty')) ty [] (h, [])
180 Nothing ctxt "" False
182type TState = ProofState -- [TacticAction])
183type RunTactic = Context -> Env -> Term -> StateT TState TC Term
184type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
186envAtFocus :: ProofState -> TC Env
188 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
190 | otherwise = fail "No holes"
192goalAtFocus :: ProofState -> TC (Binder Type)
194 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
197goal :: Hole -> Term -> TC Goal
198goal h tm = g [] tm where
199 g env (Bind n b sc) | hole b && same h n = return $ GD env b
201 = gb env b `mplus` g ((n, b):env) sc
202 g env (App f a) = g env f `mplus` g env a
203 g env t = fail "Can't find hole"
205 gb env (Let t v) = g env t `mplus` g env v
206 gb env (Guess t v) = g env t `mplus` g env v
207 gb env t = g env (binderTy t)
209tactic :: Hole -> RunTactic -> StateT TState TC ()
210tactic h f = do ps <- get
211 tm' <- atH (context ps) [] (pterm ps)
212 ps <- get -- might have changed while processing
213 put (ps { pterm = tm' })
215 atH c env binder@(Bind n b sc)
216 | hole b && same h n = f c env binder
218 = liftM2 (Bind n) (atHb c env b) (atH c ((n, b) : env) sc)
219 atH c env (App f a) = liftM2 App (atH c env f) (atH c env a)
220 atH c env t = return t
222 atHb c env (Let t v) = liftM2 Let (atH c env t) (atH c env v)
223 atHb c env (Guess t v) = liftM2 Guess (atH c env t) (atH c env v)
224 atHb c env t = do ty' <- atH c env (binderTy t)
225 return $ t { binderTy = ty' }
228attack ctxt env (Bind x (Hole t) sc)
229 = do h <- getName "hole"
230 action (\ps -> ps { holes = h : holes ps })
231 return $ Bind x (Guess t (newtm h)) sc
233 newtm h = Bind h (Hole t) (P Bound h t)
234attack ctxt env _ = fail "Not an attackable hole"
236claim :: Name -> Raw -> RunTactic
237claim n ty ctxt env t =
238 do (tyv, tyt) <- lift $ check ctxt env ty
239 lift $ isSet ctxt env tyt
240 action (\ps -> let (g:gs) = holes ps in
241 ps { holes = g : n : gs } )
242 return $ Bind n (Hole tyv) t -- (weakenTm 1 t)
244reorder_claims :: RunTactic
245reorder_claims ctxt env t
246 = -- trace (showSep "\n" (map show (scvs t))) $
247 let (bs, sc) = scvs t []
248 newbs = reverse (sortB (reverse bs)) in
249 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
250 return (bindAll newbs sc)
251 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
252 scvs sc acc = (reverse acc, sc)
254 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
256 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
257 | otherwise = sortB (insertB x xs)
260 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
261 | otherwise = y : insertB x ys
263 noOcc (n, _) (_, Let t v) = noOccurrence n t && noOccurrence n v
264 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
265 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
267focus :: Name -> RunTactic
268focus n ctxt env t = do action (\ps -> let hs = holes ps in
270 then ps { holes = n : (hs \\ [n]) }
275movelast :: Name -> RunTactic
276movelast n ctxt env t = do action (\ps -> let hs = holes ps in
278 then ps { holes = (hs \\ [n]) ++ [n] }
282instanceArg :: Name -> RunTactic
283instanceArg n ctxt env (Bind x (Hole t) sc)
284 = do action (\ps -> let hs = holes ps
286 ps { holes = (hs \\ [x]) ++ [x],
288 return (Bind x (Hole t) sc)
290defer :: Name -> RunTactic
291defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
292 do action (\ps -> let hs = holes ps in
293 ps { holes = hs \\ [x] })
294 return (Bind n (GHole (mkTy (reverse env) t))
295 (mkApp (P Ref n ty) (map getP (reverse env))))
298 mkTy ((n,b) : bs) t = Bind n (Pi (binderTy b)) (mkTy bs t)
300 getP (n, b) = P Bound n (binderTy b)
302-- as defer, but build the type and application explicitly
303deferType :: Name -> Raw -> [Name] -> RunTactic
304deferType n fty_in args ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
305 do (fty, _) <- lift $ check ctxt env fty_in
306 action (\ps -> let hs = holes ps in
307 ps { holes = hs \\ [x] })
308 return (Bind n (GHole fty)
309 (mkApp (P Ref n ty) (map getP args)))
311 getP n = case lookup n env of
312 Just b -> P Bound n (binderTy b)
315regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
316 do action (\ps -> let hs = holes ps in
317 ps { holes = hs \\ [x] })
319regret ctxt env (Bind x (Hole t) _) = fail $ show x ++ " : " ++ show t ++ " is not solved"
321addInj :: [(Term, Term, Term)] -> StateT TState TC ()
322addInj inj = do ps <- get
323 put (ps { injective = inj ++ injective ps })
325exact :: Raw -> RunTactic
326exact guess ctxt env (Bind x (Hole ty) sc) =
327 do (val, valty) <- lift $ check ctxt env guess
328 lift $ converts ctxt env valty ty
329 return $ Bind x (Guess ty val) sc
330exact _ _ _ _ = fail "Can't fill here."
332-- As exact, but attempts to solve other goals by unification
334fill :: Raw -> RunTactic
335fill guess ctxt env (Bind x (Hole ty) sc) =
336 do (val, valty) <- lift $ check ctxt env guess
338-- let valtyn = normalise ctxt env valty
339-- let tyn = normalise ctxt env ty
340 ns <- unify' ctxt env valty ty
342 let (uh, uns) = unified ps
343 put (ps { unified = (uh, uns ++ ns) })
344-- addLog (show (uh, uns ++ ns))
345 return $ Bind x (Guess ty val) sc
346fill _ _ _ _ = fail "Can't fill here."
348prep_fill :: Name -> [Name] -> RunTactic
349prep_fill f as ctxt env (Bind x (Hole ty) sc) =
350 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
351 return $ Bind x (Guess ty val) sc
352prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
354complete_fill :: RunTactic
355complete_fill ctxt env (Bind x (Guess ty val) sc) =
356 do let guess = forget val
357 (val', valty) <- lift $ check ctxt env guess
358 ns <- unify' ctxt env valty ty
360 let (uh, uns) = unified ps
361 put (ps { unified = (uh, uns ++ ns) })
362 return $ Bind x (Guess ty val) sc
363complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
366solve ctxt env (Bind x (Guess ty val) sc)
367 | pureTerm val = do ps <- get
368 let (uh, uns) = unified ps
369 action (\ps -> ps { holes = holes ps \\ [x],
370 solved = Just (x, val),
371 -- dontunify = dontunify ps \\ [x],
372 -- unified = (uh, uns ++ [(x, val)]),
373 instances = instances ps \\ [x] })
374 return $ {- Bind x (Let ty val) sc -}
375 instantiate val (pToV x sc)
376 | otherwise = lift $ tfail $ IncompleteTerm val
377solve _ _ h = do ps <- get
378 fail $ "Not a guess " ++ show h ++ "\n" ++ show (holes ps, pterm ps)
380introTy :: Raw -> Maybe Name -> RunTactic
381introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
382 do let n = case mn of
386 x@(Bind y (Pi s) _) -> x
387 _ -> normalise ctxt env t
388 (tyv, tyt) <- lift $ check ctxt env ty
389-- ns <- lift $ unify ctxt env tyv t'
391 Bind y (Pi s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
392 do ns <- unify' ctxt env s tyv
394 let (uh, uns) = unified ps
395 put (ps { unified = (uh, uns ++ ns) })
396 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
397 _ -> fail "Nothing to introduce"
398introTy ty n ctxt env _ = fail "Can't introduce here."
400intro :: Maybe Name -> RunTactic
401intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
402 do let n = case mn of
406 x@(Bind y (Pi s) _) -> x
407 _ -> normalise ctxt env t
409 Bind y (Pi s) t -> -- trace ("in type " ++ show t') $
410 let t' = instantiate (P Bound n s) (pToV y t) in
411 return $ Bind n (Lam s) (Bind x (Hole t') (P Bound x t'))
412 _ -> fail "Nothing to introduce"
413intro n ctxt env _ = fail "Can't introduce here."
415forall :: Name -> Raw -> RunTactic
416forall n ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
417 do (tyv, tyt) <- lift $ check ctxt env ty
418 lift $ isSet ctxt env tyt
419 lift $ isSet ctxt env t
420 return $ Bind n (Pi tyv) (Bind x (Hole t) (P Bound x t))
421forall n ty ctxt env _ = fail "Can't pi bind here"
423patvar :: Name -> RunTactic
424patvar n ctxt env (Bind x (Hole t) sc) =
425 do action (\ps -> ps { holes = holes ps \\ [x] })
426 return $ Bind n (PVar t) (instantiate (P Bound n t) (pToV x sc))
427patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
429letbind :: Name -> Raw -> Raw -> RunTactic
430letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
431 do (tyv, tyt) <- lift $ check ctxt env ty
432 (valv, valt) <- lift $ check ctxt env val
433 lift $ isSet ctxt env tyt
434 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
435letbind n ty val ctxt env _ = fail "Can't let bind here"
437rewrite :: Raw -> RunTactic
438rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
439 do (tmv, tmt) <- lift $ check ctxt env tm
440 let tmt' = normalise ctxt env tmt
442 (P _ (UN "=") _, [lt,rt,l,r]) ->
443 do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
444 let newt = mkP l r l t
445 let sc = forget $ (Bind x (Hole newt)
446 (mkApp (P Ref (UN "replace") (Set (UVal 0)))
447 [lt, l, r, p, tmv, xp]))
448 (scv, sct) <- lift $ check ctxt env sc
450 _ -> fail "Not an equality type"
452 -- to make the P for rewrite, replace syntactic occurrences of l in ty with
453 -- an x, and put \x : lt in front
454 mkP lt l r ty | l == ty = lt
455 mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
456 a' = if (r /= a) then mkP lt l r a else a in
458 mkP lt l r (Bind n b sc)
460 sc' = if (r /= sc) then mkP lt l r sc else sc in
462 where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
463 v' = if (r /= v) then mkP lt l r v else v in
465 mkPB b = let ty = binderTy b
466 ty' = if (r /= ty) then mkP lt l r ty else ty in
470 rname = MN 0 "replaced"
471rewrite _ _ _ _ = fail "Can't rewrite here"
473patbind :: Name -> RunTactic
474patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
475 do let t' = case t of
476 x@(Bind y (PVTy s) t) -> x
477 _ -> normalise ctxt env t
479 Bind y (PVTy s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
480 return $ Bind n (PVar s) (Bind x (Hole t') (P Bound x t'))
481 _ -> fail "Nothing to pattern bind"
482patbind n ctxt env _ = fail "Can't pattern bind here"
485compute ctxt env (Bind x (Hole ty) sc) =
486 do return $ Bind x (Hole (normalise ctxt env ty)) sc
488check_in :: Raw -> RunTactic
489check_in t ctxt env tm =
490 do (val, valty) <- lift $ check ctxt env t
491 addLog (showEnv env val ++ " : " ++ showEnv env valty)
494eval_in :: Raw -> RunTactic
495eval_in t ctxt env tm =
496 do (val, valty) <- lift $ check ctxt env t
497 let val' = normalise ctxt env val
498 let valty' = normalise ctxt env valty
499 addLog (showEnv env val ++ " : " ++
501-- " in " ++ show env ++
503 showEnv env val' ++ " : " ++
507start_unify :: Name -> RunTactic
508start_unify n ctxt env tm = do action (\ps -> ps { unified = (n, []) })
511tmap f (a, b, c) = (f a, b, c)
513solve_unified :: RunTactic
514solve_unified ctxt env tm =
516 let (_, ns) = unified ps
517 let unify = dropGiven (dontunify ps) ns
518 action (\ps -> ps { holes = holes ps \\ map fst unify })
519 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
520 action (\ps -> ps { injective = map (tmap (updateSolved unify)) (injective ps) })
521 return (updateSolved unify tm)
525dropGiven du ((n, P Bound t ty) : us) | n `elem` du && not (t `elem` du)
526 = (t, P Bound n ty) : dropGiven du us
527dropGiven du (u@(n, _) : us) | n `elem` du = dropGiven du us
528-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
529dropGiven du (u : us) = u : dropGiven du us
531updateSolved xs x = -- trace ("Updating " ++ show xs ++ " in " ++ show x) $
533updateSolved' xs (Bind n (Hole ty) t)
534 | Just v <- lookup n xs = instantiate v (pToV n (updateSolved' xs t))
535updateSolved' xs (Bind n b t)
536 | otherwise = Bind n (fmap (updateSolved' xs) b) (updateSolved' xs t)
537updateSolved' xs (App f a) = App (updateSolved' xs f) (updateSolved' xs a)
538updateSolved' xs (P _ n _)
539 | Just v <- lookup n xs = v
540updateSolved' xs t = t
542updateProblems ns [] = []
543updateProblems ns ((x, y, env, err) : ps) =
544 let x' = updateSolved ns x
545 y' = updateSolved ns y in
546 (x',y',env,err) : updateProblems ns ps
548processTactic :: Tactic -> ProofState -> TC (ProofState, String)
549processTactic QED ps = case holes ps of
550 [] -> do let tm = {- normalise (context ps) [] -} (pterm ps)
551 (tm', ty', _) <- recheck (context ps) [] (forget tm) tm
552 return (ps { done = True, pterm = tm' },
553 "Proof complete: " ++ showEnv [] tm')
554 _ -> fail "Still holes to fill."
555processTactic ProofState ps = return (ps, showEnv [] (pterm ps))
556processTactic Undo ps = case previous ps of
557 Nothing -> fail "Nothing to undo."
558 Just pold -> return (pold, "")
559processTactic EndUnify ps
560 = let (h, ns_in) = unified ps
561 ns = dropGiven (dontunify ps) ns_in
562 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
563 tm' = -- trace ("Updating " ++ show ns') $ -- ++ " in " ++ show (pterm ps)) $
564 updateSolved ns' (pterm ps)
565 probs' = updateProblems ns' (problems ps) in
567 [] -> return (ps { pterm = tm',
569 injective = map (tmap (updateSolved ns'))
571 holes = holes ps \\ map fst ns' }, "")
572 errs@((_,_,_,err):_) -> tfail err
573processTactic (Reorder n) ps
574 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
575 return (ps' { previous = Just ps, plog = "" }, plog ps')
578 [] -> fail "Nothing to fill in."
579 (h:_) -> do ps' <- execStateT (process t h) ps
580 let pterm' = case solved ps' of
581 Just s -> updateSolved [s] (pterm ps')
583 return (ps' { pterm = pterm',
585 previous = Just ps, plog = "" }, plog ps')
587process :: Tactic -> Name -> StateT TState TC ()
590 let (h, _) = unified ps
591 tactic (Just h) solve_unified
592process t h = tactic (Just h) (mktac t)
593 where mktac Attack = attack
594 mktac (Claim n r) = claim n r
595 mktac (Exact r) = exact r
596 mktac (Fill r) = fill r
597 mktac (PrepFill n ns) = prep_fill n ns
598 mktac CompleteFill = complete_fill
599 mktac Regret = regret
601 mktac (StartUnify n) = start_unify n
602 mktac Compute = compute
603 mktac (Intro n) = intro n
604 mktac (IntroTy ty n) = introTy ty n
605 mktac (Forall n t) = forall n t
606 mktac (LetBind n t v) = letbind n t v
607 mktac (Rewrite t) = rewrite t
608 mktac (PatVar n) = patvar n
609 mktac (PatBind n) = patbind n
610 mktac (CheckIn r) = check_in r
611 mktac (EvalIn r) = eval_in r
612 mktac (Focus n) = focus n
613 mktac (Defer n) = defer n
614 mktac (DeferType n t a) = deferType n t a
615 mktac (Instance n) = instanceArg n
616 mktac (MoveLast n) = movelast n
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