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