src/Tools/induct_tacs.ML
author haftmann
Wed Nov 25 09:13:46 2009 +0100 (2009-11-25)
changeset 33957 e9afca2118d4
parent 33955 fff6f11b1f09
child 35625 9c818cab0dd0
permissions -rw-r--r--
normalized uncurry take/drop
haftmann@28308
     1
(*  Title:      Tools/induct_tacs.ML
wenzelm@27326
     2
    Author:     Makarius
wenzelm@27326
     3
wenzelm@27326
     4
Unstructured induction and cases analysis.
wenzelm@27326
     5
*)
wenzelm@27326
     6
wenzelm@27326
     7
signature INDUCT_TACS =
wenzelm@27326
     8
sig
wenzelm@27326
     9
  val case_tac: Proof.context -> string -> int -> tactic
wenzelm@27326
    10
  val case_rule_tac: Proof.context -> string -> thm -> int -> tactic
wenzelm@27326
    11
  val induct_tac: Proof.context -> string option list list -> int -> tactic
wenzelm@27326
    12
  val induct_rules_tac: Proof.context -> string option list list -> thm list -> int -> tactic
wenzelm@27326
    13
  val setup: theory -> theory
wenzelm@27326
    14
end
wenzelm@27326
    15
wenzelm@27326
    16
structure InductTacs: INDUCT_TACS =
wenzelm@27326
    17
struct
wenzelm@27326
    18
wenzelm@27326
    19
(* case analysis *)
wenzelm@27326
    20
wenzelm@27326
    21
fun check_type ctxt t =
wenzelm@27326
    22
  let
wenzelm@27326
    23
    val u = singleton (Variable.polymorphic ctxt) t;
wenzelm@27326
    24
    val U = Term.fastype_of u;
wenzelm@27326
    25
    val _ = Term.is_TVar U andalso
wenzelm@27326
    26
      error ("Cannot determine type of " ^ Syntax.string_of_term ctxt u);
wenzelm@27326
    27
  in (u, U) end;
wenzelm@27326
    28
wenzelm@30541
    29
fun gen_case_tac ctxt0 s opt_rule i st =
wenzelm@27326
    30
  let
wenzelm@32863
    31
    val (_, ctxt) = Variable.focus_subgoal i st ctxt0;
wenzelm@27326
    32
    val rule =
wenzelm@27326
    33
      (case opt_rule of
wenzelm@27326
    34
        SOME rule => rule
wenzelm@27326
    35
      | NONE =>
wenzelm@27326
    36
          (case Induct.find_casesT ctxt
wenzelm@27326
    37
              (#2 (check_type ctxt (ProofContext.read_term_schematic ctxt s))) of
wenzelm@27326
    38
            rule :: _ => rule
wenzelm@27326
    39
          | [] => @{thm case_split}));
wenzelm@27326
    40
    val _ = Method.trace ctxt [rule];
wenzelm@27326
    41
wenzelm@27326
    42
    val xi =
wenzelm@27326
    43
      (case Induct.vars_of (Thm.term_of (Thm.cprem_of rule 1)) of
wenzelm@27326
    44
        Var (xi, _) :: _ => xi
wenzelm@27326
    45
      | _ => raise THM ("Malformed cases rule", 0, [rule]));
wenzelm@27326
    46
  in res_inst_tac ctxt [(xi, s)] rule i st end
wenzelm@27326
    47
  handle THM _ => Seq.empty;
wenzelm@27326
    48
wenzelm@30541
    49
fun case_tac ctxt s = gen_case_tac ctxt s NONE;
wenzelm@30541
    50
fun case_rule_tac ctxt s rule = gen_case_tac ctxt s (SOME rule);
wenzelm@27326
    51
wenzelm@27326
    52
wenzelm@27326
    53
(* induction *)
wenzelm@27326
    54
wenzelm@27326
    55
local
wenzelm@27326
    56
wenzelm@27326
    57
fun prep_var (Var (ixn, _), SOME x) = SOME (ixn, x)
wenzelm@27326
    58
  | prep_var _ = NONE;
wenzelm@27326
    59
wenzelm@27326
    60
fun prep_inst (concl, xs) =
wenzelm@27326
    61
  let val vs = Induct.vars_of concl
haftmann@33957
    62
  in map_filter prep_var (drop (length vs - length xs) vs ~~ xs) end;
wenzelm@27326
    63
wenzelm@27326
    64
in
wenzelm@27326
    65
wenzelm@30541
    66
fun gen_induct_tac ctxt0 varss opt_rules i st =
wenzelm@27326
    67
  let
wenzelm@27326
    68
    val ((_, goal), ctxt) = Variable.focus_subgoal i st ctxt0;
wenzelm@27326
    69
    val (prems, concl) = Logic.strip_horn (Thm.term_of goal);
wenzelm@27326
    70
wenzelm@27326
    71
    fun induct_var name =
wenzelm@27326
    72
      let
wenzelm@27326
    73
        val t = Syntax.read_term ctxt name;
wenzelm@27326
    74
        val (x, _) = Term.dest_Free t handle TERM _ =>
wenzelm@27326
    75
          error ("Induction argument not a variable: " ^ Syntax.string_of_term ctxt t);
wenzelm@27326
    76
        val eq_x = fn Free (y, _) => x = y | _ => false;
wenzelm@27326
    77
        val _ =
wenzelm@27326
    78
          if Term.exists_subterm eq_x concl then ()
wenzelm@27326
    79
          else error ("No such variable in subgoal: " ^ Syntax.string_of_term ctxt t);
wenzelm@27326
    80
        val _ =
wenzelm@27326
    81
          if (exists o Term.exists_subterm) eq_x prems then
wenzelm@27326
    82
            warning ("Induction variable occurs also among premises: " ^ Syntax.string_of_term ctxt t)
wenzelm@27326
    83
          else ();
wenzelm@27326
    84
      in #1 (check_type ctxt t) end;
wenzelm@27326
    85
wenzelm@27326
    86
    val argss = map (map (Option.map induct_var)) varss;
wenzelm@27326
    87
    val rule =
wenzelm@27326
    88
      (case opt_rules of
wenzelm@33368
    89
        SOME rules => #2 (Rule_Cases.strict_mutual_rule ctxt rules)
wenzelm@27326
    90
      | NONE =>
wenzelm@33368
    91
          (case map_filter (Rule_Cases.mutual_rule ctxt) (Induct.get_inductT ctxt argss) of
wenzelm@27326
    92
            (_, rule) :: _ => rule
wenzelm@27326
    93
          | [] => raise THM ("No induction rules", 0, [])));
wenzelm@27326
    94
wenzelm@27326
    95
    val rule' = rule |> Conv.fconv_rule (Conv.concl_conv ~1 ObjectLogic.atomize);
wenzelm@27326
    96
    val _ = Method.trace ctxt [rule'];
wenzelm@27326
    97
wenzelm@27326
    98
    val concls = Logic.dest_conjunctions (Thm.concl_of rule);
wenzelm@27326
    99
    val insts = maps prep_inst (concls ~~ varss) handle Library.UnequalLengths =>
wenzelm@27326
   100
      error "Induction rule has different numbers of variables";
wenzelm@27326
   101
  in res_inst_tac ctxt insts rule' i st end
wenzelm@27326
   102
  handle THM _ => Seq.empty;
wenzelm@27326
   103
wenzelm@27326
   104
end;
wenzelm@27326
   105
wenzelm@30541
   106
fun induct_tac ctxt args = gen_induct_tac ctxt args NONE;
wenzelm@30541
   107
fun induct_rules_tac ctxt args rules = gen_induct_tac ctxt args (SOME rules);
wenzelm@27326
   108
wenzelm@27326
   109
wenzelm@27326
   110
(* method syntax *)
wenzelm@27326
   111
wenzelm@27326
   112
local
wenzelm@27326
   113
wenzelm@27326
   114
val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.$$$ ":");
wenzelm@27326
   115
val opt_rule = Scan.option (rule_spec |-- Attrib.thm);
wenzelm@27326
   116
val opt_rules = Scan.option (rule_spec |-- Attrib.thms);
wenzelm@27326
   117
wenzelm@27326
   118
val varss =
wenzelm@27882
   119
  OuterParse.and_list'
wenzelm@27882
   120
    (Scan.repeat (Scan.unless rule_spec (Scan.lift (Args.maybe Args.name_source))));
wenzelm@27326
   121
wenzelm@27326
   122
in
wenzelm@27326
   123
wenzelm@27326
   124
val setup =
wenzelm@30541
   125
  Method.setup @{binding case_tac}
wenzelm@30541
   126
    (Args.goal_spec -- Scan.lift Args.name_source -- opt_rule >>
wenzelm@30541
   127
      (fn ((quant, s), r) => fn ctxt => SIMPLE_METHOD'' quant (gen_case_tac ctxt s r)))
wenzelm@30541
   128
    "unstructured case analysis on types" #>
wenzelm@30541
   129
  Method.setup @{binding induct_tac}
wenzelm@30541
   130
    (Args.goal_spec -- varss -- opt_rules >>
wenzelm@30541
   131
      (fn ((quant, vs), rs) => fn ctxt => SIMPLE_METHOD'' quant (gen_induct_tac ctxt vs rs)))
wenzelm@30541
   132
    "unstructured induction on types";
wenzelm@27326
   133
wenzelm@27326
   134
end;
wenzelm@27326
   135
wenzelm@27326
   136
end;
wenzelm@27326
   137