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