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