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