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