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