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