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