src/HOL/Analysis/Measure_Space.thy
changeset 63627 6ddb43c6b711
parent 63626 44ce6b524ff3
child 63657 3663157ee197
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Measure_Space.thy	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -0,0 +1,3326 @@
     1.4 +(*  Title:      HOL/Analysis/Measure_Space.thy
     1.5 +    Author:     Lawrence C Paulson
     1.6 +    Author:     Johannes Hölzl, TU München
     1.7 +    Author:     Armin Heller, TU München
     1.8 +*)
     1.9 +
    1.10 +section \<open>Measure spaces and their properties\<close>
    1.11 +
    1.12 +theory Measure_Space
    1.13 +imports
    1.14 +  Measurable "~~/src/HOL/Library/Extended_Nonnegative_Real"
    1.15 +begin
    1.16 +
    1.17 +subsection "Relate extended reals and the indicator function"
    1.18 +
    1.19 +lemma suminf_cmult_indicator:
    1.20 +  fixes f :: "nat \<Rightarrow> ennreal"
    1.21 +  assumes "disjoint_family A" "x \<in> A i"
    1.22 +  shows "(\<Sum>n. f n * indicator (A n) x) = f i"
    1.23 +proof -
    1.24 +  have **: "\<And>n. f n * indicator (A n) x = (if n = i then f n else 0 :: ennreal)"
    1.25 +    using \<open>x \<in> A i\<close> assms unfolding disjoint_family_on_def indicator_def by auto
    1.26 +  then have "\<And>n. (\<Sum>j<n. f j * indicator (A j) x) = (if i < n then f i else 0 :: ennreal)"
    1.27 +    by (auto simp: setsum.If_cases)
    1.28 +  moreover have "(SUP n. if i < n then f i else 0) = (f i :: ennreal)"
    1.29 +  proof (rule SUP_eqI)
    1.30 +    fix y :: ennreal assume "\<And>n. n \<in> UNIV \<Longrightarrow> (if i < n then f i else 0) \<le> y"
    1.31 +    from this[of "Suc i"] show "f i \<le> y" by auto
    1.32 +  qed (insert assms, simp)
    1.33 +  ultimately show ?thesis using assms
    1.34 +    by (subst suminf_eq_SUP) (auto simp: indicator_def)
    1.35 +qed
    1.36 +
    1.37 +lemma suminf_indicator:
    1.38 +  assumes "disjoint_family A"
    1.39 +  shows "(\<Sum>n. indicator (A n) x :: ennreal) = indicator (\<Union>i. A i) x"
    1.40 +proof cases
    1.41 +  assume *: "x \<in> (\<Union>i. A i)"
    1.42 +  then obtain i where "x \<in> A i" by auto
    1.43 +  from suminf_cmult_indicator[OF assms(1), OF \<open>x \<in> A i\<close>, of "\<lambda>k. 1"]
    1.44 +  show ?thesis using * by simp
    1.45 +qed simp
    1.46 +
    1.47 +lemma setsum_indicator_disjoint_family:
    1.48 +  fixes f :: "'d \<Rightarrow> 'e::semiring_1"
    1.49 +  assumes d: "disjoint_family_on A P" and "x \<in> A j" and "finite P" and "j \<in> P"
    1.50 +  shows "(\<Sum>i\<in>P. f i * indicator (A i) x) = f j"
    1.51 +proof -
    1.52 +  have "P \<inter> {i. x \<in> A i} = {j}"
    1.53 +    using d \<open>x \<in> A j\<close> \<open>j \<in> P\<close> unfolding disjoint_family_on_def
    1.54 +    by auto
    1.55 +  thus ?thesis
    1.56 +    unfolding indicator_def
    1.57 +    by (simp add: if_distrib setsum.If_cases[OF \<open>finite P\<close>])
    1.58 +qed
    1.59 +
    1.60 +text \<open>
    1.61 +  The type for emeasure spaces is already defined in @{theory Sigma_Algebra}, as it is also used to
    1.62 +  represent sigma algebras (with an arbitrary emeasure).
    1.63 +\<close>
    1.64 +
    1.65 +subsection "Extend binary sets"
    1.66 +
    1.67 +lemma LIMSEQ_binaryset:
    1.68 +  assumes f: "f {} = 0"
    1.69 +  shows  "(\<lambda>n. \<Sum>i<n. f (binaryset A B i)) \<longlonglongrightarrow> f A + f B"
    1.70 +proof -
    1.71 +  have "(\<lambda>n. \<Sum>i < Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
    1.72 +    proof
    1.73 +      fix n
    1.74 +      show "(\<Sum>i < Suc (Suc n). f (binaryset A B i)) = f A + f B"
    1.75 +        by (induct n)  (auto simp add: binaryset_def f)
    1.76 +    qed
    1.77 +  moreover
    1.78 +  have "... \<longlonglongrightarrow> f A + f B" by (rule tendsto_const)
    1.79 +  ultimately
    1.80 +  have "(\<lambda>n. \<Sum>i< Suc (Suc n). f (binaryset A B i)) \<longlonglongrightarrow> f A + f B"
    1.81 +    by metis
    1.82 +  hence "(\<lambda>n. \<Sum>i< n+2. f (binaryset A B i)) \<longlonglongrightarrow> f A + f B"
    1.83 +    by simp
    1.84 +  thus ?thesis by (rule LIMSEQ_offset [where k=2])
    1.85 +qed
    1.86 +
    1.87 +lemma binaryset_sums:
    1.88 +  assumes f: "f {} = 0"
    1.89 +  shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
    1.90 +    by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f] atLeast0LessThan)
    1.91 +
    1.92 +lemma suminf_binaryset_eq:
    1.93 +  fixes f :: "'a set \<Rightarrow> 'b::{comm_monoid_add, t2_space}"
    1.94 +  shows "f {} = 0 \<Longrightarrow> (\<Sum>n. f (binaryset A B n)) = f A + f B"
    1.95 +  by (metis binaryset_sums sums_unique)
    1.96 +
    1.97 +subsection \<open>Properties of a premeasure @{term \<mu>}\<close>
    1.98 +
    1.99 +text \<open>
   1.100 +  The definitions for @{const positive} and @{const countably_additive} should be here, by they are
   1.101 +  necessary to define @{typ "'a measure"} in @{theory Sigma_Algebra}.
   1.102 +\<close>
   1.103 +
   1.104 +definition subadditive where
   1.105 +  "subadditive M f \<longleftrightarrow> (\<forall>x\<in>M. \<forall>y\<in>M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
   1.106 +
   1.107 +lemma subadditiveD: "subadditive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> M \<Longrightarrow> y \<in> M \<Longrightarrow> f (x \<union> y) \<le> f x + f y"
   1.108 +  by (auto simp add: subadditive_def)
   1.109 +
   1.110 +definition countably_subadditive where
   1.111 +  "countably_subadditive M f \<longleftrightarrow>
   1.112 +    (\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))))"
   1.113 +
   1.114 +lemma (in ring_of_sets) countably_subadditive_subadditive:
   1.115 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.116 +  assumes f: "positive M f" and cs: "countably_subadditive M f"
   1.117 +  shows  "subadditive M f"
   1.118 +proof (auto simp add: subadditive_def)
   1.119 +  fix x y
   1.120 +  assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   1.121 +  hence "disjoint_family (binaryset x y)"
   1.122 +    by (auto simp add: disjoint_family_on_def binaryset_def)
   1.123 +  hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
   1.124 +         (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
   1.125 +         f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
   1.126 +    using cs by (auto simp add: countably_subadditive_def)
   1.127 +  hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
   1.128 +         f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
   1.129 +    by (simp add: range_binaryset_eq UN_binaryset_eq)
   1.130 +  thus "f (x \<union> y) \<le>  f x + f y" using f x y
   1.131 +    by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
   1.132 +qed
   1.133 +
   1.134 +definition additive where
   1.135 +  "additive M \<mu> \<longleftrightarrow> (\<forall>x\<in>M. \<forall>y\<in>M. x \<inter> y = {} \<longrightarrow> \<mu> (x \<union> y) = \<mu> x + \<mu> y)"
   1.136 +
   1.137 +definition increasing where
   1.138 +  "increasing M \<mu> \<longleftrightarrow> (\<forall>x\<in>M. \<forall>y\<in>M. x \<subseteq> y \<longrightarrow> \<mu> x \<le> \<mu> y)"
   1.139 +
   1.140 +lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
   1.141 +
   1.142 +lemma positiveD_empty:
   1.143 +  "positive M f \<Longrightarrow> f {} = 0"
   1.144 +  by (auto simp add: positive_def)
   1.145 +
   1.146 +lemma additiveD:
   1.147 +  "additive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> M \<Longrightarrow> y \<in> M \<Longrightarrow> f (x \<union> y) = f x + f y"
   1.148 +  by (auto simp add: additive_def)
   1.149 +
   1.150 +lemma increasingD:
   1.151 +  "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>M \<Longrightarrow> y\<in>M \<Longrightarrow> f x \<le> f y"
   1.152 +  by (auto simp add: increasing_def)
   1.153 +
   1.154 +lemma countably_additiveI[case_names countably]:
   1.155 +  "(\<And>A. range A \<subseteq> M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Union>i. A i) \<in> M \<Longrightarrow> (\<Sum>i. f (A i)) = f (\<Union>i. A i))
   1.156 +  \<Longrightarrow> countably_additive M f"
   1.157 +  by (simp add: countably_additive_def)
   1.158 +
   1.159 +lemma (in ring_of_sets) disjointed_additive:
   1.160 +  assumes f: "positive M f" "additive M f" and A: "range A \<subseteq> M" "incseq A"
   1.161 +  shows "(\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
   1.162 +proof (induct n)
   1.163 +  case (Suc n)
   1.164 +  then have "(\<Sum>i\<le>Suc n. f (disjointed A i)) = f (A n) + f (disjointed A (Suc n))"
   1.165 +    by simp
   1.166 +  also have "\<dots> = f (A n \<union> disjointed A (Suc n))"
   1.167 +    using A by (subst f(2)[THEN additiveD]) (auto simp: disjointed_mono)
   1.168 +  also have "A n \<union> disjointed A (Suc n) = A (Suc n)"
   1.169 +    using \<open>incseq A\<close> by (auto dest: incseq_SucD simp: disjointed_mono)
   1.170 +  finally show ?case .
   1.171 +qed simp
   1.172 +
   1.173 +lemma (in ring_of_sets) additive_sum:
   1.174 +  fixes A:: "'i \<Rightarrow> 'a set"
   1.175 +  assumes f: "positive M f" and ad: "additive M f" and "finite S"
   1.176 +      and A: "A`S \<subseteq> M"
   1.177 +      and disj: "disjoint_family_on A S"
   1.178 +  shows  "(\<Sum>i\<in>S. f (A i)) = f (\<Union>i\<in>S. A i)"
   1.179 +  using \<open>finite S\<close> disj A
   1.180 +proof induct
   1.181 +  case empty show ?case using f by (simp add: positive_def)
   1.182 +next
   1.183 +  case (insert s S)
   1.184 +  then have "A s \<inter> (\<Union>i\<in>S. A i) = {}"
   1.185 +    by (auto simp add: disjoint_family_on_def neq_iff)
   1.186 +  moreover
   1.187 +  have "A s \<in> M" using insert by blast
   1.188 +  moreover have "(\<Union>i\<in>S. A i) \<in> M"
   1.189 +    using insert \<open>finite S\<close> by auto
   1.190 +  ultimately have "f (A s \<union> (\<Union>i\<in>S. A i)) = f (A s) + f(\<Union>i\<in>S. A i)"
   1.191 +    using ad UNION_in_sets A by (auto simp add: additive_def)
   1.192 +  with insert show ?case using ad disjoint_family_on_mono[of S "insert s S" A]
   1.193 +    by (auto simp add: additive_def subset_insertI)
   1.194 +qed
   1.195 +
   1.196 +lemma (in ring_of_sets) additive_increasing:
   1.197 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.198 +  assumes posf: "positive M f" and addf: "additive M f"
   1.199 +  shows "increasing M f"
   1.200 +proof (auto simp add: increasing_def)
   1.201 +  fix x y
   1.202 +  assume xy: "x \<in> M" "y \<in> M" "x \<subseteq> y"
   1.203 +  then have "y - x \<in> M" by auto
   1.204 +  then have "f x + 0 \<le> f x + f (y-x)" by (intro add_left_mono zero_le)
   1.205 +  also have "... = f (x \<union> (y-x))" using addf
   1.206 +    by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
   1.207 +  also have "... = f y"
   1.208 +    by (metis Un_Diff_cancel Un_absorb1 xy(3))
   1.209 +  finally show "f x \<le> f y" by simp
   1.210 +qed
   1.211 +
   1.212 +lemma (in ring_of_sets) subadditive:
   1.213 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.214 +  assumes f: "positive M f" "additive M f" and A: "A`S \<subseteq> M" and S: "finite S"
   1.215 +  shows "f (\<Union>i\<in>S. A i) \<le> (\<Sum>i\<in>S. f (A i))"
   1.216 +using S A
   1.217 +proof (induct S)
   1.218 +  case empty thus ?case using f by (auto simp: positive_def)
   1.219 +next
   1.220 +  case (insert x F)
   1.221 +  hence in_M: "A x \<in> M" "(\<Union>i\<in>F. A i) \<in> M" "(\<Union>i\<in>F. A i) - A x \<in> M" using A by force+
   1.222 +  have subs: "(\<Union>i\<in>F. A i) - A x \<subseteq> (\<Union>i\<in>F. A i)" by auto
   1.223 +  have "(\<Union>i\<in>(insert x F). A i) = A x \<union> ((\<Union>i\<in>F. A i) - A x)" by auto
   1.224 +  hence "f (\<Union>i\<in>(insert x F). A i) = f (A x \<union> ((\<Union>i\<in>F. A i) - A x))"
   1.225 +    by simp
   1.226 +  also have "\<dots> = f (A x) + f ((\<Union>i\<in>F. A i) - A x)"
   1.227 +    using f(2) by (rule additiveD) (insert in_M, auto)
   1.228 +  also have "\<dots> \<le> f (A x) + f (\<Union>i\<in>F. A i)"
   1.229 +    using additive_increasing[OF f] in_M subs by (auto simp: increasing_def intro: add_left_mono)
   1.230 +  also have "\<dots> \<le> f (A x) + (\<Sum>i\<in>F. f (A i))" using insert by (auto intro: add_left_mono)
   1.231 +  finally show "f (\<Union>i\<in>(insert x F). A i) \<le> (\<Sum>i\<in>(insert x F). f (A i))" using insert by simp
   1.232 +qed
   1.233 +
   1.234 +lemma (in ring_of_sets) countably_additive_additive:
   1.235 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.236 +  assumes posf: "positive M f" and ca: "countably_additive M f"
   1.237 +  shows "additive M f"
   1.238 +proof (auto simp add: additive_def)
   1.239 +  fix x y
   1.240 +  assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   1.241 +  hence "disjoint_family (binaryset x y)"
   1.242 +    by (auto simp add: disjoint_family_on_def binaryset_def)
   1.243 +  hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
   1.244 +         (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
   1.245 +         f (\<Union>i. binaryset x y i) = (\<Sum> n. f (binaryset x y n))"
   1.246 +    using ca
   1.247 +    by (simp add: countably_additive_def)
   1.248 +  hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
   1.249 +         f (x \<union> y) = (\<Sum>n. f (binaryset x y n))"
   1.250 +    by (simp add: range_binaryset_eq UN_binaryset_eq)
   1.251 +  thus "f (x \<union> y) = f x + f y" using posf x y
   1.252 +    by (auto simp add: Un suminf_binaryset_eq positive_def)
   1.253 +qed
   1.254 +
   1.255 +lemma (in algebra) increasing_additive_bound:
   1.256 +  fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> ennreal"
   1.257 +  assumes f: "positive M f" and ad: "additive M f"
   1.258 +      and inc: "increasing M f"
   1.259 +      and A: "range A \<subseteq> M"
   1.260 +      and disj: "disjoint_family A"
   1.261 +  shows  "(\<Sum>i. f (A i)) \<le> f \<Omega>"
   1.262 +proof (safe intro!: suminf_le_const)
   1.263 +  fix N
   1.264 +  note disj_N = disjoint_family_on_mono[OF _ disj, of "{..<N}"]
   1.265 +  have "(\<Sum>i<N. f (A i)) = f (\<Union>i\<in>{..<N}. A i)"
   1.266 +    using A by (intro additive_sum [OF f ad _ _]) (auto simp: disj_N)
   1.267 +  also have "... \<le> f \<Omega>" using space_closed A
   1.268 +    by (intro increasingD[OF inc] finite_UN) auto
   1.269 +  finally show "(\<Sum>i<N. f (A i)) \<le> f \<Omega>" by simp
   1.270 +qed (insert f A, auto simp: positive_def)
   1.271 +
   1.272 +lemma (in ring_of_sets) countably_additiveI_finite:
   1.273 +  fixes \<mu> :: "'a set \<Rightarrow> ennreal"
   1.274 +  assumes "finite \<Omega>" "positive M \<mu>" "additive M \<mu>"
   1.275 +  shows "countably_additive M \<mu>"
   1.276 +proof (rule countably_additiveI)
   1.277 +  fix F :: "nat \<Rightarrow> 'a set" assume F: "range F \<subseteq> M" "(\<Union>i. F i) \<in> M" and disj: "disjoint_family F"
   1.278 +
   1.279 +  have "\<forall>i\<in>{i. F i \<noteq> {}}. \<exists>x. x \<in> F i" by auto
   1.280 +  from bchoice[OF this] obtain f where f: "\<And>i. F i \<noteq> {} \<Longrightarrow> f i \<in> F i" by auto
   1.281 +
   1.282 +  have inj_f: "inj_on f {i. F i \<noteq> {}}"
   1.283 +  proof (rule inj_onI, simp)
   1.284 +    fix i j a b assume *: "f i = f j" "F i \<noteq> {}" "F j \<noteq> {}"
   1.285 +    then have "f i \<in> F i" "f j \<in> F j" using f by force+
   1.286 +    with disj * show "i = j" by (auto simp: disjoint_family_on_def)
   1.287 +  qed
   1.288 +  have "finite (\<Union>i. F i)"
   1.289 +    by (metis F(2) assms(1) infinite_super sets_into_space)
   1.290 +
   1.291 +  have F_subset: "{i. \<mu> (F i) \<noteq> 0} \<subseteq> {i. F i \<noteq> {}}"
   1.292 +    by (auto simp: positiveD_empty[OF \<open>positive M \<mu>\<close>])
   1.293 +  moreover have fin_not_empty: "finite {i. F i \<noteq> {}}"
   1.294 +  proof (rule finite_imageD)
   1.295 +    from f have "f`{i. F i \<noteq> {}} \<subseteq> (\<Union>i. F i)" by auto
   1.296 +    then show "finite (f`{i. F i \<noteq> {}})"
   1.297 +      by (rule finite_subset) fact
   1.298 +  qed fact
   1.299 +  ultimately have fin_not_0: "finite {i. \<mu> (F i) \<noteq> 0}"
   1.300 +    by (rule finite_subset)
   1.301 +
   1.302 +  have disj_not_empty: "disjoint_family_on F {i. F i \<noteq> {}}"
   1.303 +    using disj by (auto simp: disjoint_family_on_def)
   1.304 +
   1.305 +  from fin_not_0 have "(\<Sum>i. \<mu> (F i)) = (\<Sum>i | \<mu> (F i) \<noteq> 0. \<mu> (F i))"
   1.306 +    by (rule suminf_finite) auto
   1.307 +  also have "\<dots> = (\<Sum>i | F i \<noteq> {}. \<mu> (F i))"
   1.308 +    using fin_not_empty F_subset by (rule setsum.mono_neutral_left) auto
   1.309 +  also have "\<dots> = \<mu> (\<Union>i\<in>{i. F i \<noteq> {}}. F i)"
   1.310 +    using \<open>positive M \<mu>\<close> \<open>additive M \<mu>\<close> fin_not_empty disj_not_empty F by (intro additive_sum) auto
   1.311 +  also have "\<dots> = \<mu> (\<Union>i. F i)"
   1.312 +    by (rule arg_cong[where f=\<mu>]) auto
   1.313 +  finally show "(\<Sum>i. \<mu> (F i)) = \<mu> (\<Union>i. F i)" .
   1.314 +qed
   1.315 +
   1.316 +lemma (in ring_of_sets) countably_additive_iff_continuous_from_below:
   1.317 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.318 +  assumes f: "positive M f" "additive M f"
   1.319 +  shows "countably_additive M f \<longleftrightarrow>
   1.320 +    (\<forall>A. range A \<subseteq> M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i))"
   1.321 +  unfolding countably_additive_def
   1.322 +proof safe
   1.323 +  assume count_sum: "\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> UNION UNIV A \<in> M \<longrightarrow> (\<Sum>i. f (A i)) = f (UNION UNIV A)"
   1.324 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   1.325 +  then have dA: "range (disjointed A) \<subseteq> M" by (auto simp: range_disjointed_sets)
   1.326 +  with count_sum[THEN spec, of "disjointed A"] A(3)
   1.327 +  have f_UN: "(\<Sum>i. f (disjointed A i)) = f (\<Union>i. A i)"
   1.328 +    by (auto simp: UN_disjointed_eq disjoint_family_disjointed)
   1.329 +  moreover have "(\<lambda>n. (\<Sum>i<n. f (disjointed A i))) \<longlonglongrightarrow> (\<Sum>i. f (disjointed A i))"
   1.330 +    using f(1)[unfolded positive_def] dA
   1.331 +    by (auto intro!: summable_LIMSEQ)
   1.332 +  from LIMSEQ_Suc[OF this]
   1.333 +  have "(\<lambda>n. (\<Sum>i\<le>n. f (disjointed A i))) \<longlonglongrightarrow> (\<Sum>i. f (disjointed A i))"
   1.334 +    unfolding lessThan_Suc_atMost .
   1.335 +  moreover have "\<And>n. (\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
   1.336 +    using disjointed_additive[OF f A(1,2)] .
   1.337 +  ultimately show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)" by simp
   1.338 +next
   1.339 +  assume cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> incseq A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   1.340 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "disjoint_family A" "(\<Union>i. A i) \<in> M"
   1.341 +  have *: "(\<Union>n. (\<Union>i<n. A i)) = (\<Union>i. A i)" by auto
   1.342 +  have "(\<lambda>n. f (\<Union>i<n. A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   1.343 +  proof (unfold *[symmetric], intro cont[rule_format])
   1.344 +    show "range (\<lambda>i. \<Union>i<i. A i) \<subseteq> M" "(\<Union>i. \<Union>i<i. A i) \<in> M"
   1.345 +      using A * by auto
   1.346 +  qed (force intro!: incseq_SucI)
   1.347 +  moreover have "\<And>n. f (\<Union>i<n. A i) = (\<Sum>i<n. f (A i))"
   1.348 +    using A
   1.349 +    by (intro additive_sum[OF f, of _ A, symmetric])
   1.350 +       (auto intro: disjoint_family_on_mono[where B=UNIV])
   1.351 +  ultimately
   1.352 +  have "(\<lambda>i. f (A i)) sums f (\<Union>i. A i)"
   1.353 +    unfolding sums_def by simp
   1.354 +  from sums_unique[OF this]
   1.355 +  show "(\<Sum>i. f (A i)) = f (\<Union>i. A i)" by simp
   1.356 +qed
   1.357 +
   1.358 +lemma (in ring_of_sets) continuous_from_above_iff_empty_continuous:
   1.359 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.360 +  assumes f: "positive M f" "additive M f"
   1.361 +  shows "(\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Inter>i. A i))
   1.362 +     \<longleftrightarrow> (\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> 0)"
   1.363 +proof safe
   1.364 +  assume cont: "(\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) \<in> M \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Inter>i. A i))"
   1.365 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "decseq A" "(\<Inter>i. A i) = {}" "\<forall>i. f (A i) \<noteq> \<infinity>"
   1.366 +  with cont[THEN spec, of A] show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   1.367 +    using \<open>positive M f\<close>[unfolded positive_def] by auto
   1.368 +next
   1.369 +  assume cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<forall>i. f (A i) \<noteq> \<infinity>) \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   1.370 +  fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "decseq A" "(\<Inter>i. A i) \<in> M" "\<forall>i. f (A i) \<noteq> \<infinity>"
   1.371 +
   1.372 +  have f_mono: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
   1.373 +    using additive_increasing[OF f] unfolding increasing_def by simp
   1.374 +
   1.375 +  have decseq_fA: "decseq (\<lambda>i. f (A i))"
   1.376 +    using A by (auto simp: decseq_def intro!: f_mono)
   1.377 +  have decseq: "decseq (\<lambda>i. A i - (\<Inter>i. A i))"
   1.378 +    using A by (auto simp: decseq_def)
   1.379 +  then have decseq_f: "decseq (\<lambda>i. f (A i - (\<Inter>i. A i)))"
   1.380 +    using A unfolding decseq_def by (auto intro!: f_mono Diff)
   1.381 +  have "f (\<Inter>x. A x) \<le> f (A 0)"
   1.382 +    using A by (auto intro!: f_mono)
   1.383 +  then have f_Int_fin: "f (\<Inter>x. A x) \<noteq> \<infinity>"
   1.384 +    using A by (auto simp: top_unique)
   1.385 +  { fix i
   1.386 +    have "f (A i - (\<Inter>i. A i)) \<le> f (A i)" using A by (auto intro!: f_mono)
   1.387 +    then have "f (A i - (\<Inter>i. A i)) \<noteq> \<infinity>"
   1.388 +      using A by (auto simp: top_unique) }
   1.389 +  note f_fin = this
   1.390 +  have "(\<lambda>i. f (A i - (\<Inter>i. A i))) \<longlonglongrightarrow> 0"
   1.391 +  proof (intro cont[rule_format, OF _ decseq _ f_fin])
   1.392 +    show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
   1.393 +      using A by auto
   1.394 +  qed
   1.395 +  from INF_Lim_ereal[OF decseq_f this]
   1.396 +  have "(INF n. f (A n - (\<Inter>i. A i))) = 0" .
   1.397 +  moreover have "(INF n. f (\<Inter>i. A i)) = f (\<Inter>i. A i)"
   1.398 +    by auto
   1.399 +  ultimately have "(INF n. f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i)) = 0 + f (\<Inter>i. A i)"
   1.400 +    using A(4) f_fin f_Int_fin
   1.401 +    by (subst INF_ennreal_add_const) (auto simp: decseq_f)
   1.402 +  moreover {
   1.403 +    fix n
   1.404 +    have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f ((A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i))"
   1.405 +      using A by (subst f(2)[THEN additiveD]) auto
   1.406 +    also have "(A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i) = A n"
   1.407 +      by auto
   1.408 +    finally have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f (A n)" . }
   1.409 +  ultimately have "(INF n. f (A n)) = f (\<Inter>i. A i)"
   1.410 +    by simp
   1.411 +  with LIMSEQ_INF[OF decseq_fA]
   1.412 +  show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Inter>i. A i)" by simp
   1.413 +qed
   1.414 +
   1.415 +lemma (in ring_of_sets) empty_continuous_imp_continuous_from_below:
   1.416 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.417 +  assumes f: "positive M f" "additive M f" "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   1.418 +  assumes cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   1.419 +  assumes A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   1.420 +  shows "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   1.421 +proof -
   1.422 +  from A have "(\<lambda>i. f ((\<Union>i. A i) - A i)) \<longlonglongrightarrow> 0"
   1.423 +    by (intro cont[rule_format]) (auto simp: decseq_def incseq_def)
   1.424 +  moreover
   1.425 +  { fix i
   1.426 +    have "f ((\<Union>i. A i) - A i \<union> A i) = f ((\<Union>i. A i) - A i) + f (A i)"
   1.427 +      using A by (intro f(2)[THEN additiveD]) auto
   1.428 +    also have "((\<Union>i. A i) - A i) \<union> A i = (\<Union>i. A i)"
   1.429 +      by auto
   1.430 +    finally have "f ((\<Union>i. A i) - A i) = f (\<Union>i. A i) - f (A i)"
   1.431 +      using f(3)[rule_format, of "A i"] A by (auto simp: ennreal_add_diff_cancel subset_eq) }
   1.432 +  moreover have "\<forall>\<^sub>F i in sequentially. f (A i) \<le> f (\<Union>i. A i)"
   1.433 +    using increasingD[OF additive_increasing[OF f(1, 2)], of "A _" "\<Union>i. A i"] A
   1.434 +    by (auto intro!: always_eventually simp: subset_eq)
   1.435 +  ultimately show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   1.436 +    by (auto intro: ennreal_tendsto_const_minus)
   1.437 +qed
   1.438 +
   1.439 +lemma (in ring_of_sets) empty_continuous_imp_countably_additive:
   1.440 +  fixes f :: "'a set \<Rightarrow> ennreal"
   1.441 +  assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   1.442 +  assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   1.443 +  shows "countably_additive M f"
   1.444 +  using countably_additive_iff_continuous_from_below[OF f]
   1.445 +  using empty_continuous_imp_continuous_from_below[OF f fin] cont
   1.446 +  by blast
   1.447 +
   1.448 +subsection \<open>Properties of @{const emeasure}\<close>
   1.449 +
   1.450 +lemma emeasure_positive: "positive (sets M) (emeasure M)"
   1.451 +  by (cases M) (auto simp: sets_def emeasure_def Abs_measure_inverse measure_space_def)
   1.452 +
   1.453 +lemma emeasure_empty[simp, intro]: "emeasure M {} = 0"
   1.454 +  using emeasure_positive[of M] by (simp add: positive_def)
   1.455 +
   1.456 +lemma emeasure_single_in_space: "emeasure M {x} \<noteq> 0 \<Longrightarrow> x \<in> space M"
   1.457 +  using emeasure_notin_sets[of "{x}" M] by (auto dest: sets.sets_into_space zero_less_iff_neq_zero[THEN iffD2])
   1.458 +
   1.459 +lemma emeasure_countably_additive: "countably_additive (sets M) (emeasure M)"
   1.460 +  by (cases M) (auto simp: sets_def emeasure_def Abs_measure_inverse measure_space_def)
   1.461 +
   1.462 +lemma suminf_emeasure:
   1.463 +  "range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Sum>i. emeasure M (A i)) = emeasure M (\<Union>i. A i)"
   1.464 +  using sets.countable_UN[of A UNIV M] emeasure_countably_additive[of M]
   1.465 +  by (simp add: countably_additive_def)
   1.466 +
   1.467 +lemma sums_emeasure:
   1.468 +  "disjoint_family F \<Longrightarrow> (\<And>i. F i \<in> sets M) \<Longrightarrow> (\<lambda>i. emeasure M (F i)) sums emeasure M (\<Union>i. F i)"
   1.469 +  unfolding sums_iff by (intro conjI suminf_emeasure) auto
   1.470 +
   1.471 +lemma emeasure_additive: "additive (sets M) (emeasure M)"
   1.472 +  by (metis sets.countably_additive_additive emeasure_positive emeasure_countably_additive)
   1.473 +
   1.474 +lemma plus_emeasure:
   1.475 +  "a \<in> sets M \<Longrightarrow> b \<in> sets M \<Longrightarrow> a \<inter> b = {} \<Longrightarrow> emeasure M a + emeasure M b = emeasure M (a \<union> b)"
   1.476 +  using additiveD[OF emeasure_additive] ..
   1.477 +
   1.478 +lemma setsum_emeasure:
   1.479 +  "F`I \<subseteq> sets M \<Longrightarrow> disjoint_family_on F I \<Longrightarrow> finite I \<Longrightarrow>
   1.480 +    (\<Sum>i\<in>I. emeasure M (F i)) = emeasure M (\<Union>i\<in>I. F i)"
   1.481 +  by (metis sets.additive_sum emeasure_positive emeasure_additive)
   1.482 +
   1.483 +lemma emeasure_mono:
   1.484 +  "a \<subseteq> b \<Longrightarrow> b \<in> sets M \<Longrightarrow> emeasure M a \<le> emeasure M b"
   1.485 +  by (metis zero_le sets.additive_increasing emeasure_additive emeasure_notin_sets emeasure_positive increasingD)
   1.486 +
   1.487 +lemma emeasure_space:
   1.488 +  "emeasure M A \<le> emeasure M (space M)"
   1.489 +  by (metis emeasure_mono emeasure_notin_sets sets.sets_into_space sets.top zero_le)
   1.490 +
   1.491 +lemma emeasure_Diff:
   1.492 +  assumes finite: "emeasure M B \<noteq> \<infinity>"
   1.493 +  and [measurable]: "A \<in> sets M" "B \<in> sets M" and "B \<subseteq> A"
   1.494 +  shows "emeasure M (A - B) = emeasure M A - emeasure M B"
   1.495 +proof -
   1.496 +  have "(A - B) \<union> B = A" using \<open>B \<subseteq> A\<close> by auto
   1.497 +  then have "emeasure M A = emeasure M ((A - B) \<union> B)" by simp
   1.498 +  also have "\<dots> = emeasure M (A - B) + emeasure M B"
   1.499 +    by (subst plus_emeasure[symmetric]) auto
   1.500 +  finally show "emeasure M (A - B) = emeasure M A - emeasure M B"
   1.501 +    using finite by simp
   1.502 +qed
   1.503 +
   1.504 +lemma emeasure_compl:
   1.505 +  "s \<in> sets M \<Longrightarrow> emeasure M s \<noteq> \<infinity> \<Longrightarrow> emeasure M (space M - s) = emeasure M (space M) - emeasure M s"
   1.506 +  by (rule emeasure_Diff) (auto dest: sets.sets_into_space)
   1.507 +
   1.508 +lemma Lim_emeasure_incseq:
   1.509 +  "range A \<subseteq> sets M \<Longrightarrow> incseq A \<Longrightarrow> (\<lambda>i. (emeasure M (A i))) \<longlonglongrightarrow> emeasure M (\<Union>i. A i)"
   1.510 +  using emeasure_countably_additive
   1.511 +  by (auto simp add: sets.countably_additive_iff_continuous_from_below emeasure_positive
   1.512 +    emeasure_additive)
   1.513 +
   1.514 +lemma incseq_emeasure:
   1.515 +  assumes "range B \<subseteq> sets M" "incseq B"
   1.516 +  shows "incseq (\<lambda>i. emeasure M (B i))"
   1.517 +  using assms by (auto simp: incseq_def intro!: emeasure_mono)
   1.518 +
   1.519 +lemma SUP_emeasure_incseq:
   1.520 +  assumes A: "range A \<subseteq> sets M" "incseq A"
   1.521 +  shows "(SUP n. emeasure M (A n)) = emeasure M (\<Union>i. A i)"
   1.522 +  using LIMSEQ_SUP[OF incseq_emeasure, OF A] Lim_emeasure_incseq[OF A]
   1.523 +  by (simp add: LIMSEQ_unique)
   1.524 +
   1.525 +lemma decseq_emeasure:
   1.526 +  assumes "range B \<subseteq> sets M" "decseq B"
   1.527 +  shows "decseq (\<lambda>i. emeasure M (B i))"
   1.528 +  using assms by (auto simp: decseq_def intro!: emeasure_mono)
   1.529 +
   1.530 +lemma INF_emeasure_decseq:
   1.531 +  assumes A: "range A \<subseteq> sets M" and "decseq A"
   1.532 +  and finite: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   1.533 +  shows "(INF n. emeasure M (A n)) = emeasure M (\<Inter>i. A i)"
   1.534 +proof -
   1.535 +  have le_MI: "emeasure M (\<Inter>i. A i) \<le> emeasure M (A 0)"
   1.536 +    using A by (auto intro!: emeasure_mono)
   1.537 +  hence *: "emeasure M (\<Inter>i. A i) \<noteq> \<infinity>" using finite[of 0] by (auto simp: top_unique)
   1.538 +
   1.539 +  have "emeasure M (A 0) - (INF n. emeasure M (A n)) = (SUP n. emeasure M (A 0) - emeasure M (A n))"
   1.540 +    by (simp add: ennreal_INF_const_minus)
   1.541 +  also have "\<dots> = (SUP n. emeasure M (A 0 - A n))"
   1.542 +    using A finite \<open>decseq A\<close>[unfolded decseq_def] by (subst emeasure_Diff) auto
   1.543 +  also have "\<dots> = emeasure M (\<Union>i. A 0 - A i)"
   1.544 +  proof (rule SUP_emeasure_incseq)
   1.545 +    show "range (\<lambda>n. A 0 - A n) \<subseteq> sets M"
   1.546 +      using A by auto
   1.547 +    show "incseq (\<lambda>n. A 0 - A n)"
   1.548 +      using \<open>decseq A\<close> by (auto simp add: incseq_def decseq_def)
   1.549 +  qed
   1.550 +  also have "\<dots> = emeasure M (A 0) - emeasure M (\<Inter>i. A i)"
   1.551 +    using A finite * by (simp, subst emeasure_Diff) auto
   1.552 +  finally show ?thesis
   1.553 +    by (rule ennreal_minus_cancel[rotated 3])
   1.554 +       (insert finite A, auto intro: INF_lower emeasure_mono)
   1.555 +qed
   1.556 +
   1.557 +lemma emeasure_INT_decseq_subset:
   1.558 +  fixes F :: "nat \<Rightarrow> 'a set"
   1.559 +  assumes I: "I \<noteq> {}" and F: "\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> i \<le> j \<Longrightarrow> F j \<subseteq> F i"
   1.560 +  assumes F_sets[measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M"
   1.561 +    and fin: "\<And>i. i \<in> I \<Longrightarrow> emeasure M (F i) \<noteq> \<infinity>"
   1.562 +  shows "emeasure M (\<Inter>i\<in>I. F i) = (INF i:I. emeasure M (F i))"
   1.563 +proof cases
   1.564 +  assume "finite I"
   1.565 +  have "(\<Inter>i\<in>I. F i) = F (Max I)"
   1.566 +    using I \<open>finite I\<close> by (intro antisym INF_lower INF_greatest F) auto
   1.567 +  moreover have "(INF i:I. emeasure M (F i)) = emeasure M (F (Max I))"
   1.568 +    using I \<open>finite I\<close> by (intro antisym INF_lower INF_greatest F emeasure_mono) auto
   1.569 +  ultimately show ?thesis
   1.570 +    by simp
   1.571 +next
   1.572 +  assume "infinite I"
   1.573 +  define L where "L n = (LEAST i. i \<in> I \<and> i \<ge> n)" for n
   1.574 +  have L: "L n \<in> I \<and> n \<le> L n" for n
   1.575 +    unfolding L_def
   1.576 +  proof (rule LeastI_ex)
   1.577 +    show "\<exists>x. x \<in> I \<and> n \<le> x"
   1.578 +      using \<open>infinite I\<close> finite_subset[of I "{..< n}"]
   1.579 +      by (rule_tac ccontr) (auto simp: not_le)
   1.580 +  qed
   1.581 +  have L_eq[simp]: "i \<in> I \<Longrightarrow> L i = i" for i
   1.582 +    unfolding L_def by (intro Least_equality) auto
   1.583 +  have L_mono: "i \<le> j \<Longrightarrow> L i \<le> L j" for i j
   1.584 +    using L[of j] unfolding L_def by (intro Least_le) (auto simp: L_def)
   1.585 +
   1.586 +  have "emeasure M (\<Inter>i. F (L i)) = (INF i. emeasure M (F (L i)))"
   1.587 +  proof (intro INF_emeasure_decseq[symmetric])
   1.588 +    show "decseq (\<lambda>i. F (L i))"
   1.589 +      using L by (intro antimonoI F L_mono) auto
   1.590 +  qed (insert L fin, auto)
   1.591 +  also have "\<dots> = (INF i:I. emeasure M (F i))"
   1.592 +  proof (intro antisym INF_greatest)
   1.593 +    show "i \<in> I \<Longrightarrow> (INF i. emeasure M (F (L i))) \<le> emeasure M (F i)" for i
   1.594 +      by (intro INF_lower2[of i]) auto
   1.595 +  qed (insert L, auto intro: INF_lower)
   1.596 +  also have "(\<Inter>i. F (L i)) = (\<Inter>i\<in>I. F i)"
   1.597 +  proof (intro antisym INF_greatest)
   1.598 +    show "i \<in> I \<Longrightarrow> (\<Inter>i. F (L i)) \<subseteq> F i" for i
   1.599 +      by (intro INF_lower2[of i]) auto
   1.600 +  qed (insert L, auto)
   1.601 +  finally show ?thesis .
   1.602 +qed
   1.603 +
   1.604 +lemma Lim_emeasure_decseq:
   1.605 +  assumes A: "range A \<subseteq> sets M" "decseq A" and fin: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   1.606 +  shows "(\<lambda>i. emeasure M (A i)) \<longlonglongrightarrow> emeasure M (\<Inter>i. A i)"
   1.607 +  using LIMSEQ_INF[OF decseq_emeasure, OF A]
   1.608 +  using INF_emeasure_decseq[OF A fin] by simp
   1.609 +
   1.610 +lemma emeasure_lfp'[consumes 1, case_names cont measurable]:
   1.611 +  assumes "P M"
   1.612 +  assumes cont: "sup_continuous F"
   1.613 +  assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
   1.614 +  shows "emeasure M {x\<in>space M. lfp F x} = (SUP i. emeasure M {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   1.615 +proof -
   1.616 +  have "emeasure M {x\<in>space M. lfp F x} = emeasure M (\<Union>i. {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   1.617 +    using sup_continuous_lfp[OF cont] by (auto simp add: bot_fun_def intro!: arg_cong2[where f=emeasure])
   1.618 +  moreover { fix i from \<open>P M\<close> have "{x\<in>space M. (F ^^ i) (\<lambda>x. False) x} \<in> sets M"
   1.619 +    by (induct i arbitrary: M) (auto simp add: pred_def[symmetric] intro: *) }
   1.620 +  moreover have "incseq (\<lambda>i. {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   1.621 +  proof (rule incseq_SucI)
   1.622 +    fix i
   1.623 +    have "(F ^^ i) (\<lambda>x. False) \<le> (F ^^ (Suc i)) (\<lambda>x. False)"
   1.624 +    proof (induct i)
   1.625 +      case 0 show ?case by (simp add: le_fun_def)
   1.626 +    next
   1.627 +      case Suc thus ?case using monoD[OF sup_continuous_mono[OF cont] Suc] by auto
   1.628 +    qed
   1.629 +    then show "{x \<in> space M. (F ^^ i) (\<lambda>x. False) x} \<subseteq> {x \<in> space M. (F ^^ Suc i) (\<lambda>x. False) x}"
   1.630 +      by auto
   1.631 +  qed
   1.632 +  ultimately show ?thesis
   1.633 +    by (subst SUP_emeasure_incseq) auto
   1.634 +qed
   1.635 +
   1.636 +lemma emeasure_lfp:
   1.637 +  assumes [simp]: "\<And>s. sets (M s) = sets N"
   1.638 +  assumes cont: "sup_continuous F" "sup_continuous f"
   1.639 +  assumes meas: "\<And>P. Measurable.pred N P \<Longrightarrow> Measurable.pred N (F P)"
   1.640 +  assumes iter: "\<And>P s. Measurable.pred N P \<Longrightarrow> P \<le> lfp F \<Longrightarrow> emeasure (M s) {x\<in>space N. F P x} = f (\<lambda>s. emeasure (M s) {x\<in>space N. P x}) s"
   1.641 +  shows "emeasure (M s) {x\<in>space N. lfp F x} = lfp f s"
   1.642 +proof (subst lfp_transfer_bounded[where \<alpha>="\<lambda>F s. emeasure (M s) {x\<in>space N. F x}" and g=f and f=F and P="Measurable.pred N", symmetric])
   1.643 +  fix C assume "incseq C" "\<And>i. Measurable.pred N (C i)"
   1.644 +  then show "(\<lambda>s. emeasure (M s) {x \<in> space N. (SUP i. C i) x}) = (SUP i. (\<lambda>s. emeasure (M s) {x \<in> space N. C i x}))"
   1.645 +    unfolding SUP_apply[abs_def]
   1.646 +    by (subst SUP_emeasure_incseq) (auto simp: mono_def fun_eq_iff intro!: arg_cong2[where f=emeasure])
   1.647 +qed (auto simp add: iter le_fun_def SUP_apply[abs_def] intro!: meas cont)
   1.648 +
   1.649 +lemma emeasure_subadditive_finite:
   1.650 +  "finite I \<Longrightarrow> A ` I \<subseteq> sets M \<Longrightarrow> emeasure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. emeasure M (A i))"
   1.651 +  by (rule sets.subadditive[OF emeasure_positive emeasure_additive]) auto
   1.652 +
   1.653 +lemma emeasure_subadditive:
   1.654 +  "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> emeasure M (A \<union> B) \<le> emeasure M A + emeasure M B"
   1.655 +  using emeasure_subadditive_finite[of "{True, False}" "\<lambda>True \<Rightarrow> A | False \<Rightarrow> B" M] by simp
   1.656 +
   1.657 +lemma emeasure_subadditive_countably:
   1.658 +  assumes "range f \<subseteq> sets M"
   1.659 +  shows "emeasure M (\<Union>i. f i) \<le> (\<Sum>i. emeasure M (f i))"
   1.660 +proof -
   1.661 +  have "emeasure M (\<Union>i. f i) = emeasure M (\<Union>i. disjointed f i)"
   1.662 +    unfolding UN_disjointed_eq ..
   1.663 +  also have "\<dots> = (\<Sum>i. emeasure M (disjointed f i))"
   1.664 +    using sets.range_disjointed_sets[OF assms] suminf_emeasure[of "disjointed f"]
   1.665 +    by (simp add:  disjoint_family_disjointed comp_def)
   1.666 +  also have "\<dots> \<le> (\<Sum>i. emeasure M (f i))"
   1.667 +    using sets.range_disjointed_sets[OF assms] assms
   1.668 +    by (auto intro!: suminf_le emeasure_mono disjointed_subset)
   1.669 +  finally show ?thesis .
   1.670 +qed
   1.671 +
   1.672 +lemma emeasure_insert:
   1.673 +  assumes sets: "{x} \<in> sets M" "A \<in> sets M" and "x \<notin> A"
   1.674 +  shows "emeasure M (insert x A) = emeasure M {x} + emeasure M A"
   1.675 +proof -
   1.676 +  have "{x} \<inter> A = {}" using \<open>x \<notin> A\<close> by auto
   1.677 +  from plus_emeasure[OF sets this] show ?thesis by simp
   1.678 +qed
   1.679 +
   1.680 +lemma emeasure_insert_ne:
   1.681 +  "A \<noteq> {} \<Longrightarrow> {x} \<in> sets M \<Longrightarrow> A \<in> sets M \<Longrightarrow> x \<notin> A \<Longrightarrow> emeasure M (insert x A) = emeasure M {x} + emeasure M A"
   1.682 +  by (rule emeasure_insert)
   1.683 +
   1.684 +lemma emeasure_eq_setsum_singleton:
   1.685 +  assumes "finite S" "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
   1.686 +  shows "emeasure M S = (\<Sum>x\<in>S. emeasure M {x})"
   1.687 +  using setsum_emeasure[of "\<lambda>x. {x}" S M] assms
   1.688 +  by (auto simp: disjoint_family_on_def subset_eq)
   1.689 +
   1.690 +lemma setsum_emeasure_cover:
   1.691 +  assumes "finite S" and "A \<in> sets M" and br_in_M: "B ` S \<subseteq> sets M"
   1.692 +  assumes A: "A \<subseteq> (\<Union>i\<in>S. B i)"
   1.693 +  assumes disj: "disjoint_family_on B S"
   1.694 +  shows "emeasure M A = (\<Sum>i\<in>S. emeasure M (A \<inter> (B i)))"
   1.695 +proof -
   1.696 +  have "(\<Sum>i\<in>S. emeasure M (A \<inter> (B i))) = emeasure M (\<Union>i\<in>S. A \<inter> (B i))"
   1.697 +  proof (rule setsum_emeasure)
   1.698 +    show "disjoint_family_on (\<lambda>i. A \<inter> B i) S"
   1.699 +      using \<open>disjoint_family_on B S\<close>
   1.700 +      unfolding disjoint_family_on_def by auto
   1.701 +  qed (insert assms, auto)
   1.702 +  also have "(\<Union>i\<in>S. A \<inter> (B i)) = A"
   1.703 +    using A by auto
   1.704 +  finally show ?thesis by simp
   1.705 +qed
   1.706 +
   1.707 +lemma emeasure_eq_0:
   1.708 +  "N \<in> sets M \<Longrightarrow> emeasure M N = 0 \<Longrightarrow> K \<subseteq> N \<Longrightarrow> emeasure M K = 0"
   1.709 +  by (metis emeasure_mono order_eq_iff zero_le)
   1.710 +
   1.711 +lemma emeasure_UN_eq_0:
   1.712 +  assumes "\<And>i::nat. emeasure M (N i) = 0" and "range N \<subseteq> sets M"
   1.713 +  shows "emeasure M (\<Union>i. N i) = 0"
   1.714 +proof -
   1.715 +  have "emeasure M (\<Union>i. N i) \<le> 0"
   1.716 +    using emeasure_subadditive_countably[OF assms(2)] assms(1) by simp
   1.717 +  then show ?thesis
   1.718 +    by (auto intro: antisym zero_le)
   1.719 +qed
   1.720 +
   1.721 +lemma measure_eqI_finite:
   1.722 +  assumes [simp]: "sets M = Pow A" "sets N = Pow A" and "finite A"
   1.723 +  assumes eq: "\<And>a. a \<in> A \<Longrightarrow> emeasure M {a} = emeasure N {a}"
   1.724 +  shows "M = N"
   1.725 +proof (rule measure_eqI)
   1.726 +  fix X assume "X \<in> sets M"
   1.727 +  then have X: "X \<subseteq> A" by auto
   1.728 +  then have "emeasure M X = (\<Sum>a\<in>X. emeasure M {a})"
   1.729 +    using \<open>finite A\<close> by (subst emeasure_eq_setsum_singleton) (auto dest: finite_subset)
   1.730 +  also have "\<dots> = (\<Sum>a\<in>X. emeasure N {a})"
   1.731 +    using X eq by (auto intro!: setsum.cong)
   1.732 +  also have "\<dots> = emeasure N X"
   1.733 +    using X \<open>finite A\<close> by (subst emeasure_eq_setsum_singleton) (auto dest: finite_subset)
   1.734 +  finally show "emeasure M X = emeasure N X" .
   1.735 +qed simp
   1.736 +
   1.737 +lemma measure_eqI_generator_eq:
   1.738 +  fixes M N :: "'a measure" and E :: "'a set set" and A :: "nat \<Rightarrow> 'a set"
   1.739 +  assumes "Int_stable E" "E \<subseteq> Pow \<Omega>"
   1.740 +  and eq: "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
   1.741 +  and M: "sets M = sigma_sets \<Omega> E"
   1.742 +  and N: "sets N = sigma_sets \<Omega> E"
   1.743 +  and A: "range A \<subseteq> E" "(\<Union>i. A i) = \<Omega>" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   1.744 +  shows "M = N"
   1.745 +proof -
   1.746 +  let ?\<mu>  = "emeasure M" and ?\<nu> = "emeasure N"
   1.747 +  interpret S: sigma_algebra \<Omega> "sigma_sets \<Omega> E" by (rule sigma_algebra_sigma_sets) fact
   1.748 +  have "space M = \<Omega>"
   1.749 +    using sets.top[of M] sets.space_closed[of M] S.top S.space_closed \<open>sets M = sigma_sets \<Omega> E\<close>
   1.750 +    by blast
   1.751 +
   1.752 +  { fix F D assume "F \<in> E" and "?\<mu> F \<noteq> \<infinity>"
   1.753 +    then have [intro]: "F \<in> sigma_sets \<Omega> E" by auto
   1.754 +    have "?\<nu> F \<noteq> \<infinity>" using \<open>?\<mu> F \<noteq> \<infinity>\<close> \<open>F \<in> E\<close> eq by simp
   1.755 +    assume "D \<in> sets M"
   1.756 +    with \<open>Int_stable E\<close> \<open>E \<subseteq> Pow \<Omega>\<close> have "emeasure M (F \<inter> D) = emeasure N (F \<inter> D)"
   1.757 +      unfolding M
   1.758 +    proof (induct rule: sigma_sets_induct_disjoint)
   1.759 +      case (basic A)
   1.760 +      then have "F \<inter> A \<in> E" using \<open>Int_stable E\<close> \<open>F \<in> E\<close> by (auto simp: Int_stable_def)
   1.761 +      then show ?case using eq by auto
   1.762 +    next
   1.763 +      case empty then show ?case by simp
   1.764 +    next
   1.765 +      case (compl A)
   1.766 +      then have **: "F \<inter> (\<Omega> - A) = F - (F \<inter> A)"
   1.767 +        and [intro]: "F \<inter> A \<in> sigma_sets \<Omega> E"
   1.768 +        using \<open>F \<in> E\<close> S.sets_into_space by (auto simp: M)
   1.769 +      have "?\<nu> (F \<inter> A) \<le> ?\<nu> F" by (auto intro!: emeasure_mono simp: M N)
   1.770 +      then have "?\<nu> (F \<inter> A) \<noteq> \<infinity>" using \<open>?\<nu> F \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   1.771 +      have "?\<mu> (F \<inter> A) \<le> ?\<mu> F" by (auto intro!: emeasure_mono simp: M N)
   1.772 +      then have "?\<mu> (F \<inter> A) \<noteq> \<infinity>" using \<open>?\<mu> F \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   1.773 +      then have "?\<mu> (F \<inter> (\<Omega> - A)) = ?\<mu> F - ?\<mu> (F \<inter> A)" unfolding **
   1.774 +        using \<open>F \<inter> A \<in> sigma_sets \<Omega> E\<close> by (auto intro!: emeasure_Diff simp: M N)
   1.775 +      also have "\<dots> = ?\<nu> F - ?\<nu> (F \<inter> A)" using eq \<open>F \<in> E\<close> compl by simp
   1.776 +      also have "\<dots> = ?\<nu> (F \<inter> (\<Omega> - A))" unfolding **
   1.777 +        using \<open>F \<inter> A \<in> sigma_sets \<Omega> E\<close> \<open>?\<nu> (F \<inter> A) \<noteq> \<infinity>\<close>
   1.778 +        by (auto intro!: emeasure_Diff[symmetric] simp: M N)
   1.779 +      finally show ?case
   1.780 +        using \<open>space M = \<Omega>\<close> by auto
   1.781 +    next
   1.782 +      case (union A)
   1.783 +      then have "?\<mu> (\<Union>x. F \<inter> A x) = ?\<nu> (\<Union>x. F \<inter> A x)"
   1.784 +        by (subst (1 2) suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def subset_eq M N)
   1.785 +      with A show ?case
   1.786 +        by auto
   1.787 +    qed }
   1.788 +  note * = this
   1.789 +  show "M = N"
   1.790 +  proof (rule measure_eqI)
   1.791 +    show "sets M = sets N"
   1.792 +      using M N by simp
   1.793 +    have [simp, intro]: "\<And>i. A i \<in> sets M"
   1.794 +      using A(1) by (auto simp: subset_eq M)
   1.795 +    fix F assume "F \<in> sets M"
   1.796 +    let ?D = "disjointed (\<lambda>i. F \<inter> A i)"
   1.797 +    from \<open>space M = \<Omega>\<close> have F_eq: "F = (\<Union>i. ?D i)"
   1.798 +      using \<open>F \<in> sets M\<close>[THEN sets.sets_into_space] A(2)[symmetric] by (auto simp: UN_disjointed_eq)
   1.799 +    have [simp, intro]: "\<And>i. ?D i \<in> sets M"
   1.800 +      using sets.range_disjointed_sets[of "\<lambda>i. F \<inter> A i" M] \<open>F \<in> sets M\<close>
   1.801 +      by (auto simp: subset_eq)
   1.802 +    have "disjoint_family ?D"
   1.803 +      by (auto simp: disjoint_family_disjointed)
   1.804 +    moreover
   1.805 +    have "(\<Sum>i. emeasure M (?D i)) = (\<Sum>i. emeasure N (?D i))"
   1.806 +    proof (intro arg_cong[where f=suminf] ext)
   1.807 +      fix i
   1.808 +      have "A i \<inter> ?D i = ?D i"
   1.809 +        by (auto simp: disjointed_def)
   1.810 +      then show "emeasure M (?D i) = emeasure N (?D i)"
   1.811 +        using *[of "A i" "?D i", OF _ A(3)] A(1) by auto
   1.812 +    qed
   1.813 +    ultimately show "emeasure M F = emeasure N F"
   1.814 +      by (simp add: image_subset_iff \<open>sets M = sets N\<close>[symmetric] F_eq[symmetric] suminf_emeasure)
   1.815 +  qed
   1.816 +qed
   1.817 +
   1.818 +lemma measure_of_of_measure: "measure_of (space M) (sets M) (emeasure M) = M"
   1.819 +proof (intro measure_eqI emeasure_measure_of_sigma)
   1.820 +  show "sigma_algebra (space M) (sets M)" ..
   1.821 +  show "positive (sets M) (emeasure M)"
   1.822 +    by (simp add: positive_def)
   1.823 +  show "countably_additive (sets M) (emeasure M)"
   1.824 +    by (simp add: emeasure_countably_additive)
   1.825 +qed simp_all
   1.826 +
   1.827 +subsection \<open>\<open>\<mu>\<close>-null sets\<close>
   1.828 +
   1.829 +definition null_sets :: "'a measure \<Rightarrow> 'a set set" where
   1.830 +  "null_sets M = {N\<in>sets M. emeasure M N = 0}"
   1.831 +
   1.832 +lemma null_setsD1[dest]: "A \<in> null_sets M \<Longrightarrow> emeasure M A = 0"
   1.833 +  by (simp add: null_sets_def)
   1.834 +
   1.835 +lemma null_setsD2[dest]: "A \<in> null_sets M \<Longrightarrow> A \<in> sets M"
   1.836 +  unfolding null_sets_def by simp
   1.837 +
   1.838 +lemma null_setsI[intro]: "emeasure M A = 0 \<Longrightarrow> A \<in> sets M \<Longrightarrow> A \<in> null_sets M"
   1.839 +  unfolding null_sets_def by simp
   1.840 +
   1.841 +interpretation null_sets: ring_of_sets "space M" "null_sets M" for M
   1.842 +proof (rule ring_of_setsI)
   1.843 +  show "null_sets M \<subseteq> Pow (space M)"
   1.844 +    using sets.sets_into_space by auto
   1.845 +  show "{} \<in> null_sets M"
   1.846 +    by auto
   1.847 +  fix A B assume null_sets: "A \<in> null_sets M" "B \<in> null_sets M"
   1.848 +  then have sets: "A \<in> sets M" "B \<in> sets M"
   1.849 +    by auto
   1.850 +  then have *: "emeasure M (A \<union> B) \<le> emeasure M A + emeasure M B"
   1.851 +    "emeasure M (A - B) \<le> emeasure M A"
   1.852 +    by (auto intro!: emeasure_subadditive emeasure_mono)
   1.853 +  then have "emeasure M B = 0" "emeasure M A = 0"
   1.854 +    using null_sets by auto
   1.855 +  with sets * show "A - B \<in> null_sets M" "A \<union> B \<in> null_sets M"
   1.856 +    by (auto intro!: antisym zero_le)
   1.857 +qed
   1.858 +
   1.859 +lemma UN_from_nat_into:
   1.860 +  assumes I: "countable I" "I \<noteq> {}"
   1.861 +  shows "(\<Union>i\<in>I. N i) = (\<Union>i. N (from_nat_into I i))"
   1.862 +proof -
   1.863 +  have "(\<Union>i\<in>I. N i) = \<Union>(N ` range (from_nat_into I))"
   1.864 +    using I by simp
   1.865 +  also have "\<dots> = (\<Union>i. (N \<circ> from_nat_into I) i)"
   1.866 +    by simp
   1.867 +  finally show ?thesis by simp
   1.868 +qed
   1.869 +
   1.870 +lemma null_sets_UN':
   1.871 +  assumes "countable I"
   1.872 +  assumes "\<And>i. i \<in> I \<Longrightarrow> N i \<in> null_sets M"
   1.873 +  shows "(\<Union>i\<in>I. N i) \<in> null_sets M"
   1.874 +proof cases
   1.875 +  assume "I = {}" then show ?thesis by simp
   1.876 +next
   1.877 +  assume "I \<noteq> {}"
   1.878 +  show ?thesis
   1.879 +  proof (intro conjI CollectI null_setsI)
   1.880 +    show "(\<Union>i\<in>I. N i) \<in> sets M"
   1.881 +      using assms by (intro sets.countable_UN') auto
   1.882 +    have "emeasure M (\<Union>i\<in>I. N i) \<le> (\<Sum>n. emeasure M (N (from_nat_into I n)))"
   1.883 +      unfolding UN_from_nat_into[OF \<open>countable I\<close> \<open>I \<noteq> {}\<close>]
   1.884 +      using assms \<open>I \<noteq> {}\<close> by (intro emeasure_subadditive_countably) (auto intro: from_nat_into)
   1.885 +    also have "(\<lambda>n. emeasure M (N (from_nat_into I n))) = (\<lambda>_. 0)"
   1.886 +      using assms \<open>I \<noteq> {}\<close> by (auto intro: from_nat_into)
   1.887 +    finally show "emeasure M (\<Union>i\<in>I. N i) = 0"
   1.888 +      by (intro antisym zero_le) simp
   1.889 +  qed
   1.890 +qed
   1.891 +
   1.892 +lemma null_sets_UN[intro]:
   1.893 +  "(\<And>i::'i::countable. N i \<in> null_sets M) \<Longrightarrow> (\<Union>i. N i) \<in> null_sets M"
   1.894 +  by (rule null_sets_UN') auto
   1.895 +
   1.896 +lemma null_set_Int1:
   1.897 +  assumes "B \<in> null_sets M" "A \<in> sets M" shows "A \<inter> B \<in> null_sets M"
   1.898 +proof (intro CollectI conjI null_setsI)
   1.899 +  show "emeasure M (A \<inter> B) = 0" using assms
   1.900 +    by (intro emeasure_eq_0[of B _ "A \<inter> B"]) auto
   1.901 +qed (insert assms, auto)
   1.902 +
   1.903 +lemma null_set_Int2:
   1.904 +  assumes "B \<in> null_sets M" "A \<in> sets M" shows "B \<inter> A \<in> null_sets M"
   1.905 +  using assms by (subst Int_commute) (rule null_set_Int1)
   1.906 +
   1.907 +lemma emeasure_Diff_null_set:
   1.908 +  assumes "B \<in> null_sets M" "A \<in> sets M"
   1.909 +  shows "emeasure M (A - B) = emeasure M A"
   1.910 +proof -
   1.911 +  have *: "A - B = (A - (A \<inter> B))" by auto
   1.912 +  have "A \<inter> B \<in> null_sets M" using assms by (rule null_set_Int1)
   1.913 +  then show ?thesis
   1.914 +    unfolding * using assms
   1.915 +    by (subst emeasure_Diff) auto
   1.916 +qed
   1.917 +
   1.918 +lemma null_set_Diff:
   1.919 +  assumes "B \<in> null_sets M" "A \<in> sets M" shows "B - A \<in> null_sets M"
   1.920 +proof (intro CollectI conjI null_setsI)
   1.921 +  show "emeasure M (B - A) = 0" using assms by (intro emeasure_eq_0[of B _ "B - A"]) auto
   1.922 +qed (insert assms, auto)
   1.923 +
   1.924 +lemma emeasure_Un_null_set:
   1.925 +  assumes "A \<in> sets M" "B \<in> null_sets M"
   1.926 +  shows "emeasure M (A \<union> B) = emeasure M A"
   1.927 +proof -
   1.928 +  have *: "A \<union> B = A \<union> (B - A)" by auto
   1.929 +  have "B - A \<in> null_sets M" using assms(2,1) by (rule null_set_Diff)
   1.930 +  then show ?thesis
   1.931 +    unfolding * using assms
   1.932 +    by (subst plus_emeasure[symmetric]) auto
   1.933 +qed
   1.934 +
   1.935 +subsection \<open>The almost everywhere filter (i.e.\ quantifier)\<close>
   1.936 +
   1.937 +definition ae_filter :: "'a measure \<Rightarrow> 'a filter" where
   1.938 +  "ae_filter M = (INF N:null_sets M. principal (space M - N))"
   1.939 +
   1.940 +abbreviation almost_everywhere :: "'a measure \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   1.941 +  "almost_everywhere M P \<equiv> eventually P (ae_filter M)"
   1.942 +
   1.943 +syntax
   1.944 +  "_almost_everywhere" :: "pttrn \<Rightarrow> 'a \<Rightarrow> bool \<Rightarrow> bool" ("AE _ in _. _" [0,0,10] 10)
   1.945 +
   1.946 +translations
   1.947 +  "AE x in M. P" \<rightleftharpoons> "CONST almost_everywhere M (\<lambda>x. P)"
   1.948 +
   1.949 +lemma eventually_ae_filter: "eventually P (ae_filter M) \<longleftrightarrow> (\<exists>N\<in>null_sets M. {x \<in> space M. \<not> P x} \<subseteq> N)"
   1.950 +  unfolding ae_filter_def by (subst eventually_INF_base) (auto simp: eventually_principal subset_eq)
   1.951 +
   1.952 +lemma AE_I':
   1.953 +  "N \<in> null_sets M \<Longrightarrow> {x\<in>space M. \<not> P x} \<subseteq> N \<Longrightarrow> (AE x in M. P x)"
   1.954 +  unfolding eventually_ae_filter by auto
   1.955 +
   1.956 +lemma AE_iff_null:
   1.957 +  assumes "{x\<in>space M. \<not> P x} \<in> sets M" (is "?P \<in> sets M")
   1.958 +  shows "(AE x in M. P x) \<longleftrightarrow> {x\<in>space M. \<not> P x} \<in> null_sets M"
   1.959 +proof
   1.960 +  assume "AE x in M. P x" then obtain N where N: "N \<in> sets M" "?P \<subseteq> N" "emeasure M N = 0"
   1.961 +    unfolding eventually_ae_filter by auto
   1.962 +  have "emeasure M ?P \<le> emeasure M N"
   1.963 +    using assms N(1,2) by (auto intro: emeasure_mono)
   1.964 +  then have "emeasure M ?P = 0"
   1.965 +    unfolding \<open>emeasure M N = 0\<close> by auto
   1.966 +  then show "?P \<in> null_sets M" using assms by auto
   1.967 +next
   1.968 +  assume "?P \<in> null_sets M" with assms show "AE x in M. P x" by (auto intro: AE_I')
   1.969 +qed
   1.970 +
   1.971 +lemma AE_iff_null_sets:
   1.972 +  "N \<in> sets M \<Longrightarrow> N \<in> null_sets M \<longleftrightarrow> (AE x in M. x \<notin> N)"
   1.973 +  using Int_absorb1[OF sets.sets_into_space, of N M]
   1.974 +  by (subst AE_iff_null) (auto simp: Int_def[symmetric])
   1.975 +
   1.976 +lemma AE_not_in:
   1.977 +  "N \<in> null_sets M \<Longrightarrow> AE x in M. x \<notin> N"
   1.978 +  by (metis AE_iff_null_sets null_setsD2)
   1.979 +
   1.980 +lemma AE_iff_measurable:
   1.981 +  "N \<in> sets M \<Longrightarrow> {x\<in>space M. \<not> P x} = N \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> emeasure M N = 0"
   1.982 +  using AE_iff_null[of _ P] by auto
   1.983 +
   1.984 +lemma AE_E[consumes 1]:
   1.985 +  assumes "AE x in M. P x"
   1.986 +  obtains N where "{x \<in> space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
   1.987 +  using assms unfolding eventually_ae_filter by auto
   1.988 +
   1.989 +lemma AE_E2:
   1.990 +  assumes "AE x in M. P x" "{x\<in>space M. P x} \<in> sets M"
   1.991 +  shows "emeasure M {x\<in>space M. \<not> P x} = 0" (is "emeasure M ?P = 0")
   1.992 +proof -
   1.993 +  have "{x\<in>space M. \<not> P x} = space M - {x\<in>space M. P x}" by auto
   1.994 +  with AE_iff_null[of M P] assms show ?thesis by auto
   1.995 +qed
   1.996 +
   1.997 +lemma AE_I:
   1.998 +  assumes "{x \<in> space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
   1.999 +  shows "AE x in M. P x"
  1.1000 +  using assms unfolding eventually_ae_filter by auto
  1.1001 +
  1.1002 +lemma AE_mp[elim!]:
  1.1003 +  assumes AE_P: "AE x in M. P x" and AE_imp: "AE x in M. P x \<longrightarrow> Q x"
  1.1004 +  shows "AE x in M. Q x"
  1.1005 +proof -
  1.1006 +  from AE_P obtain A where P: "{x\<in>space M. \<not> P x} \<subseteq> A"
  1.1007 +    and A: "A \<in> sets M" "emeasure M A = 0"
  1.1008 +    by (auto elim!: AE_E)
  1.1009 +
  1.1010 +  from AE_imp obtain B where imp: "{x\<in>space M. P x \<and> \<not> Q x} \<subseteq> B"
  1.1011 +    and B: "B \<in> sets M" "emeasure M B = 0"
  1.1012 +    by (auto elim!: AE_E)
  1.1013 +
  1.1014 +  show ?thesis
  1.1015 +  proof (intro AE_I)
  1.1016 +    have "emeasure M (A \<union> B) \<le> 0"
  1.1017 +      using emeasure_subadditive[of A M B] A B by auto
  1.1018 +    then show "A \<union> B \<in> sets M" "emeasure M (A \<union> B) = 0"
  1.1019 +      using A B by auto
  1.1020 +    show "{x\<in>space M. \<not> Q x} \<subseteq> A \<union> B"
  1.1021 +      using P imp by auto
  1.1022 +  qed
  1.1023 +qed
  1.1024 +
  1.1025 +(* depricated replace by laws about eventually *)
  1.1026 +lemma
  1.1027 +  shows AE_iffI: "AE x in M. P x \<Longrightarrow> AE x in M. P x \<longleftrightarrow> Q x \<Longrightarrow> AE x in M. Q x"
  1.1028 +    and AE_disjI1: "AE x in M. P x \<Longrightarrow> AE x in M. P x \<or> Q x"
  1.1029 +    and AE_disjI2: "AE x in M. Q x \<Longrightarrow> AE x in M. P x \<or> Q x"
  1.1030 +    and AE_conjI: "AE x in M. P x \<Longrightarrow> AE x in M. Q x \<Longrightarrow> AE x in M. P x \<and> Q x"
  1.1031 +    and AE_conj_iff[simp]: "(AE x in M. P x \<and> Q x) \<longleftrightarrow> (AE x in M. P x) \<and> (AE x in M. Q x)"
  1.1032 +  by auto
  1.1033 +
  1.1034 +lemma AE_impI:
  1.1035 +  "(P \<Longrightarrow> AE x in M. Q x) \<Longrightarrow> AE x in M. P \<longrightarrow> Q x"
  1.1036 +  by (cases P) auto
  1.1037 +
  1.1038 +lemma AE_measure:
  1.1039 +  assumes AE: "AE x in M. P x" and sets: "{x\<in>space M. P x} \<in> sets M" (is "?P \<in> sets M")
  1.1040 +  shows "emeasure M {x\<in>space M. P x} = emeasure M (space M)"
  1.1041 +proof -
  1.1042 +  from AE_E[OF AE] guess N . note N = this
  1.1043 +  with sets have "emeasure M (space M) \<le> emeasure M (?P \<union> N)"
  1.1044 +    by (intro emeasure_mono) auto
  1.1045 +  also have "\<dots> \<le> emeasure M ?P + emeasure M N"
  1.1046 +    using sets N by (intro emeasure_subadditive) auto
  1.1047 +  also have "\<dots> = emeasure M ?P" using N by simp
  1.1048 +  finally show "emeasure M ?P = emeasure M (space M)"
  1.1049 +    using emeasure_space[of M "?P"] by auto
  1.1050 +qed
  1.1051 +
  1.1052 +lemma AE_space: "AE x in M. x \<in> space M"
  1.1053 +  by (rule AE_I[where N="{}"]) auto
  1.1054 +
  1.1055 +lemma AE_I2[simp, intro]:
  1.1056 +  "(\<And>x. x \<in> space M \<Longrightarrow> P x) \<Longrightarrow> AE x in M. P x"
  1.1057 +  using AE_space by force
  1.1058 +
  1.1059 +lemma AE_Ball_mp:
  1.1060 +  "\<forall>x\<in>space M. P x \<Longrightarrow> AE x in M. P x \<longrightarrow> Q x \<Longrightarrow> AE x in M. Q x"
  1.1061 +  by auto
  1.1062 +
  1.1063 +lemma AE_cong[cong]:
  1.1064 +  "(\<And>x. x \<in> space M \<Longrightarrow> P x \<longleftrightarrow> Q x) \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> (AE x in M. Q x)"
  1.1065 +  by auto
  1.1066 +
  1.1067 +lemma AE_all_countable:
  1.1068 +  "(AE x in M. \<forall>i. P i x) \<longleftrightarrow> (\<forall>i::'i::countable. AE x in M. P i x)"
  1.1069 +proof
  1.1070 +  assume "\<forall>i. AE x in M. P i x"
  1.1071 +  from this[unfolded eventually_ae_filter Bex_def, THEN choice]
  1.1072 +  obtain N where N: "\<And>i. N i \<in> null_sets M" "\<And>i. {x\<in>space M. \<not> P i x} \<subseteq> N i" by auto
  1.1073 +  have "{x\<in>space M. \<not> (\<forall>i. P i x)} \<subseteq> (\<Union>i. {x\<in>space M. \<not> P i x})" by auto
  1.1074 +  also have "\<dots> \<subseteq> (\<Union>i. N i)" using N by auto
  1.1075 +  finally have "{x\<in>space M. \<not> (\<forall>i. P i x)} \<subseteq> (\<Union>i. N i)" .
  1.1076 +  moreover from N have "(\<Union>i. N i) \<in> null_sets M"
  1.1077 +    by (intro null_sets_UN) auto
  1.1078 +  ultimately show "AE x in M. \<forall>i. P i x"
  1.1079 +    unfolding eventually_ae_filter by auto
  1.1080 +qed auto
  1.1081 +
  1.1082 +lemma AE_ball_countable:
  1.1083 +  assumes [intro]: "countable X"
  1.1084 +  shows "(AE x in M. \<forall>y\<in>X. P x y) \<longleftrightarrow> (\<forall>y\<in>X. AE x in M. P x y)"
  1.1085 +proof
  1.1086 +  assume "\<forall>y\<in>X. AE x in M. P x y"
  1.1087 +  from this[unfolded eventually_ae_filter Bex_def, THEN bchoice]
  1.1088 +  obtain N where N: "\<And>y. y \<in> X \<Longrightarrow> N y \<in> null_sets M" "\<And>y. y \<in> X \<Longrightarrow> {x\<in>space M. \<not> P x y} \<subseteq> N y"
  1.1089 +    by auto
  1.1090 +  have "{x\<in>space M. \<not> (\<forall>y\<in>X. P x y)} \<subseteq> (\<Union>y\<in>X. {x\<in>space M. \<not> P x y})"
  1.1091 +    by auto
  1.1092 +  also have "\<dots> \<subseteq> (\<Union>y\<in>X. N y)"
  1.1093 +    using N by auto
  1.1094 +  finally have "{x\<in>space M. \<not> (\<forall>y\<in>X. P x y)} \<subseteq> (\<Union>y\<in>X. N y)" .
  1.1095 +  moreover from N have "(\<Union>y\<in>X. N y) \<in> null_sets M"
  1.1096 +    by (intro null_sets_UN') auto
  1.1097 +  ultimately show "AE x in M. \<forall>y\<in>X. P x y"
  1.1098 +    unfolding eventually_ae_filter by auto
  1.1099 +qed auto
  1.1100 +
  1.1101 +lemma AE_discrete_difference:
  1.1102 +  assumes X: "countable X"
  1.1103 +  assumes null: "\<And>x. x \<in> X \<Longrightarrow> emeasure M {x} = 0"
  1.1104 +  assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  1.1105 +  shows "AE x in M. x \<notin> X"
  1.1106 +proof -
  1.1107 +  have "(\<Union>x\<in>X. {x}) \<in> null_sets M"
  1.1108 +    using assms by (intro null_sets_UN') auto
  1.1109 +  from AE_not_in[OF this] show "AE x in M. x \<notin> X"
  1.1110 +    by auto
  1.1111 +qed
  1.1112 +
  1.1113 +lemma AE_finite_all:
  1.1114 +  assumes f: "finite S" shows "(AE x in M. \<forall>i\<in>S. P i x) \<longleftrightarrow> (\<forall>i\<in>S. AE x in M. P i x)"
  1.1115 +  using f by induct auto
  1.1116 +
  1.1117 +lemma AE_finite_allI:
  1.1118 +  assumes "finite S"
  1.1119 +  shows "(\<And>s. s \<in> S \<Longrightarrow> AE x in M. Q s x) \<Longrightarrow> AE x in M. \<forall>s\<in>S. Q s x"
  1.1120 +  using AE_finite_all[OF \<open>finite S\<close>] by auto
  1.1121 +
  1.1122 +lemma emeasure_mono_AE:
  1.1123 +  assumes imp: "AE x in M. x \<in> A \<longrightarrow> x \<in> B"
  1.1124 +    and B: "B \<in> sets M"
  1.1125 +  shows "emeasure M A \<le> emeasure M B"
  1.1126 +proof cases
  1.1127 +  assume A: "A \<in> sets M"
  1.1128 +  from imp obtain N where N: "{x\<in>space M. \<not> (x \<in> A \<longrightarrow> x \<in> B)} \<subseteq> N" "N \<in> null_sets M"
  1.1129 +    by (auto simp: eventually_ae_filter)
  1.1130 +  have "emeasure M A = emeasure M (A - N)"
  1.1131 +    using N A by (subst emeasure_Diff_null_set) auto
  1.1132 +  also have "emeasure M (A - N) \<le> emeasure M (B - N)"
  1.1133 +    using N A B sets.sets_into_space by (auto intro!: emeasure_mono)
  1.1134 +  also have "emeasure M (B - N) = emeasure M B"
  1.1135 +    using N B by (subst emeasure_Diff_null_set) auto
  1.1136 +  finally show ?thesis .
  1.1137 +qed (simp add: emeasure_notin_sets)
  1.1138 +
  1.1139 +lemma emeasure_eq_AE:
  1.1140 +  assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  1.1141 +  assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  1.1142 +  shows "emeasure M A = emeasure M B"
  1.1143 +  using assms by (safe intro!: antisym emeasure_mono_AE) auto
  1.1144 +
  1.1145 +lemma emeasure_Collect_eq_AE:
  1.1146 +  "AE x in M. P x \<longleftrightarrow> Q x \<Longrightarrow> Measurable.pred M Q \<Longrightarrow> Measurable.pred M P \<Longrightarrow>
  1.1147 +   emeasure M {x\<in>space M. P x} = emeasure M {x\<in>space M. Q x}"
  1.1148 +   by (intro emeasure_eq_AE) auto
  1.1149 +
  1.1150 +lemma emeasure_eq_0_AE: "AE x in M. \<not> P x \<Longrightarrow> emeasure M {x\<in>space M. P x} = 0"
  1.1151 +  using AE_iff_measurable[OF _ refl, of M "\<lambda>x. \<not> P x"]
  1.1152 +  by (cases "{x\<in>space M. P x} \<in> sets M") (simp_all add: emeasure_notin_sets)
  1.1153 +
  1.1154 +lemma emeasure_add_AE:
  1.1155 +  assumes [measurable]: "A \<in> sets M" "B \<in> sets M" "C \<in> sets M"
  1.1156 +  assumes 1: "AE x in M. x \<in> C \<longleftrightarrow> x \<in> A \<or> x \<in> B"
  1.1157 +  assumes 2: "AE x in M. \<not> (x \<in> A \<and> x \<in> B)"
  1.1158 +  shows "emeasure M C = emeasure M A + emeasure M B"
  1.1159 +proof -
  1.1160 +  have "emeasure M C = emeasure M (A \<union> B)"
  1.1161 +    by (rule emeasure_eq_AE) (insert 1, auto)
  1.1162 +  also have "\<dots> = emeasure M A + emeasure M (B - A)"
  1.1163 +    by (subst plus_emeasure) auto
  1.1164 +  also have "emeasure M (B - A) = emeasure M B"
  1.1165 +    by (rule emeasure_eq_AE) (insert 2, auto)
  1.1166 +  finally show ?thesis .
  1.1167 +qed
  1.1168 +
  1.1169 +subsection \<open>\<open>\<sigma>\<close>-finite Measures\<close>
  1.1170 +
  1.1171 +locale sigma_finite_measure =
  1.1172 +  fixes M :: "'a measure"
  1.1173 +  assumes sigma_finite_countable:
  1.1174 +    "\<exists>A::'a set set. countable A \<and> A \<subseteq> sets M \<and> (\<Union>A) = space M \<and> (\<forall>a\<in>A. emeasure M a \<noteq> \<infinity>)"
  1.1175 +
  1.1176 +lemma (in sigma_finite_measure) sigma_finite:
  1.1177 +  obtains A :: "nat \<Rightarrow> 'a set"
  1.1178 +  where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1.1179 +proof -
  1.1180 +  obtain A :: "'a set set" where
  1.1181 +    [simp]: "countable A" and
  1.1182 +    A: "A \<subseteq> sets M" "(\<Union>A) = space M" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
  1.1183 +    using sigma_finite_countable by metis
  1.1184 +  show thesis
  1.1185 +  proof cases
  1.1186 +    assume "A = {}" with \<open>(\<Union>A) = space M\<close> show thesis
  1.1187 +      by (intro that[of "\<lambda>_. {}"]) auto
  1.1188 +  next
  1.1189 +    assume "A \<noteq> {}"
  1.1190 +    show thesis
  1.1191 +    proof
  1.1192 +      show "range (from_nat_into A) \<subseteq> sets M"
  1.1193 +        using \<open>A \<noteq> {}\<close> A by auto
  1.1194 +      have "(\<Union>i. from_nat_into A i) = \<Union>A"
  1.1195 +        using range_from_nat_into[OF \<open>A \<noteq> {}\<close> \<open>countable A\<close>] by auto
  1.1196 +      with A show "(\<Union>i. from_nat_into A i) = space M"
  1.1197 +        by auto
  1.1198 +    qed (intro A from_nat_into \<open>A \<noteq> {}\<close>)
  1.1199 +  qed
  1.1200 +qed
  1.1201 +
  1.1202 +lemma (in sigma_finite_measure) sigma_finite_disjoint:
  1.1203 +  obtains A :: "nat \<Rightarrow> 'a set"
  1.1204 +  where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "disjoint_family A"
  1.1205 +proof -
  1.1206 +  obtain A :: "nat \<Rightarrow> 'a set" where
  1.1207 +    range: "range A \<subseteq> sets M" and
  1.1208 +    space: "(\<Union>i. A i) = space M" and
  1.1209 +    measure: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1.1210 +    using sigma_finite by blast
  1.1211 +  show thesis
  1.1212 +  proof (rule that[of "disjointed A"])
  1.1213 +    show "range (disjointed A) \<subseteq> sets M"
  1.1214 +      by (rule sets.range_disjointed_sets[OF range])
  1.1215 +    show "(\<Union>i. disjointed A i) = space M"
  1.1216 +      and "disjoint_family (disjointed A)"
  1.1217 +      using disjoint_family_disjointed UN_disjointed_eq[of A] space range
  1.1218 +      by auto
  1.1219 +    show "emeasure M (disjointed A i) \<noteq> \<infinity>" for i
  1.1220 +    proof -
  1.1221 +      have "emeasure M (disjointed A i) \<le> emeasure M (A i)"
  1.1222 +        using range disjointed_subset[of A i] by (auto intro!: emeasure_mono)
  1.1223 +      then show ?thesis using measure[of i] by (auto simp: top_unique)
  1.1224 +    qed
  1.1225 +  qed
  1.1226 +qed
  1.1227 +
  1.1228 +lemma (in sigma_finite_measure) sigma_finite_incseq:
  1.1229 +  obtains A :: "nat \<Rightarrow> 'a set"
  1.1230 +  where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "incseq A"
  1.1231 +proof -
  1.1232 +  obtain F :: "nat \<Rightarrow> 'a set" where
  1.1233 +    F: "range F \<subseteq> sets M" "(\<Union>i. F i) = space M" "\<And>i. emeasure M (F i) \<noteq> \<infinity>"
  1.1234 +    using sigma_finite by blast
  1.1235 +  show thesis
  1.1236 +  proof (rule that[of "\<lambda>n. \<Union>i\<le>n. F i"])
  1.1237 +    show "range (\<lambda>n. \<Union>i\<le>n. F i) \<subseteq> sets M"
  1.1238 +      using F by (force simp: incseq_def)
  1.1239 +    show "(\<Union>n. \<Union>i\<le>n. F i) = space M"
  1.1240 +    proof -
  1.1241 +      from F have "\<And>x. x \<in> space M \<Longrightarrow> \<exists>i. x \<in> F i" by auto
  1.1242 +      with F show ?thesis by fastforce
  1.1243 +    qed
  1.1244 +    show "emeasure M (\<Union>i\<le>n. F i) \<noteq> \<infinity>" for n
  1.1245 +    proof -
  1.1246 +      have "emeasure M (\<Union>i\<le>n. F i) \<le> (\<Sum>i\<le>n. emeasure M (F i))"
  1.1247 +        using F by (auto intro!: emeasure_subadditive_finite)
  1.1248 +      also have "\<dots> < \<infinity>"
  1.1249 +        using F by (auto simp: setsum_Pinfty less_top)
  1.1250 +      finally show ?thesis by simp
  1.1251 +    qed
  1.1252 +    show "incseq (\<lambda>n. \<Union>i\<le>n. F i)"
  1.1253 +      by (force simp: incseq_def)
  1.1254 +  qed
  1.1255 +qed
  1.1256 +
  1.1257 +subsection \<open>Measure space induced by distribution of @{const measurable}-functions\<close>
  1.1258 +
  1.1259 +definition distr :: "'a measure \<Rightarrow> 'b measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b measure" where
  1.1260 +  "distr M N f = measure_of (space N) (sets N) (\<lambda>A. emeasure M (f -` A \<inter> space M))"
  1.1261 +
  1.1262 +lemma
  1.1263 +  shows sets_distr[simp, measurable_cong]: "sets (distr M N f) = sets N"
  1.1264 +    and space_distr[simp]: "space (distr M N f) = space N"
  1.1265 +  by (auto simp: distr_def)
  1.1266 +
  1.1267 +lemma
  1.1268 +  shows measurable_distr_eq1[simp]: "measurable (distr Mf Nf f) Mf' = measurable Nf Mf'"
  1.1269 +    and measurable_distr_eq2[simp]: "measurable Mg' (distr Mg Ng g) = measurable Mg' Ng"
  1.1270 +  by (auto simp: measurable_def)
  1.1271 +
  1.1272 +lemma distr_cong:
  1.1273 +  "M = K \<Longrightarrow> sets N = sets L \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> distr M N f = distr K L g"
  1.1274 +  using sets_eq_imp_space_eq[of N L] by (simp add: distr_def Int_def cong: rev_conj_cong)
  1.1275 +
  1.1276 +lemma emeasure_distr:
  1.1277 +  fixes f :: "'a \<Rightarrow> 'b"
  1.1278 +  assumes f: "f \<in> measurable M N" and A: "A \<in> sets N"
  1.1279 +  shows "emeasure (distr M N f) A = emeasure M (f -` A \<inter> space M)" (is "_ = ?\<mu> A")
  1.1280 +  unfolding distr_def
  1.1281 +proof (rule emeasure_measure_of_sigma)
  1.1282 +  show "positive (sets N) ?\<mu>"
  1.1283 +    by (auto simp: positive_def)
  1.1284 +
  1.1285 +  show "countably_additive (sets N) ?\<mu>"
  1.1286 +  proof (intro countably_additiveI)
  1.1287 +    fix A :: "nat \<Rightarrow> 'b set" assume "range A \<subseteq> sets N" "disjoint_family A"
  1.1288 +    then have A: "\<And>i. A i \<in> sets N" "(\<Union>i. A i) \<in> sets N" by auto
  1.1289 +    then have *: "range (\<lambda>i. f -` (A i) \<inter> space M) \<subseteq> sets M"
  1.1290 +      using f by (auto simp: measurable_def)
  1.1291 +    moreover have "(\<Union>i. f -`  A i \<inter> space M) \<in> sets M"
  1.1292 +      using * by blast
  1.1293 +    moreover have **: "disjoint_family (\<lambda>i. f -` A i \<inter> space M)"
  1.1294 +      using \<open>disjoint_family A\<close> by (auto simp: disjoint_family_on_def)
  1.1295 +    ultimately show "(\<Sum>i. ?\<mu> (A i)) = ?\<mu> (\<Union>i. A i)"
  1.1296 +      using suminf_emeasure[OF _ **] A f
  1.1297 +      by (auto simp: comp_def vimage_UN)
  1.1298 +  qed
  1.1299 +  show "sigma_algebra (space N) (sets N)" ..
  1.1300 +qed fact
  1.1301 +
  1.1302 +lemma emeasure_Collect_distr:
  1.1303 +  assumes X[measurable]: "X \<in> measurable M N" "Measurable.pred N P"
  1.1304 +  shows "emeasure (distr M N X) {x\<in>space N. P x} = emeasure M {x\<in>space M. P (X x)}"
  1.1305 +  by (subst emeasure_distr)
  1.1306 +     (auto intro!: arg_cong2[where f=emeasure] X(1)[THEN measurable_space])
  1.1307 +
  1.1308 +lemma emeasure_lfp2[consumes 1, case_names cont f measurable]:
  1.1309 +  assumes "P M"
  1.1310 +  assumes cont: "sup_continuous F"
  1.1311 +  assumes f: "\<And>M. P M \<Longrightarrow> f \<in> measurable M' M"
  1.1312 +  assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
  1.1313 +  shows "emeasure M' {x\<in>space M'. lfp F (f x)} = (SUP i. emeasure M' {x\<in>space M'. (F ^^ i) (\<lambda>x. False) (f x)})"
  1.1314 +proof (subst (1 2) emeasure_Collect_distr[symmetric, where X=f])
  1.1315 +  show "f \<in> measurable M' M"  "f \<in> measurable M' M"
  1.1316 +    using f[OF \<open>P M\<close>] by auto
  1.1317 +  { fix i show "Measurable.pred M ((F ^^ i) (\<lambda>x. False))"
  1.1318 +    using \<open>P M\<close> by (induction i arbitrary: M) (auto intro!: *) }
  1.1319 +  show "Measurable.pred M (lfp F)"
  1.1320 +    using \<open>P M\<close> cont * by (rule measurable_lfp_coinduct[of P])
  1.1321 +
  1.1322 +  have "emeasure (distr M' M f) {x \<in> space (distr M' M f). lfp F x} =
  1.1323 +    (SUP i. emeasure (distr M' M f) {x \<in> space (distr M' M f). (F ^^ i) (\<lambda>x. False) x})"
  1.1324 +    using \<open>P M\<close>
  1.1325 +  proof (coinduction arbitrary: M rule: emeasure_lfp')
  1.1326 +    case (measurable A N) then have "\<And>N. P N \<Longrightarrow> Measurable.pred (distr M' N f) A"
  1.1327 +      by metis
  1.1328 +    then have "\<And>N. P N \<Longrightarrow> Measurable.pred N A"
  1.1329 +      by simp
  1.1330 +    with \<open>P N\<close>[THEN *] show ?case
  1.1331 +      by auto
  1.1332 +  qed fact
  1.1333 +  then show "emeasure (distr M' M f) {x \<in> space M. lfp F x} =
  1.1334 +    (SUP i. emeasure (distr M' M f) {x \<in> space M. (F ^^ i) (\<lambda>x. False) x})"
  1.1335 +   by simp
  1.1336 +qed
  1.1337 +
  1.1338 +lemma distr_id[simp]: "distr N N (\<lambda>x. x) = N"
  1.1339 +  by (rule measure_eqI) (auto simp: emeasure_distr)
  1.1340 +
  1.1341 +lemma measure_distr:
  1.1342 +  "f \<in> measurable M N \<Longrightarrow> S \<in> sets N \<Longrightarrow> measure (distr M N f) S = measure M (f -` S \<inter> space M)"
  1.1343 +  by (simp add: emeasure_distr measure_def)
  1.1344 +
  1.1345 +lemma distr_cong_AE:
  1.1346 +  assumes 1: "M = K" "sets N = sets L" and
  1.1347 +    2: "(AE x in M. f x = g x)" and "f \<in> measurable M N" and "g \<in> measurable K L"
  1.1348 +  shows "distr M N f = distr K L g"
  1.1349 +proof (rule measure_eqI)
  1.1350 +  fix A assume "A \<in> sets (distr M N f)"
  1.1351 +  with assms show "emeasure (distr M N f) A = emeasure (distr K L g) A"
  1.1352 +    by (auto simp add: emeasure_distr intro!: emeasure_eq_AE measurable_sets)
  1.1353 +qed (insert 1, simp)
  1.1354 +
  1.1355 +lemma AE_distrD:
  1.1356 +  assumes f: "f \<in> measurable M M'"
  1.1357 +    and AE: "AE x in distr M M' f. P x"
  1.1358 +  shows "AE x in M. P (f x)"
  1.1359 +proof -
  1.1360 +  from AE[THEN AE_E] guess N .
  1.1361 +  with f show ?thesis
  1.1362 +    unfolding eventually_ae_filter
  1.1363 +    by (intro bexI[of _ "f -` N \<inter> space M"])
  1.1364 +       (auto simp: emeasure_distr measurable_def)
  1.1365 +qed
  1.1366 +
  1.1367 +lemma AE_distr_iff:
  1.1368 +  assumes f[measurable]: "f \<in> measurable M N" and P[measurable]: "{x \<in> space N. P x} \<in> sets N"
  1.1369 +  shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
  1.1370 +proof (subst (1 2) AE_iff_measurable[OF _ refl])
  1.1371 +  have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
  1.1372 +    using f[THEN measurable_space] by auto
  1.1373 +  then show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
  1.1374 +    (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
  1.1375 +    by (simp add: emeasure_distr)
  1.1376 +qed auto
  1.1377 +
  1.1378 +lemma null_sets_distr_iff:
  1.1379 +  "f \<in> measurable M N \<Longrightarrow> A \<in> null_sets (distr M N f) \<longleftrightarrow> f -` A \<inter> space M \<in> null_sets M \<and> A \<in> sets N"
  1.1380 +  by (auto simp add: null_sets_def emeasure_distr)
  1.1381 +
  1.1382 +lemma distr_distr:
  1.1383 +  "g \<in> measurable N L \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> distr (distr M N f) L g = distr M L (g \<circ> f)"
  1.1384 +  by (auto simp add: emeasure_distr measurable_space
  1.1385 +           intro!: arg_cong[where f="emeasure M"] measure_eqI)
  1.1386 +
  1.1387 +subsection \<open>Real measure values\<close>
  1.1388 +
  1.1389 +lemma ring_of_finite_sets: "ring_of_sets (space M) {A\<in>sets M. emeasure M A \<noteq> top}"
  1.1390 +proof (rule ring_of_setsI)
  1.1391 +  show "a \<in> {A \<in> sets M. emeasure M A \<noteq> top} \<Longrightarrow> b \<in> {A \<in> sets M. emeasure M A \<noteq> top} \<Longrightarrow>
  1.1392 +    a \<union> b \<in> {A \<in> sets M. emeasure M A \<noteq> top}" for a b
  1.1393 +    using emeasure_subadditive[of a M b] by (auto simp: top_unique)
  1.1394 +
  1.1395 +  show "a \<in> {A \<in> sets M. emeasure M A \<noteq> top} \<Longrightarrow> b \<in> {A \<in> sets M. emeasure M A \<noteq> top} \<Longrightarrow>
  1.1396 +    a - b \<in> {A \<in> sets M. emeasure M A \<noteq> top}" for a b
  1.1397 +    using emeasure_mono[of "a - b" a M] by (auto simp: Diff_subset top_unique)
  1.1398 +qed (auto dest: sets.sets_into_space)
  1.1399 +
  1.1400 +lemma measure_nonneg[simp]: "0 \<le> measure M A"
  1.1401 +  unfolding measure_def by auto
  1.1402 +
  1.1403 +lemma zero_less_measure_iff: "0 < measure M A \<longleftrightarrow> measure M A \<noteq> 0"
  1.1404 +  using measure_nonneg[of M A] by (auto simp add: le_less)
  1.1405 +
  1.1406 +lemma measure_le_0_iff: "measure M X \<le> 0 \<longleftrightarrow> measure M X = 0"
  1.1407 +  using measure_nonneg[of M X] by linarith
  1.1408 +
  1.1409 +lemma measure_empty[simp]: "measure M {} = 0"
  1.1410 +  unfolding measure_def by (simp add: zero_ennreal.rep_eq)
  1.1411 +
  1.1412 +lemma emeasure_eq_ennreal_measure:
  1.1413 +  "emeasure M A \<noteq> top \<Longrightarrow> emeasure M A = ennreal (measure M A)"
  1.1414 +  by (cases "emeasure M A" rule: ennreal_cases) (auto simp: measure_def)
  1.1415 +
  1.1416 +lemma measure_zero_top: "emeasure M A = top \<Longrightarrow> measure M A = 0"
  1.1417 +  by (simp add: measure_def enn2ereal_top)
  1.1418 +
  1.1419 +lemma measure_eq_emeasure_eq_ennreal: "0 \<le> x \<Longrightarrow> emeasure M A = ennreal x \<Longrightarrow> measure M A = x"
  1.1420 +  using emeasure_eq_ennreal_measure[of M A]
  1.1421 +  by (cases "A \<in> M") (auto simp: measure_notin_sets emeasure_notin_sets)
  1.1422 +
  1.1423 +lemma enn2real_plus:"a < top \<Longrightarrow> b < top \<Longrightarrow> enn2real (a + b) = enn2real a + enn2real b"
  1.1424 +  by (simp add: enn2real_def plus_ennreal.rep_eq real_of_ereal_add less_top
  1.1425 +           del: real_of_ereal_enn2ereal)
  1.1426 +
  1.1427 +lemma measure_Union:
  1.1428 +  "emeasure M A \<noteq> \<infinity> \<Longrightarrow> emeasure M B \<noteq> \<infinity> \<Longrightarrow> A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> A \<inter> B = {} \<Longrightarrow>
  1.1429 +    measure M (A \<union> B) = measure M A + measure M B"
  1.1430 +  by (simp add: measure_def plus_emeasure[symmetric] enn2real_plus less_top)
  1.1431 +
  1.1432 +lemma disjoint_family_on_insert:
  1.1433 +  "i \<notin> I \<Longrightarrow> disjoint_family_on A (insert i I) \<longleftrightarrow> A i \<inter> (\<Union>i\<in>I. A i) = {} \<and> disjoint_family_on A I"
  1.1434 +  by (fastforce simp: disjoint_family_on_def)
  1.1435 +
  1.1436 +lemma measure_finite_Union:
  1.1437 +  "finite S \<Longrightarrow> A`S \<subseteq> sets M \<Longrightarrow> disjoint_family_on A S \<Longrightarrow> (\<And>i. i \<in> S \<Longrightarrow> emeasure M (A i) \<noteq> \<infinity>) \<Longrightarrow>
  1.1438 +    measure M (\<Union>i\<in>S. A i) = (\<Sum>i\<in>S. measure M (A i))"
  1.1439 +  by (induction S rule: finite_induct)
  1.1440 +     (auto simp: disjoint_family_on_insert measure_Union setsum_emeasure[symmetric] sets.countable_UN'[OF countable_finite])
  1.1441 +
  1.1442 +lemma measure_Diff:
  1.1443 +  assumes finite: "emeasure M A \<noteq> \<infinity>"
  1.1444 +  and measurable: "A \<in> sets M" "B \<in> sets M" "B \<subseteq> A"
  1.1445 +  shows "measure M (A - B) = measure M A - measure M B"
  1.1446 +proof -
  1.1447 +  have "emeasure M (A - B) \<le> emeasure M A" "emeasure M B \<le> emeasure M A"
  1.1448 +    using measurable by (auto intro!: emeasure_mono)
  1.1449 +  hence "measure M ((A - B) \<union> B) = measure M (A - B) + measure M B"
  1.1450 +    using measurable finite by (rule_tac measure_Union) (auto simp: top_unique)
  1.1451 +  thus ?thesis using \<open>B \<subseteq> A\<close> by (auto simp: Un_absorb2)
  1.1452 +qed
  1.1453 +
  1.1454 +lemma measure_UNION:
  1.1455 +  assumes measurable: "range A \<subseteq> sets M" "disjoint_family A"
  1.1456 +  assumes finite: "emeasure M (\<Union>i. A i) \<noteq> \<infinity>"
  1.1457 +  shows "(\<lambda>i. measure M (A i)) sums (measure M (\<Union>i. A i))"
  1.1458 +proof -
  1.1459 +  have "(\<lambda>i. emeasure M (A i)) sums (emeasure M (\<Union>i. A i))"
  1.1460 +    unfolding suminf_emeasure[OF measurable, symmetric] by (simp add: summable_sums)
  1.1461 +  moreover
  1.1462 +  { fix i
  1.1463 +    have "emeasure M (A i) \<le> emeasure M (\<Union>i. A i)"
  1.1464 +      using measurable by (auto intro!: emeasure_mono)
  1.1465 +    then have "emeasure M (A i) = ennreal ((measure M (A i)))"
  1.1466 +      using finite by (intro emeasure_eq_ennreal_measure) (auto simp: top_unique) }
  1.1467 +  ultimately show ?thesis using finite
  1.1468 +    by (subst (asm) (2) emeasure_eq_ennreal_measure) simp_all
  1.1469 +qed
  1.1470 +
  1.1471 +lemma measure_subadditive:
  1.1472 +  assumes measurable: "A \<in> sets M" "B \<in> sets M"
  1.1473 +  and fin: "emeasure M A \<noteq> \<infinity>" "emeasure M B \<noteq> \<infinity>"
  1.1474 +  shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  1.1475 +proof -
  1.1476 +  have "emeasure M (A \<union> B) \<noteq> \<infinity>"
  1.1477 +    using emeasure_subadditive[OF measurable] fin by (auto simp: top_unique)
  1.1478 +  then show "(measure M (A \<union> B)) \<le> (measure M A) + (measure M B)"
  1.1479 +    using emeasure_subadditive[OF measurable] fin
  1.1480 +    apply simp
  1.1481 +    apply (subst (asm) (2 3 4) emeasure_eq_ennreal_measure)
  1.1482 +    apply (auto simp add: ennreal_plus[symmetric] simp del: ennreal_plus)
  1.1483 +    done
  1.1484 +qed
  1.1485 +
  1.1486 +lemma measure_subadditive_finite:
  1.1487 +  assumes A: "finite I" "A`I \<subseteq> sets M" and fin: "\<And>i. i \<in> I \<Longrightarrow> emeasure M (A i) \<noteq> \<infinity>"
  1.1488 +  shows "measure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. measure M (A i))"
  1.1489 +proof -
  1.1490 +  { have "emeasure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. emeasure M (A i))"
  1.1491 +      using emeasure_subadditive_finite[OF A] .
  1.1492 +    also have "\<dots> < \<infinity>"
  1.1493 +      using fin by (simp add: less_top A)
  1.1494 +    finally have "emeasure M (\<Union>i\<in>I. A i) \<noteq> top" by simp }
  1.1495 +  note * = this
  1.1496 +  show ?thesis
  1.1497 +    using emeasure_subadditive_finite[OF A] fin
  1.1498 +    unfolding emeasure_eq_ennreal_measure[OF *]
  1.1499 +    by (simp_all add: setsum_nonneg emeasure_eq_ennreal_measure)
  1.1500 +qed
  1.1501 +
  1.1502 +lemma measure_subadditive_countably:
  1.1503 +  assumes A: "range A \<subseteq> sets M" and fin: "(\<Sum>i. emeasure M (A i)) \<noteq> \<infinity>"
  1.1504 +  shows "measure M (\<Union>i. A i) \<le> (\<Sum>i. measure M (A i))"
  1.1505 +proof -
  1.1506 +  from fin have **: "\<And>i. emeasure M (A i) \<noteq> top"
  1.1507 +    using ennreal_suminf_lessD[of "\<lambda>i. emeasure M (A i)"] by (simp add: less_top)
  1.1508 +  { have "emeasure M (\<Union>i. A i) \<le> (\<Sum>i. emeasure M (A i))"
  1.1509 +      using emeasure_subadditive_countably[OF A] .
  1.1510 +    also have "\<dots> < \<infinity>"
  1.1511 +      using fin by (simp add: less_top)
  1.1512 +    finally have "emeasure M (\<Union>i. A i) \<noteq> top" by simp }
  1.1513 +  then have "ennreal (measure M (\<Union>i. A i)) = emeasure M (\<Union>i. A i)"
  1.1514 +    by (rule emeasure_eq_ennreal_measure[symmetric])
  1.1515 +  also have "\<dots> \<le> (\<Sum>i. emeasure M (A i))"
  1.1516 +    using emeasure_subadditive_countably[OF A] .
  1.1517 +  also have "\<dots> = ennreal (\<Sum>i. measure M (A i))"
  1.1518 +    using fin unfolding emeasure_eq_ennreal_measure[OF **]
  1.1519 +    by (subst suminf_ennreal) (auto simp: **)
  1.1520 +  finally show ?thesis
  1.1521 +    apply (rule ennreal_le_iff[THEN iffD1, rotated])
  1.1522 +    apply (intro suminf_nonneg allI measure_nonneg summable_suminf_not_top)
  1.1523 +    using fin
  1.1524 +    apply (simp add: emeasure_eq_ennreal_measure[OF **])
  1.1525 +    done
  1.1526 +qed
  1.1527 +
  1.1528 +lemma measure_eq_setsum_singleton:
  1.1529 +  "finite S \<Longrightarrow> (\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M) \<Longrightarrow> (\<And>x. x \<in> S \<Longrightarrow> emeasure M {x} \<noteq> \<infinity>) \<Longrightarrow>
  1.1530 +    measure M S = (\<Sum>x\<in>S. measure M {x})"
  1.1531 +  using emeasure_eq_setsum_singleton[of S M]
  1.1532 +  by (intro measure_eq_emeasure_eq_ennreal) (auto simp: setsum_nonneg emeasure_eq_ennreal_measure)
  1.1533 +
  1.1534 +lemma Lim_measure_incseq:
  1.1535 +  assumes A: "range A \<subseteq> sets M" "incseq A" and fin: "emeasure M (\<Union>i. A i) \<noteq> \<infinity>"
  1.1536 +  shows "(\<lambda>i. measure M (A i)) \<longlonglongrightarrow> measure M (\<Union>i. A i)"
  1.1537 +proof (rule tendsto_ennrealD)
  1.1538 +  have "ennreal (measure M (\<Union>i. A i)) = emeasure M (\<Union>i. A i)"
  1.1539 +    using fin by (auto simp: emeasure_eq_ennreal_measure)
  1.1540 +  moreover have "ennreal (measure M (A i)) = emeasure M (A i)" for i
  1.1541 +    using assms emeasure_mono[of "A _" "\<Union>i. A i" M]
  1.1542 +    by (intro emeasure_eq_ennreal_measure[symmetric]) (auto simp: less_top UN_upper intro: le_less_trans)
  1.1543 +  ultimately show "(\<lambda>x. ennreal (Sigma_Algebra.measure M (A x))) \<longlonglongrightarrow> ennreal (Sigma_Algebra.measure M (\<Union>i. A i))"
  1.1544 +    using A by (auto intro!: Lim_emeasure_incseq)
  1.1545 +qed auto
  1.1546 +
  1.1547 +lemma Lim_measure_decseq:
  1.1548 +  assumes A: "range A \<subseteq> sets M" "decseq A" and fin: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1.1549 +  shows "(\<lambda>n. measure M (A n)) \<longlonglongrightarrow> measure M (\<Inter>i. A i)"
  1.1550 +proof (rule tendsto_ennrealD)
  1.1551 +  have "ennreal (measure M (\<Inter>i. A i)) = emeasure M (\<Inter>i. A i)"
  1.1552 +    using fin[of 0] A emeasure_mono[of "\<Inter>i. A i" "A 0" M]
  1.1553 +    by (auto intro!: emeasure_eq_ennreal_measure[symmetric] simp: INT_lower less_top intro: le_less_trans)
  1.1554 +  moreover have "ennreal (measure M (A i)) = emeasure M (A i)" for i
  1.1555 +    using A fin[of i] by (intro emeasure_eq_ennreal_measure[symmetric]) auto
  1.1556 +  ultimately show "(\<lambda>x. ennreal (Sigma_Algebra.measure M (A x))) \<longlonglongrightarrow> ennreal (Sigma_Algebra.measure M (\<Inter>i. A i))"
  1.1557 +    using fin A by (auto intro!: Lim_emeasure_decseq)
  1.1558 +qed auto
  1.1559 +
  1.1560 +subsection \<open>Measure spaces with @{term "emeasure M (space M) < \<infinity>"}\<close>
  1.1561 +
  1.1562 +locale finite_measure = sigma_finite_measure M for M +
  1.1563 +  assumes finite_emeasure_space: "emeasure M (space M) \<noteq> top"
  1.1564 +
  1.1565 +lemma finite_measureI[Pure.intro!]:
  1.1566 +  "emeasure M (space M) \<noteq> \<infinity> \<Longrightarrow> finite_measure M"
  1.1567 +  proof qed (auto intro!: exI[of _ "{space M}"])
  1.1568 +
  1.1569 +lemma (in finite_measure) emeasure_finite[simp, intro]: "emeasure M A \<noteq> top"
  1.1570 +  using finite_emeasure_space emeasure_space[of M A] by (auto simp: top_unique)
  1.1571 +
  1.1572 +lemma (in finite_measure) emeasure_eq_measure: "emeasure M A = ennreal (measure M A)"
  1.1573 +  by (intro emeasure_eq_ennreal_measure) simp
  1.1574 +
  1.1575 +lemma (in finite_measure) emeasure_real: "\<exists>r. 0 \<le> r \<and> emeasure M A = ennreal r"
  1.1576 +  using emeasure_finite[of A] by (cases "emeasure M A" rule: ennreal_cases) auto
  1.1577 +
  1.1578 +lemma (in finite_measure) bounded_measure: "measure M A \<le> measure M (space M)"
  1.1579 +  using emeasure_space[of M A] emeasure_real[of A] emeasure_real[of "space M"] by (auto simp: measure_def)
  1.1580 +
  1.1581 +lemma (in finite_measure) finite_measure_Diff:
  1.1582 +  assumes sets: "A \<in> sets M" "B \<in> sets M" and "B \<subseteq> A"
  1.1583 +  shows "measure M (A - B) = measure M A - measure M B"
  1.1584 +  using measure_Diff[OF _ assms] by simp
  1.1585 +
  1.1586 +lemma (in finite_measure) finite_measure_Union:
  1.1587 +  assumes sets: "A \<in> sets M" "B \<in> sets M" and "A \<inter> B = {}"
  1.1588 +  shows "measure M (A \<union> B) = measure M A + measure M B"
  1.1589 +  using measure_Union[OF _ _ assms] by simp
  1.1590 +
  1.1591 +lemma (in finite_measure) finite_measure_finite_Union:
  1.1592 +  assumes measurable: "finite S" "A`S \<subseteq> sets M" "disjoint_family_on A S"
  1.1593 +  shows "measure M (\<Union>i\<in>S. A i) = (\<Sum>i\<in>S. measure M (A i))"
  1.1594 +  using measure_finite_Union[OF assms] by simp
  1.1595 +
  1.1596 +lemma (in finite_measure) finite_measure_UNION:
  1.1597 +  assumes A: "range A \<subseteq> sets M" "disjoint_family A"
  1.1598 +  shows "(\<lambda>i. measure M (A i)) sums (measure M (\<Union>i. A i))"
  1.1599 +  using measure_UNION[OF A] by simp
  1.1600 +
  1.1601 +lemma (in finite_measure) finite_measure_mono:
  1.1602 +  assumes "A \<subseteq> B" "B \<in> sets M" shows "measure M A \<le> measure M B"
  1.1603 +  using emeasure_mono[OF assms] emeasure_real[of A] emeasure_real[of B] by (auto simp: measure_def)
  1.1604 +
  1.1605 +lemma (in finite_measure) finite_measure_subadditive:
  1.1606 +  assumes m: "A \<in> sets M" "B \<in> sets M"
  1.1607 +  shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  1.1608 +  using measure_subadditive[OF m] by simp
  1.1609 +
  1.1610 +lemma (in finite_measure) finite_measure_subadditive_finite:
  1.1611 +  assumes "finite I" "A`I \<subseteq> sets M" shows "measure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. measure M (A i))"
  1.1612 +  using measure_subadditive_finite[OF assms] by simp
  1.1613 +
  1.1614 +lemma (in finite_measure) finite_measure_subadditive_countably:
  1.1615 +  "range A \<subseteq> sets M \<Longrightarrow> summable (\<lambda>i. measure M (A i)) \<Longrightarrow> measure M (\<Union>i. A i) \<le> (\<Sum>i. measure M (A i))"
  1.1616 +  by (rule measure_subadditive_countably)
  1.1617 +     (simp_all add: ennreal_suminf_neq_top emeasure_eq_measure)
  1.1618 +
  1.1619 +lemma (in finite_measure) finite_measure_eq_setsum_singleton:
  1.1620 +  assumes "finite S" and *: "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
  1.1621 +  shows "measure M S = (\<Sum>x\<in>S. measure M {x})"
  1.1622 +  using measure_eq_setsum_singleton[OF assms] by simp
  1.1623 +
  1.1624 +lemma (in finite_measure) finite_Lim_measure_incseq:
  1.1625 +  assumes A: "range A \<subseteq> sets M" "incseq A"
  1.1626 +  shows "(\<lambda>i. measure M (A i)) \<longlonglongrightarrow> measure M (\<Union>i. A i)"
  1.1627 +  using Lim_measure_incseq[OF A] by simp
  1.1628 +
  1.1629 +lemma (in finite_measure) finite_Lim_measure_decseq:
  1.1630 +  assumes A: "range A \<subseteq> sets M" "decseq A"
  1.1631 +  shows "(\<lambda>n. measure M (A n)) \<longlonglongrightarrow> measure M (\<Inter>i. A i)"
  1.1632 +  using Lim_measure_decseq[OF A] by simp
  1.1633 +
  1.1634 +lemma (in finite_measure) finite_measure_compl:
  1.1635 +  assumes S: "S \<in> sets M"
  1.1636 +  shows "measure M (space M - S) = measure M (space M) - measure M S"
  1.1637 +  using measure_Diff[OF _ sets.top S sets.sets_into_space] S by simp
  1.1638 +
  1.1639 +lemma (in finite_measure) finite_measure_mono_AE:
  1.1640 +  assumes imp: "AE x in M. x \<in> A \<longrightarrow> x \<in> B" and B: "B \<in> sets M"
  1.1641 +  shows "measure M A \<le> measure M B"
  1.1642 +  using assms emeasure_mono_AE[OF imp B]
  1.1643 +  by (simp add: emeasure_eq_measure)
  1.1644 +
  1.1645 +lemma (in finite_measure) finite_measure_eq_AE:
  1.1646 +  assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  1.1647 +  assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  1.1648 +  shows "measure M A = measure M B"
  1.1649 +  using assms emeasure_eq_AE[OF assms] by (simp add: emeasure_eq_measure)
  1.1650 +
  1.1651 +lemma (in finite_measure) measure_increasing: "increasing M (measure M)"
  1.1652 +  by (auto intro!: finite_measure_mono simp: increasing_def)
  1.1653 +
  1.1654 +lemma (in finite_measure) measure_zero_union:
  1.1655 +  assumes "s \<in> sets M" "t \<in> sets M" "measure M t = 0"
  1.1656 +  shows "measure M (s \<union> t) = measure M s"
  1.1657 +using assms
  1.1658 +proof -
  1.1659 +  have "measure M (s \<union> t) \<le> measure M s"
  1.1660 +    using finite_measure_subadditive[of s t] assms by auto
  1.1661 +  moreover have "measure M (s \<union> t) \<ge> measure M s"
  1.1662 +    using assms by (blast intro: finite_measure_mono)
  1.1663 +  ultimately show ?thesis by simp
  1.1664 +qed
  1.1665 +
  1.1666 +lemma (in finite_measure) measure_eq_compl:
  1.1667 +  assumes "s \<in> sets M" "t \<in> sets M"
  1.1668 +  assumes "measure M (space M - s) = measure M (space M - t)"
  1.1669 +  shows "measure M s = measure M t"
  1.1670 +  using assms finite_measure_compl by auto
  1.1671 +
  1.1672 +lemma (in finite_measure) measure_eq_bigunion_image:
  1.1673 +  assumes "range f \<subseteq> sets M" "range g \<subseteq> sets M"
  1.1674 +  assumes "disjoint_family f" "disjoint_family g"
  1.1675 +  assumes "\<And> n :: nat. measure M (f n) = measure M (g n)"
  1.1676 +  shows "measure M (\<Union>i. f i) = measure M (\<Union>i. g i)"
  1.1677 +using assms
  1.1678 +proof -
  1.1679 +  have a: "(\<lambda> i. measure M (f i)) sums (measure M (\<Union>i. f i))"
  1.1680 +    by (rule finite_measure_UNION[OF assms(1,3)])
  1.1681 +  have b: "(\<lambda> i. measure M (g i)) sums (measure M (\<Union>i. g i))"
  1.1682 +    by (rule finite_measure_UNION[OF assms(2,4)])
  1.1683 +  show ?thesis using sums_unique[OF b] sums_unique[OF a] assms by simp
  1.1684 +qed
  1.1685 +
  1.1686 +lemma (in finite_measure) measure_countably_zero:
  1.1687 +  assumes "range c \<subseteq> sets M"
  1.1688 +  assumes "\<And> i. measure M (c i) = 0"
  1.1689 +  shows "measure M (\<Union>i :: nat. c i) = 0"
  1.1690 +proof (rule antisym)
  1.1691 +  show "measure M (\<Union>i :: nat. c i) \<le> 0"
  1.1692 +    using finite_measure_subadditive_countably[OF assms(1)] by (simp add: assms(2))
  1.1693 +qed simp
  1.1694 +
  1.1695 +lemma (in finite_measure) measure_space_inter:
  1.1696 +  assumes events:"s \<in> sets M" "t \<in> sets M"
  1.1697 +  assumes "measure M t = measure M (space M)"
  1.1698 +  shows "measure M (s \<inter> t) = measure M s"
  1.1699 +proof -
  1.1700 +  have "measure M ((space M - s) \<union> (space M - t)) = measure M (space M - s)"
  1.1701 +    using events assms finite_measure_compl[of "t"] by (auto intro!: measure_zero_union)
  1.1702 +  also have "(space M - s) \<union> (space M - t) = space M - (s \<inter> t)"
  1.1703 +    by blast
  1.1704 +  finally show "measure M (s \<inter> t) = measure M s"
  1.1705 +    using events by (auto intro!: measure_eq_compl[of "s \<inter> t" s])
  1.1706 +qed
  1.1707 +
  1.1708 +lemma (in finite_measure) measure_equiprobable_finite_unions:
  1.1709 +  assumes s: "finite s" "\<And>x. x \<in> s \<Longrightarrow> {x} \<in> sets M"
  1.1710 +  assumes "\<And> x y. \<lbrakk>x \<in> s; y \<in> s\<rbrakk> \<Longrightarrow> measure M {x} = measure M {y}"
  1.1711 +  shows "measure M s = real (card s) * measure M {SOME x. x \<in> s}"
  1.1712 +proof cases
  1.1713 +  assume "s \<noteq> {}"
  1.1714 +  then have "\<exists> x. x \<in> s" by blast
  1.1715 +  from someI_ex[OF this] assms
  1.1716 +  have prob_some: "\<And> x. x \<in> s \<Longrightarrow> measure M {x} = measure M {SOME y. y \<in> s}" by blast
  1.1717 +  have "measure M s = (\<Sum> x \<in> s. measure M {x})"
  1.1718 +    using finite_measure_eq_setsum_singleton[OF s] by simp
  1.1719 +  also have "\<dots> = (\<Sum> x \<in> s. measure M {SOME y. y \<in> s})" using prob_some by auto
  1.1720 +  also have "\<dots> = real (card s) * measure M {(SOME x. x \<in> s)}"
  1.1721 +    using setsum_constant assms by simp
  1.1722 +  finally show ?thesis by simp
  1.1723 +qed simp
  1.1724 +
  1.1725 +lemma (in finite_measure) measure_real_sum_image_fn:
  1.1726 +  assumes "e \<in> sets M"
  1.1727 +  assumes "\<And> x. x \<in> s \<Longrightarrow> e \<inter> f x \<in> sets M"
  1.1728 +  assumes "finite s"
  1.1729 +  assumes disjoint: "\<And> x y. \<lbrakk>x \<in> s ; y \<in> s ; x \<noteq> y\<rbrakk> \<Longrightarrow> f x \<inter> f y = {}"
  1.1730 +  assumes upper: "space M \<subseteq> (\<Union>i \<in> s. f i)"
  1.1731 +  shows "measure M e = (\<Sum> x \<in> s. measure M (e \<inter> f x))"
  1.1732 +proof -
  1.1733 +  have "e \<subseteq> (\<Union>i\<in>s. f i)"
  1.1734 +    using \<open>e \<in> sets M\<close> sets.sets_into_space upper by blast
  1.1735 +  then have e: "e = (\<Union>i \<in> s. e \<inter> f i)"
  1.1736 +    by auto
  1.1737 +  hence "measure M e = measure M (\<Union>i \<in> s. e \<inter> f i)" by simp
  1.1738 +  also have "\<dots> = (\<Sum> x \<in> s. measure M (e \<inter> f x))"
  1.1739 +  proof (rule finite_measure_finite_Union)
  1.1740 +    show "finite s" by fact
  1.1741 +    show "(\<lambda>i. e \<inter> f i)`s \<subseteq> sets M" using assms(2) by auto
  1.1742 +    show "disjoint_family_on (\<lambda>i. e \<inter> f i) s"
  1.1743 +      using disjoint by (auto simp: disjoint_family_on_def)
  1.1744 +  qed
  1.1745 +  finally show ?thesis .
  1.1746 +qed
  1.1747 +
  1.1748 +lemma (in finite_measure) measure_exclude:
  1.1749 +  assumes "A \<in> sets M" "B \<in> sets M"
  1.1750 +  assumes "measure M A = measure M (space M)" "A \<inter> B = {}"
  1.1751 +  shows "measure M B = 0"
  1.1752 +  using measure_space_inter[of B A] assms by (auto simp: ac_simps)
  1.1753 +lemma (in finite_measure) finite_measure_distr:
  1.1754 +  assumes f: "f \<in> measurable M M'"
  1.1755 +  shows "finite_measure (distr M M' f)"
  1.1756 +proof (rule finite_measureI)
  1.1757 +  have "f -` space M' \<inter> space M = space M" using f by (auto dest: measurable_space)
  1.1758 +  with f show "emeasure (distr M M' f) (space (distr M M' f)) \<noteq> \<infinity>" by (auto simp: emeasure_distr)
  1.1759 +qed
  1.1760 +
  1.1761 +lemma emeasure_gfp[consumes 1, case_names cont measurable]:
  1.1762 +  assumes sets[simp]: "\<And>s. sets (M s) = sets N"
  1.1763 +  assumes "\<And>s. finite_measure (M s)"
  1.1764 +  assumes cont: "inf_continuous F" "inf_continuous f"
  1.1765 +  assumes meas: "\<And>P. Measurable.pred N P \<Longrightarrow> Measurable.pred N (F P)"
  1.1766 +  assumes iter: "\<And>P s. Measurable.pred N P \<Longrightarrow> emeasure (M s) {x\<in>space N. F P x} = f (\<lambda>s. emeasure (M s) {x\<in>space N. P x}) s"
  1.1767 +  assumes bound: "\<And>P. f P \<le> f (\<lambda>s. emeasure (M s) (space (M s)))"
  1.1768 +  shows "emeasure (M s) {x\<in>space N. gfp F x} = gfp f s"
  1.1769 +proof (subst gfp_transfer_bounded[where \<alpha>="\<lambda>F s. emeasure (M s) {x\<in>space N. F x}" and g=f and f=F and
  1.1770 +    P="Measurable.pred N", symmetric])
  1.1771 +  interpret finite_measure "M s" for s by fact
  1.1772 +  fix C assume "decseq C" "\<And>i. Measurable.pred N (C i)"
  1.1773 +  then show "(\<lambda>s. emeasure (M s) {x \<in> space N. (INF i. C i) x}) = (INF i. (\<lambda>s. emeasure (M s) {x \<in> space N. C i x}))"
  1.1774 +    unfolding INF_apply[abs_def]
  1.1775 +    by (subst INF_emeasure_decseq) (auto simp: antimono_def fun_eq_iff intro!: arg_cong2[where f=emeasure])
  1.1776 +next
  1.1777 +  show "f x \<le> (\<lambda>s. emeasure (M s) {x \<in> space N. F top x})" for x
  1.1778 +    using bound[of x] sets_eq_imp_space_eq[OF sets] by (simp add: iter)
  1.1779 +qed (auto simp add: iter le_fun_def INF_apply[abs_def] intro!: meas cont)
  1.1780 +
  1.1781 +subsection \<open>Counting space\<close>
  1.1782 +
  1.1783 +lemma strict_monoI_Suc:
  1.1784 +  assumes ord [simp]: "(\<And>n. f n < f (Suc n))" shows "strict_mono f"
  1.1785 +  unfolding strict_mono_def
  1.1786 +proof safe
  1.1787 +  fix n m :: nat assume "n < m" then show "f n < f m"
  1.1788 +    by (induct m) (auto simp: less_Suc_eq intro: less_trans ord)
  1.1789 +qed
  1.1790 +
  1.1791 +lemma emeasure_count_space:
  1.1792 +  assumes "X \<subseteq> A" shows "emeasure (count_space A) X = (if finite X then of_nat (card X) else \<infinity>)"
  1.1793 +    (is "_ = ?M X")
  1.1794 +  unfolding count_space_def
  1.1795 +proof (rule emeasure_measure_of_sigma)
  1.1796 +  show "X \<in> Pow A" using \<open>X \<subseteq> A\<close> by auto
  1.1797 +  show "sigma_algebra A (Pow A)" by (rule sigma_algebra_Pow)
  1.1798 +  show positive: "positive (Pow A) ?M"
  1.1799 +    by (auto simp: positive_def)
  1.1800 +  have additive: "additive (Pow A) ?M"
  1.1801 +    by (auto simp: card_Un_disjoint additive_def)
  1.1802 +
  1.1803 +  interpret ring_of_sets A "Pow A"
  1.1804 +    by (rule ring_of_setsI) auto
  1.1805 +  show "countably_additive (Pow A) ?M"
  1.1806 +    unfolding countably_additive_iff_continuous_from_below[OF positive additive]
  1.1807 +  proof safe
  1.1808 +    fix F :: "nat \<Rightarrow> 'a set" assume "incseq F"
  1.1809 +    show "(\<lambda>i. ?M (F i)) \<longlonglongrightarrow> ?M (\<Union>i. F i)"
  1.1810 +    proof cases
  1.1811 +      assume "\<exists>i. \<forall>j\<ge>i. F i = F j"
  1.1812 +      then guess i .. note i = this
  1.1813 +      { fix j from i \<open>incseq F\<close> have "F j \<subseteq> F i"
  1.1814 +          by (cases "i \<le> j") (auto simp: incseq_def) }
  1.1815 +      then have eq: "(\<Union>i. F i) = F i"
  1.1816 +        by auto
  1.1817 +      with i show ?thesis
  1.1818 +        by (auto intro!: Lim_transform_eventually[OF _ tendsto_const] eventually_sequentiallyI[where c=i])
  1.1819 +    next
  1.1820 +      assume "\<not> (\<exists>i. \<forall>j\<ge>i. F i = F j)"
  1.1821 +      then obtain f where f: "\<And>i. i \<le> f i" "\<And>i. F i \<noteq> F (f i)" by metis
  1.1822 +      then have "\<And>i. F i \<subseteq> F (f i)" using \<open>incseq F\<close> by (auto simp: incseq_def)
  1.1823 +      with f have *: "\<And>i. F i \<subset> F (f i)" by auto
  1.1824 +
  1.1825 +      have "incseq (\<lambda>i. ?M (F i))"
  1.1826 +        using \<open>incseq F\<close> unfolding incseq_def by (auto simp: card_mono dest: finite_subset)
  1.1827 +      then have "(\<lambda>i. ?M (F i)) \<longlonglongrightarrow> (SUP n. ?M (F n))"
  1.1828 +        by (rule LIMSEQ_SUP)
  1.1829 +
  1.1830 +      moreover have "(SUP n. ?M (F n)) = top"
  1.1831 +      proof (rule ennreal_SUP_eq_top)
  1.1832 +        fix n :: nat show "\<exists>k::nat\<in>UNIV. of_nat n \<le> ?M (F k)"
  1.1833 +        proof (induct n)
  1.1834 +          case (Suc n)
  1.1835 +          then guess k .. note k = this
  1.1836 +          moreover have "finite (F k) \<Longrightarrow> finite (F (f k)) \<Longrightarrow> card (F k) < card (F (f k))"
  1.1837 +            using \<open>F k \<subset> F (f k)\<close> by (simp add: psubset_card_mono)
  1.1838 +          moreover have "finite (F (f k)) \<Longrightarrow> finite (F k)"
  1.1839 +            using \<open>k \<le> f k\<close> \<open>incseq F\<close> by (auto simp: incseq_def dest: finite_subset)
  1.1840 +          ultimately show ?case
  1.1841 +            by (auto intro!: exI[of _ "f k"] simp del: of_nat_Suc)
  1.1842 +        qed auto
  1.1843 +      qed
  1.1844 +
  1.1845 +      moreover
  1.1846 +      have "inj (\<lambda>n. F ((f ^^ n) 0))"
  1.1847 +        by (intro strict_mono_imp_inj_on strict_monoI_Suc) (simp add: *)
  1.1848 +      then have 1: "infinite (range (\<lambda>i. F ((f ^^ i) 0)))"
  1.1849 +        by (rule range_inj_infinite)
  1.1850 +      have "infinite (Pow (\<Union>i. F i))"
  1.1851 +        by (rule infinite_super[OF _ 1]) auto
  1.1852 +      then have "infinite (\<Union>i. F i)"
  1.1853 +        by auto
  1.1854 +
  1.1855 +      ultimately show ?thesis by auto
  1.1856 +    qed
  1.1857 +  qed
  1.1858 +qed
  1.1859 +
  1.1860 +lemma distr_bij_count_space:
  1.1861 +  assumes f: "bij_betw f A B"
  1.1862 +  shows "distr (count_space A) (count_space B) f = count_space B"
  1.1863 +proof (rule measure_eqI)
  1.1864 +  have f': "f \<in> measurable (count_space A) (count_space B)"
  1.1865 +    using f unfolding Pi_def bij_betw_def by auto
  1.1866 +  fix X assume "X \<in> sets (distr (count_space A) (count_space B) f)"
  1.1867 +  then have X: "X \<in> sets (count_space B)" by auto
  1.1868 +  moreover from X have "f -` X \<inter> A = the_inv_into A f ` X"
  1.1869 +    using f by (auto simp: bij_betw_def subset_image_iff image_iff the_inv_into_f_f intro: the_inv_into_f_f[symmetric])
  1.1870 +  moreover have "inj_on (the_inv_into A f) B"
  1.1871 +    using X f by (auto simp: bij_betw_def inj_on_the_inv_into)
  1.1872 +  with X have "inj_on (the_inv_into A f) X"
  1.1873 +    by (auto intro: subset_inj_on)
  1.1874 +  ultimately show "emeasure (distr (count_space A) (count_space B) f) X = emeasure (count_space B) X"
  1.1875 +    using f unfolding emeasure_distr[OF f' X]
  1.1876 +    by (subst (1 2) emeasure_count_space) (auto simp: card_image dest: finite_imageD)
  1.1877 +qed simp
  1.1878 +
  1.1879 +lemma emeasure_count_space_finite[simp]:
  1.1880 +  "X \<subseteq> A \<Longrightarrow> finite X \<Longrightarrow> emeasure (count_space A) X = of_nat (card X)"
  1.1881 +  using emeasure_count_space[of X A] by simp
  1.1882 +
  1.1883 +lemma emeasure_count_space_infinite[simp]:
  1.1884 +  "X \<subseteq> A \<Longrightarrow> infinite X \<Longrightarrow> emeasure (count_space A) X = \<infinity>"
  1.1885 +  using emeasure_count_space[of X A] by simp
  1.1886 +
  1.1887 +lemma measure_count_space: "measure (count_space A) X = (if X \<subseteq> A then of_nat (card X) else 0)"
  1.1888 +  by (cases "finite X") (auto simp: measure_notin_sets ennreal_of_nat_eq_real_of_nat
  1.1889 +                                    measure_zero_top measure_eq_emeasure_eq_ennreal)
  1.1890 +
  1.1891 +lemma emeasure_count_space_eq_0:
  1.1892 +  "emeasure (count_space A) X = 0 \<longleftrightarrow> (X \<subseteq> A \<longrightarrow> X = {})"
  1.1893 +proof cases
  1.1894 +  assume X: "X \<subseteq> A"
  1.1895 +  then show ?thesis
  1.1896 +  proof (intro iffI impI)
  1.1897 +    assume "emeasure (count_space A) X = 0"
  1.1898 +    with X show "X = {}"
  1.1899 +      by (subst (asm) emeasure_count_space) (auto split: if_split_asm)
  1.1900 +  qed simp
  1.1901 +qed (simp add: emeasure_notin_sets)
  1.1902 +
  1.1903 +lemma space_empty: "space M = {} \<Longrightarrow> M = count_space {}"
  1.1904 +  by (rule measure_eqI) (simp_all add: space_empty_iff)
  1.1905 +
  1.1906 +lemma null_sets_count_space: "null_sets (count_space A) = { {} }"
  1.1907 +  unfolding null_sets_def by (auto simp add: emeasure_count_space_eq_0)
  1.1908 +
  1.1909 +lemma AE_count_space: "(AE x in count_space A. P x) \<longleftrightarrow> (\<forall>x\<in>A. P x)"
  1.1910 +  unfolding eventually_ae_filter by (auto simp add: null_sets_count_space)
  1.1911 +
  1.1912 +lemma sigma_finite_measure_count_space_countable:
  1.1913 +  assumes A: "countable A"
  1.1914 +  shows "sigma_finite_measure (count_space A)"
  1.1915 +  proof qed (insert A, auto intro!: exI[of _ "(\<lambda>a. {a}) ` A"])
  1.1916 +
  1.1917 +lemma sigma_finite_measure_count_space:
  1.1918 +  fixes A :: "'a::countable set" shows "sigma_finite_measure (count_space A)"
  1.1919 +  by (rule sigma_finite_measure_count_space_countable) auto
  1.1920 +
  1.1921 +lemma finite_measure_count_space:
  1.1922 +  assumes [simp]: "finite A"
  1.1923 +  shows "finite_measure (count_space A)"
  1.1924 +  by rule simp
  1.1925 +
  1.1926 +lemma sigma_finite_measure_count_space_finite:
  1.1927 +  assumes A: "finite A" shows "sigma_finite_measure (count_space A)"
  1.1928 +proof -
  1.1929 +  interpret finite_measure "count_space A" using A by (rule finite_measure_count_space)
  1.1930 +  show "sigma_finite_measure (count_space A)" ..
  1.1931 +qed
  1.1932 +
  1.1933 +subsection \<open>Measure restricted to space\<close>
  1.1934 +
  1.1935 +lemma emeasure_restrict_space:
  1.1936 +  assumes "\<Omega> \<inter> space M \<in> sets M" "A \<subseteq> \<Omega>"
  1.1937 +  shows "emeasure (restrict_space M \<Omega>) A = emeasure M A"
  1.1938 +proof (cases "A \<in> sets M")
  1.1939 +  case True
  1.1940 +  show ?thesis
  1.1941 +  proof (rule emeasure_measure_of[OF restrict_space_def])
  1.1942 +    show "op \<inter> \<Omega> ` sets M \<subseteq> Pow (\<Omega> \<inter> space M)" "A \<in> sets (restrict_space M \<Omega>)"
  1.1943 +      using \<open>A \<subseteq> \<Omega>\<close> \<open>A \<in> sets M\<close> sets.space_closed by (auto simp: sets_restrict_space)
  1.1944 +    show "positive (sets (restrict_space M \<Omega>)) (emeasure M)"
  1.1945 +      by (auto simp: positive_def)
  1.1946 +    show "countably_additive (sets (restrict_space M \<Omega>)) (emeasure M)"
  1.1947 +    proof (rule countably_additiveI)
  1.1948 +      fix A :: "nat \<Rightarrow> _" assume "range A \<subseteq> sets (restrict_space M \<Omega>)" "disjoint_family A"
  1.1949 +      with assms have "\<And>i. A i \<in> sets M" "\<And>i. A i \<subseteq> space M" "disjoint_family A"
  1.1950 +        by (fastforce simp: sets_restrict_space_iff[OF assms(1)] image_subset_iff
  1.1951 +                      dest: sets.sets_into_space)+
  1.1952 +      then show "(\<Sum>i. emeasure M (A i)) = emeasure M (\<Union>i. A i)"
  1.1953 +        by (subst suminf_emeasure) (auto simp: disjoint_family_subset)
  1.1954 +    qed
  1.1955 +  qed
  1.1956 +next
  1.1957 +  case False
  1.1958 +  with assms have "A \<notin> sets (restrict_space M \<Omega>)"
  1.1959 +    by (simp add: sets_restrict_space_iff)
  1.1960 +  with False show ?thesis
  1.1961 +    by (simp add: emeasure_notin_sets)
  1.1962 +qed
  1.1963 +
  1.1964 +lemma measure_restrict_space:
  1.1965 +  assumes "\<Omega> \<inter> space M \<in> sets M" "A \<subseteq> \<Omega>"
  1.1966 +  shows "measure (restrict_space M \<Omega>) A = measure M A"
  1.1967 +  using emeasure_restrict_space[OF assms] by (simp add: measure_def)
  1.1968 +
  1.1969 +lemma AE_restrict_space_iff:
  1.1970 +  assumes "\<Omega> \<inter> space M \<in> sets M"
  1.1971 +  shows "(AE x in restrict_space M \<Omega>. P x) \<longleftrightarrow> (AE x in M. x \<in> \<Omega> \<longrightarrow> P x)"
  1.1972 +proof -
  1.1973 +  have ex_cong: "\<And>P Q f. (\<And>x. P x \<Longrightarrow> Q x) \<Longrightarrow> (\<And>x. Q x \<Longrightarrow> P (f x)) \<Longrightarrow> (\<exists>x. P x) \<longleftrightarrow> (\<exists>x. Q x)"
  1.1974 +    by auto
  1.1975 +  { fix X assume X: "X \<in> sets M" "emeasure M X = 0"
  1.1976 +    then have "emeasure M (\<Omega> \<inter> space M \<inter> X) \<le> emeasure M X"
  1.1977 +      by (intro emeasure_mono) auto
  1.1978 +    then have "emeasure M (\<Omega> \<inter> space M \<inter> X) = 0"
  1.1979 +      using X by (auto intro!: antisym) }
  1.1980 +  with assms show ?thesis
  1.1981 +    unfolding eventually_ae_filter
  1.1982 +    by (auto simp add: space_restrict_space null_sets_def sets_restrict_space_iff
  1.1983 +                       emeasure_restrict_space cong: conj_cong
  1.1984 +             intro!: ex_cong[where f="\<lambda>X. (\<Omega> \<inter> space M) \<inter> X"])
  1.1985 +qed
  1.1986 +
  1.1987 +lemma restrict_restrict_space:
  1.1988 +  assumes "A \<inter> space M \<in> sets M" "B \<inter> space M \<in> sets M"
  1.1989 +  shows "restrict_space (restrict_space M A) B = restrict_space M (A \<inter> B)" (is "?l = ?r")
  1.1990 +proof (rule measure_eqI[symmetric])
  1.1991 +  show "sets ?r = sets ?l"
  1.1992 +    unfolding sets_restrict_space image_comp by (intro image_cong) auto
  1.1993 +next
  1.1994 +  fix X assume "X \<in> sets (restrict_space M (A \<inter> B))"
  1.1995 +  then obtain Y where "Y \<in> sets M" "X = Y \<inter> A \<inter> B"
  1.1996 +    by (auto simp: sets_restrict_space)
  1.1997 +  with assms sets.Int[OF assms] show "emeasure ?r X = emeasure ?l X"
  1.1998 +    by (subst (1 2) emeasure_restrict_space)
  1.1999 +       (auto simp: space_restrict_space sets_restrict_space_iff emeasure_restrict_space ac_simps)
  1.2000 +qed
  1.2001 +
  1.2002 +lemma restrict_count_space: "restrict_space (count_space B) A = count_space (A \<inter> B)"
  1.2003 +proof (rule measure_eqI)
  1.2004 +  show "sets (restrict_space (count_space B) A) = sets (count_space (A \<inter> B))"
  1.2005 +    by (subst sets_restrict_space) auto
  1.2006 +  moreover fix X assume "X \<in> sets (restrict_space (count_space B) A)"
  1.2007 +  ultimately have "X \<subseteq> A \<inter> B" by auto
  1.2008 +  then show "emeasure (restrict_space (count_space B) A) X = emeasure (count_space (A \<inter> B)) X"
  1.2009 +    by (cases "finite X") (auto simp add: emeasure_restrict_space)
  1.2010 +qed
  1.2011 +
  1.2012 +lemma sigma_finite_measure_restrict_space:
  1.2013 +  assumes "sigma_finite_measure M"
  1.2014 +  and A: "A \<in> sets M"
  1.2015 +  shows "sigma_finite_measure (restrict_space M A)"
  1.2016 +proof -
  1.2017 +  interpret sigma_finite_measure M by fact
  1.2018 +  from sigma_finite_countable obtain C
  1.2019 +    where C: "countable C" "C \<subseteq> sets M" "(\<Union>C) = space M" "\<forall>a\<in>C. emeasure M a \<noteq> \<infinity>"
  1.2020 +    by blast
  1.2021 +  let ?C = "op \<inter> A ` C"
  1.2022 +  from C have "countable ?C" "?C \<subseteq> sets (restrict_space M A)" "(\<Union>?C) = space (restrict_space M A)"
  1.2023 +    by(auto simp add: sets_restrict_space space_restrict_space)
  1.2024 +  moreover {
  1.2025 +    fix a
  1.2026 +    assume "a \<in> ?C"
  1.2027 +    then obtain a' where "a = A \<inter> a'" "a' \<in> C" ..
  1.2028 +    then have "emeasure (restrict_space M A) a \<le> emeasure M a'"
  1.2029 +      using A C by(auto simp add: emeasure_restrict_space intro: emeasure_mono)
  1.2030 +    also have "\<dots> < \<infinity>" using C(4)[rule_format, of a'] \<open>a' \<in> C\<close> by (simp add: less_top)
  1.2031 +    finally have "emeasure (restrict_space M A) a \<noteq> \<infinity>" by simp }
  1.2032 +  ultimately show ?thesis
  1.2033 +    by unfold_locales (rule exI conjI|assumption|blast)+
  1.2034 +qed
  1.2035 +
  1.2036 +lemma finite_measure_restrict_space:
  1.2037 +  assumes "finite_measure M"
  1.2038 +  and A: "A \<in> sets M"
  1.2039 +  shows "finite_measure (restrict_space M A)"
  1.2040 +proof -
  1.2041 +  interpret finite_measure M by fact
  1.2042 +  show ?thesis
  1.2043 +    by(rule finite_measureI)(simp add: emeasure_restrict_space A space_restrict_space)
  1.2044 +qed
  1.2045 +
  1.2046 +lemma restrict_distr:
  1.2047 +  assumes [measurable]: "f \<in> measurable M N"
  1.2048 +  assumes [simp]: "\<Omega> \<inter> space N \<in> sets N" and restrict: "f \<in> space M \<rightarrow> \<Omega>"
  1.2049 +  shows "restrict_space (distr M N f) \<Omega> = distr M (restrict_space N \<Omega>) f"
  1.2050 +  (is "?l = ?r")
  1.2051 +proof (rule measure_eqI)
  1.2052 +  fix A assume "A \<in> sets (restrict_space (distr M N f) \<Omega>)"
  1.2053 +  with restrict show "emeasure ?l A = emeasure ?r A"
  1.2054 +    by (subst emeasure_distr)
  1.2055 +       (auto simp: sets_restrict_space_iff emeasure_restrict_space emeasure_distr
  1.2056 +             intro!: measurable_restrict_space2)
  1.2057 +qed (simp add: sets_restrict_space)
  1.2058 +
  1.2059 +lemma measure_eqI_restrict_generator:
  1.2060 +  assumes E: "Int_stable E" "E \<subseteq> Pow \<Omega>" "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
  1.2061 +  assumes sets_eq: "sets M = sets N" and \<Omega>: "\<Omega> \<in> sets M"
  1.2062 +  assumes "sets (restrict_space M \<Omega>) = sigma_sets \<Omega> E"
  1.2063 +  assumes "sets (restrict_space N \<Omega>) = sigma_sets \<Omega> E"
  1.2064 +  assumes ae: "AE x in M. x \<in> \<Omega>" "AE x in N. x \<in> \<Omega>"
  1.2065 +  assumes A: "countable A" "A \<noteq> {}" "A \<subseteq> E" "\<Union>A = \<Omega>" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
  1.2066 +  shows "M = N"
  1.2067 +proof (rule measure_eqI)
  1.2068 +  fix X assume X: "X \<in> sets M"
  1.2069 +  then have "emeasure M X = emeasure (restrict_space M \<Omega>) (X \<inter> \<Omega>)"
  1.2070 +    using ae \<Omega> by (auto simp add: emeasure_restrict_space intro!: emeasure_eq_AE)
  1.2071 +  also have "restrict_space M \<Omega> = restrict_space N \<Omega>"
  1.2072 +  proof (rule measure_eqI_generator_eq)
  1.2073 +    fix X assume "X \<in> E"
  1.2074 +    then show "emeasure (restrict_space M \<Omega>) X = emeasure (restrict_space N \<Omega>) X"
  1.2075 +      using E \<Omega> by (subst (1 2) emeasure_restrict_space) (auto simp: sets_eq sets_eq[THEN sets_eq_imp_space_eq])
  1.2076 +  next
  1.2077 +    show "range (from_nat_into A) \<subseteq> E" "(\<Union>i. from_nat_into A i) = \<Omega>"
  1.2078 +      using A by (auto cong del: strong_SUP_cong)
  1.2079 +  next
  1.2080 +    fix i
  1.2081 +    have "emeasure (restrict_space M \<Omega>) (from_nat_into A i) = emeasure M (from_nat_into A i)"
  1.2082 +      using A \<Omega> by (subst emeasure_restrict_space)
  1.2083 +                   (auto simp: sets_eq sets_eq[THEN sets_eq_imp_space_eq] intro: from_nat_into)
  1.2084 +    with A show "emeasure (restrict_space M \<Omega>) (from_nat_into A i) \<noteq> \<infinity>"
  1.2085 +      by (auto intro: from_nat_into)
  1.2086 +  qed fact+
  1.2087 +  also have "emeasure (restrict_space N \<Omega>) (X \<inter> \<Omega>) = emeasure N X"
  1.2088 +    using X ae \<Omega> by (auto simp add: emeasure_restrict_space sets_eq intro!: emeasure_eq_AE)
  1.2089 +  finally show "emeasure M X = emeasure N X" .
  1.2090 +qed fact
  1.2091 +
  1.2092 +subsection \<open>Null measure\<close>
  1.2093 +
  1.2094 +definition "null_measure M = sigma (space M) (sets M)"
  1.2095 +
  1.2096 +lemma space_null_measure[simp]: "space (null_measure M) = space M"
  1.2097 +  by (simp add: null_measure_def)
  1.2098 +
  1.2099 +lemma sets_null_measure[simp, measurable_cong]: "sets (null_measure M) = sets M"
  1.2100 +  by (simp add: null_measure_def)
  1.2101 +
  1.2102 +lemma emeasure_null_measure[simp]: "emeasure (null_measure M) X = 0"
  1.2103 +  by (cases "X \<in> sets M", rule emeasure_measure_of)
  1.2104 +     (auto simp: positive_def countably_additive_def emeasure_notin_sets null_measure_def
  1.2105 +           dest: sets.sets_into_space)
  1.2106 +
  1.2107 +lemma measure_null_measure[simp]: "measure (null_measure M) X = 0"
  1.2108 +  by (intro measure_eq_emeasure_eq_ennreal) auto
  1.2109 +
  1.2110 +lemma null_measure_idem [simp]: "null_measure (null_measure M) = null_measure M"
  1.2111 +  by(rule measure_eqI) simp_all
  1.2112 +
  1.2113 +subsection \<open>Scaling a measure\<close>
  1.2114 +
  1.2115 +definition scale_measure :: "ennreal \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  1.2116 +where
  1.2117 +  "scale_measure r M = measure_of (space M) (sets M) (\<lambda>A. r * emeasure M A)"
  1.2118 +
  1.2119 +lemma space_scale_measure: "space (scale_measure r M) = space M"
  1.2120 +  by (simp add: scale_measure_def)
  1.2121 +
  1.2122 +lemma sets_scale_measure [simp, measurable_cong]: "sets (scale_measure r M) = sets M"
  1.2123 +  by (simp add: scale_measure_def)
  1.2124 +
  1.2125 +lemma emeasure_scale_measure [simp]:
  1.2126 +  "emeasure (scale_measure r M) A = r * emeasure M A"
  1.2127 +  (is "_ = ?\<mu> A")
  1.2128 +proof(cases "A \<in> sets M")
  1.2129 +  case True
  1.2130 +  show ?thesis unfolding scale_measure_def
  1.2131 +  proof(rule emeasure_measure_of_sigma)
  1.2132 +    show "sigma_algebra (space M) (sets M)" ..
  1.2133 +    show "positive (sets M) ?\<mu>" by (simp add: positive_def)
  1.2134 +    show "countably_additive (sets M) ?\<mu>"
  1.2135 +    proof (rule countably_additiveI)
  1.2136 +      fix A :: "nat \<Rightarrow> _"  assume *: "range A \<subseteq> sets M" "disjoint_family A"
  1.2137 +      have "(\<Sum>i. ?\<mu> (A i)) = r * (\<Sum>i. emeasure M (A i))"
  1.2138 +        by simp
  1.2139 +      also have "\<dots> = ?\<mu> (\<Union>i. A i)" using * by(simp add: suminf_emeasure)
  1.2140 +      finally show "(\<Sum>i. ?\<mu> (A i)) = ?\<mu> (\<Union>i. A i)" .
  1.2141 +    qed
  1.2142 +  qed(fact True)
  1.2143 +qed(simp add: emeasure_notin_sets)
  1.2144 +
  1.2145 +lemma scale_measure_1 [simp]: "scale_measure 1 M = M"
  1.2146 +  by(rule measure_eqI) simp_all
  1.2147 +
  1.2148 +lemma scale_measure_0[simp]: "scale_measure 0 M = null_measure M"
  1.2149 +  by(rule measure_eqI) simp_all
  1.2150 +
  1.2151 +lemma measure_scale_measure [simp]: "0 \<le> r \<Longrightarrow> measure (scale_measure r M) A = r * measure M A"
  1.2152 +  using emeasure_scale_measure[of r M A]
  1.2153 +    emeasure_eq_ennreal_measure[of M A]
  1.2154 +    measure_eq_emeasure_eq_ennreal[of _ "scale_measure r M" A]
  1.2155 +  by (cases "emeasure (scale_measure r M) A = top")
  1.2156 +     (auto simp del: emeasure_scale_measure
  1.2157 +           simp: ennreal_top_eq_mult_iff ennreal_mult_eq_top_iff measure_zero_top ennreal_mult[symmetric])
  1.2158 +
  1.2159 +lemma scale_scale_measure [simp]:
  1.2160 +  "scale_measure r (scale_measure r' M) = scale_measure (r * r') M"
  1.2161 +  by (rule measure_eqI) (simp_all add: max_def mult.assoc)
  1.2162 +
  1.2163 +lemma scale_null_measure [simp]: "scale_measure r (null_measure M) = null_measure M"
  1.2164 +  by (rule measure_eqI) simp_all
  1.2165 +
  1.2166 +
  1.2167 +subsection \<open>Complete lattice structure on measures\<close>
  1.2168 +
  1.2169 +lemma (in finite_measure) finite_measure_Diff':
  1.2170 +  "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> measure M (A - B) = measure M A - measure M (A \<inter> B)"
  1.2171 +  using finite_measure_Diff[of A "A \<inter> B"] by (auto simp: Diff_Int)
  1.2172 +
  1.2173 +lemma (in finite_measure) finite_measure_Union':
  1.2174 +  "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> measure M (A \<union> B) = measure M A + measure M (B - A)"
  1.2175 +  using finite_measure_Union[of A "B - A"] by auto
  1.2176 +
  1.2177 +lemma finite_unsigned_Hahn_decomposition:
  1.2178 +  assumes "finite_measure M" "finite_measure N" and [simp]: "sets N = sets M"
  1.2179 +  shows "\<exists>Y\<in>sets M. (\<forall>X\<in>sets M. X \<subseteq> Y \<longrightarrow> N X \<le> M X) \<and> (\<forall>X\<in>sets M. X \<inter> Y = {} \<longrightarrow> M X \<le> N X)"
  1.2180 +proof -
  1.2181 +  interpret M: finite_measure M by fact
  1.2182 +  interpret N: finite_measure N by fact
  1.2183 +
  1.2184 +  define d where "d X = measure M X - measure N X" for X
  1.2185 +
  1.2186 +  have [intro]: "bdd_above (d`sets M)"
  1.2187 +    using sets.sets_into_space[of _ M]
  1.2188 +    by (intro bdd_aboveI[where M="measure M (space M)"])
  1.2189 +       (auto simp: d_def field_simps subset_eq intro!: add_increasing M.finite_measure_mono)
  1.2190 +
  1.2191 +  define \<gamma> where "\<gamma> = (SUP X:sets M. d X)"
  1.2192 +  have le_\<gamma>[intro]: "X \<in> sets M \<Longrightarrow> d X \<le> \<gamma>" for X
  1.2193 +    by (auto simp: \<gamma>_def intro!: cSUP_upper)
  1.2194 +
  1.2195 +  have "\<exists>f. \<forall>n. f n \<in> sets M \<and> d (f n) > \<gamma> - 1 / 2^n"
  1.2196 +  proof (intro choice_iff[THEN iffD1] allI)
  1.2197 +    fix n
  1.2198 +    have "\<exists>X\<in>sets M. \<gamma> - 1 / 2^n < d X"
  1.2199 +      unfolding \<gamma>_def by (intro less_cSUP_iff[THEN iffD1]) auto
  1.2200 +    then show "\<exists>y. y \<in> sets M \<and> \<gamma> - 1 / 2 ^ n < d y"
  1.2201 +      by auto
  1.2202 +  qed
  1.2203 +  then obtain E where [measurable]: "E n \<in> sets M" and E: "d (E n) > \<gamma> - 1 / 2^n" for n
  1.2204 +    by auto
  1.2205 +
  1.2206 +  define F where "F m n = (if m \<le> n then \<Inter>i\<in>{m..n}. E i else space M)" for m n
  1.2207 +
  1.2208 +  have [measurable]: "m \<le> n \<Longrightarrow> F m n \<in> sets M" for m n
  1.2209 +    by (auto simp: F_def)
  1.2210 +
  1.2211 +  have 1: "\<gamma> - 2 / 2 ^ m + 1 / 2 ^ n \<le> d (F m n)" if "m \<le> n" for m n
  1.2212 +    using that
  1.2213 +  proof (induct rule: dec_induct)
  1.2214 +    case base with E[of m] show ?case
  1.2215 +      by (simp add: F_def field_simps)
  1.2216 +  next
  1.2217 +    case (step i)
  1.2218 +    have F_Suc: "F m (Suc i) = F m i \<inter> E (Suc i)"
  1.2219 +      using \<open>m \<le> i\<close> by (auto simp: F_def le_Suc_eq)
  1.2220 +
  1.2221 +    have "\<gamma> + (\<gamma> - 2 / 2^m + 1 / 2 ^ Suc i) \<le> (\<gamma> - 1 / 2^Suc i) + (\<gamma> - 2 / 2^m + 1 / 2^i)"
  1.2222 +      by (simp add: field_simps)
  1.2223 +    also have "\<dots> \<le> d (E (Suc i)) + d (F m i)"
  1.2224 +      using E[of "Suc i"] by (intro add_mono step) auto
  1.2225 +    also have "\<dots> = d (E (Suc i)) + d (F m i - E (Suc i)) + d (F m (Suc i))"
  1.2226 +      using \<open>m \<le> i\<close> by (simp add: d_def field_simps F_Suc M.finite_measure_Diff' N.finite_measure_Diff')
  1.2227 +    also have "\<dots> = d (E (Suc i) \<union> F m i) + d (F m (Suc i))"
  1.2228 +      using \<open>m \<le> i\<close> by (simp add: d_def field_simps M.finite_measure_Union' N.finite_measure_Union')
  1.2229 +    also have "\<dots> \<le> \<gamma> + d (F m (Suc i))"
  1.2230 +      using \<open>m \<le> i\<close> by auto
  1.2231 +    finally show ?case
  1.2232 +      by auto
  1.2233 +  qed
  1.2234 +
  1.2235 +  define F' where "F' m = (\<Inter>i\<in>{m..}. E i)" for m
  1.2236 +  have F'_eq: "F' m = (\<Inter>i. F m (i + m))" for m
  1.2237 +    by (fastforce simp: le_iff_add[of m] F'_def F_def)
  1.2238 +
  1.2239 +  have [measurable]: "F' m \<in> sets M" for m
  1.2240 +    by (auto simp: F'_def)
  1.2241 +
  1.2242 +  have \<gamma>_le: "\<gamma> - 0 \<le> d (\<Union>m. F' m)"
  1.2243 +  proof (rule LIMSEQ_le)
  1.2244 +    show "(\<lambda>n. \<gamma> - 2 / 2 ^ n) \<longlonglongrightarrow> \<gamma> - 0"
  1.2245 +      by (intro tendsto_intros LIMSEQ_divide_realpow_zero) auto
  1.2246 +    have "incseq F'"
  1.2247 +      by (auto simp: incseq_def F'_def)
  1.2248 +    then show "(\<lambda>m. d (F' m)) \<longlonglongrightarrow> d (\<Union>m. F' m)"
  1.2249 +      unfolding d_def
  1.2250 +      by (intro tendsto_diff M.finite_Lim_measure_incseq N.finite_Lim_measure_incseq) auto
  1.2251 +
  1.2252 +    have "\<gamma> - 2 / 2 ^ m + 0 \<le> d (F' m)" for m
  1.2253 +    proof (rule LIMSEQ_le)
  1.2254 +      have *: "decseq (\<lambda>n. F m (n + m))"
  1.2255 +        by (auto simp: decseq_def F_def)
  1.2256 +      show "(\<lambda>n. d (F m n)) \<longlonglongrightarrow> d (F' m)"
  1.2257 +        unfolding d_def F'_eq
  1.2258 +        by (rule LIMSEQ_offset[where k=m])
  1.2259 +           (auto intro!: tendsto_diff M.finite_Lim_measure_decseq N.finite_Lim_measure_decseq *)
  1.2260 +      show "(\<lambda>n. \<gamma> - 2 / 2 ^ m + 1 / 2 ^ n) \<longlonglongrightarrow> \<gamma> - 2 / 2 ^ m + 0"
  1.2261 +        by (intro tendsto_add LIMSEQ_divide_realpow_zero tendsto_const) auto
  1.2262 +      show "\<exists>N. \<forall>n\<ge>N. \<gamma> - 2 / 2 ^ m + 1 / 2 ^ n \<le> d (F m n)"
  1.2263 +        using 1[of m] by (intro exI[of _ m]) auto
  1.2264 +    qed
  1.2265 +    then show "\<exists>N. \<forall>n\<ge>N. \<gamma> - 2 / 2 ^ n \<le> d (F' n)"
  1.2266 +      by auto
  1.2267 +  qed
  1.2268 +
  1.2269 +  show ?thesis
  1.2270 +  proof (safe intro!: bexI[of _ "\<Union>m. F' m"])
  1.2271 +    fix X assume [measurable]: "X \<in> sets M" and X: "X \<subseteq> (\<Union>m. F' m)"
  1.2272 +    have "d (\<Union>m. F' m) - d X = d ((\<Union>m. F' m) - X)"
  1.2273 +      using X by (auto simp: d_def M.finite_measure_Diff N.finite_measure_Diff)
  1.2274 +    also have "\<dots> \<le> \<gamma>"
  1.2275 +      by auto
  1.2276 +    finally have "0 \<le> d X"
  1.2277 +      using \<gamma>_le by auto
  1.2278 +    then show "emeasure N X \<le> emeasure M X"
  1.2279 +      by (auto simp: d_def M.emeasure_eq_measure N.emeasure_eq_measure)
  1.2280 +  next
  1.2281 +    fix X assume [measurable]: "X \<in> sets M" and X: "X \<inter> (\<Union>m. F' m) = {}"
  1.2282 +    then have "d (\<Union>m. F' m) + d X = d (X \<union> (\<Union>m. F' m))"
  1.2283 +      by (auto simp: d_def M.finite_measure_Union N.finite_measure_Union)
  1.2284 +    also have "\<dots> \<le> \<gamma>"
  1.2285 +      by auto
  1.2286 +    finally have "d X \<le> 0"
  1.2287 +      using \<gamma>_le by auto
  1.2288 +    then show "emeasure M X \<le> emeasure N X"
  1.2289 +      by (auto simp: d_def M.emeasure_eq_measure N.emeasure_eq_measure)
  1.2290 +  qed auto
  1.2291 +qed
  1.2292 +
  1.2293 +lemma unsigned_Hahn_decomposition:
  1.2294 +  assumes [simp]: "sets N = sets M" and [measurable]: "A \<in> sets M"
  1.2295 +    and [simp]: "emeasure M A \<noteq> top" "emeasure N A \<noteq> top"
  1.2296 +  shows "\<exists>Y\<in>sets M. Y \<subseteq> A \<and> (\<forall>X\<in>sets M. X \<subseteq> Y \<longrightarrow> N X \<le> M X) \<and> (\<forall>X\<in>sets M. X \<subseteq> A \<longrightarrow> X \<inter> Y = {} \<longrightarrow> M X \<le> N X)"
  1.2297 +proof -
  1.2298 +  have "\<exists>Y\<in>sets (restrict_space M A).
  1.2299 +    (\<forall>X\<in>sets (restrict_space M A). X \<subseteq> Y \<longrightarrow> (restrict_space N A) X \<le> (restrict_space M A) X) \<and>
  1.2300 +    (\<forall>X\<in>sets (restrict_space M A). X \<inter> Y = {} \<longrightarrow> (restrict_space M A) X \<le> (restrict_space N A) X)"
  1.2301 +  proof (rule finite_unsigned_Hahn_decomposition)
  1.2302 +    show "finite_measure (restrict_space M A)" "finite_measure (restrict_space N A)"
  1.2303 +      by (auto simp: space_restrict_space emeasure_restrict_space less_top intro!: finite_measureI)
  1.2304 +  qed (simp add: sets_restrict_space)
  1.2305 +  then guess Y ..
  1.2306 +  then show ?thesis
  1.2307 +    apply (intro bexI[of _ Y] conjI ballI conjI)
  1.2308 +    apply (simp_all add: sets_restrict_space emeasure_restrict_space)
  1.2309 +    apply safe
  1.2310 +    subgoal for X Z
  1.2311 +      by (erule ballE[of _ _ X]) (auto simp add: Int_absorb1)
  1.2312 +    subgoal for X Z
  1.2313 +      by (erule ballE[of _ _ X])  (auto simp add: Int_absorb1 ac_simps)
  1.2314 +    apply auto
  1.2315 +    done
  1.2316 +qed
  1.2317 +
  1.2318 +text \<open>
  1.2319 +  Define a lexicographical order on @{type measure}, in the order space, sets and measure. The parts
  1.2320 +  of the lexicographical order are point-wise ordered.
  1.2321 +\<close>
  1.2322 +
  1.2323 +instantiation measure :: (type) order_bot
  1.2324 +begin
  1.2325 +
  1.2326 +inductive less_eq_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
  1.2327 +  "space M \<subset> space N \<Longrightarrow> less_eq_measure M N"
  1.2328 +| "space M = space N \<Longrightarrow> sets M \<subset> sets N \<Longrightarrow> less_eq_measure M N"
  1.2329 +| "space M = space N \<Longrightarrow> sets M = sets N \<Longrightarrow> emeasure M \<le> emeasure N \<Longrightarrow> less_eq_measure M N"
  1.2330 +
  1.2331 +lemma le_measure_iff:
  1.2332 +  "M \<le> N \<longleftrightarrow> (if space M = space N then
  1.2333 +    if sets M = sets N then emeasure M \<le> emeasure N else sets M \<subseteq> sets N else space M \<subseteq> space N)"
  1.2334 +  by (auto elim: less_eq_measure.cases intro: less_eq_measure.intros)
  1.2335 +
  1.2336 +definition less_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
  1.2337 +  "less_measure M N \<longleftrightarrow> (M \<le> N \<and> \<not> N \<le> M)"
  1.2338 +
  1.2339 +definition bot_measure :: "'a measure" where
  1.2340 +  "bot_measure = sigma {} {}"
  1.2341 +
  1.2342 +lemma
  1.2343 +  shows space_bot[simp]: "space bot = {}"
  1.2344 +    and sets_bot[simp]: "sets bot = {{}}"
  1.2345 +    and emeasure_bot[simp]: "emeasure bot X = 0"
  1.2346 +  by (auto simp: bot_measure_def sigma_sets_empty_eq emeasure_sigma)
  1.2347 +
  1.2348 +instance
  1.2349 +proof standard
  1.2350 +  show "bot \<le> a" for a :: "'a measure"
  1.2351 +    by (simp add: le_measure_iff bot_measure_def sigma_sets_empty_eq emeasure_sigma le_fun_def)
  1.2352 +qed (auto simp: le_measure_iff less_measure_def split: if_split_asm intro: measure_eqI)
  1.2353 +
  1.2354 +end
  1.2355 +
  1.2356 +lemma le_measure: "sets M = sets N \<Longrightarrow> M \<le> N \<longleftrightarrow> (\<forall>A\<in>sets M. emeasure M A \<le> emeasure N A)"
  1.2357 +  apply (auto simp: le_measure_iff le_fun_def dest: sets_eq_imp_space_eq)
  1.2358 +  subgoal for X
  1.2359 +    by (cases "X \<in> sets M") (auto simp: emeasure_notin_sets)
  1.2360 +  done
  1.2361 +
  1.2362 +definition sup_measure' :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  1.2363 +where
  1.2364 +  "sup_measure' A B = measure_of (space A) (sets A) (\<lambda>X. SUP Y:sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  1.2365 +
  1.2366 +lemma assumes [simp]: "sets B = sets A"
  1.2367 +  shows space_sup_measure'[simp]: "space (sup_measure' A B) = space A"
  1.2368 +    and sets_sup_measure'[simp]: "sets (sup_measure' A B) = sets A"
  1.2369 +  using sets_eq_imp_space_eq[OF assms] by (simp_all add: sup_measure'_def)
  1.2370 +
  1.2371 +lemma emeasure_sup_measure':
  1.2372 +  assumes sets_eq[simp]: "sets B = sets A" and [simp, intro]: "X \<in> sets A"
  1.2373 +  shows "emeasure (sup_measure' A B) X = (SUP Y:sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  1.2374 +    (is "_ = ?S X")
  1.2375 +proof -
  1.2376 +  note sets_eq_imp_space_eq[OF sets_eq, simp]
  1.2377 +  show ?thesis
  1.2378 +    using sup_measure'_def
  1.2379 +  proof (rule emeasure_measure_of)
  1.2380 +    let ?d = "\<lambda>X Y. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y)"
  1.2381 +    show "countably_additive (sets (sup_measure' A B)) (\<lambda>X. SUP Y : sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  1.2382 +    proof (rule countably_additiveI, goal_cases)
  1.2383 +      case (1 X)
  1.2384 +      then have [measurable]: "\<And>i. X i \<in> sets A" and "disjoint_family X"
  1.2385 +        by auto
  1.2386 +      have "(\<Sum>i. ?S (X i)) = (SUP Y:sets A. \<Sum>i. ?d (X i) Y)"
  1.2387 +      proof (rule ennreal_suminf_SUP_eq_directed)
  1.2388 +        fix J :: "nat set" and a b assume "finite J" and [measurable]: "a \<in> sets A" "b \<in> sets A"
  1.2389 +        have "\<exists>c\<in>sets A. c \<subseteq> X i \<and> (\<forall>a\<in>sets A. ?d (X i) a \<le> ?d (X i) c)" for i
  1.2390 +        proof cases
  1.2391 +          assume "emeasure A (X i) = top \<or> emeasure B (X i) = top"
  1.2392 +          then show ?thesis
  1.2393 +          proof
  1.2394 +            assume "emeasure A (X i) = top" then show ?thesis
  1.2395 +              by (intro bexI[of _ "X i"]) auto
  1.2396 +          next
  1.2397 +            assume "emeasure B (X i) = top" then show ?thesis
  1.2398 +              by (intro bexI[of _ "{}"]) auto
  1.2399 +          qed
  1.2400 +        next
  1.2401 +          assume finite: "\<not> (emeasure A (X i) = top \<or> emeasure B (X i) = top)"
  1.2402 +          then have "\<exists>Y\<in>sets A. Y \<subseteq> X i \<and> (\<forall>C\<in>sets A. C \<subseteq> Y \<longrightarrow> B C \<le> A C) \<and> (\<forall>C\<in>sets A. C \<subseteq> X i \<longrightarrow> C \<inter> Y = {} \<longrightarrow> A C \<le> B C)"
  1.2403 +            using unsigned_Hahn_decomposition[of B A "X i"] by simp
  1.2404 +          then obtain Y where [measurable]: "Y \<in> sets A" and [simp]: "Y \<subseteq> X i"
  1.2405 +            and B_le_A: "\<And>C. C \<in> sets A \<Longrightarrow> C \<subseteq> Y \<Longrightarrow> B C \<le> A C"
  1.2406 +            and A_le_B: "\<And>C. C \<in> sets A \<Longrightarrow> C \<subseteq> X i \<Longrightarrow> C \<inter> Y = {} \<Longrightarrow> A C \<le> B C"
  1.2407 +            by auto
  1.2408 +
  1.2409 +          show ?thesis
  1.2410 +          proof (intro bexI[of _ Y] ballI conjI)
  1.2411 +            fix a assume [measurable]: "a \<in> sets A"
  1.2412 +            have *: "(X i \<inter> a \<inter> Y \<union> (X i \<inter> a - Y)) = X i \<inter> a" "(X i - a) \<inter> Y \<union> (X i - a - Y) = X i \<inter> - a"
  1.2413 +              for a Y by auto
  1.2414 +            then have "?d (X i) a =
  1.2415 +              (A (X i \<inter> a \<inter> Y) + A (X i \<inter> a \<inter> - Y)) + (B (X i \<inter> - a \<inter> Y) + B (X i \<inter> - a \<inter> - Y))"
  1.2416 +              by (subst (1 2) plus_emeasure) (auto simp: Diff_eq[symmetric])
  1.2417 +            also have "\<dots> \<le> (A (X i \<inter> a \<inter> Y) + B (X i \<inter> a \<inter> - Y)) + (A (X i \<inter> - a \<inter> Y) + B (X i \<inter> - a \<inter> - Y))"
  1.2418 +              by (intro add_mono order_refl B_le_A A_le_B) (auto simp: Diff_eq[symmetric])
  1.2419 +            also have "\<dots> \<le> (A (X i \<inter> Y \<inter> a) + A (X i \<inter> Y \<inter> - a)) + (B (X i \<inter> - Y \<inter> a) + B (X i \<inter> - Y \<inter> - a))"
  1.2420 +              by (simp add: ac_simps)
  1.2421 +            also have "\<dots> \<le> A (X i \<inter> Y) + B (X i \<inter> - Y)"
  1.2422 +              by (subst (1 2) plus_emeasure) (auto simp: Diff_eq[symmetric] *)
  1.2423 +            finally show "?d (X i) a \<le> ?d (X i) Y" .
  1.2424 +          qed auto
  1.2425 +        qed
  1.2426 +        then obtain C where [measurable]: "C i \<in> sets A" and "C i \<subseteq> X i"
  1.2427 +          and C: "\<And>a. a \<in> sets A \<Longrightarrow> ?d (X i) a \<le> ?d (X i) (C i)" for i
  1.2428 +          by metis
  1.2429 +        have *: "X i \<inter> (\<Union>i. C i) = X i \<inter> C i" for i
  1.2430 +        proof safe
  1.2431 +          fix x j assume "x \<in> X i" "x \<in> C j"
  1.2432 +          moreover have "i = j \<or> X i \<inter> X j = {}"
  1.2433 +            using \<open>disjoint_family X\<close> by (auto simp: disjoint_family_on_def)
  1.2434 +          ultimately show "x \<in> C i"
  1.2435 +            using \<open>C i \<subseteq> X i\<close> \<open>C j \<subseteq> X j\<close> by auto
  1.2436 +        qed auto
  1.2437 +        have **: "X i \<inter> - (\<Union>i. C i) = X i \<inter> - C i" for i
  1.2438 +        proof safe
  1.2439 +          fix x j assume "x \<in> X i" "x \<notin> C i" "x \<in> C j"
  1.2440 +          moreover have "i = j \<or> X i \<inter> X j = {}"
  1.2441 +            using \<open>disjoint_family X\<close> by (auto simp: disjoint_family_on_def)
  1.2442 +          ultimately show False
  1.2443 +            using \<open>C i \<subseteq> X i\<close> \<open>C j \<subseteq> X j\<close> by auto
  1.2444 +        qed auto
  1.2445 +        show "\<exists>c\<in>sets A. \<forall>i\<in>J. ?d (X i) a \<le> ?d (X i) c \<and> ?d (X i) b \<le> ?d (X i) c"
  1.2446 +          apply (intro bexI[of _ "\<Union>i. C i"])
  1.2447 +          unfolding * **
  1.2448 +          apply (intro C ballI conjI)
  1.2449 +          apply auto
  1.2450 +          done
  1.2451 +      qed
  1.2452 +      also have "\<dots> = ?S (\<Union>i. X i)"
  1.2453 +        unfolding UN_extend_simps(4)
  1.2454 +        by (auto simp add: suminf_add[symmetric] Diff_eq[symmetric] simp del: UN_simps
  1.2455 +                 intro!: SUP_cong arg_cong2[where f="op +"] suminf_emeasure
  1.2456 +                         disjoint_family_on_bisimulation[OF \<open>disjoint_family X\<close>])
  1.2457 +      finally show "(\<Sum>i. ?S (X i)) = ?S (\<Union>i. X i)" .
  1.2458 +    qed
  1.2459 +  qed (auto dest: sets.sets_into_space simp: positive_def intro!: SUP_const)
  1.2460 +qed
  1.2461 +
  1.2462 +lemma le_emeasure_sup_measure'1:
  1.2463 +  assumes "sets B = sets A" "X \<in> sets A" shows "emeasure A X \<le> emeasure (sup_measure' A B) X"
  1.2464 +  by (subst emeasure_sup_measure'[OF assms]) (auto intro!: SUP_upper2[of "X"] assms)
  1.2465 +
  1.2466 +lemma le_emeasure_sup_measure'2:
  1.2467 +  assumes "sets B = sets A" "X \<in> sets A" shows "emeasure B X \<le> emeasure (sup_measure' A B) X"
  1.2468 +  by (subst emeasure_sup_measure'[OF assms]) (auto intro!: SUP_upper2[of "{}"] assms)
  1.2469 +
  1.2470 +lemma emeasure_sup_measure'_le2:
  1.2471 +  assumes [simp]: "sets B = sets C" "sets A = sets C" and [measurable]: "X \<in> sets C"
  1.2472 +  assumes A: "\<And>Y. Y \<subseteq> X \<Longrightarrow> Y \<in> sets A \<Longrightarrow> emeasure A Y \<le> emeasure C Y"
  1.2473 +  assumes B: "\<And>Y. Y \<subseteq> X \<Longrightarrow> Y \<in> sets A \<Longrightarrow> emeasure B Y \<le> emeasure C Y"
  1.2474 +  shows "emeasure (sup_measure' A B) X \<le> emeasure C X"
  1.2475 +proof (subst emeasure_sup_measure')
  1.2476 +  show "(SUP Y:sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y)) \<le> emeasure C X"
  1.2477 +    unfolding \<open>sets A = sets C\<close>
  1.2478 +  proof (intro SUP_least)
  1.2479 +    fix Y assume [measurable]: "Y \<in> sets C"
  1.2480 +    have [simp]: "X \<inter> Y \<union> (X - Y) = X"
  1.2481 +      by auto
  1.2482 +    have "emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y) \<le> emeasure C (X \<inter> Y) + emeasure C (X \<inter> - Y)"
  1.2483 +      by (intro add_mono A B) (auto simp: Diff_eq[symmetric])
  1.2484 +    also have "\<dots> = emeasure C X"
  1.2485 +      by (subst plus_emeasure) (auto simp: Diff_eq[symmetric])
  1.2486 +    finally show "emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y) \<le> emeasure C X" .
  1.2487 +  qed
  1.2488 +qed simp_all
  1.2489 +
  1.2490 +definition sup_lexord :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b::order) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a"
  1.2491 +where
  1.2492 +  "sup_lexord A B k s c =
  1.2493 +    (if k A = k B then c else if \<not> k A \<le> k B \<and> \<not> k B \<le> k A then s else if k B \<le> k A then A else B)"
  1.2494 +
  1.2495 +lemma sup_lexord:
  1.2496 +  "(k A < k B \<Longrightarrow> P B) \<Longrightarrow> (k B < k A \<Longrightarrow> P A) \<Longrightarrow> (k A = k B \<Longrightarrow> P c) \<Longrightarrow>
  1.2497 +    (\<not> k B \<le> k A \<Longrightarrow> \<not> k A \<le> k B \<Longrightarrow> P s) \<Longrightarrow> P (sup_lexord A B k s c)"
  1.2498 +  by (auto simp: sup_lexord_def)
  1.2499 +
  1.2500 +lemmas le_sup_lexord = sup_lexord[where P="\<lambda>a. c \<le> a" for c]
  1.2501 +
  1.2502 +lemma sup_lexord1: "k A = k B \<Longrightarrow> sup_lexord A B k s c = c"
  1.2503 +  by (simp add: sup_lexord_def)
  1.2504 +
  1.2505 +lemma sup_lexord_commute: "sup_lexord A B k s c = sup_lexord B A k s c"
  1.2506 +  by (auto simp: sup_lexord_def)
  1.2507 +
  1.2508 +lemma sigma_sets_le_sets_iff: "(sigma_sets (space x) \<A> \<subseteq> sets x) = (\<A> \<subseteq> sets x)"
  1.2509 +  using sets.sigma_sets_subset[of \<A> x] by auto
  1.2510 +
  1.2511 +lemma sigma_le_iff: "\<A> \<subseteq> Pow \<Omega> \<Longrightarrow> sigma \<Omega> \<A> \<le> x \<longleftrightarrow> (\<Omega> \<subseteq> space x \<and> (space x = \<Omega> \<longrightarrow> \<A> \<subseteq> sets x))"
  1.2512 +  by (cases "\<Omega> = space x")
  1.2513 +     (simp_all add: eq_commute[of _ "sets x"] le_measure_iff emeasure_sigma le_fun_def
  1.2514 +                    sigma_sets_superset_generator sigma_sets_le_sets_iff)
  1.2515 +
  1.2516 +instantiation measure :: (type) semilattice_sup
  1.2517 +begin
  1.2518 +
  1.2519 +definition sup_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  1.2520 +where
  1.2521 +  "sup_measure A B =
  1.2522 +    sup_lexord A B space (sigma (space A \<union> space B) {})
  1.2523 +      (sup_lexord A B sets (sigma (space A) (sets A \<union> sets B)) (sup_measure' A B))"
  1.2524 +
  1.2525 +instance
  1.2526 +proof
  1.2527 +  fix x y z :: "'a measure"
  1.2528 +  show "x \<le> sup x y"
  1.2529 +    unfolding sup_measure_def
  1.2530 +  proof (intro le_sup_lexord)
  1.2531 +    assume "space x = space y"
  1.2532 +    then have *: "sets x \<union> sets y \<subseteq> Pow (space x)"
  1.2533 +      using sets.space_closed by auto
  1.2534 +    assume "\<not> sets y \<subseteq> sets x" "\<not> sets x \<subseteq> sets y"
  1.2535 +    then have "sets x \<subset> sets x \<union> sets y"
  1.2536 +      by auto
  1.2537 +    also have "\<dots> \<le> sigma (space x) (sets x \<union> sets y)"
  1.2538 +      by (subst sets_measure_of[OF *]) (rule sigma_sets_superset_generator)
  1.2539 +    finally show "x \<le> sigma (space x) (sets x \<union> sets y)"
  1.2540 +      by (simp add: space_measure_of[OF *] less_eq_measure.intros(2))
  1.2541 +  next
  1.2542 +    assume "\<not> space y \<subseteq> space x" "\<not> space x \<subseteq> space y"
  1.2543 +    then show "x \<le> sigma (space x \<union> space y) {}"
  1.2544 +      by (intro less_eq_measure.intros) auto
  1.2545 +  next
  1.2546 +    assume "sets x = sets y" then show "x \<le> sup_measure' x y"
  1.2547 +      by (simp add: le_measure le_emeasure_sup_measure'1)
  1.2548 +  qed (auto intro: less_eq_measure.intros)
  1.2549 +  show "y \<le> sup x y"
  1.2550 +    unfolding sup_measure_def
  1.2551 +  proof (intro le_sup_lexord)
  1.2552 +    assume **: "space x = space y"
  1.2553 +    then have *: "sets x \<union> sets y \<subseteq> Pow (space y)"
  1.2554 +      using sets.space_closed by auto
  1.2555 +    assume "\<not> sets y \<subseteq> sets x" "\<not> sets x \<subseteq> sets y"
  1.2556 +    then have "sets y \<subset> sets x \<union> sets y"
  1.2557 +      by auto
  1.2558 +    also have "\<dots> \<le> sigma (space y) (sets x \<union> sets y)"
  1.2559 +      by (subst sets_measure_of[OF *]) (rule sigma_sets_superset_generator)
  1.2560 +    finally show "y \<le> sigma (space x) (sets x \<union> sets y)"
  1.2561 +      by (simp add: ** space_measure_of[OF *] less_eq_measure.intros(2))
  1.2562 +  next
  1.2563 +    assume "\<not> space y \<subseteq> space x" "\<not> space x \<subseteq> space y"
  1.2564 +    then show "y \<le> sigma (space x \<union> space y) {}"
  1.2565 +      by (intro less_eq_measure.intros) auto
  1.2566 +  next
  1.2567 +    assume "sets x = sets y" then show "y \<le> sup_measure' x y"
  1.2568 +      by (simp add: le_measure le_emeasure_sup_measure'2)
  1.2569 +  qed (auto intro: less_eq_measure.intros)
  1.2570 +  show "x \<le> y \<Longrightarrow> z \<le> y \<Longrightarrow> sup x z \<le> y"
  1.2571 +    unfolding sup_measure_def
  1.2572 +  proof (intro sup_lexord[where P="\<lambda>x. x \<le> y"])
  1.2573 +    assume "x \<le> y" "z \<le> y" and [simp]: "space x = space z" "sets x = sets z"
  1.2574 +    from \<open>x \<le> y\<close> show "sup_measure' x z \<le> y"
  1.2575 +    proof cases
  1.2576 +      case 1 then show ?thesis
  1.2577 +        by (intro less_eq_measure.intros(1)) simp
  1.2578 +    next
  1.2579 +      case 2 then show ?thesis
  1.2580 +        by (intro less_eq_measure.intros(2)) simp_all
  1.2581 +    next
  1.2582 +      case 3 with \<open>z \<le> y\<close> \<open>x \<le> y\<close> show ?thesis
  1.2583 +        by (auto simp add: le_measure intro!: emeasure_sup_measure'_le2)
  1.2584 +    qed
  1.2585 +  next
  1.2586 +    assume **: "x \<le> y" "z \<le> y" "space x = space z" "\<not> sets z \<subseteq> sets x" "\<not> sets x \<subseteq> sets z"
  1.2587 +    then have *: "sets x \<union> sets z \<subseteq> Pow (space x)"
  1.2588 +      using sets.space_closed by auto
  1.2589 +    show "sigma (space x) (sets x \<union> sets z) \<le> y"
  1.2590 +      unfolding sigma_le_iff[OF *] using ** by (auto simp: le_measure_iff split: if_split_asm)
  1.2591 +  next
  1.2592 +    assume "x \<le> y" "z \<le> y" "\<not> space z \<subseteq> space x" "\<not> space x \<subseteq> space z"
  1.2593 +    then have "space x \<subseteq> space y" "space z \<subseteq> space y"
  1.2594 +      by (auto simp: le_measure_iff split: if_split_asm)
  1.2595 +    then show "sigma (space x \<union> space z) {} \<le> y"
  1.2596 +      by (simp add: sigma_le_iff)
  1.2597 +  qed
  1.2598 +qed
  1.2599 +
  1.2600 +end
  1.2601 +
  1.2602 +lemma space_empty_eq_bot: "space a = {} \<longleftrightarrow> a = bot"
  1.2603 +  using space_empty[of a] by (auto intro!: measure_eqI)
  1.2604 +
  1.2605 +interpretation sup_measure: comm_monoid_set sup "bot :: 'a measure"
  1.2606 +  proof qed (auto intro!: antisym)
  1.2607 +
  1.2608 +lemma sup_measure_F_mono':
  1.2609 +  "finite J \<Longrightarrow> finite I \<Longrightarrow> sup_measure.F id I \<le> sup_measure.F id (I \<union> J)"
  1.2610 +proof (induction J rule: finite_induct)
  1.2611 +  case empty then show ?case
  1.2612 +    by simp
  1.2613 +next
  1.2614 +  case (insert i J)
  1.2615 +  show ?case
  1.2616 +  proof cases
  1.2617 +    assume "i \<in> I" with insert show ?thesis
  1.2618 +      by (auto simp: insert_absorb)
  1.2619 +  next
  1.2620 +    assume "i \<notin> I"
  1.2621 +    have "sup_measure.F id I \<le> sup_measure.F id (I \<union> J)"
  1.2622 +      by (intro insert)
  1.2623 +    also have "\<dots> \<le> sup_measure.F id (insert i (I \<union> J))"
  1.2624 +      using insert \<open>i \<notin> I\<close> by (subst sup_measure.insert) auto
  1.2625 +    finally show ?thesis
  1.2626 +      by auto
  1.2627 +  qed
  1.2628 +qed
  1.2629 +
  1.2630 +lemma sup_measure_F_mono: "finite I \<Longrightarrow> J \<subseteq> I \<Longrightarrow> sup_measure.F id J \<le> sup_measure.F id I"
  1.2631 +  using sup_measure_F_mono'[of I J] by (auto simp: finite_subset Un_absorb1)
  1.2632 +
  1.2633 +lemma sets_eq_iff_bounded: "A \<le> B \<Longrightarrow> B \<le> C \<Longrightarrow> sets A = sets C \<Longrightarrow> sets B = sets A"
  1.2634 +  by (auto dest: sets_eq_imp_space_eq simp add: le_measure_iff split: if_split_asm)
  1.2635 +
  1.2636 +lemma sets_sup: "sets A = sets M \<Longrightarrow> sets B = sets M \<Longrightarrow> sets (sup A B) = sets M"
  1.2637 +  by (auto simp add: sup_measure_def sup_lexord_def dest: sets_eq_imp_space_eq)
  1.2638 +
  1.2639 +lemma sets_sup_measure_F:
  1.2640 +  "finite I \<Longrightarrow> I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> sets i = sets M) \<Longrightarrow> sets (sup_measure.F id I) = sets M"
  1.2641 +  by (induction I rule: finite_ne_induct) (simp_all add: sets_sup)
  1.2642 +
  1.2643 +lemma le_measureD1: "A \<le> B \<Longrightarrow> space A \<le> space B"
  1.2644 +  by (auto simp: le_measure_iff split: if_split_asm)
  1.2645 +
  1.2646 +lemma le_measureD2: "A \<le> B \<Longrightarrow> space A = space B \<Longrightarrow> sets A \<le> sets B"
  1.2647 +  by (auto simp: le_measure_iff split: if_split_asm)
  1.2648 +
  1.2649 +lemma le_measureD3: "A \<le> B \<Longrightarrow> sets A = sets B \<Longrightarrow> emeasure A X \<le> emeasure B X"
  1.2650 +  by (auto simp: le_measure_iff le_fun_def dest: sets_eq_imp_space_eq split: if_split_asm)
  1.2651 +
  1.2652 +definition Sup_measure' :: "'a measure set \<Rightarrow> 'a measure"
  1.2653 +where
  1.2654 +  "Sup_measure' M = measure_of (\<Union>a\<in>M. space a) (\<Union>a\<in>M. sets a)
  1.2655 +    (\<lambda>X. (SUP P:{P. finite P \<and> P \<subseteq> M }. sup_measure.F id P X))"
  1.2656 +
  1.2657 +lemma UN_space_closed: "UNION S sets \<subseteq> Pow (UNION S space)"
  1.2658 +  using sets.space_closed by auto
  1.2659 +
  1.2660 +lemma space_Sup_measure'2: "space (Sup_measure' M) = (\<Union>m\<in>M. space m)"
  1.2661 +  unfolding Sup_measure'_def by (intro space_measure_of[OF UN_space_closed])
  1.2662 +
  1.2663 +lemma sets_Sup_measure'2: "sets (Sup_measure' M) = sigma_sets (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m)"
  1.2664 +  unfolding Sup_measure'_def by (intro sets_measure_of[OF UN_space_closed])
  1.2665 +
  1.2666 +lemma sets_Sup_measure':
  1.2667 +  assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "M \<noteq> {}"
  1.2668 +  shows "sets (Sup_measure' M) = sets A"
  1.2669 +  using sets_eq[THEN sets_eq_imp_space_eq, simp] \<open>M \<noteq> {}\<close> by (simp add: Sup_measure'_def)
  1.2670 +
  1.2671 +lemma space_Sup_measure':
  1.2672 +  assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "M \<noteq> {}"
  1.2673 +  shows "space (Sup_measure' M) = space A"
  1.2674 +  using sets_eq[THEN sets_eq_imp_space_eq, simp] \<open>M \<noteq> {}\<close>
  1.2675 +  by (simp add: Sup_measure'_def )
  1.2676 +
  1.2677 +lemma emeasure_Sup_measure':
  1.2678 +  assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "X \<in> sets A" "M \<noteq> {}"
  1.2679 +  shows "emeasure (Sup_measure' M) X = (SUP P:{P. finite P \<and> P \<subseteq> M}. sup_measure.F id P X)"
  1.2680 +    (is "_ = ?S X")
  1.2681 +  using Sup_measure'_def
  1.2682 +proof (rule emeasure_measure_of)
  1.2683 +  note sets_eq[THEN sets_eq_imp_space_eq, simp]
  1.2684 +  have *: "sets (Sup_measure' M) = sets A" "space (Sup_measure' M) = space A"
  1.2685 +    using \<open>M \<noteq> {}\<close> by (simp_all add: Sup_measure'_def)
  1.2686 +  let ?\<mu> = "sup_measure.F id"
  1.2687 +  show "countably_additive (sets (Sup_measure' M)) ?S"
  1.2688 +  proof (rule countably_additiveI, goal_cases)
  1.2689 +    case (1 F)
  1.2690 +    then have **: "range F \<subseteq> sets A"
  1.2691 +      by (auto simp: *)
  1.2692 +    show "(\<Sum>i. ?S (F i)) = ?S (\<Union>i. F i)"
  1.2693 +    proof (subst ennreal_suminf_SUP_eq_directed)
  1.2694 +      fix i j and N :: "nat set" assume ij: "i \<in> {P. finite P \<and> P \<subseteq> M}" "j \<in> {P. finite P \<and> P \<subseteq> M}"
  1.2695 +      have "(i \<noteq> {} \<longrightarrow> sets (?\<mu> i) = sets A) \<and> (j \<noteq> {} \<longrightarrow> sets (?\<mu> j) = sets A) \<and>
  1.2696 +        (i \<noteq> {} \<or> j \<noteq> {} \<longrightarrow> sets (?\<mu> (i \<union> j)) = sets A)"
  1.2697 +        using ij by (intro impI sets_sup_measure_F conjI) auto
  1.2698 +      then have "?\<mu> j (F n) \<le> ?\<mu> (i \<union> j) (F n) \<and> ?\<mu> i (F n) \<le> ?\<mu> (i \<union> j) (F n)" for n
  1.2699 +        using ij
  1.2700 +        by (cases "i = {}"; cases "j = {}")
  1.2701 +           (auto intro!: le_measureD3 sup_measure_F_mono simp: sets_sup_measure_F
  1.2702 +                 simp del: id_apply)
  1.2703 +      with ij show "\<exists>k\<in>{P. finite P \<and> P \<subseteq> M}. \<forall>n\<in>N. ?\<mu> i (F n) \<le> ?\<mu> k (F n) \<and> ?\<mu> j (F n) \<le> ?\<mu> k (F n)"
  1.2704 +        by (safe intro!: bexI[of _ "i \<union> j"]) auto
  1.2705 +    next
  1.2706 +      show "(SUP P : {P. finite P \<and> P \<subseteq> M}. \<Sum>n. ?\<mu> P (F n)) = (SUP P : {P. finite P \<and> P \<subseteq> M}. ?\<mu> P (UNION UNIV F))"
  1.2707 +      proof (intro SUP_cong refl)
  1.2708 +        fix i assume i: "i \<in> {P. finite P \<and> P \<subseteq> M}"
  1.2709 +        show "(\<Sum>n. ?\<mu> i (F n)) = ?\<mu> i (UNION UNIV F)"
  1.2710 +        proof cases
  1.2711 +          assume "i \<noteq> {}" with i ** show ?thesis
  1.2712 +            apply (intro suminf_emeasure \<open>disjoint_family F\<close>)
  1.2713 +            apply (subst sets_sup_measure_F[OF _ _ sets_eq])
  1.2714 +            apply auto
  1.2715 +            done
  1.2716 +        qed simp
  1.2717 +      qed
  1.2718 +    qed
  1.2719 +  qed
  1.2720 +  show "positive (sets (Sup_measure' M)) ?S"
  1.2721 +    by (auto simp: positive_def bot_ennreal[symmetric])
  1.2722 +  show "X \<in> sets (Sup_measure' M)"
  1.2723 +    using assms * by auto
  1.2724 +qed (rule UN_space_closed)
  1.2725 +
  1.2726 +definition Sup_lexord :: "('a \<Rightarrow> 'b::complete_lattice) \<Rightarrow> ('a set \<Rightarrow> 'a) \<Rightarrow> ('a set \<Rightarrow> 'a) \<Rightarrow> 'a set \<Rightarrow> 'a"
  1.2727 +where
  1.2728 +  "Sup_lexord k c s A = (let U = (SUP a:A. k a) in if \<exists>a\<in>A. k a = U then c {a\<in>A. k a = U} else s A)"
  1.2729 +
  1.2730 +lemma Sup_lexord:
  1.2731 +  "(\<And>a S. a \<in> A \<Longrightarrow> k a = (SUP a:A. k a) \<Longrightarrow> S = {a'\<in>A. k a' = k a} \<Longrightarrow> P (c S)) \<Longrightarrow> ((\<And>a. a \<in> A \<Longrightarrow> k a \<noteq> (SUP a:A. k a)) \<Longrightarrow> P (s A)) \<Longrightarrow>
  1.2732 +    P (Sup_lexord k c s A)"
  1.2733 +  by (auto simp: Sup_lexord_def Let_def)
  1.2734 +
  1.2735 +lemma Sup_lexord1:
  1.2736 +  assumes A: "A \<noteq> {}" "(\<And>a. a \<in> A \<Longrightarrow> k a = (\<Union>a\<in>A. k a))" "P (c A)"
  1.2737 +  shows "P (Sup_lexord k c s A)"
  1.2738 +  unfolding Sup_lexord_def Let_def
  1.2739 +proof (clarsimp, safe)
  1.2740 +  show "\<forall>a\<in>A. k a \<noteq> (\<Union>x\<in>A. k x) \<Longrightarrow> P (s A)"
  1.2741 +    by (metis assms(1,2) ex_in_conv)
  1.2742 +next
  1.2743 +  fix a assume "a \<in> A" "k a = (\<Union>x\<in>A. k x)"
  1.2744 +  then have "{a \<in> A. k a = (\<Union>x\<in>A. k x)} = {a \<in> A. k a = k a}"
  1.2745 +    by (metis A(2)[symmetric])
  1.2746 +  then show "P (c {a \<in> A. k a = (\<Union>x\<in>A. k x)})"
  1.2747 +    by (simp add: A(3))
  1.2748 +qed
  1.2749 +
  1.2750 +instantiation measure :: (type) complete_lattice
  1.2751 +begin
  1.2752 +
  1.2753 +definition Sup_measure :: "'a measure set \<Rightarrow> 'a measure"
  1.2754 +where
  1.2755 +  "Sup_measure = Sup_lexord space (Sup_lexord sets Sup_measure'
  1.2756 +    (\<lambda>U. sigma (\<Union>u\<in>U. space u) (\<Union>u\<in>U. sets u))) (\<lambda>U. sigma (\<Union>u\<in>U. space u) {})"
  1.2757 +
  1.2758 +definition Inf_measure :: "'a measure set \<Rightarrow> 'a measure"
  1.2759 +where
  1.2760 +  "Inf_measure A = Sup {x. \<forall>a\<in>A. x \<le> a}"
  1.2761 +
  1.2762 +definition inf_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  1.2763 +where
  1.2764 +  "inf_measure a b = Inf {a, b}"
  1.2765 +
  1.2766 +definition top_measure :: "'a measure"
  1.2767 +where
  1.2768 +  "top_measure = Inf {}"
  1.2769 +
  1.2770 +instance
  1.2771 +proof
  1.2772 +  note UN_space_closed [simp]
  1.2773 +  show upper: "x \<le> Sup A" if x: "x \<in> A" for x :: "'a measure" and A
  1.2774 +    unfolding Sup_measure_def
  1.2775 +  proof (intro Sup_lexord[where P="\<lambda>y. x \<le> y"])
  1.2776 +    assume "\<And>a. a \<in> A \<Longrightarrow> space a \<noteq> (\<Union>a\<in>A. space a)"
  1.2777 +    from this[OF \<open>x \<in> A\<close>] \<open>x \<in> A\<close> show "x \<le> sigma (\<Union>a\<in>A. space a) {}"
  1.2778 +      by (intro less_eq_measure.intros) auto
  1.2779 +  next
  1.2780 +    fix a S assume "a \<in> A" and a: "space a = (\<Union>a\<in>A. space a)" and S: "S = {a' \<in> A. space a' = space a}"
  1.2781 +      and neq: "\<And>aa. aa \<in> S \<Longrightarrow> sets aa \<noteq> (\<Union>a\<in>S. sets a)"
  1.2782 +    have sp_a: "space a = (UNION S space)"
  1.2783 +      using \<open>a\<in>A\<close> by (auto simp: S)
  1.2784 +    show "x \<le> sigma (UNION S space) (UNION S sets)"
  1.2785 +    proof cases
  1.2786 +      assume [simp]: "space x = space a"
  1.2787 +      have "sets x \<subset> (\<Union>a\<in>S. sets a)"
  1.2788 +        using \<open>x\<in>A\<close> neq[of x] by (auto simp: S)
  1.2789 +      also have "\<dots> \<subseteq> sigma_sets (\<Union>x\<in>S. space x) (\<Union>x\<in>S. sets x)"
  1.2790 +        by (rule sigma_sets_superset_generator)
  1.2791 +      finally show ?thesis
  1.2792 +        by (intro less_eq_measure.intros(2)) (simp_all add: sp_a)
  1.2793 +    next
  1.2794 +      assume "space x \<noteq> space a"
  1.2795 +      moreover have "space x \<le> space a"
  1.2796 +        unfolding a using \<open>x\<in>A\<close> by auto
  1.2797 +      ultimately show ?thesis
  1.2798 +        by (intro less_eq_measure.intros) (simp add: less_le sp_a)
  1.2799 +    qed
  1.2800 +  next
  1.2801 +    fix a b S S' assume "a \<in> A" and a: "space a = (\<Union>a\<in>A. space a)" and S: "S = {a' \<in> A. space a' = space a}"
  1.2802 +      and "b \<in> S" and b: "sets b = (\<Union>a\<in>S. sets a)" and S': "S' = {a' \<in> S. sets a' = sets b}"
  1.2803 +    then have "S' \<noteq> {}" "space b = space a"
  1.2804 +      by auto
  1.2805 +    have sets_eq: "\<And>x. x \<in> S' \<Longrightarrow> sets x = sets b"
  1.2806 +      by (auto simp: S')
  1.2807 +    note sets_eq[THEN sets_eq_imp_space_eq, simp]
  1.2808 +    have *: "sets (Sup_measure' S') = sets b" "space (Sup_measure' S') = space b"
  1.2809 +      using \<open>S' \<noteq> {}\<close> by (simp_all add: Sup_measure'_def sets_eq)
  1.2810 +    show "x \<le> Sup_measure' S'"
  1.2811 +    proof cases
  1.2812 +      assume "x \<in> S"
  1.2813 +      with \<open>b \<in> S\<close> have "space x = space b"
  1.2814 +        by (simp add: S)
  1.2815 +      show ?thesis
  1.2816 +      proof cases
  1.2817 +        assume "x \<in> S'"
  1.2818 +        show "x \<le> Sup_measure' S'"
  1.2819 +        proof (intro le_measure[THEN iffD2] ballI)
  1.2820 +          show "sets x = sets (Sup_measure' S')"
  1.2821 +            using \<open>x\<in>S'\<close> * by (simp add: S')
  1.2822 +          fix X assume "X \<in> sets x"
  1.2823 +          show "emeasure x X \<le> emeasure (Sup_measure' S') X"
  1.2824 +          proof (subst emeasure_Sup_measure'[OF _ \<open>X \<in> sets x\<close>])
  1.2825 +            show "emeasure x X \<le> (SUP P : {P. finite P \<and> P \<subseteq> S'}. emeasure (sup_measure.F id P) X)"
  1.2826 +              using \<open>x\<in>S'\<close> by (intro SUP_upper2[where i="{x}"]) auto
  1.2827 +          qed (insert \<open>x\<in>S'\<close> S', auto)
  1.2828 +        qed
  1.2829 +      next
  1.2830 +        assume "x \<notin> S'"
  1.2831 +        then have "sets x \<noteq> sets b"
  1.2832 +          using \<open>x\<in>S\<close> by (auto simp: S')
  1.2833 +        moreover have "sets x \<le> sets b"
  1.2834 +          using \<open>x\<in>S\<close> unfolding b by auto
  1.2835 +        ultimately show ?thesis
  1.2836 +          using * \<open>x \<in> S\<close>
  1.2837 +          by (intro less_eq_measure.intros(2))
  1.2838 +             (simp_all add: * \<open>space x = space b\<close> less_le)
  1.2839 +      qed
  1.2840 +    next
  1.2841 +      assume "x \<notin> S"
  1.2842 +      with \<open>x\<in>A\<close> \<open>x \<notin> S\<close> \<open>space b = space a\<close> show ?thesis
  1.2843 +        by (intro less_eq_measure.intros)
  1.2844 +           (simp_all add: * less_le a SUP_upper S)
  1.2845 +    qed
  1.2846 +  qed
  1.2847 +  show least: "Sup A \<le> x" if x: "\<And>z. z \<in> A \<Longrightarrow> z \<le> x" for x :: "'a measure" and A
  1.2848 +    unfolding Sup_measure_def
  1.2849 +  proof (intro Sup_lexord[where P="\<lambda>y. y \<le> x"])
  1.2850 +    assume "\<And>a. a \<in> A \<Longrightarrow> space a \<noteq> (\<Union>a\<in>A. space a)"
  1.2851 +    show "sigma (UNION A space) {} \<le> x"
  1.2852 +      using x[THEN le_measureD1] by (subst sigma_le_iff) auto
  1.2853 +  next
  1.2854 +    fix a S assume "a \<in> A" "space a = (\<Union>a\<in>A. space a)" and S: "S = {a' \<in> A. space a' = space a}"
  1.2855 +      "\<And>a. a \<in> S \<Longrightarrow> sets a \<noteq> (\<Union>a\<in>S. sets a)"
  1.2856 +    have "UNION S space \<subseteq> space x"
  1.2857 +      using S le_measureD1[OF x] by auto
  1.2858 +    moreover
  1.2859 +    have "UNION S space = space a"
  1.2860 +      using \<open>a\<in>A\<close> S by auto
  1.2861 +    then have "space x = UNION S space \<Longrightarrow> UNION S sets \<subseteq> sets x"
  1.2862 +      using \<open>a \<in> A\<close> le_measureD2[OF x] by (auto simp: S)
  1.2863 +    ultimately show "sigma (UNION S space) (UNION S sets) \<le> x"
  1.2864 +      by (subst sigma_le_iff) simp_all
  1.2865 +  next
  1.2866 +    fix a b S S' assume "a \<in> A" and a: "space a = (\<Union>a\<in>A. space a)" and S: "S = {a' \<in> A. space a' = space a}"
  1.2867 +      and "b \<in> S" and b: "sets b = (\<Union>a\<in>S. sets a)" and S': "S' = {a' \<in> S. sets a' = sets b}"
  1.2868 +    then have "S' \<noteq> {}" "space b = space a"
  1.2869 +      by auto
  1.2870 +    have sets_eq: "\<And>x. x \<in> S' \<Longrightarrow> sets x = sets b"
  1.2871 +      by (auto simp: S')
  1.2872 +    note sets_eq[THEN sets_eq_imp_space_eq, simp]
  1.2873 +    have *: "sets (Sup_measure' S') = sets b" "space (Sup_measure' S') = space b"
  1.2874 +      using \<open>S' \<noteq> {}\<close> by (simp_all add: Sup_measure'_def sets_eq)
  1.2875 +    show "Sup_measure' S' \<le> x"
  1.2876 +    proof cases
  1.2877 +      assume "space x = space a"
  1.2878 +      show ?thesis
  1.2879 +      proof cases
  1.2880 +        assume **: "sets x = sets b"
  1.2881 +        show ?thesis
  1.2882 +        proof (intro le_measure[THEN iffD2] ballI)
  1.2883 +          show ***: "sets (Sup_measure' S') = sets x"
  1.2884 +            by (simp add: * **)
  1.2885 +          fix X assume "X \<in> sets (Sup_measure' S')"
  1.2886 +          show "emeasure (Sup_measure' S') X \<le> emeasure x X"
  1.2887 +            unfolding ***
  1.2888 +          proof (subst emeasure_Sup_measure'[OF _ \<open>X \<in> sets (Sup_measure' S')\<close>])
  1.2889 +            show "(SUP P : {P. finite P \<and> P \<subseteq> S'}. emeasure (sup_measure.F id P) X) \<le> emeasure x X"
  1.2890 +            proof (safe intro!: SUP_least)
  1.2891 +              fix P assume P: "finite P" "P \<subseteq> S'"
  1.2892 +              show "emeasure (sup_measure.F id P) X \<le> emeasure x X"
  1.2893 +              proof cases
  1.2894 +                assume "P = {}" then show ?thesis
  1.2895 +                  by auto
  1.2896 +              next
  1.2897 +                assume "P \<noteq> {}"
  1.2898 +                from P have "finite P" "P \<subseteq> A"
  1.2899 +                  unfolding S' S by (simp_all add: subset_eq)
  1.2900 +                then have "sup_measure.F id P \<le> x"
  1.2901 +                  by (induction P) (auto simp: x)
  1.2902 +                moreover have "sets (sup_measure.F id P) = sets x"
  1.2903 +                  using \<open>finite P\<close> \<open>P \<noteq> {}\<close> \<open>P \<subseteq> S'\<close> \<open>sets x = sets b\<close>
  1.2904 +                  by (intro sets_sup_measure_F) (auto simp: S')
  1.2905 +                ultimately show "emeasure (sup_measure.F id P) X \<le> emeasure x X"
  1.2906 +                  by (rule le_measureD3)
  1.2907 +              qed
  1.2908 +            qed
  1.2909 +            show "m \<in> S' \<Longrightarrow> sets m = sets (Sup_measure' S')" for m
  1.2910 +              unfolding * by (simp add: S')
  1.2911 +          qed fact
  1.2912 +        qed
  1.2913 +      next
  1.2914 +        assume "sets x \<noteq> sets b"
  1.2915 +        moreover have "sets b \<le> sets x"
  1.2916 +          unfolding b S using x[THEN le_measureD2] \<open>space x = space a\<close> by auto
  1.2917 +        ultimately show "Sup_measure' S' \<le> x"
  1.2918 +          using \<open>space x = space a\<close> \<open>b \<in> S\<close>
  1.2919 +          by (intro less_eq_measure.intros(2)) (simp_all add: * S)
  1.2920 +      qed
  1.2921 +    next
  1.2922 +      assume "space x \<noteq> space a"
  1.2923 +      then have "space a < space x"
  1.2924 +        using le_measureD1[OF x[OF \<open>a\<in>A\<close>]] by auto
  1.2925 +      then show "Sup_measure' S' \<le> x"
  1.2926 +        by (intro less_eq_measure.intros) (simp add: * \<open>space b = space a\<close>)
  1.2927 +    qed
  1.2928 +  qed
  1.2929 +  show "Sup {} = (bot::'a measure)" "Inf {} = (top::'a measure)"
  1.2930 +    by (auto intro!: antisym least simp: top_measure_def)
  1.2931 +  show lower: "x \<in> A \<Longrightarrow> Inf A \<le> x" for x :: "'a measure" and A
  1.2932 +    unfolding Inf_measure_def by (intro least) auto
  1.2933 +  show greatest: "(\<And>z. z \<in> A \<Longrightarrow> x \<le> z) \<Longrightarrow> x \<le> Inf A" for x :: "'a measure" and A
  1.2934 +    unfolding Inf_measure_def by (intro upper) auto
  1.2935 +  show "inf x y \<le> x" "inf x y \<le> y" "x \<le> y \<Longrightarrow> x \<le> z \<Longrightarrow> x \<le> inf y z" for x y z :: "'a measure"
  1.2936 +    by (auto simp: inf_measure_def intro!: lower greatest)
  1.2937 +qed
  1.2938 +
  1.2939 +end
  1.2940 +
  1.2941 +lemma sets_SUP:
  1.2942 +  assumes "\<And>x. x \<in> I \<Longrightarrow> sets (M x) = sets N"
  1.2943 +  shows "I \<noteq> {} \<Longrightarrow> sets (SUP i:I. M i) = sets N"
  1.2944 +  unfolding Sup_measure_def
  1.2945 +  using assms assms[THEN sets_eq_imp_space_eq]
  1.2946 +    sets_Sup_measure'[where A=N and M="M`I"]
  1.2947 +  by (intro Sup_lexord1[where P="\<lambda>x. sets x = sets N"]) auto
  1.2948 +
  1.2949 +lemma emeasure_SUP:
  1.2950 +  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets N" "X \<in> sets N" "I \<noteq> {}"
  1.2951 +  shows "emeasure (SUP i:I. M i) X = (SUP J:{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emeasure (SUP i:J. M i) X)"
  1.2952 +proof -
  1.2953 +  have eq: "finite J \<Longrightarrow> sup_measure.F id J = (SUP i:J. i)" for J :: "'b measure set"
  1.2954 +    by (induction J rule: finite_induct) auto
  1.2955 +  have 1: "J \<noteq> {} \<Longrightarrow> J \<subseteq> I \<Longrightarrow> sets (SUP x:J. M x) = sets N" for J
  1.2956 +    by (intro sets_SUP sets) (auto )
  1.2957 +
  1.2958 +  from \<open>I \<noteq> {}\<close> obtain i where "i\<in>I" by auto
  1.2959 +  have "Sup_measure' (M`I) X = (SUP P:{P. finite P \<and> P \<subseteq> M`I}. sup_measure.F id P X)"
  1.2960 +    using sets by (intro emeasure_Sup_measure') auto
  1.2961 +  also have "Sup_measure' (M`I) = (SUP i:I. M i)"
  1.2962 +    unfolding Sup_measure_def using \<open>I \<noteq> {}\<close> sets sets(1)[THEN sets_eq_imp_space_eq]
  1.2963 +    by (intro Sup_lexord1[where P="\<lambda>x. _ = x"]) auto
  1.2964 +  also have "(SUP P:{P. finite P \<and> P \<subseteq> M`I}. sup_measure.F id P X) =
  1.2965 +    (SUP J:{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. (SUP i:J. M i) X)"
  1.2966 +  proof (intro SUP_eq)
  1.2967 +    fix J assume "J \<in> {P. finite P \<and> P \<subseteq> M`I}"
  1.2968 +    then obtain J' where J': "J' \<subseteq> I" "finite J'" and J: "J = M`J'" and "finite J"
  1.2969 +      using finite_subset_image[of J M I] by auto
  1.2970 +    show "\<exists>j\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. sup_measure.F id J X \<le> (SUP i:j. M i) X"
  1.2971 +    proof cases
  1.2972 +      assume "J' = {}" with \<open>i \<in> I\<close> show ?thesis
  1.2973 +        by (auto simp add: J)
  1.2974 +    next
  1.2975 +      assume "J' \<noteq> {}" with J J' show ?thesis
  1.2976 +        by (intro bexI[of _ "J'"]) (auto simp add: eq simp del: id_apply)
  1.2977 +    qed
  1.2978 +  next
  1.2979 +    fix J assume J: "J \<in> {P. P \<noteq> {} \<and> finite P \<and> P \<subseteq> I}"
  1.2980 +    show "\<exists>J'\<in>{J. finite J \<and> J \<subseteq> M`I}. (SUP i:J. M i) X \<le> sup_measure.F id J' X"
  1.2981 +      using J by (intro bexI[of _ "M`J"]) (auto simp add: eq simp del: id_apply)
  1.2982 +  qed
  1.2983 +  finally show ?thesis .
  1.2984 +qed
  1.2985 +
  1.2986 +lemma emeasure_SUP_chain:
  1.2987 +  assumes sets: "\<And>i. i \<in> A \<Longrightarrow> sets (M i) = sets N" "X \<in> sets N"
  1.2988 +  assumes ch: "Complete_Partial_Order.chain op \<le> (M ` A)" and "A \<noteq> {}"
  1.2989 +  shows "emeasure (SUP i:A. M i) X = (SUP i:A. emeasure (M i) X)"
  1.2990 +proof (subst emeasure_SUP[OF sets \<open>A \<noteq> {}\<close>])
  1.2991 +  show "(SUP J:{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> A}. emeasure (SUPREMUM J M) X) = (SUP i:A. emeasure (M i) X)"
  1.2992 +  proof (rule SUP_eq)
  1.2993 +    fix J assume "J \<in> {J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> A}"
  1.2994 +    then have J: "Complete_Partial_Order.chain op \<le> (M ` J)" "finite J" "J \<noteq> {}" and "J \<subseteq> A"
  1.2995 +      using ch[THEN chain_subset, of "M`J"] by auto
  1.2996 +    with in_chain_finite[OF J(1)] obtain j where "j \<in> J" "(SUP j:J. M j) = M j"
  1.2997 +      by auto
  1.2998 +    with \<open>J \<subseteq> A\<close> show "\<exists>j\<in>A. emeasure (SUPREMUM J M) X \<le> emeasure (M j) X"
  1.2999 +      by auto
  1.3000 +  next
  1.3001 +    fix j assume "j\<in>A" then show "\<exists>i\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> A}. emeasure (M j) X \<le> emeasure (SUPREMUM i M) X"
  1.3002 +      by (intro bexI[of _ "{j}"]) auto
  1.3003 +  qed
  1.3004 +qed
  1.3005 +
  1.3006 +subsubsection \<open>Supremum of a set of $\sigma$-algebras\<close>
  1.3007 +
  1.3008 +lemma space_Sup_eq_UN: "space (Sup M) = (\<Union>x\<in>M. space x)"
  1.3009 +  unfolding Sup_measure_def
  1.3010 +  apply (intro Sup_lexord[where P="\<lambda>x. space x = _"])
  1.3011 +  apply (subst space_Sup_measure'2)
  1.3012 +  apply auto []
  1.3013 +  apply (subst space_measure_of[OF UN_space_closed])
  1.3014 +  apply auto
  1.3015 +  done
  1.3016 +
  1.3017 +lemma sets_Sup_eq:
  1.3018 +  assumes *: "\<And>m. m \<in> M \<Longrightarrow> space m = X" and "M \<noteq> {}"
  1.3019 +  shows "sets (Sup M) = sigma_sets X (\<Union>x\<in>M. sets x)"
  1.3020 +  unfolding Sup_measure_def
  1.3021 +  apply (rule Sup_lexord1)
  1.3022 +  apply fact
  1.3023 +  apply (simp add: assms)
  1.3024 +  apply (rule Sup_lexord)
  1.3025 +  subgoal premises that for a S
  1.3026 +    unfolding that(3) that(2)[symmetric]
  1.3027 +    using that(1)
  1.3028 +    apply (subst sets_Sup_measure'2)
  1.3029 +    apply (intro arg_cong2[where f=sigma_sets])
  1.3030 +    apply (auto simp: *)
  1.3031 +    done
  1.3032 +  apply (subst sets_measure_of[OF UN_space_closed])
  1.3033 +  apply (simp add:  assms)
  1.3034 +  done
  1.3035 +
  1.3036 +lemma in_sets_Sup: "(\<And>m. m \<in> M \<Longrightarrow> space m = X) \<Longrightarrow> m \<in> M \<Longrightarrow> A \<in> sets m \<Longrightarrow> A \<in> sets (Sup M)"
  1.3037 +  by (subst sets_Sup_eq[where X=X]) auto
  1.3038 +
  1.3039 +lemma Sup_lexord_rel:
  1.3040 +  assumes "\<And>i. i \<in> I \<Longrightarrow> k (A i) = k (B i)"
  1.3041 +    "R (c (A ` {a \<in> I. k (B a) = (SUP x:I. k (B x))})) (c (B ` {a \<in> I. k (B a) = (SUP x:I. k (B x))}))"
  1.3042 +    "R (s (A`I)) (s (B`I))"
  1.3043 +  shows "R (Sup_lexord k c s (A`I)) (Sup_lexord k c s (B`I))"
  1.3044 +proof -
  1.3045 +  have "A ` {a \<in> I. k (B a) = (SUP x:I. k (B x))} =  {a \<in> A ` I. k a = (SUP x:I. k (B x))}"
  1.3046 +    using assms(1) by auto
  1.3047 +  moreover have "B ` {a \<in> I. k (B a) = (SUP x:I. k (B x))} =  {a \<in> B ` I. k a = (SUP x:I. k (B x))}"
  1.3048 +    by auto
  1.3049 +  ultimately show ?thesis
  1.3050 +    using assms by (auto simp: Sup_lexord_def Let_def)
  1.3051 +qed
  1.3052 +
  1.3053 +lemma sets_SUP_cong:
  1.3054 +  assumes eq: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i)" shows "sets (SUP i:I. M i) = sets (SUP i:I. N i)"
  1.3055 +  unfolding Sup_measure_def
  1.3056 +  using eq eq[THEN sets_eq_imp_space_eq]
  1.3057 +  apply (intro Sup_lexord_rel[where R="\<lambda>x y. sets x = sets y"])
  1.3058 +  apply simp
  1.3059 +  apply simp
  1.3060 +  apply (simp add: sets_Sup_measure'2)
  1.3061 +  apply (intro arg_cong2[where f="\<lambda>x y. sets (sigma x y)"])
  1.3062 +  apply auto
  1.3063 +  done
  1.3064 +
  1.3065 +lemma sets_Sup_in_sets:
  1.3066 +  assumes "M \<noteq> {}"
  1.3067 +  assumes "\<And>m. m \<in> M \<Longrightarrow> space m = space N"
  1.3068 +  assumes "\<And>m. m \<in> M \<Longrightarrow> sets m \<subseteq> sets N"
  1.3069 +  shows "sets (Sup M) \<subseteq> sets N"
  1.3070 +proof -
  1.3071 +  have *: "UNION M space = space N"
  1.3072 +    using assms by auto
  1.3073 +  show ?thesis
  1.3074 +    unfolding * using assms by (subst sets_Sup_eq[of M "space N"]) (auto intro!: sets.sigma_sets_subset)
  1.3075 +qed
  1.3076 +
  1.3077 +lemma measurable_Sup1:
  1.3078 +  assumes m: "m \<in> M" and f: "f \<in> measurable m N"
  1.3079 +    and const_space: "\<And>m n. m \<in> M \<Longrightarrow> n \<in> M \<Longrightarrow> space m = space n"
  1.3080 +  shows "f \<in> measurable (Sup M) N"
  1.3081 +proof -
  1.3082 +  have "space (Sup M) = space m"
  1.3083 +    using m by (auto simp add: space_Sup_eq_UN dest: const_space)
  1.3084 +  then show ?thesis
  1.3085 +    using m f unfolding measurable_def by (auto intro: in_sets_Sup[OF const_space])
  1.3086 +qed
  1.3087 +
  1.3088 +lemma measurable_Sup2:
  1.3089 +  assumes M: "M \<noteq> {}"
  1.3090 +  assumes f: "\<And>m. m \<in> M \<Longrightarrow> f \<in> measurable N m"
  1.3091 +    and const_space: "\<And>m n. m \<in> M \<Longrightarrow> n \<in> M \<Longrightarrow> space m = space n"
  1.3092 +  shows "f \<in> measurable N (Sup M)"
  1.3093 +proof -
  1.3094 +  from M obtain m where "m \<in> M" by auto
  1.3095 +  have space_eq: "\<And>n. n \<in> M \<Longrightarrow> space n = space m"
  1.3096 +    by (intro const_space \<open>m \<in> M\<close>)
  1.3097 +  have "f \<in> measurable N (sigma (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m))"
  1.3098 +  proof (rule measurable_measure_of)
  1.3099 +    show "f \<in> space N \<rightarrow> UNION M space"
  1.3100 +      using measurable_space[OF f] M by auto
  1.3101 +  qed (auto intro: measurable_sets f dest: sets.sets_into_space)
  1.3102 +  also have "measurable N (sigma (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m)) = measurable N (Sup M)"
  1.3103 +    apply (intro measurable_cong_sets refl)
  1.3104 +    apply (subst sets_Sup_eq[OF space_eq M])
  1.3105 +    apply simp
  1.3106 +    apply (subst sets_measure_of[OF UN_space_closed])
  1.3107 +    apply (simp add: space_eq M)
  1.3108 +    done
  1.3109 +  finally show ?thesis .
  1.3110 +qed
  1.3111 +
  1.3112 +lemma sets_Sup_sigma:
  1.3113 +  assumes [simp]: "M \<noteq> {}" and M: "\<And>m. m \<in> M \<Longrightarrow> m \<subseteq> Pow \<Omega>"
  1.3114 +  shows "sets (SUP m:M. sigma \<Omega> m) = sets (sigma \<Omega> (\<Union>M))"
  1.3115 +proof -
  1.3116 +  { fix a m assume "a \<in> sigma_sets \<Omega> m" "m \<in> M"
  1.3117 +    then have "a \<in> sigma_sets \<Omega> (\<Union>M)"
  1.3118 +     by induction (auto intro: sigma_sets.intros) }
  1.3119 +  then show "sets (SUP m:M. sigma \<Omega> m) = sets (sigma \<Omega> (\<Union>M))"
  1.3120 +    apply (subst sets_Sup_eq[where X="\<Omega>"])
  1.3121 +    apply (auto simp add: M) []
  1.3122 +    apply auto []
  1.3123 +    apply (simp add: space_measure_of_conv M Union_least)
  1.3124 +    apply (rule sigma_sets_eqI)
  1.3125 +    apply auto
  1.3126 +    done
  1.3127 +qed
  1.3128 +
  1.3129 +lemma Sup_sigma:
  1.3130 +  assumes [simp]: "M \<noteq> {}" and M: "\<And>m. m \<in> M \<Longrightarrow> m \<subseteq> Pow \<Omega>"
  1.3131 +  shows "(SUP m:M. sigma \<Omega> m) = (sigma \<Omega> (\<Union>M))"
  1.3132 +proof (intro antisym SUP_least)
  1.3133 +  have *: "\<Union>M \<subseteq> Pow \<Omega>"
  1.3134 +    using M by auto
  1.3135 +  show "sigma \<Omega> (\<Union>M) \<le> (SUP m:M. sigma \<Omega> m)"
  1.3136 +  proof (intro less_eq_measure.intros(3))
  1.3137 +    show "space (sigma \<Omega> (\<Union>M)) = space (SUP m:M. sigma \<Omega> m)"
  1.3138 +      "sets (sigma \<Omega> (\<Union>M)) = sets (SUP m:M. sigma \<Omega> m)"
  1.3139 +      using sets_Sup_sigma[OF assms] sets_Sup_sigma[OF assms, THEN sets_eq_imp_space_eq]
  1.3140 +      by auto
  1.3141 +  qed (simp add: emeasure_sigma le_fun_def)
  1.3142 +  fix m assume "m \<in> M" then show "sigma \<Omega> m \<le> sigma \<Omega> (\<Union>M)"
  1.3143 +    by (subst sigma_le_iff) (auto simp add: M *)
  1.3144 +qed
  1.3145 +
  1.3146 +lemma SUP_sigma_sigma:
  1.3147 +  "M \<noteq> {} \<Longrightarrow> (\<And>m. m \<in> M \<Longrightarrow> f m \<subseteq> Pow \<Omega>) \<Longrightarrow> (SUP m:M. sigma \<Omega> (f m)) = sigma \<Omega> (\<Union>m\<in>M. f m)"
  1.3148 +  using Sup_sigma[of "f`M" \<Omega>] by auto
  1.3149 +
  1.3150 +lemma sets_vimage_Sup_eq:
  1.3151 +  assumes *: "M \<noteq> {}" "f \<in> X \<rightarrow> Y" "\<And>m. m \<in> M \<Longrightarrow> space m = Y"
  1.3152 +  shows "sets (vimage_algebra X f (Sup M)) = sets (SUP m : M. vimage_algebra X f m)"
  1.3153 +  (is "?IS = ?SI")
  1.3154 +proof
  1.3155 +  show "?IS \<subseteq> ?SI"
  1.3156 +    apply (intro sets_image_in_sets measurable_Sup2)
  1.3157 +    apply (simp add: space_Sup_eq_UN *)
  1.3158 +    apply (simp add: *)
  1.3159 +    apply (intro measurable_Sup1)
  1.3160 +    apply (rule imageI)
  1.3161 +    apply assumption
  1.3162 +    apply (rule measurable_vimage_algebra1)
  1.3163 +    apply (auto simp: *)
  1.3164 +    done
  1.3165 +  show "?SI \<subseteq> ?IS"
  1.3166 +    apply (intro sets_Sup_in_sets)
  1.3167 +    apply (auto simp: *) []
  1.3168 +    apply (auto simp: *) []
  1.3169 +    apply (elim imageE)
  1.3170 +    apply simp
  1.3171 +    apply (rule sets_image_in_sets)
  1.3172 +    apply simp
  1.3173 +    apply (simp add: measurable_def)
  1.3174 +    apply (simp add: * space_Sup_eq_UN sets_vimage_algebra2)
  1.3175 +    apply (auto intro: in_sets_Sup[OF *(3)])
  1.3176 +    done
  1.3177 +qed
  1.3178 +
  1.3179 +lemma restrict_space_eq_vimage_algebra':
  1.3180 +  "sets (restrict_space M \<Omega>) = sets (vimage_algebra (\<Omega> \<inter> space M) (\<lambda>x. x) M)"
  1.3181 +proof -
  1.3182 +  have *: "{A \<inter> (\<Omega> \<inter> space M) |A. A \<in> sets M} = {A \<inter> \<Omega> |A. A \<in> sets M}"
  1.3183 +    using sets.sets_into_space[of _ M] by blast
  1.3184 +
  1.3185 +  show ?thesis
  1.3186 +    unfolding restrict_space_def
  1.3187 +    by (subst sets_measure_of)
  1.3188 +       (auto simp add: image_subset_iff sets_vimage_algebra * dest: sets.sets_into_space intro!: arg_cong2[where f=sigma_sets])
  1.3189 +qed
  1.3190 +
  1.3191 +lemma sigma_le_sets:
  1.3192 +  assumes [simp]: "A \<subseteq> Pow X" shows "sets (sigma X A) \<subseteq> sets N \<longleftrightarrow> X \<in> sets N \<and> A \<subseteq> sets N"
  1.3193 +proof
  1.3194 +  have "X \<in> sigma_sets X A" "A \<subseteq> sigma_sets X A"
  1.3195 +    by (auto intro: sigma_sets_top)
  1.3196 +  moreover assume "sets (sigma X A) \<subseteq> sets N"
  1.3197 +  ultimately show "X \<in> sets N \<and> A \<subseteq> sets N"
  1.3198 +    by auto
  1.3199 +next
  1.3200 +  assume *: "X \<in> sets N \<and> A \<subseteq> sets N"
  1.3201 +  { fix Y assume "Y \<in> sigma_sets X A" from this * have "Y \<in> sets N"
  1.3202 +      by induction auto }
  1.3203 +  then show "sets (sigma X A) \<subseteq> sets N"
  1.3204 +    by auto
  1.3205 +qed
  1.3206 +
  1.3207 +lemma measurable_iff_sets:
  1.3208 +  "f \<in> measurable M N \<longleftrightarrow> (f \<in> space M \<rightarrow> space N \<and> sets (vimage_algebra (space M) f N) \<subseteq> sets M)"
  1.3209 +proof -
  1.3210 +  have *: "{f -` A \<inter> space M |A. A \<in> sets N} \<subseteq> Pow (space M)"
  1.3211 +    by auto
  1.3212 +  show ?thesis
  1.3213 +    unfolding measurable_def
  1.3214 +    by (auto simp add: vimage_algebra_def sigma_le_sets[OF *])
  1.3215 +qed
  1.3216 +
  1.3217 +lemma sets_vimage_algebra_space: "X \<in> sets (vimage_algebra X f M)"
  1.3218 +  using sets.top[of "vimage_algebra X f M"] by simp
  1.3219 +
  1.3220 +lemma measurable_mono:
  1.3221 +  assumes N: "sets N' \<le> sets N" "space N = space N'"
  1.3222 +  assumes M: "sets M \<le> sets M'" "space M = space M'"
  1.3223 +  shows "measurable M N \<subseteq> measurable M' N'"
  1.3224 +  unfolding measurable_def
  1.3225 +proof safe
  1.3226 +  fix f A assume "f \<in> space M \<rightarrow> space N" "A \<in> sets N'"
  1.3227 +  moreover assume "\<forall>y\<in>sets N. f -` y \<inter> space M \<in> sets M" note this[THEN bspec, of A]
  1.3228 +  ultimately show "f -` A \<inter> space M' \<in> sets M'"
  1.3229 +    using assms by auto
  1.3230 +qed (insert N M, auto)
  1.3231 +
  1.3232 +lemma measurable_Sup_measurable:
  1.3233 +  assumes f: "f \<in> space N \<rightarrow> A"
  1.3234 +  shows "f \<in> measurable N (Sup {M. space M = A \<and> f \<in> measurable N M})"
  1.3235 +proof (rule measurable_Sup2)
  1.3236 +  show "{M. space M = A \<and> f \<in> measurable N M} \<noteq> {}"
  1.3237 +    using f unfolding ex_in_conv[symmetric]
  1.3238 +    by (intro exI[of _ "sigma A {}"]) (auto intro!: measurable_measure_of)
  1.3239 +qed auto
  1.3240 +
  1.3241 +lemma (in sigma_algebra) sigma_sets_subset':
  1.3242 +  assumes a: "a \<subseteq> M" "\<Omega>' \<in> M"
  1.3243 +  shows "sigma_sets \<Omega>' a \<subseteq> M"
  1.3244 +proof
  1.3245 +  show "x \<in> M" if x: "x \<in> sigma_sets \<Omega>' a" for x
  1.3246 +    using x by (induct rule: sigma_sets.induct) (insert a, auto)
  1.3247 +qed
  1.3248 +
  1.3249 +lemma in_sets_SUP: "i \<in> I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> space (M i) = Y) \<Longrightarrow> X \<in> sets (M i) \<Longrightarrow> X \<in> sets (SUP i:I. M i)"
  1.3250 +  by (intro in_sets_Sup[where X=Y]) auto
  1.3251 +
  1.3252 +lemma measurable_SUP1:
  1.3253 +  "i \<in> I \<Longrightarrow> f \<in> measurable (M i) N \<Longrightarrow> (\<And>m n. m \<in> I \<Longrightarrow> n \<in> I \<Longrightarrow> space (M m) = space (M n)) \<Longrightarrow>
  1.3254 +    f \<in> measurable (SUP i:I. M i) N"
  1.3255 +  by (auto intro: measurable_Sup1)
  1.3256 +
  1.3257 +lemma sets_image_in_sets':
  1.3258 +  assumes X: "X \<in> sets N"
  1.3259 +  assumes f: "\<And>A. A \<in> sets M \<Longrightarrow> f -` A \<inter> X \<in> sets N"
  1.3260 +  shows "sets (vimage_algebra X f M) \<subseteq> sets N"
  1.3261 +  unfolding sets_vimage_algebra
  1.3262 +  by (rule sets.sigma_sets_subset') (auto intro!: measurable_sets X f)
  1.3263 +
  1.3264 +lemma mono_vimage_algebra:
  1.3265 +  "sets M \<le> sets N \<Longrightarrow> sets (vimage_algebra X f M) \<subseteq> sets (vimage_algebra X f N)"
  1.3266 +  using sets.top[of "sigma X {f -` A \<inter> X |A. A \<in> sets N}"]
  1.3267 +  unfolding vimage_algebra_def
  1.3268 +  apply (subst (asm) space_measure_of)
  1.3269 +  apply auto []
  1.3270 +  apply (subst sigma_le_sets)
  1.3271 +  apply auto
  1.3272 +  done
  1.3273 +
  1.3274 +lemma mono_restrict_space: "sets M \<le> sets N \<Longrightarrow> sets (restrict_space M X) \<subseteq> sets (restrict_space N X)"
  1.3275 +  unfolding sets_restrict_space by (rule image_mono)
  1.3276 +
  1.3277 +lemma sets_eq_bot: "sets M = {{}} \<longleftrightarrow> M = bot"
  1.3278 +  apply safe
  1.3279 +  apply (intro measure_eqI)
  1.3280 +  apply auto
  1.3281 +  done
  1.3282 +
  1.3283 +lemma sets_eq_bot2: "{{}} = sets M \<longleftrightarrow> M = bot"
  1.3284 +  using sets_eq_bot[of M] by blast
  1.3285 +
  1.3286 +
  1.3287 +lemma (in finite_measure) countable_support:
  1.3288 +  "countable {x. measure M {x} \<noteq> 0}"
  1.3289 +proof cases
  1.3290 +  assume "measure M (space M) = 0"
  1.3291 +  with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
  1.3292 +    by auto
  1.3293 +  then show ?thesis
  1.3294 +    by simp
  1.3295 +next
  1.3296 +  let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
  1.3297 +  assume "?M \<noteq> 0"
  1.3298 +  then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
  1.3299 +    using reals_Archimedean[of "?m x / ?M" for x]
  1.3300 +    by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
  1.3301 +  have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
  1.3302 +  proof (rule ccontr)
  1.3303 +    fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
  1.3304 +    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
  1.3305 +      by (metis infinite_arbitrarily_large)
  1.3306 +    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x"
  1.3307 +      by auto
  1.3308 +    { fix x assume "x \<in> X"
  1.3309 +      from \<open>?M \<noteq> 0\<close> *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
  1.3310 +      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
  1.3311 +    note singleton_sets = this
  1.3312 +    have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
  1.3313 +      using \<open>?M \<noteq> 0\<close>
  1.3314 +      by (simp add: \<open>card X = Suc (Suc n)\<close> of_nat_Suc field_simps less_le measure_nonneg)
  1.3315 +    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
  1.3316 +      by (rule setsum_mono) fact
  1.3317 +    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
  1.3318 +      using singleton_sets \<open>finite X\<close>
  1.3319 +      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
  1.3320 +    finally have "?M < measure M (\<Union>x\<in>X. {x})" .
  1.3321 +    moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
  1.3322 +      using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
  1.3323 +    ultimately show False by simp
  1.3324 +  qed
  1.3325 +  show ?thesis
  1.3326 +    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
  1.3327 +qed
  1.3328 +
  1.3329 +end