src/HOL/Tools/Function/induction_schema.ML
author wenzelm
Sat May 15 21:50:05 2010 +0200 (2010-05-15)
changeset 36945 9bec62c10714
parent 35625 9c818cab0dd0
child 37677 c5a8b612e571
permissions -rw-r--r--
less pervasive names from structure Thm;
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
  val setup : theory -> theory
krauss@33471
    13
end
krauss@33471
    14
krauss@33471
    15
krauss@33471
    16
structure Induction_Schema : INDUCTION_SCHEMA =
krauss@33471
    17
struct
krauss@33471
    18
krauss@33471
    19
open Function_Lib
krauss@33471
    20
krauss@33471
    21
type rec_call_info = int * (string * typ) list * term list * term list
krauss@33471
    22
krauss@34232
    23
datatype scheme_case = SchemeCase of
krauss@34232
    24
 {bidx : int,
krauss@34232
    25
  qs: (string * typ) list,
krauss@34232
    26
  oqnames: string list,
krauss@34232
    27
  gs: term list,
krauss@34232
    28
  lhs: term list,
krauss@34232
    29
  rs: rec_call_info list}
krauss@33471
    30
krauss@34232
    31
datatype scheme_branch = SchemeBranch of
krauss@34232
    32
 {P : term,
krauss@34232
    33
  xs: (string * typ) list,
krauss@34232
    34
  ws: (string * typ) list,
krauss@34232
    35
  Cs: term list}
krauss@33471
    36
krauss@34232
    37
datatype ind_scheme = IndScheme of
krauss@34232
    38
 {T: typ, (* sum of products *)
krauss@34232
    39
  branches: scheme_branch list,
krauss@34232
    40
  cases: scheme_case list}
krauss@33471
    41
krauss@33471
    42
val ind_atomize = MetaSimplifier.rewrite true @{thms induct_atomize}
krauss@33471
    43
val ind_rulify = MetaSimplifier.rewrite true @{thms induct_rulify}
krauss@33471
    44
krauss@33471
    45
fun meta thm = thm RS eq_reflection
krauss@33471
    46
krauss@34232
    47
val sum_prod_conv = MetaSimplifier.rewrite true
krauss@34232
    48
  (map meta (@{thm split_conv} :: @{thms sum.cases}))
krauss@33471
    49
krauss@34232
    50
fun term_conv thy cv t =
krauss@34232
    51
  cv (cterm_of thy t)
krauss@34232
    52
  |> prop_of |> Logic.dest_equals |> snd
krauss@33471
    53
krauss@33471
    54
fun mk_relT T = HOLogic.mk_setT (HOLogic.mk_prodT (T, T))
krauss@33471
    55
krauss@34232
    56
fun dest_hhf ctxt t =
krauss@34232
    57
  let
krauss@34232
    58
    val (ctxt', vars, imp) = dest_all_all_ctx ctxt t
krauss@34232
    59
  in
krauss@34232
    60
    (ctxt', vars, Logic.strip_imp_prems imp, Logic.strip_imp_concl imp)
krauss@34232
    61
  end
krauss@33471
    62
krauss@33471
    63
fun mk_scheme' ctxt cases concl =
krauss@34232
    64
  let
krauss@34232
    65
    fun mk_branch concl =
krauss@34232
    66
      let
krauss@34232
    67
        val (_, ws, Cs, _ $ Pxs) = dest_hhf ctxt concl
krauss@34232
    68
        val (P, xs) = strip_comb Pxs
krauss@34232
    69
      in
krauss@34232
    70
        SchemeBranch { P=P, xs=map dest_Free xs, ws=ws, Cs=Cs }
krauss@34232
    71
      end
krauss@34232
    72
krauss@34232
    73
    val (branches, cases') = (* correction *)
krauss@34232
    74
      case Logic.dest_conjunction_list concl of
krauss@34232
    75
        [conc] =>
krauss@34232
    76
        let
krauss@34232
    77
          val _ $ Pxs = Logic.strip_assums_concl conc
krauss@34232
    78
          val (P, _) = strip_comb Pxs
krauss@34232
    79
          val (cases', conds) =
krauss@34232
    80
            take_prefix (Term.exists_subterm (curry op aconv P)) cases
krauss@34232
    81
          val concl' = fold_rev (curry Logic.mk_implies) conds conc
krauss@34232
    82
        in
krauss@34232
    83
          ([mk_branch concl'], cases')
krauss@34232
    84
        end
krauss@34232
    85
      | concls => (map mk_branch concls, cases)
krauss@34232
    86
krauss@34232
    87
    fun mk_case premise =
krauss@34232
    88
      let
krauss@34232
    89
        val (ctxt', qs, prems, _ $ Plhs) = dest_hhf ctxt premise
krauss@34232
    90
        val (P, lhs) = strip_comb Plhs
krauss@34232
    91
krauss@34232
    92
        fun bidx Q =
krauss@34232
    93
          find_index (fn SchemeBranch {P=P',...} => Q aconv P') branches
krauss@34232
    94
krauss@34232
    95
        fun mk_rcinfo pr =
krauss@33471
    96
          let
krauss@34232
    97
            val (_, Gvs, Gas, _ $ Phyp) = dest_hhf ctxt' pr
krauss@34232
    98
            val (P', rcs) = strip_comb Phyp
krauss@33471
    99
          in
krauss@34232
   100
            (bidx P', Gvs, Gas, rcs)
krauss@33471
   101
          end
krauss@33471
   102
krauss@34232
   103
        fun is_pred v = exists (fn SchemeBranch {P,...} => v aconv P) branches
krauss@33471
   104
krauss@34232
   105
        val (gs, rcprs) =
krauss@34232
   106
          take_prefix (not o Term.exists_subterm is_pred) prems
krauss@34232
   107
      in
krauss@34232
   108
        SchemeCase {bidx=bidx P, qs=qs, oqnames=map fst qs(*FIXME*),
krauss@34232
   109
          gs=gs, lhs=lhs, rs=map mk_rcinfo rcprs}
krauss@34232
   110
      end
krauss@33471
   111
krauss@34232
   112
    fun PT_of (SchemeBranch { xs, ...}) =
krauss@34232
   113
      foldr1 HOLogic.mk_prodT (map snd xs)
krauss@33471
   114
krauss@34232
   115
    val ST = Balanced_Tree.make (uncurry SumTree.mk_sumT) (map PT_of branches)
krauss@34232
   116
  in
krauss@34232
   117
    IndScheme {T=ST, cases=map mk_case cases', branches=branches }
krauss@34232
   118
  end
krauss@33471
   119
krauss@33471
   120
fun mk_completeness ctxt (IndScheme {cases, branches, ...}) bidx =
krauss@34232
   121
  let
krauss@34232
   122
    val SchemeBranch { xs, ws, Cs, ... } = nth branches bidx
krauss@34232
   123
    val relevant_cases = filter (fn SchemeCase {bidx=bidx', ...} => bidx' = bidx) cases
krauss@34232
   124
krauss@34232
   125
    val allqnames = fold (fn SchemeCase {qs, ...} => fold (insert (op =) o Free) qs) relevant_cases []
krauss@34232
   126
    val (Pbool :: xs') = map Free (Variable.variant_frees ctxt allqnames (("P", HOLogic.boolT) :: xs))
krauss@34232
   127
    val Cs' = map (Pattern.rewrite_term (ProofContext.theory_of ctxt) (filter_out (op aconv) (map Free xs ~~ xs')) []) Cs
krauss@33471
   128
krauss@34232
   129
    fun mk_case (SchemeCase {qs, oqnames, gs, lhs, ...}) =
krauss@33471
   130
      HOLogic.mk_Trueprop Pbool
krauss@34232
   131
      |> fold_rev (fn x_l => curry Logic.mk_implies (HOLogic.mk_Trueprop(HOLogic.mk_eq x_l)))
krauss@34232
   132
           (xs' ~~ lhs)
krauss@34232
   133
      |> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   134
      |> fold_rev mk_forall_rename (oqnames ~~ map Free qs)
krauss@34232
   135
  in
krauss@34232
   136
    HOLogic.mk_Trueprop Pbool
krauss@34232
   137
    |> fold_rev (curry Logic.mk_implies o mk_case) relevant_cases
krauss@34232
   138
    |> fold_rev (curry Logic.mk_implies) Cs'
krauss@34232
   139
    |> fold_rev (Logic.all o Free) ws
krauss@34232
   140
    |> fold_rev mk_forall_rename (map fst xs ~~ xs')
krauss@34232
   141
    |> mk_forall_rename ("P", Pbool)
krauss@34232
   142
  end
krauss@33471
   143
krauss@33855
   144
fun mk_wf R (IndScheme {T, ...}) =
krauss@34232
   145
  HOLogic.Trueprop $ (Const (@{const_name wf}, mk_relT T --> HOLogic.boolT) $ R)
krauss@33471
   146
krauss@33471
   147
fun mk_ineqs R (IndScheme {T, cases, branches}) =
krauss@34232
   148
  let
krauss@34232
   149
    fun inject i ts =
krauss@34232
   150
       SumTree.mk_inj T (length branches) (i + 1) (foldr1 HOLogic.mk_prod ts)
krauss@33471
   151
krauss@34232
   152
    val thesis = Free ("thesis", HOLogic.boolT) (* FIXME *)
krauss@33471
   153
krauss@34232
   154
    fun mk_pres bdx args =
krauss@34232
   155
      let
krauss@34232
   156
        val SchemeBranch { xs, ws, Cs, ... } = nth branches bdx
krauss@34232
   157
        fun replace (x, v) t = betapply (lambda (Free x) t, v)
krauss@34232
   158
        val Cs' = map (fold replace (xs ~~ args)) Cs
krauss@34232
   159
        val cse =
krauss@34232
   160
          HOLogic.mk_Trueprop thesis
krauss@34232
   161
          |> fold_rev (curry Logic.mk_implies) Cs'
krauss@34232
   162
          |> fold_rev (Logic.all o Free) ws
krauss@34232
   163
      in
krauss@34232
   164
        Logic.mk_implies (cse, HOLogic.mk_Trueprop thesis)
krauss@34232
   165
      end
krauss@33471
   166
krauss@34232
   167
    fun f (SchemeCase {bidx, qs, oqnames, gs, lhs, rs, ...}) =
krauss@34232
   168
      let
krauss@34232
   169
        fun g (bidx', Gvs, Gas, rcarg) =
krauss@34232
   170
          let val export =
krauss@34232
   171
            fold_rev (curry Logic.mk_implies) Gas
krauss@34232
   172
            #> fold_rev (curry Logic.mk_implies) gs
krauss@34232
   173
            #> fold_rev (Logic.all o Free) Gvs
krauss@34232
   174
            #> fold_rev mk_forall_rename (oqnames ~~ map Free qs)
krauss@33471
   175
          in
krauss@34232
   176
            (HOLogic.mk_mem (HOLogic.mk_prod (inject bidx' rcarg, inject bidx lhs), R)
krauss@34232
   177
             |> HOLogic.mk_Trueprop
krauss@34232
   178
             |> export,
krauss@34232
   179
             mk_pres bidx' rcarg
krauss@34232
   180
             |> export
krauss@34232
   181
             |> Logic.all thesis)
krauss@33471
   182
          end
krauss@34232
   183
      in
krauss@34232
   184
        map g rs
krauss@34232
   185
      end
krauss@34232
   186
  in
krauss@34232
   187
    map f cases
krauss@34232
   188
  end
krauss@33471
   189
krauss@33471
   190
krauss@33471
   191
fun mk_ind_goal thy branches =
krauss@34232
   192
  let
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
krauss@34232
   197
      |> term_conv thy ind_atomize
wenzelm@35625
   198
      |> Object_Logic.drop_judgment thy
krauss@34232
   199
      |> tupled_lambda (foldr1 HOLogic.mk_prod (map Free xs))
krauss@34232
   200
  in
krauss@34232
   201
    SumTree.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 =
krauss@34232
   211
      SumTree.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
krauss@34232
   214
    val thy = ProofContext.theory_of ctxt
krauss@34232
   215
    val cert = cterm_of thy
krauss@34232
   216
krauss@34232
   217
    val P_comp = mk_ind_goal thy branches
krauss@34232
   218
krauss@34232
   219
    (* Inductive Hypothesis: !!z. (z,x):R ==> P z *)
krauss@34232
   220
    val ihyp = Term.all T $ Abs ("z", T,
krauss@34232
   221
      Logic.mk_implies
krauss@34232
   222
        (HOLogic.mk_Trueprop (
krauss@34232
   223
          Const ("op :", 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 =
krauss@34232
   235
      Pat_Completeness.prove_completeness thy [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
krauss@34232
   257
            val replace_x_ss = HOL_basic_ss addsimps (branch_hyp :: case_hyps)
krauss@34232
   258
            val sih = full_simplify replace_x_ss 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
krauss@34232
   315
          |> (MetaSimplifier.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 =
krauss@34232
   347
  (ALLGOALS (Method.insert_tac facts)) THEN HEADGOAL (SUBGOAL (fn (t, i) =>
krauss@33471
   348
  let
krauss@33471
   349
    val (ctxt', _, cases, concl) = dest_hhf ctxt t
krauss@33471
   350
    val scheme as IndScheme {T=ST, branches, ...} = mk_scheme' ctxt' cases concl
krauss@33471
   351
    val ([Rn,xn], ctxt'') = Variable.variant_fixes ["R","x"] ctxt'
krauss@33471
   352
    val R = Free (Rn, mk_relT ST)
krauss@33471
   353
    val x = Free (xn, ST)
krauss@33471
   354
    val cert = cterm_of (ProofContext.theory_of ctxt)
krauss@33471
   355
krauss@33471
   356
    val ineqss = mk_ineqs R scheme
wenzelm@36945
   357
      |> map (map (pairself (Thm.assume o cert)))
krauss@34232
   358
    val complete =
wenzelm@36945
   359
      map_range (mk_completeness ctxt scheme #> cert #> Thm.assume) (length branches)
wenzelm@36945
   360
    val wf_thm = mk_wf R scheme |> cert |> Thm.assume
krauss@33471
   361
krauss@33471
   362
    val (descent, pres) = split_list (flat ineqss)
krauss@34232
   363
    val newgoals = complete @ pres @ wf_thm :: descent
krauss@33471
   364
krauss@34232
   365
    val (steps, indthm) =
krauss@34232
   366
      mk_induct_rule ctxt'' R x complete wf_thm ineqss scheme
krauss@33471
   367
krauss@33471
   368
    fun project (i, SchemeBranch {xs, ...}) =
krauss@34232
   369
      let
krauss@34232
   370
        val inst = (foldr1 HOLogic.mk_prod (map Free xs))
krauss@34232
   371
          |> SumTree.mk_inj ST (length branches) (i + 1)
krauss@34232
   372
          |> cert
krauss@34232
   373
      in
krauss@34232
   374
        indthm
krauss@34232
   375
        |> Drule.instantiate' [] [SOME inst]
krauss@34232
   376
        |> simplify SumTree.sumcase_split_ss
krauss@34232
   377
        |> Conv.fconv_rule ind_rulify
krauss@34232
   378
      end
krauss@33471
   379
krauss@33471
   380
    val res = Conjunction.intr_balanced (map_index project branches)
wenzelm@36945
   381
      |> fold_rev Thm.implies_intr (map cprop_of newgoals @ steps)
krauss@34232
   382
      |> Drule.generalize ([], [Rn])
krauss@33471
   383
krauss@33471
   384
    val nbranches = length branches
krauss@33471
   385
    val npres = length pres
krauss@33471
   386
  in
krauss@33471
   387
    Thm.compose_no_flatten false (res, length newgoals) i
krauss@33471
   388
    THEN term_tac (i + nbranches + npres)
krauss@33471
   389
    THEN (EVERY (map (TRY o pres_tac) ((i + nbranches + npres - 1) downto (i + nbranches))))
krauss@33471
   390
    THEN (EVERY (map (TRY o comp_tac) ((i + nbranches - 1) downto i)))
krauss@33471
   391
  end))
krauss@33471
   392
krauss@33471
   393
krauss@33471
   394
fun induction_schema_tac ctxt =
krauss@33471
   395
  mk_ind_tac (K all_tac) (assume_tac APPEND' Goal.assume_rule_tac ctxt) (K all_tac) ctxt;
krauss@33471
   396
krauss@33471
   397
val setup =
krauss@33471
   398
  Method.setup @{binding induction_schema} (Scan.succeed (RAW_METHOD o induction_schema_tac))
krauss@33471
   399
    "proves an induction principle"
krauss@33471
   400
krauss@33471
   401
end