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