1 (* Title: HOL/Library/Lattice_Constructions.thy
3 Copyright 2010 TU Muenchen
6 theory Lattice_Constructions
10 subsection \<open>Values extended by a bottom element\<close>
12 datatype 'a bot = Value 'a | Bot
14 instantiation bot :: (preorder) preorder
17 definition less_eq_bot where
18 "x \<le> y \<longleftrightarrow> (case x of Bot \<Rightarrow> True | Value x \<Rightarrow> (case y of Bot \<Rightarrow> False | Value y \<Rightarrow> x \<le> y))"
20 definition less_bot where
21 "x < y \<longleftrightarrow> (case y of Bot \<Rightarrow> False | Value y \<Rightarrow> (case x of Bot \<Rightarrow> True | Value x \<Rightarrow> x < y))"
23 lemma less_eq_bot_Bot [simp]: "Bot \<le> x"
24 by (simp add: less_eq_bot_def)
26 lemma less_eq_bot_Bot_code [code]: "Bot \<le> x \<longleftrightarrow> True"
29 lemma less_eq_bot_Bot_is_Bot: "x \<le> Bot \<Longrightarrow> x = Bot"
30 by (cases x) (simp_all add: less_eq_bot_def)
32 lemma less_eq_bot_Value_Bot [simp, code]: "Value x \<le> Bot \<longleftrightarrow> False"
33 by (simp add: less_eq_bot_def)
35 lemma less_eq_bot_Value [simp, code]: "Value x \<le> Value y \<longleftrightarrow> x \<le> y"
36 by (simp add: less_eq_bot_def)
38 lemma less_bot_Bot [simp, code]: "x < Bot \<longleftrightarrow> False"
39 by (simp add: less_bot_def)
41 lemma less_bot_Bot_is_Value: "Bot < x \<Longrightarrow> \<exists>z. x = Value z"
42 by (cases x) (simp_all add: less_bot_def)
44 lemma less_bot_Bot_Value [simp]: "Bot < Value x"
45 by (simp add: less_bot_def)
47 lemma less_bot_Bot_Value_code [code]: "Bot < Value x \<longleftrightarrow> True"
50 lemma less_bot_Value [simp, code]: "Value x < Value y \<longleftrightarrow> x < y"
51 by (simp add: less_bot_def)
55 (auto simp add: less_eq_bot_def less_bot_def less_le_not_le elim: order_trans split: bot.splits)
59 instance bot :: (order) order
60 by standard (auto simp add: less_eq_bot_def less_bot_def split: bot.splits)
62 instance bot :: (linorder) linorder
63 by standard (auto simp add: less_eq_bot_def less_bot_def split: bot.splits)
65 instantiation bot :: (order) bot
67 definition "bot = Bot"
71 instantiation bot :: (top) top
73 definition "top = Value top"
77 instantiation bot :: (semilattice_inf) semilattice_inf
85 | Value v \<Rightarrow>
88 | Value v' \<Rightarrow> Value (inf v v')))"
91 by standard (auto simp add: inf_bot_def less_eq_bot_def split: bot.splits)
95 instantiation bot :: (semilattice_sup) semilattice_sup
103 | Value v \<Rightarrow>
106 | Value v' \<Rightarrow> Value (sup v v')))"
109 by standard (auto simp add: sup_bot_def less_eq_bot_def split: bot.splits)
113 instance bot :: (lattice) bounded_lattice_bot
114 by intro_classes (simp add: bot_bot_def)
117 subsection \<open>Values extended by a top element\<close>
119 datatype 'a top = Value 'a | Top
121 instantiation top :: (preorder) preorder
124 definition less_eq_top where
125 "x \<le> y \<longleftrightarrow> (case y of Top \<Rightarrow> True | Value y \<Rightarrow> (case x of Top \<Rightarrow> False | Value x \<Rightarrow> x \<le> y))"
127 definition less_top where
128 "x < y \<longleftrightarrow> (case x of Top \<Rightarrow> False | Value x \<Rightarrow> (case y of Top \<Rightarrow> True | Value y \<Rightarrow> x < y))"
130 lemma less_eq_top_Top [simp]: "x \<le> Top"
131 by (simp add: less_eq_top_def)
133 lemma less_eq_top_Top_code [code]: "x \<le> Top \<longleftrightarrow> True"
136 lemma less_eq_top_is_Top: "Top \<le> x \<Longrightarrow> x = Top"
137 by (cases x) (simp_all add: less_eq_top_def)
139 lemma less_eq_top_Top_Value [simp, code]: "Top \<le> Value x \<longleftrightarrow> False"
140 by (simp add: less_eq_top_def)
142 lemma less_eq_top_Value_Value [simp, code]: "Value x \<le> Value y \<longleftrightarrow> x \<le> y"
143 by (simp add: less_eq_top_def)
145 lemma less_top_Top [simp, code]: "Top < x \<longleftrightarrow> False"
146 by (simp add: less_top_def)
148 lemma less_top_Top_is_Value: "x < Top \<Longrightarrow> \<exists>z. x = Value z"
149 by (cases x) (simp_all add: less_top_def)
151 lemma less_top_Value_Top [simp]: "Value x < Top"
152 by (simp add: less_top_def)
154 lemma less_top_Value_Top_code [code]: "Value x < Top \<longleftrightarrow> True"
157 lemma less_top_Value [simp, code]: "Value x < Value y \<longleftrightarrow> x < y"
158 by (simp add: less_top_def)
162 (auto simp add: less_eq_top_def less_top_def less_le_not_le elim: order_trans split: top.splits)
166 instance top :: (order) order
167 by standard (auto simp add: less_eq_top_def less_top_def split: top.splits)
169 instance top :: (linorder) linorder
170 by standard (auto simp add: less_eq_top_def less_top_def split: top.splits)
172 instantiation top :: (order) top
174 definition "top = Top"
178 instantiation top :: (bot) bot
180 definition "bot = Value bot"
184 instantiation top :: (semilattice_inf) semilattice_inf
192 | Value v \<Rightarrow>
195 | Value v' \<Rightarrow> Value (inf v v')))"
198 by standard (auto simp add: inf_top_def less_eq_top_def split: top.splits)
202 instantiation top :: (semilattice_sup) semilattice_sup
209 Top \<Rightarrow> Top
210 | Value v \<Rightarrow>
212 Top \<Rightarrow> Top
213 | Value v' \<Rightarrow> Value (sup v v')))"
216 by standard (auto simp add: sup_top_def less_eq_top_def split: top.splits)
220 instance top :: (lattice) bounded_lattice_top
221 by standard (simp add: top_top_def)
224 subsection \<open>Values extended by a top and a bottom element\<close>
226 datatype 'a flat_complete_lattice = Value 'a | Bot | Top
228 instantiation flat_complete_lattice :: (type) order
231 definition less_eq_flat_complete_lattice
235 Bot \<Rightarrow> True
236 | Value v1 \<Rightarrow>
238 Bot \<Rightarrow> False
239 | Value v2 \<Rightarrow> v1 = v2
240 | Top \<Rightarrow> True)
241 | Top \<Rightarrow> y = Top)"
243 definition less_flat_complete_lattice
247 Bot \<Rightarrow> y \<noteq> Bot
248 | Value v1 \<Rightarrow> y = Top
249 | Top \<Rightarrow> False)"
251 lemma [simp]: "Bot \<le> y"
252 unfolding less_eq_flat_complete_lattice_def by auto
254 lemma [simp]: "y \<le> Top"
255 unfolding less_eq_flat_complete_lattice_def by (auto split: flat_complete_lattice.splits)
257 lemma greater_than_two_values:
258 assumes "a \<noteq> b" "Value a \<le> z" "Value b \<le> z"
261 by (cases z) (auto simp add: less_eq_flat_complete_lattice_def)
263 lemma lesser_than_two_values:
264 assumes "a \<noteq> b" "z \<le> Value a" "z \<le> Value b"
267 by (cases z) (auto simp add: less_eq_flat_complete_lattice_def)
271 (auto simp add: less_eq_flat_complete_lattice_def less_flat_complete_lattice_def
272 split: flat_complete_lattice.splits)
276 instantiation flat_complete_lattice :: (type) bot
278 definition "bot = Bot"
282 instantiation flat_complete_lattice :: (type) top
284 definition "top = Top"
288 instantiation flat_complete_lattice :: (type) lattice
291 definition inf_flat_complete_lattice
295 Bot \<Rightarrow> Bot
296 | Value v1 \<Rightarrow>
298 Bot \<Rightarrow> Bot
299 | Value v2 \<Rightarrow> if v1 = v2 then x else Bot
300 | Top \<Rightarrow> x)
301 | Top \<Rightarrow> y)"
303 definition sup_flat_complete_lattice
308 | Value v1 \<Rightarrow>
311 | Value v2 \<Rightarrow> if v1 = v2 then x else Top
312 | Top \<Rightarrow> Top)
313 | Top \<Rightarrow> Top)"
317 (auto simp add: inf_flat_complete_lattice_def sup_flat_complete_lattice_def
318 less_eq_flat_complete_lattice_def split: flat_complete_lattice.splits)
322 instantiation flat_complete_lattice :: (type) complete_lattice
325 definition Sup_flat_complete_lattice
328 (if A = {} \<or> A = {Bot} then Bot
329 else if \<exists>v. A - {Bot} = {Value v} then Value (THE v. A - {Bot} = {Value v})
332 definition Inf_flat_complete_lattice
335 (if A = {} \<or> A = {Top} then Top
336 else if \<exists>v. A - {Top} = {Value v} then Value (THE v. A - {Top} = {Value v})
341 fix x :: "'a flat_complete_lattice"
346 assume "A - {Top} = {Value v}"
347 then have "(THE v. A - {Top} = {Value v}) = v"
348 by (auto intro!: the1_equality)
350 from \<open>x \<in> A\<close> \<open>A - {Top} = {Value v}\<close> have "x = Top \<or> x = Value v"
352 ultimately have "Value (THE v. A - {Top} = {Value v}) \<le> x"
355 with \<open>x \<in> A\<close> show "Inf A \<le> x"
356 unfolding Inf_flat_complete_lattice_def
359 fix z :: "'a flat_complete_lattice"
361 show "z \<le> Inf A" if z: "\<And>x. x \<in> A \<Longrightarrow> z \<le> x"
363 consider "A = {} \<or> A = {Top}"
364 | "A \<noteq> {}" "A \<noteq> {Top}" "\<exists>v. A - {Top} = {Value v}"
365 | "A \<noteq> {}" "A \<noteq> {Top}" "\<not> (\<exists>v. A - {Top} = {Value v})"
370 then have "Inf A = Top"
371 unfolding Inf_flat_complete_lattice_def by auto
372 then show ?thesis by simp
375 then obtain v where v1: "A - {Top} = {Value v}"
377 then have v2: "(THE v. A - {Top} = {Value v}) = v"
378 by (auto intro!: the1_equality)
379 from 2 v2 have Inf: "Inf A = Value v"
380 unfolding Inf_flat_complete_lattice_def by simp
381 from v1 have "Value v \<in> A" by blast
382 then have "z \<le> Value v" by (rule z)
383 with Inf show ?thesis by simp
386 then have Inf: "Inf A = Bot"
387 unfolding Inf_flat_complete_lattice_def by auto
389 proof (cases "A - {Top} = {Bot}")
391 then have "Bot \<in> A" by blast
392 then show ?thesis by (rule z)
395 from 3 obtain a1 where a1: "a1 \<in> A - {Top}"
397 from 3 False a1 obtain a2 where "a2 \<in> A - {Top} \<and> a1 \<noteq> a2"
399 with a1 z[of "a1"] z[of "a2"] show ?thesis
404 apply (auto dest!: lesser_than_two_values)
407 with Inf show ?thesis by simp
411 fix x :: "'a flat_complete_lattice"
416 assume "A - {Bot} = {Value v}"
417 then have "(THE v. A - {Bot} = {Value v}) = v"
418 by (auto intro!: the1_equality)
420 from \<open>x \<in> A\<close> \<open>A - {Bot} = {Value v}\<close> have "x = Bot \<or> x = Value v"
422 ultimately have "x \<le> Value (THE v. A - {Bot} = {Value v})"
425 with \<open>x \<in> A\<close> show "x \<le> Sup A"
426 unfolding Sup_flat_complete_lattice_def
429 fix z :: "'a flat_complete_lattice"
431 show "Sup A \<le> z" if z: "\<And>x. x \<in> A \<Longrightarrow> x \<le> z"
433 consider "A = {} \<or> A = {Bot}"
434 | "A \<noteq> {}" "A \<noteq> {Bot}" "\<exists>v. A - {Bot} = {Value v}"
435 | "A \<noteq> {}" "A \<noteq> {Bot}" "\<not> (\<exists>v. A - {Bot} = {Value v})"
440 then have "Sup A = Bot"
441 unfolding Sup_flat_complete_lattice_def by auto
442 then show ?thesis by simp
445 then obtain v where v1: "A - {Bot} = {Value v}"
447 then have v2: "(THE v. A - {Bot} = {Value v}) = v"
448 by (auto intro!: the1_equality)
449 from 2 v2 have Sup: "Sup A = Value v"
450 unfolding Sup_flat_complete_lattice_def by simp
451 from v1 have "Value v \<in> A" by blast
452 then have "Value v \<le> z" by (rule z)
453 with Sup show ?thesis by simp
456 then have Sup: "Sup A = Top"
457 unfolding Sup_flat_complete_lattice_def by auto
459 proof (cases "A - {Bot} = {Top}")
461 then have "Top \<in> A" by blast
462 then show ?thesis by (rule z)
465 from 3 obtain a1 where a1: "a1 \<in> A - {Bot}"
467 from 3 False a1 obtain a2 where "a2 \<in> A - {Bot} \<and> a1 \<noteq> a2"
469 with a1 z[of "a1"] z[of "a2"] show ?thesis
473 apply (auto dest!: greater_than_two_values)
476 with Sup show ?thesis by simp
480 show "Inf {} = (top :: 'a flat_complete_lattice)"
481 by (simp add: Inf_flat_complete_lattice_def top_flat_complete_lattice_def)
482 show "Sup {} = (bot :: 'a flat_complete_lattice)"
483 by (simp add: Sup_flat_complete_lattice_def bot_flat_complete_lattice_def)