src/HOL/Nominal/nominal_induct.ML
author berghofe
Sun Jan 10 18:01:04 2010 +0100 (2010-01-10)
changeset 34907 b0aaec87751c
parent 33957 e9afca2118d4
child 36960 01594f816e3a
permissions -rw-r--r--
Added infrastructure for simplifying equality constraints.
Option (no_simp) restores old behaviour of induct method.
     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 -> bool -> (binding option * (term * bool)) 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 = 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) (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 prop
    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 simp 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 Induct.atomize_cterm)) 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 
    97     fun rule_cases ctxt r =
    98       let val r' = if simp then Induct.simplified_rule ctxt r else r
    99       in Rule_Cases.make_nested (Thm.prop_of r') (Induct.rulified_term r') end;
   100   in
   101     (fn i => fn st =>
   102       rules
   103       |> inst_mutual_rule ctxt insts avoiding
   104       |> Rule_Cases.consume (flat defs) facts
   105       |> Seq.maps (fn (((cases, concls), (more_consumes, more_facts)), rule) =>
   106         (PRECISE_CONJUNCTS (length concls) (ALLGOALS (fn j =>
   107           (CONJUNCTS (ALLGOALS
   108             let
   109               val adefs = nth_list atomized_defs (j - 1);
   110               val frees = fold (Term.add_frees o prop_of) adefs [];
   111               val xs = nth_list fixings (j - 1);
   112               val k = nth concls (j - 1) + more_consumes
   113             in
   114               Method.insert_tac (more_facts @ adefs) THEN'
   115                 (if simp then
   116                    Induct.rotate_tac k (length adefs) THEN'
   117                    Induct.fix_tac defs_ctxt k
   118                      (List.partition (member op = frees) xs |> op @)
   119                  else
   120                    Induct.fix_tac defs_ctxt k xs)
   121             end)
   122           THEN' Induct.inner_atomize_tac) j))
   123         THEN' Induct.atomize_tac) i st |> Seq.maps (fn st' =>
   124             Induct.guess_instance ctxt
   125               (finish_rule (Induct.internalize more_consumes rule)) i st'
   126             |> Seq.maps (fn rule' =>
   127               CASES (rule_cases ctxt rule' cases)
   128                 (Tactic.rtac (rename_params_rule false [] rule') i THEN
   129                   PRIMITIVE (singleton (ProofContext.export defs_ctxt ctxt))) st'))))
   130     THEN_ALL_NEW_CASES
   131       ((if simp then Induct.simplify_tac ctxt THEN' (TRY o Induct.trivial_tac)
   132         else K all_tac)
   133        THEN_ALL_NEW Induct.rulify_tac)
   134   end;
   135 
   136 
   137 (* concrete syntax *)
   138 
   139 local
   140 
   141 structure P = OuterParse;
   142 
   143 val avoidingN = "avoiding";
   144 val fixingN = "arbitrary";  (* to be consistent with induct; hopefully this changes again *)
   145 val ruleN = "rule";
   146 
   147 val inst = Scan.lift (Args.$$$ "_") >> K NONE ||
   148   Args.term >> (SOME o rpair false) ||
   149   Scan.lift (Args.$$$ "(") |-- (Args.term >> (SOME o rpair true)) --|
   150     Scan.lift (Args.$$$ ")");
   151 
   152 val def_inst =
   153   ((Scan.lift (Args.binding --| (Args.$$$ "\<equiv>" || Args.$$$ "==")) >> SOME)
   154       -- (Args.term >> rpair false)) >> SOME ||
   155     inst >> Option.map (pair NONE);
   156 
   157 val free = Args.context -- Args.term >> (fn (_, Free v) => v | (ctxt, t) =>
   158   error ("Bad free variable: " ^ Syntax.string_of_term ctxt t));
   159 
   160 fun unless_more_args scan = Scan.unless (Scan.lift
   161   ((Args.$$$ avoidingN || Args.$$$ fixingN || Args.$$$ ruleN) -- Args.colon)) scan;
   162 
   163 
   164 val avoiding = Scan.optional (Scan.lift (Args.$$$ avoidingN -- Args.colon) |--
   165   Scan.repeat (unless_more_args free)) [];
   166 
   167 val fixing = Scan.optional (Scan.lift (Args.$$$ fixingN -- Args.colon) |--
   168   P.and_list' (Scan.repeat (unless_more_args free))) [];
   169 
   170 val rule_spec = Scan.lift (Args.$$$ "rule" -- Args.colon) |-- Attrib.thms;
   171 
   172 in
   173 
   174 val nominal_induct_method =
   175   Args.mode Induct.no_simpN -- (P.and_list' (Scan.repeat (unless_more_args def_inst)) --
   176   avoiding -- fixing -- rule_spec) >>
   177   (fn (no_simp, (((x, y), z), w)) => fn ctxt =>
   178     RAW_METHOD_CASES (fn facts =>
   179       HEADGOAL (nominal_induct_tac ctxt (not no_simp) x y z w facts)));
   180 
   181 end;
   182 
   183 end;