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