src/HOL/Lattices.thy
 author wenzelm Mon Mar 16 18:24:30 2009 +0100 (2009-03-16) changeset 30549 d2d7874648bd parent 30302 5ffa9d4dbea7 child 30729 461ee3e49ad3 permissions -rw-r--r--
simplified method setup;
```     1 (*  Title:      HOL/Lattices.thy
```
```     2     Author:     Tobias Nipkow
```
```     3 *)
```
```     4
```
```     5 header {* Abstract lattices *}
```
```     6
```
```     7 theory Lattices
```
```     8 imports Orderings
```
```     9 begin
```
```    10
```
```    11 subsection {* Lattices *}
```
```    12
```
```    13 notation
```
```    14   less_eq  (infix "\<sqsubseteq>" 50) and
```
```    15   less  (infix "\<sqsubset>" 50)
```
```    16
```
```    17 class lower_semilattice = order +
```
```    18   fixes inf :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<sqinter>" 70)
```
```    19   assumes inf_le1 [simp]: "x \<sqinter> y \<sqsubseteq> x"
```
```    20   and inf_le2 [simp]: "x \<sqinter> y \<sqsubseteq> y"
```
```    21   and inf_greatest: "x \<sqsubseteq> y \<Longrightarrow> x \<sqsubseteq> z \<Longrightarrow> x \<sqsubseteq> y \<sqinter> z"
```
```    22
```
```    23 class upper_semilattice = order +
```
```    24   fixes sup :: "'a \<Rightarrow> 'a \<Rightarrow> 'a" (infixl "\<squnion>" 65)
```
```    25   assumes sup_ge1 [simp]: "x \<sqsubseteq> x \<squnion> y"
```
```    26   and sup_ge2 [simp]: "y \<sqsubseteq> x \<squnion> y"
```
```    27   and sup_least: "y \<sqsubseteq> x \<Longrightarrow> z \<sqsubseteq> x \<Longrightarrow> y \<squnion> z \<sqsubseteq> x"
```
```    28 begin
```
```    29
```
```    30 text {* Dual lattice *}
```
```    31
```
```    32 lemma dual_lattice:
```
```    33   "lower_semilattice (op \<ge>) (op >) sup"
```
```    34 by (rule lower_semilattice.intro, rule dual_order)
```
```    35   (unfold_locales, simp_all add: sup_least)
```
```    36
```
```    37 end
```
```    38
```
```    39 class lattice = lower_semilattice + upper_semilattice
```
```    40
```
```    41
```
```    42 subsubsection {* Intro and elim rules*}
```
```    43
```
```    44 context lower_semilattice
```
```    45 begin
```
```    46
```
```    47 lemma le_infI1[intro]:
```
```    48   assumes "a \<sqsubseteq> x"
```
```    49   shows "a \<sqinter> b \<sqsubseteq> x"
```
```    50 proof (rule order_trans)
```
```    51   from assms show "a \<sqsubseteq> x" .
```
```    52   show "a \<sqinter> b \<sqsubseteq> a" by simp
```
```    53 qed
```
```    54 lemmas (in -) [rule del] = le_infI1
```
```    55
```
```    56 lemma le_infI2[intro]:
```
```    57   assumes "b \<sqsubseteq> x"
```
```    58   shows "a \<sqinter> b \<sqsubseteq> x"
```
```    59 proof (rule order_trans)
```
```    60   from assms show "b \<sqsubseteq> x" .
```
```    61   show "a \<sqinter> b \<sqsubseteq> b" by simp
```
```    62 qed
```
```    63 lemmas (in -) [rule del] = le_infI2
```
```    64
```
```    65 lemma le_infI[intro!]: "x \<sqsubseteq> a \<Longrightarrow> x \<sqsubseteq> b \<Longrightarrow> x \<sqsubseteq> a \<sqinter> b"
```
```    66 by(blast intro: inf_greatest)
```
```    67 lemmas (in -) [rule del] = le_infI
```
```    68
```
```    69 lemma le_infE [elim!]: "x \<sqsubseteq> a \<sqinter> b \<Longrightarrow> (x \<sqsubseteq> a \<Longrightarrow> x \<sqsubseteq> b \<Longrightarrow> P) \<Longrightarrow> P"
```
```    70   by (blast intro: order_trans)
```
```    71 lemmas (in -) [rule del] = le_infE
```
```    72
```
```    73 lemma le_inf_iff [simp]:
```
```    74   "x \<sqsubseteq> y \<sqinter> z = (x \<sqsubseteq> y \<and> x \<sqsubseteq> z)"
```
```    75 by blast
```
```    76
```
```    77 lemma le_iff_inf: "(x \<sqsubseteq> y) = (x \<sqinter> y = x)"
```
```    78   by (blast intro: antisym dest: eq_iff [THEN iffD1])
```
```    79
```
```    80 lemma mono_inf:
```
```    81   fixes f :: "'a \<Rightarrow> 'b\<Colon>lower_semilattice"
```
```    82   shows "mono f \<Longrightarrow> f (A \<sqinter> B) \<le> f A \<sqinter> f B"
```
```    83   by (auto simp add: mono_def intro: Lattices.inf_greatest)
```
```    84
```
```    85 end
```
```    86
```
```    87 context upper_semilattice
```
```    88 begin
```
```    89
```
```    90 lemma le_supI1[intro]: "x \<sqsubseteq> a \<Longrightarrow> x \<sqsubseteq> a \<squnion> b"
```
```    91   by (rule order_trans) auto
```
```    92 lemmas (in -) [rule del] = le_supI1
```
```    93
```
```    94 lemma le_supI2[intro]: "x \<sqsubseteq> b \<Longrightarrow> x \<sqsubseteq> a \<squnion> b"
```
```    95   by (rule order_trans) auto
```
```    96 lemmas (in -) [rule del] = le_supI2
```
```    97
```
```    98 lemma le_supI[intro!]: "a \<sqsubseteq> x \<Longrightarrow> b \<sqsubseteq> x \<Longrightarrow> a \<squnion> b \<sqsubseteq> x"
```
```    99   by (blast intro: sup_least)
```
```   100 lemmas (in -) [rule del] = le_supI
```
```   101
```
```   102 lemma le_supE[elim!]: "a \<squnion> b \<sqsubseteq> x \<Longrightarrow> (a \<sqsubseteq> x \<Longrightarrow> b \<sqsubseteq> x \<Longrightarrow> P) \<Longrightarrow> P"
```
```   103   by (blast intro: order_trans)
```
```   104 lemmas (in -) [rule del] = le_supE
```
```   105
```
```   106 lemma ge_sup_conv[simp]:
```
```   107   "x \<squnion> y \<sqsubseteq> z = (x \<sqsubseteq> z \<and> y \<sqsubseteq> z)"
```
```   108 by blast
```
```   109
```
```   110 lemma le_iff_sup: "(x \<sqsubseteq> y) = (x \<squnion> y = y)"
```
```   111   by (blast intro: antisym dest: eq_iff [THEN iffD1])
```
```   112
```
```   113 lemma mono_sup:
```
```   114   fixes f :: "'a \<Rightarrow> 'b\<Colon>upper_semilattice"
```
```   115   shows "mono f \<Longrightarrow> f A \<squnion> f B \<le> f (A \<squnion> B)"
```
```   116   by (auto simp add: mono_def intro: Lattices.sup_least)
```
```   117
```
```   118 end
```
```   119
```
```   120
```
```   121 subsubsection{* Equational laws *}
```
```   122
```
```   123 context lower_semilattice
```
```   124 begin
```
```   125
```
```   126 lemma inf_commute: "(x \<sqinter> y) = (y \<sqinter> x)"
```
```   127   by (blast intro: antisym)
```
```   128
```
```   129 lemma inf_assoc: "(x \<sqinter> y) \<sqinter> z = x \<sqinter> (y \<sqinter> z)"
```
```   130   by (blast intro: antisym)
```
```   131
```
```   132 lemma inf_idem[simp]: "x \<sqinter> x = x"
```
```   133   by (blast intro: antisym)
```
```   134
```
```   135 lemma inf_left_idem[simp]: "x \<sqinter> (x \<sqinter> y) = x \<sqinter> y"
```
```   136   by (blast intro: antisym)
```
```   137
```
```   138 lemma inf_absorb1: "x \<sqsubseteq> y \<Longrightarrow> x \<sqinter> y = x"
```
```   139   by (blast intro: antisym)
```
```   140
```
```   141 lemma inf_absorb2: "y \<sqsubseteq> x \<Longrightarrow> x \<sqinter> y = y"
```
```   142   by (blast intro: antisym)
```
```   143
```
```   144 lemma inf_left_commute: "x \<sqinter> (y \<sqinter> z) = y \<sqinter> (x \<sqinter> z)"
```
```   145   by (blast intro: antisym)
```
```   146
```
```   147 lemmas inf_ACI = inf_commute inf_assoc inf_left_commute inf_left_idem
```
```   148
```
```   149 end
```
```   150
```
```   151
```
```   152 context upper_semilattice
```
```   153 begin
```
```   154
```
```   155 lemma sup_commute: "(x \<squnion> y) = (y \<squnion> x)"
```
```   156   by (blast intro: antisym)
```
```   157
```
```   158 lemma sup_assoc: "(x \<squnion> y) \<squnion> z = x \<squnion> (y \<squnion> z)"
```
```   159   by (blast intro: antisym)
```
```   160
```
```   161 lemma sup_idem[simp]: "x \<squnion> x = x"
```
```   162   by (blast intro: antisym)
```
```   163
```
```   164 lemma sup_left_idem[simp]: "x \<squnion> (x \<squnion> y) = x \<squnion> y"
```
```   165   by (blast intro: antisym)
```
```   166
```
```   167 lemma sup_absorb1: "y \<sqsubseteq> x \<Longrightarrow> x \<squnion> y = x"
```
```   168   by (blast intro: antisym)
```
```   169
```
```   170 lemma sup_absorb2: "x \<sqsubseteq> y \<Longrightarrow> x \<squnion> y = y"
```
```   171   by (blast intro: antisym)
```
```   172
```
```   173 lemma sup_left_commute: "x \<squnion> (y \<squnion> z) = y \<squnion> (x \<squnion> z)"
```
```   174   by (blast intro: antisym)
```
```   175
```
```   176 lemmas sup_ACI = sup_commute sup_assoc sup_left_commute sup_left_idem
```
```   177
```
```   178 end
```
```   179
```
```   180 context lattice
```
```   181 begin
```
```   182
```
```   183 lemma inf_sup_absorb: "x \<sqinter> (x \<squnion> y) = x"
```
```   184   by (blast intro: antisym inf_le1 inf_greatest sup_ge1)
```
```   185
```
```   186 lemma sup_inf_absorb: "x \<squnion> (x \<sqinter> y) = x"
```
```   187   by (blast intro: antisym sup_ge1 sup_least inf_le1)
```
```   188
```
```   189 lemmas ACI = inf_ACI sup_ACI
```
```   190
```
```   191 lemmas inf_sup_ord = inf_le1 inf_le2 sup_ge1 sup_ge2
```
```   192
```
```   193 text{* Towards distributivity *}
```
```   194
```
```   195 lemma distrib_sup_le: "x \<squnion> (y \<sqinter> z) \<sqsubseteq> (x \<squnion> y) \<sqinter> (x \<squnion> z)"
```
```   196   by blast
```
```   197
```
```   198 lemma distrib_inf_le: "(x \<sqinter> y) \<squnion> (x \<sqinter> z) \<sqsubseteq> x \<sqinter> (y \<squnion> z)"
```
```   199   by blast
```
```   200
```
```   201
```
```   202 text{* If you have one of them, you have them all. *}
```
```   203
```
```   204 lemma distrib_imp1:
```
```   205 assumes D: "!!x y z. x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
```
```   206 shows "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
```
```   207 proof-
```
```   208   have "x \<squnion> (y \<sqinter> z) = (x \<squnion> (x \<sqinter> z)) \<squnion> (y \<sqinter> z)" by(simp add:sup_inf_absorb)
```
```   209   also have "\<dots> = x \<squnion> (z \<sqinter> (x \<squnion> y))" by(simp add:D inf_commute sup_assoc)
```
```   210   also have "\<dots> = ((x \<squnion> y) \<sqinter> x) \<squnion> ((x \<squnion> y) \<sqinter> z)"
```
```   211     by(simp add:inf_sup_absorb inf_commute)
```
```   212   also have "\<dots> = (x \<squnion> y) \<sqinter> (x \<squnion> z)" by(simp add:D)
```
```   213   finally show ?thesis .
```
```   214 qed
```
```   215
```
```   216 lemma distrib_imp2:
```
```   217 assumes D: "!!x y z. x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
```
```   218 shows "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
```
```   219 proof-
```
```   220   have "x \<sqinter> (y \<squnion> z) = (x \<sqinter> (x \<squnion> z)) \<sqinter> (y \<squnion> z)" by(simp add:inf_sup_absorb)
```
```   221   also have "\<dots> = x \<sqinter> (z \<squnion> (x \<sqinter> y))" by(simp add:D sup_commute inf_assoc)
```
```   222   also have "\<dots> = ((x \<sqinter> y) \<squnion> x) \<sqinter> ((x \<sqinter> y) \<squnion> z)"
```
```   223     by(simp add:sup_inf_absorb sup_commute)
```
```   224   also have "\<dots> = (x \<sqinter> y) \<squnion> (x \<sqinter> z)" by(simp add:D)
```
```   225   finally show ?thesis .
```
```   226 qed
```
```   227
```
```   228 (* seems unused *)
```
```   229 lemma modular_le: "x \<sqsubseteq> z \<Longrightarrow> x \<squnion> (y \<sqinter> z) \<sqsubseteq> (x \<squnion> y) \<sqinter> z"
```
```   230 by blast
```
```   231
```
```   232 end
```
```   233
```
```   234
```
```   235 subsection {* Distributive lattices *}
```
```   236
```
```   237 class distrib_lattice = lattice +
```
```   238   assumes sup_inf_distrib1: "x \<squnion> (y \<sqinter> z) = (x \<squnion> y) \<sqinter> (x \<squnion> z)"
```
```   239
```
```   240 context distrib_lattice
```
```   241 begin
```
```   242
```
```   243 lemma sup_inf_distrib2:
```
```   244  "(y \<sqinter> z) \<squnion> x = (y \<squnion> x) \<sqinter> (z \<squnion> x)"
```
```   245 by(simp add:ACI sup_inf_distrib1)
```
```   246
```
```   247 lemma inf_sup_distrib1:
```
```   248  "x \<sqinter> (y \<squnion> z) = (x \<sqinter> y) \<squnion> (x \<sqinter> z)"
```
```   249 by(rule distrib_imp2[OF sup_inf_distrib1])
```
```   250
```
```   251 lemma inf_sup_distrib2:
```
```   252  "(y \<squnion> z) \<sqinter> x = (y \<sqinter> x) \<squnion> (z \<sqinter> x)"
```
```   253 by(simp add:ACI inf_sup_distrib1)
```
```   254
```
```   255 lemmas distrib =
```
```   256   sup_inf_distrib1 sup_inf_distrib2 inf_sup_distrib1 inf_sup_distrib2
```
```   257
```
```   258 end
```
```   259
```
```   260
```
```   261 subsection {* Uniqueness of inf and sup *}
```
```   262
```
```   263 lemma (in lower_semilattice) inf_unique:
```
```   264   fixes f (infixl "\<triangle>" 70)
```
```   265   assumes le1: "\<And>x y. x \<triangle> y \<le> x" and le2: "\<And>x y. x \<triangle> y \<le> y"
```
```   266   and greatest: "\<And>x y z. x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<triangle> z"
```
```   267   shows "x \<sqinter> y = x \<triangle> y"
```
```   268 proof (rule antisym)
```
```   269   show "x \<triangle> y \<le> x \<sqinter> y" by (rule le_infI) (rule le1, rule le2)
```
```   270 next
```
```   271   have leI: "\<And>x y z. x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> y \<triangle> z" by (blast intro: greatest)
```
```   272   show "x \<sqinter> y \<le> x \<triangle> y" by (rule leI) simp_all
```
```   273 qed
```
```   274
```
```   275 lemma (in upper_semilattice) sup_unique:
```
```   276   fixes f (infixl "\<nabla>" 70)
```
```   277   assumes ge1 [simp]: "\<And>x y. x \<le> x \<nabla> y" and ge2: "\<And>x y. y \<le> x \<nabla> y"
```
```   278   and least: "\<And>x y z. y \<le> x \<Longrightarrow> z \<le> x \<Longrightarrow> y \<nabla> z \<le> x"
```
```   279   shows "x \<squnion> y = x \<nabla> y"
```
```   280 proof (rule antisym)
```
```   281   show "x \<squnion> y \<le> x \<nabla> y" by (rule le_supI) (rule ge1, rule ge2)
```
```   282 next
```
```   283   have leI: "\<And>x y z. x \<le> z \<Longrightarrow> y \<le> z \<Longrightarrow> x \<nabla> y \<le> z" by (blast intro: least)
```
```   284   show "x \<nabla> y \<le> x \<squnion> y" by (rule leI) simp_all
```
```   285 qed
```
```   286
```
```   287
```
```   288 subsection {* @{const min}/@{const max} on linear orders as
```
```   289   special case of @{const inf}/@{const sup} *}
```
```   290
```
```   291 lemma (in linorder) distrib_lattice_min_max:
```
```   292   "distrib_lattice (op \<le>) (op <) min max"
```
```   293 proof
```
```   294   have aux: "\<And>x y \<Colon> 'a. x < y \<Longrightarrow> y \<le> x \<Longrightarrow> x = y"
```
```   295     by (auto simp add: less_le antisym)
```
```   296   fix x y z
```
```   297   show "max x (min y z) = min (max x y) (max x z)"
```
```   298   unfolding min_def max_def
```
```   299   by auto
```
```   300 qed (auto simp add: min_def max_def not_le less_imp_le)
```
```   301
```
```   302 interpretation min_max!: distrib_lattice "op \<le> :: 'a::linorder \<Rightarrow> 'a \<Rightarrow> bool" "op <" min max
```
```   303   by (rule distrib_lattice_min_max)
```
```   304
```
```   305 lemma inf_min: "inf = (min \<Colon> 'a\<Colon>{lower_semilattice, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
```
```   306   by (rule ext)+ (auto intro: antisym)
```
```   307
```
```   308 lemma sup_max: "sup = (max \<Colon> 'a\<Colon>{upper_semilattice, linorder} \<Rightarrow> 'a \<Rightarrow> 'a)"
```
```   309   by (rule ext)+ (auto intro: antisym)
```
```   310
```
```   311 lemmas le_maxI1 = min_max.sup_ge1
```
```   312 lemmas le_maxI2 = min_max.sup_ge2
```
```   313
```
```   314 lemmas max_ac = min_max.sup_assoc min_max.sup_commute
```
```   315   mk_left_commute [of max, OF min_max.sup_assoc min_max.sup_commute]
```
```   316
```
```   317 lemmas min_ac = min_max.inf_assoc min_max.inf_commute
```
```   318   mk_left_commute [of min, OF min_max.inf_assoc min_max.inf_commute]
```
```   319
```
```   320 text {*
```
```   321   Now we have inherited antisymmetry as an intro-rule on all
```
```   322   linear orders. This is a problem because it applies to bool, which is
```
```   323   undesirable.
```
```   324 *}
```
```   325
```
```   326 lemmas [rule del] = min_max.le_infI min_max.le_supI
```
```   327   min_max.le_supE min_max.le_infE min_max.le_supI1 min_max.le_supI2
```
```   328   min_max.le_infI1 min_max.le_infI2
```
```   329
```
```   330
```
```   331 subsection {* Bool as lattice *}
```
```   332
```
```   333 instantiation bool :: distrib_lattice
```
```   334 begin
```
```   335
```
```   336 definition
```
```   337   inf_bool_eq: "P \<sqinter> Q \<longleftrightarrow> P \<and> Q"
```
```   338
```
```   339 definition
```
```   340   sup_bool_eq: "P \<squnion> Q \<longleftrightarrow> P \<or> Q"
```
```   341
```
```   342 instance
```
```   343   by intro_classes (auto simp add: inf_bool_eq sup_bool_eq le_bool_def)
```
```   344
```
```   345 end
```
```   346
```
```   347
```
```   348 subsection {* Fun as lattice *}
```
```   349
```
```   350 instantiation "fun" :: (type, lattice) lattice
```
```   351 begin
```
```   352
```
```   353 definition
```
```   354   inf_fun_eq [code del]: "f \<sqinter> g = (\<lambda>x. f x \<sqinter> g x)"
```
```   355
```
```   356 definition
```
```   357   sup_fun_eq [code del]: "f \<squnion> g = (\<lambda>x. f x \<squnion> g x)"
```
```   358
```
```   359 instance
```
```   360 apply intro_classes
```
```   361 unfolding inf_fun_eq sup_fun_eq
```
```   362 apply (auto intro: le_funI)
```
```   363 apply (rule le_funI)
```
```   364 apply (auto dest: le_funD)
```
```   365 apply (rule le_funI)
```
```   366 apply (auto dest: le_funD)
```
```   367 done
```
```   368
```
```   369 end
```
```   370
```
```   371 instance "fun" :: (type, distrib_lattice) distrib_lattice
```
```   372   by default (auto simp add: inf_fun_eq sup_fun_eq sup_inf_distrib1)
```
```   373
```
```   374
```
```   375 text {* redundant bindings *}
```
```   376
```
```   377 lemmas inf_aci = inf_ACI
```
```   378 lemmas sup_aci = sup_ACI
```
```   379
```
```   380 no_notation
```
```   381   less_eq  (infix "\<sqsubseteq>" 50) and
```
```   382   less (infix "\<sqsubset>" 50) and
```
```   383   inf  (infixl "\<sqinter>" 70) and
```
```   384   sup  (infixl "\<squnion>" 65)
```
```   385
```
```   386 end
```