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