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