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