src/HOL/Nominal/nominal_induct.ML
author wenzelm
Thu Oct 04 14:42:47 2007 +0200 (2007-10-04)
changeset 24830 a7b3ab44d993
parent 23591 d32a85385e17
child 24920 2a45e400fdad
permissions -rw-r--r--
moved Pure/Isar/induct_attrib.ML and Provers/induct_method.ML to Tools/induct.ML;
     1 (*  ID:         $Id$
     2     Author:     Christian Urban and Makarius
     3 
     4 The nominal induct proof method.
     5 *)
     6 
     7 structure NominalInduct:
     8 sig
     9   val nominal_induct_tac: Proof.context -> (string option * term) option list list ->
    10     (string * typ) list -> (string * typ) list list -> thm list ->
    11     thm list -> int -> RuleCases.cases_tactic
    12   val nominal_induct_method: Method.src -> Proof.context -> Method.method
    13 end =
    14 struct
    15 
    16 (* proper tuples -- nested left *)
    17 
    18 fun tupleT Ts = HOLogic.unitT |> fold (fn T => fn U => HOLogic.mk_prodT (U, T)) Ts;
    19 fun tuple ts = HOLogic.unit |> fold (fn t => fn u => HOLogic.mk_prod (u, t)) ts;
    20 
    21 fun tuple_fun Ts (xi, T) =
    22   Library.funpow (length Ts) HOLogic.mk_split
    23     (Var (xi, (HOLogic.unitT :: Ts) ---> Term.range_type T));
    24 
    25 val split_all_tuples =
    26   Simplifier.full_simplify (HOL_basic_ss addsimps
    27     [split_conv, split_paired_all, unit_all_eq1, thm "fresh_unit_elim", thm "fresh_prod_elim"]);
    28 
    29 
    30 (* prepare rule *)
    31 
    32 fun inst_mutual_rule ctxt insts avoiding rules =
    33   let
    34 
    35     val (nconcls, joined_rule) = RuleCases.strict_mutual_rule ctxt rules;
    36     val concls = Logic.dest_conjunctions (Thm.concl_of joined_rule);
    37     val (cases, consumes) = RuleCases.get joined_rule;
    38 
    39     val l = length rules;
    40     val _ =
    41       if length insts = l then ()
    42       else error ("Bad number of instantiations for " ^ string_of_int l ^ " rules");
    43 
    44     fun subst inst concl =
    45       let
    46         val vars = Induct.vars_of concl;
    47         val m = length vars and n = length inst;
    48         val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
    49         val P :: x :: ys = vars;
    50         val zs = Library.drop (m - n - 2, ys);
    51       in
    52         (P, tuple_fun (map #2 avoiding) (Term.dest_Var P)) ::
    53         (x, tuple (map Free avoiding)) ::
    54         List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ inst)
    55       end;
    56      val substs =
    57        map2 subst insts concls |> List.concat |> distinct (op =)
    58        |> map (pairself (Thm.cterm_of (ProofContext.theory_of ctxt)));
    59   in 
    60     (((cases, nconcls), consumes), Drule.cterm_instantiate substs joined_rule) 
    61   end;
    62 
    63 fun rename_params_rule internal xs rule =
    64   let
    65     val tune =
    66       if internal then Name.internal
    67       else fn x => the_default x (try Name.dest_internal x);
    68     val n = length xs;
    69     fun rename prem =
    70       let
    71         val ps = Logic.strip_params prem;
    72         val p = length ps;
    73         val ys =
    74           if p < n then []
    75           else map (tune o #1) (Library.take (p - n, ps)) @ xs;
    76       in Logic.list_rename_params (ys, prem) end;
    77     fun rename_prems prop =
    78       let val (As, C) = Logic.strip_horn (Thm.prop_of rule)
    79       in Logic.list_implies (map rename As, C) end;
    80   in Thm.equal_elim (Thm.reflexive (Drule.cterm_fun rename_prems (Thm.cprop_of rule))) rule end;
    81 
    82 
    83 (* nominal_induct_tac *)
    84 
    85 fun nominal_induct_tac ctxt def_insts avoiding fixings rules facts =
    86   let
    87     val thy = ProofContext.theory_of ctxt;
    88     val cert = Thm.cterm_of thy;
    89 
    90     val ((insts, defs), defs_ctxt) = fold_map Induct.add_defs def_insts ctxt |>> split_list;
    91     val atomized_defs = map (map (Conv.fconv_rule ObjectLogic.atomize)) defs;
    92 
    93     val finish_rule =
    94       split_all_tuples
    95       #> rename_params_rule true (map (ProofContext.revert_skolem defs_ctxt o fst) avoiding);
    96     fun rule_cases r = RuleCases.make_nested true (Thm.prop_of r) (Induct.rulified_term r);
    97   in
    98     (fn i => fn st =>
    99       rules
   100       |> inst_mutual_rule ctxt insts avoiding
   101       |> RuleCases.consume (List.concat defs) facts
   102       |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
   103         (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
   104           (CONJUNCTS (ALLGOALS
   105             (Method.insert_tac (more_facts @ nth_list atomized_defs (j - 1))
   106               THEN' Induct.fix_tac defs_ctxt
   107                 (nth concls (j - 1) + more_consumes)
   108                 (nth_list fixings (j - 1))))
   109           THEN' Induct.inner_atomize_tac) j))
   110         THEN' Induct.atomize_tac) i st |> Seq.maps (fn st' =>
   111             Induct.guess_instance
   112               (finish_rule (Induct.internalize more_consumes rule)) i st'
   113             |> Seq.maps (fn rule' =>
   114               CASES (rule_cases rule' cases)
   115                 (Tactic.rtac (rename_params_rule false [] rule') i THEN
   116                   PRIMITIVE (singleton (ProofContext.export defs_ctxt ctxt))) st'))))
   117     THEN_ALL_NEW_CASES Induct.rulify_tac
   118   end;
   119 
   120 
   121 (* concrete syntax *)
   122 
   123 local
   124 
   125 val avoidingN = "avoiding";
   126 val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
   127 val ruleN = "rule";
   128 
   129 val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
   130 
   131 val def_inst =
   132   ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
   133       -- Args.term) >> SOME ||
   134     inst >> Option.map (pair NONE);
   135 
   136 val free = Scan.state -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
   137   error ("Bad free variable: " ^ ProofContext.string_of_term (Context.proof_of ctxt) t));
   138 
   139 fun unless_more_args scan = Scan.unless (Scan.lift
   140   ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
   141 
   142 
   143 val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
   144   Scan.repeat (unless_more_args free)) [];
   145 
   146 val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
   147   Args.and_list (Scan.repeat (unless_more_args free))) [];
   148 
   149 val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
   150 
   151 in
   152 
   153 fun nominal_induct_method src =
   154   Method.syntax
   155    (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
   156     avoiding -- fixing -- rule_spec) src
   157   #> (fn ((((x, y), z), w), ctxt) =>
   158     Method.RAW_METHOD_CASES (fn facts =>
   159       HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
   160 
   161 end;
   162 
   163 end;