src/FOL/FOL.thy
author wenzelm
Fri Apr 22 14:30:32 2011 +0200 (2011-04-22)
changeset 42456 13b4b6ba3593
parent 42455 6702c984bf5a
child 42459 38b9f023cc34
permissions -rw-r--r--
proper context for Quantifier1 simprocs (avoid bad ProofContext.init_global from abc655166d61);
tuned signature;
     1 (*  Title:      FOL/FOL.thy
     2     Author:     Lawrence C Paulson and Markus Wenzel
     3 *)
     4 
     5 header {* Classical first-order logic *}
     6 
     7 theory FOL
     8 imports IFOL
     9 uses
    10   "~~/src/Provers/classical.ML"
    11   "~~/src/Provers/blast.ML"
    12   "~~/src/Provers/clasimp.ML"
    13   "~~/src/Tools/induct.ML"
    14   "~~/src/Tools/case_product.ML"
    15   ("cladata.ML")
    16   ("simpdata.ML")
    17 begin
    18 
    19 
    20 subsection {* The classical axiom *}
    21 
    22 axiomatization where
    23   classical: "(~P ==> P) ==> P"
    24 
    25 
    26 subsection {* Lemmas and proof tools *}
    27 
    28 lemma ccontr: "(\<not> P \<Longrightarrow> False) \<Longrightarrow> P"
    29   by (erule FalseE [THEN classical])
    30 
    31 (*** Classical introduction rules for | and EX ***)
    32 
    33 lemma disjCI: "(~Q ==> P) ==> P|Q"
    34   apply (rule classical)
    35   apply (assumption | erule meta_mp | rule disjI1 notI)+
    36   apply (erule notE disjI2)+
    37   done
    38 
    39 (*introduction rule involving only EX*)
    40 lemma ex_classical:
    41   assumes r: "~(EX x. P(x)) ==> P(a)"
    42   shows "EX x. P(x)"
    43   apply (rule classical)
    44   apply (rule exI, erule r)
    45   done
    46 
    47 (*version of above, simplifying ~EX to ALL~ *)
    48 lemma exCI:
    49   assumes r: "ALL x. ~P(x) ==> P(a)"
    50   shows "EX x. P(x)"
    51   apply (rule ex_classical)
    52   apply (rule notI [THEN allI, THEN r])
    53   apply (erule notE)
    54   apply (erule exI)
    55   done
    56 
    57 lemma excluded_middle: "~P | P"
    58   apply (rule disjCI)
    59   apply assumption
    60   done
    61 
    62 lemma case_split [case_names True False]:
    63   assumes r1: "P ==> Q"
    64     and r2: "~P ==> Q"
    65   shows Q
    66   apply (rule excluded_middle [THEN disjE])
    67   apply (erule r2)
    68   apply (erule r1)
    69   done
    70 
    71 ML {*
    72   fun case_tac ctxt a = res_inst_tac ctxt [(("P", 0), a)] @{thm case_split}
    73 *}
    74 
    75 method_setup case_tac = {*
    76   Args.goal_spec -- Scan.lift Args.name_source >>
    77   (fn (quant, s) => fn ctxt => SIMPLE_METHOD'' quant (case_tac ctxt s))
    78 *} "case_tac emulation (dynamic instantiation!)"
    79 
    80 
    81 (*** Special elimination rules *)
    82 
    83 
    84 (*Classical implies (-->) elimination. *)
    85 lemma impCE:
    86   assumes major: "P-->Q"
    87     and r1: "~P ==> R"
    88     and r2: "Q ==> R"
    89   shows R
    90   apply (rule excluded_middle [THEN disjE])
    91    apply (erule r1)
    92   apply (rule r2)
    93   apply (erule major [THEN mp])
    94   done
    95 
    96 (*This version of --> elimination works on Q before P.  It works best for
    97   those cases in which P holds "almost everywhere".  Can't install as
    98   default: would break old proofs.*)
    99 lemma impCE':
   100   assumes major: "P-->Q"
   101     and r1: "Q ==> R"
   102     and r2: "~P ==> R"
   103   shows R
   104   apply (rule excluded_middle [THEN disjE])
   105    apply (erule r2)
   106   apply (rule r1)
   107   apply (erule major [THEN mp])
   108   done
   109 
   110 (*Double negation law*)
   111 lemma notnotD: "~~P ==> P"
   112   apply (rule classical)
   113   apply (erule notE)
   114   apply assumption
   115   done
   116 
   117 lemma contrapos2:  "[| Q; ~ P ==> ~ Q |] ==> P"
   118   apply (rule classical)
   119   apply (drule (1) meta_mp)
   120   apply (erule (1) notE)
   121   done
   122 
   123 (*** Tactics for implication and contradiction ***)
   124 
   125 (*Classical <-> elimination.  Proof substitutes P=Q in
   126     ~P ==> ~Q    and    P ==> Q  *)
   127 lemma iffCE:
   128   assumes major: "P<->Q"
   129     and r1: "[| P; Q |] ==> R"
   130     and r2: "[| ~P; ~Q |] ==> R"
   131   shows R
   132   apply (rule major [unfolded iff_def, THEN conjE])
   133   apply (elim impCE)
   134      apply (erule (1) r2)
   135     apply (erule (1) notE)+
   136   apply (erule (1) r1)
   137   done
   138 
   139 
   140 (*Better for fast_tac: needs no quantifier duplication!*)
   141 lemma alt_ex1E:
   142   assumes major: "EX! x. P(x)"
   143     and r: "!!x. [| P(x);  ALL y y'. P(y) & P(y') --> y=y' |] ==> R"
   144   shows R
   145   using major
   146 proof (rule ex1E)
   147   fix x
   148   assume * : "\<forall>y. P(y) \<longrightarrow> y = x"
   149   assume "P(x)"
   150   then show R
   151   proof (rule r)
   152     { fix y y'
   153       assume "P(y)" and "P(y')"
   154       with * have "x = y" and "x = y'" by - (tactic "IntPr.fast_tac 1")+
   155       then have "y = y'" by (rule subst)
   156     } note r' = this
   157     show "\<forall>y y'. P(y) \<and> P(y') \<longrightarrow> y = y'" by (intro strip, elim conjE) (rule r')
   158   qed
   159 qed
   160 
   161 lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
   162   by (rule classical) iprover
   163 
   164 lemma swap: "~ P ==> (~ R ==> P) ==> R"
   165   by (rule classical) iprover
   166 
   167 
   168 section {* Classical Reasoner *}
   169 
   170 use "cladata.ML"
   171 setup Cla.setup
   172 ML {* Context.>> (Cla.map_cs (K FOL_cs)) *}
   173 
   174 ML {*
   175   structure Blast = Blast
   176   (
   177     val thy = @{theory}
   178     type claset = Cla.claset
   179     val equality_name = @{const_name eq}
   180     val not_name = @{const_name Not}
   181     val notE = @{thm notE}
   182     val ccontr = @{thm ccontr}
   183     val contr_tac = Cla.contr_tac
   184     val dup_intr = Cla.dup_intr
   185     val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   186     val rep_cs = Cla.rep_cs
   187     val cla_modifiers = Cla.cla_modifiers
   188     val cla_meth' = Cla.cla_meth'
   189   );
   190   val blast_tac = Blast.blast_tac;
   191 *}
   192 
   193 setup Blast.setup
   194 
   195 
   196 lemma ex1_functional: "[| EX! z. P(a,z);  P(a,b);  P(a,c) |] ==> b = c"
   197   by blast
   198 
   199 (* Elimination of True from asumptions: *)
   200 lemma True_implies_equals: "(True ==> PROP P) == PROP P"
   201 proof
   202   assume "True \<Longrightarrow> PROP P"
   203   from this and TrueI show "PROP P" .
   204 next
   205   assume "PROP P"
   206   then show "PROP P" .
   207 qed
   208 
   209 lemma uncurry: "P --> Q --> R ==> P & Q --> R"
   210   by blast
   211 
   212 lemma iff_allI: "(!!x. P(x) <-> Q(x)) ==> (ALL x. P(x)) <-> (ALL x. Q(x))"
   213   by blast
   214 
   215 lemma iff_exI: "(!!x. P(x) <-> Q(x)) ==> (EX x. P(x)) <-> (EX x. Q(x))"
   216   by blast
   217 
   218 lemma all_comm: "(ALL x y. P(x,y)) <-> (ALL y x. P(x,y))" by blast
   219 
   220 lemma ex_comm: "(EX x y. P(x,y)) <-> (EX y x. P(x,y))" by blast
   221 
   222 
   223 
   224 (*** Classical simplification rules ***)
   225 
   226 (*Avoids duplication of subgoals after expand_if, when the true and false
   227   cases boil down to the same thing.*)
   228 lemma cases_simp: "(P --> Q) & (~P --> Q) <-> Q" by blast
   229 
   230 
   231 (*** Miniscoping: pushing quantifiers in
   232      We do NOT distribute of ALL over &, or dually that of EX over |
   233      Baaz and Leitsch, On Skolemization and Proof Complexity (1994)
   234      show that this step can increase proof length!
   235 ***)
   236 
   237 (*existential miniscoping*)
   238 lemma int_ex_simps:
   239   "!!P Q. (EX x. P(x) & Q) <-> (EX x. P(x)) & Q"
   240   "!!P Q. (EX x. P & Q(x)) <-> P & (EX x. Q(x))"
   241   "!!P Q. (EX x. P(x) | Q) <-> (EX x. P(x)) | Q"
   242   "!!P Q. (EX x. P | Q(x)) <-> P | (EX x. Q(x))"
   243   by iprover+
   244 
   245 (*classical rules*)
   246 lemma cla_ex_simps:
   247   "!!P Q. (EX x. P(x) --> Q) <-> (ALL x. P(x)) --> Q"
   248   "!!P Q. (EX x. P --> Q(x)) <-> P --> (EX x. Q(x))"
   249   by blast+
   250 
   251 lemmas ex_simps = int_ex_simps cla_ex_simps
   252 
   253 (*universal miniscoping*)
   254 lemma int_all_simps:
   255   "!!P Q. (ALL x. P(x) & Q) <-> (ALL x. P(x)) & Q"
   256   "!!P Q. (ALL x. P & Q(x)) <-> P & (ALL x. Q(x))"
   257   "!!P Q. (ALL x. P(x) --> Q) <-> (EX x. P(x)) --> Q"
   258   "!!P Q. (ALL x. P --> Q(x)) <-> P --> (ALL x. Q(x))"
   259   by iprover+
   260 
   261 (*classical rules*)
   262 lemma cla_all_simps:
   263   "!!P Q. (ALL x. P(x) | Q) <-> (ALL x. P(x)) | Q"
   264   "!!P Q. (ALL x. P | Q(x)) <-> P | (ALL x. Q(x))"
   265   by blast+
   266 
   267 lemmas all_simps = int_all_simps cla_all_simps
   268 
   269 
   270 (*** Named rewrite rules proved for IFOL ***)
   271 
   272 lemma imp_disj1: "(P-->Q) | R <-> (P-->Q | R)" by blast
   273 lemma imp_disj2: "Q | (P-->R) <-> (P-->Q | R)" by blast
   274 
   275 lemma de_Morgan_conj: "(~(P & Q)) <-> (~P | ~Q)" by blast
   276 
   277 lemma not_imp: "~(P --> Q) <-> (P & ~Q)" by blast
   278 lemma not_iff: "~(P <-> Q) <-> (P <-> ~Q)" by blast
   279 
   280 lemma not_all: "(~ (ALL x. P(x))) <-> (EX x.~P(x))" by blast
   281 lemma imp_all: "((ALL x. P(x)) --> Q) <-> (EX x. P(x) --> Q)" by blast
   282 
   283 
   284 lemmas meta_simps =
   285   triv_forall_equality (* prunes params *)
   286   True_implies_equals  (* prune asms `True' *)
   287 
   288 lemmas IFOL_simps =
   289   refl [THEN P_iff_T] conj_simps disj_simps not_simps
   290   imp_simps iff_simps quant_simps
   291 
   292 lemma notFalseI: "~False" by iprover
   293 
   294 lemma cla_simps_misc:
   295   "~(P&Q) <-> ~P | ~Q"
   296   "P | ~P"
   297   "~P | P"
   298   "~ ~ P <-> P"
   299   "(~P --> P) <-> P"
   300   "(~P <-> ~Q) <-> (P<->Q)" by blast+
   301 
   302 lemmas cla_simps =
   303   de_Morgan_conj de_Morgan_disj imp_disj1 imp_disj2
   304   not_imp not_all not_ex cases_simp cla_simps_misc
   305 
   306 
   307 use "simpdata.ML"
   308 
   309 simproc_setup defined_Ex ("EX x. P(x)") = {*
   310   fn _ => fn ss => Quantifier1.rearrange_ex ss o term_of
   311 *}
   312 
   313 simproc_setup defined_All ("ALL x. P(x)") = {*
   314   fn _ => fn ss => Quantifier1.rearrange_all ss o term_of
   315 *}
   316 
   317 ML {*
   318 (*intuitionistic simprules only*)
   319 val IFOL_ss =
   320   FOL_basic_ss
   321   addsimps (@{thms meta_simps} @ @{thms IFOL_simps} @ @{thms int_ex_simps} @ @{thms int_all_simps})
   322   addsimprocs [@{simproc defined_All}, @{simproc defined_Ex}]
   323   addcongs [@{thm imp_cong}];
   324 
   325 (*classical simprules too*)
   326 val FOL_ss = IFOL_ss addsimps (@{thms cla_simps} @ @{thms cla_ex_simps} @ @{thms cla_all_simps});
   327 *}
   328 
   329 setup {* Simplifier.map_simpset (K FOL_ss) *}
   330 
   331 setup "Simplifier.method_setup Splitter.split_modifiers"
   332 setup Splitter.setup
   333 setup clasimp_setup
   334 setup EqSubst.setup
   335 
   336 
   337 subsection {* Other simple lemmas *}
   338 
   339 lemma [simp]: "((P-->R) <-> (Q-->R)) <-> ((P<->Q) | R)"
   340 by blast
   341 
   342 lemma [simp]: "((P-->Q) <-> (P-->R)) <-> (P --> (Q<->R))"
   343 by blast
   344 
   345 lemma not_disj_iff_imp: "~P | Q <-> (P-->Q)"
   346 by blast
   347 
   348 (** Monotonicity of implications **)
   349 
   350 lemma conj_mono: "[| P1-->Q1; P2-->Q2 |] ==> (P1&P2) --> (Q1&Q2)"
   351 by fast (*or (IntPr.fast_tac 1)*)
   352 
   353 lemma disj_mono: "[| P1-->Q1; P2-->Q2 |] ==> (P1|P2) --> (Q1|Q2)"
   354 by fast (*or (IntPr.fast_tac 1)*)
   355 
   356 lemma imp_mono: "[| Q1-->P1; P2-->Q2 |] ==> (P1-->P2)-->(Q1-->Q2)"
   357 by fast (*or (IntPr.fast_tac 1)*)
   358 
   359 lemma imp_refl: "P-->P"
   360 by (rule impI, assumption)
   361 
   362 (*The quantifier monotonicity rules are also intuitionistically valid*)
   363 lemma ex_mono: "(!!x. P(x) --> Q(x)) ==> (EX x. P(x)) --> (EX x. Q(x))"
   364 by blast
   365 
   366 lemma all_mono: "(!!x. P(x) --> Q(x)) ==> (ALL x. P(x)) --> (ALL x. Q(x))"
   367 by blast
   368 
   369 
   370 subsection {* Proof by cases and induction *}
   371 
   372 text {* Proper handling of non-atomic rule statements. *}
   373 
   374 definition "induct_forall(P) == \<forall>x. P(x)"
   375 definition "induct_implies(A, B) == A \<longrightarrow> B"
   376 definition "induct_equal(x, y) == x = y"
   377 definition "induct_conj(A, B) == A \<and> B"
   378 
   379 lemma induct_forall_eq: "(!!x. P(x)) == Trueprop(induct_forall(\<lambda>x. P(x)))"
   380   unfolding atomize_all induct_forall_def .
   381 
   382 lemma induct_implies_eq: "(A ==> B) == Trueprop(induct_implies(A, B))"
   383   unfolding atomize_imp induct_implies_def .
   384 
   385 lemma induct_equal_eq: "(x == y) == Trueprop(induct_equal(x, y))"
   386   unfolding atomize_eq induct_equal_def .
   387 
   388 lemma induct_conj_eq: "(A &&& B) == Trueprop(induct_conj(A, B))"
   389   unfolding atomize_conj induct_conj_def .
   390 
   391 lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
   392 lemmas induct_rulify [symmetric, standard] = induct_atomize
   393 lemmas induct_rulify_fallback =
   394   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
   395 
   396 hide_const induct_forall induct_implies induct_equal induct_conj
   397 
   398 
   399 text {* Method setup. *}
   400 
   401 ML {*
   402   structure Induct = Induct
   403   (
   404     val cases_default = @{thm case_split}
   405     val atomize = @{thms induct_atomize}
   406     val rulify = @{thms induct_rulify}
   407     val rulify_fallback = @{thms induct_rulify_fallback}
   408     val equal_def = @{thm induct_equal_def}
   409     fun dest_def _ = NONE
   410     fun trivial_tac _ = no_tac
   411   );
   412 *}
   413 
   414 setup Induct.setup
   415 declare case_split [cases type: o]
   416 
   417 setup Case_Product.setup
   418 
   419 
   420 hide_const (open) eq
   421 
   422 end