src/FOL/simpdata.ML
author wenzelm
Sat Jan 20 14:09:11 2007 +0100 (2007-01-20)
changeset 22128 cdd92316dd31
parent 21539 c5cf9243ad62
child 22822 c1a6a2159e69
permissions -rw-r--r--
added @{clasimpset};
wenzelm@9889
     1
(*  Title:      FOL/simpdata.ML
clasohm@0
     2
    ID:         $Id$
clasohm@1459
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
lcp@282
     4
    Copyright   1994  University of Cambridge
clasohm@0
     5
wenzelm@9889
     6
Simplification data for FOL.
clasohm@0
     7
*)
clasohm@0
     8
clasohm@0
     9
(*** Rewrite rules ***)
clasohm@0
    10
wenzelm@10431
    11
fun int_prove_fun s =
wenzelm@10431
    12
 (writeln s;
wenzelm@21539
    13
  prove_goal (theory "IFOL") s
wenzelm@10431
    14
   (fn prems => [ (cut_facts_tac prems 1),
paulson@2601
    15
                  (IntPr.fast_tac 1) ]));
clasohm@0
    16
wenzelm@12038
    17
bind_thms ("conj_simps", map int_prove_fun
clasohm@1459
    18
 ["P & True <-> P",      "True & P <-> P",
clasohm@0
    19
  "P & False <-> False", "False & P <-> False",
nipkow@2801
    20
  "P & P <-> P", "P & P & Q <-> P & Q",
clasohm@1459
    21
  "P & ~P <-> False",    "~P & P <-> False",
wenzelm@12038
    22
  "(P & Q) & R <-> P & (Q & R)"]);
clasohm@0
    23
wenzelm@12038
    24
bind_thms ("disj_simps", map int_prove_fun
clasohm@1459
    25
 ["P | True <-> True",  "True | P <-> True",
clasohm@1459
    26
  "P | False <-> P",    "False | P <-> P",
nipkow@2801
    27
  "P | P <-> P", "P | P | Q <-> P | Q",
wenzelm@12038
    28
  "(P | Q) | R <-> P | (Q | R)"]);
clasohm@0
    29
wenzelm@12038
    30
bind_thms ("not_simps", map int_prove_fun
lcp@282
    31
 ["~(P|Q)  <-> ~P & ~Q",
wenzelm@12038
    32
  "~ False <-> True",   "~ True <-> False"]);
clasohm@0
    33
wenzelm@12038
    34
bind_thms ("imp_simps", map int_prove_fun
clasohm@1459
    35
 ["(P --> False) <-> ~P",       "(P --> True) <-> True",
wenzelm@10431
    36
  "(False --> P) <-> True",     "(True --> P) <-> P",
wenzelm@12038
    37
  "(P --> P) <-> True",         "(P --> ~P) <-> ~P"]);
clasohm@0
    38
wenzelm@12038
    39
bind_thms ("iff_simps", map int_prove_fun
clasohm@1459
    40
 ["(True <-> P) <-> P",         "(P <-> True) <-> P",
clasohm@0
    41
  "(P <-> P) <-> True",
wenzelm@12038
    42
  "(False <-> P) <-> ~P",       "(P <-> False) <-> ~P"]);
clasohm@0
    43
paulson@4349
    44
(*The x=t versions are needed for the simplification procedures*)
wenzelm@12038
    45
bind_thms ("quant_simps", map int_prove_fun
wenzelm@10431
    46
 ["(ALL x. P) <-> P",
paulson@4349
    47
  "(ALL x. x=t --> P(x)) <-> P(t)",
paulson@4349
    48
  "(ALL x. t=x --> P(x)) <-> P(t)",
paulson@4349
    49
  "(EX x. P) <-> P",
paulson@13149
    50
  "EX x. x=t", "EX x. t=x",
wenzelm@10431
    51
  "(EX x. x=t & P(x)) <-> P(t)",
wenzelm@12038
    52
  "(EX x. t=x & P(x)) <-> P(t)"]);
clasohm@0
    53
clasohm@0
    54
(*These are NOT supplied by default!*)
wenzelm@12038
    55
bind_thms ("distrib_simps", map int_prove_fun
wenzelm@10431
    56
 ["P & (Q | R) <-> P&Q | P&R",
lcp@282
    57
  "(Q | R) & P <-> Q&P | R&P",
wenzelm@12038
    58
  "(P | Q --> R) <-> (P --> R) & (Q --> R)"]);
clasohm@0
    59
lcp@282
    60
(** Conversion into rewrite rules **)
clasohm@0
    61
wenzelm@12038
    62
bind_thm ("P_iff_F", int_prove_fun "~P ==> (P <-> False)");
wenzelm@12038
    63
bind_thm ("iff_reflection_F", P_iff_F RS iff_reflection);
lcp@282
    64
wenzelm@12038
    65
bind_thm ("P_iff_T", int_prove_fun "P ==> (P <-> True)");
wenzelm@12038
    66
bind_thm ("iff_reflection_T", P_iff_T RS iff_reflection);
lcp@282
    67
lcp@282
    68
(*Make meta-equalities.  The operator below is Trueprop*)
oheimb@5555
    69
lcp@282
    70
fun mk_meta_eq th = case concl_of th of
oheimb@5555
    71
    _ $ (Const("op =",_)$_$_)   => th RS eq_reflection
oheimb@5555
    72
  | _ $ (Const("op <->",_)$_$_) => th RS iff_reflection
wenzelm@10431
    73
  | _                           =>
oheimb@5555
    74
  error("conclusion must be a =-equality or <->");;
oheimb@5555
    75
oheimb@5555
    76
fun mk_eq th = case concl_of th of
nipkow@394
    77
    Const("==",_)$_$_           => th
oheimb@5555
    78
  | _ $ (Const("op =",_)$_$_)   => mk_meta_eq th
oheimb@5555
    79
  | _ $ (Const("op <->",_)$_$_) => mk_meta_eq th
lcp@282
    80
  | _ $ (Const("Not",_)$_)      => th RS iff_reflection_F
lcp@282
    81
  | _                           => th RS iff_reflection_T;
clasohm@0
    82
paulson@6114
    83
(*Replace premises x=y, X<->Y by X==Y*)
wenzelm@10431
    84
val mk_meta_prems =
wenzelm@10431
    85
    rule_by_tactic
wenzelm@21539
    86
      (REPEAT_FIRST (resolve_tac [meta_eq_to_obj_eq, thm "def_imp_iff"]));
paulson@6114
    87
wenzelm@9713
    88
(*Congruence rules for = or <-> (instead of ==)*)
paulson@6114
    89
fun mk_meta_cong rl =
paulson@6114
    90
  standard(mk_meta_eq (mk_meta_prems rl))
paulson@6114
    91
  handle THM _ =>
paulson@6114
    92
  error("Premises and conclusion of congruence rules must use =-equality or <->");
oheimb@5555
    93
oheimb@5304
    94
val mksimps_pairs =
oheimb@5304
    95
  [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
oheimb@5304
    96
   ("All", [spec]), ("True", []), ("False", [])];
oheimb@5304
    97
wenzelm@16019
    98
(* ###FIXME: move to simplifier.ML
oheimb@5304
    99
val mk_atomize:      (string * thm list) list -> thm -> thm list
oheimb@5304
   100
*)
wenzelm@16019
   101
(* ###FIXME: move to simplifier.ML *)
oheimb@5304
   102
fun mk_atomize pairs =
oheimb@5304
   103
  let fun atoms th =
oheimb@5304
   104
        (case concl_of th of
oheimb@5304
   105
           Const("Trueprop",_) $ p =>
oheimb@5304
   106
             (case head_of p of
oheimb@5304
   107
                Const(a,_) =>
haftmann@17325
   108
                  (case AList.lookup (op =) pairs a of
skalberg@15570
   109
                     SOME(rls) => List.concat (map atoms ([th] RL rls))
skalberg@15531
   110
                   | NONE => [th])
oheimb@5304
   111
              | _ => [th])
oheimb@5304
   112
         | _ => [th])
oheimb@5304
   113
  in atoms end;
oheimb@5304
   114
wenzelm@12725
   115
fun mksimps pairs = (map mk_eq o mk_atomize pairs o gen_all);
lcp@981
   116
paulson@2074
   117
(*** Classical laws ***)
lcp@282
   118
wenzelm@10431
   119
fun prove_fun s =
wenzelm@10431
   120
 (writeln s;
wenzelm@7355
   121
  prove_goal (the_context ()) s
wenzelm@10431
   122
   (fn prems => [ (cut_facts_tac prems 1),
clasohm@1459
   123
                  (Cla.fast_tac FOL_cs 1) ]));
lcp@745
   124
wenzelm@10431
   125
(*Avoids duplication of subgoals after expand_if, when the true and false
wenzelm@10431
   126
  cases boil down to the same thing.*)
wenzelm@12038
   127
bind_thm ("cases_simp", prove_fun "(P --> Q) & (~P --> Q) <-> Q");
paulson@1953
   128
paulson@4349
   129
paulson@4349
   130
(*** Miniscoping: pushing quantifiers in
paulson@4349
   131
     We do NOT distribute of ALL over &, or dually that of EX over |
wenzelm@10431
   132
     Baaz and Leitsch, On Skolemization and Proof Complexity (1994)
paulson@4349
   133
     show that this step can increase proof length!
paulson@4349
   134
***)
paulson@4349
   135
paulson@4349
   136
(*existential miniscoping*)
wenzelm@12038
   137
bind_thms ("int_ex_simps", map int_prove_fun
wenzelm@12038
   138
 ["(EX x. P(x) & Q) <-> (EX x. P(x)) & Q",
wenzelm@12038
   139
  "(EX x. P & Q(x)) <-> P & (EX x. Q(x))",
wenzelm@12038
   140
  "(EX x. P(x) | Q) <-> (EX x. P(x)) | Q",
wenzelm@12038
   141
  "(EX x. P | Q(x)) <-> P | (EX x. Q(x))"]);
paulson@4349
   142
paulson@4349
   143
(*classical rules*)
wenzelm@12038
   144
bind_thms ("cla_ex_simps", map prove_fun
wenzelm@12038
   145
 ["(EX x. P(x) --> Q) <-> (ALL x. P(x)) --> Q",
wenzelm@12038
   146
  "(EX x. P --> Q(x)) <-> P --> (EX x. Q(x))"]);
clasohm@0
   147
wenzelm@12038
   148
bind_thms ("ex_simps", int_ex_simps @ cla_ex_simps);
paulson@4349
   149
paulson@4349
   150
(*universal miniscoping*)
wenzelm@12038
   151
bind_thms ("int_all_simps", map int_prove_fun
wenzelm@12038
   152
 ["(ALL x. P(x) & Q) <-> (ALL x. P(x)) & Q",
wenzelm@12038
   153
  "(ALL x. P & Q(x)) <-> P & (ALL x. Q(x))",
wenzelm@12038
   154
  "(ALL x. P(x) --> Q) <-> (EX x. P(x)) --> Q",
wenzelm@12038
   155
  "(ALL x. P --> Q(x)) <-> P --> (ALL x. Q(x))"]);
paulson@1953
   156
paulson@4349
   157
(*classical rules*)
wenzelm@12038
   158
bind_thms ("cla_all_simps", map prove_fun
wenzelm@12038
   159
 ["(ALL x. P(x) | Q) <-> (ALL x. P(x)) | Q",
wenzelm@12038
   160
  "(ALL x. P | Q(x)) <-> P | (ALL x. Q(x))"]);
paulson@4349
   161
wenzelm@12038
   162
bind_thms ("all_simps", int_all_simps @ cla_all_simps);
paulson@4349
   163
paulson@4349
   164
paulson@4349
   165
(*** Named rewrite rules proved for IFOL ***)
paulson@1953
   166
wenzelm@21539
   167
fun int_prove nm thm  = qed_goal nm (theory "IFOL") thm
wenzelm@10431
   168
    (fn prems => [ (cut_facts_tac prems 1),
paulson@2601
   169
                   (IntPr.fast_tac 1) ]);
paulson@1914
   170
wenzelm@7355
   171
fun prove nm thm  = qed_goal nm (the_context ()) thm (fn _ => [Blast_tac 1]);
paulson@1914
   172
paulson@1914
   173
int_prove "conj_commute" "P&Q <-> Q&P";
paulson@1914
   174
int_prove "conj_left_commute" "P&(Q&R) <-> Q&(P&R)";
wenzelm@12038
   175
bind_thms ("conj_comms", [conj_commute, conj_left_commute]);
paulson@1914
   176
paulson@1914
   177
int_prove "disj_commute" "P|Q <-> Q|P";
paulson@1914
   178
int_prove "disj_left_commute" "P|(Q|R) <-> Q|(P|R)";
wenzelm@12038
   179
bind_thms ("disj_comms", [disj_commute, disj_left_commute]);
paulson@1914
   180
paulson@1914
   181
int_prove "conj_disj_distribL" "P&(Q|R) <-> (P&Q | P&R)";
paulson@1914
   182
int_prove "conj_disj_distribR" "(P|Q)&R <-> (P&R | Q&R)";
paulson@1914
   183
paulson@1914
   184
int_prove "disj_conj_distribL" "P|(Q&R) <-> (P|Q) & (P|R)";
paulson@1914
   185
int_prove "disj_conj_distribR" "(P&Q)|R <-> (P|R) & (Q|R)";
paulson@1914
   186
paulson@1914
   187
int_prove "imp_conj_distrib" "(P --> (Q&R)) <-> (P-->Q) & (P-->R)";
paulson@1914
   188
int_prove "imp_conj"         "((P&Q)-->R)   <-> (P --> (Q --> R))";
paulson@1914
   189
int_prove "imp_disj"         "(P|Q --> R)   <-> (P-->R) & (Q-->R)";
paulson@1914
   190
paulson@3910
   191
prove "imp_disj1" "(P-->Q) | R <-> (P-->Q | R)";
paulson@3910
   192
prove "imp_disj2" "Q | (P-->R) <-> (P-->Q | R)";
paulson@3910
   193
paulson@1914
   194
int_prove "de_Morgan_disj" "(~(P | Q)) <-> (~P & ~Q)";
paulson@1914
   195
prove     "de_Morgan_conj" "(~(P & Q)) <-> (~P | ~Q)";
paulson@1914
   196
paulson@12765
   197
prove     "not_imp" "~(P --> Q) <-> (P & ~Q)";
paulson@1914
   198
prove     "not_iff" "~(P <-> Q) <-> (P <-> ~Q)";
paulson@1914
   199
wenzelm@3835
   200
prove     "not_all" "(~ (ALL x. P(x))) <-> (EX x.~P(x))";
wenzelm@3835
   201
prove     "imp_all" "((ALL x. P(x)) --> Q) <-> (EX x. P(x) --> Q)";
wenzelm@3835
   202
int_prove "not_ex"  "(~ (EX x. P(x))) <-> (ALL x.~P(x))";
paulson@1914
   203
int_prove "imp_ex" "((EX x. P(x)) --> Q) <-> (ALL x. P(x) --> Q)";
paulson@1914
   204
paulson@1914
   205
int_prove "ex_disj_distrib"
paulson@1914
   206
    "(EX x. P(x) | Q(x)) <-> ((EX x. P(x)) | (EX x. Q(x)))";
paulson@1914
   207
int_prove "all_conj_distrib"
paulson@1914
   208
    "(ALL x. P(x) & Q(x)) <-> ((ALL x. P(x)) & (ALL x. Q(x)))";
paulson@1914
   209
paulson@1914
   210
paulson@4349
   211
(** make simplification procedures for quantifier elimination **)
paulson@4349
   212
structure Quantifier1 = Quantifier1Fun(
paulson@4349
   213
struct
paulson@4349
   214
  (*abstract syntax*)
skalberg@15531
   215
  fun dest_eq((c as Const("op =",_)) $ s $ t) = SOME(c,s,t)
skalberg@15531
   216
    | dest_eq _ = NONE;
skalberg@15531
   217
  fun dest_conj((c as Const("op &",_)) $ s $ t) = SOME(c,s,t)
skalberg@15531
   218
    | dest_conj _ = NONE;
skalberg@15531
   219
  fun dest_imp((c as Const("op -->",_)) $ s $ t) = SOME(c,s,t)
skalberg@15531
   220
    | dest_imp _ = NONE;
paulson@4349
   221
  val conj = FOLogic.conj
paulson@4349
   222
  val imp  = FOLogic.imp
paulson@4349
   223
  (*rules*)
paulson@4349
   224
  val iff_reflection = iff_reflection
paulson@4349
   225
  val iffI = iffI
nipkow@12526
   226
  val iff_trans = iff_trans
paulson@4349
   227
  val conjI= conjI
paulson@4349
   228
  val conjE= conjE
paulson@4349
   229
  val impI = impI
paulson@4349
   230
  val mp   = mp
wenzelm@21539
   231
  val uncurry = thm "uncurry"
paulson@4349
   232
  val exI  = exI
paulson@4349
   233
  val exE  = exE
wenzelm@21539
   234
  val iff_allI = thm "iff_allI"
wenzelm@21539
   235
  val iff_exI = thm "iff_exI"
wenzelm@21539
   236
  val all_comm = thm "all_comm"
wenzelm@21539
   237
  val ex_comm = thm "ex_comm"
paulson@4349
   238
end);
paulson@4349
   239
wenzelm@13462
   240
val defEX_regroup =
wenzelm@17002
   241
  Simplifier.simproc (the_context ())
wenzelm@13462
   242
    "defined EX" ["EX x. P(x)"] Quantifier1.rearrange_ex;
paulson@4349
   243
paulson@4349
   244
val defALL_regroup =
wenzelm@17002
   245
  Simplifier.simproc (the_context ())
wenzelm@13462
   246
    "defined ALL" ["ALL x. P(x)"] Quantifier1.rearrange_all;
paulson@4349
   247
paulson@4349
   248
paulson@4349
   249
(*** Case splitting ***)
clasohm@0
   250
oheimb@5304
   251
structure SplitterData =
oheimb@5304
   252
  struct
oheimb@5304
   253
  structure Simplifier = Simplifier
oheimb@5555
   254
  val mk_eq          = mk_eq
oheimb@5304
   255
  val meta_eq_to_iff = meta_eq_to_iff
oheimb@5304
   256
  val iffD           = iffD2
oheimb@5304
   257
  val disjE          = disjE
oheimb@5304
   258
  val conjE          = conjE
oheimb@5304
   259
  val exE            = exE
wenzelm@21539
   260
  val contrapos      = thm "contrapos"
wenzelm@21539
   261
  val contrapos2     = thm "contrapos2"
wenzelm@21539
   262
  val notnotD        = thm "notnotD"
oheimb@5304
   263
  end;
berghofe@1722
   264
oheimb@5304
   265
structure Splitter = SplitterFun(SplitterData);
berghofe@1722
   266
oheimb@5304
   267
val split_tac        = Splitter.split_tac;
oheimb@5304
   268
val split_inside_tac = Splitter.split_inside_tac;
oheimb@5304
   269
val split_asm_tac    = Splitter.split_asm_tac;
oheimb@5307
   270
val op addsplits     = Splitter.addsplits;
oheimb@5307
   271
val op delsplits     = Splitter.delsplits;
oheimb@5304
   272
val Addsplits        = Splitter.Addsplits;
oheimb@5304
   273
val Delsplits        = Splitter.Delsplits;
paulson@4325
   274
paulson@4325
   275
paulson@2074
   276
(*** Standard simpsets ***)
paulson@2074
   277
wenzelm@21539
   278
structure Induction = InductionFun(struct val spec = spec end);
paulson@2074
   279
paulson@4349
   280
open Induction;
paulson@2074
   281
oheimb@5555
   282
wenzelm@12038
   283
bind_thms ("meta_simps",
wenzelm@12038
   284
 [triv_forall_equality,   (* prunes params *)
wenzelm@21539
   285
  thm "True_implies_equals"]);  (* prune asms `True' *)
paulson@5496
   286
wenzelm@12038
   287
bind_thms ("IFOL_simps",
wenzelm@12038
   288
 [refl RS P_iff_T] @ conj_simps @ disj_simps @ not_simps @
wenzelm@12038
   289
  imp_simps @ iff_simps @ quant_simps);
paulson@2074
   290
wenzelm@12038
   291
bind_thm ("notFalseI", int_prove_fun "~False");
wenzelm@21539
   292
bind_thms ("triv_rls",
wenzelm@21539
   293
  [TrueI, refl, reflexive_thm, iff_refl, thm "notFalseI"]);
paulson@2074
   294
oheimb@2633
   295
fun unsafe_solver prems = FIRST'[resolve_tac (triv_rls@prems),
wenzelm@9713
   296
                                 atac, etac FalseE];
oheimb@2633
   297
(*No premature instantiation of variables during simplification*)
oheimb@2633
   298
fun   safe_solver prems = FIRST'[match_tac (triv_rls@prems),
wenzelm@9713
   299
                                 eq_assume_tac, ematch_tac [FalseE]];
oheimb@2633
   300
paulson@3910
   301
(*No simprules, but basic infastructure for simplification*)
wenzelm@17892
   302
val FOL_basic_ss =
wenzelm@17892
   303
  Simplifier.theory_context (the_context ()) empty_ss
wenzelm@10431
   304
  setsubgoaler asm_simp_tac
wenzelm@10431
   305
  setSSolver (mk_solver "FOL safe" safe_solver)
wenzelm@10431
   306
  setSolver (mk_solver "FOL unsafe" unsafe_solver)
wenzelm@10431
   307
  setmksimps (mksimps mksimps_pairs)
wenzelm@10431
   308
  setmkcong mk_meta_cong;
oheimb@5304
   309
wenzelm@18324
   310
fun unfold_tac ths =
wenzelm@18324
   311
  let val ss0 = Simplifier.clear_ss FOL_basic_ss addsimps ths
wenzelm@18324
   312
  in fn ss => ALLGOALS (full_simp_tac (Simplifier.inherit_context ss ss0)) end;
wenzelm@17002
   313
oheimb@2633
   314
paulson@3910
   315
(*intuitionistic simprules only*)
wenzelm@21539
   316
val IFOL_ss =
wenzelm@21539
   317
  FOL_basic_ss
wenzelm@10431
   318
  addsimps (meta_simps @ IFOL_simps @ int_ex_simps @ int_all_simps)
wenzelm@10431
   319
  addsimprocs [defALL_regroup, defEX_regroup]    
wenzelm@21539
   320
  addcongs [thm "imp_cong"];
paulson@2074
   321
wenzelm@12038
   322
bind_thms ("cla_simps",
wenzelm@12038
   323
  [de_Morgan_conj, de_Morgan_disj, imp_disj1, imp_disj2,
paulson@12825
   324
   not_imp, not_all, not_ex, cases_simp] @
wenzelm@12038
   325
  map prove_fun
wenzelm@12038
   326
   ["~(P&Q) <-> ~P | ~Q",
wenzelm@12038
   327
    "P | ~P",             "~P | P",
wenzelm@12038
   328
    "~ ~ P <-> P",        "(~P --> P) <-> P",
wenzelm@12038
   329
    "(~P <-> ~Q) <-> (P<->Q)"]);
paulson@2074
   330
paulson@3910
   331
(*classical simprules too*)
paulson@4349
   332
val FOL_ss = IFOL_ss addsimps (cla_simps @ cla_ex_simps @ cla_all_simps);
paulson@2074
   333
wenzelm@18708
   334
val simpsetup = (fn thy => (change_simpset_of thy (fn _ => FOL_ss); thy));
oheimb@2633
   335
oheimb@2633
   336
wenzelm@5219
   337
(*** integration of simplifier with classical reasoner ***)
oheimb@2633
   338
wenzelm@5219
   339
structure Clasimp = ClasimpFun
wenzelm@8472
   340
 (structure Simplifier = Simplifier and Splitter = Splitter
wenzelm@9851
   341
  and Classical  = Cla and Blast = Blast
wenzelm@18529
   342
  val iffD1 = iffD1 val iffD2 = iffD2 val notE = notE);
oheimb@4652
   343
open Clasimp;
oheimb@2633
   344
wenzelm@22128
   345
ML_Context.value_antiq "clasimpset"
wenzelm@22128
   346
  (Scan.succeed ("clasimpset", "Clasimp.local_clasimpset_of (ML_Context.the_local_context ())"));
wenzelm@22128
   347
oheimb@2633
   348
val FOL_css = (FOL_cs, FOL_ss);