src/HOL/Nominal/nominal_induct.ML
author wenzelm
Wed Aug 02 22:26:40 2006 +0200 (2006-08-02)
changeset 20288 8ff4a0ea49b2
parent 20072 c4710df2c953
child 20998 714a08286899
permissions -rw-r--r--
simplified Assumption/ProofContext.export;
     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 (*conclusions: ?P avoiding_struct ... insts*)
    33 fun inst_mutual_rule ctxt insts avoiding rules =
    34   let
    35     val (concls, rule) =
    36       (case RuleCases.mutual_rule ctxt rules of
    37         NONE => error "Failed to join given rules into one mutual rule"
    38       | SOME res => res);
    39     val (cases, consumes) = RuleCases.get rule;
    40 
    41     val l = length rules;
    42     val _ =
    43       if length insts = l then ()
    44       else error ("Bad number of instantiations for " ^ string_of_int l ^ " rules");
    45 
    46     fun subst inst rule =
    47       let
    48         val vars = InductAttrib.vars_of (Thm.concl_of rule);
    49         val m = length vars and n = length inst;
    50         val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
    51         val P :: x :: ys = vars;
    52         val zs = Library.drop (m - n - 2, ys);
    53       in
    54         (P, tuple_fun (map #2 avoiding) (Term.dest_Var P)) ::
    55         (x, tuple (map Free avoiding)) ::
    56         List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ inst)
    57       end;
    58      val substs =
    59        map2 subst insts rules |> List.concat |> distinct (op =)
    60        |> map (pairself (Thm.cterm_of (ProofContext.theory_of ctxt)));
    61   in (((cases, concls), consumes), Drule.cterm_instantiate substs rule) 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 InductMethod.add_defs def_insts ctxt |>> split_list;
    91     val atomized_defs = map (map ObjectLogic.atomize_thm) 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) (InductMethod.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' InductMethod.fix_tac defs_ctxt
   107                 (nth concls (j - 1) + more_consumes)
   108                 (nth_list fixings (j - 1))))
   109           THEN' InductMethod.inner_atomize_tac) j))
   110         THEN' InductMethod.atomize_tac) i st |> Seq.maps (fn st' =>
   111             InductMethod.guess_instance
   112               (finish_rule (InductMethod.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 InductMethod.rulify_tac
   118   end;
   119 
   120 
   121 (* concrete syntax *)
   122 
   123 local
   124 
   125 val avoidingN = "avoiding";
   126 val fixingN = "fixing";
   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 (ctxt, (((x, y), z), w)) =>
   158     Method.RAW_METHOD_CASES (fn facts =>
   159       HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
   160 
   161 end;
   162 
   163 end;