src/FOL/simpdata.ML
author wenzelm
Tue Aug 29 00:55:31 2000 +0200 (2000-08-29 ago)
changeset 9713 2c5b42311eb0
parent 9300 ee5c9672d208
child 9851 e22db9397e17
permissions -rw-r--r--
cong setup now part of Simplifier;
     1 (*  Title:      FOL/simpdata
     2     ID:         $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4     Copyright   1994  University of Cambridge
     5 
     6 Simplification data for FOL
     7 *)
     8 
     9 (*** Addition of rules to simpsets and clasets simultaneously ***)      (* FIXME move to Provers/clasimp.ML? *)
    10 
    11 infix 4 addIffs delIffs;
    12 
    13 (*Takes UNCONDITIONAL theorems of the form A<->B to 
    14         the Safe Intr     rule B==>A and 
    15         the Safe Destruct rule A==>B.
    16   Also ~A goes to the Safe Elim rule A ==> ?R
    17   Failing other cases, A is added as a Safe Intr rule*)
    18 local
    19   fun addIff ((cla, simp), th) = 
    20       (case FOLogic.dest_Trueprop (#prop (rep_thm th)) of
    21                 (Const("Not", _) $ A) =>
    22                     cla addSEs [zero_var_indexes (th RS notE)]
    23               | (Const("op <->", _) $ _ $ _) =>
    24                     cla addSIs [zero_var_indexes (th RS iffD2)]  
    25                         addSDs [zero_var_indexes (th RS iffD1)]
    26               | _ => cla addSIs [th],
    27        simp addsimps [th])
    28       handle TERM _ => error ("AddIffs: theorem must be unconditional\n" ^ 
    29                          string_of_thm th);
    30 
    31   fun delIff ((cla, simp), th) = 
    32       (case FOLogic.dest_Trueprop (#prop (rep_thm th)) of
    33            (Const ("Not", _) $ A) =>
    34                cla delrules [zero_var_indexes (th RS notE)]
    35          | (Const("op <->", _) $ _ $ _) =>
    36                cla delrules [zero_var_indexes (th RS iffD2),
    37                              cla_make_elim (zero_var_indexes (th RS iffD1))]
    38          | _ => cla delrules [th],
    39        simp delsimps [th])
    40       handle TERM _ => (warning("DelIffs: ignoring conditional theorem\n" ^ 
    41                                 string_of_thm th); (cla, simp));
    42 
    43   fun store_clasimp (cla, simp) = (claset_ref () := cla; simpset_ref () := simp)
    44 in
    45 val op addIffs = foldl addIff;
    46 val op delIffs = foldl delIff;
    47 fun AddIffs thms = store_clasimp ((claset (), simpset ()) addIffs thms);
    48 fun DelIffs thms = store_clasimp ((claset (), simpset ()) delIffs thms);
    49 end;
    50 
    51 
    52 
    53 (* Elimination of True from asumptions: *)
    54 
    55 val True_implies_equals = prove_goal IFOL.thy
    56  "(True ==> PROP P) == PROP P"
    57 (K [rtac equal_intr_rule 1, atac 2,
    58           METAHYPS (fn prems => resolve_tac prems 1) 1,
    59           rtac TrueI 1]);
    60 
    61 
    62 (*** Rewrite rules ***)
    63 
    64 fun int_prove_fun s = 
    65  (writeln s;  
    66   prove_goal IFOL.thy s
    67    (fn prems => [ (cut_facts_tac prems 1), 
    68                   (IntPr.fast_tac 1) ]));
    69 
    70 val conj_simps = map int_prove_fun
    71  ["P & True <-> P",      "True & P <-> P",
    72   "P & False <-> False", "False & P <-> False",
    73   "P & P <-> P", "P & P & Q <-> P & Q",
    74   "P & ~P <-> False",    "~P & P <-> False",
    75   "(P & Q) & R <-> P & (Q & R)"];
    76 
    77 val disj_simps = map int_prove_fun
    78  ["P | True <-> True",  "True | P <-> True",
    79   "P | False <-> P",    "False | P <-> P",
    80   "P | P <-> P", "P | P | Q <-> P | Q",
    81   "(P | Q) | R <-> P | (Q | R)"];
    82 
    83 val not_simps = map int_prove_fun
    84  ["~(P|Q)  <-> ~P & ~Q",
    85   "~ False <-> True",   "~ True <-> False"];
    86 
    87 val imp_simps = map int_prove_fun
    88  ["(P --> False) <-> ~P",       "(P --> True) <-> True",
    89   "(False --> P) <-> True",     "(True --> P) <-> P", 
    90   "(P --> P) <-> True",         "(P --> ~P) <-> ~P"];
    91 
    92 val iff_simps = map int_prove_fun
    93  ["(True <-> P) <-> P",         "(P <-> True) <-> P",
    94   "(P <-> P) <-> True",
    95   "(False <-> P) <-> ~P",       "(P <-> False) <-> ~P"];
    96 
    97 (*The x=t versions are needed for the simplification procedures*)
    98 val quant_simps = map int_prove_fun
    99  ["(ALL x. P) <-> P",   
   100   "(ALL x. x=t --> P(x)) <-> P(t)",
   101   "(ALL x. t=x --> P(x)) <-> P(t)",
   102   "(EX x. P) <-> P",
   103   "(EX x. x=t & P(x)) <-> P(t)", 
   104   "(EX x. t=x & P(x)) <-> P(t)"];
   105 
   106 (*These are NOT supplied by default!*)
   107 val distrib_simps  = map int_prove_fun
   108  ["P & (Q | R) <-> P&Q | P&R", 
   109   "(Q | R) & P <-> Q&P | R&P",
   110   "(P | Q --> R) <-> (P --> R) & (Q --> R)"];
   111 
   112 (** Conversion into rewrite rules **)
   113 
   114 fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
   115 
   116 val P_iff_F = int_prove_fun "~P ==> (P <-> False)";
   117 val iff_reflection_F = P_iff_F RS iff_reflection;
   118 
   119 val P_iff_T = int_prove_fun "P ==> (P <-> True)";
   120 val iff_reflection_T = P_iff_T RS iff_reflection;
   121 
   122 (*Make meta-equalities.  The operator below is Trueprop*)
   123 
   124 fun mk_meta_eq th = case concl_of th of
   125     _ $ (Const("op =",_)$_$_)   => th RS eq_reflection
   126   | _ $ (Const("op <->",_)$_$_) => th RS iff_reflection
   127   | _                           => 
   128   error("conclusion must be a =-equality or <->");;
   129 
   130 fun mk_eq th = case concl_of th of
   131     Const("==",_)$_$_           => th
   132   | _ $ (Const("op =",_)$_$_)   => mk_meta_eq th
   133   | _ $ (Const("op <->",_)$_$_) => mk_meta_eq th
   134   | _ $ (Const("Not",_)$_)      => th RS iff_reflection_F
   135   | _                           => th RS iff_reflection_T;
   136 
   137 (*Replace premises x=y, X<->Y by X==Y*)
   138 val mk_meta_prems = 
   139     rule_by_tactic 
   140       (REPEAT_FIRST (resolve_tac [meta_eq_to_obj_eq, def_imp_iff]));
   141 
   142 (*Congruence rules for = or <-> (instead of ==)*)
   143 fun mk_meta_cong rl =
   144   standard(mk_meta_eq (mk_meta_prems rl))
   145   handle THM _ =>
   146   error("Premises and conclusion of congruence rules must use =-equality or <->");
   147 
   148 val mksimps_pairs =
   149   [("op -->", [mp]), ("op &", [conjunct1,conjunct2]),
   150    ("All", [spec]), ("True", []), ("False", [])];
   151 
   152 (* ###FIXME: move to Provers/simplifier.ML
   153 val mk_atomize:      (string * thm list) list -> thm -> thm list
   154 *)
   155 (* ###FIXME: move to Provers/simplifier.ML *)
   156 fun mk_atomize pairs =
   157   let fun atoms th =
   158         (case concl_of th of
   159            Const("Trueprop",_) $ p =>
   160              (case head_of p of
   161                 Const(a,_) =>
   162                   (case assoc(pairs,a) of
   163                      Some(rls) => flat (map atoms ([th] RL rls))
   164                    | None => [th])
   165               | _ => [th])
   166          | _ => [th])
   167   in atoms end;
   168 
   169 fun mksimps pairs = (map mk_eq o mk_atomize pairs o gen_all);
   170 
   171 (*** Classical laws ***)
   172 
   173 fun prove_fun s = 
   174  (writeln s;  
   175   prove_goal (the_context ()) s
   176    (fn prems => [ (cut_facts_tac prems 1), 
   177                   (Cla.fast_tac FOL_cs 1) ]));
   178 
   179 (*Avoids duplication of subgoals after expand_if, when the true and false 
   180   cases boil down to the same thing.*) 
   181 val cases_simp = prove_fun "(P --> Q) & (~P --> Q) <-> Q";
   182 
   183 
   184 (*** Miniscoping: pushing quantifiers in
   185      We do NOT distribute of ALL over &, or dually that of EX over |
   186      Baaz and Leitsch, On Skolemization and Proof Complexity (1994) 
   187      show that this step can increase proof length!
   188 ***)
   189 
   190 (*existential miniscoping*)
   191 val int_ex_simps = map int_prove_fun 
   192                      ["(EX x. P(x) & Q) <-> (EX x. P(x)) & Q",
   193                       "(EX x. P & Q(x)) <-> P & (EX x. Q(x))",
   194                       "(EX x. P(x) | Q) <-> (EX x. P(x)) | Q",
   195                       "(EX x. P | Q(x)) <-> P | (EX x. Q(x))"];
   196 
   197 (*classical rules*)
   198 val cla_ex_simps = map prove_fun 
   199                      ["(EX x. P(x) --> Q) <-> (ALL x. P(x)) --> Q",
   200                       "(EX x. P --> Q(x)) <-> P --> (EX x. Q(x))"];
   201 
   202 val ex_simps = int_ex_simps @ cla_ex_simps;
   203 
   204 (*universal miniscoping*)
   205 val int_all_simps = map int_prove_fun
   206                       ["(ALL x. P(x) & Q) <-> (ALL x. P(x)) & Q",
   207                        "(ALL x. P & Q(x)) <-> P & (ALL x. Q(x))",
   208                        "(ALL x. P(x) --> Q) <-> (EX x. P(x)) --> Q",
   209                        "(ALL x. P --> Q(x)) <-> P --> (ALL x. Q(x))"];
   210 
   211 (*classical rules*)
   212 val cla_all_simps = map prove_fun
   213                       ["(ALL x. P(x) | Q) <-> (ALL x. P(x)) | Q",
   214                        "(ALL x. P | Q(x)) <-> P | (ALL x. Q(x))"];
   215 
   216 val all_simps = int_all_simps @ cla_all_simps;
   217 
   218 
   219 (*** Named rewrite rules proved for IFOL ***)
   220 
   221 fun int_prove nm thm  = qed_goal nm IFOL.thy thm
   222     (fn prems => [ (cut_facts_tac prems 1), 
   223                    (IntPr.fast_tac 1) ]);
   224 
   225 fun prove nm thm  = qed_goal nm (the_context ()) thm (fn _ => [Blast_tac 1]);
   226 
   227 int_prove "conj_commute" "P&Q <-> Q&P";
   228 int_prove "conj_left_commute" "P&(Q&R) <-> Q&(P&R)";
   229 val conj_comms = [conj_commute, conj_left_commute];
   230 
   231 int_prove "disj_commute" "P|Q <-> Q|P";
   232 int_prove "disj_left_commute" "P|(Q|R) <-> Q|(P|R)";
   233 val disj_comms = [disj_commute, disj_left_commute];
   234 
   235 int_prove "conj_disj_distribL" "P&(Q|R) <-> (P&Q | P&R)";
   236 int_prove "conj_disj_distribR" "(P|Q)&R <-> (P&R | Q&R)";
   237 
   238 int_prove "disj_conj_distribL" "P|(Q&R) <-> (P|Q) & (P|R)";
   239 int_prove "disj_conj_distribR" "(P&Q)|R <-> (P|R) & (Q|R)";
   240 
   241 int_prove "imp_conj_distrib" "(P --> (Q&R)) <-> (P-->Q) & (P-->R)";
   242 int_prove "imp_conj"         "((P&Q)-->R)   <-> (P --> (Q --> R))";
   243 int_prove "imp_disj"         "(P|Q --> R)   <-> (P-->R) & (Q-->R)";
   244 
   245 prove "imp_disj1" "(P-->Q) | R <-> (P-->Q | R)";
   246 prove "imp_disj2" "Q | (P-->R) <-> (P-->Q | R)";
   247 
   248 int_prove "de_Morgan_disj" "(~(P | Q)) <-> (~P & ~Q)";
   249 prove     "de_Morgan_conj" "(~(P & Q)) <-> (~P | ~Q)";
   250 
   251 prove     "not_iff" "~(P <-> Q) <-> (P <-> ~Q)";
   252 
   253 prove     "not_all" "(~ (ALL x. P(x))) <-> (EX x.~P(x))";
   254 prove     "imp_all" "((ALL x. P(x)) --> Q) <-> (EX x. P(x) --> Q)";
   255 int_prove "not_ex"  "(~ (EX x. P(x))) <-> (ALL x.~P(x))";
   256 int_prove "imp_ex" "((EX x. P(x)) --> Q) <-> (ALL x. P(x) --> Q)";
   257 
   258 int_prove "ex_disj_distrib"
   259     "(EX x. P(x) | Q(x)) <-> ((EX x. P(x)) | (EX x. Q(x)))";
   260 int_prove "all_conj_distrib"
   261     "(ALL x. P(x) & Q(x)) <-> ((ALL x. P(x)) & (ALL x. Q(x)))";
   262 
   263 
   264 (** make simplification procedures for quantifier elimination **)
   265 structure Quantifier1 = Quantifier1Fun(
   266 struct
   267   (*abstract syntax*)
   268   fun dest_eq((c as Const("op =",_)) $ s $ t) = Some(c,s,t)
   269     | dest_eq _ = None;
   270   fun dest_conj((c as Const("op &",_)) $ s $ t) = Some(c,s,t)
   271     | dest_conj _ = None;
   272   val conj = FOLogic.conj
   273   val imp  = FOLogic.imp
   274   (*rules*)
   275   val iff_reflection = iff_reflection
   276   val iffI = iffI
   277   val sym  = sym
   278   val conjI= conjI
   279   val conjE= conjE
   280   val impI = impI
   281   val impE = impE
   282   val mp   = mp
   283   val exI  = exI
   284   val exE  = exE
   285   val allI = allI
   286   val allE = allE
   287 end);
   288 
   289 local
   290 
   291 val ex_pattern =
   292   read_cterm (Theory.sign_of (the_context ())) ("EX x. P(x) & Q(x)", FOLogic.oT)
   293 
   294 val all_pattern =
   295   read_cterm (Theory.sign_of (the_context ())) ("ALL x. P(x) & P'(x) --> Q(x)", FOLogic.oT)
   296 
   297 in
   298 val defEX_regroup =
   299   mk_simproc "defined EX" [ex_pattern] Quantifier1.rearrange_ex;
   300 val defALL_regroup =
   301   mk_simproc "defined ALL" [all_pattern] Quantifier1.rearrange_all;
   302 end;
   303 
   304 
   305 (*** Case splitting ***)
   306 
   307 val meta_eq_to_iff = prove_goal IFOL.thy "x==y ==> x<->y"
   308   (fn [prem] => [rewtac prem, rtac iffI 1, atac 1, atac 1]);
   309 
   310 structure SplitterData =
   311   struct
   312   structure Simplifier = Simplifier
   313   val mk_eq          = mk_eq
   314   val meta_eq_to_iff = meta_eq_to_iff
   315   val iffD           = iffD2
   316   val disjE          = disjE
   317   val conjE          = conjE
   318   val exE            = exE
   319   val contrapos      = contrapos
   320   val contrapos2     = contrapos2
   321   val notnotD        = notnotD
   322   end;
   323 
   324 structure Splitter = SplitterFun(SplitterData);
   325 
   326 val split_tac        = Splitter.split_tac;
   327 val split_inside_tac = Splitter.split_inside_tac;
   328 val split_asm_tac    = Splitter.split_asm_tac;
   329 val op addsplits     = Splitter.addsplits;
   330 val op delsplits     = Splitter.delsplits;
   331 val Addsplits        = Splitter.Addsplits;
   332 val Delsplits        = Splitter.Delsplits;
   333 
   334 
   335 (*** Standard simpsets ***)
   336 
   337 structure Induction = InductionFun(struct val spec=IFOL.spec end);
   338 
   339 open Induction;
   340 
   341 
   342 val meta_simps =
   343    [triv_forall_equality,  (* prunes params *)
   344     True_implies_equals];  (* prune asms `True' *)
   345 
   346 val IFOL_simps =
   347     [refl RS P_iff_T] @ conj_simps @ disj_simps @ not_simps @ 
   348     imp_simps @ iff_simps @ quant_simps;
   349 
   350 val notFalseI = int_prove_fun "~False";
   351 val triv_rls = [TrueI,refl,reflexive_thm,iff_refl,notFalseI];
   352 
   353 fun unsafe_solver prems = FIRST'[resolve_tac (triv_rls@prems),
   354                                  atac, etac FalseE];
   355 (*No premature instantiation of variables during simplification*)
   356 fun   safe_solver prems = FIRST'[match_tac (triv_rls@prems),
   357                                  eq_assume_tac, ematch_tac [FalseE]];
   358 
   359 (*No simprules, but basic infastructure for simplification*)
   360 val FOL_basic_ss =
   361   empty_ss setsubgoaler asm_simp_tac
   362     addsimprocs [defALL_regroup, defEX_regroup]
   363     setSSolver (mk_solver "FOL safe" safe_solver)
   364     setSolver (mk_solver "FOL unsafe" unsafe_solver)
   365     setmksimps (mksimps mksimps_pairs)
   366     setmkcong mk_meta_cong;
   367 
   368 
   369 (*intuitionistic simprules only*)
   370 val IFOL_ss =
   371     FOL_basic_ss addsimps (meta_simps @ IFOL_simps @
   372                            int_ex_simps @ int_all_simps)
   373                  addcongs [imp_cong];
   374 
   375 val cla_simps =
   376     [de_Morgan_conj, de_Morgan_disj, imp_disj1, imp_disj2,
   377      not_all, not_ex, cases_simp] @
   378     map prove_fun
   379      ["~(P&Q)  <-> ~P | ~Q",
   380       "P | ~P",             "~P | P",
   381       "~ ~ P <-> P",        "(~P --> P) <-> P",
   382       "(~P <-> ~Q) <-> (P<->Q)"];
   383 
   384 (*classical simprules too*)
   385 val FOL_ss = IFOL_ss addsimps (cla_simps @ cla_ex_simps @ cla_all_simps);
   386 
   387 val simpsetup = [fn thy => (simpset_ref_of thy := FOL_ss; thy)];
   388 
   389 
   390 (*** integration of simplifier with classical reasoner ***)
   391 
   392 structure Clasimp = ClasimpFun
   393  (structure Simplifier = Simplifier and Splitter = Splitter
   394    and Classical  = Cla and Blast = Blast);
   395 open Clasimp;
   396 
   397 val FOL_css = (FOL_cs, FOL_ss);