src/FOL/IFOL.thy
author haftmann
Thu Nov 23 17:03:27 2017 +0000 (23 months ago)
changeset 67087 733017b19de9
parent 63906 fa799a8e4adc
child 68816 5a53724fe247
permissions -rw-r--r--
generalized more lemmas
     1 (*  Title:      FOL/IFOL.thy
     2     Author:     Lawrence C Paulson and Markus Wenzel
     3 *)
     4 
     5 section \<open>Intuitionistic first-order logic\<close>
     6 
     7 theory IFOL
     8 imports Pure
     9 begin
    10 
    11 ML_file "~~/src/Tools/misc_legacy.ML"
    12 ML_file "~~/src/Provers/splitter.ML"
    13 ML_file "~~/src/Provers/hypsubst.ML"
    14 ML_file "~~/src/Tools/IsaPlanner/zipper.ML"
    15 ML_file "~~/src/Tools/IsaPlanner/isand.ML"
    16 ML_file "~~/src/Tools/IsaPlanner/rw_inst.ML"
    17 ML_file "~~/src/Provers/quantifier1.ML"
    18 ML_file "~~/src/Tools/intuitionistic.ML"
    19 ML_file "~~/src/Tools/project_rule.ML"
    20 ML_file "~~/src/Tools/atomize_elim.ML"
    21 
    22 
    23 subsection \<open>Syntax and axiomatic basis\<close>
    24 
    25 setup Pure_Thy.old_appl_syntax_setup
    26 
    27 class "term"
    28 default_sort "term"
    29 
    30 typedecl o
    31 
    32 judgment
    33   Trueprop :: "o \<Rightarrow> prop"  ("(_)" 5)
    34 
    35 
    36 subsubsection \<open>Equality\<close>
    37 
    38 axiomatization
    39   eq :: "['a, 'a] \<Rightarrow> o"  (infixl "=" 50)
    40 where
    41   refl: "a = a" and
    42   subst: "a = b \<Longrightarrow> P(a) \<Longrightarrow> P(b)"
    43 
    44 
    45 subsubsection \<open>Propositional logic\<close>
    46 
    47 axiomatization
    48   False :: o and
    49   conj :: "[o, o] => o"  (infixr "\<and>" 35) and
    50   disj :: "[o, o] => o"  (infixr "\<or>" 30) and
    51   imp :: "[o, o] => o"  (infixr "\<longrightarrow>" 25)
    52 where
    53   conjI: "\<lbrakk>P;  Q\<rbrakk> \<Longrightarrow> P \<and> Q" and
    54   conjunct1: "P \<and> Q \<Longrightarrow> P" and
    55   conjunct2: "P \<and> Q \<Longrightarrow> Q" and
    56 
    57   disjI1: "P \<Longrightarrow> P \<or> Q" and
    58   disjI2: "Q \<Longrightarrow> P \<or> Q" and
    59   disjE: "\<lbrakk>P \<or> Q; P \<Longrightarrow> R; Q \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R" and
    60 
    61   impI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<longrightarrow> Q" and
    62   mp: "\<lbrakk>P \<longrightarrow> Q; P\<rbrakk> \<Longrightarrow> Q" and
    63 
    64   FalseE: "False \<Longrightarrow> P"
    65 
    66 
    67 subsubsection \<open>Quantifiers\<close>
    68 
    69 axiomatization
    70   All :: "('a \<Rightarrow> o) \<Rightarrow> o"  (binder "\<forall>" 10) and
    71   Ex :: "('a \<Rightarrow> o) \<Rightarrow> o"  (binder "\<exists>" 10)
    72 where
    73   allI: "(\<And>x. P(x)) \<Longrightarrow> (\<forall>x. P(x))" and
    74   spec: "(\<forall>x. P(x)) \<Longrightarrow> P(x)" and
    75   exI: "P(x) \<Longrightarrow> (\<exists>x. P(x))" and
    76   exE: "\<lbrakk>\<exists>x. P(x); \<And>x. P(x) \<Longrightarrow> R\<rbrakk> \<Longrightarrow> R"
    77 
    78 
    79 subsubsection \<open>Definitions\<close>
    80 
    81 definition "True \<equiv> False \<longrightarrow> False"
    82 
    83 definition Not ("\<not> _" [40] 40)
    84   where not_def: "\<not> P \<equiv> P \<longrightarrow> False"
    85 
    86 definition iff  (infixr "\<longleftrightarrow>" 25)
    87   where "P \<longleftrightarrow> Q \<equiv> (P \<longrightarrow> Q) \<and> (Q \<longrightarrow> P)"
    88 
    89 definition Ex1 :: "('a \<Rightarrow> o) \<Rightarrow> o"  (binder "\<exists>!" 10)
    90   where ex1_def: "\<exists>!x. P(x) \<equiv> \<exists>x. P(x) \<and> (\<forall>y. P(y) \<longrightarrow> y = x)"
    91 
    92 axiomatization where  \<comment> \<open>Reflection, admissible\<close>
    93   eq_reflection: "(x = y) \<Longrightarrow> (x \<equiv> y)" and
    94   iff_reflection: "(P \<longleftrightarrow> Q) \<Longrightarrow> (P \<equiv> Q)"
    95 
    96 abbreviation not_equal :: "['a, 'a] \<Rightarrow> o"  (infixl "\<noteq>" 50)
    97   where "x \<noteq> y \<equiv> \<not> (x = y)"
    98 
    99 
   100 subsubsection \<open>Old-style ASCII syntax\<close>
   101 
   102 notation (ASCII)
   103   not_equal  (infixl "~=" 50) and
   104   Not  ("~ _" [40] 40) and
   105   conj  (infixr "&" 35) and
   106   disj  (infixr "|" 30) and
   107   All  (binder "ALL " 10) and
   108   Ex  (binder "EX " 10) and
   109   Ex1  (binder "EX! " 10) and
   110   imp  (infixr "-->" 25) and
   111   iff  (infixr "<->" 25)
   112 
   113 
   114 subsection \<open>Lemmas and proof tools\<close>
   115 
   116 lemmas strip = impI allI
   117 
   118 lemma TrueI: True
   119   unfolding True_def by (rule impI)
   120 
   121 
   122 subsubsection \<open>Sequent-style elimination rules for \<open>\<and>\<close> \<open>\<longrightarrow>\<close> and \<open>\<forall>\<close>\<close>
   123 
   124 lemma conjE:
   125   assumes major: "P \<and> Q"
   126     and r: "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> R"
   127   shows R
   128   apply (rule r)
   129    apply (rule major [THEN conjunct1])
   130   apply (rule major [THEN conjunct2])
   131   done
   132 
   133 lemma impE:
   134   assumes major: "P \<longrightarrow> Q"
   135     and P
   136   and r: "Q \<Longrightarrow> R"
   137   shows R
   138   apply (rule r)
   139   apply (rule major [THEN mp])
   140   apply (rule \<open>P\<close>)
   141   done
   142 
   143 lemma allE:
   144   assumes major: "\<forall>x. P(x)"
   145     and r: "P(x) \<Longrightarrow> R"
   146   shows R
   147   apply (rule r)
   148   apply (rule major [THEN spec])
   149   done
   150 
   151 text \<open>Duplicates the quantifier; for use with @{ML eresolve_tac}.\<close>
   152 lemma all_dupE:
   153   assumes major: "\<forall>x. P(x)"
   154     and r: "\<lbrakk>P(x); \<forall>x. P(x)\<rbrakk> \<Longrightarrow> R"
   155   shows R
   156   apply (rule r)
   157    apply (rule major [THEN spec])
   158   apply (rule major)
   159   done
   160 
   161 
   162 subsubsection \<open>Negation rules, which translate between \<open>\<not> P\<close> and \<open>P \<longrightarrow> False\<close>\<close>
   163 
   164 lemma notI: "(P \<Longrightarrow> False) \<Longrightarrow> \<not> P"
   165   unfolding not_def by (erule impI)
   166 
   167 lemma notE: "\<lbrakk>\<not> P; P\<rbrakk> \<Longrightarrow> R"
   168   unfolding not_def by (erule mp [THEN FalseE])
   169 
   170 lemma rev_notE: "\<lbrakk>P; \<not> P\<rbrakk> \<Longrightarrow> R"
   171   by (erule notE)
   172 
   173 text \<open>This is useful with the special implication rules for each kind of \<open>P\<close>.\<close>
   174 lemma not_to_imp:
   175   assumes "\<not> P"
   176     and r: "P \<longrightarrow> False \<Longrightarrow> Q"
   177   shows Q
   178   apply (rule r)
   179   apply (rule impI)
   180   apply (erule notE [OF \<open>\<not> P\<close>])
   181   done
   182 
   183 text \<open>
   184   For substitution into an assumption \<open>P\<close>, reduce \<open>Q\<close> to \<open>P \<longrightarrow> Q\<close>, substitute into this implication, then apply \<open>impI\<close> to
   185   move \<open>P\<close> back into the assumptions.
   186 \<close>
   187 lemma rev_mp: "\<lbrakk>P; P \<longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
   188   by (erule mp)
   189 
   190 text \<open>Contrapositive of an inference rule.\<close>
   191 lemma contrapos:
   192   assumes major: "\<not> Q"
   193     and minor: "P \<Longrightarrow> Q"
   194   shows "\<not> P"
   195   apply (rule major [THEN notE, THEN notI])
   196   apply (erule minor)
   197   done
   198 
   199 
   200 subsubsection \<open>Modus Ponens Tactics\<close>
   201 
   202 text \<open>
   203   Finds \<open>P \<longrightarrow> Q\<close> and P in the assumptions, replaces implication by
   204   \<open>Q\<close>.
   205 \<close>
   206 ML \<open>
   207   fun mp_tac ctxt i =
   208     eresolve_tac ctxt @{thms notE impE} i THEN assume_tac ctxt i;
   209   fun eq_mp_tac ctxt i =
   210     eresolve_tac ctxt @{thms notE impE} i THEN eq_assume_tac i;
   211 \<close>
   212 
   213 
   214 subsection \<open>If-and-only-if\<close>
   215 
   216 lemma iffI: "\<lbrakk>P \<Longrightarrow> Q; Q \<Longrightarrow> P\<rbrakk> \<Longrightarrow> P \<longleftrightarrow> Q"
   217   apply (unfold iff_def)
   218   apply (rule conjI)
   219    apply (erule impI)
   220   apply (erule impI)
   221   done
   222 
   223 lemma iffE:
   224   assumes major: "P \<longleftrightarrow> Q"
   225     and r: "P \<longrightarrow> Q \<Longrightarrow> Q \<longrightarrow> P \<Longrightarrow> R"
   226   shows R
   227   apply (insert major, unfold iff_def)
   228   apply (erule conjE)
   229   apply (erule r)
   230   apply assumption
   231   done
   232 
   233 
   234 subsubsection \<open>Destruct rules for \<open>\<longleftrightarrow>\<close> similar to Modus Ponens\<close>
   235 
   236 lemma iffD1: "\<lbrakk>P \<longleftrightarrow> Q; P\<rbrakk> \<Longrightarrow> Q"
   237   apply (unfold iff_def)
   238   apply (erule conjunct1 [THEN mp])
   239   apply assumption
   240   done
   241 
   242 lemma iffD2: "\<lbrakk>P \<longleftrightarrow> Q; Q\<rbrakk> \<Longrightarrow> P"
   243   apply (unfold iff_def)
   244   apply (erule conjunct2 [THEN mp])
   245   apply assumption
   246   done
   247 
   248 lemma rev_iffD1: "\<lbrakk>P; P \<longleftrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
   249   apply (erule iffD1)
   250   apply assumption
   251   done
   252 
   253 lemma rev_iffD2: "\<lbrakk>Q; P \<longleftrightarrow> Q\<rbrakk> \<Longrightarrow> P"
   254   apply (erule iffD2)
   255   apply assumption
   256   done
   257 
   258 lemma iff_refl: "P \<longleftrightarrow> P"
   259   by (rule iffI)
   260 
   261 lemma iff_sym: "Q \<longleftrightarrow> P \<Longrightarrow> P \<longleftrightarrow> Q"
   262   apply (erule iffE)
   263   apply (rule iffI)
   264   apply (assumption | erule mp)+
   265   done
   266 
   267 lemma iff_trans: "\<lbrakk>P \<longleftrightarrow> Q; Q \<longleftrightarrow> R\<rbrakk> \<Longrightarrow> P \<longleftrightarrow> R"
   268   apply (rule iffI)
   269   apply (assumption | erule iffE | erule (1) notE impE)+
   270   done
   271 
   272 
   273 subsection \<open>Unique existence\<close>
   274 
   275 text \<open>
   276   NOTE THAT the following 2 quantifications:
   277 
   278     \<^item> \<open>\<exists>!x\<close> such that [\<open>\<exists>!y\<close> such that P(x,y)]   (sequential)
   279     \<^item> \<open>\<exists>!x,y\<close> such that P(x,y)                   (simultaneous)
   280 
   281   do NOT mean the same thing. The parser treats \<open>\<exists>!x y.P(x,y)\<close> as sequential.
   282 \<close>
   283 
   284 lemma ex1I: "P(a) \<Longrightarrow> (\<And>x. P(x) \<Longrightarrow> x = a) \<Longrightarrow> \<exists>!x. P(x)"
   285   apply (unfold ex1_def)
   286   apply (assumption | rule exI conjI allI impI)+
   287   done
   288 
   289 text \<open>Sometimes easier to use: the premises have no shared variables. Safe!\<close>
   290 lemma ex_ex1I: "\<exists>x. P(x) \<Longrightarrow> (\<And>x y. \<lbrakk>P(x); P(y)\<rbrakk> \<Longrightarrow> x = y) \<Longrightarrow> \<exists>!x. P(x)"
   291   apply (erule exE)
   292   apply (rule ex1I)
   293    apply assumption
   294   apply assumption
   295   done
   296 
   297 lemma ex1E: "\<exists>! x. P(x) \<Longrightarrow> (\<And>x. \<lbrakk>P(x); \<forall>y. P(y) \<longrightarrow> y = x\<rbrakk> \<Longrightarrow> R) \<Longrightarrow> R"
   298   apply (unfold ex1_def)
   299   apply (assumption | erule exE conjE)+
   300   done
   301 
   302 
   303 subsubsection \<open>\<open>\<longleftrightarrow>\<close> congruence rules for simplification\<close>
   304 
   305 text \<open>Use \<open>iffE\<close> on a premise. For \<open>conj_cong\<close>, \<open>imp_cong\<close>, \<open>all_cong\<close>, \<open>ex_cong\<close>.\<close>
   306 ML \<open>
   307   fun iff_tac ctxt prems i =
   308     resolve_tac ctxt (prems RL @{thms iffE}) i THEN
   309     REPEAT1 (eresolve_tac ctxt @{thms asm_rl mp} i);
   310 \<close>
   311 
   312 method_setup iff =
   313   \<open>Attrib.thms >>
   314     (fn prems => fn ctxt => SIMPLE_METHOD' (iff_tac ctxt prems))\<close>
   315 
   316 lemma conj_cong:
   317   assumes "P \<longleftrightarrow> P'"
   318     and "P' \<Longrightarrow> Q \<longleftrightarrow> Q'"
   319   shows "(P \<and> Q) \<longleftrightarrow> (P' \<and> Q')"
   320   apply (insert assms)
   321   apply (assumption | rule iffI conjI | erule iffE conjE mp | iff assms)+
   322   done
   323 
   324 text \<open>Reversed congruence rule!  Used in ZF/Order.\<close>
   325 lemma conj_cong2:
   326   assumes "P \<longleftrightarrow> P'"
   327     and "P' \<Longrightarrow> Q \<longleftrightarrow> Q'"
   328   shows "(Q \<and> P) \<longleftrightarrow> (Q' \<and> P')"
   329   apply (insert assms)
   330   apply (assumption | rule iffI conjI | erule iffE conjE mp | iff assms)+
   331   done
   332 
   333 lemma disj_cong:
   334   assumes "P \<longleftrightarrow> P'" and "Q \<longleftrightarrow> Q'"
   335   shows "(P \<or> Q) \<longleftrightarrow> (P' \<or> Q')"
   336   apply (insert assms)
   337   apply (erule iffE disjE disjI1 disjI2 |
   338     assumption | rule iffI | erule (1) notE impE)+
   339   done
   340 
   341 lemma imp_cong:
   342   assumes "P \<longleftrightarrow> P'"
   343     and "P' \<Longrightarrow> Q \<longleftrightarrow> Q'"
   344   shows "(P \<longrightarrow> Q) \<longleftrightarrow> (P' \<longrightarrow> Q')"
   345   apply (insert assms)
   346   apply (assumption | rule iffI impI | erule iffE | erule (1) notE impE | iff assms)+
   347   done
   348 
   349 lemma iff_cong: "\<lbrakk>P \<longleftrightarrow> P'; Q \<longleftrightarrow> Q'\<rbrakk> \<Longrightarrow> (P \<longleftrightarrow> Q) \<longleftrightarrow> (P' \<longleftrightarrow> Q')"
   350   apply (erule iffE | assumption | rule iffI | erule (1) notE impE)+
   351   done
   352 
   353 lemma not_cong: "P \<longleftrightarrow> P' \<Longrightarrow> \<not> P \<longleftrightarrow> \<not> P'"
   354   apply (assumption | rule iffI notI | erule (1) notE impE | erule iffE notE)+
   355   done
   356 
   357 lemma all_cong:
   358   assumes "\<And>x. P(x) \<longleftrightarrow> Q(x)"
   359   shows "(\<forall>x. P(x)) \<longleftrightarrow> (\<forall>x. Q(x))"
   360   apply (assumption | rule iffI allI | erule (1) notE impE | erule allE | iff assms)+
   361   done
   362 
   363 lemma ex_cong:
   364   assumes "\<And>x. P(x) \<longleftrightarrow> Q(x)"
   365   shows "(\<exists>x. P(x)) \<longleftrightarrow> (\<exists>x. Q(x))"
   366   apply (erule exE | assumption | rule iffI exI | erule (1) notE impE | iff assms)+
   367   done
   368 
   369 lemma ex1_cong:
   370   assumes "\<And>x. P(x) \<longleftrightarrow> Q(x)"
   371   shows "(\<exists>!x. P(x)) \<longleftrightarrow> (\<exists>!x. Q(x))"
   372   apply (erule ex1E spec [THEN mp] | assumption | rule iffI ex1I | erule (1) notE impE | iff assms)+
   373   done
   374 
   375 
   376 subsection \<open>Equality rules\<close>
   377 
   378 lemma sym: "a = b \<Longrightarrow> b = a"
   379   apply (erule subst)
   380   apply (rule refl)
   381   done
   382 
   383 lemma trans: "\<lbrakk>a = b; b = c\<rbrakk> \<Longrightarrow> a = c"
   384   apply (erule subst, assumption)
   385   done
   386 
   387 lemma not_sym: "b \<noteq> a \<Longrightarrow> a \<noteq> b"
   388   apply (erule contrapos)
   389   apply (erule sym)
   390   done
   391 
   392 text \<open>
   393   Two theorems for rewriting only one instance of a definition:
   394   the first for definitions of formulae and the second for terms.
   395 \<close>
   396 
   397 lemma def_imp_iff: "(A \<equiv> B) \<Longrightarrow> A \<longleftrightarrow> B"
   398   apply unfold
   399   apply (rule iff_refl)
   400   done
   401 
   402 lemma meta_eq_to_obj_eq: "(A \<equiv> B) \<Longrightarrow> A = B"
   403   apply unfold
   404   apply (rule refl)
   405   done
   406 
   407 lemma meta_eq_to_iff: "x \<equiv> y \<Longrightarrow> x \<longleftrightarrow> y"
   408   by unfold (rule iff_refl)
   409 
   410 text \<open>Substitution.\<close>
   411 lemma ssubst: "\<lbrakk>b = a; P(a)\<rbrakk> \<Longrightarrow> P(b)"
   412   apply (drule sym)
   413   apply (erule (1) subst)
   414   done
   415 
   416 text \<open>A special case of \<open>ex1E\<close> that would otherwise need quantifier
   417   expansion.\<close>
   418 lemma ex1_equalsE: "\<lbrakk>\<exists>!x. P(x); P(a); P(b)\<rbrakk> \<Longrightarrow> a = b"
   419   apply (erule ex1E)
   420   apply (rule trans)
   421    apply (rule_tac [2] sym)
   422    apply (assumption | erule spec [THEN mp])+
   423   done
   424 
   425 
   426 subsubsection \<open>Polymorphic congruence rules\<close>
   427 
   428 lemma subst_context: "a = b \<Longrightarrow> t(a) = t(b)"
   429   apply (erule ssubst)
   430   apply (rule refl)
   431   done
   432 
   433 lemma subst_context2: "\<lbrakk>a = b; c = d\<rbrakk> \<Longrightarrow> t(a,c) = t(b,d)"
   434   apply (erule ssubst)+
   435   apply (rule refl)
   436   done
   437 
   438 lemma subst_context3: "\<lbrakk>a = b; c = d; e = f\<rbrakk> \<Longrightarrow> t(a,c,e) = t(b,d,f)"
   439   apply (erule ssubst)+
   440   apply (rule refl)
   441   done
   442 
   443 text \<open>
   444   Useful with @{ML eresolve_tac} for proving equalities from known
   445   equalities.
   446 
   447         a = b
   448         |   |
   449         c = d
   450 \<close>
   451 lemma box_equals: "\<lbrakk>a = b; a = c; b = d\<rbrakk> \<Longrightarrow> c = d"
   452   apply (rule trans)
   453    apply (rule trans)
   454     apply (rule sym)
   455     apply assumption+
   456   done
   457 
   458 text \<open>Dual of \<open>box_equals\<close>: for proving equalities backwards.\<close>
   459 lemma simp_equals: "\<lbrakk>a = c; b = d; c = d\<rbrakk> \<Longrightarrow> a = b"
   460   apply (rule trans)
   461    apply (rule trans)
   462     apply assumption+
   463   apply (erule sym)
   464   done
   465 
   466 
   467 subsubsection \<open>Congruence rules for predicate letters\<close>
   468 
   469 lemma pred1_cong: "a = a' \<Longrightarrow> P(a) \<longleftrightarrow> P(a')"
   470   apply (rule iffI)
   471    apply (erule (1) subst)
   472   apply (erule (1) ssubst)
   473   done
   474 
   475 lemma pred2_cong: "\<lbrakk>a = a'; b = b'\<rbrakk> \<Longrightarrow> P(a,b) \<longleftrightarrow> P(a',b')"
   476   apply (rule iffI)
   477    apply (erule subst)+
   478    apply assumption
   479   apply (erule ssubst)+
   480   apply assumption
   481   done
   482 
   483 lemma pred3_cong: "\<lbrakk>a = a'; b = b'; c = c'\<rbrakk> \<Longrightarrow> P(a,b,c) \<longleftrightarrow> P(a',b',c')"
   484   apply (rule iffI)
   485    apply (erule subst)+
   486    apply assumption
   487   apply (erule ssubst)+
   488   apply assumption
   489   done
   490 
   491 text \<open>Special case for the equality predicate!\<close>
   492 lemma eq_cong: "\<lbrakk>a = a'; b = b'\<rbrakk> \<Longrightarrow> a = b \<longleftrightarrow> a' = b'"
   493   apply (erule (1) pred2_cong)
   494   done
   495 
   496 
   497 subsection \<open>Simplifications of assumed implications\<close>
   498 
   499 text \<open>
   500   Roy Dyckhoff has proved that \<open>conj_impE\<close>, \<open>disj_impE\<close>, and
   501   \<open>imp_impE\<close> used with @{ML mp_tac} (restricted to atomic formulae) is
   502   COMPLETE for intuitionistic propositional logic.
   503 
   504   See R. Dyckhoff, Contraction-free sequent calculi for intuitionistic logic
   505   (preprint, University of St Andrews, 1991).
   506 \<close>
   507 
   508 lemma conj_impE:
   509   assumes major: "(P \<and> Q) \<longrightarrow> S"
   510     and r: "P \<longrightarrow> (Q \<longrightarrow> S) \<Longrightarrow> R"
   511   shows R
   512   by (assumption | rule conjI impI major [THEN mp] r)+
   513 
   514 lemma disj_impE:
   515   assumes major: "(P \<or> Q) \<longrightarrow> S"
   516     and r: "\<lbrakk>P \<longrightarrow> S; Q \<longrightarrow> S\<rbrakk> \<Longrightarrow> R"
   517   shows R
   518   by (assumption | rule disjI1 disjI2 impI major [THEN mp] r)+
   519 
   520 text \<open>Simplifies the implication.  Classical version is stronger.
   521   Still UNSAFE since Q must be provable -- backtracking needed.\<close>
   522 lemma imp_impE:
   523   assumes major: "(P \<longrightarrow> Q) \<longrightarrow> S"
   524     and r1: "\<lbrakk>P; Q \<longrightarrow> S\<rbrakk> \<Longrightarrow> Q"
   525     and r2: "S \<Longrightarrow> R"
   526   shows R
   527   by (assumption | rule impI major [THEN mp] r1 r2)+
   528 
   529 text \<open>Simplifies the implication.  Classical version is stronger.
   530   Still UNSAFE since ~P must be provable -- backtracking needed.\<close>
   531 lemma not_impE: "\<not> P \<longrightarrow> S \<Longrightarrow> (P \<Longrightarrow> False) \<Longrightarrow> (S \<Longrightarrow> R) \<Longrightarrow> R"
   532   apply (drule mp)
   533    apply (rule notI)
   534    apply assumption
   535   apply assumption
   536   done
   537 
   538 text \<open>Simplifies the implication. UNSAFE.\<close>
   539 lemma iff_impE:
   540   assumes major: "(P \<longleftrightarrow> Q) \<longrightarrow> S"
   541     and r1: "\<lbrakk>P; Q \<longrightarrow> S\<rbrakk> \<Longrightarrow> Q"
   542     and r2: "\<lbrakk>Q; P \<longrightarrow> S\<rbrakk> \<Longrightarrow> P"
   543     and r3: "S \<Longrightarrow> R"
   544   shows R
   545   apply (assumption | rule iffI impI major [THEN mp] r1 r2 r3)+
   546   done
   547 
   548 text \<open>What if \<open>(\<forall>x. \<not> \<not> P(x)) \<longrightarrow> \<not> \<not> (\<forall>x. P(x))\<close> is an assumption?
   549   UNSAFE.\<close>
   550 lemma all_impE:
   551   assumes major: "(\<forall>x. P(x)) \<longrightarrow> S"
   552     and r1: "\<And>x. P(x)"
   553     and r2: "S \<Longrightarrow> R"
   554   shows R
   555   apply (rule allI impI major [THEN mp] r1 r2)+
   556   done
   557 
   558 text \<open>
   559   Unsafe: \<open>\<exists>x. P(x)) \<longrightarrow> S\<close> is equivalent
   560   to \<open>\<forall>x. P(x) \<longrightarrow> S\<close>.\<close>
   561 lemma ex_impE:
   562   assumes major: "(\<exists>x. P(x)) \<longrightarrow> S"
   563     and r: "P(x) \<longrightarrow> S \<Longrightarrow> R"
   564   shows R
   565   apply (assumption | rule exI impI major [THEN mp] r)+
   566   done
   567 
   568 text \<open>Courtesy of Krzysztof Grabczewski.\<close>
   569 lemma disj_imp_disj: "P \<or> Q \<Longrightarrow> (P \<Longrightarrow> R) \<Longrightarrow> (Q \<Longrightarrow> S) \<Longrightarrow> R \<or> S"
   570   apply (erule disjE)
   571   apply (rule disjI1) apply assumption
   572   apply (rule disjI2) apply assumption
   573   done
   574 
   575 ML \<open>
   576 structure Project_Rule = Project_Rule
   577 (
   578   val conjunct1 = @{thm conjunct1}
   579   val conjunct2 = @{thm conjunct2}
   580   val mp = @{thm mp}
   581 )
   582 \<close>
   583 
   584 ML_file "fologic.ML"
   585 
   586 lemma thin_refl: "\<lbrakk>x = x; PROP W\<rbrakk> \<Longrightarrow> PROP W" .
   587 
   588 ML \<open>
   589 structure Hypsubst = Hypsubst
   590 (
   591   val dest_eq = FOLogic.dest_eq
   592   val dest_Trueprop = FOLogic.dest_Trueprop
   593   val dest_imp = FOLogic.dest_imp
   594   val eq_reflection = @{thm eq_reflection}
   595   val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
   596   val imp_intr = @{thm impI}
   597   val rev_mp = @{thm rev_mp}
   598   val subst = @{thm subst}
   599   val sym = @{thm sym}
   600   val thin_refl = @{thm thin_refl}
   601 );
   602 open Hypsubst;
   603 \<close>
   604 
   605 ML_file "intprover.ML"
   606 
   607 
   608 subsection \<open>Intuitionistic Reasoning\<close>
   609 
   610 setup \<open>Intuitionistic.method_setup @{binding iprover}\<close>
   611 
   612 lemma impE':
   613   assumes 1: "P \<longrightarrow> Q"
   614     and 2: "Q \<Longrightarrow> R"
   615     and 3: "P \<longrightarrow> Q \<Longrightarrow> P"
   616   shows R
   617 proof -
   618   from 3 and 1 have P .
   619   with 1 have Q by (rule impE)
   620   with 2 show R .
   621 qed
   622 
   623 lemma allE':
   624   assumes 1: "\<forall>x. P(x)"
   625     and 2: "P(x) \<Longrightarrow> \<forall>x. P(x) \<Longrightarrow> Q"
   626   shows Q
   627 proof -
   628   from 1 have "P(x)" by (rule spec)
   629   from this and 1 show Q by (rule 2)
   630 qed
   631 
   632 lemma notE':
   633   assumes 1: "\<not> P"
   634     and 2: "\<not> P \<Longrightarrow> P"
   635   shows R
   636 proof -
   637   from 2 and 1 have P .
   638   with 1 show R by (rule notE)
   639 qed
   640 
   641 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE
   642   and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
   643   and [Pure.elim 2] = allE notE' impE'
   644   and [Pure.intro] = exI disjI2 disjI1
   645 
   646 setup \<open>
   647   Context_Rules.addSWrapper
   648     (fn ctxt => fn tac => hyp_subst_tac ctxt ORELSE' tac)
   649 \<close>
   650 
   651 
   652 lemma iff_not_sym: "\<not> (Q \<longleftrightarrow> P) \<Longrightarrow> \<not> (P \<longleftrightarrow> Q)"
   653   by iprover
   654 
   655 lemmas [sym] = sym iff_sym not_sym iff_not_sym
   656   and [Pure.elim?] = iffD1 iffD2 impE
   657 
   658 
   659 lemma eq_commute: "a = b \<longleftrightarrow> b = a"
   660   apply (rule iffI)
   661   apply (erule sym)+
   662   done
   663 
   664 
   665 subsection \<open>Atomizing meta-level rules\<close>
   666 
   667 lemma atomize_all [atomize]: "(\<And>x. P(x)) \<equiv> Trueprop (\<forall>x. P(x))"
   668 proof
   669   assume "\<And>x. P(x)"
   670   then show "\<forall>x. P(x)" ..
   671 next
   672   assume "\<forall>x. P(x)"
   673   then show "\<And>x. P(x)" ..
   674 qed
   675 
   676 lemma atomize_imp [atomize]: "(A \<Longrightarrow> B) \<equiv> Trueprop (A \<longrightarrow> B)"
   677 proof
   678   assume "A \<Longrightarrow> B"
   679   then show "A \<longrightarrow> B" ..
   680 next
   681   assume "A \<longrightarrow> B" and A
   682   then show B by (rule mp)
   683 qed
   684 
   685 lemma atomize_eq [atomize]: "(x \<equiv> y) \<equiv> Trueprop (x = y)"
   686 proof
   687   assume "x \<equiv> y"
   688   show "x = y" unfolding \<open>x \<equiv> y\<close> by (rule refl)
   689 next
   690   assume "x = y"
   691   then show "x \<equiv> y" by (rule eq_reflection)
   692 qed
   693 
   694 lemma atomize_iff [atomize]: "(A \<equiv> B) \<equiv> Trueprop (A \<longleftrightarrow> B)"
   695 proof
   696   assume "A \<equiv> B"
   697   show "A \<longleftrightarrow> B" unfolding \<open>A \<equiv> B\<close> by (rule iff_refl)
   698 next
   699   assume "A \<longleftrightarrow> B"
   700   then show "A \<equiv> B" by (rule iff_reflection)
   701 qed
   702 
   703 lemma atomize_conj [atomize]: "(A &&& B) \<equiv> Trueprop (A \<and> B)"
   704 proof
   705   assume conj: "A &&& B"
   706   show "A \<and> B"
   707   proof (rule conjI)
   708     from conj show A by (rule conjunctionD1)
   709     from conj show B by (rule conjunctionD2)
   710   qed
   711 next
   712   assume conj: "A \<and> B"
   713   show "A &&& B"
   714   proof -
   715     from conj show A ..
   716     from conj show B ..
   717   qed
   718 qed
   719 
   720 lemmas [symmetric, rulify] = atomize_all atomize_imp
   721   and [symmetric, defn] = atomize_all atomize_imp atomize_eq atomize_iff
   722 
   723 
   724 subsection \<open>Atomizing elimination rules\<close>
   725 
   726 lemma atomize_exL[atomize_elim]: "(\<And>x. P(x) \<Longrightarrow> Q) \<equiv> ((\<exists>x. P(x)) \<Longrightarrow> Q)"
   727   by rule iprover+
   728 
   729 lemma atomize_conjL[atomize_elim]: "(A \<Longrightarrow> B \<Longrightarrow> C) \<equiv> (A \<and> B \<Longrightarrow> C)"
   730   by rule iprover+
   731 
   732 lemma atomize_disjL[atomize_elim]: "((A \<Longrightarrow> C) \<Longrightarrow> (B \<Longrightarrow> C) \<Longrightarrow> C) \<equiv> ((A \<or> B \<Longrightarrow> C) \<Longrightarrow> C)"
   733   by rule iprover+
   734 
   735 lemma atomize_elimL[atomize_elim]: "(\<And>B. (A \<Longrightarrow> B) \<Longrightarrow> B) \<equiv> Trueprop(A)" ..
   736 
   737 
   738 subsection \<open>Calculational rules\<close>
   739 
   740 lemma forw_subst: "a = b \<Longrightarrow> P(b) \<Longrightarrow> P(a)"
   741   by (rule ssubst)
   742 
   743 lemma back_subst: "P(a) \<Longrightarrow> a = b \<Longrightarrow> P(b)"
   744   by (rule subst)
   745 
   746 text \<open>
   747   Note that this list of rules is in reverse order of priorities.
   748 \<close>
   749 
   750 lemmas basic_trans_rules [trans] =
   751   forw_subst
   752   back_subst
   753   rev_mp
   754   mp
   755   trans
   756 
   757 
   758 subsection \<open>``Let'' declarations\<close>
   759 
   760 nonterminal letbinds and letbind
   761 
   762 definition Let :: "['a::{}, 'a => 'b] \<Rightarrow> ('b::{})"
   763   where "Let(s, f) \<equiv> f(s)"
   764 
   765 syntax
   766   "_bind"       :: "[pttrn, 'a] => letbind"           ("(2_ =/ _)" 10)
   767   ""            :: "letbind => letbinds"              ("_")
   768   "_binds"      :: "[letbind, letbinds] => letbinds"  ("_;/ _")
   769   "_Let"        :: "[letbinds, 'a] => 'a"             ("(let (_)/ in (_))" 10)
   770 
   771 translations
   772   "_Let(_binds(b, bs), e)"  == "_Let(b, _Let(bs, e))"
   773   "let x = a in e"          == "CONST Let(a, \<lambda>x. e)"
   774 
   775 lemma LetI:
   776   assumes "\<And>x. x = t \<Longrightarrow> P(u(x))"
   777   shows "P(let x = t in u(x))"
   778   apply (unfold Let_def)
   779   apply (rule refl [THEN assms])
   780   done
   781 
   782 
   783 subsection \<open>Intuitionistic simplification rules\<close>
   784 
   785 lemma conj_simps:
   786   "P \<and> True \<longleftrightarrow> P"
   787   "True \<and> P \<longleftrightarrow> P"
   788   "P \<and> False \<longleftrightarrow> False"
   789   "False \<and> P \<longleftrightarrow> False"
   790   "P \<and> P \<longleftrightarrow> P"
   791   "P \<and> P \<and> Q \<longleftrightarrow> P \<and> Q"
   792   "P \<and> \<not> P \<longleftrightarrow> False"
   793   "\<not> P \<and> P \<longleftrightarrow> False"
   794   "(P \<and> Q) \<and> R \<longleftrightarrow> P \<and> (Q \<and> R)"
   795   by iprover+
   796 
   797 lemma disj_simps:
   798   "P \<or> True \<longleftrightarrow> True"
   799   "True \<or> P \<longleftrightarrow> True"
   800   "P \<or> False \<longleftrightarrow> P"
   801   "False \<or> P \<longleftrightarrow> P"
   802   "P \<or> P \<longleftrightarrow> P"
   803   "P \<or> P \<or> Q \<longleftrightarrow> P \<or> Q"
   804   "(P \<or> Q) \<or> R \<longleftrightarrow> P \<or> (Q \<or> R)"
   805   by iprover+
   806 
   807 lemma not_simps:
   808   "\<not> (P \<or> Q) \<longleftrightarrow> \<not> P \<and> \<not> Q"
   809   "\<not> False \<longleftrightarrow> True"
   810   "\<not> True \<longleftrightarrow> False"
   811   by iprover+
   812 
   813 lemma imp_simps:
   814   "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
   815   "(P \<longrightarrow> True) \<longleftrightarrow> True"
   816   "(False \<longrightarrow> P) \<longleftrightarrow> True"
   817   "(True \<longrightarrow> P) \<longleftrightarrow> P"
   818   "(P \<longrightarrow> P) \<longleftrightarrow> True"
   819   "(P \<longrightarrow> \<not> P) \<longleftrightarrow> \<not> P"
   820   by iprover+
   821 
   822 lemma iff_simps:
   823   "(True \<longleftrightarrow> P) \<longleftrightarrow> P"
   824   "(P \<longleftrightarrow> True) \<longleftrightarrow> P"
   825   "(P \<longleftrightarrow> P) \<longleftrightarrow> True"
   826   "(False \<longleftrightarrow> P) \<longleftrightarrow> \<not> P"
   827   "(P \<longleftrightarrow> False) \<longleftrightarrow> \<not> P"
   828   by iprover+
   829 
   830 text \<open>The \<open>x = t\<close> versions are needed for the simplification
   831   procedures.\<close>
   832 lemma quant_simps:
   833   "\<And>P. (\<forall>x. P) \<longleftrightarrow> P"
   834   "(\<forall>x. x = t \<longrightarrow> P(x)) \<longleftrightarrow> P(t)"
   835   "(\<forall>x. t = x \<longrightarrow> P(x)) \<longleftrightarrow> P(t)"
   836   "\<And>P. (\<exists>x. P) \<longleftrightarrow> P"
   837   "\<exists>x. x = t"
   838   "\<exists>x. t = x"
   839   "(\<exists>x. x = t \<and> P(x)) \<longleftrightarrow> P(t)"
   840   "(\<exists>x. t = x \<and> P(x)) \<longleftrightarrow> P(t)"
   841   by iprover+
   842 
   843 text \<open>These are NOT supplied by default!\<close>
   844 lemma distrib_simps:
   845   "P \<and> (Q \<or> R) \<longleftrightarrow> P \<and> Q \<or> P \<and> R"
   846   "(Q \<or> R) \<and> P \<longleftrightarrow> Q \<and> P \<or> R \<and> P"
   847   "(P \<or> Q \<longrightarrow> R) \<longleftrightarrow> (P \<longrightarrow> R) \<and> (Q \<longrightarrow> R)"
   848   by iprover+
   849 
   850 
   851 subsubsection \<open>Conversion into rewrite rules\<close>
   852 
   853 lemma P_iff_F: "\<not> P \<Longrightarrow> (P \<longleftrightarrow> False)"
   854   by iprover
   855 lemma iff_reflection_F: "\<not> P \<Longrightarrow> (P \<equiv> False)"
   856   by (rule P_iff_F [THEN iff_reflection])
   857 
   858 lemma P_iff_T: "P \<Longrightarrow> (P \<longleftrightarrow> True)"
   859   by iprover
   860 lemma iff_reflection_T: "P \<Longrightarrow> (P \<equiv> True)"
   861   by (rule P_iff_T [THEN iff_reflection])
   862 
   863 
   864 subsubsection \<open>More rewrite rules\<close>
   865 
   866 lemma conj_commute: "P \<and> Q \<longleftrightarrow> Q \<and> P" by iprover
   867 lemma conj_left_commute: "P \<and> (Q \<and> R) \<longleftrightarrow> Q \<and> (P \<and> R)" by iprover
   868 lemmas conj_comms = conj_commute conj_left_commute
   869 
   870 lemma disj_commute: "P \<or> Q \<longleftrightarrow> Q \<or> P" by iprover
   871 lemma disj_left_commute: "P \<or> (Q \<or> R) \<longleftrightarrow> Q \<or> (P \<or> R)" by iprover
   872 lemmas disj_comms = disj_commute disj_left_commute
   873 
   874 lemma conj_disj_distribL: "P \<and> (Q \<or> R) \<longleftrightarrow> (P \<and> Q \<or> P \<and> R)" by iprover
   875 lemma conj_disj_distribR: "(P \<or> Q) \<and> R \<longleftrightarrow> (P \<and> R \<or> Q \<and> R)" by iprover
   876 
   877 lemma disj_conj_distribL: "P \<or> (Q \<and> R) \<longleftrightarrow> (P \<or> Q) \<and> (P \<or> R)" by iprover
   878 lemma disj_conj_distribR: "(P \<and> Q) \<or> R \<longleftrightarrow> (P \<or> R) \<and> (Q \<or> R)" by iprover
   879 
   880 lemma imp_conj_distrib: "(P \<longrightarrow> (Q \<and> R)) \<longleftrightarrow> (P \<longrightarrow> Q) \<and> (P \<longrightarrow> R)" by iprover
   881 lemma imp_conj: "((P \<and> Q) \<longrightarrow> R) \<longleftrightarrow> (P \<longrightarrow> (Q \<longrightarrow> R))" by iprover
   882 lemma imp_disj: "(P \<or> Q \<longrightarrow> R) \<longleftrightarrow> (P \<longrightarrow> R) \<and> (Q \<longrightarrow> R)" by iprover
   883 
   884 lemma de_Morgan_disj: "(\<not> (P \<or> Q)) \<longleftrightarrow> (\<not> P \<and> \<not> Q)" by iprover
   885 
   886 lemma not_ex: "(\<not> (\<exists>x. P(x))) \<longleftrightarrow> (\<forall>x. \<not> P(x))" by iprover
   887 lemma imp_ex: "((\<exists>x. P(x)) \<longrightarrow> Q) \<longleftrightarrow> (\<forall>x. P(x) \<longrightarrow> Q)" by iprover
   888 
   889 lemma ex_disj_distrib: "(\<exists>x. P(x) \<or> Q(x)) \<longleftrightarrow> ((\<exists>x. P(x)) \<or> (\<exists>x. Q(x)))"
   890   by iprover
   891 
   892 lemma all_conj_distrib: "(\<forall>x. P(x) \<and> Q(x)) \<longleftrightarrow> ((\<forall>x. P(x)) \<and> (\<forall>x. Q(x)))"
   893   by iprover
   894 
   895 end