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