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 Idris.Core.ProofState(ProofState(..), newProof, envAtFocus, goalAtFocus,
8 Tactic(..), Goal(..), processTactic,
9 dropGiven, keepGiven) where
11import Idris.Core.Typecheck
12import Idris.Core.Evaluate
14import Idris.Core.Unify
16import Control.Monad.State.Strict
17import Control.Applicative hiding (empty)
21import Util.Pretty hiding (fill)
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),
35 deferred :: [Name], -- names we'll need to define
36 instances :: [Name], -- instance arguments (for type classes)
37 previous :: Maybe ProofState, -- for undo
44data Goal = GD { premises :: Env,
45 goalType :: Binder Term
54 | PrepFill Name [Name]
67 | IntroTy Raw (Maybe Name)
69 | LetBind Name Raw Raw
78 | DeferType Name Raw [Name]
89-- Some utilites on proof and tactic states
91instance Show ProofState where
92 show (PS nm [] _ _ tm _ _ _ _ _ _ _ _ _ _ _ _ _ _)
93 = show nm ++ ": no more goals"
94 show (PS nm (h:hs) _ _ tm _ _ _ _ _ _ _ i _ _ ctxt _ _ _)
95 = let OK g = goal (Just h) tm
97 "Other goals: " ++ show hs ++ "\n" ++
98 showPs wkenv (reverse wkenv) ++ "\n" ++
99 "-------------------------------- (" ++ show nm ++
101 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
102 where showPs env [] = ""
103 showPs env ((n, Let t v):bs)
104 = " " ++ show n ++ " : " ++
105 showEnv env ({- normalise ctxt env -} t) ++ " = " ++
106 showEnv env ({- normalise ctxt env -} v) ++
107 "\n" ++ showPs env bs
108 showPs env ((n, b):bs)
109 = " " ++ show n ++ " : " ++
110 showEnv env ({- normalise ctxt env -} (binderTy b)) ++
111 "\n" ++ showPs env bs
112 showG ps (Guess t v) = showEnv ps ({- normalise ctxt ps -} t) ++
113 " =?= " ++ showEnv ps v
114 showG ps b = showEnv ps (binderTy b)
116instance Pretty ProofState OutputAnnotation where
117 pretty (PS nm [] _ _ trm _ _ _ _ _ _ _ _ _ _ _ _ _ _) =
118 if size nm > breakingSize then
119 pretty nm <+> colon <+> nest nestingSize (text " no more goals.")
121 pretty nm <+> colon <+> text " no more goals."
122 pretty p@(PS nm (h:hs) _ _ tm _ _ _ _ _ _ _ i _ _ ctxt _ _ _) =
123 let OK g = goal (Just h) tm in
124 let wkEnv = premises g in
125 text "Other goals" <+> colon <+> pretty hs <+>
126 prettyPs wkEnv (reverse wkEnv) <+>
127 text "---------- " <+> text "Focussing on" <> colon <+> pretty nm <+> text " ----------" <+>
128 pretty h <+> colon <+> prettyGoal wkEnv (goalType g)
130 prettyGoal ps (Guess t v) =
131 if size v > breakingSize then
132 prettyEnv ps t <+> text "=?=" <+>
133 nest nestingSize (prettyEnv ps v)
135 prettyEnv ps t <+> text "=?=" <+> prettyEnv ps v
136 prettyGoal ps b = prettyEnv ps $ binderTy b
138 prettyPs env [] = empty
139 prettyPs env ((n, Let t v):bs) =
140 nest nestingSize (pretty n <+> colon <+>
141 if size v > breakingSize then
142 prettyEnv env t <+> text "=" <+>
143 nest nestingSize (prettyEnv env v) <+>
144 nest nestingSize (prettyPs env bs)
146 prettyEnv env t <+> text "=" <+> prettyEnv env v <+>
147 nest nestingSize (prettyPs env bs))
148 prettyPs env ((n, b):bs) =
149 if size (binderTy b) > breakingSize then
150 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+> prettyPs env bs)
152 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+>
153 nest nestingSize (prettyPs env bs))
156same (Just x) n = x == n
159hole (Guess _ _) = True
162holeName i = sMN i "hole"
164qshow :: Fails -> String
165qshow fs = show (map (\ (x, y, _, _) -> (x, y)) fs)
167match_unify' :: Context -> Env -> TT Name -> TT Name ->
168 StateT TState TC [(Name, TT Name)]
169match_unify' ctxt env topx topy =
171 let dont = dontunify ps
172 u <- lift $ match_unify ctxt env topx topy dont (holes ps)
173 let (h, ns) = unified ps
174 put (ps { unified = (h, u ++ ns) })
177unify' :: Context -> Env -> TT Name -> TT Name ->
178 StateT TState TC [(Name, TT Name)]
179unify' ctxt env topx topy =
181 let dont = dontunify ps
182 (u, fails) <- traceWhen (unifylog ps)
183 ("Trying " ++ show (topx, topy) ++
184 " in " ++ show env ++
185 "\nHoles: " ++ show (holes ps) ++ "\n") $
186 lift $ unify ctxt env topx topy dont (holes ps)
187 let notu = filter (\ (n, t) -> case t of
189 _ -> n `elem` dont) u
190 traceWhen (unifylog ps)
191 ("Unified " ++ show (topx, topy) ++ " without " ++ show dont ++
192 "\nSolved: " ++ show u ++ "\nNew problems: " ++ qshow fails
193 ++ "\nNot unified:\n" ++ show (notunified ps)
194 ++ "\nCurrent problems:\n" ++ qshow (problems ps)
198 let (h, ns) = unified ps
199 let (ns', probs') = updateProblems (context ps) (u ++ ns)
200 (fails ++ problems ps)
203 put (ps { problems = probs',
205 notunified = notu ++ notunified ps })
208getName :: Monad m => String -> StateT TState m Name
209getName tag = do ps <- get
211 put (ps { nextname = n+1 })
214action :: Monad m => (ProofState -> ProofState) -> StateT TState m ()
215action a = do ps <- get
218addLog :: Monad m => String -> StateT TState m ()
219addLog str = action (\ps -> ps { plog = plog ps ++ str ++ "\n" })
221newProof :: Name -> Context -> Type -> ProofState
222newProof n ctxt ty = let h = holeName 0
224 PS n [h] [] 1 (Bind h (Hole ty')
225 (P Bound h ty')) ty [] (h, []) []
228 Nothing ctxt "" False False
230type TState = ProofState -- [TacticAction])
231type RunTactic = Context -> Env -> Term -> StateT TState TC Term
232type Hole = Maybe Name -- Nothing = default hole, first in list in proof state
234envAtFocus :: ProofState -> TC Env
236 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
238 | otherwise = fail "No holes"
240goalAtFocus :: ProofState -> TC (Binder Type)
242 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
244 | otherwise = Error . Msg $ "No goal in " ++ show (holes ps) ++ show (pterm ps)
246goal :: Hole -> Term -> TC Goal
247goal h tm = g [] tm where
248 g env (Bind n b@(Guess _ _) sc)
249 | same h n = return $ GD env b
251 = gb env b `mplus` g ((n, b):env) sc
252 g env (Bind n b sc) | hole b && same h n = return $ GD env b
254 = g ((n, b):env) sc `mplus` gb env b
255 g env (App f a) = g env f `mplus` g env a
256 g env t = fail "Can't find hole"
258 gb env (Let t v) = g env v `mplus` g env t
259 gb env (Guess t v) = g env v `mplus` g env t
260 gb env t = g env (binderTy t)
262tactic :: Hole -> RunTactic -> StateT TState TC ()
263tactic h f = do ps <- get
264 (tm', _) <- atH (context ps) [] (pterm ps)
265 ps <- get -- might have changed while processing
266 put (ps { pterm = tm' })
268 updated o = do o' <- o
272 = do (b', u) <- atH c env b
273 if u then return (op a b', True)
274 else do (a', u) <- atH c env a
277 -- Search the things most likely to contain the binding first!
279 atH :: Context -> Env -> Term -> StateT TState TC (Term, Bool)
280 atH c env binder@(Bind n b@(Guess t v) sc)
281 | same h n = updated (f c env binder)
284 (b', u) <- ulift2 c env Guess t v
285 if u then return (Bind n b' sc, True)
286 else do (sc', u) <- atH c ((n, b) : env) sc
287 return (Bind n b' sc', u)
288 atH c env binder@(Bind n b sc)
289 | hole b && same h n = updated (f c env binder)
290 | otherwise -- scope first
291 = do (sc', u) <- atH c ((n, b) : env) sc
292 if u then return (Bind n b sc', True)
293 else do (b', u) <- atHb c env b
294 return (Bind n b' sc', u)
295 atH c env (App f a) = ulift2 c env App f a
296 atH c env t = return (t, False)
298 atHb c env (Let t v) = ulift2 c env Let t v
299 atHb c env (Guess t v) = ulift2 c env Guess t v
300 atHb c env t = do (ty', u) <- atH c env (binderTy t)
301 return (t { binderTy = ty' }, u)
303computeLet :: Context -> Name -> Term -> Term
304computeLet ctxt n tm = cl [] tm where
305 cl env (Bind n' (Let t v) sc)
306 | n' == n = let v' = normalise ctxt env v in
307 Bind n' (Let t v') sc
308 cl env (Bind n' b sc) = Bind n' (fmap (cl env) b) (cl ((n, b):env) sc)
309 cl env (App f a) = App (cl env f) (cl env a)
313attack ctxt env (Bind x (Hole t) sc)
314 = do h <- getName "hole"
315 action (\ps -> ps { holes = h : holes ps })
316 return $ Bind x (Guess t (newtm h)) sc
318 newtm h = Bind h (Hole t) (P Bound h t)
319attack ctxt env _ = fail "Not an attackable hole"
321claim :: Name -> Raw -> RunTactic
322claim n ty ctxt env t =
323 do (tyv, tyt) <- lift $ check ctxt env ty
324 lift $ isType ctxt env tyt
325 action (\ps -> let (g:gs) = holes ps in
326 ps { holes = g : n : gs } )
327 return $ Bind n (Hole tyv) t -- (weakenTm 1 t)
329reorder_claims :: RunTactic
330reorder_claims ctxt env t
331 = -- trace (showSep "\n" (map show (scvs t))) $
332 let (bs, sc) = scvs t []
333 newbs = reverse (sortB (reverse bs)) in
334 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
335 return (bindAll newbs sc)
336 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
337 scvs sc acc = (reverse acc, sc)
339 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
341 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
342 | otherwise = sortB (insertB x xs)
345 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
346 | otherwise = y : insertB x ys
348 noOcc (n, _) (_, Let t v) = noOccurrence n t && noOccurrence n v
349 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
350 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
352focus :: Name -> RunTactic
353focus n ctxt env t = do action (\ps -> let hs = holes ps in
355 then ps { holes = n : (hs \\ [n]) }
360movelast :: Name -> RunTactic
361movelast n ctxt env t = do action (\ps -> let hs = holes ps in
363 then ps { holes = (hs \\ [n]) ++ [n] }
367instanceArg :: Name -> RunTactic
368instanceArg n ctxt env (Bind x (Hole t) sc)
369 = do action (\ps -> let hs = holes ps
371 ps { holes = (hs \\ [x]) ++ [x],
373 return (Bind x (Hole t) sc)
375setinj :: Name -> RunTactic
376setinj n ctxt env (Bind x (Hole t) sc)
377 = do action (\ps -> let is = injective ps in
378 ps { injective = n : is })
379 return (Bind x (Hole t) sc)
381defer :: Name -> RunTactic
382defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
383 do action (\ps -> let hs = holes ps in
384 ps { holes = hs \\ [x] })
385 return (Bind n (GHole (length env) (mkTy (reverse env) t))
386 (mkApp (P Ref n ty) (map getP (reverse env))))
389 mkTy ((n,b) : bs) t = Bind n (Pi (binderTy b)) (mkTy bs t)
391 getP (n, b) = P Bound n (binderTy b)
393-- as defer, but build the type and application explicitly
394deferType :: Name -> Raw -> [Name] -> RunTactic
395deferType n fty_in args ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
396 do (fty, _) <- lift $ check ctxt env fty_in
397 action (\ps -> let hs = holes ps
399 ps { holes = hs \\ [x],
401 return (Bind n (GHole 0 fty)
402 (mkApp (P Ref n ty) (map getP args)))
404 getP n = case lookup n env of
405 Just b -> P Bound n (binderTy b)
406 Nothing -> error ("deferType can't find " ++ show n)
409regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
410 do action (\ps -> let hs = holes ps in
411 ps { holes = hs \\ [x] })
413regret ctxt env (Bind x (Hole t) _)
414 = fail $ show x ++ " : " ++ show t ++ " is not solved..."
416exact :: Raw -> RunTactic
417exact guess ctxt env (Bind x (Hole ty) sc) =
418 do (val, valty) <- lift $ check ctxt env guess
419 lift $ converts ctxt env valty ty
420 return $ Bind x (Guess ty val) sc
421exact _ _ _ _ = fail "Can't fill here."
423-- As exact, but attempts to solve other goals by unification
425fill :: Raw -> RunTactic
426fill guess ctxt env (Bind x (Hole ty) sc) =
427 do (val, valty) <- lift $ check ctxt env guess
428-- let valtyn = normalise ctxt env valty
429-- let tyn = normalise ctxt env ty
430 ns <- unify' ctxt env valty ty
432 let (uh, uns) = unified ps
433-- put (ps { unified = (uh, uns ++ ns) })
434-- addLog (show (uh, uns ++ ns))
435 return $ Bind x (Guess ty val) sc
436fill _ _ _ _ = fail "Can't fill here."
438-- As fill, but attempts to solve other goals by matching
440match_fill :: Raw -> RunTactic
441match_fill guess ctxt env (Bind x (Hole ty) sc) =
442 do (val, valty) <- lift $ check ctxt env guess
443-- let valtyn = normalise ctxt env valty
444-- let tyn = normalise ctxt env ty
445 ns <- match_unify' ctxt env valty ty
447 let (uh, uns) = unified ps
448-- put (ps { unified = (uh, uns ++ ns) })
449-- addLog (show (uh, uns ++ ns))
450 return $ Bind x (Guess ty val) sc
451match_fill _ _ _ _ = fail "Can't fill here."
453prep_fill :: Name -> [Name] -> RunTactic
454prep_fill f as ctxt env (Bind x (Hole ty) sc) =
455 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
456 return $ Bind x (Guess ty val) sc
457prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
459complete_fill :: RunTactic
460complete_fill ctxt env (Bind x (Guess ty val) sc) =
461 do let guess = forget val
462 (val', valty) <- lift $ check ctxt env guess
463 ns <- unify' ctxt env valty ty
465 let (uh, uns) = unified ps
466-- put (ps { unified = (uh, uns ++ ns) })
467 return $ Bind x (Guess ty val) sc
468complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
470-- When solving something in the 'dont unify' set, we should check
471-- that the guess we are solving it with unifies with the thing unification
472-- found for it, if anything.
475solve ctxt env (Bind x (Guess ty val) sc)
476 | True = do ps <- get
477 let (uh, uns) = unified ps
478 case lookup x (notunified ps) of
479 Just tm -> unify' ctxt env tm val
481 action (\ps -> ps { holes = holes ps \\ [x],
482 solved = Just (x, val),
483 instances = instances ps \\ [x] })
484 let tm' = subst x val sc in
486 | otherwise = lift $ tfail $ IncompleteTerm val
487solve _ _ h@(Bind x t sc)
489 case findType x sc of
490 Just t -> lift $ tfail (CantInferType (show t))
491 _ -> fail $ "Not a guess " ++ show h ++ "\n" ++ show (holes ps, pterm ps)
492 where findType x (Bind n (Let t v) sc)
493 = findType x v `mplus` findType x sc
494 findType x (Bind n t sc)
495 | P _ x' _ <- binderTy t, x == x' = Just n
496 | otherwise = findType x sc
497 findType x _ = Nothing
499introTy :: Raw -> Maybe Name -> RunTactic
500introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
501 do let n = case mn of
505 x@(Bind y (Pi s) _) -> x
507 (tyv, tyt) <- lift $ check ctxt env ty
508-- ns <- lift $ unify ctxt env tyv t'
510 Bind y (Pi s) t -> let t' = subst y (P Bound n s) t in
511 do ns <- unify' ctxt env s tyv
513 let (uh, uns) = unified ps
514-- put (ps { unified = (uh, uns ++ ns) })
515 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
516 _ -> lift $ tfail $ CantIntroduce t'
517introTy ty n ctxt env _ = fail "Can't introduce here."
519intro :: Maybe Name -> RunTactic
520intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
521 do let n = case mn of
525 x@(Bind y (Pi s) _) -> x
528 Bind y (Pi s) t -> -- trace ("in type " ++ show t') $
529 let t' = subst y (P Bound n s) t in
530 return $ Bind n (Lam s) (Bind x (Hole t') (P Bound x t'))
531 _ -> lift $ tfail $ CantIntroduce t'
532intro n ctxt env _ = fail "Can't introduce here."
534forall :: Name -> Raw -> RunTactic
535forall n ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
536 do (tyv, tyt) <- lift $ check ctxt env ty
537 unify' ctxt env tyt (TType (UVar 0))
538 unify' ctxt env t (TType (UVar 0))
539 return $ Bind n (Pi tyv) (Bind x (Hole t) (P Bound x t))
540forall n ty ctxt env _ = fail "Can't pi bind here"
542patvar :: Name -> RunTactic
543patvar n ctxt env (Bind x (Hole t) sc) =
544 do action (\ps -> ps { holes = holes ps \\ [x] })
545 return $ Bind n (PVar t) (subst x (P Bound n t) sc)
546patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
548letbind :: Name -> Raw -> Raw -> RunTactic
549letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
550 do (tyv, tyt) <- lift $ check ctxt env ty
551 (valv, valt) <- lift $ check ctxt env val
552 lift $ isType ctxt env tyt
553 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
554letbind n ty val ctxt env _ = fail "Can't let bind here"
556expandLet :: Name -> Term -> RunTactic
557expandLet n v ctxt env tm =
558 return $ subst n v tm
560rewrite :: Raw -> RunTactic
561rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
562 do (tmv, tmt) <- lift $ check ctxt env tm
563 let tmt' = normalise ctxt env tmt
565 (P _ (UN q) _, [lt,rt,l,r]) | q == txt "=" ->
566 do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
567 let newt = mkP l r l t
568 let sc = forget $ (Bind x (Hole newt)
569 (mkApp (P Ref (sUN "replace") (TType (UVal 0)))
570 [lt, l, r, p, tmv, xp]))
571 (scv, sct) <- lift $ check ctxt env sc
573 _ -> lift $ tfail (NotEquality tmv tmt')
574 where rname = sMN 0 "replaced"
575rewrite _ _ _ _ = fail "Can't rewrite here"
577-- To make the P for rewrite, replace syntactic occurrences of l in ty with
578-- an x, and put \x : lt in front
579mkP :: TT Name -> TT Name -> TT Name -> TT Name -> TT Name
580mkP lt l r ty | l == ty = lt
581mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
582 a' = if (r /= a) then mkP lt l r a else a in
584mkP lt l r (Bind n b sc)
586 sc' = if (r /= sc) then mkP lt l r sc else sc in
588 where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
589 v' = if (r /= v) then mkP lt l r v else v in
591 mkPB b = let ty = binderTy b
592 ty' = if (r /= ty) then mkP lt l r ty else ty in
596induction :: Name -> RunTactic
597induction nm ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' = do
598 (tmv, tmt) <- lift $ check ctxt env (Var nm)
599 let tmt' = normalise ctxt env tmt
601 (P _ tnm _, tyargs) -> do
602 case lookupTy (SN (ElimN tnm)) ctxt of
604 param_pos <- case lookupMetaInformation tnm ctxt of
605 [DataMI param_pos] -> return param_pos
606 m | length tyargs > 0 -> fail $ "Invalid meta information for " ++ show tnm ++ " where the metainformation is " ++ show m ++ " and definition is" ++ show (lookupDef tnm ctxt)
608 let (params, indicies) = splitTyArgs param_pos tyargs
609 let args = getArgTys elimTy
610 let pmargs = take (length params) args
611 let args' = drop (length params) args
612 let propTy = head args'
613 let restargs = init $ tail args'
614 let consargs = take (length restargs - length indicies) $ restargs
615 let indxargs = drop (length restargs - length indicies) $ restargs
616 let scr = last $ tail args'
617 let indxnames = makeIndexNames indicies
618 prop <- replaceIndicies indxnames indicies $ Bind nm (Lam tmt') t
619 let res = flip (foldr substV) params $ (substV prop $ bindConsArgs consargs (mkApp (P Ref (SN (ElimN tnm)) (TType (UVal 0)))
620 (params ++ [prop] ++ map makeConsArg consargs ++ indicies ++ [tmv])))
621 action (\ps -> ps {holes = holes ps \\ [x]})
622 mapM_ addConsHole (reverse consargs)
623 let res' = forget $ res
624 (scv, sct) <- lift $ check ctxt env res'
625 let scv' = specialise ctxt env [] scv
627 [] -> fail $ "Induction needs an eliminator for " ++ show tnm
628 xs -> fail $ "Multiple definitions found when searching for the eliminator of " ++ show tnm
629 _ -> fail "Unkown type for induction"
630 where scname = sMN 0 "scarg"
631 makeConsArg (nm, ty) = P Bound nm ty
632 bindConsArgs ((nm, ty):args) v = Bind nm (Hole ty) $ bindConsArgs args v
633 bindConsArgs [] v = v
634 addConsHole (nm, ty) =
635 action (\ps -> ps { holes = nm : holes ps })
636 splitTyArgs param_pos tyargs =
637 let (params, indicies) = partition (flip elem param_pos . fst) . zip [0..] $ tyargs
638 in (map snd params, map snd indicies)
639 makeIndexNames = foldr (\_ nms -> (uniqueNameCtxt ctxt (sMN 0 "idx") nms):nms) []
640 replaceIndicies idnms idxs prop = foldM (\t (idnm, idx) -> do (idxv, idxt) <- lift $ check ctxt env (forget idx)
641 let var = P Bound idnm idxt
642 return $ Bind idnm (Lam idxt) (mkP var idxv var t)) prop $ zip idnms idxs
643induction tm ctxt env _ = do fail "Can't do induction here"
646equiv :: Raw -> RunTactic
647equiv tm ctxt env (Bind x (Hole t) sc) =
648 do (tmv, tmt) <- lift $ check ctxt env tm
649 lift $ converts ctxt env tmv t
650 return $ Bind x (Hole tmv) sc
651equiv tm ctxt env _ = fail "Can't equiv here"
653patbind :: Name -> RunTactic
654patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
655 do let t' = case t of
656 x@(Bind y (PVTy s) t) -> x
659 Bind y (PVTy s) t -> let t' = subst y (P Bound n s) t in
660 return $ Bind n (PVar s) (Bind x (Hole t') (P Bound x t'))
661 _ -> fail "Nothing to pattern bind"
662patbind n ctxt env _ = fail "Can't pattern bind here"
665compute ctxt env (Bind x (Hole ty) sc) =
666 do return $ Bind x (Hole (normalise ctxt env ty)) sc
667compute ctxt env t = return t
669hnf_compute :: RunTactic
670hnf_compute ctxt env (Bind x (Hole ty) sc) =
671 do let ty' = hnf ctxt env ty in
672-- trace ("HNF " ++ show (ty, ty')) $
673 return $ Bind x (Hole ty') sc
674hnf_compute ctxt env t = return t
676-- reduce let bindings only
678simplify ctxt env (Bind x (Hole ty) sc) =
679 do return $ Bind x (Hole (specialise ctxt env [] ty)) sc
680simplify ctxt env t = return t
682check_in :: Raw -> RunTactic
683check_in t ctxt env tm =
684 do (val, valty) <- lift $ check ctxt env t
685 addLog (showEnv env val ++ " : " ++ showEnv env valty)
688eval_in :: Raw -> RunTactic
689eval_in t ctxt env tm =
690 do (val, valty) <- lift $ check ctxt env t
691 let val' = normalise ctxt env val
692 let valty' = normalise ctxt env valty
693 addLog (showEnv env val ++ " : " ++
695-- " in " ++ show env ++
697 showEnv env val' ++ " : " ++
701start_unify :: Name -> RunTactic
702start_unify n ctxt env tm = do -- action (\ps -> ps { unified = (n, []) })
705tmap f (a, b, c) = (f a, b, c)
707solve_unified :: RunTactic
708solve_unified ctxt env tm =
710 let (_, ns) = unified ps
711 let unify = dropGiven (dontunify ps) ns (holes ps)
712 action (\ps -> ps { holes = holes ps \\ map fst unify })
713 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
714 return (updateSolved unify tm)
717dropGiven du [] hs = []
718dropGiven du ((n, P Bound t ty) : us) hs
719 | n `elem` du && not (t `elem` du)
720 && n `elem` hs && t `elem` hs
721 = (t, P Bound n ty) : dropGiven du us hs
722dropGiven du (u@(n, _) : us) hs
723 | n `elem` du = dropGiven du us hs
724-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
725dropGiven du (u : us) hs = u : dropGiven du us hs
727keepGiven du [] hs = []
728keepGiven du ((n, P Bound t ty) : us) hs
729 | n `elem` du && not (t `elem` du)
730 && n `elem` hs && t `elem` hs
732keepGiven du (u@(n, _) : us) hs
733 | n `elem` du = u : keepGiven du us hs
734keepGiven du (u : us) hs = keepGiven du us hs
736updateSolved xs x = updateSolved' xs x
737updateSolved' [] x = x
738updateSolved' xs (Bind n (Hole ty) t)
739 | Just v <- lookup n xs
742 _ -> psubst n v (updateSolved' xs t)
743updateSolved' xs (Bind n b t)
744 | otherwise = Bind n (fmap (updateSolved' xs) b) (updateSolved' xs t)
745updateSolved' xs (App f a)
746 = App (updateSolved' xs f) (updateSolved' xs a)
747updateSolved' xs (P _ n@(MN _ _) _)
748 | Just v <- lookup n xs = v
749updateSolved' xs t = t
753updateEnv ns ((n, b) : env) = (n, fmap (updateSolved ns) b) : updateEnv ns env
755updateError [] err = err
756updateError ns (CantUnify b l r e xs sc)
757 = CantUnify b (updateSolved ns l) (updateSolved ns r) (updateError ns e) xs sc
760solveInProblems x val [] = []
761solveInProblems x val ((l, r, env, err) : ps)
762 = ((psubst x val l, psubst x val r,
763 updateEnv [(x, val)] env, err) : solveInProblems x val ps)
765updateNotunified [] nu = nu
766updateNotunified ns nu = up nu where
768 up ((n, t) : nus) = let t' = updateSolved ns t in
771updateProblems ctxt [] ps inj holes = ([], ps)
772updateProblems ctxt ns ps inj holes = up ns ps where
774 up ns ((x, y, env, err) : ps) =
775 let x' = updateSolved ns x
776 y' = updateSolved ns y
777 err' = updateError ns err
778 env' = updateEnv ns env in
779 case unify ctxt env' x' y' inj holes of
780 OK (v, []) -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
782 _ -> let (ns', ps') = up ns ps in
783 (ns', (x',y',env',err') : ps')
785-- attempt to solve remaining problems with match_unify
786matchProblems ctxt ps inj holes = up [] ps where
788 up ns ((x, y, env, err) : ps) =
789 let x' = updateSolved ns x
790 y' = updateSolved ns y
791 err' = updateError ns err
792 env' = updateEnv ns env in
793 case match_unify ctxt env' x' y' inj holes of
794 OK v -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
796 _ -> let (ns', ps') = up ns ps in
797 (ns', (x',y',env',err') : ps')
799processTactic :: Tactic -> ProofState -> TC (ProofState, String)
800processTactic QED ps = case holes ps of
801 [] -> do let tm = {- normalise (context ps) [] -} (pterm ps)
802 (tm', ty', _) <- recheck (context ps) [] (forget tm) tm
803 return (ps { done = True, pterm = tm' },
804 "Proof complete: " ++ showEnv [] tm')
805 _ -> fail "Still holes to fill."
806processTactic ProofState ps = return (ps, showEnv [] (pterm ps))
807processTactic Undo ps = case previous ps of
808 Nothing -> fail "Nothing to undo."
809 Just pold -> return (pold, "")
810processTactic EndUnify ps
811 = let (h, ns_in) = unified ps
812 ns = dropGiven (dontunify ps) ns_in (holes ps)
813 ns' = map (\ (n, t) -> (n, updateSolved ns t)) ns
814 (ns'', probs') = updateProblems (context ps) ns' (problems ps)
815 (injective ps) (holes ps)
816 tm' = updateSolved ns'' (pterm ps) in
817 return (ps { pterm = tm',
820 notunified = updateNotunified ns'' (notunified ps),
821 holes = holes ps \\ map fst ns'' }, "")
822processTactic (Reorder n) ps
823 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
824 return (ps' { previous = Just ps, plog = "" }, plog ps')
825processTactic (ComputeLet n) ps
826 = return (ps { pterm = computeLet (context ps) n (pterm ps) }, "")
827processTactic UnifyProblems ps
828 = let (ns', probs') = updateProblems (context ps) []
832 pterm' = updateSolved ns' (pterm ps) in
833 return (ps { pterm = pterm', solved = Nothing, problems = probs',
834 previous = Just ps, plog = "",
835 notunified = updateNotunified ns' (notunified ps),
836 holes = holes ps \\ (map fst ns') }, plog ps)
837processTactic MatchProblems ps
838 = let (ns', probs') = matchProblems (context ps)
842 pterm' = updateSolved ns' (pterm ps) in
843 return (ps { pterm = pterm', solved = Nothing, problems = probs',
844 previous = Just ps, plog = "",
845 notunified = updateNotunified ns' (notunified ps),
846 holes = holes ps \\ (map fst ns') }, plog ps)
849 [] -> fail "Nothing to fill in."
850 (h:_) -> do ps' <- execStateT (process t h) ps
853 Just s -> updateProblems (context ps')
857 _ -> ([], problems ps')
858 -- rechecking problems may find more solutions, so
860 let pterm'' = updateSolved ns' (pterm ps')
861 return (ps' { pterm = pterm'',
864 notunified = updateNotunified ns' (notunified ps'),
865 previous = Just ps, plog = "",
866 holes = holes ps' \\ (map fst ns')}, plog ps')
868process :: Tactic -> Name -> StateT TState TC ()
871 let (h, _) = unified ps
872 tactic (Just h) solve_unified
873process t h = tactic (Just h) (mktac t)
874 where mktac Attack = attack
875 mktac (Claim n r) = claim n r
876 mktac (Exact r) = exact r
877 mktac (Fill r) = fill r
878 mktac (MatchFill r) = match_fill r
879 mktac (PrepFill n ns) = prep_fill n ns
880 mktac CompleteFill = complete_fill
881 mktac Regret = regret
883 mktac (StartUnify n) = start_unify n
884 mktac Compute = compute
885 mktac Simplify = Idris.Core.ProofState.simplify
886 mktac HNF_Compute = hnf_compute
887 mktac (Intro n) = intro n
888 mktac (IntroTy ty n) = introTy ty n
889 mktac (Forall n t) = forall n t
890 mktac (LetBind n t v) = letbind n t v
891 mktac (ExpandLet n b) = expandLet n b
892 mktac (Rewrite t) = rewrite t
893 mktac (Induction t) = induction t
894 mktac (Equiv t) = equiv t
895 mktac (PatVar n) = patvar n
896 mktac (PatBind n) = patbind n
897 mktac (CheckIn r) = check_in r
898 mktac (EvalIn r) = eval_in r
899 mktac (Focus n) = focus n
900 mktac (Defer n) = defer n
901 mktac (DeferType n t a) = deferType n t a
902 mktac (Instance n) = instanceArg n
903 mktac (SetInjective n) = setinj n
904 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