| 21249 |      1 | (*  Title:      HOL/Lattices.thy
 | 
|  |      2 |     ID:         $Id$
 | 
|  |      3 |     Author:     Tobias Nipkow
 | 
|  |      4 | *)
 | 
|  |      5 | 
 | 
|  |      6 | header {* Lattices via Locales *}
 | 
|  |      7 | 
 | 
|  |      8 | theory Lattices
 | 
|  |      9 | imports Orderings
 | 
|  |     10 | begin
 | 
|  |     11 | 
 | 
|  |     12 | subsection{* Lattices *}
 | 
|  |     13 | 
 | 
|  |     14 | text{* This theory of lattice locales only defines binary sup and inf
 | 
|  |     15 | operations. The extension to finite sets is done in theory @{text
 | 
|  |     16 | Finite_Set}. In the longer term it may be better to define arbitrary
 | 
|  |     17 | sups and infs via @{text THE}. *}
 | 
|  |     18 | 
 | 
|  |     19 | locale lower_semilattice = partial_order +
 | 
|  |     20 |   fixes inf :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 70)
 | 
| 21312 |     21 |   assumes inf_le1[simp]: "x \<sqinter> y \<sqsubseteq> x" and inf_le2[simp]: "x \<sqinter> y \<sqsubseteq> y"
 | 
| 21249 |     22 |   and inf_least: "x \<sqsubseteq> y \<Longrightarrow> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<sqinter> z"
 | 
|  |     23 | 
 | 
|  |     24 | locale upper_semilattice = partial_order +
 | 
|  |     25 |   fixes sup :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65)
 | 
| 21312 |     26 |   assumes sup_ge1[simp]: "x \<sqsubseteq> x \<squnion> y" and sup_ge2[simp]: "y \<sqsubseteq> x \<squnion> y"
 | 
| 21249 |     27 |   and sup_greatest: "y \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> x \<Longrightarrow> y \<squnion> z \<sqsubseteq> x"
 | 
|  |     28 | 
 | 
|  |     29 | locale lattice = lower_semilattice + upper_semilattice
 | 
|  |     30 | 
 | 
|  |     31 | lemma (in lower_semilattice) inf_commute: "(x \<sqinter> y) = (y \<sqinter> x)"
 | 
|  |     32 | by(blast intro: antisym inf_le1 inf_le2 inf_least)
 | 
|  |     33 | 
 | 
|  |     34 | lemma (in upper_semilattice) sup_commute: "(x \<squnion> y) = (y \<squnion> x)"
 | 
|  |     35 | by(blast intro: antisym sup_ge1 sup_ge2 sup_greatest)
 | 
|  |     36 | 
 | 
|  |     37 | lemma (in lower_semilattice) inf_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
 | 
|  |     38 | by(blast intro: antisym inf_le1 inf_le2 inf_least trans del:refl)
 | 
|  |     39 | 
 | 
|  |     40 | lemma (in upper_semilattice) sup_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
 | 
|  |     41 | by(blast intro!: antisym sup_ge1 sup_ge2 intro: sup_greatest trans del:refl)
 | 
|  |     42 | 
 | 
|  |     43 | lemma (in lower_semilattice) inf_idem[simp]: "x \<sqinter> x = x"
 | 
|  |     44 | by(blast intro: antisym inf_le1 inf_le2 inf_least refl)
 | 
|  |     45 | 
 | 
|  |     46 | lemma (in upper_semilattice) sup_idem[simp]: "x \<squnion> x = x"
 | 
|  |     47 | by(blast intro: antisym sup_ge1 sup_ge2 sup_greatest refl)
 | 
|  |     48 | 
 | 
|  |     49 | lemma (in lower_semilattice) inf_left_idem[simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
 | 
|  |     50 | by (simp add: inf_assoc[symmetric])
 | 
|  |     51 | 
 | 
|  |     52 | lemma (in upper_semilattice) sup_left_idem[simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
 | 
|  |     53 | by (simp add: sup_assoc[symmetric])
 | 
|  |     54 | 
 | 
|  |     55 | lemma (in lattice) inf_sup_absorb: "x \<sqinter> (x \<squnion> y) = x"
 | 
|  |     56 | by(blast intro: antisym inf_le1 inf_least sup_ge1)
 | 
|  |     57 | 
 | 
|  |     58 | lemma (in lattice) sup_inf_absorb: "x \<squnion> (x \<sqinter> y) = x"
 | 
|  |     59 | by(blast intro: antisym sup_ge1 sup_greatest inf_le1)
 | 
|  |     60 | 
 | 
|  |     61 | lemma (in lower_semilattice) inf_absorb: "x \<sqsubseteq> y \<Longrightarrow> x \<sqinter> y = x"
 | 
|  |     62 | by(blast intro: antisym inf_le1 inf_least refl)
 | 
|  |     63 | 
 | 
|  |     64 | lemma (in upper_semilattice) sup_absorb: "x \<sqsubseteq> y \<Longrightarrow> x \<squnion> y = y"
 | 
|  |     65 | by(blast intro: antisym sup_ge2 sup_greatest refl)
 | 
|  |     66 | 
 | 
|  |     67 | 
 | 
|  |     68 | lemma (in lower_semilattice) less_eq_inf_conv [simp]:
 | 
|  |     69 |  "x \<sqsubseteq> y \<sqinter> z = (x \<sqsubseteq> y \<and> x \<sqsubseteq> z)"
 | 
|  |     70 | by(blast intro: antisym inf_le1 inf_le2 inf_least refl trans)
 | 
|  |     71 | 
 | 
|  |     72 | lemmas (in lower_semilattice) below_inf_conv = less_eq_inf_conv
 | 
|  |     73 |   -- {* a duplicate for backward compatibility *}
 | 
|  |     74 | 
 | 
|  |     75 | lemma (in upper_semilattice) above_sup_conv[simp]:
 | 
|  |     76 |  "x \<squnion> y \<sqsubseteq> z = (x \<sqsubseteq> z \<and> y \<sqsubseteq> z)"
 | 
|  |     77 | by(blast intro: antisym sup_ge1 sup_ge2 sup_greatest refl trans)
 | 
|  |     78 | 
 | 
|  |     79 | 
 | 
|  |     80 | text{* Towards distributivity: if you have one of them, you have them all. *}
 | 
|  |     81 | 
 | 
|  |     82 | lemma (in lattice) distrib_imp1:
 | 
|  |     83 | assumes D: "!!x y z. x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
 | 
|  |     84 | shows "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
 | 
|  |     85 | proof-
 | 
|  |     86 |   have "x \<squnion> (y \<sqinter> z) = (x \<squnion> (x \<sqinter> z)) \<squnion> (y \<sqinter> z)" by(simp add:sup_inf_absorb)
 | 
|  |     87 |   also have "\<dots> = x \<squnion> (z \<sqinter> (x \<squnion> y))" by(simp add:D inf_commute sup_assoc)
 | 
|  |     88 |   also have "\<dots> = ((x \<squnion> y) \<sqinter> x) \<squnion> ((x \<squnion> y) \<sqinter> z)"
 | 
|  |     89 |     by(simp add:inf_sup_absorb inf_commute)
 | 
|  |     90 |   also have "\<dots> = (x \<squnion> y) \<sqinter> (x \<squnion> z)" by(simp add:D)
 | 
|  |     91 |   finally show ?thesis .
 | 
|  |     92 | qed
 | 
|  |     93 | 
 | 
|  |     94 | lemma (in lattice) distrib_imp2:
 | 
|  |     95 | assumes D: "!!x y z. x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
 | 
|  |     96 | shows "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
 | 
|  |     97 | proof-
 | 
|  |     98 |   have "x \<sqinter> (y \<squnion> z) = (x \<sqinter> (x \<squnion> z)) \<sqinter> (y \<squnion> z)" by(simp add:inf_sup_absorb)
 | 
|  |     99 |   also have "\<dots> = x \<sqinter> (z \<squnion> (x \<sqinter> y))" by(simp add:D sup_commute inf_assoc)
 | 
|  |    100 |   also have "\<dots> = ((x \<sqinter> y) \<squnion> x) \<sqinter> ((x \<sqinter> y) \<squnion> z)"
 | 
|  |    101 |     by(simp add:sup_inf_absorb sup_commute)
 | 
|  |    102 |   also have "\<dots> = (x \<sqinter> y) \<squnion> (x \<sqinter> z)" by(simp add:D)
 | 
|  |    103 |   finally show ?thesis .
 | 
|  |    104 | qed
 | 
|  |    105 | 
 | 
|  |    106 | text{* A package of rewrite rules for deciding equivalence wrt ACI: *}
 | 
|  |    107 | 
 | 
|  |    108 | lemma (in lower_semilattice) inf_left_commute: "x \<sqinter> (y \<sqinter> z) = y \<sqinter> (x \<sqinter> z)"
 | 
|  |    109 | proof -
 | 
|  |    110 |   have "x \<sqinter> (y \<sqinter> z) = (y \<sqinter> z) \<sqinter> x" by (simp only: inf_commute)
 | 
|  |    111 |   also have "... = y \<sqinter> (z \<sqinter> x)" by (simp only: inf_assoc)
 | 
|  |    112 |   also have "z \<sqinter> x = x \<sqinter> z" by (simp only: inf_commute)
 | 
|  |    113 |   finally(back_subst) show ?thesis .
 | 
|  |    114 | qed
 | 
|  |    115 | 
 | 
|  |    116 | lemma (in upper_semilattice) sup_left_commute: "x \<squnion> (y \<squnion> z) = y \<squnion> (x \<squnion> z)"
 | 
|  |    117 | proof -
 | 
|  |    118 |   have "x \<squnion> (y \<squnion> z) = (y \<squnion> z) \<squnion> x" by (simp only: sup_commute)
 | 
|  |    119 |   also have "... = y \<squnion> (z \<squnion> x)" by (simp only: sup_assoc)
 | 
|  |    120 |   also have "z \<squnion> x = x \<squnion> z" by (simp only: sup_commute)
 | 
|  |    121 |   finally(back_subst) show ?thesis .
 | 
|  |    122 | qed
 | 
|  |    123 | 
 | 
|  |    124 | lemma (in lower_semilattice) inf_left_idem: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
 | 
|  |    125 | proof -
 | 
|  |    126 |   have "x \<sqinter> (x \<sqinter> y) = (x \<sqinter> x) \<sqinter> y" by(simp only:inf_assoc)
 | 
|  |    127 |   also have "\<dots> = x \<sqinter> y" by(simp)
 | 
|  |    128 |   finally show ?thesis .
 | 
|  |    129 | qed
 | 
|  |    130 | 
 | 
|  |    131 | lemma (in upper_semilattice) sup_left_idem: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
 | 
|  |    132 | proof -
 | 
|  |    133 |   have "x \<squnion> (x \<squnion> y) = (x \<squnion> x) \<squnion> y" by(simp only:sup_assoc)
 | 
|  |    134 |   also have "\<dots> = x \<squnion> y" by(simp)
 | 
|  |    135 |   finally show ?thesis .
 | 
|  |    136 | qed
 | 
|  |    137 | 
 | 
|  |    138 | 
 | 
|  |    139 | lemmas (in lower_semilattice) inf_ACI =
 | 
|  |    140 |  inf_commute inf_assoc inf_left_commute inf_left_idem
 | 
|  |    141 | 
 | 
|  |    142 | lemmas (in upper_semilattice) sup_ACI =
 | 
|  |    143 |  sup_commute sup_assoc sup_left_commute sup_left_idem
 | 
|  |    144 | 
 | 
|  |    145 | lemmas (in lattice) ACI = inf_ACI sup_ACI
 | 
|  |    146 | 
 | 
|  |    147 | 
 | 
|  |    148 | subsection{* Distributive lattices *}
 | 
|  |    149 | 
 | 
|  |    150 | locale distrib_lattice = lattice +
 | 
|  |    151 |   assumes sup_inf_distrib1: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
 | 
|  |    152 | 
 | 
|  |    153 | lemma (in distrib_lattice) sup_inf_distrib2:
 | 
|  |    154 |  "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
 | 
|  |    155 | by(simp add:ACI sup_inf_distrib1)
 | 
|  |    156 | 
 | 
|  |    157 | lemma (in distrib_lattice) inf_sup_distrib1:
 | 
|  |    158 |  "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
 | 
|  |    159 | by(rule distrib_imp2[OF sup_inf_distrib1])
 | 
|  |    160 | 
 | 
|  |    161 | lemma (in distrib_lattice) inf_sup_distrib2:
 | 
|  |    162 |  "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)"
 | 
|  |    163 | by(simp add:ACI inf_sup_distrib1)
 | 
|  |    164 | 
 | 
|  |    165 | lemmas (in distrib_lattice) distrib =
 | 
|  |    166 |   sup_inf_distrib1 sup_inf_distrib2 inf_sup_distrib1 inf_sup_distrib2
 | 
|  |    167 | 
 | 
|  |    168 | 
 | 
| 21381 |    169 | subsection {* min/max on linear orders as special case of inf/sup *}
 | 
| 21249 |    170 | 
 | 
|  |    171 | interpretation min_max:
 | 
| 21381 |    172 |   distrib_lattice ["op \<le>" "op <" "min \<Colon> 'a\<Colon>linorder \<Rightarrow> 'a \<Rightarrow> 'a" "max"]
 | 
| 21249 |    173 | apply unfold_locales
 | 
| 21381 |    174 | apply (simp add: min_def linorder_not_le order_less_imp_le)
 | 
|  |    175 | apply (simp add: min_def linorder_not_le order_less_imp_le)
 | 
|  |    176 | apply (simp add: min_def linorder_not_le order_less_imp_le)
 | 
|  |    177 | apply (simp add: max_def linorder_not_le order_less_imp_le)
 | 
|  |    178 | apply (simp add: max_def linorder_not_le order_less_imp_le)
 | 
|  |    179 | unfolding min_def max_def by auto
 | 
| 21249 |    180 | 
 | 
|  |    181 | lemmas le_maxI1 = min_max.sup_ge1
 | 
|  |    182 | lemmas le_maxI2 = min_max.sup_ge2
 | 
| 21381 |    183 |  
 | 
| 21249 |    184 | lemmas max_ac = min_max.sup_assoc min_max.sup_commute
 | 
|  |    185 |                mk_left_commute[of max,OF min_max.sup_assoc min_max.sup_commute]
 | 
|  |    186 | 
 | 
|  |    187 | lemmas min_ac = min_max.inf_assoc min_max.inf_commute
 | 
|  |    188 |                mk_left_commute[of min,OF min_max.inf_assoc min_max.inf_commute]
 | 
|  |    189 | 
 | 
|  |    190 | text {* ML legacy bindings *}
 | 
|  |    191 | 
 | 
|  |    192 | ML {*
 | 
|  |    193 | val Least_def = thm "Least_def";
 | 
|  |    194 | val Least_equality = thm "Least_equality";
 | 
|  |    195 | val min_def = thm "min_def";
 | 
|  |    196 | val min_of_mono = thm "min_of_mono";
 | 
|  |    197 | val max_def = thm "max_def";
 | 
|  |    198 | val max_of_mono = thm "max_of_mono";
 | 
|  |    199 | val min_leastL = thm "min_leastL";
 | 
|  |    200 | val max_leastL = thm "max_leastL";
 | 
|  |    201 | val min_leastR = thm "min_leastR";
 | 
|  |    202 | val max_leastR = thm "max_leastR";
 | 
|  |    203 | val le_max_iff_disj = thm "le_max_iff_disj";
 | 
|  |    204 | val le_maxI1 = thm "le_maxI1";
 | 
|  |    205 | val le_maxI2 = thm "le_maxI2";
 | 
|  |    206 | val less_max_iff_disj = thm "less_max_iff_disj";
 | 
|  |    207 | val max_less_iff_conj = thm "max_less_iff_conj";
 | 
|  |    208 | val min_less_iff_conj = thm "min_less_iff_conj";
 | 
|  |    209 | val min_le_iff_disj = thm "min_le_iff_disj";
 | 
|  |    210 | val min_less_iff_disj = thm "min_less_iff_disj";
 | 
|  |    211 | val split_min = thm "split_min";
 | 
|  |    212 | val split_max = thm "split_max";
 | 
|  |    213 | *}
 | 
|  |    214 | 
 | 
|  |    215 | end
 |