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