src/HOL/HOL.thy
author wenzelm
Thu May 31 20:55:29 2007 +0200 (2007-05-31)
changeset 23171 861f63a35d31
parent 23163 eef345eff987
child 23247 b99dce43d252
permissions -rw-r--r--
moved IsaPlanner from Provers to Tools;
     1 (*  Title:      HOL/HOL.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
     4 *)
     5 
     6 header {* The basis of Higher-Order Logic *}
     7 
     8 theory HOL
     9 imports CPure
    10 uses
    11   "hologic.ML"
    12   "~~/src/Provers/splitter.ML"
    13   "~~/src/Provers/hypsubst.ML"
    14   "~~/src/Tools/IsaPlanner/zipper.ML"
    15   "~~/src/Tools/IsaPlanner/isand.ML"
    16   "~~/src/Tools/IsaPlanner/rw_tools.ML"
    17   "~~/src/Tools/IsaPlanner/rw_inst.ML"
    18   "~~/src/Provers/eqsubst.ML"
    19   "~~/src/Provers/induct_method.ML"
    20   "~~/src/Provers/classical.ML"
    21   "~~/src/Provers/blast.ML"
    22   "~~/src/Provers/clasimp.ML"
    23   "~~/src/Pure/General/int.ML"
    24   "~~/src/Pure/General/rat.ML"
    25   "~~/src/Provers/Arith/fast_lin_arith.ML"
    26   "~~/src/Provers/Arith/cancel_sums.ML"
    27   "~~/src/Provers/quantifier1.ML"
    28   "~~/src/Provers/project_rule.ML"
    29   "~~/src/Provers/Arith/cancel_numerals.ML"
    30   "~~/src/Provers/Arith/combine_numerals.ML"
    31   "~~/src/Provers/Arith/cancel_numeral_factor.ML"
    32   "~~/src/Provers/Arith/extract_common_term.ML"
    33   "~~/src/Provers/quasi.ML"
    34   "~~/src/Provers/order.ML"
    35   ("simpdata.ML")
    36   "Tools/res_atpset.ML"
    37 begin
    38 
    39 subsection {* Primitive logic *}
    40 
    41 subsubsection {* Core syntax *}
    42 
    43 classes type
    44 defaultsort type
    45 
    46 global
    47 
    48 typedecl bool
    49 
    50 arities
    51   bool :: type
    52   "fun" :: (type, type) type
    53 
    54 judgment
    55   Trueprop      :: "bool => prop"                   ("(_)" 5)
    56 
    57 consts
    58   Not           :: "bool => bool"                   ("~ _" [40] 40)
    59   True          :: bool
    60   False         :: bool
    61   arbitrary     :: 'a
    62 
    63   The           :: "('a => bool) => 'a"
    64   All           :: "('a => bool) => bool"           (binder "ALL " 10)
    65   Ex            :: "('a => bool) => bool"           (binder "EX " 10)
    66   Ex1           :: "('a => bool) => bool"           (binder "EX! " 10)
    67   Let           :: "['a, 'a => 'b] => 'b"
    68 
    69   "op ="        :: "['a, 'a] => bool"               (infixl "=" 50)
    70   "op &"        :: "[bool, bool] => bool"           (infixr "&" 35)
    71   "op |"        :: "[bool, bool] => bool"           (infixr "|" 30)
    72   "op -->"      :: "[bool, bool] => bool"           (infixr "-->" 25)
    73 
    74 local
    75 
    76 consts
    77   If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
    78 
    79 
    80 subsubsection {* Additional concrete syntax *}
    81 
    82 notation (output)
    83   "op ="  (infix "=" 50)
    84 
    85 abbreviation
    86   not_equal :: "['a, 'a] => bool"  (infixl "~=" 50) where
    87   "x ~= y == ~ (x = y)"
    88 
    89 notation (output)
    90   not_equal  (infix "~=" 50)
    91 
    92 notation (xsymbols)
    93   Not  ("\<not> _" [40] 40) and
    94   "op &"  (infixr "\<and>" 35) and
    95   "op |"  (infixr "\<or>" 30) and
    96   "op -->"  (infixr "\<longrightarrow>" 25) and
    97   not_equal  (infix "\<noteq>" 50)
    98 
    99 notation (HTML output)
   100   Not  ("\<not> _" [40] 40) and
   101   "op &"  (infixr "\<and>" 35) and
   102   "op |"  (infixr "\<or>" 30) and
   103   not_equal  (infix "\<noteq>" 50)
   104 
   105 abbreviation (iff)
   106   iff :: "[bool, bool] => bool"  (infixr "<->" 25) where
   107   "A <-> B == A = B"
   108 
   109 notation (xsymbols)
   110   iff  (infixr "\<longleftrightarrow>" 25)
   111 
   112 
   113 nonterminals
   114   letbinds  letbind
   115   case_syn  cases_syn
   116 
   117 syntax
   118   "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
   119 
   120   "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
   121   ""            :: "letbind => letbinds"                 ("_")
   122   "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
   123   "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" 10)
   124 
   125   "_case_syntax":: "['a, cases_syn] => 'b"               ("(case _ of/ _)" 10)
   126   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ =>/ _)" 10)
   127   ""            :: "case_syn => cases_syn"               ("_")
   128   "_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
   129 
   130 translations
   131   "THE x. P"              == "The (%x. P)"
   132   "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
   133   "let x = a in e"        == "Let a (%x. e)"
   134 
   135 print_translation {*
   136 (* To avoid eta-contraction of body: *)
   137 [("The", fn [Abs abs] =>
   138      let val (x,t) = atomic_abs_tr' abs
   139      in Syntax.const "_The" $ x $ t end)]
   140 *}
   141 
   142 syntax (xsymbols)
   143   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
   144 
   145 notation (xsymbols)
   146   All  (binder "\<forall>" 10) and
   147   Ex  (binder "\<exists>" 10) and
   148   Ex1  (binder "\<exists>!" 10)
   149 
   150 notation (HTML output)
   151   All  (binder "\<forall>" 10) and
   152   Ex  (binder "\<exists>" 10) and
   153   Ex1  (binder "\<exists>!" 10)
   154 
   155 notation (HOL)
   156   All  (binder "! " 10) and
   157   Ex  (binder "? " 10) and
   158   Ex1  (binder "?! " 10)
   159 
   160 
   161 subsubsection {* Axioms and basic definitions *}
   162 
   163 axioms
   164   eq_reflection:  "(x=y) ==> (x==y)"
   165 
   166   refl:           "t = (t::'a)"
   167 
   168   ext:            "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
   169     -- {*Extensionality is built into the meta-logic, and this rule expresses
   170          a related property.  It is an eta-expanded version of the traditional
   171          rule, and similar to the ABS rule of HOL*}
   172 
   173   the_eq_trivial: "(THE x. x = a) = (a::'a)"
   174 
   175   impI:           "(P ==> Q) ==> P-->Q"
   176   mp:             "[| P-->Q;  P |] ==> Q"
   177 
   178 
   179 defs
   180   True_def:     "True      == ((%x::bool. x) = (%x. x))"
   181   All_def:      "All(P)    == (P = (%x. True))"
   182   Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
   183   False_def:    "False     == (!P. P)"
   184   not_def:      "~ P       == P-->False"
   185   and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
   186   or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
   187   Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
   188 
   189 axioms
   190   iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
   191   True_or_False:  "(P=True) | (P=False)"
   192 
   193 defs
   194   Let_def [code func]: "Let s f == f(s)"
   195   if_def:       "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
   196 
   197 finalconsts
   198   "op ="
   199   "op -->"
   200   The
   201   arbitrary
   202 
   203 axiomatization
   204   undefined :: 'a
   205 
   206 axiomatization where
   207   undefined_fun: "undefined x = undefined"
   208 
   209 
   210 subsubsection {* Generic classes and algebraic operations *}
   211 
   212 class default = type +
   213   fixes default :: "'a"
   214 
   215 class zero = type + 
   216   fixes zero :: "'a"  ("\<^loc>0")
   217 
   218 class one = type +
   219   fixes one  :: "'a"  ("\<^loc>1")
   220 
   221 hide (open) const zero one
   222 
   223 class plus = type +
   224   fixes plus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>+" 65)
   225 
   226 class minus = type +
   227   fixes uminus :: "'a \<Rightarrow> 'a" 
   228     and minus :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>-" 65)
   229     and abs :: "'a \<Rightarrow> 'a"
   230 
   231 class times = type +
   232   fixes times :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>*" 70)
   233 
   234 class inverse = type +
   235   fixes inverse :: "'a \<Rightarrow> 'a"
   236     and divide :: "'a \<Rightarrow> 'a \<Rightarrow> 'a"  (infixl "\<^loc>'/" 70)
   237 
   238 notation
   239   uminus  ("- _" [81] 80)
   240 
   241 notation (xsymbols)
   242   abs  ("\<bar>_\<bar>")
   243 notation (HTML output)
   244   abs  ("\<bar>_\<bar>")
   245 
   246 syntax
   247   "_index1"  :: index    ("\<^sub>1")
   248 translations
   249   (index) "\<^sub>1" => (index) "\<^bsub>\<struct>\<^esub>"
   250 
   251 typed_print_translation {*
   252 let
   253   fun tr' c = (c, fn show_sorts => fn T => fn ts =>
   254     if T = dummyT orelse not (! show_types) andalso can Term.dest_Type T then raise Match
   255     else Syntax.const Syntax.constrainC $ Syntax.const c $ Syntax.term_of_typ show_sorts T);
   256 in map tr' [@{const_syntax HOL.one}, @{const_syntax HOL.zero}] end;
   257 *} -- {* show types that are presumably too general *}
   258 
   259 
   260 subsection {* Fundamental rules *}
   261 
   262 subsubsection {* Equality *}
   263 
   264 text {* Thanks to Stephan Merz *}
   265 lemma subst:
   266   assumes eq: "s = t" and p: "P s"
   267   shows "P t"
   268 proof -
   269   from eq have meta: "s \<equiv> t"
   270     by (rule eq_reflection)
   271   from p show ?thesis
   272     by (unfold meta)
   273 qed
   274 
   275 lemma sym: "s = t ==> t = s"
   276   by (erule subst) (rule refl)
   277 
   278 lemma ssubst: "t = s ==> P s ==> P t"
   279   by (drule sym) (erule subst)
   280 
   281 lemma trans: "[| r=s; s=t |] ==> r=t"
   282   by (erule subst)
   283 
   284 lemma meta_eq_to_obj_eq: 
   285   assumes meq: "A == B"
   286   shows "A = B"
   287   by (unfold meq) (rule refl)
   288 
   289 text {* Useful with @{text erule} for proving equalities from known equalities. *}
   290      (* a = b
   291         |   |
   292         c = d   *)
   293 lemma box_equals: "[| a=b;  a=c;  b=d |] ==> c=d"
   294 apply (rule trans)
   295 apply (rule trans)
   296 apply (rule sym)
   297 apply assumption+
   298 done
   299 
   300 text {* For calculational reasoning: *}
   301 
   302 lemma forw_subst: "a = b ==> P b ==> P a"
   303   by (rule ssubst)
   304 
   305 lemma back_subst: "P a ==> a = b ==> P b"
   306   by (rule subst)
   307 
   308 
   309 subsubsection {*Congruence rules for application*}
   310 
   311 (*similar to AP_THM in Gordon's HOL*)
   312 lemma fun_cong: "(f::'a=>'b) = g ==> f(x)=g(x)"
   313 apply (erule subst)
   314 apply (rule refl)
   315 done
   316 
   317 (*similar to AP_TERM in Gordon's HOL and FOL's subst_context*)
   318 lemma arg_cong: "x=y ==> f(x)=f(y)"
   319 apply (erule subst)
   320 apply (rule refl)
   321 done
   322 
   323 lemma arg_cong2: "\<lbrakk> a = b; c = d \<rbrakk> \<Longrightarrow> f a c = f b d"
   324 apply (erule ssubst)+
   325 apply (rule refl)
   326 done
   327 
   328 lemma cong: "[| f = g; (x::'a) = y |] ==> f(x) = g(y)"
   329 apply (erule subst)+
   330 apply (rule refl)
   331 done
   332 
   333 
   334 subsubsection {*Equality of booleans -- iff*}
   335 
   336 lemma iffI: assumes "P ==> Q" and "Q ==> P" shows "P=Q"
   337   by (iprover intro: iff [THEN mp, THEN mp] impI assms)
   338 
   339 lemma iffD2: "[| P=Q; Q |] ==> P"
   340   by (erule ssubst)
   341 
   342 lemma rev_iffD2: "[| Q; P=Q |] ==> P"
   343   by (erule iffD2)
   344 
   345 lemma iffD1: "Q = P \<Longrightarrow> Q \<Longrightarrow> P"
   346   by (drule sym) (rule iffD2)
   347 
   348 lemma rev_iffD1: "Q \<Longrightarrow> Q = P \<Longrightarrow> P"
   349   by (drule sym) (rule rev_iffD2)
   350 
   351 lemma iffE:
   352   assumes major: "P=Q"
   353     and minor: "[| P --> Q; Q --> P |] ==> R"
   354   shows R
   355   by (iprover intro: minor impI major [THEN iffD2] major [THEN iffD1])
   356 
   357 
   358 subsubsection {*True*}
   359 
   360 lemma TrueI: "True"
   361   unfolding True_def by (rule refl)
   362 
   363 lemma eqTrueI: "P ==> P = True"
   364   by (iprover intro: iffI TrueI)
   365 
   366 lemma eqTrueE: "P = True ==> P"
   367   by (erule iffD2) (rule TrueI)
   368 
   369 
   370 subsubsection {*Universal quantifier*}
   371 
   372 lemma allI: assumes "!!x::'a. P(x)" shows "ALL x. P(x)"
   373   unfolding All_def by (iprover intro: ext eqTrueI assms)
   374 
   375 lemma spec: "ALL x::'a. P(x) ==> P(x)"
   376 apply (unfold All_def)
   377 apply (rule eqTrueE)
   378 apply (erule fun_cong)
   379 done
   380 
   381 lemma allE:
   382   assumes major: "ALL x. P(x)"
   383     and minor: "P(x) ==> R"
   384   shows R
   385   by (iprover intro: minor major [THEN spec])
   386 
   387 lemma all_dupE:
   388   assumes major: "ALL x. P(x)"
   389     and minor: "[| P(x); ALL x. P(x) |] ==> R"
   390   shows R
   391   by (iprover intro: minor major major [THEN spec])
   392 
   393 
   394 subsubsection {* False *}
   395 
   396 text {*
   397   Depends upon @{text spec}; it is impossible to do propositional
   398   logic before quantifiers!
   399 *}
   400 
   401 lemma FalseE: "False ==> P"
   402   apply (unfold False_def)
   403   apply (erule spec)
   404   done
   405 
   406 lemma False_neq_True: "False = True ==> P"
   407   by (erule eqTrueE [THEN FalseE])
   408 
   409 
   410 subsubsection {* Negation *}
   411 
   412 lemma notI:
   413   assumes "P ==> False"
   414   shows "~P"
   415   apply (unfold not_def)
   416   apply (iprover intro: impI assms)
   417   done
   418 
   419 lemma False_not_True: "False ~= True"
   420   apply (rule notI)
   421   apply (erule False_neq_True)
   422   done
   423 
   424 lemma True_not_False: "True ~= False"
   425   apply (rule notI)
   426   apply (drule sym)
   427   apply (erule False_neq_True)
   428   done
   429 
   430 lemma notE: "[| ~P;  P |] ==> R"
   431   apply (unfold not_def)
   432   apply (erule mp [THEN FalseE])
   433   apply assumption
   434   done
   435 
   436 lemma notI2: "(P \<Longrightarrow> \<not> Pa) \<Longrightarrow> (P \<Longrightarrow> Pa) \<Longrightarrow> \<not> P"
   437   by (erule notE [THEN notI]) (erule meta_mp)
   438 
   439 
   440 subsubsection {*Implication*}
   441 
   442 lemma impE:
   443   assumes "P-->Q" "P" "Q ==> R"
   444   shows "R"
   445 by (iprover intro: prems mp)
   446 
   447 (* Reduces Q to P-->Q, allowing substitution in P. *)
   448 lemma rev_mp: "[| P;  P --> Q |] ==> Q"
   449 by (iprover intro: mp)
   450 
   451 lemma contrapos_nn:
   452   assumes major: "~Q"
   453       and minor: "P==>Q"
   454   shows "~P"
   455 by (iprover intro: notI minor major [THEN notE])
   456 
   457 (*not used at all, but we already have the other 3 combinations *)
   458 lemma contrapos_pn:
   459   assumes major: "Q"
   460       and minor: "P ==> ~Q"
   461   shows "~P"
   462 by (iprover intro: notI minor major notE)
   463 
   464 lemma not_sym: "t ~= s ==> s ~= t"
   465   by (erule contrapos_nn) (erule sym)
   466 
   467 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
   468   by (erule subst, erule ssubst, assumption)
   469 
   470 (*still used in HOLCF*)
   471 lemma rev_contrapos:
   472   assumes pq: "P ==> Q"
   473       and nq: "~Q"
   474   shows "~P"
   475 apply (rule nq [THEN contrapos_nn])
   476 apply (erule pq)
   477 done
   478 
   479 subsubsection {*Existential quantifier*}
   480 
   481 lemma exI: "P x ==> EX x::'a. P x"
   482 apply (unfold Ex_def)
   483 apply (iprover intro: allI allE impI mp)
   484 done
   485 
   486 lemma exE:
   487   assumes major: "EX x::'a. P(x)"
   488       and minor: "!!x. P(x) ==> Q"
   489   shows "Q"
   490 apply (rule major [unfolded Ex_def, THEN spec, THEN mp])
   491 apply (iprover intro: impI [THEN allI] minor)
   492 done
   493 
   494 
   495 subsubsection {*Conjunction*}
   496 
   497 lemma conjI: "[| P; Q |] ==> P&Q"
   498 apply (unfold and_def)
   499 apply (iprover intro: impI [THEN allI] mp)
   500 done
   501 
   502 lemma conjunct1: "[| P & Q |] ==> P"
   503 apply (unfold and_def)
   504 apply (iprover intro: impI dest: spec mp)
   505 done
   506 
   507 lemma conjunct2: "[| P & Q |] ==> Q"
   508 apply (unfold and_def)
   509 apply (iprover intro: impI dest: spec mp)
   510 done
   511 
   512 lemma conjE:
   513   assumes major: "P&Q"
   514       and minor: "[| P; Q |] ==> R"
   515   shows "R"
   516 apply (rule minor)
   517 apply (rule major [THEN conjunct1])
   518 apply (rule major [THEN conjunct2])
   519 done
   520 
   521 lemma context_conjI:
   522   assumes prems: "P" "P ==> Q" shows "P & Q"
   523 by (iprover intro: conjI prems)
   524 
   525 
   526 subsubsection {*Disjunction*}
   527 
   528 lemma disjI1: "P ==> P|Q"
   529 apply (unfold or_def)
   530 apply (iprover intro: allI impI mp)
   531 done
   532 
   533 lemma disjI2: "Q ==> P|Q"
   534 apply (unfold or_def)
   535 apply (iprover intro: allI impI mp)
   536 done
   537 
   538 lemma disjE:
   539   assumes major: "P|Q"
   540       and minorP: "P ==> R"
   541       and minorQ: "Q ==> R"
   542   shows "R"
   543 by (iprover intro: minorP minorQ impI
   544                  major [unfolded or_def, THEN spec, THEN mp, THEN mp])
   545 
   546 
   547 subsubsection {*Classical logic*}
   548 
   549 lemma classical:
   550   assumes prem: "~P ==> P"
   551   shows "P"
   552 apply (rule True_or_False [THEN disjE, THEN eqTrueE])
   553 apply assumption
   554 apply (rule notI [THEN prem, THEN eqTrueI])
   555 apply (erule subst)
   556 apply assumption
   557 done
   558 
   559 lemmas ccontr = FalseE [THEN classical, standard]
   560 
   561 (*notE with premises exchanged; it discharges ~R so that it can be used to
   562   make elimination rules*)
   563 lemma rev_notE:
   564   assumes premp: "P"
   565       and premnot: "~R ==> ~P"
   566   shows "R"
   567 apply (rule ccontr)
   568 apply (erule notE [OF premnot premp])
   569 done
   570 
   571 (*Double negation law*)
   572 lemma notnotD: "~~P ==> P"
   573 apply (rule classical)
   574 apply (erule notE)
   575 apply assumption
   576 done
   577 
   578 lemma contrapos_pp:
   579   assumes p1: "Q"
   580       and p2: "~P ==> ~Q"
   581   shows "P"
   582 by (iprover intro: classical p1 p2 notE)
   583 
   584 
   585 subsubsection {*Unique existence*}
   586 
   587 lemma ex1I:
   588   assumes prems: "P a" "!!x. P(x) ==> x=a"
   589   shows "EX! x. P(x)"
   590 by (unfold Ex1_def, iprover intro: prems exI conjI allI impI)
   591 
   592 text{*Sometimes easier to use: the premises have no shared variables.  Safe!*}
   593 lemma ex_ex1I:
   594   assumes ex_prem: "EX x. P(x)"
   595       and eq: "!!x y. [| P(x); P(y) |] ==> x=y"
   596   shows "EX! x. P(x)"
   597 by (iprover intro: ex_prem [THEN exE] ex1I eq)
   598 
   599 lemma ex1E:
   600   assumes major: "EX! x. P(x)"
   601       and minor: "!!x. [| P(x);  ALL y. P(y) --> y=x |] ==> R"
   602   shows "R"
   603 apply (rule major [unfolded Ex1_def, THEN exE])
   604 apply (erule conjE)
   605 apply (iprover intro: minor)
   606 done
   607 
   608 lemma ex1_implies_ex: "EX! x. P x ==> EX x. P x"
   609 apply (erule ex1E)
   610 apply (rule exI)
   611 apply assumption
   612 done
   613 
   614 
   615 subsubsection {*THE: definite description operator*}
   616 
   617 lemma the_equality:
   618   assumes prema: "P a"
   619       and premx: "!!x. P x ==> x=a"
   620   shows "(THE x. P x) = a"
   621 apply (rule trans [OF _ the_eq_trivial])
   622 apply (rule_tac f = "The" in arg_cong)
   623 apply (rule ext)
   624 apply (rule iffI)
   625  apply (erule premx)
   626 apply (erule ssubst, rule prema)
   627 done
   628 
   629 lemma theI:
   630   assumes "P a" and "!!x. P x ==> x=a"
   631   shows "P (THE x. P x)"
   632 by (iprover intro: prems the_equality [THEN ssubst])
   633 
   634 lemma theI': "EX! x. P x ==> P (THE x. P x)"
   635 apply (erule ex1E)
   636 apply (erule theI)
   637 apply (erule allE)
   638 apply (erule mp)
   639 apply assumption
   640 done
   641 
   642 (*Easier to apply than theI: only one occurrence of P*)
   643 lemma theI2:
   644   assumes "P a" "!!x. P x ==> x=a" "!!x. P x ==> Q x"
   645   shows "Q (THE x. P x)"
   646 by (iprover intro: prems theI)
   647 
   648 lemma the1_equality [elim?]: "[| EX!x. P x; P a |] ==> (THE x. P x) = a"
   649 apply (rule the_equality)
   650 apply  assumption
   651 apply (erule ex1E)
   652 apply (erule all_dupE)
   653 apply (drule mp)
   654 apply  assumption
   655 apply (erule ssubst)
   656 apply (erule allE)
   657 apply (erule mp)
   658 apply assumption
   659 done
   660 
   661 lemma the_sym_eq_trivial: "(THE y. x=y) = x"
   662 apply (rule the_equality)
   663 apply (rule refl)
   664 apply (erule sym)
   665 done
   666 
   667 
   668 subsubsection {*Classical intro rules for disjunction and existential quantifiers*}
   669 
   670 lemma disjCI:
   671   assumes "~Q ==> P" shows "P|Q"
   672 apply (rule classical)
   673 apply (iprover intro: prems disjI1 disjI2 notI elim: notE)
   674 done
   675 
   676 lemma excluded_middle: "~P | P"
   677 by (iprover intro: disjCI)
   678 
   679 text {*
   680   case distinction as a natural deduction rule.
   681   Note that @{term "~P"} is the second case, not the first
   682 *}
   683 lemma case_split_thm:
   684   assumes prem1: "P ==> Q"
   685       and prem2: "~P ==> Q"
   686   shows "Q"
   687 apply (rule excluded_middle [THEN disjE])
   688 apply (erule prem2)
   689 apply (erule prem1)
   690 done
   691 lemmas case_split = case_split_thm [case_names True False]
   692 
   693 (*Classical implies (-->) elimination. *)
   694 lemma impCE:
   695   assumes major: "P-->Q"
   696       and minor: "~P ==> R" "Q ==> R"
   697   shows "R"
   698 apply (rule excluded_middle [of P, THEN disjE])
   699 apply (iprover intro: minor major [THEN mp])+
   700 done
   701 
   702 (*This version of --> elimination works on Q before P.  It works best for
   703   those cases in which P holds "almost everywhere".  Can't install as
   704   default: would break old proofs.*)
   705 lemma impCE':
   706   assumes major: "P-->Q"
   707       and minor: "Q ==> R" "~P ==> R"
   708   shows "R"
   709 apply (rule excluded_middle [of P, THEN disjE])
   710 apply (iprover intro: minor major [THEN mp])+
   711 done
   712 
   713 (*Classical <-> elimination. *)
   714 lemma iffCE:
   715   assumes major: "P=Q"
   716       and minor: "[| P; Q |] ==> R"  "[| ~P; ~Q |] ==> R"
   717   shows "R"
   718 apply (rule major [THEN iffE])
   719 apply (iprover intro: minor elim: impCE notE)
   720 done
   721 
   722 lemma exCI:
   723   assumes "ALL x. ~P(x) ==> P(a)"
   724   shows "EX x. P(x)"
   725 apply (rule ccontr)
   726 apply (iprover intro: prems exI allI notI notE [of "\<exists>x. P x"])
   727 done
   728 
   729 
   730 subsubsection {* Intuitionistic Reasoning *}
   731 
   732 lemma impE':
   733   assumes 1: "P --> Q"
   734     and 2: "Q ==> R"
   735     and 3: "P --> Q ==> P"
   736   shows R
   737 proof -
   738   from 3 and 1 have P .
   739   with 1 have Q by (rule impE)
   740   with 2 show R .
   741 qed
   742 
   743 lemma allE':
   744   assumes 1: "ALL x. P x"
   745     and 2: "P x ==> ALL x. P x ==> Q"
   746   shows Q
   747 proof -
   748   from 1 have "P x" by (rule spec)
   749   from this and 1 show Q by (rule 2)
   750 qed
   751 
   752 lemma notE':
   753   assumes 1: "~ P"
   754     and 2: "~ P ==> P"
   755   shows R
   756 proof -
   757   from 2 and 1 have P .
   758   with 1 show R by (rule notE)
   759 qed
   760 
   761 lemma TrueE: "True ==> P ==> P" .
   762 lemma notFalseE: "~ False ==> P ==> P" .
   763 
   764 lemmas [Pure.elim!] = disjE iffE FalseE conjE exE TrueE notFalseE
   765   and [Pure.intro!] = iffI conjI impI TrueI notI allI refl
   766   and [Pure.elim 2] = allE notE' impE'
   767   and [Pure.intro] = exI disjI2 disjI1
   768 
   769 lemmas [trans] = trans
   770   and [sym] = sym not_sym
   771   and [Pure.elim?] = iffD1 iffD2 impE
   772 
   773 
   774 subsubsection {* Atomizing meta-level connectives *}
   775 
   776 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
   777 proof
   778   assume "!!x. P x"
   779   show "ALL x. P x" by (rule allI)
   780 next
   781   assume "ALL x. P x"
   782   thus "!!x. P x" by (rule allE)
   783 qed
   784 
   785 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
   786 proof
   787   assume r: "A ==> B"
   788   show "A --> B" by (rule impI) (rule r)
   789 next
   790   assume "A --> B" and A
   791   thus B by (rule mp)
   792 qed
   793 
   794 lemma atomize_not: "(A ==> False) == Trueprop (~A)"
   795 proof
   796   assume r: "A ==> False"
   797   show "~A" by (rule notI) (rule r)
   798 next
   799   assume "~A" and A
   800   thus False by (rule notE)
   801 qed
   802 
   803 lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
   804 proof
   805   assume "x == y"
   806   show "x = y" by (unfold prems) (rule refl)
   807 next
   808   assume "x = y"
   809   thus "x == y" by (rule eq_reflection)
   810 qed
   811 
   812 lemma atomize_conj [atomize]:
   813   includes meta_conjunction_syntax
   814   shows "(A && B) == Trueprop (A & B)"
   815 proof
   816   assume conj: "A && B"
   817   show "A & B"
   818   proof (rule conjI)
   819     from conj show A by (rule conjunctionD1)
   820     from conj show B by (rule conjunctionD2)
   821   qed
   822 next
   823   assume conj: "A & B"
   824   show "A && B"
   825   proof -
   826     from conj show A ..
   827     from conj show B ..
   828   qed
   829 qed
   830 
   831 lemmas [symmetric, rulify] = atomize_all atomize_imp
   832   and [symmetric, defn] = atomize_all atomize_imp atomize_eq
   833 
   834 
   835 subsection {* Package setup *}
   836 
   837 subsubsection {* Classical Reasoner setup *}
   838 
   839 lemma thin_refl:
   840   "\<And>X. \<lbrakk> x=x; PROP W \<rbrakk> \<Longrightarrow> PROP W" .
   841 
   842 ML {*
   843 structure Hypsubst = HypsubstFun(
   844 struct
   845   structure Simplifier = Simplifier
   846   val dest_eq = HOLogic.dest_eq
   847   val dest_Trueprop = HOLogic.dest_Trueprop
   848   val dest_imp = HOLogic.dest_imp
   849   val eq_reflection = @{thm HOL.eq_reflection}
   850   val rev_eq_reflection = @{thm HOL.meta_eq_to_obj_eq}
   851   val imp_intr = @{thm HOL.impI}
   852   val rev_mp = @{thm HOL.rev_mp}
   853   val subst = @{thm HOL.subst}
   854   val sym = @{thm HOL.sym}
   855   val thin_refl = @{thm thin_refl};
   856 end);
   857 open Hypsubst;
   858 
   859 structure Classical = ClassicalFun(
   860 struct
   861   val mp = @{thm HOL.mp}
   862   val not_elim = @{thm HOL.notE}
   863   val classical = @{thm HOL.classical}
   864   val sizef = Drule.size_of_thm
   865   val hyp_subst_tacs = [Hypsubst.hyp_subst_tac]
   866 end);
   867 
   868 structure BasicClassical: BASIC_CLASSICAL = Classical; 
   869 open BasicClassical;
   870 
   871 ML_Context.value_antiq "claset"
   872   (Scan.succeed ("claset", "Classical.local_claset_of (ML_Context.the_local_context ())"));
   873 *}
   874 
   875 setup {*
   876 let
   877   (*prevent substitution on bool*)
   878   fun hyp_subst_tac' i thm = if i <= Thm.nprems_of thm andalso
   879     Term.exists_Const (fn ("op =", Type (_, [T, _])) => T <> Type ("bool", []) | _ => false)
   880       (nth (Thm.prems_of thm) (i - 1)) then Hypsubst.hyp_subst_tac i thm else no_tac thm;
   881 in
   882   Hypsubst.hypsubst_setup
   883   #> ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac)
   884   #> Classical.setup
   885   #> ResAtpset.setup
   886 end
   887 *}
   888 
   889 declare iffI [intro!]
   890   and notI [intro!]
   891   and impI [intro!]
   892   and disjCI [intro!]
   893   and conjI [intro!]
   894   and TrueI [intro!]
   895   and refl [intro!]
   896 
   897 declare iffCE [elim!]
   898   and FalseE [elim!]
   899   and impCE [elim!]
   900   and disjE [elim!]
   901   and conjE [elim!]
   902   and conjE [elim!]
   903 
   904 declare ex_ex1I [intro!]
   905   and allI [intro!]
   906   and the_equality [intro]
   907   and exI [intro]
   908 
   909 declare exE [elim!]
   910   allE [elim]
   911 
   912 ML {* val HOL_cs = @{claset} *}
   913 
   914 lemma contrapos_np: "~ Q ==> (~ P ==> Q) ==> P"
   915   apply (erule swap)
   916   apply (erule (1) meta_mp)
   917   done
   918 
   919 declare ex_ex1I [rule del, intro! 2]
   920   and ex1I [intro]
   921 
   922 lemmas [intro?] = ext
   923   and [elim?] = ex1_implies_ex
   924 
   925 (*Better then ex1E for classical reasoner: needs no quantifier duplication!*)
   926 lemma alt_ex1E [elim!]:
   927   assumes major: "\<exists>!x. P x"
   928       and prem: "\<And>x. \<lbrakk> P x; \<forall>y y'. P y \<and> P y' \<longrightarrow> y = y' \<rbrakk> \<Longrightarrow> R"
   929   shows R
   930 apply (rule ex1E [OF major])
   931 apply (rule prem)
   932 apply (tactic {* ares_tac @{thms allI} 1 *})+
   933 apply (tactic {* etac (Classical.dup_elim @{thm allE}) 1 *})
   934 apply iprover
   935 done
   936 
   937 ML {*
   938 structure Blast = BlastFun(
   939 struct
   940   type claset = Classical.claset
   941   val equality_name = @{const_name "op ="}
   942   val not_name = @{const_name Not}
   943   val notE = @{thm HOL.notE}
   944   val ccontr = @{thm HOL.ccontr}
   945   val contr_tac = Classical.contr_tac
   946   val dup_intr = Classical.dup_intr
   947   val hyp_subst_tac = Hypsubst.blast_hyp_subst_tac
   948   val claset = Classical.claset
   949   val rep_cs = Classical.rep_cs
   950   val cla_modifiers = Classical.cla_modifiers
   951   val cla_meth' = Classical.cla_meth'
   952 end);
   953 val Blast_tac = Blast.Blast_tac;
   954 val blast_tac = Blast.blast_tac;
   955 *}
   956 
   957 setup Blast.setup
   958 
   959 
   960 subsubsection {* Simplifier *}
   961 
   962 lemma eta_contract_eq: "(%s. f s) = f" ..
   963 
   964 lemma simp_thms:
   965   shows not_not: "(~ ~ P) = P"
   966   and Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
   967   and
   968     "(P ~= Q) = (P = (~Q))"
   969     "(P | ~P) = True"    "(~P | P) = True"
   970     "(x = x) = True"
   971   and not_True_eq_False: "(\<not> True) = False"
   972   and not_False_eq_True: "(\<not> False) = True"
   973   and
   974     "(~P) ~= P"  "P ~= (~P)"
   975     "(True=P) = P"
   976   and eq_True: "(P = True) = P"
   977   and "(False=P) = (~P)"
   978   and eq_False: "(P = False) = (\<not> P)"
   979   and
   980     "(True --> P) = P"  "(False --> P) = True"
   981     "(P --> True) = True"  "(P --> P) = True"
   982     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
   983     "(P & True) = P"  "(True & P) = P"
   984     "(P & False) = False"  "(False & P) = False"
   985     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
   986     "(P & ~P) = False"    "(~P & P) = False"
   987     "(P | True) = True"  "(True | P) = True"
   988     "(P | False) = P"  "(False | P) = P"
   989     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
   990     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
   991     -- {* needed for the one-point-rule quantifier simplification procs *}
   992     -- {* essential for termination!! *} and
   993     "!!P. (EX x. x=t & P(x)) = P(t)"
   994     "!!P. (EX x. t=x & P(x)) = P(t)"
   995     "!!P. (ALL x. x=t --> P(x)) = P(t)"
   996     "!!P. (ALL x. t=x --> P(x)) = P(t)"
   997   by (blast, blast, blast, blast, blast, iprover+)
   998 
   999 lemma disj_absorb: "(A | A) = A"
  1000   by blast
  1001 
  1002 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
  1003   by blast
  1004 
  1005 lemma conj_absorb: "(A & A) = A"
  1006   by blast
  1007 
  1008 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
  1009   by blast
  1010 
  1011 lemma eq_ac:
  1012   shows eq_commute: "(a=b) = (b=a)"
  1013     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
  1014     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (iprover, blast+)
  1015 lemma neq_commute: "(a~=b) = (b~=a)" by iprover
  1016 
  1017 lemma conj_comms:
  1018   shows conj_commute: "(P&Q) = (Q&P)"
  1019     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by iprover+
  1020 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by iprover
  1021 
  1022 lemmas conj_ac = conj_commute conj_left_commute conj_assoc
  1023 
  1024 lemma disj_comms:
  1025   shows disj_commute: "(P|Q) = (Q|P)"
  1026     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by iprover+
  1027 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by iprover
  1028 
  1029 lemmas disj_ac = disj_commute disj_left_commute disj_assoc
  1030 
  1031 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by iprover
  1032 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by iprover
  1033 
  1034 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by iprover
  1035 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by iprover
  1036 
  1037 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by iprover
  1038 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by iprover
  1039 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by iprover
  1040 
  1041 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
  1042 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
  1043 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
  1044 
  1045 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
  1046 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
  1047 
  1048 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
  1049   by iprover
  1050 
  1051 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by iprover
  1052 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
  1053 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
  1054 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
  1055 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
  1056 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
  1057   by blast
  1058 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
  1059 
  1060 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by iprover
  1061 
  1062 
  1063 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
  1064   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
  1065   -- {* cases boil down to the same thing. *}
  1066   by blast
  1067 
  1068 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
  1069 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
  1070 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by iprover
  1071 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by iprover
  1072 
  1073 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by iprover
  1074 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by iprover
  1075 
  1076 text {*
  1077   \medskip The @{text "&"} congruence rule: not included by default!
  1078   May slow rewrite proofs down by as much as 50\% *}
  1079 
  1080 lemma conj_cong:
  1081     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
  1082   by iprover
  1083 
  1084 lemma rev_conj_cong:
  1085     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
  1086   by iprover
  1087 
  1088 text {* The @{text "|"} congruence rule: not included by default! *}
  1089 
  1090 lemma disj_cong:
  1091     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
  1092   by blast
  1093 
  1094 
  1095 text {* \medskip if-then-else rules *}
  1096 
  1097 lemma if_True: "(if True then x else y) = x"
  1098   by (unfold if_def) blast
  1099 
  1100 lemma if_False: "(if False then x else y) = y"
  1101   by (unfold if_def) blast
  1102 
  1103 lemma if_P: "P ==> (if P then x else y) = x"
  1104   by (unfold if_def) blast
  1105 
  1106 lemma if_not_P: "~P ==> (if P then x else y) = y"
  1107   by (unfold if_def) blast
  1108 
  1109 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
  1110   apply (rule case_split [of Q])
  1111    apply (simplesubst if_P)
  1112     prefer 3 apply (simplesubst if_not_P, blast+)
  1113   done
  1114 
  1115 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
  1116 by (simplesubst split_if, blast)
  1117 
  1118 lemmas if_splits = split_if split_if_asm
  1119 
  1120 lemma if_cancel: "(if c then x else x) = x"
  1121 by (simplesubst split_if, blast)
  1122 
  1123 lemma if_eq_cancel: "(if x = y then y else x) = x"
  1124 by (simplesubst split_if, blast)
  1125 
  1126 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
  1127   -- {* This form is useful for expanding @{text "if"}s on the RIGHT of the @{text "==>"} symbol. *}
  1128   by (rule split_if)
  1129 
  1130 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
  1131   -- {* And this form is useful for expanding @{text "if"}s on the LEFT. *}
  1132   apply (simplesubst split_if, blast)
  1133   done
  1134 
  1135 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) iprover
  1136 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) iprover
  1137 
  1138 text {* \medskip let rules for simproc *}
  1139 
  1140 lemma Let_folded: "f x \<equiv> g x \<Longrightarrow>  Let x f \<equiv> Let x g"
  1141   by (unfold Let_def)
  1142 
  1143 lemma Let_unfold: "f x \<equiv> g \<Longrightarrow>  Let x f \<equiv> g"
  1144   by (unfold Let_def)
  1145 
  1146 text {*
  1147   The following copy of the implication operator is useful for
  1148   fine-tuning congruence rules.  It instructs the simplifier to simplify
  1149   its premise.
  1150 *}
  1151 
  1152 constdefs
  1153   simp_implies :: "[prop, prop] => prop"  (infixr "=simp=>" 1)
  1154   "simp_implies \<equiv> op ==>"
  1155 
  1156 lemma simp_impliesI:
  1157   assumes PQ: "(PROP P \<Longrightarrow> PROP Q)"
  1158   shows "PROP P =simp=> PROP Q"
  1159   apply (unfold simp_implies_def)
  1160   apply (rule PQ)
  1161   apply assumption
  1162   done
  1163 
  1164 lemma simp_impliesE:
  1165   assumes PQ:"PROP P =simp=> PROP Q"
  1166   and P: "PROP P"
  1167   and QR: "PROP Q \<Longrightarrow> PROP R"
  1168   shows "PROP R"
  1169   apply (rule QR)
  1170   apply (rule PQ [unfolded simp_implies_def])
  1171   apply (rule P)
  1172   done
  1173 
  1174 lemma simp_implies_cong:
  1175   assumes PP' :"PROP P == PROP P'"
  1176   and P'QQ': "PROP P' ==> (PROP Q == PROP Q')"
  1177   shows "(PROP P =simp=> PROP Q) == (PROP P' =simp=> PROP Q')"
  1178 proof (unfold simp_implies_def, rule equal_intr_rule)
  1179   assume PQ: "PROP P \<Longrightarrow> PROP Q"
  1180   and P': "PROP P'"
  1181   from PP' [symmetric] and P' have "PROP P"
  1182     by (rule equal_elim_rule1)
  1183   hence "PROP Q" by (rule PQ)
  1184   with P'QQ' [OF P'] show "PROP Q'" by (rule equal_elim_rule1)
  1185 next
  1186   assume P'Q': "PROP P' \<Longrightarrow> PROP Q'"
  1187   and P: "PROP P"
  1188   from PP' and P have P': "PROP P'" by (rule equal_elim_rule1)
  1189   hence "PROP Q'" by (rule P'Q')
  1190   with P'QQ' [OF P', symmetric] show "PROP Q"
  1191     by (rule equal_elim_rule1)
  1192 qed
  1193 
  1194 lemma uncurry:
  1195   assumes "P \<longrightarrow> Q \<longrightarrow> R"
  1196   shows "P \<and> Q \<longrightarrow> R"
  1197   using prems by blast
  1198 
  1199 lemma iff_allI:
  1200   assumes "\<And>x. P x = Q x"
  1201   shows "(\<forall>x. P x) = (\<forall>x. Q x)"
  1202   using prems by blast
  1203 
  1204 lemma iff_exI:
  1205   assumes "\<And>x. P x = Q x"
  1206   shows "(\<exists>x. P x) = (\<exists>x. Q x)"
  1207   using prems by blast
  1208 
  1209 lemma all_comm:
  1210   "(\<forall>x y. P x y) = (\<forall>y x. P x y)"
  1211   by blast
  1212 
  1213 lemma ex_comm:
  1214   "(\<exists>x y. P x y) = (\<exists>y x. P x y)"
  1215   by blast
  1216 
  1217 use "simpdata.ML"
  1218 ML {* open Simpdata *}
  1219 
  1220 setup {*
  1221   Simplifier.method_setup Splitter.split_modifiers
  1222   #> (fn thy => (change_simpset_of thy (fn _ => Simpdata.simpset_simprocs); thy))
  1223   #> Splitter.setup
  1224   #> Clasimp.setup
  1225   #> EqSubst.setup
  1226 *}
  1227 
  1228 lemma True_implies_equals: "(True \<Longrightarrow> PROP P) \<equiv> PROP P"
  1229 proof
  1230   assume prem: "True \<Longrightarrow> PROP P"
  1231   from prem [OF TrueI] show "PROP P" . 
  1232 next
  1233   assume "PROP P"
  1234   show "PROP P" .
  1235 qed
  1236 
  1237 lemma ex_simps:
  1238   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
  1239   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
  1240   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
  1241   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
  1242   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
  1243   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
  1244   -- {* Miniscoping: pushing in existential quantifiers. *}
  1245   by (iprover | blast)+
  1246 
  1247 lemma all_simps:
  1248   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
  1249   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
  1250   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
  1251   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
  1252   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
  1253   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
  1254   -- {* Miniscoping: pushing in universal quantifiers. *}
  1255   by (iprover | blast)+
  1256 
  1257 lemmas [simp] =
  1258   triv_forall_equality (*prunes params*)
  1259   True_implies_equals  (*prune asms `True'*)
  1260   if_True
  1261   if_False
  1262   if_cancel
  1263   if_eq_cancel
  1264   imp_disjL
  1265   (*In general it seems wrong to add distributive laws by default: they
  1266     might cause exponential blow-up.  But imp_disjL has been in for a while
  1267     and cannot be removed without affecting existing proofs.  Moreover,
  1268     rewriting by "(P|Q --> R) = ((P-->R)&(Q-->R))" might be justified on the
  1269     grounds that it allows simplification of R in the two cases.*)
  1270   conj_assoc
  1271   disj_assoc
  1272   de_Morgan_conj
  1273   de_Morgan_disj
  1274   imp_disj1
  1275   imp_disj2
  1276   not_imp
  1277   disj_not1
  1278   not_all
  1279   not_ex
  1280   cases_simp
  1281   the_eq_trivial
  1282   the_sym_eq_trivial
  1283   ex_simps
  1284   all_simps
  1285   simp_thms
  1286 
  1287 lemmas [cong] = imp_cong simp_implies_cong
  1288 lemmas [split] = split_if
  1289 
  1290 ML {* val HOL_ss = @{simpset} *}
  1291 
  1292 text {* Simplifies x assuming c and y assuming ~c *}
  1293 lemma if_cong:
  1294   assumes "b = c"
  1295       and "c \<Longrightarrow> x = u"
  1296       and "\<not> c \<Longrightarrow> y = v"
  1297   shows "(if b then x else y) = (if c then u else v)"
  1298   unfolding if_def using prems by simp
  1299 
  1300 text {* Prevents simplification of x and y:
  1301   faster and allows the execution of functional programs. *}
  1302 lemma if_weak_cong [cong]:
  1303   assumes "b = c"
  1304   shows "(if b then x else y) = (if c then x else y)"
  1305   using prems by (rule arg_cong)
  1306 
  1307 text {* Prevents simplification of t: much faster *}
  1308 lemma let_weak_cong:
  1309   assumes "a = b"
  1310   shows "(let x = a in t x) = (let x = b in t x)"
  1311   using prems by (rule arg_cong)
  1312 
  1313 text {* To tidy up the result of a simproc.  Only the RHS will be simplified. *}
  1314 lemma eq_cong2:
  1315   assumes "u = u'"
  1316   shows "(t \<equiv> u) \<equiv> (t \<equiv> u')"
  1317   using prems by simp
  1318 
  1319 lemma if_distrib:
  1320   "f (if c then x else y) = (if c then f x else f y)"
  1321   by simp
  1322 
  1323 text {* This lemma restricts the effect of the rewrite rule u=v to the left-hand
  1324   side of an equality.  Used in @{text "{Integ,Real}/simproc.ML"} *}
  1325 lemma restrict_to_left:
  1326   assumes "x = y"
  1327   shows "(x = z) = (y = z)"
  1328   using prems by simp
  1329 
  1330 
  1331 subsubsection {* Generic cases and induction *}
  1332 
  1333 text {* Rule projections: *}
  1334 
  1335 ML {*
  1336 structure ProjectRule = ProjectRuleFun
  1337 (struct
  1338   val conjunct1 = @{thm conjunct1};
  1339   val conjunct2 = @{thm conjunct2};
  1340   val mp = @{thm mp};
  1341 end)
  1342 *}
  1343 
  1344 constdefs
  1345   induct_forall where "induct_forall P == \<forall>x. P x"
  1346   induct_implies where "induct_implies A B == A \<longrightarrow> B"
  1347   induct_equal where "induct_equal x y == x = y"
  1348   induct_conj where "induct_conj A B == A \<and> B"
  1349 
  1350 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
  1351   by (unfold atomize_all induct_forall_def)
  1352 
  1353 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
  1354   by (unfold atomize_imp induct_implies_def)
  1355 
  1356 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
  1357   by (unfold atomize_eq induct_equal_def)
  1358 
  1359 lemma induct_conj_eq:
  1360   includes meta_conjunction_syntax
  1361   shows "(A && B) == Trueprop (induct_conj A B)"
  1362   by (unfold atomize_conj induct_conj_def)
  1363 
  1364 lemmas induct_atomize = induct_forall_eq induct_implies_eq induct_equal_eq induct_conj_eq
  1365 lemmas induct_rulify [symmetric, standard] = induct_atomize
  1366 lemmas induct_rulify_fallback =
  1367   induct_forall_def induct_implies_def induct_equal_def induct_conj_def
  1368 
  1369 
  1370 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
  1371     induct_conj (induct_forall A) (induct_forall B)"
  1372   by (unfold induct_forall_def induct_conj_def) iprover
  1373 
  1374 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
  1375     induct_conj (induct_implies C A) (induct_implies C B)"
  1376   by (unfold induct_implies_def induct_conj_def) iprover
  1377 
  1378 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
  1379 proof
  1380   assume r: "induct_conj A B ==> PROP C" and A B
  1381   show "PROP C" by (rule r) (simp add: induct_conj_def `A` `B`)
  1382 next
  1383   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
  1384   show "PROP C" by (rule r) (simp_all add: `induct_conj A B` [unfolded induct_conj_def])
  1385 qed
  1386 
  1387 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
  1388 
  1389 hide const induct_forall induct_implies induct_equal induct_conj
  1390 
  1391 text {* Method setup. *}
  1392 
  1393 ML {*
  1394   structure InductMethod = InductMethodFun
  1395   (struct
  1396     val cases_default = @{thm case_split}
  1397     val atomize = @{thms induct_atomize}
  1398     val rulify = @{thms induct_rulify}
  1399     val rulify_fallback = @{thms induct_rulify_fallback}
  1400   end);
  1401 *}
  1402 
  1403 setup InductMethod.setup
  1404 
  1405 
  1406 
  1407 subsection {* Other simple lemmas and lemma duplicates *}
  1408 
  1409 lemma ex1_eq [iff]: "EX! x. x = t" "EX! x. t = x"
  1410   by blast+
  1411 
  1412 lemma choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
  1413   apply (rule iffI)
  1414   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
  1415   apply (fast dest!: theI')
  1416   apply (fast intro: ext the1_equality [symmetric])
  1417   apply (erule ex1E)
  1418   apply (rule allI)
  1419   apply (rule ex1I)
  1420   apply (erule spec)
  1421   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
  1422   apply (erule impE)
  1423   apply (rule allI)
  1424   apply (rule_tac P = "xa = x" in case_split_thm)
  1425   apply (drule_tac [3] x = x in fun_cong, simp_all)
  1426   done
  1427 
  1428 lemma mk_left_commute:
  1429   fixes f (infix "\<otimes>" 60)
  1430   assumes a: "\<And>x y z. (x \<otimes> y) \<otimes> z = x \<otimes> (y \<otimes> z)" and
  1431           c: "\<And>x y. x \<otimes> y = y \<otimes> x"
  1432   shows "x \<otimes> (y \<otimes> z) = y \<otimes> (x \<otimes> z)"
  1433   by (rule trans [OF trans [OF c a] arg_cong [OF c, of "f y"]])
  1434 
  1435 lemmas eq_sym_conv = eq_commute
  1436 
  1437 lemma nnf_simps:
  1438   "(\<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)" 
  1439   "(P = Q) = ((P \<and> Q) \<or> (\<not>P \<and> \<not> Q))" "(\<not>(P = Q)) = ((P \<and> \<not> Q) \<or> (\<not>P \<and> Q))" 
  1440   "(\<not> \<not>(P)) = P"
  1441 by blast+
  1442 
  1443 
  1444 subsection {* Basic ML bindings *}
  1445 
  1446 ML {*
  1447 val FalseE = @{thm FalseE}
  1448 val Let_def = @{thm Let_def}
  1449 val TrueI = @{thm TrueI}
  1450 val allE = @{thm allE}
  1451 val allI = @{thm allI}
  1452 val all_dupE = @{thm all_dupE}
  1453 val arg_cong = @{thm arg_cong}
  1454 val box_equals = @{thm box_equals}
  1455 val ccontr = @{thm ccontr}
  1456 val classical = @{thm classical}
  1457 val conjE = @{thm conjE}
  1458 val conjI = @{thm conjI}
  1459 val conjunct1 = @{thm conjunct1}
  1460 val conjunct2 = @{thm conjunct2}
  1461 val disjCI = @{thm disjCI}
  1462 val disjE = @{thm disjE}
  1463 val disjI1 = @{thm disjI1}
  1464 val disjI2 = @{thm disjI2}
  1465 val eq_reflection = @{thm eq_reflection}
  1466 val ex1E = @{thm ex1E}
  1467 val ex1I = @{thm ex1I}
  1468 val ex1_implies_ex = @{thm ex1_implies_ex}
  1469 val exE = @{thm exE}
  1470 val exI = @{thm exI}
  1471 val excluded_middle = @{thm excluded_middle}
  1472 val ext = @{thm ext}
  1473 val fun_cong = @{thm fun_cong}
  1474 val iffD1 = @{thm iffD1}
  1475 val iffD2 = @{thm iffD2}
  1476 val iffI = @{thm iffI}
  1477 val impE = @{thm impE}
  1478 val impI = @{thm impI}
  1479 val meta_eq_to_obj_eq = @{thm meta_eq_to_obj_eq}
  1480 val mp = @{thm mp}
  1481 val notE = @{thm notE}
  1482 val notI = @{thm notI}
  1483 val not_all = @{thm not_all}
  1484 val not_ex = @{thm not_ex}
  1485 val not_iff = @{thm not_iff}
  1486 val not_not = @{thm not_not}
  1487 val not_sym = @{thm not_sym}
  1488 val refl = @{thm refl}
  1489 val rev_mp = @{thm rev_mp}
  1490 val spec = @{thm spec}
  1491 val ssubst = @{thm ssubst}
  1492 val subst = @{thm subst}
  1493 val sym = @{thm sym}
  1494 val trans = @{thm trans}
  1495 *}
  1496 
  1497 
  1498 subsection {* Legacy tactics and ML bindings *}
  1499 
  1500 ML {*
  1501 fun strip_tac i = REPEAT (resolve_tac [impI, allI] i);
  1502 
  1503 (* combination of (spec RS spec RS ...(j times) ... spec RS mp) *)
  1504 local
  1505   fun wrong_prem (Const ("All", _) $ (Abs (_, _, t))) = wrong_prem t
  1506     | wrong_prem (Bound _) = true
  1507     | wrong_prem _ = false;
  1508   val filter_right = filter (not o wrong_prem o HOLogic.dest_Trueprop o hd o Thm.prems_of);
  1509 in
  1510   fun smp i = funpow i (fn m => filter_right ([spec] RL m)) ([mp]);
  1511   fun smp_tac j = EVERY'[dresolve_tac (smp j), atac];
  1512 end;
  1513 
  1514 val all_conj_distrib = thm "all_conj_distrib";
  1515 val all_simps = thms "all_simps";
  1516 val atomize_not = thm "atomize_not";
  1517 val case_split = thm "case_split_thm";
  1518 val case_split_thm = thm "case_split_thm"
  1519 val cases_simp = thm "cases_simp";
  1520 val choice_eq = thm "choice_eq"
  1521 val cong = thm "cong"
  1522 val conj_comms = thms "conj_comms";
  1523 val conj_cong = thm "conj_cong";
  1524 val de_Morgan_conj = thm "de_Morgan_conj";
  1525 val de_Morgan_disj = thm "de_Morgan_disj";
  1526 val disj_assoc = thm "disj_assoc";
  1527 val disj_comms = thms "disj_comms";
  1528 val disj_cong = thm "disj_cong";
  1529 val eq_ac = thms "eq_ac";
  1530 val eq_cong2 = thm "eq_cong2"
  1531 val Eq_FalseI = thm "Eq_FalseI";
  1532 val Eq_TrueI = thm "Eq_TrueI";
  1533 val Ex1_def = thm "Ex1_def"
  1534 val ex_disj_distrib = thm "ex_disj_distrib";
  1535 val ex_simps = thms "ex_simps";
  1536 val if_cancel = thm "if_cancel";
  1537 val if_eq_cancel = thm "if_eq_cancel";
  1538 val if_False = thm "if_False";
  1539 val iff_conv_conj_imp = thm "iff_conv_conj_imp";
  1540 val iff = thm "iff"
  1541 val if_splits = thms "if_splits";
  1542 val if_True = thm "if_True";
  1543 val if_weak_cong = thm "if_weak_cong"
  1544 val imp_all = thm "imp_all";
  1545 val imp_cong = thm "imp_cong";
  1546 val imp_conjL = thm "imp_conjL";
  1547 val imp_conjR = thm "imp_conjR";
  1548 val imp_conv_disj = thm "imp_conv_disj";
  1549 val simp_implies_def = thm "simp_implies_def";
  1550 val simp_thms = thms "simp_thms";
  1551 val split_if = thm "split_if";
  1552 val the1_equality = thm "the1_equality"
  1553 val theI = thm "theI"
  1554 val theI' = thm "theI'"
  1555 val True_implies_equals = thm "True_implies_equals";
  1556 val nnf_conv = Simplifier.rewrite (HOL_basic_ss addsimps simp_thms @ @{thms "nnf_simps"})
  1557 
  1558 *}
  1559 
  1560 end