src/HOL/simpdata.ML
author paulson
Mon Aug 19 13:06:30 1996 +0200 (1996-08-19)
changeset 1922 ce495557ac33
parent 1892 23765bc3e8e2
child 1948 78e5bfcbc1e9
permissions -rw-r--r--
Installation of auto_tac; re-organization
     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 open Simplifier;
    10 
    11 (*** Integration of simplifier with classical reasoner ***)
    12 
    13 (*Add a simpset to a classical set!*)
    14 infix 4 addss;
    15 fun cs addss ss = cs addbefore asm_full_simp_tac ss 1;
    16 
    17 fun Addss ss = (claset := !claset addbefore asm_full_simp_tac ss 1);
    18 
    19 (*Maybe swap the safe_tac and simp_tac lines?**)
    20 fun auto_tac (cs,ss) = 
    21     TRY (safe_tac cs) THEN 
    22     ALLGOALS (asm_full_simp_tac ss) THEN
    23     REPEAT (FIRSTGOAL (best_tac (cs addss ss)));
    24 
    25 fun Auto_tac() = auto_tac (!claset, !simpset);
    26 
    27 fun auto() = by (Auto_tac());
    28 
    29 
    30 local
    31 
    32   fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
    33 
    34   val P_imp_P_iff_True = prover "P --> (P = True)" RS mp;
    35   val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
    36 
    37   val not_P_imp_P_iff_F = prover "~P --> (P = False)" RS mp;
    38   val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection;
    39 
    40   fun atomize pairs =
    41     let fun atoms th =
    42 	  (case concl_of th of
    43 	     Const("Trueprop",_) $ p =>
    44 	       (case head_of p of
    45 		  Const(a,_) =>
    46 		    (case assoc(pairs,a) of
    47 		       Some(rls) => flat (map atoms ([th] RL rls))
    48 		     | None => [th])
    49 		| _ => [th])
    50 	   | _ => [th])
    51     in atoms end;
    52 
    53   fun mk_meta_eq r = case concl_of r of
    54 	  Const("==",_)$_$_ => r
    55       |   _$(Const("op =",_)$_$_) => r RS eq_reflection
    56       |   _$(Const("not",_)$_) => r RS not_P_imp_P_eq_False
    57       |   _ => r RS P_imp_P_eq_True;
    58   (* last 2 lines requires all formulae to be of the from Trueprop(.) *)
    59 
    60   fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
    61 
    62   val simp_thms = map prover
    63    [ "(x=x) = True",
    64      "(~True) = False", "(~False) = True", "(~ ~ P) = P",
    65      "(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))",
    66      "(True=P) = P", "(P=True) = P",
    67      "(True --> P) = P", "(False --> P) = True", 
    68      "(P --> True) = True", "(P --> P) = True",
    69      "(P --> False) = (~P)", "(P --> ~P) = (~P)",
    70      "(P & True) = P", "(True & P) = P", 
    71      "(P & False) = False", "(False & P) = False", "(P & P) = P",
    72      "(P | True) = True", "(True | P) = True", 
    73      "(P | False) = P", "(False | P) = P", "(P | P) = P",
    74      "(!x.P) = P", "(? x.P) = P", "? x. x=t", 
    75      "(? x. x=t & P(x)) = P(t)", "(! x. x=t --> P(x)) = P(t)" ];
    76 
    77 in
    78 
    79 val meta_eq_to_obj_eq = prove_goal HOL.thy "x==y ==> x=y"
    80   (fn [prem] => [rewtac prem, rtac refl 1]);
    81 
    82 val eq_sym_conv = prover "(x=y) = (y=x)";
    83 
    84 val conj_assoc = prover "((P&Q)&R) = (P&(Q&R))";
    85 
    86 val disj_assoc = prover "((P|Q)|R) = (P|(Q|R))";
    87 
    88 val imp_disj   = prover "(P|Q --> R) = ((P-->R)&(Q-->R))";
    89 
    90 
    91 val if_True = prove_goalw HOL.thy [if_def] "(if True then x else y) = x"
    92  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
    93 
    94 val if_False = prove_goalw HOL.thy [if_def] "(if False then x else y) = y"
    95  (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
    96 
    97 val if_P = prove_goal HOL.thy "P ==> (if P then x else y) = x"
    98  (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
    99 
   100 val if_not_P = prove_goal HOL.thy "~P ==> (if P then x else y) = y"
   101  (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
   102 
   103 val expand_if = prove_goal HOL.thy
   104     "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
   105  (fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1),
   106          rtac (if_P RS ssubst) 2,
   107          rtac (if_not_P RS ssubst) 1,
   108          REPEAT(fast_tac HOL_cs 1) ]);
   109 
   110 val if_bool_eq = prove_goal HOL.thy
   111                    "(if P then Q else R) = ((P-->Q) & (~P-->R))"
   112                    (fn _ => [rtac expand_if 1]);
   113 
   114 (*Add congruence rules for = (instead of ==) *)
   115 infix 4 addcongs;
   116 fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]);
   117 
   118 fun Addcongs congs = (simpset := !simpset addcongs congs);
   119 
   120 val mksimps_pairs =
   121   [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
   122    ("All", [spec]), ("True", []), ("False", []),
   123    ("If", [if_bool_eq RS iffD1])];
   124 
   125 fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all;
   126 
   127 val imp_cong = impI RSN
   128     (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
   129         (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
   130 
   131 val o_apply = prove_goalw HOL.thy [o_def] "(f o g)(x) = f(g(x))"
   132  (fn _ => [rtac refl 1]);
   133 
   134 val HOL_ss = empty_ss
   135       setmksimps (mksimps mksimps_pairs)
   136       setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac
   137                              ORELSE' etac FalseE)
   138       setsubgoaler asm_simp_tac
   139       addsimps ([if_True, if_False, o_apply, imp_disj, conj_assoc, disj_assoc]
   140         @ simp_thms)
   141       addcongs [imp_cong];
   142 
   143 
   144 (*In general it seems wrong to add distributive laws by default: they
   145   might cause exponential blow-up.  This one has been added for a while
   146   and cannot be removed without affecting existing proofs.  Moreover, 
   147   rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
   148   grounds that it allows simplification of R in the two cases.*)
   149 
   150 
   151 local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
   152 in
   153 fun split_tac splits = mktac (map mk_meta_eq splits)
   154 end;
   155 
   156 local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
   157 in
   158 fun split_inside_tac splits = mktac (map mk_meta_eq splits)
   159 end;
   160 
   161 
   162 (* eliminiation of existential quantifiers in assumptions *)
   163 
   164 val ex_all_equiv =
   165   let val lemma1 = prove_goal HOL.thy
   166         "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
   167         (fn prems => [resolve_tac prems 1, etac exI 1]);
   168       val lemma2 = prove_goalw HOL.thy [Ex_def]
   169         "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
   170         (fn prems => [REPEAT(resolve_tac prems 1)])
   171   in equal_intr lemma1 lemma2 end;
   172 
   173 (* '&' congruence rule: not included by default!
   174    May slow rewrite proofs down by as much as 50% *)
   175 
   176 val conj_cong = impI RSN
   177     (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
   178         (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
   179 
   180 val rev_conj_cong = impI RSN
   181     (2, prove_goal HOL.thy "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
   182         (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
   183 
   184 (** 'if' congruence rules: neither included by default! *)
   185 
   186 (*Simplifies x assuming c and y assuming ~c*)
   187 val if_cong = prove_goal HOL.thy
   188   "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
   189 \  (if b then x else y) = (if c then u else v)"
   190   (fn rew::prems =>
   191    [stac rew 1, stac expand_if 1, stac expand_if 1,
   192     fast_tac (HOL_cs addDs prems) 1]);
   193 
   194 (*Prevents simplification of x and y: much faster*)
   195 val if_weak_cong = prove_goal HOL.thy
   196   "b=c ==> (if b then x else y) = (if c then x else y)"
   197   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   198 
   199 (*Prevents simplification of t: much faster*)
   200 val let_weak_cong = prove_goal HOL.thy
   201   "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
   202   (fn [prem] => [rtac (prem RS arg_cong) 1]);
   203 
   204 end;
   205 
   206 fun prove nm thm  = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]);
   207 
   208 prove "conj_commute" "(P&Q) = (Q&P)";
   209 prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
   210 val conj_comms = [conj_commute, conj_left_commute];
   211 
   212 prove "disj_commute" "(P|Q) = (Q|P)";
   213 prove "disj_left_commute" "(P|(Q|R)) = (Q|(P|R))";
   214 val disj_comms = [disj_commute, disj_left_commute];
   215 
   216 prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
   217 prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
   218 
   219 prove "disj_conj_distribL" "(P|(Q&R)) = ((P|Q) & (P|R))";
   220 prove "disj_conj_distribR" "((P&Q)|R) = ((P|R) & (Q|R))";
   221 
   222 prove "imp_conj_distrib" "(P --> (Q&R)) = ((P-->Q) & (P-->R))";
   223 prove "imp_conj"         "((P&Q)-->R)   = (P --> (Q --> R))";
   224 
   225 prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
   226 prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
   227 prove "not_iff" "(P~=Q) = (P = (~Q))";
   228 
   229 prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))";
   230 prove "imp_all" "((! x. P x) --> Q) = (? x. P x --> Q)";
   231 prove "not_ex"  "(~ (? x.P(x))) = (! x.~P(x))";
   232 prove "imp_ex" "((? x. P x) --> Q) = (! x. P x --> Q)";
   233 
   234 prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
   235 prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
   236 
   237 
   238 qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
   239   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   240 
   241 qed_goal "if_distrib" HOL.thy
   242   "f(if c then x else y) = (if c then f x else f y)" 
   243   (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
   244 
   245 qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = (f o g o h)"
   246   (fn _=>[rtac ext 1, rtac refl 1]);