src/HOL/Tools/Function/induction_schema.ML
author wenzelm
Thu Apr 18 17:07:01 2013 +0200 (2013-04-18)
changeset 51717 9e7d1c139569
parent 47432 e1576d13e933
child 52223 5bb6ae8acb87
permissions -rw-r--r--
simplifier uses proper Proof.context instead of historic type simpset;
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@41228
    41
val ind_atomize = Raw_Simplifier.rewrite true @{thms induct_atomize}
wenzelm@41228
    42
val ind_rulify = Raw_Simplifier.rewrite true @{thms induct_rulify}
krauss@33471
    43
krauss@33471
    44
fun meta thm = thm RS eq_reflection
krauss@33471
    45
wenzelm@41228
    46
val sum_prod_conv = Raw_Simplifier.rewrite 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
krauss@33471
   190
fun mk_ind_goal thy branches =
krauss@34232
   191
  let
krauss@34232
   192
    fun brnch (SchemeBranch { P, xs, ws, Cs, ... }) =
krauss@34232
   193
      HOLogic.mk_Trueprop (list_comb (P, map Free xs))
krauss@34232
   194
      |> fold_rev (curry Logic.mk_implies) Cs
krauss@34232
   195
      |> fold_rev (Logic.all o Free) ws
krauss@34232
   196
      |> term_conv thy ind_atomize
wenzelm@35625
   197
      |> Object_Logic.drop_judgment thy
krauss@39756
   198
      |> HOLogic.tupled_lambda (foldr1 HOLogic.mk_prod (map Free xs))
krauss@34232
   199
  in
krauss@34232
   200
    SumTree.mk_sumcases HOLogic.boolT (map brnch branches)
krauss@34232
   201
  end
krauss@34232
   202
krauss@34232
   203
fun mk_induct_rule ctxt R x complete_thms wf_thm ineqss
krauss@34232
   204
  (IndScheme {T, cases=scases, branches}) =
krauss@34232
   205
  let
krauss@34232
   206
    val n = length branches
krauss@34232
   207
    val scases_idx = map_index I scases
krauss@34232
   208
krauss@34232
   209
    fun inject i ts =
krauss@34232
   210
      SumTree.mk_inj T n (i + 1) (foldr1 HOLogic.mk_prod ts)
krauss@34232
   211
    val P_of = nth (map (fn (SchemeBranch { P, ... }) => P) branches)
krauss@34232
   212
wenzelm@42361
   213
    val thy = Proof_Context.theory_of ctxt
krauss@34232
   214
    val cert = cterm_of thy
krauss@34232
   215
krauss@34232
   216
    val P_comp = mk_ind_goal thy branches
krauss@34232
   217
krauss@34232
   218
    (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
wenzelm@46217
   219
    val ihyp = Logic.all_const T $ Abs ("z", T,
krauss@34232
   220
      Logic.mk_implies
krauss@34232
   221
        (HOLogic.mk_Trueprop (
haftmann@37677
   222
          Const (@{const_name Set.member}, HOLogic.mk_prodT (T, T) --> mk_relT T --> HOLogic.boolT) 
krauss@34232
   223
          $ (HOLogic.pair_const T T $ Bound 0 $ x)
krauss@34232
   224
          $ R),
krauss@34232
   225
         HOLogic.mk_Trueprop (P_comp $ Bound 0)))
krauss@34232
   226
      |> cert
krauss@34232
   227
wenzelm@36945
   228
    val aihyp = Thm.assume ihyp
krauss@34232
   229
krauss@34232
   230
    (* Rule for case splitting along the sum types *)
krauss@34232
   231
    val xss = map (fn (SchemeBranch { xs, ... }) => map Free xs) branches
krauss@34232
   232
    val pats = map_index (uncurry inject) xss
krauss@34232
   233
    val sum_split_rule =
wenzelm@51717
   234
      Pat_Completeness.prove_completeness ctxt [x] (P_comp $ x) xss (map single pats)
krauss@34232
   235
krauss@34232
   236
    fun prove_branch (bidx, (SchemeBranch { P, xs, ws, Cs, ... }, (complete_thm, pat))) =
krauss@34232
   237
      let
krauss@34232
   238
        val fxs = map Free xs
wenzelm@36945
   239
        val branch_hyp = Thm.assume (cert (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, pat))))
krauss@34232
   240
wenzelm@36945
   241
        val C_hyps = map (cert #> Thm.assume) Cs
krauss@34232
   242
krauss@34232
   243
        val (relevant_cases, ineqss') =
krauss@34232
   244
          (scases_idx ~~ ineqss)
krauss@34232
   245
          |> filter (fn ((_, SchemeCase {bidx=bidx', ...}), _) => bidx' = bidx)
krauss@34232
   246
          |> split_list
krauss@34232
   247
krauss@34232
   248
        fun prove_case (cidx, SchemeCase {qs, gs, lhs, rs, ...}) ineq_press =
krauss@34232
   249
          let
wenzelm@36945
   250
            val case_hyps =
wenzelm@36945
   251
              map (Thm.assume o cert o HOLogic.mk_Trueprop o HOLogic.mk_eq) (fxs ~~ lhs)
krauss@34232
   252
krauss@34232
   253
            val cqs = map (cert o Free) qs
wenzelm@36945
   254
            val ags = map (Thm.assume o cert) gs
krauss@34232
   255
wenzelm@51717
   256
            val replace_x_simpset =
wenzelm@51717
   257
              put_simpset HOL_basic_ss ctxt addsimps (branch_hyp :: case_hyps)
wenzelm@51717
   258
            val sih = full_simplify replace_x_simpset aihyp
krauss@34232
   259
krauss@34232
   260
            fun mk_Prec (idx, Gvs, Gas, rcargs) (ineq, pres) =
krauss@34232
   261
              let
wenzelm@36945
   262
                val cGas = map (Thm.assume o cert) Gas
krauss@34232
   263
                val cGvs = map (cert o Free) Gvs
wenzelm@36945
   264
                val import = fold Thm.forall_elim (cqs @ cGvs)
krauss@34232
   265
                  #> fold Thm.elim_implies (ags @ cGas)
krauss@34232
   266
                val ipres = pres
wenzelm@36945
   267
                  |> Thm.forall_elim (cert (list_comb (P_of idx, rcargs)))
krauss@34232
   268
                  |> import
krauss@34232
   269
              in
krauss@34232
   270
                sih
wenzelm@36945
   271
                |> Thm.forall_elim (cert (inject idx rcargs))
krauss@34232
   272
                |> Thm.elim_implies (import ineq) (* Psum rcargs *)
krauss@34232
   273
                |> Conv.fconv_rule sum_prod_conv
krauss@34232
   274
                |> Conv.fconv_rule ind_rulify
krauss@34232
   275
                |> (fn th => th COMP ipres) (* P rs *)
wenzelm@36945
   276
                |> fold_rev (Thm.implies_intr o cprop_of) cGas
wenzelm@36945
   277
                |> fold_rev Thm.forall_intr cGvs
krauss@34232
   278
              end
krauss@34232
   279
krauss@34232
   280
            val P_recs = map2 mk_Prec rs ineq_press   (*  [P rec1, P rec2, ... ]  *)
krauss@34232
   281
krauss@34232
   282
            val step = HOLogic.mk_Trueprop (list_comb (P, lhs))
krauss@34232
   283
              |> fold_rev (curry Logic.mk_implies o prop_of) P_recs
krauss@34232
   284
              |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   285
              |> fold_rev (Logic.all o Free) qs
krauss@34232
   286
              |> cert
krauss@34232
   287
krauss@34232
   288
            val Plhs_to_Pxs_conv =
krauss@34232
   289
              foldl1 (uncurry Conv.combination_conv)
krauss@34232
   290
                (Conv.all_conv :: map (fn ch => K (Thm.symmetric (ch RS eq_reflection))) case_hyps)
krauss@34232
   291
wenzelm@36945
   292
            val res = Thm.assume step
wenzelm@36945
   293
              |> fold Thm.forall_elim cqs
krauss@34232
   294
              |> fold Thm.elim_implies ags
krauss@34232
   295
              |> fold Thm.elim_implies P_recs (* P lhs *)
krauss@34232
   296
              |> Conv.fconv_rule (Conv.arg_conv Plhs_to_Pxs_conv) (* P xs *)
wenzelm@36945
   297
              |> fold_rev (Thm.implies_intr o cprop_of) (ags @ case_hyps)
wenzelm@36945
   298
              |> fold_rev Thm.forall_intr cqs (* !!qs. Gas ==> xs = lhss ==> P xs *)
krauss@34232
   299
          in
krauss@34232
   300
            (res, (cidx, step))
krauss@34232
   301
          end
krauss@34232
   302
krauss@34232
   303
        val (cases, steps) = split_list (map2 prove_case relevant_cases ineqss')
krauss@34232
   304
krauss@34232
   305
        val bstep = complete_thm
wenzelm@36945
   306
          |> Thm.forall_elim (cert (list_comb (P, fxs)))
wenzelm@36945
   307
          |> fold (Thm.forall_elim o cert) (fxs @ map Free ws)
krauss@34232
   308
          |> fold Thm.elim_implies C_hyps
krauss@34232
   309
          |> fold Thm.elim_implies cases (* P xs *)
wenzelm@36945
   310
          |> fold_rev (Thm.implies_intr o cprop_of) C_hyps
wenzelm@36945
   311
          |> fold_rev (Thm.forall_intr o cert o Free) ws
krauss@34232
   312
krauss@34232
   313
        val Pxs = cert (HOLogic.mk_Trueprop (P_comp $ x))
krauss@34232
   314
          |> Goal.init
wenzelm@41225
   315
          |> (Simplifier.rewrite_goals_tac (map meta (branch_hyp :: @{thm split_conv} :: @{thms sum.cases}))
krauss@34232
   316
              THEN CONVERSION ind_rulify 1)
krauss@34232
   317
          |> Seq.hd
krauss@34232
   318
          |> Thm.elim_implies (Conv.fconv_rule Drule.beta_eta_conversion bstep)
krauss@34232
   319
          |> Goal.finish ctxt
wenzelm@36945
   320
          |> Thm.implies_intr (cprop_of branch_hyp)
wenzelm@36945
   321
          |> fold_rev (Thm.forall_intr o cert) fxs
krauss@34232
   322
      in
krauss@34232
   323
        (Pxs, steps)
krauss@34232
   324
      end
krauss@34232
   325
krauss@34232
   326
    val (branches, steps) =
krauss@34232
   327
      map_index prove_branch (branches ~~ (complete_thms ~~ pats))
krauss@34232
   328
      |> split_list |> apsnd flat
krauss@34232
   329
krauss@34232
   330
    val istep = sum_split_rule
krauss@34232
   331
      |> fold (fn b => fn th => Drule.compose_single (b, 1, th)) branches
wenzelm@36945
   332
      |> Thm.implies_intr ihyp
wenzelm@36945
   333
      |> Thm.forall_intr (cert x) (* "!!x. (!!y<x. P y) ==> P x" *)
krauss@34232
   334
krauss@34232
   335
    val induct_rule =
krauss@34232
   336
      @{thm "wf_induct_rule"}
krauss@34232
   337
      |> (curry op COMP) wf_thm
krauss@34232
   338
      |> (curry op COMP) istep
krauss@34232
   339
krauss@34232
   340
    val steps_sorted = map snd (sort (int_ord o pairself fst) steps)
krauss@34232
   341
  in
krauss@34232
   342
    (steps_sorted, induct_rule)
krauss@34232
   343
  end
krauss@33471
   344
krauss@33471
   345
krauss@34232
   346
fun mk_ind_tac comp_tac pres_tac term_tac ctxt facts =
wenzelm@46467
   347
  (* FIXME proper use of facts!? *)
krauss@34232
   348
  (ALLGOALS (Method.insert_tac facts)) THEN HEADGOAL (SUBGOAL (fn (t, i) =>
krauss@33471
   349
  let
krauss@33471
   350
    val (ctxt', _, cases, concl) = dest_hhf ctxt t
krauss@33471
   351
    val scheme as IndScheme {T=ST, branches, ...} = mk_scheme' ctxt' cases concl
krauss@33471
   352
    val ([Rn,xn], ctxt'') = Variable.variant_fixes ["R","x"] ctxt'
krauss@33471
   353
    val R = Free (Rn, mk_relT ST)
krauss@33471
   354
    val x = Free (xn, ST)
wenzelm@42361
   355
    val cert = cterm_of (Proof_Context.theory_of ctxt)
krauss@33471
   356
krauss@33471
   357
    val ineqss = mk_ineqs R scheme
wenzelm@36945
   358
      |> map (map (pairself (Thm.assume o cert)))
krauss@34232
   359
    val complete =
wenzelm@36945
   360
      map_range (mk_completeness ctxt scheme #> cert #> Thm.assume) (length branches)
wenzelm@36945
   361
    val wf_thm = mk_wf R scheme |> cert |> Thm.assume
krauss@33471
   362
krauss@33471
   363
    val (descent, pres) = split_list (flat ineqss)
krauss@34232
   364
    val newgoals = complete @ pres @ wf_thm :: descent
krauss@33471
   365
krauss@34232
   366
    val (steps, indthm) =
krauss@34232
   367
      mk_induct_rule ctxt'' R x complete wf_thm ineqss scheme
krauss@33471
   368
krauss@33471
   369
    fun project (i, SchemeBranch {xs, ...}) =
krauss@34232
   370
      let
krauss@34232
   371
        val inst = (foldr1 HOLogic.mk_prod (map Free xs))
krauss@34232
   372
          |> SumTree.mk_inj ST (length branches) (i + 1)
krauss@34232
   373
          |> cert
krauss@34232
   374
      in
krauss@34232
   375
        indthm
krauss@34232
   376
        |> Drule.instantiate' [] [SOME inst]
wenzelm@51717
   377
        |> simplify (put_simpset SumTree.sumcase_split_ss ctxt'')
krauss@34232
   378
        |> Conv.fconv_rule ind_rulify
krauss@34232
   379
      end
krauss@33471
   380
krauss@33471
   381
    val res = Conjunction.intr_balanced (map_index project branches)
wenzelm@36945
   382
      |> fold_rev Thm.implies_intr (map cprop_of newgoals @ steps)
krauss@34232
   383
      |> Drule.generalize ([], [Rn])
krauss@33471
   384
krauss@33471
   385
    val nbranches = length branches
krauss@33471
   386
    val npres = length pres
krauss@33471
   387
  in
krauss@33471
   388
    Thm.compose_no_flatten false (res, length newgoals) i
krauss@33471
   389
    THEN term_tac (i + nbranches + npres)
krauss@33471
   390
    THEN (EVERY (map (TRY o pres_tac) ((i + nbranches + npres - 1) downto (i + nbranches))))
krauss@33471
   391
    THEN (EVERY (map (TRY o comp_tac) ((i + nbranches - 1) downto i)))
krauss@33471
   392
  end))
krauss@33471
   393
krauss@33471
   394
krauss@33471
   395
fun induction_schema_tac ctxt =
krauss@33471
   396
  mk_ind_tac (K all_tac) (assume_tac APPEND' Goal.assume_rule_tac ctxt) (K all_tac) ctxt;
krauss@33471
   397
krauss@33471
   398
end