@@ -69,6 +69,7 @@ data Tactic = Attack
69 | LetBind Name Raw Raw
69 | LetBind Name Raw Raw
70 | ExpandLet Name Term
70 | ExpandLet Name Term
71 | Rewrite Raw
71 | Rewrite Raw
72 | Equiv Raw
73 | Equiv Raw
73 | PatVar Name
74 | PatVar Name
74 | PatBind Name
75 | PatBind Name
@@ -566,28 +567,63 @@ rewrite tm ctxt env (Bind x (Hole t) xp@(P _ x' _)) | x == x' =
566 (scv, sct) <- lift $ check ctxt env sc
567 (scv, sct) <- lift $ check ctxt env sc
567 return scv
568 return scv
568 _ -> fail "Not an equality type"
569 _ -> fail "Not an equality type"
570− -- to make the P for rewrite, replace syntactic occurrences of l in ty with
571− -- an x, and put \x : lt in front
572− mkP lt l r ty | l == ty = lt
573− mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
574− a' = if (r /= a) then mkP lt l r a else a in
576− mkP lt l r (Bind n b sc)
578− sc' = if (r /= sc) then mkP lt l r sc else sc in
580− where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
581− v' = if (r /= v) then mkP lt l r v else v in
583− mkPB b = let ty = binderTy b
584− ty' = if (r /= ty) then mkP lt l r ty else ty in
585− b { binderTy = ty' }
588− rname = MN 0 "replaced"
570+ where rname = MN 0 "replaced"
589rewrite _ _ _ _ = fail "Can't rewrite here"
571rewrite _ _ _ _ = fail "Can't rewrite here"
573+-- To make the P for rewrite, replace syntactic occurrences of l in ty with
574+-- an x, and put \x : lt in front
575+mkP :: TT Name -> TT Name -> TT Name -> TT Name -> TT Name
576+mkP lt l r ty | l == ty = lt
577+mkP lt l r (App f a) = let f' = if (r /= f) then mkP lt l r f else f
578+ a' = if (r /= a) then mkP lt l r a else a in
580+mkP lt l r (Bind n b sc)
582+ sc' = if (r /= sc) then mkP lt l r sc else sc in
584+ where mkPB (Let t v) = let t' = if (r /= t) then mkP lt l r t else t
585+ v' = if (r /= v) then mkP lt l r v else v in
587+ mkPB b = let ty = binderTy b
588+ ty' = if (r /= ty) then mkP lt l r ty else ty in
589+ b { binderTy = ty' }
592+induction :: Name -> RunTactic
593+induction nm ctxt env (Bind x (Hole t) (P _ x' _)) | x == x' = do
594+ (tmv, tmt) <- lift $ check ctxt env (Var nm)
595+ let tmt' = normalise ctxt env tmt
596+ case unApply tmt' of
597+ (P _ tnm _, pms@[]) -> do
598+ case lookupTy (SN (ElimN tnm)) ctxt of
600+ let args = getArgTys elimTy
601+ let pmargs = take (length pms) args -- TODO: Fill these correctly when having multiple parameters working
602+ let args' = drop (length pms) args
603+ let propTy = head args'
604+ let consargs = init $ tail args -- TODO: This is incorrect when type has indicies, get indicies and use the correct count
605+ let scr = last $ tail args
606+ let prop = Bind nm (Lam tmt') t -- TODO: Make this work for types with indicies
607+ let res = substV prop $ bindConsArgs consargs (mkApp (P Ref (SN (ElimN tnm)) (TType (UVal 0))) ([prop] ++ map makeConsArg consargs ++ [tmv]))
608+ action (\ps -> ps {holes = holes ps \\ [x]})
609+ mapM_ addConsHole (reverse consargs)
610+ let res' = forget $ res
611+ (scv, sct) <- lift $ check ctxt env res'
612+ let scv' = specialise ctxt env [] scv
614+ [] -> fail $ "Induction needs an eliminator for " ++ show nm
615+ xs -> fail $ "Multiple definitions found when searching for the eliminator of " ++ show nm
616+ (P _ nm _, _) -> fail "Induction not yet supported for types with arguments"
617+ _ -> fail "Unkown type for induction"
618+ where scname = MN 0 "scarg"
619+ makeConsArg (nm, ty) = P Bound nm ty
620+ bindConsArgs ((nm, ty):args) v = Bind nm (Hole ty) $ bindConsArgs args v
621+ bindConsArgs [] v = v
622+ addConsHole (nm, ty) =
623+ action (\ps -> ps { holes = nm : holes ps })
624+induction tm ctxt env _ = do fail "Can't do induction here"
591equiv :: Raw -> RunTactic
627equiv :: Raw -> RunTactic
592equiv tm ctxt env (Bind x (Hole t) sc) =
628equiv tm ctxt env (Bind x (Hole t) sc) =
593 do (tmv, tmt) <- lift $ check ctxt env tm
629 do (tmv, tmt) <- lift $ check ctxt env tm
@@ -819,6 +855,7 @@ process t h = tactic (Just h) (mktac t)
819 mktac (LetBind n t v) = letbind n t v
855 mktac (LetBind n t v) = letbind n t v
820 mktac (ExpandLet n b) = expandLet n b
856 mktac (ExpandLet n b) = expandLet n b
821 mktac (Rewrite t) = rewrite t
857 mktac (Rewrite t) = rewrite t
858+ mktac (Induction t) = induction t
822 mktac (Equiv t) = equiv t
859 mktac (Equiv t) = equiv t
823 mktac (PatVar n) = patvar n
860 mktac (PatVar n) = patvar n
824 mktac (PatBind n) = patbind n
861 mktac (PatBind n) = patbind n