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