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