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