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