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