renamed former datatype.ML to datatype_data.ML; datatype.ML provides uniform view on datatype.ML and datatype_rep_proofs.ML
1 (* Title: HOL/Lattices.thy
5 header {* Abstract lattices *}
11 subsection {* Lattices *}
14 less_eq (infix "\<sqsubseteq>" 50) and
15 less (infix "\<sqsubset>" 50) and
19 class lower_semilattice = order +
20 fixes inf :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 70)
21 assumes inf_le1 [simp]: "x \<sqinter> y \<sqsubseteq> x"
22 and inf_le2 [simp]: "x \<sqinter> y \<sqsubseteq> y"
23 and inf_greatest: "x \<sqsubseteq> y \<Longrightarrow> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<sqinter> z"
25 class upper_semilattice = order +
26 fixes sup :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65)
27 assumes sup_ge1 [simp]: "x \<sqsubseteq> x \<squnion> y"
28 and sup_ge2 [simp]: "y \<sqsubseteq> x \<squnion> y"
29 and sup_least: "y \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> x \<Longrightarrow> y \<squnion> z \<sqsubseteq> x"
32 text {* Dual lattice *}
34 lemma dual_semilattice:
35 "lower_semilattice (op \<ge>) (op >) sup"
36 by (rule lower_semilattice.intro, rule dual_order)
37 (unfold_locales, simp_all add: sup_least)
41 class lattice = lower_semilattice + upper_semilattice
44 subsubsection {* Intro and elim rules*}
46 context lower_semilattice
50 "a \<sqsubseteq> x \<Longrightarrow> a \<sqinter> b \<sqsubseteq> x"
51 by (rule order_trans) auto
54 "b \<sqsubseteq> x \<Longrightarrow> a \<sqinter> b \<sqsubseteq> x"
55 by (rule order_trans) auto
57 lemma le_infI: "x \<sqsubseteq> a \<Longrightarrow> x \<sqsubseteq> b \<Longrightarrow> x \<sqsubseteq> a \<sqinter> b"
58 by (blast intro: inf_greatest)
60 lemma le_infE: "x \<sqsubseteq> a \<sqinter> b \<Longrightarrow> (x \<sqsubseteq> a \<Longrightarrow> x \<sqsubseteq> b \<Longrightarrow> P) \<Longrightarrow> P"
61 by (blast intro: order_trans le_infI1 le_infI2)
63 lemma le_inf_iff [simp]:
64 "x \<sqsubseteq> y \<sqinter> z \<longleftrightarrow> x \<sqsubseteq> y \<and> x \<sqsubseteq> z"
65 by (blast intro: le_infI elim: le_infE)
68 "x \<sqsubseteq> y \<longleftrightarrow> x \<sqinter> y = x"
69 by (auto intro: le_infI1 antisym dest: eq_iff [THEN iffD1])
72 fixes f :: "'a \<Rightarrow> 'b\<Colon>lower_semilattice"
73 shows "mono f \<Longrightarrow> f (A \<sqinter> B) \<le> f A \<sqinter> f B"
74 by (auto simp add: mono_def intro: Lattices.inf_greatest)
78 context upper_semilattice
82 "x \<sqsubseteq> a \<Longrightarrow> x \<sqsubseteq> a \<squnion> b"
83 by (rule order_trans) auto
86 "x \<sqsubseteq> b \<Longrightarrow> x \<sqsubseteq> a \<squnion> b"
87 by (rule order_trans) auto
90 "a \<sqsubseteq> x \<Longrightarrow> b \<sqsubseteq> x \<Longrightarrow> a \<squnion> b \<sqsubseteq> x"
91 by (blast intro: sup_least)
94 "a \<squnion> b \<sqsubseteq> x \<Longrightarrow> (a \<sqsubseteq> x \<Longrightarrow> b \<sqsubseteq> x \<Longrightarrow> P) \<Longrightarrow> P"
95 by (blast intro: le_supI1 le_supI2 order_trans)
97 lemma le_sup_iff [simp]:
98 "x \<squnion> y \<sqsubseteq> z \<longleftrightarrow> x \<sqsubseteq> z \<and> y \<sqsubseteq> z"
99 by (blast intro: le_supI elim: le_supE)
102 "x \<sqsubseteq> y \<longleftrightarrow> x \<squnion> y = y"
103 by (auto intro: le_supI2 antisym dest: eq_iff [THEN iffD1])
106 fixes f :: "'a \<Rightarrow> 'b\<Colon>upper_semilattice"
107 shows "mono f \<Longrightarrow> f A \<squnion> f B \<le> f (A \<squnion> B)"
108 by (auto simp add: mono_def intro: Lattices.sup_least)
113 subsubsection {* Equational laws *}
115 context lower_semilattice
118 lemma inf_commute: "(x \<sqinter> y) = (y \<sqinter> x)"
119 by (rule antisym) auto
121 lemma inf_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
122 by (rule antisym) (auto intro: le_infI1 le_infI2)
124 lemma inf_idem[simp]: "x \<sqinter> x = x"
125 by (rule antisym) auto
127 lemma inf_left_idem[simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
128 by (rule antisym) (auto intro: le_infI2)
130 lemma inf_absorb1: "x \<sqsubseteq> y \<Longrightarrow> x \<sqinter> y = x"
131 by (rule antisym) auto
133 lemma inf_absorb2: "y \<sqsubseteq> x \<Longrightarrow> x \<sqinter> y = y"
134 by (rule antisym) auto
136 lemma inf_left_commute: "x \<sqinter> (y \<sqinter> z) = y \<sqinter> (x \<sqinter> z)"
137 by (rule mk_left_commute [of inf]) (fact inf_assoc inf_commute)+
139 lemmas inf_aci = inf_commute inf_assoc inf_left_commute inf_left_idem
143 context upper_semilattice
146 lemma sup_commute: "(x \<squnion> y) = (y \<squnion> x)"
147 by (rule antisym) auto
149 lemma sup_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
150 by (rule antisym) (auto intro: le_supI1 le_supI2)
152 lemma sup_idem[simp]: "x \<squnion> x = x"
153 by (rule antisym) auto
155 lemma sup_left_idem[simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
156 by (rule antisym) (auto intro: le_supI2)
158 lemma sup_absorb1: "y \<sqsubseteq> x \<Longrightarrow> x \<squnion> y = x"
159 by (rule antisym) auto
161 lemma sup_absorb2: "x \<sqsubseteq> y \<Longrightarrow> x \<squnion> y = y"
162 by (rule antisym) auto
164 lemma sup_left_commute: "x \<squnion> (y \<squnion> z) = y \<squnion> (x \<squnion> z)"
165 by (rule mk_left_commute [of sup]) (fact sup_assoc sup_commute)+
167 lemmas sup_aci = sup_commute sup_assoc sup_left_commute sup_left_idem
175 "lattice (op \<ge>) (op >) sup inf"
176 by (rule lattice.intro, rule dual_semilattice, rule upper_semilattice.intro, rule dual_order)
177 (unfold_locales, auto)
179 lemma inf_sup_absorb: "x \<sqinter> (x \<squnion> y) = x"
180 by (blast intro: antisym inf_le1 inf_greatest sup_ge1)
182 lemma sup_inf_absorb: "x \<squnion> (x \<sqinter> y) = x"
183 by (blast intro: antisym sup_ge1 sup_least inf_le1)
185 lemmas inf_sup_aci = inf_aci sup_aci
187 lemmas inf_sup_ord = inf_le1 inf_le2 sup_ge1 sup_ge2
189 text{* Towards distributivity *}
191 lemma distrib_sup_le: "x \<squnion> (y \<sqinter> z) \<sqsubseteq> (x \<squnion> y) \<sqinter> (x \<squnion> z)"
192 by (auto intro: le_infI1 le_infI2 le_supI1 le_supI2)
194 lemma distrib_inf_le: "(x \<sqinter> y) \<squnion> (x \<sqinter> z) \<sqsubseteq> x \<sqinter> (y \<squnion> z)"
195 by (auto intro: le_infI1 le_infI2 le_supI1 le_supI2)
197 text{* If you have one of them, you have them all. *}
200 assumes D: "!!x y z. x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
201 shows "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
203 have "x \<squnion> (y \<sqinter> z) = (x \<squnion> (x \<sqinter> z)) \<squnion> (y \<sqinter> z)" by(simp add:sup_inf_absorb)
204 also have "\<dots> = x \<squnion> (z \<sqinter> (x \<squnion> y))" by(simp add:D inf_commute sup_assoc del:sup_absorb1)
205 also have "\<dots> = ((x \<squnion> y) \<sqinter> x) \<squnion> ((x \<squnion> y) \<sqinter> z)"
206 by(simp add:inf_sup_absorb inf_commute)
207 also have "\<dots> = (x \<squnion> y) \<sqinter> (x \<squnion> z)" by(simp add:D)
208 finally show ?thesis .
212 assumes D: "!!x y z. x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
213 shows "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
215 have "x \<sqinter> (y \<squnion> z) = (x \<sqinter> (x \<squnion> z)) \<sqinter> (y \<squnion> z)" by(simp add:inf_sup_absorb)
216 also have "\<dots> = x \<sqinter> (z \<squnion> (x \<sqinter> y))" by(simp add:D sup_commute inf_assoc del:inf_absorb1)
217 also have "\<dots> = ((x \<sqinter> y) \<squnion> x) \<sqinter> ((x \<sqinter> y) \<squnion> z)"
218 by(simp add:sup_inf_absorb sup_commute)
219 also have "\<dots> = (x \<sqinter> y) \<squnion> (x \<sqinter> z)" by(simp add:D)
220 finally show ?thesis .
225 subsubsection {* Strict order *}
227 context lower_semilattice
231 "a \<sqsubset> x \<Longrightarrow> a \<sqinter> b \<sqsubset> x"
232 by (auto simp add: less_le inf_absorb1 intro: le_infI1)
235 "b \<sqsubset> x \<Longrightarrow> a \<sqinter> b \<sqsubset> x"
236 by (auto simp add: less_le inf_absorb2 intro: le_infI2)
240 context upper_semilattice
244 "x < a \<Longrightarrow> x < a \<squnion> b"
246 interpret dual: lower_semilattice "op \<ge>" "op >" sup
247 by (fact dual_semilattice)
249 then show "x < a \<squnion> b"
250 by (fact dual.less_infI1)
254 "x < b \<Longrightarrow> x < a \<squnion> b"
256 interpret dual: lower_semilattice "op \<ge>" "op >" sup
257 by (fact dual_semilattice)
259 then show "x < a \<squnion> b"
260 by (fact dual.less_infI2)
266 subsection {* Distributive lattices *}
268 class distrib_lattice = lattice +
269 assumes sup_inf_distrib1: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
271 context distrib_lattice
274 lemma sup_inf_distrib2:
275 "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
276 by(simp add: inf_sup_aci sup_inf_distrib1)
278 lemma inf_sup_distrib1:
279 "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
280 by(rule distrib_imp2[OF sup_inf_distrib1])
282 lemma inf_sup_distrib2:
283 "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)"
284 by(simp add: inf_sup_aci inf_sup_distrib1)
286 lemma dual_distrib_lattice:
287 "distrib_lattice (op \<ge>) (op >) sup inf"
288 by (rule distrib_lattice.intro, rule dual_lattice)
289 (unfold_locales, fact inf_sup_distrib1)
292 sup_inf_distrib1 sup_inf_distrib2 inf_sup_distrib1 inf_sup_distrib2
297 subsection {* Boolean algebras *}
299 class boolean_algebra = distrib_lattice + top + bot + minus + uminus +
300 assumes inf_compl_bot: "x \<sqinter> - x = bot"
301 and sup_compl_top: "x \<squnion> - x = top"
302 assumes diff_eq: "x - y = x \<sqinter> - y"
305 lemma dual_boolean_algebra:
306 "boolean_algebra (\<lambda>x y. x \<squnion> - y) uminus (op \<ge>) (op >) (op \<squnion>) (op \<sqinter>) top bot"
307 by (rule boolean_algebra.intro, rule dual_distrib_lattice)
309 auto simp add: inf_compl_bot sup_compl_top diff_eq less_le_not_le)
312 "- x \<sqinter> x = bot"
313 by (simp add: inf_commute inf_compl_bot)
316 "- x \<squnion> x = top"
317 by (simp add: sup_commute sup_compl_top)
319 lemma inf_bot_left [simp]:
320 "bot \<sqinter> x = bot"
321 by (rule inf_absorb1) simp
323 lemma inf_bot_right [simp]:
324 "x \<sqinter> bot = bot"
325 by (rule inf_absorb2) simp
327 lemma sup_top_left [simp]:
328 "top \<squnion> x = top"
329 by (rule sup_absorb1) simp
331 lemma sup_top_right [simp]:
332 "x \<squnion> top = top"
333 by (rule sup_absorb2) simp
335 lemma inf_top_left [simp]:
336 "top \<sqinter> x = x"
337 by (rule inf_absorb2) simp
339 lemma inf_top_right [simp]:
340 "x \<sqinter> top = x"
341 by (rule inf_absorb1) simp
343 lemma sup_bot_left [simp]:
344 "bot \<squnion> x = x"
345 by (rule sup_absorb2) simp
347 lemma sup_bot_right [simp]:
348 "x \<squnion> bot = x"
349 by (rule sup_absorb1) simp
351 lemma inf_eq_top_eq1:
352 assumes "A \<sqinter> B = \<top>"
354 proof (cases "B = \<top>")
355 case True with assms show ?thesis by simp
357 case False with top_greatest have "B < \<top>" by (auto intro: neq_le_trans)
358 then have "A \<sqinter> B < \<top>" by (rule less_infI2)
359 with assms show ?thesis by simp
362 lemma inf_eq_top_eq2:
363 assumes "A \<sqinter> B = \<top>"
365 by (rule inf_eq_top_eq1, unfold inf_commute [of B]) (fact assms)
367 lemma sup_eq_bot_eq1:
368 assumes "A \<squnion> B = \<bottom>"
369 shows "A = \<bottom>"
371 interpret dual: boolean_algebra "\<lambda>x y. x \<squnion> - y" uminus "op \<ge>" "op >" "op \<squnion>" "op \<sqinter>" top bot
372 by (rule dual_boolean_algebra)
373 from dual.inf_eq_top_eq1 assms show ?thesis .
376 lemma sup_eq_bot_eq2:
377 assumes "A \<squnion> B = \<bottom>"
378 shows "B = \<bottom>"
380 interpret dual: boolean_algebra "\<lambda>x y. x \<squnion> - y" uminus "op \<ge>" "op >" "op \<squnion>" "op \<sqinter>" top bot
381 by (rule dual_boolean_algebra)
382 from dual.inf_eq_top_eq2 assms show ?thesis .
386 assumes "x \<sqinter> y = bot"
387 and "x \<squnion> y = top"
390 have "(x \<sqinter> - x) \<squnion> (- x \<sqinter> y) = (x \<sqinter> y) \<squnion> (- x \<sqinter> y)"
391 using inf_compl_bot assms(1) by simp
392 then have "(- x \<sqinter> x) \<squnion> (- x \<sqinter> y) = (y \<sqinter> x) \<squnion> (y \<sqinter> - x)"
393 by (simp add: inf_commute)
394 then have "- x \<sqinter> (x \<squnion> y) = y \<sqinter> (x \<squnion> - x)"
395 by (simp add: inf_sup_distrib1)
396 then have "- x \<sqinter> top = y \<sqinter> top"
397 using sup_compl_top assms(2) by simp
398 then show "- x = y" by (simp add: inf_top_right)
401 lemma double_compl [simp]:
403 using compl_inf_bot compl_sup_top by (rule compl_unique)
405 lemma compl_eq_compl_iff [simp]:
406 "- x = - y \<longleftrightarrow> x = y"
409 then have "- x \<sqinter> y = bot"
410 and "- x \<squnion> y = top"
411 by (simp_all add: compl_inf_bot compl_sup_top)
412 then have "- (- x) = y" by (rule compl_unique)
413 then show "x = y" by simp
416 then show "- x = - y" by simp
419 lemma compl_bot_eq [simp]:
422 from sup_compl_top have "bot \<squnion> - bot = top" .
423 then show ?thesis by simp
426 lemma compl_top_eq [simp]:
429 from inf_compl_bot have "top \<sqinter> - top = bot" .
430 then show ?thesis by simp
433 lemma compl_inf [simp]:
434 "- (x \<sqinter> y) = - x \<squnion> - y"
435 proof (rule compl_unique)
436 have "(x \<sqinter> y) \<sqinter> (- x \<squnion> - y) = ((x \<sqinter> y) \<sqinter> - x) \<squnion> ((x \<sqinter> y) \<sqinter> - y)"
437 by (rule inf_sup_distrib1)
438 also have "... = (y \<sqinter> (x \<sqinter> - x)) \<squnion> (x \<sqinter> (y \<sqinter> - y))"
439 by (simp only: inf_commute inf_assoc inf_left_commute)
440 finally show "(x \<sqinter> y) \<sqinter> (- x \<squnion> - y) = bot"
441 by (simp add: inf_compl_bot)
443 have "(x \<sqinter> y) \<squnion> (- x \<squnion> - y) = (x \<squnion> (- x \<squnion> - y)) \<sqinter> (y \<squnion> (- x \<squnion> - y))"
444 by (rule sup_inf_distrib2)
445 also have "... = (- y \<squnion> (x \<squnion> - x)) \<sqinter> (- x \<squnion> (y \<squnion> - y))"
446 by (simp only: sup_commute sup_assoc sup_left_commute)
447 finally show "(x \<sqinter> y) \<squnion> (- x \<squnion> - y) = top"
448 by (simp add: sup_compl_top)
451 lemma compl_sup [simp]:
452 "- (x \<squnion> y) = - x \<sqinter> - y"
454 interpret boolean_algebra "\<lambda>x y. x \<squnion> - y" uminus "op \<ge>" "op >" "op \<squnion>" "op \<sqinter>" top bot
455 by (rule dual_boolean_algebra)
456 then show ?thesis by simp
462 subsection {* Uniqueness of inf and sup *}
464 lemma (in lower_semilattice) inf_unique:
465 fixes f (infixl "\<triangle>" 70)
466 assumes le1: "\<And>x y. x \<triangle> y \<le> x" and le2: "\<And>x y. x \<triangle> y \<le> y"
467 and greatest: "\<And>x y z. x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<triangle> z"
468 shows "x \<sqinter> y = x \<triangle> y"
470 show "x \<triangle> y \<le> x \<sqinter> y" by (rule le_infI) (rule le1, rule le2)
472 have leI: "\<And>x y z. x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<triangle> z" by (blast intro: greatest)
473 show "x \<sqinter> y \<le> x \<triangle> y" by (rule leI) simp_all
476 lemma (in upper_semilattice) sup_unique:
477 fixes f (infixl "\<nabla>" 70)
478 assumes ge1 [simp]: "\<And>x y. x \<le> x \<nabla> y" and ge2: "\<And>x y. y \<le> x \<nabla> y"
479 and least: "\<And>x y z. y \<le> x \<Longrightarrow> z \<le> x \<Longrightarrow> y \<nabla> z \<le> x"
480 shows "x \<squnion> y = x \<nabla> y"
482 show "x \<squnion> y \<le> x \<nabla> y" by (rule le_supI) (rule ge1, rule ge2)
484 have leI: "\<And>x y z. x \<le> z \<Longrightarrow> y \<le> z \<Longrightarrow> x \<nabla> y \<le> z" by (blast intro: least)
485 show "x \<nabla> y \<le> x \<squnion> y" by (rule leI) simp_all
489 subsection {* @{const min}/@{const max} on linear orders as
490 special case of @{const inf}/@{const sup} *}
492 sublocale linorder < min_max!: distrib_lattice less_eq less min max
495 show "max x (min y z) = min (max x y) (max x z)"
496 by (auto simp add: min_def max_def)
497 qed (auto simp add: min_def max_def not_le less_imp_le)
499 lemma inf_min: "inf = (min \<Colon> 'a\<Colon>{lower_semilattice, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
500 by (rule ext)+ (auto intro: antisym)
502 lemma sup_max: "sup = (max \<Colon> 'a\<Colon>{upper_semilattice, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
503 by (rule ext)+ (auto intro: antisym)
505 lemmas le_maxI1 = min_max.sup_ge1
506 lemmas le_maxI2 = min_max.sup_ge2
508 lemmas max_ac = min_max.sup_assoc min_max.sup_commute
509 mk_left_commute [of max, OF min_max.sup_assoc min_max.sup_commute]
511 lemmas min_ac = min_max.inf_assoc min_max.inf_commute
512 mk_left_commute [of min, OF min_max.inf_assoc min_max.inf_commute]
515 subsection {* Bool as lattice *}
517 instantiation bool :: boolean_algebra
521 bool_Compl_def: "uminus = Not"
524 bool_diff_def: "A - B \<longleftrightarrow> A \<and> \<not> B"
527 inf_bool_eq: "P \<sqinter> Q \<longleftrightarrow> P \<and> Q"
530 sup_bool_eq: "P \<squnion> Q \<longleftrightarrow> P \<or> Q"
533 qed (simp_all add: inf_bool_eq sup_bool_eq le_bool_def
534 bot_bool_eq top_bool_eq bool_Compl_def bool_diff_def, auto)
539 "P \<Longrightarrow> P \<squnion> Q"
540 by (simp add: sup_bool_eq)
543 "Q \<Longrightarrow> P \<squnion> Q"
544 by (simp add: sup_bool_eq)
547 "P \<squnion> Q \<Longrightarrow> (P \<Longrightarrow> R) \<Longrightarrow> (Q \<Longrightarrow> R) \<Longrightarrow> R"
548 by (auto simp add: sup_bool_eq)
551 subsection {* Fun as lattice *}
553 instantiation "fun" :: (type, lattice) lattice
557 inf_fun_eq [code del]: "f \<sqinter> g = (\<lambda>x. f x \<sqinter> g x)"
560 sup_fun_eq [code del]: "f \<squnion> g = (\<lambda>x. f x \<squnion> g x)"
563 qed (simp_all add: le_fun_def inf_fun_eq sup_fun_eq)
567 instance "fun" :: (type, distrib_lattice) distrib_lattice
569 qed (simp_all add: inf_fun_eq sup_fun_eq sup_inf_distrib1)
571 instantiation "fun" :: (type, uminus) uminus
575 fun_Compl_def: "- A = (\<lambda>x. - A x)"
581 instantiation "fun" :: (type, minus) minus
585 fun_diff_def: "A - B = (\<lambda>x. A x - B x)"
591 instance "fun" :: (type, boolean_algebra) boolean_algebra
593 qed (simp_all add: inf_fun_eq sup_fun_eq bot_fun_eq top_fun_eq fun_Compl_def fun_diff_def
594 inf_compl_bot sup_compl_top diff_eq)
598 less_eq (infix "\<sqsubseteq>" 50) and
599 less (infix "\<sqsubset>" 50) and
600 inf (infixl "\<sqinter>" 70) and
601 sup (infixl "\<squnion>" 65) and