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