src/HOL/HOL.thy
author wenzelm
Fri Apr 08 20:15:20 2016 +0200 (2016-04-08)
changeset 62913 13252110a6fe
parent 62522 d32c23d29968
child 62958 b41c1cb5e251
permissions -rw-r--r--
eliminated unused simproc identifier;
     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: "\<And>X. \<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 
   993 lemma iff_conv_conj_imp: "(P = Q) = ((P \<longrightarrow> Q) \<and> (Q \<longrightarrow> P))" by iprover
   994 
   995 
   996 lemma cases_simp: "((P \<longrightarrow> Q) \<and> (\<not> P \<longrightarrow> Q)) = Q"
   997   \<comment> \<open>Avoids duplication of subgoals after \<open>if_split\<close>, when the true and false\<close>
   998   \<comment> \<open>cases boil down to the same thing.\<close>
   999   by blast
  1000 
  1001 lemma not_all: "(\<not> (\<forall>x. P x)) = (\<exists>x. \<not> P x)" by blast
  1002 lemma imp_all: "((\<forall>x. P x) \<longrightarrow> Q) = (\<exists>x. P x \<longrightarrow> Q)" by blast
  1003 lemma not_ex: "(\<not> (\<exists>x. P x)) = (\<forall>x. \<not> P x)" by iprover
  1004 lemma imp_ex: "((\<exists>x. P x) \<longrightarrow> Q) = (\<forall>x. P x \<longrightarrow> Q)" by iprover
  1005 lemma all_not_ex: "(\<forall>x. P x) = (\<not> (\<exists>x. \<not> P x ))" by blast
  1006 
  1007 declare All_def [no_atp]
  1008 
  1009 lemma ex_disj_distrib: "(\<exists>x. P x \<or> Q x) = ((\<exists>x. P x) \<or> (\<exists>x. Q x))" by iprover
  1010 lemma all_conj_distrib: "(\<forall>x. P x \<and> Q x) = ((\<forall>x. P x) \<and> (\<forall>x. Q x))" by iprover
  1011 
  1012 text \<open>
  1013   \medskip The \<open>\<and>\<close> congruence rule: not included by default!
  1014   May slow rewrite proofs down by as much as 50\%\<close>
  1015 
  1016 lemma conj_cong:
  1017     "(P = P') \<Longrightarrow> (P' \<Longrightarrow> (Q = Q')) \<Longrightarrow> ((P \<and> Q) = (P' \<and> Q'))"
  1018   by iprover
  1019 
  1020 lemma rev_conj_cong:
  1021     "(Q = Q') \<Longrightarrow> (Q' \<Longrightarrow> (P = P')) \<Longrightarrow> ((P \<and> Q) = (P' \<and> Q'))"
  1022   by iprover
  1023 
  1024 text \<open>The \<open>|\<close> congruence rule: not included by default!\<close>
  1025 
  1026 lemma disj_cong:
  1027     "(P = P') \<Longrightarrow> (\<not> P' \<Longrightarrow> (Q = Q')) \<Longrightarrow> ((P \<or> Q) = (P' \<or> Q'))"
  1028   by blast
  1029 
  1030 
  1031 text \<open>\medskip if-then-else rules\<close>
  1032 
  1033 lemma if_True [code]: "(if True then x else y) = x"
  1034   by (unfold If_def) blast
  1035 
  1036 lemma if_False [code]: "(if False then x else y) = y"
  1037   by (unfold If_def) blast
  1038 
  1039 lemma if_P: "P \<Longrightarrow> (if P then x else y) = x"
  1040   by (unfold If_def) blast
  1041 
  1042 lemma if_not_P: "\<not> P \<Longrightarrow> (if P then x else y) = y"
  1043   by (unfold If_def) blast
  1044 
  1045 lemma if_split: "P (if Q then x else y) = ((Q \<longrightarrow> P x) \<and> (\<not> Q \<longrightarrow> P y))"
  1046   apply (rule case_split [of Q])
  1047    apply (simplesubst if_P)
  1048     prefer 3 apply (simplesubst if_not_P, blast+)
  1049   done
  1050 
  1051 lemma if_split_asm: "P (if Q then x else y) = (\<not> ((Q \<and> \<not> P x) \<or> (\<not> Q \<and> \<not> P y)))"
  1052 by (simplesubst if_split, blast)
  1053 
  1054 lemmas if_splits [no_atp] = if_split if_split_asm
  1055 
  1056 lemma if_cancel: "(if c then x else x) = x"
  1057 by (simplesubst if_split, blast)
  1058 
  1059 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1060 by (simplesubst if_split, blast)
  1061 
  1062 lemma if_bool_eq_conj: "(if P then Q else R) = ((P \<longrightarrow> Q) \<and> (\<not> P \<longrightarrow> R))"
  1063   \<comment> \<open>This form is useful for expanding \<open>if\<close>s on the RIGHT of the \<open>\<Longrightarrow>\<close> symbol.\<close>
  1064   by (rule if_split)
  1065 
  1066 lemma if_bool_eq_disj: "(if P then Q else R) = ((P \<and> Q) \<or> (\<not> P \<and> R))"
  1067   \<comment> \<open>And this form is useful for expanding \<open>if\<close>s on the LEFT.\<close>
  1068   by (simplesubst if_split) blast
  1069 
  1070 lemma Eq_TrueI: "P \<Longrightarrow> P \<equiv> True" by (unfold atomize_eq) iprover
  1071 lemma Eq_FalseI: "\<not> P \<Longrightarrow> P \<equiv> False" by (unfold atomize_eq) iprover
  1072 
  1073 text \<open>\medskip let rules for simproc\<close>
  1074 
  1075 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow> Let x f \<equiv> Let x g"
  1076   by (unfold Let_def)
  1077 
  1078 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow> Let x f \<equiv> g"
  1079   by (unfold Let_def)
  1080 
  1081 text \<open>
  1082   The following copy of the implication operator is useful for
  1083   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1084   its premise.
  1085 \<close>
  1086 
  1087 definition simp_implies :: "[prop, prop] \<Rightarrow> prop"  (infixr "=simp=>" 1) where
  1088   "simp_implies \<equiv> op \<Longrightarrow>"
  1089 
  1090 lemma simp_impliesI:
  1091   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1092   shows "PROP P =simp=> PROP Q"
  1093   apply (unfold simp_implies_def)
  1094   apply (rule PQ)
  1095   apply assumption
  1096   done
  1097 
  1098 lemma simp_impliesE:
  1099   assumes PQ: "PROP P =simp=> PROP Q"
  1100   and P: "PROP P"
  1101   and QR: "PROP Q \<Longrightarrow> PROP R"
  1102   shows "PROP R"
  1103   apply (rule QR)
  1104   apply (rule PQ [unfolded simp_implies_def])
  1105   apply (rule P)
  1106   done
  1107 
  1108 lemma simp_implies_cong:
  1109   assumes PP' :"PROP P \<equiv> PROP P'"
  1110   and P'QQ': "PROP P' \<Longrightarrow> (PROP Q \<equiv> PROP Q')"
  1111   shows "(PROP P =simp=> PROP Q) \<equiv> (PROP P' =simp=> PROP Q')"
  1112 proof (unfold simp_implies_def, rule equal_intr_rule)
  1113   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1114   and P': "PROP P'"
  1115   from PP' [symmetric] and P' have "PROP P"
  1116     by (rule equal_elim_rule1)
  1117   then have "PROP Q" by (rule PQ)
  1118   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1119 next
  1120   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1121   and P: "PROP P"
  1122   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1123   then have "PROP Q'" by (rule P'Q')
  1124   with P'QQ' [OF P', symmetric] show "PROP Q"
  1125     by (rule equal_elim_rule1)
  1126 qed
  1127 
  1128 lemma uncurry:
  1129   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1130   shows "P \<and> Q \<longrightarrow> R"
  1131   using assms by blast
  1132 
  1133 lemma iff_allI:
  1134   assumes "\<And>x. P x = Q x"
  1135   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1136   using assms by blast
  1137 
  1138 lemma iff_exI:
  1139   assumes "\<And>x. P x = Q x"
  1140   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1141   using assms by blast
  1142 
  1143 lemma all_comm:
  1144   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1145   by blast
  1146 
  1147 lemma ex_comm:
  1148   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1149   by blast
  1150 
  1151 ML_file "Tools/simpdata.ML"
  1152 ML \<open>open Simpdata\<close>
  1153 
  1154 setup \<open>
  1155   map_theory_simpset (put_simpset HOL_basic_ss) #>
  1156   Simplifier.method_setup Splitter.split_modifiers
  1157 \<close>
  1158 
  1159 simproc_setup defined_Ex ("\<exists>x. P x") = \<open>fn _ => Quantifier1.rearrange_ex\<close>
  1160 simproc_setup defined_All ("\<forall>x. P x") = \<open>fn _ => Quantifier1.rearrange_all\<close>
  1161 
  1162 text \<open>Simproc for proving \<open>(y = x) \<equiv> False\<close> from premise \<open>\<not> (x = y)\<close>:\<close>
  1163 
  1164 simproc_setup neq ("x = y") = \<open>fn _ =>
  1165 let
  1166   val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
  1167   fun is_neq eq lhs rhs thm =
  1168     (case Thm.prop_of thm of
  1169       _ $ (Not $ (eq' $ l' $ r')) =>
  1170         Not = HOLogic.Not andalso eq' = eq andalso
  1171         r' aconv lhs andalso l' aconv rhs
  1172     | _ => false);
  1173   fun proc ss ct =
  1174     (case Thm.term_of ct of
  1175       eq $ lhs $ rhs =>
  1176         (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of ss) of
  1177           SOME thm => SOME (thm RS neq_to_EQ_False)
  1178         | NONE => NONE)
  1179      | _ => NONE);
  1180 in proc end;
  1181 \<close>
  1182 
  1183 simproc_setup let_simp ("Let x f") = \<open>
  1184 let
  1185   fun count_loose (Bound i) k = if i >= k then 1 else 0
  1186     | count_loose (s $ t) k = count_loose s k + count_loose t k
  1187     | count_loose (Abs (_, _, t)) k = count_loose  t (k + 1)
  1188     | count_loose _ _ = 0;
  1189   fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
  1190     (case t of
  1191       Abs (_, _, t') => count_loose t' 0 <= 1
  1192     | _ => true);
  1193 in
  1194   fn _ => fn ctxt => fn ct =>
  1195     if is_trivial_let (Thm.term_of ct)
  1196     then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
  1197     else
  1198       let (*Norbert Schirmer's case*)
  1199         val t = Thm.term_of ct;
  1200         val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
  1201       in
  1202         Option.map (hd o Variable.export ctxt' ctxt o single)
  1203           (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
  1204             if is_Free x orelse is_Bound x orelse is_Const x
  1205             then SOME @{thm Let_def}
  1206             else
  1207               let
  1208                 val n = case f of (Abs (x, _, _)) => x | _ => "x";
  1209                 val cx = Thm.cterm_of ctxt x;
  1210                 val xT = Thm.typ_of_cterm cx;
  1211                 val cf = Thm.cterm_of ctxt f;
  1212                 val fx_g = Simplifier.rewrite ctxt (Thm.apply cf cx);
  1213                 val (_ $ _ $ g) = Thm.prop_of fx_g;
  1214                 val g' = abstract_over (x, g);
  1215                 val abs_g'= Abs (n, xT, g');
  1216               in
  1217                 if g aconv g' then
  1218                   let
  1219                     val rl =
  1220                       infer_instantiate ctxt [(("f", 0), cf), (("x", 0), cx)] @{thm Let_unfold};
  1221                   in SOME (rl OF [fx_g]) end
  1222                 else if (Envir.beta_eta_contract f) aconv (Envir.beta_eta_contract abs_g')
  1223                 then NONE (*avoid identity conversion*)
  1224                 else
  1225                   let
  1226                     val g'x = abs_g' $ x;
  1227                     val g_g'x = Thm.symmetric (Thm.beta_conversion false (Thm.cterm_of ctxt g'x));
  1228                     val rl =
  1229                       @{thm Let_folded} |> infer_instantiate ctxt
  1230                         [(("f", 0), Thm.cterm_of ctxt f),
  1231                          (("x", 0), cx),
  1232                          (("g", 0), Thm.cterm_of ctxt abs_g')];
  1233                   in SOME (rl OF [Thm.transitive fx_g g_g'x]) end
  1234               end
  1235           | _ => NONE)
  1236       end
  1237 end\<close>
  1238 
  1239 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
  1240 proof
  1241   assume "True \<Longrightarrow> PROP P"
  1242   from this [OF TrueI] show "PROP P" .
  1243 next
  1244   assume "PROP P"
  1245   then show "PROP P" .
  1246 qed
  1247 
  1248 lemma implies_True_equals: "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True"
  1249   by standard (intro TrueI)
  1250 
  1251 lemma False_implies_equals: "(False \<Longrightarrow> P) \<equiv> Trueprop True"
  1252   by standard simp_all
  1253 
  1254 (* This is not made a simp rule because it does not improve any proofs
  1255    but slows some AFP entries down by 5% (cpu time). May 2015 *)
  1256 lemma implies_False_swap: "NO_MATCH (Trueprop False) P \<Longrightarrow>
  1257   (False \<Longrightarrow> PROP P \<Longrightarrow> PROP Q) \<equiv> (PROP P \<Longrightarrow> False \<Longrightarrow> PROP Q)"
  1258 by(rule swap_prems_eq)
  1259 
  1260 lemma ex_simps:
  1261   "\<And>P Q. (\<exists>x. P x \<and> Q)   = ((\<exists>x. P x) \<and> Q)"
  1262   "\<And>P Q. (\<exists>x. P \<and> Q x)   = (P \<and> (\<exists>x. Q x))"
  1263   "\<And>P Q. (\<exists>x. P x \<or> Q)   = ((\<exists>x. P x) \<or> Q)"
  1264   "\<And>P Q. (\<exists>x. P \<or> Q x)   = (P \<or> (\<exists>x. Q x))"
  1265   "\<And>P Q. (\<exists>x. P x \<longrightarrow> Q) = ((\<forall>x. P x) \<longrightarrow> Q)"
  1266   "\<And>P Q. (\<exists>x. P \<longrightarrow> Q x) = (P \<longrightarrow> (\<exists>x. Q x))"
  1267   \<comment> \<open>Miniscoping: pushing in existential quantifiers.\<close>
  1268   by (iprover | blast)+
  1269 
  1270 lemma all_simps:
  1271   "\<And>P Q. (\<forall>x. P x \<and> Q)   = ((\<forall>x. P x) \<and> Q)"
  1272   "\<And>P Q. (\<forall>x. P \<and> Q x)   = (P \<and> (\<forall>x. Q x))"
  1273   "\<And>P Q. (\<forall>x. P x \<or> Q)   = ((\<forall>x. P x) \<or> Q)"
  1274   "\<And>P Q. (\<forall>x. P \<or> Q x)   = (P \<or> (\<forall>x. Q x))"
  1275   "\<And>P Q. (\<forall>x. P x \<longrightarrow> Q) = ((\<exists>x. P x) \<longrightarrow> Q)"
  1276   "\<And>P Q. (\<forall>x. P \<longrightarrow> Q x) = (P \<longrightarrow> (\<forall>x. Q x))"
  1277   \<comment> \<open>Miniscoping: pushing in universal quantifiers.\<close>
  1278   by (iprover | blast)+
  1279 
  1280 lemmas [simp] =
  1281   triv_forall_equality (*prunes params*)
  1282   True_implies_equals implies_True_equals (*prune True in asms*)
  1283   False_implies_equals (*prune False in asms*)
  1284   if_True
  1285   if_False
  1286   if_cancel
  1287   if_eq_cancel
  1288   imp_disjL
  1289   (*In general it seems wrong to add distributive laws by default: they
  1290     might cause exponential blow-up.  But imp_disjL has been in for a while
  1291     and cannot be removed without affecting existing proofs.  Moreover,
  1292     rewriting by "(P \<or> Q \<longrightarrow> R) = ((P \<longrightarrow> R) \<and> (Q \<longrightarrow> R))" might be justified on the
  1293     grounds that it allows simplification of R in the two cases.*)
  1294   conj_assoc
  1295   disj_assoc
  1296   de_Morgan_conj
  1297   de_Morgan_disj
  1298   imp_disj1
  1299   imp_disj2
  1300   not_imp
  1301   disj_not1
  1302   not_all
  1303   not_ex
  1304   cases_simp
  1305   the_eq_trivial
  1306   the_sym_eq_trivial
  1307   ex_simps
  1308   all_simps
  1309   simp_thms
  1310 
  1311 lemmas [cong] = imp_cong simp_implies_cong
  1312 lemmas [split] = if_split
  1313 
  1314 ML \<open>val HOL_ss = simpset_of @{context}\<close>
  1315 
  1316 text \<open>Simplifies @{term x} assuming @{prop c} and @{term y} assuming @{prop "\<not> c"}\<close>
  1317 lemma if_cong:
  1318   assumes "b = c"
  1319       and "c \<Longrightarrow> x = u"
  1320       and "\<not> c \<Longrightarrow> y = v"
  1321   shows "(if b then x else y) = (if c then u else v)"
  1322   using assms by simp
  1323 
  1324 text \<open>Prevents simplification of x and y:
  1325   faster and allows the execution of functional programs.\<close>
  1326 lemma if_weak_cong [cong]:
  1327   assumes "b = c"
  1328   shows "(if b then x else y) = (if c then x else y)"
  1329   using assms by (rule arg_cong)
  1330 
  1331 text \<open>Prevents simplification of t: much faster\<close>
  1332 lemma let_weak_cong:
  1333   assumes "a = b"
  1334   shows "(let x = a in t x) = (let x = b in t x)"
  1335   using assms by (rule arg_cong)
  1336 
  1337 text \<open>To tidy up the result of a simproc.  Only the RHS will be simplified.\<close>
  1338 lemma eq_cong2:
  1339   assumes "u = u'"
  1340   shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
  1341   using assms by simp
  1342 
  1343 lemma if_distrib:
  1344   "f (if c then x else y) = (if c then f x else f y)"
  1345   by simp
  1346 
  1347 text\<open>As a simplification rule, it replaces all function equalities by
  1348   first-order equalities.\<close>
  1349 lemma fun_eq_iff: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
  1350   by auto
  1351 
  1352 
  1353 subsubsection \<open>Generic cases and induction\<close>
  1354 
  1355 text \<open>Rule projections:\<close>
  1356 ML \<open>
  1357 structure Project_Rule = Project_Rule
  1358 (
  1359   val conjunct1 = @{thm conjunct1}
  1360   val conjunct2 = @{thm conjunct2}
  1361   val mp = @{thm mp}
  1362 );
  1363 \<close>
  1364 
  1365 context
  1366 begin
  1367 
  1368 qualified definition "induct_forall P \<equiv> \<forall>x. P x"
  1369 qualified definition "induct_implies A B \<equiv> A \<longrightarrow> B"
  1370 qualified definition "induct_equal x y \<equiv> x = y"
  1371 qualified definition "induct_conj A B \<equiv> A \<and> B"
  1372 qualified definition "induct_true \<equiv> True"
  1373 qualified definition "induct_false \<equiv> False"
  1374 
  1375 lemma induct_forall_eq: "(\<And>x. P x) \<equiv> Trueprop (induct_forall (\<lambda>x. P x))"
  1376   by (unfold atomize_all induct_forall_def)
  1377 
  1378 lemma induct_implies_eq: "(A \<Longrightarrow> B) \<equiv> Trueprop (induct_implies A B)"
  1379   by (unfold atomize_imp induct_implies_def)
  1380 
  1381 lemma induct_equal_eq: "(x \<equiv> y) \<equiv> Trueprop (induct_equal x y)"
  1382   by (unfold atomize_eq induct_equal_def)
  1383 
  1384 lemma induct_conj_eq: "(A &&& B) \<equiv> Trueprop (induct_conj A B)"
  1385   by (unfold atomize_conj induct_conj_def)
  1386 
  1387 lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
  1388 lemmas induct_atomize = induct_atomize' induct_equal_eq
  1389 lemmas induct_rulify' [symmetric] = induct_atomize'
  1390 lemmas induct_rulify [symmetric] = induct_atomize
  1391 lemmas induct_rulify_fallback =
  1392   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
  1393   induct_true_def induct_false_def
  1394 
  1395 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
  1396     induct_conj (induct_forall A) (induct_forall B)"
  1397   by (unfold induct_forall_def induct_conj_def) iprover
  1398 
  1399 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
  1400     induct_conj (induct_implies C A) (induct_implies C B)"
  1401   by (unfold induct_implies_def induct_conj_def) iprover
  1402 
  1403 lemma induct_conj_curry: "(induct_conj A B \<Longrightarrow> PROP C) \<equiv> (A \<Longrightarrow> B \<Longrightarrow> PROP C)"
  1404 proof
  1405   assume r: "induct_conj A B \<Longrightarrow> PROP C"
  1406   assume ab: A B
  1407   show "PROP C" by (rule r) (simp add: induct_conj_def ab)
  1408 next
  1409   assume r: "A \<Longrightarrow> B \<Longrightarrow> PROP C"
  1410   assume ab: "induct_conj A B"
  1411   show "PROP C" by (rule r) (simp_all add: ab [unfolded induct_conj_def])
  1412 qed
  1413 
  1414 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1415 
  1416 lemma induct_trueI: "induct_true"
  1417   by (simp add: induct_true_def)
  1418 
  1419 text \<open>Method setup.\<close>
  1420 
  1421 ML_file "~~/src/Tools/induct.ML"
  1422 ML \<open>
  1423 structure Induct = Induct
  1424 (
  1425   val cases_default = @{thm case_split}
  1426   val atomize = @{thms induct_atomize}
  1427   val rulify = @{thms induct_rulify'}
  1428   val rulify_fallback = @{thms induct_rulify_fallback}
  1429   val equal_def = @{thm induct_equal_def}
  1430   fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
  1431     | dest_def _ = NONE
  1432   fun trivial_tac ctxt = match_tac ctxt @{thms induct_trueI}
  1433 )
  1434 \<close>
  1435 
  1436 ML_file "~~/src/Tools/induction.ML"
  1437 
  1438 declaration \<open>
  1439   fn _ => Induct.map_simpset (fn ss => ss
  1440     addsimprocs
  1441       [Simplifier.make_simproc @{context} "swap_induct_false"
  1442         {lhss = [@{term "induct_false \<Longrightarrow> PROP P \<Longrightarrow> PROP Q"}],
  1443          proc = fn _ => fn _ => fn ct =>
  1444           (case Thm.term_of ct of
  1445             _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
  1446               if P <> Q then SOME Drule.swap_prems_eq else NONE
  1447           | _ => NONE)},
  1448        Simplifier.make_simproc @{context} "induct_equal_conj_curry"
  1449         {lhss = [@{term "induct_conj P Q \<Longrightarrow> PROP R"}],
  1450          proc = fn _ => fn _ => fn ct =>
  1451           (case Thm.term_of ct of
  1452             _ $ (_ $ P) $ _ =>
  1453               let
  1454                 fun is_conj (@{const induct_conj} $ P $ Q) =
  1455                       is_conj P andalso is_conj Q
  1456                   | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
  1457                   | is_conj @{const induct_true} = true
  1458                   | is_conj @{const induct_false} = true
  1459                   | is_conj _ = false
  1460               in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
  1461             | _ => NONE)}]
  1462     |> Simplifier.set_mksimps (fn ctxt =>
  1463         Simpdata.mksimps Simpdata.mksimps_pairs ctxt #>
  1464         map (rewrite_rule ctxt (map Thm.symmetric @{thms induct_rulify_fallback}))))
  1465 \<close>
  1466 
  1467 text \<open>Pre-simplification of induction and cases rules\<close>
  1468 
  1469 lemma [induct_simp]: "(\<And>x. induct_equal x t \<Longrightarrow> PROP P x) \<equiv> PROP P t"
  1470   unfolding induct_equal_def
  1471 proof
  1472   assume r: "\<And>x. x = t \<Longrightarrow> PROP P x"
  1473   show "PROP P t" by (rule r [OF refl])
  1474 next
  1475   fix x
  1476   assume "PROP P t" "x = t"
  1477   then show "PROP P x" by simp
  1478 qed
  1479 
  1480 lemma [induct_simp]: "(\<And>x. induct_equal t x \<Longrightarrow> PROP P x) \<equiv> PROP P t"
  1481   unfolding induct_equal_def
  1482 proof
  1483   assume r: "\<And>x. t = x \<Longrightarrow> PROP P x"
  1484   show "PROP P t" by (rule r [OF refl])
  1485 next
  1486   fix x
  1487   assume "PROP P t" "t = x"
  1488   then show "PROP P x" by simp
  1489 qed
  1490 
  1491 lemma [induct_simp]: "(induct_false \<Longrightarrow> P) \<equiv> Trueprop induct_true"
  1492   unfolding induct_false_def induct_true_def
  1493   by (iprover intro: equal_intr_rule)
  1494 
  1495 lemma [induct_simp]: "(induct_true \<Longrightarrow> PROP P) \<equiv> PROP P"
  1496   unfolding induct_true_def
  1497 proof
  1498   assume "True \<Longrightarrow> PROP P"
  1499   then show "PROP P" using TrueI .
  1500 next
  1501   assume "PROP P"
  1502   then show "PROP P" .
  1503 qed
  1504 
  1505 lemma [induct_simp]: "(PROP P \<Longrightarrow> induct_true) \<equiv> Trueprop induct_true"
  1506   unfolding induct_true_def
  1507   by (iprover intro: equal_intr_rule)
  1508 
  1509 lemma [induct_simp]: "(\<And>x. induct_true) \<equiv> Trueprop induct_true"
  1510   unfolding induct_true_def
  1511   by (iprover intro: equal_intr_rule)
  1512 
  1513 lemma [induct_simp]: "induct_implies induct_true P \<equiv> P"
  1514   by (simp add: induct_implies_def induct_true_def)
  1515 
  1516 lemma [induct_simp]: "x = x \<longleftrightarrow> True"
  1517   by (rule simp_thms)
  1518 
  1519 end
  1520 
  1521 ML_file "~~/src/Tools/induct_tacs.ML"
  1522 
  1523 
  1524 subsubsection \<open>Coherent logic\<close>
  1525 
  1526 ML_file "~~/src/Tools/coherent.ML"
  1527 ML \<open>
  1528 structure Coherent = Coherent
  1529 (
  1530   val atomize_elimL = @{thm atomize_elimL};
  1531   val atomize_exL = @{thm atomize_exL};
  1532   val atomize_conjL = @{thm atomize_conjL};
  1533   val atomize_disjL = @{thm atomize_disjL};
  1534   val operator_names = [@{const_name HOL.disj}, @{const_name HOL.conj}, @{const_name Ex}];
  1535 );
  1536 \<close>
  1537 
  1538 
  1539 subsubsection \<open>Reorienting equalities\<close>
  1540 
  1541 ML \<open>
  1542 signature REORIENT_PROC =
  1543 sig
  1544   val add : (term -> bool) -> theory -> theory
  1545   val proc : morphism -> Proof.context -> cterm -> thm option
  1546 end;
  1547 
  1548 structure Reorient_Proc : REORIENT_PROC =
  1549 struct
  1550   structure Data = Theory_Data
  1551   (
  1552     type T = ((term -> bool) * stamp) list;
  1553     val empty = [];
  1554     val extend = I;
  1555     fun merge data : T = Library.merge (eq_snd op =) data;
  1556   );
  1557   fun add m = Data.map (cons (m, stamp ()));
  1558   fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
  1559 
  1560   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1561   fun proc phi ctxt ct =
  1562     let
  1563       val thy = Proof_Context.theory_of ctxt;
  1564     in
  1565       case Thm.term_of ct of
  1566         (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
  1567       | _ => NONE
  1568     end;
  1569 end;
  1570 \<close>
  1571 
  1572 
  1573 subsection \<open>Other simple lemmas and lemma duplicates\<close>
  1574 
  1575 lemma ex1_eq [iff]: "\<exists>!x. x = t" "\<exists>!x. t = x"
  1576   by blast+
  1577 
  1578 lemma choice_eq: "(\<forall>x. \<exists>!y. P x y) = (\<exists>!f. \<forall>x. P x (f x))"
  1579   apply (rule iffI)
  1580   apply (rule_tac a = "\<lambda>x. THE y. P x y" in ex1I)
  1581   apply (fast dest!: theI')
  1582   apply (fast intro: the1_equality [symmetric])
  1583   apply (erule ex1E)
  1584   apply (rule allI)
  1585   apply (rule ex1I)
  1586   apply (erule spec)
  1587   apply (erule_tac x = "\<lambda>z. if z = x then y else f z" in allE)
  1588   apply (erule impE)
  1589   apply (rule allI)
  1590   apply (case_tac "xa = x")
  1591   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1592   done
  1593 
  1594 lemmas eq_sym_conv = eq_commute
  1595 
  1596 lemma nnf_simps:
  1597   "(\<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)"
  1598   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))"
  1599   "(\<not> \<not>(P)) = P"
  1600 by blast+
  1601 
  1602 subsection \<open>Basic ML bindings\<close>
  1603 
  1604 ML \<open>
  1605 val FalseE = @{thm FalseE}
  1606 val Let_def = @{thm Let_def}
  1607 val TrueI = @{thm TrueI}
  1608 val allE = @{thm allE}
  1609 val allI = @{thm allI}
  1610 val all_dupE = @{thm all_dupE}
  1611 val arg_cong = @{thm arg_cong}
  1612 val box_equals = @{thm box_equals}
  1613 val ccontr = @{thm ccontr}
  1614 val classical = @{thm classical}
  1615 val conjE = @{thm conjE}
  1616 val conjI = @{thm conjI}
  1617 val conjunct1 = @{thm conjunct1}
  1618 val conjunct2 = @{thm conjunct2}
  1619 val disjCI = @{thm disjCI}
  1620 val disjE = @{thm disjE}
  1621 val disjI1 = @{thm disjI1}
  1622 val disjI2 = @{thm disjI2}
  1623 val eq_reflection = @{thm eq_reflection}
  1624 val ex1E = @{thm ex1E}
  1625 val ex1I = @{thm ex1I}
  1626 val ex1_implies_ex = @{thm ex1_implies_ex}
  1627 val exE = @{thm exE}
  1628 val exI = @{thm exI}
  1629 val excluded_middle = @{thm excluded_middle}
  1630 val ext = @{thm ext}
  1631 val fun_cong = @{thm fun_cong}
  1632 val iffD1 = @{thm iffD1}
  1633 val iffD2 = @{thm iffD2}
  1634 val iffI = @{thm iffI}
  1635 val impE = @{thm impE}
  1636 val impI = @{thm impI}
  1637 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1638 val mp = @{thm mp}
  1639 val notE = @{thm notE}
  1640 val notI = @{thm notI}
  1641 val not_all = @{thm not_all}
  1642 val not_ex = @{thm not_ex}
  1643 val not_iff = @{thm not_iff}
  1644 val not_not = @{thm not_not}
  1645 val not_sym = @{thm not_sym}
  1646 val refl = @{thm refl}
  1647 val rev_mp = @{thm rev_mp}
  1648 val spec = @{thm spec}
  1649 val ssubst = @{thm ssubst}
  1650 val subst = @{thm subst}
  1651 val sym = @{thm sym}
  1652 val trans = @{thm trans}
  1653 \<close>
  1654 
  1655 ML_file "Tools/cnf.ML"
  1656 
  1657 
  1658 section \<open>\<open>NO_MATCH\<close> simproc\<close>
  1659 
  1660 text \<open>
  1661  The simplification procedure can be used to avoid simplification of terms of a certain form
  1662 \<close>
  1663 
  1664 definition NO_MATCH :: "'a \<Rightarrow> 'b \<Rightarrow> bool" where "NO_MATCH pat val \<equiv> True"
  1665 
  1666 lemma NO_MATCH_cong[cong]: "NO_MATCH pat val = NO_MATCH pat val" by (rule refl)
  1667 
  1668 declare [[coercion_args NO_MATCH - -]]
  1669 
  1670 simproc_setup NO_MATCH ("NO_MATCH pat val") = \<open>fn _ => fn ctxt => fn ct =>
  1671   let
  1672     val thy = Proof_Context.theory_of ctxt
  1673     val dest_binop = Term.dest_comb #> apfst (Term.dest_comb #> snd)
  1674     val m = Pattern.matches thy (dest_binop (Thm.term_of ct))
  1675   in if m then NONE else SOME @{thm NO_MATCH_def} end
  1676 \<close>
  1677 
  1678 text \<open>
  1679   This setup ensures that a rewrite rule of the form @{term "NO_MATCH pat val \<Longrightarrow> t"}
  1680   is only applied, if the pattern @{term pat} does not match the value @{term val}.
  1681 \<close>
  1682 
  1683 
  1684 text\<open>Tagging a premise of a simp rule with ASSUMPTION forces the simplifier
  1685 not to simplify the argument and to solve it by an assumption.\<close>
  1686 
  1687 definition ASSUMPTION :: "bool \<Rightarrow> bool" where
  1688 "ASSUMPTION A \<equiv> A"
  1689 
  1690 lemma ASSUMPTION_cong[cong]: "ASSUMPTION A = ASSUMPTION A"
  1691 by (rule refl)
  1692 
  1693 lemma ASSUMPTION_I: "A \<Longrightarrow> ASSUMPTION A"
  1694 by(simp add: ASSUMPTION_def)
  1695 
  1696 lemma ASSUMPTION_D: "ASSUMPTION A \<Longrightarrow> A"
  1697 by(simp add: ASSUMPTION_def)
  1698 
  1699 setup \<open>
  1700 let
  1701   val asm_sol = mk_solver "ASSUMPTION" (fn ctxt =>
  1702     resolve_tac ctxt [@{thm ASSUMPTION_I}] THEN'
  1703     resolve_tac ctxt (Simplifier.prems_of ctxt))
  1704 in
  1705   map_theory_simpset (fn ctxt => Simplifier.addSolver (ctxt,asm_sol))
  1706 end
  1707 \<close>
  1708 
  1709 
  1710 subsection \<open>Code generator setup\<close>
  1711 
  1712 subsubsection \<open>Generic code generator preprocessor setup\<close>
  1713 
  1714 lemma conj_left_cong:
  1715   "P \<longleftrightarrow> Q \<Longrightarrow> P \<and> R \<longleftrightarrow> Q \<and> R"
  1716   by (fact arg_cong)
  1717 
  1718 lemma disj_left_cong:
  1719   "P \<longleftrightarrow> Q \<Longrightarrow> P \<or> R \<longleftrightarrow> Q \<or> R"
  1720   by (fact arg_cong)
  1721 
  1722 setup \<open>
  1723   Code_Preproc.map_pre (put_simpset HOL_basic_ss) #>
  1724   Code_Preproc.map_post (put_simpset HOL_basic_ss) #>
  1725   Code_Simp.map_ss (put_simpset HOL_basic_ss #>
  1726   Simplifier.add_cong @{thm conj_left_cong} #>
  1727   Simplifier.add_cong @{thm disj_left_cong})
  1728 \<close>
  1729 
  1730 
  1731 subsubsection \<open>Equality\<close>
  1732 
  1733 class equal =
  1734   fixes equal :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1735   assumes equal_eq: "equal x y \<longleftrightarrow> x = y"
  1736 begin
  1737 
  1738 lemma equal: "equal = (op =)"
  1739   by (rule ext equal_eq)+
  1740 
  1741 lemma equal_refl: "equal x x \<longleftrightarrow> True"
  1742   unfolding equal by rule+
  1743 
  1744 lemma eq_equal: "(op =) \<equiv> equal"
  1745   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule equal_eq)
  1746 
  1747 end
  1748 
  1749 declare eq_equal [symmetric, code_post]
  1750 declare eq_equal [code]
  1751 
  1752 setup \<open>
  1753   Code_Preproc.map_pre (fn ctxt =>
  1754     ctxt addsimprocs
  1755       [Simplifier.make_simproc @{context} "equal"
  1756         {lhss = [@{term HOL.eq}],
  1757          proc = fn _ => fn _ => fn ct =>
  1758           (case Thm.term_of ct of
  1759             Const (_, Type (@{type_name fun}, [Type _, _])) => SOME @{thm eq_equal}
  1760           | _ => NONE)}])
  1761 \<close>
  1762 
  1763 
  1764 subsubsection \<open>Generic code generator foundation\<close>
  1765 
  1766 text \<open>Datatype @{typ bool}\<close>
  1767 
  1768 code_datatype True False
  1769 
  1770 lemma [code]:
  1771   shows "False \<and> P \<longleftrightarrow> False"
  1772     and "True \<and> P \<longleftrightarrow> P"
  1773     and "P \<and> False \<longleftrightarrow> False"
  1774     and "P \<and> True \<longleftrightarrow> P" by simp_all
  1775 
  1776 lemma [code]:
  1777   shows "False \<or> P \<longleftrightarrow> P"
  1778     and "True \<or> P \<longleftrightarrow> True"
  1779     and "P \<or> False \<longleftrightarrow> P"
  1780     and "P \<or> True \<longleftrightarrow> True" by simp_all
  1781 
  1782 lemma [code]:
  1783   shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
  1784     and "(True \<longrightarrow> P) \<longleftrightarrow> P"
  1785     and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
  1786     and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
  1787 
  1788 text \<open>More about @{typ prop}\<close>
  1789 
  1790 lemma [code nbe]:
  1791   shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q"
  1792     and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
  1793     and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
  1794 
  1795 lemma Trueprop_code [code]:
  1796   "Trueprop True \<equiv> Code_Generator.holds"
  1797   by (auto intro!: equal_intr_rule holds)
  1798 
  1799 declare Trueprop_code [symmetric, code_post]
  1800 
  1801 text \<open>Equality\<close>
  1802 
  1803 declare simp_thms(6) [code nbe]
  1804 
  1805 instantiation itself :: (type) equal
  1806 begin
  1807 
  1808 definition equal_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1809   "equal_itself x y \<longleftrightarrow> x = y"
  1810 
  1811 instance proof
  1812 qed (fact equal_itself_def)
  1813 
  1814 end
  1815 
  1816 lemma equal_itself_code [code]:
  1817   "equal TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1818   by (simp add: equal)
  1819 
  1820 setup \<open>Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a::type \<Rightarrow> 'a \<Rightarrow> bool"})\<close>
  1821 
  1822 lemma equal_alias_cert: "OFCLASS('a, equal_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> equal)" (is "?ofclass \<equiv> ?equal")
  1823 proof
  1824   assume "PROP ?ofclass"
  1825   show "PROP ?equal"
  1826     by (tactic \<open>ALLGOALS (resolve_tac @{context} [Thm.unconstrainT @{thm eq_equal}])\<close>)
  1827       (fact \<open>PROP ?ofclass\<close>)
  1828 next
  1829   assume "PROP ?equal"
  1830   show "PROP ?ofclass" proof
  1831   qed (simp add: \<open>PROP ?equal\<close>)
  1832 qed
  1833 
  1834 setup \<open>Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a::equal \<Rightarrow> 'a \<Rightarrow> bool"})\<close>
  1835 
  1836 setup \<open>Nbe.add_const_alias @{thm equal_alias_cert}\<close>
  1837 
  1838 text \<open>Cases\<close>
  1839 
  1840 lemma Let_case_cert:
  1841   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1842   shows "CASE x \<equiv> f x"
  1843   using assms by simp_all
  1844 
  1845 setup \<open>
  1846   Code.add_case @{thm Let_case_cert} #>
  1847   Code.add_undefined @{const_name undefined}
  1848 \<close>
  1849 
  1850 declare [[code abort: undefined]]
  1851 
  1852 
  1853 subsubsection \<open>Generic code generator target languages\<close>
  1854 
  1855 text \<open>type @{typ bool}\<close>
  1856 
  1857 code_printing
  1858   type_constructor bool \<rightharpoonup>
  1859     (SML) "bool" and (OCaml) "bool" and (Haskell) "Bool" and (Scala) "Boolean"
  1860 | constant True \<rightharpoonup>
  1861     (SML) "true" and (OCaml) "true" and (Haskell) "True" and (Scala) "true"
  1862 | constant False \<rightharpoonup>
  1863     (SML) "false" and (OCaml) "false" and (Haskell) "False" and (Scala) "false"
  1864 
  1865 code_reserved SML
  1866   bool true false
  1867 
  1868 code_reserved OCaml
  1869   bool
  1870 
  1871 code_reserved Scala
  1872   Boolean
  1873 
  1874 code_printing
  1875   constant Not \<rightharpoonup>
  1876     (SML) "not" and (OCaml) "not" and (Haskell) "not" and (Scala) "'! _"
  1877 | constant HOL.conj \<rightharpoonup>
  1878     (SML) infixl 1 "andalso" and (OCaml) infixl 3 "&&" and (Haskell) infixr 3 "&&" and (Scala) infixl 3 "&&"
  1879 | constant HOL.disj \<rightharpoonup>
  1880     (SML) infixl 0 "orelse" and (OCaml) infixl 2 "||" and (Haskell) infixl 2 "||" and (Scala) infixl 1 "||"
  1881 | constant HOL.implies \<rightharpoonup>
  1882     (SML) "!(if (_)/ then (_)/ else true)"
  1883     and (OCaml) "!(if (_)/ then (_)/ else true)"
  1884     and (Haskell) "!(if (_)/ then (_)/ else True)"
  1885     and (Scala) "!(if ((_))/ (_)/ else true)"
  1886 | constant If \<rightharpoonup>
  1887     (SML) "!(if (_)/ then (_)/ else (_))"
  1888     and (OCaml) "!(if (_)/ then (_)/ else (_))"
  1889     and (Haskell) "!(if (_)/ then (_)/ else (_))"
  1890     and (Scala) "!(if ((_))/ (_)/ else (_))"
  1891 
  1892 code_reserved SML
  1893   not
  1894 
  1895 code_reserved OCaml
  1896   not
  1897 
  1898 code_identifier
  1899   code_module Pure \<rightharpoonup>
  1900     (SML) HOL and (OCaml) HOL and (Haskell) HOL and (Scala) HOL
  1901 
  1902 text \<open>using built-in Haskell equality\<close>
  1903 
  1904 code_printing
  1905   type_class equal \<rightharpoonup> (Haskell) "Eq"
  1906 | constant HOL.equal \<rightharpoonup> (Haskell) infix 4 "=="
  1907 | constant HOL.eq \<rightharpoonup> (Haskell) infix 4 "=="
  1908 
  1909 text \<open>undefined\<close>
  1910 
  1911 code_printing
  1912   constant undefined \<rightharpoonup>
  1913     (SML) "!(raise/ Fail/ \"undefined\")"
  1914     and (OCaml) "failwith/ \"undefined\""
  1915     and (Haskell) "error/ \"undefined\""
  1916     and (Scala) "!sys.error(\"undefined\")"
  1917 
  1918 
  1919 subsubsection \<open>Evaluation and normalization by evaluation\<close>
  1920 
  1921 method_setup eval = \<open>
  1922   let
  1923     fun eval_tac ctxt =
  1924       let val conv = Code_Runtime.dynamic_holds_conv ctxt
  1925       in
  1926         CONVERSION (Conv.params_conv ~1 (K (Conv.concl_conv ~1 conv)) ctxt) THEN'
  1927         resolve_tac ctxt [TrueI]
  1928       end
  1929   in
  1930     Scan.succeed (SIMPLE_METHOD' o eval_tac)
  1931   end
  1932 \<close> "solve goal by evaluation"
  1933 
  1934 method_setup normalization = \<open>
  1935   Scan.succeed (fn ctxt =>
  1936     SIMPLE_METHOD'
  1937       (CHANGED_PROP o
  1938         (CONVERSION (Nbe.dynamic_conv ctxt)
  1939           THEN_ALL_NEW (TRY o resolve_tac ctxt [TrueI]))))
  1940 \<close> "solve goal by normalization"
  1941 
  1942 
  1943 subsection \<open>Counterexample Search Units\<close>
  1944 
  1945 subsubsection \<open>Quickcheck\<close>
  1946 
  1947 quickcheck_params [size = 5, iterations = 50]
  1948 
  1949 
  1950 subsubsection \<open>Nitpick setup\<close>
  1951 
  1952 named_theorems nitpick_unfold "alternative definitions of constants as needed by Nitpick"
  1953   and nitpick_simp "equational specification of constants as needed by Nitpick"
  1954   and nitpick_psimp "partial equational specification of constants as needed by Nitpick"
  1955   and nitpick_choice_spec "choice specification of constants as needed by Nitpick"
  1956 
  1957 declare if_bool_eq_conj [nitpick_unfold, no_atp]
  1958         if_bool_eq_disj [no_atp]
  1959 
  1960 
  1961 subsection \<open>Preprocessing for the predicate compiler\<close>
  1962 
  1963 named_theorems code_pred_def "alternative definitions of constants for the Predicate Compiler"
  1964   and code_pred_inline "inlining definitions for the Predicate Compiler"
  1965   and code_pred_simp "simplification rules for the optimisations in the Predicate Compiler"
  1966 
  1967 
  1968 subsection \<open>Legacy tactics and ML bindings\<close>
  1969 
  1970 ML \<open>
  1971   (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  1972   local
  1973     fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
  1974       | wrong_prem (Bound _) = true
  1975       | wrong_prem _ = false;
  1976     val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  1977     fun smp i = funpow i (fn m => filter_right ([spec] RL m)) [mp];
  1978   in
  1979     fun smp_tac ctxt j = EVERY' [dresolve_tac ctxt (smp j), assume_tac ctxt];
  1980   end;
  1981 
  1982   local
  1983     val nnf_ss =
  1984       simpset_of (put_simpset HOL_basic_ss @{context} addsimps @{thms simp_thms nnf_simps});
  1985   in
  1986     fun nnf_conv ctxt = Simplifier.rewrite (put_simpset nnf_ss ctxt);
  1987   end
  1988 \<close>
  1989 
  1990 hide_const (open) eq equal
  1991 
  1992 end