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