src/HOL/HOL.thy
author Andreas Lochbihler
Fri Jul 29 09:49:23 2016 +0200 (2016-07-29)
changeset 63561 fba08009ff3e
parent 62958 b41c1cb5e251
child 63575 b9bd9e61fd63
permissions -rw-r--r--
add lemmas contributed by Peter Gammie
     1 (*  Title:      HOL/HOL.thy
     2     Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
     3 *)
     4 
     5 section \<open>The basis of Higher-Order Logic\<close>
     6 
     7 theory HOL
     8 imports Pure "~~/src/Tools/Code_Generator"
     9 keywords
    10   "try" "solve_direct" "quickcheck" "print_coercions" "print_claset"
    11     "print_induct_rules" :: diag and
    12   "quickcheck_params" :: thy_decl
    13 begin
    14 
    15 ML_file "~~/src/Tools/misc_legacy.ML"
    16 ML_file "~~/src/Tools/try.ML"
    17 ML_file "~~/src/Tools/quickcheck.ML"
    18 ML_file "~~/src/Tools/solve_direct.ML"
    19 ML_file "~~/src/Tools/IsaPlanner/zipper.ML"
    20 ML_file "~~/src/Tools/IsaPlanner/isand.ML"
    21 ML_file "~~/src/Tools/IsaPlanner/rw_inst.ML"
    22 ML_file "~~/src/Provers/hypsubst.ML"
    23 ML_file "~~/src/Provers/splitter.ML"
    24 ML_file "~~/src/Provers/classical.ML"
    25 ML_file "~~/src/Provers/blast.ML"
    26 ML_file "~~/src/Provers/clasimp.ML"
    27 ML_file "~~/src/Tools/eqsubst.ML"
    28 ML_file "~~/src/Provers/quantifier1.ML"
    29 ML_file "~~/src/Tools/atomize_elim.ML"
    30 ML_file "~~/src/Tools/cong_tac.ML"
    31 ML_file "~~/src/Tools/intuitionistic.ML" setup \<open>Intuitionistic.method_setup @{binding iprover}\<close>
    32 ML_file "~~/src/Tools/project_rule.ML"
    33 ML_file "~~/src/Tools/subtyping.ML"
    34 ML_file "~~/src/Tools/case_product.ML"
    35 
    36 
    37 ML \<open>Plugin_Name.declare_setup @{binding extraction}\<close>
    38 
    39 ML \<open>
    40   Plugin_Name.declare_setup @{binding quickcheck_random};
    41   Plugin_Name.declare_setup @{binding quickcheck_exhaustive};
    42   Plugin_Name.declare_setup @{binding quickcheck_bounded_forall};
    43   Plugin_Name.declare_setup @{binding quickcheck_full_exhaustive};
    44   Plugin_Name.declare_setup @{binding quickcheck_narrowing};
    45 \<close>
    46 ML \<open>
    47   Plugin_Name.define_setup @{binding quickcheck}
    48    [@{plugin quickcheck_exhaustive},
    49     @{plugin quickcheck_random},
    50     @{plugin quickcheck_bounded_forall},
    51     @{plugin quickcheck_full_exhaustive},
    52     @{plugin quickcheck_narrowing}]
    53 \<close>
    54 
    55 
    56 subsection \<open>Primitive logic\<close>
    57 
    58 subsubsection \<open>Core syntax\<close>
    59 
    60 setup \<open>Axclass.class_axiomatization (@{binding type}, [])\<close>
    61 default_sort type
    62 setup \<open>Object_Logic.add_base_sort @{sort type}\<close>
    63 
    64 axiomatization where fun_arity: "OFCLASS('a \<Rightarrow> 'b, type_class)"
    65 instance "fun" :: (type, type) type by (rule fun_arity)
    66 
    67 axiomatization where itself_arity: "OFCLASS('a itself, type_class)"
    68 instance itself :: (type) type by (rule itself_arity)
    69 
    70 typedecl bool
    71 
    72 judgment Trueprop :: "bool \<Rightarrow> prop"  ("(_)" 5)
    73 
    74 axiomatization implies :: "[bool, bool] \<Rightarrow> bool"  (infixr "\<longrightarrow>" 25)
    75   and eq :: "['a, 'a] \<Rightarrow> bool"  (infixl "=" 50)
    76   and The :: "('a \<Rightarrow> bool) \<Rightarrow> 'a"
    77 
    78 
    79 subsubsection \<open>Defined connectives and quantifiers\<close>
    80 
    81 definition True :: bool
    82   where "True \<equiv> ((\<lambda>x::bool. x) = (\<lambda>x. x))"
    83 
    84 definition All :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "\<forall>" 10)
    85   where "All P \<equiv> (P = (\<lambda>x. True))"
    86 
    87 definition Ex :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "\<exists>" 10)
    88   where "Ex P \<equiv> \<forall>Q. (\<forall>x. P x \<longrightarrow> Q) \<longrightarrow> Q"
    89 
    90 definition False :: bool
    91   where "False \<equiv> (\<forall>P. P)"
    92 
    93 definition Not :: "bool \<Rightarrow> bool"  ("\<not> _" [40] 40)
    94   where not_def: "\<not> P \<equiv> P \<longrightarrow> False"
    95 
    96 definition conj :: "[bool, bool] \<Rightarrow> bool"  (infixr "\<and>" 35)
    97   where and_def: "P \<and> Q \<equiv> \<forall>R. (P \<longrightarrow> Q \<longrightarrow> R) \<longrightarrow> R"
    98 
    99 definition disj :: "[bool, bool] \<Rightarrow> bool"  (infixr "\<or>" 30)
   100   where or_def: "P \<or> Q \<equiv> \<forall>R. (P \<longrightarrow> R) \<longrightarrow> (Q \<longrightarrow> R) \<longrightarrow> R"
   101 
   102 definition Ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "\<exists>!" 10)
   103   where "Ex1 P \<equiv> \<exists>x. P x \<and> (\<forall>y. P y \<longrightarrow> y = x)"
   104 
   105 
   106 subsubsection \<open>Additional concrete syntax\<close>
   107 
   108 abbreviation Not_Ex :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "\<nexists>" 10)
   109   where "\<nexists>x. P x \<equiv> \<not> (\<exists>x. P x)"
   110 
   111 abbreviation Not_Ex1 :: "('a \<Rightarrow> bool) \<Rightarrow> bool"  (binder "\<nexists>!" 10)
   112   where "\<nexists>!x. P x \<equiv> \<not> (\<exists>!x. P x)"
   113 
   114 abbreviation not_equal :: "['a, 'a] \<Rightarrow> bool"  (infixl "\<noteq>" 50)
   115   where "x \<noteq> y \<equiv> \<not> (x = y)"
   116 
   117 notation (output)
   118   eq  (infix "=" 50) and
   119   not_equal  (infix "\<noteq>" 50)
   120 
   121 notation (ASCII output)
   122   not_equal  (infix "~=" 50)
   123 
   124 notation (ASCII)
   125   Not  ("~ _" [40] 40) and
   126   conj  (infixr "&" 35) and
   127   disj  (infixr "|" 30) and
   128   implies  (infixr "-->" 25) and
   129   not_equal  (infixl "~=" 50)
   130 
   131 abbreviation (iff)
   132   iff :: "[bool, bool] \<Rightarrow> bool"  (infixr "\<longleftrightarrow>" 25)
   133   where "A \<longleftrightarrow> B \<equiv> A = B"
   134 
   135 syntax "_The" :: "[pttrn, bool] \<Rightarrow> 'a"  ("(3THE _./ _)" [0, 10] 10)
   136 translations "THE x. P" \<rightleftharpoons> "CONST The (\<lambda>x. P)"
   137 print_translation \<open>
   138   [(@{const_syntax The}, fn _ => fn [Abs abs] =>
   139       let val (x, t) = Syntax_Trans.atomic_abs_tr' abs
   140       in Syntax.const @{syntax_const "_The"} $ x $ t end)]
   141 \<close>  \<comment> \<open>To avoid eta-contraction of body\<close>
   142 
   143 nonterminal letbinds and letbind
   144 syntax
   145   "_bind"       :: "[pttrn, 'a] \<Rightarrow> letbind"              ("(2_ =/ _)" 10)
   146   ""            :: "letbind \<Rightarrow> letbinds"                 ("_")
   147   "_binds"      :: "[letbind, letbinds] \<Rightarrow> letbinds"     ("_;/ _")
   148   "_Let"        :: "[letbinds, 'a] \<Rightarrow> 'a"                ("(let (_)/ in (_))" [0, 10] 10)
   149 
   150 nonterminal case_syn and cases_syn
   151 syntax
   152   "_case_syntax" :: "['a, cases_syn] \<Rightarrow> 'b"  ("(case _ of/ _)" 10)
   153   "_case1" :: "['a, 'b] \<Rightarrow> case_syn"  ("(2_ \<Rightarrow>/ _)" 10)
   154   "" :: "case_syn \<Rightarrow> cases_syn"  ("_")
   155   "_case2" :: "[case_syn, cases_syn] \<Rightarrow> cases_syn"  ("_/ | _")
   156 syntax (ASCII)
   157   "_case1" :: "['a, 'b] \<Rightarrow> case_syn"  ("(2_ =>/ _)" 10)
   158 
   159 notation (ASCII)
   160   All  (binder "ALL " 10) and
   161   Ex  (binder "EX " 10) and
   162   Ex1  (binder "EX! " 10)
   163 
   164 notation (input)
   165   All  (binder "! " 10) and
   166   Ex  (binder "? " 10) and
   167   Ex1  (binder "?! " 10)
   168 
   169 
   170 subsubsection \<open>Axioms and basic definitions\<close>
   171 
   172 axiomatization where
   173   refl: "t = (t::'a)" and
   174   subst: "s = t \<Longrightarrow> P s \<Longrightarrow> P t" and
   175   ext: "(\<And>x::'a. (f x ::'b) = g x) \<Longrightarrow> (\<lambda>x. f x) = (\<lambda>x. g x)"
   176     \<comment> \<open>Extensionality is built into the meta-logic, and this rule expresses
   177          a related property.  It is an eta-expanded version of the traditional
   178          rule, and similar to the ABS rule of HOL\<close> and
   179 
   180   the_eq_trivial: "(THE x. x = a) = (a::'a)"
   181 
   182 axiomatization where
   183   impI: "(P \<Longrightarrow> Q) \<Longrightarrow> P \<longrightarrow> Q" and
   184   mp: "\<lbrakk>P \<longrightarrow> Q; P\<rbrakk> \<Longrightarrow> Q" and
   185 
   186   iff: "(P \<longrightarrow> Q) \<longrightarrow> (Q \<longrightarrow> P) \<longrightarrow> (P = Q)" and
   187   True_or_False: "(P = True) \<or> (P = False)"
   188 
   189 definition If :: "bool \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a" ("(if (_)/ then (_)/ else (_))" [0, 0, 10] 10)
   190   where "If P x y \<equiv> (THE z::'a. (P = True \<longrightarrow> z = x) \<and> (P = False \<longrightarrow> z = y))"
   191 
   192 definition Let :: "'a \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b"
   193   where "Let s f \<equiv> f s"
   194 
   195 translations
   196   "_Let (_binds b bs) e"  \<rightleftharpoons> "_Let b (_Let bs e)"
   197   "let x = a in e"        \<rightleftharpoons> "CONST Let a (\<lambda>x. e)"
   198 
   199 axiomatization undefined :: 'a
   200 
   201 class default = fixes default :: 'a
   202 
   203 
   204 subsection \<open>Fundamental rules\<close>
   205 
   206 subsubsection \<open>Equality\<close>
   207 
   208 lemma sym: "s = t \<Longrightarrow> t = s"
   209   by (erule subst) (rule refl)
   210 
   211 lemma ssubst: "t = s \<Longrightarrow> P s \<Longrightarrow> P t"
   212   by (drule sym) (erule subst)
   213 
   214 lemma trans: "\<lbrakk>r = s; s = t\<rbrakk> \<Longrightarrow> r = t"
   215   by (erule subst)
   216 
   217 lemma trans_sym [Pure.elim?]: "r = s \<Longrightarrow> t = s \<Longrightarrow> r = t"
   218   by (rule trans [OF _ sym])
   219 
   220 lemma meta_eq_to_obj_eq:
   221   assumes meq: "A \<equiv> B"
   222   shows "A = B"
   223   by (unfold meq) (rule refl)
   224 
   225 text \<open>Useful with \<open>erule\<close> for proving equalities from known equalities.\<close>
   226      (* a = b
   227         |   |
   228         c = d   *)
   229 lemma box_equals: "\<lbrakk>a = b; a = c; b = d\<rbrakk> \<Longrightarrow> c = d"
   230 apply (rule trans)
   231 apply (rule trans)
   232 apply (rule sym)
   233 apply assumption+
   234 done
   235 
   236 text \<open>For calculational reasoning:\<close>
   237 
   238 lemma forw_subst: "a = b \<Longrightarrow> P b \<Longrightarrow> P a"
   239   by (rule ssubst)
   240 
   241 lemma back_subst: "P a \<Longrightarrow> a = b \<Longrightarrow> P b"
   242   by (rule subst)
   243 
   244 
   245 subsubsection \<open>Congruence rules for application\<close>
   246 
   247 text \<open>Similar to \<open>AP_THM\<close> in Gordon's HOL.\<close>
   248 lemma fun_cong: "(f :: 'a \<Rightarrow> 'b) = g \<Longrightarrow> f x = g x"
   249 apply (erule subst)
   250 apply (rule refl)
   251 done
   252 
   253 text \<open>Similar to \<open>AP_TERM\<close> in Gordon's HOL and FOL's \<open>subst_context\<close>.\<close>
   254 lemma arg_cong: "x = y \<Longrightarrow> f x = f y"
   255 apply (erule subst)
   256 apply (rule refl)
   257 done
   258 
   259 lemma arg_cong2: "\<lbrakk>a = b; c = d\<rbrakk> \<Longrightarrow> f a c = f b d"
   260 apply (erule ssubst)+
   261 apply (rule refl)
   262 done
   263 
   264 lemma cong: "\<lbrakk>f = g; (x::'a) = y\<rbrakk> \<Longrightarrow> f x = g y"
   265 apply (erule subst)+
   266 apply (rule refl)
   267 done
   268 
   269 ML \<open>fun cong_tac ctxt = Cong_Tac.cong_tac ctxt @{thm cong}\<close>
   270 
   271 
   272 subsubsection \<open>Equality of booleans -- iff\<close>
   273 
   274 lemma iffI: assumes "P \<Longrightarrow> Q" and "Q \<Longrightarrow> P" shows "P = Q"
   275   by (iprover intro: iff [THEN mp, THEN mp] impI assms)
   276 
   277 lemma iffD2: "\<lbrakk>P = Q; Q\<rbrakk> \<Longrightarrow> P"
   278   by (erule ssubst)
   279 
   280 lemma rev_iffD2: "\<lbrakk>Q; P = Q\<rbrakk> \<Longrightarrow> P"
   281   by (erule iffD2)
   282 
   283 lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
   284   by (drule sym) (rule iffD2)
   285 
   286 lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
   287   by (drule sym) (rule rev_iffD2)
   288 
   289 lemma iffE:
   290   assumes major: "P = Q"
   291     and minor: "\<lbrakk>P \<longrightarrow> Q; Q \<longrightarrow> P\<rbrakk> \<Longrightarrow> R"
   292   shows R
   293   by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
   294 
   295 
   296 subsubsection \<open>True\<close>
   297 
   298 lemma TrueI: "True"
   299   unfolding True_def by (rule refl)
   300 
   301 lemma eqTrueI: "P \<Longrightarrow> P = True"
   302   by (iprover intro: iffI TrueI)
   303 
   304 lemma eqTrueE: "P = True \<Longrightarrow> P"
   305   by (erule iffD2) (rule TrueI)
   306 
   307 
   308 subsubsection \<open>Universal quantifier\<close>
   309 
   310 lemma allI: assumes "\<And>x::'a. P x" shows "\<forall>x. P x"
   311   unfolding All_def by (iprover intro: ext eqTrueI assms)
   312 
   313 lemma spec: "\<forall>x::'a. P x \<Longrightarrow> P x"
   314 apply (unfold All_def)
   315 apply (rule eqTrueE)
   316 apply (erule fun_cong)
   317 done
   318 
   319 lemma allE:
   320   assumes major: "\<forall>x. P x"
   321     and minor: "P x \<Longrightarrow> R"
   322   shows R
   323   by (iprover intro: minor major [THEN spec])
   324 
   325 lemma all_dupE:
   326   assumes major: "\<forall>x. P x"
   327     and minor: "\<lbrakk>P x; \<forall>x. P x\<rbrakk> \<Longrightarrow> R"
   328   shows R
   329   by (iprover intro: minor major major [THEN spec])
   330 
   331 
   332 subsubsection \<open>False\<close>
   333 
   334 text \<open>
   335   Depends upon \<open>spec\<close>; it is impossible to do propositional
   336   logic before quantifiers!
   337 \<close>
   338 
   339 lemma FalseE: "False \<Longrightarrow> P"
   340   apply (unfold False_def)
   341   apply (erule spec)
   342   done
   343 
   344 lemma False_neq_True: "False = True \<Longrightarrow> P"
   345   by (erule eqTrueE [THEN FalseE])
   346 
   347 
   348 subsubsection \<open>Negation\<close>
   349 
   350 lemma notI:
   351   assumes "P \<Longrightarrow> False"
   352   shows "\<not> P"
   353   apply (unfold not_def)
   354   apply (iprover intro: impI assms)
   355   done
   356 
   357 lemma False_not_True: "False \<noteq> True"
   358   apply (rule notI)
   359   apply (erule False_neq_True)
   360   done
   361 
   362 lemma True_not_False: "True \<noteq> False"
   363   apply (rule notI)
   364   apply (drule sym)
   365   apply (erule False_neq_True)
   366   done
   367 
   368 lemma notE: "\<lbrakk>\<not> P; P\<rbrakk> \<Longrightarrow> R"
   369   apply (unfold not_def)
   370   apply (erule mp [THEN FalseE])
   371   apply assumption
   372   done
   373 
   374 lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
   375   by (erule notE [THEN notI]) (erule meta_mp)
   376 
   377 
   378 subsubsection \<open>Implication\<close>
   379 
   380 lemma impE:
   381   assumes "P \<longrightarrow> Q" P "Q \<Longrightarrow> R"
   382   shows R
   383 by (iprover intro: assms mp)
   384 
   385 (* Reduces Q to P \<longrightarrow> Q, allowing substitution in P. *)
   386 lemma rev_mp: "\<lbrakk>P; P \<longrightarrow> Q\<rbrakk> \<Longrightarrow> Q"
   387 by (iprover intro: mp)
   388 
   389 lemma contrapos_nn:
   390   assumes major: "\<not> Q"
   391       and minor: "P \<Longrightarrow> Q"
   392   shows "\<not> P"
   393 by (iprover intro: notI minor major [THEN notE])
   394 
   395 (*not used at all, but we already have the other 3 combinations *)
   396 lemma contrapos_pn:
   397   assumes major: "Q"
   398       and minor: "P \<Longrightarrow> \<not> Q"
   399   shows "\<not> P"
   400 by (iprover intro: notI minor major notE)
   401 
   402 lemma not_sym: "t \<noteq> s \<Longrightarrow> s \<noteq> t"
   403   by (erule contrapos_nn) (erule sym)
   404 
   405 lemma eq_neq_eq_imp_neq: "\<lbrakk>x = a; a \<noteq> b; b = y\<rbrakk> \<Longrightarrow> x \<noteq> y"
   406   by (erule subst, erule ssubst, assumption)
   407 
   408 
   409 subsubsection \<open>Existential quantifier\<close>
   410 
   411 lemma exI: "P x \<Longrightarrow> \<exists>x::'a. P x"
   412 apply (unfold Ex_def)
   413 apply (iprover intro: allI allE impI mp)
   414 done
   415 
   416 lemma exE:
   417   assumes major: "\<exists>x::'a. P x"
   418       and minor: "\<And>x. P x \<Longrightarrow> Q"
   419   shows "Q"
   420 apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
   421 apply (iprover intro: impI [THEN allI] minor)
   422 done
   423 
   424 
   425 subsubsection \<open>Conjunction\<close>
   426 
   427 lemma conjI: "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> P \<and> Q"
   428 apply (unfold and_def)
   429 apply (iprover intro: impI [THEN allI] mp)
   430 done
   431 
   432 lemma conjunct1: "\<lbrakk>P \<and> Q\<rbrakk> \<Longrightarrow> P"
   433 apply (unfold and_def)
   434 apply (iprover intro: impI dest: spec mp)
   435 done
   436 
   437 lemma conjunct2: "\<lbrakk>P \<and> Q\<rbrakk> \<Longrightarrow> Q"
   438 apply (unfold and_def)
   439 apply (iprover intro: impI dest: spec mp)
   440 done
   441 
   442 lemma conjE:
   443   assumes major: "P \<and> Q"
   444       and minor: "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> R"
   445   shows R
   446 apply (rule minor)
   447 apply (rule major [THEN conjunct1])
   448 apply (rule major [THEN conjunct2])
   449 done
   450 
   451 lemma context_conjI:
   452   assumes P "P \<Longrightarrow> Q" shows "P \<and> Q"
   453 by (iprover intro: conjI assms)
   454 
   455 
   456 subsubsection \<open>Disjunction\<close>
   457 
   458 lemma disjI1: "P \<Longrightarrow> P \<or> Q"
   459 apply (unfold or_def)
   460 apply (iprover intro: allI impI mp)
   461 done
   462 
   463 lemma disjI2: "Q \<Longrightarrow> P \<or> Q"
   464 apply (unfold or_def)
   465 apply (iprover intro: allI impI mp)
   466 done
   467 
   468 lemma disjE:
   469   assumes major: "P \<or> Q"
   470       and minorP: "P \<Longrightarrow> R"
   471       and minorQ: "Q \<Longrightarrow> R"
   472   shows R
   473 by (iprover intro: minorP minorQ impI
   474                  major [unfolded or_def, THEN spec, THEN mp, THEN mp])
   475 
   476 
   477 subsubsection \<open>Classical logic\<close>
   478 
   479 lemma classical:
   480   assumes prem: "\<not> P \<Longrightarrow> P"
   481   shows P
   482 apply (rule True_or_False [THEN disjE, THEN eqTrueE])
   483 apply assumption
   484 apply (rule notI [THEN prem, THEN eqTrueI])
   485 apply (erule subst)
   486 apply assumption
   487 done
   488 
   489 lemmas ccontr = FalseE [THEN classical]
   490 
   491 (*notE with premises exchanged; it discharges \<not> R so that it can be used to
   492   make elimination rules*)
   493 lemma rev_notE:
   494   assumes premp: P
   495       and premnot: "\<not> R \<Longrightarrow> \<not> P"
   496   shows R
   497 apply (rule ccontr)
   498 apply (erule notE [OF premnot premp])
   499 done
   500 
   501 (*Double negation law*)
   502 lemma notnotD: "\<not>\<not> P \<Longrightarrow> P"
   503 apply (rule classical)
   504 apply (erule notE)
   505 apply assumption
   506 done
   507 
   508 lemma contrapos_pp:
   509   assumes p1: Q
   510       and p2: "\<not> P \<Longrightarrow> \<not> Q"
   511   shows P
   512 by (iprover intro: classical p1 p2 notE)
   513 
   514 
   515 subsubsection \<open>Unique existence\<close>
   516 
   517 lemma ex1I:
   518   assumes "P a" "\<And>x. P x \<Longrightarrow> x = a"
   519   shows "\<exists>!x. P x"
   520 by (unfold Ex1_def, iprover intro: assms exI conjI allI impI)
   521 
   522 text\<open>Sometimes easier to use: the premises have no shared variables.  Safe!\<close>
   523 lemma ex_ex1I:
   524   assumes ex_prem: "\<exists>x. P x"
   525       and eq: "\<And>x y. \<lbrakk>P x; P y\<rbrakk> \<Longrightarrow> x = y"
   526   shows "\<exists>!x. P x"
   527 by (iprover intro: ex_prem [THEN exE] ex1I eq)
   528 
   529 lemma ex1E:
   530   assumes major: "\<exists>!x. P x"
   531       and minor: "\<And>x. \<lbrakk>P x; \<forall>y. P y \<longrightarrow> y = x\<rbrakk> \<Longrightarrow> R"
   532   shows R
   533 apply (rule major [unfolded Ex1_def, THEN exE])
   534 apply (erule conjE)
   535 apply (iprover intro: minor)
   536 done
   537 
   538 lemma ex1_implies_ex: "\<exists>!x. P x \<Longrightarrow> \<exists>x. P x"
   539 apply (erule ex1E)
   540 apply (rule exI)
   541 apply assumption
   542 done
   543 
   544 
   545 subsubsection \<open>Classical intro rules for disjunction and existential quantifiers\<close>
   546 
   547 lemma disjCI:
   548   assumes "\<not> Q \<Longrightarrow> P" shows "P \<or> Q"
   549 apply (rule classical)
   550 apply (iprover intro: assms disjI1 disjI2 notI elim: notE)
   551 done
   552 
   553 lemma excluded_middle: "\<not> P \<or> P"
   554 by (iprover intro: disjCI)
   555 
   556 text \<open>
   557   case distinction as a natural deduction rule.
   558   Note that @{term "\<not> P"} is the second case, not the first
   559 \<close>
   560 lemma case_split [case_names True False]:
   561   assumes prem1: "P \<Longrightarrow> Q"
   562       and prem2: "\<not> P \<Longrightarrow> Q"
   563   shows Q
   564 apply (rule excluded_middle [THEN disjE])
   565 apply (erule prem2)
   566 apply (erule prem1)
   567 done
   568 
   569 (*Classical implies (\<longrightarrow>) elimination. *)
   570 lemma impCE:
   571   assumes major: "P \<longrightarrow> Q"
   572       and minor: "\<not> P \<Longrightarrow> R" "Q \<Longrightarrow> R"
   573   shows R
   574 apply (rule excluded_middle [of P, THEN disjE])
   575 apply (iprover intro: minor major [THEN mp])+
   576 done
   577 
   578 (*This version of \<longrightarrow> elimination works on Q before P.  It works best for
   579   those cases in which P holds "almost everywhere".  Can't install as
   580   default: would break old proofs.*)
   581 lemma impCE':
   582   assumes major: "P \<longrightarrow> Q"
   583       and minor: "Q \<Longrightarrow> R" "\<not> P \<Longrightarrow> R"
   584   shows R
   585 apply (rule excluded_middle [of P, THEN disjE])
   586 apply (iprover intro: minor major [THEN mp])+
   587 done
   588 
   589 (*Classical <-> elimination. *)
   590 lemma iffCE:
   591   assumes major: "P = Q"
   592       and minor: "\<lbrakk>P; Q\<rbrakk> \<Longrightarrow> R" "\<lbrakk>\<not> P; \<not> Q\<rbrakk> \<Longrightarrow> R"
   593   shows R
   594 apply (rule major [THEN iffE])
   595 apply (iprover intro: minor elim: impCE notE)
   596 done
   597 
   598 lemma exCI:
   599   assumes "\<forall>x. \<not> P x \<Longrightarrow> P a"
   600   shows "\<exists>x. P x"
   601 apply (rule ccontr)
   602 apply (iprover intro: assms exI allI notI notE [of "\<exists>x. P x"])
   603 done
   604 
   605 
   606 subsubsection \<open>Intuitionistic Reasoning\<close>
   607 
   608 lemma impE':
   609   assumes 1: "P \<longrightarrow> Q"
   610     and 2: "Q \<Longrightarrow> R"
   611     and 3: "P \<longrightarrow> Q \<Longrightarrow> P"
   612   shows R
   613 proof -
   614   from 3 and 1 have P .
   615   with 1 have Q by (rule impE)
   616   with 2 show R .
   617 qed
   618 
   619 lemma allE':
   620   assumes 1: "\<forall>x. P x"
   621     and 2: "P x \<Longrightarrow> \<forall>x. P x \<Longrightarrow> Q"
   622   shows Q
   623 proof -
   624   from 1 have "P x" by (rule spec)
   625   from this and 1 show Q by (rule 2)
   626 qed
   627 
   628 lemma notE':
   629   assumes 1: "\<not> P"
   630     and 2: "\<not> P \<Longrightarrow> P"
   631   shows R
   632 proof -
   633   from 2 and 1 have P .
   634   with 1 show R by (rule notE)
   635 qed
   636 
   637 lemma TrueE: "True \<Longrightarrow> P \<Longrightarrow> P" .
   638 lemma notFalseE: "\<not> False \<Longrightarrow> P \<Longrightarrow> P" .
   639 
   640 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
   641   and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
   642   and [Pure.elim 2] = allE notE' impE'
   643   and [Pure.intro] = exI disjI2 disjI1
   644 
   645 lemmas [trans] = trans
   646   and [sym] = sym not_sym
   647   and [Pure.elim?] = iffD1 iffD2 impE
   648 
   649 
   650 subsubsection \<open>Atomizing meta-level connectives\<close>
   651 
   652 axiomatization where
   653   eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
   654 
   655 lemma atomize_all [atomize]: "(\<And>x. P x) \<equiv> Trueprop (\<forall>x. P x)"
   656 proof
   657   assume "\<And>x. P x"
   658   then show "\<forall>x. P x" ..
   659 next
   660   assume "\<forall>x. P x"
   661   then show "\<And>x. P x" by (rule allE)
   662 qed
   663 
   664 lemma atomize_imp [atomize]: "(A \<Longrightarrow> B) \<equiv> Trueprop (A \<longrightarrow> B)"
   665 proof
   666   assume r: "A \<Longrightarrow> B"
   667   show "A \<longrightarrow> B" by (rule impI) (rule r)
   668 next
   669   assume "A \<longrightarrow> B" and A
   670   then show B by (rule mp)
   671 qed
   672 
   673 lemma atomize_not: "(A \<Longrightarrow> False) \<equiv> Trueprop (\<not> A)"
   674 proof
   675   assume r: "A \<Longrightarrow> False"
   676   show "\<not> A" by (rule notI) (rule r)
   677 next
   678   assume "\<not> A" and A
   679   then show False by (rule notE)
   680 qed
   681 
   682 lemma atomize_eq [atomize, code]: "(x \<equiv> y) \<equiv> Trueprop (x = y)"
   683 proof
   684   assume "x \<equiv> y"
   685   show "x = y" by (unfold \<open>x \<equiv> y\<close>) (rule refl)
   686 next
   687   assume "x = y"
   688   then show "x \<equiv> y" by (rule eq_reflection)
   689 qed
   690 
   691 lemma atomize_conj [atomize]: "(A &&& B) \<equiv> Trueprop (A \<and> B)"
   692 proof
   693   assume conj: "A &&& B"
   694   show "A \<and> B"
   695   proof (rule conjI)
   696     from conj show A by (rule conjunctionD1)
   697     from conj show B by (rule conjunctionD2)
   698   qed
   699 next
   700   assume conj: "A \<and> B"
   701   show "A &&& B"
   702   proof -
   703     from conj show A ..
   704     from conj show B ..
   705   qed
   706 qed
   707 
   708 lemmas [symmetric, rulify] = atomize_all atomize_imp
   709   and [symmetric, defn] = atomize_all atomize_imp atomize_eq
   710 
   711 
   712 subsubsection \<open>Atomizing elimination rules\<close>
   713 
   714 lemma atomize_exL[atomize_elim]: "(\<And>x. P x \<Longrightarrow> Q) \<equiv> ((\<exists>x. P x) \<Longrightarrow> Q)"
   715   by rule iprover+
   716 
   717 lemma atomize_conjL[atomize_elim]: "(A \<Longrightarrow> B \<Longrightarrow> C) \<equiv> (A \<and> B \<Longrightarrow> C)"
   718   by rule iprover+
   719 
   720 lemma atomize_disjL[atomize_elim]: "((A \<Longrightarrow> C) \<Longrightarrow> (B \<Longrightarrow> C) \<Longrightarrow> C) \<equiv> ((A \<or> B \<Longrightarrow> C) \<Longrightarrow> C)"
   721   by rule iprover+
   722 
   723 lemma atomize_elimL[atomize_elim]: "(\<And>B. (A \<Longrightarrow> B) \<Longrightarrow> B) \<equiv> Trueprop A" ..
   724 
   725 
   726 subsection \<open>Package setup\<close>
   727 
   728 ML_file "Tools/hologic.ML"
   729 
   730 
   731 subsubsection \<open>Sledgehammer setup\<close>
   732 
   733 text \<open>
   734 Theorems blacklisted to Sledgehammer. These theorems typically produce clauses
   735 that are prolific (match too many equality or membership literals) and relate to
   736 seldom-used facts. Some duplicate other rules.
   737 \<close>
   738 
   739 named_theorems no_atp "theorems that should be filtered out by Sledgehammer"
   740 
   741 
   742 subsubsection \<open>Classical Reasoner setup\<close>
   743 
   744 lemma imp_elim: "P \<longrightarrow> Q \<Longrightarrow> (\<not> R \<Longrightarrow> P) \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
   745   by (rule classical) iprover
   746 
   747 lemma swap: "\<not> P \<Longrightarrow> (\<not> R \<Longrightarrow> P) \<Longrightarrow> R"
   748   by (rule classical) iprover
   749 
   750 lemma thin_refl: "\<lbrakk>x = x; PROP W\<rbrakk> \<Longrightarrow> PROP W" .
   751 
   752 ML \<open>
   753 structure Hypsubst = Hypsubst
   754 (
   755   val dest_eq = HOLogic.dest_eq
   756   val dest_Trueprop = HOLogic.dest_Trueprop
   757   val dest_imp = HOLogic.dest_imp
   758   val eq_reflection = @{thm eq_reflection}
   759   val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
   760   val imp_intr = @{thm impI}
   761   val rev_mp = @{thm rev_mp}
   762   val subst = @{thm subst}
   763   val sym = @{thm sym}
   764   val thin_refl = @{thm thin_refl};
   765 );
   766 open Hypsubst;
   767 
   768 structure Classical = Classical
   769 (
   770   val imp_elim = @{thm imp_elim}
   771   val not_elim = @{thm notE}
   772   val swap = @{thm swap}
   773   val classical = @{thm classical}
   774   val sizef = Drule.size_of_thm
   775   val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
   776 );
   777 
   778 structure Basic_Classical: BASIC_CLASSICAL = Classical;
   779 open Basic_Classical;
   780 \<close>
   781 
   782 setup \<open>
   783   (*prevent substitution on bool*)
   784   let
   785     fun non_bool_eq (@{const_name HOL.eq}, Type (_, [T, _])) = T <> @{typ bool}
   786       | non_bool_eq _ = false;
   787     fun hyp_subst_tac' ctxt =
   788       SUBGOAL (fn (goal, i) =>
   789         if Term.exists_Const non_bool_eq goal
   790         then Hypsubst.hyp_subst_tac ctxt i
   791         else no_tac);
   792   in
   793     Context_Rules.addSWrapper (fn ctxt => fn tac => hyp_subst_tac' ctxt ORELSE' tac)
   794   end
   795 \<close>
   796 
   797 declare iffI [intro!]
   798   and notI [intro!]
   799   and impI [intro!]
   800   and disjCI [intro!]
   801   and conjI [intro!]
   802   and TrueI [intro!]
   803   and refl [intro!]
   804 
   805 declare iffCE [elim!]
   806   and FalseE [elim!]
   807   and impCE [elim!]
   808   and disjE [elim!]
   809   and conjE [elim!]
   810 
   811 declare ex_ex1I [intro!]
   812   and allI [intro!]
   813   and exI [intro]
   814 
   815 declare exE [elim!]
   816   allE [elim]
   817 
   818 ML \<open>val HOL_cs = claset_of @{context}\<close>
   819 
   820 lemma contrapos_np: "\<not> Q \<Longrightarrow> (\<not> P \<Longrightarrow> Q) \<Longrightarrow> P"
   821   apply (erule swap)
   822   apply (erule (1) meta_mp)
   823   done
   824 
   825 declare ex_ex1I [rule del, intro! 2]
   826   and ex1I [intro]
   827 
   828 declare ext [intro]
   829 
   830 lemmas [intro?] = ext
   831   and [elim?] = ex1_implies_ex
   832 
   833 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
   834 lemma alt_ex1E [elim!]:
   835   assumes major: "\<exists>!x. P x"
   836       and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
   837   shows R
   838 apply (rule ex1E [OF major])
   839 apply (rule prem)
   840 apply assumption
   841 apply (rule allI)+
   842 apply (tactic \<open>eresolve_tac @{context} [Classical.dup_elim @{context} @{thm allE}] 1\<close>)
   843 apply iprover
   844 done
   845 
   846 ML \<open>
   847   structure Blast = Blast
   848   (
   849     structure Classical = Classical
   850     val Trueprop_const = dest_Const @{const Trueprop}
   851     val equality_name = @{const_name HOL.eq}
   852     val not_name = @{const_name Not}
   853     val notE = @{thm notE}
   854     val ccontr = @{thm ccontr}
   855     val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   856   );
   857   val blast_tac = Blast.blast_tac;
   858 \<close>
   859 
   860 
   861 subsubsection \<open>THE: definite description operator\<close>
   862 
   863 lemma the_equality [intro]:
   864   assumes "P a"
   865       and "\<And>x. P x \<Longrightarrow> x = a"
   866   shows "(THE x. P x) = a"
   867   by (blast intro: assms trans [OF arg_cong [where f=The] the_eq_trivial])
   868 
   869 lemma theI:
   870   assumes "P a" and "\<And>x. P x \<Longrightarrow> x = a"
   871   shows "P (THE x. P x)"
   872 by (iprover intro: assms the_equality [THEN ssubst])
   873 
   874 lemma theI': "\<exists>!x. P x \<Longrightarrow> P (THE x. P x)"
   875   by (blast intro: theI)
   876 
   877 (*Easier to apply than theI: only one occurrence of P*)
   878 lemma theI2:
   879   assumes "P a" "\<And>x. P x \<Longrightarrow> x = a" "\<And>x. P x \<Longrightarrow> Q x"
   880   shows "Q (THE x. P x)"
   881 by (iprover intro: assms theI)
   882 
   883 lemma the1I2: assumes "\<exists>!x. P x" "\<And>x. P x \<Longrightarrow> Q x" shows "Q (THE x. P x)"
   884 by(iprover intro:assms(2) theI2[where P=P and Q=Q] ex1E[OF assms(1)]
   885            elim:allE impE)
   886 
   887 lemma the1_equality [elim?]: "\<lbrakk>\<exists>!x. P x; P a\<rbrakk> \<Longrightarrow> (THE x. P x) = a"
   888   by blast
   889 
   890 lemma the_sym_eq_trivial: "(THE y. x = y) = x"
   891   by blast
   892 
   893 
   894 subsubsection \<open>Simplifier\<close>
   895 
   896 lemma eta_contract_eq: "(\<lambda>s. f s) = f" ..
   897 
   898 lemma simp_thms:
   899   shows not_not: "(\<not> \<not> P) = P"
   900   and Not_eq_iff: "((\<not> P) = (\<not> Q)) = (P = Q)"
   901   and
   902     "(P \<noteq> Q) = (P = (\<not> Q))"
   903     "(P \<or> \<not>P) = True"    "(\<not> P \<or> P) = True"
   904     "(x = x) = True"
   905   and not_True_eq_False [code]: "(\<not> True) = False"
   906   and not_False_eq_True [code]: "(\<not> False) = True"
   907   and
   908     "(\<not> P) \<noteq> P"  "P \<noteq> (\<not> P)"
   909     "(True = P) = P"
   910   and eq_True: "(P = True) = P"
   911   and "(False = P) = (\<not> P)"
   912   and eq_False: "(P = False) = (\<not> P)"
   913   and
   914     "(True \<longrightarrow> P) = P"  "(False \<longrightarrow> P) = True"
   915     "(P \<longrightarrow> True) = True"  "(P \<longrightarrow> P) = True"
   916     "(P \<longrightarrow> False) = (\<not> P)"  "(P \<longrightarrow> \<not> P) = (\<not> P)"
   917     "(P \<and> True) = P"  "(True \<and> P) = P"
   918     "(P \<and> False) = False"  "(False \<and> P) = False"
   919     "(P \<and> P) = P"  "(P \<and> (P \<and> Q)) = (P \<and> Q)"
   920     "(P \<and> \<not> P) = False"    "(\<not> P \<and> P) = False"
   921     "(P \<or> True) = True"  "(True \<or> P) = True"
   922     "(P \<or> False) = P"  "(False \<or> P) = P"
   923     "(P \<or> P) = P"  "(P \<or> (P \<or> Q)) = (P \<or> Q)" and
   924     "(\<forall>x. P) = P"  "(\<exists>x. P) = P"  "\<exists>x. x = t"  "\<exists>x. t = x"
   925   and
   926     "\<And>P. (\<exists>x. x = t \<and> P x) = P t"
   927     "\<And>P. (\<exists>x. t = x \<and> P x) = P t"
   928     "\<And>P. (\<forall>x. x = t \<longrightarrow> P x) = P t"
   929     "\<And>P. (\<forall>x. t = x \<longrightarrow> P x) = P t"
   930   by (blast, blast, blast, blast, blast, iprover+)
   931 
   932 lemma disj_absorb: "(A \<or> A) = A"
   933   by blast
   934 
   935 lemma disj_left_absorb: "(A \<or> (A \<or> B)) = (A \<or> B)"
   936   by blast
   937 
   938 lemma conj_absorb: "(A \<and> A) = A"
   939   by blast
   940 
   941 lemma conj_left_absorb: "(A \<and> (A \<and> B)) = (A \<and> B)"
   942   by blast
   943 
   944 lemma eq_ac:
   945   shows eq_commute: "a = b \<longleftrightarrow> b = a"
   946     and iff_left_commute: "(P \<longleftrightarrow> (Q \<longleftrightarrow> R)) \<longleftrightarrow> (Q \<longleftrightarrow> (P \<longleftrightarrow> R))"
   947     and iff_assoc: "((P \<longleftrightarrow> Q) \<longleftrightarrow> R) \<longleftrightarrow> (P \<longleftrightarrow> (Q \<longleftrightarrow> R))" by (iprover, blast+)
   948 lemma neq_commute: "a \<noteq> b \<longleftrightarrow> b \<noteq> a" by iprover
   949 
   950 lemma conj_comms:
   951   shows conj_commute: "(P \<and> Q) = (Q \<and> P)"
   952     and conj_left_commute: "(P \<and> (Q \<and> R)) = (Q \<and> (P \<and> R))" by iprover+
   953 lemma conj_assoc: "((P \<and> Q) \<and> R) = (P \<and> (Q \<and> R))" by iprover
   954 
   955 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
   956 
   957 lemma disj_comms:
   958   shows disj_commute: "(P \<or> Q) = (Q \<or> P)"
   959     and disj_left_commute: "(P \<or> (Q \<or> R)) = (Q \<or> (P \<or> R))" by iprover+
   960 lemma disj_assoc: "((P \<or> Q) \<or> R) = (P \<or> (Q \<or> R))" by iprover
   961 
   962 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
   963 
   964 lemma conj_disj_distribL: "(P \<and> (Q \<or> R)) = (P \<and> Q \<or> P \<and> R)" by iprover
   965 lemma conj_disj_distribR: "((P \<or> Q) \<and> R) = (P \<and> R \<or> Q \<and> R)" by iprover
   966 
   967 lemma disj_conj_distribL: "(P \<or> (Q \<and> R)) = ((P \<or> Q) \<and> (P \<or> R))" by iprover
   968 lemma disj_conj_distribR: "((P \<and> Q) \<or> R) = ((P \<or> R) \<and> (Q \<or> R))" by iprover
   969 
   970 lemma imp_conjR: "(P \<longrightarrow> (Q \<and> R)) = ((P \<longrightarrow> Q) \<and> (P \<longrightarrow> R))" by iprover
   971 lemma imp_conjL: "((P \<and> Q) \<longrightarrow> R) = (P \<longrightarrow> (Q \<longrightarrow> R))" by iprover
   972 lemma imp_disjL: "((P \<or> Q) \<longrightarrow> R) = ((P \<longrightarrow> R) \<and> (Q \<longrightarrow> R))" by iprover
   973 
   974 text \<open>These two are specialized, but \<open>imp_disj_not1\<close> is useful in \<open>Auth/Yahalom\<close>.\<close>
   975 lemma imp_disj_not1: "(P \<longrightarrow> Q \<or> R) = (\<not> Q \<longrightarrow> P \<longrightarrow> R)" by blast
   976 lemma imp_disj_not2: "(P \<longrightarrow> Q \<or> R) = (\<not> R \<longrightarrow> P \<longrightarrow> Q)" by blast
   977 
   978 lemma imp_disj1: "((P \<longrightarrow> Q) \<or> R) = (P \<longrightarrow> Q \<or> R)" by blast
   979 lemma imp_disj2: "(Q \<or> (P \<longrightarrow> R)) = (P \<longrightarrow> Q \<or> R)" by blast
   980 
   981 lemma imp_cong: "(P = P') \<Longrightarrow> (P' \<Longrightarrow> (Q = Q')) \<Longrightarrow> ((P \<longrightarrow> Q) = (P' \<longrightarrow> Q'))"
   982   by iprover
   983 
   984 lemma de_Morgan_disj: "(\<not> (P \<or> Q)) = (\<not> P \<and> \<not> Q)" by iprover
   985 lemma de_Morgan_conj: "(\<not> (P \<and> Q)) = (\<not> P \<or> \<not> Q)" by blast
   986 lemma not_imp: "(\<not> (P \<longrightarrow> Q)) = (P \<and> \<not> Q)" by blast
   987 lemma not_iff: "(P \<noteq> Q) = (P = (\<not> Q))" by blast
   988 lemma disj_not1: "(\<not> P \<or> Q) = (P \<longrightarrow> Q)" by blast
   989 lemma disj_not2: "(P \<or> \<not> Q) = (Q \<longrightarrow> P)"  \<comment> \<open>changes orientation :-(\<close>
   990   by blast
   991 lemma imp_conv_disj: "(P \<longrightarrow> Q) = ((\<not> P) \<or> Q)" by blast
   992 lemma disj_imp: "P \<or> Q \<longleftrightarrow> \<not> P \<longrightarrow> Q" by blast
   993 
   994 lemma iff_conv_conj_imp: "(P = Q) = ((P \<longrightarrow> Q) \<and> (Q \<longrightarrow> P))" by iprover
   995 
   996 
   997 lemma cases_simp: "((P \<longrightarrow> Q) \<and> (\<not> P \<longrightarrow> Q)) = Q"
   998   \<comment> \<open>Avoids duplication of subgoals after \<open>if_split\<close>, when the true and false\<close>
   999   \<comment> \<open>cases boil down to the same thing.\<close>
  1000   by blast
  1001 
  1002 lemma not_all: "(\<not> (\<forall>x. P x)) = (\<exists>x. \<not> P x)" by blast
  1003 lemma imp_all: "((\<forall>x. P x) \<longrightarrow> Q) = (\<exists>x. P x \<longrightarrow> Q)" by blast
  1004 lemma not_ex: "(\<not> (\<exists>x. P x)) = (\<forall>x. \<not> P x)" by iprover
  1005 lemma imp_ex: "((\<exists>x. P x) \<longrightarrow> Q) = (\<forall>x. P x \<longrightarrow> Q)" by iprover
  1006 lemma all_not_ex: "(\<forall>x. P x) = (\<not> (\<exists>x. \<not> P x ))" by blast
  1007 
  1008 declare All_def [no_atp]
  1009 
  1010 lemma ex_disj_distrib: "(\<exists>x. P x \<or> Q x) = ((\<exists>x. P x) \<or> (\<exists>x. Q x))" by iprover
  1011 lemma all_conj_distrib: "(\<forall>x. P x \<and> Q x) = ((\<forall>x. P x) \<and> (\<forall>x. Q x))" by iprover
  1012 
  1013 text \<open>
  1014   \medskip The \<open>\<and>\<close> congruence rule: not included by default!
  1015   May slow rewrite proofs down by as much as 50\%\<close>
  1016 
  1017 lemma conj_cong:
  1018     "(P = P') \<Longrightarrow> (P' \<Longrightarrow> (Q = Q')) \<Longrightarrow> ((P \<and> Q) = (P' \<and> Q'))"
  1019   by iprover
  1020 
  1021 lemma rev_conj_cong:
  1022     "(Q = Q') \<Longrightarrow> (Q' \<Longrightarrow> (P = P')) \<Longrightarrow> ((P \<and> Q) = (P' \<and> Q'))"
  1023   by iprover
  1024 
  1025 text \<open>The \<open>|\<close> congruence rule: not included by default!\<close>
  1026 
  1027 lemma disj_cong:
  1028     "(P = P') \<Longrightarrow> (\<not> P' \<Longrightarrow> (Q = Q')) \<Longrightarrow> ((P \<or> Q) = (P' \<or> Q'))"
  1029   by blast
  1030 
  1031 
  1032 text \<open>\medskip if-then-else rules\<close>
  1033 
  1034 lemma if_True [code]: "(if True then x else y) = x"
  1035   by (unfold If_def) blast
  1036 
  1037 lemma if_False [code]: "(if False then x else y) = y"
  1038   by (unfold If_def) blast
  1039 
  1040 lemma if_P: "P \<Longrightarrow> (if P then x else y) = x"
  1041   by (unfold If_def) blast
  1042 
  1043 lemma if_not_P: "\<not> P \<Longrightarrow> (if P then x else y) = y"
  1044   by (unfold If_def) blast
  1045 
  1046 lemma if_split: "P (if Q then x else y) = ((Q \<longrightarrow> P x) \<and> (\<not> Q \<longrightarrow> P y))"
  1047   apply (rule case_split [of Q])
  1048    apply (simplesubst if_P)
  1049     prefer 3 apply (simplesubst if_not_P, blast+)
  1050   done
  1051 
  1052 lemma if_split_asm: "P (if Q then x else y) = (\<not> ((Q \<and> \<not> P x) \<or> (\<not> Q \<and> \<not> P y)))"
  1053 by (simplesubst if_split, blast)
  1054 
  1055 lemmas if_splits [no_atp] = if_split if_split_asm
  1056 
  1057 lemma if_cancel: "(if c then x else x) = x"
  1058 by (simplesubst if_split, blast)
  1059 
  1060 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1061 by (simplesubst if_split, blast)
  1062 
  1063 lemma if_bool_eq_conj: "(if P then Q else R) = ((P \<longrightarrow> Q) \<and> (\<not> P \<longrightarrow> R))"
  1064   \<comment> \<open>This form is useful for expanding \<open>if\<close>s on the RIGHT of the \<open>\<Longrightarrow>\<close> symbol.\<close>
  1065   by (rule if_split)
  1066 
  1067 lemma if_bool_eq_disj: "(if P then Q else R) = ((P \<and> Q) \<or> (\<not> P \<and> R))"
  1068   \<comment> \<open>And this form is useful for expanding \<open>if\<close>s on the LEFT.\<close>
  1069   by (simplesubst if_split) blast
  1070 
  1071 lemma Eq_TrueI: "P \<Longrightarrow> P \<equiv> True" by (unfold atomize_eq) iprover
  1072 lemma Eq_FalseI: "\<not> P \<Longrightarrow> P \<equiv> False" by (unfold atomize_eq) iprover
  1073 
  1074 text \<open>\medskip let rules for simproc\<close>
  1075 
  1076 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow> Let x f \<equiv> Let x g"
  1077   by (unfold Let_def)
  1078 
  1079 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow> Let x f \<equiv> g"
  1080   by (unfold Let_def)
  1081 
  1082 text \<open>
  1083   The following copy of the implication operator is useful for
  1084   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1085   its premise.
  1086 \<close>
  1087 
  1088 definition simp_implies :: "[prop, prop] \<Rightarrow> prop"  (infixr "=simp=>" 1) where
  1089   "simp_implies \<equiv> op \<Longrightarrow>"
  1090 
  1091 lemma simp_impliesI:
  1092   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1093   shows "PROP P =simp=> PROP Q"
  1094   apply (unfold simp_implies_def)
  1095   apply (rule PQ)
  1096   apply assumption
  1097   done
  1098 
  1099 lemma simp_impliesE:
  1100   assumes PQ: "PROP P =simp=> PROP Q"
  1101   and P: "PROP P"
  1102   and QR: "PROP Q \<Longrightarrow> PROP R"
  1103   shows "PROP R"
  1104   apply (rule QR)
  1105   apply (rule PQ [unfolded simp_implies_def])
  1106   apply (rule P)
  1107   done
  1108 
  1109 lemma simp_implies_cong:
  1110   assumes PP' :"PROP P \<equiv> PROP P'"
  1111   and P'QQ': "PROP P' \<Longrightarrow> (PROP Q \<equiv> PROP Q')"
  1112   shows "(PROP P =simp=> PROP Q) \<equiv> (PROP P' =simp=> PROP Q')"
  1113 proof (unfold simp_implies_def, rule equal_intr_rule)
  1114   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1115   and P': "PROP P'"
  1116   from PP' [symmetric] and P' have "PROP P"
  1117     by (rule equal_elim_rule1)
  1118   then have "PROP Q" by (rule PQ)
  1119   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1120 next
  1121   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1122   and P: "PROP P"
  1123   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1124   then have "PROP Q'" by (rule P'Q')
  1125   with P'QQ' [OF P', symmetric] show "PROP Q"
  1126     by (rule equal_elim_rule1)
  1127 qed
  1128 
  1129 lemma uncurry:
  1130   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1131   shows "P \<and> Q \<longrightarrow> R"
  1132   using assms by blast
  1133 
  1134 lemma iff_allI:
  1135   assumes "\<And>x. P x = Q x"
  1136   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1137   using assms by blast
  1138 
  1139 lemma iff_exI:
  1140   assumes "\<And>x. P x = Q x"
  1141   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1142   using assms by blast
  1143 
  1144 lemma all_comm:
  1145   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1146   by blast
  1147 
  1148 lemma ex_comm:
  1149   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1150   by blast
  1151 
  1152 ML_file "Tools/simpdata.ML"
  1153 ML \<open>open Simpdata\<close>
  1154 
  1155 setup \<open>
  1156   map_theory_simpset (put_simpset HOL_basic_ss) #>
  1157   Simplifier.method_setup Splitter.split_modifiers
  1158 \<close>
  1159 
  1160 simproc_setup defined_Ex ("\<exists>x. P x") = \<open>fn _ => Quantifier1.rearrange_ex\<close>
  1161 simproc_setup defined_All ("\<forall>x. P x") = \<open>fn _ => Quantifier1.rearrange_all\<close>
  1162 
  1163 text \<open>Simproc for proving \<open>(y = x) \<equiv> False\<close> from premise \<open>\<not> (x = y)\<close>:\<close>
  1164 
  1165 simproc_setup neq ("x = y") = \<open>fn _ =>
  1166 let
  1167   val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
  1168   fun is_neq eq lhs rhs thm =
  1169     (case Thm.prop_of thm of
  1170       _ $ (Not $ (eq' $ l' $ r')) =>
  1171         Not = HOLogic.Not andalso eq' = eq andalso
  1172         r' aconv lhs andalso l' aconv rhs
  1173     | _ => false);
  1174   fun proc ss ct =
  1175     (case Thm.term_of ct of
  1176       eq $ lhs $ rhs =>
  1177         (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of ss) of
  1178           SOME thm => SOME (thm RS neq_to_EQ_False)
  1179         | NONE => NONE)
  1180      | _ => NONE);
  1181 in proc end;
  1182 \<close>
  1183 
  1184 simproc_setup let_simp ("Let x f") = \<open>
  1185 let
  1186   fun count_loose (Bound i) k = if i >= k then 1 else 0
  1187     | count_loose (s $ t) k = count_loose s k + count_loose t k
  1188     | count_loose (Abs (_, _, t)) k = count_loose  t (k + 1)
  1189     | count_loose _ _ = 0;
  1190   fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
  1191     (case t of
  1192       Abs (_, _, t') => count_loose t' 0 <= 1
  1193     | _ => true);
  1194 in
  1195   fn _ => fn ctxt => fn ct =>
  1196     if is_trivial_let (Thm.term_of ct)
  1197     then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
  1198     else
  1199       let (*Norbert Schirmer's case*)
  1200         val t = Thm.term_of ct;
  1201         val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
  1202       in
  1203         Option.map (hd o Variable.export ctxt' ctxt o single)
  1204           (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
  1205             if is_Free x orelse is_Bound x orelse is_Const x
  1206             then SOME @{thm Let_def}
  1207             else
  1208               let
  1209                 val n = case f of (Abs (x, _, _)) => x | _ => "x";
  1210                 val cx = Thm.cterm_of ctxt x;
  1211                 val xT = Thm.typ_of_cterm cx;
  1212                 val cf = Thm.cterm_of ctxt f;
  1213                 val fx_g = Simplifier.rewrite ctxt (Thm.apply cf cx);
  1214                 val (_ $ _ $ g) = Thm.prop_of fx_g;
  1215                 val g' = abstract_over (x, g);
  1216                 val abs_g'= Abs (n, xT, g');
  1217               in
  1218                 if g aconv g' then
  1219                   let
  1220                     val rl =
  1221                       infer_instantiate ctxt [(("f", 0), cf), (("x", 0), cx)] @{thm Let_unfold};
  1222                   in SOME (rl OF [fx_g]) end
  1223                 else if (Envir.beta_eta_contract f) aconv (Envir.beta_eta_contract abs_g')
  1224                 then NONE (*avoid identity conversion*)
  1225                 else
  1226                   let
  1227                     val g'x = abs_g' $ x;
  1228                     val g_g'x = Thm.symmetric (Thm.beta_conversion false (Thm.cterm_of ctxt g'x));
  1229                     val rl =
  1230                       @{thm Let_folded} |> infer_instantiate ctxt
  1231                         [(("f", 0), Thm.cterm_of ctxt f),
  1232                          (("x", 0), cx),
  1233                          (("g", 0), Thm.cterm_of ctxt abs_g')];
  1234                   in SOME (rl OF [Thm.transitive fx_g g_g'x]) end
  1235               end
  1236           | _ => NONE)
  1237       end
  1238 end\<close>
  1239 
  1240 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
  1241 proof
  1242   assume "True \<Longrightarrow> PROP P"
  1243   from this [OF TrueI] show "PROP P" .
  1244 next
  1245   assume "PROP P"
  1246   then show "PROP P" .
  1247 qed
  1248 
  1249 lemma implies_True_equals: "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True"
  1250   by standard (intro TrueI)
  1251 
  1252 lemma False_implies_equals: "(False \<Longrightarrow> P) \<equiv> Trueprop True"
  1253   by standard simp_all
  1254 
  1255 (* This is not made a simp rule because it does not improve any proofs
  1256    but slows some AFP entries down by 5% (cpu time). May 2015 *)
  1257 lemma implies_False_swap: "NO_MATCH (Trueprop False) P \<Longrightarrow>
  1258   (False \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> False \<Longrightarrow> PROP Q)"
  1259 by(rule swap_prems_eq)
  1260 
  1261 lemma ex_simps:
  1262   "\<And>P Q. (\<exists>x. P x \<and> Q)   = ((\<exists>x. P x) \<and> Q)"
  1263   "\<And>P Q. (\<exists>x. P \<and> Q x)   = (P \<and> (\<exists>x. Q x))"
  1264   "\<And>P Q. (\<exists>x. P x \<or> Q)   = ((\<exists>x. P x) \<or> Q)"
  1265   "\<And>P Q. (\<exists>x. P \<or> Q x)   = (P \<or> (\<exists>x. Q x))"
  1266   "\<And>P Q. (\<exists>x. P x \<longrightarrow> Q) = ((\<forall>x. P x) \<longrightarrow> Q)"
  1267   "\<And>P Q. (\<exists>x. P \<longrightarrow> Q x) = (P \<longrightarrow> (\<exists>x. Q x))"
  1268   \<comment> \<open>Miniscoping: pushing in existential quantifiers.\<close>
  1269   by (iprover | blast)+
  1270 
  1271 lemma all_simps:
  1272   "\<And>P Q. (\<forall>x. P x \<and> Q)   = ((\<forall>x. P x) \<and> Q)"
  1273   "\<And>P Q. (\<forall>x. P \<and> Q x)   = (P \<and> (\<forall>x. Q x))"
  1274   "\<And>P Q. (\<forall>x. P x \<or> Q)   = ((\<forall>x. P x) \<or> Q)"
  1275   "\<And>P Q. (\<forall>x. P \<or> Q x)   = (P \<or> (\<forall>x. Q x))"
  1276   "\<And>P Q. (\<forall>x. P x \<longrightarrow> Q) = ((\<exists>x. P x) \<longrightarrow> Q)"
  1277   "\<And>P Q. (\<forall>x. P \<longrightarrow> Q x) = (P \<longrightarrow> (\<forall>x. Q x))"
  1278   \<comment> \<open>Miniscoping: pushing in universal quantifiers.\<close>
  1279   by (iprover | blast)+
  1280 
  1281 lemmas [simp] =
  1282   triv_forall_equality (*prunes params*)
  1283   True_implies_equals implies_True_equals (*prune True in asms*)
  1284   False_implies_equals (*prune False in asms*)
  1285   if_True
  1286   if_False
  1287   if_cancel
  1288   if_eq_cancel
  1289   imp_disjL
  1290   (*In general it seems wrong to add distributive laws by default: they
  1291     might cause exponential blow-up.  But imp_disjL has been in for a while
  1292     and cannot be removed without affecting existing proofs.  Moreover,
  1293     rewriting by "(P \<or> Q \<longrightarrow> R) = ((P \<longrightarrow> R) \<and> (Q \<longrightarrow> R))" might be justified on the
  1294     grounds that it allows simplification of R in the two cases.*)
  1295   conj_assoc
  1296   disj_assoc
  1297   de_Morgan_conj
  1298   de_Morgan_disj
  1299   imp_disj1
  1300   imp_disj2
  1301   not_imp
  1302   disj_not1
  1303   not_all
  1304   not_ex
  1305   cases_simp
  1306   the_eq_trivial
  1307   the_sym_eq_trivial
  1308   ex_simps
  1309   all_simps
  1310   simp_thms
  1311 
  1312 lemmas [cong] = imp_cong simp_implies_cong
  1313 lemmas [split] = if_split
  1314 
  1315 ML \<open>val HOL_ss = simpset_of @{context}\<close>
  1316 
  1317 text \<open>Simplifies @{term x} assuming @{prop c} and @{term y} assuming @{prop "\<not> c"}\<close>
  1318 lemma if_cong:
  1319   assumes "b = c"
  1320       and "c \<Longrightarrow> x = u"
  1321       and "\<not> c \<Longrightarrow> y = v"
  1322   shows "(if b then x else y) = (if c then u else v)"
  1323   using assms by simp
  1324 
  1325 text \<open>Prevents simplification of x and y:
  1326   faster and allows the execution of functional programs.\<close>
  1327 lemma if_weak_cong [cong]:
  1328   assumes "b = c"
  1329   shows "(if b then x else y) = (if c then x else y)"
  1330   using assms by (rule arg_cong)
  1331 
  1332 text \<open>Prevents simplification of t: much faster\<close>
  1333 lemma let_weak_cong:
  1334   assumes "a = b"
  1335   shows "(let x = a in t x) = (let x = b in t x)"
  1336   using assms by (rule arg_cong)
  1337 
  1338 text \<open>To tidy up the result of a simproc.  Only the RHS will be simplified.\<close>
  1339 lemma eq_cong2:
  1340   assumes "u = u'"
  1341   shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
  1342   using assms by simp
  1343 
  1344 lemma if_distrib:
  1345   "f (if c then x else y) = (if c then f x else f y)"
  1346   by simp
  1347 
  1348 text\<open>As a simplification rule, it replaces all function equalities by
  1349   first-order equalities.\<close>
  1350 lemma fun_eq_iff: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
  1351   by auto
  1352 
  1353 
  1354 subsubsection \<open>Generic cases and induction\<close>
  1355 
  1356 text \<open>Rule projections:\<close>
  1357 ML \<open>
  1358 structure Project_Rule = Project_Rule
  1359 (
  1360   val conjunct1 = @{thm conjunct1}
  1361   val conjunct2 = @{thm conjunct2}
  1362   val mp = @{thm mp}
  1363 );
  1364 \<close>
  1365 
  1366 context
  1367 begin
  1368 
  1369 qualified definition "induct_forall P \<equiv> \<forall>x. P x"
  1370 qualified definition "induct_implies A B \<equiv> A \<longrightarrow> B"
  1371 qualified definition "induct_equal x y \<equiv> x = y"
  1372 qualified definition "induct_conj A B \<equiv> A \<and> B"
  1373 qualified definition "induct_true \<equiv> True"
  1374 qualified definition "induct_false \<equiv> False"
  1375 
  1376 lemma induct_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (induct_forall (\<lambda>x. P x))"
  1377   by (unfold atomize_all induct_forall_def)
  1378 
  1379 lemma induct_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (induct_implies A B)"
  1380   by (unfold atomize_imp induct_implies_def)
  1381 
  1382 lemma induct_equal_eq: "(x \<equiv> y) \<equiv> Trueprop (induct_equal x y)"
  1383   by (unfold atomize_eq induct_equal_def)
  1384 
  1385 lemma induct_conj_eq: "(A &&& B) \<equiv> Trueprop (induct_conj A B)"
  1386   by (unfold atomize_conj induct_conj_def)
  1387 
  1388 lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
  1389 lemmas induct_atomize = induct_atomize' induct_equal_eq
  1390 lemmas induct_rulify' [symmetric] = induct_atomize'
  1391 lemmas induct_rulify [symmetric] = induct_atomize
  1392 lemmas induct_rulify_fallback =
  1393   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
  1394   induct_true_def induct_false_def
  1395 
  1396 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
  1397     induct_conj (induct_forall A) (induct_forall B)"
  1398   by (unfold induct_forall_def induct_conj_def) iprover
  1399 
  1400 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
  1401     induct_conj (induct_implies C A) (induct_implies C B)"
  1402   by (unfold induct_implies_def induct_conj_def) iprover
  1403 
  1404 lemma induct_conj_curry: "(induct_conj A B \<Longrightarrow> PROP C) \<equiv> (A \<Longrightarrow> B \<Longrightarrow> PROP C)"
  1405 proof
  1406   assume r: "induct_conj A B \<Longrightarrow> PROP C"
  1407   assume ab: A B
  1408   show "PROP C" by (rule r) (simp add: induct_conj_def ab)
  1409 next
  1410   assume r: "A \<Longrightarrow> B \<Longrightarrow> PROP C"
  1411   assume ab: "induct_conj A B"
  1412   show "PROP C" by (rule r) (simp_all add: ab [unfolded induct_conj_def])
  1413 qed
  1414 
  1415 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1416 
  1417 lemma induct_trueI: "induct_true"
  1418   by (simp add: induct_true_def)
  1419 
  1420 text \<open>Method setup.\<close>
  1421 
  1422 ML_file "~~/src/Tools/induct.ML"
  1423 ML \<open>
  1424 structure Induct = Induct
  1425 (
  1426   val cases_default = @{thm case_split}
  1427   val atomize = @{thms induct_atomize}
  1428   val rulify = @{thms induct_rulify'}
  1429   val rulify_fallback = @{thms induct_rulify_fallback}
  1430   val equal_def = @{thm induct_equal_def}
  1431   fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
  1432     | dest_def _ = NONE
  1433   fun trivial_tac ctxt = match_tac ctxt @{thms induct_trueI}
  1434 )
  1435 \<close>
  1436 
  1437 ML_file "~~/src/Tools/induction.ML"
  1438 
  1439 declaration \<open>
  1440   fn _ => Induct.map_simpset (fn ss => ss
  1441     addsimprocs
  1442       [Simplifier.make_simproc @{context} "swap_induct_false"
  1443         {lhss = [@{term "induct_false \<Longrightarrow> PROP P \<Longrightarrow> PROP Q"}],
  1444          proc = fn _ => fn _ => fn ct =>
  1445           (case Thm.term_of ct of
  1446             _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
  1447               if P <> Q then SOME Drule.swap_prems_eq else NONE
  1448           | _ => NONE)},
  1449        Simplifier.make_simproc @{context} "induct_equal_conj_curry"
  1450         {lhss = [@{term "induct_conj P Q \<Longrightarrow> PROP R"}],
  1451          proc = fn _ => fn _ => fn ct =>
  1452           (case Thm.term_of ct of
  1453             _ $ (_ $ P) $ _ =>
  1454               let
  1455                 fun is_conj (@{const induct_conj} $ P $ Q) =
  1456                       is_conj P andalso is_conj Q
  1457                   | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
  1458                   | is_conj @{const induct_true} = true
  1459                   | is_conj @{const induct_false} = true
  1460                   | is_conj _ = false
  1461               in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
  1462             | _ => NONE)}]
  1463     |> Simplifier.set_mksimps (fn ctxt =>
  1464         Simpdata.mksimps Simpdata.mksimps_pairs ctxt #>
  1465         map (rewrite_rule ctxt (map Thm.symmetric @{thms induct_rulify_fallback}))))
  1466 \<close>
  1467 
  1468 text \<open>Pre-simplification of induction and cases rules\<close>
  1469 
  1470 lemma [induct_simp]: "(\<And>x. induct_equal x t \<Longrightarrow> PROP P x) \<equiv> PROP P t"
  1471   unfolding induct_equal_def
  1472 proof
  1473   assume r: "\<And>x. x = t \<Longrightarrow> PROP P x"
  1474   show "PROP P t" by (rule r [OF refl])
  1475 next
  1476   fix x
  1477   assume "PROP P t" "x = t"
  1478   then show "PROP P x" by simp
  1479 qed
  1480 
  1481 lemma [induct_simp]: "(\<And>x. induct_equal t x \<Longrightarrow> PROP P x) \<equiv> PROP P t"
  1482   unfolding induct_equal_def
  1483 proof
  1484   assume r: "\<And>x. t = x \<Longrightarrow> PROP P x"
  1485   show "PROP P t" by (rule r [OF refl])
  1486 next
  1487   fix x
  1488   assume "PROP P t" "t = x"
  1489   then show "PROP P x" by simp
  1490 qed
  1491 
  1492 lemma [induct_simp]: "(induct_false \<Longrightarrow> P) \<equiv> Trueprop induct_true"
  1493   unfolding induct_false_def induct_true_def
  1494   by (iprover intro: equal_intr_rule)
  1495 
  1496 lemma [induct_simp]: "(induct_true \<Longrightarrow> PROP P) \<equiv> PROP P"
  1497   unfolding induct_true_def
  1498 proof
  1499   assume "True \<Longrightarrow> PROP P"
  1500   then show "PROP P" using TrueI .
  1501 next
  1502   assume "PROP P"
  1503   then show "PROP P" .
  1504 qed
  1505 
  1506 lemma [induct_simp]: "(PROP P \<Longrightarrow> induct_true) \<equiv> Trueprop induct_true"
  1507   unfolding induct_true_def
  1508   by (iprover intro: equal_intr_rule)
  1509 
  1510 lemma [induct_simp]: "(\<And>x::'a::{}. induct_true) \<equiv> Trueprop induct_true"
  1511   unfolding induct_true_def
  1512   by (iprover intro: equal_intr_rule)
  1513 
  1514 lemma [induct_simp]: "induct_implies induct_true P \<equiv> P"
  1515   by (simp add: induct_implies_def induct_true_def)
  1516 
  1517 lemma [induct_simp]: "x = x \<longleftrightarrow> True"
  1518   by (rule simp_thms)
  1519 
  1520 end
  1521 
  1522 ML_file "~~/src/Tools/induct_tacs.ML"
  1523 
  1524 
  1525 subsubsection \<open>Coherent logic\<close>
  1526 
  1527 ML_file "~~/src/Tools/coherent.ML"
  1528 ML \<open>
  1529 structure Coherent = Coherent
  1530 (
  1531   val atomize_elimL = @{thm atomize_elimL};
  1532   val atomize_exL = @{thm atomize_exL};
  1533   val atomize_conjL = @{thm atomize_conjL};
  1534   val atomize_disjL = @{thm atomize_disjL};
  1535   val operator_names = [@{const_name HOL.disj}, @{const_name HOL.conj}, @{const_name Ex}];
  1536 );
  1537 \<close>
  1538 
  1539 
  1540 subsubsection \<open>Reorienting equalities\<close>
  1541 
  1542 ML \<open>
  1543 signature REORIENT_PROC =
  1544 sig
  1545   val add : (term -> bool) -> theory -> theory
  1546   val proc : morphism -> Proof.context -> cterm -> thm option
  1547 end;
  1548 
  1549 structure Reorient_Proc : REORIENT_PROC =
  1550 struct
  1551   structure Data = Theory_Data
  1552   (
  1553     type T = ((term -> bool) * stamp) list;
  1554     val empty = [];
  1555     val extend = I;
  1556     fun merge data : T = Library.merge (eq_snd op =) data;
  1557   );
  1558   fun add m = Data.map (cons (m, stamp ()));
  1559   fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
  1560 
  1561   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1562   fun proc phi ctxt ct =
  1563     let
  1564       val thy = Proof_Context.theory_of ctxt;
  1565     in
  1566       case Thm.term_of ct of
  1567         (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
  1568       | _ => NONE
  1569     end;
  1570 end;
  1571 \<close>
  1572 
  1573 
  1574 subsection \<open>Other simple lemmas and lemma duplicates\<close>
  1575 
  1576 lemma ex1_eq [iff]: "\<exists>!x. x = t" "\<exists>!x. t = x"
  1577   by blast+
  1578 
  1579 lemma choice_eq: "(\<forall>x. \<exists>!y. P x y) = (\<exists>!f. \<forall>x. P x (f x))"
  1580   apply (rule iffI)
  1581   apply (rule_tac a = "\<lambda>x. THE y. P x y" in ex1I)
  1582   apply (fast dest!: theI')
  1583   apply (fast intro: the1_equality [symmetric])
  1584   apply (erule ex1E)
  1585   apply (rule allI)
  1586   apply (rule ex1I)
  1587   apply (erule spec)
  1588   apply (erule_tac x = "\<lambda>z. if z = x then y else f z" in allE)
  1589   apply (erule impE)
  1590   apply (rule allI)
  1591   apply (case_tac "xa = x")
  1592   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1593   done
  1594 
  1595 lemmas eq_sym_conv = eq_commute
  1596 
  1597 lemma nnf_simps:
  1598   "(\<not>(P \<and> Q)) = (\<not> P \<or> \<not> Q)" "(\<not> (P \<or> Q)) = (\<not> P \<and> \<not>Q)" "(P \<longrightarrow> Q) = (\<not>P \<or> Q)"
  1599   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))"
  1600   "(\<not> \<not>(P)) = P"
  1601 by blast+
  1602 
  1603 subsection \<open>Basic ML bindings\<close>
  1604 
  1605 ML \<open>
  1606 val FalseE = @{thm FalseE}
  1607 val Let_def = @{thm Let_def}
  1608 val TrueI = @{thm TrueI}
  1609 val allE = @{thm allE}
  1610 val allI = @{thm allI}
  1611 val all_dupE = @{thm all_dupE}
  1612 val arg_cong = @{thm arg_cong}
  1613 val box_equals = @{thm box_equals}
  1614 val ccontr = @{thm ccontr}
  1615 val classical = @{thm classical}
  1616 val conjE = @{thm conjE}
  1617 val conjI = @{thm conjI}
  1618 val conjunct1 = @{thm conjunct1}
  1619 val conjunct2 = @{thm conjunct2}
  1620 val disjCI = @{thm disjCI}
  1621 val disjE = @{thm disjE}
  1622 val disjI1 = @{thm disjI1}
  1623 val disjI2 = @{thm disjI2}
  1624 val eq_reflection = @{thm eq_reflection}
  1625 val ex1E = @{thm ex1E}
  1626 val ex1I = @{thm ex1I}
  1627 val ex1_implies_ex = @{thm ex1_implies_ex}
  1628 val exE = @{thm exE}
  1629 val exI = @{thm exI}
  1630 val excluded_middle = @{thm excluded_middle}
  1631 val ext = @{thm ext}
  1632 val fun_cong = @{thm fun_cong}
  1633 val iffD1 = @{thm iffD1}
  1634 val iffD2 = @{thm iffD2}
  1635 val iffI = @{thm iffI}
  1636 val impE = @{thm impE}
  1637 val impI = @{thm impI}
  1638 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1639 val mp = @{thm mp}
  1640 val notE = @{thm notE}
  1641 val notI = @{thm notI}
  1642 val not_all = @{thm not_all}
  1643 val not_ex = @{thm not_ex}
  1644 val not_iff = @{thm not_iff}
  1645 val not_not = @{thm not_not}
  1646 val not_sym = @{thm not_sym}
  1647 val refl = @{thm refl}
  1648 val rev_mp = @{thm rev_mp}
  1649 val spec = @{thm spec}
  1650 val ssubst = @{thm ssubst}
  1651 val subst = @{thm subst}
  1652 val sym = @{thm sym}
  1653 val trans = @{thm trans}
  1654 \<close>
  1655 
  1656 ML_file "Tools/cnf.ML"
  1657 
  1658 
  1659 section \<open>\<open>NO_MATCH\<close> simproc\<close>
  1660 
  1661 text \<open>
  1662  The simplification procedure can be used to avoid simplification of terms of a certain form
  1663 \<close>
  1664 
  1665 definition NO_MATCH :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where "NO_MATCH pat val \<equiv> True"
  1666 
  1667 lemma NO_MATCH_cong[cong]: "NO_MATCH pat val = NO_MATCH pat val" by (rule refl)
  1668 
  1669 declare [[coercion_args NO_MATCH - -]]
  1670 
  1671 simproc_setup NO_MATCH ("NO_MATCH pat val") = \<open>fn _ => fn ctxt => fn ct =>
  1672   let
  1673     val thy = Proof_Context.theory_of ctxt
  1674     val dest_binop = Term.dest_comb #> apfst (Term.dest_comb #> snd)
  1675     val m = Pattern.matches thy (dest_binop (Thm.term_of ct))
  1676   in if m then NONE else SOME @{thm NO_MATCH_def} end
  1677 \<close>
  1678 
  1679 text \<open>
  1680   This setup ensures that a rewrite rule of the form @{term "NO_MATCH pat val \<Longrightarrow> t"}
  1681   is only applied, if the pattern @{term pat} does not match the value @{term val}.
  1682 \<close>
  1683 
  1684 
  1685 text\<open>Tagging a premise of a simp rule with ASSUMPTION forces the simplifier
  1686 not to simplify the argument and to solve it by an assumption.\<close>
  1687 
  1688 definition ASSUMPTION :: "bool \<Rightarrow> bool" where
  1689 "ASSUMPTION A \<equiv> A"
  1690 
  1691 lemma ASSUMPTION_cong[cong]: "ASSUMPTION A = ASSUMPTION A"
  1692 by (rule refl)
  1693 
  1694 lemma ASSUMPTION_I: "A \<Longrightarrow> ASSUMPTION A"
  1695 by(simp add: ASSUMPTION_def)
  1696 
  1697 lemma ASSUMPTION_D: "ASSUMPTION A \<Longrightarrow> A"
  1698 by(simp add: ASSUMPTION_def)
  1699 
  1700 setup \<open>
  1701 let
  1702   val asm_sol = mk_solver "ASSUMPTION" (fn ctxt =>
  1703     resolve_tac ctxt [@{thm ASSUMPTION_I}] THEN'
  1704     resolve_tac ctxt (Simplifier.prems_of ctxt))
  1705 in
  1706   map_theory_simpset (fn ctxt => Simplifier.addSolver (ctxt,asm_sol))
  1707 end
  1708 \<close>
  1709 
  1710 
  1711 subsection \<open>Code generator setup\<close>
  1712 
  1713 subsubsection \<open>Generic code generator preprocessor setup\<close>
  1714 
  1715 lemma conj_left_cong:
  1716   "P \<longleftrightarrow> Q \<Longrightarrow> P \<and> R \<longleftrightarrow> Q \<and> R"
  1717   by (fact arg_cong)
  1718 
  1719 lemma disj_left_cong:
  1720   "P \<longleftrightarrow> Q \<Longrightarrow> P \<or> R \<longleftrightarrow> Q \<or> R"
  1721   by (fact arg_cong)
  1722 
  1723 setup \<open>
  1724   Code_Preproc.map_pre (put_simpset HOL_basic_ss) #>
  1725   Code_Preproc.map_post (put_simpset HOL_basic_ss) #>
  1726   Code_Simp.map_ss (put_simpset HOL_basic_ss #>
  1727   Simplifier.add_cong @{thm conj_left_cong} #>
  1728   Simplifier.add_cong @{thm disj_left_cong})
  1729 \<close>
  1730 
  1731 
  1732 subsubsection \<open>Equality\<close>
  1733 
  1734 class equal =
  1735   fixes equal :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1736   assumes equal_eq: "equal x y \<longleftrightarrow> x = y"
  1737 begin
  1738 
  1739 lemma equal: "equal = (op =)"
  1740   by (rule ext equal_eq)+
  1741 
  1742 lemma equal_refl: "equal x x \<longleftrightarrow> True"
  1743   unfolding equal by rule+
  1744 
  1745 lemma eq_equal: "(op =) \<equiv> equal"
  1746   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule equal_eq)
  1747 
  1748 end
  1749 
  1750 declare eq_equal [symmetric, code_post]
  1751 declare eq_equal [code]
  1752 
  1753 setup \<open>
  1754   Code_Preproc.map_pre (fn ctxt =>
  1755     ctxt addsimprocs
  1756       [Simplifier.make_simproc @{context} "equal"
  1757         {lhss = [@{term HOL.eq}],
  1758          proc = fn _ => fn _ => fn ct =>
  1759           (case Thm.term_of ct of
  1760             Const (_, Type (@{type_name fun}, [Type _, _])) => SOME @{thm eq_equal}
  1761           | _ => NONE)}])
  1762 \<close>
  1763 
  1764 
  1765 subsubsection \<open>Generic code generator foundation\<close>
  1766 
  1767 text \<open>Datatype @{typ bool}\<close>
  1768 
  1769 code_datatype True False
  1770 
  1771 lemma [code]:
  1772   shows "False \<and> P \<longleftrightarrow> False"
  1773     and "True \<and> P \<longleftrightarrow> P"
  1774     and "P \<and> False \<longleftrightarrow> False"
  1775     and "P \<and> True \<longleftrightarrow> P" by simp_all
  1776 
  1777 lemma [code]:
  1778   shows "False \<or> P \<longleftrightarrow> P"
  1779     and "True \<or> P \<longleftrightarrow> True"
  1780     and "P \<or> False \<longleftrightarrow> P"
  1781     and "P \<or> True \<longleftrightarrow> True" by simp_all
  1782 
  1783 lemma [code]:
  1784   shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
  1785     and "(True \<longrightarrow> P) \<longleftrightarrow> P"
  1786     and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
  1787     and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
  1788 
  1789 text \<open>More about @{typ prop}\<close>
  1790 
  1791 lemma [code nbe]:
  1792   shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q"
  1793     and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
  1794     and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
  1795 
  1796 lemma Trueprop_code [code]:
  1797   "Trueprop True \<equiv> Code_Generator.holds"
  1798   by (auto intro!: equal_intr_rule holds)
  1799 
  1800 declare Trueprop_code [symmetric, code_post]
  1801 
  1802 text \<open>Equality\<close>
  1803 
  1804 declare simp_thms(6) [code nbe]
  1805 
  1806 instantiation itself :: (type) equal
  1807 begin
  1808 
  1809 definition equal_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1810   "equal_itself x y \<longleftrightarrow> x = y"
  1811 
  1812 instance proof
  1813 qed (fact equal_itself_def)
  1814 
  1815 end
  1816 
  1817 lemma equal_itself_code [code]:
  1818   "equal TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1819   by (simp add: equal)
  1820 
  1821 setup \<open>Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a::type \<Rightarrow> 'a \<Rightarrow> bool"})\<close>
  1822 
  1823 lemma equal_alias_cert: "OFCLASS('a, equal_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> equal)" (is "?ofclass \<equiv> ?equal")
  1824 proof
  1825   assume "PROP ?ofclass"
  1826   show "PROP ?equal"
  1827     by (tactic \<open>ALLGOALS (resolve_tac @{context} [Thm.unconstrainT @{thm eq_equal}])\<close>)
  1828       (fact \<open>PROP ?ofclass\<close>)
  1829 next
  1830   assume "PROP ?equal"
  1831   show "PROP ?ofclass" proof
  1832   qed (simp add: \<open>PROP ?equal\<close>)
  1833 qed
  1834 
  1835 setup \<open>Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a::equal \<Rightarrow> 'a \<Rightarrow> bool"})\<close>
  1836 
  1837 setup \<open>Nbe.add_const_alias @{thm equal_alias_cert}\<close>
  1838 
  1839 text \<open>Cases\<close>
  1840 
  1841 lemma Let_case_cert:
  1842   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1843   shows "CASE x \<equiv> f x"
  1844   using assms by simp_all
  1845 
  1846 setup \<open>
  1847   Code.add_case @{thm Let_case_cert} #>
  1848   Code.add_undefined @{const_name undefined}
  1849 \<close>
  1850 
  1851 declare [[code abort: undefined]]
  1852 
  1853 
  1854 subsubsection \<open>Generic code generator target languages\<close>
  1855 
  1856 text \<open>type @{typ bool}\<close>
  1857 
  1858 code_printing
  1859   type_constructor bool \<rightharpoonup>
  1860     (SML) "bool" and (OCaml) "bool" and (Haskell) "Bool" and (Scala) "Boolean"
  1861 | constant True \<rightharpoonup>
  1862     (SML) "true" and (OCaml) "true" and (Haskell) "True" and (Scala) "true"
  1863 | constant False \<rightharpoonup>
  1864     (SML) "false" and (OCaml) "false" and (Haskell) "False" and (Scala) "false"
  1865 
  1866 code_reserved SML
  1867   bool true false
  1868 
  1869 code_reserved OCaml
  1870   bool
  1871 
  1872 code_reserved Scala
  1873   Boolean
  1874 
  1875 code_printing
  1876   constant Not \<rightharpoonup>
  1877     (SML) "not" and (OCaml) "not" and (Haskell) "not" and (Scala) "'! _"
  1878 | constant HOL.conj \<rightharpoonup>
  1879     (SML) infixl 1 "andalso" and (OCaml) infixl 3 "&&" and (Haskell) infixr 3 "&&" and (Scala) infixl 3 "&&"
  1880 | constant HOL.disj \<rightharpoonup>
  1881     (SML) infixl 0 "orelse" and (OCaml) infixl 2 "||" and (Haskell) infixl 2 "||" and (Scala) infixl 1 "||"
  1882 | constant HOL.implies \<rightharpoonup>
  1883     (SML) "!(if (_)/ then (_)/ else true)"
  1884     and (OCaml) "!(if (_)/ then (_)/ else true)"
  1885     and (Haskell) "!(if (_)/ then (_)/ else True)"
  1886     and (Scala) "!(if ((_))/ (_)/ else true)"
  1887 | constant If \<rightharpoonup>
  1888     (SML) "!(if (_)/ then (_)/ else (_))"
  1889     and (OCaml) "!(if (_)/ then (_)/ else (_))"
  1890     and (Haskell) "!(if (_)/ then (_)/ else (_))"
  1891     and (Scala) "!(if ((_))/ (_)/ else (_))"
  1892 
  1893 code_reserved SML
  1894   not
  1895 
  1896 code_reserved OCaml
  1897   not
  1898 
  1899 code_identifier
  1900   code_module Pure \<rightharpoonup>
  1901     (SML) HOL and (OCaml) HOL and (Haskell) HOL and (Scala) HOL
  1902 
  1903 text \<open>using built-in Haskell equality\<close>
  1904 
  1905 code_printing
  1906   type_class equal \<rightharpoonup> (Haskell) "Eq"
  1907 | constant HOL.equal \<rightharpoonup> (Haskell) infix 4 "=="
  1908 | constant HOL.eq \<rightharpoonup> (Haskell) infix 4 "=="
  1909 
  1910 text \<open>undefined\<close>
  1911 
  1912 code_printing
  1913   constant undefined \<rightharpoonup>
  1914     (SML) "!(raise/ Fail/ \"undefined\")"
  1915     and (OCaml) "failwith/ \"undefined\""
  1916     and (Haskell) "error/ \"undefined\""
  1917     and (Scala) "!sys.error(\"undefined\")"
  1918 
  1919 
  1920 subsubsection \<open>Evaluation and normalization by evaluation\<close>
  1921 
  1922 method_setup eval = \<open>
  1923   let
  1924     fun eval_tac ctxt =
  1925       let val conv = Code_Runtime.dynamic_holds_conv ctxt
  1926       in
  1927         CONVERSION (Conv.params_conv ~1 (K (Conv.concl_conv ~1 conv)) ctxt) THEN'
  1928         resolve_tac ctxt [TrueI]
  1929       end
  1930   in
  1931     Scan.succeed (SIMPLE_METHOD' o eval_tac)
  1932   end
  1933 \<close> "solve goal by evaluation"
  1934 
  1935 method_setup normalization = \<open>
  1936   Scan.succeed (fn ctxt =>
  1937     SIMPLE_METHOD'
  1938       (CHANGED_PROP o
  1939         (CONVERSION (Nbe.dynamic_conv ctxt)
  1940           THEN_ALL_NEW (TRY o resolve_tac ctxt [TrueI]))))
  1941 \<close> "solve goal by normalization"
  1942 
  1943 
  1944 subsection \<open>Counterexample Search Units\<close>
  1945 
  1946 subsubsection \<open>Quickcheck\<close>
  1947 
  1948 quickcheck_params [size = 5, iterations = 50]
  1949 
  1950 
  1951 subsubsection \<open>Nitpick setup\<close>
  1952 
  1953 named_theorems nitpick_unfold "alternative definitions of constants as needed by Nitpick"
  1954   and nitpick_simp "equational specification of constants as needed by Nitpick"
  1955   and nitpick_psimp "partial equational specification of constants as needed by Nitpick"
  1956   and nitpick_choice_spec "choice specification of constants as needed by Nitpick"
  1957 
  1958 declare if_bool_eq_conj [nitpick_unfold, no_atp]
  1959         if_bool_eq_disj [no_atp]
  1960 
  1961 
  1962 subsection \<open>Preprocessing for the predicate compiler\<close>
  1963 
  1964 named_theorems code_pred_def "alternative definitions of constants for the Predicate Compiler"
  1965   and code_pred_inline "inlining definitions for the Predicate Compiler"
  1966   and code_pred_simp "simplification rules for the optimisations in the Predicate Compiler"
  1967 
  1968 
  1969 subsection \<open>Legacy tactics and ML bindings\<close>
  1970 
  1971 ML \<open>
  1972   (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  1973   local
  1974     fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
  1975       | wrong_prem (Bound _) = true
  1976       | wrong_prem _ = false;
  1977     val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  1978     fun smp i = funpow i (fn m => filter_right ([spec] RL m)) [mp];
  1979   in
  1980     fun smp_tac ctxt j = EVERY' [dresolve_tac ctxt (smp j), assume_tac ctxt];
  1981   end;
  1982 
  1983   local
  1984     val nnf_ss =
  1985       simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms simp_thms nnf_simps});
  1986   in
  1987     fun nnf_conv ctxt = Simplifier.rewrite (put_simpset nnf_ss ctxt);
  1988   end
  1989 \<close>
  1990 
  1991 hide_const (open) eq equal
  1992 
  1993 end