src/HOL/HOL.thy
 author paulson Thu Mar 04 12:06:07 2004 +0100 (2004-03-04) changeset 14430 5cb24165a2e1 parent 14398 c5c47703f763 child 14444 24724afce166 permissions -rw-r--r--
new material from Avigad, and simplified treatment of division by 0
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 *)
7 header {* The basis of Higher-Order Logic *}
9 theory HOL = CPure
10 files ("HOL_lemmas.ML") ("cladata.ML") ("blastdata.ML") ("simpdata.ML"):
13 subsection {* Primitive logic *}
15 subsubsection {* Core syntax *}
17 classes type < logic
18 defaultsort type
20 global
22 typedecl bool
24 arities
25   bool :: type
26   fun :: (type, type) type
28 judgment
29   Trueprop      :: "bool => prop"                   ("(_)" 5)
31 consts
32   Not           :: "bool => bool"                   ("~ _"  40)
33   True          :: bool
34   False         :: bool
35   If            :: "[bool, 'a, 'a] => 'a"           ("(if (_)/ then (_)/ else (_))" 10)
36   arbitrary     :: 'a
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"
44   "="           :: "['a, 'a] => bool"               (infixl 50)
45   &             :: "[bool, bool] => bool"           (infixr 35)
46   "|"           :: "[bool, bool] => bool"           (infixr 30)
47   -->           :: "[bool, bool] => bool"           (infixr 25)
49 local
52 subsubsection {* Additional concrete syntax *}
54 nonterminals
55   letbinds  letbind
56   case_syn  cases_syn
58 syntax
59   "_not_equal"  :: "['a, 'a] => bool"                    (infixl "~=" 50)
60   "_The"        :: "[pttrn, bool] => 'a"                 ("(3THE _./ _)" [0, 10] 10)
62   "_bind"       :: "[pttrn, 'a] => letbind"              ("(2_ =/ _)" 10)
63   ""            :: "letbind => letbinds"                 ("_")
64   "_binds"      :: "[letbind, letbinds] => letbinds"     ("_;/ _")
65   "_Let"        :: "[letbinds, 'a] => 'a"                ("(let (_)/ in (_))" 10)
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"  ("_/ | _")
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)"
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 *}
85 syntax (output)
86   "="           :: "['a, 'a] => bool"                    (infix 50)
87   "_not_equal"  :: "['a, 'a] => bool"                    (infix "~=" 50)
89 syntax (xsymbols)
90   Not           :: "bool => bool"                        ("\<not> _"  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> _")*)
101 syntax (xsymbols output)
102   "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
104 syntax (HTML output)
105   Not           :: "bool => bool"                        ("\<not> _"  40)
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)
113 subsubsection {* Axioms and basic definitions *}
115 axioms
116   eq_reflection: "(x=y) ==> (x==y)"
118   refl:         "t = (t::'a)"
119   subst:        "[| s = t; P(s) |] ==> P(t::'a)"
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 *}
126   the_eq_trivial: "(THE x. x = a) = (a::'a)"
128   impI:         "(P ==> Q) ==> P-->Q"
129   mp:           "[| P-->Q;  P |] ==> Q"
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)"
141 axioms
142   iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
143   True_or_False:  "(P=True) | (P=False)"
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)"
149 finalconsts
150   "op ="
151   "op -->"
152   The
153   arbitrary
155 subsubsection {* Generic algebraic operations *}
157 axclass zero < type
158 axclass one < type
159 axclass plus < type
160 axclass minus < type
161 axclass times < type
162 axclass inverse < type
164 global
166 consts
167   "0"           :: "'a::zero"                       ("0")
168   "1"           :: "'a::one"                        ("1")
169   "+"           :: "['a::plus, 'a]  => 'a"          (infixl 65)
170   -             :: "['a::minus, 'a] => 'a"          (infixl 65)
171   uminus        :: "['a::minus] => 'a"              ("- _"  80)
172   *             :: "['a::times, 'a] => 'a"          (infixl 70)
174 syntax
175   "_index1"  :: index    ("\<^sub>1")
176 translations
177   (index) "\<^sub>1" == "_index 1"
179 local
181 typed_print_translation {*
182   let
183     fun tr' c = (c, fn show_sorts => fn T => fn ts =>
184       if T = dummyT orelse not (! show_types) andalso can Term.dest_Type T then raise Match
185       else Syntax.const Syntax.constrainC \$ Syntax.const c \$ Syntax.term_of_typ show_sorts T);
186   in [tr' "0", tr' "1"] end;
187 *} -- {* show types that are presumably too general *}
190 consts
191   abs           :: "'a::minus => 'a"
192   inverse       :: "'a::inverse => 'a"
193   divide        :: "['a::inverse, 'a] => 'a"        (infixl "'/" 70)
195 syntax (xsymbols)
196   abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
197 syntax (HTML output)
198   abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
201 subsection {* Theory and package setup *}
203 subsubsection {* Basic lemmas *}
205 use "HOL_lemmas.ML"
206 theorems case_split = case_split_thm [case_names True False]
209 subsubsection {* Intuitionistic Reasoning *}
211 lemma impE':
212   assumes 1: "P --> Q"
213     and 2: "Q ==> R"
214     and 3: "P --> Q ==> P"
215   shows R
216 proof -
217   from 3 and 1 have P .
218   with 1 have Q by (rule impE)
219   with 2 show R .
220 qed
222 lemma allE':
223   assumes 1: "ALL x. P x"
224     and 2: "P x ==> ALL x. P x ==> Q"
225   shows Q
226 proof -
227   from 1 have "P x" by (rule spec)
228   from this and 1 show Q by (rule 2)
229 qed
231 lemma notE':
232   assumes 1: "~ P"
233     and 2: "~ P ==> P"
234   shows R
235 proof -
236   from 2 and 1 have P .
237   with 1 show R by (rule notE)
238 qed
240 lemmas [CPure.elim!] = disjE iffE FalseE conjE exE
241   and [CPure.intro!] = iffI conjI impI TrueI notI allI refl
242   and [CPure.elim 2] = allE notE' impE'
243   and [CPure.intro] = exI disjI2 disjI1
245 lemmas [trans] = trans
246   and [sym] = sym not_sym
247   and [CPure.elim?] = iffD1 iffD2 impE
250 subsubsection {* Atomizing meta-level connectives *}
252 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
253 proof
254   assume "!!x. P x"
255   show "ALL x. P x" by (rule allI)
256 next
257   assume "ALL x. P x"
258   thus "!!x. P x" by (rule allE)
259 qed
261 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
262 proof
263   assume r: "A ==> B"
264   show "A --> B" by (rule impI) (rule r)
265 next
266   assume "A --> B" and A
267   thus B by (rule mp)
268 qed
270 lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
271 proof
272   assume "x == y"
273   show "x = y" by (unfold prems) (rule refl)
274 next
275   assume "x = y"
276   thus "x == y" by (rule eq_reflection)
277 qed
279 lemma atomize_conj [atomize]:
280   "(!!C. (A ==> B ==> PROP C) ==> PROP C) == Trueprop (A & B)"
281 proof
282   assume "!!C. (A ==> B ==> PROP C) ==> PROP C"
283   show "A & B" by (rule conjI)
284 next
285   fix C
286   assume "A & B"
287   assume "A ==> B ==> PROP C"
288   thus "PROP C"
289   proof this
290     show A by (rule conjunct1)
291     show B by (rule conjunct2)
292   qed
293 qed
295 lemmas [symmetric, rulify] = atomize_all atomize_imp
298 subsubsection {* Classical Reasoner setup *}
301 setup hypsubst_setup
303 ML_setup {*
304   Context.>> (ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac));
305 *}
307 setup Classical.setup
308 setup clasetup
310 lemmas [intro?] = ext
311   and [elim?] = ex1_implies_ex
313 use "blastdata.ML"
314 setup Blast.setup
317 subsubsection {* Simplifier setup *}
319 lemma meta_eq_to_obj_eq: "x == y ==> x = y"
320 proof -
321   assume r: "x == y"
322   show "x = y" by (unfold r) (rule refl)
323 qed
325 lemma eta_contract_eq: "(%s. f s) = f" ..
327 lemma simp_thms:
328   shows not_not: "(~ ~ P) = P"
329   and
330     "(P ~= Q) = (P = (~Q))"
331     "(P | ~P) = True"    "(~P | P) = True"
332     "((~P) = (~Q)) = (P=Q)"
333     "(x = x) = True"
334     "(~True) = False"  "(~False) = True"
335     "(~P) ~= P"  "P ~= (~P)"
336     "(True=P) = P"  "(P=True) = P"  "(False=P) = (~P)"  "(P=False) = (~P)"
337     "(True --> P) = P"  "(False --> P) = True"
338     "(P --> True) = True"  "(P --> P) = True"
339     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
340     "(P & True) = P"  "(True & P) = P"
341     "(P & False) = False"  "(False & P) = False"
342     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
343     "(P & ~P) = False"    "(~P & P) = False"
344     "(P | True) = True"  "(True | P) = True"
345     "(P | False) = P"  "(False | P) = P"
346     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
347     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
348     -- {* needed for the one-point-rule quantifier simplification procs *}
349     -- {* essential for termination!! *} and
350     "!!P. (EX x. x=t & P(x)) = P(t)"
351     "!!P. (EX x. t=x & P(x)) = P(t)"
352     "!!P. (ALL x. x=t --> P(x)) = P(t)"
353     "!!P. (ALL x. t=x --> P(x)) = P(t)"
354   by (blast, blast, blast, blast, blast, rules+)
356 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
357   by rules
359 lemma ex_simps:
360   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
361   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
362   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
363   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
364   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
365   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
366   -- {* Miniscoping: pushing in existential quantifiers. *}
367   by (rules | blast)+
369 lemma all_simps:
370   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
371   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
372   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
373   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
374   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
375   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
376   -- {* Miniscoping: pushing in universal quantifiers. *}
377   by (rules | blast)+
379 lemma disj_absorb: "(A | A) = A"
380   by blast
382 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
383   by blast
385 lemma conj_absorb: "(A & A) = A"
386   by blast
388 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
389   by blast
391 lemma eq_ac:
392   shows eq_commute: "(a=b) = (b=a)"
393     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
394     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (rules, blast+)
395 lemma neq_commute: "(a~=b) = (b~=a)" by rules
397 lemma conj_comms:
398   shows conj_commute: "(P&Q) = (Q&P)"
399     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by rules+
400 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by rules
402 lemma disj_comms:
403   shows disj_commute: "(P|Q) = (Q|P)"
404     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by rules+
405 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by rules
407 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by rules
408 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by rules
410 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by rules
411 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by rules
413 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by rules
414 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by rules
415 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by rules
417 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
418 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
419 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
421 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
422 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
424 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by rules
425 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
426 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
427 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
428 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
429 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
430   by blast
431 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
433 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by rules
436 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
437   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
438   -- {* cases boil down to the same thing. *}
439   by blast
441 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
442 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
443 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by rules
444 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by rules
446 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by rules
447 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by rules
449 text {*
450   \medskip The @{text "&"} congruence rule: not included by default!
451   May slow rewrite proofs down by as much as 50\% *}
453 lemma conj_cong:
454     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
455   by rules
457 lemma rev_conj_cong:
458     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
459   by rules
461 text {* The @{text "|"} congruence rule: not included by default! *}
463 lemma disj_cong:
464     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
465   by blast
467 lemma eq_sym_conv: "(x = y) = (y = x)"
468   by rules
471 text {* \medskip if-then-else rules *}
473 lemma if_True: "(if True then x else y) = x"
474   by (unfold if_def) blast
476 lemma if_False: "(if False then x else y) = y"
477   by (unfold if_def) blast
479 lemma if_P: "P ==> (if P then x else y) = x"
480   by (unfold if_def) blast
482 lemma if_not_P: "~P ==> (if P then x else y) = y"
483   by (unfold if_def) blast
485 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
486   apply (rule case_split [of Q])
487    apply (subst if_P)
488     prefer 3 apply (subst if_not_P, blast+)
489   done
491 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
492 by (subst split_if, blast)
494 lemmas if_splits = split_if split_if_asm
496 lemma if_def2: "(if Q then x else y) = ((Q --> x) & (~ Q --> y))"
497   by (rule split_if)
499 lemma if_cancel: "(if c then x else x) = x"
500 by (subst split_if, blast)
502 lemma if_eq_cancel: "(if x = y then y else x) = x"
503 by (subst split_if, blast)
505 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
506   -- {* This form is useful for expanding @{text if}s on the RIGHT of the @{text "==>"} symbol. *}
507   by (rule split_if)
509 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
510   -- {* And this form is useful for expanding @{text if}s on the LEFT. *}
511   apply (subst split_if, blast)
512   done
514 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) rules
515 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) rules
517 subsubsection {* Actual Installation of the Simplifier *}
519 use "simpdata.ML"
520 setup Simplifier.setup
521 setup "Simplifier.method_setup Splitter.split_modifiers" setup simpsetup
522 setup Splitter.setup setup Clasimp.setup
524 declare disj_absorb [simp] conj_absorb [simp]
526 lemma ex1_eq[iff]: "EX! x. x = t" "EX! x. t = x"
527 by blast+
529 theorem choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
530   apply (rule iffI)
531   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
532   apply (fast dest!: theI')
533   apply (fast intro: ext the1_equality [symmetric])
534   apply (erule ex1E)
535   apply (rule allI)
536   apply (rule ex1I)
537   apply (erule spec)
538   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
539   apply (erule impE)
540   apply (rule allI)
541   apply (rule_tac P = "xa = x" in case_split_thm)
542   apply (drule_tac  x = x in fun_cong, simp_all)
543   done
545 text{*Needs only HOL-lemmas:*}
546 lemma mk_left_commute:
547   assumes a: "\<And>x y z. f (f x y) z = f x (f y z)" and
548           c: "\<And>x y. f x y = f y x"
549   shows "f x (f y z) = f y (f x z)"
550 by(rule trans[OF trans[OF c a] arg_cong[OF c, of "f y"]])
553 subsubsection {* Generic cases and induction *}
555 constdefs
556   induct_forall :: "('a => bool) => bool"
557   "induct_forall P == \<forall>x. P x"
558   induct_implies :: "bool => bool => bool"
559   "induct_implies A B == A --> B"
560   induct_equal :: "'a => 'a => bool"
561   "induct_equal x y == x = y"
562   induct_conj :: "bool => bool => bool"
563   "induct_conj A B == A & B"
565 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
566   by (simp only: atomize_all induct_forall_def)
568 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
569   by (simp only: atomize_imp induct_implies_def)
571 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
572   by (simp only: atomize_eq induct_equal_def)
574 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
575     induct_conj (induct_forall A) (induct_forall B)"
576   by (unfold induct_forall_def induct_conj_def) rules
578 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
579     induct_conj (induct_implies C A) (induct_implies C B)"
580   by (unfold induct_implies_def induct_conj_def) rules
582 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
583 proof
584   assume r: "induct_conj A B ==> PROP C" and A B
585   show "PROP C" by (rule r) (simp! add: induct_conj_def)
586 next
587   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
588   show "PROP C" by (rule r) (simp! add: induct_conj_def)+
589 qed
591 lemma induct_impliesI: "(A ==> B) ==> induct_implies A B"
592   by (simp add: induct_implies_def)
594 lemmas induct_atomize = atomize_conj induct_forall_eq induct_implies_eq induct_equal_eq
595 lemmas induct_rulify1 [symmetric, standard] = induct_forall_eq induct_implies_eq induct_equal_eq
596 lemmas induct_rulify2 = induct_forall_def induct_implies_def induct_equal_def induct_conj_def
597 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
599 hide const induct_forall induct_implies induct_equal induct_conj
602 text {* Method setup. *}
604 ML {*
605   structure InductMethod = InductMethodFun
606   (struct
607     val dest_concls = HOLogic.dest_concls;
608     val cases_default = thm "case_split";
609     val local_impI = thm "induct_impliesI";
610     val conjI = thm "conjI";
611     val atomize = thms "induct_atomize";
612     val rulify1 = thms "induct_rulify1";
613     val rulify2 = thms "induct_rulify2";
614     val localize = [Thm.symmetric (thm "induct_implies_def")];
615   end);
616 *}
618 setup InductMethod.setup
621 subsection {* Order signatures and orders *}
623 axclass
624   ord < type
626 syntax
627   "op <"        :: "['a::ord, 'a] => bool"             ("op <")
628   "op <="       :: "['a::ord, 'a] => bool"             ("op <=")
630 global
632 consts
633   "op <"        :: "['a::ord, 'a] => bool"             ("(_/ < _)"  [50, 51] 50)
634   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ <= _)" [50, 51] 50)
636 local
638 syntax (xsymbols)
639   "op <="       :: "['a::ord, 'a] => bool"             ("op \<le>")
640   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
643 lemma Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
644 by blast
646 subsubsection {* Monotonicity *}
648 locale mono =
649   fixes f
650   assumes mono: "A <= B ==> f A <= f B"
652 lemmas monoI [intro?] = mono.intro
653   and monoD [dest?] = mono.mono
655 constdefs
656   min :: "['a::ord, 'a] => 'a"
657   "min a b == (if a <= b then a else b)"
658   max :: "['a::ord, 'a] => 'a"
659   "max a b == (if a <= b then b else a)"
661 lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
662   by (simp add: min_def)
664 lemma min_of_mono:
665     "ALL x y. (f x <= f y) = (x <= y) ==> min (f m) (f n) = f (min m n)"
666   by (simp add: min_def)
668 lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
669   by (simp add: max_def)
671 lemma max_of_mono:
672     "ALL x y. (f x <= f y) = (x <= y) ==> max (f m) (f n) = f (max m n)"
673   by (simp add: max_def)
676 subsubsection "Orders"
678 axclass order < ord
679   order_refl [iff]: "x <= x"
680   order_trans: "x <= y ==> y <= z ==> x <= z"
681   order_antisym: "x <= y ==> y <= x ==> x = y"
682   order_less_le: "(x < y) = (x <= y & x ~= y)"
685 text {* Reflexivity. *}
687 lemma order_eq_refl: "!!x::'a::order. x = y ==> x <= y"
688     -- {* This form is useful with the classical reasoner. *}
689   apply (erule ssubst)
690   apply (rule order_refl)
691   done
693 lemma order_less_irrefl [iff]: "~ x < (x::'a::order)"
694   by (simp add: order_less_le)
696 lemma order_le_less: "((x::'a::order) <= y) = (x < y | x = y)"
697     -- {* NOT suitable for iff, since it can cause PROOF FAILED. *}
698   apply (simp add: order_less_le, blast)
699   done
701 lemmas order_le_imp_less_or_eq = order_le_less [THEN iffD1, standard]
703 lemma order_less_imp_le: "!!x::'a::order. x < y ==> x <= y"
704   by (simp add: order_less_le)
707 text {* Asymmetry. *}
709 lemma order_less_not_sym: "(x::'a::order) < y ==> ~ (y < x)"
710   by (simp add: order_less_le order_antisym)
712 lemma order_less_asym: "x < (y::'a::order) ==> (~P ==> y < x) ==> P"
713   apply (drule order_less_not_sym)
714   apply (erule contrapos_np, simp)
715   done
717 lemma order_eq_iff: "!!x::'a::order. (x = y) = (x \<le> y & y \<le> x)"
718 by (blast intro: order_antisym)
721 text {* Transitivity. *}
723 lemma order_less_trans: "!!x::'a::order. [| x < y; y < z |] ==> x < z"
724   apply (simp add: order_less_le)
725   apply (blast intro: order_trans order_antisym)
726   done
728 lemma order_le_less_trans: "!!x::'a::order. [| x <= y; y < z |] ==> x < z"
729   apply (simp add: order_less_le)
730   apply (blast intro: order_trans order_antisym)
731   done
733 lemma order_less_le_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
739 text {* Useful for simplification, but too risky to include by default. *}
741 lemma order_less_imp_not_less: "(x::'a::order) < y ==>  (~ y < x) = True"
742   by (blast elim: order_less_asym)
744 lemma order_less_imp_triv: "(x::'a::order) < y ==>  (y < x --> P) = True"
745   by (blast elim: order_less_asym)
747 lemma order_less_imp_not_eq: "(x::'a::order) < y ==>  (x = y) = False"
748   by auto
750 lemma order_less_imp_not_eq2: "(x::'a::order) < y ==>  (y = x) = False"
751   by auto
754 text {* Other operators. *}
756 lemma min_leastR: "(!!x::'a::order. least <= x) ==> min x least = least"
757   apply (simp add: min_def)
758   apply (blast intro: order_antisym)
759   done
761 lemma max_leastR: "(!!x::'a::order. least <= x) ==> max x least = x"
762   apply (simp add: max_def)
763   apply (blast intro: order_antisym)
764   done
767 subsubsection {* Least value operator *}
769 constdefs
770   Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
771   "Least P == THE x. P x & (ALL y. P y --> x <= y)"
772     -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
774 lemma LeastI2:
775   "[| P (x::'a::order);
776       !!y. P y ==> x <= y;
777       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
778    ==> Q (Least P)"
779   apply (unfold Least_def)
780   apply (rule theI2)
781     apply (blast intro: order_antisym)+
782   done
784 lemma Least_equality:
785     "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
786   apply (simp add: Least_def)
787   apply (rule the_equality)
788   apply (auto intro!: order_antisym)
789   done
792 subsubsection "Linear / total orders"
794 axclass linorder < order
795   linorder_linear: "x <= y | y <= x"
797 lemma linorder_less_linear: "!!x::'a::linorder. x<y | x=y | y<x"
798   apply (simp add: order_less_le)
799   apply (insert linorder_linear, blast)
800   done
802 lemma linorder_le_cases [case_names le ge]:
803     "((x::'a::linorder) \<le> y ==> P) ==> (y \<le> x ==> P) ==> P"
804   by (insert linorder_linear, blast)
806 lemma linorder_cases [case_names less equal greater]:
807     "((x::'a::linorder) < y ==> P) ==> (x = y ==> P) ==> (y < x ==> P) ==> P"
808   by (insert linorder_less_linear, blast)
810 lemma linorder_not_less: "!!x::'a::linorder. (~ x < y) = (y <= x)"
811   apply (simp add: order_less_le)
812   apply (insert linorder_linear)
813   apply (blast intro: order_antisym)
814   done
816 lemma linorder_not_le: "!!x::'a::linorder. (~ x <= y) = (y < x)"
817   apply (simp add: order_less_le)
818   apply (insert linorder_linear)
819   apply (blast intro: order_antisym)
820   done
822 lemma linorder_neq_iff: "!!x::'a::linorder. (x ~= y) = (x<y | y<x)"
823 by (cut_tac x = x and y = y in linorder_less_linear, auto)
825 lemma linorder_neqE: "x ~= (y::'a::linorder) ==> (x < y ==> R) ==> (y < x ==> R) ==> R"
826 by (simp add: linorder_neq_iff, blast)
829 subsubsection "Min and max on (linear) orders"
831 lemma min_same [simp]: "min (x::'a::order) x = x"
832   by (simp add: min_def)
834 lemma max_same [simp]: "max (x::'a::order) x = x"
835   by (simp add: max_def)
837 lemma le_max_iff_disj: "!!z::'a::linorder. (z <= max x y) = (z <= x | z <= y)"
838   apply (simp add: max_def)
839   apply (insert linorder_linear)
840   apply (blast intro: order_trans)
841   done
843 lemma le_maxI1: "(x::'a::linorder) <= max x y"
844   by (simp add: le_max_iff_disj)
846 lemma le_maxI2: "(y::'a::linorder) <= max x y"
847     -- {* CANNOT use with @{text "[intro!]"} because blast will give PROOF FAILED. *}
848   by (simp add: le_max_iff_disj)
850 lemma less_max_iff_disj: "!!z::'a::linorder. (z < max x y) = (z < x | z < y)"
851   apply (simp add: max_def order_le_less)
852   apply (insert linorder_less_linear)
853   apply (blast intro: order_less_trans)
854   done
856 lemma max_le_iff_conj [simp]:
857     "!!z::'a::linorder. (max x y <= z) = (x <= z & y <= z)"
858   apply (simp add: max_def)
859   apply (insert linorder_linear)
860   apply (blast intro: order_trans)
861   done
863 lemma max_less_iff_conj [simp]:
864     "!!z::'a::linorder. (max x y < z) = (x < z & y < z)"
865   apply (simp add: order_le_less max_def)
866   apply (insert linorder_less_linear)
867   apply (blast intro: order_less_trans)
868   done
870 lemma le_min_iff_conj [simp]:
871     "!!z::'a::linorder. (z <= min x y) = (z <= x & z <= y)"
872     -- {* @{text "[iff]"} screws up a @{text blast} in MiniML *}
873   apply (simp add: min_def)
874   apply (insert linorder_linear)
875   apply (blast intro: order_trans)
876   done
878 lemma min_less_iff_conj [simp]:
879     "!!z::'a::linorder. (z < min x y) = (z < x & z < y)"
880   apply (simp add: order_le_less min_def)
881   apply (insert linorder_less_linear)
882   apply (blast intro: order_less_trans)
883   done
885 lemma min_le_iff_disj: "!!z::'a::linorder. (min x y <= z) = (x <= z | y <= z)"
886   apply (simp add: min_def)
887   apply (insert linorder_linear)
888   apply (blast intro: order_trans)
889   done
891 lemma min_less_iff_disj: "!!z::'a::linorder. (min x y < z) = (x < z | y < z)"
892   apply (simp add: min_def order_le_less)
893   apply (insert linorder_less_linear)
894   apply (blast intro: order_less_trans)
895   done
897 lemma max_assoc: "!!x::'a::linorder. max (max x y) z = max x (max y z)"
899 apply(rule conjI)
900 apply(blast intro:order_trans)
902 apply(blast dest: order_less_trans order_le_less_trans)
903 done
905 lemma max_commute: "!!x::'a::linorder. max x y = max y x"
907 apply(rule conjI)
908 apply(blast intro:order_antisym)
910 apply(blast dest: order_less_trans)
911 done
913 lemmas max_ac = max_assoc max_commute
914                 mk_left_commute[of max,OF max_assoc max_commute]
916 lemma min_assoc: "!!x::'a::linorder. min (min x y) z = min x (min y z)"
918 apply(rule conjI)
919 apply(blast intro:order_trans)
921 apply(blast dest: order_less_trans order_le_less_trans)
922 done
924 lemma min_commute: "!!x::'a::linorder. min x y = min y x"
926 apply(rule conjI)
927 apply(blast intro:order_antisym)
929 apply(blast dest: order_less_trans)
930 done
932 lemmas min_ac = min_assoc min_commute
933                 mk_left_commute[of min,OF min_assoc min_commute]
935 lemma split_min:
936     "P (min (i::'a::linorder) j) = ((i <= j --> P(i)) & (~ i <= j --> P(j)))"
937   by (simp add: min_def)
939 lemma split_max:
940     "P (max (i::'a::linorder) j) = ((i <= j --> P(j)) & (~ i <= j --> P(i)))"
941   by (simp add: max_def)
944 subsubsection {* Transitivity rules for calculational reasoning *}
947 lemma order_neq_le_trans: "a ~= b ==> (a::'a::order) <= b ==> a < b"
948   by (simp add: order_less_le)
950 lemma order_le_neq_trans: "(a::'a::order) <= b ==> a ~= b ==> a < b"
951   by (simp add: order_less_le)
953 lemma order_less_asym': "(a::'a::order) < b ==> b < a ==> P"
954   by (rule order_less_asym)
957 subsubsection {* Setup of transitivity reasoner *}
959 lemma less_imp_neq: "[| (x::'a::order) < y |] ==> x ~= y"
960   by (erule contrapos_pn, erule subst, rule order_less_irrefl)
962 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
963   by (erule subst, erule ssubst, assumption)
965 ML_setup {*
967 structure Trans_Tac = Trans_Tac_Fun (
968   struct
969     val less_reflE = thm "order_less_irrefl" RS thm "notE";
970     val le_refl = thm "order_refl";
971     val less_imp_le = thm "order_less_imp_le";
972     val not_lessI = thm "linorder_not_less" RS thm "iffD2";
973     val not_leI = thm "linorder_not_le" RS thm "iffD2";
974     val not_lessD = thm "linorder_not_less" RS thm "iffD1";
975     val not_leD = thm "linorder_not_le" RS thm "iffD1";
976     val eqI = thm "order_antisym";
977     val eqD1 = thm "order_eq_refl";
978     val eqD2 = thm "sym" RS thm "order_eq_refl";
979     val less_trans = thm "order_less_trans";
980     val less_le_trans = thm "order_less_le_trans";
981     val le_less_trans = thm "order_le_less_trans";
982     val le_trans = thm "order_trans";
983     val le_neq_trans = thm "order_le_neq_trans";
984     val neq_le_trans = thm "order_neq_le_trans";
985     val less_imp_neq = thm "less_imp_neq";
986     val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
988     fun decomp_gen sort sign (Trueprop \$ t) =
989       let fun of_sort t = Sign.of_sort sign (type_of t, sort)
990       fun dec (Const ("Not", _) \$ t) = (
991               case dec t of
992                 None => None
993               | Some (t1, rel, t2) => Some (t1, "~" ^ rel, t2))
994             | dec (Const ("op =",  _) \$ t1 \$ t2) =
995                 if of_sort t1
996                 then Some (t1, "=", t2)
997                 else None
998             | dec (Const ("op <=",  _) \$ t1 \$ t2) =
999                 if of_sort t1
1000                 then Some (t1, "<=", t2)
1001                 else None
1002             | dec (Const ("op <",  _) \$ t1 \$ t2) =
1003                 if of_sort t1
1004                 then Some (t1, "<", t2)
1005                 else None
1006             | dec _ = None
1007       in dec t end;
1009     val decomp_part = decomp_gen ["HOL.order"];
1010     val decomp_lin = decomp_gen ["HOL.linorder"];
1012   end);  (* struct *)
1014 Context.>> (fn thy => (simpset_ref_of thy :=
1015   simpset_of thy
1016     addSolver (mk_solver "Trans_linear" (fn _ => Trans_Tac.linear_tac))
1017     addSolver (mk_solver "Trans_partial" (fn _ => Trans_Tac.partial_tac));
1018   thy))
1019 *}
1021 (* Optional methods
1023 method_setup trans_partial =
1024   {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (trans_tac_partial)) *}
1025   {* simple transitivity reasoner *}
1026 method_setup trans_linear =
1027   {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (trans_tac_linear)) *}
1028   {* simple transitivity reasoner *}
1029 *)
1031 subsubsection "Bounded quantifiers"
1033 syntax
1034   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _<_./ _)"  [0, 0, 10] 10)
1035   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _<_./ _)"  [0, 0, 10] 10)
1036   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _<=_./ _)" [0, 0, 10] 10)
1037   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _<=_./ _)" [0, 0, 10] 10)
1039 syntax (xsymbols)
1040   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
1041   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
1042   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
1043   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
1045 syntax (HOL)
1046   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
1047   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
1048   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
1049   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
1051 translations
1052  "ALL x<y. P"   =>  "ALL x. x < y --> P"
1053  "EX x<y. P"    =>  "EX x. x < y  & P"
1054  "ALL x<=y. P"  =>  "ALL x. x <= y --> P"
1055  "EX x<=y. P"   =>  "EX x. x <= y & P"
1057 print_translation {*
1058 let
1059   fun all_tr' [Const ("_bound",_) \$ Free (v,_),
1060                Const("op -->",_) \$ (Const ("op <",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1061   (if v=v' then Syntax.const "_lessAll" \$ Syntax.mark_bound v' \$ n \$ P else raise Match)
1063   | all_tr' [Const ("_bound",_) \$ Free (v,_),
1064                Const("op -->",_) \$ (Const ("op <=",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1065   (if v=v' then Syntax.const "_leAll" \$ Syntax.mark_bound v' \$ n \$ P else raise Match);
1067   fun ex_tr' [Const ("_bound",_) \$ Free (v,_),
1068                Const("op &",_) \$ (Const ("op <",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1069   (if v=v' then Syntax.const "_lessEx" \$ Syntax.mark_bound v' \$ n \$ P else raise Match)
1071   | ex_tr' [Const ("_bound",_) \$ Free (v,_),
1072                Const("op &",_) \$ (Const ("op <=",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1073   (if v=v' then Syntax.const "_leEx" \$ Syntax.mark_bound v' \$ n \$ P else raise Match)
1074 in
1075 [("ALL ", all_tr'), ("EX ", ex_tr')]
1076 end
1077 *}
1079 end