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, nowElaboratingPS, doneElaboratingAppPS,
9 doneElaboratingArgPS, dropGiven, keepGiven) where
11import Idris.Core.Typecheck
12import Idris.Core.Evaluate
14import Idris.Core.Unify
15import Idris.Core.ProofTerm
17import Control.Monad.State.Strict
18import Control.Applicative hiding (empty)
19import Control.Arrow ((***))
23import Util.Pretty hiding (fill)
25data ProofState = PS { thname :: Name,
26 holes :: [Name], -- holes still to be solved
27 usedns :: [Name], -- used names, don't use again
28 nextname :: Int, -- name supply
29 pterm :: ProofTerm, -- current proof term
30 ptype :: Type, -- original goal
31 dontunify :: [Name], -- explicitly given by programmer, leave it
32 unified :: (Name, [(Name, Term)]),
33 notunified :: [(Name, Term)],
34 solved :: Maybe (Name, Term),
37 deferred :: [Name], -- names we'll need to define
38 instances :: [Name], -- instance arguments (for type classes)
39 previous :: Maybe ProofState, -- for undo
44 while_elaborating :: [FailContext]
53 | PrepFill Name [Name]
67 | IntroTy Raw (Maybe Name)
69 | LetBind Name Raw Raw
79 | DeferType Name Raw [Name]
90-- Some utilites on proof and tactic states
92instance Show ProofState where
93 show (PS nm [] _ _ tm _ _ _ _ _ _ _ _ _ _ _ _ _ _ _)
94 = show nm ++ ": no more goals"
95 show (PS nm (h:hs) _ _ tm _ _ _ _ _ _ _ i _ _ ctxt _ _ _ _)
96 = let OK g = goal (Just h) tm
98 "Other goals: " ++ show hs ++ "\n" ++
99 showPs wkenv (reverse wkenv) ++ "\n" ++
100 "-------------------------------- (" ++ show nm ++
102 show h ++ " : " ++ showG wkenv (goalType g) ++ "\n"
103 where showPs env [] = ""
104 showPs env ((n, Let t v):bs)
105 = " " ++ show n ++ " : " ++
106 showEnv env ({- normalise ctxt env -} t) ++ " = " ++
107 showEnv env ({- normalise ctxt env -} v) ++
108 "\n" ++ showPs env bs
109 showPs env ((n, b):bs)
110 = " " ++ show n ++ " : " ++
111 showEnv env ({- normalise ctxt env -} (binderTy b)) ++
112 "\n" ++ showPs env bs
113 showG ps (Guess t v) = showEnv ps ({- normalise ctxt ps -} t) ++
114 " =?= " ++ showEnv ps v
115 showG ps b = showEnv ps (binderTy b)
117instance Pretty ProofState OutputAnnotation where
118 pretty (PS nm [] _ _ trm _ _ _ _ _ _ _ _ _ _ _ _ _ _ _) =
119 pretty nm <+> colon <+> text " no more goals."
120 pretty p@(PS nm (h:hs) _ _ tm _ _ _ _ _ _ _ i _ _ ctxt _ _ _ _) =
121 let OK g = goal (Just h) tm in
122 let wkEnv = premises g in
123 text "Other goals" <+> colon <+> pretty hs <+>
124 prettyPs wkEnv (reverse wkEnv) <+>
125 text "---------- " <+> text "Focussing on" <> colon <+> pretty nm <+> text " ----------" <+>
126 pretty h <+> colon <+> prettyGoal wkEnv (goalType g)
128 prettyGoal ps (Guess t v) =
129 prettyEnv ps t <+> text "=?=" <+> prettyEnv ps v
130 prettyGoal ps b = prettyEnv ps $ binderTy b
132 prettyPs env [] = empty
133 prettyPs env ((n, Let t v):bs) =
134 nest nestingSize (pretty n <+> colon <+>
135 prettyEnv env t <+> text "=" <+> prettyEnv env v <+>
136 nest nestingSize (prettyPs env bs))
137 prettyPs env ((n, b):bs) =
138 nest nestingSize (pretty n <+> colon <+> prettyEnv env (binderTy b) <+>
139 nest nestingSize (prettyPs env bs))
141holeName i = sMN i "hole"
143qshow :: Fails -> String
144qshow fs = show (map (\ (x, y, _, _, _, t) -> (t, x, y)) fs)
146match_unify' :: Context -> Env -> TT Name -> TT Name ->
147 StateT TState TC [(Name, TT Name)]
148match_unify' ctxt env topx topy =
150 let while = while_elaborating ps
151 let dont = dontunify ps
152 let inj = injective ps
153 traceWhen (unifylog ps)
154 ("Matching " ++ show (topx, topy) ++
155 " in " ++ show env ++
156 "\nHoles: " ++ show (holes ps)
158 ++ "\n" ++ show (getProofTerm (pterm ps)) ++ "\n\n"
160 case match_unify ctxt env topx topy inj (holes ps) while of
161 OK u -> traceWhen (unifylog ps)
162 ("Matched " ++ show u) $
163 do let (h, ns) = unified ps
164 put (ps { unified = (h, u ++ ns) })
166 Error e -> traceWhen (unifylog ps)
167 ("No match " ++ show e) $
168 do put (ps { problems = (topx, topy, env, e, while, Match) :
171-- traceWhen (unifylog ps)
172-- ("Matched " ++ show (topx, topy) ++ " without " ++ show dont ++
173-- "\nSolved: " ++ show u
174-- ++ "\nCurrent problems:\n" ++ qshow (problems ps)
175-- -- ++ show (pterm ps)
176-- ++ "\n----------") $
178mergeSolutions :: Env -> [(Name, TT Name)] -> StateT TState TC [(Name, TT Name)]
179mergeSolutions env ns = merge [] ns
181 merge acc [] = return (reverse acc)
182 merge acc ((n, t) : ns)
183 | Just t' <- lookup n ns
185 let probs = problems ps
186 put (ps { problems = probs ++ [(t,t',env,Msg "New problem",
189 | otherwise = merge ((n, t): acc) ns
191unify' :: Context -> Env -> TT Name -> TT Name ->
192 StateT TState TC [(Name, TT Name)]
193unify' ctxt env topx topy =
195 let while = while_elaborating ps
196 let dont = dontunify ps
197 let inj = injective ps
198 (u, fails) <- traceWhen (unifylog ps)
199 ("Trying " ++ show (topx, topy) ++
200 "\nNormalised " ++ show (normalise ctxt env topx,
201 normalise ctxt env topy) ++
202 " in " ++ show env ++
203 "\nHoles: " ++ show (holes ps)
204 ++ "\nInjective: " ++ show (injective ps)
206 lift $ unify ctxt env topx topy inj (holes ps)
207 (map fst (notunified ps)) while
208 let notu = filter (\ (n, t) -> case t of
210 _ -> n `elem` dont) u
211 traceWhen (unifylog ps)
212 ("Unified " ++ show (topx, topy) ++ " without " ++ show dont ++
213 "\nSolved: " ++ show u ++ "\nNew problems: " ++ qshow fails
214 ++ "\nNot unified:\n" ++ show (notunified ps)
215 ++ "\nCurrent problems:\n" ++ qshow (problems ps)
218 do let (h, ns) = unified ps
219 -- solve in results (maybe unify should do this itself...)
220 let u' = map (\(n, sol) -> (n, updateSolvedTerm u sol)) u
221 -- if a metavar has multiple solutions, make a new unification
223 uns <- mergeSolutions env (u' ++ ns)
225 let (ns', probs') = updateProblems (context ps) uns
226 (fails ++ problems ps)
229 (map fst (notunified ps))
230 let (notu', probs_notu) = mergeNotunified env (notu ++ notunified ps)
231 traceWhen (unifylog ps)
232 ("Now solved: " ++ show ns' ++
233 "\nNow problems: " ++ qshow (probs' ++ probs_notu)) $
234 put (ps { problems = probs' ++ probs_notu,
236 injective = updateInj u (injective ps),
237 notunified = notu' })
239 where updateInj ((n, a) : us) inj
240 | (P _ n' _, _) <- unApply a,
241 n `elem` inj = updateInj us (n':inj)
242 | (P _ n' _, _) <- unApply a,
243 n' `elem` inj = updateInj us (n:inj)
244 updateInj (_ : us) inj = updateInj us inj
245 updateInj [] inj = inj
247nowElaboratingPS :: FC -> Name -> Name -> ProofState -> ProofState
248nowElaboratingPS fc f arg ps = ps { while_elaborating = FailContext fc f arg : while_elaborating ps }
250dropUntil :: (a -> Bool) -> [a] -> [a]
252dropUntil p (x:xs) | p x = xs
253 | otherwise = dropUntil p xs
255doneElaboratingAppPS :: Name -> ProofState -> ProofState
256doneElaboratingAppPS f ps = let while = while_elaborating ps
257 while' = dropUntil (\ (FailContext _ f' _) -> f == f') while
258 in ps { while_elaborating = while' }
260doneElaboratingArgPS :: Name -> Name -> ProofState -> ProofState
261doneElaboratingArgPS f x ps = let while = while_elaborating ps
262 while' = dropUntil (\ (FailContext _ f' x') -> f == f' && x == x') while
263 in ps { while_elaborating = while' }
265getName :: Monad m => String -> StateT TState m Name
266getName tag = do ps <- get
268 put (ps { nextname = n+1 })
271action :: Monad m => (ProofState -> ProofState) -> StateT TState m ()
272action a = do ps <- get
275query :: Monad m => (ProofState -> r) -> StateT TState m r
276query q = do ps <- get
279addLog :: Monad m => String -> StateT TState m ()
280addLog str = action (\ps -> ps { plog = plog ps ++ str ++ "\n" })
282newProof :: Name -> Context -> Type -> ProofState
283newProof n ctxt ty = let h = holeName 0
285 PS n [h] [] 1 (mkProofTerm (Bind h (Hole ty')
286 (P Bound h ty'))) ty [] (h, []) []
289 Nothing ctxt "" False False []
291type TState = ProofState -- [TacticAction])
292type RunTactic = RunTactic' TState
294envAtFocus :: ProofState -> TC Env
296 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
298 | otherwise = fail "No holes"
300goalAtFocus :: ProofState -> TC (Binder Type)
302 | not $ null (holes ps) = do g <- goal (Just (head (holes ps))) (pterm ps)
304 | otherwise = Error . Msg $ "No goal in " ++ show (holes ps) ++ show (getProofTerm (pterm ps))
306tactic :: Hole -> RunTactic -> StateT TState TC ()
307tactic h f = do ps <- get
308 (tm', _) <- atHole h f (context ps) [] (pterm ps)
309 ps <- get -- might have changed while processing
310 put (ps { pterm = tm' })
312computeLet :: Context -> Name -> Term -> Term
313computeLet ctxt n tm = cl [] tm where
314 cl env (Bind n' (Let t v) sc)
315 | n' == n = let v' = normalise ctxt env v in
316 Bind n' (Let t v') sc
317 cl env (Bind n' b sc) = Bind n' (fmap (cl env) b) (cl ((n, b):env) sc)
318 cl env (App f a) = App (cl env f) (cl env a)
322attack ctxt env (Bind x (Hole t) sc)
323 = do h <- getName "hole"
324 action (\ps -> ps { holes = h : holes ps })
325 return $ Bind x (Guess t (newtm h)) sc
327 newtm h = Bind h (Hole t) (P Bound h t)
328attack ctxt env _ = fail "Not an attackable hole"
330claim :: Name -> Raw -> RunTactic
331claim n ty ctxt env t =
332 do (tyv, tyt) <- lift $ check ctxt env ty
333 lift $ isType ctxt env tyt
334 action (\ps -> let (g:gs) = holes ps in
335 ps { holes = g : n : gs } )
336 return $ Bind n (Hole tyv) t -- (weakenTm 1 t)
338reorder_claims :: RunTactic
339reorder_claims ctxt env t
340 = -- trace (showSep "\n" (map show (scvs t))) $
341 let (bs, sc) = scvs t []
342 newbs = reverse (sortB (reverse bs)) in
343 traceWhen (bs /= newbs) (show bs ++ "\n ==> \n" ++ show newbs) $
344 return (bindAll newbs sc)
345 where scvs (Bind n b@(Hole _) sc) acc = scvs sc ((n, b):acc)
346 scvs sc acc = (reverse acc, sc)
348 sortB :: [(Name, Binder (TT Name))] -> [(Name, Binder (TT Name))]
350 sortB (x:xs) | all (noOcc x) xs = x : sortB xs
351 | otherwise = sortB (insertB x xs)
354 insertB x (y:ys) | all (noOcc x) (y:ys) = x : y : ys
355 | otherwise = y : insertB x ys
357 noOcc (n, _) (_, Let t v) = noOccurrence n t && noOccurrence n v
358 noOcc (n, _) (_, Guess t v) = noOccurrence n t && noOccurrence n v
359 noOcc (n, _) (_, b) = noOccurrence n (binderTy b)
361focus :: Name -> RunTactic
362focus n ctxt env t = do action (\ps -> let hs = holes ps in
364 then ps { holes = n : (hs \\ [n]) }
369movelast :: Name -> RunTactic
370movelast n ctxt env t = do action (\ps -> let hs = holes ps in
372 then ps { holes = (hs \\ [n]) ++ [n] }
376instanceArg :: Name -> RunTactic
377instanceArg n ctxt env (Bind x (Hole t) sc)
378 = do action (\ps -> let hs = holes ps
380 ps { holes = (hs \\ [x]) ++ [x],
382 return (Bind x (Hole t) sc)
384setinj :: Name -> RunTactic
385setinj n ctxt env (Bind x b sc)
386 = do action (\ps -> let is = injective ps in
387 ps { injective = n : is })
390defer :: Name -> RunTactic
391defer n ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
392 do action (\ps -> let hs = holes ps in
393 ps { holes = hs \\ [x] })
394 return (Bind n (GHole (length env) (mkTy (reverse env) t))
395 (mkApp (P Ref n ty) (map getP (reverse env))))
398 mkTy ((n,b) : bs) t = Bind n (Pi (binderTy b)) (mkTy bs t)
400 getP (n, b) = P Bound n (binderTy b)
402-- as defer, but build the type and application explicitly
403deferType :: Name -> Raw -> [Name] -> RunTactic
404deferType n fty_in args ctxt env (Bind x (Hole t) (P nt x' ty)) | x == x' =
405 do (fty, _) <- lift $ check ctxt env fty_in
406 action (\ps -> let hs = holes ps
408 ps { holes = hs \\ [x],
410 return (Bind n (GHole 0 fty)
411 (mkApp (P Ref n ty) (map getP args)))
413 getP n = case lookup n env of
414 Just b -> P Bound n (binderTy b)
415 Nothing -> error ("deferType can't find " ++ show n)
418regret ctxt env (Bind x (Hole t) sc) | noOccurrence x sc =
419 do action (\ps -> let hs = holes ps in
420 ps { holes = hs \\ [x] })
422regret ctxt env (Bind x (Hole t) _)
423 = fail $ show x ++ " : " ++ show t ++ " is not solved..."
425exact :: Raw -> RunTactic
426exact guess ctxt env (Bind x (Hole ty) sc) =
427 do (val, valty) <- lift $ check ctxt env guess
428 lift $ converts ctxt env valty ty
429 return $ Bind x (Guess ty val) sc
430exact _ _ _ _ = fail "Can't fill here."
432-- As exact, but attempts to solve other goals by unification
434fill :: Raw -> RunTactic
435fill guess ctxt env (Bind x (Hole ty) sc) =
436 do (val, valty) <- lift $ check ctxt env guess
437-- let valtyn = normalise ctxt env valty
438-- let tyn = normalise ctxt env ty
439 ns <- unify' ctxt env valty ty
441 let (uh, uns) = unified ps
442-- put (ps { unified = (uh, uns ++ ns) })
443-- addLog (show (uh, uns ++ ns))
444 return $ Bind x (Guess ty val) sc
445fill _ _ _ _ = fail "Can't fill here."
447-- As fill, but attempts to solve other goals by matching
449match_fill :: Raw -> RunTactic
450match_fill guess ctxt env (Bind x (Hole ty) sc) =
451 do (val, valty) <- lift $ check ctxt env guess
452-- let valtyn = normalise ctxt env valty
453-- let tyn = normalise ctxt env ty
454 ns <- match_unify' ctxt env valty ty
456 let (uh, uns) = unified ps
457-- put (ps { unified = (uh, uns ++ ns) })
458-- addLog (show (uh, uns ++ ns))
459 return $ Bind x (Guess ty val) sc
460match_fill _ _ _ _ = fail "Can't fill here."
462prep_fill :: Name -> [Name] -> RunTactic
463prep_fill f as ctxt env (Bind x (Hole ty) sc) =
464 do let val = mkApp (P Ref f Erased) (map (\n -> P Ref n Erased) as)
465 return $ Bind x (Guess ty val) sc
466prep_fill f as ctxt env t = fail $ "Can't prepare fill at " ++ show t
468complete_fill :: RunTactic
469complete_fill ctxt env (Bind x (Guess ty val) sc) =
470 do let guess = forget val
471 (val', valty) <- lift $ check ctxt env guess
472 ns <- unify' ctxt env valty ty
474 let (uh, uns) = unified ps
475-- put (ps { unified = (uh, uns ++ ns) })
476 return $ Bind x (Guess ty val) sc
477complete_fill ctxt env t = fail $ "Can't complete fill at " ++ show t
479-- When solving something in the 'dont unify' set, we should check
480-- that the guess we are solving it with unifies with the thing unification
481-- found for it, if anything.
484solve ctxt env (Bind x (Guess ty val) sc)
486 let (uh, uns) = unified ps
487 case lookup x (notunified ps) of
488 Just tm -> -- trace ("NEED MATCH: " ++ show (x, tm, val) ++ "\nIN " ++ show (pterm ps)) $
489 match_unify' ctxt env tm val
491 action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping hole " ++ show x) $
493 solved = Just (x, val),
494 notunified = updateNotunified [(x,val)]
496 instances = instances ps \\ [x] })
497 let tm' = subst x val sc in
499solve _ _ h@(Bind x t sc)
501 case findType x sc of
502 Just t -> lift $ tfail (CantInferType (show t))
503 _ -> lift $ tfail (IncompleteTerm h)
504 where findType x (Bind n (Let t v) sc)
505 = findType x v `mplus` findType x sc
506 findType x (Bind n t sc)
507 | P _ x' _ <- binderTy t, x == x' = Just n
508 | otherwise = findType x sc
509 findType x _ = Nothing
511introTy :: Raw -> Maybe Name -> RunTactic
512introTy ty mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
513 do let n = case mn of
517 x@(Bind y (Pi s) _) -> x
519 (tyv, tyt) <- lift $ check ctxt env ty
520-- ns <- lift $ unify ctxt env tyv t'
522 Bind y (Pi s) t -> let t' = subst y (P Bound n s) t in
523 do ns <- unify' ctxt env s tyv
525 let (uh, uns) = unified ps
526-- put (ps { unified = (uh, uns ++ ns) })
527 return $ Bind n (Lam tyv) (Bind x (Hole t') (P Bound x t'))
528 _ -> lift $ tfail $ CantIntroduce t'
529introTy ty n ctxt env _ = fail "Can't introduce here."
531intro :: Maybe Name -> RunTactic
532intro mn ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
533 do let n = case mn of
537 x@(Bind y (Pi s) _) -> x
540 Bind y (Pi s) t -> -- trace ("in type " ++ show t') $
541 let t' = subst y (P Bound n s) t in
542 return $ Bind n (Lam s) (Bind x (Hole t') (P Bound x t'))
543 _ -> lift $ tfail $ CantIntroduce t'
544intro n ctxt env _ = fail "Can't introduce here."
546forall :: Name -> Raw -> RunTactic
547forall n ty ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
548 do (tyv, tyt) <- lift $ check ctxt env ty
549 unify' ctxt env tyt (TType (UVar 0))
550 unify' ctxt env t (TType (UVar 0))
551 return $ Bind n (Pi tyv) (Bind x (Hole t) (P Bound x t))
552forall n ty ctxt env _ = fail "Can't pi bind here"
554patvar :: Name -> RunTactic
555patvar n ctxt env (Bind x (Hole t) sc) =
556 do action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping pattern hole " ++ show x) $
558 solved = Just (x, P Bound n t),
559 notunified = updateNotunified [(x,P Bound n t)]
561 injective = addInj n x (injective ps) })
562 return $ Bind n (PVar t) (subst x (P Bound n t) sc)
563 where addInj n x ps | x `elem` ps = n : ps
565patvar n ctxt env tm = fail $ "Can't add pattern var at " ++ show tm
567letbind :: Name -> Raw -> Raw -> RunTactic
568letbind n ty val ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
569 do (tyv, tyt) <- lift $ check ctxt env ty
570 (valv, valt) <- lift $ check ctxt env val
571 lift $ isType ctxt env tyt
572 return $ Bind n (Let tyv valv) (Bind x (Hole t) (P Bound x t))
573letbind n ty val ctxt env _ = fail "Can't let bind here"
575expandLet :: Name -> Term -> RunTactic
576expandLet n v ctxt env tm =
577 return $ subst n v tm
579rewrite :: Raw -> RunTactic
580rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
581 do (tmv, tmt) <- lift $ check ctxt env tm
582 let tmt' = normalise ctxt env tmt
584 (P _ (UN q) _, [lt,rt,l,r]) | q == txt "=" ->
585 do let p = Bind rname (Lam lt) (mkP (P Bound rname lt) r l t)
586 let newt = mkP l r l t
587 let sc = forget $ (Bind x (Hole newt)
588 (mkApp (P Ref (sUN "replace") (TType (UVal 0)))
589 [lt, l, r, p, tmv, xp]))
590 (scv, sct) <- lift $ check ctxt env sc
592 _ -> lift $ tfail (NotEquality tmv tmt')
593 where rname = sMN 0 "replaced"
594rewrite _ _ _ _ = fail "Can't rewrite here"
596-- To make the P for rewrite, replace syntactic occurrences of l in ty with
597-- an x, and put \x : lt in front
598mkP :: TT Name -> TT Name -> TT Name -> TT Name -> TT Name
599mkP lt l r ty | l == ty = lt
600mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
601 a' = if (r /= a) then mkP lt l r a else a in
603mkP lt l r (Bind n b sc)
605 sc' = if (r /= sc) then mkP lt l r sc else sc in
607 where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
608 v' = if (r /= v) then mkP lt l r v else v in
610 mkPB b = let ty = binderTy b
611 ty' = if (r /= ty) then mkP lt l r ty else ty in
615casetac :: Raw -> Bool -> RunTactic
616casetac tm induction ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' = do
617 (tmv, tmt) <- lift $ check ctxt env tm
618 let tmt' = normalise ctxt env tmt
619 let (tacn, tacstr, tactt) = if induction
620 then (ElimN, "an eliminator", "induction")
621 else (CaseN, "a case function", "case analysis")
623 (P _ tnm _, tyargs) -> do
624 case lookupTy (SN (tacn tnm)) ctxt of
626 param_pos <- case lookupMetaInformation tnm ctxt of
627 [DataMI param_pos] -> return param_pos
628 m | length tyargs > 0 -> fail $ "Invalid meta information for " ++ show tnm ++ " where the metainformation is " ++ show m ++ " and definition is" ++ show (lookupDef tnm ctxt)
630 let (params, indicies) = splitTyArgs param_pos tyargs
631 let args = getArgTys elimTy
632 let pmargs = take (length params) args
633 let args' = drop (length params) args
634 let propTy = head args'
635 let restargs = init $ tail args'
636 let consargs = take (length restargs - length indicies) $ restargs
637 let indxargs = drop (length restargs - length indicies) $ restargs
638 let scr = last $ tail args'
639 let indxnames = makeIndexNames indicies
640 currentNames <- query $ allTTNames . getProofTerm . pterm
641 let tmnm = case tm of
642 Var nm -> uniqueNameCtxt ctxt nm currentNames
643 _ -> uniqueNameCtxt ctxt (sMN 0 "iv") currentNames
644 let tmvar = P Bound tmnm tmt'
645 prop <- replaceIndicies indxnames indicies $ Bind tmnm (Lam tmt') (mkP tmvar tmv tmvar t)
646 consargs' <- query (\ps -> map (flip (uniqueNameCtxt (context ps)) (holes ps ++ allTTNames (getProofTerm (pterm ps))) *** uniqueBindersCtxt (context ps) (holes ps ++ allTTNames (getProofTerm (pterm ps)))) consargs)
647 let res = flip (foldr substV) params $ (substV prop $ bindConsArgs consargs' (mkApp (P Ref (SN (tacn tnm)) (TType (UVal 0)))
648 (params ++ [prop] ++ map makeConsArg consargs' ++ indicies ++ [tmv])))
649 action (\ps -> ps {holes = holes ps \\ [x]})
650 mapM_ addConsHole (reverse consargs')
651 let res' = forget $ res
652 (scv, sct) <- lift $ check ctxt env res'
653 let scv' = specialise ctxt env [] scv
655 [] -> fail $ tactt ++ " needs " ++ tacstr ++ " for " ++ show tnm
656 xs -> fail $ "Multiple definitions found when searching for " ++ tacstr ++ "of " ++ show tnm
657 _ -> fail $ "Unkown type for " ++ if induction then "induction" else "case analysis"
658 where scname = sMN 0 "scarg"
659 makeConsArg (nm, ty) = P Bound nm ty
660 bindConsArgs ((nm, ty):args) v = Bind nm (Hole ty) $ bindConsArgs args v
661 bindConsArgs [] v = v
662 addConsHole (nm, ty) =
663 action (\ps -> ps { holes = nm : holes ps })
664 splitTyArgs param_pos tyargs =
665 let (params, indicies) = partition (flip elem param_pos . fst) . zip [0..] $ tyargs
666 in (map snd params, map snd indicies)
667 makeIndexNames = foldr (\_ nms -> (uniqueNameCtxt ctxt (sMN 0 "idx") nms):nms) []
668 replaceIndicies idnms idxs prop = foldM (\t (idnm, idx) -> do (idxv, idxt) <- lift $ check ctxt env (forget idx)
669 let var = P Bound idnm idxt
670 return $ Bind idnm (Lam idxt) (mkP var idxv var t)) prop $ zip idnms idxs
671induction tm induction ctxt env _ = do fail "Can't do induction here"
674equiv :: Raw -> RunTactic
675equiv tm ctxt env (Bind x (Hole t) sc) =
676 do (tmv, tmt) <- lift $ check ctxt env tm
677 lift $ converts ctxt env tmv t
678 return $ Bind x (Hole tmv) sc
679equiv tm ctxt env _ = fail "Can't equiv here"
681patbind :: Name -> RunTactic
682patbind n ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' =
683 do let t' = case t of
684 x@(Bind y (PVTy s) t) -> x
687 Bind y (PVTy s) t -> let t' = subst y (P Bound n s) t in
688 return $ Bind n (PVar s) (Bind x (Hole t') (P Bound x t'))
689 _ -> fail "Nothing to pattern bind"
690patbind n ctxt env _ = fail "Can't pattern bind here"
693compute ctxt env (Bind x (Hole ty) sc) =
694 do return $ Bind x (Hole (normalise ctxt env ty)) sc
695compute ctxt env t = return t
697hnf_compute :: RunTactic
698hnf_compute ctxt env (Bind x (Hole ty) sc) =
699 do let ty' = hnf ctxt env ty in
700-- trace ("HNF " ++ show (ty, ty')) $
701 return $ Bind x (Hole ty') sc
702hnf_compute ctxt env t = return t
704-- reduce let bindings only
706simplify ctxt env (Bind x (Hole ty) sc) =
707 do return $ Bind x (Hole (specialise ctxt env [] ty)) sc
708simplify ctxt env t = return t
710check_in :: Raw -> RunTactic
711check_in t ctxt env tm =
712 do (val, valty) <- lift $ check ctxt env t
713 addLog (showEnv env val ++ " : " ++ showEnv env valty)
716eval_in :: Raw -> RunTactic
717eval_in t ctxt env tm =
718 do (val, valty) <- lift $ check ctxt env t
719 let val' = normalise ctxt env val
720 let valty' = normalise ctxt env valty
721 addLog (showEnv env val ++ " : " ++
723-- " in " ++ show env ++
725 showEnv env val' ++ " : " ++
729start_unify :: Name -> RunTactic
730start_unify n ctxt env tm = do -- action (\ps -> ps { unified = (n, []) })
733tmap f (a, b, c) = (f a, b, c)
735solve_unified :: RunTactic
736solve_unified ctxt env tm =
738 let (_, ns) = unified ps
739 let unify = dropGiven (dontunify ps) ns (holes ps)
740 action (\ps -> ps { holes = traceWhen (unifylog ps) ("Dropping holes " ++ show (map fst unify)) $
741 holes ps \\ map fst unify })
742 action (\ps -> ps { pterm = updateSolved unify (pterm ps) })
743 return (updateSolvedTerm unify tm)
745dropGiven du [] hs = []
746dropGiven du ((n, P Bound t ty) : us) hs
747 | n `elem` du && not (t `elem` du)
748 && n `elem` hs && t `elem` hs
749 = (t, P Bound n ty) : dropGiven du us hs
750dropGiven du (u@(n, _) : us) hs
751 | n `elem` du = dropGiven du us hs
752-- dropGiven du (u@(_, P a n ty) : us) | n `elem` du = dropGiven du us
753dropGiven du (u : us) hs = u : dropGiven du us hs
755keepGiven du [] hs = []
756keepGiven du ((n, P Bound t ty) : us) hs
757 | n `elem` du && not (t `elem` du)
758 && n `elem` hs && t `elem` hs
760keepGiven du (u@(n, _) : us) hs
761 | n `elem` du = u : keepGiven du us hs
762keepGiven du (u : us) hs = keepGiven du us hs
766updateEnv ns ((n, b) : env) = (n, fmap (updateSolvedTerm ns) b) : updateEnv ns env
768updateError [] err = err
769updateError ns (At f e) = At f (updateError ns e)
770updateError ns (Elaborating s n e) = Elaborating s n (updateError ns e)
771updateError ns (ElaboratingArg f a env e)
772 = ElaboratingArg f a env (updateError ns e)
773updateError ns (CantUnify b l r e xs sc)
774 = CantUnify b (updateSolvedTerm ns l) (updateSolvedTerm ns r) (updateError ns e) xs sc
777solveInProblems x val [] = []
778solveInProblems x val ((l, r, env, err) : ps)
779 = ((psubst x val l, psubst x val r,
780 updateEnv [(x, val)] env, err) : solveInProblems x val ps)
782mergeNotunified :: Env -> [(Name, Term)] -> ([(Name, Term)], Fails)
783mergeNotunified env ns = mnu ns [] [] where
784 mnu [] ns_acc ps_acc = (reverse ns_acc, reverse ps_acc)
785 mnu ((n, t):ns) ns_acc ps_acc
786 | Just t' <- lookup n ns, t /= t'
787 = mnu ns ((n,t') : ns_acc)
788 ((t,t',env,CantUnify True t t' (Msg "") [] 0, [],Match) : ps_acc)
789 | otherwise = mnu ns ((n,t) : ns_acc) ps_acc
791updateNotunified [] nu = nu
792updateNotunified ns nu = up nu where
794 up ((n, t) : nus) = let t' = updateSolvedTerm ns t in
797-- FIXME: Why not just pass the whole proof state?
798updateProblems :: Context -> [(Name, TT Name)] -> Fails -> [Name] -> [Name] -> [Name]
799 -> ([(Name, TT Name)], Fails)
800-- updateProblems ctxt [] ps inj holes = ([], ps)
801updateProblems ctxt ns ps inj holes usupp = up ns ps where
803 up ns ((x, y, env, err, while, um) : ps) =
804 let x' = updateSolvedTerm ns x
805 y' = updateSolvedTerm ns y
806 err' = updateError ns err
807 env' = updateEnv ns env in
808-- trace ("Updating " ++ show (x',y')) $
809 case unify ctxt env' x' y' inj holes usupp while of
810 OK (v, []) -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
812 e -> -- trace ("Failed " ++ show e) $
813 let (ns', ps') = up ns ps in
814 (ns', (x',y',env',err', while, um) : ps')
816-- attempt to solve remaining problems with match_unify
817matchProblems all ns ctxt ps inj holes = up ns ps where
819 up ns ((x, y, env, err, while, um) : ps)
820 | all || um == Match =
821 let x' = updateSolvedTerm ns x
822 y' = updateSolvedTerm ns y
823 err' = updateError ns err
824 env' = updateEnv ns env in
825 case match_unify ctxt env' x' y' inj holes while of
826 OK v -> -- trace ("Added " ++ show v ++ " from " ++ show (x', y')) $
828 _ -> let (ns', ps') = up ns ps in
829 (ns', (x', y', env', err', while, um) : ps')
830 up ns (p : ps) = let (ns', ps') = up ns ps in
833processTactic :: Tactic -> ProofState -> TC (ProofState, String)
834processTactic QED ps = case holes ps of
835 [] -> do let tm = {- normalise (context ps) [] -} getProofTerm (pterm ps)
836 (tm', ty', _) <- recheck (context ps) [] (forget tm) tm
837 return (ps { done = True, pterm = mkProofTerm tm' },
838 "Proof complete: " ++ showEnv [] tm')
839 _ -> fail "Still holes to fill."
840processTactic ProofState ps = return (ps, showEnv [] (getProofTerm (pterm ps)))
841processTactic Undo ps = case previous ps of
842 Nothing -> Error . Msg $ "Nothing to undo."
843 Just pold -> return (pold, "")
844processTactic EndUnify ps
845 = let (h, ns_in) = unified ps
846 ns = dropGiven (dontunify ps) ns_in (holes ps)
847 ns' = map (\ (n, t) -> (n, updateSolvedTerm ns t)) ns
848 (ns'', probs') = updateProblems (context ps) ns' (problems ps)
849 (injective ps) (holes ps)
850 (map fst (notunified ps))
851 tm' = updateSolved ns'' (pterm ps) in
852 traceWhen (unifylog ps) ("Dropping holes: " ++ show (map fst ns'')) $
853 return (ps { pterm = tm',
856 notunified = updateNotunified ns'' (notunified ps),
857 holes = holes ps \\ map fst ns'' }, "")
858processTactic UnifyAll ps
859 = let tm' = updateSolved (notunified ps) (pterm ps) in
860 return (ps { pterm = tm',
862 holes = holes ps \\ map fst (notunified ps) }, "")
863processTactic (Reorder n) ps
864 = do ps' <- execStateT (tactic (Just n) reorder_claims) ps
865 return (ps' { previous = Just ps, plog = "" }, plog ps')
866processTactic (ComputeLet n) ps
867 = return (ps { pterm = mkProofTerm $
868 computeLet (context ps) n
869 (getProofTerm (pterm ps)) }, "")
870processTactic UnifyProblems ps
871 = let (ns', probs') = updateProblems (context ps) []
875 (map fst (notunified ps))
876 pterm' = updateSolved ns' (pterm ps) in
877 traceWhen (unifylog ps) ("Dropping holes: " ++ show (map fst ns')) $
878 return (ps { pterm = pterm', solved = Nothing, problems = probs',
879 previous = Just ps, plog = "",
880 notunified = updateNotunified ns' (notunified ps),
881 holes = holes ps \\ (map fst ns') }, plog ps)
882processTactic (MatchProblems all) ps
883 = let (ns', probs') = matchProblems all [] (context ps)
887 (ns'', probs'') = matchProblems all ns' (context ps)
891 pterm' = updateSolved ns'' (pterm ps) in
892 traceWhen (unifylog ps) ("Dropping holes: " ++ show (map fst ns'')) $
893 return (ps { pterm = pterm', solved = Nothing, problems = probs'',
894 previous = Just ps, plog = "",
895 notunified = updateNotunified ns'' (notunified ps),
896 holes = holes ps \\ (map fst ns'') }, plog ps)
899 [] -> fail "Nothing to fill in."
900 (h:_) -> do ps' <- execStateT (process t h) ps
903 Just s -> traceWhen (unifylog ps')
904 ("SOLVED " ++ show s) $
905 updateProblems (context ps')
909 (map fst (notunified ps))
910 _ -> ([], problems ps')
911 -- rechecking problems may find more solutions, so
913 let pterm'' = updateSolved ns' (pterm ps')
914 traceWhen (unifylog ps) ("Dropping holes: " ++ show (map fst ns')) $
915 return (ps' { pterm = pterm'',
918 notunified = updateNotunified ns' (notunified ps'),
919 previous = Just ps, plog = "",
920 holes = holes ps' \\ (map fst ns')}, plog ps')
922process :: Tactic -> Name -> StateT TState TC ()
925 let (h, _) = unified ps
926 tactic (Just h) solve_unified
927process t h = tactic (Just h) (mktac t)
928 where mktac Attack = attack
929 mktac (Claim n r) = claim n r
930 mktac (Exact r) = exact r
931 mktac (Fill r) = fill r
932 mktac (MatchFill r) = match_fill r
933 mktac (PrepFill n ns) = prep_fill n ns
934 mktac CompleteFill = complete_fill
935 mktac Regret = regret
937 mktac (StartUnify n) = start_unify n
938 mktac Compute = compute
939 mktac Simplify = Idris.Core.ProofState.simplify
940 mktac HNF_Compute = hnf_compute
941 mktac (Intro n) = intro n
942 mktac (IntroTy ty n) = introTy ty n
943 mktac (Forall n t) = forall n t
944 mktac (LetBind n t v) = letbind n t v
945 mktac (ExpandLet n b) = expandLet n b
946 mktac (Rewrite t) = rewrite t
947 mktac (Induction t) = casetac t True
948 mktac (CaseTac t) = casetac t False
949 mktac (Equiv t) = equiv t
950 mktac (PatVar n) = patvar n
951 mktac (PatBind n) = patbind n
952 mktac (CheckIn r) = check_in r
953 mktac (EvalIn r) = eval_in r
954 mktac (Focus n) = focus n
955 mktac (Defer n) = defer n
956 mktac (DeferType n t a) = deferType n t a
957 mktac (Instance n) = instanceArg n
958 mktac (SetInjective n) = setinj n
959 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