src/HOL/HOL.thy
author nipkow
Wed Dec 05 11:05:23 2012 +0100 (2012-12-05)
changeset 50360 628b37b9e8a2
parent 49339 d1fcb4de8349
child 51021 1cf4faed8b22
permissions -rw-r--r--
\<noteq> now has the same associativity as ~= and =
     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 ML_file "Tools/hologic.ML"
   703 
   704 
   705 subsubsection {* Atomizing meta-level connectives *}
   706 
   707 axiomatization where
   708   eq_reflection: "x = y \<Longrightarrow> x \<equiv> y" (*admissible axiom*)
   709 
   710 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
   711 proof
   712   assume "!!x. P x"
   713   then show "ALL x. P x" ..
   714 next
   715   assume "ALL x. P x"
   716   then show "!!x. P x" by (rule allE)
   717 qed
   718 
   719 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
   720 proof
   721   assume r: "A ==> B"
   722   show "A --> B" by (rule impI) (rule r)
   723 next
   724   assume "A --> B" and A
   725   then show B by (rule mp)
   726 qed
   727 
   728 lemma atomize_not: "(A ==> False) == Trueprop (~A)"
   729 proof
   730   assume r: "A ==> False"
   731   show "~A" by (rule notI) (rule r)
   732 next
   733   assume "~A" and A
   734   then show False by (rule notE)
   735 qed
   736 
   737 lemma atomize_eq [atomize, code]: "(x == y) == Trueprop (x = y)"
   738 proof
   739   assume "x == y"
   740   show "x = y" by (unfold `x == y`) (rule refl)
   741 next
   742   assume "x = y"
   743   then show "x == y" by (rule eq_reflection)
   744 qed
   745 
   746 lemma atomize_conj [atomize]: "(A &&& B) == Trueprop (A & B)"
   747 proof
   748   assume conj: "A &&& B"
   749   show "A & B"
   750   proof (rule conjI)
   751     from conj show A by (rule conjunctionD1)
   752     from conj show B by (rule conjunctionD2)
   753   qed
   754 next
   755   assume conj: "A & B"
   756   show "A &&& B"
   757   proof -
   758     from conj show A ..
   759     from conj show B ..
   760   qed
   761 qed
   762 
   763 lemmas [symmetric, rulify] = atomize_all atomize_imp
   764   and [symmetric, defn] = atomize_all atomize_imp atomize_eq
   765 
   766 
   767 subsubsection {* Atomizing elimination rules *}
   768 
   769 setup AtomizeElim.setup
   770 
   771 lemma atomize_exL[atomize_elim]: "(!!x. P x ==> Q) == ((EX x. P x) ==> Q)"
   772   by rule iprover+
   773 
   774 lemma atomize_conjL[atomize_elim]: "(A ==> B ==> C) == (A & B ==> C)"
   775   by rule iprover+
   776 
   777 lemma atomize_disjL[atomize_elim]: "((A ==> C) ==> (B ==> C) ==> C) == ((A | B ==> C) ==> C)"
   778   by rule iprover+
   779 
   780 lemma atomize_elimL[atomize_elim]: "(!!B. (A ==> B) ==> B) == Trueprop A" ..
   781 
   782 
   783 subsection {* Package setup *}
   784 
   785 subsubsection {* Sledgehammer setup *}
   786 
   787 text {*
   788 Theorems blacklisted to Sledgehammer. These theorems typically produce clauses
   789 that are prolific (match too many equality or membership literals) and relate to
   790 seldom-used facts. Some duplicate other rules.
   791 *}
   792 
   793 ML {*
   794 structure No_ATPs = Named_Thms
   795 (
   796   val name = @{binding no_atp}
   797   val description = "theorems that should be filtered out by Sledgehammer"
   798 )
   799 *}
   800 
   801 setup {* No_ATPs.setup *}
   802 
   803 
   804 subsubsection {* Classical Reasoner setup *}
   805 
   806 lemma imp_elim: "P --> Q ==> (~ R ==> P) ==> (Q ==> R) ==> R"
   807   by (rule classical) iprover
   808 
   809 lemma swap: "~ P ==> (~ R ==> P) ==> R"
   810   by (rule classical) iprover
   811 
   812 lemma thin_refl:
   813   "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
   814 
   815 ML {*
   816 structure Hypsubst = Hypsubst
   817 (
   818   val dest_eq = HOLogic.dest_eq
   819   val dest_Trueprop = HOLogic.dest_Trueprop
   820   val dest_imp = HOLogic.dest_imp
   821   val eq_reflection = @{thm eq_reflection}
   822   val rev_eq_reflection = @{thm meta_eq_to_obj_eq}
   823   val imp_intr = @{thm impI}
   824   val rev_mp = @{thm rev_mp}
   825   val subst = @{thm subst}
   826   val sym = @{thm sym}
   827   val thin_refl = @{thm thin_refl};
   828 );
   829 open Hypsubst;
   830 
   831 structure Classical = Classical
   832 (
   833   val imp_elim = @{thm imp_elim}
   834   val not_elim = @{thm notE}
   835   val swap = @{thm swap}
   836   val classical = @{thm classical}
   837   val sizef = Drule.size_of_thm
   838   val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
   839 );
   840 
   841 structure Basic_Classical: BASIC_CLASSICAL = Classical; 
   842 open Basic_Classical;
   843 *}
   844 
   845 setup {*
   846   ML_Antiquote.value @{binding claset}
   847     (Scan.succeed "Classical.claset_of ML_context")
   848 *}
   849 
   850 setup Classical.setup
   851 
   852 setup {*
   853 let
   854   fun non_bool_eq (@{const_name HOL.eq}, Type (_, [T, _])) = T <> @{typ bool}
   855     | non_bool_eq _ = false;
   856   val hyp_subst_tac' =
   857     SUBGOAL (fn (goal, i) =>
   858       if Term.exists_Const non_bool_eq goal
   859       then Hypsubst.hyp_subst_tac i
   860       else no_tac);
   861 in
   862   Hypsubst.hypsubst_setup
   863   (*prevent substitution on bool*)
   864   #> Context_Rules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac)
   865 end
   866 *}
   867 
   868 declare iffI [intro!]
   869   and notI [intro!]
   870   and impI [intro!]
   871   and disjCI [intro!]
   872   and conjI [intro!]
   873   and TrueI [intro!]
   874   and refl [intro!]
   875 
   876 declare iffCE [elim!]
   877   and FalseE [elim!]
   878   and impCE [elim!]
   879   and disjE [elim!]
   880   and conjE [elim!]
   881 
   882 declare ex_ex1I [intro!]
   883   and allI [intro!]
   884   and the_equality [intro]
   885   and exI [intro]
   886 
   887 declare exE [elim!]
   888   allE [elim]
   889 
   890 ML {* val HOL_cs = @{claset} *}
   891 
   892 lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
   893   apply (erule swap)
   894   apply (erule (1) meta_mp)
   895   done
   896 
   897 declare ex_ex1I [rule del, intro! 2]
   898   and ex1I [intro]
   899 
   900 declare ext [intro]
   901 
   902 lemmas [intro?] = ext
   903   and [elim?] = ex1_implies_ex
   904 
   905 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
   906 lemma alt_ex1E [elim!]:
   907   assumes major: "\<exists>!x. P x"
   908       and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
   909   shows R
   910 apply (rule ex1E [OF major])
   911 apply (rule prem)
   912 apply (tactic {* ares_tac @{thms allI} 1 *})+
   913 apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
   914 apply iprover
   915 done
   916 
   917 ML {*
   918   structure Blast = Blast
   919   (
   920     structure Classical = Classical
   921     val Trueprop_const = dest_Const @{const Trueprop}
   922     val equality_name = @{const_name HOL.eq}
   923     val not_name = @{const_name Not}
   924     val notE = @{thm notE}
   925     val ccontr = @{thm ccontr}
   926     val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   927   );
   928   val blast_tac = Blast.blast_tac;
   929 *}
   930 
   931 setup Blast.setup
   932 
   933 
   934 subsubsection {* Simplifier *}
   935 
   936 lemma eta_contract_eq: "(%s. f s) = f" ..
   937 
   938 lemma simp_thms:
   939   shows not_not: "(~ ~ P) = P"
   940   and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
   941   and
   942     "(P ~= Q) = (P = (~Q))"
   943     "(P | ~P) = True"    "(~P | P) = True"
   944     "(x = x) = True"
   945   and not_True_eq_False [code]: "(\<not> True) = False"
   946   and not_False_eq_True [code]: "(\<not> False) = True"
   947   and
   948     "(~P) ~= P"  "P ~= (~P)"
   949     "(True=P) = P"
   950   and eq_True: "(P = True) = P"
   951   and "(False=P) = (~P)"
   952   and eq_False: "(P = False) = (\<not> P)"
   953   and
   954     "(True --> P) = P"  "(False --> P) = True"
   955     "(P --> True) = True"  "(P --> P) = True"
   956     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
   957     "(P & True) = P"  "(True & P) = P"
   958     "(P & False) = False"  "(False & P) = False"
   959     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
   960     "(P & ~P) = False"    "(~P & P) = False"
   961     "(P | True) = True"  "(True | P) = True"
   962     "(P | False) = P"  "(False | P) = P"
   963     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
   964     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
   965   and
   966     "!!P. (EX x. x=t & P(x)) = P(t)"
   967     "!!P. (EX x. t=x & P(x)) = P(t)"
   968     "!!P. (ALL x. x=t --> P(x)) = P(t)"
   969     "!!P. (ALL x. t=x --> P(x)) = P(t)"
   970   by (blast, blast, blast, blast, blast, iprover+)
   971 
   972 lemma disj_absorb: "(A | A) = A"
   973   by blast
   974 
   975 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
   976   by blast
   977 
   978 lemma conj_absorb: "(A & A) = A"
   979   by blast
   980 
   981 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
   982   by blast
   983 
   984 lemma eq_ac:
   985   shows eq_commute: "(a=b) = (b=a)"
   986     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
   987     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
   988 lemma neq_commute: "(a~=b) = (b~=a)" by iprover
   989 
   990 lemma conj_comms:
   991   shows conj_commute: "(P&Q) = (Q&P)"
   992     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
   993 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
   994 
   995 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
   996 
   997 lemma disj_comms:
   998   shows disj_commute: "(P|Q) = (Q|P)"
   999     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
  1000 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
  1001 
  1002 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
  1003 
  1004 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
  1005 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
  1006 
  1007 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
  1008 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
  1009 
  1010 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
  1011 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
  1012 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
  1013 
  1014 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
  1015 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
  1016 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
  1017 
  1018 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
  1019 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
  1020 
  1021 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
  1022   by iprover
  1023 
  1024 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
  1025 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
  1026 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
  1027 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
  1028 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
  1029 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
  1030   by blast
  1031 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
  1032 
  1033 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
  1034 
  1035 
  1036 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
  1037   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
  1038   -- {* cases boil down to the same thing. *}
  1039   by blast
  1040 
  1041 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
  1042 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
  1043 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
  1044 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
  1045 lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
  1046 
  1047 declare All_def [no_atp]
  1048 
  1049 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
  1050 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
  1051 
  1052 text {*
  1053   \medskip The @{text "&"} congruence rule: not included by default!
  1054   May slow rewrite proofs down by as much as 50\% *}
  1055 
  1056 lemma conj_cong:
  1057     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
  1058   by iprover
  1059 
  1060 lemma rev_conj_cong:
  1061     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
  1062   by iprover
  1063 
  1064 text {* The @{text "|"} congruence rule: not included by default! *}
  1065 
  1066 lemma disj_cong:
  1067     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
  1068   by blast
  1069 
  1070 
  1071 text {* \medskip if-then-else rules *}
  1072 
  1073 lemma if_True [code]: "(if True then x else y) = x"
  1074   by (unfold If_def) blast
  1075 
  1076 lemma if_False [code]: "(if False then x else y) = y"
  1077   by (unfold If_def) blast
  1078 
  1079 lemma if_P: "P ==> (if P then x else y) = x"
  1080   by (unfold If_def) blast
  1081 
  1082 lemma if_not_P: "~P ==> (if P then x else y) = y"
  1083   by (unfold If_def) blast
  1084 
  1085 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
  1086   apply (rule case_split [of Q])
  1087    apply (simplesubst if_P)
  1088     prefer 3 apply (simplesubst if_not_P, blast+)
  1089   done
  1090 
  1091 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
  1092 by (simplesubst split_if, blast)
  1093 
  1094 lemmas if_splits [no_atp] = split_if split_if_asm
  1095 
  1096 lemma if_cancel: "(if c then x else x) = x"
  1097 by (simplesubst split_if, blast)
  1098 
  1099 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1100 by (simplesubst split_if, blast)
  1101 
  1102 lemma if_bool_eq_conj:
  1103 "(if P then Q else R) = ((P-->Q) & (~P-->R))"
  1104   -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
  1105   by (rule split_if)
  1106 
  1107 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
  1108   -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
  1109   apply (simplesubst split_if, blast)
  1110   done
  1111 
  1112 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
  1113 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
  1114 
  1115 text {* \medskip let rules for simproc *}
  1116 
  1117 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
  1118   by (unfold Let_def)
  1119 
  1120 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
  1121   by (unfold Let_def)
  1122 
  1123 text {*
  1124   The following copy of the implication operator is useful for
  1125   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1126   its premise.
  1127 *}
  1128 
  1129 definition simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1) where
  1130   "simp_implies \<equiv> op ==>"
  1131 
  1132 lemma simp_impliesI:
  1133   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1134   shows "PROP P =simp=> PROP Q"
  1135   apply (unfold simp_implies_def)
  1136   apply (rule PQ)
  1137   apply assumption
  1138   done
  1139 
  1140 lemma simp_impliesE:
  1141   assumes PQ: "PROP P =simp=> PROP Q"
  1142   and P: "PROP P"
  1143   and QR: "PROP Q \<Longrightarrow> PROP R"
  1144   shows "PROP R"
  1145   apply (rule QR)
  1146   apply (rule PQ [unfolded simp_implies_def])
  1147   apply (rule P)
  1148   done
  1149 
  1150 lemma simp_implies_cong:
  1151   assumes PP' :"PROP P == PROP P'"
  1152   and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
  1153   shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
  1154 proof (unfold simp_implies_def, rule equal_intr_rule)
  1155   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1156   and P': "PROP P'"
  1157   from PP' [symmetric] and P' have "PROP P"
  1158     by (rule equal_elim_rule1)
  1159   then have "PROP Q" by (rule PQ)
  1160   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1161 next
  1162   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1163   and P: "PROP P"
  1164   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1165   then have "PROP Q'" by (rule P'Q')
  1166   with P'QQ' [OF P', symmetric] show "PROP Q"
  1167     by (rule equal_elim_rule1)
  1168 qed
  1169 
  1170 lemma uncurry:
  1171   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1172   shows "P \<and> Q \<longrightarrow> R"
  1173   using assms by blast
  1174 
  1175 lemma iff_allI:
  1176   assumes "\<And>x. P x = Q x"
  1177   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1178   using assms by blast
  1179 
  1180 lemma iff_exI:
  1181   assumes "\<And>x. P x = Q x"
  1182   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1183   using assms by blast
  1184 
  1185 lemma all_comm:
  1186   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1187   by blast
  1188 
  1189 lemma ex_comm:
  1190   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1191   by blast
  1192 
  1193 ML_file "Tools/simpdata.ML"
  1194 ML {* open Simpdata *}
  1195 
  1196 setup {* Simplifier.map_simpset_global (K HOL_basic_ss) *}
  1197 
  1198 simproc_setup defined_Ex ("EX x. P x") = {* fn _ => Quantifier1.rearrange_ex *}
  1199 simproc_setup defined_All ("ALL x. P x") = {* fn _ => Quantifier1.rearrange_all *}
  1200 
  1201 setup {*
  1202   Simplifier.method_setup Splitter.split_modifiers
  1203   #> Splitter.setup
  1204   #> clasimp_setup
  1205   #> EqSubst.setup
  1206 *}
  1207 
  1208 text {* Simproc for proving @{text "(y = x) == False"} from premise @{text "~(x = y)"}: *}
  1209 
  1210 simproc_setup neq ("x = y") = {* fn _ =>
  1211 let
  1212   val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
  1213   fun is_neq eq lhs rhs thm =
  1214     (case Thm.prop_of thm of
  1215       _ $ (Not $ (eq' $ l' $ r')) =>
  1216         Not = HOLogic.Not andalso eq' = eq andalso
  1217         r' aconv lhs andalso l' aconv rhs
  1218     | _ => false);
  1219   fun proc ss ct =
  1220     (case Thm.term_of ct of
  1221       eq $ lhs $ rhs =>
  1222         (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of ss) of
  1223           SOME thm => SOME (thm RS neq_to_EQ_False)
  1224         | NONE => NONE)
  1225      | _ => NONE);
  1226 in proc end;
  1227 *}
  1228 
  1229 simproc_setup let_simp ("Let x f") = {*
  1230 let
  1231   val (f_Let_unfold, x_Let_unfold) =
  1232     let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_unfold}
  1233     in (cterm_of @{theory} f, cterm_of @{theory} x) end
  1234   val (f_Let_folded, x_Let_folded) =
  1235     let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_folded}
  1236     in (cterm_of @{theory} f, cterm_of @{theory} x) end;
  1237   val g_Let_folded =
  1238     let val [(_ $ _ $ (g $ _))] = prems_of @{thm Let_folded}
  1239     in cterm_of @{theory} g end;
  1240   fun count_loose (Bound i) k = if i >= k then 1 else 0
  1241     | count_loose (s $ t) k = count_loose s k + count_loose t k
  1242     | count_loose (Abs (_, _, t)) k = count_loose  t (k + 1)
  1243     | count_loose _ _ = 0;
  1244   fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
  1245    case t
  1246     of Abs (_, _, t') => count_loose t' 0 <= 1
  1247      | _ => true;
  1248 in fn _ => fn ss => fn ct => if is_trivial_let (Thm.term_of ct)
  1249   then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
  1250   else let (*Norbert Schirmer's case*)
  1251     val ctxt = Simplifier.the_context ss;
  1252     val thy = Proof_Context.theory_of ctxt;
  1253     val t = Thm.term_of ct;
  1254     val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
  1255   in Option.map (hd o Variable.export ctxt' ctxt o single)
  1256     (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
  1257       if is_Free x orelse is_Bound x orelse is_Const x
  1258       then SOME @{thm Let_def}
  1259       else
  1260         let
  1261           val n = case f of (Abs (x, _, _)) => x | _ => "x";
  1262           val cx = cterm_of thy x;
  1263           val {T = xT, ...} = rep_cterm cx;
  1264           val cf = cterm_of thy f;
  1265           val fx_g = Simplifier.rewrite ss (Thm.apply cf cx);
  1266           val (_ $ _ $ g) = prop_of fx_g;
  1267           val g' = abstract_over (x,g);
  1268         in (if (g aconv g')
  1269              then
  1270                 let
  1271                   val rl =
  1272                     cterm_instantiate [(f_Let_unfold, cf), (x_Let_unfold, cx)] @{thm Let_unfold};
  1273                 in SOME (rl OF [fx_g]) end
  1274              else if Term.betapply (f, x) aconv g then NONE (*avoid identity conversion*)
  1275              else let
  1276                    val abs_g'= Abs (n,xT,g');
  1277                    val g'x = abs_g'$x;
  1278                    val g_g'x = Thm.symmetric (Thm.beta_conversion false (cterm_of thy g'x));
  1279                    val rl = cterm_instantiate
  1280                              [(f_Let_folded, cterm_of thy f), (x_Let_folded, cx),
  1281                               (g_Let_folded, cterm_of thy abs_g')]
  1282                              @{thm Let_folded};
  1283                  in SOME (rl OF [Thm.transitive fx_g g_g'x])
  1284                  end)
  1285         end
  1286     | _ => NONE)
  1287   end
  1288 end *}
  1289 
  1290 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
  1291 proof
  1292   assume "True \<Longrightarrow> PROP P"
  1293   from this [OF TrueI] show "PROP P" .
  1294 next
  1295   assume "PROP P"
  1296   then show "PROP P" .
  1297 qed
  1298 
  1299 lemma ex_simps:
  1300   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
  1301   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
  1302   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
  1303   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
  1304   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
  1305   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
  1306   -- {* Miniscoping: pushing in existential quantifiers. *}
  1307   by (iprover | blast)+
  1308 
  1309 lemma all_simps:
  1310   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
  1311   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
  1312   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
  1313   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
  1314   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
  1315   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
  1316   -- {* Miniscoping: pushing in universal quantifiers. *}
  1317   by (iprover | blast)+
  1318 
  1319 lemmas [simp] =
  1320   triv_forall_equality (*prunes params*)
  1321   True_implies_equals  (*prune asms `True'*)
  1322   if_True
  1323   if_False
  1324   if_cancel
  1325   if_eq_cancel
  1326   imp_disjL
  1327   (*In general it seems wrong to add distributive laws by default: they
  1328     might cause exponential blow-up.  But imp_disjL has been in for a while
  1329     and cannot be removed without affecting existing proofs.  Moreover,
  1330     rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
  1331     grounds that it allows simplification of R in the two cases.*)
  1332   conj_assoc
  1333   disj_assoc
  1334   de_Morgan_conj
  1335   de_Morgan_disj
  1336   imp_disj1
  1337   imp_disj2
  1338   not_imp
  1339   disj_not1
  1340   not_all
  1341   not_ex
  1342   cases_simp
  1343   the_eq_trivial
  1344   the_sym_eq_trivial
  1345   ex_simps
  1346   all_simps
  1347   simp_thms
  1348 
  1349 lemmas [cong] = imp_cong simp_implies_cong
  1350 lemmas [split] = split_if
  1351 
  1352 ML {* val HOL_ss = @{simpset} *}
  1353 
  1354 text {* Simplifies x assuming c and y assuming ~c *}
  1355 lemma if_cong:
  1356   assumes "b = c"
  1357       and "c \<Longrightarrow> x = u"
  1358       and "\<not> c \<Longrightarrow> y = v"
  1359   shows "(if b then x else y) = (if c then u else v)"
  1360   using assms by simp
  1361 
  1362 text {* Prevents simplification of x and y:
  1363   faster and allows the execution of functional programs. *}
  1364 lemma if_weak_cong [cong]:
  1365   assumes "b = c"
  1366   shows "(if b then x else y) = (if c then x else y)"
  1367   using assms by (rule arg_cong)
  1368 
  1369 text {* Prevents simplification of t: much faster *}
  1370 lemma let_weak_cong:
  1371   assumes "a = b"
  1372   shows "(let x = a in t x) = (let x = b in t x)"
  1373   using assms by (rule arg_cong)
  1374 
  1375 text {* To tidy up the result of a simproc.  Only the RHS will be simplified. *}
  1376 lemma eq_cong2:
  1377   assumes "u = u'"
  1378   shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
  1379   using assms by simp
  1380 
  1381 lemma if_distrib:
  1382   "f (if c then x else y) = (if c then f x else f y)"
  1383   by simp
  1384 
  1385 text{*As a simplification rule, it replaces all function equalities by
  1386   first-order equalities.*}
  1387 lemma fun_eq_iff: "f = g \<longleftrightarrow> (\<forall>x. f x = g x)"
  1388   by auto
  1389 
  1390 
  1391 subsubsection {* Generic cases and induction *}
  1392 
  1393 text {* Rule projections: *}
  1394 
  1395 ML {*
  1396 structure Project_Rule = Project_Rule
  1397 (
  1398   val conjunct1 = @{thm conjunct1}
  1399   val conjunct2 = @{thm conjunct2}
  1400   val mp = @{thm mp}
  1401 )
  1402 *}
  1403 
  1404 definition induct_forall where
  1405   "induct_forall P == \<forall>x. P x"
  1406 
  1407 definition induct_implies where
  1408   "induct_implies A B == A \<longrightarrow> B"
  1409 
  1410 definition induct_equal where
  1411   "induct_equal x y == x = y"
  1412 
  1413 definition induct_conj where
  1414   "induct_conj A B == A \<and> B"
  1415 
  1416 definition induct_true where
  1417   "induct_true == True"
  1418 
  1419 definition induct_false where
  1420   "induct_false == False"
  1421 
  1422 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
  1423   by (unfold atomize_all induct_forall_def)
  1424 
  1425 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
  1426   by (unfold atomize_imp induct_implies_def)
  1427 
  1428 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
  1429   by (unfold atomize_eq induct_equal_def)
  1430 
  1431 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
  1432   by (unfold atomize_conj induct_conj_def)
  1433 
  1434 lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
  1435 lemmas induct_atomize = induct_atomize' induct_equal_eq
  1436 lemmas induct_rulify' [symmetric] = induct_atomize'
  1437 lemmas induct_rulify [symmetric] = induct_atomize
  1438 lemmas induct_rulify_fallback =
  1439   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
  1440   induct_true_def induct_false_def
  1441 
  1442 
  1443 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
  1444     induct_conj (induct_forall A) (induct_forall B)"
  1445   by (unfold induct_forall_def induct_conj_def) iprover
  1446 
  1447 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
  1448     induct_conj (induct_implies C A) (induct_implies C B)"
  1449   by (unfold induct_implies_def induct_conj_def) iprover
  1450 
  1451 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
  1452 proof
  1453   assume r: "induct_conj A B ==> PROP C" and A B
  1454   show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
  1455 next
  1456   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
  1457   show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
  1458 qed
  1459 
  1460 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1461 
  1462 lemma induct_trueI: "induct_true"
  1463   by (simp add: induct_true_def)
  1464 
  1465 text {* Method setup. *}
  1466 
  1467 ML {*
  1468 structure Induct = Induct
  1469 (
  1470   val cases_default = @{thm case_split}
  1471   val atomize = @{thms induct_atomize}
  1472   val rulify = @{thms induct_rulify'}
  1473   val rulify_fallback = @{thms induct_rulify_fallback}
  1474   val equal_def = @{thm induct_equal_def}
  1475   fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
  1476     | dest_def _ = NONE
  1477   val trivial_tac = match_tac @{thms induct_trueI}
  1478 )
  1479 *}
  1480 
  1481 ML_file "~~/src/Tools/induction.ML"
  1482 
  1483 setup {*
  1484   Induct.setup #> Induction.setup #>
  1485   Context.theory_map (Induct.map_simpset (fn ss => ss
  1486     addsimprocs
  1487       [Simplifier.simproc_global @{theory} "swap_induct_false"
  1488          ["induct_false ==> PROP P ==> PROP Q"]
  1489          (fn _ => fn _ =>
  1490             (fn _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
  1491                   if P <> Q then SOME Drule.swap_prems_eq else NONE
  1492               | _ => NONE)),
  1493        Simplifier.simproc_global @{theory} "induct_equal_conj_curry"
  1494          ["induct_conj P Q ==> PROP R"]
  1495          (fn _ => fn _ =>
  1496             (fn _ $ (_ $ P) $ _ =>
  1497                 let
  1498                   fun is_conj (@{const induct_conj} $ P $ Q) =
  1499                         is_conj P andalso is_conj Q
  1500                     | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
  1501                     | is_conj @{const induct_true} = true
  1502                     | is_conj @{const induct_false} = true
  1503                     | is_conj _ = false
  1504                 in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
  1505               | _ => NONE))]
  1506     |> Simplifier.set_mksimps (fn ss => Simpdata.mksimps Simpdata.mksimps_pairs ss #>
  1507       map (Simplifier.rewrite_rule (map Thm.symmetric
  1508         @{thms induct_rulify_fallback})))))
  1509 *}
  1510 
  1511 text {* Pre-simplification of induction and cases rules *}
  1512 
  1513 lemma [induct_simp]: "(!!x. induct_equal x t ==> PROP P x) == PROP P t"
  1514   unfolding induct_equal_def
  1515 proof
  1516   assume R: "!!x. x = t ==> PROP P x"
  1517   show "PROP P t" by (rule R [OF refl])
  1518 next
  1519   fix x assume "PROP P t" "x = t"
  1520   then show "PROP P x" by simp
  1521 qed
  1522 
  1523 lemma [induct_simp]: "(!!x. induct_equal t x ==> PROP P x) == PROP P t"
  1524   unfolding induct_equal_def
  1525 proof
  1526   assume R: "!!x. t = x ==> PROP P x"
  1527   show "PROP P t" by (rule R [OF refl])
  1528 next
  1529   fix x assume "PROP P t" "t = x"
  1530   then show "PROP P x" by simp
  1531 qed
  1532 
  1533 lemma [induct_simp]: "(induct_false ==> P) == Trueprop induct_true"
  1534   unfolding induct_false_def induct_true_def
  1535   by (iprover intro: equal_intr_rule)
  1536 
  1537 lemma [induct_simp]: "(induct_true ==> PROP P) == PROP P"
  1538   unfolding induct_true_def
  1539 proof
  1540   assume R: "True \<Longrightarrow> PROP P"
  1541   from TrueI show "PROP P" by (rule R)
  1542 next
  1543   assume "PROP P"
  1544   then show "PROP P" .
  1545 qed
  1546 
  1547 lemma [induct_simp]: "(PROP P ==> induct_true) == Trueprop induct_true"
  1548   unfolding induct_true_def
  1549   by (iprover intro: equal_intr_rule)
  1550 
  1551 lemma [induct_simp]: "(!!x. induct_true) == Trueprop induct_true"
  1552   unfolding induct_true_def
  1553   by (iprover intro: equal_intr_rule)
  1554 
  1555 lemma [induct_simp]: "induct_implies induct_true P == P"
  1556   by (simp add: induct_implies_def induct_true_def)
  1557 
  1558 lemma [induct_simp]: "(x = x) = True" 
  1559   by (rule simp_thms)
  1560 
  1561 hide_const induct_forall induct_implies induct_equal induct_conj induct_true induct_false
  1562 
  1563 ML_file "~~/src/Tools/induct_tacs.ML"
  1564 setup Induct_Tacs.setup
  1565 
  1566 
  1567 subsubsection {* Coherent logic *}
  1568 
  1569 ML {*
  1570 structure Coherent = Coherent
  1571 (
  1572   val atomize_elimL = @{thm atomize_elimL}
  1573   val atomize_exL = @{thm atomize_exL}
  1574   val atomize_conjL = @{thm atomize_conjL}
  1575   val atomize_disjL = @{thm atomize_disjL}
  1576   val operator_names =
  1577     [@{const_name HOL.disj}, @{const_name HOL.conj}, @{const_name Ex}]
  1578 );
  1579 *}
  1580 
  1581 setup Coherent.setup
  1582 
  1583 
  1584 subsubsection {* Reorienting equalities *}
  1585 
  1586 ML {*
  1587 signature REORIENT_PROC =
  1588 sig
  1589   val add : (term -> bool) -> theory -> theory
  1590   val proc : morphism -> simpset -> cterm -> thm option
  1591 end;
  1592 
  1593 structure Reorient_Proc : REORIENT_PROC =
  1594 struct
  1595   structure Data = Theory_Data
  1596   (
  1597     type T = ((term -> bool) * stamp) list;
  1598     val empty = [];
  1599     val extend = I;
  1600     fun merge data : T = Library.merge (eq_snd op =) data;
  1601   );
  1602   fun add m = Data.map (cons (m, stamp ()));
  1603   fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
  1604 
  1605   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1606   fun proc phi ss ct =
  1607     let
  1608       val ctxt = Simplifier.the_context ss;
  1609       val thy = Proof_Context.theory_of ctxt;
  1610     in
  1611       case Thm.term_of ct of
  1612         (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
  1613       | _ => NONE
  1614     end;
  1615 end;
  1616 *}
  1617 
  1618 
  1619 subsection {* Other simple lemmas and lemma duplicates *}
  1620 
  1621 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
  1622   by blast+
  1623 
  1624 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
  1625   apply (rule iffI)
  1626   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
  1627   apply (fast dest!: theI')
  1628   apply (fast intro: the1_equality [symmetric])
  1629   apply (erule ex1E)
  1630   apply (rule allI)
  1631   apply (rule ex1I)
  1632   apply (erule spec)
  1633   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
  1634   apply (erule impE)
  1635   apply (rule allI)
  1636   apply (case_tac "xa = x")
  1637   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1638   done
  1639 
  1640 lemmas eq_sym_conv = eq_commute
  1641 
  1642 lemma nnf_simps:
  1643   "(\<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)" 
  1644   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))" 
  1645   "(\<not> \<not>(P)) = P"
  1646 by blast+
  1647 
  1648 subsection {* Basic ML bindings *}
  1649 
  1650 ML {*
  1651 val FalseE = @{thm FalseE}
  1652 val Let_def = @{thm Let_def}
  1653 val TrueI = @{thm TrueI}
  1654 val allE = @{thm allE}
  1655 val allI = @{thm allI}
  1656 val all_dupE = @{thm all_dupE}
  1657 val arg_cong = @{thm arg_cong}
  1658 val box_equals = @{thm box_equals}
  1659 val ccontr = @{thm ccontr}
  1660 val classical = @{thm classical}
  1661 val conjE = @{thm conjE}
  1662 val conjI = @{thm conjI}
  1663 val conjunct1 = @{thm conjunct1}
  1664 val conjunct2 = @{thm conjunct2}
  1665 val disjCI = @{thm disjCI}
  1666 val disjE = @{thm disjE}
  1667 val disjI1 = @{thm disjI1}
  1668 val disjI2 = @{thm disjI2}
  1669 val eq_reflection = @{thm eq_reflection}
  1670 val ex1E = @{thm ex1E}
  1671 val ex1I = @{thm ex1I}
  1672 val ex1_implies_ex = @{thm ex1_implies_ex}
  1673 val exE = @{thm exE}
  1674 val exI = @{thm exI}
  1675 val excluded_middle = @{thm excluded_middle}
  1676 val ext = @{thm ext}
  1677 val fun_cong = @{thm fun_cong}
  1678 val iffD1 = @{thm iffD1}
  1679 val iffD2 = @{thm iffD2}
  1680 val iffI = @{thm iffI}
  1681 val impE = @{thm impE}
  1682 val impI = @{thm impI}
  1683 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1684 val mp = @{thm mp}
  1685 val notE = @{thm notE}
  1686 val notI = @{thm notI}
  1687 val not_all = @{thm not_all}
  1688 val not_ex = @{thm not_ex}
  1689 val not_iff = @{thm not_iff}
  1690 val not_not = @{thm not_not}
  1691 val not_sym = @{thm not_sym}
  1692 val refl = @{thm refl}
  1693 val rev_mp = @{thm rev_mp}
  1694 val spec = @{thm spec}
  1695 val ssubst = @{thm ssubst}
  1696 val subst = @{thm subst}
  1697 val sym = @{thm sym}
  1698 val trans = @{thm trans}
  1699 *}
  1700 
  1701 ML_file "Tools/cnf_funcs.ML"
  1702 
  1703 subsection {* Code generator setup *}
  1704 
  1705 subsubsection {* Generic code generator preprocessor setup *}
  1706 
  1707 setup {*
  1708   Code_Preproc.map_pre (K HOL_basic_ss)
  1709   #> Code_Preproc.map_post (K HOL_basic_ss)
  1710   #> Code_Simp.map_ss (K HOL_basic_ss)
  1711 *}
  1712 
  1713 subsubsection {* Equality *}
  1714 
  1715 class equal =
  1716   fixes equal :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1717   assumes equal_eq: "equal x y \<longleftrightarrow> x = y"
  1718 begin
  1719 
  1720 lemma equal: "equal = (op =)"
  1721   by (rule ext equal_eq)+
  1722 
  1723 lemma equal_refl: "equal x x \<longleftrightarrow> True"
  1724   unfolding equal by rule+
  1725 
  1726 lemma eq_equal: "(op =) \<equiv> equal"
  1727   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule equal_eq)
  1728 
  1729 end
  1730 
  1731 declare eq_equal [symmetric, code_post]
  1732 declare eq_equal [code]
  1733 
  1734 setup {*
  1735   Code_Preproc.map_pre (fn simpset =>
  1736     simpset addsimprocs [Simplifier.simproc_global_i @{theory} "equal" [@{term HOL.eq}]
  1737       (fn thy => fn _ =>
  1738         fn Const (_, Type ("fun", [Type _, _])) => SOME @{thm eq_equal} | _ => NONE)])
  1739 *}
  1740 
  1741 
  1742 subsubsection {* Generic code generator foundation *}
  1743 
  1744 text {* Datatype @{typ bool} *}
  1745 
  1746 code_datatype True False
  1747 
  1748 lemma [code]:
  1749   shows "False \<and> P \<longleftrightarrow> False"
  1750     and "True \<and> P \<longleftrightarrow> P"
  1751     and "P \<and> False \<longleftrightarrow> False"
  1752     and "P \<and> True \<longleftrightarrow> P" by simp_all
  1753 
  1754 lemma [code]:
  1755   shows "False \<or> P \<longleftrightarrow> P"
  1756     and "True \<or> P \<longleftrightarrow> True"
  1757     and "P \<or> False \<longleftrightarrow> P"
  1758     and "P \<or> True \<longleftrightarrow> True" by simp_all
  1759 
  1760 lemma [code]:
  1761   shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
  1762     and "(True \<longrightarrow> P) \<longleftrightarrow> P"
  1763     and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
  1764     and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
  1765 
  1766 text {* More about @{typ prop} *}
  1767 
  1768 lemma [code nbe]:
  1769   shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q" 
  1770     and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
  1771     and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
  1772 
  1773 lemma Trueprop_code [code]:
  1774   "Trueprop True \<equiv> Code_Generator.holds"
  1775   by (auto intro!: equal_intr_rule holds)
  1776 
  1777 declare Trueprop_code [symmetric, code_post]
  1778 
  1779 text {* Equality *}
  1780 
  1781 declare simp_thms(6) [code nbe]
  1782 
  1783 instantiation itself :: (type) equal
  1784 begin
  1785 
  1786 definition equal_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1787   "equal_itself x y \<longleftrightarrow> x = y"
  1788 
  1789 instance proof
  1790 qed (fact equal_itself_def)
  1791 
  1792 end
  1793 
  1794 lemma equal_itself_code [code]:
  1795   "equal TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1796   by (simp add: equal)
  1797 
  1798 setup {*
  1799   Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
  1800 *}
  1801 
  1802 lemma equal_alias_cert: "OFCLASS('a, equal_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> equal)" (is "?ofclass \<equiv> ?equal")
  1803 proof
  1804   assume "PROP ?ofclass"
  1805   show "PROP ?equal"
  1806     by (tactic {* ALLGOALS (rtac (Thm.unconstrainT @{thm eq_equal})) *})
  1807       (fact `PROP ?ofclass`)
  1808 next
  1809   assume "PROP ?equal"
  1810   show "PROP ?ofclass" proof
  1811   qed (simp add: `PROP ?equal`)
  1812 qed
  1813   
  1814 setup {*
  1815   Sign.add_const_constraint (@{const_name equal}, SOME @{typ "'a\<Colon>equal \<Rightarrow> 'a \<Rightarrow> bool"})
  1816 *}
  1817 
  1818 setup {*
  1819   Nbe.add_const_alias @{thm equal_alias_cert}
  1820 *}
  1821 
  1822 text {* Cases *}
  1823 
  1824 lemma Let_case_cert:
  1825   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1826   shows "CASE x \<equiv> f x"
  1827   using assms by simp_all
  1828 
  1829 setup {*
  1830   Code.add_case @{thm Let_case_cert}
  1831   #> Code.add_undefined @{const_name undefined}
  1832 *}
  1833 
  1834 code_abort undefined
  1835 
  1836 
  1837 subsubsection {* Generic code generator target languages *}
  1838 
  1839 text {* type @{typ bool} *}
  1840 
  1841 code_type bool
  1842   (SML "bool")
  1843   (OCaml "bool")
  1844   (Haskell "Bool")
  1845   (Scala "Boolean")
  1846 
  1847 code_const True and False and Not and HOL.conj and HOL.disj and HOL.implies and If 
  1848   (SML "true" and "false" and "not"
  1849     and infixl 1 "andalso" and infixl 0 "orelse"
  1850     and "!(if (_)/ then (_)/ else true)"
  1851     and "!(if (_)/ then (_)/ else (_))")
  1852   (OCaml "true" and "false" and "not"
  1853     and infixl 3 "&&" and infixl 2 "||"
  1854     and "!(if (_)/ then (_)/ else true)"
  1855     and "!(if (_)/ then (_)/ else (_))")
  1856   (Haskell "True" and "False" and "not"
  1857     and infixr 3 "&&" and infixr 2 "||"
  1858     and "!(if (_)/ then (_)/ else True)"
  1859     and "!(if (_)/ then (_)/ else (_))")
  1860   (Scala "true" and "false" and "'! _"
  1861     and infixl 3 "&&" and infixl 1 "||"
  1862     and "!(if ((_))/ (_)/ else true)"
  1863     and "!(if ((_))/ (_)/ else (_))")
  1864 
  1865 code_reserved SML
  1866   bool true false not
  1867 
  1868 code_reserved OCaml
  1869   bool not
  1870 
  1871 code_reserved Scala
  1872   Boolean
  1873 
  1874 code_modulename SML Pure HOL
  1875 code_modulename OCaml Pure HOL
  1876 code_modulename Haskell Pure HOL
  1877 
  1878 text {* using built-in Haskell equality *}
  1879 
  1880 code_class equal
  1881   (Haskell "Eq")
  1882 
  1883 code_const "HOL.equal"
  1884   (Haskell infix 4 "==")
  1885 
  1886 code_const HOL.eq
  1887   (Haskell infix 4 "==")
  1888 
  1889 text {* undefined *}
  1890 
  1891 code_const undefined
  1892   (SML "!(raise/ Fail/ \"undefined\")")
  1893   (OCaml "failwith/ \"undefined\"")
  1894   (Haskell "error/ \"undefined\"")
  1895   (Scala "!sys.error(\"undefined\")")
  1896 
  1897 subsubsection {* Evaluation and normalization by evaluation *}
  1898 
  1899 ML {*
  1900 fun eval_tac ctxt =
  1901   let val conv = Code_Runtime.dynamic_holds_conv (Proof_Context.theory_of ctxt)
  1902   in CONVERSION (Conv.params_conv ~1 (K (Conv.concl_conv ~1 conv)) ctxt) THEN' rtac TrueI end
  1903 *}
  1904 
  1905 method_setup eval = {* Scan.succeed (SIMPLE_METHOD' o eval_tac) *}
  1906   "solve goal by evaluation"
  1907 
  1908 method_setup normalization = {*
  1909   Scan.succeed (fn ctxt =>
  1910     SIMPLE_METHOD'
  1911       (CHANGED_PROP o
  1912         (CONVERSION (Nbe.dynamic_conv (Proof_Context.theory_of ctxt))
  1913           THEN_ALL_NEW (TRY o rtac TrueI))))
  1914 *} "solve goal by normalization"
  1915 
  1916 
  1917 subsection {* Counterexample Search Units *}
  1918 
  1919 subsubsection {* Quickcheck *}
  1920 
  1921 quickcheck_params [size = 5, iterations = 50]
  1922 
  1923 
  1924 subsubsection {* Nitpick setup *}
  1925 
  1926 ML {*
  1927 structure Nitpick_Unfolds = Named_Thms
  1928 (
  1929   val name = @{binding nitpick_unfold}
  1930   val description = "alternative definitions of constants as needed by Nitpick"
  1931 )
  1932 structure Nitpick_Simps = Named_Thms
  1933 (
  1934   val name = @{binding nitpick_simp}
  1935   val description = "equational specification of constants as needed by Nitpick"
  1936 )
  1937 structure Nitpick_Psimps = Named_Thms
  1938 (
  1939   val name = @{binding nitpick_psimp}
  1940   val description = "partial equational specification of constants as needed by Nitpick"
  1941 )
  1942 structure Nitpick_Choice_Specs = Named_Thms
  1943 (
  1944   val name = @{binding nitpick_choice_spec}
  1945   val description = "choice specification of constants as needed by Nitpick"
  1946 )
  1947 *}
  1948 
  1949 setup {*
  1950   Nitpick_Unfolds.setup
  1951   #> Nitpick_Simps.setup
  1952   #> Nitpick_Psimps.setup
  1953   #> Nitpick_Choice_Specs.setup
  1954 *}
  1955 
  1956 declare if_bool_eq_conj [nitpick_unfold, no_atp]
  1957         if_bool_eq_disj [no_atp]
  1958 
  1959 
  1960 subsection {* Preprocessing for the predicate compiler *}
  1961 
  1962 ML {*
  1963 structure Predicate_Compile_Alternative_Defs = Named_Thms
  1964 (
  1965   val name = @{binding code_pred_def}
  1966   val description = "alternative definitions of constants for the Predicate Compiler"
  1967 )
  1968 structure Predicate_Compile_Inline_Defs = Named_Thms
  1969 (
  1970   val name = @{binding code_pred_inline}
  1971   val description = "inlining definitions for the Predicate Compiler"
  1972 )
  1973 structure Predicate_Compile_Simps = Named_Thms
  1974 (
  1975   val name = @{binding code_pred_simp}
  1976   val description = "simplification rules for the optimisations in the Predicate Compiler"
  1977 )
  1978 *}
  1979 
  1980 setup {*
  1981   Predicate_Compile_Alternative_Defs.setup
  1982   #> Predicate_Compile_Inline_Defs.setup
  1983   #> Predicate_Compile_Simps.setup
  1984 *}
  1985 
  1986 
  1987 subsection {* Legacy tactics and ML bindings *}
  1988 
  1989 ML {*
  1990 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
  1991 
  1992 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  1993 local
  1994   fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
  1995     | wrong_prem (Bound _) = true
  1996     | wrong_prem _ = false;
  1997   val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  1998 in
  1999   fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
  2000   fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
  2001 end;
  2002 
  2003 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps @{thms simp_thms nnf_simps});
  2004 *}
  2005 
  2006 hide_const (open) eq equal
  2007 
  2008 end
  2009