src/Provers/induct_method.ML
author wenzelm
Wed Nov 16 17:45:24 2005 +0100 (2005-11-16)
changeset 18178 9e4dfe031525
parent 18147 31634a2af39e
child 18205 744803a2d5a5
permissions -rw-r--r--
induct: support local definitions to be passed through the induction;
deprecate open rule cases;
misc cleanup;
wenzelm@11670
     1
(*  Title:      Provers/induct_method.ML
wenzelm@11670
     2
    ID:         $Id$
wenzelm@11670
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@11670
     4
wenzelm@11735
     5
Proof by cases and induction on sets and types.
wenzelm@11670
     6
*)
wenzelm@11670
     7
wenzelm@11670
     8
signature INDUCT_METHOD_DATA =
wenzelm@11670
     9
sig
wenzelm@11670
    10
  val dest_concls: term -> term list
wenzelm@11670
    11
  val cases_default: thm
wenzelm@11996
    12
  val local_impI: thm
wenzelm@11670
    13
  val conjI: thm
wenzelm@11670
    14
  val atomize: thm list
wenzelm@11670
    15
  val rulify1: thm list
wenzelm@11670
    16
  val rulify2: thm list
wenzelm@12240
    17
  val localize: thm list
wenzelm@11670
    18
end;
wenzelm@11670
    19
wenzelm@11670
    20
signature INDUCT_METHOD =
wenzelm@11670
    21
sig
wenzelm@16391
    22
  val cases_tac: Proof.context -> bool -> term option list list -> thm option ->
wenzelm@16391
    23
    thm list -> int -> RuleCases.tactic
wenzelm@18178
    24
  val fix_tac: Proof.context -> (string * typ) list -> int -> tactic
wenzelm@18178
    25
  val induct_tac: Proof.context -> bool -> (string option * term) option list list ->
wenzelm@18147
    26
    thm option -> (string * typ) list -> thm list -> int -> RuleCases.tactic
wenzelm@11670
    27
  val setup: (theory -> theory) list
wenzelm@11670
    28
end;
wenzelm@11670
    29
wenzelm@11670
    30
functor InductMethodFun(Data: INDUCT_METHOD_DATA): INDUCT_METHOD =
wenzelm@11670
    31
struct
wenzelm@11670
    32
wenzelm@11670
    33
wenzelm@11670
    34
(** misc utils **)
wenzelm@11670
    35
wenzelm@11670
    36
(* align lists *)
wenzelm@11670
    37
wenzelm@11670
    38
fun align_left msg xs ys =
wenzelm@11670
    39
  let val m = length xs and n = length ys
wenzelm@11735
    40
  in if m < n then raise ERROR_MESSAGE msg else (Library.take (n, xs) ~~ ys) end;
wenzelm@11670
    41
wenzelm@11670
    42
fun align_right msg xs ys =
wenzelm@11670
    43
  let val m = length xs and n = length ys
wenzelm@11735
    44
  in if m < n then raise ERROR_MESSAGE msg else (Library.drop (m - n, xs) ~~ ys) end;
wenzelm@11670
    45
wenzelm@11670
    46
wenzelm@11735
    47
(* prep_inst *)
wenzelm@11670
    48
wenzelm@11735
    49
fun prep_inst align cert tune (tm, ts) =
wenzelm@11670
    50
  let
skalberg@15531
    51
    fun prep_var (x, SOME t) =
wenzelm@11670
    52
          let
wenzelm@11670
    53
            val cx = cert x;
wenzelm@18147
    54
            val {T = xT, thy, ...} = Thm.rep_cterm cx;
wenzelm@12799
    55
            val ct = cert (tune t);
wenzelm@11670
    56
          in
wenzelm@18147
    57
            if Sign.typ_instance thy (#T (Thm.rep_cterm ct), xT) then SOME (cx, ct)
wenzelm@11735
    58
            else raise ERROR_MESSAGE (Pretty.string_of (Pretty.block
wenzelm@11735
    59
             [Pretty.str "Ill-typed instantiation:", Pretty.fbrk,
wenzelm@11735
    60
              Display.pretty_cterm ct, Pretty.str " ::", Pretty.brk 1,
wenzelm@11735
    61
              Display.pretty_ctyp (#T (Thm.crep_cterm ct))]))
wenzelm@11670
    62
          end
skalberg@15531
    63
      | prep_var (_, NONE) = NONE;
wenzelm@11735
    64
    val xs = InductAttrib.vars_of tm;
wenzelm@11670
    65
  in
wenzelm@11735
    66
    align "Rule has fewer variables than instantiations given" xs ts
skalberg@15570
    67
    |> List.mapPartial prep_var
wenzelm@11670
    68
  end;
wenzelm@11670
    69
wenzelm@11670
    70
wenzelm@18178
    71
(* warn_open *)
wenzelm@18178
    72
wenzelm@18178
    73
fun warn_open true = warning "Encountered open rule cases -- deprecated"
wenzelm@18178
    74
  | warn_open false = ();
wenzelm@18178
    75
wenzelm@18178
    76
wenzelm@11670
    77
wenzelm@11670
    78
(** cases method **)
wenzelm@11670
    79
wenzelm@11670
    80
(*
wenzelm@11735
    81
  rule selection scheme:
wenzelm@18178
    82
          cases         - default case split
wenzelm@18178
    83
    `x:A` cases ...     - set cases
wenzelm@11735
    84
          cases t       - type cases
wenzelm@18178
    85
    ...   cases ... r   - explicit rule
wenzelm@11670
    86
*)
wenzelm@11670
    87
wenzelm@11670
    88
local
wenzelm@11670
    89
wenzelm@18178
    90
fun find_casesT ctxt ((SOME t :: _) :: _) = InductAttrib.find_casesT ctxt (Term.fastype_of t)
wenzelm@11735
    91
  | find_casesT _ _ = [];
wenzelm@11735
    92
wenzelm@11735
    93
fun find_casesS ctxt (fact :: _) = InductAttrib.find_casesS ctxt fact
wenzelm@11735
    94
  | find_casesS _ _ = [];
wenzelm@11735
    95
wenzelm@16391
    96
in
wenzelm@16391
    97
wenzelm@16391
    98
fun cases_tac ctxt is_open insts opt_rule facts =
wenzelm@11670
    99
  let
wenzelm@18147
   100
    val thy = ProofContext.theory_of ctxt;
wenzelm@18147
   101
    val cert = Thm.cterm_of thy;
wenzelm@11670
   102
wenzelm@11735
   103
    fun inst_rule r =
wenzelm@11735
   104
      if null insts then RuleCases.add r
wenzelm@11735
   105
      else (align_left "Rule has fewer premises than arguments given" (Thm.prems_of r) insts
skalberg@15570
   106
        |> (List.concat o map (prep_inst align_left cert I))
wenzelm@11735
   107
        |> Drule.cterm_instantiate) r |> rpair (RuleCases.get r);
wenzelm@11670
   108
wenzelm@11735
   109
    val ruleq =
wenzelm@12852
   110
      (case opt_rule of
skalberg@15531
   111
        NONE =>
wenzelm@12852
   112
          let val rules = find_casesS ctxt facts @ find_casesT ctxt insts @ [Data.cases_default] in
wenzelm@12053
   113
            Method.trace ctxt rules;
wenzelm@18147
   114
            Seq.maps (Seq.try inst_rule) (Seq.of_list rules)
wenzelm@11670
   115
          end
skalberg@15531
   116
      | SOME r => Seq.single (inst_rule r));
wenzelm@18178
   117
    val ruleq_cases = ruleq |> Seq.maps (fn (th, (cases, n)) =>
wenzelm@18178
   118
      Method.multi_resolves (Library.take (n, facts)) [th]
wenzelm@18178
   119
      |> Seq.map (apsnd (rpair (Library.drop (n, facts))) o rpair cases));
wenzelm@11670
   120
wenzelm@18178
   121
    fun make_cases rule = RuleCases.make (tap warn_open is_open) NONE (thy, Thm.prop_of rule);
wenzelm@18178
   122
  in
wenzelm@18178
   123
    fn i => fn st =>
wenzelm@18178
   124
      ruleq_cases |> Seq.maps (fn (rule, (cases, facts)) =>
wenzelm@18178
   125
        (Method.insert_tac facts THEN' Tactic.rtac rule) i st
wenzelm@18178
   126
        |> Seq.map (rpair (make_cases rule cases)))
wenzelm@18178
   127
  end;
wenzelm@11670
   128
wenzelm@16391
   129
val cases_meth = Method.METHOD_CASES o ((Seq.DETERM o HEADGOAL) oo
wenzelm@16391
   130
  (fn (ctxt, (is_open, (insts, opt_rule))) => cases_tac ctxt is_open insts opt_rule));
wenzelm@11670
   131
wenzelm@11670
   132
end;
wenzelm@11670
   133
wenzelm@11670
   134
wenzelm@11670
   135
wenzelm@11670
   136
(** induct method **)
wenzelm@11670
   137
wenzelm@18178
   138
(* fixes *)
wenzelm@11670
   139
wenzelm@11670
   140
local
wenzelm@11670
   141
wenzelm@18178
   142
val meta_spec = PureThy.get_thm Pure.thy (Name "meta_spec");
wenzelm@18178
   143
wenzelm@18178
   144
fun meta_spec_tac ctxt (x, T) i st = SUBGOAL (fn (goal, _) =>
wenzelm@18178
   145
  let
wenzelm@18178
   146
    val thy = Thm.theory_of_thm st;
wenzelm@18178
   147
    val cert = Thm.cterm_of thy;
wenzelm@18178
   148
    val certT = Thm.ctyp_of thy;
wenzelm@18178
   149
wenzelm@18178
   150
    val v = Free (x, T);
wenzelm@18178
   151
    val _ = Term.exists_subterm (fn t => t aconv v) goal orelse
wenzelm@18178
   152
      error ("No variable " ^ ProofContext.string_of_term ctxt v ^ " in subgoal");
wenzelm@18178
   153
    val P = Term.absfree (x, T, goal);
wenzelm@18178
   154
    val rule = meta_spec
wenzelm@18178
   155
      |> Drule.instantiate' [SOME (certT T)] [SOME (cert P), SOME (cert v)]
wenzelm@18178
   156
      |> Thm.rename_params_rule ([x], 1);
wenzelm@18178
   157
  in compose_tac (false, rule, 1) end i) i st;
wenzelm@18178
   158
wenzelm@18178
   159
in
wenzelm@18178
   160
wenzelm@18178
   161
fun fix_tac ctxt fixes =
wenzelm@18178
   162
  (case gen_duplicates (op =) fixes of
wenzelm@18178
   163
    [] => EVERY' (map (meta_spec_tac ctxt) (rev fixes))
wenzelm@18178
   164
  | dups => error ("Duplicate specification of variable(s): " ^
wenzelm@18178
   165
      commas (map (ProofContext.string_of_term ctxt o Free) dups)));
wenzelm@18178
   166
wenzelm@18178
   167
end;
wenzelm@18178
   168
wenzelm@18178
   169
wenzelm@18178
   170
(* defs *)
wenzelm@18178
   171
wenzelm@18178
   172
fun add_defs def_insts =
wenzelm@18178
   173
  let
wenzelm@18178
   174
    fun add (SOME (SOME x, t)) ctxt =
wenzelm@18178
   175
          let val ((lhs, def), ctxt') = ProofContext.add_def (x, t) ctxt
wenzelm@18178
   176
          in ((SOME (Free lhs), [def]), ctxt') end
wenzelm@18178
   177
      | add (SOME (NONE, t)) ctxt = ((SOME t, []), ctxt)
wenzelm@18178
   178
      | add NONE ctxt = ((NONE, []), ctxt);
wenzelm@18178
   179
  in fold_map add def_insts #> apfst (split_list #> apsnd List.concat) end;
wenzelm@18178
   180
wenzelm@11790
   181
wenzelm@11790
   182
(* atomize and rulify *)
wenzelm@11790
   183
wenzelm@18147
   184
fun atomize_term thy =
wenzelm@18147
   185
  ObjectLogic.drop_judgment thy o MetaSimplifier.rewrite_term thy Data.atomize [];
wenzelm@12799
   186
wenzelm@12799
   187
fun rulified_term thm =
wenzelm@18147
   188
  let val thy = Thm.theory_of_thm thm in
wenzelm@12799
   189
    Thm.prop_of thm
wenzelm@18147
   190
    |> MetaSimplifier.rewrite_term thy Data.rulify1 []
wenzelm@18147
   191
    |> MetaSimplifier.rewrite_term thy Data.rulify2 []
wenzelm@18147
   192
    |> pair thy
wenzelm@12799
   193
  end;
wenzelm@11756
   194
wenzelm@11670
   195
val atomize_tac = Tactic.rewrite_goal_tac Data.atomize;
wenzelm@11670
   196
wenzelm@11670
   197
val rulify_tac =
wenzelm@11670
   198
  Tactic.rewrite_goal_tac Data.rulify1 THEN'
wenzelm@11670
   199
  Tactic.rewrite_goal_tac Data.rulify2 THEN'
wenzelm@11670
   200
  Tactic.norm_hhf_tac;
wenzelm@11670
   201
wenzelm@18023
   202
val localize = Goal.norm_hhf o Tactic.simplify false Data.localize;
wenzelm@12162
   203
wenzelm@11670
   204
wenzelm@18147
   205
(* internalize implications -- limited to atomic prems *)
wenzelm@18147
   206
wenzelm@18147
   207
local
wenzelm@11996
   208
wenzelm@11996
   209
fun imp_intr i raw_th =
wenzelm@11996
   210
  let
wenzelm@11996
   211
    val th = Thm.permute_prems (i - 1) 1 raw_th;
wenzelm@18147
   212
    val {thy, maxidx, ...} = Thm.rep_thm th;
wenzelm@11996
   213
    val cprems = Drule.cprems_of th;
skalberg@15570
   214
    val As = Library.take (length cprems - 1, cprems);
wenzelm@18147
   215
    val C = Thm.cterm_of thy (Var (("C", maxidx + 1), propT));
wenzelm@18023
   216
  in th COMP Thm.lift_rule (Drule.list_implies (As, C)) Data.local_impI end;
wenzelm@11996
   217
wenzelm@18147
   218
in
wenzelm@18147
   219
wenzelm@18147
   220
fun internalize k th = if k > 0 then internalize (k - 1) (imp_intr k th) else th;
wenzelm@18147
   221
wenzelm@18147
   222
end;
wenzelm@18147
   223
wenzelm@11996
   224
wenzelm@11790
   225
(* join multi-rules *)
wenzelm@11790
   226
wenzelm@11735
   227
val eq_prems = curry (Term.aconvs o pairself Thm.prems_of);
wenzelm@11670
   228
wenzelm@11735
   229
fun join_rules [] = []
wenzelm@11735
   230
  | join_rules [th] = [th]
wenzelm@11735
   231
  | join_rules (rules as r :: rs) =
wenzelm@11735
   232
      if not (forall (eq_prems r) rs) then []
wenzelm@11735
   233
      else
wenzelm@11735
   234
        let
wenzelm@11735
   235
          val th :: ths = map Drule.freeze_all rules;
wenzelm@11735
   236
          val cprems = Drule.cprems_of th;
wenzelm@11735
   237
          val asms = map Thm.assume cprems;
wenzelm@11735
   238
        in
wenzelm@11735
   239
          [foldr1 (fn (x, x') => [x, x'] MRS Data.conjI)
wenzelm@11735
   240
            (map (fn x => Drule.implies_elim_list x asms) (th :: ths))
wenzelm@11735
   241
          |> Drule.implies_intr_list cprems
wenzelm@12305
   242
          |> Drule.standard'
wenzelm@13425
   243
          |> RuleCases.save r]
wenzelm@11735
   244
        end;
wenzelm@11670
   245
wenzelm@11790
   246
wenzelm@18178
   247
(* divinate rule instantiation -- cannot handle pending goal parameters *)
wenzelm@11790
   248
wenzelm@18147
   249
fun dest_env thy (env as Envir.Envir {iTs, ...}) =
wenzelm@11790
   250
  let
wenzelm@18147
   251
    val cert = Thm.cterm_of thy;
wenzelm@18147
   252
    val certT = Thm.ctyp_of thy;
berghofe@15794
   253
    val pairs = Envir.alist_of env;
wenzelm@18147
   254
    val ts = map (cert o Envir.norm_term env o #2 o #2) pairs;
wenzelm@18147
   255
    val xs = map2 (cert o Var) (map #1 pairs, map (#T o Thm.rep_cterm) ts);
wenzelm@18147
   256
  in (map (fn (xi, (S, T)) => (certT (TVar (xi, S)), certT T)) (Vartab.dest iTs), xs ~~ ts) end;
wenzelm@11790
   257
wenzelm@11790
   258
fun divinate_inst rule i st =
wenzelm@11790
   259
  let
wenzelm@18147
   260
    val {thy, maxidx, ...} = Thm.rep_thm st;
wenzelm@18147
   261
    val goal = Thm.term_of (Thm.cprem_of st i);  (*exception Subscript*)
wenzelm@11790
   262
    val params = rev (rename_wrt_term goal (Logic.strip_params goal));  (*as they are printed :-*)
wenzelm@11790
   263
  in
wenzelm@11790
   264
    if not (null params) then
wenzelm@11790
   265
      (warning ("Cannot determine rule instantiation due to pending parameter(s): " ^
wenzelm@18147
   266
        commas (map (Sign.string_of_term thy o Syntax.mark_boundT) params));
wenzelm@11790
   267
      Seq.single rule)
wenzelm@11790
   268
    else
wenzelm@11790
   269
      let
wenzelm@11790
   270
        val rule' = Thm.incr_indexes (maxidx + 1) rule;
wenzelm@11790
   271
        val concl = Logic.strip_assums_concl goal;
wenzelm@11790
   272
      in
wenzelm@18147
   273
        Unify.smash_unifiers (thy, Envir.empty (#maxidx (Thm.rep_thm rule')),
wenzelm@11790
   274
          [(Thm.concl_of rule', concl)])
wenzelm@18147
   275
        |> Seq.map (fn env => Drule.instantiate (dest_env thy env) rule')
wenzelm@11790
   276
      end
wenzelm@11790
   277
  end handle Subscript => Seq.empty;
wenzelm@11790
   278
wenzelm@11790
   279
wenzelm@18178
   280
(* special renaming of rule parameters *)
wenzelm@11790
   281
wenzelm@18178
   282
fun special_rename_params ctxt [[SOME (Free (z, Type (T, _)))]] thm =
wenzelm@18147
   283
      let
wenzelm@18178
   284
        val x = the_default z (ProofContext.revert_skolem ctxt z);
wenzelm@18147
   285
        fun index i [] = []
wenzelm@18147
   286
          | index i (y :: ys) =
wenzelm@18147
   287
              if x = y then x ^ string_of_int i :: index (i + 1) ys
wenzelm@18147
   288
              else y :: index i ys;
wenzelm@18147
   289
        fun rename_params [] = []
wenzelm@18147
   290
          | rename_params ((y, Type (U, _)) :: ys) =
wenzelm@18147
   291
              (if U = T then x else y) :: rename_params ys
wenzelm@18147
   292
          | rename_params ((y, _) :: ys) = y :: rename_params ys;
wenzelm@18147
   293
        fun rename_asm A =
wenzelm@18147
   294
          let
wenzelm@18147
   295
            val xs = rename_params (Logic.strip_params A);
wenzelm@18147
   296
            val xs' =
wenzelm@18147
   297
              (case List.filter (equal x) xs of
wenzelm@18147
   298
                [] => xs | [_] => xs | _ => index 1 xs);
wenzelm@18147
   299
          in Logic.list_rename_params (xs', A) end;
wenzelm@18147
   300
        fun rename_prop p =
wenzelm@18147
   301
          let val (As, C) = Logic.strip_horn p
wenzelm@18147
   302
          in Logic.list_implies (map rename_asm As, C) end;
wenzelm@18147
   303
        val cp' = cterm_fun rename_prop (Thm.cprop_of thm);
wenzelm@18147
   304
        val thm' = Thm.equal_elim (Thm.reflexive cp') thm;
wenzelm@18178
   305
      in RuleCases.save thm thm' end
wenzelm@18178
   306
  | special_rename_params _ _ thm = thm;
wenzelm@18178
   307
wenzelm@18178
   308
wenzelm@18178
   309
(* induct_tac *)
wenzelm@18178
   310
wenzelm@18178
   311
(*
wenzelm@18178
   312
  rule selection scheme:
wenzelm@18178
   313
    `x:A` induct ...     - set induction
wenzelm@18178
   314
          induct x       - type induction
wenzelm@18178
   315
    ...   induct ... r   - explicit rule
wenzelm@18178
   316
*)
wenzelm@18178
   317
wenzelm@18178
   318
local
nipkow@15235
   319
wenzelm@11735
   320
fun find_inductT ctxt insts =
wenzelm@18147
   321
  fold_rev multiply (insts |> List.mapPartial (fn [] => NONE | ts => List.last ts)
wenzelm@18147
   322
    |> map (InductAttrib.find_inductT ctxt o fastype_of)) [[]]
wenzelm@18178
   323
  |> map join_rules |> List.concat;
wenzelm@11670
   324
wenzelm@11735
   325
fun find_inductS ctxt (fact :: _) = InductAttrib.find_inductS ctxt fact
wenzelm@11735
   326
  | find_inductS _ _ = [];
wenzelm@11735
   327
wenzelm@16391
   328
in
wenzelm@11790
   329
wenzelm@18178
   330
fun induct_tac ctxt is_open def_insts opt_rule fixes facts =
wenzelm@11670
   331
  let
wenzelm@18147
   332
    val thy = ProofContext.theory_of ctxt;
wenzelm@18147
   333
    val cert = Thm.cterm_of thy;
wenzelm@11670
   334
wenzelm@18178
   335
    val ((insts, defs), defs_ctxt) = fold_map add_defs def_insts ctxt |>> split_list;
wenzelm@18178
   336
wenzelm@13105
   337
    fun rule_versions r = Seq.cons (r, Seq.filter (not o curry Thm.eq_thm r)
skalberg@15531
   338
        (Seq.make (fn () => SOME (localize r, Seq.empty))))
wenzelm@12168
   339
      |> Seq.map (rpair (RuleCases.get r));
wenzelm@12168
   340
wenzelm@12168
   341
    val inst_rule = apfst (fn r =>
wenzelm@12168
   342
      if null insts then r
wenzelm@11735
   343
      else (align_right "Rule has fewer conclusions than arguments given"
wenzelm@11735
   344
          (Data.dest_concls (Thm.concl_of r)) insts
wenzelm@18147
   345
        |> (List.concat o map (prep_inst align_right cert (atomize_term thy)))
wenzelm@12168
   346
        |> Drule.cterm_instantiate) r);
wenzelm@11670
   347
wenzelm@11735
   348
    val ruleq =
wenzelm@11735
   349
      (case opt_rule of
skalberg@15531
   350
        NONE =>
wenzelm@18178
   351
          let val rules = find_inductS ctxt facts @
wenzelm@18178
   352
            map (special_rename_params defs_ctxt insts) (find_inductT ctxt insts)
wenzelm@18178
   353
          in
wenzelm@12168
   354
            conditional (null rules) (fn () => error "Unable to figure out induct rule");
wenzelm@12053
   355
            Method.trace ctxt rules;
wenzelm@18147
   356
            rules |> Seq.of_list |> Seq.maps rule_versions |> Seq.maps (Seq.try inst_rule)
wenzelm@11735
   357
          end
wenzelm@18147
   358
      | SOME r => r |> rule_versions |> Seq.map inst_rule);
wenzelm@18178
   359
    val ruleq_cases = ruleq |> Seq.maps (fn (th, (cases, n)) =>
wenzelm@18178
   360
      Method.multi_resolves (Library.take (n, facts)) [th]
wenzelm@18178
   361
      |> Seq.map (rpair (cases, n - length facts, Library.drop (n, facts))));
wenzelm@11670
   362
wenzelm@18178
   363
    fun make_cases rule =
wenzelm@18178
   364
      RuleCases.make (tap warn_open is_open) (SOME (Thm.prop_of rule)) (rulified_term rule);
wenzelm@18178
   365
  in
wenzelm@18178
   366
    (fn i => fn st => ruleq_cases |> Seq.maps (fn (rule, (cases, k, more_facts)) =>
wenzelm@18178
   367
      (Method.insert_tac (List.concat defs @ more_facts)  (* FIXME CONJUNCTS *)
wenzelm@18178
   368
        THEN' fix_tac defs_ctxt fixes
wenzelm@18178
   369
        THEN' atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@18178
   370
        divinate_inst (internalize k rule) i st' |> Seq.maps (fn rule' =>
wenzelm@18178
   371
          Tactic.rtac rule' i st'
wenzelm@18178
   372
          |> Seq.maps (ProofContext.exports defs_ctxt ctxt)
wenzelm@18178
   373
          |> Seq.map (rpair (make_cases rule' cases))))))
wenzelm@18178
   374
    THEN_ALL_NEW_CASES rulify_tac
wenzelm@18178
   375
  end;
wenzelm@11670
   376
wenzelm@16391
   377
val induct_meth = Method.RAW_METHOD_CASES o ((Seq.DETERM o HEADGOAL) oo
wenzelm@18147
   378
  (fn (ctxt, (is_open, (insts, (opt_rule, fixes)))) =>
wenzelm@18147
   379
    induct_tac ctxt is_open insts opt_rule fixes));
wenzelm@11670
   380
wenzelm@11670
   381
end;
wenzelm@11670
   382
wenzelm@11670
   383
wenzelm@11670
   384
wenzelm@11670
   385
(** concrete syntax **)
wenzelm@11670
   386
wenzelm@11670
   387
val openN = "open";
wenzelm@11670
   388
val ruleN = "rule";
wenzelm@11670
   389
val ofN = "of";
wenzelm@18147
   390
val fixingN = "fixing";
wenzelm@11670
   391
wenzelm@11670
   392
local
wenzelm@11670
   393
wenzelm@15703
   394
fun named_rule k arg get =
wenzelm@15703
   395
  Scan.lift (Args.$$$ k -- Args.colon) |-- arg :-- (fn name => Scan.peek (fn ctxt =>
wenzelm@15703
   396
    (case get ctxt name of SOME x => Scan.succeed x
wenzelm@15703
   397
    | NONE => error ("No rule for " ^ k ^ " " ^ quote name)))) >> #2;
wenzelm@11670
   398
wenzelm@11670
   399
fun rule get_type get_set =
wenzelm@15703
   400
  named_rule InductAttrib.typeN Args.local_tyname get_type ||
wenzelm@15703
   401
  named_rule InductAttrib.setN Args.local_const get_set ||
wenzelm@11670
   402
  Scan.lift (Args.$$$ ruleN -- Args.colon) |-- Attrib.local_thm;
wenzelm@11670
   403
wenzelm@11670
   404
val cases_rule = rule InductAttrib.lookup_casesT InductAttrib.lookup_casesS;
wenzelm@11670
   405
val induct_rule = rule InductAttrib.lookup_inductT InductAttrib.lookup_inductS;
wenzelm@11670
   406
wenzelm@18178
   407
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
wenzelm@18147
   408
wenzelm@18178
   409
val def_inst =
wenzelm@18178
   410
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@18178
   411
      -- Args.local_term) >> SOME ||
wenzelm@18178
   412
    inst >> Option.map (pair NONE);
wenzelm@11670
   413
wenzelm@18147
   414
val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@18147
   415
  error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
wenzelm@18147
   416
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |-- Scan.repeat1 free) [];
wenzelm@18147
   417
wenzelm@18178
   418
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18178
   419
  ((Args.$$$ InductAttrib.typeN || Args.$$$ InductAttrib.setN || Args.$$$ ruleN ||
wenzelm@18178
   420
    Args.$$$ ofN || Args.$$$ fixingN) -- Args.colon)) scan;
wenzelm@18178
   421
wenzelm@11670
   422
in
wenzelm@11670
   423
wenzelm@18178
   424
val cases_args = Method.syntax (Args.mode openN --
wenzelm@18178
   425
  (Args.and_list (Scan.repeat (unless_more_args inst)) -- Scan.option cases_rule));
wenzelm@18178
   426
wenzelm@18178
   427
val induct_args = Method.syntax (Args.mode openN --
wenzelm@18178
   428
  (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
wenzelm@18178
   429
  (Scan.option induct_rule -- fixing)));
wenzelm@11670
   430
wenzelm@11670
   431
end;
wenzelm@11670
   432
wenzelm@11670
   433
wenzelm@11670
   434
wenzelm@11670
   435
(** theory setup **)
wenzelm@11670
   436
wenzelm@11670
   437
val setup =
wenzelm@11670
   438
  [Method.add_methods
wenzelm@11670
   439
    [(InductAttrib.casesN, cases_meth oo cases_args, "case analysis on types or sets"),
wenzelm@11670
   440
     (InductAttrib.inductN, induct_meth oo induct_args, "induction on types or sets")]];
wenzelm@11670
   441
wenzelm@11670
   442
end;