src/HOL/Nominal/nominal_induct.ML
author urbanc
Thu Dec 01 04:46:17 2005 +0100 (2005-12-01)
changeset 18311 b83b00cbaecf
parent 18299 af72dfc4b9f9
child 18583 96e1ef2f806f
permissions -rw-r--r--
changed "fresh:" to "avoiding:" and cleaned up the weakening example
     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 ->
    10     (string * typ) list -> (string * typ) list list -> thm ->
    11     thm list -> int -> RuleCases.cases_tactic
    12   val nominal_induct_method: Method.src -> Proof.context -> Method.method
    13 end =
    14 struct
    15 
    16 
    17 (* proper tuples -- nested left *)
    18 
    19 fun tupleT Ts = HOLogic.unitT |> fold (fn T => fn U => HOLogic.mk_prodT (U, T)) Ts;
    20 fun tuple ts = HOLogic.unit |> fold (fn t => fn u => HOLogic.mk_prod (u, t)) ts;
    21 
    22 fun tuple_fun Ts (xi, T) =
    23   Library.funpow (length Ts) HOLogic.mk_split
    24     (Var (xi, (HOLogic.unitT :: Ts) ---> Term.range_type T));
    25 
    26 val split_all_tuples =
    27   Simplifier.full_simplify (HOL_basic_ss addsimps
    28     [split_conv, split_paired_all, unit_all_eq1, thm "fresh_unit_elim", thm "fresh_prod_elim"]);
    29 
    30 
    31 (* prepare rule *)
    32 
    33 (*conclusion: ?P fresh_struct ... insts*)
    34 fun inst_rule thy insts fresh rule =
    35   let
    36     val vars = InductAttrib.vars_of (Thm.concl_of rule);
    37     val m = length vars and n = length insts;
    38     val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
    39     val P :: x :: ys = vars;
    40     val zs = Library.drop (m - n - 2, ys);
    41 
    42     val subst =
    43       (P, tuple_fun (map #2 fresh) (Term.dest_Var P)) ::
    44       (x, tuple (map Free fresh)) ::
    45       List.mapPartial (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ insts);
    46   in
    47     rule
    48     |> Drule.cterm_instantiate (map (pairself (Thm.cterm_of thy)) subst)
    49   end;
    50 
    51 fun rename_params_rule internal xs rule =
    52   let
    53     val tune =
    54       if internal then Syntax.internal
    55       else fn x => the_default x (try Syntax.dest_internal x);
    56     val n = length xs;
    57     fun rename prem =
    58       let
    59         val ps = Logic.strip_params prem;
    60         val p = length ps;
    61         val ys =
    62           if p < n then []
    63           else map (tune o #1) (Library.take (p - n, ps)) @ xs;
    64       in Logic.list_rename_params (ys, prem) end;
    65     fun rename_prems prop =
    66       let val (As, C) = Logic.strip_horn (Thm.prop_of rule)
    67       in Logic.list_implies (map rename As, C) end;
    68   in Thm.equal_elim (Thm.reflexive (Drule.cterm_fun rename_prems (Thm.cprop_of rule))) rule end;
    69 
    70 
    71 (* nominal_induct_tac *)
    72 
    73 fun nominal_induct_tac ctxt def_insts fresh fixing rule facts =
    74   let
    75     val thy = ProofContext.theory_of ctxt;
    76     val cert = Thm.cterm_of thy;
    77 
    78     val ((insts, defs), defs_ctxt) = InductMethod.add_defs def_insts ctxt;
    79     val atomized_defs = map ObjectLogic.atomize_thm defs;
    80 
    81     val finish_rule =
    82       split_all_tuples
    83       #> rename_params_rule true (map (ProofContext.revert_skolem defs_ctxt o #1) fresh);
    84     fun rule_cases r = RuleCases.make true (SOME (Thm.prop_of r)) (InductMethod.rulified_term r);
    85   in
    86     (fn i => fn st =>
    87       rule
    88       |> `RuleCases.get
    89       ||> inst_rule thy insts fresh
    90       |> RuleCases.consume defs facts
    91       |> Seq.maps (fn ((cases, (k, more_facts)), r) =>
    92         (CONJUNCTS (ALLGOALS (fn j =>
    93             Method.insert_tac (more_facts @ atomized_defs) j
    94             THEN InductMethod.fix_tac defs_ctxt k (Library.nth_list fixing (j - 1)) j))
    95           THEN' InductMethod.atomize_tac) i st |> Seq.maps (fn st' =>
    96             InductMethod.guess_instance (finish_rule r) i st'
    97             |> Seq.maps (fn r' =>
    98               CASES (rule_cases r' cases)
    99                 (Tactic.rtac (rename_params_rule false [] r') i THEN
   100                   PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
   101     THEN_ALL_NEW_CASES InductMethod.rulify_tac
   102   end;
   103 
   104 
   105 (* concrete syntax *)
   106 
   107 local
   108 
   109 val freshN = "avoiding";
   110 val fixingN = "fixing";
   111 val ruleN = "rule";
   112 
   113 val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.local_term >> SOME;
   114 
   115 val def_inst =
   116   ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
   117       -- Args.local_term) >> SOME ||
   118     inst >> Option.map (pair NONE);
   119 
   120 val free = Scan.state -- Args.local_term >> (fn (_, Free v) => v | (ctxt, t) =>
   121   error ("Bad free variable: " ^ ProofContext.string_of_term ctxt t));
   122 
   123 fun unless_more_args scan = Scan.unless (Scan.lift
   124   ((Args.$$$ freshN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
   125 
   126 
   127 val def_insts = Scan.repeat (unless_more_args def_inst);
   128 
   129 val fresh = Scan.optional (Scan.lift (Args.$$$ freshN -- Args.colon) |--
   130   Scan.repeat (unless_more_args free)) [];
   131 
   132 val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
   133   Args.and_list1 (Scan.repeat (unless_more_args free))) [];
   134 
   135 val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.local_thm;
   136 
   137 in
   138 
   139 fun nominal_induct_method src =
   140   Method.syntax (def_insts -- fresh -- fixing -- rule_spec) src
   141   #> (fn (ctxt, (((x, y), z), w)) =>
   142     Method.RAW_METHOD_CASES (fn facts =>
   143       HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
   144 
   145 end;
   146 
   147 end;