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