src/Sequents/simpdata.ML
author wenzelm
Sun Sep 18 15:20:08 2005 +0200 (2005-09-18)
changeset 17481 75166ebb619b
parent 12725 7ede865e1fe5
child 17876 b9c92f384109
permissions -rw-r--r--
converted to Isar theory format;
paulson@7098
     1
(*  Title:      Sequents/simpdata.ML
paulson@7098
     2
    ID:         $Id$
paulson@7098
     3
    Author:     Lawrence C Paulson
paulson@7098
     4
    Copyright   1999  University of Cambridge
paulson@7098
     5
paulson@7098
     6
Instantiation of the generic simplifier for LK
paulson@7098
     7
paulson@7098
     8
Borrows from the DC simplifier of Soren Heilmann.
paulson@7098
     9
*)
paulson@7098
    10
paulson@7098
    11
(*** Rewrite rules ***)
paulson@7098
    12
wenzelm@9713
    13
fun prove_fun s =
wenzelm@9713
    14
 (writeln s;
wenzelm@17481
    15
  prove_goal (the_context ()) s
wenzelm@9713
    16
   (fn prems => [ (cut_facts_tac prems 1),
paulson@7123
    17
                  (fast_tac (pack() add_safes [subst]) 1) ]));
paulson@7098
    18
paulson@7098
    19
val conj_simps = map prove_fun
paulson@7098
    20
 ["|- P & True <-> P",      "|- True & P <-> P",
paulson@7098
    21
  "|- P & False <-> False", "|- False & P <-> False",
paulson@7098
    22
  "|- P & P <-> P", "        |- P & P & Q <-> P & Q",
paulson@7098
    23
  "|- P & ~P <-> False",    "|- ~P & P <-> False",
paulson@7098
    24
  "|- (P & Q) & R <-> P & (Q & R)"];
paulson@7098
    25
paulson@7098
    26
val disj_simps = map prove_fun
paulson@7098
    27
 ["|- P | True <-> True",  "|- True | P <-> True",
paulson@7098
    28
  "|- P | False <-> P",    "|- False | P <-> P",
paulson@7098
    29
  "|- P | P <-> P",        "|- P | P | Q <-> P | Q",
paulson@7098
    30
  "|- (P | Q) | R <-> P | (Q | R)"];
paulson@7098
    31
paulson@7098
    32
val not_simps = map prove_fun
paulson@7098
    33
 ["|- ~ False <-> True",   "|- ~ True <-> False"];
paulson@7098
    34
paulson@7098
    35
val imp_simps = map prove_fun
paulson@7098
    36
 ["|- (P --> False) <-> ~P",       "|- (P --> True) <-> True",
wenzelm@9713
    37
  "|- (False --> P) <-> True",     "|- (True --> P) <-> P",
paulson@7098
    38
  "|- (P --> P) <-> True",         "|- (P --> ~P) <-> ~P"];
paulson@7098
    39
paulson@7098
    40
val iff_simps = map prove_fun
paulson@7098
    41
 ["|- (True <-> P) <-> P",         "|- (P <-> True) <-> P",
paulson@7098
    42
  "|- (P <-> P) <-> True",
paulson@7098
    43
  "|- (False <-> P) <-> ~P",       "|- (P <-> False) <-> ~P"];
paulson@7098
    44
paulson@7123
    45
paulson@7123
    46
val quant_simps = map prove_fun
wenzelm@9713
    47
 ["|- (ALL x. P) <-> P",
paulson@7123
    48
  "|- (ALL x. x=t --> P(x)) <-> P(t)",
paulson@7123
    49
  "|- (ALL x. t=x --> P(x)) <-> P(t)",
paulson@7123
    50
  "|- (EX x. P) <-> P",
wenzelm@9713
    51
  "|- (EX x. x=t & P(x)) <-> P(t)",
paulson@7123
    52
  "|- (EX x. t=x & P(x)) <-> P(t)"];
paulson@7123
    53
paulson@7123
    54
(*** Miniscoping: pushing quantifiers in
paulson@7123
    55
     We do NOT distribute of ALL over &, or dually that of EX over |
wenzelm@9713
    56
     Baaz and Leitsch, On Skolemization and Proof Complexity (1994)
paulson@7123
    57
     show that this step can increase proof length!
paulson@7123
    58
***)
paulson@7123
    59
paulson@7123
    60
(*existential miniscoping*)
wenzelm@9713
    61
val ex_simps = map prove_fun
paulson@7123
    62
                   ["|- (EX x. P(x) & Q) <-> (EX x. P(x)) & Q",
wenzelm@9713
    63
                    "|- (EX x. P & Q(x)) <-> P & (EX x. Q(x))",
wenzelm@9713
    64
                    "|- (EX x. P(x) | Q) <-> (EX x. P(x)) | Q",
wenzelm@9713
    65
                    "|- (EX x. P | Q(x)) <-> P | (EX x. Q(x))",
wenzelm@9713
    66
                    "|- (EX x. P(x) --> Q) <-> (ALL x. P(x)) --> Q",
wenzelm@9713
    67
                    "|- (EX x. P --> Q(x)) <-> P --> (EX x. Q(x))"];
paulson@7123
    68
paulson@7123
    69
(*universal miniscoping*)
paulson@7123
    70
val all_simps = map prove_fun
paulson@7123
    71
                    ["|- (ALL x. P(x) & Q) <-> (ALL x. P(x)) & Q",
wenzelm@9713
    72
                     "|- (ALL x. P & Q(x)) <-> P & (ALL x. Q(x))",
wenzelm@9713
    73
                     "|- (ALL x. P(x) --> Q) <-> (EX x. P(x)) --> Q",
wenzelm@9713
    74
                     "|- (ALL x. P --> Q(x)) <-> P --> (ALL x. Q(x))",
wenzelm@9713
    75
                     "|- (ALL x. P(x) | Q) <-> (ALL x. P(x)) | Q",
wenzelm@9713
    76
                     "|- (ALL x. P | Q(x)) <-> P | (ALL x. Q(x))"];
paulson@7123
    77
paulson@7098
    78
(*These are NOT supplied by default!*)
paulson@7098
    79
val distrib_simps  = map prove_fun
wenzelm@9713
    80
 ["|- P & (Q | R) <-> P&Q | P&R",
paulson@7098
    81
  "|- (Q | R) & P <-> Q&P | R&P",
paulson@7098
    82
  "|- (P | Q --> R) <-> (P --> R) & (Q --> R)"];
paulson@7098
    83
paulson@7098
    84
(** Conversion into rewrite rules **)
paulson@7098
    85
paulson@7098
    86
(*Make atomic rewrite rules*)
paulson@7098
    87
fun atomize r =
paulson@7098
    88
 case concl_of r of
paulson@7098
    89
   Const("Trueprop",_) $ Abs(_,_,a) $ Abs(_,_,c) =>
paulson@7098
    90
     (case (forms_of_seq a, forms_of_seq c) of
wenzelm@9713
    91
        ([], [p]) =>
wenzelm@9713
    92
          (case p of
wenzelm@9713
    93
               Const("op -->",_)$_$_ => atomize(r RS mp_R)
wenzelm@9713
    94
             | Const("op &",_)$_$_   => atomize(r RS conjunct1) @
wenzelm@9713
    95
                   atomize(r RS conjunct2)
wenzelm@9713
    96
             | Const("All",_)$_      => atomize(r RS spec)
wenzelm@9713
    97
             | Const("True",_)       => []    (*True is DELETED*)
wenzelm@9713
    98
             | Const("False",_)      => []    (*should False do something?*)
wenzelm@9713
    99
             | _                     => [r])
paulson@7098
   100
      | _ => [])  (*ignore theorem unless it has precisely one conclusion*)
paulson@7098
   101
 | _ => [r];
paulson@7098
   102
paulson@7098
   103
paulson@9259
   104
Goal "|- ~P ==> |- (P <-> False)";
paulson@9259
   105
by (etac (thinR RS cut) 1);
wenzelm@9713
   106
by (Fast_tac 1);
paulson@9259
   107
qed "P_iff_F";
paulson@9259
   108
paulson@7098
   109
val iff_reflection_F = P_iff_F RS iff_reflection;
paulson@7098
   110
paulson@9259
   111
Goal "|- P ==> |- (P <-> True)";
paulson@9259
   112
by (etac (thinR RS cut) 1);
wenzelm@9713
   113
by (Fast_tac 1);
paulson@9259
   114
qed "P_iff_T";
paulson@9259
   115
paulson@7098
   116
val iff_reflection_T = P_iff_T RS iff_reflection;
paulson@7098
   117
paulson@7098
   118
(*Make meta-equalities.*)
paulson@7098
   119
fun mk_meta_eq th = case concl_of th of
paulson@7098
   120
    Const("==",_)$_$_           => th
paulson@7098
   121
  | Const("Trueprop",_) $ Abs(_,_,a) $ Abs(_,_,c) =>
wenzelm@9713
   122
        (case (forms_of_seq a, forms_of_seq c) of
wenzelm@9713
   123
             ([], [p]) =>
wenzelm@9713
   124
                 (case p of
wenzelm@9713
   125
                      (Const("op =",_)$_$_)   => th RS eq_reflection
wenzelm@9713
   126
                    | (Const("op <->",_)$_$_) => th RS iff_reflection
wenzelm@9713
   127
                    | (Const("Not",_)$_)      => th RS iff_reflection_F
wenzelm@9713
   128
                    | _                       => th RS iff_reflection_T)
wenzelm@9713
   129
           | _ => error ("addsimps: unable to use theorem\n" ^
wenzelm@9713
   130
                         string_of_thm th));
paulson@7098
   131
paulson@7098
   132
paulson@7123
   133
(*Replace premises x=y, X<->Y by X==Y*)
wenzelm@9713
   134
val mk_meta_prems =
wenzelm@9713
   135
    rule_by_tactic
paulson@7123
   136
      (REPEAT_FIRST (resolve_tac [meta_eq_to_obj_eq, def_imp_iff]));
paulson@7123
   137
wenzelm@9713
   138
(*Congruence rules for = or <-> (instead of ==)*)
paulson@7123
   139
fun mk_meta_cong rl =
paulson@7123
   140
  standard(mk_meta_eq (mk_meta_prems rl))
paulson@7123
   141
  handle THM _ =>
paulson@7123
   142
  error("Premises and conclusion of congruence rules must use =-equality or <->");
paulson@7123
   143
paulson@7123
   144
paulson@7123
   145
(*** Named rewrite rules ***)
paulson@7098
   146
wenzelm@17481
   147
fun prove nm thm  = qed_goal nm (the_context ()) thm
wenzelm@9713
   148
    (fn prems => [ (cut_facts_tac prems 1),
paulson@7098
   149
                   (fast_tac LK_pack 1) ]);
paulson@7098
   150
paulson@7098
   151
prove "conj_commute" "|- P&Q <-> Q&P";
paulson@7098
   152
prove "conj_left_commute" "|- P&(Q&R) <-> Q&(P&R)";
paulson@7098
   153
val conj_comms = [conj_commute, conj_left_commute];
paulson@7098
   154
paulson@7098
   155
prove "disj_commute" "|- P|Q <-> Q|P";
paulson@7098
   156
prove "disj_left_commute" "|- P|(Q|R) <-> Q|(P|R)";
paulson@7098
   157
val disj_comms = [disj_commute, disj_left_commute];
paulson@7098
   158
paulson@7098
   159
prove "conj_disj_distribL" "|- P&(Q|R) <-> (P&Q | P&R)";
paulson@7098
   160
prove "conj_disj_distribR" "|- (P|Q)&R <-> (P&R | Q&R)";
paulson@7098
   161
paulson@7098
   162
prove "disj_conj_distribL" "|- P|(Q&R) <-> (P|Q) & (P|R)";
paulson@7098
   163
prove "disj_conj_distribR" "|- (P&Q)|R <-> (P|R) & (Q|R)";
paulson@7098
   164
paulson@7098
   165
prove "imp_conj_distrib" "|- (P --> (Q&R)) <-> (P-->Q) & (P-->R)";
paulson@7098
   166
prove "imp_conj"         "|- ((P&Q)-->R)   <-> (P --> (Q --> R))";
paulson@7098
   167
prove "imp_disj"         "|- (P|Q --> R)   <-> (P-->R) & (Q-->R)";
paulson@7098
   168
paulson@7098
   169
prove "imp_disj1" "|- (P-->Q) | R <-> (P-->Q | R)";
paulson@7098
   170
prove "imp_disj2" "|- Q | (P-->R) <-> (P-->Q | R)";
paulson@7098
   171
paulson@7098
   172
prove "de_Morgan_disj" "|- (~(P | Q)) <-> (~P & ~Q)";
paulson@7098
   173
prove "de_Morgan_conj" "|- (~(P & Q)) <-> (~P | ~Q)";
paulson@7098
   174
paulson@7098
   175
prove "not_iff" "|- ~(P <-> Q) <-> (P <-> ~Q)";
paulson@7098
   176
paulson@7098
   177
wenzelm@9713
   178
val [p1,p2] = Goal
paulson@7098
   179
    "[| |- P <-> P';  |- P' ==> |- Q <-> Q' |] ==> |- (P-->Q) <-> (P'-->Q')";
paulson@7098
   180
by (lemma_tac p1 1);
paulson@7098
   181
by (Safe_tac 1);
wenzelm@9713
   182
by (REPEAT (rtac cut 1
wenzelm@9713
   183
            THEN
wenzelm@9713
   184
            DEPTH_SOLVE_1 (resolve_tac [thinL, thinR, p2 COMP monotonic] 1)
wenzelm@9713
   185
            THEN
wenzelm@9713
   186
            Safe_tac 1));
paulson@7098
   187
qed "imp_cong";
paulson@7098
   188
wenzelm@9713
   189
val [p1,p2] = Goal
paulson@7098
   190
    "[| |- P <-> P';  |- P' ==> |- Q <-> Q' |] ==> |- (P&Q) <-> (P'&Q')";
paulson@7098
   191
by (lemma_tac p1 1);
paulson@7098
   192
by (Safe_tac 1);
wenzelm@9713
   193
by (REPEAT (rtac cut 1
wenzelm@9713
   194
            THEN
wenzelm@9713
   195
            DEPTH_SOLVE_1 (resolve_tac [thinL, thinR, p2 COMP monotonic] 1)
wenzelm@9713
   196
            THEN
wenzelm@9713
   197
            Safe_tac 1));
paulson@7098
   198
qed "conj_cong";
paulson@7098
   199
paulson@7123
   200
Goal "|- (x=y) <-> (y=x)";
paulson@7123
   201
by (fast_tac (pack() add_safes [subst]) 1);
paulson@7123
   202
qed "eq_sym_conv";
paulson@7123
   203
paulson@7123
   204
paulson@7123
   205
(** if-then-else rules **)
paulson@7123
   206
paulson@7123
   207
Goalw [If_def] "|- (if True then x else y) = x";
paulson@7123
   208
by (Fast_tac 1);
paulson@7123
   209
qed "if_True";
paulson@7123
   210
paulson@7123
   211
Goalw [If_def] "|- (if False then x else y) = y";
paulson@7123
   212
by (Fast_tac 1);
paulson@7123
   213
qed "if_False";
paulson@7123
   214
paulson@7123
   215
Goalw [If_def] "|- P ==> |- (if P then x else y) = x";
paulson@7123
   216
by (etac (thinR RS cut) 1);
paulson@7123
   217
by (Fast_tac 1);
paulson@7123
   218
qed "if_P";
paulson@7123
   219
paulson@7123
   220
Goalw [If_def] "|- ~P ==> |- (if P then x else y) = y";
paulson@7123
   221
by (etac (thinR RS cut) 1);
paulson@7123
   222
by (Fast_tac 1);
paulson@7123
   223
qed "if_not_P";
paulson@7123
   224
paulson@7098
   225
paulson@7098
   226
open Simplifier;
paulson@7098
   227
paulson@7098
   228
(*** Standard simpsets ***)
paulson@7098
   229
paulson@7123
   230
val triv_rls = [FalseL, TrueR, basic, refl, iff_refl, reflexive_thm];
paulson@7098
   231
paulson@7098
   232
fun unsafe_solver prems = FIRST'[resolve_tac (triv_rls@prems),
wenzelm@9713
   233
                                 assume_tac];
paulson@7098
   234
(*No premature instantiation of variables during simplification*)
paulson@7098
   235
fun   safe_solver prems = FIRST'[fn i => DETERM (match_tac (triv_rls@prems) i),
wenzelm@9713
   236
                                 eq_assume_tac];
paulson@7098
   237
paulson@7098
   238
(*No simprules, but basic infrastructure for simplification*)
wenzelm@9713
   239
val LK_basic_ss =
wenzelm@9713
   240
  empty_ss setsubgoaler asm_simp_tac
wenzelm@9713
   241
    setSSolver (mk_solver "safe" safe_solver)
wenzelm@9713
   242
    setSolver (mk_solver "unsafe" unsafe_solver)
wenzelm@12725
   243
    setmksimps (map mk_meta_eq o atomize o gen_all)
wenzelm@9713
   244
    setmkcong mk_meta_cong;
paulson@7098
   245
paulson@7098
   246
val LK_simps =
paulson@7123
   247
   [triv_forall_equality, (* prunes params *)
wenzelm@9713
   248
    refl RS P_iff_T] @
wenzelm@9713
   249
    conj_simps @ disj_simps @ not_simps @
paulson@7123
   250
    imp_simps @ iff_simps @quant_simps @ all_simps @ ex_simps @
paulson@7098
   251
    [de_Morgan_conj, de_Morgan_disj, imp_disj1, imp_disj2] @
paulson@7098
   252
    map prove_fun
paulson@7098
   253
     ["|- P | ~P",             "|- ~P | P",
paulson@7098
   254
      "|- ~ ~ P <-> P",        "|- (~P --> P) <-> P",
paulson@7098
   255
      "|- (~P <-> ~Q) <-> (P<->Q)"];
paulson@7098
   256
wenzelm@9713
   257
val LK_ss =
wenzelm@9713
   258
  LK_basic_ss addsimps LK_simps
wenzelm@9713
   259
  addeqcongs [left_cong]
wenzelm@9713
   260
  addcongs [imp_cong];
paulson@7098
   261
paulson@7098
   262
simpset_ref() := LK_ss;
paulson@7098
   263
paulson@7098
   264
paulson@7123
   265
(* To create substition rules *)
paulson@7098
   266
wenzelm@17481
   267
qed_goal "eq_imp_subst" (the_context ()) "|- a=b ==> $H, A(a), $G |- $E, A(b), $F"
paulson@7098
   268
  (fn prems =>
paulson@7098
   269
   [cut_facts_tac prems 1,
paulson@7098
   270
    asm_simp_tac LK_basic_ss 1]);
paulson@7098
   271
paulson@7123
   272
Goal "|- P(if Q then x else y) <-> ((Q --> P(x)) & (~Q --> P(y)))";
paulson@7123
   273
by (res_inst_tac [ ("P","Q") ] cut 1);
paulson@7123
   274
by (simp_tac (simpset() addsimps [if_P]) 2);
paulson@7123
   275
by (res_inst_tac [ ("P","~Q") ] cut 1);
paulson@7123
   276
by (simp_tac (simpset() addsimps [if_not_P]) 2);
paulson@7123
   277
by (Fast_tac 1);
paulson@7123
   278
qed "split_if";
paulson@7098
   279
paulson@7123
   280
Goal "|- (if P then x else x) = x";
paulson@7123
   281
by (lemma_tac split_if 1);
paulson@7123
   282
by (Fast_tac 1);
paulson@7123
   283
qed "if_cancel";
paulson@7123
   284
paulson@7123
   285
Goal "|- (if x=y then y else x) = x";
paulson@7123
   286
by (lemma_tac split_if 1);
paulson@7123
   287
by (Safe_tac 1);
paulson@7123
   288
by (rtac symL 1);
paulson@7123
   289
by (rtac basic 1);
paulson@7123
   290
qed "if_eq_cancel";
paulson@7123
   291
paulson@7123
   292
(*Putting in automatic case splits seems to require a lot of work.*)