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
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@1922
    74
     "(!x.P) = P", "(? x.P) = P", "? x. x=t", 
paulson@1922
    75
     "(? x. x=t & P(x)) = P(t)", "(! x. x=t --> P(x)) = P(t)" ];
clasohm@923
    76
clasohm@923
    77
in
clasohm@923
    78
clasohm@923
    79
val meta_eq_to_obj_eq = prove_goal HOL.thy "x==y ==> x=y"
clasohm@923
    80
  (fn [prem] => [rewtac prem, rtac refl 1]);
clasohm@923
    81
clasohm@923
    82
val eq_sym_conv = prover "(x=y) = (y=x)";
clasohm@923
    83
clasohm@923
    84
val conj_assoc = prover "((P&Q)&R) = (P&(Q&R))";
clasohm@923
    85
paulson@1922
    86
val disj_assoc = prover "((P|Q)|R) = (P|(Q|R))";
paulson@1922
    87
paulson@1922
    88
val imp_disj   = prover "(P|Q --> R) = ((P-->R)&(Q-->R))";
paulson@1922
    89
paulson@1922
    90
clasohm@965
    91
val if_True = prove_goalw HOL.thy [if_def] "(if True then x else y) = x"
clasohm@923
    92
 (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
clasohm@923
    93
clasohm@965
    94
val if_False = prove_goalw HOL.thy [if_def] "(if False then x else y) = y"
clasohm@923
    95
 (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
clasohm@923
    96
clasohm@965
    97
val if_P = prove_goal HOL.thy "P ==> (if P then x else y) = x"
clasohm@923
    98
 (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
clasohm@923
    99
clasohm@965
   100
val if_not_P = prove_goal HOL.thy "~P ==> (if P then x else y) = y"
clasohm@923
   101
 (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
clasohm@923
   102
clasohm@923
   103
val expand_if = prove_goal HOL.thy
clasohm@965
   104
    "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
clasohm@923
   105
 (fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1),
clasohm@1465
   106
         rtac (if_P RS ssubst) 2,
clasohm@1465
   107
         rtac (if_not_P RS ssubst) 1,
clasohm@1465
   108
         REPEAT(fast_tac HOL_cs 1) ]);
clasohm@923
   109
clasohm@965
   110
val if_bool_eq = prove_goal HOL.thy
clasohm@965
   111
                   "(if P then Q else R) = ((P-->Q) & (~P-->R))"
clasohm@965
   112
                   (fn _ => [rtac expand_if 1]);
clasohm@923
   113
lcp@988
   114
(*Add congruence rules for = (instead of ==) *)
lcp@988
   115
infix 4 addcongs;
clasohm@923
   116
fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]);
clasohm@923
   117
clasohm@1264
   118
fun Addcongs congs = (simpset := !simpset addcongs congs);
clasohm@1264
   119
clasohm@923
   120
val mksimps_pairs =
clasohm@923
   121
  [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
clasohm@923
   122
   ("All", [spec]), ("True", []), ("False", []),
clasohm@965
   123
   ("If", [if_bool_eq RS iffD1])];
clasohm@923
   124
clasohm@923
   125
fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all;
clasohm@923
   126
paulson@1922
   127
val imp_cong = impI RSN
paulson@1922
   128
    (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
paulson@1922
   129
        (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
paulson@1922
   130
paulson@1922
   131
val o_apply = prove_goalw HOL.thy [o_def] "(f o g)(x) = f(g(x))"
paulson@1922
   132
 (fn _ => [rtac refl 1]);
paulson@1922
   133
clasohm@923
   134
val HOL_ss = empty_ss
clasohm@923
   135
      setmksimps (mksimps mksimps_pairs)
clasohm@923
   136
      setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac
clasohm@923
   137
                             ORELSE' etac FalseE)
clasohm@923
   138
      setsubgoaler asm_simp_tac
paulson@1922
   139
      addsimps ([if_True, if_False, o_apply, imp_disj, conj_assoc, disj_assoc]
paulson@1922
   140
        @ simp_thms)
clasohm@923
   141
      addcongs [imp_cong];
clasohm@923
   142
paulson@1922
   143
paulson@1922
   144
(*In general it seems wrong to add distributive laws by default: they
paulson@1922
   145
  might cause exponential blow-up.  This one has been added for a while
paulson@1922
   146
  and cannot be removed without affecting existing proofs.  Moreover, 
paulson@1922
   147
  rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
paulson@1922
   148
  grounds that it allows simplification of R in the two cases.*)
paulson@1922
   149
paulson@1922
   150
nipkow@941
   151
local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
nipkow@941
   152
in
nipkow@941
   153
fun split_tac splits = mktac (map mk_meta_eq splits)
nipkow@941
   154
end;
nipkow@941
   155
berghofe@1722
   156
local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
berghofe@1722
   157
in
berghofe@1722
   158
fun split_inside_tac splits = mktac (map mk_meta_eq splits)
berghofe@1722
   159
end;
berghofe@1722
   160
clasohm@923
   161
clasohm@923
   162
(* eliminiation of existential quantifiers in assumptions *)
clasohm@923
   163
clasohm@923
   164
val ex_all_equiv =
clasohm@923
   165
  let val lemma1 = prove_goal HOL.thy
clasohm@923
   166
        "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
clasohm@923
   167
        (fn prems => [resolve_tac prems 1, etac exI 1]);
clasohm@923
   168
      val lemma2 = prove_goalw HOL.thy [Ex_def]
clasohm@923
   169
        "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
clasohm@923
   170
        (fn prems => [REPEAT(resolve_tac prems 1)])
clasohm@923
   171
  in equal_intr lemma1 lemma2 end;
clasohm@923
   172
clasohm@923
   173
(* '&' congruence rule: not included by default!
clasohm@923
   174
   May slow rewrite proofs down by as much as 50% *)
clasohm@923
   175
clasohm@923
   176
val conj_cong = impI RSN
clasohm@923
   177
    (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
clasohm@1465
   178
        (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
clasohm@923
   179
nipkow@1548
   180
val rev_conj_cong = impI RSN
nipkow@1548
   181
    (2, prove_goal HOL.thy "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
nipkow@1548
   182
        (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
nipkow@1548
   183
clasohm@923
   184
(** 'if' congruence rules: neither included by default! *)
clasohm@923
   185
clasohm@923
   186
(*Simplifies x assuming c and y assuming ~c*)
clasohm@923
   187
val if_cong = prove_goal HOL.thy
clasohm@965
   188
  "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
clasohm@965
   189
\  (if b then x else y) = (if c then u else v)"
clasohm@923
   190
  (fn rew::prems =>
clasohm@923
   191
   [stac rew 1, stac expand_if 1, stac expand_if 1,
clasohm@923
   192
    fast_tac (HOL_cs addDs prems) 1]);
clasohm@923
   193
clasohm@923
   194
(*Prevents simplification of x and y: much faster*)
clasohm@923
   195
val if_weak_cong = prove_goal HOL.thy
clasohm@965
   196
  "b=c ==> (if b then x else y) = (if c then x else y)"
clasohm@923
   197
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
clasohm@923
   198
clasohm@923
   199
(*Prevents simplification of t: much faster*)
clasohm@923
   200
val let_weak_cong = prove_goal HOL.thy
clasohm@923
   201
  "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
clasohm@923
   202
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
clasohm@923
   203
clasohm@923
   204
end;
clasohm@923
   205
clasohm@923
   206
fun prove nm thm  = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]);
clasohm@923
   207
clasohm@923
   208
prove "conj_commute" "(P&Q) = (Q&P)";
clasohm@923
   209
prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
clasohm@923
   210
val conj_comms = [conj_commute, conj_left_commute];
clasohm@923
   211
paulson@1922
   212
prove "disj_commute" "(P|Q) = (Q|P)";
paulson@1922
   213
prove "disj_left_commute" "(P|(Q|R)) = (Q|(P|R))";
paulson@1922
   214
val disj_comms = [disj_commute, disj_left_commute];
paulson@1922
   215
clasohm@923
   216
prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
clasohm@923
   217
prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
nipkow@1485
   218
paulson@1892
   219
prove "disj_conj_distribL" "(P|(Q&R)) = ((P|Q) & (P|R))";
paulson@1892
   220
prove "disj_conj_distribR" "((P&Q)|R) = ((P|R) & (Q|R))";
paulson@1892
   221
paulson@1892
   222
prove "imp_conj_distrib" "(P --> (Q&R)) = ((P-->Q) & (P-->R))";
paulson@1922
   223
prove "imp_conj"         "((P&Q)-->R)   = (P --> (Q --> R))";
paulson@1892
   224
nipkow@1485
   225
prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
nipkow@1485
   226
prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
paulson@1922
   227
prove "not_iff" "(P~=Q) = (P = (~Q))";
nipkow@1485
   228
oheimb@1660
   229
prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))";
paulson@1922
   230
prove "imp_all" "((! x. P x) --> Q) = (? x. P x --> Q)";
oheimb@1660
   231
prove "not_ex"  "(~ (? x.P(x))) = (! x.~P(x))";
paulson@1922
   232
prove "imp_ex" "((? x. P x) --> Q) = (! x. P x --> Q)";
oheimb@1660
   233
nipkow@1655
   234
prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
nipkow@1655
   235
prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
nipkow@1655
   236
nipkow@1758
   237
nipkow@1655
   238
qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
nipkow@1655
   239
  (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
nipkow@1655
   240
nipkow@1655
   241
qed_goal "if_distrib" HOL.thy
nipkow@1655
   242
  "f(if c then x else y) = (if c then f x else f y)" 
nipkow@1655
   243
  (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
nipkow@1655
   244
pusch@1874
   245
qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = (f o g o h)"
nipkow@1655
   246
  (fn _=>[rtac ext 1, rtac refl 1]);