src/HOL/Nominal/nominal_induct.ML
author haftmann
Tue Nov 24 17:28:25 2009 +0100 (2009-11-24)
changeset 33955 fff6f11b1f09
parent 33368 b1cf34f1855c
child 33957 e9afca2118d4
permissions -rw-r--r--
curried take/drop
     1 (*  Author:     Christian Urban and Makarius
     2 
     3 The nominal induct proof method.
     4 *)
     5 
     6 structure NominalInduct:
     7 sig
     8   val nominal_induct_tac: Proof.context -> (binding option * term) option list list ->
     9     (string * typ) list -> (string * typ) list list -> thm list ->
    10     thm list -> int -> Rule_Cases.cases_tactic
    11   val nominal_induct_method: (Proof.context -> Proof.method) context_parser
    12 end =
    13 struct
    14 
    15 (* proper tuples -- nested left *)
    16 
    17 fun tupleT Ts = HOLogic.unitT |> fold (fn T => fn U => HOLogic.mk_prodT (U, T)) Ts;
    18 fun tuple ts = HOLogic.unit |> fold (fn t => fn u => HOLogic.mk_prod (u, t)) ts;
    19 
    20 fun tuple_fun Ts (xi, T) =
    21   Library.funpow (length Ts) HOLogic.mk_split
    22     (Var (xi, (HOLogic.unitT :: Ts) ---> Term.range_type T));
    23 
    24 val split_all_tuples =
    25   Simplifier.full_simplify (HOL_basic_ss addsimps
    26     [split_conv, split_paired_all, unit_all_eq1, @{thm fresh_unit_elim}, @{thm fresh_prod_elim}] @
    27     @{thms fresh_star_unit_elim} @ @{thms fresh_star_prod_elim});
    28 
    29 
    30 (* prepare rule *)
    31 
    32 fun inst_mutual_rule ctxt insts avoiding rules =
    33   let
    34     val (nconcls, joined_rule) = Rule_Cases.strict_mutual_rule ctxt rules;
    35     val concls = Logic.dest_conjunctions (Thm.concl_of joined_rule);
    36     val (cases, consumes) = Rule_Cases.get joined_rule;
    37 
    38     val l = length rules;
    39     val _ =
    40       if length insts = l then ()
    41       else error ("Bad number of instantiations for " ^ string_of_int l ^ " rules");
    42 
    43     fun subst inst concl =
    44       let
    45         val vars = Induct.vars_of concl;
    46         val m = length vars and n = length inst;
    47         val _ = if m >= n + 2 then () else error "Too few variables in conclusion of rule";
    48         val P :: x :: ys = vars;
    49         val zs = (uncurry drop) (m - n - 2, ys);
    50       in
    51         (P, tuple_fun (map #2 avoiding) (Term.dest_Var P)) ::
    52         (x, tuple (map Free avoiding)) ::
    53         map_filter (fn (z, SOME t) => SOME (z, t) | _ => NONE) (zs ~~ inst)
    54       end;
    55      val substs =
    56        map2 subst insts concls |> flat |> distinct (op =)
    57        |> map (pairself (Thm.cterm_of (ProofContext.theory_of ctxt)));
    58   in 
    59     (((cases, nconcls), consumes), Drule.cterm_instantiate substs joined_rule) 
    60   end;
    61 
    62 fun rename_params_rule internal xs rule =
    63   let
    64     val tune =
    65       if internal then Name.internal
    66       else fn x => the_default x (try Name.dest_internal x);
    67     val n = length xs;
    68     fun rename prem =
    69       let
    70         val ps = Logic.strip_params prem;
    71         val p = length ps;
    72         val ys =
    73           if p < n then []
    74           else map (tune o #1) ((uncurry take) (p - n, ps)) @ xs;
    75       in Logic.list_rename_params (ys, prem) end;
    76     fun rename_prems prop =
    77       let val (As, C) = Logic.strip_horn (Thm.prop_of rule)
    78       in Logic.list_implies (map rename As, C) end;
    79   in Thm.equal_elim (Thm.reflexive (Drule.cterm_fun rename_prems (Thm.cprop_of rule))) rule end;
    80 
    81 
    82 (* nominal_induct_tac *)
    83 
    84 fun nominal_induct_tac ctxt def_insts avoiding fixings rules facts =
    85   let
    86     val thy = ProofContext.theory_of ctxt;
    87     val cert = Thm.cterm_of thy;
    88 
    89     val ((insts, defs), defs_ctxt) = fold_map Induct.add_defs def_insts ctxt |>> split_list;
    90     val atomized_defs = map (map (Conv.fconv_rule ObjectLogic.atomize)) defs;
    91 
    92     val finish_rule =
    93       split_all_tuples
    94       #> rename_params_rule true
    95         (map (Name.clean o ProofContext.revert_skolem defs_ctxt o fst) avoiding);
    96     fun rule_cases r = Rule_Cases.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       |> Rule_Cases.consume (flat 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 ctxt
   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 structure P = OuterParse;
   126 
   127 val avoidingN = "avoiding";
   128 val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
   129 val ruleN = "rule";
   130 
   131 val inst = Scan.lift (Args.$$$ "_") >> K NONE || Args.term >> SOME;
   132 
   133 val def_inst =
   134   ((Scan.lift (Args.binding --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
   135       -- Args.term) >> SOME ||
   136     inst >> Option.map (pair NONE);
   137 
   138 val free = Args.context -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
   139   error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
   140 
   141 fun unless_more_args scan = Scan.unless (Scan.lift
   142   ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
   143 
   144 
   145 val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
   146   Scan.repeat (unless_more_args free)) [];
   147 
   148 val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
   149   P.and_list' (Scan.repeat (unless_more_args free))) [];
   150 
   151 val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
   152 
   153 in
   154 
   155 val nominal_induct_method =
   156   P.and_list' (Scan.repeat (unless_more_args def_inst)) --
   157   avoiding -- fixing -- rule_spec >>
   158   (fn (((x, y), z), w) => fn ctxt =>
   159     RAW_METHOD_CASES (fn facts =>
   160       HEADGOAL (nominal_induct_tac ctxt x y z w facts)));
   161 
   162 end;
   163 
   164 end;