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 nextname :: Int, -- name supply
25 pterm :: Term, -- current proof term
26 ptype :: Type, -- original goal
27 dontunify :: [Name], -- explicitly given by programmer, leave it
28 unified :: (Name, [(Name, Term)]),
29 solved :: Maybe (Name, Term),
31 injective :: [(Term, Term, Term)],
32 deferred :: [Name], -- names we'll need to define
33 instances :: [Name], -- instance arguments (for type classes)
34 previous :: Maybe ProofState, -- for undo
40data Goal = GD { premises :: Env,
41 goalType :: Binder Term
49 | PrepFill Name [Name]
59 | IntroTy Raw (Maybe Name)
61 | LetBind Name Raw Raw
74-- Some utilites on proof and tactic states
76instance Show ProofState where
77 show (PS nm [] _ tm _ _ _ _ _ _ _ _ _ _ _ _) = show nm ++ ": no more goals"
78 show (PS nm (h:hs) _ tm _ _ _ _ _ _ i _ _ ctxt _ _)
79 = let OK g = goal (Just h) tm
81 "Other goals: " ++ show hs ++ "\n" ++
82 showPs wkenv (reverse wkenv) ++ "\n" ++
83 "-------------------------------- (" ++ show nm ++
85 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
86 where showPs env [] = ""
87 showPs env ((n, Let t v):bs)
88 = " " ++ show n ++ " : " ++
89 showEnv env ({- normalise ctxt env -} t) ++ " = " ++
90 showEnv env ({- normalise ctxt env -} v) ++
92 showPs env ((n, b):bs)
93 = " " ++ show n ++ " : " ++
94 showEnv env ({- normalise ctxt env -} (binderTy b)) ++
96 showG ps (Guess t v) = showEnv ps ({- normalise ctxt ps -} t) ++
97 " =?= " ++ showEnv ps v
98 showG ps b = showEnv ps (binderTy b)
100instance Pretty ProofState where
101 pretty (PS nm [] _ trm _ _ _ _ _ _ _ _ _ _ _ _) =
102 if size nm > breakingSize then
103 pretty nm <> colon $$ nest nestingSize (text " no more goals.")
105 pretty nm <> colon <+> text " no more goals."
106 pretty p@(PS nm (h:hs) _ tm _ _ _ _ _ _ i _ _ ctxt _ _) =
107 let OK g = goal (Just h) tm in
108 let wkEnv = premises g in
109 text "Other goals" <+> colon <+> pretty hs $$
110 prettyPs wkEnv (reverse wkEnv) $$
111 text "---------- " <+> text "Focussing on" <> colon <+> pretty nm <+> text " ----------" $$
112 pretty h <+> colon <+> prettyGoal wkEnv (goalType g)
114 prettyGoal ps (Guess t v) =
115 if size v > breakingSize then
116 prettyEnv ps t <+> text "=?=" $$
117 nest nestingSize (prettyEnv ps v)
119 prettyEnv ps t <+> text "=?=" <+> prettyEnv ps v
120 prettyGoal ps b = prettyEnv ps $ binderTy b
122 prettyPs env [] = empty
123 prettyPs env ((n, Let t v):bs) =
124 nest nestingSize (pretty n <+> colon <+>
125 if size v > breakingSize then
126 prettyEnv env t <+> text "=" $$
127 nest nestingSize (prettyEnv env v) $$
128 nest nestingSize (prettyPs env bs)
130 prettyEnv env t <+> text "=" <+> prettyEnv env v $$
131 nest nestingSize (prettyPs env bs))
132 prettyPs env ((n, b):bs) =
133 if size (binderTy b) > breakingSize then
134 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+> prettyPs env bs)
136 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) $$
137 nest nestingSize (prettyPs env bs))
140same (Just x) n = x == n
143hole (Guess _ _) = True
146holeName i = MN i "hole"
148unify' :: Context -> Env -> TT Name -> TT Name -> StateT TState TC [(Name, TT Name)]
149unify' ctxt env topx topy = do (u, inj, fails) <- lift $ unify ctxt env topx topy
155 put (ps { problems = err ++ problems ps })
158getName :: Monad m => String -> StateT TState m Name
159getName tag = do ps <- get
161 put (ps { nextname = n+1 })
164action :: Monad m => (ProofState -> ProofState) -> StateT TState m ()
165action a = do ps <- get
168addLog :: Monad m => String -> StateT TState m ()
169addLog str = action (\ps -> ps { plog = plog ps ++ str ++ "\n" })
171newProof :: Name -> Context -> Type -> ProofState
172newProof n ctxt ty = let h = holeName 0
174 PS n [h] 1 (Bind h (Hole ty') (P Bound h ty')) ty [] (h, [])
177 Nothing ctxt "" False
179type TState = ProofState -- [TacticAction])
180type RunTactic = Context -> Env -> Term -> StateT TState TC Term
181type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
183envAtFocus :: ProofState -> TC Env
185 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
187 | otherwise = fail "No holes"
189goalAtFocus :: ProofState -> TC (Binder Type)
191 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
194goal :: Hole -> Term -> TC Goal
195goal h tm = g [] tm where
196 g env (Bind n b sc) | hole b && same h n = return $ GD env b
198 = gb env b `mplus` g ((n, b):env) sc
199 g env (App f a) = g env f `mplus` g env a
200 g env t = fail "Can't find hole"
202 gb env (Let t v) = g env t `mplus` g env v
203 gb env (Guess t v) = g env t `mplus` g env v
204 gb env t = g env (binderTy t)
206tactic :: Hole -> RunTactic -> StateT TState TC ()
207tactic h f = do ps <- get
208 tm' <- atH (context ps) [] (pterm ps)
209 ps <- get -- might have changed while processing
210 put (ps { pterm = tm' })
212 atH c env binder@(Bind n b sc)
213 | hole b && same h n = f c env binder
215 = liftM2 (Bind n) (atHb c env b) (atH c ((n, b) : env) sc)
216 atH c env (App f a) = liftM2 App (atH c env f) (atH c env a)
217 atH c env t = return t
219 atHb c env (Let t v) = liftM2 Let (atH c env t) (atH c env v)
220 atHb c env (Guess t v) = liftM2 Guess (atH c env t) (atH c env v)
221 atHb c env t = do ty' <- atH c env (binderTy t)
222 return $ t { binderTy = ty' }
225attack ctxt env (Bind x (Hole t) sc)
226 = do h <- getName "hole"
227 action (\ps -> ps { holes = h : holes ps })
228 return $ Bind x (Guess t (newtm h)) sc
230 newtm h = Bind h (Hole t) (P Bound h t)
231attack ctxt env _ = fail "Not an attackable hole"
233claim :: Name -> Raw -> RunTactic
234claim n ty ctxt env t =
235 do (tyv, tyt) <- lift $ check ctxt env ty
236 lift $ isSet ctxt env tyt
237 action (\ps -> let (g:gs) = holes ps in
238 ps { holes = g : n : gs } )
239 return $ Bind n (Hole tyv) t -- (weakenTm 1 t)
241reorder_claims :: RunTactic
242reorder_claims ctxt env t
243 = -- trace (showSep "\n" (map show (scvs t))) $
244 let (bs, sc) = scvs t []
245 newbs = reverse (sortB (reverse bs)) in
246 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
247 return (bindAll newbs sc)
248 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
249 scvs sc acc = (reverse acc, sc)
251 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
253 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
254 | otherwise = sortB (insertB x xs)
257 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
258 | otherwise = y : insertB x ys
260 noOcc (n, _) (_, Let t v) = noOccurrence n t && noOccurrence n v
261 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
262 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
264focus :: Name -> RunTactic
265focus n ctxt env t = do action (\ps -> let hs = holes ps in
267 then ps { holes = n : (hs \\ [n]) }
271movelast :: Name -> RunTactic
272movelast n ctxt env t = do action (\ps -> let hs = holes ps in
274 then ps { holes = (hs \\ [n]) ++ [n] }
278instanceArg :: Name -> RunTactic
279instanceArg n ctxt env (Bind x (Hole t) sc)
280 = do action (\ps -> let hs = holes ps
282 ps { holes = (hs \\ [x]) ++ [x],
284 return (Bind x (Hole t) sc)
286defer :: Name -> RunTactic
287defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
288 do action (\ps -> let hs = holes ps in
289 ps { holes = hs \\ [x] })
290 return (Bind n (GHole (mkTy (reverse env) t))
291 (mkApp (P Ref n ty) (map getP (reverse env))))
294 mkTy ((n,b) : bs) t = Bind n (Pi (binderTy b)) (mkTy bs t)
296 getP (n, b) = P Bound n (binderTy b)
299regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
300 do action (\ps -> let hs = holes ps in
301 ps { holes = hs \\ [x] })
303regret ctxt env (Bind x (Hole t) _) = fail $ show x ++ " : " ++ show t ++ " is not solved"
305addInj :: [(Term, Term, Term)] -> StateT TState TC ()
306addInj inj = do ps <- get
307 put (ps { injective = inj ++ injective ps })
309exact :: Raw -> RunTactic
310exact guess ctxt env (Bind x (Hole ty) sc) =
311 do (val, valty) <- lift $ check ctxt env guess
312 lift $ converts ctxt env valty ty
313 return $ Bind x (Guess ty val) sc
314exact _ _ _ _ = fail "Can't fill here."
316-- As exact, but attempts to solve other goals by unification
318fill :: Raw -> RunTactic
319fill guess ctxt env (Bind x (Hole ty) sc) =
320 do (val, valty) <- lift $ check ctxt env guess
322-- let valtyn = normalise ctxt env valty
323-- let tyn = normalise ctxt env ty
324 ns <- unify' ctxt env valty ty
326 let (uh, uns) = unified ps
327 put (ps { unified = (uh, uns ++ ns) })
328-- addLog (show (uh, uns ++ ns))
329 return $ Bind x (Guess ty val) sc
330fill _ _ _ _ = fail "Can't fill here."
332prep_fill :: Name -> [Name] -> RunTactic
333prep_fill f as ctxt env (Bind x (Hole ty) sc) =
334 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
335 return $ Bind x (Guess ty val) sc
336prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
338complete_fill :: RunTactic
339complete_fill ctxt env (Bind x (Guess ty val) sc) =
340 do let guess = forget val
341 (val', valty) <- lift $ check ctxt env guess
342 ns <- unify' ctxt env valty ty
344 let (uh, uns) = unified ps
345 put (ps { unified = (uh, uns ++ ns) })
346 return $ Bind x (Guess ty val) sc
347complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
350solve ctxt env (Bind x (Guess ty val) sc)
351 | pureTerm val = do ps <- get
352 let (uh, uns) = unified ps
353 action (\ps -> ps { holes = holes ps \\ [x],
354 solved = Just (x, val),
355 -- dontunify = dontunify ps \\ [x],
356 -- unified = (uh, uns ++ [(x, val)]),
357 instances = instances ps \\ [x] })
358 return $ {- Bind x (Let ty val) sc -} instantiate val (pToV x sc)
359 | otherwise = lift $ tfail $ IncompleteTerm val
360solve _ _ h = fail $ "Not a guess " ++ show h
362introTy :: Raw -> Maybe Name -> RunTactic
363introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
364 do let n = case mn of
367 let t' = normalise ctxt env t
368 (tyv, tyt) <- lift $ check ctxt env ty
369-- ns <- lift $ unify ctxt env tyv t'
371 Bind y (Pi s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
372 do ns <- unify' ctxt env s tyv
374 let (uh, uns) = unified ps
375 put (ps { unified = (uh, uns ++ ns) })
376 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
377 _ -> fail "Nothing to introduce"
378introTy ty n ctxt env _ = fail "Can't introduce here."
380intro :: Maybe Name -> RunTactic
381intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
382 do let n = case mn of
385 let t' = normalise ctxt env t
387 Bind y (Pi s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
388 return $ Bind n (Lam s) (Bind x (Hole t') (P Bound x t'))
389 _ -> fail "Nothing to introduce"
390intro n ctxt env _ = fail "Can't introduce here."
392forall :: Name -> Raw -> RunTactic
393forall n ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
394 do (tyv, tyt) <- lift $ check ctxt env ty
395 lift $ isSet ctxt env tyt
396 lift $ isSet ctxt env t
397 return $ Bind n (Pi tyv) (Bind x (Hole t) (P Bound x t))
398forall n ty ctxt env _ = fail "Can't pi bind here"
400patvar :: Name -> RunTactic
401patvar n ctxt env (Bind x (Hole t) sc) =
402 do action (\ps -> ps { holes = holes ps \\ [x] })
403 return $ Bind n (PVar t) (instantiate (P Bound n t) (pToV x sc))
404patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
406letbind :: Name -> Raw -> Raw -> RunTactic
407letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
408 do (tyv, tyt) <- lift $ check ctxt env ty
409 (valv, valt) <- lift $ check ctxt env val
410 lift $ isSet ctxt env tyt
411 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
412letbind n ty val ctxt env _ = fail "Can't let bind here"
414rewrite :: Raw -> RunTactic
415rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
416 do (tmv, tmt) <- lift $ check ctxt env tm
417 let tmt' = normalise ctxt env tmt
419 (P _ (UN "=") _, [lt,rt,l,r]) ->
420 do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
421 let newt = mkP l r l t
422 let sc = forget $ (Bind x (Hole newt)
423 (mkApp (P Ref (UN "replace") (Set (UVal 0)))
424 [lt, l, r, p, tmv, xp]))
425 (scv, sct) <- lift $ check ctxt env sc
427 _ -> fail "Not an equality type"
429 -- to make the P for rewrite, replace syntactic occurrences of l in ty with
430 -- an x, and put \x : lt in front
431 mkP lt l r ty | l == ty = lt
432 mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
433 a' = if (r /= a) then mkP lt l r a else a in
435 mkP lt l r (Bind n b sc)
437 sc' = if (r /= sc) then mkP lt l r sc else sc in
439 where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
440 v' = if (r /= v) then mkP lt l r v else v in
442 mkPB b = let ty = binderTy b
443 ty' = if (r /= ty) then mkP lt l r ty else ty in
447 rname = MN 0 "replaced"
448rewrite _ _ _ _ = fail "Can't rewrite here"
450patbind :: Name -> RunTactic
451patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
452 do let t' = normalise ctxt env t
454 Bind y (PVTy s) t -> let t' = instantiate (P Bound n s) (pToV y t) in
455 return $ Bind n (PVar s) (Bind x (Hole t') (P Bound x t'))
456 _ -> fail "Nothing to pattern bind"
457patbind n ctxt env _ = fail "Can't pattern bind here"
460compute ctxt env (Bind x (Hole ty) sc) =
461 do return $ Bind x (Hole (normalise ctxt env ty)) sc
463check_in :: Raw -> RunTactic
464check_in t ctxt env tm =
465 do (val, valty) <- lift $ check ctxt env t
466 addLog (showEnv env val ++ " : " ++ showEnv env valty)
469eval_in :: Raw -> RunTactic
470eval_in t ctxt env tm =
471 do (val, valty) <- lift $ check ctxt env t
472 let val' = normalise ctxt env val
473 let valty' = normalise ctxt env valty
474 addLog (showEnv env val ++ " : " ++
476-- " in " ++ show env ++
478 showEnv env val' ++ " : " ++
482start_unify :: Name -> RunTactic
483start_unify n ctxt env tm = do action (\ps -> ps { unified = (n, []) })
486tmap f (a, b, c) = (f a, b, c)
488solve_unified :: RunTactic
489solve_unified ctxt env tm =
491 let (_, ns) = unified ps
492 let unify = dropGiven (dontunify ps) ns
493 action (\ps -> ps { holes = holes ps \\ map fst unify })
494 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
495 action (\ps -> ps { injective = map (tmap (updateSolved unify)) (injective ps) })
496 return (updateSolved unify tm)
500dropGiven du ((n, P Bound t ty) : us) | n `elem` du && not (t `elem` du)
501 = (t, P Bound n ty) : dropGiven du us
502dropGiven du (u@(n, _) : us) | n `elem` du = dropGiven du us
503-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
504dropGiven du (u : us) = u : dropGiven du us
506updateSolved xs (Bind n (Hole ty) t)
507 | Just v <- lookup n xs = instantiate v (pToV n (updateSolved xs t))
508updateSolved xs (Bind n b t)
509 | otherwise = Bind n (fmap (updateSolved xs) b) (updateSolved xs t)
510updateSolved xs (App f a) = App (updateSolved xs f) (updateSolved xs a)
511updateSolved xs (P _ n _)
512 | Just v <- lookup n xs = v
515updateProblems ns [] = []
516updateProblems ns ((x, y, env, err) : ps) =
517 let x' = updateSolved ns x
518 y' = updateSolved ns y in
519 (x',y',env,err) : updateProblems ns ps
521processTactic :: Tactic -> ProofState -> TC (ProofState, String)
522processTactic QED ps = case holes ps of
523 [] -> do let tm = {- normalise (context ps) [] -} (pterm ps)
524 (tm', ty', _) <- recheck (context ps) [] (forget tm) tm
525 return (ps { done = True, pterm = tm' },
526 "Proof complete: " ++ showEnv [] tm')
527 _ -> fail "Still holes to fill."
528processTactic ProofState ps = return (ps, showEnv [] (pterm ps))
529processTactic Undo ps = case previous ps of
530 Nothing -> fail "Nothing to undo."
531 Just pold -> return (pold, "")
532processTactic EndUnify ps
533 = let (h, ns_in) = unified ps
534 ns = dropGiven (dontunify ps) ns_in
535 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
536 tm' = -- trace ("Updating " ++ show ns') $ -- ++ " in " ++ show (pterm ps)) $
537 updateSolved ns' (pterm ps)
538 probs' = updateProblems ns' (problems ps) in
540 [] -> return (ps { pterm = tm',
542 injective = map (tmap (updateSolved ns'))
544 holes = holes ps \\ map fst ns' }, "")
545 errs@((_,_,_,err):_) -> tfail err
546processTactic (Reorder n) ps
547 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
548 return (ps' { previous = Just ps, plog = "" }, plog ps')
551 [] -> fail "Nothing to fill in."
552 (h:_) -> do ps' <- execStateT (process t h) ps
553 let pterm' = case solved ps' of
554 Just s -> updateSolved [s] (pterm ps')
556 return (ps' { pterm = pterm',
558 previous = Just ps, plog = "" }, plog ps')
560process :: Tactic -> Name -> StateT TState TC ()
563 let (h, _) = unified ps
564 tactic (Just h) solve_unified
565process t h = tactic (Just h) (mktac t)
566 where mktac Attack = attack
567 mktac (Claim n r) = claim n r
568 mktac (Exact r) = exact r
569 mktac (Fill r) = fill r
570 mktac (PrepFill n ns) = prep_fill n ns
571 mktac CompleteFill = complete_fill
572 mktac Regret = regret
574 mktac (StartUnify n) = start_unify n
575 mktac Compute = compute
576 mktac (Intro n) = intro n
577 mktac (IntroTy ty n) = introTy ty n
578 mktac (Forall n t) = forall n t
579 mktac (LetBind n t v) = letbind n t v
580 mktac (Rewrite t) = rewrite t
581 mktac (PatVar n) = patvar n
582 mktac (PatBind n) = patbind n
583 mktac (CheckIn r) = check_in r
584 mktac (EvalIn r) = eval_in r
585 mktac (Focus n) = focus n
586 mktac (Defer n) = defer n
587 mktac (Instance n) = instanceArg n
588 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