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