src/HOL/Tools/Function/induction_schema.ML
author wenzelm
Fri Mar 06 13:39:34 2015 +0100 (2015-03-06)
changeset 59618 e6939796717e
parent 59582 0fbed69ff081
child 59621 291934bac95e
permissions -rw-r--r--
clarified context;
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 =
wenzelm@59582
    49
  cv (Thm.cterm_of thy t)
wenzelm@59582
    50
  |> Thm.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
krauss@34232
   207
    val n = length branches
krauss@34232
   208
    val scases_idx = map_index I scases
krauss@34232
   209
krauss@34232
   210
    fun inject i ts =
blanchet@55968
   211
      Sum_Tree.mk_inj T n (i + 1) (foldr1 HOLogic.mk_prod ts)
krauss@34232
   212
    val P_of = nth (map (fn (SchemeBranch { P, ... }) => P) branches)
krauss@34232
   213
wenzelm@54742
   214
    val P_comp = mk_ind_goal ctxt branches
krauss@34232
   215
krauss@34232
   216
    (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
wenzelm@46217
   217
    val ihyp = Logic.all_const T $ Abs ("z", T,
krauss@34232
   218
      Logic.mk_implies
krauss@34232
   219
        (HOLogic.mk_Trueprop (
haftmann@37677
   220
          Const (@{const_name Set.member}, HOLogic.mk_prodT (T, T) --> mk_relT T --> HOLogic.boolT) 
krauss@34232
   221
          $ (HOLogic.pair_const T T $ Bound 0 $ x)
krauss@34232
   222
          $ R),
krauss@34232
   223
         HOLogic.mk_Trueprop (P_comp $ Bound 0)))
wenzelm@59618
   224
      |> Proof_Context.cterm_of ctxt
krauss@34232
   225
wenzelm@36945
   226
    val aihyp = Thm.assume ihyp
krauss@34232
   227
krauss@34232
   228
    (* Rule for case splitting along the sum types *)
krauss@34232
   229
    val xss = map (fn (SchemeBranch { xs, ... }) => map Free xs) branches
krauss@34232
   230
    val pats = map_index (uncurry inject) xss
krauss@34232
   231
    val sum_split_rule =
wenzelm@51717
   232
      Pat_Completeness.prove_completeness ctxt [x] (P_comp $ x) xss (map single pats)
krauss@34232
   233
krauss@34232
   234
    fun prove_branch (bidx, (SchemeBranch { P, xs, ws, Cs, ... }, (complete_thm, pat))) =
krauss@34232
   235
      let
krauss@34232
   236
        val fxs = map Free xs
wenzelm@59618
   237
        val branch_hyp =
wenzelm@59618
   238
          Thm.assume (Proof_Context.cterm_of ctxt (HOLogic.mk_Trueprop (HOLogic.mk_eq (x, pat))))
krauss@34232
   239
wenzelm@59618
   240
        val C_hyps = map (Proof_Context.cterm_of ctxt #> Thm.assume) Cs
krauss@34232
   241
krauss@34232
   242
        val (relevant_cases, ineqss') =
krauss@34232
   243
          (scases_idx ~~ ineqss)
krauss@34232
   244
          |> filter (fn ((_, SchemeCase {bidx=bidx', ...}), _) => bidx' = bidx)
krauss@34232
   245
          |> split_list
krauss@34232
   246
krauss@34232
   247
        fun prove_case (cidx, SchemeCase {qs, gs, lhs, rs, ...}) ineq_press =
krauss@34232
   248
          let
wenzelm@36945
   249
            val case_hyps =
wenzelm@59618
   250
              map (Thm.assume o Proof_Context.cterm_of ctxt o HOLogic.mk_Trueprop o HOLogic.mk_eq)
wenzelm@59618
   251
                (fxs ~~ lhs)
krauss@34232
   252
wenzelm@59618
   253
            val cqs = map (Proof_Context.cterm_of ctxt o Free) qs
wenzelm@59618
   254
            val ags = map (Thm.assume o Proof_Context.cterm_of ctxt) 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@59618
   262
                val cGas = map (Thm.assume o Proof_Context.cterm_of ctxt) Gas
wenzelm@59618
   263
                val cGvs = map (Proof_Context.cterm_of ctxt 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@59618
   267
                  |> Thm.forall_elim (Proof_Context.cterm_of ctxt (list_comb (P_of idx, rcargs)))
krauss@34232
   268
                  |> import
krauss@34232
   269
              in
krauss@34232
   270
                sih
wenzelm@59618
   271
                |> Thm.forall_elim (Proof_Context.cterm_of ctxt (inject idx rcargs))
krauss@34232
   272
                |> Thm.elim_implies (import ineq) (* Psum rcargs *)
wenzelm@54742
   273
                |> Conv.fconv_rule (sum_prod_conv ctxt)
wenzelm@54742
   274
                |> Conv.fconv_rule (ind_rulify ctxt)
krauss@34232
   275
                |> (fn th => th COMP ipres) (* P rs *)
wenzelm@59582
   276
                |> fold_rev (Thm.implies_intr o Thm.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))
wenzelm@59582
   283
              |> fold_rev (curry Logic.mk_implies o Thm.prop_of) P_recs
krauss@34232
   284
              |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   285
              |> fold_rev (Logic.all o Free) qs
wenzelm@59618
   286
              |> Proof_Context.cterm_of ctxt
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@59582
   297
              |> fold_rev (Thm.implies_intr o Thm.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@59618
   306
          |> Thm.forall_elim (Proof_Context.cterm_of ctxt (list_comb (P, fxs)))
wenzelm@59618
   307
          |> fold (Thm.forall_elim o Proof_Context.cterm_of ctxt) (fxs @ map Free ws)
krauss@34232
   308
          |> fold Thm.elim_implies C_hyps
krauss@34232
   309
          |> fold Thm.elim_implies cases (* P xs *)
wenzelm@59582
   310
          |> fold_rev (Thm.implies_intr o Thm.cprop_of) C_hyps
wenzelm@59618
   311
          |> fold_rev (Thm.forall_intr o Proof_Context.cterm_of ctxt o Free) ws
krauss@34232
   312
wenzelm@59618
   313
        val Pxs =
wenzelm@59618
   314
          Proof_Context.cterm_of ctxt (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@59582
   322
          |> Thm.implies_intr (Thm.cprop_of branch_hyp)
wenzelm@59618
   323
          |> fold_rev (Thm.forall_intr o Proof_Context.cterm_of ctxt) 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@59618
   335
      |> Thm.forall_intr (Proof_Context.cterm_of ctxt 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
wenzelm@59058
   342
    val steps_sorted = map snd (sort (int_ord o apply2 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
wenzelm@59618
   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)
krauss@33471
   357
wenzelm@59618
   358
    val ineqss =
wenzelm@59618
   359
      mk_ineqs R scheme
wenzelm@59618
   360
      |> map (map (apply2 (Thm.assume o Proof_Context.cterm_of ctxt'')))
krauss@34232
   361
    val complete =
wenzelm@59618
   362
      map_range (mk_completeness ctxt'' scheme #> Proof_Context.cterm_of ctxt'' #> Thm.assume)
wenzelm@59618
   363
        (length branches)
wenzelm@59618
   364
    val wf_thm = mk_wf R scheme |> Proof_Context.cterm_of ctxt'' |> 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))
blanchet@55968
   375
          |> Sum_Tree.mk_inj ST (length branches) (i + 1)
wenzelm@59618
   376
          |> Proof_Context.cterm_of ctxt''
krauss@34232
   377
      in
krauss@34232
   378
        indthm
krauss@34232
   379
        |> Drule.instantiate' [] [SOME inst]
blanchet@55968
   380
        |> simplify (put_simpset Sum_Tree.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@59582
   385
      |> fold_rev Thm.implies_intr (map Thm.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@59618
   391
    Thm.bicompose (SOME ctxt'') {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 =
wenzelm@58963
   400
  mk_ind_tac (K all_tac) (assume_tac ctxt APPEND' Goal.assume_rule_tac ctxt) (K all_tac) ctxt;
krauss@33471
   401
krauss@33471
   402
end