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