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