src/HOL/Lattices.thy
changeset 21249 d594c58e24ed
child 21312 1d39091a3208
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Lattices.thy	Wed Nov 08 19:48:34 2006 +0100
     1.3 @@ -0,0 +1,336 @@
     1.4 +(*  Title:      HOL/Lattices.thy
     1.5 +    ID:         $Id$
     1.6 +    Author:     Tobias Nipkow
     1.7 +*)
     1.8 +
     1.9 +header {* Lattices via Locales *}
    1.10 +
    1.11 +theory Lattices
    1.12 +imports Orderings
    1.13 +begin
    1.14 +
    1.15 +subsection{* Lattices *}
    1.16 +
    1.17 +text{* This theory of lattice locales only defines binary sup and inf
    1.18 +operations. The extension to finite sets is done in theory @{text
    1.19 +Finite_Set}. In the longer term it may be better to define arbitrary
    1.20 +sups and infs via @{text THE}. *}
    1.21 +
    1.22 +locale lower_semilattice = partial_order +
    1.23 +  fixes inf :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 70)
    1.24 +  assumes inf_le1: "x \<sqinter> y \<sqsubseteq> x" and inf_le2: "x \<sqinter> y \<sqsubseteq> y"
    1.25 +  and inf_least: "x \<sqsubseteq> y \<Longrightarrow> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<sqinter> z"
    1.26 +
    1.27 +locale upper_semilattice = partial_order +
    1.28 +  fixes sup :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65)
    1.29 +  assumes sup_ge1: "x \<sqsubseteq> x \<squnion> y" and sup_ge2: "y \<sqsubseteq> x \<squnion> y"
    1.30 +  and sup_greatest: "y \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> x \<Longrightarrow> y \<squnion> z \<sqsubseteq> x"
    1.31 +
    1.32 +locale lattice = lower_semilattice + upper_semilattice
    1.33 +
    1.34 +lemma (in lower_semilattice) inf_commute: "(x \<sqinter> y) = (y \<sqinter> x)"
    1.35 +by(blast intro: antisym inf_le1 inf_le2 inf_least)
    1.36 +
    1.37 +lemma (in upper_semilattice) sup_commute: "(x \<squnion> y) = (y \<squnion> x)"
    1.38 +by(blast intro: antisym sup_ge1 sup_ge2 sup_greatest)
    1.39 +
    1.40 +lemma (in lower_semilattice) inf_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
    1.41 +by(blast intro: antisym inf_le1 inf_le2 inf_least trans del:refl)
    1.42 +
    1.43 +lemma (in upper_semilattice) sup_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
    1.44 +by(blast intro!: antisym sup_ge1 sup_ge2 intro: sup_greatest trans del:refl)
    1.45 +
    1.46 +lemma (in lower_semilattice) inf_idem[simp]: "x \<sqinter> x = x"
    1.47 +by(blast intro: antisym inf_le1 inf_le2 inf_least refl)
    1.48 +
    1.49 +lemma (in upper_semilattice) sup_idem[simp]: "x \<squnion> x = x"
    1.50 +by(blast intro: antisym sup_ge1 sup_ge2 sup_greatest refl)
    1.51 +
    1.52 +lemma (in lower_semilattice) inf_left_idem[simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
    1.53 +by (simp add: inf_assoc[symmetric])
    1.54 +
    1.55 +lemma (in upper_semilattice) sup_left_idem[simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
    1.56 +by (simp add: sup_assoc[symmetric])
    1.57 +
    1.58 +lemma (in lattice) inf_sup_absorb: "x \<sqinter> (x \<squnion> y) = x"
    1.59 +by(blast intro: antisym inf_le1 inf_least sup_ge1)
    1.60 +
    1.61 +lemma (in lattice) sup_inf_absorb: "x \<squnion> (x \<sqinter> y) = x"
    1.62 +by(blast intro: antisym sup_ge1 sup_greatest inf_le1)
    1.63 +
    1.64 +lemma (in lower_semilattice) inf_absorb: "x \<sqsubseteq> y \<Longrightarrow> x \<sqinter> y = x"
    1.65 +by(blast intro: antisym inf_le1 inf_least refl)
    1.66 +
    1.67 +lemma (in upper_semilattice) sup_absorb: "x \<sqsubseteq> y \<Longrightarrow> x \<squnion> y = y"
    1.68 +by(blast intro: antisym sup_ge2 sup_greatest refl)
    1.69 +
    1.70 +
    1.71 +lemma (in lower_semilattice) less_eq_inf_conv [simp]:
    1.72 + "x \<sqsubseteq> y \<sqinter> z = (x \<sqsubseteq> y \<and> x \<sqsubseteq> z)"
    1.73 +by(blast intro: antisym inf_le1 inf_le2 inf_least refl trans)
    1.74 +
    1.75 +lemmas (in lower_semilattice) below_inf_conv = less_eq_inf_conv
    1.76 +  -- {* a duplicate for backward compatibility *}
    1.77 +
    1.78 +lemma (in upper_semilattice) above_sup_conv[simp]:
    1.79 + "x \<squnion> y \<sqsubseteq> z = (x \<sqsubseteq> z \<and> y \<sqsubseteq> z)"
    1.80 +by(blast intro: antisym sup_ge1 sup_ge2 sup_greatest refl trans)
    1.81 +
    1.82 +
    1.83 +text{* Towards distributivity: if you have one of them, you have them all. *}
    1.84 +
    1.85 +lemma (in lattice) distrib_imp1:
    1.86 +assumes D: "!!x y z. x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
    1.87 +shows "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
    1.88 +proof-
    1.89 +  have "x \<squnion> (y \<sqinter> z) = (x \<squnion> (x \<sqinter> z)) \<squnion> (y \<sqinter> z)" by(simp add:sup_inf_absorb)
    1.90 +  also have "\<dots> = x \<squnion> (z \<sqinter> (x \<squnion> y))" by(simp add:D inf_commute sup_assoc)
    1.91 +  also have "\<dots> = ((x \<squnion> y) \<sqinter> x) \<squnion> ((x \<squnion> y) \<sqinter> z)"
    1.92 +    by(simp add:inf_sup_absorb inf_commute)
    1.93 +  also have "\<dots> = (x \<squnion> y) \<sqinter> (x \<squnion> z)" by(simp add:D)
    1.94 +  finally show ?thesis .
    1.95 +qed
    1.96 +
    1.97 +lemma (in lattice) distrib_imp2:
    1.98 +assumes D: "!!x y z. x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
    1.99 +shows "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
   1.100 +proof-
   1.101 +  have "x \<sqinter> (y \<squnion> z) = (x \<sqinter> (x \<squnion> z)) \<sqinter> (y \<squnion> z)" by(simp add:inf_sup_absorb)
   1.102 +  also have "\<dots> = x \<sqinter> (z \<squnion> (x \<sqinter> y))" by(simp add:D sup_commute inf_assoc)
   1.103 +  also have "\<dots> = ((x \<sqinter> y) \<squnion> x) \<sqinter> ((x \<sqinter> y) \<squnion> z)"
   1.104 +    by(simp add:sup_inf_absorb sup_commute)
   1.105 +  also have "\<dots> = (x \<sqinter> y) \<squnion> (x \<sqinter> z)" by(simp add:D)
   1.106 +  finally show ?thesis .
   1.107 +qed
   1.108 +
   1.109 +text{* A package of rewrite rules for deciding equivalence wrt ACI: *}
   1.110 +
   1.111 +lemma (in lower_semilattice) inf_left_commute: "x \<sqinter> (y \<sqinter> z) = y \<sqinter> (x \<sqinter> z)"
   1.112 +proof -
   1.113 +  have "x \<sqinter> (y \<sqinter> z) = (y \<sqinter> z) \<sqinter> x" by (simp only: inf_commute)
   1.114 +  also have "... = y \<sqinter> (z \<sqinter> x)" by (simp only: inf_assoc)
   1.115 +  also have "z \<sqinter> x = x \<sqinter> z" by (simp only: inf_commute)
   1.116 +  finally(back_subst) show ?thesis .
   1.117 +qed
   1.118 +
   1.119 +lemma (in upper_semilattice) sup_left_commute: "x \<squnion> (y \<squnion> z) = y \<squnion> (x \<squnion> z)"
   1.120 +proof -
   1.121 +  have "x \<squnion> (y \<squnion> z) = (y \<squnion> z) \<squnion> x" by (simp only: sup_commute)
   1.122 +  also have "... = y \<squnion> (z \<squnion> x)" by (simp only: sup_assoc)
   1.123 +  also have "z \<squnion> x = x \<squnion> z" by (simp only: sup_commute)
   1.124 +  finally(back_subst) show ?thesis .
   1.125 +qed
   1.126 +
   1.127 +lemma (in lower_semilattice) inf_left_idem: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
   1.128 +proof -
   1.129 +  have "x \<sqinter> (x \<sqinter> y) = (x \<sqinter> x) \<sqinter> y" by(simp only:inf_assoc)
   1.130 +  also have "\<dots> = x \<sqinter> y" by(simp)
   1.131 +  finally show ?thesis .
   1.132 +qed
   1.133 +
   1.134 +lemma (in upper_semilattice) sup_left_idem: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
   1.135 +proof -
   1.136 +  have "x \<squnion> (x \<squnion> y) = (x \<squnion> x) \<squnion> y" by(simp only:sup_assoc)
   1.137 +  also have "\<dots> = x \<squnion> y" by(simp)
   1.138 +  finally show ?thesis .
   1.139 +qed
   1.140 +
   1.141 +
   1.142 +lemmas (in lower_semilattice) inf_ACI =
   1.143 + inf_commute inf_assoc inf_left_commute inf_left_idem
   1.144 +
   1.145 +lemmas (in upper_semilattice) sup_ACI =
   1.146 + sup_commute sup_assoc sup_left_commute sup_left_idem
   1.147 +
   1.148 +lemmas (in lattice) ACI = inf_ACI sup_ACI
   1.149 +
   1.150 +
   1.151 +subsection{* Distributive lattices *}
   1.152 +
   1.153 +locale distrib_lattice = lattice +
   1.154 +  assumes sup_inf_distrib1: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
   1.155 +
   1.156 +lemma (in distrib_lattice) sup_inf_distrib2:
   1.157 + "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
   1.158 +by(simp add:ACI sup_inf_distrib1)
   1.159 +
   1.160 +lemma (in distrib_lattice) inf_sup_distrib1:
   1.161 + "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
   1.162 +by(rule distrib_imp2[OF sup_inf_distrib1])
   1.163 +
   1.164 +lemma (in distrib_lattice) inf_sup_distrib2:
   1.165 + "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)"
   1.166 +by(simp add:ACI inf_sup_distrib1)
   1.167 +
   1.168 +lemmas (in distrib_lattice) distrib =
   1.169 +  sup_inf_distrib1 sup_inf_distrib2 inf_sup_distrib1 inf_sup_distrib2
   1.170 +
   1.171 +
   1.172 +subsection {* Least value operator and min/max -- properties *}
   1.173 + 
   1.174 +(*FIXME: derive more of the min/max laws generically via semilattices*)
   1.175 +
   1.176 +lemma LeastI2_order:
   1.177 +  "[| P (x::'a::order);
   1.178 +      !!y. P y ==> x <= y;
   1.179 +      !!x. [| P x; ALL y. P y --> x \<le> y |] ==> Q x |]
   1.180 +   ==> Q (Least P)"
   1.181 +  apply (unfold Least_def)
   1.182 +  apply (rule theI2)
   1.183 +    apply (blast intro: order_antisym)+
   1.184 +  done
   1.185 +
   1.186 +lemma Least_equality:
   1.187 +    "[| P (k::'a::order); !!x. P x ==> k <= x |] ==> (LEAST x. P x) = k"
   1.188 +  apply (simp add: Least_def)
   1.189 +  apply (rule the_equality)
   1.190 +  apply (auto intro!: order_antisym)
   1.191 +  done
   1.192 +
   1.193 +lemma min_leastL: "(!!x. least <= x) ==> min least x = least"
   1.194 +  by (simp add: min_def)
   1.195 +
   1.196 +lemma max_leastL: "(!!x. least <= x) ==> max least x = x"
   1.197 +  by (simp add: max_def)
   1.198 +
   1.199 +lemma min_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> min x least = least"
   1.200 +  apply (simp add: min_def)
   1.201 +  apply (blast intro: order_antisym)
   1.202 +  done
   1.203 +
   1.204 +lemma max_leastR: "(\<And>x\<Colon>'a\<Colon>order. least \<le> x) \<Longrightarrow> max x least = x"
   1.205 +  apply (simp add: max_def)
   1.206 +  apply (blast intro: order_antisym)
   1.207 +  done
   1.208 +
   1.209 +lemma min_of_mono:
   1.210 +    "(!!x y. (f x <= f y) = (x <= y)) ==> min (f m) (f n) = f (min m n)"
   1.211 +  by (simp add: min_def)
   1.212 +
   1.213 +lemma max_of_mono:
   1.214 +    "(!!x y. (f x <= f y) = (x <= y)) ==> max (f m) (f n) = f (max m n)"
   1.215 +  by (simp add: max_def)
   1.216 +
   1.217 +text{* Instantiate locales: *}
   1.218 +
   1.219 +interpretation min_max:
   1.220 +  lower_semilattice["op \<le>" "op <" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a"]
   1.221 +apply unfold_locales
   1.222 +apply(simp add:min_def linorder_not_le order_less_imp_le)
   1.223 +apply(simp add:min_def linorder_not_le order_less_imp_le)
   1.224 +apply(simp add:min_def linorder_not_le order_less_imp_le)
   1.225 +done
   1.226 +
   1.227 +interpretation min_max:
   1.228 +  upper_semilattice["op \<le>" "op <" "max :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a"]
   1.229 +apply unfold_locales
   1.230 +apply(simp add: max_def linorder_not_le order_less_imp_le)
   1.231 +apply(simp add: max_def linorder_not_le order_less_imp_le)
   1.232 +apply(simp add: max_def linorder_not_le order_less_imp_le)
   1.233 +done
   1.234 +
   1.235 +interpretation min_max:
   1.236 +  lattice["op \<le>" "op <" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
   1.237 +  by unfold_locales
   1.238 +
   1.239 +interpretation min_max:
   1.240 +  distrib_lattice["op \<le>" "op <" "min :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
   1.241 +apply unfold_locales
   1.242 +apply(rule_tac x=x and y=y in linorder_le_cases)
   1.243 +apply(rule_tac x=x and y=z in linorder_le_cases)
   1.244 +apply(rule_tac x=y and y=z in linorder_le_cases)
   1.245 +apply(simp add:min_def max_def)
   1.246 +apply(simp add:min_def max_def)
   1.247 +apply(rule_tac x=y and y=z in linorder_le_cases)
   1.248 +apply(simp add:min_def max_def)
   1.249 +apply(simp add:min_def max_def)
   1.250 +apply(rule_tac x=x and y=z in linorder_le_cases)
   1.251 +apply(rule_tac x=y and y=z in linorder_le_cases)
   1.252 +apply(simp add:min_def max_def)
   1.253 +apply(simp add:min_def max_def)
   1.254 +apply(rule_tac x=y and y=z in linorder_le_cases)
   1.255 +apply(simp add:min_def max_def)
   1.256 +apply(simp add:min_def max_def)
   1.257 +done
   1.258 +
   1.259 +lemma le_max_iff_disj: "!!z::'a::linorder. (z <= max x y) = (z <= x | z <= y)"
   1.260 +  apply(simp add:max_def)
   1.261 +  apply (insert linorder_linear)
   1.262 +  apply (blast intro: order_trans)
   1.263 +  done
   1.264 +
   1.265 +lemmas le_maxI1 = min_max.sup_ge1
   1.266 +lemmas le_maxI2 = min_max.sup_ge2
   1.267 +
   1.268 +lemma less_max_iff_disj: "!!z::'a::linorder. (z < max x y) = (z < x | z < y)"
   1.269 +  apply (simp add: max_def order_le_less)
   1.270 +  apply (insert linorder_less_linear)
   1.271 +  apply (blast intro: order_less_trans)
   1.272 +  done
   1.273 +
   1.274 +lemma max_less_iff_conj [simp]:
   1.275 +    "!!z::'a::linorder. (max x y < z) = (x < z & y < z)"
   1.276 +  apply (simp add: order_le_less max_def)
   1.277 +  apply (insert linorder_less_linear)
   1.278 +  apply (blast intro: order_less_trans)
   1.279 +  done
   1.280 +
   1.281 +lemma min_less_iff_conj [simp]:
   1.282 +    "!!z::'a::linorder. (z < min x y) = (z < x & z < y)"
   1.283 +  apply (simp add: order_le_less min_def)
   1.284 +  apply (insert linorder_less_linear)
   1.285 +  apply (blast intro: order_less_trans)
   1.286 +  done
   1.287 +
   1.288 +lemma min_le_iff_disj: "!!z::'a::linorder. (min x y <= z) = (x <= z | y <= z)"
   1.289 +  apply (simp add: min_def)
   1.290 +  apply (insert linorder_linear)
   1.291 +  apply (blast intro: order_trans)
   1.292 +  done
   1.293 +
   1.294 +lemma min_less_iff_disj: "!!z::'a::linorder. (min x y < z) = (x < z | y < z)"
   1.295 +  apply (simp add: min_def order_le_less)
   1.296 +  apply (insert linorder_less_linear)
   1.297 +  apply (blast intro: order_less_trans)
   1.298 +  done
   1.299 +
   1.300 +lemmas max_ac = min_max.sup_assoc min_max.sup_commute
   1.301 +               mk_left_commute[of max,OF min_max.sup_assoc min_max.sup_commute]
   1.302 +
   1.303 +lemmas min_ac = min_max.inf_assoc min_max.inf_commute
   1.304 +               mk_left_commute[of min,OF min_max.inf_assoc min_max.inf_commute]
   1.305 +
   1.306 +lemma split_min:
   1.307 +    "P (min (i::'a::linorder) j) = ((i <= j --> P(i)) & (~ i <= j --> P(j)))"
   1.308 +  by (simp add: min_def)
   1.309 +
   1.310 +lemma split_max:
   1.311 +    "P (max (i::'a::linorder) j) = ((i <= j --> P(j)) & (~ i <= j --> P(i)))"
   1.312 +  by (simp add: max_def)
   1.313 +
   1.314 +text {* ML legacy bindings *}
   1.315 +
   1.316 +ML {*
   1.317 +val Least_def = thm "Least_def";
   1.318 +val Least_equality = thm "Least_equality";
   1.319 +val min_def = thm "min_def";
   1.320 +val min_of_mono = thm "min_of_mono";
   1.321 +val max_def = thm "max_def";
   1.322 +val max_of_mono = thm "max_of_mono";
   1.323 +val min_leastL = thm "min_leastL";
   1.324 +val max_leastL = thm "max_leastL";
   1.325 +val min_leastR = thm "min_leastR";
   1.326 +val max_leastR = thm "max_leastR";
   1.327 +val le_max_iff_disj = thm "le_max_iff_disj";
   1.328 +val le_maxI1 = thm "le_maxI1";
   1.329 +val le_maxI2 = thm "le_maxI2";
   1.330 +val less_max_iff_disj = thm "less_max_iff_disj";
   1.331 +val max_less_iff_conj = thm "max_less_iff_conj";
   1.332 +val min_less_iff_conj = thm "min_less_iff_conj";
   1.333 +val min_le_iff_disj = thm "min_le_iff_disj";
   1.334 +val min_less_iff_disj = thm "min_less_iff_disj";
   1.335 +val split_min = thm "split_min";
   1.336 +val split_max = thm "split_max";
   1.337 +*}
   1.338 +
   1.339 +end