src/HOL/Nominal/nominal_induct.ML
author berghofe
Mon Feb 13 17:02:54 2006 +0100 (2006-02-13)
changeset 19036 73782d21e855
parent 18610 05a5e950d5f1
child 19115 bc8da9b4a81c
permissions -rw-r--r--
Adapted to Context.generic syntax.
     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 thy insts avoiding rules =
    34   let
    35     val (concls, rule) =
    36       (case RuleCases.mutual_rule 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
    60        |> map (pairself (Thm.cterm_of thy));
    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 Syntax.internal
    67       else fn x => the_default x (try Syntax.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 = PolyML.print #>
    94       split_all_tuples
    95       #> rename_params_rule true (map (ProofContext.revert_skolem defs_ctxt o fst) avoiding) #> PolyML.print;
    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 thy 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 (finish_rule (InductMethod.internalize more_consumes rule)) i (PolyML.print st')
   112             |> Seq.maps (fn rule' =>
   113               CASES (rule_cases (PolyML.print rule') cases)
   114                 (Tactic.rtac (rename_params_rule false [] rule') i THEN
   115                   PRIMSEQ (ProofContext.exports defs_ctxt ctxt)) st'))))
   116     THEN_ALL_NEW_CASES InductMethod.rulify_tac
   117   end;
   118 
   119 
   120 (* concrete syntax *)
   121 
   122 local
   123 
   124 val avoidingN = "avoiding";
   125 val fixingN = "fixing";
   126 val ruleN = "rule";
   127 
   128 val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
   129 
   130 val def_inst =
   131   ((Scan.lift (Args.name --| (Args.$$$ "\\<equiv>" || Args.$$$ "==")) >> SOME)
   132       -- Args.term) >> SOME ||
   133     inst >> Option.map (pair NONE);
   134 
   135 val free = Scan.state -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
   136   error ("Bad free variable: " ^ ProofContext.string_of_term (Context.proof_of ctxt) t));
   137 
   138 fun unless_more_args scan = Scan.unless (Scan.lift
   139   ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
   140 
   141 
   142 val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
   143   Scan.repeat (unless_more_args free)) [];
   144 
   145 val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
   146   Args.and_list (Scan.repeat (unless_more_args free))) [];
   147 
   148 val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
   149 
   150 in
   151 
   152 fun nominal_induct_method src =
   153   Method.syntax
   154    (Args.and_list (Scan.repeat (unless_more_args def_inst)) --
   155     avoiding -- fixing -- rule_spec) src
   156   #> (fn (ctxt, (((x, y), z), w)) =>
   157     Method.RAW_METHOD_CASES (fn facts =>
   158       HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
   159 
   160 end;
   161 
   162 end;