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