src/HOL/Complete_Lattices.thy
author blanchet
Tue Nov 07 15:16:42 2017 +0100 (19 months ago)
changeset 67022 49309fe530fd
parent 64966 d53d7ca3303e
child 67399 eab6ce8368fa
permissions -rw-r--r--
more robust parsing for THF proofs (esp. polymorphic Leo-III proofs)
     1 (*  Title:      HOL/Complete_Lattices.thy
     2     Author:     Tobias Nipkow
     3     Author:     Lawrence C Paulson
     4     Author:     Markus Wenzel
     5     Author:     Florian Haftmann
     6 *)
     7 
     8 section \<open>Complete lattices\<close>
     9 
    10 theory Complete_Lattices
    11   imports Fun
    12 begin
    13 
    14 subsection \<open>Syntactic infimum and supremum operations\<close>
    15 
    16 class Inf =
    17   fixes Inf :: "'a set \<Rightarrow> 'a"  ("\<Sqinter>_" [900] 900)
    18 begin
    19 
    20 abbreviation INFIMUM :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
    21   where "INFIMUM A f \<equiv> \<Sqinter>(f ` A)"
    22 
    23 lemma INF_image [simp]: "INFIMUM (f ` A) g = INFIMUM A (g \<circ> f)"
    24   by (simp add: image_comp)
    25 
    26 lemma INF_identity_eq [simp]: "INFIMUM A (\<lambda>x. x) = \<Sqinter>A"
    27   by simp
    28 
    29 lemma INF_id_eq [simp]: "INFIMUM A id = \<Sqinter>A"
    30   by simp
    31 
    32 lemma INF_cong: "A = B \<Longrightarrow> (\<And>x. x \<in> B \<Longrightarrow> C x = D x) \<Longrightarrow> INFIMUM A C = INFIMUM B D"
    33   by (simp add: image_def)
    34 
    35 lemma strong_INF_cong [cong]:
    36   "A = B \<Longrightarrow> (\<And>x. x \<in> B =simp=> C x = D x) \<Longrightarrow> INFIMUM A C = INFIMUM B D"
    37   unfolding simp_implies_def by (fact INF_cong)
    38 
    39 end
    40 
    41 class Sup =
    42   fixes Sup :: "'a set \<Rightarrow> 'a"  ("\<Squnion>_" [900] 900)
    43 begin
    44 
    45 abbreviation SUPREMUM :: "'b set \<Rightarrow> ('b \<Rightarrow> 'a) \<Rightarrow> 'a"
    46   where "SUPREMUM A f \<equiv> \<Squnion>(f ` A)"
    47 
    48 lemma SUP_image [simp]: "SUPREMUM (f ` A) g = SUPREMUM A (g \<circ> f)"
    49   by (simp add: image_comp)
    50 
    51 lemma SUP_identity_eq [simp]: "SUPREMUM A (\<lambda>x. x) = \<Squnion>A"
    52   by simp
    53 
    54 lemma SUP_id_eq [simp]: "SUPREMUM A id = \<Squnion>A"
    55   by (simp add: id_def)
    56 
    57 lemma SUP_cong: "A = B \<Longrightarrow> (\<And>x. x \<in> B \<Longrightarrow> C x = D x) \<Longrightarrow> SUPREMUM A C = SUPREMUM B D"
    58   by (simp add: image_def)
    59 
    60 lemma strong_SUP_cong [cong]:
    61   "A = B \<Longrightarrow> (\<And>x. x \<in> B =simp=> C x = D x) \<Longrightarrow> SUPREMUM A C = SUPREMUM B D"
    62   unfolding simp_implies_def by (fact SUP_cong)
    63 
    64 end
    65 
    66 text \<open>
    67   Note: must use names @{const INFIMUM} and @{const SUPREMUM} here instead of
    68   \<open>INF\<close> and \<open>SUP\<close> to allow the following syntax coexist
    69   with the plain constant names.
    70 \<close>
    71 
    72 syntax (ASCII)
    73   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3INF _./ _)" [0, 10] 10)
    74   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3INF _:_./ _)" [0, 0, 10] 10)
    75   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3SUP _./ _)" [0, 10] 10)
    76   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3SUP _:_./ _)" [0, 0, 10] 10)
    77 
    78 syntax (output)
    79   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3INF _./ _)" [0, 10] 10)
    80   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3INF _:_./ _)" [0, 0, 10] 10)
    81   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3SUP _./ _)" [0, 10] 10)
    82   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3SUP _:_./ _)" [0, 0, 10] 10)
    83 
    84 syntax
    85   "_INF1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Sqinter>_./ _)" [0, 10] 10)
    86   "_INF"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Sqinter>_\<in>_./ _)" [0, 0, 10] 10)
    87   "_SUP1"     :: "pttrns \<Rightarrow> 'b \<Rightarrow> 'b"           ("(3\<Squnion>_./ _)" [0, 10] 10)
    88   "_SUP"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b \<Rightarrow> 'b"  ("(3\<Squnion>_\<in>_./ _)" [0, 0, 10] 10)
    89 
    90 translations
    91   "\<Sqinter>x y. B"   \<rightleftharpoons> "\<Sqinter>x. \<Sqinter>y. B"
    92   "\<Sqinter>x. B"     \<rightleftharpoons> "CONST INFIMUM CONST UNIV (\<lambda>x. B)"
    93   "\<Sqinter>x. B"     \<rightleftharpoons> "\<Sqinter>x \<in> CONST UNIV. B"
    94   "\<Sqinter>x\<in>A. B"   \<rightleftharpoons> "CONST INFIMUM A (\<lambda>x. B)"
    95   "\<Squnion>x y. B"   \<rightleftharpoons> "\<Squnion>x. \<Squnion>y. B"
    96   "\<Squnion>x. B"     \<rightleftharpoons> "CONST SUPREMUM CONST UNIV (\<lambda>x. B)"
    97   "\<Squnion>x. B"     \<rightleftharpoons> "\<Squnion>x \<in> CONST UNIV. B"
    98   "\<Squnion>x\<in>A. B"   \<rightleftharpoons> "CONST SUPREMUM A (\<lambda>x. B)"
    99 
   100 print_translation \<open>
   101   [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax INFIMUM} @{syntax_const "_INF"},
   102     Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax SUPREMUM} @{syntax_const "_SUP"}]
   103 \<close> \<comment> \<open>to avoid eta-contraction of body\<close>
   104 
   105 
   106 subsection \<open>Abstract complete lattices\<close>
   107 
   108 text \<open>A complete lattice always has a bottom and a top,
   109 so we include them into the following type class,
   110 along with assumptions that define bottom and top
   111 in terms of infimum and supremum.\<close>
   112 
   113 class complete_lattice = lattice + Inf + Sup + bot + top +
   114   assumes Inf_lower: "x \<in> A \<Longrightarrow> \<Sqinter>A \<le> x"
   115     and Inf_greatest: "(\<And>x. x \<in> A \<Longrightarrow> z \<le> x) \<Longrightarrow> z \<le> \<Sqinter>A"
   116     and Sup_upper: "x \<in> A \<Longrightarrow> x \<le> \<Squnion>A"
   117     and Sup_least: "(\<And>x. x \<in> A \<Longrightarrow> x \<le> z) \<Longrightarrow> \<Squnion>A \<le> z"
   118     and Inf_empty [simp]: "\<Sqinter>{} = \<top>"
   119     and Sup_empty [simp]: "\<Squnion>{} = \<bottom>"
   120 begin
   121 
   122 subclass bounded_lattice
   123 proof
   124   fix a
   125   show "\<bottom> \<le> a"
   126     by (auto intro: Sup_least simp only: Sup_empty [symmetric])
   127   show "a \<le> \<top>"
   128     by (auto intro: Inf_greatest simp only: Inf_empty [symmetric])
   129 qed
   130 
   131 lemma dual_complete_lattice: "class.complete_lattice Sup Inf sup (op \<ge>) (op >) inf \<top> \<bottom>"
   132   by (auto intro!: class.complete_lattice.intro dual_lattice)
   133     (unfold_locales, (fact Inf_empty Sup_empty Sup_upper Sup_least Inf_lower Inf_greatest)+)
   134 
   135 end
   136 
   137 context complete_lattice
   138 begin
   139 
   140 lemma Sup_eqI:
   141   "(\<And>y. y \<in> A \<Longrightarrow> y \<le> x) \<Longrightarrow> (\<And>y. (\<And>z. z \<in> A \<Longrightarrow> z \<le> y) \<Longrightarrow> x \<le> y) \<Longrightarrow> \<Squnion>A = x"
   142   by (blast intro: antisym Sup_least Sup_upper)
   143 
   144 lemma Inf_eqI:
   145   "(\<And>i. i \<in> A \<Longrightarrow> x \<le> i) \<Longrightarrow> (\<And>y. (\<And>i. i \<in> A \<Longrightarrow> y \<le> i) \<Longrightarrow> y \<le> x) \<Longrightarrow> \<Sqinter>A = x"
   146   by (blast intro: antisym Inf_greatest Inf_lower)
   147 
   148 lemma SUP_eqI:
   149   "(\<And>i. i \<in> A \<Longrightarrow> f i \<le> x) \<Longrightarrow> (\<And>y. (\<And>i. i \<in> A \<Longrightarrow> f i \<le> y) \<Longrightarrow> x \<le> y) \<Longrightarrow> (\<Squnion>i\<in>A. f i) = x"
   150   using Sup_eqI [of "f ` A" x] by auto
   151 
   152 lemma INF_eqI:
   153   "(\<And>i. i \<in> A \<Longrightarrow> x \<le> f i) \<Longrightarrow> (\<And>y. (\<And>i. i \<in> A \<Longrightarrow> f i \<ge> y) \<Longrightarrow> x \<ge> y) \<Longrightarrow> (\<Sqinter>i\<in>A. f i) = x"
   154   using Inf_eqI [of "f ` A" x] by auto
   155 
   156 lemma INF_lower: "i \<in> A \<Longrightarrow> (\<Sqinter>i\<in>A. f i) \<le> f i"
   157   using Inf_lower [of _ "f ` A"] by simp
   158 
   159 lemma INF_greatest: "(\<And>i. i \<in> A \<Longrightarrow> u \<le> f i) \<Longrightarrow> u \<le> (\<Sqinter>i\<in>A. f i)"
   160   using Inf_greatest [of "f ` A"] by auto
   161 
   162 lemma SUP_upper: "i \<in> A \<Longrightarrow> f i \<le> (\<Squnion>i\<in>A. f i)"
   163   using Sup_upper [of _ "f ` A"] by simp
   164 
   165 lemma SUP_least: "(\<And>i. i \<in> A \<Longrightarrow> f i \<le> u) \<Longrightarrow> (\<Squnion>i\<in>A. f i) \<le> u"
   166   using Sup_least [of "f ` A"] by auto
   167 
   168 lemma Inf_lower2: "u \<in> A \<Longrightarrow> u \<le> v \<Longrightarrow> \<Sqinter>A \<le> v"
   169   using Inf_lower [of u A] by auto
   170 
   171 lemma INF_lower2: "i \<in> A \<Longrightarrow> f i \<le> u \<Longrightarrow> (\<Sqinter>i\<in>A. f i) \<le> u"
   172   using INF_lower [of i A f] by auto
   173 
   174 lemma Sup_upper2: "u \<in> A \<Longrightarrow> v \<le> u \<Longrightarrow> v \<le> \<Squnion>A"
   175   using Sup_upper [of u A] by auto
   176 
   177 lemma SUP_upper2: "i \<in> A \<Longrightarrow> u \<le> f i \<Longrightarrow> u \<le> (\<Squnion>i\<in>A. f i)"
   178   using SUP_upper [of i A f] by auto
   179 
   180 lemma le_Inf_iff: "b \<le> \<Sqinter>A \<longleftrightarrow> (\<forall>a\<in>A. b \<le> a)"
   181   by (auto intro: Inf_greatest dest: Inf_lower)
   182 
   183 lemma le_INF_iff: "u \<le> (\<Sqinter>i\<in>A. f i) \<longleftrightarrow> (\<forall>i\<in>A. u \<le> f i)"
   184   using le_Inf_iff [of _ "f ` A"] by simp
   185 
   186 lemma Sup_le_iff: "\<Squnion>A \<le> b \<longleftrightarrow> (\<forall>a\<in>A. a \<le> b)"
   187   by (auto intro: Sup_least dest: Sup_upper)
   188 
   189 lemma SUP_le_iff: "(\<Squnion>i\<in>A. f i) \<le> u \<longleftrightarrow> (\<forall>i\<in>A. f i \<le> u)"
   190   using Sup_le_iff [of "f ` A"] by simp
   191 
   192 lemma Inf_insert [simp]: "\<Sqinter>insert a A = a \<sqinter> \<Sqinter>A"
   193   by (auto intro: le_infI le_infI1 le_infI2 antisym Inf_greatest Inf_lower)
   194 
   195 lemma INF_insert [simp]: "(\<Sqinter>x\<in>insert a A. f x) = f a \<sqinter> INFIMUM A f"
   196   by (simp cong del: strong_INF_cong)
   197 
   198 lemma Sup_insert [simp]: "\<Squnion>insert a A = a \<squnion> \<Squnion>A"
   199   by (auto intro: le_supI le_supI1 le_supI2 antisym Sup_least Sup_upper)
   200 
   201 lemma SUP_insert [simp]: "(\<Squnion>x\<in>insert a A. f x) = f a \<squnion> SUPREMUM A f"
   202   by (simp cong del: strong_SUP_cong)
   203 
   204 lemma INF_empty [simp]: "(\<Sqinter>x\<in>{}. f x) = \<top>"
   205   by (simp cong del: strong_INF_cong)
   206 
   207 lemma SUP_empty [simp]: "(\<Squnion>x\<in>{}. f x) = \<bottom>"
   208   by (simp cong del: strong_SUP_cong)
   209 
   210 lemma Inf_UNIV [simp]: "\<Sqinter>UNIV = \<bottom>"
   211   by (auto intro!: antisym Inf_lower)
   212 
   213 lemma Sup_UNIV [simp]: "\<Squnion>UNIV = \<top>"
   214   by (auto intro!: antisym Sup_upper)
   215 
   216 lemma Inf_Sup: "\<Sqinter>A = \<Squnion>{b. \<forall>a \<in> A. b \<le> a}"
   217   by (auto intro: antisym Inf_lower Inf_greatest Sup_upper Sup_least)
   218 
   219 lemma Sup_Inf:  "\<Squnion>A = \<Sqinter>{b. \<forall>a \<in> A. a \<le> b}"
   220   by (auto intro: antisym Inf_lower Inf_greatest Sup_upper Sup_least)
   221 
   222 lemma Inf_superset_mono: "B \<subseteq> A \<Longrightarrow> \<Sqinter>A \<le> \<Sqinter>B"
   223   by (auto intro: Inf_greatest Inf_lower)
   224 
   225 lemma Sup_subset_mono: "A \<subseteq> B \<Longrightarrow> \<Squnion>A \<le> \<Squnion>B"
   226   by (auto intro: Sup_least Sup_upper)
   227 
   228 lemma Inf_mono:
   229   assumes "\<And>b. b \<in> B \<Longrightarrow> \<exists>a\<in>A. a \<le> b"
   230   shows "\<Sqinter>A \<le> \<Sqinter>B"
   231 proof (rule Inf_greatest)
   232   fix b assume "b \<in> B"
   233   with assms obtain a where "a \<in> A" and "a \<le> b" by blast
   234   from \<open>a \<in> A\<close> have "\<Sqinter>A \<le> a" by (rule Inf_lower)
   235   with \<open>a \<le> b\<close> show "\<Sqinter>A \<le> b" by auto
   236 qed
   237 
   238 lemma INF_mono: "(\<And>m. m \<in> B \<Longrightarrow> \<exists>n\<in>A. f n \<le> g m) \<Longrightarrow> (\<Sqinter>n\<in>A. f n) \<le> (\<Sqinter>n\<in>B. g n)"
   239   using Inf_mono [of "g ` B" "f ` A"] by auto
   240 
   241 lemma Sup_mono:
   242   assumes "\<And>a. a \<in> A \<Longrightarrow> \<exists>b\<in>B. a \<le> b"
   243   shows "\<Squnion>A \<le> \<Squnion>B"
   244 proof (rule Sup_least)
   245   fix a assume "a \<in> A"
   246   with assms obtain b where "b \<in> B" and "a \<le> b" by blast
   247   from \<open>b \<in> B\<close> have "b \<le> \<Squnion>B" by (rule Sup_upper)
   248   with \<open>a \<le> b\<close> show "a \<le> \<Squnion>B" by auto
   249 qed
   250 
   251 lemma SUP_mono: "(\<And>n. n \<in> A \<Longrightarrow> \<exists>m\<in>B. f n \<le> g m) \<Longrightarrow> (\<Squnion>n\<in>A. f n) \<le> (\<Squnion>n\<in>B. g n)"
   252   using Sup_mono [of "f ` A" "g ` B"] by auto
   253 
   254 lemma INF_superset_mono: "B \<subseteq> A \<Longrightarrow> (\<And>x. x \<in> B \<Longrightarrow> f x \<le> g x) \<Longrightarrow> (\<Sqinter>x\<in>A. f x) \<le> (\<Sqinter>x\<in>B. g x)"
   255   \<comment> \<open>The last inclusion is POSITIVE!\<close>
   256   by (blast intro: INF_mono dest: subsetD)
   257 
   258 lemma SUP_subset_mono: "A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<le> g x) \<Longrightarrow> (\<Squnion>x\<in>A. f x) \<le> (\<Squnion>x\<in>B. g x)"
   259   by (blast intro: SUP_mono dest: subsetD)
   260 
   261 lemma Inf_less_eq:
   262   assumes "\<And>v. v \<in> A \<Longrightarrow> v \<le> u"
   263     and "A \<noteq> {}"
   264   shows "\<Sqinter>A \<le> u"
   265 proof -
   266   from \<open>A \<noteq> {}\<close> obtain v where "v \<in> A" by blast
   267   moreover from \<open>v \<in> A\<close> assms(1) have "v \<le> u" by blast
   268   ultimately show ?thesis by (rule Inf_lower2)
   269 qed
   270 
   271 lemma less_eq_Sup:
   272   assumes "\<And>v. v \<in> A \<Longrightarrow> u \<le> v"
   273     and "A \<noteq> {}"
   274   shows "u \<le> \<Squnion>A"
   275 proof -
   276   from \<open>A \<noteq> {}\<close> obtain v where "v \<in> A" by blast
   277   moreover from \<open>v \<in> A\<close> assms(1) have "u \<le> v" by blast
   278   ultimately show ?thesis by (rule Sup_upper2)
   279 qed
   280 
   281 lemma INF_eq:
   282   assumes "\<And>i. i \<in> A \<Longrightarrow> \<exists>j\<in>B. f i \<ge> g j"
   283     and "\<And>j. j \<in> B \<Longrightarrow> \<exists>i\<in>A. g j \<ge> f i"
   284   shows "INFIMUM A f = INFIMUM B g"
   285   by (intro antisym INF_greatest) (blast intro: INF_lower2 dest: assms)+
   286 
   287 lemma SUP_eq:
   288   assumes "\<And>i. i \<in> A \<Longrightarrow> \<exists>j\<in>B. f i \<le> g j"
   289     and "\<And>j. j \<in> B \<Longrightarrow> \<exists>i\<in>A. g j \<le> f i"
   290   shows "SUPREMUM A f = SUPREMUM B g"
   291   by (intro antisym SUP_least) (blast intro: SUP_upper2 dest: assms)+
   292 
   293 lemma less_eq_Inf_inter: "\<Sqinter>A \<squnion> \<Sqinter>B \<le> \<Sqinter>(A \<inter> B)"
   294   by (auto intro: Inf_greatest Inf_lower)
   295 
   296 lemma Sup_inter_less_eq: "\<Squnion>(A \<inter> B) \<le> \<Squnion>A \<sqinter> \<Squnion>B "
   297   by (auto intro: Sup_least Sup_upper)
   298 
   299 lemma Inf_union_distrib: "\<Sqinter>(A \<union> B) = \<Sqinter>A \<sqinter> \<Sqinter>B"
   300   by (rule antisym) (auto intro: Inf_greatest Inf_lower le_infI1 le_infI2)
   301 
   302 lemma INF_union: "(\<Sqinter>i \<in> A \<union> B. M i) = (\<Sqinter>i \<in> A. M i) \<sqinter> (\<Sqinter>i\<in>B. M i)"
   303   by (auto intro!: antisym INF_mono intro: le_infI1 le_infI2 INF_greatest INF_lower)
   304 
   305 lemma Sup_union_distrib: "\<Squnion>(A \<union> B) = \<Squnion>A \<squnion> \<Squnion>B"
   306   by (rule antisym) (auto intro: Sup_least Sup_upper le_supI1 le_supI2)
   307 
   308 lemma SUP_union: "(\<Squnion>i \<in> A \<union> B. M i) = (\<Squnion>i \<in> A. M i) \<squnion> (\<Squnion>i\<in>B. M i)"
   309   by (auto intro!: antisym SUP_mono intro: le_supI1 le_supI2 SUP_least SUP_upper)
   310 
   311 lemma INF_inf_distrib: "(\<Sqinter>a\<in>A. f a) \<sqinter> (\<Sqinter>a\<in>A. g a) = (\<Sqinter>a\<in>A. f a \<sqinter> g a)"
   312   by (rule antisym) (rule INF_greatest, auto intro: le_infI1 le_infI2 INF_lower INF_mono)
   313 
   314 lemma SUP_sup_distrib: "(\<Squnion>a\<in>A. f a) \<squnion> (\<Squnion>a\<in>A. g a) = (\<Squnion>a\<in>A. f a \<squnion> g a)"
   315   (is "?L = ?R")
   316 proof (rule antisym)
   317   show "?L \<le> ?R"
   318     by (auto intro: le_supI1 le_supI2 SUP_upper SUP_mono)
   319   show "?R \<le> ?L"
   320     by (rule SUP_least) (auto intro: le_supI1 le_supI2 SUP_upper)
   321 qed
   322 
   323 lemma Inf_top_conv [simp]:
   324   "\<Sqinter>A = \<top> \<longleftrightarrow> (\<forall>x\<in>A. x = \<top>)"
   325   "\<top> = \<Sqinter>A \<longleftrightarrow> (\<forall>x\<in>A. x = \<top>)"
   326 proof -
   327   show "\<Sqinter>A = \<top> \<longleftrightarrow> (\<forall>x\<in>A. x = \<top>)"
   328   proof
   329     assume "\<forall>x\<in>A. x = \<top>"
   330     then have "A = {} \<or> A = {\<top>}" by auto
   331     then show "\<Sqinter>A = \<top>" by auto
   332   next
   333     assume "\<Sqinter>A = \<top>"
   334     show "\<forall>x\<in>A. x = \<top>"
   335     proof (rule ccontr)
   336       assume "\<not> (\<forall>x\<in>A. x = \<top>)"
   337       then obtain x where "x \<in> A" and "x \<noteq> \<top>" by blast
   338       then obtain B where "A = insert x B" by blast
   339       with \<open>\<Sqinter>A = \<top>\<close> \<open>x \<noteq> \<top>\<close> show False by simp
   340     qed
   341   qed
   342   then show "\<top> = \<Sqinter>A \<longleftrightarrow> (\<forall>x\<in>A. x = \<top>)" by auto
   343 qed
   344 
   345 lemma INF_top_conv [simp]:
   346   "(\<Sqinter>x\<in>A. B x) = \<top> \<longleftrightarrow> (\<forall>x\<in>A. B x = \<top>)"
   347   "\<top> = (\<Sqinter>x\<in>A. B x) \<longleftrightarrow> (\<forall>x\<in>A. B x = \<top>)"
   348   using Inf_top_conv [of "B ` A"] by simp_all
   349 
   350 lemma Sup_bot_conv [simp]:
   351   "\<Squnion>A = \<bottom> \<longleftrightarrow> (\<forall>x\<in>A. x = \<bottom>)"
   352   "\<bottom> = \<Squnion>A \<longleftrightarrow> (\<forall>x\<in>A. x = \<bottom>)"
   353   using dual_complete_lattice
   354   by (rule complete_lattice.Inf_top_conv)+
   355 
   356 lemma SUP_bot_conv [simp]:
   357   "(\<Squnion>x\<in>A. B x) = \<bottom> \<longleftrightarrow> (\<forall>x\<in>A. B x = \<bottom>)"
   358   "\<bottom> = (\<Squnion>x\<in>A. B x) \<longleftrightarrow> (\<forall>x\<in>A. B x = \<bottom>)"
   359   using Sup_bot_conv [of "B ` A"] by simp_all
   360 
   361 lemma INF_const [simp]: "A \<noteq> {} \<Longrightarrow> (\<Sqinter>i\<in>A. f) = f"
   362   by (auto intro: antisym INF_lower INF_greatest)
   363 
   364 lemma SUP_const [simp]: "A \<noteq> {} \<Longrightarrow> (\<Squnion>i\<in>A. f) = f"
   365   by (auto intro: antisym SUP_upper SUP_least)
   366 
   367 lemma INF_top [simp]: "(\<Sqinter>x\<in>A. \<top>) = \<top>"
   368   by (cases "A = {}") simp_all
   369 
   370 lemma SUP_bot [simp]: "(\<Squnion>x\<in>A. \<bottom>) = \<bottom>"
   371   by (cases "A = {}") simp_all
   372 
   373 lemma INF_commute: "(\<Sqinter>i\<in>A. \<Sqinter>j\<in>B. f i j) = (\<Sqinter>j\<in>B. \<Sqinter>i\<in>A. f i j)"
   374   by (iprover intro: INF_lower INF_greatest order_trans antisym)
   375 
   376 lemma SUP_commute: "(\<Squnion>i\<in>A. \<Squnion>j\<in>B. f i j) = (\<Squnion>j\<in>B. \<Squnion>i\<in>A. f i j)"
   377   by (iprover intro: SUP_upper SUP_least order_trans antisym)
   378 
   379 lemma INF_absorb:
   380   assumes "k \<in> I"
   381   shows "A k \<sqinter> (\<Sqinter>i\<in>I. A i) = (\<Sqinter>i\<in>I. A i)"
   382 proof -
   383   from assms obtain J where "I = insert k J" by blast
   384   then show ?thesis by simp
   385 qed
   386 
   387 lemma SUP_absorb:
   388   assumes "k \<in> I"
   389   shows "A k \<squnion> (\<Squnion>i\<in>I. A i) = (\<Squnion>i\<in>I. A i)"
   390 proof -
   391   from assms obtain J where "I = insert k J" by blast
   392   then show ?thesis by simp
   393 qed
   394 
   395 lemma INF_inf_const1: "I \<noteq> {} \<Longrightarrow> (INF i:I. inf x (f i)) = inf x (INF i:I. f i)"
   396   by (intro antisym INF_greatest inf_mono order_refl INF_lower)
   397      (auto intro: INF_lower2 le_infI2 intro!: INF_mono)
   398 
   399 lemma INF_inf_const2: "I \<noteq> {} \<Longrightarrow> (INF i:I. inf (f i) x) = inf (INF i:I. f i) x"
   400   using INF_inf_const1[of I x f] by (simp add: inf_commute)
   401 
   402 lemma INF_constant: "(\<Sqinter>y\<in>A. c) = (if A = {} then \<top> else c)"
   403   by simp
   404 
   405 lemma SUP_constant: "(\<Squnion>y\<in>A. c) = (if A = {} then \<bottom> else c)"
   406   by simp
   407 
   408 lemma less_INF_D:
   409   assumes "y < (\<Sqinter>i\<in>A. f i)" "i \<in> A"
   410   shows "y < f i"
   411 proof -
   412   note \<open>y < (\<Sqinter>i\<in>A. f i)\<close>
   413   also have "(\<Sqinter>i\<in>A. f i) \<le> f i" using \<open>i \<in> A\<close>
   414     by (rule INF_lower)
   415   finally show "y < f i" .
   416 qed
   417 
   418 lemma SUP_lessD:
   419   assumes "(\<Squnion>i\<in>A. f i) < y" "i \<in> A"
   420   shows "f i < y"
   421 proof -
   422   have "f i \<le> (\<Squnion>i\<in>A. f i)"
   423     using \<open>i \<in> A\<close> by (rule SUP_upper)
   424   also note \<open>(\<Squnion>i\<in>A. f i) < y\<close>
   425   finally show "f i < y" .
   426 qed
   427 
   428 lemma INF_UNIV_bool_expand: "(\<Sqinter>b. A b) = A True \<sqinter> A False"
   429   by (simp add: UNIV_bool inf_commute)
   430 
   431 lemma SUP_UNIV_bool_expand: "(\<Squnion>b. A b) = A True \<squnion> A False"
   432   by (simp add: UNIV_bool sup_commute)
   433 
   434 lemma Inf_le_Sup: "A \<noteq> {} \<Longrightarrow> Inf A \<le> Sup A"
   435   by (blast intro: Sup_upper2 Inf_lower ex_in_conv)
   436 
   437 lemma INF_le_SUP: "A \<noteq> {} \<Longrightarrow> INFIMUM A f \<le> SUPREMUM A f"
   438   using Inf_le_Sup [of "f ` A"] by simp
   439 
   440 lemma INF_eq_const: "I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i = x) \<Longrightarrow> INFIMUM I f = x"
   441   by (auto intro: INF_eqI)
   442 
   443 lemma SUP_eq_const: "I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i = x) \<Longrightarrow> SUPREMUM I f = x"
   444   by (auto intro: SUP_eqI)
   445 
   446 lemma INF_eq_iff: "I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<le> c) \<Longrightarrow> INFIMUM I f = c \<longleftrightarrow> (\<forall>i\<in>I. f i = c)"
   447   using INF_eq_const [of I f c] INF_lower [of _ I f]
   448   by (auto intro: antisym cong del: strong_INF_cong)
   449 
   450 lemma SUP_eq_iff: "I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> c \<le> f i) \<Longrightarrow> SUPREMUM I f = c \<longleftrightarrow> (\<forall>i\<in>I. f i = c)"
   451   using SUP_eq_const [of I f c] SUP_upper [of _ I f]
   452   by (auto intro: antisym cong del: strong_SUP_cong)
   453 
   454 end
   455 
   456 class complete_distrib_lattice = complete_lattice +
   457   assumes sup_Inf: "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)"
   458     and inf_Sup: "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
   459 begin
   460 
   461 lemma sup_INF: "a \<squnion> (\<Sqinter>b\<in>B. f b) = (\<Sqinter>b\<in>B. a \<squnion> f b)"
   462   by (simp add: sup_Inf)
   463 
   464 lemma inf_SUP: "a \<sqinter> (\<Squnion>b\<in>B. f b) = (\<Squnion>b\<in>B. a \<sqinter> f b)"
   465   by (simp add: inf_Sup)
   466 
   467 lemma dual_complete_distrib_lattice:
   468   "class.complete_distrib_lattice Sup Inf sup (op \<ge>) (op >) inf \<top> \<bottom>"
   469   apply (rule class.complete_distrib_lattice.intro)
   470    apply (fact dual_complete_lattice)
   471   apply (rule class.complete_distrib_lattice_axioms.intro)
   472    apply (simp_all add: inf_Sup sup_Inf)
   473   done
   474 
   475 subclass distrib_lattice
   476 proof
   477   fix a b c
   478   have "a \<squnion> \<Sqinter>{b, c} = (\<Sqinter>d\<in>{b, c}. a \<squnion> d)" by (rule sup_Inf)
   479   then show "a \<squnion> b \<sqinter> c = (a \<squnion> b) \<sqinter> (a \<squnion> c)" by simp
   480 qed
   481 
   482 lemma Inf_sup: "\<Sqinter>B \<squnion> a = (\<Sqinter>b\<in>B. b \<squnion> a)"
   483   by (simp add: sup_Inf sup_commute)
   484 
   485 lemma Sup_inf: "\<Squnion>B \<sqinter> a = (\<Squnion>b\<in>B. b \<sqinter> a)"
   486   by (simp add: inf_Sup inf_commute)
   487 
   488 lemma INF_sup: "(\<Sqinter>b\<in>B. f b) \<squnion> a = (\<Sqinter>b\<in>B. f b \<squnion> a)"
   489   by (simp add: sup_INF sup_commute)
   490 
   491 lemma SUP_inf: "(\<Squnion>b\<in>B. f b) \<sqinter> a = (\<Squnion>b\<in>B. f b \<sqinter> a)"
   492   by (simp add: inf_SUP inf_commute)
   493 
   494 lemma Inf_sup_eq_top_iff: "(\<Sqinter>B \<squnion> a = \<top>) \<longleftrightarrow> (\<forall>b\<in>B. b \<squnion> a = \<top>)"
   495   by (simp only: Inf_sup INF_top_conv)
   496 
   497 lemma Sup_inf_eq_bot_iff: "(\<Squnion>B \<sqinter> a = \<bottom>) \<longleftrightarrow> (\<forall>b\<in>B. b \<sqinter> a = \<bottom>)"
   498   by (simp only: Sup_inf SUP_bot_conv)
   499 
   500 lemma INF_sup_distrib2: "(\<Sqinter>a\<in>A. f a) \<squnion> (\<Sqinter>b\<in>B. g b) = (\<Sqinter>a\<in>A. \<Sqinter>b\<in>B. f a \<squnion> g b)"
   501   by (subst INF_commute) (simp add: sup_INF INF_sup)
   502 
   503 lemma SUP_inf_distrib2: "(\<Squnion>a\<in>A. f a) \<sqinter> (\<Squnion>b\<in>B. g b) = (\<Squnion>a\<in>A. \<Squnion>b\<in>B. f a \<sqinter> g b)"
   504   by (subst SUP_commute) (simp add: inf_SUP SUP_inf)
   505 
   506 context
   507   fixes f :: "'a \<Rightarrow> 'b::complete_lattice"
   508   assumes "mono f"
   509 begin
   510 
   511 lemma mono_Inf: "f (\<Sqinter>A) \<le> (\<Sqinter>x\<in>A. f x)"
   512   using \<open>mono f\<close> by (auto intro: complete_lattice_class.INF_greatest Inf_lower dest: monoD)
   513 
   514 lemma mono_Sup: "(\<Squnion>x\<in>A. f x) \<le> f (\<Squnion>A)"
   515   using \<open>mono f\<close> by (auto intro: complete_lattice_class.SUP_least Sup_upper dest: monoD)
   516 
   517 lemma mono_INF: "f (INF i : I. A i) \<le> (INF x : I. f (A x))"
   518   by (intro complete_lattice_class.INF_greatest monoD[OF \<open>mono f\<close>] INF_lower)
   519 
   520 lemma mono_SUP: "(SUP x : I. f (A x)) \<le> f (SUP i : I. A i)"
   521   by (intro complete_lattice_class.SUP_least monoD[OF \<open>mono f\<close>] SUP_upper)
   522 
   523 end
   524 
   525 end
   526 
   527 class complete_boolean_algebra = boolean_algebra + complete_distrib_lattice
   528 begin
   529 
   530 lemma dual_complete_boolean_algebra:
   531   "class.complete_boolean_algebra Sup Inf sup (op \<ge>) (op >) inf \<top> \<bottom> (\<lambda>x y. x \<squnion> - y) uminus"
   532   by (rule class.complete_boolean_algebra.intro,
   533       rule dual_complete_distrib_lattice,
   534       rule dual_boolean_algebra)
   535 
   536 lemma uminus_Inf: "- (\<Sqinter>A) = \<Squnion>(uminus ` A)"
   537 proof (rule antisym)
   538   show "- \<Sqinter>A \<le> \<Squnion>(uminus ` A)"
   539     by (rule compl_le_swap2, rule Inf_greatest, rule compl_le_swap2, rule Sup_upper) simp
   540   show "\<Squnion>(uminus ` A) \<le> - \<Sqinter>A"
   541     by (rule Sup_least, rule compl_le_swap1, rule Inf_lower) auto
   542 qed
   543 
   544 lemma uminus_INF: "- (\<Sqinter>x\<in>A. B x) = (\<Squnion>x\<in>A. - B x)"
   545   by (simp add: uminus_Inf image_image)
   546 
   547 lemma uminus_Sup: "- (\<Squnion>A) = \<Sqinter>(uminus ` A)"
   548 proof -
   549   have "\<Squnion>A = - \<Sqinter>(uminus ` A)"
   550     by (simp add: image_image uminus_INF)
   551   then show ?thesis by simp
   552 qed
   553 
   554 lemma uminus_SUP: "- (\<Squnion>x\<in>A. B x) = (\<Sqinter>x\<in>A. - B x)"
   555   by (simp add: uminus_Sup image_image)
   556 
   557 end
   558 
   559 class complete_linorder = linorder + complete_lattice
   560 begin
   561 
   562 lemma dual_complete_linorder:
   563   "class.complete_linorder Sup Inf sup (op \<ge>) (op >) inf \<top> \<bottom>"
   564   by (rule class.complete_linorder.intro, rule dual_complete_lattice, rule dual_linorder)
   565 
   566 lemma complete_linorder_inf_min: "inf = min"
   567   by (auto intro: antisym simp add: min_def fun_eq_iff)
   568 
   569 lemma complete_linorder_sup_max: "sup = max"
   570   by (auto intro: antisym simp add: max_def fun_eq_iff)
   571 
   572 lemma Inf_less_iff: "\<Sqinter>S < a \<longleftrightarrow> (\<exists>x\<in>S. x < a)"
   573   by (simp add: not_le [symmetric] le_Inf_iff)
   574 
   575 lemma INF_less_iff: "(\<Sqinter>i\<in>A. f i) < a \<longleftrightarrow> (\<exists>x\<in>A. f x < a)"
   576   by (simp add: Inf_less_iff [of "f ` A"])
   577 
   578 lemma less_Sup_iff: "a < \<Squnion>S \<longleftrightarrow> (\<exists>x\<in>S. a < x)"
   579   by (simp add: not_le [symmetric] Sup_le_iff)
   580 
   581 lemma less_SUP_iff: "a < (\<Squnion>i\<in>A. f i) \<longleftrightarrow> (\<exists>x\<in>A. a < f x)"
   582   by (simp add: less_Sup_iff [of _ "f ` A"])
   583 
   584 lemma Sup_eq_top_iff [simp]: "\<Squnion>A = \<top> \<longleftrightarrow> (\<forall>x<\<top>. \<exists>i\<in>A. x < i)"
   585 proof
   586   assume *: "\<Squnion>A = \<top>"
   587   show "(\<forall>x<\<top>. \<exists>i\<in>A. x < i)"
   588     unfolding * [symmetric]
   589   proof (intro allI impI)
   590     fix x
   591     assume "x < \<Squnion>A"
   592     then show "\<exists>i\<in>A. x < i"
   593       by (simp add: less_Sup_iff)
   594   qed
   595 next
   596   assume *: "\<forall>x<\<top>. \<exists>i\<in>A. x < i"
   597   show "\<Squnion>A = \<top>"
   598   proof (rule ccontr)
   599     assume "\<Squnion>A \<noteq> \<top>"
   600     with top_greatest [of "\<Squnion>A"] have "\<Squnion>A < \<top>"
   601       unfolding le_less by auto
   602     with * have "\<Squnion>A < \<Squnion>A"
   603       unfolding less_Sup_iff by auto
   604     then show False by auto
   605   qed
   606 qed
   607 
   608 lemma SUP_eq_top_iff [simp]: "(\<Squnion>i\<in>A. f i) = \<top> \<longleftrightarrow> (\<forall>x<\<top>. \<exists>i\<in>A. x < f i)"
   609   using Sup_eq_top_iff [of "f ` A"] by simp
   610 
   611 lemma Inf_eq_bot_iff [simp]: "\<Sqinter>A = \<bottom> \<longleftrightarrow> (\<forall>x>\<bottom>. \<exists>i\<in>A. i < x)"
   612   using dual_complete_linorder
   613   by (rule complete_linorder.Sup_eq_top_iff)
   614 
   615 lemma INF_eq_bot_iff [simp]: "(\<Sqinter>i\<in>A. f i) = \<bottom> \<longleftrightarrow> (\<forall>x>\<bottom>. \<exists>i\<in>A. f i < x)"
   616   using Inf_eq_bot_iff [of "f ` A"] by simp
   617 
   618 lemma Inf_le_iff: "\<Sqinter>A \<le> x \<longleftrightarrow> (\<forall>y>x. \<exists>a\<in>A. y > a)"
   619 proof safe
   620   fix y
   621   assume "x \<ge> \<Sqinter>A" "y > x"
   622   then have "y > \<Sqinter>A" by auto
   623   then show "\<exists>a\<in>A. y > a"
   624     unfolding Inf_less_iff .
   625 qed (auto elim!: allE[of _ "\<Sqinter>A"] simp add: not_le[symmetric] Inf_lower)
   626 
   627 lemma INF_le_iff: "INFIMUM A f \<le> x \<longleftrightarrow> (\<forall>y>x. \<exists>i\<in>A. y > f i)"
   628   using Inf_le_iff [of "f ` A"] by simp
   629 
   630 lemma le_Sup_iff: "x \<le> \<Squnion>A \<longleftrightarrow> (\<forall>y<x. \<exists>a\<in>A. y < a)"
   631 proof safe
   632   fix y
   633   assume "x \<le> \<Squnion>A" "y < x"
   634   then have "y < \<Squnion>A" by auto
   635   then show "\<exists>a\<in>A. y < a"
   636     unfolding less_Sup_iff .
   637 qed (auto elim!: allE[of _ "\<Squnion>A"] simp add: not_le[symmetric] Sup_upper)
   638 
   639 lemma le_SUP_iff: "x \<le> SUPREMUM A f \<longleftrightarrow> (\<forall>y<x. \<exists>i\<in>A. y < f i)"
   640   using le_Sup_iff [of _ "f ` A"] by simp
   641 
   642 subclass complete_distrib_lattice
   643 proof
   644   fix a and B
   645   show "a \<squnion> \<Sqinter>B = (\<Sqinter>b\<in>B. a \<squnion> b)" and "a \<sqinter> \<Squnion>B = (\<Squnion>b\<in>B. a \<sqinter> b)"
   646     by (safe intro!: INF_eqI [symmetric] sup_mono Inf_lower SUP_eqI [symmetric] inf_mono Sup_upper)
   647       (auto simp: not_less [symmetric] Inf_less_iff less_Sup_iff
   648         le_max_iff_disj complete_linorder_sup_max min_le_iff_disj complete_linorder_inf_min)
   649 qed
   650 
   651 end
   652 
   653 
   654 subsection \<open>Complete lattice on @{typ bool}\<close>
   655 
   656 instantiation bool :: complete_lattice
   657 begin
   658 
   659 definition [simp, code]: "\<Sqinter>A \<longleftrightarrow> False \<notin> A"
   660 
   661 definition [simp, code]: "\<Squnion>A \<longleftrightarrow> True \<in> A"
   662 
   663 instance
   664   by standard (auto intro: bool_induct)
   665 
   666 end
   667 
   668 lemma not_False_in_image_Ball [simp]: "False \<notin> P ` A \<longleftrightarrow> Ball A P"
   669   by auto
   670 
   671 lemma True_in_image_Bex [simp]: "True \<in> P ` A \<longleftrightarrow> Bex A P"
   672   by auto
   673 
   674 lemma INF_bool_eq [simp]: "INFIMUM = Ball"
   675   by (simp add: fun_eq_iff)
   676 
   677 lemma SUP_bool_eq [simp]: "SUPREMUM = Bex"
   678   by (simp add: fun_eq_iff)
   679 
   680 instance bool :: complete_boolean_algebra
   681   by standard (auto intro: bool_induct)
   682 
   683 
   684 subsection \<open>Complete lattice on @{typ "_ \<Rightarrow> _"}\<close>
   685 
   686 instantiation "fun" :: (type, Inf) Inf
   687 begin
   688 
   689 definition "\<Sqinter>A = (\<lambda>x. \<Sqinter>f\<in>A. f x)"
   690 
   691 lemma Inf_apply [simp, code]: "(\<Sqinter>A) x = (\<Sqinter>f\<in>A. f x)"
   692   by (simp add: Inf_fun_def)
   693 
   694 instance ..
   695 
   696 end
   697 
   698 instantiation "fun" :: (type, Sup) Sup
   699 begin
   700 
   701 definition "\<Squnion>A = (\<lambda>x. \<Squnion>f\<in>A. f x)"
   702 
   703 lemma Sup_apply [simp, code]: "(\<Squnion>A) x = (\<Squnion>f\<in>A. f x)"
   704   by (simp add: Sup_fun_def)
   705 
   706 instance ..
   707 
   708 end
   709 
   710 instantiation "fun" :: (type, complete_lattice) complete_lattice
   711 begin
   712 
   713 instance
   714   by standard (auto simp add: le_fun_def intro: INF_lower INF_greatest SUP_upper SUP_least)
   715 
   716 end
   717 
   718 lemma INF_apply [simp]: "(\<Sqinter>y\<in>A. f y) x = (\<Sqinter>y\<in>A. f y x)"
   719   using Inf_apply [of "f ` A"] by (simp add: comp_def)
   720 
   721 lemma SUP_apply [simp]: "(\<Squnion>y\<in>A. f y) x = (\<Squnion>y\<in>A. f y x)"
   722   using Sup_apply [of "f ` A"] by (simp add: comp_def)
   723 
   724 instance "fun" :: (type, complete_distrib_lattice) complete_distrib_lattice
   725   by standard (auto simp add: inf_Sup sup_Inf fun_eq_iff image_image)
   726 
   727 instance "fun" :: (type, complete_boolean_algebra) complete_boolean_algebra ..
   728 
   729 
   730 subsection \<open>Complete lattice on unary and binary predicates\<close>
   731 
   732 lemma Inf1_I: "(\<And>P. P \<in> A \<Longrightarrow> P a) \<Longrightarrow> (\<Sqinter>A) a"
   733   by auto
   734 
   735 lemma INF1_I: "(\<And>x. x \<in> A \<Longrightarrow> B x b) \<Longrightarrow> (\<Sqinter>x\<in>A. B x) b"
   736   by simp
   737 
   738 lemma INF2_I: "(\<And>x. x \<in> A \<Longrightarrow> B x b c) \<Longrightarrow> (\<Sqinter>x\<in>A. B x) b c"
   739   by simp
   740 
   741 lemma Inf2_I: "(\<And>r. r \<in> A \<Longrightarrow> r a b) \<Longrightarrow> (\<Sqinter>A) a b"
   742   by auto
   743 
   744 lemma Inf1_D: "(\<Sqinter>A) a \<Longrightarrow> P \<in> A \<Longrightarrow> P a"
   745   by auto
   746 
   747 lemma INF1_D: "(\<Sqinter>x\<in>A. B x) b \<Longrightarrow> a \<in> A \<Longrightarrow> B a b"
   748   by simp
   749 
   750 lemma Inf2_D: "(\<Sqinter>A) a b \<Longrightarrow> r \<in> A \<Longrightarrow> r a b"
   751   by auto
   752 
   753 lemma INF2_D: "(\<Sqinter>x\<in>A. B x) b c \<Longrightarrow> a \<in> A \<Longrightarrow> B a b c"
   754   by simp
   755 
   756 lemma Inf1_E:
   757   assumes "(\<Sqinter>A) a"
   758   obtains "P a" | "P \<notin> A"
   759   using assms by auto
   760 
   761 lemma INF1_E:
   762   assumes "(\<Sqinter>x\<in>A. B x) b"
   763   obtains "B a b" | "a \<notin> A"
   764   using assms by auto
   765 
   766 lemma Inf2_E:
   767   assumes "(\<Sqinter>A) a b"
   768   obtains "r a b" | "r \<notin> A"
   769   using assms by auto
   770 
   771 lemma INF2_E:
   772   assumes "(\<Sqinter>x\<in>A. B x) b c"
   773   obtains "B a b c" | "a \<notin> A"
   774   using assms by auto
   775 
   776 lemma Sup1_I: "P \<in> A \<Longrightarrow> P a \<Longrightarrow> (\<Squnion>A) a"
   777   by auto
   778 
   779 lemma SUP1_I: "a \<in> A \<Longrightarrow> B a b \<Longrightarrow> (\<Squnion>x\<in>A. B x) b"
   780   by auto
   781 
   782 lemma Sup2_I: "r \<in> A \<Longrightarrow> r a b \<Longrightarrow> (\<Squnion>A) a b"
   783   by auto
   784 
   785 lemma SUP2_I: "a \<in> A \<Longrightarrow> B a b c \<Longrightarrow> (\<Squnion>x\<in>A. B x) b c"
   786   by auto
   787 
   788 lemma Sup1_E:
   789   assumes "(\<Squnion>A) a"
   790   obtains P where "P \<in> A" and "P a"
   791   using assms by auto
   792 
   793 lemma SUP1_E:
   794   assumes "(\<Squnion>x\<in>A. B x) b"
   795   obtains x where "x \<in> A" and "B x b"
   796   using assms by auto
   797 
   798 lemma Sup2_E:
   799   assumes "(\<Squnion>A) a b"
   800   obtains r where "r \<in> A" "r a b"
   801   using assms by auto
   802 
   803 lemma SUP2_E:
   804   assumes "(\<Squnion>x\<in>A. B x) b c"
   805   obtains x where "x \<in> A" "B x b c"
   806   using assms by auto
   807 
   808 
   809 subsection \<open>Complete lattice on @{typ "_ set"}\<close>
   810 
   811 instantiation "set" :: (type) complete_lattice
   812 begin
   813 
   814 definition "\<Sqinter>A = {x. \<Sqinter>((\<lambda>B. x \<in> B) ` A)}"
   815 
   816 definition "\<Squnion>A = {x. \<Squnion>((\<lambda>B. x \<in> B) ` A)}"
   817 
   818 instance
   819   by standard (auto simp add: less_eq_set_def Inf_set_def Sup_set_def le_fun_def)
   820 
   821 end
   822 
   823 instance "set" :: (type) complete_boolean_algebra
   824   by standard (auto simp add: Inf_set_def Sup_set_def image_def)
   825 
   826 
   827 subsubsection \<open>Inter\<close>
   828 
   829 abbreviation Inter :: "'a set set \<Rightarrow> 'a set"  ("\<Inter>_" [900] 900)
   830   where "\<Inter>S \<equiv> \<Sqinter>S"
   831 
   832 lemma Inter_eq: "\<Inter>A = {x. \<forall>B \<in> A. x \<in> B}"
   833 proof (rule set_eqI)
   834   fix x
   835   have "(\<forall>Q\<in>{P. \<exists>B\<in>A. P \<longleftrightarrow> x \<in> B}. Q) \<longleftrightarrow> (\<forall>B\<in>A. x \<in> B)"
   836     by auto
   837   then show "x \<in> \<Inter>A \<longleftrightarrow> x \<in> {x. \<forall>B \<in> A. x \<in> B}"
   838     by (simp add: Inf_set_def image_def)
   839 qed
   840 
   841 lemma Inter_iff [simp]: "A \<in> \<Inter>C \<longleftrightarrow> (\<forall>X\<in>C. A \<in> X)"
   842   by (unfold Inter_eq) blast
   843 
   844 lemma InterI [intro!]: "(\<And>X. X \<in> C \<Longrightarrow> A \<in> X) \<Longrightarrow> A \<in> \<Inter>C"
   845   by (simp add: Inter_eq)
   846 
   847 text \<open>
   848   \<^medskip> A ``destruct'' rule -- every @{term X} in @{term C}
   849   contains @{term A} as an element, but @{prop "A \<in> X"} can hold when
   850   @{prop "X \<in> C"} does not!  This rule is analogous to \<open>spec\<close>.
   851 \<close>
   852 
   853 lemma InterD [elim, Pure.elim]: "A \<in> \<Inter>C \<Longrightarrow> X \<in> C \<Longrightarrow> A \<in> X"
   854   by auto
   855 
   856 lemma InterE [elim]: "A \<in> \<Inter>C \<Longrightarrow> (X \<notin> C \<Longrightarrow> R) \<Longrightarrow> (A \<in> X \<Longrightarrow> R) \<Longrightarrow> R"
   857   \<comment> \<open>``Classical'' elimination rule -- does not require proving
   858     @{prop "X \<in> C"}.\<close>
   859   unfolding Inter_eq by blast
   860 
   861 lemma Inter_lower: "B \<in> A \<Longrightarrow> \<Inter>A \<subseteq> B"
   862   by (fact Inf_lower)
   863 
   864 lemma Inter_subset: "(\<And>X. X \<in> A \<Longrightarrow> X \<subseteq> B) \<Longrightarrow> A \<noteq> {} \<Longrightarrow> \<Inter>A \<subseteq> B"
   865   by (fact Inf_less_eq)
   866 
   867 lemma Inter_greatest: "(\<And>X. X \<in> A \<Longrightarrow> C \<subseteq> X) \<Longrightarrow> C \<subseteq> \<Inter>A"
   868   by (fact Inf_greatest)
   869 
   870 lemma Inter_empty: "\<Inter>{} = UNIV"
   871   by (fact Inf_empty) (* already simp *)
   872 
   873 lemma Inter_UNIV: "\<Inter>UNIV = {}"
   874   by (fact Inf_UNIV) (* already simp *)
   875 
   876 lemma Inter_insert: "\<Inter>(insert a B) = a \<inter> \<Inter>B"
   877   by (fact Inf_insert) (* already simp *)
   878 
   879 lemma Inter_Un_subset: "\<Inter>A \<union> \<Inter>B \<subseteq> \<Inter>(A \<inter> B)"
   880   by (fact less_eq_Inf_inter)
   881 
   882 lemma Inter_Un_distrib: "\<Inter>(A \<union> B) = \<Inter>A \<inter> \<Inter>B"
   883   by (fact Inf_union_distrib)
   884 
   885 lemma Inter_UNIV_conv [simp]:
   886   "\<Inter>A = UNIV \<longleftrightarrow> (\<forall>x\<in>A. x = UNIV)"
   887   "UNIV = \<Inter>A \<longleftrightarrow> (\<forall>x\<in>A. x = UNIV)"
   888   by (fact Inf_top_conv)+
   889 
   890 lemma Inter_anti_mono: "B \<subseteq> A \<Longrightarrow> \<Inter>A \<subseteq> \<Inter>B"
   891   by (fact Inf_superset_mono)
   892 
   893 
   894 subsubsection \<open>Intersections of families\<close>
   895 
   896 abbreviation INTER :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set"
   897   where "INTER \<equiv> INFIMUM"
   898 
   899 text \<open>
   900   Note: must use name @{const INTER} here instead of \<open>INT\<close>
   901   to allow the following syntax coexist with the plain constant name.
   902 \<close>
   903 
   904 syntax (ASCII)
   905   "_INTER1"     :: "pttrns \<Rightarrow> 'b set \<Rightarrow> 'b set"           ("(3INT _./ _)" [0, 10] 10)
   906   "_INTER"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> 'b set"  ("(3INT _:_./ _)" [0, 0, 10] 10)
   907 
   908 syntax (latex output)
   909   "_INTER1"     :: "pttrns \<Rightarrow> 'b set \<Rightarrow> 'b set"           ("(3\<Inter>(\<open>unbreakable\<close>\<^bsub>_\<^esub>)/ _)" [0, 10] 10)
   910   "_INTER"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> 'b set"  ("(3\<Inter>(\<open>unbreakable\<close>\<^bsub>_\<in>_\<^esub>)/ _)" [0, 0, 10] 10)
   911 
   912 syntax
   913   "_INTER1"     :: "pttrns \<Rightarrow> 'b set \<Rightarrow> 'b set"           ("(3\<Inter>_./ _)" [0, 10] 10)
   914   "_INTER"      :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'b set \<Rightarrow> 'b set"  ("(3\<Inter>_\<in>_./ _)" [0, 0, 10] 10)
   915 
   916 translations
   917   "\<Inter>x y. B"  \<rightleftharpoons> "\<Inter>x. \<Inter>y. B"
   918   "\<Inter>x. B"    \<rightleftharpoons> "CONST INTER CONST UNIV (\<lambda>x. B)"
   919   "\<Inter>x. B"    \<rightleftharpoons> "\<Inter>x \<in> CONST UNIV. B"
   920   "\<Inter>x\<in>A. B"  \<rightleftharpoons> "CONST INTER A (\<lambda>x. B)"
   921 
   922 print_translation \<open>
   923   [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax INTER} @{syntax_const "_INTER"}]
   924 \<close> \<comment> \<open>to avoid eta-contraction of body\<close>
   925 
   926 lemma INTER_eq: "(\<Inter>x\<in>A. B x) = {y. \<forall>x\<in>A. y \<in> B x}"
   927   by (auto intro!: INF_eqI)
   928 
   929 lemma INT_iff [simp]: "b \<in> (\<Inter>x\<in>A. B x) \<longleftrightarrow> (\<forall>x\<in>A. b \<in> B x)"
   930   using Inter_iff [of _ "B ` A"] by simp
   931 
   932 lemma INT_I [intro!]: "(\<And>x. x \<in> A \<Longrightarrow> b \<in> B x) \<Longrightarrow> b \<in> (\<Inter>x\<in>A. B x)"
   933   by auto
   934 
   935 lemma INT_D [elim, Pure.elim]: "b \<in> (\<Inter>x\<in>A. B x) \<Longrightarrow> a \<in> A \<Longrightarrow> b \<in> B a"
   936   by auto
   937 
   938 lemma INT_E [elim]: "b \<in> (\<Inter>x\<in>A. B x) \<Longrightarrow> (b \<in> B a \<Longrightarrow> R) \<Longrightarrow> (a \<notin> A \<Longrightarrow> R) \<Longrightarrow> R"
   939   \<comment> \<open>"Classical" elimination -- by the Excluded Middle on @{prop "a\<in>A"}.\<close>
   940   by auto
   941 
   942 lemma Collect_ball_eq: "{x. \<forall>y\<in>A. P x y} = (\<Inter>y\<in>A. {x. P x y})"
   943   by blast
   944 
   945 lemma Collect_all_eq: "{x. \<forall>y. P x y} = (\<Inter>y. {x. P x y})"
   946   by blast
   947 
   948 lemma INT_lower: "a \<in> A \<Longrightarrow> (\<Inter>x\<in>A. B x) \<subseteq> B a"
   949   by (fact INF_lower)
   950 
   951 lemma INT_greatest: "(\<And>x. x \<in> A \<Longrightarrow> C \<subseteq> B x) \<Longrightarrow> C \<subseteq> (\<Inter>x\<in>A. B x)"
   952   by (fact INF_greatest)
   953 
   954 lemma INT_empty: "(\<Inter>x\<in>{}. B x) = UNIV"
   955   by (fact INF_empty)
   956 
   957 lemma INT_absorb: "k \<in> I \<Longrightarrow> A k \<inter> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. A i)"
   958   by (fact INF_absorb)
   959 
   960 lemma INT_subset_iff: "B \<subseteq> (\<Inter>i\<in>I. A i) \<longleftrightarrow> (\<forall>i\<in>I. B \<subseteq> A i)"
   961   by (fact le_INF_iff)
   962 
   963 lemma INT_insert [simp]: "(\<Inter>x \<in> insert a A. B x) = B a \<inter> INTER A B"
   964   by (fact INF_insert)
   965 
   966 lemma INT_Un: "(\<Inter>i \<in> A \<union> B. M i) = (\<Inter>i \<in> A. M i) \<inter> (\<Inter>i\<in>B. M i)"
   967   by (fact INF_union)
   968 
   969 lemma INT_insert_distrib: "u \<in> A \<Longrightarrow> (\<Inter>x\<in>A. insert a (B x)) = insert a (\<Inter>x\<in>A. B x)"
   970   by blast
   971 
   972 lemma INT_constant [simp]: "(\<Inter>y\<in>A. c) = (if A = {} then UNIV else c)"
   973   by (fact INF_constant)
   974 
   975 lemma INTER_UNIV_conv:
   976   "(UNIV = (\<Inter>x\<in>A. B x)) = (\<forall>x\<in>A. B x = UNIV)"
   977   "((\<Inter>x\<in>A. B x) = UNIV) = (\<forall>x\<in>A. B x = UNIV)"
   978   by (fact INF_top_conv)+ (* already simp *)
   979 
   980 lemma INT_bool_eq: "(\<Inter>b. A b) = A True \<inter> A False"
   981   by (fact INF_UNIV_bool_expand)
   982 
   983 lemma INT_anti_mono: "A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<subseteq> g x) \<Longrightarrow> (\<Inter>x\<in>B. f x) \<subseteq> (\<Inter>x\<in>A. g x)"
   984   \<comment> \<open>The last inclusion is POSITIVE!\<close>
   985   by (fact INF_superset_mono)
   986 
   987 lemma Pow_INT_eq: "Pow (\<Inter>x\<in>A. B x) = (\<Inter>x\<in>A. Pow (B x))"
   988   by blast
   989 
   990 lemma vimage_INT: "f -` (\<Inter>x\<in>A. B x) = (\<Inter>x\<in>A. f -` B x)"
   991   by blast
   992 
   993 
   994 subsubsection \<open>Union\<close>
   995 
   996 abbreviation Union :: "'a set set \<Rightarrow> 'a set"  ("\<Union>_" [900] 900)
   997   where "\<Union>S \<equiv> \<Squnion>S"
   998 
   999 lemma Union_eq: "\<Union>A = {x. \<exists>B \<in> A. x \<in> B}"
  1000 proof (rule set_eqI)
  1001   fix x
  1002   have "(\<exists>Q\<in>{P. \<exists>B\<in>A. P \<longleftrightarrow> x \<in> B}. Q) \<longleftrightarrow> (\<exists>B\<in>A. x \<in> B)"
  1003     by auto
  1004   then show "x \<in> \<Union>A \<longleftrightarrow> x \<in> {x. \<exists>B\<in>A. x \<in> B}"
  1005     by (simp add: Sup_set_def image_def)
  1006 qed
  1007 
  1008 lemma Union_iff [simp]: "A \<in> \<Union>C \<longleftrightarrow> (\<exists>X\<in>C. A\<in>X)"
  1009   by (unfold Union_eq) blast
  1010 
  1011 lemma UnionI [intro]: "X \<in> C \<Longrightarrow> A \<in> X \<Longrightarrow> A \<in> \<Union>C"
  1012   \<comment> \<open>The order of the premises presupposes that @{term C} is rigid;
  1013     @{term A} may be flexible.\<close>
  1014   by auto
  1015 
  1016 lemma UnionE [elim!]: "A \<in> \<Union>C \<Longrightarrow> (\<And>X. A \<in> X \<Longrightarrow> X \<in> C \<Longrightarrow> R) \<Longrightarrow> R"
  1017   by auto
  1018 
  1019 lemma Union_upper: "B \<in> A \<Longrightarrow> B \<subseteq> \<Union>A"
  1020   by (fact Sup_upper)
  1021 
  1022 lemma Union_least: "(\<And>X. X \<in> A \<Longrightarrow> X \<subseteq> C) \<Longrightarrow> \<Union>A \<subseteq> C"
  1023   by (fact Sup_least)
  1024 
  1025 lemma Union_empty: "\<Union>{} = {}"
  1026   by (fact Sup_empty) (* already simp *)
  1027 
  1028 lemma Union_UNIV: "\<Union>UNIV = UNIV"
  1029   by (fact Sup_UNIV) (* already simp *)
  1030 
  1031 lemma Union_insert: "\<Union>insert a B = a \<union> \<Union>B"
  1032   by (fact Sup_insert) (* already simp *)
  1033 
  1034 lemma Union_Un_distrib [simp]: "\<Union>(A \<union> B) = \<Union>A \<union> \<Union>B"
  1035   by (fact Sup_union_distrib)
  1036 
  1037 lemma Union_Int_subset: "\<Union>(A \<inter> B) \<subseteq> \<Union>A \<inter> \<Union>B"
  1038   by (fact Sup_inter_less_eq)
  1039 
  1040 lemma Union_empty_conv: "(\<Union>A = {}) \<longleftrightarrow> (\<forall>x\<in>A. x = {})"
  1041   by (fact Sup_bot_conv) (* already simp *)
  1042 
  1043 lemma empty_Union_conv: "({} = \<Union>A) \<longleftrightarrow> (\<forall>x\<in>A. x = {})"
  1044   by (fact Sup_bot_conv) (* already simp *)
  1045 
  1046 lemma subset_Pow_Union: "A \<subseteq> Pow (\<Union>A)"
  1047   by blast
  1048 
  1049 lemma Union_Pow_eq [simp]: "\<Union>(Pow A) = A"
  1050   by blast
  1051 
  1052 lemma Union_mono: "A \<subseteq> B \<Longrightarrow> \<Union>A \<subseteq> \<Union>B"
  1053   by (fact Sup_subset_mono)
  1054 
  1055 lemma Union_subsetI: "(\<And>x. x \<in> A \<Longrightarrow> \<exists>y. y \<in> B \<and> x \<subseteq> y) \<Longrightarrow> \<Union>A \<subseteq> \<Union>B"
  1056   by blast
  1057 
  1058 lemma disjnt_inj_on_iff:
  1059      "\<lbrakk>inj_on f (\<Union>\<A>); X \<in> \<A>; Y \<in> \<A>\<rbrakk> \<Longrightarrow> disjnt (f ` X) (f ` Y) \<longleftrightarrow> disjnt X Y"
  1060   apply (auto simp: disjnt_def)
  1061   using inj_on_eq_iff by fastforce
  1062 
  1063 
  1064 subsubsection \<open>Unions of families\<close>
  1065 
  1066 abbreviation UNION :: "'a set \<Rightarrow> ('a \<Rightarrow> 'b set) \<Rightarrow> 'b set"
  1067   where "UNION \<equiv> SUPREMUM"
  1068 
  1069 text \<open>
  1070   Note: must use name @{const UNION} here instead of \<open>UN\<close>
  1071   to allow the following syntax coexist with the plain constant name.
  1072 \<close>
  1073 
  1074 syntax (ASCII)
  1075   "_UNION1"     :: "pttrns => 'b set => 'b set"           ("(3UN _./ _)" [0, 10] 10)
  1076   "_UNION"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3UN _:_./ _)" [0, 0, 10] 10)
  1077 
  1078 syntax (latex output)
  1079   "_UNION1"     :: "pttrns => 'b set => 'b set"           ("(3\<Union>(\<open>unbreakable\<close>\<^bsub>_\<^esub>)/ _)" [0, 10] 10)
  1080   "_UNION"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3\<Union>(\<open>unbreakable\<close>\<^bsub>_\<in>_\<^esub>)/ _)" [0, 0, 10] 10)
  1081 
  1082 syntax
  1083   "_UNION1"     :: "pttrns => 'b set => 'b set"           ("(3\<Union>_./ _)" [0, 10] 10)
  1084   "_UNION"      :: "pttrn => 'a set => 'b set => 'b set"  ("(3\<Union>_\<in>_./ _)" [0, 0, 10] 10)
  1085 
  1086 translations
  1087   "\<Union>x y. B"   \<rightleftharpoons> "\<Union>x. \<Union>y. B"
  1088   "\<Union>x. B"     \<rightleftharpoons> "CONST UNION CONST UNIV (\<lambda>x. B)"
  1089   "\<Union>x. B"     \<rightleftharpoons> "\<Union>x \<in> CONST UNIV. B"
  1090   "\<Union>x\<in>A. B"   \<rightleftharpoons> "CONST UNION A (\<lambda>x. B)"
  1091 
  1092 text \<open>
  1093   Note the difference between ordinary syntax of indexed
  1094   unions and intersections (e.g.\ \<open>\<Union>a\<^sub>1\<in>A\<^sub>1. B\<close>)
  1095   and their \LaTeX\ rendition: @{term"\<Union>a\<^sub>1\<in>A\<^sub>1. B"}.
  1096 \<close>
  1097 
  1098 print_translation \<open>
  1099   [Syntax_Trans.preserve_binder_abs2_tr' @{const_syntax UNION} @{syntax_const "_UNION"}]
  1100 \<close> \<comment> \<open>to avoid eta-contraction of body\<close>
  1101 
  1102 lemma UNION_eq: "(\<Union>x\<in>A. B x) = {y. \<exists>x\<in>A. y \<in> B x}"
  1103   by (auto intro!: SUP_eqI)
  1104 
  1105 lemma bind_UNION [code]: "Set.bind A f = UNION A f"
  1106   by (simp add: bind_def UNION_eq)
  1107 
  1108 lemma member_bind [simp]: "x \<in> Set.bind P f \<longleftrightarrow> x \<in> UNION P f "
  1109   by (simp add: bind_UNION)
  1110 
  1111 lemma Union_SetCompr_eq: "\<Union>{f x| x. P x} = {a. \<exists>x. P x \<and> a \<in> f x}"
  1112   by blast
  1113 
  1114 lemma UN_iff [simp]: "b \<in> (\<Union>x\<in>A. B x) \<longleftrightarrow> (\<exists>x\<in>A. b \<in> B x)"
  1115   using Union_iff [of _ "B ` A"] by simp
  1116 
  1117 lemma UN_I [intro]: "a \<in> A \<Longrightarrow> b \<in> B a \<Longrightarrow> b \<in> (\<Union>x\<in>A. B x)"
  1118   \<comment> \<open>The order of the premises presupposes that @{term A} is rigid;
  1119     @{term b} may be flexible.\<close>
  1120   by auto
  1121 
  1122 lemma UN_E [elim!]: "b \<in> (\<Union>x\<in>A. B x) \<Longrightarrow> (\<And>x. x\<in>A \<Longrightarrow> b \<in> B x \<Longrightarrow> R) \<Longrightarrow> R"
  1123   by auto
  1124 
  1125 lemma UN_upper: "a \<in> A \<Longrightarrow> B a \<subseteq> (\<Union>x\<in>A. B x)"
  1126   by (fact SUP_upper)
  1127 
  1128 lemma UN_least: "(\<And>x. x \<in> A \<Longrightarrow> B x \<subseteq> C) \<Longrightarrow> (\<Union>x\<in>A. B x) \<subseteq> C"
  1129   by (fact SUP_least)
  1130 
  1131 lemma Collect_bex_eq: "{x. \<exists>y\<in>A. P x y} = (\<Union>y\<in>A. {x. P x y})"
  1132   by blast
  1133 
  1134 lemma UN_insert_distrib: "u \<in> A \<Longrightarrow> (\<Union>x\<in>A. insert a (B x)) = insert a (\<Union>x\<in>A. B x)"
  1135   by blast
  1136 
  1137 lemma UN_empty: "(\<Union>x\<in>{}. B x) = {}"
  1138   by (fact SUP_empty)
  1139 
  1140 lemma UN_empty2: "(\<Union>x\<in>A. {}) = {}"
  1141   by (fact SUP_bot) (* already simp *)
  1142 
  1143 lemma UN_absorb: "k \<in> I \<Longrightarrow> A k \<union> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. A i)"
  1144   by (fact SUP_absorb)
  1145 
  1146 lemma UN_insert [simp]: "(\<Union>x\<in>insert a A. B x) = B a \<union> UNION A B"
  1147   by (fact SUP_insert)
  1148 
  1149 lemma UN_Un [simp]: "(\<Union>i \<in> A \<union> B. M i) = (\<Union>i\<in>A. M i) \<union> (\<Union>i\<in>B. M i)"
  1150   by (fact SUP_union)
  1151 
  1152 lemma UN_UN_flatten: "(\<Union>x \<in> (\<Union>y\<in>A. B y). C x) = (\<Union>y\<in>A. \<Union>x\<in>B y. C x)"
  1153   by blast
  1154 
  1155 lemma UN_subset_iff: "((\<Union>i\<in>I. A i) \<subseteq> B) = (\<forall>i\<in>I. A i \<subseteq> B)"
  1156   by (fact SUP_le_iff)
  1157 
  1158 lemma UN_constant [simp]: "(\<Union>y\<in>A. c) = (if A = {} then {} else c)"
  1159   by (fact SUP_constant)
  1160 
  1161 lemma image_Union: "f ` \<Union>S = (\<Union>x\<in>S. f ` x)"
  1162   by blast
  1163 
  1164 lemma UNION_empty_conv:
  1165   "{} = (\<Union>x\<in>A. B x) \<longleftrightarrow> (\<forall>x\<in>A. B x = {})"
  1166   "(\<Union>x\<in>A. B x) = {} \<longleftrightarrow> (\<forall>x\<in>A. B x = {})"
  1167   by (fact SUP_bot_conv)+ (* already simp *)
  1168 
  1169 lemma Collect_ex_eq: "{x. \<exists>y. P x y} = (\<Union>y. {x. P x y})"
  1170   by blast
  1171 
  1172 lemma ball_UN: "(\<forall>z \<in> UNION A B. P z) \<longleftrightarrow> (\<forall>x\<in>A. \<forall>z \<in> B x. P z)"
  1173   by blast
  1174 
  1175 lemma bex_UN: "(\<exists>z \<in> UNION A B. P z) \<longleftrightarrow> (\<exists>x\<in>A. \<exists>z\<in>B x. P z)"
  1176   by blast
  1177 
  1178 lemma Un_eq_UN: "A \<union> B = (\<Union>b. if b then A else B)"
  1179   by safe (auto simp add: if_split_mem2)
  1180 
  1181 lemma UN_bool_eq: "(\<Union>b. A b) = (A True \<union> A False)"
  1182   by (fact SUP_UNIV_bool_expand)
  1183 
  1184 lemma UN_Pow_subset: "(\<Union>x\<in>A. Pow (B x)) \<subseteq> Pow (\<Union>x\<in>A. B x)"
  1185   by blast
  1186 
  1187 lemma UN_mono:
  1188   "A \<subseteq> B \<Longrightarrow> (\<And>x. x \<in> A \<Longrightarrow> f x \<subseteq> g x) \<Longrightarrow>
  1189     (\<Union>x\<in>A. f x) \<subseteq> (\<Union>x\<in>B. g x)"
  1190   by (fact SUP_subset_mono)
  1191 
  1192 lemma vimage_Union: "f -` (\<Union>A) = (\<Union>X\<in>A. f -` X)"
  1193   by blast
  1194 
  1195 lemma vimage_UN: "f -` (\<Union>x\<in>A. B x) = (\<Union>x\<in>A. f -` B x)"
  1196   by blast
  1197 
  1198 lemma vimage_eq_UN: "f -` B = (\<Union>y\<in>B. f -` {y})"
  1199   \<comment> \<open>NOT suitable for rewriting\<close>
  1200   by blast
  1201 
  1202 lemma image_UN: "f ` UNION A B = (\<Union>x\<in>A. f ` B x)"
  1203   by blast
  1204 
  1205 lemma UN_singleton [simp]: "(\<Union>x\<in>A. {x}) = A"
  1206   by blast
  1207 
  1208 lemma inj_on_image: "inj_on f (\<Union>A) \<Longrightarrow> inj_on (op ` f) A"
  1209   unfolding inj_on_def by blast
  1210 
  1211 
  1212 subsubsection \<open>Distributive laws\<close>
  1213 
  1214 lemma Int_Union: "A \<inter> \<Union>B = (\<Union>C\<in>B. A \<inter> C)"
  1215   by (fact inf_Sup)
  1216 
  1217 lemma Un_Inter: "A \<union> \<Inter>B = (\<Inter>C\<in>B. A \<union> C)"
  1218   by (fact sup_Inf)
  1219 
  1220 lemma Int_Union2: "\<Union>B \<inter> A = (\<Union>C\<in>B. C \<inter> A)"
  1221   by (fact Sup_inf)
  1222 
  1223 lemma INT_Int_distrib: "(\<Inter>i\<in>I. A i \<inter> B i) = (\<Inter>i\<in>I. A i) \<inter> (\<Inter>i\<in>I. B i)"
  1224   by (rule sym) (rule INF_inf_distrib)
  1225 
  1226 lemma UN_Un_distrib: "(\<Union>i\<in>I. A i \<union> B i) = (\<Union>i\<in>I. A i) \<union> (\<Union>i\<in>I. B i)"
  1227   by (rule sym) (rule SUP_sup_distrib)
  1228 
  1229 lemma Int_Inter_image: "(\<Inter>x\<in>C. A x \<inter> B x) = \<Inter>(A ` C) \<inter> \<Inter>(B ` C)"  (* FIXME drop *)
  1230   by (simp add: INT_Int_distrib)
  1231 
  1232 lemma Un_Union_image: "(\<Union>x\<in>C. A x \<union> B x) = \<Union>(A ` C) \<union> \<Union>(B ` C)"  (* FIXME drop *)
  1233   \<comment> \<open>Devlin, Fundamentals of Contemporary Set Theory, page 12, exercise 5:\<close>
  1234   \<comment> \<open>Union of a family of unions\<close>
  1235   by (simp add: UN_Un_distrib)
  1236 
  1237 lemma Un_INT_distrib: "B \<union> (\<Inter>i\<in>I. A i) = (\<Inter>i\<in>I. B \<union> A i)"
  1238   by (fact sup_INF)
  1239 
  1240 lemma Int_UN_distrib: "B \<inter> (\<Union>i\<in>I. A i) = (\<Union>i\<in>I. B \<inter> A i)"
  1241   \<comment> \<open>Halmos, Naive Set Theory, page 35.\<close>
  1242   by (fact inf_SUP)
  1243 
  1244 lemma Int_UN_distrib2: "(\<Union>i\<in>I. A i) \<inter> (\<Union>j\<in>J. B j) = (\<Union>i\<in>I. \<Union>j\<in>J. A i \<inter> B j)"
  1245   by (fact SUP_inf_distrib2)
  1246 
  1247 lemma Un_INT_distrib2: "(\<Inter>i\<in>I. A i) \<union> (\<Inter>j\<in>J. B j) = (\<Inter>i\<in>I. \<Inter>j\<in>J. A i \<union> B j)"
  1248   by (fact INF_sup_distrib2)
  1249 
  1250 lemma Union_disjoint: "(\<Union>C \<inter> A = {}) \<longleftrightarrow> (\<forall>B\<in>C. B \<inter> A = {})"
  1251   by (fact Sup_inf_eq_bot_iff)
  1252 
  1253 lemma SUP_UNION: "(SUP x:(UN y:A. g y). f x) = (SUP y:A. SUP x:g y. f x :: _ :: complete_lattice)"
  1254   by (rule order_antisym) (blast intro: SUP_least SUP_upper2)+
  1255 
  1256 
  1257 subsection \<open>Injections and bijections\<close>
  1258 
  1259 lemma inj_on_Inter: "S \<noteq> {} \<Longrightarrow> (\<And>A. A \<in> S \<Longrightarrow> inj_on f A) \<Longrightarrow> inj_on f (\<Inter>S)"
  1260   unfolding inj_on_def by blast
  1261 
  1262 lemma inj_on_INTER: "I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> inj_on f (A i)) \<Longrightarrow> inj_on f (\<Inter>i \<in> I. A i)"
  1263   unfolding inj_on_def by safe simp
  1264 
  1265 lemma inj_on_UNION_chain:
  1266   assumes chain: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> A i \<le> A j \<or> A j \<le> A i"
  1267     and inj: "\<And>i. i \<in> I \<Longrightarrow> inj_on f (A i)"
  1268   shows "inj_on f (\<Union>i \<in> I. A i)"
  1269 proof -
  1270   have "x = y"
  1271     if *: "i \<in> I" "j \<in> I"
  1272     and **: "x \<in> A i" "y \<in> A j"
  1273     and ***: "f x = f y"
  1274     for i j x y
  1275     using chain [OF *]
  1276   proof
  1277     assume "A i \<le> A j"
  1278     with ** have "x \<in> A j" by auto
  1279     with inj * ** *** show ?thesis
  1280       by (auto simp add: inj_on_def)
  1281   next
  1282     assume "A j \<le> A i"
  1283     with ** have "y \<in> A i" by auto
  1284     with inj * ** *** show ?thesis
  1285       by (auto simp add: inj_on_def)
  1286   qed
  1287   then show ?thesis
  1288     by (unfold inj_on_def UNION_eq) auto
  1289 qed
  1290 
  1291 lemma bij_betw_UNION_chain:
  1292   assumes chain: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> A i \<le> A j \<or> A j \<le> A i"
  1293     and bij: "\<And>i. i \<in> I \<Longrightarrow> bij_betw f (A i) (A' i)"
  1294   shows "bij_betw f (\<Union>i \<in> I. A i) (\<Union>i \<in> I. A' i)"
  1295   unfolding bij_betw_def
  1296 proof safe
  1297   have "\<And>i. i \<in> I \<Longrightarrow> inj_on f (A i)"
  1298     using bij bij_betw_def[of f] by auto
  1299   then show "inj_on f (UNION I A)"
  1300     using chain inj_on_UNION_chain[of I A f] by auto
  1301 next
  1302   fix i x
  1303   assume *: "i \<in> I" "x \<in> A i"
  1304   with bij have "f x \<in> A' i"
  1305     by (auto simp: bij_betw_def)
  1306   with * show "f x \<in> UNION I A'" by blast
  1307 next
  1308   fix i x'
  1309   assume *: "i \<in> I" "x' \<in> A' i"
  1310   with bij have "\<exists>x \<in> A i. x' = f x"
  1311     unfolding bij_betw_def by blast
  1312   with * have "\<exists>j \<in> I. \<exists>x \<in> A j. x' = f x"
  1313     by blast
  1314   then show "x' \<in> f ` UNION I A"
  1315     by blast
  1316 qed
  1317 
  1318 (*injectivity's required.  Left-to-right inclusion holds even if A is empty*)
  1319 lemma image_INT: "inj_on f C \<Longrightarrow> \<forall>x\<in>A. B x \<subseteq> C \<Longrightarrow> j \<in> A \<Longrightarrow> f ` (INTER A B) = (INT x:A. f ` B x)"
  1320   by (auto simp add: inj_on_def) blast
  1321 
  1322 lemma bij_image_INT: "bij f \<Longrightarrow> f ` (INTER A B) = (INT x:A. f ` B x)"
  1323   by (auto simp: bij_def inj_def surj_def) blast
  1324 
  1325 lemma UNION_fun_upd: "UNION J (A(i := B)) = UNION (J - {i}) A \<union> (if i \<in> J then B else {})"
  1326   by (auto simp add: set_eq_iff)
  1327 
  1328 lemma bij_betw_Pow:
  1329   assumes "bij_betw f A B"
  1330   shows "bij_betw (image f) (Pow A) (Pow B)"
  1331 proof -
  1332   from assms have "inj_on f A"
  1333     by (rule bij_betw_imp_inj_on)
  1334   then have "inj_on f (\<Union>Pow A)"
  1335     by simp
  1336   then have "inj_on (image f) (Pow A)"
  1337     by (rule inj_on_image)
  1338   then have "bij_betw (image f) (Pow A) (image f ` Pow A)"
  1339     by (rule inj_on_imp_bij_betw)
  1340   moreover from assms have "f ` A = B"
  1341     by (rule bij_betw_imp_surj_on)
  1342   then have "image f ` Pow A = Pow B"
  1343     by (rule image_Pow_surj)
  1344   ultimately show ?thesis by simp
  1345 qed
  1346 
  1347 
  1348 subsubsection \<open>Complement\<close>
  1349 
  1350 lemma Compl_INT [simp]: "- (\<Inter>x\<in>A. B x) = (\<Union>x\<in>A. -B x)"
  1351   by (fact uminus_INF)
  1352 
  1353 lemma Compl_UN [simp]: "- (\<Union>x\<in>A. B x) = (\<Inter>x\<in>A. -B x)"
  1354   by (fact uminus_SUP)
  1355 
  1356 
  1357 subsubsection \<open>Miniscoping and maxiscoping\<close>
  1358 
  1359 text \<open>\<^medskip> Miniscoping: pushing in quantifiers and big Unions and Intersections.\<close>
  1360 
  1361 lemma UN_simps [simp]:
  1362   "\<And>a B C. (\<Union>x\<in>C. insert a (B x)) = (if C={} then {} else insert a (\<Union>x\<in>C. B x))"
  1363   "\<And>A B C. (\<Union>x\<in>C. A x \<union> B) = ((if C={} then {} else (\<Union>x\<in>C. A x) \<union> B))"
  1364   "\<And>A B C. (\<Union>x\<in>C. A \<union> B x) = ((if C={} then {} else A \<union> (\<Union>x\<in>C. B x)))"
  1365   "\<And>A B C. (\<Union>x\<in>C. A x \<inter> B) = ((\<Union>x\<in>C. A x) \<inter> B)"
  1366   "\<And>A B C. (\<Union>x\<in>C. A \<inter> B x) = (A \<inter>(\<Union>x\<in>C. B x))"
  1367   "\<And>A B C. (\<Union>x\<in>C. A x - B) = ((\<Union>x\<in>C. A x) - B)"
  1368   "\<And>A B C. (\<Union>x\<in>C. A - B x) = (A - (\<Inter>x\<in>C. B x))"
  1369   "\<And>A B. (\<Union>x\<in>\<Union>A. B x) = (\<Union>y\<in>A. \<Union>x\<in>y. B x)"
  1370   "\<And>A B C. (\<Union>z\<in>UNION A B. C z) = (\<Union>x\<in>A. \<Union>z\<in>B x. C z)"
  1371   "\<And>A B f. (\<Union>x\<in>f`A. B x) = (\<Union>a\<in>A. B (f a))"
  1372   by auto
  1373 
  1374 lemma INT_simps [simp]:
  1375   "\<And>A B C. (\<Inter>x\<in>C. A x \<inter> B) = (if C={} then UNIV else (\<Inter>x\<in>C. A x) \<inter> B)"
  1376   "\<And>A B C. (\<Inter>x\<in>C. A \<inter> B x) = (if C={} then UNIV else A \<inter>(\<Inter>x\<in>C. B x))"
  1377   "\<And>A B C. (\<Inter>x\<in>C. A x - B) = (if C={} then UNIV else (\<Inter>x\<in>C. A x) - B)"
  1378   "\<And>A B C. (\<Inter>x\<in>C. A - B x) = (if C={} then UNIV else A - (\<Union>x\<in>C. B x))"
  1379   "\<And>a B C. (\<Inter>x\<in>C. insert a (B x)) = insert a (\<Inter>x\<in>C. B x)"
  1380   "\<And>A B C. (\<Inter>x\<in>C. A x \<union> B) = ((\<Inter>x\<in>C. A x) \<union> B)"
  1381   "\<And>A B C. (\<Inter>x\<in>C. A \<union> B x) = (A \<union> (\<Inter>x\<in>C. B x))"
  1382   "\<And>A B. (\<Inter>x\<in>\<Union>A. B x) = (\<Inter>y\<in>A. \<Inter>x\<in>y. B x)"
  1383   "\<And>A B C. (\<Inter>z\<in>UNION A B. C z) = (\<Inter>x\<in>A. \<Inter>z\<in>B x. C z)"
  1384   "\<And>A B f. (\<Inter>x\<in>f`A. B x) = (\<Inter>a\<in>A. B (f a))"
  1385   by auto
  1386 
  1387 lemma UN_ball_bex_simps [simp]:
  1388   "\<And>A P. (\<forall>x\<in>\<Union>A. P x) \<longleftrightarrow> (\<forall>y\<in>A. \<forall>x\<in>y. P x)"
  1389   "\<And>A B P. (\<forall>x\<in>UNION A B. P x) = (\<forall>a\<in>A. \<forall>x\<in> B a. P x)"
  1390   "\<And>A P. (\<exists>x\<in>\<Union>A. P x) \<longleftrightarrow> (\<exists>y\<in>A. \<exists>x\<in>y. P x)"
  1391   "\<And>A B P. (\<exists>x\<in>UNION A B. P x) \<longleftrightarrow> (\<exists>a\<in>A. \<exists>x\<in>B a. P x)"
  1392   by auto
  1393 
  1394 
  1395 text \<open>\<^medskip> Maxiscoping: pulling out big Unions and Intersections.\<close>
  1396 
  1397 lemma UN_extend_simps:
  1398   "\<And>a B C. insert a (\<Union>x\<in>C. B x) = (if C={} then {a} else (\<Union>x\<in>C. insert a (B x)))"
  1399   "\<And>A B C. (\<Union>x\<in>C. A x) \<union> B = (if C={} then B else (\<Union>x\<in>C. A x \<union> B))"
  1400   "\<And>A B C. A \<union> (\<Union>x\<in>C. B x) = (if C={} then A else (\<Union>x\<in>C. A \<union> B x))"
  1401   "\<And>A B C. ((\<Union>x\<in>C. A x) \<inter> B) = (\<Union>x\<in>C. A x \<inter> B)"
  1402   "\<And>A B C. (A \<inter> (\<Union>x\<in>C. B x)) = (\<Union>x\<in>C. A \<inter> B x)"
  1403   "\<And>A B C. ((\<Union>x\<in>C. A x) - B) = (\<Union>x\<in>C. A x - B)"
  1404   "\<And>A B C. (A - (\<Inter>x\<in>C. B x)) = (\<Union>x\<in>C. A - B x)"
  1405   "\<And>A B. (\<Union>y\<in>A. \<Union>x\<in>y. B x) = (\<Union>x\<in>\<Union>A. B x)"
  1406   "\<And>A B C. (\<Union>x\<in>A. \<Union>z\<in>B x. C z) = (\<Union>z\<in>UNION A B. C z)"
  1407   "\<And>A B f. (\<Union>a\<in>A. B (f a)) = (\<Union>x\<in>f`A. B x)"
  1408   by auto
  1409 
  1410 lemma INT_extend_simps:
  1411   "\<And>A B C. (\<Inter>x\<in>C. A x) \<inter> B = (if C={} then B else (\<Inter>x\<in>C. A x \<inter> B))"
  1412   "\<And>A B C. A \<inter> (\<Inter>x\<in>C. B x) = (if C={} then A else (\<Inter>x\<in>C. A \<inter> B x))"
  1413   "\<And>A B C. (\<Inter>x\<in>C. A x) - B = (if C={} then UNIV - B else (\<Inter>x\<in>C. A x - B))"
  1414   "\<And>A B C. A - (\<Union>x\<in>C. B x) = (if C={} then A else (\<Inter>x\<in>C. A - B x))"
  1415   "\<And>a B C. insert a (\<Inter>x\<in>C. B x) = (\<Inter>x\<in>C. insert a (B x))"
  1416   "\<And>A B C. ((\<Inter>x\<in>C. A x) \<union> B) = (\<Inter>x\<in>C. A x \<union> B)"
  1417   "\<And>A B C. A \<union> (\<Inter>x\<in>C. B x) = (\<Inter>x\<in>C. A \<union> B x)"
  1418   "\<And>A B. (\<Inter>y\<in>A. \<Inter>x\<in>y. B x) = (\<Inter>x\<in>\<Union>A. B x)"
  1419   "\<And>A B C. (\<Inter>x\<in>A. \<Inter>z\<in>B x. C z) = (\<Inter>z\<in>UNION A B. C z)"
  1420   "\<And>A B f. (\<Inter>a\<in>A. B (f a)) = (\<Inter>x\<in>f`A. B x)"
  1421   by auto
  1422 
  1423 text \<open>Finally\<close>
  1424 
  1425 lemmas mem_simps =
  1426   insert_iff empty_iff Un_iff Int_iff Compl_iff Diff_iff
  1427   mem_Collect_eq UN_iff Union_iff INT_iff Inter_iff
  1428   \<comment> \<open>Each of these has ALREADY been added \<open>[simp]\<close> above.\<close>
  1429 
  1430 end