src/HOL/simpdata.ML
author paulson
Thu Sep 26 16:38:02 1996 +0200 (1996-09-26)
changeset 2036 62ff902eeffc
parent 2031 03a843f0f447
child 2054 bf3891343aa5
permissions -rw-r--r--
Ran expandshort; used stac instead of ssubst
     1 (*  Title:      HOL/simpdata.ML
     2     ID:         $Id$
     3     Author:     Tobias Nipkow
     4     Copyright   1991  University of Cambridge
     5 
     6 Instantiation of the generic simplifier
     7 *)
     8 
     9 section "Simplifier";
    10 
    11 open Simplifier;
    12 
    13 (*** Integration of simplifier with classical reasoner ***)
    14 
    15 (*Add a simpset to a classical set!*)
    16 infix 4 addss;
    17 fun cs addss ss = cs addbefore asm_full_simp_tac ss 1;
    18 
    19 fun Addss ss = (claset := !claset addbefore asm_full_simp_tac ss 1);
    20 
    21 (*Designed to be idempotent, except if best_tac instantiates variables
    22   in some of the subgoals*)
    23 fun auto_tac (cs,ss) = 
    24     ALLGOALS (asm_full_simp_tac ss) THEN
    25     REPEAT (safe_tac cs THEN ALLGOALS (asm_full_simp_tac ss)) THEN
    26     REPEAT (FIRSTGOAL (best_tac (cs addss ss))) THEN
    27     prune_params_tac;
    28 
    29 fun Auto_tac() = auto_tac (!claset, !simpset);
    30 
    31 fun auto() = by (Auto_tac());
    32 
    33 
    34 (*** Addition of rules to simpsets and clasets simultaneously ***)
    35 
    36 (*Takes UNCONDITIONAL theorems of the form A<->B to 
    37         the Safe Intr     rule B==>A and 
    38         the Safe Destruct rule A==>B.
    39   Also ~A goes to the Safe Elim rule A ==> ?R
    40   Failing other cases, A is added as a Safe Intr rule*)
    41 local
    42   val iff_const = HOLogic.eq_const HOLogic.boolT;
    43 
    44   fun addIff th = 
    45       (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
    46                 (Const("not",_) $ A) =>
    47                     AddSEs [zero_var_indexes (th RS notE)]
    48               | (con $ _ $ _) =>
    49                     if con=iff_const
    50                     then (AddSIs [zero_var_indexes (th RS iffD2)];  
    51                           AddSDs [zero_var_indexes (th RS iffD1)])
    52                     else  AddSIs [th]
    53               | _ => AddSIs [th];
    54        Addsimps [th])
    55       handle _ => error ("AddIffs: theorem must be unconditional\n" ^ 
    56                          string_of_thm th)
    57 
    58   fun delIff th = 
    59       (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
    60                 (Const("not",_) $ A) =>
    61                     Delrules [zero_var_indexes (th RS notE)]
    62               | (con $ _ $ _) =>
    63                     if con=iff_const
    64                     then Delrules [zero_var_indexes (th RS iffD2),
    65                                    zero_var_indexes (th RS iffD1)]
    66                     else Delrules [th]
    67               | _ => Delrules [th];
    68        Delsimps [th])
    69       handle _ => warning("DelIffs: ignoring conditional theorem\n" ^ 
    70                           string_of_thm th)
    71 in
    72 val AddIffs = seq addIff
    73 val DelIffs = seq delIff
    74 end;
    75 
    76 
    77 local
    78 
    79   fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
    80 
    81   val P_imp_P_iff_True = prover "P --> (P = True)" RS mp;
    82   val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
    83 
    84   val not_P_imp_P_iff_F = prover "~P --> (P = False)" RS mp;
    85   val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection;
    86 
    87   fun atomize pairs =
    88     let fun atoms th =
    89           (case concl_of th of
    90              Const("Trueprop",_) $ p =>
    91                (case head_of p of
    92                   Const(a,_) =>
    93                     (case assoc(pairs,a) of
    94                        Some(rls) => flat (map atoms ([th] RL rls))
    95                      | None => [th])
    96                 | _ => [th])
    97            | _ => [th])
    98     in atoms end;
    99 
   100   fun mk_meta_eq r = case concl_of r of
   101           Const("==",_)$_$_ => r
   102       |   _$(Const("op =",_)$_$_) => r RS eq_reflection
   103       |   _$(Const("not",_)$_) => r RS not_P_imp_P_eq_False
   104       |   _ => r RS P_imp_P_eq_True;
   105   (* last 2 lines requires all formulae to be of the from Trueprop(.) *)
   106 
   107   fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
   108 
   109   val simp_thms = map prover
   110    [ "(x=x) = True",
   111      "(~True) = False", "(~False) = True", "(~ ~ P) = P",
   112      "(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))",
   113      "(True=P) = P", "(P=True) = P",
   114      "(True --> P) = P", "(False --> P) = True", 
   115      "(P --> True) = True", "(P --> P) = True",
   116      "(P --> False) = (~P)", "(P --> ~P) = (~P)",
   117      "(P & True) = P", "(True & P) = P", 
   118      "(P & False) = False", "(False & P) = False", "(P & P) = P",
   119      "(P | True) = True", "(True | P) = True", 
   120      "(P | False) = P", "(False | P) = P", "(P | P) = P",
   121      "((~P) = (~Q)) = (P=Q)",
   122      "(!x.P) = P", "(? x.P) = P", "? x. x=t", 
   123      "(? x. x=t & P(x)) = P(t)", "(! x. x=t --> P(x)) = P(t)" ];
   124 
   125 in
   126 
   127 val meta_eq_to_obj_eq = prove_goal HOL.thy "x==y ==> x=y"
   128   (fn [prem] => [rewtac prem, rtac refl 1]);
   129 
   130 val eq_sym_conv = prover "(x=y) = (y=x)";
   131 
   132 val conj_assoc = prover "((P&Q)&R) = (P&(Q&R))";
   133 
   134 val disj_assoc = prover "((P|Q)|R) = (P|(Q|R))";
   135 
   136 val imp_disj   = prover "(P|Q --> R) = ((P-->R)&(Q-->R))";
   137 
   138 (*Avoids duplication of subgoals after expand_if, when the true and false 
   139   cases boil down to the same thing.*) 
   140 val cases_simp = prover "((P --> Q) & (~P --> Q)) = Q";
   141 
   142 val if_True = prove_goalw HOL.thy [if_def] "(if True then x else y) = x"
   143  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
   144 
   145 val if_False = prove_goalw HOL.thy [if_def] "(if False then x else y) = y"
   146  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
   147 
   148 val if_P = prove_goal HOL.thy "P ==> (if P then x else y) = x"
   149  (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
   150 
   151 val if_not_P = prove_goal HOL.thy "~P ==> (if P then x else y) = y"
   152  (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
   153 
   154 val expand_if = prove_goal HOL.thy
   155     "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
   156  (fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1),
   157          stac if_P 2,
   158          stac if_not_P 1,
   159          REPEAT(fast_tac HOL_cs 1) ]);
   160 
   161 val if_bool_eq = prove_goal HOL.thy
   162                    "(if P then Q else R) = ((P-->Q) & (~P-->R))"
   163                    (fn _ => [rtac expand_if 1]);
   164 
   165 (*Add congruence rules for = (instead of ==) *)
   166 infix 4 addcongs;
   167 fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]);
   168 
   169 fun Addcongs congs = (simpset := !simpset addcongs congs);
   170 
   171 val mksimps_pairs =
   172   [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
   173    ("All", [spec]), ("True", []), ("False", []),
   174    ("If", [if_bool_eq RS iffD1])];
   175 
   176 fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all;
   177 
   178 val imp_cong = impI RSN
   179     (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
   180         (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
   181 
   182 val o_apply = prove_goalw HOL.thy [o_def] "(f o g)(x) = f(g(x))"
   183  (fn _ => [rtac refl 1]);
   184 
   185 (*Miniscoping: pushing in existential quantifiers*)
   186 val ex_simps = map prover 
   187                 ["(EX x. P x & Q)   = ((EX x.P x) & Q)",
   188                  "(EX x. P & Q x)   = (P & (EX x.Q x))",
   189                  "(EX x. P x | Q)   = ((EX x.P x) | Q)",
   190                  "(EX x. P | Q x)   = (P | (EX x.Q x))",
   191                  "(EX x. P x --> Q) = ((ALL x.P x) --> Q)",
   192                  "(EX x. P --> Q x) = (P --> (EX x.Q x))"];
   193 
   194 (*Miniscoping: pushing in universal quantifiers*)
   195 val all_simps = map prover
   196                 ["(ALL x. P x & Q)   = ((ALL x.P x) & Q)",
   197                  "(ALL x. P & Q x)   = (P & (ALL x.Q x))",
   198                  "(ALL x. P x | Q)   = ((ALL x.P x) | Q)",
   199                  "(ALL x. P | Q x)   = (P | (ALL x.Q x))",
   200                  "(ALL x. P x --> Q) = ((EX x.P x) --> Q)",
   201                  "(ALL x. P --> Q x) = (P --> (ALL x.Q x))"];
   202 
   203 val HOL_ss = empty_ss
   204       setmksimps (mksimps mksimps_pairs)
   205       setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac
   206                              ORELSE' etac FalseE)
   207       setsubgoaler asm_simp_tac
   208       addsimps ([if_True, if_False, o_apply, imp_disj, conj_assoc, disj_assoc,
   209                  cases_simp]
   210         @ ex_simps @ all_simps @ simp_thms)
   211       addcongs [imp_cong];
   212 
   213 
   214 (*In general it seems wrong to add distributive laws by default: they
   215   might cause exponential blow-up.  But imp_disj has been in for a while
   216   and cannot be removed without affecting existing proofs.  Moreover, 
   217   rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
   218   grounds that it allows simplification of R in the two cases.*)
   219 
   220 
   221 local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
   222 in
   223 fun split_tac splits = mktac (map mk_meta_eq splits)
   224 end;
   225 
   226 local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
   227 in
   228 fun split_inside_tac splits = mktac (map mk_meta_eq splits)
   229 end;
   230 
   231 
   232 (* elimination of existential quantifiers in assumptions *)
   233 
   234 val ex_all_equiv =
   235   let val lemma1 = prove_goal HOL.thy
   236         "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
   237         (fn prems => [resolve_tac prems 1, etac exI 1]);
   238       val lemma2 = prove_goalw HOL.thy [Ex_def]
   239         "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
   240         (fn prems => [REPEAT(resolve_tac prems 1)])
   241   in equal_intr lemma1 lemma2 end;
   242 
   243 (* '&' congruence rule: not included by default!
   244    May slow rewrite proofs down by as much as 50% *)
   245 
   246 val conj_cong = 
   247   let val th = prove_goal HOL.thy 
   248                 "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
   249                 (fn _=> [fast_tac HOL_cs 1])
   250   in  standard (impI RSN (2, th RS mp RS mp))  end;
   251 
   252 val rev_conj_cong =
   253   let val th = prove_goal HOL.thy 
   254                 "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
   255                 (fn _=> [fast_tac HOL_cs 1])
   256   in  standard (impI RSN (2, th RS mp RS mp))  end;
   257 
   258 (* '|' congruence rule: not included by default! *)
   259 
   260 val disj_cong = 
   261   let val th = prove_goal HOL.thy 
   262                 "(P=P')--> (~P'--> (Q=Q'))--> ((P|Q) = (P'|Q'))"
   263                 (fn _=> [fast_tac HOL_cs 1])
   264   in  standard (impI RSN (2, th RS mp RS mp))  end;
   265 
   266 (** 'if' congruence rules: neither included by default! *)
   267 
   268 (*Simplifies x assuming c and y assuming ~c*)
   269 val if_cong = prove_goal HOL.thy
   270   "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
   271 \  (if b then x else y) = (if c then u else v)"
   272   (fn rew::prems =>
   273    [stac rew 1, stac expand_if 1, stac expand_if 1,
   274     fast_tac (HOL_cs addDs prems) 1]);
   275 
   276 (*Prevents simplification of x and y: much faster*)
   277 val if_weak_cong = prove_goal HOL.thy
   278   "b=c ==> (if b then x else y) = (if c then x else y)"
   279   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   280 
   281 (*Prevents simplification of t: much faster*)
   282 val let_weak_cong = prove_goal HOL.thy
   283   "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
   284   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   285 
   286 end;
   287 
   288 fun prove nm thm  = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]);
   289 
   290 prove "conj_commute" "(P&Q) = (Q&P)";
   291 prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
   292 val conj_comms = [conj_commute, conj_left_commute];
   293 
   294 prove "disj_commute" "(P|Q) = (Q|P)";
   295 prove "disj_left_commute" "(P|(Q|R)) = (Q|(P|R))";
   296 val disj_comms = [disj_commute, disj_left_commute];
   297 
   298 prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
   299 prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
   300 
   301 prove "disj_conj_distribL" "(P|(Q&R)) = ((P|Q) & (P|R))";
   302 prove "disj_conj_distribR" "((P&Q)|R) = ((P|R) & (Q|R))";
   303 
   304 prove "imp_conj_distrib" "(P --> (Q&R)) = ((P-->Q) & (P-->R))";
   305 prove "imp_conj"         "((P&Q)-->R)   = (P --> (Q --> R))";
   306 
   307 prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
   308 prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
   309 prove "not_iff" "(P~=Q) = (P = (~Q))";
   310 
   311 prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))";
   312 prove "imp_all" "((! x. P x) --> Q) = (? x. P x --> Q)";
   313 prove "not_ex"  "(~ (? x.P(x))) = (! x.~P(x))";
   314 prove "imp_ex" "((? x. P x) --> Q) = (! x. P x --> Q)";
   315 
   316 prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
   317 prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
   318 
   319 
   320 qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
   321   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   322 
   323 qed_goal "if_distrib" HOL.thy
   324   "f(if c then x else y) = (if c then f x else f y)" 
   325   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   326 
   327 qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = (f o g o h)"
   328   (fn _=>[rtac ext 1, rtac refl 1]);
   329 
   330 
   331 
   332 
   333 (*** Install simpsets and datatypes in theory structure ***)
   334 
   335 simpset := HOL_ss;
   336 
   337 exception SS_DATA of simpset;
   338 
   339 let fun merge [] = SS_DATA empty_ss
   340       | merge ss = let val ss = map (fn SS_DATA x => x) ss;
   341                    in SS_DATA (foldl merge_ss (hd ss, tl ss)) end;
   342 
   343     fun put (SS_DATA ss) = simpset := ss;
   344 
   345     fun get () = SS_DATA (!simpset);
   346 in add_thydata "HOL"
   347      ("simpset", ThyMethods {merge = merge, put = put, get = get})
   348 end;
   349 
   350 type dtype_info = {case_const:term, case_rewrites:thm list,
   351                    constructors:term list, nchotomy:thm, case_cong:thm};
   352 
   353 exception DT_DATA of (string * dtype_info) list;
   354 val datatypes = ref [] : (string * dtype_info) list ref;
   355 
   356 let fun merge [] = DT_DATA []
   357       | merge ds =
   358           let val ds = map (fn DT_DATA x => x) ds;
   359           in DT_DATA (foldl (gen_union eq_fst) (hd ds, tl ds)) end;
   360 
   361     fun put (DT_DATA ds) = datatypes := ds;
   362 
   363     fun get () = DT_DATA (!datatypes);
   364 in add_thydata "HOL"
   365      ("datatypes", ThyMethods {merge = merge, put = put, get = get})
   366 end;
   367 
   368 
   369 add_thy_reader_file "thy_data.ML";