src/HOL/simpdata.ML
author berghofe
Mon May 06 15:21:05 1996 +0200 (1996-05-06 ago)
changeset 1722 bb326972ede6
parent 1660 8cb42cd97579
child 1758 60613b065e9b
permissions -rw-r--r--
Added split_inside_tac.
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
clasohm@923
    11
local
clasohm@923
    12
clasohm@923
    13
fun prover s = prove_goal HOL.thy s (fn _ => [fast_tac HOL_cs 1]);
clasohm@923
    14
clasohm@923
    15
val P_imp_P_iff_True = prover "P --> (P = True)" RS mp;
clasohm@923
    16
val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
clasohm@923
    17
clasohm@923
    18
val not_P_imp_P_iff_F = prover "~P --> (P = False)" RS mp;
clasohm@923
    19
val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection;
clasohm@923
    20
clasohm@923
    21
fun atomize pairs =
clasohm@923
    22
  let fun atoms th =
clasohm@923
    23
        (case concl_of th of
clasohm@923
    24
           Const("Trueprop",_) $ p =>
clasohm@923
    25
             (case head_of p of
clasohm@923
    26
                Const(a,_) =>
clasohm@923
    27
                  (case assoc(pairs,a) of
clasohm@923
    28
                     Some(rls) => flat (map atoms ([th] RL rls))
clasohm@923
    29
                   | None => [th])
clasohm@923
    30
              | _ => [th])
clasohm@923
    31
         | _ => [th])
clasohm@923
    32
  in atoms end;
clasohm@923
    33
clasohm@923
    34
fun mk_meta_eq r = case concl_of r of
clasohm@1465
    35
        Const("==",_)$_$_ => r
clasohm@1465
    36
    |   _$(Const("op =",_)$_$_) => r RS eq_reflection
clasohm@1465
    37
    |   _$(Const("not",_)$_) => r RS not_P_imp_P_eq_False
clasohm@923
    38
    |   _ => r RS P_imp_P_eq_True;
clasohm@923
    39
(* last 2 lines requires all formulae to be of the from Trueprop(.) *)
clasohm@923
    40
clasohm@923
    41
fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
clasohm@923
    42
clasohm@923
    43
val imp_cong = impI RSN
clasohm@923
    44
    (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
clasohm@1465
    45
        (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
clasohm@923
    46
clasohm@923
    47
val o_apply = prove_goalw HOL.thy [o_def] "(f o g)(x) = f(g(x))"
clasohm@923
    48
 (fn _ => [rtac refl 1]);
clasohm@923
    49
clasohm@923
    50
val simp_thms = map prover
clasohm@923
    51
 [ "(x=x) = True",
clasohm@923
    52
   "(~True) = False", "(~False) = True", "(~ ~ P) = P",
clasohm@923
    53
   "(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))",
clasohm@923
    54
   "(True=P) = P", "(P=True) = P",
clasohm@923
    55
   "(True --> P) = P", "(False --> P) = True", 
clasohm@923
    56
   "(P --> True) = True", "(P --> P) = True",
clasohm@923
    57
   "(P --> False) = (~P)", "(P --> ~P) = (~P)",
clasohm@923
    58
   "(P & True) = P", "(True & P) = P", 
clasohm@923
    59
   "(P & False) = False", "(False & P) = False", "(P & P) = P",
clasohm@923
    60
   "(P | True) = True", "(True | P) = True", 
clasohm@923
    61
   "(P | False) = P", "(False | P) = P", "(P | P) = P",
clasohm@923
    62
   "(!x.P) = P", "(? x.P) = P", "? x. x=t", "(? x. x=t & P(x)) = P(t)",
clasohm@923
    63
   "(P|Q --> R) = ((P-->R)&(Q-->R))" ];
clasohm@923
    64
clasohm@923
    65
in
clasohm@923
    66
clasohm@923
    67
val meta_eq_to_obj_eq = prove_goal HOL.thy "x==y ==> x=y"
clasohm@923
    68
  (fn [prem] => [rewtac prem, rtac refl 1]);
clasohm@923
    69
clasohm@923
    70
val eq_sym_conv = prover "(x=y) = (y=x)";
clasohm@923
    71
clasohm@923
    72
val conj_assoc = prover "((P&Q)&R) = (P&(Q&R))";
clasohm@923
    73
clasohm@965
    74
val if_True = prove_goalw HOL.thy [if_def] "(if True then x else y) = x"
clasohm@923
    75
 (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
clasohm@923
    76
clasohm@965
    77
val if_False = prove_goalw HOL.thy [if_def] "(if False then x else y) = y"
clasohm@923
    78
 (fn _=>[fast_tac (HOL_cs addIs [select_equality]) 1]);
clasohm@923
    79
clasohm@965
    80
val if_P = prove_goal HOL.thy "P ==> (if P then x else y) = x"
clasohm@923
    81
 (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
clasohm@923
    82
clasohm@965
    83
val if_not_P = prove_goal HOL.thy "~P ==> (if P then x else y) = y"
clasohm@923
    84
 (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
clasohm@923
    85
clasohm@923
    86
val expand_if = prove_goal HOL.thy
clasohm@965
    87
    "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
clasohm@923
    88
 (fn _=> [ (res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1),
clasohm@1465
    89
         rtac (if_P RS ssubst) 2,
clasohm@1465
    90
         rtac (if_not_P RS ssubst) 1,
clasohm@1465
    91
         REPEAT(fast_tac HOL_cs 1) ]);
clasohm@923
    92
clasohm@965
    93
val if_bool_eq = prove_goal HOL.thy
clasohm@965
    94
                   "(if P then Q else R) = ((P-->Q) & (~P-->R))"
clasohm@965
    95
                   (fn _ => [rtac expand_if 1]);
clasohm@923
    96
lcp@988
    97
(*Add congruence rules for = (instead of ==) *)
lcp@988
    98
infix 4 addcongs;
clasohm@923
    99
fun ss addcongs congs = ss addeqcongs (congs RL [eq_reflection]);
clasohm@923
   100
clasohm@1264
   101
fun Addcongs congs = (simpset := !simpset addcongs congs);
clasohm@1264
   102
lcp@988
   103
(*Add a simpset to a classical set!*)
lcp@988
   104
infix 4 addss;
lcp@988
   105
fun cs addss ss = cs addbefore asm_full_simp_tac ss 1;
lcp@988
   106
clasohm@923
   107
val mksimps_pairs =
clasohm@923
   108
  [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
clasohm@923
   109
   ("All", [spec]), ("True", []), ("False", []),
clasohm@965
   110
   ("If", [if_bool_eq RS iffD1])];
clasohm@923
   111
clasohm@923
   112
fun mksimps pairs = map mk_meta_eq o atomize pairs o gen_all;
clasohm@923
   113
clasohm@923
   114
val HOL_ss = empty_ss
clasohm@923
   115
      setmksimps (mksimps mksimps_pairs)
clasohm@923
   116
      setsolver (fn prems => resolve_tac (TrueI::refl::prems) ORELSE' atac
clasohm@923
   117
                             ORELSE' etac FalseE)
clasohm@923
   118
      setsubgoaler asm_simp_tac
clasohm@923
   119
      addsimps ([if_True, if_False, o_apply, conj_assoc] @ simp_thms)
clasohm@923
   120
      addcongs [imp_cong];
clasohm@923
   121
nipkow@941
   122
local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
nipkow@941
   123
in
nipkow@941
   124
fun split_tac splits = mktac (map mk_meta_eq splits)
nipkow@941
   125
end;
nipkow@941
   126
berghofe@1722
   127
local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
berghofe@1722
   128
in
berghofe@1722
   129
fun split_inside_tac splits = mktac (map mk_meta_eq splits)
berghofe@1722
   130
end;
berghofe@1722
   131
clasohm@923
   132
clasohm@923
   133
(* eliminiation of existential quantifiers in assumptions *)
clasohm@923
   134
clasohm@923
   135
val ex_all_equiv =
clasohm@923
   136
  let val lemma1 = prove_goal HOL.thy
clasohm@923
   137
        "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
clasohm@923
   138
        (fn prems => [resolve_tac prems 1, etac exI 1]);
clasohm@923
   139
      val lemma2 = prove_goalw HOL.thy [Ex_def]
clasohm@923
   140
        "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
clasohm@923
   141
        (fn prems => [REPEAT(resolve_tac prems 1)])
clasohm@923
   142
  in equal_intr lemma1 lemma2 end;
clasohm@923
   143
clasohm@923
   144
(* '&' congruence rule: not included by default!
clasohm@923
   145
   May slow rewrite proofs down by as much as 50% *)
clasohm@923
   146
clasohm@923
   147
val conj_cong = impI RSN
clasohm@923
   148
    (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
clasohm@1465
   149
        (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
clasohm@923
   150
nipkow@1548
   151
val rev_conj_cong = impI RSN
nipkow@1548
   152
    (2, prove_goal HOL.thy "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
nipkow@1548
   153
        (fn _=> [fast_tac HOL_cs 1]) RS mp RS mp);
nipkow@1548
   154
clasohm@923
   155
(** 'if' congruence rules: neither included by default! *)
clasohm@923
   156
clasohm@923
   157
(*Simplifies x assuming c and y assuming ~c*)
clasohm@923
   158
val if_cong = prove_goal HOL.thy
clasohm@965
   159
  "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
clasohm@965
   160
\  (if b then x else y) = (if c then u else v)"
clasohm@923
   161
  (fn rew::prems =>
clasohm@923
   162
   [stac rew 1, stac expand_if 1, stac expand_if 1,
clasohm@923
   163
    fast_tac (HOL_cs addDs prems) 1]);
clasohm@923
   164
clasohm@923
   165
(*Prevents simplification of x and y: much faster*)
clasohm@923
   166
val if_weak_cong = prove_goal HOL.thy
clasohm@965
   167
  "b=c ==> (if b then x else y) = (if c then x else y)"
clasohm@923
   168
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
clasohm@923
   169
clasohm@923
   170
(*Prevents simplification of t: much faster*)
clasohm@923
   171
val let_weak_cong = prove_goal HOL.thy
clasohm@923
   172
  "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
clasohm@923
   173
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
clasohm@923
   174
clasohm@923
   175
end;
clasohm@923
   176
clasohm@923
   177
fun prove nm thm  = qed_goal nm HOL.thy thm (fn _ => [fast_tac HOL_cs 1]);
clasohm@923
   178
clasohm@923
   179
prove "conj_commute" "(P&Q) = (Q&P)";
clasohm@923
   180
prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
clasohm@923
   181
val conj_comms = [conj_commute, conj_left_commute];
clasohm@923
   182
clasohm@923
   183
prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
clasohm@923
   184
prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
nipkow@1485
   185
nipkow@1485
   186
prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
nipkow@1485
   187
prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
nipkow@1485
   188
oheimb@1660
   189
prove "not_all" "(~ (! x.P(x))) = (? x.~P(x))";
oheimb@1660
   190
prove "not_ex"  "(~ (? x.P(x))) = (! x.~P(x))";
oheimb@1660
   191
nipkow@1655
   192
prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
nipkow@1655
   193
prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
nipkow@1655
   194
nipkow@1655
   195
qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
nipkow@1655
   196
  (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
nipkow@1655
   197
nipkow@1655
   198
qed_goal "if_distrib" HOL.thy
nipkow@1655
   199
  "f(if c then x else y) = (if c then f x else f y)" 
nipkow@1655
   200
  (fn _ => [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
nipkow@1655
   201
nipkow@1655
   202
qed_goalw "o_assoc" HOL.thy [o_def] "(f o g) o h = (f o g o h)"
nipkow@1655
   203
  (fn _=>[rtac ext 1, rtac refl 1]);