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