src/HOL/HOL.thy
author paulson
Tue Sep 23 15:42:01 2003 +0200 (2003-09-23)
changeset 14201 7ad7ab89c402
parent 13764 3e180bf68496
child 14208 144f45277d5a
permissions -rw-r--r--
some basic new lemmas
     1 (*  Title:      HOL/HOL.thy
     2     ID:         $Id$
     3     Author:     Tobias Nipkow, Markus Wenzel, and Larry Paulson
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 *)
     6 
     7 header {* The basis of Higher-Order Logic *}
     8 
     9 theory HOL = CPure
    10 files ("HOL_lemmas.ML") ("cladata.ML") ("blastdata.ML") ("simpdata.ML"):
    11 
    12 
    13 subsection {* Primitive logic *}
    14 
    15 subsubsection {* Core syntax *}
    16 
    17 classes type < logic
    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   If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
    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 
    52 subsubsection {* Additional concrete syntax *}
    53 
    54 nonterminals
    55   letbinds  letbind
    56   case_syn  cases_syn
    57 
    58 syntax
    59   "_not_equal"  :: "['a, 'a] => bool"                    (infixl "~=" 50)
    60   "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
    61 
    62   "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
    63   ""            :: "letbind => letbinds"                 ("_")
    64   "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
    65   "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" 10)
    66 
    67   "_case_syntax":: "['a, cases_syn] => 'b"               ("(case _ of/ _)" 10)
    68   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ =>/ _)" 10)
    69   ""            :: "case_syn => cases_syn"               ("_")
    70   "_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ | _")
    71 
    72 translations
    73   "x ~= y"                == "~ (x = y)"
    74   "THE x. P"              == "The (%x. P)"
    75   "_Let (_binds b bs) e"  == "_Let b (_Let bs e)"
    76   "let x = a in e"        == "Let a (%x. e)"
    77 
    78 print_translation {*
    79 (* To avoid eta-contraction of body: *)
    80 [("The", fn [Abs abs] =>
    81      let val (x,t) = atomic_abs_tr' abs
    82      in Syntax.const "_The" $ x $ t end)]
    83 *}
    84 
    85 syntax (output)
    86   "="           :: "['a, 'a] => bool"                    (infix 50)
    87   "_not_equal"  :: "['a, 'a] => bool"                    (infix "~=" 50)
    88 
    89 syntax (xsymbols)
    90   Not           :: "bool => bool"                        ("\<not> _" [40] 40)
    91   "op &"        :: "[bool, bool] => bool"                (infixr "\<and>" 35)
    92   "op |"        :: "[bool, bool] => bool"                (infixr "\<or>" 30)
    93   "op -->"      :: "[bool, bool] => bool"                (infixr "\<longrightarrow>" 25)
    94   "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
    95   "ALL "        :: "[idts, bool] => bool"                ("(3\<forall>_./ _)" [0, 10] 10)
    96   "EX "         :: "[idts, bool] => bool"                ("(3\<exists>_./ _)" [0, 10] 10)
    97   "EX! "        :: "[idts, bool] => bool"                ("(3\<exists>!_./ _)" [0, 10] 10)
    98   "_case1"      :: "['a, 'b] => case_syn"                ("(2_ \<Rightarrow>/ _)" 10)
    99 (*"_case2"      :: "[case_syn, cases_syn] => cases_syn"  ("_/ \\<orelse> _")*)
   100 
   101 syntax (xsymbols output)
   102   "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
   103 
   104 syntax (HTML output)
   105   Not           :: "bool => bool"                        ("\<not> _" [40] 40)
   106 
   107 syntax (HOL)
   108   "ALL "        :: "[idts, bool] => bool"                ("(3! _./ _)" [0, 10] 10)
   109   "EX "         :: "[idts, bool] => bool"                ("(3? _./ _)" [0, 10] 10)
   110   "EX! "        :: "[idts, bool] => bool"                ("(3?! _./ _)" [0, 10] 10)
   111 
   112 
   113 subsubsection {* Axioms and basic definitions *}
   114 
   115 axioms
   116   eq_reflection: "(x=y) ==> (x==y)"
   117 
   118   refl:         "t = (t::'a)"
   119   subst:        "[| s = t; P(s) |] ==> P(t::'a)"
   120 
   121   ext:          "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
   122     -- {* Extensionality is built into the meta-logic, and this rule expresses *}
   123     -- {* a related property.  It is an eta-expanded version of the traditional *}
   124     -- {* rule, and similar to the ABS rule of HOL *}
   125 
   126   the_eq_trivial: "(THE x. x = a) = (a::'a)"
   127 
   128   impI:         "(P ==> Q) ==> P-->Q"
   129   mp:           "[| P-->Q;  P |] ==> Q"
   130 
   131 defs
   132   True_def:     "True      == ((%x::bool. x) = (%x. x))"
   133   All_def:      "All(P)    == (P = (%x. True))"
   134   Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
   135   False_def:    "False     == (!P. P)"
   136   not_def:      "~ P       == P-->False"
   137   and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
   138   or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
   139   Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
   140 
   141 axioms
   142   iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
   143   True_or_False:  "(P=True) | (P=False)"
   144 
   145 defs
   146   Let_def:      "Let s f == f(s)"
   147   if_def:       "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
   148 
   149   arbitrary_def:  "False ==> arbitrary == (THE x. False)"
   150     -- {* @{term arbitrary} is completely unspecified, but is made to appear as a
   151     definition syntactically *}
   152 
   153 
   154 subsubsection {* Generic algebraic operations *}
   155 
   156 axclass zero < type
   157 axclass one < type
   158 axclass plus < type
   159 axclass minus < type
   160 axclass times < type
   161 axclass inverse < type
   162 
   163 global
   164 
   165 consts
   166   "0"           :: "'a::zero"                       ("0")
   167   "1"           :: "'a::one"                        ("1")
   168   "+"           :: "['a::plus, 'a]  => 'a"          (infixl 65)
   169   -             :: "['a::minus, 'a] => 'a"          (infixl 65)
   170   uminus        :: "['a::minus] => 'a"              ("- _" [81] 80)
   171   *             :: "['a::times, 'a] => 'a"          (infixl 70)
   172 
   173 syntax
   174   "_index1"  :: index    ("\<^sub>1")
   175 translations
   176   (index) "\<^sub>1" == "_index 1"
   177 
   178 local
   179 
   180 typed_print_translation {*
   181   let
   182     fun tr' c = (c, fn show_sorts => fn T => fn ts =>
   183       if T = dummyT orelse not (! show_types) andalso can Term.dest_Type T then raise Match
   184       else Syntax.const Syntax.constrainC $ Syntax.const c $ Syntax.term_of_typ show_sorts T);
   185   in [tr' "0", tr' "1"] end;
   186 *} -- {* show types that are presumably too general *}
   187 
   188 
   189 consts
   190   abs           :: "'a::minus => 'a"
   191   inverse       :: "'a::inverse => 'a"
   192   divide        :: "['a::inverse, 'a] => 'a"        (infixl "'/" 70)
   193 
   194 syntax (xsymbols)
   195   abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
   196 syntax (HTML output)
   197   abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
   198 
   199 axclass plus_ac0 < plus, zero
   200   commute: "x + y = y + x"
   201   assoc:   "(x + y) + z = x + (y + z)"
   202   zero:    "0 + x = x"
   203 
   204 
   205 subsection {* Theory and package setup *}
   206 
   207 subsubsection {* Basic lemmas *}
   208 
   209 use "HOL_lemmas.ML"
   210 theorems case_split = case_split_thm [case_names True False]
   211 
   212 
   213 subsubsection {* Intuitionistic Reasoning *}
   214 
   215 lemma impE':
   216   assumes 1: "P --> Q"
   217     and 2: "Q ==> R"
   218     and 3: "P --> Q ==> P"
   219   shows R
   220 proof -
   221   from 3 and 1 have P .
   222   with 1 have Q by (rule impE)
   223   with 2 show R .
   224 qed
   225 
   226 lemma allE':
   227   assumes 1: "ALL x. P x"
   228     and 2: "P x ==> ALL x. P x ==> Q"
   229   shows Q
   230 proof -
   231   from 1 have "P x" by (rule spec)
   232   from this and 1 show Q by (rule 2)
   233 qed
   234 
   235 lemma notE':
   236   assumes 1: "~ P"
   237     and 2: "~ P ==> P"
   238   shows R
   239 proof -
   240   from 2 and 1 have P .
   241   with 1 show R by (rule notE)
   242 qed
   243 
   244 lemmas [CPure.elim!] = disjE iffE FalseE conjE exE
   245   and [CPure.intro!] = iffI conjI impI TrueI notI allI refl
   246   and [CPure.elim 2] = allE notE' impE'
   247   and [CPure.intro] = exI disjI2 disjI1
   248 
   249 lemmas [trans] = trans
   250   and [sym] = sym not_sym
   251   and [CPure.elim?] = iffD1 iffD2 impE
   252 
   253 
   254 subsubsection {* Atomizing meta-level connectives *}
   255 
   256 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
   257 proof
   258   assume "!!x. P x"
   259   show "ALL x. P x" by (rule allI)
   260 next
   261   assume "ALL x. P x"
   262   thus "!!x. P x" by (rule allE)
   263 qed
   264 
   265 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
   266 proof
   267   assume r: "A ==> B"
   268   show "A --> B" by (rule impI) (rule r)
   269 next
   270   assume "A --> B" and A
   271   thus B by (rule mp)
   272 qed
   273 
   274 lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
   275 proof
   276   assume "x == y"
   277   show "x = y" by (unfold prems) (rule refl)
   278 next
   279   assume "x = y"
   280   thus "x == y" by (rule eq_reflection)
   281 qed
   282 
   283 lemma atomize_conj [atomize]:
   284   "(!!C. (A ==> B ==> PROP C) ==> PROP C) == Trueprop (A & B)"
   285 proof
   286   assume "!!C. (A ==> B ==> PROP C) ==> PROP C"
   287   show "A & B" by (rule conjI)
   288 next
   289   fix C
   290   assume "A & B"
   291   assume "A ==> B ==> PROP C"
   292   thus "PROP C"
   293   proof this
   294     show A by (rule conjunct1)
   295     show B by (rule conjunct2)
   296   qed
   297 qed
   298 
   299 lemmas [symmetric, rulify] = atomize_all atomize_imp
   300 
   301 
   302 subsubsection {* Classical Reasoner setup *}
   303 
   304 use "cladata.ML"
   305 setup hypsubst_setup
   306 
   307 ML_setup {*
   308   Context.>> (ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac));
   309 *}
   310 
   311 setup Classical.setup
   312 setup clasetup
   313 
   314 lemmas [intro?] = ext
   315   and [elim?] = ex1_implies_ex
   316 
   317 use "blastdata.ML"
   318 setup Blast.setup
   319 
   320 
   321 subsubsection {* Simplifier setup *}
   322 
   323 lemma meta_eq_to_obj_eq: "x == y ==> x = y"
   324 proof -
   325   assume r: "x == y"
   326   show "x = y" by (unfold r) (rule refl)
   327 qed
   328 
   329 lemma eta_contract_eq: "(%s. f s) = f" ..
   330 
   331 lemma simp_thms:
   332   shows not_not: "(~ ~ P) = P"
   333   and
   334     "(P ~= Q) = (P = (~Q))"
   335     "(P | ~P) = True"    "(~P | P) = True"
   336     "((~P) = (~Q)) = (P=Q)"
   337     "(x = x) = True"
   338     "(~True) = False"  "(~False) = True"
   339     "(~P) ~= P"  "P ~= (~P)"
   340     "(True=P) = P"  "(P=True) = P"  "(False=P) = (~P)"  "(P=False) = (~P)"
   341     "(True --> P) = P"  "(False --> P) = True"
   342     "(P --> True) = True"  "(P --> P) = True"
   343     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
   344     "(P & True) = P"  "(True & P) = P"
   345     "(P & False) = False"  "(False & P) = False"
   346     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
   347     "(P & ~P) = False"    "(~P & P) = False"
   348     "(P | True) = True"  "(True | P) = True"
   349     "(P | False) = P"  "(False | P) = P"
   350     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
   351     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
   352     -- {* needed for the one-point-rule quantifier simplification procs *}
   353     -- {* essential for termination!! *} and
   354     "!!P. (EX x. x=t & P(x)) = P(t)"
   355     "!!P. (EX x. t=x & P(x)) = P(t)"
   356     "!!P. (ALL x. x=t --> P(x)) = P(t)"
   357     "!!P. (ALL x. t=x --> P(x)) = P(t)"
   358   by (blast, blast, blast, blast, blast, rules+)
   359 
   360 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
   361   by rules
   362 
   363 lemma ex_simps:
   364   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
   365   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
   366   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
   367   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
   368   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
   369   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
   370   -- {* Miniscoping: pushing in existential quantifiers. *}
   371   by (rules | blast)+
   372 
   373 lemma all_simps:
   374   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
   375   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
   376   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
   377   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
   378   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
   379   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
   380   -- {* Miniscoping: pushing in universal quantifiers. *}
   381   by (rules | blast)+
   382 
   383 lemma disj_absorb: "(A | A) = A"
   384   by blast
   385 
   386 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
   387   by blast
   388 
   389 lemma conj_absorb: "(A & A) = A"
   390   by blast
   391 
   392 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
   393   by blast
   394 
   395 lemma eq_ac:
   396   shows eq_commute: "(a=b) = (b=a)"
   397     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
   398     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (rules, blast+)
   399 lemma neq_commute: "(a~=b) = (b~=a)" by rules
   400 
   401 lemma conj_comms:
   402   shows conj_commute: "(P&Q) = (Q&P)"
   403     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by rules+
   404 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by rules
   405 
   406 lemma disj_comms:
   407   shows disj_commute: "(P|Q) = (Q|P)"
   408     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by rules+
   409 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by rules
   410 
   411 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by rules
   412 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by rules
   413 
   414 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by rules
   415 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by rules
   416 
   417 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by rules
   418 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by rules
   419 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by rules
   420 
   421 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
   422 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
   423 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
   424 
   425 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
   426 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
   427 
   428 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by rules
   429 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
   430 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
   431 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
   432 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
   433 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
   434   by blast
   435 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
   436 
   437 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by rules
   438 
   439 
   440 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
   441   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
   442   -- {* cases boil down to the same thing. *}
   443   by blast
   444 
   445 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
   446 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
   447 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by rules
   448 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by rules
   449 
   450 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by rules
   451 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by rules
   452 
   453 text {*
   454   \medskip The @{text "&"} congruence rule: not included by default!
   455   May slow rewrite proofs down by as much as 50\% *}
   456 
   457 lemma conj_cong:
   458     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
   459   by rules
   460 
   461 lemma rev_conj_cong:
   462     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
   463   by rules
   464 
   465 text {* The @{text "|"} congruence rule: not included by default! *}
   466 
   467 lemma disj_cong:
   468     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
   469   by blast
   470 
   471 lemma eq_sym_conv: "(x = y) = (y = x)"
   472   by rules
   473 
   474 
   475 text {* \medskip if-then-else rules *}
   476 
   477 lemma if_True: "(if True then x else y) = x"
   478   by (unfold if_def) blast
   479 
   480 lemma if_False: "(if False then x else y) = y"
   481   by (unfold if_def) blast
   482 
   483 lemma if_P: "P ==> (if P then x else y) = x"
   484   by (unfold if_def) blast
   485 
   486 lemma if_not_P: "~P ==> (if P then x else y) = y"
   487   by (unfold if_def) blast
   488 
   489 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
   490   apply (rule case_split [of Q])
   491    apply (subst if_P)
   492     prefer 3 apply (subst if_not_P)
   493      apply blast+
   494   done
   495 
   496 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
   497   apply (subst split_if)
   498   apply blast
   499   done
   500 
   501 lemmas if_splits = split_if split_if_asm
   502 
   503 lemma if_def2: "(if Q then x else y) = ((Q --> x) & (~ Q --> y))"
   504   by (rule split_if)
   505 
   506 lemma if_cancel: "(if c then x else x) = x"
   507   apply (subst split_if)
   508   apply blast
   509   done
   510 
   511 lemma if_eq_cancel: "(if x = y then y else x) = x"
   512   apply (subst split_if)
   513   apply blast
   514   done
   515 
   516 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
   517   -- {* This form is useful for expanding @{text if}s on the RIGHT of the @{text "==>"} symbol. *}
   518   by (rule split_if)
   519 
   520 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
   521   -- {* And this form is useful for expanding @{text if}s on the LEFT. *}
   522   apply (subst split_if)
   523   apply blast
   524   done
   525 
   526 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) rules
   527 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) rules
   528 
   529 subsubsection {* Actual Installation of the Simplifier *}
   530 
   531 use "simpdata.ML"
   532 setup Simplifier.setup
   533 setup "Simplifier.method_setup Splitter.split_modifiers" setup simpsetup
   534 setup Splitter.setup setup Clasimp.setup
   535 
   536 declare disj_absorb [simp] conj_absorb [simp] 
   537 
   538 lemma ex1_eq[iff]: "EX! x. x = t" "EX! x. t = x"
   539 by blast+
   540 
   541 theorem choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
   542   apply (rule iffI)
   543   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
   544   apply (fast dest!: theI')
   545   apply (fast intro: ext the1_equality [symmetric])
   546   apply (erule ex1E)
   547   apply (rule allI)
   548   apply (rule ex1I)
   549   apply (erule spec)
   550   apply (rule ccontr)
   551   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
   552   apply (erule impE)
   553   apply (rule allI)
   554   apply (rule_tac P = "xa = x" in case_split_thm)
   555   apply (drule_tac [3] x = x in fun_cong)
   556   apply simp_all
   557   done
   558 
   559 text{*Needs only HOL-lemmas:*}
   560 lemma mk_left_commute:
   561   assumes a: "\<And>x y z. f (f x y) z = f x (f y z)" and
   562           c: "\<And>x y. f x y = f y x"
   563   shows "f x (f y z) = f y (f x z)"
   564 by(rule trans[OF trans[OF c a] arg_cong[OF c, of "f y"]])
   565 
   566 
   567 subsubsection {* Generic cases and induction *}
   568 
   569 constdefs
   570   induct_forall :: "('a => bool) => bool"
   571   "induct_forall P == \<forall>x. P x"
   572   induct_implies :: "bool => bool => bool"
   573   "induct_implies A B == A --> B"
   574   induct_equal :: "'a => 'a => bool"
   575   "induct_equal x y == x = y"
   576   induct_conj :: "bool => bool => bool"
   577   "induct_conj A B == A & B"
   578 
   579 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
   580   by (simp only: atomize_all induct_forall_def)
   581 
   582 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
   583   by (simp only: atomize_imp induct_implies_def)
   584 
   585 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
   586   by (simp only: atomize_eq induct_equal_def)
   587 
   588 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
   589     induct_conj (induct_forall A) (induct_forall B)"
   590   by (unfold induct_forall_def induct_conj_def) rules
   591 
   592 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
   593     induct_conj (induct_implies C A) (induct_implies C B)"
   594   by (unfold induct_implies_def induct_conj_def) rules
   595 
   596 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
   597 proof
   598   assume r: "induct_conj A B ==> PROP C" and A B
   599   show "PROP C" by (rule r) (simp! add: induct_conj_def)
   600 next
   601   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
   602   show "PROP C" by (rule r) (simp! add: induct_conj_def)+
   603 qed
   604 
   605 lemma induct_impliesI: "(A ==> B) ==> induct_implies A B"
   606   by (simp add: induct_implies_def)
   607 
   608 lemmas induct_atomize = atomize_conj induct_forall_eq induct_implies_eq induct_equal_eq
   609 lemmas induct_rulify1 [symmetric, standard] = induct_forall_eq induct_implies_eq induct_equal_eq
   610 lemmas induct_rulify2 = induct_forall_def induct_implies_def induct_equal_def induct_conj_def
   611 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
   612 
   613 hide const induct_forall induct_implies induct_equal induct_conj
   614 
   615 
   616 text {* Method setup. *}
   617 
   618 ML {*
   619   structure InductMethod = InductMethodFun
   620   (struct
   621     val dest_concls = HOLogic.dest_concls;
   622     val cases_default = thm "case_split";
   623     val local_impI = thm "induct_impliesI";
   624     val conjI = thm "conjI";
   625     val atomize = thms "induct_atomize";
   626     val rulify1 = thms "induct_rulify1";
   627     val rulify2 = thms "induct_rulify2";
   628     val localize = [Thm.symmetric (thm "induct_implies_def")];
   629   end);
   630 *}
   631 
   632 setup InductMethod.setup
   633 
   634 
   635 subsection {* Order signatures and orders *}
   636 
   637 axclass
   638   ord < type
   639 
   640 syntax
   641   "op <"        :: "['a::ord, 'a] => bool"             ("op <")
   642   "op <="       :: "['a::ord, 'a] => bool"             ("op <=")
   643 
   644 global
   645 
   646 consts
   647   "op <"        :: "['a::ord, 'a] => bool"             ("(_/ < _)"  [50, 51] 50)
   648   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ <= _)" [50, 51] 50)
   649 
   650 local
   651 
   652 syntax (xsymbols)
   653   "op <="       :: "['a::ord, 'a] => bool"             ("op \<le>")
   654   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
   655 
   656 
   657 subsubsection {* Monotonicity *}
   658 
   659 locale mono =
   660   fixes f
   661   assumes mono: "A <= B ==> f A <= f B"
   662 
   663 lemmas monoI [intro?] = mono.intro
   664   and monoD [dest?] = mono.mono
   665 
   666 constdefs
   667   min :: "['a::ord, 'a] => 'a"
   668   "min a b == (if a <= b then a else b)"
   669   max :: "['a::ord, 'a] => 'a"
   670   "max a b == (if a <= b then b else a)"
   671 
   672 lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
   673   by (simp add: min_def)
   674 
   675 lemma min_of_mono:
   676     "ALL x y. (f x <= f y) = (x <= y) ==> min (f m) (f n) = f (min m n)"
   677   by (simp add: min_def)
   678 
   679 lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
   680   by (simp add: max_def)
   681 
   682 lemma max_of_mono:
   683     "ALL x y. (f x <= f y) = (x <= y) ==> max (f m) (f n) = f (max m n)"
   684   by (simp add: max_def)
   685 
   686 
   687 subsubsection "Orders"
   688 
   689 axclass order < ord
   690   order_refl [iff]: "x <= x"
   691   order_trans: "x <= y ==> y <= z ==> x <= z"
   692   order_antisym: "x <= y ==> y <= x ==> x = y"
   693   order_less_le: "(x < y) = (x <= y & x ~= y)"
   694 
   695 
   696 text {* Reflexivity. *}
   697 
   698 lemma order_eq_refl: "!!x::'a::order. x = y ==> x <= y"
   699     -- {* This form is useful with the classical reasoner. *}
   700   apply (erule ssubst)
   701   apply (rule order_refl)
   702   done
   703 
   704 lemma order_less_irrefl [iff]: "~ x < (x::'a::order)"
   705   by (simp add: order_less_le)
   706 
   707 lemma order_le_less: "((x::'a::order) <= y) = (x < y | x = y)"
   708     -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
   709   apply (simp add: order_less_le)
   710   apply blast
   711   done
   712 
   713 lemmas order_le_imp_less_or_eq = order_le_less [THEN iffD1, standard]
   714 
   715 lemma order_less_imp_le: "!!x::'a::order. x < y ==> x <= y"
   716   by (simp add: order_less_le)
   717 
   718 
   719 text {* Asymmetry. *}
   720 
   721 lemma order_less_not_sym: "(x::'a::order) < y ==> ~ (y < x)"
   722   by (simp add: order_less_le order_antisym)
   723 
   724 lemma order_less_asym: "x < (y::'a::order) ==> (~P ==> y < x) ==> P"
   725   apply (drule order_less_not_sym)
   726   apply (erule contrapos_np)
   727   apply simp
   728   done
   729 
   730 
   731 text {* Transitivity. *}
   732 
   733 lemma order_less_trans: "!!x::'a::order. [| x < y; y < z |] ==> x < z"
   734   apply (simp add: order_less_le)
   735   apply (blast intro: order_trans order_antisym)
   736   done
   737 
   738 lemma order_le_less_trans: "!!x::'a::order. [| x <= y; y < z |] ==> x < z"
   739   apply (simp add: order_less_le)
   740   apply (blast intro: order_trans order_antisym)
   741   done
   742 
   743 lemma order_less_le_trans: "!!x::'a::order. [| x < y; y <= z |] ==> x < z"
   744   apply (simp add: order_less_le)
   745   apply (blast intro: order_trans order_antisym)
   746   done
   747 
   748 
   749 text {* Useful for simplification, but too risky to include by default. *}
   750 
   751 lemma order_less_imp_not_less: "(x::'a::order) < y ==>  (~ y < x) = True"
   752   by (blast elim: order_less_asym)
   753 
   754 lemma order_less_imp_triv: "(x::'a::order) < y ==>  (y < x --> P) = True"
   755   by (blast elim: order_less_asym)
   756 
   757 lemma order_less_imp_not_eq: "(x::'a::order) < y ==>  (x = y) = False"
   758   by auto
   759 
   760 lemma order_less_imp_not_eq2: "(x::'a::order) < y ==>  (y = x) = False"
   761   by auto
   762 
   763 
   764 text {* Other operators. *}
   765 
   766 lemma min_leastR: "(!!x::'a::order. least <= x) ==> min x least = least"
   767   apply (simp add: min_def)
   768   apply (blast intro: order_antisym)
   769   done
   770 
   771 lemma max_leastR: "(!!x::'a::order. least <= x) ==> max x least = x"
   772   apply (simp add: max_def)
   773   apply (blast intro: order_antisym)
   774   done
   775 
   776 
   777 subsubsection {* Least value operator *}
   778 
   779 constdefs
   780   Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
   781   "Least P == THE x. P x & (ALL y. P y --> x <= y)"
   782     -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
   783 
   784 lemma LeastI2:
   785   "[| P (x::'a::order);
   786       !!y. P y ==> x <= y;
   787       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
   788    ==> Q (Least P)"
   789   apply (unfold Least_def)
   790   apply (rule theI2)
   791     apply (blast intro: order_antisym)+
   792   done
   793 
   794 lemma Least_equality:
   795     "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
   796   apply (simp add: Least_def)
   797   apply (rule the_equality)
   798   apply (auto intro!: order_antisym)
   799   done
   800 
   801 
   802 subsubsection "Linear / total orders"
   803 
   804 axclass linorder < order
   805   linorder_linear: "x <= y | y <= x"
   806 
   807 lemma linorder_less_linear: "!!x::'a::linorder. x<y | x=y | y<x"
   808   apply (simp add: order_less_le)
   809   apply (insert linorder_linear)
   810   apply blast
   811   done
   812 
   813 lemma linorder_cases [case_names less equal greater]:
   814     "((x::'a::linorder) < y ==> P) ==> (x = y ==> P) ==> (y < x ==> P) ==> P"
   815   apply (insert linorder_less_linear)
   816   apply blast
   817   done
   818 
   819 lemma linorder_not_less: "!!x::'a::linorder. (~ x < y) = (y <= x)"
   820   apply (simp add: order_less_le)
   821   apply (insert linorder_linear)
   822   apply (blast intro: order_antisym)
   823   done
   824 
   825 lemma linorder_not_le: "!!x::'a::linorder. (~ x <= y) = (y < x)"
   826   apply (simp add: order_less_le)
   827   apply (insert linorder_linear)
   828   apply (blast intro: order_antisym)
   829   done
   830 
   831 lemma linorder_neq_iff: "!!x::'a::linorder. (x ~= y) = (x<y | y<x)"
   832   apply (cut_tac x = x and y = y in linorder_less_linear)
   833   apply auto
   834   done
   835 
   836 lemma linorder_neqE: "x ~= (y::'a::linorder) ==> (x < y ==> R) ==> (y < x ==> R) ==> R"
   837   apply (simp add: linorder_neq_iff)
   838   apply blast
   839   done
   840 
   841 
   842 subsubsection "Min and max on (linear) orders"
   843 
   844 lemma min_same [simp]: "min (x::'a::order) x = x"
   845   by (simp add: min_def)
   846 
   847 lemma max_same [simp]: "max (x::'a::order) x = x"
   848   by (simp add: max_def)
   849 
   850 lemma le_max_iff_disj: "!!z::'a::linorder. (z <= max x y) = (z <= x | z <= y)"
   851   apply (simp add: max_def)
   852   apply (insert linorder_linear)
   853   apply (blast intro: order_trans)
   854   done
   855 
   856 lemma le_maxI1: "(x::'a::linorder) <= max x y"
   857   by (simp add: le_max_iff_disj)
   858 
   859 lemma le_maxI2: "(y::'a::linorder) <= max x y"
   860     -- {* CANNOT use with @{text "[intro!]"} because blast will give PROOF FAILED. *}
   861   by (simp add: le_max_iff_disj)
   862 
   863 lemma less_max_iff_disj: "!!z::'a::linorder. (z < max x y) = (z < x | z < y)"
   864   apply (simp add: max_def order_le_less)
   865   apply (insert linorder_less_linear)
   866   apply (blast intro: order_less_trans)
   867   done
   868 
   869 lemma max_le_iff_conj [simp]:
   870     "!!z::'a::linorder. (max x y <= z) = (x <= z & y <= z)"
   871   apply (simp add: max_def)
   872   apply (insert linorder_linear)
   873   apply (blast intro: order_trans)
   874   done
   875 
   876 lemma max_less_iff_conj [simp]:
   877     "!!z::'a::linorder. (max x y < z) = (x < z & y < z)"
   878   apply (simp add: order_le_less max_def)
   879   apply (insert linorder_less_linear)
   880   apply (blast intro: order_less_trans)
   881   done
   882 
   883 lemma le_min_iff_conj [simp]:
   884     "!!z::'a::linorder. (z <= min x y) = (z <= x & z <= y)"
   885     -- {* @{text "[iff]"} screws up a @{text blast} in MiniML *}
   886   apply (simp add: min_def)
   887   apply (insert linorder_linear)
   888   apply (blast intro: order_trans)
   889   done
   890 
   891 lemma min_less_iff_conj [simp]:
   892     "!!z::'a::linorder. (z < min x y) = (z < x & z < y)"
   893   apply (simp add: order_le_less min_def)
   894   apply (insert linorder_less_linear)
   895   apply (blast intro: order_less_trans)
   896   done
   897 
   898 lemma min_le_iff_disj: "!!z::'a::linorder. (min x y <= z) = (x <= z | y <= z)"
   899   apply (simp add: min_def)
   900   apply (insert linorder_linear)
   901   apply (blast intro: order_trans)
   902   done
   903 
   904 lemma min_less_iff_disj: "!!z::'a::linorder. (min x y < z) = (x < z | y < z)"
   905   apply (simp add: min_def order_le_less)
   906   apply (insert linorder_less_linear)
   907   apply (blast intro: order_less_trans)
   908   done
   909 
   910 lemma max_assoc: "!!x::'a::linorder. max (max x y) z = max x (max y z)"
   911 apply(simp add:max_def)
   912 apply(rule conjI)
   913 apply(blast intro:order_trans)
   914 apply(simp add:linorder_not_le)
   915 apply(blast dest: order_less_trans order_le_less_trans)
   916 done
   917 
   918 lemma max_commute: "!!x::'a::linorder. max x y = max y x"
   919 apply(simp add:max_def)
   920 apply(rule conjI)
   921 apply(blast intro:order_antisym)
   922 apply(simp add:linorder_not_le)
   923 apply(blast dest: order_less_trans)
   924 done
   925 
   926 lemmas max_ac = max_assoc max_commute
   927                 mk_left_commute[of max,OF max_assoc max_commute]
   928 
   929 lemma min_assoc: "!!x::'a::linorder. min (min x y) z = min x (min y z)"
   930 apply(simp add:min_def)
   931 apply(rule conjI)
   932 apply(blast intro:order_trans)
   933 apply(simp add:linorder_not_le)
   934 apply(blast dest: order_less_trans order_le_less_trans)
   935 done
   936 
   937 lemma min_commute: "!!x::'a::linorder. min x y = min y x"
   938 apply(simp add:min_def)
   939 apply(rule conjI)
   940 apply(blast intro:order_antisym)
   941 apply(simp add:linorder_not_le)
   942 apply(blast dest: order_less_trans)
   943 done
   944 
   945 lemmas min_ac = min_assoc min_commute
   946                 mk_left_commute[of min,OF min_assoc min_commute]
   947 
   948 lemma split_min:
   949     "P (min (i::'a::linorder) j) = ((i <= j --> P(i)) & (~ i <= j --> P(j)))"
   950   by (simp add: min_def)
   951 
   952 lemma split_max:
   953     "P (max (i::'a::linorder) j) = ((i <= j --> P(j)) & (~ i <= j --> P(i)))"
   954   by (simp add: max_def)
   955 
   956 
   957 subsubsection "Bounded quantifiers"
   958 
   959 syntax
   960   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _<_./ _)"  [0, 0, 10] 10)
   961   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _<_./ _)"  [0, 0, 10] 10)
   962   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _<=_./ _)" [0, 0, 10] 10)
   963   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _<=_./ _)" [0, 0, 10] 10)
   964 
   965 syntax (xsymbols)
   966   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
   967   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
   968   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
   969   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
   970 
   971 syntax (HOL)
   972   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
   973   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
   974   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
   975   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
   976 
   977 translations
   978  "ALL x<y. P"   =>  "ALL x. x < y --> P"
   979  "EX x<y. P"    =>  "EX x. x < y  & P"
   980  "ALL x<=y. P"  =>  "ALL x. x <= y --> P"
   981  "EX x<=y. P"   =>  "EX x. x <= y & P"
   982 
   983 end