src/HOL/Tools/Function/induction_schema.ML
author wenzelm
Sat Dec 14 17:28:05 2013 +0100 (2013-12-14)
changeset 54742 7a86358a3c0b
parent 52467 24c6ddb48cb8
child 55642 63beb38e9258
permissions -rw-r--r--
proper context for basic Simplifier operations: rewrite_rule, rewrite_goals_rule, rewrite_goals_tac etc.;
clarified tool context in some boundary cases;
krauss@33471
     1
(*  Title:      HOL/Tools/Function/induction_schema.ML
krauss@33471
     2
    Author:     Alexander Krauss, TU Muenchen
krauss@33471
     3
krauss@33471
     4
A method to prove induction schemas.
krauss@33471
     5
*)
krauss@33471
     6
krauss@33471
     7
signature INDUCTION_SCHEMA =
krauss@33471
     8
sig
krauss@33471
     9
  val mk_ind_tac : (int -> tactic) -> (int -> tactic) -> (int -> tactic)
krauss@33471
    10
                   -> Proof.context -> thm list -> tactic
krauss@33471
    11
  val induction_schema_tac : Proof.context -> thm list -> tactic
krauss@33471
    12
end
krauss@33471
    13
krauss@33471
    14
krauss@33471
    15
structure Induction_Schema : INDUCTION_SCHEMA =
krauss@33471
    16
struct
krauss@33471
    17
krauss@33471
    18
open Function_Lib
krauss@33471
    19
krauss@33471
    20
type rec_call_info = int * (string * typ) list * term list * term list
krauss@33471
    21
krauss@34232
    22
datatype scheme_case = SchemeCase of
krauss@34232
    23
 {bidx : int,
krauss@34232
    24
  qs: (string * typ) list,
krauss@34232
    25
  oqnames: string list,
krauss@34232
    26
  gs: term list,
krauss@34232
    27
  lhs: term list,
krauss@34232
    28
  rs: rec_call_info list}
krauss@33471
    29
krauss@34232
    30
datatype scheme_branch = SchemeBranch of
krauss@34232
    31
 {P : term,
krauss@34232
    32
  xs: (string * typ) list,
krauss@34232
    33
  ws: (string * typ) list,
krauss@34232
    34
  Cs: term list}
krauss@33471
    35
krauss@34232
    36
datatype ind_scheme = IndScheme of
krauss@34232
    37
 {T: typ, (* sum of products *)
krauss@34232
    38
  branches: scheme_branch list,
krauss@34232
    39
  cases: scheme_case list}
krauss@33471
    40
wenzelm@54742
    41
fun ind_atomize ctxt = Raw_Simplifier.rewrite ctxt true @{thms induct_atomize}
wenzelm@54742
    42
fun ind_rulify ctxt = Raw_Simplifier.rewrite ctxt true @{thms induct_rulify}
krauss@33471
    43
krauss@33471
    44
fun meta thm = thm RS eq_reflection
krauss@33471
    45
wenzelm@54742
    46
fun sum_prod_conv ctxt = Raw_Simplifier.rewrite ctxt true
krauss@34232
    47
  (map meta (@{thm split_conv} :: @{thms sum.cases}))
krauss@33471
    48
krauss@34232
    49
fun term_conv thy cv t =
krauss@34232
    50
  cv (cterm_of thy t)
krauss@34232
    51
  |> prop_of |> Logic.dest_equals |> snd
krauss@33471
    52
krauss@33471
    53
fun mk_relT T = HOLogic.mk_setT (HOLogic.mk_prodT (T, T))
krauss@33471
    54
krauss@34232
    55
fun dest_hhf ctxt t =
krauss@34232
    56
  let
wenzelm@42495
    57
    val ((params, imp), ctxt') = Variable.focus t ctxt
krauss@34232
    58
  in
wenzelm@42495
    59
    (ctxt', map #2 params, Logic.strip_imp_prems imp, Logic.strip_imp_concl imp)
krauss@34232
    60
  end
krauss@33471
    61
krauss@33471
    62
fun mk_scheme' ctxt cases concl =
krauss@34232
    63
  let
krauss@34232
    64
    fun mk_branch concl =
krauss@34232
    65
      let
krauss@34232
    66
        val (_, ws, Cs, _ $ Pxs) = dest_hhf ctxt concl
krauss@34232
    67
        val (P, xs) = strip_comb Pxs
krauss@34232
    68
      in
krauss@34232
    69
        SchemeBranch { P=P, xs=map dest_Free xs, ws=ws, Cs=Cs }
krauss@34232
    70
      end
krauss@34232
    71
krauss@34232
    72
    val (branches, cases') = (* correction *)
krauss@41418
    73
      case Logic.dest_conjunctions concl of
krauss@34232
    74
        [conc] =>
krauss@34232
    75
        let
krauss@34232
    76
          val _ $ Pxs = Logic.strip_assums_concl conc
krauss@34232
    77
          val (P, _) = strip_comb Pxs
krauss@34232
    78
          val (cases', conds) =
krauss@34232
    79
            take_prefix (Term.exists_subterm (curry op aconv P)) cases
krauss@34232
    80
          val concl' = fold_rev (curry Logic.mk_implies) conds conc
krauss@34232
    81
        in
krauss@34232
    82
          ([mk_branch concl'], cases')
krauss@34232
    83
        end
krauss@34232
    84
      | concls => (map mk_branch concls, cases)
krauss@34232
    85
krauss@34232
    86
    fun mk_case premise =
krauss@34232
    87
      let
krauss@34232
    88
        val (ctxt', qs, prems, _ $ Plhs) = dest_hhf ctxt premise
krauss@34232
    89
        val (P, lhs) = strip_comb Plhs
krauss@34232
    90
krauss@34232
    91
        fun bidx Q =
krauss@34232
    92
          find_index (fn SchemeBranch {P=P',...} => Q aconv P') branches
krauss@34232
    93
krauss@34232
    94
        fun mk_rcinfo pr =
krauss@33471
    95
          let
krauss@34232
    96
            val (_, Gvs, Gas, _ $ Phyp) = dest_hhf ctxt' pr
krauss@34232
    97
            val (P', rcs) = strip_comb Phyp
krauss@33471
    98
          in
krauss@34232
    99
            (bidx P', Gvs, Gas, rcs)
krauss@33471
   100
          end
krauss@33471
   101
krauss@34232
   102
        fun is_pred v = exists (fn SchemeBranch {P,...} => v aconv P) branches
krauss@33471
   103
krauss@34232
   104
        val (gs, rcprs) =
krauss@34232
   105
          take_prefix (not o Term.exists_subterm is_pred) prems
krauss@34232
   106
      in
krauss@34232
   107
        SchemeCase {bidx=bidx P, qs=qs, oqnames=map fst qs(*FIXME*),
krauss@34232
   108
          gs=gs, lhs=lhs, rs=map mk_rcinfo rcprs}
krauss@34232
   109
      end
krauss@33471
   110
krauss@34232
   111
    fun PT_of (SchemeBranch { xs, ...}) =
krauss@34232
   112
      foldr1 HOLogic.mk_prodT (map snd xs)
krauss@33471
   113
krauss@34232
   114
    val ST = Balanced_Tree.make (uncurry SumTree.mk_sumT) (map PT_of branches)
krauss@34232
   115
  in
krauss@34232
   116
    IndScheme {T=ST, cases=map mk_case cases', branches=branches }
krauss@34232
   117
  end
krauss@33471
   118
krauss@33471
   119
fun mk_completeness ctxt (IndScheme {cases, branches, ...}) bidx =
krauss@34232
   120
  let
krauss@34232
   121
    val SchemeBranch { xs, ws, Cs, ... } = nth branches bidx
krauss@34232
   122
    val relevant_cases = filter (fn SchemeCase {bidx=bidx', ...} => bidx' = bidx) cases
krauss@34232
   123
krauss@34232
   124
    val allqnames = fold (fn SchemeCase {qs, ...} => fold (insert (op =) o Free) qs) relevant_cases []
krauss@34232
   125
    val (Pbool :: xs') = map Free (Variable.variant_frees ctxt allqnames (("P", HOLogic.boolT) :: xs))
wenzelm@42361
   126
    val Cs' = map (Pattern.rewrite_term (Proof_Context.theory_of ctxt) (filter_out (op aconv) (map Free xs ~~ xs')) []) Cs
krauss@33471
   127
krauss@34232
   128
    fun mk_case (SchemeCase {qs, oqnames, gs, lhs, ...}) =
krauss@33471
   129
      HOLogic.mk_Trueprop Pbool
krauss@34232
   130
      |> fold_rev (fn x_l => curry Logic.mk_implies (HOLogic.mk_Trueprop(HOLogic.mk_eq x_l)))
krauss@34232
   131
           (xs' ~~ lhs)
krauss@34232
   132
      |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   133
      |> fold_rev mk_forall_rename (oqnames ~~ map Free qs)
krauss@34232
   134
  in
krauss@34232
   135
    HOLogic.mk_Trueprop Pbool
krauss@34232
   136
    |> fold_rev (curry Logic.mk_implies o mk_case) relevant_cases
krauss@34232
   137
    |> fold_rev (curry Logic.mk_implies) Cs'
krauss@34232
   138
    |> fold_rev (Logic.all o Free) ws
krauss@34232
   139
    |> fold_rev mk_forall_rename (map fst xs ~~ xs')
krauss@34232
   140
    |> mk_forall_rename ("P", Pbool)
krauss@34232
   141
  end
krauss@33471
   142
krauss@33855
   143
fun mk_wf R (IndScheme {T, ...}) =
krauss@34232
   144
  HOLogic.Trueprop $ (Const (@{const_name wf}, mk_relT T --> HOLogic.boolT) $ R)
krauss@33471
   145
krauss@33471
   146
fun mk_ineqs R (IndScheme {T, cases, branches}) =
krauss@34232
   147
  let
krauss@34232
   148
    fun inject i ts =
krauss@34232
   149
       SumTree.mk_inj T (length branches) (i + 1) (foldr1 HOLogic.mk_prod ts)
krauss@33471
   150
krauss@34232
   151
    val thesis = Free ("thesis", HOLogic.boolT) (* FIXME *)
krauss@33471
   152
krauss@34232
   153
    fun mk_pres bdx args =
krauss@34232
   154
      let
krauss@34232
   155
        val SchemeBranch { xs, ws, Cs, ... } = nth branches bdx
krauss@34232
   156
        fun replace (x, v) t = betapply (lambda (Free x) t, v)
krauss@34232
   157
        val Cs' = map (fold replace (xs ~~ args)) Cs
krauss@34232
   158
        val cse =
krauss@34232
   159
          HOLogic.mk_Trueprop thesis
krauss@34232
   160
          |> fold_rev (curry Logic.mk_implies) Cs'
krauss@34232
   161
          |> fold_rev (Logic.all o Free) ws
krauss@34232
   162
      in
krauss@34232
   163
        Logic.mk_implies (cse, HOLogic.mk_Trueprop thesis)
krauss@34232
   164
      end
krauss@33471
   165
krauss@34232
   166
    fun f (SchemeCase {bidx, qs, oqnames, gs, lhs, rs, ...}) =
krauss@34232
   167
      let
krauss@34232
   168
        fun g (bidx', Gvs, Gas, rcarg) =
krauss@34232
   169
          let val export =
krauss@34232
   170
            fold_rev (curry Logic.mk_implies) Gas
krauss@34232
   171
            #> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   172
            #> fold_rev (Logic.all o Free) Gvs
krauss@34232
   173
            #> fold_rev mk_forall_rename (oqnames ~~ map Free qs)
krauss@33471
   174
          in
krauss@34232
   175
            (HOLogic.mk_mem (HOLogic.mk_prod (inject bidx' rcarg, inject bidx lhs), R)
krauss@34232
   176
             |> HOLogic.mk_Trueprop
krauss@34232
   177
             |> export,
krauss@34232
   178
             mk_pres bidx' rcarg
krauss@34232
   179
             |> export
krauss@34232
   180
             |> Logic.all thesis)
krauss@33471
   181
          end
krauss@34232
   182
      in
krauss@34232
   183
        map g rs
krauss@34232
   184
      end
krauss@34232
   185
  in
krauss@34232
   186
    map f cases
krauss@34232
   187
  end
krauss@33471
   188
krauss@33471
   189
wenzelm@54742
   190
fun mk_ind_goal ctxt branches =
krauss@34232
   191
  let
wenzelm@54742
   192
    val thy = Proof_Context.theory_of ctxt
wenzelm@54742
   193
krauss@34232
   194
    fun brnch (SchemeBranch { P, xs, ws, Cs, ... }) =
krauss@34232
   195
      HOLogic.mk_Trueprop (list_comb (P, map Free xs))
krauss@34232
   196
      |> fold_rev (curry Logic.mk_implies) Cs
krauss@34232
   197
      |> fold_rev (Logic.all o Free) ws
wenzelm@54742
   198
      |> term_conv thy (ind_atomize ctxt)
wenzelm@35625
   199
      |> Object_Logic.drop_judgment thy
krauss@39756
   200
      |> HOLogic.tupled_lambda (foldr1 HOLogic.mk_prod (map Free xs))
krauss@34232
   201
  in
krauss@34232
   202
    SumTree.mk_sumcases HOLogic.boolT (map brnch branches)
krauss@34232
   203
  end
krauss@34232
   204
krauss@34232
   205
fun mk_induct_rule ctxt R x complete_thms wf_thm ineqss
krauss@34232
   206
  (IndScheme {T, cases=scases, branches}) =
krauss@34232
   207
  let
wenzelm@54742
   208
    val thy = Proof_Context.theory_of ctxt
wenzelm@54742
   209
    val cert = cterm_of thy
wenzelm@54742
   210
krauss@34232
   211
    val n = length branches
krauss@34232
   212
    val scases_idx = map_index I scases
krauss@34232
   213
krauss@34232
   214
    fun inject i ts =
krauss@34232
   215
      SumTree.mk_inj T n (i + 1) (foldr1 HOLogic.mk_prod ts)
krauss@34232
   216
    val P_of = nth (map (fn (SchemeBranch { P, ... }) => P) branches)
krauss@34232
   217
wenzelm@54742
   218
    val P_comp = mk_ind_goal ctxt branches
krauss@34232
   219
krauss@34232
   220
    (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
wenzelm@46217
   221
    val ihyp = Logic.all_const T $ Abs ("z", T,
krauss@34232
   222
      Logic.mk_implies
krauss@34232
   223
        (HOLogic.mk_Trueprop (
haftmann@37677
   224
          Const (@{const_name Set.member}, HOLogic.mk_prodT (T, T) --> mk_relT T --> HOLogic.boolT) 
krauss@34232
   225
          $ (HOLogic.pair_const T T $ Bound 0 $ x)
krauss@34232
   226
          $ R),
krauss@34232
   227
         HOLogic.mk_Trueprop (P_comp $ Bound 0)))
krauss@34232
   228
      |> cert
krauss@34232
   229
wenzelm@36945
   230
    val aihyp = Thm.assume ihyp
krauss@34232
   231
krauss@34232
   232
    (* Rule for case splitting along the sum types *)
krauss@34232
   233
    val xss = map (fn (SchemeBranch { xs, ... }) => map Free xs) branches
krauss@34232
   234
    val pats = map_index (uncurry inject) xss
krauss@34232
   235
    val sum_split_rule =
wenzelm@51717
   236
      Pat_Completeness.prove_completeness ctxt [x] (P_comp $ x) xss (map single pats)
krauss@34232
   237
krauss@34232
   238
    fun prove_branch (bidx, (SchemeBranch { P, xs, ws, Cs, ... }, (complete_thm, pat))) =
krauss@34232
   239
      let
krauss@34232
   240
        val fxs = map Free xs
wenzelm@36945
   241
        val branch_hyp = Thm.assume (cert (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, pat))))
krauss@34232
   242
wenzelm@36945
   243
        val C_hyps = map (cert #> Thm.assume) Cs
krauss@34232
   244
krauss@34232
   245
        val (relevant_cases, ineqss') =
krauss@34232
   246
          (scases_idx ~~ ineqss)
krauss@34232
   247
          |> filter (fn ((_, SchemeCase {bidx=bidx', ...}), _) => bidx' = bidx)
krauss@34232
   248
          |> split_list
krauss@34232
   249
krauss@34232
   250
        fun prove_case (cidx, SchemeCase {qs, gs, lhs, rs, ...}) ineq_press =
krauss@34232
   251
          let
wenzelm@36945
   252
            val case_hyps =
wenzelm@36945
   253
              map (Thm.assume o cert o HOLogic.mk_Trueprop o HOLogic.mk_eq) (fxs ~~ lhs)
krauss@34232
   254
krauss@34232
   255
            val cqs = map (cert o Free) qs
wenzelm@36945
   256
            val ags = map (Thm.assume o cert) gs
krauss@34232
   257
wenzelm@51717
   258
            val replace_x_simpset =
wenzelm@51717
   259
              put_simpset HOL_basic_ss ctxt addsimps (branch_hyp :: case_hyps)
wenzelm@51717
   260
            val sih = full_simplify replace_x_simpset aihyp
krauss@34232
   261
krauss@34232
   262
            fun mk_Prec (idx, Gvs, Gas, rcargs) (ineq, pres) =
krauss@34232
   263
              let
wenzelm@36945
   264
                val cGas = map (Thm.assume o cert) Gas
krauss@34232
   265
                val cGvs = map (cert o Free) Gvs
wenzelm@36945
   266
                val import = fold Thm.forall_elim (cqs @ cGvs)
krauss@34232
   267
                  #> fold Thm.elim_implies (ags @ cGas)
krauss@34232
   268
                val ipres = pres
wenzelm@36945
   269
                  |> Thm.forall_elim (cert (list_comb (P_of idx, rcargs)))
krauss@34232
   270
                  |> import
krauss@34232
   271
              in
krauss@34232
   272
                sih
wenzelm@36945
   273
                |> Thm.forall_elim (cert (inject idx rcargs))
krauss@34232
   274
                |> Thm.elim_implies (import ineq) (* Psum rcargs *)
wenzelm@54742
   275
                |> Conv.fconv_rule (sum_prod_conv ctxt)
wenzelm@54742
   276
                |> Conv.fconv_rule (ind_rulify ctxt)
krauss@34232
   277
                |> (fn th => th COMP ipres) (* P rs *)
wenzelm@36945
   278
                |> fold_rev (Thm.implies_intr o cprop_of) cGas
wenzelm@36945
   279
                |> fold_rev Thm.forall_intr cGvs
krauss@34232
   280
              end
krauss@34232
   281
krauss@34232
   282
            val P_recs = map2 mk_Prec rs ineq_press   (*  [P rec1, P rec2, ... ]  *)
krauss@34232
   283
krauss@34232
   284
            val step = HOLogic.mk_Trueprop (list_comb (P, lhs))
krauss@34232
   285
              |> fold_rev (curry Logic.mk_implies o prop_of) P_recs
krauss@34232
   286
              |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   287
              |> fold_rev (Logic.all o Free) qs
krauss@34232
   288
              |> cert
krauss@34232
   289
krauss@34232
   290
            val Plhs_to_Pxs_conv =
krauss@34232
   291
              foldl1 (uncurry Conv.combination_conv)
krauss@34232
   292
                (Conv.all_conv :: map (fn ch => K (Thm.symmetric (ch RS eq_reflection))) case_hyps)
krauss@34232
   293
wenzelm@36945
   294
            val res = Thm.assume step
wenzelm@36945
   295
              |> fold Thm.forall_elim cqs
krauss@34232
   296
              |> fold Thm.elim_implies ags
krauss@34232
   297
              |> fold Thm.elim_implies P_recs (* P lhs *)
krauss@34232
   298
              |> Conv.fconv_rule (Conv.arg_conv Plhs_to_Pxs_conv) (* P xs *)
wenzelm@36945
   299
              |> fold_rev (Thm.implies_intr o cprop_of) (ags @ case_hyps)
wenzelm@36945
   300
              |> fold_rev Thm.forall_intr cqs (* !!qs. Gas ==> xs = lhss ==> P xs *)
krauss@34232
   301
          in
krauss@34232
   302
            (res, (cidx, step))
krauss@34232
   303
          end
krauss@34232
   304
krauss@34232
   305
        val (cases, steps) = split_list (map2 prove_case relevant_cases ineqss')
krauss@34232
   306
krauss@34232
   307
        val bstep = complete_thm
wenzelm@36945
   308
          |> Thm.forall_elim (cert (list_comb (P, fxs)))
wenzelm@36945
   309
          |> fold (Thm.forall_elim o cert) (fxs @ map Free ws)
krauss@34232
   310
          |> fold Thm.elim_implies C_hyps
krauss@34232
   311
          |> fold Thm.elim_implies cases (* P xs *)
wenzelm@36945
   312
          |> fold_rev (Thm.implies_intr o cprop_of) C_hyps
wenzelm@36945
   313
          |> fold_rev (Thm.forall_intr o cert o Free) ws
krauss@34232
   314
krauss@34232
   315
        val Pxs = cert (HOLogic.mk_Trueprop (P_comp $ x))
krauss@34232
   316
          |> Goal.init
wenzelm@54742
   317
          |> (Simplifier.rewrite_goals_tac ctxt
wenzelm@54742
   318
                (map meta (branch_hyp :: @{thm split_conv} :: @{thms sum.cases}))
wenzelm@54742
   319
              THEN CONVERSION (ind_rulify ctxt) 1)
krauss@34232
   320
          |> Seq.hd
krauss@34232
   321
          |> Thm.elim_implies (Conv.fconv_rule Drule.beta_eta_conversion bstep)
krauss@34232
   322
          |> Goal.finish ctxt
wenzelm@36945
   323
          |> Thm.implies_intr (cprop_of branch_hyp)
wenzelm@36945
   324
          |> fold_rev (Thm.forall_intr o cert) fxs
krauss@34232
   325
      in
krauss@34232
   326
        (Pxs, steps)
krauss@34232
   327
      end
krauss@34232
   328
krauss@34232
   329
    val (branches, steps) =
krauss@34232
   330
      map_index prove_branch (branches ~~ (complete_thms ~~ pats))
krauss@34232
   331
      |> split_list |> apsnd flat
krauss@34232
   332
krauss@34232
   333
    val istep = sum_split_rule
wenzelm@52467
   334
      |> fold (fn b => fn th => Drule.compose (b, 1, th)) branches
wenzelm@36945
   335
      |> Thm.implies_intr ihyp
wenzelm@36945
   336
      |> Thm.forall_intr (cert x) (* "!!x. (!!y<x. P y) ==> P x" *)
krauss@34232
   337
krauss@34232
   338
    val induct_rule =
krauss@34232
   339
      @{thm "wf_induct_rule"}
krauss@34232
   340
      |> (curry op COMP) wf_thm
krauss@34232
   341
      |> (curry op COMP) istep
krauss@34232
   342
krauss@34232
   343
    val steps_sorted = map snd (sort (int_ord o pairself fst) steps)
krauss@34232
   344
  in
krauss@34232
   345
    (steps_sorted, induct_rule)
krauss@34232
   346
  end
krauss@33471
   347
krauss@33471
   348
krauss@34232
   349
fun mk_ind_tac comp_tac pres_tac term_tac ctxt facts =
wenzelm@46467
   350
  (* FIXME proper use of facts!? *)
krauss@34232
   351
  (ALLGOALS (Method.insert_tac facts)) THEN HEADGOAL (SUBGOAL (fn (t, i) =>
krauss@33471
   352
  let
krauss@33471
   353
    val (ctxt', _, cases, concl) = dest_hhf ctxt t
krauss@33471
   354
    val scheme as IndScheme {T=ST, branches, ...} = mk_scheme' ctxt' cases concl
krauss@33471
   355
    val ([Rn,xn], ctxt'') = Variable.variant_fixes ["R","x"] ctxt'
krauss@33471
   356
    val R = Free (Rn, mk_relT ST)
krauss@33471
   357
    val x = Free (xn, ST)
wenzelm@42361
   358
    val cert = cterm_of (Proof_Context.theory_of ctxt)
krauss@33471
   359
krauss@33471
   360
    val ineqss = mk_ineqs R scheme
wenzelm@36945
   361
      |> map (map (pairself (Thm.assume o cert)))
krauss@34232
   362
    val complete =
wenzelm@36945
   363
      map_range (mk_completeness ctxt scheme #> cert #> Thm.assume) (length branches)
wenzelm@36945
   364
    val wf_thm = mk_wf R scheme |> cert |> Thm.assume
krauss@33471
   365
krauss@33471
   366
    val (descent, pres) = split_list (flat ineqss)
krauss@34232
   367
    val newgoals = complete @ pres @ wf_thm :: descent
krauss@33471
   368
krauss@34232
   369
    val (steps, indthm) =
krauss@34232
   370
      mk_induct_rule ctxt'' R x complete wf_thm ineqss scheme
krauss@33471
   371
krauss@33471
   372
    fun project (i, SchemeBranch {xs, ...}) =
krauss@34232
   373
      let
krauss@34232
   374
        val inst = (foldr1 HOLogic.mk_prod (map Free xs))
krauss@34232
   375
          |> SumTree.mk_inj ST (length branches) (i + 1)
krauss@34232
   376
          |> cert
krauss@34232
   377
      in
krauss@34232
   378
        indthm
krauss@34232
   379
        |> Drule.instantiate' [] [SOME inst]
wenzelm@51717
   380
        |> simplify (put_simpset SumTree.sumcase_split_ss ctxt'')
wenzelm@54742
   381
        |> Conv.fconv_rule (ind_rulify ctxt'')
krauss@34232
   382
      end
krauss@33471
   383
krauss@33471
   384
    val res = Conjunction.intr_balanced (map_index project branches)
wenzelm@36945
   385
      |> fold_rev Thm.implies_intr (map cprop_of newgoals @ steps)
krauss@34232
   386
      |> Drule.generalize ([], [Rn])
krauss@33471
   387
krauss@33471
   388
    val nbranches = length branches
krauss@33471
   389
    val npres = length pres
krauss@33471
   390
  in
wenzelm@52223
   391
    Thm.bicompose {flatten = false, match = false, incremented = false}
wenzelm@52223
   392
      (false, res, length newgoals) i
krauss@33471
   393
    THEN term_tac (i + nbranches + npres)
krauss@33471
   394
    THEN (EVERY (map (TRY o pres_tac) ((i + nbranches + npres - 1) downto (i + nbranches))))
krauss@33471
   395
    THEN (EVERY (map (TRY o comp_tac) ((i + nbranches - 1) downto i)))
krauss@33471
   396
  end))
krauss@33471
   397
krauss@33471
   398
krauss@33471
   399
fun induction_schema_tac ctxt =
krauss@33471
   400
  mk_ind_tac (K all_tac) (assume_tac APPEND' Goal.assume_rule_tac ctxt) (K all_tac) ctxt;
krauss@33471
   401
krauss@33471
   402
end