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