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