src/HOL/HOL.thy
author wenzelm
Sat May 15 21:50:05 2010 +0200 (2010-05-15)
changeset 36945 9bec62c10714
parent 36936 c52d1c130898
child 37264 8b931fb51cc6
permissions -rw-r--r--
less pervasive names from structure Thm;
     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 begin
    33 
    34 setup {* Intuitionistic.method_setup @{binding iprover} *}
    35 
    36 
    37 subsection {* Primitive logic *}
    38 
    39 subsubsection {* Core syntax *}
    40 
    41 classes type
    42 default_sort type
    43 setup {* Object_Logic.add_base_sort @{sort type} *}
    44 
    45 arities
    46   "fun" :: (type, type) type
    47   itself :: (type) type
    48 
    49 global
    50 
    51 typedecl bool
    52 
    53 judgment
    54   Trueprop      :: "bool => prop"                   ("(_)" 5)
    55 
    56 consts
    57   Not           :: "bool => bool"                   ("~ _" [40] 40)
    58   True          :: bool
    59   False         :: bool
    60 
    61   The           :: "('a => bool) => 'a"
    62   All           :: "('a => bool) => bool"           (binder "ALL " 10)
    63   Ex            :: "('a => bool) => bool"           (binder "EX " 10)
    64   Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
    65   Let           :: "['a, 'a => 'b] => 'b"
    66 
    67   "op ="        :: "['a, 'a] => bool"               (infixl "=" 50)
    68   "op &"        :: "[bool, bool] => bool"           (infixr "&" 35)
    69   "op |"        :: "[bool, bool] => bool"           (infixr "|" 30)
    70   "op -->"      :: "[bool, bool] => bool"           (infixr "-->" 25)
    71 
    72 local
    73 
    74 consts
    75   If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" [0, 0, 10] 10)
    76 
    77 
    78 subsubsection {* Additional concrete syntax *}
    79 
    80 notation (output)
    81   "op ="  (infix "=" 50)
    82 
    83 abbreviation
    84   not_equal :: "['a, 'a] => bool"  (infixl "~=" 50) where
    85   "x ~= y == ~ (x = y)"
    86 
    87 notation (output)
    88   not_equal  (infix "~=" 50)
    89 
    90 notation (xsymbols)
    91   Not  ("\<not> _" [40] 40) and
    92   "op &"  (infixr "\<and>" 35) and
    93   "op |"  (infixr "\<or>" 30) and
    94   "op -->"  (infixr "\<longrightarrow>" 25) and
    95   not_equal  (infix "\<noteq>" 50)
    96 
    97 notation (HTML output)
    98   Not  ("\<not> _" [40] 40) and
    99   "op &"  (infixr "\<and>" 35) and
   100   "op |"  (infixr "\<or>" 30) and
   101   not_equal  (infix "\<noteq>" 50)
   102 
   103 abbreviation (iff)
   104   iff :: "[bool, bool] => bool"  (infixr "<->" 25) where
   105   "A <-> B == A = B"
   106 
   107 notation (xsymbols)
   108   iff  (infixr "\<longleftrightarrow>" 25)
   109 
   110 nonterminals
   111   letbinds  letbind
   112   case_syn  cases_syn
   113 
   114 syntax
   115   "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
   116 
   117   "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
   118   ""            :: "letbind => letbinds"                 ("_")
   119   "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
   120   "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" [0, 10] 10)
   121 
   122   "_case_syntax":: "['a, cases_syn] => 'b"               ("(case _ of/ _)" 10)
   123   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ =>/ _)" 10)
   124   ""            :: "case_syn => cases_syn"               ("_")
   125   "_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
   126 
   127 translations
   128   "THE x. P"              == "CONST The (%x. P)"
   129   "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
   130   "let x = a in e"        == "CONST Let a (%x. e)"
   131 
   132 print_translation {*
   133   [(@{const_syntax The}, fn [Abs abs] =>
   134       let val (x, t) = atomic_abs_tr' abs
   135       in Syntax.const @{syntax_const "_The"} $ x $ t end)]
   136 *}  -- {* To avoid eta-contraction of body *}
   137 
   138 syntax (xsymbols)
   139   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
   140 
   141 notation (xsymbols)
   142   All  (binder "\<forall>" 10) and
   143   Ex  (binder "\<exists>" 10) and
   144   Ex1  (binder "\<exists>!" 10)
   145 
   146 notation (HTML output)
   147   All  (binder "\<forall>" 10) and
   148   Ex  (binder "\<exists>" 10) and
   149   Ex1  (binder "\<exists>!" 10)
   150 
   151 notation (HOL)
   152   All  (binder "! " 10) and
   153   Ex  (binder "? " 10) and
   154   Ex1  (binder "?! " 10)
   155 
   156 
   157 subsubsection {* Axioms and basic definitions *}
   158 
   159 axioms
   160   refl:           "t = (t::'a)"
   161   subst:          "s = t \<Longrightarrow> P s \<Longrightarrow> P t"
   162   ext:            "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
   163     -- {*Extensionality is built into the meta-logic, and this rule expresses
   164          a related property.  It is an eta-expanded version of the traditional
   165          rule, and similar to the ABS rule of HOL*}
   166 
   167   the_eq_trivial: "(THE x. x = a) = (a::'a)"
   168 
   169   impI:           "(P ==> Q) ==> P-->Q"
   170   mp:             "[| P-->Q;  P |] ==> Q"
   171 
   172 
   173 defs
   174   True_def:     "True      == ((%x::bool. x) = (%x. x))"
   175   All_def:      "All(P)    == (P = (%x. True))"
   176   Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
   177   False_def:    "False     == (!P. P)"
   178   not_def:      "~ P       == P-->False"
   179   and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
   180   or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
   181   Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
   182 
   183 axioms
   184   iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
   185   True_or_False:  "(P=True) | (P=False)"
   186 
   187 defs
   188   Let_def [code]: "Let s f == f(s)"
   189   if_def:         "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
   190 
   191 finalconsts
   192   "op ="
   193   "op -->"
   194   The
   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 "op ="}, 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 "op ="}
   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   [code del]: "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   unfolding if_def 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 text {* This lemma restricts the effect of the rewrite rule u=v to the left-hand
  1393   side of an equality.  Used in @{text "{Integ,Real}/simproc.ML"} *}
  1394 lemma restrict_to_left:
  1395   assumes "x = y"
  1396   shows "(x = z) = (y = z)"
  1397   using assms by simp
  1398 
  1399 
  1400 subsubsection {* Generic cases and induction *}
  1401 
  1402 text {* Rule projections: *}
  1403 
  1404 ML {*
  1405 structure Project_Rule = Project_Rule
  1406 (
  1407   val conjunct1 = @{thm conjunct1}
  1408   val conjunct2 = @{thm conjunct2}
  1409   val mp = @{thm mp}
  1410 )
  1411 *}
  1412 
  1413 definition induct_forall where
  1414   "induct_forall P == \<forall>x. P x"
  1415 
  1416 definition induct_implies where
  1417   "induct_implies A B == A \<longrightarrow> B"
  1418 
  1419 definition induct_equal where
  1420   "induct_equal x y == x = y"
  1421 
  1422 definition induct_conj where
  1423   "induct_conj A B == A \<and> B"
  1424 
  1425 definition induct_true where
  1426   "induct_true == True"
  1427 
  1428 definition induct_false where
  1429   "induct_false == False"
  1430 
  1431 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
  1432   by (unfold atomize_all induct_forall_def)
  1433 
  1434 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
  1435   by (unfold atomize_imp induct_implies_def)
  1436 
  1437 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
  1438   by (unfold atomize_eq induct_equal_def)
  1439 
  1440 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
  1441   by (unfold atomize_conj induct_conj_def)
  1442 
  1443 lemmas induct_atomize' = induct_forall_eq induct_implies_eq induct_conj_eq
  1444 lemmas induct_atomize = induct_atomize' induct_equal_eq
  1445 lemmas induct_rulify' [symmetric, standard] = induct_atomize'
  1446 lemmas induct_rulify [symmetric, standard] = induct_atomize
  1447 lemmas induct_rulify_fallback =
  1448   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
  1449   induct_true_def induct_false_def
  1450 
  1451 
  1452 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
  1453     induct_conj (induct_forall A) (induct_forall B)"
  1454   by (unfold induct_forall_def induct_conj_def) iprover
  1455 
  1456 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
  1457     induct_conj (induct_implies C A) (induct_implies C B)"
  1458   by (unfold induct_implies_def induct_conj_def) iprover
  1459 
  1460 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
  1461 proof
  1462   assume r: "induct_conj A B ==> PROP C" and A B
  1463   show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
  1464 next
  1465   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
  1466   show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
  1467 qed
  1468 
  1469 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1470 
  1471 lemma induct_trueI: "induct_true"
  1472   by (simp add: induct_true_def)
  1473 
  1474 text {* Method setup. *}
  1475 
  1476 ML {*
  1477 structure Induct = Induct
  1478 (
  1479   val cases_default = @{thm case_split}
  1480   val atomize = @{thms induct_atomize}
  1481   val rulify = @{thms induct_rulify'}
  1482   val rulify_fallback = @{thms induct_rulify_fallback}
  1483   val equal_def = @{thm induct_equal_def}
  1484   fun dest_def (Const (@{const_name induct_equal}, _) $ t $ u) = SOME (t, u)
  1485     | dest_def _ = NONE
  1486   val trivial_tac = match_tac @{thms induct_trueI}
  1487 )
  1488 *}
  1489 
  1490 setup {*
  1491   Induct.setup #>
  1492   Context.theory_map (Induct.map_simpset (fn ss => ss
  1493     setmksimps (fn ss => Simpdata.mksimps Simpdata.mksimps_pairs ss #>
  1494       map (Simplifier.rewrite_rule (map Thm.symmetric
  1495         @{thms induct_rulify_fallback})))
  1496     addsimprocs
  1497       [Simplifier.simproc @{theory} "swap_induct_false"
  1498          ["induct_false ==> PROP P ==> PROP Q"]
  1499          (fn _ => fn _ =>
  1500             (fn _ $ (P as _ $ @{const induct_false}) $ (_ $ Q $ _) =>
  1501                   if P <> Q then SOME Drule.swap_prems_eq else NONE
  1502               | _ => NONE)),
  1503        Simplifier.simproc @{theory} "induct_equal_conj_curry"
  1504          ["induct_conj P Q ==> PROP R"]
  1505          (fn _ => fn _ =>
  1506             (fn _ $ (_ $ P) $ _ =>
  1507                 let
  1508                   fun is_conj (@{const induct_conj} $ P $ Q) =
  1509                         is_conj P andalso is_conj Q
  1510                     | is_conj (Const (@{const_name induct_equal}, _) $ _ $ _) = true
  1511                     | is_conj @{const induct_true} = true
  1512                     | is_conj @{const induct_false} = true
  1513                     | is_conj _ = false
  1514                 in if is_conj P then SOME @{thm induct_conj_curry} else NONE end
  1515               | _ => NONE))]))
  1516 *}
  1517 
  1518 text {* Pre-simplification of induction and cases rules *}
  1519 
  1520 lemma [induct_simp]: "(!!x. induct_equal x t ==> PROP P x) == PROP P t"
  1521   unfolding induct_equal_def
  1522 proof
  1523   assume R: "!!x. x = t ==> PROP P x"
  1524   show "PROP P t" by (rule R [OF refl])
  1525 next
  1526   fix x assume "PROP P t" "x = t"
  1527   then show "PROP P x" by simp
  1528 qed
  1529 
  1530 lemma [induct_simp]: "(!!x. induct_equal t x ==> PROP P x) == PROP P t"
  1531   unfolding induct_equal_def
  1532 proof
  1533   assume R: "!!x. t = x ==> PROP P x"
  1534   show "PROP P t" by (rule R [OF refl])
  1535 next
  1536   fix x assume "PROP P t" "t = x"
  1537   then show "PROP P x" by simp
  1538 qed
  1539 
  1540 lemma [induct_simp]: "(induct_false ==> P) == Trueprop induct_true"
  1541   unfolding induct_false_def induct_true_def
  1542   by (iprover intro: equal_intr_rule)
  1543 
  1544 lemma [induct_simp]: "(induct_true ==> PROP P) == PROP P"
  1545   unfolding induct_true_def
  1546 proof
  1547   assume R: "True \<Longrightarrow> PROP P"
  1548   from TrueI show "PROP P" by (rule R)
  1549 next
  1550   assume "PROP P"
  1551   then show "PROP P" .
  1552 qed
  1553 
  1554 lemma [induct_simp]: "(PROP P ==> induct_true) == Trueprop induct_true"
  1555   unfolding induct_true_def
  1556   by (iprover intro: equal_intr_rule)
  1557 
  1558 lemma [induct_simp]: "(!!x. induct_true) == Trueprop induct_true"
  1559   unfolding induct_true_def
  1560   by (iprover intro: equal_intr_rule)
  1561 
  1562 lemma [induct_simp]: "induct_implies induct_true P == P"
  1563   by (simp add: induct_implies_def induct_true_def)
  1564 
  1565 lemma [induct_simp]: "(x = x) = True" 
  1566   by (rule simp_thms)
  1567 
  1568 hide_const induct_forall induct_implies induct_equal induct_conj induct_true induct_false
  1569 
  1570 use "~~/src/Tools/induct_tacs.ML"
  1571 setup InductTacs.setup
  1572 
  1573 
  1574 subsubsection {* Coherent logic *}
  1575 
  1576 ML {*
  1577 structure Coherent = Coherent
  1578 (
  1579   val atomize_elimL = @{thm atomize_elimL}
  1580   val atomize_exL = @{thm atomize_exL}
  1581   val atomize_conjL = @{thm atomize_conjL}
  1582   val atomize_disjL = @{thm atomize_disjL}
  1583   val operator_names =
  1584     [@{const_name "op |"}, @{const_name "op &"}, @{const_name "Ex"}]
  1585 );
  1586 *}
  1587 
  1588 setup Coherent.setup
  1589 
  1590 
  1591 subsubsection {* Reorienting equalities *}
  1592 
  1593 ML {*
  1594 signature REORIENT_PROC =
  1595 sig
  1596   val add : (term -> bool) -> theory -> theory
  1597   val proc : morphism -> simpset -> cterm -> thm option
  1598 end;
  1599 
  1600 structure Reorient_Proc : REORIENT_PROC =
  1601 struct
  1602   structure Data = Theory_Data
  1603   (
  1604     type T = ((term -> bool) * stamp) list;
  1605     val empty = [];
  1606     val extend = I;
  1607     fun merge data : T = Library.merge (eq_snd op =) data;
  1608   );
  1609   fun add m = Data.map (cons (m, stamp ()));
  1610   fun matches thy t = exists (fn (m, _) => m t) (Data.get thy);
  1611 
  1612   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1613   fun proc phi ss ct =
  1614     let
  1615       val ctxt = Simplifier.the_context ss;
  1616       val thy = ProofContext.theory_of ctxt;
  1617     in
  1618       case Thm.term_of ct of
  1619         (_ $ t $ u) => if matches thy u then NONE else SOME meta_reorient
  1620       | _ => NONE
  1621     end;
  1622 end;
  1623 *}
  1624 
  1625 
  1626 subsection {* Other simple lemmas and lemma duplicates *}
  1627 
  1628 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
  1629   by blast+
  1630 
  1631 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
  1632   apply (rule iffI)
  1633   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
  1634   apply (fast dest!: theI')
  1635   apply (fast intro: ext the1_equality [symmetric])
  1636   apply (erule ex1E)
  1637   apply (rule allI)
  1638   apply (rule ex1I)
  1639   apply (erule spec)
  1640   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
  1641   apply (erule impE)
  1642   apply (rule allI)
  1643   apply (case_tac "xa = x")
  1644   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1645   done
  1646 
  1647 lemmas eq_sym_conv = eq_commute
  1648 
  1649 lemma nnf_simps:
  1650   "(\<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)" 
  1651   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))" 
  1652   "(\<not> \<not>(P)) = P"
  1653 by blast+
  1654 
  1655 
  1656 subsection {* Basic ML bindings *}
  1657 
  1658 ML {*
  1659 val FalseE = @{thm FalseE}
  1660 val Let_def = @{thm Let_def}
  1661 val TrueI = @{thm TrueI}
  1662 val allE = @{thm allE}
  1663 val allI = @{thm allI}
  1664 val all_dupE = @{thm all_dupE}
  1665 val arg_cong = @{thm arg_cong}
  1666 val box_equals = @{thm box_equals}
  1667 val ccontr = @{thm ccontr}
  1668 val classical = @{thm classical}
  1669 val conjE = @{thm conjE}
  1670 val conjI = @{thm conjI}
  1671 val conjunct1 = @{thm conjunct1}
  1672 val conjunct2 = @{thm conjunct2}
  1673 val disjCI = @{thm disjCI}
  1674 val disjE = @{thm disjE}
  1675 val disjI1 = @{thm disjI1}
  1676 val disjI2 = @{thm disjI2}
  1677 val eq_reflection = @{thm eq_reflection}
  1678 val ex1E = @{thm ex1E}
  1679 val ex1I = @{thm ex1I}
  1680 val ex1_implies_ex = @{thm ex1_implies_ex}
  1681 val exE = @{thm exE}
  1682 val exI = @{thm exI}
  1683 val excluded_middle = @{thm excluded_middle}
  1684 val ext = @{thm ext}
  1685 val fun_cong = @{thm fun_cong}
  1686 val iffD1 = @{thm iffD1}
  1687 val iffD2 = @{thm iffD2}
  1688 val iffI = @{thm iffI}
  1689 val impE = @{thm impE}
  1690 val impI = @{thm impI}
  1691 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1692 val mp = @{thm mp}
  1693 val notE = @{thm notE}
  1694 val notI = @{thm notI}
  1695 val not_all = @{thm not_all}
  1696 val not_ex = @{thm not_ex}
  1697 val not_iff = @{thm not_iff}
  1698 val not_not = @{thm not_not}
  1699 val not_sym = @{thm not_sym}
  1700 val refl = @{thm refl}
  1701 val rev_mp = @{thm rev_mp}
  1702 val spec = @{thm spec}
  1703 val ssubst = @{thm ssubst}
  1704 val subst = @{thm subst}
  1705 val sym = @{thm sym}
  1706 val trans = @{thm trans}
  1707 *}
  1708 
  1709 
  1710 subsection {* Code generator setup *}
  1711 
  1712 subsubsection {* SML code generator setup *}
  1713 
  1714 use "Tools/recfun_codegen.ML"
  1715 
  1716 setup {*
  1717   Codegen.setup
  1718   #> RecfunCodegen.setup
  1719   #> Codegen.map_unfold (K HOL_basic_ss)
  1720 *}
  1721 
  1722 types_code
  1723   "bool"  ("bool")
  1724 attach (term_of) {*
  1725 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
  1726 *}
  1727 attach (test) {*
  1728 fun gen_bool i =
  1729   let val b = one_of [false, true]
  1730   in (b, fn () => term_of_bool b) end;
  1731 *}
  1732   "prop"  ("bool")
  1733 attach (term_of) {*
  1734 fun term_of_prop b =
  1735   HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
  1736 *}
  1737 
  1738 consts_code
  1739   "Trueprop" ("(_)")
  1740   "True"    ("true")
  1741   "False"   ("false")
  1742   "Not"     ("Bool.not")
  1743   "op |"    ("(_ orelse/ _)")
  1744   "op &"    ("(_ andalso/ _)")
  1745   "If"      ("(if _/ then _/ else _)")
  1746 
  1747 setup {*
  1748 let
  1749 
  1750 fun eq_codegen thy defs dep thyname b t gr =
  1751     (case strip_comb t of
  1752        (Const (@{const_name "op ="}, Type (_, [Type ("fun", _), _])), _) => NONE
  1753      | (Const (@{const_name "op ="}, _), [t, u]) =>
  1754           let
  1755             val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
  1756             val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
  1757             val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
  1758           in
  1759             SOME (Codegen.parens
  1760               (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
  1761           end
  1762      | (t as Const (@{const_name "op ="}, _), ts) => SOME (Codegen.invoke_codegen
  1763          thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
  1764      | _ => NONE);
  1765 
  1766 in
  1767   Codegen.add_codegen "eq_codegen" eq_codegen
  1768 end
  1769 *}
  1770 
  1771 subsubsection {* Generic code generator preprocessor setup *}
  1772 
  1773 setup {*
  1774   Code_Preproc.map_pre (K HOL_basic_ss)
  1775   #> Code_Preproc.map_post (K HOL_basic_ss)
  1776 *}
  1777 
  1778 subsubsection {* Equality *}
  1779 
  1780 class eq =
  1781   fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1782   assumes eq_equals: "eq x y \<longleftrightarrow> x = y"
  1783 begin
  1784 
  1785 lemma eq [code_unfold, code_inline del]: "eq = (op =)"
  1786   by (rule ext eq_equals)+
  1787 
  1788 lemma eq_refl: "eq x x \<longleftrightarrow> True"
  1789   unfolding eq by rule+
  1790 
  1791 lemma equals_eq: "(op =) \<equiv> eq"
  1792   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule eq_equals)
  1793 
  1794 declare equals_eq [symmetric, code_post]
  1795 
  1796 end
  1797 
  1798 declare equals_eq [code]
  1799 
  1800 setup {*
  1801   Code_Preproc.map_pre (fn simpset =>
  1802     simpset addsimprocs [Simplifier.simproc_i @{theory} "eq" [@{term "op ="}]
  1803       (fn thy => fn _ => fn t as Const (_, T) => case strip_type T
  1804         of ((T as Type _) :: _, _) => SOME @{thm equals_eq}
  1805          | _ => NONE)])
  1806 *}
  1807 
  1808 
  1809 subsubsection {* Generic code generator foundation *}
  1810 
  1811 text {* Datatypes *}
  1812 
  1813 code_datatype True False
  1814 
  1815 code_datatype "TYPE('a\<Colon>{})"
  1816 
  1817 code_datatype "prop" Trueprop
  1818 
  1819 text {* Code equations *}
  1820 
  1821 lemma [code]:
  1822   shows "(True \<Longrightarrow> PROP Q) \<equiv> PROP Q" 
  1823     and "(PROP Q \<Longrightarrow> True) \<equiv> Trueprop True"
  1824     and "(P \<Longrightarrow> R) \<equiv> Trueprop (P \<longrightarrow> R)" by (auto intro!: equal_intr_rule)
  1825 
  1826 lemma [code]:
  1827   shows "False \<and> P \<longleftrightarrow> False"
  1828     and "True \<and> P \<longleftrightarrow> P"
  1829     and "P \<and> False \<longleftrightarrow> False"
  1830     and "P \<and> True \<longleftrightarrow> P" by simp_all
  1831 
  1832 lemma [code]:
  1833   shows "False \<or> P \<longleftrightarrow> P"
  1834     and "True \<or> P \<longleftrightarrow> True"
  1835     and "P \<or> False \<longleftrightarrow> P"
  1836     and "P \<or> True \<longleftrightarrow> True" by simp_all
  1837 
  1838 lemma [code]:
  1839   shows "(False \<longrightarrow> P) \<longleftrightarrow> True"
  1840     and "(True \<longrightarrow> P) \<longleftrightarrow> P"
  1841     and "(P \<longrightarrow> False) \<longleftrightarrow> \<not> P"
  1842     and "(P \<longrightarrow> True) \<longleftrightarrow> True" by simp_all
  1843 
  1844 instantiation itself :: (type) eq
  1845 begin
  1846 
  1847 definition eq_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1848   "eq_itself x y \<longleftrightarrow> x = y"
  1849 
  1850 instance proof
  1851 qed (fact eq_itself_def)
  1852 
  1853 end
  1854 
  1855 lemma eq_itself_code [code]:
  1856   "eq_class.eq TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1857   by (simp add: eq)
  1858 
  1859 text {* Equality *}
  1860 
  1861 declare simp_thms(6) [code nbe]
  1862 
  1863 setup {*
  1864   Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
  1865 *}
  1866 
  1867 lemma equals_alias_cert: "OFCLASS('a, eq_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> eq)" (is "?ofclass \<equiv> ?eq")
  1868 proof
  1869   assume "PROP ?ofclass"
  1870   show "PROP ?eq"
  1871     by (tactic {* ALLGOALS (rtac (Thm.unconstrainT @{thm equals_eq})) *})
  1872       (fact `PROP ?ofclass`)
  1873 next
  1874   assume "PROP ?eq"
  1875   show "PROP ?ofclass" proof
  1876   qed (simp add: `PROP ?eq`)
  1877 qed
  1878   
  1879 setup {*
  1880   Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>eq \<Rightarrow> 'a \<Rightarrow> bool"})
  1881 *}
  1882 
  1883 setup {*
  1884   Nbe.add_const_alias @{thm equals_alias_cert}
  1885 *}
  1886 
  1887 hide_const (open) eq
  1888 
  1889 text {* Cases *}
  1890 
  1891 lemma Let_case_cert:
  1892   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1893   shows "CASE x \<equiv> f x"
  1894   using assms by simp_all
  1895 
  1896 lemma If_case_cert:
  1897   assumes "CASE \<equiv> (\<lambda>b. If b f g)"
  1898   shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
  1899   using assms by simp_all
  1900 
  1901 setup {*
  1902   Code.add_case @{thm Let_case_cert}
  1903   #> Code.add_case @{thm If_case_cert}
  1904   #> Code.add_undefined @{const_name undefined}
  1905 *}
  1906 
  1907 code_abort undefined
  1908 
  1909 subsubsection {* Generic code generator target languages *}
  1910 
  1911 text {* type bool *}
  1912 
  1913 code_type bool
  1914   (SML "bool")
  1915   (OCaml "bool")
  1916   (Haskell "Bool")
  1917   (Scala "Boolean")
  1918 
  1919 code_const True and False and Not and "op &" and "op |" and If
  1920   (SML "true" and "false" and "not"
  1921     and infixl 1 "andalso" and infixl 0 "orelse"
  1922     and "!(if (_)/ then (_)/ else (_))")
  1923   (OCaml "true" and "false" and "not"
  1924     and infixl 4 "&&" and infixl 2 "||"
  1925     and "!(if (_)/ then (_)/ else (_))")
  1926   (Haskell "True" and "False" and "not"
  1927     and infixl 3 "&&" and infixl 2 "||"
  1928     and "!(if (_)/ then (_)/ else (_))")
  1929   (Scala "true" and "false" and "'! _"
  1930     and infixl 3 "&&" and infixl 1 "||"
  1931     and "!(if ((_))/ (_)/ else (_))")
  1932 
  1933 code_reserved SML
  1934   bool true false not
  1935 
  1936 code_reserved OCaml
  1937   bool not
  1938 
  1939 code_reserved Scala
  1940   Boolean
  1941 
  1942 text {* using built-in Haskell equality *}
  1943 
  1944 code_class eq
  1945   (Haskell "Eq")
  1946 
  1947 code_const "eq_class.eq"
  1948   (Haskell infixl 4 "==")
  1949 
  1950 code_const "op ="
  1951   (Haskell infixl 4 "==")
  1952 
  1953 text {* undefined *}
  1954 
  1955 code_const undefined
  1956   (SML "!(raise/ Fail/ \"undefined\")")
  1957   (OCaml "failwith/ \"undefined\"")
  1958   (Haskell "error/ \"undefined\"")
  1959   (Scala "!error(\"undefined\")")
  1960 
  1961 subsubsection {* Evaluation and normalization by evaluation *}
  1962 
  1963 text {* Avoid some named infixes in evaluation environment *}
  1964 
  1965 code_reserved Eval oo ooo oooo upto downto orf andf
  1966 
  1967 setup {*
  1968   Value.add_evaluator ("SML", Codegen.eval_term o ProofContext.theory_of)
  1969 *}
  1970 
  1971 ML {*
  1972 structure Eval_Method =
  1973 struct
  1974 
  1975 val eval_ref : (unit -> bool) option Unsynchronized.ref = Unsynchronized.ref NONE;
  1976 
  1977 end;
  1978 *}
  1979 
  1980 oracle eval_oracle = {* fn ct =>
  1981   let
  1982     val thy = Thm.theory_of_cterm ct;
  1983     val t = Thm.term_of ct;
  1984     val dummy = @{cprop True};
  1985   in case try HOLogic.dest_Trueprop t
  1986    of SOME t' => if Code_Eval.eval NONE
  1987          ("Eval_Method.eval_ref", Eval_Method.eval_ref) (K I) thy t' [] 
  1988        then Thm.capply (Thm.capply @{cterm "op \<equiv> \<Colon> prop \<Rightarrow> prop \<Rightarrow> prop"} ct) dummy
  1989        else dummy
  1990     | NONE => dummy
  1991   end
  1992 *}
  1993 
  1994 ML {*
  1995 fun gen_eval_method conv ctxt = SIMPLE_METHOD'
  1996   (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
  1997     THEN' rtac TrueI)
  1998 *}
  1999 
  2000 method_setup eval = {* Scan.succeed (gen_eval_method eval_oracle) *}
  2001   "solve goal by evaluation"
  2002 
  2003 method_setup evaluation = {* Scan.succeed (gen_eval_method Codegen.evaluation_conv) *}
  2004   "solve goal by evaluation"
  2005 
  2006 method_setup normalization = {*
  2007   Scan.succeed (K (SIMPLE_METHOD' (CONVERSION Nbe.norm_conv THEN' (fn k => TRY (rtac TrueI k)))))
  2008 *} "solve goal by normalization"
  2009 
  2010 
  2011 subsection {* Counterexample Search Units *}
  2012 
  2013 subsubsection {* Quickcheck *}
  2014 
  2015 quickcheck_params [size = 5, iterations = 50]
  2016 
  2017 
  2018 subsubsection {* Nitpick setup *}
  2019 
  2020 ML {*
  2021 structure Nitpick_Defs = Named_Thms
  2022 (
  2023   val name = "nitpick_def"
  2024   val description = "alternative definitions of constants as needed by Nitpick"
  2025 )
  2026 structure Nitpick_Simps = Named_Thms
  2027 (
  2028   val name = "nitpick_simp"
  2029   val description = "equational specification of constants as needed by Nitpick"
  2030 )
  2031 structure Nitpick_Psimps = Named_Thms
  2032 (
  2033   val name = "nitpick_psimp"
  2034   val description = "partial equational specification of constants as needed by Nitpick"
  2035 )
  2036 structure Nitpick_Intros = Named_Thms
  2037 (
  2038   val name = "nitpick_intro"
  2039   val description = "introduction rules for (co)inductive predicates as needed by Nitpick"
  2040 )
  2041 structure Nitpick_Choice_Specs = Named_Thms
  2042 (
  2043   val name = "nitpick_choice_spec"
  2044   val description = "choice specification of constants as needed by Nitpick"
  2045 )
  2046 *}
  2047 
  2048 setup {*
  2049   Nitpick_Defs.setup
  2050   #> Nitpick_Simps.setup
  2051   #> Nitpick_Psimps.setup
  2052   #> Nitpick_Intros.setup
  2053   #> Nitpick_Choice_Specs.setup
  2054 *}
  2055 
  2056 
  2057 subsection {* Preprocessing for the predicate compiler *}
  2058 
  2059 ML {*
  2060 structure Predicate_Compile_Alternative_Defs = Named_Thms
  2061 (
  2062   val name = "code_pred_def"
  2063   val description = "alternative definitions of constants for the Predicate Compiler"
  2064 )
  2065 structure Predicate_Compile_Inline_Defs = Named_Thms
  2066 (
  2067   val name = "code_pred_inline"
  2068   val description = "inlining definitions for the Predicate Compiler"
  2069 )
  2070 structure Predicate_Compile_Simps = Named_Thms
  2071 (
  2072   val name = "code_pred_simp"
  2073   val description = "simplification rules for the optimisations in the Predicate Compiler"
  2074 )
  2075 *}
  2076 
  2077 setup {*
  2078   Predicate_Compile_Alternative_Defs.setup
  2079   #> Predicate_Compile_Inline_Defs.setup
  2080   #> Predicate_Compile_Simps.setup
  2081 *}
  2082 
  2083 
  2084 subsection {* Legacy tactics and ML bindings *}
  2085 
  2086 ML {*
  2087 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
  2088 
  2089 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  2090 local
  2091   fun wrong_prem (Const (@{const_name All}, _) $ Abs (_, _, t)) = wrong_prem t
  2092     | wrong_prem (Bound _) = true
  2093     | wrong_prem _ = false;
  2094   val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  2095 in
  2096   fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
  2097   fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
  2098 end;
  2099 
  2100 val all_conj_distrib = thm "all_conj_distrib";
  2101 val all_simps = thms "all_simps";
  2102 val atomize_not = thm "atomize_not";
  2103 val case_split = thm "case_split";
  2104 val cases_simp = thm "cases_simp";
  2105 val choice_eq = thm "choice_eq"
  2106 val cong = thm "cong"
  2107 val conj_comms = thms "conj_comms";
  2108 val conj_cong = thm "conj_cong";
  2109 val de_Morgan_conj = thm "de_Morgan_conj";
  2110 val de_Morgan_disj = thm "de_Morgan_disj";
  2111 val disj_assoc = thm "disj_assoc";
  2112 val disj_comms = thms "disj_comms";
  2113 val disj_cong = thm "disj_cong";
  2114 val eq_ac = thms "eq_ac";
  2115 val eq_cong2 = thm "eq_cong2"
  2116 val Eq_FalseI = thm "Eq_FalseI";
  2117 val Eq_TrueI = thm "Eq_TrueI";
  2118 val Ex1_def = thm "Ex1_def"
  2119 val ex_disj_distrib = thm "ex_disj_distrib";
  2120 val ex_simps = thms "ex_simps";
  2121 val if_cancel = thm "if_cancel";
  2122 val if_eq_cancel = thm "if_eq_cancel";
  2123 val if_False = thm "if_False";
  2124 val iff_conv_conj_imp = thm "iff_conv_conj_imp";
  2125 val iff = thm "iff"
  2126 val if_splits = thms "if_splits";
  2127 val if_True = thm "if_True";
  2128 val if_weak_cong = thm "if_weak_cong"
  2129 val imp_all = thm "imp_all";
  2130 val imp_cong = thm "imp_cong";
  2131 val imp_conjL = thm "imp_conjL";
  2132 val imp_conjR = thm "imp_conjR";
  2133 val imp_conv_disj = thm "imp_conv_disj";
  2134 val simp_implies_def = thm "simp_implies_def";
  2135 val simp_thms = thms "simp_thms";
  2136 val split_if = thm "split_if";
  2137 val the1_equality = thm "the1_equality"
  2138 val theI = thm "theI"
  2139 val theI' = thm "theI'"
  2140 val True_implies_equals = thm "True_implies_equals";
  2141 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})
  2142 
  2143 *}
  2144 
  2145 end