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