src/Provers/induct_method.ML
author wenzelm
Thu Jan 19 21:22:08 2006 +0100 (2006-01-19 ago)
changeset 18708 4b3dadb4fe33
parent 18678 dd0c569fa43d
child 18799 f137c5e971f5
permissions -rw-r--r--
setup: theory -> theory;
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 cases_default: thm
wenzelm@11670
    11
  val atomize: thm list
wenzelm@18465
    12
  val rulify: thm list
wenzelm@18465
    13
  val rulify_fallback: thm list
wenzelm@11670
    14
end;
wenzelm@11670
    15
wenzelm@11670
    16
signature INDUCT_METHOD =
wenzelm@11670
    17
sig
wenzelm@18250
    18
  val fix_tac: Proof.context -> int -> (string * typ) list -> int -> tactic
wenzelm@18287
    19
  val add_defs: (string option * term) option list -> Proof.context ->
wenzelm@18287
    20
    (term option list * thm list) * Proof.context
wenzelm@18250
    21
  val atomize_term: theory -> term -> term
wenzelm@18250
    22
  val atomize_tac: int -> tactic
wenzelm@18512
    23
  val inner_atomize_tac: int -> tactic
wenzelm@18250
    24
  val rulified_term: thm -> theory * term
wenzelm@18250
    25
  val rulify_tac: int -> tactic
wenzelm@18580
    26
  val internalize: int -> thm -> thm
wenzelm@18259
    27
  val guess_instance: thm -> int -> thm -> thm Seq.seq
wenzelm@16391
    28
  val cases_tac: Proof.context -> bool -> term option list list -> thm option ->
wenzelm@18224
    29
    thm list -> int -> cases_tactic
wenzelm@18178
    30
  val induct_tac: Proof.context -> bool -> (string option * term) option list list ->
wenzelm@18465
    31
    (string * typ) list list -> term option list -> thm list option -> thm list -> int ->
wenzelm@18465
    32
    cases_tactic
wenzelm@18235
    33
  val coinduct_tac: Proof.context -> bool -> term option list -> term option list ->
wenzelm@18235
    34
    thm option -> thm list -> int -> cases_tactic
wenzelm@18708
    35
  val setup: theory -> theory
wenzelm@11670
    36
end;
wenzelm@11670
    37
wenzelm@11670
    38
functor InductMethodFun(Data: INDUCT_METHOD_DATA): INDUCT_METHOD =
wenzelm@11670
    39
struct
wenzelm@11670
    40
wenzelm@18465
    41
val meta_spec = thm "Pure.meta_spec";
wenzelm@18465
    42
val all_conjunction = thm "Pure.all_conjunction";
wenzelm@18465
    43
val imp_conjunction = thm "Pure.imp_conjunction";
wenzelm@18465
    44
val conjunction_imp = thm "Pure.conjunction_imp";
wenzelm@18465
    45
val conjunction_assoc = thm "Pure.conjunction_assoc";
wenzelm@18465
    46
val conjunction_congs = [all_conjunction, imp_conjunction];
wenzelm@18465
    47
wenzelm@18465
    48
wenzelm@11670
    49
wenzelm@11670
    50
(** misc utils **)
wenzelm@11670
    51
wenzelm@18287
    52
(* alignment *)
wenzelm@11670
    53
wenzelm@11670
    54
fun align_left msg xs ys =
wenzelm@11670
    55
  let val m = length xs and n = length ys
wenzelm@18678
    56
  in if m < n then error msg else (Library.take (n, xs) ~~ ys) end;
wenzelm@11670
    57
wenzelm@11670
    58
fun align_right msg xs ys =
wenzelm@11670
    59
  let val m = length xs and n = length ys
wenzelm@18678
    60
  in if m < n then error msg else (Library.drop (m - n, xs) ~~ ys) end;
wenzelm@11670
    61
wenzelm@11670
    62
wenzelm@11735
    63
(* prep_inst *)
wenzelm@11670
    64
wenzelm@18205
    65
fun prep_inst thy align tune (tm, ts) =
wenzelm@11670
    66
  let
wenzelm@18205
    67
    val cert = Thm.cterm_of thy;
skalberg@15531
    68
    fun prep_var (x, SOME t) =
wenzelm@11670
    69
          let
wenzelm@11670
    70
            val cx = cert x;
wenzelm@18147
    71
            val {T = xT, thy, ...} = Thm.rep_cterm cx;
wenzelm@12799
    72
            val ct = cert (tune t);
wenzelm@11670
    73
          in
wenzelm@18147
    74
            if Sign.typ_instance thy (#T (Thm.rep_cterm ct), xT) then SOME (cx, ct)
wenzelm@18678
    75
            else error (Pretty.string_of (Pretty.block
wenzelm@11735
    76
             [Pretty.str "Ill-typed instantiation:", Pretty.fbrk,
wenzelm@11735
    77
              Display.pretty_cterm ct, Pretty.str " ::", Pretty.brk 1,
wenzelm@11735
    78
              Display.pretty_ctyp (#T (Thm.crep_cterm ct))]))
wenzelm@11670
    79
          end
skalberg@15531
    80
      | prep_var (_, NONE) = NONE;
wenzelm@11735
    81
    val xs = InductAttrib.vars_of tm;
wenzelm@11670
    82
  in
wenzelm@11735
    83
    align "Rule has fewer variables than instantiations given" xs ts
skalberg@15570
    84
    |> List.mapPartial prep_var
wenzelm@11670
    85
  end;
wenzelm@11670
    86
wenzelm@11670
    87
wenzelm@18205
    88
(* trace_rules *)
wenzelm@18205
    89
wenzelm@18205
    90
fun trace_rules _ kind [] = error ("Unable to figure out " ^ kind ^ " rule")
wenzelm@18205
    91
  | trace_rules ctxt _ rules = Method.trace ctxt rules;
wenzelm@18205
    92
wenzelm@18205
    93
wenzelm@18205
    94
(* make_cases *)
wenzelm@18178
    95
wenzelm@18224
    96
fun make_cases is_open rule =
wenzelm@18602
    97
  RuleCases.make_common is_open (Thm.theory_of_thm rule, Thm.prop_of rule);
wenzelm@18224
    98
wenzelm@18178
    99
fun warn_open true = warning "Encountered open rule cases -- deprecated"
wenzelm@18178
   100
  | warn_open false = ();
wenzelm@18178
   101
wenzelm@18178
   102
wenzelm@11670
   103
wenzelm@11670
   104
(** cases method **)
wenzelm@11670
   105
wenzelm@11670
   106
(*
wenzelm@11735
   107
  rule selection scheme:
wenzelm@18178
   108
          cases         - default case split
wenzelm@18178
   109
    `x:A` cases ...     - set cases
wenzelm@11735
   110
          cases t       - type cases
wenzelm@18178
   111
    ...   cases ... r   - explicit rule
wenzelm@11670
   112
*)
wenzelm@11670
   113
wenzelm@11670
   114
local
wenzelm@11670
   115
wenzelm@18178
   116
fun find_casesT ctxt ((SOME t :: _) :: _) = InductAttrib.find_casesT ctxt (Term.fastype_of t)
wenzelm@11735
   117
  | find_casesT _ _ = [];
wenzelm@11735
   118
wenzelm@18224
   119
fun find_casesS ctxt (fact :: _) = InductAttrib.find_casesS ctxt (Thm.concl_of fact)
wenzelm@11735
   120
  | find_casesS _ _ = [];
wenzelm@11735
   121
wenzelm@16391
   122
in
wenzelm@16391
   123
wenzelm@16391
   124
fun cases_tac ctxt is_open insts opt_rule facts =
wenzelm@11670
   125
  let
wenzelm@18224
   126
    val _ = warn_open is_open;
wenzelm@18147
   127
    val thy = ProofContext.theory_of ctxt;
wenzelm@18147
   128
    val cert = Thm.cterm_of thy;
wenzelm@11670
   129
wenzelm@11735
   130
    fun inst_rule r =
wenzelm@18224
   131
      if null insts then `RuleCases.get r
wenzelm@11735
   132
      else (align_left "Rule has fewer premises than arguments given" (Thm.prems_of r) insts
wenzelm@18205
   133
        |> (List.concat o map (prep_inst thy align_left I))
wenzelm@18224
   134
        |> Drule.cterm_instantiate) r |> pair (RuleCases.get r);
wenzelm@11670
   135
wenzelm@11735
   136
    val ruleq =
wenzelm@12852
   137
      (case opt_rule of
wenzelm@18205
   138
        SOME r => Seq.single (inst_rule r)
wenzelm@18205
   139
      | NONE =>
wenzelm@18205
   140
          (find_casesS ctxt facts @ find_casesT ctxt insts @ [Data.cases_default])
wenzelm@18205
   141
          |> tap (trace_rules ctxt InductAttrib.casesN)
wenzelm@18224
   142
          |> Seq.of_list |> Seq.maps (Seq.try inst_rule));
wenzelm@18178
   143
  in
wenzelm@18178
   144
    fn i => fn st =>
wenzelm@18224
   145
      ruleq
wenzelm@18235
   146
      |> Seq.maps (RuleCases.consume [] facts)
wenzelm@18224
   147
      |> Seq.maps (fn ((cases, (_, more_facts)), rule) =>
wenzelm@18224
   148
        CASES (make_cases is_open rule cases)
wenzelm@18224
   149
          (Method.insert_tac more_facts i THEN Tactic.rtac rule i) st)
wenzelm@18178
   150
  end;
wenzelm@11670
   151
wenzelm@11670
   152
end;
wenzelm@11670
   153
wenzelm@11670
   154
wenzelm@11670
   155
wenzelm@11670
   156
(** induct method **)
wenzelm@11670
   157
wenzelm@18465
   158
(* atomize *)
wenzelm@18465
   159
wenzelm@18512
   160
fun atomize_term thy =
wenzelm@18512
   161
  MetaSimplifier.rewrite_term thy Data.atomize []
wenzelm@18465
   162
  #> ObjectLogic.drop_judgment thy;
wenzelm@18465
   163
wenzelm@18512
   164
val atomize_cterm = Tactic.rewrite true Data.atomize;
wenzelm@18465
   165
wenzelm@18512
   166
val atomize_tac = Tactic.rewrite_goal_tac Data.atomize;
wenzelm@18465
   167
wenzelm@18512
   168
val inner_atomize_tac =
wenzelm@18512
   169
  Tactic.rewrite_goal_tac (map Thm.symmetric conjunction_congs) THEN' atomize_tac;
wenzelm@18465
   170
wenzelm@18465
   171
wenzelm@18465
   172
(* rulify *)
wenzelm@18465
   173
wenzelm@18465
   174
fun rulify_term thy =
wenzelm@18465
   175
  MetaSimplifier.rewrite_term thy (Data.rulify @ conjunction_congs) [] #>
wenzelm@18465
   176
  MetaSimplifier.rewrite_term thy Data.rulify_fallback [];
wenzelm@18465
   177
wenzelm@18465
   178
fun rulified_term thm =
wenzelm@18465
   179
  let
wenzelm@18465
   180
    val thy = Thm.theory_of_thm thm;
wenzelm@18465
   181
    val rulify = rulify_term thy;
wenzelm@18465
   182
    val (As, B) = Logic.strip_horn (Thm.prop_of thm);
wenzelm@18465
   183
  in (thy, Logic.list_implies (map rulify As, rulify B)) end;
wenzelm@18465
   184
wenzelm@18465
   185
val curry_prems_tac =
wenzelm@18465
   186
  let
wenzelm@18465
   187
    val rule = conjunction_imp;
wenzelm@18465
   188
    val thy = Thm.theory_of_thm rule;
wenzelm@18465
   189
    val proc = Simplifier.simproc_i thy "curry_prems"
wenzelm@18465
   190
      [#1 (Logic.dest_equals (Thm.prop_of rule))]
wenzelm@18465
   191
      (fn _ => fn ss => fn _ =>  (*WORKAROUND: prevent descending into meta conjunctions*)
wenzelm@18465
   192
        if exists (equal propT o #2 o #1) (#2 (#bounds (#1 (Simplifier.rep_ss ss))))
wenzelm@18465
   193
        then NONE else SOME rule);
wenzelm@18465
   194
    val ss = MetaSimplifier.theory_context thy Simplifier.empty_ss addsimprocs [proc];
wenzelm@18465
   195
  in Simplifier.full_simp_tac ss end;
wenzelm@18465
   196
wenzelm@18465
   197
val rulify_tac =
wenzelm@18465
   198
  Tactic.rewrite_goal_tac (Data.rulify @ conjunction_congs) THEN'
wenzelm@18465
   199
  Tactic.rewrite_goal_tac Data.rulify_fallback THEN'
wenzelm@18465
   200
  Tactic.conjunction_tac THEN_ALL_NEW
wenzelm@18465
   201
  (curry_prems_tac THEN' Tactic.norm_hhf_tac);
wenzelm@18465
   202
wenzelm@18465
   203
wenzelm@18465
   204
(* prepare rule *)
wenzelm@18465
   205
wenzelm@18465
   206
fun rule_instance thy inst rule =
wenzelm@18465
   207
  Drule.cterm_instantiate (prep_inst thy align_left I (Thm.prop_of rule, inst)) rule;
wenzelm@18465
   208
wenzelm@18512
   209
fun internalize k th =
wenzelm@18465
   210
  th |> Thm.permute_prems 0 k
wenzelm@18512
   211
  |> Drule.fconv_rule (Drule.concl_conv (Thm.nprems_of th - k) atomize_cterm);
wenzelm@18465
   212
wenzelm@18465
   213
wenzelm@18465
   214
(* guess rule instantiation -- cannot handle pending goal parameters *)
wenzelm@18465
   215
wenzelm@18465
   216
local
wenzelm@18465
   217
wenzelm@18465
   218
fun dest_env thy (env as Envir.Envir {iTs, ...}) =
wenzelm@18465
   219
  let
wenzelm@18465
   220
    val cert = Thm.cterm_of thy;
wenzelm@18465
   221
    val certT = Thm.ctyp_of thy;
wenzelm@18465
   222
    val pairs = Envir.alist_of env;
wenzelm@18465
   223
    val ts = map (cert o Envir.norm_term env o #2 o #2) pairs;
wenzelm@18465
   224
    val xs = map2 (curry (cert o Var)) (map #1 pairs) (map (#T o Thm.rep_cterm) ts);
wenzelm@18465
   225
  in (map (fn (xi, (S, T)) => (certT (TVar (xi, S)), certT T)) (Vartab.dest iTs), xs ~~ ts) end;
wenzelm@18465
   226
wenzelm@18465
   227
in
wenzelm@18465
   228
wenzelm@18465
   229
fun guess_instance rule i st =
wenzelm@18465
   230
  let
wenzelm@18465
   231
    val {thy, maxidx, ...} = Thm.rep_thm st;
wenzelm@18465
   232
    val goal = Thm.term_of (Thm.cprem_of st i);  (*exception Subscript*)
wenzelm@18465
   233
    val params = rev (rename_wrt_term goal (Logic.strip_params goal));
wenzelm@18465
   234
  in
wenzelm@18465
   235
    if not (null params) then
wenzelm@18465
   236
      (warning ("Cannot determine rule instantiation due to pending parameter(s): " ^
wenzelm@18465
   237
        commas_quote (map (Sign.string_of_term thy o Syntax.mark_boundT) params));
wenzelm@18465
   238
      Seq.single rule)
wenzelm@18465
   239
    else
wenzelm@18465
   240
      let
wenzelm@18465
   241
        val rule' = Thm.incr_indexes (maxidx + 1) rule;
wenzelm@18465
   242
        val concl = Logic.strip_assums_concl goal;
wenzelm@18465
   243
      in
wenzelm@18465
   244
        Unify.smash_unifiers (thy, Envir.empty (#maxidx (Thm.rep_thm rule')),
wenzelm@18465
   245
          [(Thm.concl_of rule', concl)])
wenzelm@18465
   246
        |> Seq.map (fn env => Drule.instantiate (dest_env thy env) rule')
wenzelm@18465
   247
      end
wenzelm@18465
   248
  end handle Subscript => Seq.empty;
wenzelm@18465
   249
wenzelm@18465
   250
end;
wenzelm@18465
   251
wenzelm@18465
   252
wenzelm@18465
   253
(* special renaming of rule parameters *)
wenzelm@18465
   254
wenzelm@18465
   255
fun special_rename_params ctxt [[SOME (Free (z, Type (T, _)))]] [thm] =
wenzelm@18465
   256
      let
wenzelm@18465
   257
        val x = ProofContext.revert_skolem ctxt z;
wenzelm@18465
   258
        fun index i [] = []
wenzelm@18465
   259
          | index i (y :: ys) =
wenzelm@18465
   260
              if x = y then x ^ string_of_int i :: index (i + 1) ys
wenzelm@18465
   261
              else y :: index i ys;
wenzelm@18465
   262
        fun rename_params [] = []
wenzelm@18465
   263
          | rename_params ((y, Type (U, _)) :: ys) =
wenzelm@18465
   264
              (if U = T then x else y) :: rename_params ys
wenzelm@18465
   265
          | rename_params ((y, _) :: ys) = y :: rename_params ys;
wenzelm@18465
   266
        fun rename_asm A =
wenzelm@18465
   267
          let
wenzelm@18465
   268
            val xs = rename_params (Logic.strip_params A);
wenzelm@18465
   269
            val xs' =
wenzelm@18465
   270
              (case List.filter (equal x) xs of
wenzelm@18465
   271
                [] => xs | [_] => xs | _ => index 1 xs);
wenzelm@18465
   272
          in Logic.list_rename_params (xs', A) end;
wenzelm@18465
   273
        fun rename_prop p =
wenzelm@18465
   274
          let val (As, C) = Logic.strip_horn p
wenzelm@18465
   275
          in Logic.list_implies (map rename_asm As, C) end;
wenzelm@18465
   276
        val cp' = cterm_fun rename_prop (Thm.cprop_of thm);
wenzelm@18465
   277
        val thm' = Thm.equal_elim (Thm.reflexive cp') thm;
wenzelm@18465
   278
      in [RuleCases.save thm thm'] end
wenzelm@18465
   279
  | special_rename_params _ _ ths = ths;
wenzelm@18465
   280
wenzelm@18465
   281
wenzelm@18235
   282
(* fix_tac *)
wenzelm@11670
   283
wenzelm@18250
   284
local
wenzelm@18240
   285
wenzelm@18250
   286
fun goal_prefix k ((c as Const ("all", _)) $ Abs (a, T, B)) = c $ Abs (a, T, goal_prefix k B)
wenzelm@18250
   287
  | goal_prefix 0 _ = Term.dummy_pattern propT
wenzelm@18250
   288
  | goal_prefix k ((c as Const ("==>", _)) $ A $ B) = c $ A $ goal_prefix (k - 1) B
wenzelm@18250
   289
  | goal_prefix _ _ = Term.dummy_pattern propT;
wenzelm@18250
   290
wenzelm@18250
   291
fun goal_params k (Const ("all", _) $ Abs (_, _, B)) = goal_params k B + 1
wenzelm@18250
   292
  | goal_params 0 _ = 0
wenzelm@18250
   293
  | goal_params k (Const ("==>", _) $ _ $ B) = goal_params (k - 1) B
wenzelm@18250
   294
  | goal_params _ _ = 0;
wenzelm@11670
   295
wenzelm@18250
   296
fun meta_spec_tac ctxt n (x, T) = SUBGOAL (fn (goal, i) =>
wenzelm@18178
   297
  let
wenzelm@18250
   298
    val thy = ProofContext.theory_of ctxt;
wenzelm@18178
   299
    val cert = Thm.cterm_of thy;
wenzelm@18178
   300
    val certT = Thm.ctyp_of thy;
wenzelm@18178
   301
wenzelm@18178
   302
    val v = Free (x, T);
wenzelm@18250
   303
    fun spec_rule prfx (xs, body) =
wenzelm@18250
   304
      meta_spec
wenzelm@18250
   305
      |> Thm.rename_params_rule ([ProofContext.revert_skolem ctxt x], 1)
wenzelm@18250
   306
      |> Thm.lift_rule (cert prfx)
wenzelm@18250
   307
      |> `(Thm.prop_of #> Logic.strip_assums_concl)
wenzelm@18250
   308
      |-> (fn pred $ arg =>
wenzelm@18250
   309
        Drule.cterm_instantiate
wenzelm@18250
   310
          [(cert (Term.head_of pred), cert (Unify.rlist_abs (xs, body))),
wenzelm@18250
   311
           (cert (Term.head_of arg), cert (Unify.rlist_abs (xs, v)))]);
wenzelm@18250
   312
wenzelm@18250
   313
    fun goal_concl k xs (Const ("all", _) $ Abs (a, T, B)) = goal_concl k ((a, T) :: xs) B
wenzelm@18250
   314
      | goal_concl 0 xs B =
wenzelm@18250
   315
          if not (Term.exists_subterm (fn t => t aconv v) B) then NONE
wenzelm@18250
   316
          else SOME (xs, Term.absfree (x, T, Term.incr_boundvars 1 B))
wenzelm@18250
   317
      | goal_concl k xs (Const ("==>", _) $ _ $ B) = goal_concl (k - 1) xs B
wenzelm@18250
   318
      | goal_concl _ _ _ = NONE;
wenzelm@18205
   319
  in
wenzelm@18250
   320
    (case goal_concl n [] goal of
wenzelm@18250
   321
      SOME concl =>
wenzelm@18250
   322
        (compose_tac (false, spec_rule (goal_prefix n goal) concl, 1) THEN' rtac asm_rl) i
wenzelm@18631
   323
    | NONE => all_tac)
wenzelm@18250
   324
  end);
wenzelm@18250
   325
wenzelm@18465
   326
fun miniscope_tac p i = PRIMITIVE (Drule.fconv_rule
wenzelm@18250
   327
  (Drule.goals_conv (Library.equal i)
wenzelm@18465
   328
    (Drule.forall_conv p (Tactic.rewrite true [Thm.symmetric Drule.norm_hhf_eq]))));
wenzelm@18178
   329
wenzelm@18178
   330
in
wenzelm@18178
   331
wenzelm@18250
   332
fun fix_tac _ _ [] = K all_tac
wenzelm@18250
   333
  | fix_tac ctxt n xs = SUBGOAL (fn (goal, i) =>
wenzelm@18250
   334
     (EVERY' (map (meta_spec_tac ctxt n) xs) THEN'
wenzelm@18250
   335
      (miniscope_tac (goal_params n goal))) i);
wenzelm@18178
   336
wenzelm@18178
   337
end;
wenzelm@18178
   338
wenzelm@18178
   339
wenzelm@18235
   340
(* add_defs *)
wenzelm@18178
   341
wenzelm@18178
   342
fun add_defs def_insts =
wenzelm@18178
   343
  let
wenzelm@18178
   344
    fun add (SOME (SOME x, t)) ctxt =
wenzelm@18178
   345
          let val ((lhs, def), ctxt') = ProofContext.add_def (x, t) ctxt
wenzelm@18178
   346
          in ((SOME (Free lhs), [def]), ctxt') end
wenzelm@18178
   347
      | add (SOME (NONE, t)) ctxt = ((SOME t, []), ctxt)
wenzelm@18178
   348
      | add NONE ctxt = ((NONE, []), ctxt);
wenzelm@18178
   349
  in fold_map add def_insts #> apfst (split_list #> apsnd List.concat) end;
wenzelm@18178
   350
wenzelm@11790
   351
wenzelm@18178
   352
(* induct_tac *)
wenzelm@18178
   353
wenzelm@18178
   354
(*
wenzelm@18178
   355
  rule selection scheme:
wenzelm@18178
   356
    `x:A` induct ...     - set induction
wenzelm@18178
   357
          induct x       - type induction
wenzelm@18178
   358
    ...   induct ... r   - explicit rule
wenzelm@18178
   359
*)
wenzelm@18178
   360
wenzelm@18178
   361
local
nipkow@15235
   362
wenzelm@11735
   363
fun find_inductT ctxt insts =
wenzelm@18147
   364
  fold_rev multiply (insts |> List.mapPartial (fn [] => NONE | ts => List.last ts)
wenzelm@18205
   365
    |> map (InductAttrib.find_inductT ctxt o Term.fastype_of)) [[]]
wenzelm@18465
   366
  |> filter_out (forall Drule.is_internal);
wenzelm@11670
   367
wenzelm@18465
   368
fun find_inductS ctxt (fact :: _) = map single (InductAttrib.find_inductS ctxt (Thm.concl_of fact))
wenzelm@11735
   369
  | find_inductS _ _ = [];
wenzelm@11735
   370
wenzelm@16391
   371
in
wenzelm@11790
   372
wenzelm@18512
   373
fun induct_tac ctxt is_open def_insts fixing taking opt_rule facts =
wenzelm@11670
   374
  let
wenzelm@18224
   375
    val _ = warn_open is_open;
wenzelm@18147
   376
    val thy = ProofContext.theory_of ctxt;
wenzelm@18147
   377
    val cert = Thm.cterm_of thy;
wenzelm@11670
   378
wenzelm@18178
   379
    val ((insts, defs), defs_ctxt) = fold_map add_defs def_insts ctxt |>> split_list;
wenzelm@18259
   380
    val atomized_defs = map (map ObjectLogic.atomize_thm) defs;
wenzelm@18178
   381
wenzelm@18465
   382
    fun inst_rule (concls, r) =
wenzelm@18465
   383
      (if null insts then `RuleCases.get r
wenzelm@18512
   384
       else (align_left "Rule has fewer conclusions than arguments given"
wenzelm@18465
   385
          (map Logic.strip_imp_concl (Logic.dest_conjunctions (Thm.concl_of r))) insts
wenzelm@18512
   386
        |> (List.concat o map (prep_inst thy align_right (atomize_term thy)))
wenzelm@18465
   387
        |> Drule.cterm_instantiate) r |> pair (RuleCases.get r))
wenzelm@18465
   388
      |> (fn ((cases, consumes), th) => (((cases, concls), consumes), th));
wenzelm@11670
   389
wenzelm@11735
   390
    val ruleq =
wenzelm@11735
   391
      (case opt_rule of
wenzelm@18580
   392
        SOME rs => Seq.single (inst_rule (RuleCases.strict_mutual_rule rs))
wenzelm@18205
   393
      | NONE =>
wenzelm@18205
   394
          (find_inductS ctxt facts @
wenzelm@18205
   395
            map (special_rename_params defs_ctxt insts) (find_inductT ctxt insts))
wenzelm@18465
   396
          |> List.mapPartial RuleCases.mutual_rule
wenzelm@18465
   397
          |> tap (trace_rules ctxt InductAttrib.inductN o map #2)
wenzelm@18465
   398
          |> Seq.of_list |> Seq.maps (Seq.try inst_rule));
wenzelm@11670
   399
wenzelm@18602
   400
    fun rule_cases rule =
wenzelm@18602
   401
      RuleCases.make_nested is_open (Thm.prop_of rule) (rulified_term rule);
wenzelm@18178
   402
  in
wenzelm@18205
   403
    (fn i => fn st =>
wenzelm@18224
   404
      ruleq
wenzelm@18235
   405
      |> Seq.maps (RuleCases.consume (List.concat defs) facts)
wenzelm@18465
   406
      |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
wenzelm@18496
   407
        (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
wenzelm@18496
   408
          (CONJUNCTS (ALLGOALS
wenzelm@18465
   409
            (Method.insert_tac (more_facts @ nth_list atomized_defs (j - 1))
wenzelm@18465
   410
              THEN' fix_tac defs_ctxt
wenzelm@18465
   411
                (nth concls (j - 1) + more_consumes)
wenzelm@18465
   412
                (nth_list fixing (j - 1))))
wenzelm@18512
   413
          THEN' inner_atomize_tac) j))
wenzelm@18512
   414
        THEN' atomize_tac) i st |> Seq.maps (fn st' =>
wenzelm@18512
   415
            guess_instance (internalize more_consumes rule) i st'
wenzelm@18235
   416
            |> Seq.map (rule_instance thy taking)
wenzelm@18235
   417
            |> Seq.maps (fn rule' =>
wenzelm@18224
   418
              CASES (rule_cases rule' cases)
wenzelm@18224
   419
                (Tactic.rtac rule' i THEN
wenzelm@18224
   420
                  PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
wenzelm@18178
   421
    THEN_ALL_NEW_CASES rulify_tac
wenzelm@18178
   422
  end;
wenzelm@11670
   423
wenzelm@18205
   424
end;
wenzelm@18205
   425
wenzelm@18205
   426
wenzelm@18205
   427
wenzelm@18205
   428
(** coinduct method **)
wenzelm@18205
   429
wenzelm@18205
   430
(*
wenzelm@18205
   431
  rule selection scheme:
wenzelm@18224
   432
    goal "x:A" coinduct ...   - set coinduction
wenzelm@18224
   433
               coinduct x     - type coinduction
wenzelm@18224
   434
               coinduct ... r - explicit rule
wenzelm@18205
   435
*)
wenzelm@18205
   436
wenzelm@18205
   437
local
wenzelm@18205
   438
wenzelm@18205
   439
fun find_coinductT ctxt (SOME t :: _) = InductAttrib.find_coinductT ctxt (Term.fastype_of t)
wenzelm@18205
   440
  | find_coinductT _ _ = [];
wenzelm@18205
   441
wenzelm@18224
   442
fun find_coinductS ctxt goal = InductAttrib.find_coinductS ctxt (Logic.strip_assums_concl goal);
wenzelm@18205
   443
wenzelm@18205
   444
in
wenzelm@18205
   445
wenzelm@18235
   446
fun coinduct_tac ctxt is_open inst taking opt_rule facts =
wenzelm@18205
   447
  let
wenzelm@18224
   448
    val _ = warn_open is_open;
wenzelm@18205
   449
    val thy = ProofContext.theory_of ctxt;
wenzelm@18205
   450
    val cert = Thm.cterm_of thy;
wenzelm@18205
   451
wenzelm@18465
   452
    fun inst_rule r =
wenzelm@18465
   453
      if null inst then `RuleCases.get r
wenzelm@18465
   454
      else Drule.cterm_instantiate (prep_inst thy align_left I (Thm.concl_of r, inst)) r
wenzelm@18465
   455
        |> pair (RuleCases.get r);
wenzelm@18205
   456
wenzelm@18224
   457
    fun ruleq goal =
wenzelm@18205
   458
      (case opt_rule of
wenzelm@18465
   459
        SOME r => Seq.single (inst_rule r)
wenzelm@18205
   460
      | NONE =>
wenzelm@18224
   461
          (find_coinductS ctxt goal @ find_coinductT ctxt inst)
wenzelm@18205
   462
          |> tap (trace_rules ctxt InductAttrib.coinductN)
wenzelm@18465
   463
          |> Seq.of_list |> Seq.maps (Seq.try inst_rule));
wenzelm@18205
   464
  in
wenzelm@18224
   465
    SUBGOAL_CASES (fn (goal, i) => fn st =>
wenzelm@18224
   466
      ruleq goal
wenzelm@18235
   467
      |> Seq.maps (RuleCases.consume [] facts)
wenzelm@18224
   468
      |> Seq.maps (fn ((cases, (_, more_facts)), rule) =>
wenzelm@18259
   469
        guess_instance rule i st
wenzelm@18235
   470
        |> Seq.map (rule_instance thy taking)
wenzelm@18235
   471
        |> Seq.maps (fn rule' =>
wenzelm@18224
   472
          CASES (make_cases is_open rule' cases)
wenzelm@18224
   473
            (Method.insert_tac more_facts i THEN Tactic.rtac rule' i) st)))
wenzelm@18205
   474
  end;
wenzelm@18205
   475
wenzelm@11670
   476
end;
wenzelm@11670
   477
wenzelm@11670
   478
wenzelm@11670
   479
wenzelm@11670
   480
(** concrete syntax **)
wenzelm@11670
   481
wenzelm@11670
   482
val openN = "open";
wenzelm@18205
   483
val fixingN = "fixing";
wenzelm@18235
   484
val takingN = "taking";
wenzelm@11670
   485
val ruleN = "rule";
wenzelm@11670
   486
wenzelm@11670
   487
local
wenzelm@11670
   488
wenzelm@18465
   489
fun single_rule [rule] = rule
wenzelm@18465
   490
  | single_rule _ = error "Single rule expected";
wenzelm@18465
   491
wenzelm@15703
   492
fun named_rule k arg get =
wenzelm@18465
   493
  Scan.lift (Args.$$$ k -- Args.colon) |-- Scan.repeat arg :--
wenzelm@18465
   494
    (fn names => Scan.peek (fn ctxt => Scan.succeed (names |> map (fn name =>
wenzelm@18465
   495
      (case get ctxt name of SOME x => x
wenzelm@18465
   496
      | NONE => error ("No rule for " ^ k ^ " " ^ quote name)))))) >> #2;
wenzelm@11670
   497
wenzelm@11670
   498
fun rule get_type get_set =
wenzelm@15703
   499
  named_rule InductAttrib.typeN Args.local_tyname get_type ||
wenzelm@15703
   500
  named_rule InductAttrib.setN Args.local_const get_set ||
wenzelm@18465
   501
  Scan.lift (Args.$$$ ruleN -- Args.colon) |-- Attrib.local_thmss;
wenzelm@11670
   502
wenzelm@18465
   503
val cases_rule = rule InductAttrib.lookup_casesT InductAttrib.lookup_casesS >> single_rule;
wenzelm@11670
   504
val induct_rule = rule InductAttrib.lookup_inductT InductAttrib.lookup_inductS;
wenzelm@18465
   505
val coinduct_rule =
wenzelm@18465
   506
  rule InductAttrib.lookup_coinductT InductAttrib.lookup_coinductS >> single_rule;
wenzelm@11670
   507
wenzelm@18178
   508
val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
wenzelm@18147
   509
wenzelm@18178
   510
val def_inst =
wenzelm@18178
   511
  ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
wenzelm@18178
   512
      -- Args.local_term) >> SOME ||
wenzelm@18178
   513
    inst >> Option.map (pair NONE);
wenzelm@11670
   514
wenzelm@18147
   515
val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
wenzelm@18147
   516
  error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
wenzelm@18147
   517
wenzelm@18178
   518
fun unless_more_args scan = Scan.unless (Scan.lift
wenzelm@18235
   519
  ((Args.$$$ fixingN || Args.$$$ takingN || Args.$$$ InductAttrib.typeN ||
wenzelm@18235
   520
    Args.$$$ InductAttrib.setN || Args.$$$ ruleN) -- Args.colon)) scan;
wenzelm@18205
   521
wenzelm@18205
   522
val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
wenzelm@18205
   523
  Args.and_list1 (Scan.repeat (unless_more_args free))) [];
wenzelm@18178
   524
wenzelm@18235
   525
val taking = Scan.optional (Scan.lift (Args.$$$ takingN -- Args.colon) |--
wenzelm@18235
   526
  Scan.repeat1 (unless_more_args inst)) [];
wenzelm@18235
   527
wenzelm@11670
   528
in
wenzelm@11670
   529
wenzelm@18235
   530
fun cases_meth src =
wenzelm@18235
   531
  Method.syntax (Args.mode openN --
wenzelm@18235
   532
    (Args.and_list (Scan.repeat (unless_more_args inst)) -- Scan.option cases_rule)) src
wenzelm@18235
   533
  #> (fn (ctxt, (is_open, (insts, opt_rule))) =>
wenzelm@18235
   534
    Method.METHOD_CASES (fn facts =>
wenzelm@18235
   535
      Seq.DETERM (HEADGOAL (cases_tac ctxt is_open insts opt_rule facts))));
wenzelm@18178
   536
wenzelm@18512
   537
fun induct_meth src =
wenzelm@18235
   538
  Method.syntax (Args.mode openN --
wenzelm@18235
   539
    (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
wenzelm@18235
   540
    (fixing -- taking -- Scan.option induct_rule))) src
wenzelm@18235
   541
  #> (fn (ctxt, (is_open, (insts, ((fixing, taking), opt_rule)))) =>
wenzelm@18235
   542
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18512
   543
      Seq.DETERM (HEADGOAL (induct_tac ctxt is_open insts fixing taking opt_rule facts))));
wenzelm@18205
   544
wenzelm@18235
   545
fun coinduct_meth src =
wenzelm@18235
   546
  Method.syntax (Args.mode openN --
wenzelm@18235
   547
    (Scan.repeat (unless_more_args inst) -- taking -- Scan.option coinduct_rule)) src
wenzelm@18235
   548
  #> (fn (ctxt, (is_open, ((insts, taking), opt_rule))) =>
wenzelm@18235
   549
    Method.RAW_METHOD_CASES (fn facts =>
wenzelm@18235
   550
      Seq.DETERM (HEADGOAL (coinduct_tac ctxt is_open insts taking opt_rule facts))));
wenzelm@11670
   551
wenzelm@11670
   552
end;
wenzelm@11670
   553
wenzelm@11670
   554
wenzelm@11670
   555
wenzelm@11670
   556
(** theory setup **)
wenzelm@11670
   557
wenzelm@11670
   558
val setup =
wenzelm@18708
   559
  Method.add_methods
wenzelm@18235
   560
    [(InductAttrib.casesN, cases_meth, "case analysis on types or sets"),
wenzelm@18512
   561
     (InductAttrib.inductN, induct_meth, "induction on types or sets"),
wenzelm@18708
   562
     (InductAttrib.coinductN, coinduct_meth, "coinduction on types or sets")];
wenzelm@11670
   563
wenzelm@11670
   564
end;