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