src/Tools/induct.ML
author wenzelm
Thu Oct 04 14:42:47 2007 +0200 (2007-10-04)
changeset 24830 a7b3ab44d993
child 24832 64cd13299d39
permissions -rw-r--r--
moved Pure/Isar/induct_attrib.ML and Provers/induct_method.ML to Tools/induct.ML;
wenzelm@24830
     1
(*  Title:      Tools/induct.ML
wenzelm@24830
     2
    ID:         $Id$
wenzelm@24830
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@24830
     4
wenzelm@24830
     5
Proof by cases and induction.
wenzelm@24830
     6
*)
wenzelm@24830
     7
wenzelm@24830
     8
signature INDUCT_DATA =
wenzelm@24830
     9
sig
wenzelm@24830
    10
  val cases_default: thm
wenzelm@24830
    11
  val atomize: thm list
wenzelm@24830
    12
  val rulify: thm list
wenzelm@24830
    13
  val rulify_fallback: thm list
wenzelm@24830
    14
end;
wenzelm@24830
    15
wenzelm@24830
    16
signature INDUCT =
wenzelm@24830
    17
sig
wenzelm@24830
    18
  (*rule declarations*)
wenzelm@24830
    19
  val vars_of: term -> term list
wenzelm@24830
    20
  val dest_rules: Proof.context ->
wenzelm@24830
    21
    {type_cases: (string * thm) list, set_cases: (string * thm) list,
wenzelm@24830
    22
      type_induct: (string * thm) list, set_induct: (string * thm) list,
wenzelm@24830
    23
      type_coinduct: (string * thm) list, set_coinduct: (string * thm) list}
wenzelm@24830
    24
  val print_rules: Proof.context -> unit
wenzelm@24830
    25
  val lookup_casesT: Proof.context -> string -> thm option
wenzelm@24830
    26
  val lookup_casesS: Proof.context -> string -> thm option
wenzelm@24830
    27
  val lookup_inductT: Proof.context -> string -> thm option
wenzelm@24830
    28
  val lookup_inductS: Proof.context -> string -> thm option
wenzelm@24830
    29
  val lookup_coinductT: Proof.context -> string -> thm option
wenzelm@24830
    30
  val lookup_coinductS: Proof.context -> string -> thm option
wenzelm@24830
    31
  val find_casesT: Proof.context -> typ -> thm list
wenzelm@24830
    32
  val find_casesS: Proof.context -> term -> thm list
wenzelm@24830
    33
  val find_inductT: Proof.context -> typ -> thm list
wenzelm@24830
    34
  val find_inductS: Proof.context -> term -> thm list
wenzelm@24830
    35
  val find_coinductT: Proof.context -> typ -> thm list
wenzelm@24830
    36
  val find_coinductS: Proof.context -> term -> thm list
wenzelm@24830
    37
  val cases_type: string -> attribute
wenzelm@24830
    38
  val cases_set: string -> attribute
wenzelm@24830
    39
  val induct_type: string -> attribute
wenzelm@24830
    40
  val induct_set: string -> attribute
wenzelm@24830
    41
  val coinduct_type: string -> attribute
wenzelm@24830
    42
  val coinduct_set: string -> attribute
wenzelm@24830
    43
  val casesN: string
wenzelm@24830
    44
  val inductN: string
wenzelm@24830
    45
  val coinductN: string
wenzelm@24830
    46
  val typeN: string
wenzelm@24830
    47
  val setN: string
wenzelm@24830
    48
  (*proof methods*)
wenzelm@24830
    49
  val fix_tac: Proof.context -> int -> (string * typ) list -> int -> tactic
wenzelm@24830
    50
  val add_defs: (string option * term) option list -> Proof.context ->
wenzelm@24830
    51
    (term option list * thm list) * Proof.context
wenzelm@24830
    52
  val atomize_term: theory -> term -> term
wenzelm@24830
    53
  val atomize_tac: int -> tactic
wenzelm@24830
    54
  val inner_atomize_tac: int -> tactic
wenzelm@24830
    55
  val rulified_term: thm -> theory * term
wenzelm@24830
    56
  val rulify_tac: int -> tactic
wenzelm@24830
    57
  val internalize: int -> thm -> thm
wenzelm@24830
    58
  val guess_instance: thm -> int -> thm -> thm Seq.seq
wenzelm@24830
    59
  val cases_tac: Proof.context -> bool -> term option list list -> thm option ->
wenzelm@24830
    60
    thm list -> int -> cases_tactic
wenzelm@24830
    61
  val induct_tac: Proof.context -> bool -> (string option * term) option list list ->
wenzelm@24830
    62
    (string * typ) list list -> term option list -> thm list option -> thm list -> int ->
wenzelm@24830
    63
    cases_tactic
wenzelm@24830
    64
  val coinduct_tac: Proof.context -> bool -> term option list -> term option list ->
wenzelm@24830
    65
    thm option -> thm list -> int -> cases_tactic
wenzelm@24830
    66
  val setup: theory -> theory
wenzelm@24830
    67
end;
wenzelm@24830
    68
wenzelm@24830
    69
functor InductFun(Data: INDUCT_DATA): INDUCT =
wenzelm@24830
    70
struct
wenzelm@24830
    71
wenzelm@24830
    72
wenzelm@24830
    73
(** misc utils **)
wenzelm@24830
    74
wenzelm@24830
    75
(* encode_type -- for indexing purposes *)
wenzelm@24830
    76
wenzelm@24830
    77
fun encode_type (Type (c, Ts)) = Term.list_comb (Const (c, dummyT), map encode_type Ts)
wenzelm@24830
    78
  | encode_type (TFree (a, _)) = Free (a, dummyT)
wenzelm@24830
    79
  | encode_type (TVar (a, _)) = Var (a, dummyT);
wenzelm@24830
    80
wenzelm@24830
    81
wenzelm@24830
    82
(* variables -- ordered left-to-right, preferring right *)
wenzelm@24830
    83
wenzelm@24830
    84
fun vars_of tm =
wenzelm@24830
    85
  rev (distinct (op =) (Term.fold_aterms (fn (t as Var _) => cons t | _ => I) tm []));
wenzelm@24830
    86
wenzelm@24830
    87
local
wenzelm@24830
    88
wenzelm@24830
    89
val mk_var = encode_type o #2 o Term.dest_Var;
wenzelm@24830
    90
wenzelm@24830
    91
fun concl_var which thm = mk_var (which (vars_of (Thm.concl_of thm))) handle Empty =>
wenzelm@24830
    92
  raise THM ("No variables in conclusion of rule", 0, [thm]);
wenzelm@24830
    93
wenzelm@24830
    94
in
wenzelm@24830
    95
wenzelm@24830
    96
fun left_var_prem thm = mk_var (hd (vars_of (hd (Thm.prems_of thm)))) handle Empty =>
wenzelm@24830
    97
  raise THM ("No variables in major premise of rule", 0, [thm]);
wenzelm@24830
    98
wenzelm@24830
    99
val left_var_concl = concl_var hd;
wenzelm@24830
   100
val right_var_concl = concl_var List.last;
wenzelm@24830
   101
wenzelm@24830
   102
end;
wenzelm@24830
   103
wenzelm@24830
   104
wenzelm@24830
   105
wenzelm@24830
   106
(** induct data **)
wenzelm@24830
   107
wenzelm@24830
   108
(* rules *)
wenzelm@24830
   109
wenzelm@24830
   110
type rules = (string * thm) NetRules.T;
wenzelm@24830
   111
wenzelm@24830
   112
val init_rules =
wenzelm@24830
   113
  NetRules.init (fn ((s1: string, th1), (s2, th2)) => s1 = s2 andalso
wenzelm@24830
   114
    Thm.eq_thm_prop (th1, th2));
wenzelm@24830
   115
wenzelm@24830
   116
fun lookup_rule (rs: rules) = AList.lookup (op =) (NetRules.rules rs);
wenzelm@24830
   117
wenzelm@24830
   118
fun pretty_rules ctxt kind rs =
wenzelm@24830
   119
  let val thms = map snd (NetRules.rules rs)
wenzelm@24830
   120
  in Pretty.big_list kind (map (ProofContext.pretty_thm ctxt) thms) end;
wenzelm@24830
   121
wenzelm@24830
   122
wenzelm@24830
   123
(* context data *)
wenzelm@24830
   124
wenzelm@24830
   125
structure Induct = GenericDataFun
wenzelm@24830
   126
(
wenzelm@24830
   127
  type T = (rules * rules) * (rules * rules) * (rules * rules);
wenzelm@24830
   128
  val empty =
wenzelm@24830
   129
    ((init_rules (left_var_prem o #2), init_rules (Thm.major_prem_of o #2)),
wenzelm@24830
   130
     (init_rules (right_var_concl o #2), init_rules (Thm.major_prem_of o #2)),
wenzelm@24830
   131
     (init_rules (left_var_concl o #2), init_rules (Thm.concl_of o #2)));
wenzelm@24830
   132
  val extend = I;
wenzelm@24830
   133
  fun merge _ (((casesT1, casesS1), (inductT1, inductS1), (coinductT1, coinductS1)),
wenzelm@24830
   134
      ((casesT2, casesS2), (inductT2, inductS2), (coinductT2, coinductS2))) =
wenzelm@24830
   135
    ((NetRules.merge (casesT1, casesT2), NetRules.merge (casesS1, casesS2)),
wenzelm@24830
   136
      (NetRules.merge (inductT1, inductT2), NetRules.merge (inductS1, inductS2)),
wenzelm@24830
   137
      (NetRules.merge (coinductT1, coinductT2), NetRules.merge (coinductS1, coinductS2)));
wenzelm@24830
   138
);
wenzelm@24830
   139
wenzelm@24830
   140
val get_local = Induct.get o Context.Proof;
wenzelm@24830
   141
wenzelm@24830
   142
fun dest_rules ctxt =
wenzelm@24830
   143
  let val ((casesT, casesS), (inductT, inductS), (coinductT, coinductS)) = get_local ctxt in
wenzelm@24830
   144
    {type_cases = NetRules.rules casesT,
wenzelm@24830
   145
     set_cases = NetRules.rules casesS,
wenzelm@24830
   146
     type_induct = NetRules.rules inductT,
wenzelm@24830
   147
     set_induct = NetRules.rules inductS,
wenzelm@24830
   148
     type_coinduct = NetRules.rules coinductT,
wenzelm@24830
   149
     set_coinduct = NetRules.rules coinductS}
wenzelm@24830
   150
  end;
wenzelm@24830
   151
wenzelm@24830
   152
fun print_rules ctxt =
wenzelm@24830
   153
  let val ((casesT, casesS), (inductT, inductS), (coinductT, coinductS)) = get_local ctxt in
wenzelm@24830
   154
   [pretty_rules ctxt "coinduct type:" coinductT,
wenzelm@24830
   155
    pretty_rules ctxt "coinduct set:" coinductS,
wenzelm@24830
   156
    pretty_rules ctxt "induct type:" inductT,
wenzelm@24830
   157
    pretty_rules ctxt "induct set:" inductS,
wenzelm@24830
   158
    pretty_rules ctxt "cases type:" casesT,
wenzelm@24830
   159
    pretty_rules ctxt "cases set:" casesS]
wenzelm@24830
   160
    |> Pretty.chunks |> Pretty.writeln
wenzelm@24830
   161
  end;
wenzelm@24830
   162
wenzelm@24830
   163
val _ = OuterSyntax.add_parsers [
wenzelm@24830
   164
  OuterSyntax.improper_command "print_induct_rules" "print induction and cases rules"
wenzelm@24830
   165
    OuterKeyword.diag (Scan.succeed (Toplevel.no_timing o Toplevel.unknown_context o
wenzelm@24830
   166
      Toplevel.keep (print_rules o Toplevel.context_of)))];
wenzelm@24830
   167
wenzelm@24830
   168
wenzelm@24830
   169
(* access rules *)
wenzelm@24830
   170
wenzelm@24830
   171
val lookup_casesT = lookup_rule o #1 o #1 o get_local;
wenzelm@24830
   172
val lookup_casesS = lookup_rule o #2 o #1 o get_local;
wenzelm@24830
   173
val lookup_inductT = lookup_rule o #1 o #2 o get_local;
wenzelm@24830
   174
val lookup_inductS = lookup_rule o #2 o #2 o get_local;
wenzelm@24830
   175
val lookup_coinductT = lookup_rule o #1 o #3 o get_local;
wenzelm@24830
   176
val lookup_coinductS = lookup_rule o #2 o #3 o get_local;
wenzelm@24830
   177
wenzelm@24830
   178
wenzelm@24830
   179
fun find_rules which how ctxt x =
wenzelm@24830
   180
  map snd (NetRules.retrieve (which (get_local ctxt)) (how x));
wenzelm@24830
   181
wenzelm@24830
   182
val find_casesT = find_rules (#1 o #1) encode_type;
wenzelm@24830
   183
val find_casesS = find_rules (#2 o #1) I;
wenzelm@24830
   184
val find_inductT = find_rules (#1 o #2) encode_type;
wenzelm@24830
   185
val find_inductS = find_rules (#2 o #2) I;
wenzelm@24830
   186
val find_coinductT = find_rules (#1 o #3) encode_type;
wenzelm@24830
   187
val find_coinductS = find_rules (#2 o #3) I;
wenzelm@24830
   188
wenzelm@24830
   189
wenzelm@24830
   190
wenzelm@24830
   191
(** attributes **)
wenzelm@24830
   192
wenzelm@24830
   193
local
wenzelm@24830
   194
wenzelm@24830
   195
fun mk_att f g name arg =
wenzelm@24830
   196
  let val (x, thm) = g arg in (Induct.map (f (name, thm)) x, thm) end;
wenzelm@24830
   197
wenzelm@24830
   198
fun map1 f (x, y, z) = (f x, y, z);
wenzelm@24830
   199
fun map2 f (x, y, z) = (x, f y, z);
wenzelm@24830
   200
fun map3 f (x, y, z) = (x, y, f z);
wenzelm@24830
   201
wenzelm@24830
   202
fun add_casesT rule x = map1 (apfst (NetRules.insert rule)) x;
wenzelm@24830
   203
fun add_casesS rule x = map1 (apsnd (NetRules.insert rule)) x;
wenzelm@24830
   204
fun add_inductT rule x = map2 (apfst (NetRules.insert rule)) x;
wenzelm@24830
   205
fun add_inductS rule x = map2 (apsnd (NetRules.insert rule)) x;
wenzelm@24830
   206
fun add_coinductT rule x = map3 (apfst (NetRules.insert rule)) x;
wenzelm@24830
   207
fun add_coinductS rule x = map3 (apsnd (NetRules.insert rule)) x;
wenzelm@24830
   208
wenzelm@24830
   209
fun consumes0 x = RuleCases.consumes_default 0 x;
wenzelm@24830
   210
fun consumes1 x = RuleCases.consumes_default 1 x;
wenzelm@24830
   211
wenzelm@24830
   212
in
wenzelm@24830
   213
wenzelm@24830
   214
val cases_type = mk_att add_casesT consumes0;
wenzelm@24830
   215
val cases_set = mk_att add_casesS consumes1;
wenzelm@24830
   216
val induct_type = mk_att add_inductT consumes0;
wenzelm@24830
   217
val induct_set = mk_att add_inductS consumes1;
wenzelm@24830
   218
val coinduct_type = mk_att add_coinductT consumes0;
wenzelm@24830
   219
val coinduct_set = mk_att add_coinductS consumes1;
wenzelm@24830
   220
wenzelm@24830
   221
end;
wenzelm@24830
   222
wenzelm@24830
   223
wenzelm@24830
   224
wenzelm@24830
   225
(** attribute syntax **)
wenzelm@24830
   226
wenzelm@24830
   227
val casesN = "cases";
wenzelm@24830
   228
val inductN = "induct";
wenzelm@24830
   229
val coinductN = "coinduct";
wenzelm@24830
   230
wenzelm@24830
   231
val typeN = "type";
wenzelm@24830
   232
val setN = "set";
wenzelm@24830
   233
wenzelm@24830
   234
local
wenzelm@24830
   235
wenzelm@24830
   236
fun spec k arg =
wenzelm@24830
   237
  Scan.lift (Args.$$$ k -- Args.colon) |-- arg ||
wenzelm@24830
   238
  Scan.lift (Args.$$$ k) >> K "";
wenzelm@24830
   239
wenzelm@24830
   240
fun attrib add_type add_set =
wenzelm@24830
   241
  Attrib.syntax (spec typeN Args.tyname >> add_type || spec setN Args.const >> add_set);
wenzelm@24830
   242
wenzelm@24830
   243
val cases_att = attrib cases_type cases_set;
wenzelm@24830
   244
val induct_att = attrib induct_type induct_set;
wenzelm@24830
   245
val coinduct_att = attrib coinduct_type coinduct_set;
wenzelm@24830
   246
wenzelm@24830
   247
in
wenzelm@24830
   248
wenzelm@24830
   249
val attrib_setup = Attrib.add_attributes
wenzelm@24830
   250
 [(casesN, cases_att, "declaration of cases rule for type or set"),
wenzelm@24830
   251
  (inductN, induct_att, "declaration of induction rule for type or set"),
wenzelm@24830
   252
  (coinductN, coinduct_att, "declaration of coinduction rule for type or set")];
wenzelm@24830
   253
wenzelm@24830
   254
end;
wenzelm@24830
   255
wenzelm@24830
   256
wenzelm@24830
   257
wenzelm@24830
   258
(** method utils **)
wenzelm@24830
   259
wenzelm@24830
   260
(* alignment *)
wenzelm@24830
   261
wenzelm@24830
   262
fun align_left msg xs ys =
wenzelm@24830
   263
  let val m = length xs and n = length ys
wenzelm@24830
   264
  in if m < n then error msg else (Library.take (n, xs) ~~ ys) end;
wenzelm@24830
   265
wenzelm@24830
   266
fun align_right msg xs ys =
wenzelm@24830
   267
  let val m = length xs and n = length ys
wenzelm@24830
   268
  in if m < n then error msg else (Library.drop (m - n, xs) ~~ ys) end;
wenzelm@24830
   269
wenzelm@24830
   270
wenzelm@24830
   271
(* prep_inst *)
wenzelm@24830
   272
wenzelm@24830
   273
fun prep_inst thy align tune (tm, ts) =
wenzelm@24830
   274
  let
wenzelm@24830
   275
    val cert = Thm.cterm_of thy;
wenzelm@24830
   276
    fun prep_var (x, SOME t) =
wenzelm@24830
   277
          let
wenzelm@24830
   278
            val cx = cert x;
wenzelm@24830
   279
            val {T = xT, thy, ...} = Thm.rep_cterm cx;
wenzelm@24830
   280
            val ct = cert (tune t);
wenzelm@24830
   281
          in
wenzelm@24830
   282
            if Sign.typ_instance thy (#T (Thm.rep_cterm ct), xT) then SOME (cx, ct)
wenzelm@24830
   283
            else error (Pretty.string_of (Pretty.block
wenzelm@24830
   284
             [Pretty.str "Ill-typed instantiation:", Pretty.fbrk,
wenzelm@24830
   285
              Display.pretty_cterm ct, Pretty.str " ::", Pretty.brk 1,
wenzelm@24830
   286
              Display.pretty_ctyp (#T (Thm.crep_cterm ct))]))
wenzelm@24830
   287
          end
wenzelm@24830
   288
      | prep_var (_, NONE) = NONE;
wenzelm@24830
   289
    val xs = vars_of tm;
wenzelm@24830
   290
  in
wenzelm@24830
   291
    align "Rule has fewer variables than instantiations given" xs ts
wenzelm@24830
   292
    |> map_filter prep_var
wenzelm@24830
   293
  end;
wenzelm@24830
   294
wenzelm@24830
   295
wenzelm@24830
   296
(* trace_rules *)
wenzelm@24830
   297
wenzelm@24830
   298
fun trace_rules _ kind [] = error ("Unable to figure out " ^ kind ^ " rule")
wenzelm@24830
   299
  | trace_rules ctxt _ rules = Method.trace ctxt rules;
wenzelm@24830
   300
wenzelm@24830
   301
wenzelm@24830
   302
(* make_cases *)
wenzelm@24830
   303
wenzelm@24830
   304
fun make_cases is_open rule =
wenzelm@24830
   305
  RuleCases.make_common is_open (Thm.theory_of_thm rule, Thm.prop_of rule);
wenzelm@24830
   306
wenzelm@24830
   307
fun warn_open true = legacy_feature "open rule cases in proof method"
wenzelm@24830
   308
  | warn_open false = ();
wenzelm@24830
   309
wenzelm@24830
   310
wenzelm@24830
   311
wenzelm@24830
   312
(** cases method **)
wenzelm@24830
   313
wenzelm@24830
   314
(*
wenzelm@24830
   315
  rule selection scheme:
wenzelm@24830
   316
          cases         - default case split
wenzelm@24830
   317
    `x:A` cases ...     - set cases
wenzelm@24830
   318
          cases t       - type cases
wenzelm@24830
   319
    ...   cases ... r   - explicit rule
wenzelm@24830
   320
*)
wenzelm@24830
   321
wenzelm@24830
   322
local
wenzelm@24830
   323
wenzelm@24830
   324
fun get_casesT ctxt ((SOME t :: _) :: _) = find_casesT ctxt (Term.fastype_of t)
wenzelm@24830
   325
  | get_casesT _ _ = [];
wenzelm@24830
   326
wenzelm@24830
   327
fun get_casesS ctxt (fact :: _) = find_casesS ctxt (Thm.concl_of fact)
wenzelm@24830
   328
  | get_casesS _ _ = [];
wenzelm@24830
   329
wenzelm@24830
   330
in
wenzelm@24830
   331
wenzelm@24830
   332
fun cases_tac ctxt is_open insts opt_rule facts =
wenzelm@24830
   333
  let
wenzelm@24830
   334
    val _ = warn_open is_open;
wenzelm@24830
   335
    val thy = ProofContext.theory_of ctxt;
wenzelm@24830
   336
    val cert = Thm.cterm_of thy;
wenzelm@24830
   337
wenzelm@24830
   338
    fun inst_rule r =
wenzelm@24830
   339
      if null insts then `RuleCases.get r
wenzelm@24830
   340
      else (align_left "Rule has fewer premises than arguments given" (Thm.prems_of r) insts
wenzelm@24830
   341
        |> maps (prep_inst thy align_left I)
wenzelm@24830
   342
        |> Drule.cterm_instantiate) r |> pair (RuleCases.get r);
wenzelm@24830
   343
wenzelm@24830
   344
    val ruleq =
wenzelm@24830
   345
      (case opt_rule of
wenzelm@24830
   346
        SOME r => Seq.single (inst_rule r)
wenzelm@24830
   347
      | NONE =>
wenzelm@24830
   348
          (get_casesS ctxt facts @ get_casesT ctxt insts @ [Data.cases_default])
wenzelm@24830
   349
          |> tap (trace_rules ctxt casesN)
wenzelm@24830
   350
          |> Seq.of_list |> Seq.maps (Seq.try inst_rule));
wenzelm@24830
   351
  in
wenzelm@24830
   352
    fn i => fn st =>
wenzelm@24830
   353
      ruleq
wenzelm@24830
   354
      |> Seq.maps (RuleCases.consume [] facts)
wenzelm@24830
   355
      |> Seq.maps (fn ((cases, (_, more_facts)), rule) =>
wenzelm@24830
   356
        CASES (make_cases is_open rule cases)
wenzelm@24830
   357
          (Method.insert_tac more_facts i THEN Tactic.rtac rule i) st)
wenzelm@24830
   358
  end;
wenzelm@24830
   359
wenzelm@24830
   360
end;
wenzelm@24830
   361
wenzelm@24830
   362
wenzelm@24830
   363
wenzelm@24830
   364
(** induct method **)
wenzelm@24830
   365
wenzelm@24830
   366
val conjunction_congs = [@{thm Pure.all_conjunction}, @{thm imp_conjunction}];
wenzelm@24830
   367
wenzelm@24830
   368
wenzelm@24830
   369
(* atomize *)
wenzelm@24830
   370
wenzelm@24830
   371
fun atomize_term thy =
wenzelm@24830
   372
  MetaSimplifier.rewrite_term thy Data.atomize []
wenzelm@24830
   373
  #> ObjectLogic.drop_judgment thy;
wenzelm@24830
   374
wenzelm@24830
   375
val atomize_cterm = MetaSimplifier.rewrite true Data.atomize;
wenzelm@24830
   376
wenzelm@24830
   377
val atomize_tac = Simplifier.rewrite_goal_tac Data.atomize;
wenzelm@24830
   378
wenzelm@24830
   379
val inner_atomize_tac =
wenzelm@24830
   380
  Simplifier.rewrite_goal_tac (map Thm.symmetric conjunction_congs) THEN' atomize_tac;
wenzelm@24830
   381
wenzelm@24830
   382
wenzelm@24830
   383
(* rulify *)
wenzelm@24830
   384
wenzelm@24830
   385
fun rulify_term thy =
wenzelm@24830
   386
  MetaSimplifier.rewrite_term thy (Data.rulify @ conjunction_congs) [] #>
wenzelm@24830
   387
  MetaSimplifier.rewrite_term thy Data.rulify_fallback [];
wenzelm@24830
   388
wenzelm@24830
   389
fun rulified_term thm =
wenzelm@24830
   390
  let
wenzelm@24830
   391
    val thy = Thm.theory_of_thm thm;
wenzelm@24830
   392
    val rulify = rulify_term thy;
wenzelm@24830
   393
    val (As, B) = Logic.strip_horn (Thm.prop_of thm);
wenzelm@24830
   394
  in (thy, Logic.list_implies (map rulify As, rulify B)) end;
wenzelm@24830
   395
wenzelm@24830
   396
val rulify_tac =
wenzelm@24830
   397
  Simplifier.rewrite_goal_tac (Data.rulify @ conjunction_congs) THEN'
wenzelm@24830
   398
  Simplifier.rewrite_goal_tac Data.rulify_fallback THEN'
wenzelm@24830
   399
  Goal.conjunction_tac THEN_ALL_NEW
wenzelm@24830
   400
  (Simplifier.rewrite_goal_tac [@{thm Pure.conjunction_imp}] THEN' Goal.norm_hhf_tac);
wenzelm@24830
   401
wenzelm@24830
   402
wenzelm@24830
   403
(* prepare rule *)
wenzelm@24830
   404
wenzelm@24830
   405
fun rule_instance thy inst rule =
wenzelm@24830
   406
  Drule.cterm_instantiate (prep_inst thy align_left I (Thm.prop_of rule, inst)) rule;
wenzelm@24830
   407
wenzelm@24830
   408
fun internalize k th =
wenzelm@24830
   409
  th |> Thm.permute_prems 0 k
wenzelm@24830
   410
  |> Conv.fconv_rule (Conv.concl_conv (Thm.nprems_of th - k) atomize_cterm);
wenzelm@24830
   411
wenzelm@24830
   412
wenzelm@24830
   413
(* guess rule instantiation -- cannot handle pending goal parameters *)
wenzelm@24830
   414
wenzelm@24830
   415
local
wenzelm@24830
   416
wenzelm@24830
   417
fun dest_env thy (env as Envir.Envir {iTs, ...}) =
wenzelm@24830
   418
  let
wenzelm@24830
   419
    val cert = Thm.cterm_of thy;
wenzelm@24830
   420
    val certT = Thm.ctyp_of thy;
wenzelm@24830
   421
    val pairs = Envir.alist_of env;
wenzelm@24830
   422
    val ts = map (cert o Envir.norm_term env o #2 o #2) pairs;
wenzelm@24830
   423
    val xs = map2 (curry (cert o Var)) (map #1 pairs) (map (#T o Thm.rep_cterm) ts);
wenzelm@24830
   424
  in (map (fn (xi, (S, T)) => (certT (TVar (xi, S)), certT T)) (Vartab.dest iTs), xs ~~ ts) end;
wenzelm@24830
   425
wenzelm@24830
   426
in
wenzelm@24830
   427
wenzelm@24830
   428
fun guess_instance rule i st =
wenzelm@24830
   429
  let
wenzelm@24830
   430
    val {thy, maxidx, ...} = Thm.rep_thm st;
wenzelm@24830
   431
    val goal = Thm.term_of (Thm.cprem_of st i);  (*exception Subscript*)
wenzelm@24830
   432
    val params = rev (rename_wrt_term goal (Logic.strip_params goal));
wenzelm@24830
   433
  in
wenzelm@24830
   434
    if not (null params) then
wenzelm@24830
   435
      (warning ("Cannot determine rule instantiation due to pending parameter(s): " ^
wenzelm@24830
   436
        commas_quote (map (Sign.string_of_term thy o Syntax.mark_boundT) params));
wenzelm@24830
   437
      Seq.single rule)
wenzelm@24830
   438
    else
wenzelm@24830
   439
      let
wenzelm@24830
   440
        val rule' = Thm.incr_indexes (maxidx + 1) rule;
wenzelm@24830
   441
        val concl = Logic.strip_assums_concl goal;
wenzelm@24830
   442
      in
wenzelm@24830
   443
        Unify.smash_unifiers thy [(Thm.concl_of rule', concl)]
wenzelm@24830
   444
          (Envir.empty (#maxidx (Thm.rep_thm rule')))
wenzelm@24830
   445
        |> Seq.map (fn env => Drule.instantiate (dest_env thy env) rule')
wenzelm@24830
   446
      end
wenzelm@24830
   447
  end handle Subscript => Seq.empty;
wenzelm@24830
   448
wenzelm@24830
   449
end;
wenzelm@24830
   450
wenzelm@24830
   451
wenzelm@24830
   452
(* special renaming of rule parameters *)
wenzelm@24830
   453
wenzelm@24830
   454
fun special_rename_params ctxt [[SOME (Free (z, Type (T, _)))]] [thm] =
wenzelm@24830
   455
      let
wenzelm@24830
   456
        val x = ProofContext.revert_skolem ctxt z;
wenzelm@24830
   457
        fun index i [] = []
wenzelm@24830
   458
          | index i (y :: ys) =
wenzelm@24830
   459
              if x = y then x ^ string_of_int i :: index (i + 1) ys
wenzelm@24830
   460
              else y :: index i ys;
wenzelm@24830
   461
        fun rename_params [] = []
wenzelm@24830
   462
          | rename_params ((y, Type (U, _)) :: ys) =
wenzelm@24830
   463
              (if U = T then x else y) :: rename_params ys
wenzelm@24830
   464
          | rename_params ((y, _) :: ys) = y :: rename_params ys;
wenzelm@24830
   465
        fun rename_asm A =
wenzelm@24830
   466
          let
wenzelm@24830
   467
            val xs = rename_params (Logic.strip_params A);
wenzelm@24830
   468
            val xs' =
wenzelm@24830
   469
              (case List.filter (equal x) xs of
wenzelm@24830
   470
                [] => xs | [_] => xs | _ => index 1 xs);
wenzelm@24830
   471
          in Logic.list_rename_params (xs', A) end;
wenzelm@24830
   472
        fun rename_prop p =
wenzelm@24830
   473
          let val (As, C) = Logic.strip_horn p
wenzelm@24830
   474
          in Logic.list_implies (map rename_asm As, C) end;
wenzelm@24830
   475
        val cp' = cterm_fun rename_prop (Thm.cprop_of thm);
wenzelm@24830
   476
        val thm' = Thm.equal_elim (Thm.reflexive cp') thm;
wenzelm@24830
   477
      in [RuleCases.save thm thm'] end
wenzelm@24830
   478
  | special_rename_params _ _ ths = ths;
wenzelm@24830
   479
wenzelm@24830
   480
wenzelm@24830
   481
(* fix_tac *)
wenzelm@24830
   482
wenzelm@24830
   483
local
wenzelm@24830
   484
wenzelm@24830
   485
fun goal_prefix k ((c as Const ("all", _)) $ Abs (a, T, B)) = c $ Abs (a, T, goal_prefix k B)
wenzelm@24830
   486
  | goal_prefix 0 _ = Term.dummy_pattern propT
wenzelm@24830
   487
  | goal_prefix k ((c as Const ("==>", _)) $ A $ B) = c $ A $ goal_prefix (k - 1) B
wenzelm@24830
   488
  | goal_prefix _ _ = Term.dummy_pattern propT;
wenzelm@24830
   489
wenzelm@24830
   490
fun goal_params k (Const ("all", _) $ Abs (_, _, B)) = goal_params k B + 1
wenzelm@24830
   491
  | goal_params 0 _ = 0
wenzelm@24830
   492
  | goal_params k (Const ("==>", _) $ _ $ B) = goal_params (k - 1) B
wenzelm@24830
   493
  | goal_params _ _ = 0;
wenzelm@24830
   494
wenzelm@24830
   495
fun meta_spec_tac ctxt n (x, T) = SUBGOAL (fn (goal, i) =>
wenzelm@24830
   496
  let
wenzelm@24830
   497
    val thy = ProofContext.theory_of ctxt;
wenzelm@24830
   498
    val cert = Thm.cterm_of thy;
wenzelm@24830
   499
    val certT = Thm.ctyp_of thy;
wenzelm@24830
   500
wenzelm@24830
   501
    val v = Free (x, T);
wenzelm@24830
   502
    fun spec_rule prfx (xs, body) =
wenzelm@24830
   503
      @{thm Pure.meta_spec}
wenzelm@24830
   504
      |> Thm.rename_params_rule ([ProofContext.revert_skolem ctxt x], 1)
wenzelm@24830
   505
      |> Thm.lift_rule (cert prfx)
wenzelm@24830
   506
      |> `(Thm.prop_of #> Logic.strip_assums_concl)
wenzelm@24830
   507
      |-> (fn pred $ arg =>
wenzelm@24830
   508
        Drule.cterm_instantiate
wenzelm@24830
   509
          [(cert (Term.head_of pred), cert (Logic.rlist_abs (xs, body))),
wenzelm@24830
   510
           (cert (Term.head_of arg), cert (Logic.rlist_abs (xs, v)))]);
wenzelm@24830
   511
wenzelm@24830
   512
    fun goal_concl k xs (Const ("all", _) $ Abs (a, T, B)) = goal_concl k ((a, T) :: xs) B
wenzelm@24830
   513
      | goal_concl 0 xs B =
wenzelm@24830
   514
          if not (Term.exists_subterm (fn t => t aconv v) B) then NONE
wenzelm@24830
   515
          else SOME (xs, Term.absfree (x, T, Term.incr_boundvars 1 B))
wenzelm@24830
   516
      | goal_concl k xs (Const ("==>", _) $ _ $ B) = goal_concl (k - 1) xs B
wenzelm@24830
   517
      | goal_concl _ _ _ = NONE;
wenzelm@24830
   518
  in
wenzelm@24830
   519
    (case goal_concl n [] goal of
wenzelm@24830
   520
      SOME concl =>
wenzelm@24830
   521
        (compose_tac (false, spec_rule (goal_prefix n goal) concl, 1) THEN' rtac asm_rl) i
wenzelm@24830
   522
    | NONE => all_tac)
wenzelm@24830
   523
  end);
wenzelm@24830
   524
wenzelm@24830
   525
fun miniscope_tac p =
wenzelm@24830
   526
  CONVERSION (Conv.forall_conv p (MetaSimplifier.rewrite true [Thm.symmetric Drule.norm_hhf_eq]));
wenzelm@24830
   527
wenzelm@24830
   528
in
wenzelm@24830
   529
wenzelm@24830
   530
fun fix_tac _ _ [] = K all_tac
wenzelm@24830
   531
  | fix_tac ctxt n xs = SUBGOAL (fn (goal, i) =>
wenzelm@24830
   532
     (EVERY' (map (meta_spec_tac ctxt n) xs) THEN'
wenzelm@24830
   533
      (miniscope_tac (goal_params n goal))) i);
wenzelm@24830
   534
wenzelm@24830
   535
end;
wenzelm@24830
   536
wenzelm@24830
   537
wenzelm@24830
   538
(* add_defs *)
wenzelm@24830
   539
wenzelm@24830
   540
fun add_defs def_insts =
wenzelm@24830
   541
  let
wenzelm@24830
   542
    fun add (SOME (SOME x, t)) ctxt =
wenzelm@24830
   543
          let val ([(lhs, (_, th))], ctxt') = LocalDefs.add_defs [((x, NoSyn), (("", []), t))] ctxt
wenzelm@24830
   544
          in ((SOME lhs, [th]), ctxt') end
wenzelm@24830
   545
      | add (SOME (NONE, t)) ctxt = ((SOME t, []), ctxt)
wenzelm@24830
   546
      | add NONE ctxt = ((NONE, []), ctxt);
wenzelm@24830
   547
  in fold_map add def_insts #> apfst (split_list #> apsnd flat) end;
wenzelm@24830
   548
wenzelm@24830
   549
wenzelm@24830
   550
(* induct_tac *)
wenzelm@24830
   551
wenzelm@24830
   552
(*
wenzelm@24830
   553
  rule selection scheme:
wenzelm@24830
   554
    `x:A` induct ...     - set induction
wenzelm@24830
   555
          induct x       - type induction
wenzelm@24830
   556
    ...   induct ... r   - explicit rule
wenzelm@24830
   557
*)
wenzelm@24830
   558
wenzelm@24830
   559
local
wenzelm@24830
   560
wenzelm@24830
   561
fun get_inductT ctxt insts =
wenzelm@24830
   562
  fold_rev multiply (insts |> map_filter (fn [] => NONE | ts => List.last ts)
wenzelm@24830
   563
    |> map (find_inductT ctxt o Term.fastype_of)) [[]]
wenzelm@24830
   564
  |> filter_out (forall PureThy.is_internal);
wenzelm@24830
   565
wenzelm@24830
   566
fun get_inductS ctxt (fact :: _) = map single (find_inductS ctxt (Thm.concl_of fact))
wenzelm@24830
   567
  | get_inductS _ _ = [];
wenzelm@24830
   568
wenzelm@24830
   569
in
wenzelm@24830
   570
wenzelm@24830
   571
fun induct_tac ctxt is_open def_insts arbitrary taking opt_rule facts =
wenzelm@24830
   572
  let
wenzelm@24830
   573
    val _ = warn_open is_open;
wenzelm@24830
   574
    val thy = ProofContext.theory_of ctxt;
wenzelm@24830
   575
    val cert = Thm.cterm_of thy;
wenzelm@24830
   576
wenzelm@24830
   577
    val ((insts, defs), defs_ctxt) = fold_map add_defs def_insts ctxt |>> split_list;
wenzelm@24830
   578
    val atomized_defs = map (map (Conv.fconv_rule ObjectLogic.atomize)) defs;
wenzelm@24830
   579
wenzelm@24830
   580
    fun inst_rule (concls, r) =
wenzelm@24830
   581
      (if null insts then `RuleCases.get r
wenzelm@24830
   582
       else (align_left "Rule has fewer conclusions than arguments given"
wenzelm@24830
   583
          (map Logic.strip_imp_concl (Logic.dest_conjunctions (Thm.concl_of r))) insts
wenzelm@24830
   584
        |> maps (prep_inst thy align_right (atomize_term thy))
wenzelm@24830
   585
        |> Drule.cterm_instantiate) r |> pair (RuleCases.get r))
wenzelm@24830
   586
      |> (fn ((cases, consumes), th) => (((cases, concls), consumes), th));
wenzelm@24830
   587
wenzelm@24830
   588
    val ruleq =
wenzelm@24830
   589
      (case opt_rule of
wenzelm@24830
   590
        SOME rs => Seq.single (inst_rule (RuleCases.strict_mutual_rule ctxt rs))
wenzelm@24830
   591
      | NONE =>
wenzelm@24830
   592
          (get_inductS ctxt facts @
wenzelm@24830
   593
            map (special_rename_params defs_ctxt insts) (get_inductT ctxt insts))
wenzelm@24830
   594
          |> map_filter (RuleCases.mutual_rule ctxt)
wenzelm@24830
   595
          |> tap (trace_rules ctxt inductN o map #2)
wenzelm@24830
   596
          |> Seq.of_list |> Seq.maps (Seq.try inst_rule));
wenzelm@24830
   597
wenzelm@24830
   598
    fun rule_cases rule =
wenzelm@24830
   599
      RuleCases.make_nested is_open (Thm.prop_of rule) (rulified_term rule);
wenzelm@24830
   600
  in
wenzelm@24830
   601
    (fn i => fn st =>
wenzelm@24830
   602
      ruleq
wenzelm@24830
   603
      |> Seq.maps (RuleCases.consume (flat defs) facts)
wenzelm@24830
   604
      |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
wenzelm@24830
   605
        (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
wenzelm@24830
   606
          (CONJUNCTS (ALLGOALS
wenzelm@24830
   607
            (Method.insert_tac (more_facts @ nth_list atomized_defs (j - 1))
wenzelm@24830
   608
              THEN' fix_tac defs_ctxt
wenzelm@24830
   609
                (nth concls (j - 1) + more_consumes)
wenzelm@24830
   610
                (nth_list arbitrary (j - 1))))
wenzelm@24830
   611
          THEN' inner_atomize_tac) j))
wenzelm@24830
   612
        THEN' atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@24830
   613
            guess_instance (internalize more_consumes rule) i st'
wenzelm@24830
   614
            |> Seq.map (rule_instance thy taking)
wenzelm@24830
   615
            |> Seq.maps (fn rule' =>
wenzelm@24830
   616
              CASES (rule_cases rule' cases)
wenzelm@24830
   617
                (Tactic.rtac rule' i THEN
wenzelm@24830
   618
                  PRIMITIVE (singleton (ProofContext.export defs_ctxt ctxt))) st'))))
wenzelm@24830
   619
    THEN_ALL_NEW_CASES rulify_tac
wenzelm@24830
   620
  end;
wenzelm@24830
   621
wenzelm@24830
   622
end;
wenzelm@24830
   623
wenzelm@24830
   624
wenzelm@24830
   625
wenzelm@24830
   626
(** coinduct method **)
wenzelm@24830
   627
wenzelm@24830
   628
(*
wenzelm@24830
   629
  rule selection scheme:
wenzelm@24830
   630
    goal "x:A" coinduct ...   - set coinduction
wenzelm@24830
   631
               coinduct x     - type coinduction
wenzelm@24830
   632
               coinduct ... r - explicit rule
wenzelm@24830
   633
*)
wenzelm@24830
   634
wenzelm@24830
   635
local
wenzelm@24830
   636
wenzelm@24830
   637
fun get_coinductT ctxt (SOME t :: _) = find_coinductT ctxt (Term.fastype_of t)
wenzelm@24830
   638
  | get_coinductT _ _ = [];
wenzelm@24830
   639
wenzelm@24830
   640
fun get_coinductS ctxt goal = find_coinductS ctxt (Logic.strip_assums_concl goal);
wenzelm@24830
   641
wenzelm@24830
   642
in
wenzelm@24830
   643
wenzelm@24830
   644
fun coinduct_tac ctxt is_open inst taking opt_rule facts =
wenzelm@24830
   645
  let
wenzelm@24830
   646
    val _ = warn_open is_open;
wenzelm@24830
   647
    val thy = ProofContext.theory_of ctxt;
wenzelm@24830
   648
    val cert = Thm.cterm_of thy;
wenzelm@24830
   649
wenzelm@24830
   650
    fun inst_rule r =
wenzelm@24830
   651
      if null inst then `RuleCases.get r
wenzelm@24830
   652
      else Drule.cterm_instantiate (prep_inst thy align_left I (Thm.concl_of r, inst)) r
wenzelm@24830
   653
        |> pair (RuleCases.get r);
wenzelm@24830
   654
wenzelm@24830
   655
    fun ruleq goal =
wenzelm@24830
   656
      (case opt_rule of
wenzelm@24830
   657
        SOME r => Seq.single (inst_rule r)
wenzelm@24830
   658
      | NONE =>
wenzelm@24830
   659
          (get_coinductS ctxt goal @ get_coinductT ctxt inst)
wenzelm@24830
   660
          |> tap (trace_rules ctxt coinductN)
wenzelm@24830
   661
          |> Seq.of_list |> Seq.maps (Seq.try inst_rule));
wenzelm@24830
   662
  in
wenzelm@24830
   663
    SUBGOAL_CASES (fn (goal, i) => fn st =>
wenzelm@24830
   664
      ruleq goal
wenzelm@24830
   665
      |> Seq.maps (RuleCases.consume [] facts)
wenzelm@24830
   666
      |> Seq.maps (fn ((cases, (_, more_facts)), rule) =>
wenzelm@24830
   667
        guess_instance rule i st
wenzelm@24830
   668
        |> Seq.map (rule_instance thy taking)
wenzelm@24830
   669
        |> Seq.maps (fn rule' =>
wenzelm@24830
   670
          CASES (make_cases is_open rule' cases)
wenzelm@24830
   671
            (Method.insert_tac more_facts i THEN Tactic.rtac rule' i) st)))
wenzelm@24830
   672
  end;
wenzelm@24830
   673
wenzelm@24830
   674
end;
wenzelm@24830
   675
wenzelm@24830
   676
wenzelm@24830
   677
wenzelm@24830
   678
(** concrete syntax **)
wenzelm@24830
   679
wenzelm@24830
   680
val openN = "open";
wenzelm@24830
   681
val arbitraryN = "arbitrary";
wenzelm@24830
   682
val takingN = "taking";
wenzelm@24830
   683
val ruleN = "rule";
wenzelm@24830
   684
wenzelm@24830
   685
local
wenzelm@24830
   686
wenzelm@24830
   687
fun single_rule [rule] = rule
wenzelm@24830
   688
  | single_rule _ = error "Single rule expected";
wenzelm@24830
   689
wenzelm@24830
   690
fun named_rule k arg get =
wenzelm@24830
   691
  Scan.lift (Args.$$$ k -- Args.colon) |-- Scan.repeat arg :|--
wenzelm@24830
   692
    (fn names => Scan.peek (fn context => Scan.succeed (names |> map (fn name =>
wenzelm@24830
   693
      (case get (Context.proof_of context) name of SOME x => x
wenzelm@24830
   694
      | NONE => error ("No rule for " ^ k ^ " " ^ quote name))))));
wenzelm@24830
   695
wenzelm@24830
   696
fun rule get_type get_set =
wenzelm@24830
   697
  named_rule typeN Args.tyname get_type ||
wenzelm@24830
   698
  named_rule setN Args.const get_set ||
wenzelm@24830
   699
  Scan.lift (Args.$$$ ruleN -- Args.colon) |-- Attrib.thms;
wenzelm@24830
   700
wenzelm@24830
   701
val cases_rule = rule lookup_casesT lookup_casesS >> single_rule;
wenzelm@24830
   702
val induct_rule = rule lookup_inductT lookup_inductS;
wenzelm@24830
   703
val coinduct_rule = rule lookup_coinductT lookup_coinductS >> single_rule;
wenzelm@24830
   704
wenzelm@24830
   705
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
wenzelm@24830
   706
wenzelm@24830
   707
val def_inst =
wenzelm@24830
   708
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@24830
   709
      -- Args.term) >> SOME ||
wenzelm@24830
   710
    inst >> Option.map (pair NONE);
wenzelm@24830
   711
wenzelm@24830
   712
val free = Scan.state -- Args.term >> (fn (_, Free v) => v | (context, t) =>
wenzelm@24830
   713
  error ("Bad free variable: " ^ ProofContext.string_of_term (Context.proof_of context) t));
wenzelm@24830
   714
wenzelm@24830
   715
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@24830
   716
  ((Args.$$$ arbitraryN || Args.$$$ takingN || Args.$$$ typeN ||
wenzelm@24830
   717
    Args.$$$ setN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@24830
   718
wenzelm@24830
   719
val arbitrary = Scan.optional (Scan.lift (Args.$$$ arbitraryN -- Args.colon) |--
wenzelm@24830
   720
  Args.and_list1 (Scan.repeat (unless_more_args free))) [];
wenzelm@24830
   721
wenzelm@24830
   722
val taking = Scan.optional (Scan.lift (Args.$$$ takingN -- Args.colon) |--
wenzelm@24830
   723
  Scan.repeat1 (unless_more_args inst)) [];
wenzelm@24830
   724
wenzelm@24830
   725
in
wenzelm@24830
   726
wenzelm@24830
   727
fun cases_meth src =
wenzelm@24830
   728
  Method.syntax (Args.mode openN --
wenzelm@24830
   729
    (Args.and_list (Scan.repeat (unless_more_args inst)) -- Scan.option cases_rule)) src
wenzelm@24830
   730
  #> (fn ((is_open, (insts, opt_rule)), ctxt) =>
wenzelm@24830
   731
    Method.METHOD_CASES (fn facts =>
wenzelm@24830
   732
      Seq.DETERM (HEADGOAL (cases_tac ctxt is_open insts opt_rule facts))));
wenzelm@24830
   733
wenzelm@24830
   734
fun induct_meth src =
wenzelm@24830
   735
  Method.syntax (Args.mode openN --
wenzelm@24830
   736
    (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
wenzelm@24830
   737
    (arbitrary -- taking -- Scan.option induct_rule))) src
wenzelm@24830
   738
  #> (fn ((is_open, (insts, ((arbitrary, taking), opt_rule))), ctxt) =>
wenzelm@24830
   739
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@24830
   740
      Seq.DETERM (HEADGOAL (induct_tac ctxt is_open insts arbitrary taking opt_rule facts))));
wenzelm@24830
   741
wenzelm@24830
   742
fun coinduct_meth src =
wenzelm@24830
   743
  Method.syntax (Args.mode openN --
wenzelm@24830
   744
    (Scan.repeat (unless_more_args inst) -- taking -- Scan.option coinduct_rule)) src
wenzelm@24830
   745
  #> (fn ((is_open, ((insts, taking), opt_rule)), ctxt) =>
wenzelm@24830
   746
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@24830
   747
      Seq.DETERM (HEADGOAL (coinduct_tac ctxt is_open insts taking opt_rule facts))));
wenzelm@24830
   748
wenzelm@24830
   749
end;
wenzelm@24830
   750
wenzelm@24830
   751
wenzelm@24830
   752
wenzelm@24830
   753
(** theory setup **)
wenzelm@24830
   754
wenzelm@24830
   755
val setup =
wenzelm@24830
   756
  attrib_setup #>
wenzelm@24830
   757
  Method.add_methods
wenzelm@24830
   758
    [(casesN, cases_meth, "case analysis on types or sets"),
wenzelm@24830
   759
     (inductN, induct_meth, "induction on types or sets"),
wenzelm@24830
   760
     (coinductN, coinduct_meth, "coinduction on types or sets")];
wenzelm@24830
   761
wenzelm@24830
   762
end;