src/HOL/HOL.thy
 author wenzelm Fri Apr 16 13:52:43 2004 +0200 (2004-04-16) changeset 14590 276ef51cedbf parent 14565 c6dc17aab88a child 14690 f61ea8bfa81e permissions -rw-r--r--
simplified ML code for setsubgoaler;
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_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
106   Not           :: "bool => bool"                        ("\<not> _"  40)
107   "op &"        :: "[bool, bool] => bool"                (infixr "\<and>" 35)
108   "op |"        :: "[bool, bool] => bool"                (infixr "\<or>" 30)
109   "_not_equal"  :: "['a, 'a] => bool"                    (infix "\<noteq>" 50)
110   "ALL "        :: "[idts, bool] => bool"                ("(3\<forall>_./ _)" [0, 10] 10)
111   "EX "         :: "[idts, bool] => bool"                ("(3\<exists>_./ _)" [0, 10] 10)
112   "EX! "        :: "[idts, bool] => bool"                ("(3\<exists>!_./ _)" [0, 10] 10)
114 syntax (HOL)
115   "ALL "        :: "[idts, bool] => bool"                ("(3! _./ _)" [0, 10] 10)
116   "EX "         :: "[idts, bool] => bool"                ("(3? _./ _)" [0, 10] 10)
117   "EX! "        :: "[idts, bool] => bool"                ("(3?! _./ _)" [0, 10] 10)
120 subsubsection {* Axioms and basic definitions *}
122 axioms
123   eq_reflection: "(x=y) ==> (x==y)"
125   refl:         "t = (t::'a)"
126   subst:        "[| s = t; P(s) |] ==> P(t::'a)"
128   ext:          "(!!x::'a. (f x ::'b) = g x) ==> (%x. f x) = (%x. g x)"
129     -- {* Extensionality is built into the meta-logic, and this rule expresses *}
130     -- {* a related property.  It is an eta-expanded version of the traditional *}
131     -- {* rule, and similar to the ABS rule of HOL *}
133   the_eq_trivial: "(THE x. x = a) = (a::'a)"
135   impI:         "(P ==> Q) ==> P-->Q"
136   mp:           "[| P-->Q;  P |] ==> Q"
138 defs
139   True_def:     "True      == ((%x::bool. x) = (%x. x))"
140   All_def:      "All(P)    == (P = (%x. True))"
141   Ex_def:       "Ex(P)     == !Q. (!x. P x --> Q) --> Q"
142   False_def:    "False     == (!P. P)"
143   not_def:      "~ P       == P-->False"
144   and_def:      "P & Q     == !R. (P-->Q-->R) --> R"
145   or_def:       "P | Q     == !R. (P-->R) --> (Q-->R) --> R"
146   Ex1_def:      "Ex1(P)    == ? x. P(x) & (! y. P(y) --> y=x)"
148 axioms
149   iff:          "(P-->Q) --> (Q-->P) --> (P=Q)"
150   True_or_False:  "(P=True) | (P=False)"
152 defs
153   Let_def:      "Let s f == f(s)"
154   if_def:       "If P x y == THE z::'a. (P=True --> z=x) & (P=False --> z=y)"
156 finalconsts
157   "op ="
158   "op -->"
159   The
160   arbitrary
162 subsubsection {* Generic algebraic operations *}
164 axclass zero < type
165 axclass one < type
166 axclass plus < type
167 axclass minus < type
168 axclass times < type
169 axclass inverse < type
171 global
173 consts
174   "0"           :: "'a::zero"                       ("0")
175   "1"           :: "'a::one"                        ("1")
176   "+"           :: "['a::plus, 'a]  => 'a"          (infixl 65)
177   -             :: "['a::minus, 'a] => 'a"          (infixl 65)
178   uminus        :: "['a::minus] => 'a"              ("- _"  80)
179   *             :: "['a::times, 'a] => 'a"          (infixl 70)
181 syntax
182   "_index1"  :: index    ("\<^sub>1")
183 translations
184   (index) "\<^sub>1" == "_index 1"
186 local
188 typed_print_translation {*
189   let
190     fun tr' c = (c, fn show_sorts => fn T => fn ts =>
191       if T = dummyT orelse not (! show_types) andalso can Term.dest_Type T then raise Match
192       else Syntax.const Syntax.constrainC \$ Syntax.const c \$ Syntax.term_of_typ show_sorts T);
193   in [tr' "0", tr' "1"] end;
194 *} -- {* show types that are presumably too general *}
197 consts
198   abs           :: "'a::minus => 'a"
199   inverse       :: "'a::inverse => 'a"
200   divide        :: "['a::inverse, 'a] => 'a"        (infixl "'/" 70)
202 syntax (xsymbols)
203   abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
204 syntax (HTML output)
205   abs :: "'a::minus => 'a"    ("\<bar>_\<bar>")
208 subsection {* Theory and package setup *}
210 subsubsection {* Basic lemmas *}
212 use "HOL_lemmas.ML"
213 theorems case_split = case_split_thm [case_names True False]
216 subsubsection {* Intuitionistic Reasoning *}
218 lemma impE':
219   assumes 1: "P --> Q"
220     and 2: "Q ==> R"
221     and 3: "P --> Q ==> P"
222   shows R
223 proof -
224   from 3 and 1 have P .
225   with 1 have Q by (rule impE)
226   with 2 show R .
227 qed
229 lemma allE':
230   assumes 1: "ALL x. P x"
231     and 2: "P x ==> ALL x. P x ==> Q"
232   shows Q
233 proof -
234   from 1 have "P x" by (rule spec)
235   from this and 1 show Q by (rule 2)
236 qed
238 lemma notE':
239   assumes 1: "~ P"
240     and 2: "~ P ==> P"
241   shows R
242 proof -
243   from 2 and 1 have P .
244   with 1 show R by (rule notE)
245 qed
247 lemmas [CPure.elim!] = disjE iffE FalseE conjE exE
248   and [CPure.intro!] = iffI conjI impI TrueI notI allI refl
249   and [CPure.elim 2] = allE notE' impE'
250   and [CPure.intro] = exI disjI2 disjI1
252 lemmas [trans] = trans
253   and [sym] = sym not_sym
254   and [CPure.elim?] = iffD1 iffD2 impE
257 subsubsection {* Atomizing meta-level connectives *}
259 lemma atomize_all [atomize]: "(!!x. P x) == Trueprop (ALL x. P x)"
260 proof
261   assume "!!x. P x"
262   show "ALL x. P x" by (rule allI)
263 next
264   assume "ALL x. P x"
265   thus "!!x. P x" by (rule allE)
266 qed
268 lemma atomize_imp [atomize]: "(A ==> B) == Trueprop (A --> B)"
269 proof
270   assume r: "A ==> B"
271   show "A --> B" by (rule impI) (rule r)
272 next
273   assume "A --> B" and A
274   thus B by (rule mp)
275 qed
277 lemma atomize_eq [atomize]: "(x == y) == Trueprop (x = y)"
278 proof
279   assume "x == y"
280   show "x = y" by (unfold prems) (rule refl)
281 next
282   assume "x = y"
283   thus "x == y" by (rule eq_reflection)
284 qed
286 lemma atomize_conj [atomize]:
287   "(!!C. (A ==> B ==> PROP C) ==> PROP C) == Trueprop (A & B)"
288 proof
289   assume "!!C. (A ==> B ==> PROP C) ==> PROP C"
290   show "A & B" by (rule conjI)
291 next
292   fix C
293   assume "A & B"
294   assume "A ==> B ==> PROP C"
295   thus "PROP C"
296   proof this
297     show A by (rule conjunct1)
298     show B by (rule conjunct2)
299   qed
300 qed
302 lemmas [symmetric, rulify] = atomize_all atomize_imp
305 subsubsection {* Classical Reasoner setup *}
308 setup hypsubst_setup
310 ML_setup {*
311   Context.>> (ContextRules.addSWrapper (fn tac => hyp_subst_tac' ORELSE' tac));
312 *}
314 setup Classical.setup
315 setup clasetup
317 lemmas [intro?] = ext
318   and [elim?] = ex1_implies_ex
320 use "blastdata.ML"
321 setup Blast.setup
324 subsubsection {* Simplifier setup *}
326 lemma meta_eq_to_obj_eq: "x == y ==> x = y"
327 proof -
328   assume r: "x == y"
329   show "x = y" by (unfold r) (rule refl)
330 qed
332 lemma eta_contract_eq: "(%s. f s) = f" ..
334 lemma simp_thms:
335   shows not_not: "(~ ~ P) = P"
336   and
337     "(P ~= Q) = (P = (~Q))"
338     "(P | ~P) = True"    "(~P | P) = True"
339     "((~P) = (~Q)) = (P=Q)"
340     "(x = x) = True"
341     "(~True) = False"  "(~False) = True"
342     "(~P) ~= P"  "P ~= (~P)"
343     "(True=P) = P"  "(P=True) = P"  "(False=P) = (~P)"  "(P=False) = (~P)"
344     "(True --> P) = P"  "(False --> P) = True"
345     "(P --> True) = True"  "(P --> P) = True"
346     "(P --> False) = (~P)"  "(P --> ~P) = (~P)"
347     "(P & True) = P"  "(True & P) = P"
348     "(P & False) = False"  "(False & P) = False"
349     "(P & P) = P"  "(P & (P & Q)) = (P & Q)"
350     "(P & ~P) = False"    "(~P & P) = False"
351     "(P | True) = True"  "(True | P) = True"
352     "(P | False) = P"  "(False | P) = P"
353     "(P | P) = P"  "(P | (P | Q)) = (P | Q)" and
354     "(ALL x. P) = P"  "(EX x. P) = P"  "EX x. x=t"  "EX x. t=x"
355     -- {* needed for the one-point-rule quantifier simplification procs *}
356     -- {* essential for termination!! *} and
357     "!!P. (EX x. x=t & P(x)) = P(t)"
358     "!!P. (EX x. t=x & P(x)) = P(t)"
359     "!!P. (ALL x. x=t --> P(x)) = P(t)"
360     "!!P. (ALL x. t=x --> P(x)) = P(t)"
361   by (blast, blast, blast, blast, blast, rules+)
363 lemma imp_cong: "(P = P') ==> (P' ==> (Q = Q')) ==> ((P --> Q) = (P' --> Q'))"
364   by rules
366 lemma ex_simps:
367   "!!P Q. (EX x. P x & Q)   = ((EX x. P x) & Q)"
368   "!!P Q. (EX x. P & Q x)   = (P & (EX x. Q x))"
369   "!!P Q. (EX x. P x | Q)   = ((EX x. P x) | Q)"
370   "!!P Q. (EX x. P | Q x)   = (P | (EX x. Q x))"
371   "!!P Q. (EX x. P x --> Q) = ((ALL x. P x) --> Q)"
372   "!!P Q. (EX x. P --> Q x) = (P --> (EX x. Q x))"
373   -- {* Miniscoping: pushing in existential quantifiers. *}
374   by (rules | blast)+
376 lemma all_simps:
377   "!!P Q. (ALL x. P x & Q)   = ((ALL x. P x) & Q)"
378   "!!P Q. (ALL x. P & Q x)   = (P & (ALL x. Q x))"
379   "!!P Q. (ALL x. P x | Q)   = ((ALL x. P x) | Q)"
380   "!!P Q. (ALL x. P | Q x)   = (P | (ALL x. Q x))"
381   "!!P Q. (ALL x. P x --> Q) = ((EX x. P x) --> Q)"
382   "!!P Q. (ALL x. P --> Q x) = (P --> (ALL x. Q x))"
383   -- {* Miniscoping: pushing in universal quantifiers. *}
384   by (rules | blast)+
386 lemma disj_absorb: "(A | A) = A"
387   by blast
389 lemma disj_left_absorb: "(A | (A | B)) = (A | B)"
390   by blast
392 lemma conj_absorb: "(A & A) = A"
393   by blast
395 lemma conj_left_absorb: "(A & (A & B)) = (A & B)"
396   by blast
398 lemma eq_ac:
399   shows eq_commute: "(a=b) = (b=a)"
400     and eq_left_commute: "(P=(Q=R)) = (Q=(P=R))"
401     and eq_assoc: "((P=Q)=R) = (P=(Q=R))" by (rules, blast+)
402 lemma neq_commute: "(a~=b) = (b~=a)" by rules
404 lemma conj_comms:
405   shows conj_commute: "(P&Q) = (Q&P)"
406     and conj_left_commute: "(P&(Q&R)) = (Q&(P&R))" by rules+
407 lemma conj_assoc: "((P&Q)&R) = (P&(Q&R))" by rules
409 lemma disj_comms:
410   shows disj_commute: "(P|Q) = (Q|P)"
411     and disj_left_commute: "(P|(Q|R)) = (Q|(P|R))" by rules+
412 lemma disj_assoc: "((P|Q)|R) = (P|(Q|R))" by rules
414 lemma conj_disj_distribL: "(P&(Q|R)) = (P&Q | P&R)" by rules
415 lemma conj_disj_distribR: "((P|Q)&R) = (P&R | Q&R)" by rules
417 lemma disj_conj_distribL: "(P|(Q&R)) = ((P|Q) & (P|R))" by rules
418 lemma disj_conj_distribR: "((P&Q)|R) = ((P|R) & (Q|R))" by rules
420 lemma imp_conjR: "(P --> (Q&R)) = ((P-->Q) & (P-->R))" by rules
421 lemma imp_conjL: "((P&Q) -->R)  = (P --> (Q --> R))" by rules
422 lemma imp_disjL: "((P|Q) --> R) = ((P-->R)&(Q-->R))" by rules
424 text {* These two are specialized, but @{text imp_disj_not1} is useful in @{text "Auth/Yahalom"}. *}
425 lemma imp_disj_not1: "(P --> Q | R) = (~Q --> P --> R)" by blast
426 lemma imp_disj_not2: "(P --> Q | R) = (~R --> P --> Q)" by blast
428 lemma imp_disj1: "((P-->Q)|R) = (P--> Q|R)" by blast
429 lemma imp_disj2: "(Q|(P-->R)) = (P--> Q|R)" by blast
431 lemma de_Morgan_disj: "(~(P | Q)) = (~P & ~Q)" by rules
432 lemma de_Morgan_conj: "(~(P & Q)) = (~P | ~Q)" by blast
433 lemma not_imp: "(~(P --> Q)) = (P & ~Q)" by blast
434 lemma not_iff: "(P~=Q) = (P = (~Q))" by blast
435 lemma disj_not1: "(~P | Q) = (P --> Q)" by blast
436 lemma disj_not2: "(P | ~Q) = (Q --> P)"  -- {* changes orientation :-( *}
437   by blast
438 lemma imp_conv_disj: "(P --> Q) = ((~P) | Q)" by blast
440 lemma iff_conv_conj_imp: "(P = Q) = ((P --> Q) & (Q --> P))" by rules
443 lemma cases_simp: "((P --> Q) & (~P --> Q)) = Q"
444   -- {* Avoids duplication of subgoals after @{text split_if}, when the true and false *}
445   -- {* cases boil down to the same thing. *}
446   by blast
448 lemma not_all: "(~ (! x. P(x))) = (? x.~P(x))" by blast
449 lemma imp_all: "((! x. P x) --> Q) = (? x. P x --> Q)" by blast
450 lemma not_ex: "(~ (? x. P(x))) = (! x.~P(x))" by rules
451 lemma imp_ex: "((? x. P x) --> Q) = (! x. P x --> Q)" by rules
453 lemma ex_disj_distrib: "(? x. P(x) | Q(x)) = ((? x. P(x)) | (? x. Q(x)))" by rules
454 lemma all_conj_distrib: "(!x. P(x) & Q(x)) = ((! x. P(x)) & (! x. Q(x)))" by rules
456 text {*
457   \medskip The @{text "&"} congruence rule: not included by default!
458   May slow rewrite proofs down by as much as 50\% *}
460 lemma conj_cong:
461     "(P = P') ==> (P' ==> (Q = Q')) ==> ((P & Q) = (P' & Q'))"
462   by rules
464 lemma rev_conj_cong:
465     "(Q = Q') ==> (Q' ==> (P = P')) ==> ((P & Q) = (P' & Q'))"
466   by rules
468 text {* The @{text "|"} congruence rule: not included by default! *}
470 lemma disj_cong:
471     "(P = P') ==> (~P' ==> (Q = Q')) ==> ((P | Q) = (P' | Q'))"
472   by blast
474 lemma eq_sym_conv: "(x = y) = (y = x)"
475   by rules
478 text {* \medskip if-then-else rules *}
480 lemma if_True: "(if True then x else y) = x"
481   by (unfold if_def) blast
483 lemma if_False: "(if False then x else y) = y"
484   by (unfold if_def) blast
486 lemma if_P: "P ==> (if P then x else y) = x"
487   by (unfold if_def) blast
489 lemma if_not_P: "~P ==> (if P then x else y) = y"
490   by (unfold if_def) blast
492 lemma split_if: "P (if Q then x else y) = ((Q --> P(x)) & (~Q --> P(y)))"
493   apply (rule case_split [of Q])
494    apply (subst if_P)
495     prefer 3 apply (subst if_not_P, blast+)
496   done
498 lemma split_if_asm: "P (if Q then x else y) = (~((Q & ~P x) | (~Q & ~P y)))"
499 by (subst split_if, blast)
501 lemmas if_splits = split_if split_if_asm
503 lemma if_def2: "(if Q then x else y) = ((Q --> x) & (~ Q --> y))"
504   by (rule split_if)
506 lemma if_cancel: "(if c then x else x) = x"
507 by (subst split_if, blast)
509 lemma if_eq_cancel: "(if x = y then y else x) = x"
510 by (subst split_if, blast)
512 lemma if_bool_eq_conj: "(if P then Q else R) = ((P-->Q) & (~P-->R))"
513   -- {* This form is useful for expanding @{text if}s on the RIGHT of the @{text "==>"} symbol. *}
514   by (rule split_if)
516 lemma if_bool_eq_disj: "(if P then Q else R) = ((P&Q) | (~P&R))"
517   -- {* And this form is useful for expanding @{text if}s on the LEFT. *}
518   apply (subst split_if, blast)
519   done
521 lemma Eq_TrueI: "P ==> P == True" by (unfold atomize_eq) rules
522 lemma Eq_FalseI: "~P ==> P == False" by (unfold atomize_eq) rules
524 subsubsection {* Actual Installation of the Simplifier *}
526 use "simpdata.ML"
527 setup Simplifier.setup
528 setup "Simplifier.method_setup Splitter.split_modifiers" setup simpsetup
529 setup Splitter.setup setup Clasimp.setup
531 declare disj_absorb [simp] conj_absorb [simp]
533 lemma ex1_eq[iff]: "EX! x. x = t" "EX! x. t = x"
534 by blast+
536 theorem choice_eq: "(ALL x. EX! y. P x y) = (EX! f. ALL x. P x (f x))"
537   apply (rule iffI)
538   apply (rule_tac a = "%x. THE y. P x y" in ex1I)
539   apply (fast dest!: theI')
540   apply (fast intro: ext the1_equality [symmetric])
541   apply (erule ex1E)
542   apply (rule allI)
543   apply (rule ex1I)
544   apply (erule spec)
545   apply (erule_tac x = "%z. if z = x then y else f z" in allE)
546   apply (erule impE)
547   apply (rule allI)
548   apply (rule_tac P = "xa = x" in case_split_thm)
549   apply (drule_tac  x = x in fun_cong, simp_all)
550   done
552 text{*Needs only HOL-lemmas:*}
553 lemma mk_left_commute:
554   assumes a: "\<And>x y z. f (f x y) z = f x (f y z)" and
555           c: "\<And>x y. f x y = f y x"
556   shows "f x (f y z) = f y (f x z)"
557 by(rule trans[OF trans[OF c a] arg_cong[OF c, of "f y"]])
560 subsubsection {* Generic cases and induction *}
562 constdefs
563   induct_forall :: "('a => bool) => bool"
564   "induct_forall P == \<forall>x. P x"
565   induct_implies :: "bool => bool => bool"
566   "induct_implies A B == A --> B"
567   induct_equal :: "'a => 'a => bool"
568   "induct_equal x y == x = y"
569   induct_conj :: "bool => bool => bool"
570   "induct_conj A B == A & B"
572 lemma induct_forall_eq: "(!!x. P x) == Trueprop (induct_forall (\<lambda>x. P x))"
573   by (simp only: atomize_all induct_forall_def)
575 lemma induct_implies_eq: "(A ==> B) == Trueprop (induct_implies A B)"
576   by (simp only: atomize_imp induct_implies_def)
578 lemma induct_equal_eq: "(x == y) == Trueprop (induct_equal x y)"
579   by (simp only: atomize_eq induct_equal_def)
581 lemma induct_forall_conj: "induct_forall (\<lambda>x. induct_conj (A x) (B x)) =
582     induct_conj (induct_forall A) (induct_forall B)"
583   by (unfold induct_forall_def induct_conj_def) rules
585 lemma induct_implies_conj: "induct_implies C (induct_conj A B) =
586     induct_conj (induct_implies C A) (induct_implies C B)"
587   by (unfold induct_implies_def induct_conj_def) rules
589 lemma induct_conj_curry: "(induct_conj A B ==> PROP C) == (A ==> B ==> PROP C)"
590 proof
591   assume r: "induct_conj A B ==> PROP C" and A B
592   show "PROP C" by (rule r) (simp! add: induct_conj_def)
593 next
594   assume r: "A ==> B ==> PROP C" and "induct_conj A B"
595   show "PROP C" by (rule r) (simp! add: induct_conj_def)+
596 qed
598 lemma induct_impliesI: "(A ==> B) ==> induct_implies A B"
599   by (simp add: induct_implies_def)
601 lemmas induct_atomize = atomize_conj induct_forall_eq induct_implies_eq induct_equal_eq
602 lemmas induct_rulify1 [symmetric, standard] = induct_forall_eq induct_implies_eq induct_equal_eq
603 lemmas induct_rulify2 = induct_forall_def induct_implies_def induct_equal_def induct_conj_def
604 lemmas induct_conj = induct_forall_conj induct_implies_conj induct_conj_curry
606 hide const induct_forall induct_implies induct_equal induct_conj
609 text {* Method setup. *}
611 ML {*
612   structure InductMethod = InductMethodFun
613   (struct
614     val dest_concls = HOLogic.dest_concls;
615     val cases_default = thm "case_split";
616     val local_impI = thm "induct_impliesI";
617     val conjI = thm "conjI";
618     val atomize = thms "induct_atomize";
619     val rulify1 = thms "induct_rulify1";
620     val rulify2 = thms "induct_rulify2";
621     val localize = [Thm.symmetric (thm "induct_implies_def")];
622   end);
623 *}
625 setup InductMethod.setup
628 subsection {* Order signatures and orders *}
630 axclass
631   ord < type
633 syntax
634   "op <"        :: "['a::ord, 'a] => bool"             ("op <")
635   "op <="       :: "['a::ord, 'a] => bool"             ("op <=")
637 global
639 consts
640   "op <"        :: "['a::ord, 'a] => bool"             ("(_/ < _)"  [50, 51] 50)
641   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ <= _)" [50, 51] 50)
643 local
645 syntax (xsymbols)
646   "op <="       :: "['a::ord, 'a] => bool"             ("op \<le>")
647   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
649 syntax (HTML output)
650   "op <="       :: "['a::ord, 'a] => bool"             ("op \<le>")
651   "op <="       :: "['a::ord, 'a] => bool"             ("(_/ \<le> _)"  [50, 51] 50)
654 lemma Not_eq_iff: "((~P) = (~Q)) = (P = Q)"
655 by blast
657 subsubsection {* Monotonicity *}
659 locale mono =
660   fixes f
661   assumes mono: "A <= B ==> f A <= f B"
663 lemmas monoI [intro?] = mono.intro
664   and monoD [dest?] = mono.mono
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)"
672 lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
673   by (simp add: min_def)
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)
679 lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
680   by (simp add: max_def)
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)
687 subsubsection "Orders"
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)"
696 text {* Reflexivity. *}
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
704 lemma order_less_irrefl [iff]: "~ x < (x::'a::order)"
705   by (simp add: order_less_le)
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, blast)
710   done
712 lemmas order_le_imp_less_or_eq = order_le_less [THEN iffD1, standard]
714 lemma order_less_imp_le: "!!x::'a::order. x < y ==> x <= y"
715   by (simp add: order_less_le)
718 text {* Asymmetry. *}
720 lemma order_less_not_sym: "(x::'a::order) < y ==> ~ (y < x)"
721   by (simp add: order_less_le order_antisym)
723 lemma order_less_asym: "x < (y::'a::order) ==> (~P ==> y < x) ==> P"
724   apply (drule order_less_not_sym)
725   apply (erule contrapos_np, simp)
726   done
728 lemma order_eq_iff: "!!x::'a::order. (x = y) = (x \<le> y & y \<le> x)"
729 by (blast intro: order_antisym)
732 text {* Transitivity. *}
734 lemma order_less_trans: "!!x::'a::order. [| x < y; y < z |] ==> x < z"
735   apply (simp add: order_less_le)
736   apply (blast intro: order_trans order_antisym)
737   done
739 lemma order_le_less_trans: "!!x::'a::order. [| x <= y; y < z |] ==> x < z"
740   apply (simp add: order_less_le)
741   apply (blast intro: order_trans order_antisym)
742   done
744 lemma order_less_le_trans: "!!x::'a::order. [| x < y; y <= z |] ==> x < z"
745   apply (simp add: order_less_le)
746   apply (blast intro: order_trans order_antisym)
747   done
750 text {* Useful for simplification, but too risky to include by default. *}
752 lemma order_less_imp_not_less: "(x::'a::order) < y ==>  (~ y < x) = True"
753   by (blast elim: order_less_asym)
755 lemma order_less_imp_triv: "(x::'a::order) < y ==>  (y < x --> P) = True"
756   by (blast elim: order_less_asym)
758 lemma order_less_imp_not_eq: "(x::'a::order) < y ==>  (x = y) = False"
759   by auto
761 lemma order_less_imp_not_eq2: "(x::'a::order) < y ==>  (y = x) = False"
762   by auto
765 text {* Other operators. *}
767 lemma min_leastR: "(!!x::'a::order. least <= x) ==> min x least = least"
768   apply (simp add: min_def)
769   apply (blast intro: order_antisym)
770   done
772 lemma max_leastR: "(!!x::'a::order. least <= x) ==> max x least = x"
773   apply (simp add: max_def)
774   apply (blast intro: order_antisym)
775   done
778 subsubsection {* Least value operator *}
780 constdefs
781   Least :: "('a::ord => bool) => 'a"               (binder "LEAST " 10)
782   "Least P == THE x. P x & (ALL y. P y --> x <= y)"
783     -- {* We can no longer use LeastM because the latter requires Hilbert-AC. *}
785 lemma LeastI2:
786   "[| P (x::'a::order);
787       !!y. P y ==> x <= y;
788       !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
789    ==> Q (Least P)"
790   apply (unfold Least_def)
791   apply (rule theI2)
792     apply (blast intro: order_antisym)+
793   done
795 lemma Least_equality:
796     "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
797   apply (simp add: Least_def)
798   apply (rule the_equality)
799   apply (auto intro!: order_antisym)
800   done
803 subsubsection "Linear / total orders"
805 axclass linorder < order
806   linorder_linear: "x <= y | y <= x"
808 lemma linorder_less_linear: "!!x::'a::linorder. x<y | x=y | y<x"
809   apply (simp add: order_less_le)
810   apply (insert linorder_linear, blast)
811   done
813 lemma linorder_le_cases [case_names le ge]:
814     "((x::'a::linorder) \<le> y ==> P) ==> (y \<le> x ==> P) ==> P"
815   by (insert linorder_linear, blast)
817 lemma linorder_cases [case_names less equal greater]:
818     "((x::'a::linorder) < y ==> P) ==> (x = y ==> P) ==> (y < x ==> P) ==> P"
819   by (insert linorder_less_linear, blast)
821 lemma linorder_not_less: "!!x::'a::linorder. (~ x < y) = (y <= x)"
822   apply (simp add: order_less_le)
823   apply (insert linorder_linear)
824   apply (blast intro: order_antisym)
825   done
827 lemma linorder_not_le: "!!x::'a::linorder. (~ x <= y) = (y < x)"
828   apply (simp add: order_less_le)
829   apply (insert linorder_linear)
830   apply (blast intro: order_antisym)
831   done
833 lemma linorder_neq_iff: "!!x::'a::linorder. (x ~= y) = (x<y | y<x)"
834 by (cut_tac x = x and y = y in linorder_less_linear, auto)
836 lemma linorder_neqE: "x ~= (y::'a::linorder) ==> (x < y ==> R) ==> (y < x ==> R) ==> R"
837 by (simp add: linorder_neq_iff, blast)
840 subsubsection "Min and max on (linear) orders"
842 lemma min_same [simp]: "min (x::'a::order) x = x"
843   by (simp add: min_def)
845 lemma max_same [simp]: "max (x::'a::order) x = x"
846   by (simp add: max_def)
848 lemma le_max_iff_disj: "!!z::'a::linorder. (z <= max x y) = (z <= x | z <= y)"
849   apply (simp add: max_def)
850   apply (insert linorder_linear)
851   apply (blast intro: order_trans)
852   done
854 lemma le_maxI1: "(x::'a::linorder) <= max x y"
855   by (simp add: le_max_iff_disj)
857 lemma le_maxI2: "(y::'a::linorder) <= max x y"
858     -- {* CANNOT use with @{text "[intro!]"} because blast will give PROOF FAILED. *}
859   by (simp add: le_max_iff_disj)
861 lemma less_max_iff_disj: "!!z::'a::linorder. (z < max x y) = (z < x | z < y)"
862   apply (simp add: max_def order_le_less)
863   apply (insert linorder_less_linear)
864   apply (blast intro: order_less_trans)
865   done
867 lemma max_le_iff_conj [simp]:
868     "!!z::'a::linorder. (max x y <= z) = (x <= z & y <= z)"
869   apply (simp add: max_def)
870   apply (insert linorder_linear)
871   apply (blast intro: order_trans)
872   done
874 lemma max_less_iff_conj [simp]:
875     "!!z::'a::linorder. (max x y < z) = (x < z & y < z)"
876   apply (simp add: order_le_less max_def)
877   apply (insert linorder_less_linear)
878   apply (blast intro: order_less_trans)
879   done
881 lemma le_min_iff_conj [simp]:
882     "!!z::'a::linorder. (z <= min x y) = (z <= x & z <= y)"
883     -- {* @{text "[iff]"} screws up a @{text blast} in MiniML *}
884   apply (simp add: min_def)
885   apply (insert linorder_linear)
886   apply (blast intro: order_trans)
887   done
889 lemma min_less_iff_conj [simp]:
890     "!!z::'a::linorder. (z < min x y) = (z < x & z < y)"
891   apply (simp add: order_le_less min_def)
892   apply (insert linorder_less_linear)
893   apply (blast intro: order_less_trans)
894   done
896 lemma min_le_iff_disj: "!!z::'a::linorder. (min x y <= z) = (x <= z | y <= z)"
897   apply (simp add: min_def)
898   apply (insert linorder_linear)
899   apply (blast intro: order_trans)
900   done
902 lemma min_less_iff_disj: "!!z::'a::linorder. (min x y < z) = (x < z | y < z)"
903   apply (simp add: min_def order_le_less)
904   apply (insert linorder_less_linear)
905   apply (blast intro: order_less_trans)
906   done
908 lemma max_assoc: "!!x::'a::linorder. max (max x y) z = max x (max y z)"
910 apply(rule conjI)
911 apply(blast intro:order_trans)
913 apply(blast dest: order_less_trans order_le_less_trans)
914 done
916 lemma max_commute: "!!x::'a::linorder. max x y = max y x"
918 apply(rule conjI)
919 apply(blast intro:order_antisym)
921 apply(blast dest: order_less_trans)
922 done
924 lemmas max_ac = max_assoc max_commute
925                 mk_left_commute[of max,OF max_assoc max_commute]
927 lemma min_assoc: "!!x::'a::linorder. min (min x y) z = min x (min y z)"
929 apply(rule conjI)
930 apply(blast intro:order_trans)
932 apply(blast dest: order_less_trans order_le_less_trans)
933 done
935 lemma min_commute: "!!x::'a::linorder. min x y = min y x"
937 apply(rule conjI)
938 apply(blast intro:order_antisym)
940 apply(blast dest: order_less_trans)
941 done
943 lemmas min_ac = min_assoc min_commute
944                 mk_left_commute[of min,OF min_assoc min_commute]
946 lemma split_min:
947     "P (min (i::'a::linorder) j) = ((i <= j --> P(i)) & (~ i <= j --> P(j)))"
948   by (simp add: min_def)
950 lemma split_max:
951     "P (max (i::'a::linorder) j) = ((i <= j --> P(j)) & (~ i <= j --> P(i)))"
952   by (simp add: max_def)
955 subsubsection {* Transitivity rules for calculational reasoning *}
958 lemma order_neq_le_trans: "a ~= b ==> (a::'a::order) <= b ==> a < b"
959   by (simp add: order_less_le)
961 lemma order_le_neq_trans: "(a::'a::order) <= b ==> a ~= b ==> a < b"
962   by (simp add: order_less_le)
964 lemma order_less_asym': "(a::'a::order) < b ==> b < a ==> P"
965   by (rule order_less_asym)
968 subsubsection {* Setup of transitivity reasoner as Solver *}
970 lemma less_imp_neq: "[| (x::'a::order) < y |] ==> x ~= y"
971   by (erule contrapos_pn, erule subst, rule order_less_irrefl)
973 lemma eq_neq_eq_imp_neq: "[| x = a ; a ~= b; b = y |] ==> x ~= y"
974   by (erule subst, erule ssubst, assumption)
976 ML_setup {*
978 structure Trans_Tac = Trans_Tac_Fun (
979   struct
980     val less_reflE = thm "order_less_irrefl" RS thm "notE";
981     val le_refl = thm "order_refl";
982     val less_imp_le = thm "order_less_imp_le";
983     val not_lessI = thm "linorder_not_less" RS thm "iffD2";
984     val not_leI = thm "linorder_not_le" RS thm "iffD2";
985     val not_lessD = thm "linorder_not_less" RS thm "iffD1";
986     val not_leD = thm "linorder_not_le" RS thm "iffD1";
987     val eqI = thm "order_antisym";
988     val eqD1 = thm "order_eq_refl";
989     val eqD2 = thm "sym" RS thm "order_eq_refl";
990     val less_trans = thm "order_less_trans";
991     val less_le_trans = thm "order_less_le_trans";
992     val le_less_trans = thm "order_le_less_trans";
993     val le_trans = thm "order_trans";
994     val le_neq_trans = thm "order_le_neq_trans";
995     val neq_le_trans = thm "order_neq_le_trans";
996     val less_imp_neq = thm "less_imp_neq";
997     val eq_neq_eq_imp_neq = thm "eq_neq_eq_imp_neq";
999     fun decomp_gen sort sign (Trueprop \$ t) =
1000       let fun of_sort t = Sign.of_sort sign (type_of t, sort)
1001       fun dec (Const ("Not", _) \$ t) = (
1002               case dec t of
1003                 None => None
1004               | Some (t1, rel, t2) => Some (t1, "~" ^ rel, t2))
1005             | dec (Const ("op =",  _) \$ t1 \$ t2) =
1006                 if of_sort t1
1007                 then Some (t1, "=", t2)
1008                 else None
1009             | dec (Const ("op <=",  _) \$ t1 \$ t2) =
1010                 if of_sort t1
1011                 then Some (t1, "<=", t2)
1012                 else None
1013             | dec (Const ("op <",  _) \$ t1 \$ t2) =
1014                 if of_sort t1
1015                 then Some (t1, "<", t2)
1016                 else None
1017             | dec _ = None
1018       in dec t end;
1020     val decomp_part = decomp_gen ["HOL.order"];
1021     val decomp_lin = decomp_gen ["HOL.linorder"];
1023   end);  (* struct *)
1025 simpset_ref() := simpset ()
1026     addSolver (mk_solver "Trans_linear" (fn _ => Trans_Tac.linear_tac))
1027     addSolver (mk_solver "Trans_partial" (fn _ => Trans_Tac.partial_tac));
1028   (* Adding the transitivity reasoners also as safe solvers showed a slight
1029      speed up, but the reasoning strength appears to be not higher (at least
1030      no breaking of additional proofs in the entire HOL distribution, as
1031      of 5 March 2004, was observed). *)
1032 *}
1034 (* Optional methods
1036 method_setup trans_partial =
1037   {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (trans_tac_partial)) *}
1038   {* simple transitivity reasoner *}
1039 method_setup trans_linear =
1040   {* Method.no_args (Method.SIMPLE_METHOD' HEADGOAL (trans_tac_linear)) *}
1041   {* simple transitivity reasoner *}
1042 *)
1044 (*
1045 declare order.order_refl [simp del] order_less_irrefl [simp del]
1046 *)
1048 subsubsection "Bounded quantifiers"
1050 syntax
1051   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3ALL _<_./ _)"  [0, 0, 10] 10)
1052   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3EX _<_./ _)"  [0, 0, 10] 10)
1053   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3ALL _<=_./ _)" [0, 0, 10] 10)
1054   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3EX _<=_./ _)" [0, 0, 10] 10)
1056 syntax (xsymbols)
1057   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
1058   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
1059   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
1060   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
1062 syntax (HOL)
1063   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3! _<_./ _)"  [0, 0, 10] 10)
1064   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3? _<_./ _)"  [0, 0, 10] 10)
1065   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3! _<=_./ _)" [0, 0, 10] 10)
1066   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3? _<=_./ _)" [0, 0, 10] 10)
1068 syntax (HTML output)
1069   "_lessAll" :: "[idt, 'a, bool] => bool"   ("(3\<forall>_<_./ _)"  [0, 0, 10] 10)
1070   "_lessEx"  :: "[idt, 'a, bool] => bool"   ("(3\<exists>_<_./ _)"  [0, 0, 10] 10)
1071   "_leAll"   :: "[idt, 'a, bool] => bool"   ("(3\<forall>_\<le>_./ _)" [0, 0, 10] 10)
1072   "_leEx"    :: "[idt, 'a, bool] => bool"   ("(3\<exists>_\<le>_./ _)" [0, 0, 10] 10)
1074 translations
1075  "ALL x<y. P"   =>  "ALL x. x < y --> P"
1076  "EX x<y. P"    =>  "EX x. x < y  & P"
1077  "ALL x<=y. P"  =>  "ALL x. x <= y --> P"
1078  "EX x<=y. P"   =>  "EX x. x <= y & P"
1080 print_translation {*
1081 let
1082   fun all_tr' [Const ("_bound",_) \$ Free (v,_),
1083                Const("op -->",_) \$ (Const ("op <",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1084   (if v=v' then Syntax.const "_lessAll" \$ Syntax.mark_bound v' \$ n \$ P else raise Match)
1086   | all_tr' [Const ("_bound",_) \$ Free (v,_),
1087                Const("op -->",_) \$ (Const ("op <=",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1088   (if v=v' then Syntax.const "_leAll" \$ Syntax.mark_bound v' \$ n \$ P else raise Match);
1090   fun ex_tr' [Const ("_bound",_) \$ Free (v,_),
1091                Const("op &",_) \$ (Const ("op <",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1092   (if v=v' then Syntax.const "_lessEx" \$ Syntax.mark_bound v' \$ n \$ P else raise Match)
1094   | ex_tr' [Const ("_bound",_) \$ Free (v,_),
1095                Const("op &",_) \$ (Const ("op <=",_) \$ (Const ("_bound",_) \$ Free (v',_)) \$ n ) \$ P] =
1096   (if v=v' then Syntax.const "_leEx" \$ Syntax.mark_bound v' \$ n \$ P else raise Match)
1097 in
1098 [("ALL ", all_tr'), ("EX ", ex_tr')]
1099 end
1100 *}
1102 end