1 (* Title: HOL/Lattice/Lattice.thy |
1 (* Title: HOL/Lattice/Lattice.thy |
2 Author: Markus Wenzel, TU Muenchen |
2 Author: Markus Wenzel, TU Muenchen |
3 *) |
3 *) |
4 |
4 |
5 section {* Lattices *} |
5 section \<open>Lattices\<close> |
6 |
6 |
7 theory Lattice imports Bounds begin |
7 theory Lattice imports Bounds begin |
8 |
8 |
9 subsection {* Lattice operations *} |
9 subsection \<open>Lattice operations\<close> |
10 |
10 |
11 text {* |
11 text \<open> |
12 A \emph{lattice} is a partial order with infimum and supremum of any |
12 A \emph{lattice} is a partial order with infimum and supremum of any |
13 two elements (thus any \emph{finite} number of elements have bounds |
13 two elements (thus any \emph{finite} number of elements have bounds |
14 as well). |
14 as well). |
15 *} |
15 \<close> |
16 |
16 |
17 class lattice = |
17 class lattice = |
18 assumes ex_inf: "\<exists>inf. is_inf x y inf" |
18 assumes ex_inf: "\<exists>inf. is_inf x y inf" |
19 assumes ex_sup: "\<exists>sup. is_sup x y sup" |
19 assumes ex_sup: "\<exists>sup. is_sup x y sup" |
20 |
20 |
21 text {* |
21 text \<open> |
22 The @{text \<sqinter>} (meet) and @{text \<squnion>} (join) operations select such |
22 The \<open>\<sqinter>\<close> (meet) and \<open>\<squnion>\<close> (join) operations select such |
23 infimum and supremum elements. |
23 infimum and supremum elements. |
24 *} |
24 \<close> |
25 |
25 |
26 definition |
26 definition |
27 meet :: "'a::lattice \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 70) where |
27 meet :: "'a::lattice \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 70) where |
28 "x \<sqinter> y = (THE inf. is_inf x y inf)" |
28 "x \<sqinter> y = (THE inf. is_inf x y inf)" |
29 definition |
29 definition |
30 join :: "'a::lattice \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65) where |
30 join :: "'a::lattice \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65) where |
31 "x \<squnion> y = (THE sup. is_sup x y sup)" |
31 "x \<squnion> y = (THE sup. is_sup x y sup)" |
32 |
32 |
33 text {* |
33 text \<open> |
34 Due to unique existence of bounds, the lattice operations may be |
34 Due to unique existence of bounds, the lattice operations may be |
35 exhibited as follows. |
35 exhibited as follows. |
36 *} |
36 \<close> |
37 |
37 |
38 lemma meet_equality [elim?]: "is_inf x y inf \<Longrightarrow> x \<sqinter> y = inf" |
38 lemma meet_equality [elim?]: "is_inf x y inf \<Longrightarrow> x \<sqinter> y = inf" |
39 proof (unfold meet_def) |
39 proof (unfold meet_def) |
40 assume "is_inf x y inf" |
40 assume "is_inf x y inf" |
41 then show "(THE inf. is_inf x y inf) = inf" |
41 then show "(THE inf. is_inf x y inf) = inf" |
42 by (rule the_equality) (rule is_inf_uniq [OF _ `is_inf x y inf`]) |
42 by (rule the_equality) (rule is_inf_uniq [OF _ \<open>is_inf x y inf\<close>]) |
43 qed |
43 qed |
44 |
44 |
45 lemma meetI [intro?]: |
45 lemma meetI [intro?]: |
46 "inf \<sqsubseteq> x \<Longrightarrow> inf \<sqsubseteq> y \<Longrightarrow> (\<And>z. z \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> y \<Longrightarrow> z \<sqsubseteq> inf) \<Longrightarrow> x \<sqinter> y = inf" |
46 "inf \<sqsubseteq> x \<Longrightarrow> inf \<sqsubseteq> y \<Longrightarrow> (\<And>z. z \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> y \<Longrightarrow> z \<sqsubseteq> inf) \<Longrightarrow> x \<sqinter> y = inf" |
47 by (rule meet_equality, rule is_infI) blast+ |
47 by (rule meet_equality, rule is_infI) blast+ |
48 |
48 |
49 lemma join_equality [elim?]: "is_sup x y sup \<Longrightarrow> x \<squnion> y = sup" |
49 lemma join_equality [elim?]: "is_sup x y sup \<Longrightarrow> x \<squnion> y = sup" |
50 proof (unfold join_def) |
50 proof (unfold join_def) |
51 assume "is_sup x y sup" |
51 assume "is_sup x y sup" |
52 then show "(THE sup. is_sup x y sup) = sup" |
52 then show "(THE sup. is_sup x y sup) = sup" |
53 by (rule the_equality) (rule is_sup_uniq [OF _ `is_sup x y sup`]) |
53 by (rule the_equality) (rule is_sup_uniq [OF _ \<open>is_sup x y sup\<close>]) |
54 qed |
54 qed |
55 |
55 |
56 lemma joinI [intro?]: "x \<sqsubseteq> sup \<Longrightarrow> y \<sqsubseteq> sup \<Longrightarrow> |
56 lemma joinI [intro?]: "x \<sqsubseteq> sup \<Longrightarrow> y \<sqsubseteq> sup \<Longrightarrow> |
57 (\<And>z. x \<sqsubseteq> z \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> sup \<sqsubseteq> z) \<Longrightarrow> x \<squnion> y = sup" |
57 (\<And>z. x \<sqsubseteq> z \<Longrightarrow> y \<sqsubseteq> z \<Longrightarrow> sup \<sqsubseteq> z) \<Longrightarrow> x \<squnion> y = sup" |
58 by (rule join_equality, rule is_supI) blast+ |
58 by (rule join_equality, rule is_supI) blast+ |
59 |
59 |
60 |
60 |
61 text {* |
61 text \<open> |
62 \medskip The @{text \<sqinter>} and @{text \<squnion>} operations indeed determine |
62 \medskip The \<open>\<sqinter>\<close> and \<open>\<squnion>\<close> operations indeed determine |
63 bounds on a lattice structure. |
63 bounds on a lattice structure. |
64 *} |
64 \<close> |
65 |
65 |
66 lemma is_inf_meet [intro?]: "is_inf x y (x \<sqinter> y)" |
66 lemma is_inf_meet [intro?]: "is_inf x y (x \<sqinter> y)" |
67 proof (unfold meet_def) |
67 proof (unfold meet_def) |
68 from ex_inf obtain inf where "is_inf x y inf" .. |
68 from ex_inf obtain inf where "is_inf x y inf" .. |
69 then show "is_inf x y (THE inf. is_inf x y inf)" |
69 then show "is_inf x y (THE inf. is_inf x y inf)" |
70 by (rule theI) (rule is_inf_uniq [OF _ `is_inf x y inf`]) |
70 by (rule theI) (rule is_inf_uniq [OF _ \<open>is_inf x y inf\<close>]) |
71 qed |
71 qed |
72 |
72 |
73 lemma meet_greatest [intro?]: "z \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> y \<Longrightarrow> z \<sqsubseteq> x \<sqinter> y" |
73 lemma meet_greatest [intro?]: "z \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> y \<Longrightarrow> z \<sqsubseteq> x \<sqinter> y" |
74 by (rule is_inf_greatest) (rule is_inf_meet) |
74 by (rule is_inf_greatest) (rule is_inf_meet) |
75 |
75 |
95 |
95 |
96 lemma join_upper2 [intro?]: "y \<sqsubseteq> x \<squnion> y" |
96 lemma join_upper2 [intro?]: "y \<sqsubseteq> x \<squnion> y" |
97 by (rule is_sup_upper) (rule is_sup_join) |
97 by (rule is_sup_upper) (rule is_sup_join) |
98 |
98 |
99 |
99 |
100 subsection {* Duality *} |
100 subsection \<open>Duality\<close> |
101 |
101 |
102 text {* |
102 text \<open> |
103 The class of lattices is closed under formation of dual structures. |
103 The class of lattices is closed under formation of dual structures. |
104 This means that for any theorem of lattice theory, the dualized |
104 This means that for any theorem of lattice theory, the dualized |
105 statement holds as well; this important fact simplifies many proofs |
105 statement holds as well; this important fact simplifies many proofs |
106 of lattice theory. |
106 of lattice theory. |
107 *} |
107 \<close> |
108 |
108 |
109 instance dual :: (lattice) lattice |
109 instance dual :: (lattice) lattice |
110 proof |
110 proof |
111 fix x' y' :: "'a::lattice dual" |
111 fix x' y' :: "'a::lattice dual" |
112 show "\<exists>inf'. is_inf x' y' inf'" |
112 show "\<exists>inf'. is_inf x' y' inf'" |
310 have "x \<sqsubseteq> x \<squnion> y" .. |
310 have "x \<sqsubseteq> x \<squnion> y" .. |
311 also assume "x \<squnion> y = y" |
311 also assume "x \<squnion> y = y" |
312 finally show "x \<sqsubseteq> y" . |
312 finally show "x \<sqsubseteq> y" . |
313 qed |
313 qed |
314 |
314 |
315 text {* |
315 text \<open> |
316 \medskip The most fundamental result of the meta-theory of lattices |
316 \medskip The most fundamental result of the meta-theory of lattices |
317 is as follows (we do not prove it here). |
317 is as follows (we do not prove it here). |
318 |
318 |
319 Given a structure with binary operations @{text \<sqinter>} and @{text \<squnion>} |
319 Given a structure with binary operations \<open>\<sqinter>\<close> and \<open>\<squnion>\<close> |
320 such that (A), (C), and (AB) hold (cf.\ |
320 such that (A), (C), and (AB) hold (cf.\ |
321 \S\ref{sec:lattice-algebra}). This structure represents a lattice, |
321 \S\ref{sec:lattice-algebra}). This structure represents a lattice, |
322 if the relation @{term "x \<sqsubseteq> y"} is defined as @{term "x \<sqinter> y = x"} |
322 if the relation @{term "x \<sqsubseteq> y"} is defined as @{term "x \<sqinter> y = x"} |
323 (alternatively as @{term "x \<squnion> y = y"}). Furthermore, infimum and |
323 (alternatively as @{term "x \<squnion> y = y"}). Furthermore, infimum and |
324 supremum with respect to this ordering coincide with the original |
324 supremum with respect to this ordering coincide with the original |
325 @{text \<sqinter>} and @{text \<squnion>} operations. |
325 \<open>\<sqinter>\<close> and \<open>\<squnion>\<close> operations. |
326 *} |
326 \<close> |
327 |
327 |
328 |
328 |
329 subsection {* Example instances *} |
329 subsection \<open>Example instances\<close> |
330 |
330 |
331 subsubsection {* Linear orders *} |
331 subsubsection \<open>Linear orders\<close> |
332 |
332 |
333 text {* |
333 text \<open> |
334 Linear orders with @{term minimum} and @{term maximum} operations |
334 Linear orders with @{term minimum} and @{term maximum} operations |
335 are a (degenerate) example of lattice structures. |
335 are a (degenerate) example of lattice structures. |
336 *} |
336 \<close> |
337 |
337 |
338 definition |
338 definition |
339 minimum :: "'a::linear_order \<Rightarrow> 'a \<Rightarrow> 'a" where |
339 minimum :: "'a::linear_order \<Rightarrow> 'a \<Rightarrow> 'a" where |
340 "minimum x y = (if x \<sqsubseteq> y then x else y)" |
340 "minimum x y = (if x \<sqsubseteq> y then x else y)" |
341 definition |
341 definition |
365 fix x y :: "'a::linear_order" |
365 fix x y :: "'a::linear_order" |
366 from is_inf_minimum show "\<exists>inf. is_inf x y inf" .. |
366 from is_inf_minimum show "\<exists>inf. is_inf x y inf" .. |
367 from is_sup_maximum show "\<exists>sup. is_sup x y sup" .. |
367 from is_sup_maximum show "\<exists>sup. is_sup x y sup" .. |
368 qed |
368 qed |
369 |
369 |
370 text {* |
370 text \<open> |
371 The lattice operations on linear orders indeed coincide with @{term |
371 The lattice operations on linear orders indeed coincide with @{term |
372 minimum} and @{term maximum}. |
372 minimum} and @{term maximum}. |
373 *} |
373 \<close> |
374 |
374 |
375 theorem meet_mimimum: "x \<sqinter> y = minimum x y" |
375 theorem meet_mimimum: "x \<sqinter> y = minimum x y" |
376 by (rule meet_equality) (rule is_inf_minimum) |
376 by (rule meet_equality) (rule is_inf_minimum) |
377 |
377 |
378 theorem meet_maximum: "x \<squnion> y = maximum x y" |
378 theorem meet_maximum: "x \<squnion> y = maximum x y" |
379 by (rule join_equality) (rule is_sup_maximum) |
379 by (rule join_equality) (rule is_sup_maximum) |
380 |
380 |
381 |
381 |
382 |
382 |
383 subsubsection {* Binary products *} |
383 subsubsection \<open>Binary products\<close> |
384 |
384 |
385 text {* |
385 text \<open> |
386 The class of lattices is closed under direct binary products (cf.\ |
386 The class of lattices is closed under direct binary products (cf.\ |
387 \S\ref{sec:prod-order}). |
387 \S\ref{sec:prod-order}). |
388 *} |
388 \<close> |
389 |
389 |
390 lemma is_inf_prod: "is_inf p q (fst p \<sqinter> fst q, snd p \<sqinter> snd q)" |
390 lemma is_inf_prod: "is_inf p q (fst p \<sqinter> fst q, snd p \<sqinter> snd q)" |
391 proof |
391 proof |
392 show "(fst p \<sqinter> fst q, snd p \<sqinter> snd q) \<sqsubseteq> p" |
392 show "(fst p \<sqinter> fst q, snd p \<sqinter> snd q) \<sqsubseteq> p" |
393 proof - |
393 proof - |
454 fix p q :: "'a::lattice \<times> 'b::lattice" |
454 fix p q :: "'a::lattice \<times> 'b::lattice" |
455 from is_inf_prod show "\<exists>inf. is_inf p q inf" .. |
455 from is_inf_prod show "\<exists>inf. is_inf p q inf" .. |
456 from is_sup_prod show "\<exists>sup. is_sup p q sup" .. |
456 from is_sup_prod show "\<exists>sup. is_sup p q sup" .. |
457 qed |
457 qed |
458 |
458 |
459 text {* |
459 text \<open> |
460 The lattice operations on a binary product structure indeed coincide |
460 The lattice operations on a binary product structure indeed coincide |
461 with the products of the original ones. |
461 with the products of the original ones. |
462 *} |
462 \<close> |
463 |
463 |
464 theorem meet_prod: "p \<sqinter> q = (fst p \<sqinter> fst q, snd p \<sqinter> snd q)" |
464 theorem meet_prod: "p \<sqinter> q = (fst p \<sqinter> fst q, snd p \<sqinter> snd q)" |
465 by (rule meet_equality) (rule is_inf_prod) |
465 by (rule meet_equality) (rule is_inf_prod) |
466 |
466 |
467 theorem join_prod: "p \<squnion> q = (fst p \<squnion> fst q, snd p \<squnion> snd q)" |
467 theorem join_prod: "p \<squnion> q = (fst p \<squnion> fst q, snd p \<squnion> snd q)" |
468 by (rule join_equality) (rule is_sup_prod) |
468 by (rule join_equality) (rule is_sup_prod) |
469 |
469 |
470 |
470 |
471 subsubsection {* General products *} |
471 subsubsection \<open>General products\<close> |
472 |
472 |
473 text {* |
473 text \<open> |
474 The class of lattices is closed under general products (function |
474 The class of lattices is closed under general products (function |
475 spaces) as well (cf.\ \S\ref{sec:fun-order}). |
475 spaces) as well (cf.\ \S\ref{sec:fun-order}). |
476 *} |
476 \<close> |
477 |
477 |
478 lemma is_inf_fun: "is_inf f g (\<lambda>x. f x \<sqinter> g x)" |
478 lemma is_inf_fun: "is_inf f g (\<lambda>x. f x \<sqinter> g x)" |
479 proof |
479 proof |
480 show "(\<lambda>x. f x \<sqinter> g x) \<sqsubseteq> f" |
480 show "(\<lambda>x. f x \<sqinter> g x) \<sqsubseteq> f" |
481 proof |
481 proof |
524 fix f g :: "'a \<Rightarrow> 'b::lattice" |
524 fix f g :: "'a \<Rightarrow> 'b::lattice" |
525 show "\<exists>inf. is_inf f g inf" by rule (rule is_inf_fun) (* FIXME @{text "from \<dots> show \<dots> .."} does not work!? unification incompleteness!? *) |
525 show "\<exists>inf. is_inf f g inf" by rule (rule is_inf_fun) (* FIXME @{text "from \<dots> show \<dots> .."} does not work!? unification incompleteness!? *) |
526 show "\<exists>sup. is_sup f g sup" by rule (rule is_sup_fun) |
526 show "\<exists>sup. is_sup f g sup" by rule (rule is_sup_fun) |
527 qed |
527 qed |
528 |
528 |
529 text {* |
529 text \<open> |
530 The lattice operations on a general product structure (function |
530 The lattice operations on a general product structure (function |
531 space) indeed emerge by point-wise lifting of the original ones. |
531 space) indeed emerge by point-wise lifting of the original ones. |
532 *} |
532 \<close> |
533 |
533 |
534 theorem meet_fun: "f \<sqinter> g = (\<lambda>x. f x \<sqinter> g x)" |
534 theorem meet_fun: "f \<sqinter> g = (\<lambda>x. f x \<sqinter> g x)" |
535 by (rule meet_equality) (rule is_inf_fun) |
535 by (rule meet_equality) (rule is_inf_fun) |
536 |
536 |
537 theorem join_fun: "f \<squnion> g = (\<lambda>x. f x \<squnion> g x)" |
537 theorem join_fun: "f \<squnion> g = (\<lambda>x. f x \<squnion> g x)" |
538 by (rule join_equality) (rule is_sup_fun) |
538 by (rule join_equality) (rule is_sup_fun) |
539 |
539 |
540 |
540 |
541 subsection {* Monotonicity and semi-morphisms *} |
541 subsection \<open>Monotonicity and semi-morphisms\<close> |
542 |
542 |
543 text {* |
543 text \<open> |
544 The lattice operations are monotone in both argument positions. In |
544 The lattice operations are monotone in both argument positions. In |
545 fact, monotonicity of the second position is trivial due to |
545 fact, monotonicity of the second position is trivial due to |
546 commutativity. |
546 commutativity. |
547 *} |
547 \<close> |
548 |
548 |
549 theorem meet_mono: "x \<sqsubseteq> z \<Longrightarrow> y \<sqsubseteq> w \<Longrightarrow> x \<sqinter> y \<sqsubseteq> z \<sqinter> w" |
549 theorem meet_mono: "x \<sqsubseteq> z \<Longrightarrow> y \<sqsubseteq> w \<Longrightarrow> x \<sqinter> y \<sqsubseteq> z \<sqinter> w" |
550 proof - |
550 proof - |
551 { |
551 { |
552 fix a b c :: "'a::lattice" |
552 fix a b c :: "'a::lattice" |
574 then have "dual (z \<squnion> w) \<sqsubseteq> dual (x \<squnion> y)" |
574 then have "dual (z \<squnion> w) \<sqsubseteq> dual (x \<squnion> y)" |
575 by (simp only: dual_join) |
575 by (simp only: dual_join) |
576 then show ?thesis .. |
576 then show ?thesis .. |
577 qed |
577 qed |
578 |
578 |
579 text {* |
579 text \<open> |
580 \medskip A semi-morphisms is a function @{text f} that preserves the |
580 \medskip A semi-morphisms is a function \<open>f\<close> that preserves the |
581 lattice operations in the following manner: @{term "f (x \<sqinter> y) \<sqsubseteq> f x |
581 lattice operations in the following manner: @{term "f (x \<sqinter> y) \<sqsubseteq> f x |
582 \<sqinter> f y"} and @{term "f x \<squnion> f y \<sqsubseteq> f (x \<squnion> y)"}, respectively. Any of |
582 \<sqinter> f y"} and @{term "f x \<squnion> f y \<sqsubseteq> f (x \<squnion> y)"}, respectively. Any of |
583 these properties is equivalent with monotonicity. |
583 these properties is equivalent with monotonicity. |
584 *} |
584 \<close> |
585 |
585 |
586 theorem meet_semimorph: |
586 theorem meet_semimorph: |
587 "(\<And>x y. f (x \<sqinter> y) \<sqsubseteq> f x \<sqinter> f y) \<equiv> (\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y)" |
587 "(\<And>x y. f (x \<sqinter> y) \<sqsubseteq> f x \<sqinter> f y) \<equiv> (\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y)" |
588 proof |
588 proof |
589 assume morph: "\<And>x y. f (x \<sqinter> y) \<sqsubseteq> f x \<sqinter> f y" |
589 assume morph: "\<And>x y. f (x \<sqinter> y) \<sqsubseteq> f x \<sqinter> f y" |
613 assume morph: "\<And>x y. f x \<squnion> f y \<sqsubseteq> f (x \<squnion> y)" |
613 assume morph: "\<And>x y. f x \<squnion> f y \<sqsubseteq> f (x \<squnion> y)" |
614 fix x y :: "'a::lattice" |
614 fix x y :: "'a::lattice" |
615 assume "x \<sqsubseteq> y" then have "x \<squnion> y = y" .. |
615 assume "x \<sqsubseteq> y" then have "x \<squnion> y = y" .. |
616 have "f x \<sqsubseteq> f x \<squnion> f y" .. |
616 have "f x \<sqsubseteq> f x \<squnion> f y" .. |
617 also have "\<dots> \<sqsubseteq> f (x \<squnion> y)" by (rule morph) |
617 also have "\<dots> \<sqsubseteq> f (x \<squnion> y)" by (rule morph) |
618 also from `x \<sqsubseteq> y` have "x \<squnion> y = y" .. |
618 also from \<open>x \<sqsubseteq> y\<close> have "x \<squnion> y = y" .. |
619 finally show "f x \<sqsubseteq> f y" . |
619 finally show "f x \<sqsubseteq> f y" . |
620 next |
620 next |
621 assume mono: "\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y" |
621 assume mono: "\<And>x y. x \<sqsubseteq> y \<Longrightarrow> f x \<sqsubseteq> f y" |
622 show "\<And>x y. f x \<squnion> f y \<sqsubseteq> f (x \<squnion> y)" |
622 show "\<And>x y. f x \<squnion> f y \<sqsubseteq> f (x \<squnion> y)" |
623 proof - |
623 proof - |