src/HOL/simpdata.ML
author paulson
Fri Mar 07 10:20:26 1997 +0100 (1997-03-07)
changeset 2748 3ae9ccdd701e
parent 2718 460fd0f8d478
child 2800 9741c4c6b62b
permissions -rw-r--r--
Eta-expanded some declarations for compatibility with value polymorphism
     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 (*** Addition of rules to simpsets and clasets simultaneously ***)
    14 
    15 (*Takes UNCONDITIONAL theorems of the form A<->B to 
    16         the Safe Intr     rule B==>A and 
    17         the Safe Destruct rule A==>B.
    18   Also ~A goes to the Safe Elim rule A ==> ?R
    19   Failing other cases, A is added as a Safe Intr rule*)
    20 local
    21   val iff_const = HOLogic.eq_const HOLogic.boolT;
    22 
    23   fun addIff th = 
    24       (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
    25                 (Const("Not",_) $ A) =>
    26                     AddSEs [zero_var_indexes (th RS notE)]
    27               | (con $ _ $ _) =>
    28                     if con=iff_const
    29                     then (AddSIs [zero_var_indexes (th RS iffD2)];  
    30                           AddSDs [zero_var_indexes (th RS iffD1)])
    31                     else  AddSIs [th]
    32               | _ => AddSIs [th];
    33        Addsimps [th])
    34       handle _ => error ("AddIffs: theorem must be unconditional\n" ^ 
    35                          string_of_thm th)
    36 
    37   fun delIff th = 
    38       (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
    39                 (Const("Not",_) $ A) =>
    40                     Delrules [zero_var_indexes (th RS notE)]
    41               | (con $ _ $ _) =>
    42                     if con=iff_const
    43                     then Delrules [zero_var_indexes (th RS iffD2),
    44                                    zero_var_indexes (th RS iffD1)]
    45                     else Delrules [th]
    46               | _ => Delrules [th];
    47        Delsimps [th])
    48       handle _ => warning("DelIffs: ignoring conditional theorem\n" ^ 
    49                           string_of_thm th)
    50 in
    51 val AddIffs = seq addIff
    52 val DelIffs = seq delIff
    53 end;
    54 
    55 
    56 local
    57 
    58   fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
    59 
    60   val P_imp_P_iff_True = prover "P --> (P = True)" RS mp;
    61   val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
    62 
    63   val not_P_imp_P_iff_F = prover "~P --> (P = False)" RS mp;
    64   val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection;
    65 
    66   fun atomize pairs =
    67     let fun atoms th =
    68           (case concl_of th of
    69              Const("Trueprop",_) $ p =>
    70                (case head_of p of
    71                   Const(a,_) =>
    72                     (case assoc(pairs,a) of
    73                        Some(rls) => flat (map atoms ([th] RL rls))
    74                      | None => [th])
    75                 | _ => [th])
    76            | _ => [th])
    77     in atoms end;
    78 
    79   fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
    80 
    81 in
    82 
    83   fun mk_meta_eq r = case concl_of r of
    84           Const("==",_)$_$_ => r
    85       |   _$(Const("op =",_)$_$_) => r RS eq_reflection
    86       |   _$(Const("Not",_)$_) => r RS not_P_imp_P_eq_False
    87       |   _ => r RS P_imp_P_eq_True;
    88   (* last 2 lines requires all formulae to be of the from Trueprop(.) *)
    89 
    90 val simp_thms = map prover
    91  [ "(x=x) = True",
    92    "(~True) = False", "(~False) = True", "(~ ~ P) = P",
    93    "(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))",
    94    "(True=P) = P", "(P=True) = P",
    95    "(True --> P) = P", "(False --> P) = True", 
    96    "(P --> True) = True", "(P --> P) = True",
    97    "(P --> False) = (~P)", "(P --> ~P) = (~P)",
    98    "(P & True) = P", "(True & P) = P", 
    99    "(P & False) = False", "(False & P) = False", "(P & P) = P",
   100    "(P | True) = True", "(True | P) = True", 
   101    "(P | False) = P", "(False | P) = P", "(P | P) = P",
   102    "((~P) = (~Q)) = (P=Q)",
   103    "(!x.P) = P", "(? x.P) = P", "? x. x=t", "? x. t=x", 
   104    "(? x. x=t & P(x)) = P(t)", "(? x. t=x & P(x)) = P(t)", 
   105    "(! x. x=t --> P(x)) = P(t)", "(! x. t=x --> P(x)) = P(t)" ];
   106 
   107 (*Add congruence rules for = (instead of ==) *)
   108 infix 4 addcongs delcongs;
   109 fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]);
   110 fun ss delcongs congs = ss deleqcongs (congs RL [eq_reflection]);
   111 
   112 fun Addcongs congs = (simpset := !simpset addcongs congs);
   113 fun Delcongs congs = (simpset := !simpset delcongs congs);
   114 
   115 fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all;
   116 
   117 val imp_cong = impI RSN
   118     (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
   119         (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
   120 
   121 (*Miniscoping: pushing in existential quantifiers*)
   122 val ex_simps = map prover 
   123                 ["(EX x. P x & Q)   = ((EX x.P x) & Q)",
   124                  "(EX x. P & Q x)   = (P & (EX x.Q x))",
   125                  "(EX x. P x | Q)   = ((EX x.P x) | Q)",
   126                  "(EX x. P | Q x)   = (P | (EX x.Q x))",
   127                  "(EX x. P x --> Q) = ((ALL x.P x) --> Q)",
   128                  "(EX x. P --> Q x) = (P --> (EX x.Q x))"];
   129 
   130 (*Miniscoping: pushing in universal quantifiers*)
   131 val all_simps = map prover
   132                 ["(ALL x. P x & Q)   = ((ALL x.P x) & Q)",
   133                  "(ALL x. P & Q x)   = (P & (ALL x.Q x))",
   134                  "(ALL x. P x | Q)   = ((ALL x.P x) | Q)",
   135                  "(ALL x. P | Q x)   = (P | (ALL x.Q x))",
   136                  "(ALL x. P x --> Q) = ((EX x.P x) --> Q)",
   137                  "(ALL x. P --> Q x) = (P --> (ALL x.Q x))"];
   138 
   139 
   140 
   141 (* elimination of existential quantifiers in assumptions *)
   142 
   143 val ex_all_equiv =
   144   let val lemma1 = prove_goal HOL.thy
   145         "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
   146         (fn prems => [resolve_tac prems 1, etac exI 1]);
   147       val lemma2 = prove_goalw HOL.thy [Ex_def]
   148         "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
   149         (fn prems => [REPEAT(resolve_tac prems 1)])
   150   in equal_intr lemma1 lemma2 end;
   151 
   152 end;
   153 
   154 fun prove nm thm  = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]);
   155 
   156 prove "conj_commute" "(P&Q) = (Q&P)";
   157 prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
   158 val conj_comms = [conj_commute, conj_left_commute];
   159 prove "conj_assoc" "((P&Q)&R) = (P&(Q&R))";
   160 
   161 prove "disj_commute" "(P|Q) = (Q|P)";
   162 prove "disj_left_commute" "(P|(Q|R)) = (Q|(P|R))";
   163 val disj_comms = [disj_commute, disj_left_commute];
   164 prove "disj_assoc" "((P|Q)|R) = (P|(Q|R))";
   165 
   166 prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
   167 prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
   168 
   169 prove "disj_conj_distribL" "(P|(Q&R)) = ((P|Q) & (P|R))";
   170 prove "disj_conj_distribR" "((P&Q)|R) = ((P|R) & (Q|R))";
   171 
   172 prove "imp_conjR" "(P --> (Q&R)) = ((P-->Q) & (P-->R))";
   173 prove "imp_conjL" "((P&Q) -->R)  = (P --> (Q --> R))";
   174 prove "imp_disjL" "((P|Q) --> R) = ((P-->R)&(Q-->R))";
   175 
   176 prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
   177 prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
   178 prove "not_iff" "(P~=Q) = (P = (~Q))";
   179 
   180 (*Avoids duplication of subgoals after expand_if, when the true and false 
   181   cases boil down to the same thing.*) 
   182 prove "cases_simp" "((P --> Q) & (~P --> Q)) = Q";
   183 
   184 prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))";
   185 prove "imp_all" "((! x. P x) --> Q) = (? x. P x --> Q)";
   186 prove "not_ex"  "(~ (? x.P(x))) = (! x.~P(x))";
   187 prove "imp_ex" "((? x. P x) --> Q) = (! x. P x --> Q)";
   188 
   189 prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
   190 prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
   191 
   192 (* '&' congruence rule: not included by default!
   193    May slow rewrite proofs down by as much as 50% *)
   194 
   195 let val th = prove_goal HOL.thy 
   196                 "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
   197                 (fn _=> [fast_tac HOL_cs 1])
   198 in  bind_thm("conj_cong",standard (impI RSN (2, th RS mp RS mp)))  end;
   199 
   200 let val th = prove_goal HOL.thy 
   201                 "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
   202                 (fn _=> [fast_tac HOL_cs 1])
   203 in  bind_thm("rev_conj_cong",standard (impI RSN (2, th RS mp RS mp)))  end;
   204 
   205 (* '|' congruence rule: not included by default! *)
   206 
   207 let val th = prove_goal HOL.thy 
   208                 "(P=P')--> (~P'--> (Q=Q'))--> ((P|Q) = (P'|Q'))"
   209                 (fn _=> [fast_tac HOL_cs 1])
   210 in  bind_thm("disj_cong",standard (impI RSN (2, th RS mp RS mp)))  end;
   211 
   212 prove "eq_sym_conv" "(x=y) = (y=x)";
   213 
   214 qed_goalw "o_apply" HOL.thy [o_def] "(f o g) x = f (g x)"
   215  (fn _ => [rtac refl 1]);
   216 
   217 qed_goal "meta_eq_to_obj_eq" HOL.thy "x==y ==> x=y"
   218   (fn [prem] => [rewtac prem, rtac refl 1]);
   219 
   220 qed_goalw "if_True" HOL.thy [if_def] "(if True then x else y) = x"
   221  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
   222 
   223 qed_goalw "if_False" HOL.thy [if_def] "(if False then x else y) = y"
   224  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
   225 
   226 qed_goal "if_P" HOL.thy "P ==> (if P then x else y) = x"
   227  (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
   228 (*
   229 qed_goal "if_not_P" HOL.thy "~P ==> (if P then x else y) = y"
   230  (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
   231 *)
   232 qed_goalw "if_not_P" HOL.thy [if_def] "!!P. ~P ==> (if P then x else y) = y"
   233  (fn _ => [fast_tac (HOL_cs addIs [select_equality]) 1]);
   234 
   235 qed_goal "expand_if" HOL.thy
   236     "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
   237  (fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1),
   238          stac if_P 2,
   239          stac if_not_P 1,
   240          REPEAT(fast_tac HOL_cs 1) ]);
   241 
   242 qed_goal "if_bool_eq" HOL.thy
   243                    "(if P then Q else R) = ((P-->Q) & (~P-->R))"
   244                    (fn _ => [rtac expand_if 1]);
   245 
   246 local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
   247 in
   248 fun split_tac splits = mktac (map mk_meta_eq splits)
   249 end;
   250 
   251 local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
   252 in
   253 fun split_inside_tac splits = mktac (map mk_meta_eq splits)
   254 end;
   255 
   256 
   257 qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
   258   (fn _ => [split_tac [expand_if] 1, fast_tac HOL_cs 1]);
   259 
   260 (** 'if' congruence rules: neither included by default! *)
   261 
   262 (*Simplifies x assuming c and y assuming ~c*)
   263 qed_goal "if_cong" HOL.thy
   264   "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
   265 \  (if b then x else y) = (if c then u else v)"
   266   (fn rew::prems =>
   267    [stac rew 1, stac expand_if 1, stac expand_if 1,
   268     fast_tac (HOL_cs addDs prems) 1]);
   269 
   270 (*Prevents simplification of x and y: much faster*)
   271 qed_goal "if_weak_cong" HOL.thy
   272   "b=c ==> (if b then x else y) = (if c then x else y)"
   273   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   274 
   275 (*Prevents simplification of t: much faster*)
   276 qed_goal "let_weak_cong" HOL.thy
   277   "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
   278   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   279 
   280 (*In general it seems wrong to add distributive laws by default: they
   281   might cause exponential blow-up.  But imp_disjL has been in for a while
   282   and cannot be removed without affecting existing proofs.  Moreover, 
   283   rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
   284   grounds that it allows simplification of R in the two cases.*)
   285 
   286 val mksimps_pairs =
   287   [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
   288    ("All", [spec]), ("True", []), ("False", []),
   289    ("If", [if_bool_eq RS iffD1])];
   290 
   291 fun unsafe_solver prems = FIRST'[resolve_tac (TrueI::refl::prems),
   292 				 atac, etac FalseE];
   293 (*No premature instantiation of variables during simplification*)
   294 fun   safe_solver prems = FIRST'[match_tac (TrueI::refl::prems),
   295 				 eq_assume_tac, ematch_tac [FalseE]];
   296 
   297 val HOL_basic_ss = empty_ss setsubgoaler asm_simp_tac
   298 			    setSSolver   safe_solver
   299 			    setSolver  unsafe_solver
   300 			    setmksimps (mksimps mksimps_pairs);
   301 
   302 val HOL_ss = HOL_basic_ss addsimps ([triv_forall_equality, (* prunes params *)
   303 				     if_True, if_False, if_cancel,
   304 				     o_apply, imp_disjL, conj_assoc, disj_assoc,
   305 				     de_Morgan_conj, de_Morgan_disj, 
   306 				     not_all, not_ex, cases_simp]
   307 				    @ ex_simps @ all_simps @ simp_thms)
   308 			  addcongs [imp_cong];
   309 
   310 qed_goal "if_distrib" HOL.thy
   311   "f(if c then x else y) = (if c then f x else f y)" 
   312   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   313 
   314 qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = f o g o h"
   315   (fn _ => [rtac ext 1, rtac refl 1]);
   316 
   317 
   318 
   319 
   320 (*** Install simpsets and datatypes in theory structure ***)
   321 
   322 simpset := HOL_ss;
   323 
   324 exception SS_DATA of simpset;
   325 
   326 let fun merge [] = SS_DATA empty_ss
   327       | merge ss = let val ss = map (fn SS_DATA x => x) ss;
   328                    in SS_DATA (foldl merge_ss (hd ss, tl ss)) end;
   329 
   330     fun put (SS_DATA ss) = simpset := ss;
   331 
   332     fun get () = SS_DATA (!simpset);
   333 in add_thydata "HOL"
   334      ("simpset", ThyMethods {merge = merge, put = put, get = get})
   335 end;
   336 
   337 type dtype_info = {case_const:term, case_rewrites:thm list,
   338                    constructors:term list, nchotomy:thm, case_cong:thm};
   339 
   340 exception DT_DATA of (string * dtype_info) list;
   341 val datatypes = ref [] : (string * dtype_info) list ref;
   342 
   343 let fun merge [] = DT_DATA []
   344       | merge ds =
   345           let val ds = map (fn DT_DATA x => x) ds;
   346           in DT_DATA (foldl (gen_union eq_fst) (hd ds, tl ds)) end;
   347 
   348     fun put (DT_DATA ds) = datatypes := ds;
   349 
   350     fun get () = DT_DATA (!datatypes);
   351 in add_thydata "HOL"
   352      ("datatypes", ThyMethods {merge = merge, put = put, get = get})
   353 end;
   354 
   355 
   356 add_thy_reader_file "thy_data.ML";
   357 
   358 
   359 
   360 
   361 (*** Integration of simplifier with classical reasoner ***)
   362 
   363 (* rot_eq_tac rotates the first equality premise of subgoal i to the front,
   364    fails if there is no equaliy or if an equality is already at the front *)
   365 fun rot_eq_tac i = let
   366   fun is_eq (Const ("Trueprop", _) $ (Const("op =",_) $ _ $ _)) = true
   367   |   is_eq _ = false;
   368   fun find_eq n [] = None
   369   |   find_eq n (t :: ts) = if (is_eq t) then Some n else find_eq (n + 1) ts;
   370   fun rot_eq state = let val (_, _, Bi, _) = dest_state (state, i) in
   371 	    (case find_eq 0 (Logic.strip_assums_hyp Bi) of
   372 	      None   => no_tac
   373 	    | Some 0 => no_tac
   374 	    | Some n => rotate_tac n i) end;
   375 in STATE rot_eq end;
   376 
   377 (*an unsatisfactory fix for the incomplete asm_full_simp_tac!
   378   better: asm_really_full_simp_tac, a yet to be implemented version of
   379 			asm_full_simp_tac that applies all equalities in the
   380 			premises to all the premises *)
   381 fun safe_asm_more_full_simp_tac ss = TRY o rot_eq_tac THEN' 
   382 				     safe_asm_full_simp_tac ss;
   383 
   384 (*Add a simpset to a classical set!*)
   385 infix 4 addss;
   386 fun cs addss ss = cs addSaltern (CHANGED o (safe_asm_more_full_simp_tac ss));
   387 (*old version, for compatibility with unstable old proofs*)
   388 infix 4 unsafe_addss;
   389 fun cs unsafe_addss ss = cs addbefore asm_full_simp_tac ss;
   390 
   391 fun Addss ss = (claset := !claset addss ss);
   392 (*old version, for compatibility with unstable old proofs*)
   393 fun Unsafe_Addss ss = (claset := !claset unsafe_addss ss);
   394 
   395 (*Designed to be idempotent, except if best_tac instantiates variables
   396   in some of the subgoals*)
   397 (*old version, for compatibility with unstable old proofs*)
   398 fun unsafe_auto_tac (cs,ss) = 
   399     ALLGOALS (asm_full_simp_tac ss) THEN
   400     REPEAT   (safe_tac cs THEN ALLGOALS (asm_full_simp_tac ss)) THEN
   401     REPEAT   (FIRSTGOAL (best_tac (cs addss ss))) THEN
   402     prune_params_tac;
   403 
   404 type clasimpset = (claset * simpset);
   405 
   406 val HOL_css = (HOL_cs, HOL_ss);
   407 
   408 fun pair_upd1 f ((a,b),x) = (f(a,x), b);
   409 fun pair_upd2 f ((a,b),x) = (a, f(b,x));
   410 
   411 infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2
   412 	addsimps2 delsimps2 addcongs2 delcongs2;
   413 fun op addSIs2   arg = pair_upd1 (op addSIs) arg;
   414 fun op addSEs2   arg = pair_upd1 (op addSEs) arg;
   415 fun op addSDs2   arg = pair_upd1 (op addSDs) arg;
   416 fun op addIs2    arg = pair_upd1 (op addIs ) arg;
   417 fun op addEs2    arg = pair_upd1 (op addEs ) arg;
   418 fun op addDs2    arg = pair_upd1 (op addDs ) arg;
   419 fun op addsimps2 arg = pair_upd2 (op addsimps) arg;
   420 fun op delsimps2 arg = pair_upd2 (op delsimps) arg;
   421 fun op addcongs2 arg = pair_upd2 (op addcongs) arg;
   422 fun op delcongs2 arg = pair_upd2 (op delcongs) arg;
   423 
   424 fun auto_tac (cs,ss) = let val cs' = cs addss ss in
   425 EVERY [	TRY (safe_tac cs'),
   426 	REPEAT (FIRSTGOAL (fast_tac cs')),
   427 	prune_params_tac] end;
   428 
   429 fun Auto_tac () = auto_tac (!claset, !simpset);
   430 
   431 fun auto () = by (Auto_tac ());