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