src/HOL/simpdata.ML
author nipkow
Fri Feb 20 17:56:39 1998 +0100 (1998-02-20)
changeset 4640 ac6cf9f18653
parent 4633 d4a074973715
child 4651 70dd492a1698
permissions -rw-r--r--
Congruence rules use == in premises now.
New class linord.
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
paulson@1984
     9
section "Simplifier";
paulson@1984
    10
clasohm@923
    11
open Simplifier;
clasohm@923
    12
paulson@1984
    13
(*** Addition of rules to simpsets and clasets simultaneously ***)
paulson@1984
    14
paulson@1984
    15
(*Takes UNCONDITIONAL theorems of the form A<->B to 
paulson@2031
    16
        the Safe Intr     rule B==>A and 
paulson@2031
    17
        the Safe Destruct rule A==>B.
paulson@1984
    18
  Also ~A goes to the Safe Elim rule A ==> ?R
paulson@1984
    19
  Failing other cases, A is added as a Safe Intr rule*)
paulson@1984
    20
local
paulson@1984
    21
  val iff_const = HOLogic.eq_const HOLogic.boolT;
paulson@1984
    22
paulson@1984
    23
  fun addIff th = 
paulson@1984
    24
      (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
paulson@2718
    25
                (Const("Not",_) $ A) =>
paulson@2031
    26
                    AddSEs [zero_var_indexes (th RS notE)]
paulson@2031
    27
              | (con $ _ $ _) =>
paulson@2031
    28
                    if con=iff_const
paulson@2031
    29
                    then (AddSIs [zero_var_indexes (th RS iffD2)];  
paulson@2031
    30
                          AddSDs [zero_var_indexes (th RS iffD1)])
paulson@2031
    31
                    else  AddSIs [th]
paulson@2031
    32
              | _ => AddSIs [th];
paulson@1984
    33
       Addsimps [th])
paulson@1984
    34
      handle _ => error ("AddIffs: theorem must be unconditional\n" ^ 
paulson@2031
    35
                         string_of_thm th)
paulson@1984
    36
paulson@1984
    37
  fun delIff th = 
paulson@1984
    38
      (case HOLogic.dest_Trueprop (#prop(rep_thm th)) of
paulson@2718
    39
                (Const("Not",_) $ A) =>
paulson@2031
    40
                    Delrules [zero_var_indexes (th RS notE)]
paulson@2031
    41
              | (con $ _ $ _) =>
paulson@2031
    42
                    if con=iff_const
paulson@2031
    43
                    then Delrules [zero_var_indexes (th RS iffD2),
paulson@3518
    44
                                   make_elim (zero_var_indexes (th RS iffD1))]
paulson@2031
    45
                    else Delrules [th]
paulson@2031
    46
              | _ => Delrules [th];
paulson@1984
    47
       Delsimps [th])
paulson@1984
    48
      handle _ => warning("DelIffs: ignoring conditional theorem\n" ^ 
paulson@2031
    49
                          string_of_thm th)
paulson@1984
    50
in
paulson@1984
    51
val AddIffs = seq addIff
paulson@1984
    52
val DelIffs = seq delIff
paulson@1984
    53
end;
paulson@1984
    54
nipkow@4640
    55
qed_goal "meta_eq_to_obj_eq" HOL.thy "x==y ==> x=y"
nipkow@4640
    56
  (fn [prem] => [rewtac prem, rtac refl 1]);
nipkow@4640
    57
clasohm@923
    58
local
clasohm@923
    59
oheimb@4525
    60
  fun prover s = prove_goal HOL.thy s (K [blast_tac HOL_cs 1]);
clasohm@923
    61
paulson@1922
    62
  val P_imp_P_iff_True = prover "P --> (P = True)" RS mp;
paulson@1922
    63
  val P_imp_P_eq_True = P_imp_P_iff_True RS eq_reflection;
clasohm@923
    64
paulson@1922
    65
  val not_P_imp_P_iff_F = prover "~P --> (P = False)" RS mp;
paulson@1922
    66
  val not_P_imp_P_eq_False = not_P_imp_P_iff_F RS eq_reflection;
clasohm@923
    67
paulson@1922
    68
  fun atomize pairs =
paulson@1922
    69
    let fun atoms th =
paulson@2031
    70
          (case concl_of th of
paulson@2031
    71
             Const("Trueprop",_) $ p =>
paulson@2031
    72
               (case head_of p of
paulson@2031
    73
                  Const(a,_) =>
paulson@2031
    74
                    (case assoc(pairs,a) of
paulson@2031
    75
                       Some(rls) => flat (map atoms ([th] RL rls))
paulson@2031
    76
                     | None => [th])
paulson@2031
    77
                | _ => [th])
paulson@2031
    78
           | _ => [th])
paulson@1922
    79
    in atoms end;
clasohm@923
    80
nipkow@2134
    81
  fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
nipkow@2134
    82
nipkow@2134
    83
in
nipkow@2134
    84
nipkow@3896
    85
  fun mk_meta_eq r = r RS eq_reflection;
nipkow@3896
    86
nipkow@3896
    87
  fun mk_meta_eq_simp r = case concl_of r of
paulson@2031
    88
          Const("==",_)$_$_ => r
nipkow@3896
    89
      |   _$(Const("op =",_)$lhs$rhs) =>
nipkow@4117
    90
             (case fst(Logic.rewrite_rule_ok (#sign(rep_thm r)) (prems_of r) lhs rhs) of
nipkow@3896
    91
                None => mk_meta_eq r
nipkow@3896
    92
              | Some _ => r RS P_imp_P_eq_True)
paulson@2718
    93
      |   _$(Const("Not",_)$_) => r RS not_P_imp_P_eq_False
paulson@1922
    94
      |   _ => r RS P_imp_P_eq_True;
paulson@1922
    95
  (* last 2 lines requires all formulae to be of the from Trueprop(.) *)
clasohm@923
    96
paulson@2082
    97
val simp_thms = map prover
paulson@2082
    98
 [ "(x=x) = True",
paulson@2082
    99
   "(~True) = False", "(~False) = True", "(~ ~ P) = P",
paulson@2082
   100
   "(~P) ~= P", "P ~= (~P)", "(P ~= Q) = (P = (~Q))",
nipkow@4640
   101
   "(True=P) = P", "(P=True) = P", "(False=P) = (~P)", "(P=False) = (~P)",
paulson@2082
   102
   "(True --> P) = P", "(False --> P) = True", 
paulson@2082
   103
   "(P --> True) = True", "(P --> P) = True",
paulson@2082
   104
   "(P --> False) = (~P)", "(P --> ~P) = (~P)",
paulson@2082
   105
   "(P & True) = P", "(True & P) = P", 
nipkow@2800
   106
   "(P & False) = False", "(False & P) = False",
nipkow@2800
   107
   "(P & P) = P", "(P & (P & Q)) = (P & Q)",
paulson@3913
   108
   "(P & ~P) = False",    "(~P & P) = False",
paulson@2082
   109
   "(P | True) = True", "(True | P) = True", 
nipkow@2800
   110
   "(P | False) = P", "(False | P) = P",
nipkow@2800
   111
   "(P | P) = P", "(P | (P | Q)) = (P | Q)",
paulson@3913
   112
   "(P | ~P) = True",    "(~P | P) = True",
paulson@2082
   113
   "((~P) = (~Q)) = (P=Q)",
wenzelm@3842
   114
   "(!x. P) = P", "(? x. P) = P", "? x. x=t", "? x. t=x", 
paulson@4351
   115
(*two needed for the one-point-rule quantifier simplification procs*)
paulson@4351
   116
   "(? x. x=t & P(x)) = P(t)",		(*essential for termination!!*)
paulson@4351
   117
   "(! x. t=x --> P(x)) = P(t)" ];      (*covers a stray case*)
clasohm@923
   118
lcp@988
   119
(*Add congruence rules for = (instead of ==) *)
oheimb@2636
   120
infix 4 addcongs delcongs;
paulson@4351
   121
nipkow@4640
   122
fun mk_meta_cong rl =
nipkow@4640
   123
  standard(mk_meta_eq(replicate (nprems_of rl) meta_eq_to_obj_eq MRS rl))
nipkow@4640
   124
  handle THM _ =>
nipkow@4640
   125
  error("Premises and conclusion of congruence rules must be =-equalities");
nipkow@4640
   126
nipkow@4640
   127
fun ss addcongs congs = ss addeqcongs (map mk_meta_cong congs);
nipkow@4640
   128
nipkow@4640
   129
fun ss delcongs congs = ss deleqcongs (map mk_meta_cong congs);
clasohm@923
   130
wenzelm@4086
   131
fun Addcongs congs = (simpset_ref() := simpset() addcongs congs);
wenzelm@4086
   132
fun Delcongs congs = (simpset_ref() := simpset() delcongs congs);
clasohm@1264
   133
nipkow@3896
   134
fun mksimps pairs = map mk_meta_eq_simp o atomize pairs o gen_all;
clasohm@923
   135
paulson@1922
   136
val imp_cong = impI RSN
paulson@1922
   137
    (2, prove_goal HOL.thy "(P=P')--> (P'--> (Q=Q'))--> ((P-->Q) = (P'-->Q'))"
paulson@2935
   138
        (fn _=> [blast_tac HOL_cs 1]) RS mp RS mp);
paulson@1922
   139
paulson@1948
   140
(*Miniscoping: pushing in existential quantifiers*)
paulson@1948
   141
val ex_simps = map prover 
wenzelm@3842
   142
                ["(EX x. P x & Q)   = ((EX x. P x) & Q)",
wenzelm@3842
   143
                 "(EX x. P & Q x)   = (P & (EX x. Q x))",
wenzelm@3842
   144
                 "(EX x. P x | Q)   = ((EX x. P x) | Q)",
wenzelm@3842
   145
                 "(EX x. P | Q x)   = (P | (EX x. Q x))",
wenzelm@3842
   146
                 "(EX x. P x --> Q) = ((ALL x. P x) --> Q)",
wenzelm@3842
   147
                 "(EX x. P --> Q x) = (P --> (EX x. Q x))"];
paulson@1948
   148
paulson@1948
   149
(*Miniscoping: pushing in universal quantifiers*)
paulson@1948
   150
val all_simps = map prover
wenzelm@3842
   151
                ["(ALL x. P x & Q)   = ((ALL x. P x) & Q)",
wenzelm@3842
   152
                 "(ALL x. P & Q x)   = (P & (ALL x. Q x))",
wenzelm@3842
   153
                 "(ALL x. P x | Q)   = ((ALL x. P x) | Q)",
wenzelm@3842
   154
                 "(ALL x. P | Q x)   = (P | (ALL x. Q x))",
wenzelm@3842
   155
                 "(ALL x. P x --> Q) = ((EX x. P x) --> Q)",
wenzelm@3842
   156
                 "(ALL x. P --> Q x) = (P --> (ALL x. Q x))"];
paulson@1948
   157
clasohm@923
   158
paulson@2022
   159
(* elimination of existential quantifiers in assumptions *)
clasohm@923
   160
clasohm@923
   161
val ex_all_equiv =
clasohm@923
   162
  let val lemma1 = prove_goal HOL.thy
clasohm@923
   163
        "(? x. P(x) ==> PROP Q) ==> (!!x. P(x) ==> PROP Q)"
clasohm@923
   164
        (fn prems => [resolve_tac prems 1, etac exI 1]);
clasohm@923
   165
      val lemma2 = prove_goalw HOL.thy [Ex_def]
clasohm@923
   166
        "(!!x. P(x) ==> PROP Q) ==> (? x. P(x) ==> PROP Q)"
clasohm@923
   167
        (fn prems => [REPEAT(resolve_tac prems 1)])
clasohm@923
   168
  in equal_intr lemma1 lemma2 end;
clasohm@923
   169
clasohm@923
   170
end;
clasohm@923
   171
nipkow@3654
   172
(* Elimination of True from asumptions: *)
nipkow@3654
   173
nipkow@3654
   174
val True_implies_equals = prove_goal HOL.thy
nipkow@3654
   175
 "(True ==> PROP P) == PROP P"
oheimb@4525
   176
(K [rtac equal_intr_rule 1, atac 2,
nipkow@3654
   177
          METAHYPS (fn prems => resolve_tac prems 1) 1,
nipkow@3654
   178
          rtac TrueI 1]);
nipkow@3654
   179
oheimb@4525
   180
fun prove nm thm  = qed_goal nm HOL.thy thm (K [blast_tac HOL_cs 1]);
clasohm@923
   181
clasohm@923
   182
prove "conj_commute" "(P&Q) = (Q&P)";
clasohm@923
   183
prove "conj_left_commute" "(P&(Q&R)) = (Q&(P&R))";
clasohm@923
   184
val conj_comms = [conj_commute, conj_left_commute];
nipkow@2134
   185
prove "conj_assoc" "((P&Q)&R) = (P&(Q&R))";
clasohm@923
   186
paulson@1922
   187
prove "disj_commute" "(P|Q) = (Q|P)";
paulson@1922
   188
prove "disj_left_commute" "(P|(Q|R)) = (Q|(P|R))";
paulson@1922
   189
val disj_comms = [disj_commute, disj_left_commute];
nipkow@2134
   190
prove "disj_assoc" "((P|Q)|R) = (P|(Q|R))";
paulson@1922
   191
clasohm@923
   192
prove "conj_disj_distribL" "(P&(Q|R)) = (P&Q | P&R)";
clasohm@923
   193
prove "conj_disj_distribR" "((P|Q)&R) = (P&R | Q&R)";
nipkow@1485
   194
paulson@1892
   195
prove "disj_conj_distribL" "(P|(Q&R)) = ((P|Q) & (P|R))";
paulson@1892
   196
prove "disj_conj_distribR" "((P&Q)|R) = ((P|R) & (Q|R))";
paulson@1892
   197
nipkow@2134
   198
prove "imp_conjR" "(P --> (Q&R)) = ((P-->Q) & (P-->R))";
nipkow@2134
   199
prove "imp_conjL" "((P&Q) -->R)  = (P --> (Q --> R))";
nipkow@2134
   200
prove "imp_disjL" "((P|Q) --> R) = ((P-->R)&(Q-->R))";
paulson@1892
   201
paulson@3448
   202
(*These two are specialized, but imp_disj_not1 is useful in Auth/Yahalom.ML*)
paulson@3448
   203
prove "imp_disj_not1" "((P --> Q | R)) = (~Q --> P --> R)";
paulson@3448
   204
prove "imp_disj_not2" "((P --> Q | R)) = (~R --> P --> Q)";
paulson@3448
   205
paulson@3904
   206
prove "imp_disj1" "((P-->Q)|R) = (P--> Q|R)";
paulson@3904
   207
prove "imp_disj2" "(Q|(P-->R)) = (P--> Q|R)";
paulson@3904
   208
nipkow@1485
   209
prove "de_Morgan_disj" "(~(P | Q)) = (~P & ~Q)";
nipkow@1485
   210
prove "de_Morgan_conj" "(~(P & Q)) = (~P | ~Q)";
paulson@3446
   211
prove "not_imp" "(~(P --> Q)) = (P & ~Q)";
paulson@1922
   212
prove "not_iff" "(P~=Q) = (P = (~Q))";
nipkow@1485
   213
nipkow@2134
   214
(*Avoids duplication of subgoals after expand_if, when the true and false 
nipkow@2134
   215
  cases boil down to the same thing.*) 
nipkow@2134
   216
prove "cases_simp" "((P --> Q) & (~P --> Q)) = Q";
nipkow@2134
   217
wenzelm@3842
   218
prove "not_all" "(~ (! x. P(x))) = (? x.~P(x))";
paulson@1922
   219
prove "imp_all" "((! x. P x) --> Q) = (? x. P x --> Q)";
wenzelm@3842
   220
prove "not_ex"  "(~ (? x. P(x))) = (! x.~P(x))";
paulson@1922
   221
prove "imp_ex" "((? x. P x) --> Q) = (! x. P x --> Q)";
oheimb@1660
   222
nipkow@1655
   223
prove "ex_disj_distrib" "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))";
nipkow@1655
   224
prove "all_conj_distrib" "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))";
nipkow@1655
   225
nipkow@2134
   226
(* '&' congruence rule: not included by default!
nipkow@2134
   227
   May slow rewrite proofs down by as much as 50% *)
nipkow@2134
   228
nipkow@2134
   229
let val th = prove_goal HOL.thy 
nipkow@2134
   230
                "(P=P')--> (P'--> (Q=Q'))--> ((P&Q) = (P'&Q'))"
paulson@2935
   231
                (fn _=> [blast_tac HOL_cs 1])
nipkow@2134
   232
in  bind_thm("conj_cong",standard (impI RSN (2, th RS mp RS mp)))  end;
nipkow@2134
   233
nipkow@2134
   234
let val th = prove_goal HOL.thy 
nipkow@2134
   235
                "(Q=Q')--> (Q'--> (P=P'))--> ((P&Q) = (P'&Q'))"
paulson@2935
   236
                (fn _=> [blast_tac HOL_cs 1])
nipkow@2134
   237
in  bind_thm("rev_conj_cong",standard (impI RSN (2, th RS mp RS mp)))  end;
nipkow@2134
   238
nipkow@2134
   239
(* '|' congruence rule: not included by default! *)
nipkow@2134
   240
nipkow@2134
   241
let val th = prove_goal HOL.thy 
nipkow@2134
   242
                "(P=P')--> (~P'--> (Q=Q'))--> ((P|Q) = (P'|Q'))"
paulson@2935
   243
                (fn _=> [blast_tac HOL_cs 1])
nipkow@2134
   244
in  bind_thm("disj_cong",standard (impI RSN (2, th RS mp RS mp)))  end;
nipkow@2134
   245
nipkow@2134
   246
prove "eq_sym_conv" "(x=y) = (y=x)";
nipkow@2134
   247
nipkow@2134
   248
qed_goalw "o_apply" HOL.thy [o_def] "(f o g) x = f (g x)"
oheimb@4525
   249
 (K [rtac refl 1]);
nipkow@2134
   250
nipkow@2134
   251
qed_goalw "if_True" HOL.thy [if_def] "(if True then x else y) = x"
oheimb@4525
   252
 (K [Blast_tac 1]);
nipkow@2134
   253
nipkow@2134
   254
qed_goalw "if_False" HOL.thy [if_def] "(if False then x else y) = y"
oheimb@4525
   255
 (K [Blast_tac 1]);
nipkow@2134
   256
nipkow@2134
   257
qed_goal "if_P" HOL.thy "P ==> (if P then x else y) = x"
nipkow@2134
   258
 (fn [prem] => [ stac (prem RS eqTrueI) 1, rtac if_True 1 ]);
nipkow@2134
   259
(*
nipkow@2134
   260
qed_goal "if_not_P" HOL.thy "~P ==> (if P then x else y) = y"
nipkow@2134
   261
 (fn [prem] => [ stac (prem RS not_P_imp_P_iff_F) 1, rtac if_False 1 ]);
nipkow@2134
   262
*)
nipkow@2134
   263
qed_goalw "if_not_P" HOL.thy [if_def] "!!P. ~P ==> (if P then x else y) = y"
oheimb@4525
   264
 (K [Blast_tac 1]);
nipkow@2134
   265
nipkow@2134
   266
qed_goal "expand_if" HOL.thy
oheimb@4205
   267
    "P(if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))" (K [
oheimb@4205
   268
	res_inst_tac [("Q","Q")] (excluded_middle RS disjE) 1,
nipkow@2134
   269
         stac if_P 2,
nipkow@2134
   270
         stac if_not_P 1,
oheimb@4205
   271
         ALLGOALS (blast_tac HOL_cs)]);
oheimb@4205
   272
oheimb@4205
   273
qed_goal "split_if_asm" HOL.thy
oheimb@4205
   274
    "P(if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))" (K [
oheimb@4205
   275
	stac expand_if 1,
oheimb@4205
   276
        blast_tac HOL_cs 1]);
nipkow@2134
   277
nipkow@2134
   278
qed_goal "if_bool_eq" HOL.thy
nipkow@2134
   279
                   "(if P then Q else R) = ((P-->Q) & (~P-->R))"
oheimb@4525
   280
                   (K [rtac expand_if 1]);
nipkow@2134
   281
paulson@4351
   282
paulson@4351
   283
(*** make simplification procedures for quantifier elimination ***)
paulson@4351
   284
paulson@4351
   285
structure Quantifier1 = Quantifier1Fun(
paulson@4351
   286
struct
paulson@4351
   287
  (*abstract syntax*)
paulson@4351
   288
  fun dest_eq((c as Const("op =",_)) $ s $ t) = Some(c,s,t)
paulson@4351
   289
    | dest_eq _ = None;
paulson@4351
   290
  fun dest_conj((c as Const("op &",_)) $ s $ t) = Some(c,s,t)
paulson@4351
   291
    | dest_conj _ = None;
paulson@4351
   292
  val conj = HOLogic.conj
paulson@4351
   293
  val imp  = HOLogic.imp
paulson@4351
   294
  (*rules*)
paulson@4351
   295
  val iff_reflection = eq_reflection
paulson@4351
   296
  val iffI = iffI
paulson@4351
   297
  val sym  = sym
paulson@4351
   298
  val conjI= conjI
paulson@4351
   299
  val conjE= conjE
paulson@4351
   300
  val impI = impI
paulson@4351
   301
  val impE = impE
paulson@4351
   302
  val mp   = mp
paulson@4351
   303
  val exI  = exI
paulson@4351
   304
  val exE  = exE
paulson@4351
   305
  val allI = allI
paulson@4351
   306
  val allE = allE
paulson@4351
   307
end);
paulson@4351
   308
nipkow@4320
   309
local
nipkow@4320
   310
val ex_pattern =
paulson@4351
   311
  read_cterm (sign_of HOL.thy) ("EX x. P(x) & Q(x)",HOLogic.boolT)
paulson@3913
   312
nipkow@4320
   313
val all_pattern =
paulson@4351
   314
  read_cterm (sign_of HOL.thy) ("ALL x. P(x) & P'(x) --> Q(x)",HOLogic.boolT)
nipkow@4320
   315
nipkow@4320
   316
in
nipkow@4320
   317
val defEX_regroup =
nipkow@4320
   318
  mk_simproc "defined EX" [ex_pattern] Quantifier1.rearrange_ex;
nipkow@4320
   319
val defALL_regroup =
nipkow@4320
   320
  mk_simproc "defined ALL" [all_pattern] Quantifier1.rearrange_all;
nipkow@4320
   321
end;
paulson@3913
   322
paulson@4351
   323
paulson@4351
   324
(*** Case splitting ***)
paulson@3913
   325
oheimb@2263
   326
local val mktac = mk_case_split_tac (meta_eq_to_obj_eq RS iffD2)
oheimb@2263
   327
in
oheimb@2263
   328
fun split_tac splits = mktac (map mk_meta_eq splits)
oheimb@2263
   329
end;
oheimb@2263
   330
oheimb@2263
   331
local val mktac = mk_case_split_inside_tac (meta_eq_to_obj_eq RS iffD2)
oheimb@2263
   332
in
oheimb@2263
   333
fun split_inside_tac splits = mktac (map mk_meta_eq splits)
oheimb@2263
   334
end;
oheimb@2263
   335
oheimb@4205
   336
val split_asm_tac = mk_case_split_asm_tac split_tac 
oheimb@4205
   337
			(disjE,conjE,exE,contrapos,contrapos2,notnotD);
oheimb@4189
   338
nipkow@3919
   339
infix 4 addsplits;
nipkow@3919
   340
fun ss addsplits splits = ss addloop (split_tac splits);
nipkow@3919
   341
oheimb@2263
   342
oheimb@2251
   343
qed_goal "if_cancel" HOL.thy "(if c then x else x) = x"
oheimb@4525
   344
  (K [split_tac [expand_if] 1, blast_tac HOL_cs 1]);
oheimb@2251
   345
nipkow@2134
   346
(** 'if' congruence rules: neither included by default! *)
nipkow@2134
   347
nipkow@2134
   348
(*Simplifies x assuming c and y assuming ~c*)
nipkow@2134
   349
qed_goal "if_cong" HOL.thy
nipkow@2134
   350
  "[| b=c; c ==> x=u; ~c ==> y=v |] ==>\
nipkow@2134
   351
\  (if b then x else y) = (if c then u else v)"
nipkow@2134
   352
  (fn rew::prems =>
nipkow@2134
   353
   [stac rew 1, stac expand_if 1, stac expand_if 1,
paulson@2935
   354
    blast_tac (HOL_cs addDs prems) 1]);
nipkow@2134
   355
nipkow@2134
   356
(*Prevents simplification of x and y: much faster*)
nipkow@2134
   357
qed_goal "if_weak_cong" HOL.thy
nipkow@2134
   358
  "b=c ==> (if b then x else y) = (if c then x else y)"
nipkow@2134
   359
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
nipkow@2134
   360
nipkow@2134
   361
(*Prevents simplification of t: much faster*)
nipkow@2134
   362
qed_goal "let_weak_cong" HOL.thy
nipkow@2134
   363
  "a = b ==> (let x=a in t(x)) = (let x=b in t(x))"
nipkow@2134
   364
  (fn [prem] => [rtac (prem RS arg_cong) 1]);
nipkow@2134
   365
nipkow@2134
   366
(*In general it seems wrong to add distributive laws by default: they
nipkow@2134
   367
  might cause exponential blow-up.  But imp_disjL has been in for a while
nipkow@2134
   368
  and cannot be removed without affecting existing proofs.  Moreover, 
nipkow@2134
   369
  rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
nipkow@2134
   370
  grounds that it allows simplification of R in the two cases.*)
nipkow@2134
   371
nipkow@2134
   372
val mksimps_pairs =
nipkow@2134
   373
  [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
nipkow@2134
   374
   ("All", [spec]), ("True", []), ("False", []),
nipkow@2134
   375
   ("If", [if_bool_eq RS iffD1])];
nipkow@1758
   376
nipkow@4640
   377
fun unsafe_solver prems = FIRST'[resolve_tac (reflexive_thm::TrueI::refl::prems),
oheimb@2636
   378
				 atac, etac FalseE];
oheimb@2636
   379
(*No premature instantiation of variables during simplification*)
nipkow@4640
   380
fun   safe_solver prems = FIRST'[match_tac (reflexive_thm::TrueI::prems),
oheimb@2636
   381
				 eq_assume_tac, ematch_tac [FalseE]];
oheimb@2443
   382
oheimb@2636
   383
val HOL_basic_ss = empty_ss setsubgoaler asm_simp_tac
oheimb@2636
   384
			    setSSolver   safe_solver
oheimb@2636
   385
			    setSolver  unsafe_solver
oheimb@2636
   386
			    setmksimps (mksimps mksimps_pairs);
oheimb@2443
   387
paulson@3446
   388
val HOL_ss = 
paulson@3446
   389
    HOL_basic_ss addsimps 
paulson@3446
   390
     ([triv_forall_equality, (* prunes params *)
nipkow@3654
   391
       True_implies_equals, (* prune asms `True' *)
paulson@3446
   392
       if_True, if_False, if_cancel,
paulson@3446
   393
       o_apply, imp_disjL, conj_assoc, disj_assoc,
paulson@3904
   394
       de_Morgan_conj, de_Morgan_disj, imp_disj1, imp_disj2, not_imp,
paulson@3446
   395
       not_all, not_ex, cases_simp]
paulson@3446
   396
     @ ex_simps @ all_simps @ simp_thms)
nipkow@4032
   397
     addsimprocs [defALL_regroup,defEX_regroup]
paulson@3446
   398
     addcongs [imp_cong];
paulson@2082
   399
nipkow@1655
   400
qed_goal "if_distrib" HOL.thy
nipkow@1655
   401
  "f(if c then x else y) = (if c then f x else f y)" 
oheimb@4525
   402
  (K [simp_tac (HOL_ss setloop (split_tac [expand_if])) 1]);
nipkow@1655
   403
oheimb@2097
   404
qed_goalw "o_assoc" HOL.thy [o_def] "f o (g o h) = f o g o h"
oheimb@4525
   405
  (K [rtac ext 1, rtac refl 1]);
paulson@1984
   406
paulson@1984
   407
paulson@4327
   408
(*For expand_case_tac*)
paulson@2948
   409
val prems = goal HOL.thy "[| P ==> Q(True); ~P ==> Q(False) |] ==> Q(P)";
paulson@2948
   410
by (case_tac "P" 1);
paulson@2948
   411
by (ALLGOALS (asm_simp_tac (HOL_ss addsimps prems)));
paulson@2948
   412
val expand_case = result();
paulson@2948
   413
paulson@4327
   414
(*Used in Auth proofs.  Typically P contains Vars that become instantiated
paulson@4327
   415
  during unification.*)
paulson@2948
   416
fun expand_case_tac P i =
paulson@2948
   417
    res_inst_tac [("P",P)] expand_case i THEN
paulson@2948
   418
    Simp_tac (i+1) THEN 
paulson@2948
   419
    Simp_tac i;
paulson@2948
   420
paulson@2948
   421
wenzelm@4119
   422
(* install implicit simpset *)
paulson@1984
   423
wenzelm@4086
   424
simpset_ref() := HOL_ss;
paulson@1984
   425
berghofe@3615
   426
oheimb@2636
   427
(*** Integration of simplifier with classical reasoner ***)
oheimb@2636
   428
oheimb@2636
   429
(* rot_eq_tac rotates the first equality premise of subgoal i to the front,
oheimb@2636
   430
   fails if there is no equaliy or if an equality is already at the front *)
paulson@3538
   431
local
paulson@3538
   432
  fun is_eq (Const ("Trueprop", _) $ (Const("op ="  ,_) $ _ $ _)) = true
paulson@3538
   433
    | is_eq _ = false;
oheimb@4188
   434
  val find_eq = find_index is_eq;
paulson@3538
   435
in
paulson@3538
   436
val rot_eq_tac = 
oheimb@4188
   437
     SUBGOAL (fn (Bi,i) => let val n = find_eq (Logic.strip_assums_hyp Bi) in
oheimb@4188
   438
		if n>0 then rotate_tac n i else no_tac end)
paulson@3538
   439
end;
oheimb@2636
   440
oheimb@2636
   441
(*an unsatisfactory fix for the incomplete asm_full_simp_tac!
oheimb@2636
   442
  better: asm_really_full_simp_tac, a yet to be implemented version of
oheimb@2636
   443
			asm_full_simp_tac that applies all equalities in the
oheimb@2636
   444
			premises to all the premises *)
oheimb@2636
   445
fun safe_asm_more_full_simp_tac ss = TRY o rot_eq_tac THEN' 
oheimb@2636
   446
				     safe_asm_full_simp_tac ss;
oheimb@2636
   447
oheimb@2636
   448
(*Add a simpset to a classical set!*)
oheimb@3206
   449
infix 4 addSss addss;
oheimb@3206
   450
fun cs addSss ss = cs addSaltern (CHANGED o (safe_asm_more_full_simp_tac ss));
oheimb@3206
   451
fun cs addss  ss = cs addbefore                        asm_full_simp_tac ss;
oheimb@2636
   452
wenzelm@4086
   453
fun Addss ss = (claset_ref() := claset() addss ss);
oheimb@2636
   454
oheimb@2636
   455
(*Designed to be idempotent, except if best_tac instantiates variables
oheimb@2636
   456
  in some of the subgoals*)
oheimb@2636
   457
oheimb@2636
   458
type clasimpset = (claset * simpset);
oheimb@2636
   459
oheimb@2636
   460
val HOL_css = (HOL_cs, HOL_ss);
oheimb@2636
   461
oheimb@2636
   462
fun pair_upd1 f ((a,b),x) = (f(a,x), b);
oheimb@2636
   463
fun pair_upd2 f ((a,b),x) = (a, f(b,x));
oheimb@2636
   464
oheimb@2636
   465
infix 4 addSIs2 addSEs2 addSDs2 addIs2 addEs2 addDs2
oheimb@2636
   466
	addsimps2 delsimps2 addcongs2 delcongs2;
paulson@2748
   467
fun op addSIs2   arg = pair_upd1 (op addSIs) arg;
paulson@2748
   468
fun op addSEs2   arg = pair_upd1 (op addSEs) arg;
paulson@2748
   469
fun op addSDs2   arg = pair_upd1 (op addSDs) arg;
paulson@2748
   470
fun op addIs2    arg = pair_upd1 (op addIs ) arg;
paulson@2748
   471
fun op addEs2    arg = pair_upd1 (op addEs ) arg;
paulson@2748
   472
fun op addDs2    arg = pair_upd1 (op addDs ) arg;
paulson@2748
   473
fun op addsimps2 arg = pair_upd2 (op addsimps) arg;
paulson@2748
   474
fun op delsimps2 arg = pair_upd2 (op delsimps) arg;
paulson@2748
   475
fun op addcongs2 arg = pair_upd2 (op addcongs) arg;
paulson@2748
   476
fun op delcongs2 arg = pair_upd2 (op delcongs) arg;
oheimb@2636
   477
paulson@4477
   478
fun mk_auto_tac (cs, ss) m n =
paulson@2805
   479
    let val cs' = cs addss ss 
paulson@4477
   480
	val bdt = Blast.depth_tac cs m;
paulson@4477
   481
	fun blast_depth_tac i thm = bdt i thm handle Blast.TRANS s => 
paulson@4477
   482
		(warning ("Blast_tac: " ^ s); Seq.empty);
oheimb@4633
   483
oheimb@4633
   484
	(* a variant of depth_tac that avoids interference of the simplifier 
oheimb@4633
   485
	   with dup_step_tac when they are combined by auto_tac *)
oheimb@4633
   486
	fun nodup_depth_tac cs m i state = 
oheimb@4633
   487
	  SELECT_GOAL 
oheimb@4633
   488
	   (getWrapper cs
oheimb@4633
   489
	    (fn i => REPEAT_DETERM1 (COND (has_fewer_prems i) no_tac
oheimb@4633
   490
				     (safe_step_tac cs i)) THEN_ELSE
oheimb@4633
   491
	     (DEPTH_SOLVE (nodup_depth_tac cs m i),
oheimb@4633
   492
	      inst0_step_tac cs i  APPEND
oheimb@4633
   493
	      COND (K(m=0)) no_tac
oheimb@4633
   494
	        ((instp_step_tac cs i APPEND step_tac cs i)
oheimb@4633
   495
		 THEN DEPTH_SOLVE (nodup_depth_tac cs (m-1) i)))) 1)
oheimb@4633
   496
	  i state;
oheimb@4633
   497
paulson@4477
   498
        val maintac = 
paulson@4477
   499
          blast_depth_tac	   (*fast but can't use addss*)
paulson@4477
   500
          ORELSE'
oheimb@4633
   501
          nodup_depth_tac cs' n;   (*slower but more general*)
paulson@4477
   502
    in  EVERY [ALLGOALS (asm_full_simp_tac ss),
paulson@4477
   503
	       TRY (safe_tac cs'),
paulson@4477
   504
	       REPEAT (FIRSTGOAL maintac),
oheimb@3206
   505
               TRY (safe_tac (cs addSss ss)),
paulson@2805
   506
	       prune_params_tac] 
paulson@2805
   507
    end;
oheimb@2636
   508
paulson@4477
   509
fun auto_tac (cs,ss) = mk_auto_tac (cs,ss) 4 2;
oheimb@2636
   510
paulson@4477
   511
fun Auto_tac st = auto_tac (claset(), simpset()) st;
paulson@4477
   512
paulson@4477
   513
fun auto () = by Auto_tac;