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