src/FOL/simpdata.ML
author nipkow
Sat Feb 28 15:41:17 1998 +0100 (1998-02-28 ago)
changeset 4669 06f3c56dcba8
parent 4652 d24cca140eeb
child 4794 9db0916ecdae
permissions -rw-r--r--
Splitters via named loopers.
     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 (*** Rewrite rules ***)
    10 
    11 fun int_prove_fun s = 
    12  (writeln s;  
    13   prove_goal IFOL.thy s
    14    (fn prems => [ (cut_facts_tac prems 1), 
    15                   (IntPr.fast_tac 1) ]));
    16 
    17 val conj_simps = map int_prove_fun
    18  ["P & True <-> P",      "True & P <-> P",
    19   "P & False <-> False", "False & P <-> False",
    20   "P & P <-> P", "P & P & Q <-> P & Q",
    21   "P & ~P <-> False",    "~P & P <-> False",
    22   "(P & Q) & R <-> P & (Q & R)"];
    23 
    24 val disj_simps = map int_prove_fun
    25  ["P | True <-> True",  "True | P <-> True",
    26   "P | False <-> P",    "False | P <-> P",
    27   "P | P <-> P", "P | P | Q <-> P | Q",
    28   "(P | Q) | R <-> P | (Q | R)"];
    29 
    30 val not_simps = map int_prove_fun
    31  ["~(P|Q)  <-> ~P & ~Q",
    32   "~ False <-> True",   "~ True <-> False"];
    33 
    34 val imp_simps = map int_prove_fun
    35  ["(P --> False) <-> ~P",       "(P --> True) <-> True",
    36   "(False --> P) <-> True",     "(True --> P) <-> P", 
    37   "(P --> P) <-> True",         "(P --> ~P) <-> ~P"];
    38 
    39 val iff_simps = map int_prove_fun
    40  ["(True <-> P) <-> P",         "(P <-> True) <-> P",
    41   "(P <-> P) <-> True",
    42   "(False <-> P) <-> ~P",       "(P <-> False) <-> ~P"];
    43 
    44 (*The x=t versions are needed for the simplification procedures*)
    45 val quant_simps = map int_prove_fun
    46  ["(ALL x. P) <-> P",   
    47   "(ALL x. x=t --> P(x)) <-> P(t)",
    48   "(ALL x. t=x --> P(x)) <-> P(t)",
    49   "(EX x. P) <-> P",
    50   "(EX x. x=t & P(x)) <-> P(t)", 
    51   "(EX x. t=x & P(x)) <-> P(t)"];
    52 
    53 (*These are NOT supplied by default!*)
    54 val distrib_simps  = map int_prove_fun
    55  ["P & (Q | R) <-> P&Q | P&R", 
    56   "(Q | R) & P <-> Q&P | R&P",
    57   "(P | Q --> R) <-> (P --> R) & (Q --> R)"];
    58 
    59 (** Conversion into rewrite rules **)
    60 
    61 fun gen_all th = forall_elim_vars (#maxidx(rep_thm th)+1) th;
    62 
    63 (*Make atomic rewrite rules*)
    64 fun atomize r =
    65   case concl_of r of
    66     Const("Trueprop",_) $ p =>
    67       (case p of
    68          Const("op -->",_)$_$_ => atomize(r RS mp)
    69        | Const("op &",_)$_$_   => atomize(r RS conjunct1) @
    70                                   atomize(r RS conjunct2)
    71        | Const("All",_)$_      => atomize(r RS spec)
    72        | Const("True",_)       => []    (*True is DELETED*)
    73        | Const("False",_)      => []    (*should False do something?*)
    74        | _                     => [r])
    75   | _ => [r];
    76 
    77 
    78 val P_iff_F = int_prove_fun "~P ==> (P <-> False)";
    79 val iff_reflection_F = P_iff_F RS iff_reflection;
    80 
    81 val P_iff_T = int_prove_fun "P ==> (P <-> True)";
    82 val iff_reflection_T = P_iff_T RS iff_reflection;
    83 
    84 (*Make meta-equalities.  The operator below is Trueprop*)
    85 fun mk_meta_eq th = case concl_of th of
    86     Const("==",_)$_$_           => th
    87   | _ $ (Const("op =",_)$_$_)   => th RS eq_reflection
    88   | _ $ (Const("op <->",_)$_$_) => th RS iff_reflection
    89   | _ $ (Const("Not",_)$_)      => th RS iff_reflection_F
    90   | _                           => th RS iff_reflection_T;
    91 
    92 
    93 (*** Classical laws ***)
    94 
    95 fun prove_fun s = 
    96  (writeln s;  
    97   prove_goal FOL.thy s
    98    (fn prems => [ (cut_facts_tac prems 1), 
    99                   (Cla.fast_tac FOL_cs 1) ]));
   100 
   101 (*Avoids duplication of subgoals after expand_if, when the true and false 
   102   cases boil down to the same thing.*) 
   103 val cases_simp = prove_fun "(P --> Q) & (~P --> Q) <-> Q";
   104 
   105 
   106 (*** Miniscoping: pushing quantifiers in
   107      We do NOT distribute of ALL over &, or dually that of EX over |
   108      Baaz and Leitsch, On Skolemization and Proof Complexity (1994) 
   109      show that this step can increase proof length!
   110 ***)
   111 
   112 (*existential miniscoping*)
   113 val int_ex_simps = map int_prove_fun 
   114 		     ["(EX x. P(x) & Q) <-> (EX x. P(x)) & Q",
   115 		      "(EX x. P & Q(x)) <-> P & (EX x. Q(x))",
   116 		      "(EX x. P(x) | Q) <-> (EX x. P(x)) | Q",
   117 		      "(EX x. P | Q(x)) <-> P | (EX x. Q(x))"];
   118 
   119 (*classical rules*)
   120 val cla_ex_simps = map prove_fun 
   121                      ["(EX x. P(x) --> Q) <-> (ALL x. P(x)) --> Q",
   122 		      "(EX x. P --> Q(x)) <-> P --> (EX x. Q(x))"];
   123 
   124 val ex_simps = int_ex_simps @ cla_ex_simps;
   125 
   126 (*universal miniscoping*)
   127 val int_all_simps = map int_prove_fun
   128 		      ["(ALL x. P(x) & Q) <-> (ALL x. P(x)) & Q",
   129 		       "(ALL x. P & Q(x)) <-> P & (ALL x. Q(x))",
   130 		       "(ALL x. P(x) --> Q) <-> (EX x. P(x)) --> Q",
   131 		       "(ALL x. P --> Q(x)) <-> P --> (ALL x. Q(x))"];
   132 
   133 (*classical rules*)
   134 val cla_all_simps = map prove_fun
   135                       ["(ALL x. P(x) | Q) <-> (ALL x. P(x)) | Q",
   136 		       "(ALL x. P | Q(x)) <-> P | (ALL x. Q(x))"];
   137 
   138 val all_simps = int_all_simps @ cla_all_simps;
   139 
   140 
   141 (*** Named rewrite rules proved for IFOL ***)
   142 
   143 fun int_prove nm thm  = qed_goal nm IFOL.thy thm
   144     (fn prems => [ (cut_facts_tac prems 1), 
   145                    (IntPr.fast_tac 1) ]);
   146 
   147 fun prove nm thm  = qed_goal nm FOL.thy thm (fn _ => [Blast_tac 1]);
   148 
   149 int_prove "conj_commute" "P&Q <-> Q&P";
   150 int_prove "conj_left_commute" "P&(Q&R) <-> Q&(P&R)";
   151 val conj_comms = [conj_commute, conj_left_commute];
   152 
   153 int_prove "disj_commute" "P|Q <-> Q|P";
   154 int_prove "disj_left_commute" "P|(Q|R) <-> Q|(P|R)";
   155 val disj_comms = [disj_commute, disj_left_commute];
   156 
   157 int_prove "conj_disj_distribL" "P&(Q|R) <-> (P&Q | P&R)";
   158 int_prove "conj_disj_distribR" "(P|Q)&R <-> (P&R | Q&R)";
   159 
   160 int_prove "disj_conj_distribL" "P|(Q&R) <-> (P|Q) & (P|R)";
   161 int_prove "disj_conj_distribR" "(P&Q)|R <-> (P|R) & (Q|R)";
   162 
   163 int_prove "imp_conj_distrib" "(P --> (Q&R)) <-> (P-->Q) & (P-->R)";
   164 int_prove "imp_conj"         "((P&Q)-->R)   <-> (P --> (Q --> R))";
   165 int_prove "imp_disj"         "(P|Q --> R)   <-> (P-->R) & (Q-->R)";
   166 
   167 prove "imp_disj1" "(P-->Q) | R <-> (P-->Q | R)";
   168 prove "imp_disj2" "Q | (P-->R) <-> (P-->Q | R)";
   169 
   170 int_prove "de_Morgan_disj" "(~(P | Q)) <-> (~P & ~Q)";
   171 prove     "de_Morgan_conj" "(~(P & Q)) <-> (~P | ~Q)";
   172 
   173 prove     "not_iff" "~(P <-> Q) <-> (P <-> ~Q)";
   174 
   175 prove     "not_all" "(~ (ALL x. P(x))) <-> (EX x.~P(x))";
   176 prove     "imp_all" "((ALL x. P(x)) --> Q) <-> (EX x. P(x) --> Q)";
   177 int_prove "not_ex"  "(~ (EX x. P(x))) <-> (ALL x.~P(x))";
   178 int_prove "imp_ex" "((EX x. P(x)) --> Q) <-> (ALL x. P(x) --> Q)";
   179 
   180 int_prove "ex_disj_distrib"
   181     "(EX x. P(x) | Q(x)) <-> ((EX x. P(x)) | (EX x. Q(x)))";
   182 int_prove "all_conj_distrib"
   183     "(ALL x. P(x) & Q(x)) <-> ((ALL x. P(x)) & (ALL x. Q(x)))";
   184 
   185 
   186 (*Used in ZF, perhaps elsewhere?*)
   187 val meta_eq_to_obj_eq = prove_goal IFOL.thy "x==y ==> x=y"
   188   (fn [prem] => [rewtac prem, rtac refl 1]);
   189 
   190 
   191 open Simplifier;
   192 
   193 (** make simplification procedures for quantifier elimination **)
   194 structure Quantifier1 = Quantifier1Fun(
   195 struct
   196   (*abstract syntax*)
   197   fun dest_eq((c as Const("op =",_)) $ s $ t) = Some(c,s,t)
   198     | dest_eq _ = None;
   199   fun dest_conj((c as Const("op &",_)) $ s $ t) = Some(c,s,t)
   200     | dest_conj _ = None;
   201   val conj = FOLogic.conj
   202   val imp  = FOLogic.imp
   203   (*rules*)
   204   val iff_reflection = iff_reflection
   205   val iffI = iffI
   206   val sym  = sym
   207   val conjI= conjI
   208   val conjE= conjE
   209   val impI = impI
   210   val impE = impE
   211   val mp   = mp
   212   val exI  = exI
   213   val exE  = exE
   214   val allI = allI
   215   val allE = allE
   216 end);
   217 
   218 local
   219 val ex_pattern =
   220   read_cterm (sign_of FOL.thy) ("EX x. P(x) & Q(x)", FOLogic.oT)
   221 
   222 val all_pattern =
   223   read_cterm (sign_of FOL.thy) ("ALL x. P(x) & P'(x) --> Q(x)", FOLogic.oT)
   224 
   225 in
   226 val defEX_regroup =
   227   mk_simproc "defined EX" [ex_pattern] Quantifier1.rearrange_ex;
   228 val defALL_regroup =
   229   mk_simproc "defined ALL" [all_pattern] Quantifier1.rearrange_all;
   230 end;
   231 
   232 
   233 (*** Case splitting ***)
   234 
   235 qed_goal "meta_iffD" IFOL.thy "[| P==Q; Q |] ==> P"
   236         (fn [prem1,prem2] => [rewtac prem1, rtac prem2 1]);
   237 
   238 local val mktac = mk_case_split_tac meta_iffD
   239 in
   240 fun split_tac splits = mktac (map mk_meta_eq splits)
   241 end;
   242 
   243 local val mktac = mk_case_split_inside_tac meta_iffD
   244 in
   245 fun split_inside_tac splits = mktac (map mk_meta_eq splits)
   246 end;
   247 
   248 val split_asm_tac = mk_case_split_asm_tac split_tac 
   249 			(disjE,conjE,exE,contrapos,contrapos2,notnotD);
   250 
   251 
   252 
   253 (*** Standard simpsets ***)
   254 
   255 structure Induction = InductionFun(struct val spec=IFOL.spec end);
   256 
   257 open Induction;
   258 
   259 (*Add congruence rules for = or <-> (instead of ==) *)
   260 infix 4 addcongs delcongs;
   261 fun ss addcongs congs =
   262         ss addeqcongs (map standard (congs RL [eq_reflection,iff_reflection]));
   263 fun ss delcongs congs =
   264         ss deleqcongs (map standard (congs RL [eq_reflection,iff_reflection]));
   265 
   266 fun Addcongs congs = (simpset_ref() := simpset() addcongs congs);
   267 fun Delcongs congs = (simpset_ref() := simpset() delcongs congs);
   268 
   269 infix 4 addsplits;
   270 fun ss addsplits splits =
   271   let fun addsplit(ss,split) =
   272         let val name = "split " ^ const_of_split_thm split
   273         in ss addloop (name,split_tac [split]) end
   274   in foldl addsplit (ss,splits) end;
   275 
   276 val IFOL_simps =
   277    [refl RS P_iff_T] @ conj_simps @ disj_simps @ not_simps @ 
   278     imp_simps @ iff_simps @ quant_simps;
   279 
   280 val notFalseI = int_prove_fun "~False";
   281 val triv_rls = [TrueI,refl,iff_refl,notFalseI];
   282 
   283 fun unsafe_solver prems = FIRST'[resolve_tac (triv_rls@prems),
   284 				 atac, etac FalseE];
   285 (*No premature instantiation of variables during simplification*)
   286 fun   safe_solver prems = FIRST'[match_tac (triv_rls@prems),
   287 				 eq_assume_tac, ematch_tac [FalseE]];
   288 
   289 (*No simprules, but basic infastructure for simplification*)
   290 val FOL_basic_ss = empty_ss setsubgoaler asm_simp_tac
   291                             addsimprocs [defALL_regroup,defEX_regroup]
   292 			    setSSolver   safe_solver
   293 			    setSolver  unsafe_solver
   294 			    setmksimps (map mk_meta_eq o atomize o gen_all);
   295 
   296 (*intuitionistic simprules only*)
   297 val IFOL_ss = FOL_basic_ss addsimps (IFOL_simps @ int_ex_simps @ int_all_simps)
   298 			   addcongs [imp_cong];
   299 
   300 val cla_simps = 
   301     [de_Morgan_conj, de_Morgan_disj, imp_disj1, imp_disj2,
   302      not_all, not_ex, cases_simp] @
   303     map prove_fun
   304      ["~(P&Q)  <-> ~P | ~Q",
   305       "P | ~P",             "~P | P",
   306       "~ ~ P <-> P",        "(~P --> P) <-> P",
   307       "(~P <-> ~Q) <-> (P<->Q)"];
   308 
   309 (*classical simprules too*)
   310 val FOL_ss = IFOL_ss addsimps (cla_simps @ cla_ex_simps @ cla_all_simps);
   311 
   312 simpset_ref() := FOL_ss;
   313 
   314 
   315 
   316 
   317 
   318 
   319 
   320 (*** Integration of simplifier with classical reasoner ***)
   321 
   322 (* rot_eq_tac rotates the first equality premise of subgoal i to the front,
   323    fails if there is no equaliy or if an equality is already at the front *)
   324 local
   325   fun is_eq (Const ("Trueprop", _) $ (Const("op ="  ,_) $ _ $ _)) = true
   326     | is_eq (Const ("Trueprop", _) $ (Const("op <->",_) $ _ $ _)) = true
   327     | is_eq _ = false;
   328   val find_eq = find_index is_eq;
   329 in
   330 val rot_eq_tac = 
   331      SUBGOAL (fn (Bi,i) => let val n = find_eq (Logic.strip_assums_hyp Bi) in
   332 		if n>0 then rotate_tac n i else no_tac end)
   333 end;
   334 
   335 use "$ISABELLE_HOME/src/Provers/clasimp.ML";
   336 open Clasimp;
   337 
   338 val FOL_css = (FOL_cs, FOL_ss);
   339