src/HOL/HOL.thy
author wenzelm
Fri Jul 24 21:21:45 2009 +0200 (2009-07-24)
changeset 32176 893614e2c35c
parent 32172 c4e55f30d527
child 32402 5731300da417
permissions -rw-r--r--
renamed functor BlastFun to Blast, require explicit theory;
eliminated src/FOL/blastdata.ML;
     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 = Blast
   914 (
   915   val thy = @{theory}
   916   type claset = Classical.claset
   917   val equality_name = @{const_name "op ="}
   918   val not_name = @{const_name Not}
   919   val notE = @{thm notE}
   920   val ccontr = @{thm ccontr}
   921   val contr_tac = Classical.contr_tac
   922   val dup_intr = Classical.dup_intr
   923   val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   924   val rep_cs = Classical.rep_cs
   925   val cla_modifiers = Classical.cla_modifiers
   926   val cla_meth' = Classical.cla_meth'
   927 );
   928 val blast_tac = Blast.blast_tac;
   929 *}
   930 
   931 setup Blast.setup
   932 
   933 
   934 subsubsection {* Simplifier *}
   935 
   936 lemma eta_contract_eq: "(%s. f s) = f" ..
   937 
   938 lemma simp_thms:
   939   shows not_not: "(~ ~ P) = P"
   940   and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
   941   and
   942     "(P ~= Q) = (P = (~Q))"
   943     "(P | ~P) = True"    "(~P | P) = True"
   944     "(x = x) = True"
   945   and not_True_eq_False [code]: "(\<not> True) = False"
   946   and not_False_eq_True [code]: "(\<not> False) = True"
   947   and
   948     "(~P) ~= P"  "P ~= (~P)"
   949     "(True=P) = P"
   950   and eq_True: "(P = True) = P"
   951   and "(False=P) = (~P)"
   952   and eq_False: "(P = False) = (\<not> P)"
   953   and
   954     "(True --> P) = P"  "(False --> P) = True"
   955     "(P --> True) = True"  "(P --> P) = True"
   956     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
   957     "(P & True) = P"  "(True & P) = P"
   958     "(P & False) = False"  "(False & P) = False"
   959     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
   960     "(P & ~P) = False"    "(~P & P) = False"
   961     "(P | True) = True"  "(True | P) = True"
   962     "(P | False) = P"  "(False | P) = P"
   963     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
   964     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
   965   and
   966     "!!P. (EX x. x=t & P(x)) = P(t)"
   967     "!!P. (EX x. t=x & P(x)) = P(t)"
   968     "!!P. (ALL x. x=t --> P(x)) = P(t)"
   969     "!!P. (ALL x. t=x --> P(x)) = P(t)"
   970   by (blast, blast, blast, blast, blast, iprover+)
   971 
   972 lemma disj_absorb: "(A | A) = A"
   973   by blast
   974 
   975 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
   976   by blast
   977 
   978 lemma conj_absorb: "(A & A) = A"
   979   by blast
   980 
   981 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
   982   by blast
   983 
   984 lemma eq_ac:
   985   shows eq_commute: "(a=b) = (b=a)"
   986     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
   987     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
   988 lemma neq_commute: "(a~=b) = (b~=a)" by iprover
   989 
   990 lemma conj_comms:
   991   shows conj_commute: "(P&Q) = (Q&P)"
   992     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
   993 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
   994 
   995 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
   996 
   997 lemma disj_comms:
   998   shows disj_commute: "(P|Q) = (Q|P)"
   999     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
  1000 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
  1001 
  1002 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
  1003 
  1004 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
  1005 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
  1006 
  1007 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
  1008 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
  1009 
  1010 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
  1011 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
  1012 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
  1013 
  1014 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
  1015 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
  1016 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
  1017 
  1018 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
  1019 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
  1020 
  1021 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
  1022   by iprover
  1023 
  1024 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
  1025 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
  1026 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
  1027 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
  1028 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
  1029 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
  1030   by blast
  1031 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
  1032 
  1033 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
  1034 
  1035 
  1036 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
  1037   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
  1038   -- {* cases boil down to the same thing. *}
  1039   by blast
  1040 
  1041 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
  1042 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
  1043 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
  1044 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
  1045 lemma all_not_ex: "(ALL x. P x) = (~ (EX x. ~ P x ))" by blast
  1046 
  1047 declare All_def [noatp]
  1048 
  1049 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
  1050 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
  1051 
  1052 text {*
  1053   \medskip The @{text "&"} congruence rule: not included by default!
  1054   May slow rewrite proofs down by as much as 50\% *}
  1055 
  1056 lemma conj_cong:
  1057     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
  1058   by iprover
  1059 
  1060 lemma rev_conj_cong:
  1061     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
  1062   by iprover
  1063 
  1064 text {* The @{text "|"} congruence rule: not included by default! *}
  1065 
  1066 lemma disj_cong:
  1067     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
  1068   by blast
  1069 
  1070 
  1071 text {* \medskip if-then-else rules *}
  1072 
  1073 lemma if_True [code]: "(if True then x else y) = x"
  1074   by (unfold if_def) blast
  1075 
  1076 lemma if_False [code]: "(if False then x else y) = y"
  1077   by (unfold if_def) blast
  1078 
  1079 lemma if_P: "P ==> (if P then x else y) = x"
  1080   by (unfold if_def) blast
  1081 
  1082 lemma if_not_P: "~P ==> (if P then x else y) = y"
  1083   by (unfold if_def) blast
  1084 
  1085 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
  1086   apply (rule case_split [of Q])
  1087    apply (simplesubst if_P)
  1088     prefer 3 apply (simplesubst if_not_P, blast+)
  1089   done
  1090 
  1091 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
  1092 by (simplesubst split_if, blast)
  1093 
  1094 lemmas if_splits [noatp] = split_if split_if_asm
  1095 
  1096 lemma if_cancel: "(if c then x else x) = x"
  1097 by (simplesubst split_if, blast)
  1098 
  1099 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1100 by (simplesubst split_if, blast)
  1101 
  1102 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
  1103   -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
  1104   by (rule split_if)
  1105 
  1106 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
  1107   -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
  1108   apply (simplesubst split_if, blast)
  1109   done
  1110 
  1111 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
  1112 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
  1113 
  1114 text {* \medskip let rules for simproc *}
  1115 
  1116 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
  1117   by (unfold Let_def)
  1118 
  1119 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
  1120   by (unfold Let_def)
  1121 
  1122 text {*
  1123   The following copy of the implication operator is useful for
  1124   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1125   its premise.
  1126 *}
  1127 
  1128 constdefs
  1129   simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1)
  1130   [code del]: "simp_implies \<equiv> op ==>"
  1131 
  1132 lemma simp_impliesI:
  1133   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1134   shows "PROP P =simp=> PROP Q"
  1135   apply (unfold simp_implies_def)
  1136   apply (rule PQ)
  1137   apply assumption
  1138   done
  1139 
  1140 lemma simp_impliesE:
  1141   assumes PQ: "PROP P =simp=> PROP Q"
  1142   and P: "PROP P"
  1143   and QR: "PROP Q \<Longrightarrow> PROP R"
  1144   shows "PROP R"
  1145   apply (rule QR)
  1146   apply (rule PQ [unfolded simp_implies_def])
  1147   apply (rule P)
  1148   done
  1149 
  1150 lemma simp_implies_cong:
  1151   assumes PP' :"PROP P == PROP P'"
  1152   and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
  1153   shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
  1154 proof (unfold simp_implies_def, rule equal_intr_rule)
  1155   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1156   and P': "PROP P'"
  1157   from PP' [symmetric] and P' have "PROP P"
  1158     by (rule equal_elim_rule1)
  1159   then have "PROP Q" by (rule PQ)
  1160   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1161 next
  1162   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1163   and P: "PROP P"
  1164   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1165   then have "PROP Q'" by (rule P'Q')
  1166   with P'QQ' [OF P', symmetric] show "PROP Q"
  1167     by (rule equal_elim_rule1)
  1168 qed
  1169 
  1170 lemma uncurry:
  1171   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1172   shows "P \<and> Q \<longrightarrow> R"
  1173   using assms by blast
  1174 
  1175 lemma iff_allI:
  1176   assumes "\<And>x. P x = Q x"
  1177   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1178   using assms by blast
  1179 
  1180 lemma iff_exI:
  1181   assumes "\<And>x. P x = Q x"
  1182   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1183   using assms by blast
  1184 
  1185 lemma all_comm:
  1186   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1187   by blast
  1188 
  1189 lemma ex_comm:
  1190   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1191   by blast
  1192 
  1193 use "Tools/simpdata.ML"
  1194 ML {* open Simpdata *}
  1195 
  1196 setup {*
  1197   Simplifier.method_setup Splitter.split_modifiers
  1198   #> Simplifier.map_simpset (K Simpdata.simpset_simprocs)
  1199   #> Splitter.setup
  1200   #> clasimp_setup
  1201   #> EqSubst.setup
  1202 *}
  1203 
  1204 text {* Simproc for proving @{text "(y = x) == False"} from premise @{text "~(x = y)"}: *}
  1205 
  1206 simproc_setup neq ("x = y") = {* fn _ =>
  1207 let
  1208   val neq_to_EQ_False = @{thm not_sym} RS @{thm Eq_FalseI};
  1209   fun is_neq eq lhs rhs thm =
  1210     (case Thm.prop_of thm of
  1211       _ $ (Not $ (eq' $ l' $ r')) =>
  1212         Not = HOLogic.Not andalso eq' = eq andalso
  1213         r' aconv lhs andalso l' aconv rhs
  1214     | _ => false);
  1215   fun proc ss ct =
  1216     (case Thm.term_of ct of
  1217       eq $ lhs $ rhs =>
  1218         (case find_first (is_neq eq lhs rhs) (Simplifier.prems_of_ss ss) of
  1219           SOME thm => SOME (thm RS neq_to_EQ_False)
  1220         | NONE => NONE)
  1221      | _ => NONE);
  1222 in proc end;
  1223 *}
  1224 
  1225 simproc_setup let_simp ("Let x f") = {*
  1226 let
  1227   val (f_Let_unfold, x_Let_unfold) =
  1228     let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_unfold}
  1229     in (cterm_of @{theory} f, cterm_of @{theory} x) end
  1230   val (f_Let_folded, x_Let_folded) =
  1231     let val [(_ $ (f $ x) $ _)] = prems_of @{thm Let_folded}
  1232     in (cterm_of @{theory} f, cterm_of @{theory} x) end;
  1233   val g_Let_folded =
  1234     let val [(_ $ _ $ (g $ _))] = prems_of @{thm Let_folded}
  1235     in cterm_of @{theory} g end;
  1236   fun count_loose (Bound i) k = if i >= k then 1 else 0
  1237     | count_loose (s $ t) k = count_loose s k + count_loose t k
  1238     | count_loose (Abs (_, _, t)) k = count_loose  t (k + 1)
  1239     | count_loose _ _ = 0;
  1240   fun is_trivial_let (Const (@{const_name Let}, _) $ x $ t) =
  1241    case t
  1242     of Abs (_, _, t') => count_loose t' 0 <= 1
  1243      | _ => true;
  1244 in fn _ => fn ss => fn ct => if is_trivial_let (Thm.term_of ct)
  1245   then SOME @{thm Let_def} (*no or one ocurrence of bound variable*)
  1246   else let (*Norbert Schirmer's case*)
  1247     val ctxt = Simplifier.the_context ss;
  1248     val thy = ProofContext.theory_of ctxt;
  1249     val t = Thm.term_of ct;
  1250     val ([t'], ctxt') = Variable.import_terms false [t] ctxt;
  1251   in Option.map (hd o Variable.export ctxt' ctxt o single)
  1252     (case t' of Const (@{const_name Let},_) $ x $ f => (* x and f are already in normal form *)
  1253       if is_Free x orelse is_Bound x orelse is_Const x
  1254       then SOME @{thm Let_def}
  1255       else
  1256         let
  1257           val n = case f of (Abs (x, _, _)) => x | _ => "x";
  1258           val cx = cterm_of thy x;
  1259           val {T = xT, ...} = rep_cterm cx;
  1260           val cf = cterm_of thy f;
  1261           val fx_g = Simplifier.rewrite ss (Thm.capply cf cx);
  1262           val (_ $ _ $ g) = prop_of fx_g;
  1263           val g' = abstract_over (x,g);
  1264         in (if (g aconv g')
  1265              then
  1266                 let
  1267                   val rl =
  1268                     cterm_instantiate [(f_Let_unfold, cf), (x_Let_unfold, cx)] @{thm Let_unfold};
  1269                 in SOME (rl OF [fx_g]) end
  1270              else if Term.betapply (f, x) aconv g then NONE (*avoid identity conversion*)
  1271              else let
  1272                    val abs_g'= Abs (n,xT,g');
  1273                    val g'x = abs_g'$x;
  1274                    val g_g'x = symmetric (beta_conversion false (cterm_of thy g'x));
  1275                    val rl = cterm_instantiate
  1276                              [(f_Let_folded, cterm_of thy f), (x_Let_folded, cx),
  1277                               (g_Let_folded, cterm_of thy abs_g')]
  1278                              @{thm Let_folded};
  1279                  in SOME (rl OF [transitive fx_g g_g'x])
  1280                  end)
  1281         end
  1282     | _ => NONE)
  1283   end
  1284 end *}
  1285 
  1286 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
  1287 proof
  1288   assume "True \<Longrightarrow> PROP P"
  1289   from this [OF TrueI] show "PROP P" .
  1290 next
  1291   assume "PROP P"
  1292   then show "PROP P" .
  1293 qed
  1294 
  1295 lemma ex_simps:
  1296   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
  1297   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
  1298   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
  1299   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
  1300   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
  1301   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
  1302   -- {* Miniscoping: pushing in existential quantifiers. *}
  1303   by (iprover | blast)+
  1304 
  1305 lemma all_simps:
  1306   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
  1307   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
  1308   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
  1309   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
  1310   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
  1311   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
  1312   -- {* Miniscoping: pushing in universal quantifiers. *}
  1313   by (iprover | blast)+
  1314 
  1315 lemmas [simp] =
  1316   triv_forall_equality (*prunes params*)
  1317   True_implies_equals  (*prune asms `True'*)
  1318   if_True
  1319   if_False
  1320   if_cancel
  1321   if_eq_cancel
  1322   imp_disjL
  1323   (*In general it seems wrong to add distributive laws by default: they
  1324     might cause exponential blow-up.  But imp_disjL has been in for a while
  1325     and cannot be removed without affecting existing proofs.  Moreover,
  1326     rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
  1327     grounds that it allows simplification of R in the two cases.*)
  1328   conj_assoc
  1329   disj_assoc
  1330   de_Morgan_conj
  1331   de_Morgan_disj
  1332   imp_disj1
  1333   imp_disj2
  1334   not_imp
  1335   disj_not1
  1336   not_all
  1337   not_ex
  1338   cases_simp
  1339   the_eq_trivial
  1340   the_sym_eq_trivial
  1341   ex_simps
  1342   all_simps
  1343   simp_thms
  1344 
  1345 lemmas [cong] = imp_cong simp_implies_cong
  1346 lemmas [split] = split_if
  1347 
  1348 ML {* val HOL_ss = @{simpset} *}
  1349 
  1350 text {* Simplifies x assuming c and y assuming ~c *}
  1351 lemma if_cong:
  1352   assumes "b = c"
  1353       and "c \<Longrightarrow> x = u"
  1354       and "\<not> c \<Longrightarrow> y = v"
  1355   shows "(if b then x else y) = (if c then u else v)"
  1356   unfolding if_def using assms by simp
  1357 
  1358 text {* Prevents simplification of x and y:
  1359   faster and allows the execution of functional programs. *}
  1360 lemma if_weak_cong [cong]:
  1361   assumes "b = c"
  1362   shows "(if b then x else y) = (if c then x else y)"
  1363   using assms by (rule arg_cong)
  1364 
  1365 text {* Prevents simplification of t: much faster *}
  1366 lemma let_weak_cong:
  1367   assumes "a = b"
  1368   shows "(let x = a in t x) = (let x = b in t x)"
  1369   using assms by (rule arg_cong)
  1370 
  1371 text {* To tidy up the result of a simproc.  Only the RHS will be simplified. *}
  1372 lemma eq_cong2:
  1373   assumes "u = u'"
  1374   shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
  1375   using assms by simp
  1376 
  1377 lemma if_distrib:
  1378   "f (if c then x else y) = (if c then f x else f y)"
  1379   by simp
  1380 
  1381 text {* This lemma restricts the effect of the rewrite rule u=v to the left-hand
  1382   side of an equality.  Used in @{text "{Integ,Real}/simproc.ML"} *}
  1383 lemma restrict_to_left:
  1384   assumes "x = y"
  1385   shows "(x = z) = (y = z)"
  1386   using assms by simp
  1387 
  1388 
  1389 subsubsection {* Generic cases and induction *}
  1390 
  1391 text {* Rule projections: *}
  1392 
  1393 ML {*
  1394 structure Project_Rule = Project_Rule
  1395 (
  1396   val conjunct1 = @{thm conjunct1}
  1397   val conjunct2 = @{thm conjunct2}
  1398   val mp = @{thm mp}
  1399 )
  1400 *}
  1401 
  1402 constdefs
  1403   induct_forall where "induct_forall P == \<forall>x. P x"
  1404   induct_implies where "induct_implies A B == A \<longrightarrow> B"
  1405   induct_equal where "induct_equal x y == x = y"
  1406   induct_conj where "induct_conj A B == A \<and> B"
  1407 
  1408 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
  1409   by (unfold atomize_all induct_forall_def)
  1410 
  1411 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
  1412   by (unfold atomize_imp induct_implies_def)
  1413 
  1414 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
  1415   by (unfold atomize_eq induct_equal_def)
  1416 
  1417 lemma induct_conj_eq: "(A &&& B) == Trueprop (induct_conj A B)"
  1418   by (unfold atomize_conj induct_conj_def)
  1419 
  1420 lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
  1421 lemmas induct_rulify [symmetric, standard] = induct_atomize
  1422 lemmas induct_rulify_fallback =
  1423   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
  1424 
  1425 
  1426 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
  1427     induct_conj (induct_forall A) (induct_forall B)"
  1428   by (unfold induct_forall_def induct_conj_def) iprover
  1429 
  1430 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
  1431     induct_conj (induct_implies C A) (induct_implies C B)"
  1432   by (unfold induct_implies_def induct_conj_def) iprover
  1433 
  1434 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
  1435 proof
  1436   assume r: "induct_conj A B ==> PROP C" and A B
  1437   show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
  1438 next
  1439   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
  1440   show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
  1441 qed
  1442 
  1443 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1444 
  1445 hide const induct_forall induct_implies induct_equal induct_conj
  1446 
  1447 text {* Method setup. *}
  1448 
  1449 ML {*
  1450 structure Induct = Induct
  1451 (
  1452   val cases_default = @{thm case_split}
  1453   val atomize = @{thms induct_atomize}
  1454   val rulify = @{thms induct_rulify}
  1455   val rulify_fallback = @{thms induct_rulify_fallback}
  1456 )
  1457 *}
  1458 
  1459 setup Induct.setup
  1460 
  1461 use "~~/src/Tools/induct_tacs.ML"
  1462 setup InductTacs.setup
  1463 
  1464 
  1465 subsubsection {* Coherent logic *}
  1466 
  1467 ML {*
  1468 structure Coherent = CoherentFun
  1469 (
  1470   val atomize_elimL = @{thm atomize_elimL}
  1471   val atomize_exL = @{thm atomize_exL}
  1472   val atomize_conjL = @{thm atomize_conjL}
  1473   val atomize_disjL = @{thm atomize_disjL}
  1474   val operator_names =
  1475     [@{const_name "op |"}, @{const_name "op &"}, @{const_name "Ex"}]
  1476 );
  1477 *}
  1478 
  1479 setup Coherent.setup
  1480 
  1481 
  1482 subsubsection {* Reorienting equalities *}
  1483 
  1484 ML {*
  1485 signature REORIENT_PROC =
  1486 sig
  1487   val init : theory -> theory
  1488   val add : (term -> bool) -> theory -> theory
  1489   val proc : morphism -> simpset -> cterm -> thm option
  1490 end;
  1491 
  1492 structure ReorientProc : REORIENT_PROC =
  1493 struct
  1494   structure Data = TheoryDataFun
  1495   (
  1496     type T = term -> bool;
  1497     val empty = (fn _ => false);
  1498     val copy = I;
  1499     val extend = I;
  1500     fun merge _ (m1, m2) = (fn t => m1 t orelse m2 t);
  1501   )
  1502 
  1503   val init = Data.init;
  1504   fun add m = Data.map (fn matches => fn t => matches t orelse m t);
  1505   val meta_reorient = @{thm eq_commute [THEN eq_reflection]};
  1506   fun proc phi ss ct =
  1507     let
  1508       val ctxt = Simplifier.the_context ss;
  1509       val thy = ProofContext.theory_of ctxt;
  1510       val matches = Data.get thy;
  1511     in
  1512       case Thm.term_of ct of
  1513         (_ $ t $ u) => if matches u then NONE else SOME meta_reorient
  1514       | _ => NONE
  1515     end;
  1516 end;
  1517 *}
  1518 
  1519 setup ReorientProc.init
  1520 
  1521 
  1522 subsection {* Other simple lemmas and lemma duplicates *}
  1523 
  1524 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
  1525   by blast+
  1526 
  1527 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
  1528   apply (rule iffI)
  1529   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
  1530   apply (fast dest!: theI')
  1531   apply (fast intro: ext the1_equality [symmetric])
  1532   apply (erule ex1E)
  1533   apply (rule allI)
  1534   apply (rule ex1I)
  1535   apply (erule spec)
  1536   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
  1537   apply (erule impE)
  1538   apply (rule allI)
  1539   apply (case_tac "xa = x")
  1540   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1541   done
  1542 
  1543 lemmas eq_sym_conv = eq_commute
  1544 
  1545 lemma nnf_simps:
  1546   "(\<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)" 
  1547   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))" 
  1548   "(\<not> \<not>(P)) = P"
  1549 by blast+
  1550 
  1551 
  1552 subsection {* Generic classes and algebraic operations *}
  1553 
  1554 class zero = 
  1555   fixes zero :: 'a  ("0")
  1556 
  1557 class one =
  1558   fixes one  :: 'a  ("1")
  1559 
  1560 lemma Let_0 [simp]: "Let 0 f = f 0"
  1561   unfolding Let_def ..
  1562 
  1563 lemma Let_1 [simp]: "Let 1 f = f 1"
  1564   unfolding Let_def ..
  1565 
  1566 setup {*
  1567   ReorientProc.add
  1568     (fn Const(@{const_name HOL.zero}, _) => true
  1569       | Const(@{const_name HOL.one}, _) => true
  1570       | _ => false)
  1571 *}
  1572 
  1573 simproc_setup reorient_zero ("0 = x") = ReorientProc.proc
  1574 simproc_setup reorient_one ("1 = x") = ReorientProc.proc
  1575 
  1576 typed_print_translation {*
  1577 let
  1578   fun tr' c = (c, fn show_sorts => fn T => fn ts =>
  1579     if (not o null) ts orelse T = dummyT
  1580       orelse not (! show_types) andalso can Term.dest_Type T
  1581     then raise Match
  1582     else Syntax.const Syntax.constrainC $ Syntax.const c $ Syntax.term_of_typ show_sorts T);
  1583 in map tr' [@{const_syntax HOL.one}, @{const_syntax HOL.zero}] end;
  1584 *} -- {* show types that are presumably too general *}
  1585 
  1586 hide (open) const zero one
  1587 
  1588 class plus =
  1589   fixes plus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "+" 65)
  1590 
  1591 class minus =
  1592   fixes minus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "-" 65)
  1593 
  1594 class uminus =
  1595   fixes uminus :: "'a \<Rightarrow> 'a"  ("- _" [81] 80)
  1596 
  1597 class times =
  1598   fixes times :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "*" 70)
  1599 
  1600 class inverse =
  1601   fixes inverse :: "'a \<Rightarrow> 'a"
  1602     and divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "'/" 70)
  1603 
  1604 class abs =
  1605   fixes abs :: "'a \<Rightarrow> 'a"
  1606 begin
  1607 
  1608 notation (xsymbols)
  1609   abs  ("\<bar>_\<bar>")
  1610 
  1611 notation (HTML output)
  1612   abs  ("\<bar>_\<bar>")
  1613 
  1614 end
  1615 
  1616 class sgn =
  1617   fixes sgn :: "'a \<Rightarrow> 'a"
  1618 
  1619 class ord =
  1620   fixes less_eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1621     and less :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1622 begin
  1623 
  1624 notation
  1625   less_eq  ("op <=") and
  1626   less_eq  ("(_/ <= _)" [51, 51] 50) and
  1627   less  ("op <") and
  1628   less  ("(_/ < _)"  [51, 51] 50)
  1629   
  1630 notation (xsymbols)
  1631   less_eq  ("op \<le>") and
  1632   less_eq  ("(_/ \<le> _)"  [51, 51] 50)
  1633 
  1634 notation (HTML output)
  1635   less_eq  ("op \<le>") and
  1636   less_eq  ("(_/ \<le> _)"  [51, 51] 50)
  1637 
  1638 abbreviation (input)
  1639   greater_eq  (infix ">=" 50) where
  1640   "x >= y \<equiv> y <= x"
  1641 
  1642 notation (input)
  1643   greater_eq  (infix "\<ge>" 50)
  1644 
  1645 abbreviation (input)
  1646   greater  (infix ">" 50) where
  1647   "x > y \<equiv> y < x"
  1648 
  1649 end
  1650 
  1651 syntax
  1652   "_index1"  :: index    ("\<^sub>1")
  1653 translations
  1654   (index) "\<^sub>1" => (index) "\<^bsub>\<struct>\<^esub>"
  1655 
  1656 lemma mk_left_commute:
  1657   fixes f (infix "\<otimes>" 60)
  1658   assumes a: "\<And>x y z. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)" and
  1659           c: "\<And>x y. x \<otimes> y = y \<otimes> x"
  1660   shows "x \<otimes> (y \<otimes> z) = y \<otimes> (x \<otimes> z)"
  1661   by (rule trans [OF trans [OF c a] arg_cong [OF c, of "f y"]])
  1662 
  1663 
  1664 subsection {* Basic ML bindings *}
  1665 
  1666 ML {*
  1667 val FalseE = @{thm FalseE}
  1668 val Let_def = @{thm Let_def}
  1669 val TrueI = @{thm TrueI}
  1670 val allE = @{thm allE}
  1671 val allI = @{thm allI}
  1672 val all_dupE = @{thm all_dupE}
  1673 val arg_cong = @{thm arg_cong}
  1674 val box_equals = @{thm box_equals}
  1675 val ccontr = @{thm ccontr}
  1676 val classical = @{thm classical}
  1677 val conjE = @{thm conjE}
  1678 val conjI = @{thm conjI}
  1679 val conjunct1 = @{thm conjunct1}
  1680 val conjunct2 = @{thm conjunct2}
  1681 val disjCI = @{thm disjCI}
  1682 val disjE = @{thm disjE}
  1683 val disjI1 = @{thm disjI1}
  1684 val disjI2 = @{thm disjI2}
  1685 val eq_reflection = @{thm eq_reflection}
  1686 val ex1E = @{thm ex1E}
  1687 val ex1I = @{thm ex1I}
  1688 val ex1_implies_ex = @{thm ex1_implies_ex}
  1689 val exE = @{thm exE}
  1690 val exI = @{thm exI}
  1691 val excluded_middle = @{thm excluded_middle}
  1692 val ext = @{thm ext}
  1693 val fun_cong = @{thm fun_cong}
  1694 val iffD1 = @{thm iffD1}
  1695 val iffD2 = @{thm iffD2}
  1696 val iffI = @{thm iffI}
  1697 val impE = @{thm impE}
  1698 val impI = @{thm impI}
  1699 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1700 val mp = @{thm mp}
  1701 val notE = @{thm notE}
  1702 val notI = @{thm notI}
  1703 val not_all = @{thm not_all}
  1704 val not_ex = @{thm not_ex}
  1705 val not_iff = @{thm not_iff}
  1706 val not_not = @{thm not_not}
  1707 val not_sym = @{thm not_sym}
  1708 val refl = @{thm refl}
  1709 val rev_mp = @{thm rev_mp}
  1710 val spec = @{thm spec}
  1711 val ssubst = @{thm ssubst}
  1712 val subst = @{thm subst}
  1713 val sym = @{thm sym}
  1714 val trans = @{thm trans}
  1715 *}
  1716 
  1717 
  1718 subsection {* Code generator setup *}
  1719 
  1720 subsubsection {* SML code generator setup *}
  1721 
  1722 use "Tools/recfun_codegen.ML"
  1723 
  1724 setup {*
  1725   Codegen.setup
  1726   #> RecfunCodegen.setup
  1727   #> Codegen.map_unfold (K HOL_basic_ss)
  1728 *}
  1729 
  1730 types_code
  1731   "bool"  ("bool")
  1732 attach (term_of) {*
  1733 fun term_of_bool b = if b then HOLogic.true_const else HOLogic.false_const;
  1734 *}
  1735 attach (test) {*
  1736 fun gen_bool i =
  1737   let val b = one_of [false, true]
  1738   in (b, fn () => term_of_bool b) end;
  1739 *}
  1740   "prop"  ("bool")
  1741 attach (term_of) {*
  1742 fun term_of_prop b =
  1743   HOLogic.mk_Trueprop (if b then HOLogic.true_const else HOLogic.false_const);
  1744 *}
  1745 
  1746 consts_code
  1747   "Trueprop" ("(_)")
  1748   "True"    ("true")
  1749   "False"   ("false")
  1750   "Not"     ("Bool.not")
  1751   "op |"    ("(_ orelse/ _)")
  1752   "op &"    ("(_ andalso/ _)")
  1753   "If"      ("(if _/ then _/ else _)")
  1754 
  1755 setup {*
  1756 let
  1757 
  1758 fun eq_codegen thy defs dep thyname b t gr =
  1759     (case strip_comb t of
  1760        (Const ("op =", Type (_, [Type ("fun", _), _])), _) => NONE
  1761      | (Const ("op =", _), [t, u]) =>
  1762           let
  1763             val (pt, gr') = Codegen.invoke_codegen thy defs dep thyname false t gr;
  1764             val (pu, gr'') = Codegen.invoke_codegen thy defs dep thyname false u gr';
  1765             val (_, gr''') = Codegen.invoke_tycodegen thy defs dep thyname false HOLogic.boolT gr'';
  1766           in
  1767             SOME (Codegen.parens
  1768               (Pretty.block [pt, Codegen.str " =", Pretty.brk 1, pu]), gr''')
  1769           end
  1770      | (t as Const ("op =", _), ts) => SOME (Codegen.invoke_codegen
  1771          thy defs dep thyname b (Codegen.eta_expand t ts 2) gr)
  1772      | _ => NONE);
  1773 
  1774 in
  1775   Codegen.add_codegen "eq_codegen" eq_codegen
  1776 end
  1777 *}
  1778 
  1779 subsubsection {* Generic code generator preprocessor setup *}
  1780 
  1781 setup {*
  1782   Code_Preproc.map_pre (K HOL_basic_ss)
  1783   #> Code_Preproc.map_post (K HOL_basic_ss)
  1784 *}
  1785 
  1786 subsubsection {* Equality *}
  1787 
  1788 class eq =
  1789   fixes eq :: "'a \<Rightarrow> 'a \<Rightarrow> bool"
  1790   assumes eq_equals: "eq x y \<longleftrightarrow> x = y"
  1791 begin
  1792 
  1793 lemma eq [code_unfold, code_inline del]: "eq = (op =)"
  1794   by (rule ext eq_equals)+
  1795 
  1796 lemma eq_refl: "eq x x \<longleftrightarrow> True"
  1797   unfolding eq by rule+
  1798 
  1799 lemma equals_eq: "(op =) \<equiv> eq"
  1800   by (rule eq_reflection) (rule ext, rule ext, rule sym, rule eq_equals)
  1801 
  1802 declare equals_eq [symmetric, code_post]
  1803 
  1804 end
  1805 
  1806 declare equals_eq [code]
  1807 
  1808 setup {*
  1809   Code_Preproc.map_pre (fn simpset =>
  1810     simpset addsimprocs [Simplifier.simproc_i @{theory} "eq" [@{term "op ="}]
  1811       (fn thy => fn _ => fn t as Const (_, T) => case strip_type T
  1812         of ((T as Type _) :: _, _) => SOME @{thm equals_eq}
  1813          | _ => NONE)])
  1814 *}
  1815 
  1816 
  1817 subsubsection {* Generic code generator foundation *}
  1818 
  1819 text {* Datatypes *}
  1820 
  1821 code_datatype True False
  1822 
  1823 code_datatype "TYPE('a\<Colon>{})"
  1824 
  1825 code_datatype Trueprop "prop"
  1826 
  1827 text {* Code equations *}
  1828 
  1829 lemma [code]:
  1830   shows "(True \<Longrightarrow> PROP P) \<equiv> PROP P" 
  1831     and "(False \<Longrightarrow> Q) \<equiv> Trueprop True" 
  1832     and "(PROP P \<Longrightarrow> True) \<equiv> Trueprop True" 
  1833     and "(Q \<Longrightarrow> False) \<equiv> Trueprop (\<not> Q)" by (auto intro!: equal_intr_rule)
  1834 
  1835 lemma [code]:
  1836   shows "False \<and> x \<longleftrightarrow> False"
  1837     and "True \<and> x \<longleftrightarrow> x"
  1838     and "x \<and> False \<longleftrightarrow> False"
  1839     and "x \<and> True \<longleftrightarrow> x" by simp_all
  1840 
  1841 lemma [code]:
  1842   shows "False \<or> x \<longleftrightarrow> x"
  1843     and "True \<or> x \<longleftrightarrow> True"
  1844     and "x \<or> False \<longleftrightarrow> x"
  1845     and "x \<or> True \<longleftrightarrow> True" by simp_all
  1846 
  1847 declare imp_conv_disj [code, code_unfold_post]
  1848 
  1849 instantiation itself :: (type) eq
  1850 begin
  1851 
  1852 definition eq_itself :: "'a itself \<Rightarrow> 'a itself \<Rightarrow> bool" where
  1853   "eq_itself x y \<longleftrightarrow> x = y"
  1854 
  1855 instance proof
  1856 qed (fact eq_itself_def)
  1857 
  1858 end
  1859 
  1860 lemma eq_itself_code [code]:
  1861   "eq_class.eq TYPE('a) TYPE('a) \<longleftrightarrow> True"
  1862   by (simp add: eq)
  1863 
  1864 text {* Equality *}
  1865 
  1866 declare simp_thms(6) [code nbe]
  1867 
  1868 setup {*
  1869   Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>type \<Rightarrow> 'a \<Rightarrow> bool"})
  1870 *}
  1871 
  1872 lemma equals_alias_cert: "OFCLASS('a, eq_class) \<equiv> ((op = :: 'a \<Rightarrow> 'a \<Rightarrow> bool) \<equiv> eq)" (is "?ofclass \<equiv> ?eq")
  1873 proof
  1874   assume "PROP ?ofclass"
  1875   show "PROP ?eq"
  1876     by (tactic {* ALLGOALS (rtac (Drule.unconstrainTs @{thm equals_eq})) *}) 
  1877       (fact `PROP ?ofclass`)
  1878 next
  1879   assume "PROP ?eq"
  1880   show "PROP ?ofclass" proof
  1881   qed (simp add: `PROP ?eq`)
  1882 qed
  1883   
  1884 setup {*
  1885   Sign.add_const_constraint (@{const_name eq}, SOME @{typ "'a\<Colon>eq \<Rightarrow> 'a \<Rightarrow> bool"})
  1886 *}
  1887 
  1888 setup {*
  1889   Code.add_const_alias @{thm equals_alias_cert}
  1890 *}
  1891 
  1892 hide (open) const eq
  1893 hide const eq
  1894 
  1895 text {* Cases *}
  1896 
  1897 lemma Let_case_cert:
  1898   assumes "CASE \<equiv> (\<lambda>x. Let x f)"
  1899   shows "CASE x \<equiv> f x"
  1900   using assms by simp_all
  1901 
  1902 lemma If_case_cert:
  1903   assumes "CASE \<equiv> (\<lambda>b. If b f g)"
  1904   shows "(CASE True \<equiv> f) &&& (CASE False \<equiv> g)"
  1905   using assms by simp_all
  1906 
  1907 setup {*
  1908   Code.add_case @{thm Let_case_cert}
  1909   #> Code.add_case @{thm If_case_cert}
  1910   #> Code.add_undefined @{const_name undefined}
  1911 *}
  1912 
  1913 code_abort undefined
  1914 
  1915 subsubsection {* Generic code generator target languages *}
  1916 
  1917 text {* type bool *}
  1918 
  1919 code_type bool
  1920   (SML "bool")
  1921   (OCaml "bool")
  1922   (Haskell "Bool")
  1923 
  1924 code_const True and False and Not and "op &" and "op |" and If
  1925   (SML "true" and "false" and "not"
  1926     and infixl 1 "andalso" and infixl 0 "orelse"
  1927     and "!(if (_)/ then (_)/ else (_))")
  1928   (OCaml "true" and "false" and "not"
  1929     and infixl 4 "&&" and infixl 2 "||"
  1930     and "!(if (_)/ then (_)/ else (_))")
  1931   (Haskell "True" and "False" and "not"
  1932     and infixl 3 "&&" and infixl 2 "||"
  1933     and "!(if (_)/ then (_)/ else (_))")
  1934 
  1935 code_reserved SML
  1936   bool true false not
  1937 
  1938 code_reserved OCaml
  1939   bool not
  1940 
  1941 text {* using built-in Haskell equality *}
  1942 
  1943 code_class eq
  1944   (Haskell "Eq")
  1945 
  1946 code_const "eq_class.eq"
  1947   (Haskell infixl 4 "==")
  1948 
  1949 code_const "op ="
  1950   (Haskell infixl 4 "==")
  1951 
  1952 text {* undefined *}
  1953 
  1954 code_const undefined
  1955   (SML "!(raise/ Fail/ \"undefined\")")
  1956   (OCaml "failwith/ \"undefined\"")
  1957   (Haskell "error/ \"undefined\"")
  1958 
  1959 subsubsection {* Evaluation and normalization by evaluation *}
  1960 
  1961 setup {*
  1962   Value.add_evaluator ("SML", Codegen.eval_term o ProofContext.theory_of)
  1963 *}
  1964 
  1965 ML {*
  1966 structure Eval_Method =
  1967 struct
  1968 
  1969 val eval_ref : (unit -> bool) option ref = ref NONE;
  1970 
  1971 end;
  1972 *}
  1973 
  1974 oracle eval_oracle = {* fn ct =>
  1975   let
  1976     val thy = Thm.theory_of_cterm ct;
  1977     val t = Thm.term_of ct;
  1978     val dummy = @{cprop True};
  1979   in case try HOLogic.dest_Trueprop t
  1980    of SOME t' => if Code_ML.eval NONE
  1981          ("Eval_Method.eval_ref", Eval_Method.eval_ref) (K I) thy t' [] 
  1982        then Thm.capply (Thm.capply @{cterm "op \<equiv> \<Colon> prop \<Rightarrow> prop \<Rightarrow> prop"} ct) dummy
  1983        else dummy
  1984     | NONE => dummy
  1985   end
  1986 *}
  1987 
  1988 ML {*
  1989 fun gen_eval_method conv ctxt = SIMPLE_METHOD'
  1990   (CONVERSION (Conv.params_conv (~1) (K (Conv.concl_conv (~1) conv)) ctxt)
  1991     THEN' rtac TrueI)
  1992 *}
  1993 
  1994 method_setup eval = {* Scan.succeed (gen_eval_method eval_oracle) *}
  1995   "solve goal by evaluation"
  1996 
  1997 method_setup evaluation = {* Scan.succeed (gen_eval_method Codegen.evaluation_conv) *}
  1998   "solve goal by evaluation"
  1999 
  2000 method_setup normalization = {*
  2001   Scan.succeed (K (SIMPLE_METHOD' (CONVERSION Nbe.norm_conv THEN' (fn k => TRY (rtac TrueI k)))))
  2002 *} "solve goal by normalization"
  2003 
  2004 
  2005 subsubsection {* Quickcheck *}
  2006 
  2007 ML {*
  2008 structure Quickcheck_RecFun_Simps = Named_Thms
  2009 (
  2010   val name = "quickcheck_recfun_simp"
  2011   val description = "simplification rules of recursive functions as needed by Quickcheck"
  2012 )
  2013 *}
  2014 
  2015 setup Quickcheck_RecFun_Simps.setup
  2016 
  2017 setup {*
  2018   Quickcheck.add_generator ("SML", Codegen.test_term)
  2019 *}
  2020 
  2021 quickcheck_params [size = 5, iterations = 50]
  2022 
  2023 
  2024 subsection {* Nitpick setup *}
  2025 
  2026 text {* This will be relocated once Nitpick is moved to HOL. *}
  2027 
  2028 ML {*
  2029 structure Nitpick_Const_Defs = Named_Thms
  2030 (
  2031   val name = "nitpick_const_def"
  2032   val description = "alternative definitions of constants as needed by Nitpick"
  2033 )
  2034 structure Nitpick_Const_Simps = Named_Thms
  2035 (
  2036   val name = "nitpick_const_simp"
  2037   val description = "equational specification of constants as needed by Nitpick"
  2038 )
  2039 structure Nitpick_Const_Psimps = Named_Thms
  2040 (
  2041   val name = "nitpick_const_psimp"
  2042   val description = "partial equational specification of constants as needed by Nitpick"
  2043 )
  2044 structure Nitpick_Ind_Intros = Named_Thms
  2045 (
  2046   val name = "nitpick_ind_intro"
  2047   val description = "introduction rules for (co)inductive predicates as needed by Nitpick"
  2048 )
  2049 *}
  2050 
  2051 setup {*
  2052   Nitpick_Const_Defs.setup
  2053   #> Nitpick_Const_Simps.setup
  2054   #> Nitpick_Const_Psimps.setup
  2055   #> Nitpick_Ind_Intros.setup
  2056 *}
  2057 
  2058 
  2059 subsection {* Legacy tactics and ML bindings *}
  2060 
  2061 ML {*
  2062 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
  2063 
  2064 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  2065 local
  2066   fun wrong_prem (Const ("All", _) $ (Abs (_, _, t))) = wrong_prem t
  2067     | wrong_prem (Bound _) = true
  2068     | wrong_prem _ = false;
  2069   val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  2070 in
  2071   fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
  2072   fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
  2073 end;
  2074 
  2075 val all_conj_distrib = thm "all_conj_distrib";
  2076 val all_simps = thms "all_simps";
  2077 val atomize_not = thm "atomize_not";
  2078 val case_split = thm "case_split";
  2079 val cases_simp = thm "cases_simp";
  2080 val choice_eq = thm "choice_eq"
  2081 val cong = thm "cong"
  2082 val conj_comms = thms "conj_comms";
  2083 val conj_cong = thm "conj_cong";
  2084 val de_Morgan_conj = thm "de_Morgan_conj";
  2085 val de_Morgan_disj = thm "de_Morgan_disj";
  2086 val disj_assoc = thm "disj_assoc";
  2087 val disj_comms = thms "disj_comms";
  2088 val disj_cong = thm "disj_cong";
  2089 val eq_ac = thms "eq_ac";
  2090 val eq_cong2 = thm "eq_cong2"
  2091 val Eq_FalseI = thm "Eq_FalseI";
  2092 val Eq_TrueI = thm "Eq_TrueI";
  2093 val Ex1_def = thm "Ex1_def"
  2094 val ex_disj_distrib = thm "ex_disj_distrib";
  2095 val ex_simps = thms "ex_simps";
  2096 val if_cancel = thm "if_cancel";
  2097 val if_eq_cancel = thm "if_eq_cancel";
  2098 val if_False = thm "if_False";
  2099 val iff_conv_conj_imp = thm "iff_conv_conj_imp";
  2100 val iff = thm "iff"
  2101 val if_splits = thms "if_splits";
  2102 val if_True = thm "if_True";
  2103 val if_weak_cong = thm "if_weak_cong"
  2104 val imp_all = thm "imp_all";
  2105 val imp_cong = thm "imp_cong";
  2106 val imp_conjL = thm "imp_conjL";
  2107 val imp_conjR = thm "imp_conjR";
  2108 val imp_conv_disj = thm "imp_conv_disj";
  2109 val simp_implies_def = thm "simp_implies_def";
  2110 val simp_thms = thms "simp_thms";
  2111 val split_if = thm "split_if";
  2112 val the1_equality = thm "the1_equality"
  2113 val theI = thm "theI"
  2114 val theI' = thm "theI'"
  2115 val True_implies_equals = thm "True_implies_equals";
  2116 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})
  2117 
  2118 *}
  2119 
  2120 end