src/HOL/Analysis/Measure_Space.thy
author hoelzl
Fri Sep 23 10:26:04 2016 +0200 (2016-09-23)
changeset 63940 0d82c4c94014
parent 63658 7faa9bf9860b
child 63958 02de4a58e210
permissions -rw-r--r--
prove HK-integrable implies Lebesgue measurable; prove HK-integral equals Lebesgue integral for nonneg functions
     1 (*  Title:      HOL/Analysis/Measure_Space.thy
     2     Author:     Lawrence C Paulson
     3     Author:     Johannes Hölzl, TU München
     4     Author:     Armin Heller, TU München
     5 *)
     6 
     7 section \<open>Measure spaces and their properties\<close>
     8 
     9 theory Measure_Space
    10 imports
    11   Measurable "~~/src/HOL/Library/Extended_Nonnegative_Real"
    12 begin
    13 
    14 subsection "Relate extended reals and the indicator function"
    15 
    16 lemma suminf_cmult_indicator:
    17   fixes f :: "nat \<Rightarrow> ennreal"
    18   assumes "disjoint_family A" "x \<in> A i"
    19   shows "(\<Sum>n. f n * indicator (A n) x) = f i"
    20 proof -
    21   have **: "\<And>n. f n * indicator (A n) x = (if n = i then f n else 0 :: ennreal)"
    22     using \<open>x \<in> A i\<close> assms unfolding disjoint_family_on_def indicator_def by auto
    23   then have "\<And>n. (\<Sum>j<n. f j * indicator (A j) x) = (if i < n then f i else 0 :: ennreal)"
    24     by (auto simp: setsum.If_cases)
    25   moreover have "(SUP n. if i < n then f i else 0) = (f i :: ennreal)"
    26   proof (rule SUP_eqI)
    27     fix y :: ennreal assume "\<And>n. n \<in> UNIV \<Longrightarrow> (if i < n then f i else 0) \<le> y"
    28     from this[of "Suc i"] show "f i \<le> y" by auto
    29   qed (insert assms, simp)
    30   ultimately show ?thesis using assms
    31     by (subst suminf_eq_SUP) (auto simp: indicator_def)
    32 qed
    33 
    34 lemma suminf_indicator:
    35   assumes "disjoint_family A"
    36   shows "(\<Sum>n. indicator (A n) x :: ennreal) = indicator (\<Union>i. A i) x"
    37 proof cases
    38   assume *: "x \<in> (\<Union>i. A i)"
    39   then obtain i where "x \<in> A i" by auto
    40   from suminf_cmult_indicator[OF assms(1), OF \<open>x \<in> A i\<close>, of "\<lambda>k. 1"]
    41   show ?thesis using * by simp
    42 qed simp
    43 
    44 lemma setsum_indicator_disjoint_family:
    45   fixes f :: "'d \<Rightarrow> 'e::semiring_1"
    46   assumes d: "disjoint_family_on A P" and "x \<in> A j" and "finite P" and "j \<in> P"
    47   shows "(\<Sum>i\<in>P. f i * indicator (A i) x) = f j"
    48 proof -
    49   have "P \<inter> {i. x \<in> A i} = {j}"
    50     using d \<open>x \<in> A j\<close> \<open>j \<in> P\<close> unfolding disjoint_family_on_def
    51     by auto
    52   thus ?thesis
    53     unfolding indicator_def
    54     by (simp add: if_distrib setsum.If_cases[OF \<open>finite P\<close>])
    55 qed
    56 
    57 text \<open>
    58   The type for emeasure spaces is already defined in @{theory Sigma_Algebra}, as it is also used to
    59   represent sigma algebras (with an arbitrary emeasure).
    60 \<close>
    61 
    62 subsection "Extend binary sets"
    63 
    64 lemma LIMSEQ_binaryset:
    65   assumes f: "f {} = 0"
    66   shows  "(\<lambda>n. \<Sum>i<n. f (binaryset A B i)) \<longlonglongrightarrow> f A + f B"
    67 proof -
    68   have "(\<lambda>n. \<Sum>i < Suc (Suc n). f (binaryset A B i)) = (\<lambda>n. f A + f B)"
    69     proof
    70       fix n
    71       show "(\<Sum>i < Suc (Suc n). f (binaryset A B i)) = f A + f B"
    72         by (induct n)  (auto simp add: binaryset_def f)
    73     qed
    74   moreover
    75   have "... \<longlonglongrightarrow> f A + f B" by (rule tendsto_const)
    76   ultimately
    77   have "(\<lambda>n. \<Sum>i< Suc (Suc n). f (binaryset A B i)) \<longlonglongrightarrow> f A + f B"
    78     by metis
    79   hence "(\<lambda>n. \<Sum>i< n+2. f (binaryset A B i)) \<longlonglongrightarrow> f A + f B"
    80     by simp
    81   thus ?thesis by (rule LIMSEQ_offset [where k=2])
    82 qed
    83 
    84 lemma binaryset_sums:
    85   assumes f: "f {} = 0"
    86   shows  "(\<lambda>n. f (binaryset A B n)) sums (f A + f B)"
    87     by (simp add: sums_def LIMSEQ_binaryset [where f=f, OF f] atLeast0LessThan)
    88 
    89 lemma suminf_binaryset_eq:
    90   fixes f :: "'a set \<Rightarrow> 'b::{comm_monoid_add, t2_space}"
    91   shows "f {} = 0 \<Longrightarrow> (\<Sum>n. f (binaryset A B n)) = f A + f B"
    92   by (metis binaryset_sums sums_unique)
    93 
    94 subsection \<open>Properties of a premeasure @{term \<mu>}\<close>
    95 
    96 text \<open>
    97   The definitions for @{const positive} and @{const countably_additive} should be here, by they are
    98   necessary to define @{typ "'a measure"} in @{theory Sigma_Algebra}.
    99 \<close>
   100 
   101 definition subadditive where
   102   "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)"
   103 
   104 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"
   105   by (auto simp add: subadditive_def)
   106 
   107 definition countably_subadditive where
   108   "countably_subadditive M f \<longleftrightarrow>
   109     (\<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))))"
   110 
   111 lemma (in ring_of_sets) countably_subadditive_subadditive:
   112   fixes f :: "'a set \<Rightarrow> ennreal"
   113   assumes f: "positive M f" and cs: "countably_subadditive M f"
   114   shows  "subadditive M f"
   115 proof (auto simp add: subadditive_def)
   116   fix x y
   117   assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   118   hence "disjoint_family (binaryset x y)"
   119     by (auto simp add: disjoint_family_on_def binaryset_def)
   120   hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
   121          (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
   122          f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
   123     using cs by (auto simp add: countably_subadditive_def)
   124   hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
   125          f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
   126     by (simp add: range_binaryset_eq UN_binaryset_eq)
   127   thus "f (x \<union> y) \<le>  f x + f y" using f x y
   128     by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
   129 qed
   130 
   131 definition additive where
   132   "additive M \<mu> \<longleftrightarrow> (\<forall>x\<in>M. \<forall>y\<in>M. x \<inter> y = {} \<longrightarrow> \<mu> (x \<union> y) = \<mu> x + \<mu> y)"
   133 
   134 definition increasing where
   135   "increasing M \<mu> \<longleftrightarrow> (\<forall>x\<in>M. \<forall>y\<in>M. x \<subseteq> y \<longrightarrow> \<mu> x \<le> \<mu> y)"
   136 
   137 lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
   138 
   139 lemma positiveD_empty:
   140   "positive M f \<Longrightarrow> f {} = 0"
   141   by (auto simp add: positive_def)
   142 
   143 lemma additiveD:
   144   "additive M f \<Longrightarrow> x \<inter> y = {} \<Longrightarrow> x \<in> M \<Longrightarrow> y \<in> M \<Longrightarrow> f (x \<union> y) = f x + f y"
   145   by (auto simp add: additive_def)
   146 
   147 lemma increasingD:
   148   "increasing M f \<Longrightarrow> x \<subseteq> y \<Longrightarrow> x\<in>M \<Longrightarrow> y\<in>M \<Longrightarrow> f x \<le> f y"
   149   by (auto simp add: increasing_def)
   150 
   151 lemma countably_additiveI[case_names countably]:
   152   "(\<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))
   153   \<Longrightarrow> countably_additive M f"
   154   by (simp add: countably_additive_def)
   155 
   156 lemma (in ring_of_sets) disjointed_additive:
   157   assumes f: "positive M f" "additive M f" and A: "range A \<subseteq> M" "incseq A"
   158   shows "(\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
   159 proof (induct n)
   160   case (Suc n)
   161   then have "(\<Sum>i\<le>Suc n. f (disjointed A i)) = f (A n) + f (disjointed A (Suc n))"
   162     by simp
   163   also have "\<dots> = f (A n \<union> disjointed A (Suc n))"
   164     using A by (subst f(2)[THEN additiveD]) (auto simp: disjointed_mono)
   165   also have "A n \<union> disjointed A (Suc n) = A (Suc n)"
   166     using \<open>incseq A\<close> by (auto dest: incseq_SucD simp: disjointed_mono)
   167   finally show ?case .
   168 qed simp
   169 
   170 lemma (in ring_of_sets) additive_sum:
   171   fixes A:: "'i \<Rightarrow> 'a set"
   172   assumes f: "positive M f" and ad: "additive M f" and "finite S"
   173       and A: "A`S \<subseteq> M"
   174       and disj: "disjoint_family_on A S"
   175   shows  "(\<Sum>i\<in>S. f (A i)) = f (\<Union>i\<in>S. A i)"
   176   using \<open>finite S\<close> disj A
   177 proof induct
   178   case empty show ?case using f by (simp add: positive_def)
   179 next
   180   case (insert s S)
   181   then have "A s \<inter> (\<Union>i\<in>S. A i) = {}"
   182     by (auto simp add: disjoint_family_on_def neq_iff)
   183   moreover
   184   have "A s \<in> M" using insert by blast
   185   moreover have "(\<Union>i\<in>S. A i) \<in> M"
   186     using insert \<open>finite S\<close> by auto
   187   ultimately have "f (A s \<union> (\<Union>i\<in>S. A i)) = f (A s) + f(\<Union>i\<in>S. A i)"
   188     using ad UNION_in_sets A by (auto simp add: additive_def)
   189   with insert show ?case using ad disjoint_family_on_mono[of S "insert s S" A]
   190     by (auto simp add: additive_def subset_insertI)
   191 qed
   192 
   193 lemma (in ring_of_sets) additive_increasing:
   194   fixes f :: "'a set \<Rightarrow> ennreal"
   195   assumes posf: "positive M f" and addf: "additive M f"
   196   shows "increasing M f"
   197 proof (auto simp add: increasing_def)
   198   fix x y
   199   assume xy: "x \<in> M" "y \<in> M" "x \<subseteq> y"
   200   then have "y - x \<in> M" by auto
   201   then have "f x + 0 \<le> f x + f (y-x)" by (intro add_left_mono zero_le)
   202   also have "... = f (x \<union> (y-x))" using addf
   203     by (auto simp add: additive_def) (metis Diff_disjoint Un_Diff_cancel Diff xy(1,2))
   204   also have "... = f y"
   205     by (metis Un_Diff_cancel Un_absorb1 xy(3))
   206   finally show "f x \<le> f y" by simp
   207 qed
   208 
   209 lemma (in ring_of_sets) subadditive:
   210   fixes f :: "'a set \<Rightarrow> ennreal"
   211   assumes f: "positive M f" "additive M f" and A: "A`S \<subseteq> M" and S: "finite S"
   212   shows "f (\<Union>i\<in>S. A i) \<le> (\<Sum>i\<in>S. f (A i))"
   213 using S A
   214 proof (induct S)
   215   case empty thus ?case using f by (auto simp: positive_def)
   216 next
   217   case (insert x F)
   218   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+
   219   have subs: "(\<Union>i\<in>F. A i) - A x \<subseteq> (\<Union>i\<in>F. A i)" by auto
   220   have "(\<Union>i\<in>(insert x F). A i) = A x \<union> ((\<Union>i\<in>F. A i) - A x)" by auto
   221   hence "f (\<Union>i\<in>(insert x F). A i) = f (A x \<union> ((\<Union>i\<in>F. A i) - A x))"
   222     by simp
   223   also have "\<dots> = f (A x) + f ((\<Union>i\<in>F. A i) - A x)"
   224     using f(2) by (rule additiveD) (insert in_M, auto)
   225   also have "\<dots> \<le> f (A x) + f (\<Union>i\<in>F. A i)"
   226     using additive_increasing[OF f] in_M subs by (auto simp: increasing_def intro: add_left_mono)
   227   also have "\<dots> \<le> f (A x) + (\<Sum>i\<in>F. f (A i))" using insert by (auto intro: add_left_mono)
   228   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
   229 qed
   230 
   231 lemma (in ring_of_sets) countably_additive_additive:
   232   fixes f :: "'a set \<Rightarrow> ennreal"
   233   assumes posf: "positive M f" and ca: "countably_additive M f"
   234   shows "additive M f"
   235 proof (auto simp add: additive_def)
   236   fix x y
   237   assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   238   hence "disjoint_family (binaryset x y)"
   239     by (auto simp add: disjoint_family_on_def binaryset_def)
   240   hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
   241          (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
   242          f (\<Union>i. binaryset x y i) = (\<Sum> n. f (binaryset x y n))"
   243     using ca
   244     by (simp add: countably_additive_def)
   245   hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
   246          f (x \<union> y) = (\<Sum>n. f (binaryset x y n))"
   247     by (simp add: range_binaryset_eq UN_binaryset_eq)
   248   thus "f (x \<union> y) = f x + f y" using posf x y
   249     by (auto simp add: Un suminf_binaryset_eq positive_def)
   250 qed
   251 
   252 lemma (in algebra) increasing_additive_bound:
   253   fixes A:: "nat \<Rightarrow> 'a set" and  f :: "'a set \<Rightarrow> ennreal"
   254   assumes f: "positive M f" and ad: "additive M f"
   255       and inc: "increasing M f"
   256       and A: "range A \<subseteq> M"
   257       and disj: "disjoint_family A"
   258   shows  "(\<Sum>i. f (A i)) \<le> f \<Omega>"
   259 proof (safe intro!: suminf_le_const)
   260   fix N
   261   note disj_N = disjoint_family_on_mono[OF _ disj, of "{..<N}"]
   262   have "(\<Sum>i<N. f (A i)) = f (\<Union>i\<in>{..<N}. A i)"
   263     using A by (intro additive_sum [OF f ad _ _]) (auto simp: disj_N)
   264   also have "... \<le> f \<Omega>" using space_closed A
   265     by (intro increasingD[OF inc] finite_UN) auto
   266   finally show "(\<Sum>i<N. f (A i)) \<le> f \<Omega>" by simp
   267 qed (insert f A, auto simp: positive_def)
   268 
   269 lemma (in ring_of_sets) countably_additiveI_finite:
   270   fixes \<mu> :: "'a set \<Rightarrow> ennreal"
   271   assumes "finite \<Omega>" "positive M \<mu>" "additive M \<mu>"
   272   shows "countably_additive M \<mu>"
   273 proof (rule countably_additiveI)
   274   fix F :: "nat \<Rightarrow> 'a set" assume F: "range F \<subseteq> M" "(\<Union>i. F i) \<in> M" and disj: "disjoint_family F"
   275 
   276   have "\<forall>i\<in>{i. F i \<noteq> {}}. \<exists>x. x \<in> F i" by auto
   277   from bchoice[OF this] obtain f where f: "\<And>i. F i \<noteq> {} \<Longrightarrow> f i \<in> F i" by auto
   278 
   279   have inj_f: "inj_on f {i. F i \<noteq> {}}"
   280   proof (rule inj_onI, simp)
   281     fix i j a b assume *: "f i = f j" "F i \<noteq> {}" "F j \<noteq> {}"
   282     then have "f i \<in> F i" "f j \<in> F j" using f by force+
   283     with disj * show "i = j" by (auto simp: disjoint_family_on_def)
   284   qed
   285   have "finite (\<Union>i. F i)"
   286     by (metis F(2) assms(1) infinite_super sets_into_space)
   287 
   288   have F_subset: "{i. \<mu> (F i) \<noteq> 0} \<subseteq> {i. F i \<noteq> {}}"
   289     by (auto simp: positiveD_empty[OF \<open>positive M \<mu>\<close>])
   290   moreover have fin_not_empty: "finite {i. F i \<noteq> {}}"
   291   proof (rule finite_imageD)
   292     from f have "f`{i. F i \<noteq> {}} \<subseteq> (\<Union>i. F i)" by auto
   293     then show "finite (f`{i. F i \<noteq> {}})"
   294       by (rule finite_subset) fact
   295   qed fact
   296   ultimately have fin_not_0: "finite {i. \<mu> (F i) \<noteq> 0}"
   297     by (rule finite_subset)
   298 
   299   have disj_not_empty: "disjoint_family_on F {i. F i \<noteq> {}}"
   300     using disj by (auto simp: disjoint_family_on_def)
   301 
   302   from fin_not_0 have "(\<Sum>i. \<mu> (F i)) = (\<Sum>i | \<mu> (F i) \<noteq> 0. \<mu> (F i))"
   303     by (rule suminf_finite) auto
   304   also have "\<dots> = (\<Sum>i | F i \<noteq> {}. \<mu> (F i))"
   305     using fin_not_empty F_subset by (rule setsum.mono_neutral_left) auto
   306   also have "\<dots> = \<mu> (\<Union>i\<in>{i. F i \<noteq> {}}. F i)"
   307     using \<open>positive M \<mu>\<close> \<open>additive M \<mu>\<close> fin_not_empty disj_not_empty F by (intro additive_sum) auto
   308   also have "\<dots> = \<mu> (\<Union>i. F i)"
   309     by (rule arg_cong[where f=\<mu>]) auto
   310   finally show "(\<Sum>i. \<mu> (F i)) = \<mu> (\<Union>i. F i)" .
   311 qed
   312 
   313 lemma (in ring_of_sets) countably_additive_iff_continuous_from_below:
   314   fixes f :: "'a set \<Rightarrow> ennreal"
   315   assumes f: "positive M f" "additive M f"
   316   shows "countably_additive M f \<longleftrightarrow>
   317     (\<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))"
   318   unfolding countably_additive_def
   319 proof safe
   320   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)"
   321   fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   322   then have dA: "range (disjointed A) \<subseteq> M" by (auto simp: range_disjointed_sets)
   323   with count_sum[THEN spec, of "disjointed A"] A(3)
   324   have f_UN: "(\<Sum>i. f (disjointed A i)) = f (\<Union>i. A i)"
   325     by (auto simp: UN_disjointed_eq disjoint_family_disjointed)
   326   moreover have "(\<lambda>n. (\<Sum>i<n. f (disjointed A i))) \<longlonglongrightarrow> (\<Sum>i. f (disjointed A i))"
   327     using f(1)[unfolded positive_def] dA
   328     by (auto intro!: summable_LIMSEQ)
   329   from LIMSEQ_Suc[OF this]
   330   have "(\<lambda>n. (\<Sum>i\<le>n. f (disjointed A i))) \<longlonglongrightarrow> (\<Sum>i. f (disjointed A i))"
   331     unfolding lessThan_Suc_atMost .
   332   moreover have "\<And>n. (\<Sum>i\<le>n. f (disjointed A i)) = f (A n)"
   333     using disjointed_additive[OF f A(1,2)] .
   334   ultimately show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)" by simp
   335 next
   336   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)"
   337   fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> M" "disjoint_family A" "(\<Union>i. A i) \<in> M"
   338   have *: "(\<Union>n. (\<Union>i<n. A i)) = (\<Union>i. A i)" by auto
   339   have "(\<lambda>n. f (\<Union>i<n. A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   340   proof (unfold *[symmetric], intro cont[rule_format])
   341     show "range (\<lambda>i. \<Union>i<i. A i) \<subseteq> M" "(\<Union>i. \<Union>i<i. A i) \<in> M"
   342       using A * by auto
   343   qed (force intro!: incseq_SucI)
   344   moreover have "\<And>n. f (\<Union>i<n. A i) = (\<Sum>i<n. f (A i))"
   345     using A
   346     by (intro additive_sum[OF f, of _ A, symmetric])
   347        (auto intro: disjoint_family_on_mono[where B=UNIV])
   348   ultimately
   349   have "(\<lambda>i. f (A i)) sums f (\<Union>i. A i)"
   350     unfolding sums_def by simp
   351   from sums_unique[OF this]
   352   show "(\<Sum>i. f (A i)) = f (\<Union>i. A i)" by simp
   353 qed
   354 
   355 lemma (in ring_of_sets) continuous_from_above_iff_empty_continuous:
   356   fixes f :: "'a set \<Rightarrow> ennreal"
   357   assumes f: "positive M f" "additive M f"
   358   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))
   359      \<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)"
   360 proof safe
   361   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))"
   362   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>"
   363   with cont[THEN spec, of A] show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   364     using \<open>positive M f\<close>[unfolded positive_def] by auto
   365 next
   366   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"
   367   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>"
   368 
   369   have f_mono: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<subseteq> b \<Longrightarrow> f a \<le> f b"
   370     using additive_increasing[OF f] unfolding increasing_def by simp
   371 
   372   have decseq_fA: "decseq (\<lambda>i. f (A i))"
   373     using A by (auto simp: decseq_def intro!: f_mono)
   374   have decseq: "decseq (\<lambda>i. A i - (\<Inter>i. A i))"
   375     using A by (auto simp: decseq_def)
   376   then have decseq_f: "decseq (\<lambda>i. f (A i - (\<Inter>i. A i)))"
   377     using A unfolding decseq_def by (auto intro!: f_mono Diff)
   378   have "f (\<Inter>x. A x) \<le> f (A 0)"
   379     using A by (auto intro!: f_mono)
   380   then have f_Int_fin: "f (\<Inter>x. A x) \<noteq> \<infinity>"
   381     using A by (auto simp: top_unique)
   382   { fix i
   383     have "f (A i - (\<Inter>i. A i)) \<le> f (A i)" using A by (auto intro!: f_mono)
   384     then have "f (A i - (\<Inter>i. A i)) \<noteq> \<infinity>"
   385       using A by (auto simp: top_unique) }
   386   note f_fin = this
   387   have "(\<lambda>i. f (A i - (\<Inter>i. A i))) \<longlonglongrightarrow> 0"
   388   proof (intro cont[rule_format, OF _ decseq _ f_fin])
   389     show "range (\<lambda>i. A i - (\<Inter>i. A i)) \<subseteq> M" "(\<Inter>i. A i - (\<Inter>i. A i)) = {}"
   390       using A by auto
   391   qed
   392   from INF_Lim_ereal[OF decseq_f this]
   393   have "(INF n. f (A n - (\<Inter>i. A i))) = 0" .
   394   moreover have "(INF n. f (\<Inter>i. A i)) = f (\<Inter>i. A i)"
   395     by auto
   396   ultimately have "(INF n. f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i)) = 0 + f (\<Inter>i. A i)"
   397     using A(4) f_fin f_Int_fin
   398     by (subst INF_ennreal_add_const) (auto simp: decseq_f)
   399   moreover {
   400     fix n
   401     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))"
   402       using A by (subst f(2)[THEN additiveD]) auto
   403     also have "(A n - (\<Inter>i. A i)) \<union> (\<Inter>i. A i) = A n"
   404       by auto
   405     finally have "f (A n - (\<Inter>i. A i)) + f (\<Inter>i. A i) = f (A n)" . }
   406   ultimately have "(INF n. f (A n)) = f (\<Inter>i. A i)"
   407     by simp
   408   with LIMSEQ_INF[OF decseq_fA]
   409   show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Inter>i. A i)" by simp
   410 qed
   411 
   412 lemma (in ring_of_sets) empty_continuous_imp_continuous_from_below:
   413   fixes f :: "'a set \<Rightarrow> ennreal"
   414   assumes f: "positive M f" "additive M f" "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   415   assumes cont: "\<forall>A. range A \<subseteq> M \<longrightarrow> decseq A \<longrightarrow> (\<Inter>i. A i) = {} \<longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   416   assumes A: "range A \<subseteq> M" "incseq A" "(\<Union>i. A i) \<in> M"
   417   shows "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   418 proof -
   419   from A have "(\<lambda>i. f ((\<Union>i. A i) - A i)) \<longlonglongrightarrow> 0"
   420     by (intro cont[rule_format]) (auto simp: decseq_def incseq_def)
   421   moreover
   422   { fix i
   423     have "f ((\<Union>i. A i) - A i \<union> A i) = f ((\<Union>i. A i) - A i) + f (A i)"
   424       using A by (intro f(2)[THEN additiveD]) auto
   425     also have "((\<Union>i. A i) - A i) \<union> A i = (\<Union>i. A i)"
   426       by auto
   427     finally have "f ((\<Union>i. A i) - A i) = f (\<Union>i. A i) - f (A i)"
   428       using f(3)[rule_format, of "A i"] A by (auto simp: ennreal_add_diff_cancel subset_eq) }
   429   moreover have "\<forall>\<^sub>F i in sequentially. f (A i) \<le> f (\<Union>i. A i)"
   430     using increasingD[OF additive_increasing[OF f(1, 2)], of "A _" "\<Union>i. A i"] A
   431     by (auto intro!: always_eventually simp: subset_eq)
   432   ultimately show "(\<lambda>i. f (A i)) \<longlonglongrightarrow> f (\<Union>i. A i)"
   433     by (auto intro: ennreal_tendsto_const_minus)
   434 qed
   435 
   436 lemma (in ring_of_sets) empty_continuous_imp_countably_additive:
   437   fixes f :: "'a set \<Rightarrow> ennreal"
   438   assumes f: "positive M f" "additive M f" and fin: "\<forall>A\<in>M. f A \<noteq> \<infinity>"
   439   assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) \<longlonglongrightarrow> 0"
   440   shows "countably_additive M f"
   441   using countably_additive_iff_continuous_from_below[OF f]
   442   using empty_continuous_imp_continuous_from_below[OF f fin] cont
   443   by blast
   444 
   445 subsection \<open>Properties of @{const emeasure}\<close>
   446 
   447 lemma emeasure_positive: "positive (sets M) (emeasure M)"
   448   by (cases M) (auto simp: sets_def emeasure_def Abs_measure_inverse measure_space_def)
   449 
   450 lemma emeasure_empty[simp, intro]: "emeasure M {} = 0"
   451   using emeasure_positive[of M] by (simp add: positive_def)
   452 
   453 lemma emeasure_single_in_space: "emeasure M {x} \<noteq> 0 \<Longrightarrow> x \<in> space M"
   454   using emeasure_notin_sets[of "{x}" M] by (auto dest: sets.sets_into_space zero_less_iff_neq_zero[THEN iffD2])
   455 
   456 lemma emeasure_countably_additive: "countably_additive (sets M) (emeasure M)"
   457   by (cases M) (auto simp: sets_def emeasure_def Abs_measure_inverse measure_space_def)
   458 
   459 lemma suminf_emeasure:
   460   "range A \<subseteq> sets M \<Longrightarrow> disjoint_family A \<Longrightarrow> (\<Sum>i. emeasure M (A i)) = emeasure M (\<Union>i. A i)"
   461   using sets.countable_UN[of A UNIV M] emeasure_countably_additive[of M]
   462   by (simp add: countably_additive_def)
   463 
   464 lemma sums_emeasure:
   465   "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)"
   466   unfolding sums_iff by (intro conjI suminf_emeasure) auto
   467 
   468 lemma emeasure_additive: "additive (sets M) (emeasure M)"
   469   by (metis sets.countably_additive_additive emeasure_positive emeasure_countably_additive)
   470 
   471 lemma plus_emeasure:
   472   "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)"
   473   using additiveD[OF emeasure_additive] ..
   474 
   475 lemma setsum_emeasure:
   476   "F`I \<subseteq> sets M \<Longrightarrow> disjoint_family_on F I \<Longrightarrow> finite I \<Longrightarrow>
   477     (\<Sum>i\<in>I. emeasure M (F i)) = emeasure M (\<Union>i\<in>I. F i)"
   478   by (metis sets.additive_sum emeasure_positive emeasure_additive)
   479 
   480 lemma emeasure_mono:
   481   "a \<subseteq> b \<Longrightarrow> b \<in> sets M \<Longrightarrow> emeasure M a \<le> emeasure M b"
   482   by (metis zero_le sets.additive_increasing emeasure_additive emeasure_notin_sets emeasure_positive increasingD)
   483 
   484 lemma emeasure_space:
   485   "emeasure M A \<le> emeasure M (space M)"
   486   by (metis emeasure_mono emeasure_notin_sets sets.sets_into_space sets.top zero_le)
   487 
   488 lemma emeasure_Diff:
   489   assumes finite: "emeasure M B \<noteq> \<infinity>"
   490   and [measurable]: "A \<in> sets M" "B \<in> sets M" and "B \<subseteq> A"
   491   shows "emeasure M (A - B) = emeasure M A - emeasure M B"
   492 proof -
   493   have "(A - B) \<union> B = A" using \<open>B \<subseteq> A\<close> by auto
   494   then have "emeasure M A = emeasure M ((A - B) \<union> B)" by simp
   495   also have "\<dots> = emeasure M (A - B) + emeasure M B"
   496     by (subst plus_emeasure[symmetric]) auto
   497   finally show "emeasure M (A - B) = emeasure M A - emeasure M B"
   498     using finite by simp
   499 qed
   500 
   501 lemma emeasure_compl:
   502   "s \<in> sets M \<Longrightarrow> emeasure M s \<noteq> \<infinity> \<Longrightarrow> emeasure M (space M - s) = emeasure M (space M) - emeasure M s"
   503   by (rule emeasure_Diff) (auto dest: sets.sets_into_space)
   504 
   505 lemma Lim_emeasure_incseq:
   506   "range A \<subseteq> sets M \<Longrightarrow> incseq A \<Longrightarrow> (\<lambda>i. (emeasure M (A i))) \<longlonglongrightarrow> emeasure M (\<Union>i. A i)"
   507   using emeasure_countably_additive
   508   by (auto simp add: sets.countably_additive_iff_continuous_from_below emeasure_positive
   509     emeasure_additive)
   510 
   511 lemma incseq_emeasure:
   512   assumes "range B \<subseteq> sets M" "incseq B"
   513   shows "incseq (\<lambda>i. emeasure M (B i))"
   514   using assms by (auto simp: incseq_def intro!: emeasure_mono)
   515 
   516 lemma SUP_emeasure_incseq:
   517   assumes A: "range A \<subseteq> sets M" "incseq A"
   518   shows "(SUP n. emeasure M (A n)) = emeasure M (\<Union>i. A i)"
   519   using LIMSEQ_SUP[OF incseq_emeasure, OF A] Lim_emeasure_incseq[OF A]
   520   by (simp add: LIMSEQ_unique)
   521 
   522 lemma decseq_emeasure:
   523   assumes "range B \<subseteq> sets M" "decseq B"
   524   shows "decseq (\<lambda>i. emeasure M (B i))"
   525   using assms by (auto simp: decseq_def intro!: emeasure_mono)
   526 
   527 lemma INF_emeasure_decseq:
   528   assumes A: "range A \<subseteq> sets M" and "decseq A"
   529   and finite: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   530   shows "(INF n. emeasure M (A n)) = emeasure M (\<Inter>i. A i)"
   531 proof -
   532   have le_MI: "emeasure M (\<Inter>i. A i) \<le> emeasure M (A 0)"
   533     using A by (auto intro!: emeasure_mono)
   534   hence *: "emeasure M (\<Inter>i. A i) \<noteq> \<infinity>" using finite[of 0] by (auto simp: top_unique)
   535 
   536   have "emeasure M (A 0) - (INF n. emeasure M (A n)) = (SUP n. emeasure M (A 0) - emeasure M (A n))"
   537     by (simp add: ennreal_INF_const_minus)
   538   also have "\<dots> = (SUP n. emeasure M (A 0 - A n))"
   539     using A finite \<open>decseq A\<close>[unfolded decseq_def] by (subst emeasure_Diff) auto
   540   also have "\<dots> = emeasure M (\<Union>i. A 0 - A i)"
   541   proof (rule SUP_emeasure_incseq)
   542     show "range (\<lambda>n. A 0 - A n) \<subseteq> sets M"
   543       using A by auto
   544     show "incseq (\<lambda>n. A 0 - A n)"
   545       using \<open>decseq A\<close> by (auto simp add: incseq_def decseq_def)
   546   qed
   547   also have "\<dots> = emeasure M (A 0) - emeasure M (\<Inter>i. A i)"
   548     using A finite * by (simp, subst emeasure_Diff) auto
   549   finally show ?thesis
   550     by (rule ennreal_minus_cancel[rotated 3])
   551        (insert finite A, auto intro: INF_lower emeasure_mono)
   552 qed
   553 
   554 lemma INF_emeasure_decseq':
   555   assumes A: "\<And>i. A i \<in> sets M" and "decseq A"
   556   and finite: "\<exists>i. emeasure M (A i) \<noteq> \<infinity>"
   557   shows "(INF n. emeasure M (A n)) = emeasure M (\<Inter>i. A i)"
   558 proof -
   559   from finite obtain i where i: "emeasure M (A i) < \<infinity>"
   560     by (auto simp: less_top)
   561   have fin: "i \<le> j \<Longrightarrow> emeasure M (A j) < \<infinity>" for j
   562     by (rule le_less_trans[OF emeasure_mono i]) (auto intro!: decseqD[OF \<open>decseq A\<close>] A)
   563 
   564   have "(INF n. emeasure M (A n)) = (INF n. emeasure M (A (n + i)))"
   565   proof (rule INF_eq)
   566     show "\<exists>j\<in>UNIV. emeasure M (A (j + i)) \<le> emeasure M (A i')" for i'
   567       by (intro bexI[of _ i'] emeasure_mono decseqD[OF \<open>decseq A\<close>] A) auto
   568   qed auto
   569   also have "\<dots> = emeasure M (INF n. (A (n + i)))"
   570     using A \<open>decseq A\<close> fin by (intro INF_emeasure_decseq) (auto simp: decseq_def less_top)
   571   also have "(INF n. (A (n + i))) = (INF n. A n)"
   572     by (meson INF_eq UNIV_I assms(2) decseqD le_add1)
   573   finally show ?thesis .
   574 qed
   575 
   576 lemma emeasure_INT_decseq_subset:
   577   fixes F :: "nat \<Rightarrow> 'a set"
   578   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"
   579   assumes F_sets[measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M"
   580     and fin: "\<And>i. i \<in> I \<Longrightarrow> emeasure M (F i) \<noteq> \<infinity>"
   581   shows "emeasure M (\<Inter>i\<in>I. F i) = (INF i:I. emeasure M (F i))"
   582 proof cases
   583   assume "finite I"
   584   have "(\<Inter>i\<in>I. F i) = F (Max I)"
   585     using I \<open>finite I\<close> by (intro antisym INF_lower INF_greatest F) auto
   586   moreover have "(INF i:I. emeasure M (F i)) = emeasure M (F (Max I))"
   587     using I \<open>finite I\<close> by (intro antisym INF_lower INF_greatest F emeasure_mono) auto
   588   ultimately show ?thesis
   589     by simp
   590 next
   591   assume "infinite I"
   592   define L where "L n = (LEAST i. i \<in> I \<and> i \<ge> n)" for n
   593   have L: "L n \<in> I \<and> n \<le> L n" for n
   594     unfolding L_def
   595   proof (rule LeastI_ex)
   596     show "\<exists>x. x \<in> I \<and> n \<le> x"
   597       using \<open>infinite I\<close> finite_subset[of I "{..< n}"]
   598       by (rule_tac ccontr) (auto simp: not_le)
   599   qed
   600   have L_eq[simp]: "i \<in> I \<Longrightarrow> L i = i" for i
   601     unfolding L_def by (intro Least_equality) auto
   602   have L_mono: "i \<le> j \<Longrightarrow> L i \<le> L j" for i j
   603     using L[of j] unfolding L_def by (intro Least_le) (auto simp: L_def)
   604 
   605   have "emeasure M (\<Inter>i. F (L i)) = (INF i. emeasure M (F (L i)))"
   606   proof (intro INF_emeasure_decseq[symmetric])
   607     show "decseq (\<lambda>i. F (L i))"
   608       using L by (intro antimonoI F L_mono) auto
   609   qed (insert L fin, auto)
   610   also have "\<dots> = (INF i:I. emeasure M (F i))"
   611   proof (intro antisym INF_greatest)
   612     show "i \<in> I \<Longrightarrow> (INF i. emeasure M (F (L i))) \<le> emeasure M (F i)" for i
   613       by (intro INF_lower2[of i]) auto
   614   qed (insert L, auto intro: INF_lower)
   615   also have "(\<Inter>i. F (L i)) = (\<Inter>i\<in>I. F i)"
   616   proof (intro antisym INF_greatest)
   617     show "i \<in> I \<Longrightarrow> (\<Inter>i. F (L i)) \<subseteq> F i" for i
   618       by (intro INF_lower2[of i]) auto
   619   qed (insert L, auto)
   620   finally show ?thesis .
   621 qed
   622 
   623 lemma Lim_emeasure_decseq:
   624   assumes A: "range A \<subseteq> sets M" "decseq A" and fin: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   625   shows "(\<lambda>i. emeasure M (A i)) \<longlonglongrightarrow> emeasure M (\<Inter>i. A i)"
   626   using LIMSEQ_INF[OF decseq_emeasure, OF A]
   627   using INF_emeasure_decseq[OF A fin] by simp
   628 
   629 lemma emeasure_lfp'[consumes 1, case_names cont measurable]:
   630   assumes "P M"
   631   assumes cont: "sup_continuous F"
   632   assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
   633   shows "emeasure M {x\<in>space M. lfp F x} = (SUP i. emeasure M {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   634 proof -
   635   have "emeasure M {x\<in>space M. lfp F x} = emeasure M (\<Union>i. {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   636     using sup_continuous_lfp[OF cont] by (auto simp add: bot_fun_def intro!: arg_cong2[where f=emeasure])
   637   moreover { fix i from \<open>P M\<close> have "{x\<in>space M. (F ^^ i) (\<lambda>x. False) x} \<in> sets M"
   638     by (induct i arbitrary: M) (auto simp add: pred_def[symmetric] intro: *) }
   639   moreover have "incseq (\<lambda>i. {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   640   proof (rule incseq_SucI)
   641     fix i
   642     have "(F ^^ i) (\<lambda>x. False) \<le> (F ^^ (Suc i)) (\<lambda>x. False)"
   643     proof (induct i)
   644       case 0 show ?case by (simp add: le_fun_def)
   645     next
   646       case Suc thus ?case using monoD[OF sup_continuous_mono[OF cont] Suc] by auto
   647     qed
   648     then show "{x \<in> space M. (F ^^ i) (\<lambda>x. False) x} \<subseteq> {x \<in> space M. (F ^^ Suc i) (\<lambda>x. False) x}"
   649       by auto
   650   qed
   651   ultimately show ?thesis
   652     by (subst SUP_emeasure_incseq) auto
   653 qed
   654 
   655 lemma emeasure_lfp:
   656   assumes [simp]: "\<And>s. sets (M s) = sets N"
   657   assumes cont: "sup_continuous F" "sup_continuous f"
   658   assumes meas: "\<And>P. Measurable.pred N P \<Longrightarrow> Measurable.pred N (F P)"
   659   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"
   660   shows "emeasure (M s) {x\<in>space N. lfp F x} = lfp f s"
   661 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])
   662   fix C assume "incseq C" "\<And>i. Measurable.pred N (C i)"
   663   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}))"
   664     unfolding SUP_apply[abs_def]
   665     by (subst SUP_emeasure_incseq) (auto simp: mono_def fun_eq_iff intro!: arg_cong2[where f=emeasure])
   666 qed (auto simp add: iter le_fun_def SUP_apply[abs_def] intro!: meas cont)
   667 
   668 lemma emeasure_subadditive_finite:
   669   "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))"
   670   by (rule sets.subadditive[OF emeasure_positive emeasure_additive]) auto
   671 
   672 lemma emeasure_subadditive:
   673   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> emeasure M (A \<union> B) \<le> emeasure M A + emeasure M B"
   674   using emeasure_subadditive_finite[of "{True, False}" "\<lambda>True \<Rightarrow> A | False \<Rightarrow> B" M] by simp
   675 
   676 lemma emeasure_subadditive_countably:
   677   assumes "range f \<subseteq> sets M"
   678   shows "emeasure M (\<Union>i. f i) \<le> (\<Sum>i. emeasure M (f i))"
   679 proof -
   680   have "emeasure M (\<Union>i. f i) = emeasure M (\<Union>i. disjointed f i)"
   681     unfolding UN_disjointed_eq ..
   682   also have "\<dots> = (\<Sum>i. emeasure M (disjointed f i))"
   683     using sets.range_disjointed_sets[OF assms] suminf_emeasure[of "disjointed f"]
   684     by (simp add:  disjoint_family_disjointed comp_def)
   685   also have "\<dots> \<le> (\<Sum>i. emeasure M (f i))"
   686     using sets.range_disjointed_sets[OF assms] assms
   687     by (auto intro!: suminf_le emeasure_mono disjointed_subset)
   688   finally show ?thesis .
   689 qed
   690 
   691 lemma emeasure_insert:
   692   assumes sets: "{x} \<in> sets M" "A \<in> sets M" and "x \<notin> A"
   693   shows "emeasure M (insert x A) = emeasure M {x} + emeasure M A"
   694 proof -
   695   have "{x} \<inter> A = {}" using \<open>x \<notin> A\<close> by auto
   696   from plus_emeasure[OF sets this] show ?thesis by simp
   697 qed
   698 
   699 lemma emeasure_insert_ne:
   700   "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"
   701   by (rule emeasure_insert)
   702 
   703 lemma emeasure_eq_setsum_singleton:
   704   assumes "finite S" "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
   705   shows "emeasure M S = (\<Sum>x\<in>S. emeasure M {x})"
   706   using setsum_emeasure[of "\<lambda>x. {x}" S M] assms
   707   by (auto simp: disjoint_family_on_def subset_eq)
   708 
   709 lemma setsum_emeasure_cover:
   710   assumes "finite S" and "A \<in> sets M" and br_in_M: "B ` S \<subseteq> sets M"
   711   assumes A: "A \<subseteq> (\<Union>i\<in>S. B i)"
   712   assumes disj: "disjoint_family_on B S"
   713   shows "emeasure M A = (\<Sum>i\<in>S. emeasure M (A \<inter> (B i)))"
   714 proof -
   715   have "(\<Sum>i\<in>S. emeasure M (A \<inter> (B i))) = emeasure M (\<Union>i\<in>S. A \<inter> (B i))"
   716   proof (rule setsum_emeasure)
   717     show "disjoint_family_on (\<lambda>i. A \<inter> B i) S"
   718       using \<open>disjoint_family_on B S\<close>
   719       unfolding disjoint_family_on_def by auto
   720   qed (insert assms, auto)
   721   also have "(\<Union>i\<in>S. A \<inter> (B i)) = A"
   722     using A by auto
   723   finally show ?thesis by simp
   724 qed
   725 
   726 lemma emeasure_eq_0:
   727   "N \<in> sets M \<Longrightarrow> emeasure M N = 0 \<Longrightarrow> K \<subseteq> N \<Longrightarrow> emeasure M K = 0"
   728   by (metis emeasure_mono order_eq_iff zero_le)
   729 
   730 lemma emeasure_UN_eq_0:
   731   assumes "\<And>i::nat. emeasure M (N i) = 0" and "range N \<subseteq> sets M"
   732   shows "emeasure M (\<Union>i. N i) = 0"
   733 proof -
   734   have "emeasure M (\<Union>i. N i) \<le> 0"
   735     using emeasure_subadditive_countably[OF assms(2)] assms(1) by simp
   736   then show ?thesis
   737     by (auto intro: antisym zero_le)
   738 qed
   739 
   740 lemma measure_eqI_finite:
   741   assumes [simp]: "sets M = Pow A" "sets N = Pow A" and "finite A"
   742   assumes eq: "\<And>a. a \<in> A \<Longrightarrow> emeasure M {a} = emeasure N {a}"
   743   shows "M = N"
   744 proof (rule measure_eqI)
   745   fix X assume "X \<in> sets M"
   746   then have X: "X \<subseteq> A" by auto
   747   then have "emeasure M X = (\<Sum>a\<in>X. emeasure M {a})"
   748     using \<open>finite A\<close> by (subst emeasure_eq_setsum_singleton) (auto dest: finite_subset)
   749   also have "\<dots> = (\<Sum>a\<in>X. emeasure N {a})"
   750     using X eq by (auto intro!: setsum.cong)
   751   also have "\<dots> = emeasure N X"
   752     using X \<open>finite A\<close> by (subst emeasure_eq_setsum_singleton) (auto dest: finite_subset)
   753   finally show "emeasure M X = emeasure N X" .
   754 qed simp
   755 
   756 lemma measure_eqI_generator_eq:
   757   fixes M N :: "'a measure" and E :: "'a set set" and A :: "nat \<Rightarrow> 'a set"
   758   assumes "Int_stable E" "E \<subseteq> Pow \<Omega>"
   759   and eq: "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
   760   and M: "sets M = sigma_sets \<Omega> E"
   761   and N: "sets N = sigma_sets \<Omega> E"
   762   and A: "range A \<subseteq> E" "(\<Union>i. A i) = \<Omega>" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   763   shows "M = N"
   764 proof -
   765   let ?\<mu>  = "emeasure M" and ?\<nu> = "emeasure N"
   766   interpret S: sigma_algebra \<Omega> "sigma_sets \<Omega> E" by (rule sigma_algebra_sigma_sets) fact
   767   have "space M = \<Omega>"
   768     using sets.top[of M] sets.space_closed[of M] S.top S.space_closed \<open>sets M = sigma_sets \<Omega> E\<close>
   769     by blast
   770 
   771   { fix F D assume "F \<in> E" and "?\<mu> F \<noteq> \<infinity>"
   772     then have [intro]: "F \<in> sigma_sets \<Omega> E" by auto
   773     have "?\<nu> F \<noteq> \<infinity>" using \<open>?\<mu> F \<noteq> \<infinity>\<close> \<open>F \<in> E\<close> eq by simp
   774     assume "D \<in> sets M"
   775     with \<open>Int_stable E\<close> \<open>E \<subseteq> Pow \<Omega>\<close> have "emeasure M (F \<inter> D) = emeasure N (F \<inter> D)"
   776       unfolding M
   777     proof (induct rule: sigma_sets_induct_disjoint)
   778       case (basic A)
   779       then have "F \<inter> A \<in> E" using \<open>Int_stable E\<close> \<open>F \<in> E\<close> by (auto simp: Int_stable_def)
   780       then show ?case using eq by auto
   781     next
   782       case empty then show ?case by simp
   783     next
   784       case (compl A)
   785       then have **: "F \<inter> (\<Omega> - A) = F - (F \<inter> A)"
   786         and [intro]: "F \<inter> A \<in> sigma_sets \<Omega> E"
   787         using \<open>F \<in> E\<close> S.sets_into_space by (auto simp: M)
   788       have "?\<nu> (F \<inter> A) \<le> ?\<nu> F" by (auto intro!: emeasure_mono simp: M N)
   789       then have "?\<nu> (F \<inter> A) \<noteq> \<infinity>" using \<open>?\<nu> F \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   790       have "?\<mu> (F \<inter> A) \<le> ?\<mu> F" by (auto intro!: emeasure_mono simp: M N)
   791       then have "?\<mu> (F \<inter> A) \<noteq> \<infinity>" using \<open>?\<mu> F \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   792       then have "?\<mu> (F \<inter> (\<Omega> - A)) = ?\<mu> F - ?\<mu> (F \<inter> A)" unfolding **
   793         using \<open>F \<inter> A \<in> sigma_sets \<Omega> E\<close> by (auto intro!: emeasure_Diff simp: M N)
   794       also have "\<dots> = ?\<nu> F - ?\<nu> (F \<inter> A)" using eq \<open>F \<in> E\<close> compl by simp
   795       also have "\<dots> = ?\<nu> (F \<inter> (\<Omega> - A))" unfolding **
   796         using \<open>F \<inter> A \<in> sigma_sets \<Omega> E\<close> \<open>?\<nu> (F \<inter> A) \<noteq> \<infinity>\<close>
   797         by (auto intro!: emeasure_Diff[symmetric] simp: M N)
   798       finally show ?case
   799         using \<open>space M = \<Omega>\<close> by auto
   800     next
   801       case (union A)
   802       then have "?\<mu> (\<Union>x. F \<inter> A x) = ?\<nu> (\<Union>x. F \<inter> A x)"
   803         by (subst (1 2) suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def subset_eq M N)
   804       with A show ?case
   805         by auto
   806     qed }
   807   note * = this
   808   show "M = N"
   809   proof (rule measure_eqI)
   810     show "sets M = sets N"
   811       using M N by simp
   812     have [simp, intro]: "\<And>i. A i \<in> sets M"
   813       using A(1) by (auto simp: subset_eq M)
   814     fix F assume "F \<in> sets M"
   815     let ?D = "disjointed (\<lambda>i. F \<inter> A i)"
   816     from \<open>space M = \<Omega>\<close> have F_eq: "F = (\<Union>i. ?D i)"
   817       using \<open>F \<in> sets M\<close>[THEN sets.sets_into_space] A(2)[symmetric] by (auto simp: UN_disjointed_eq)
   818     have [simp, intro]: "\<And>i. ?D i \<in> sets M"
   819       using sets.range_disjointed_sets[of "\<lambda>i. F \<inter> A i" M] \<open>F \<in> sets M\<close>
   820       by (auto simp: subset_eq)
   821     have "disjoint_family ?D"
   822       by (auto simp: disjoint_family_disjointed)
   823     moreover
   824     have "(\<Sum>i. emeasure M (?D i)) = (\<Sum>i. emeasure N (?D i))"
   825     proof (intro arg_cong[where f=suminf] ext)
   826       fix i
   827       have "A i \<inter> ?D i = ?D i"
   828         by (auto simp: disjointed_def)
   829       then show "emeasure M (?D i) = emeasure N (?D i)"
   830         using *[of "A i" "?D i", OF _ A(3)] A(1) by auto
   831     qed
   832     ultimately show "emeasure M F = emeasure N F"
   833       by (simp add: image_subset_iff \<open>sets M = sets N\<close>[symmetric] F_eq[symmetric] suminf_emeasure)
   834   qed
   835 qed
   836 
   837 lemma measure_of_of_measure: "measure_of (space M) (sets M) (emeasure M) = M"
   838 proof (intro measure_eqI emeasure_measure_of_sigma)
   839   show "sigma_algebra (space M) (sets M)" ..
   840   show "positive (sets M) (emeasure M)"
   841     by (simp add: positive_def)
   842   show "countably_additive (sets M) (emeasure M)"
   843     by (simp add: emeasure_countably_additive)
   844 qed simp_all
   845 
   846 subsection \<open>\<open>\<mu>\<close>-null sets\<close>
   847 
   848 definition null_sets :: "'a measure \<Rightarrow> 'a set set" where
   849   "null_sets M = {N\<in>sets M. emeasure M N = 0}"
   850 
   851 lemma null_setsD1[dest]: "A \<in> null_sets M \<Longrightarrow> emeasure M A = 0"
   852   by (simp add: null_sets_def)
   853 
   854 lemma null_setsD2[dest]: "A \<in> null_sets M \<Longrightarrow> A \<in> sets M"
   855   unfolding null_sets_def by simp
   856 
   857 lemma null_setsI[intro]: "emeasure M A = 0 \<Longrightarrow> A \<in> sets M \<Longrightarrow> A \<in> null_sets M"
   858   unfolding null_sets_def by simp
   859 
   860 interpretation null_sets: ring_of_sets "space M" "null_sets M" for M
   861 proof (rule ring_of_setsI)
   862   show "null_sets M \<subseteq> Pow (space M)"
   863     using sets.sets_into_space by auto
   864   show "{} \<in> null_sets M"
   865     by auto
   866   fix A B assume null_sets: "A \<in> null_sets M" "B \<in> null_sets M"
   867   then have sets: "A \<in> sets M" "B \<in> sets M"
   868     by auto
   869   then have *: "emeasure M (A \<union> B) \<le> emeasure M A + emeasure M B"
   870     "emeasure M (A - B) \<le> emeasure M A"
   871     by (auto intro!: emeasure_subadditive emeasure_mono)
   872   then have "emeasure M B = 0" "emeasure M A = 0"
   873     using null_sets by auto
   874   with sets * show "A - B \<in> null_sets M" "A \<union> B \<in> null_sets M"
   875     by (auto intro!: antisym zero_le)
   876 qed
   877 
   878 lemma UN_from_nat_into:
   879   assumes I: "countable I" "I \<noteq> {}"
   880   shows "(\<Union>i\<in>I. N i) = (\<Union>i. N (from_nat_into I i))"
   881 proof -
   882   have "(\<Union>i\<in>I. N i) = \<Union>(N ` range (from_nat_into I))"
   883     using I by simp
   884   also have "\<dots> = (\<Union>i. (N \<circ> from_nat_into I) i)"
   885     by simp
   886   finally show ?thesis by simp
   887 qed
   888 
   889 lemma null_sets_UN':
   890   assumes "countable I"
   891   assumes "\<And>i. i \<in> I \<Longrightarrow> N i \<in> null_sets M"
   892   shows "(\<Union>i\<in>I. N i) \<in> null_sets M"
   893 proof cases
   894   assume "I = {}" then show ?thesis by simp
   895 next
   896   assume "I \<noteq> {}"
   897   show ?thesis
   898   proof (intro conjI CollectI null_setsI)
   899     show "(\<Union>i\<in>I. N i) \<in> sets M"
   900       using assms by (intro sets.countable_UN') auto
   901     have "emeasure M (\<Union>i\<in>I. N i) \<le> (\<Sum>n. emeasure M (N (from_nat_into I n)))"
   902       unfolding UN_from_nat_into[OF \<open>countable I\<close> \<open>I \<noteq> {}\<close>]
   903       using assms \<open>I \<noteq> {}\<close> by (intro emeasure_subadditive_countably) (auto intro: from_nat_into)
   904     also have "(\<lambda>n. emeasure M (N (from_nat_into I n))) = (\<lambda>_. 0)"
   905       using assms \<open>I \<noteq> {}\<close> by (auto intro: from_nat_into)
   906     finally show "emeasure M (\<Union>i\<in>I. N i) = 0"
   907       by (intro antisym zero_le) simp
   908   qed
   909 qed
   910 
   911 lemma null_sets_UN[intro]:
   912   "(\<And>i::'i::countable. N i \<in> null_sets M) \<Longrightarrow> (\<Union>i. N i) \<in> null_sets M"
   913   by (rule null_sets_UN') auto
   914 
   915 lemma null_set_Int1:
   916   assumes "B \<in> null_sets M" "A \<in> sets M" shows "A \<inter> B \<in> null_sets M"
   917 proof (intro CollectI conjI null_setsI)
   918   show "emeasure M (A \<inter> B) = 0" using assms
   919     by (intro emeasure_eq_0[of B _ "A \<inter> B"]) auto
   920 qed (insert assms, auto)
   921 
   922 lemma null_set_Int2:
   923   assumes "B \<in> null_sets M" "A \<in> sets M" shows "B \<inter> A \<in> null_sets M"
   924   using assms by (subst Int_commute) (rule null_set_Int1)
   925 
   926 lemma emeasure_Diff_null_set:
   927   assumes "B \<in> null_sets M" "A \<in> sets M"
   928   shows "emeasure M (A - B) = emeasure M A"
   929 proof -
   930   have *: "A - B = (A - (A \<inter> B))" by auto
   931   have "A \<inter> B \<in> null_sets M" using assms by (rule null_set_Int1)
   932   then show ?thesis
   933     unfolding * using assms
   934     by (subst emeasure_Diff) auto
   935 qed
   936 
   937 lemma null_set_Diff:
   938   assumes "B \<in> null_sets M" "A \<in> sets M" shows "B - A \<in> null_sets M"
   939 proof (intro CollectI conjI null_setsI)
   940   show "emeasure M (B - A) = 0" using assms by (intro emeasure_eq_0[of B _ "B - A"]) auto
   941 qed (insert assms, auto)
   942 
   943 lemma emeasure_Un_null_set:
   944   assumes "A \<in> sets M" "B \<in> null_sets M"
   945   shows "emeasure M (A \<union> B) = emeasure M A"
   946 proof -
   947   have *: "A \<union> B = A \<union> (B - A)" by auto
   948   have "B - A \<in> null_sets M" using assms(2,1) by (rule null_set_Diff)
   949   then show ?thesis
   950     unfolding * using assms
   951     by (subst plus_emeasure[symmetric]) auto
   952 qed
   953 
   954 subsection \<open>The almost everywhere filter (i.e.\ quantifier)\<close>
   955 
   956 definition ae_filter :: "'a measure \<Rightarrow> 'a filter" where
   957   "ae_filter M = (INF N:null_sets M. principal (space M - N))"
   958 
   959 abbreviation almost_everywhere :: "'a measure \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   960   "almost_everywhere M P \<equiv> eventually P (ae_filter M)"
   961 
   962 syntax
   963   "_almost_everywhere" :: "pttrn \<Rightarrow> 'a \<Rightarrow> bool \<Rightarrow> bool" ("AE _ in _. _" [0,0,10] 10)
   964 
   965 translations
   966   "AE x in M. P" \<rightleftharpoons> "CONST almost_everywhere M (\<lambda>x. P)"
   967 
   968 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)"
   969   unfolding ae_filter_def by (subst eventually_INF_base) (auto simp: eventually_principal subset_eq)
   970 
   971 lemma AE_I':
   972   "N \<in> null_sets M \<Longrightarrow> {x\<in>space M. \<not> P x} \<subseteq> N \<Longrightarrow> (AE x in M. P x)"
   973   unfolding eventually_ae_filter by auto
   974 
   975 lemma AE_iff_null:
   976   assumes "{x\<in>space M. \<not> P x} \<in> sets M" (is "?P \<in> sets M")
   977   shows "(AE x in M. P x) \<longleftrightarrow> {x\<in>space M. \<not> P x} \<in> null_sets M"
   978 proof
   979   assume "AE x in M. P x" then obtain N where N: "N \<in> sets M" "?P \<subseteq> N" "emeasure M N = 0"
   980     unfolding eventually_ae_filter by auto
   981   have "emeasure M ?P \<le> emeasure M N"
   982     using assms N(1,2) by (auto intro: emeasure_mono)
   983   then have "emeasure M ?P = 0"
   984     unfolding \<open>emeasure M N = 0\<close> by auto
   985   then show "?P \<in> null_sets M" using assms by auto
   986 next
   987   assume "?P \<in> null_sets M" with assms show "AE x in M. P x" by (auto intro: AE_I')
   988 qed
   989 
   990 lemma AE_iff_null_sets:
   991   "N \<in> sets M \<Longrightarrow> N \<in> null_sets M \<longleftrightarrow> (AE x in M. x \<notin> N)"
   992   using Int_absorb1[OF sets.sets_into_space, of N M]
   993   by (subst AE_iff_null) (auto simp: Int_def[symmetric])
   994 
   995 lemma AE_not_in:
   996   "N \<in> null_sets M \<Longrightarrow> AE x in M. x \<notin> N"
   997   by (metis AE_iff_null_sets null_setsD2)
   998 
   999 lemma AE_iff_measurable:
  1000   "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"
  1001   using AE_iff_null[of _ P] by auto
  1002 
  1003 lemma AE_E[consumes 1]:
  1004   assumes "AE x in M. P x"
  1005   obtains N where "{x \<in> space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
  1006   using assms unfolding eventually_ae_filter by auto
  1007 
  1008 lemma AE_E2:
  1009   assumes "AE x in M. P x" "{x\<in>space M. P x} \<in> sets M"
  1010   shows "emeasure M {x\<in>space M. \<not> P x} = 0" (is "emeasure M ?P = 0")
  1011 proof -
  1012   have "{x\<in>space M. \<not> P x} = space M - {x\<in>space M. P x}" by auto
  1013   with AE_iff_null[of M P] assms show ?thesis by auto
  1014 qed
  1015 
  1016 lemma AE_I:
  1017   assumes "{x \<in> space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
  1018   shows "AE x in M. P x"
  1019   using assms unfolding eventually_ae_filter by auto
  1020 
  1021 lemma AE_mp[elim!]:
  1022   assumes AE_P: "AE x in M. P x" and AE_imp: "AE x in M. P x \<longrightarrow> Q x"
  1023   shows "AE x in M. Q x"
  1024 proof -
  1025   from AE_P obtain A where P: "{x\<in>space M. \<not> P x} \<subseteq> A"
  1026     and A: "A \<in> sets M" "emeasure M A = 0"
  1027     by (auto elim!: AE_E)
  1028 
  1029   from AE_imp obtain B where imp: "{x\<in>space M. P x \<and> \<not> Q x} \<subseteq> B"
  1030     and B: "B \<in> sets M" "emeasure M B = 0"
  1031     by (auto elim!: AE_E)
  1032 
  1033   show ?thesis
  1034   proof (intro AE_I)
  1035     have "emeasure M (A \<union> B) \<le> 0"
  1036       using emeasure_subadditive[of A M B] A B by auto
  1037     then show "A \<union> B \<in> sets M" "emeasure M (A \<union> B) = 0"
  1038       using A B by auto
  1039     show "{x\<in>space M. \<not> Q x} \<subseteq> A \<union> B"
  1040       using P imp by auto
  1041   qed
  1042 qed
  1043 
  1044 (* depricated replace by laws about eventually *)
  1045 lemma
  1046   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"
  1047     and AE_disjI1: "AE x in M. P x \<Longrightarrow> AE x in M. P x \<or> Q x"
  1048     and AE_disjI2: "AE x in M. Q x \<Longrightarrow> AE x in M. P x \<or> Q x"
  1049     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"
  1050     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)"
  1051   by auto
  1052 
  1053 lemma AE_impI:
  1054   "(P \<Longrightarrow> AE x in M. Q x) \<Longrightarrow> AE x in M. P \<longrightarrow> Q x"
  1055   by (cases P) auto
  1056 
  1057 lemma AE_measure:
  1058   assumes AE: "AE x in M. P x" and sets: "{x\<in>space M. P x} \<in> sets M" (is "?P \<in> sets M")
  1059   shows "emeasure M {x\<in>space M. P x} = emeasure M (space M)"
  1060 proof -
  1061   from AE_E[OF AE] guess N . note N = this
  1062   with sets have "emeasure M (space M) \<le> emeasure M (?P \<union> N)"
  1063     by (intro emeasure_mono) auto
  1064   also have "\<dots> \<le> emeasure M ?P + emeasure M N"
  1065     using sets N by (intro emeasure_subadditive) auto
  1066   also have "\<dots> = emeasure M ?P" using N by simp
  1067   finally show "emeasure M ?P = emeasure M (space M)"
  1068     using emeasure_space[of M "?P"] by auto
  1069 qed
  1070 
  1071 lemma AE_space: "AE x in M. x \<in> space M"
  1072   by (rule AE_I[where N="{}"]) auto
  1073 
  1074 lemma AE_I2[simp, intro]:
  1075   "(\<And>x. x \<in> space M \<Longrightarrow> P x) \<Longrightarrow> AE x in M. P x"
  1076   using AE_space by force
  1077 
  1078 lemma AE_Ball_mp:
  1079   "\<forall>x\<in>space M. P x \<Longrightarrow> AE x in M. P x \<longrightarrow> Q x \<Longrightarrow> AE x in M. Q x"
  1080   by auto
  1081 
  1082 lemma AE_cong[cong]:
  1083   "(\<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)"
  1084   by auto
  1085 
  1086 lemma AE_all_countable:
  1087   "(AE x in M. \<forall>i. P i x) \<longleftrightarrow> (\<forall>i::'i::countable. AE x in M. P i x)"
  1088 proof
  1089   assume "\<forall>i. AE x in M. P i x"
  1090   from this[unfolded eventually_ae_filter Bex_def, THEN choice]
  1091   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
  1092   have "{x\<in>space M. \<not> (\<forall>i. P i x)} \<subseteq> (\<Union>i. {x\<in>space M. \<not> P i x})" by auto
  1093   also have "\<dots> \<subseteq> (\<Union>i. N i)" using N by auto
  1094   finally have "{x\<in>space M. \<not> (\<forall>i. P i x)} \<subseteq> (\<Union>i. N i)" .
  1095   moreover from N have "(\<Union>i. N i) \<in> null_sets M"
  1096     by (intro null_sets_UN) auto
  1097   ultimately show "AE x in M. \<forall>i. P i x"
  1098     unfolding eventually_ae_filter by auto
  1099 qed auto
  1100 
  1101 lemma AE_ball_countable:
  1102   assumes [intro]: "countable X"
  1103   shows "(AE x in M. \<forall>y\<in>X. P x y) \<longleftrightarrow> (\<forall>y\<in>X. AE x in M. P x y)"
  1104 proof
  1105   assume "\<forall>y\<in>X. AE x in M. P x y"
  1106   from this[unfolded eventually_ae_filter Bex_def, THEN bchoice]
  1107   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"
  1108     by auto
  1109   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})"
  1110     by auto
  1111   also have "\<dots> \<subseteq> (\<Union>y\<in>X. N y)"
  1112     using N by auto
  1113   finally have "{x\<in>space M. \<not> (\<forall>y\<in>X. P x y)} \<subseteq> (\<Union>y\<in>X. N y)" .
  1114   moreover from N have "(\<Union>y\<in>X. N y) \<in> null_sets M"
  1115     by (intro null_sets_UN') auto
  1116   ultimately show "AE x in M. \<forall>y\<in>X. P x y"
  1117     unfolding eventually_ae_filter by auto
  1118 qed auto
  1119 
  1120 lemma AE_discrete_difference:
  1121   assumes X: "countable X"
  1122   assumes null: "\<And>x. x \<in> X \<Longrightarrow> emeasure M {x} = 0"
  1123   assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  1124   shows "AE x in M. x \<notin> X"
  1125 proof -
  1126   have "(\<Union>x\<in>X. {x}) \<in> null_sets M"
  1127     using assms by (intro null_sets_UN') auto
  1128   from AE_not_in[OF this] show "AE x in M. x \<notin> X"
  1129     by auto
  1130 qed
  1131 
  1132 lemma AE_finite_all:
  1133   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)"
  1134   using f by induct auto
  1135 
  1136 lemma AE_finite_allI:
  1137   assumes "finite S"
  1138   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"
  1139   using AE_finite_all[OF \<open>finite S\<close>] by auto
  1140 
  1141 lemma emeasure_mono_AE:
  1142   assumes imp: "AE x in M. x \<in> A \<longrightarrow> x \<in> B"
  1143     and B: "B \<in> sets M"
  1144   shows "emeasure M A \<le> emeasure M B"
  1145 proof cases
  1146   assume A: "A \<in> sets M"
  1147   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"
  1148     by (auto simp: eventually_ae_filter)
  1149   have "emeasure M A = emeasure M (A - N)"
  1150     using N A by (subst emeasure_Diff_null_set) auto
  1151   also have "emeasure M (A - N) \<le> emeasure M (B - N)"
  1152     using N A B sets.sets_into_space by (auto intro!: emeasure_mono)
  1153   also have "emeasure M (B - N) = emeasure M B"
  1154     using N B by (subst emeasure_Diff_null_set) auto
  1155   finally show ?thesis .
  1156 qed (simp add: emeasure_notin_sets)
  1157 
  1158 lemma emeasure_eq_AE:
  1159   assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  1160   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  1161   shows "emeasure M A = emeasure M B"
  1162   using assms by (safe intro!: antisym emeasure_mono_AE) auto
  1163 
  1164 lemma emeasure_Collect_eq_AE:
  1165   "AE x in M. P x \<longleftrightarrow> Q x \<Longrightarrow> Measurable.pred M Q \<Longrightarrow> Measurable.pred M P \<Longrightarrow>
  1166    emeasure M {x\<in>space M. P x} = emeasure M {x\<in>space M. Q x}"
  1167    by (intro emeasure_eq_AE) auto
  1168 
  1169 lemma emeasure_eq_0_AE: "AE x in M. \<not> P x \<Longrightarrow> emeasure M {x\<in>space M. P x} = 0"
  1170   using AE_iff_measurable[OF _ refl, of M "\<lambda>x. \<not> P x"]
  1171   by (cases "{x\<in>space M. P x} \<in> sets M") (simp_all add: emeasure_notin_sets)
  1172 
  1173 lemma emeasure_add_AE:
  1174   assumes [measurable]: "A \<in> sets M" "B \<in> sets M" "C \<in> sets M"
  1175   assumes 1: "AE x in M. x \<in> C \<longleftrightarrow> x \<in> A \<or> x \<in> B"
  1176   assumes 2: "AE x in M. \<not> (x \<in> A \<and> x \<in> B)"
  1177   shows "emeasure M C = emeasure M A + emeasure M B"
  1178 proof -
  1179   have "emeasure M C = emeasure M (A \<union> B)"
  1180     by (rule emeasure_eq_AE) (insert 1, auto)
  1181   also have "\<dots> = emeasure M A + emeasure M (B - A)"
  1182     by (subst plus_emeasure) auto
  1183   also have "emeasure M (B - A) = emeasure M B"
  1184     by (rule emeasure_eq_AE) (insert 2, auto)
  1185   finally show ?thesis .
  1186 qed
  1187 
  1188 subsection \<open>\<open>\<sigma>\<close>-finite Measures\<close>
  1189 
  1190 locale sigma_finite_measure =
  1191   fixes M :: "'a measure"
  1192   assumes sigma_finite_countable:
  1193     "\<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>)"
  1194 
  1195 lemma (in sigma_finite_measure) sigma_finite:
  1196   obtains A :: "nat \<Rightarrow> 'a set"
  1197   where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1198 proof -
  1199   obtain A :: "'a set set" where
  1200     [simp]: "countable A" and
  1201     A: "A \<subseteq> sets M" "(\<Union>A) = space M" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
  1202     using sigma_finite_countable by metis
  1203   show thesis
  1204   proof cases
  1205     assume "A = {}" with \<open>(\<Union>A) = space M\<close> show thesis
  1206       by (intro that[of "\<lambda>_. {}"]) auto
  1207   next
  1208     assume "A \<noteq> {}"
  1209     show thesis
  1210     proof
  1211       show "range (from_nat_into A) \<subseteq> sets M"
  1212         using \<open>A \<noteq> {}\<close> A by auto
  1213       have "(\<Union>i. from_nat_into A i) = \<Union>A"
  1214         using range_from_nat_into[OF \<open>A \<noteq> {}\<close> \<open>countable A\<close>] by auto
  1215       with A show "(\<Union>i. from_nat_into A i) = space M"
  1216         by auto
  1217     qed (intro A from_nat_into \<open>A \<noteq> {}\<close>)
  1218   qed
  1219 qed
  1220 
  1221 lemma (in sigma_finite_measure) sigma_finite_disjoint:
  1222   obtains A :: "nat \<Rightarrow> 'a set"
  1223   where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "disjoint_family A"
  1224 proof -
  1225   obtain A :: "nat \<Rightarrow> 'a set" where
  1226     range: "range A \<subseteq> sets M" and
  1227     space: "(\<Union>i. A i) = space M" and
  1228     measure: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1229     using sigma_finite by blast
  1230   show thesis
  1231   proof (rule that[of "disjointed A"])
  1232     show "range (disjointed A) \<subseteq> sets M"
  1233       by (rule sets.range_disjointed_sets[OF range])
  1234     show "(\<Union>i. disjointed A i) = space M"
  1235       and "disjoint_family (disjointed A)"
  1236       using disjoint_family_disjointed UN_disjointed_eq[of A] space range
  1237       by auto
  1238     show "emeasure M (disjointed A i) \<noteq> \<infinity>" for i
  1239     proof -
  1240       have "emeasure M (disjointed A i) \<le> emeasure M (A i)"
  1241         using range disjointed_subset[of A i] by (auto intro!: emeasure_mono)
  1242       then show ?thesis using measure[of i] by (auto simp: top_unique)
  1243     qed
  1244   qed
  1245 qed
  1246 
  1247 lemma (in sigma_finite_measure) sigma_finite_incseq:
  1248   obtains A :: "nat \<Rightarrow> 'a set"
  1249   where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "incseq A"
  1250 proof -
  1251   obtain F :: "nat \<Rightarrow> 'a set" where
  1252     F: "range F \<subseteq> sets M" "(\<Union>i. F i) = space M" "\<And>i. emeasure M (F i) \<noteq> \<infinity>"
  1253     using sigma_finite by blast
  1254   show thesis
  1255   proof (rule that[of "\<lambda>n. \<Union>i\<le>n. F i"])
  1256     show "range (\<lambda>n. \<Union>i\<le>n. F i) \<subseteq> sets M"
  1257       using F by (force simp: incseq_def)
  1258     show "(\<Union>n. \<Union>i\<le>n. F i) = space M"
  1259     proof -
  1260       from F have "\<And>x. x \<in> space M \<Longrightarrow> \<exists>i. x \<in> F i" by auto
  1261       with F show ?thesis by fastforce
  1262     qed
  1263     show "emeasure M (\<Union>i\<le>n. F i) \<noteq> \<infinity>" for n
  1264     proof -
  1265       have "emeasure M (\<Union>i\<le>n. F i) \<le> (\<Sum>i\<le>n. emeasure M (F i))"
  1266         using F by (auto intro!: emeasure_subadditive_finite)
  1267       also have "\<dots> < \<infinity>"
  1268         using F by (auto simp: setsum_Pinfty less_top)
  1269       finally show ?thesis by simp
  1270     qed
  1271     show "incseq (\<lambda>n. \<Union>i\<le>n. F i)"
  1272       by (force simp: incseq_def)
  1273   qed
  1274 qed
  1275 
  1276 subsection \<open>Measure space induced by distribution of @{const measurable}-functions\<close>
  1277 
  1278 definition distr :: "'a measure \<Rightarrow> 'b measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b measure" where
  1279   "distr M N f = measure_of (space N) (sets N) (\<lambda>A. emeasure M (f -` A \<inter> space M))"
  1280 
  1281 lemma
  1282   shows sets_distr[simp, measurable_cong]: "sets (distr M N f) = sets N"
  1283     and space_distr[simp]: "space (distr M N f) = space N"
  1284   by (auto simp: distr_def)
  1285 
  1286 lemma
  1287   shows measurable_distr_eq1[simp]: "measurable (distr Mf Nf f) Mf' = measurable Nf Mf'"
  1288     and measurable_distr_eq2[simp]: "measurable Mg' (distr Mg Ng g) = measurable Mg' Ng"
  1289   by (auto simp: measurable_def)
  1290 
  1291 lemma distr_cong:
  1292   "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"
  1293   using sets_eq_imp_space_eq[of N L] by (simp add: distr_def Int_def cong: rev_conj_cong)
  1294 
  1295 lemma emeasure_distr:
  1296   fixes f :: "'a \<Rightarrow> 'b"
  1297   assumes f: "f \<in> measurable M N" and A: "A \<in> sets N"
  1298   shows "emeasure (distr M N f) A = emeasure M (f -` A \<inter> space M)" (is "_ = ?\<mu> A")
  1299   unfolding distr_def
  1300 proof (rule emeasure_measure_of_sigma)
  1301   show "positive (sets N) ?\<mu>"
  1302     by (auto simp: positive_def)
  1303 
  1304   show "countably_additive (sets N) ?\<mu>"
  1305   proof (intro countably_additiveI)
  1306     fix A :: "nat \<Rightarrow> 'b set" assume "range A \<subseteq> sets N" "disjoint_family A"
  1307     then have A: "\<And>i. A i \<in> sets N" "(\<Union>i. A i) \<in> sets N" by auto
  1308     then have *: "range (\<lambda>i. f -` (A i) \<inter> space M) \<subseteq> sets M"
  1309       using f by (auto simp: measurable_def)
  1310     moreover have "(\<Union>i. f -`  A i \<inter> space M) \<in> sets M"
  1311       using * by blast
  1312     moreover have **: "disjoint_family (\<lambda>i. f -` A i \<inter> space M)"
  1313       using \<open>disjoint_family A\<close> by (auto simp: disjoint_family_on_def)
  1314     ultimately show "(\<Sum>i. ?\<mu> (A i)) = ?\<mu> (\<Union>i. A i)"
  1315       using suminf_emeasure[OF _ **] A f
  1316       by (auto simp: comp_def vimage_UN)
  1317   qed
  1318   show "sigma_algebra (space N) (sets N)" ..
  1319 qed fact
  1320 
  1321 lemma emeasure_Collect_distr:
  1322   assumes X[measurable]: "X \<in> measurable M N" "Measurable.pred N P"
  1323   shows "emeasure (distr M N X) {x\<in>space N. P x} = emeasure M {x\<in>space M. P (X x)}"
  1324   by (subst emeasure_distr)
  1325      (auto intro!: arg_cong2[where f=emeasure] X(1)[THEN measurable_space])
  1326 
  1327 lemma emeasure_lfp2[consumes 1, case_names cont f measurable]:
  1328   assumes "P M"
  1329   assumes cont: "sup_continuous F"
  1330   assumes f: "\<And>M. P M \<Longrightarrow> f \<in> measurable M' M"
  1331   assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
  1332   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)})"
  1333 proof (subst (1 2) emeasure_Collect_distr[symmetric, where X=f])
  1334   show "f \<in> measurable M' M"  "f \<in> measurable M' M"
  1335     using f[OF \<open>P M\<close>] by auto
  1336   { fix i show "Measurable.pred M ((F ^^ i) (\<lambda>x. False))"
  1337     using \<open>P M\<close> by (induction i arbitrary: M) (auto intro!: *) }
  1338   show "Measurable.pred M (lfp F)"
  1339     using \<open>P M\<close> cont * by (rule measurable_lfp_coinduct[of P])
  1340 
  1341   have "emeasure (distr M' M f) {x \<in> space (distr M' M f). lfp F x} =
  1342     (SUP i. emeasure (distr M' M f) {x \<in> space (distr M' M f). (F ^^ i) (\<lambda>x. False) x})"
  1343     using \<open>P M\<close>
  1344   proof (coinduction arbitrary: M rule: emeasure_lfp')
  1345     case (measurable A N) then have "\<And>N. P N \<Longrightarrow> Measurable.pred (distr M' N f) A"
  1346       by metis
  1347     then have "\<And>N. P N \<Longrightarrow> Measurable.pred N A"
  1348       by simp
  1349     with \<open>P N\<close>[THEN *] show ?case
  1350       by auto
  1351   qed fact
  1352   then show "emeasure (distr M' M f) {x \<in> space M. lfp F x} =
  1353     (SUP i. emeasure (distr M' M f) {x \<in> space M. (F ^^ i) (\<lambda>x. False) x})"
  1354    by simp
  1355 qed
  1356 
  1357 lemma distr_id[simp]: "distr N N (\<lambda>x. x) = N"
  1358   by (rule measure_eqI) (auto simp: emeasure_distr)
  1359 
  1360 lemma measure_distr:
  1361   "f \<in> measurable M N \<Longrightarrow> S \<in> sets N \<Longrightarrow> measure (distr M N f) S = measure M (f -` S \<inter> space M)"
  1362   by (simp add: emeasure_distr measure_def)
  1363 
  1364 lemma distr_cong_AE:
  1365   assumes 1: "M = K" "sets N = sets L" and
  1366     2: "(AE x in M. f x = g x)" and "f \<in> measurable M N" and "g \<in> measurable K L"
  1367   shows "distr M N f = distr K L g"
  1368 proof (rule measure_eqI)
  1369   fix A assume "A \<in> sets (distr M N f)"
  1370   with assms show "emeasure (distr M N f) A = emeasure (distr K L g) A"
  1371     by (auto simp add: emeasure_distr intro!: emeasure_eq_AE measurable_sets)
  1372 qed (insert 1, simp)
  1373 
  1374 lemma AE_distrD:
  1375   assumes f: "f \<in> measurable M M'"
  1376     and AE: "AE x in distr M M' f. P x"
  1377   shows "AE x in M. P (f x)"
  1378 proof -
  1379   from AE[THEN AE_E] guess N .
  1380   with f show ?thesis
  1381     unfolding eventually_ae_filter
  1382     by (intro bexI[of _ "f -` N \<inter> space M"])
  1383        (auto simp: emeasure_distr measurable_def)
  1384 qed
  1385 
  1386 lemma AE_distr_iff:
  1387   assumes f[measurable]: "f \<in> measurable M N" and P[measurable]: "{x \<in> space N. P x} \<in> sets N"
  1388   shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
  1389 proof (subst (1 2) AE_iff_measurable[OF _ refl])
  1390   have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
  1391     using f[THEN measurable_space] by auto
  1392   then show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
  1393     (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
  1394     by (simp add: emeasure_distr)
  1395 qed auto
  1396 
  1397 lemma null_sets_distr_iff:
  1398   "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"
  1399   by (auto simp add: null_sets_def emeasure_distr)
  1400 
  1401 lemma distr_distr:
  1402   "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)"
  1403   by (auto simp add: emeasure_distr measurable_space
  1404            intro!: arg_cong[where f="emeasure M"] measure_eqI)
  1405 
  1406 subsection \<open>Real measure values\<close>
  1407 
  1408 lemma ring_of_finite_sets: "ring_of_sets (space M) {A\<in>sets M. emeasure M A \<noteq> top}"
  1409 proof (rule ring_of_setsI)
  1410   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>
  1411     a \<union> b \<in> {A \<in> sets M. emeasure M A \<noteq> top}" for a b
  1412     using emeasure_subadditive[of a M b] by (auto simp: top_unique)
  1413 
  1414   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>
  1415     a - b \<in> {A \<in> sets M. emeasure M A \<noteq> top}" for a b
  1416     using emeasure_mono[of "a - b" a M] by (auto simp: Diff_subset top_unique)
  1417 qed (auto dest: sets.sets_into_space)
  1418 
  1419 lemma measure_nonneg[simp]: "0 \<le> measure M A"
  1420   unfolding measure_def by auto
  1421 
  1422 lemma zero_less_measure_iff: "0 < measure M A \<longleftrightarrow> measure M A \<noteq> 0"
  1423   using measure_nonneg[of M A] by (auto simp add: le_less)
  1424 
  1425 lemma measure_le_0_iff: "measure M X \<le> 0 \<longleftrightarrow> measure M X = 0"
  1426   using measure_nonneg[of M X] by linarith
  1427 
  1428 lemma measure_empty[simp]: "measure M {} = 0"
  1429   unfolding measure_def by (simp add: zero_ennreal.rep_eq)
  1430 
  1431 lemma emeasure_eq_ennreal_measure:
  1432   "emeasure M A \<noteq> top \<Longrightarrow> emeasure M A = ennreal (measure M A)"
  1433   by (cases "emeasure M A" rule: ennreal_cases) (auto simp: measure_def)
  1434 
  1435 lemma measure_zero_top: "emeasure M A = top \<Longrightarrow> measure M A = 0"
  1436   by (simp add: measure_def enn2ereal_top)
  1437 
  1438 lemma measure_eq_emeasure_eq_ennreal: "0 \<le> x \<Longrightarrow> emeasure M A = ennreal x \<Longrightarrow> measure M A = x"
  1439   using emeasure_eq_ennreal_measure[of M A]
  1440   by (cases "A \<in> M") (auto simp: measure_notin_sets emeasure_notin_sets)
  1441 
  1442 lemma enn2real_plus:"a < top \<Longrightarrow> b < top \<Longrightarrow> enn2real (a + b) = enn2real a + enn2real b"
  1443   by (simp add: enn2real_def plus_ennreal.rep_eq real_of_ereal_add less_top
  1444            del: real_of_ereal_enn2ereal)
  1445 
  1446 lemma measure_Union:
  1447   "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>
  1448     measure M (A \<union> B) = measure M A + measure M B"
  1449   by (simp add: measure_def plus_emeasure[symmetric] enn2real_plus less_top)
  1450 
  1451 lemma disjoint_family_on_insert:
  1452   "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"
  1453   by (fastforce simp: disjoint_family_on_def)
  1454 
  1455 lemma measure_finite_Union:
  1456   "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>
  1457     measure M (\<Union>i\<in>S. A i) = (\<Sum>i\<in>S. measure M (A i))"
  1458   by (induction S rule: finite_induct)
  1459      (auto simp: disjoint_family_on_insert measure_Union setsum_emeasure[symmetric] sets.countable_UN'[OF countable_finite])
  1460 
  1461 lemma measure_Diff:
  1462   assumes finite: "emeasure M A \<noteq> \<infinity>"
  1463   and measurable: "A \<in> sets M" "B \<in> sets M" "B \<subseteq> A"
  1464   shows "measure M (A - B) = measure M A - measure M B"
  1465 proof -
  1466   have "emeasure M (A - B) \<le> emeasure M A" "emeasure M B \<le> emeasure M A"
  1467     using measurable by (auto intro!: emeasure_mono)
  1468   hence "measure M ((A - B) \<union> B) = measure M (A - B) + measure M B"
  1469     using measurable finite by (rule_tac measure_Union) (auto simp: top_unique)
  1470   thus ?thesis using \<open>B \<subseteq> A\<close> by (auto simp: Un_absorb2)
  1471 qed
  1472 
  1473 lemma measure_UNION:
  1474   assumes measurable: "range A \<subseteq> sets M" "disjoint_family A"
  1475   assumes finite: "emeasure M (\<Union>i. A i) \<noteq> \<infinity>"
  1476   shows "(\<lambda>i. measure M (A i)) sums (measure M (\<Union>i. A i))"
  1477 proof -
  1478   have "(\<lambda>i. emeasure M (A i)) sums (emeasure M (\<Union>i. A i))"
  1479     unfolding suminf_emeasure[OF measurable, symmetric] by (simp add: summable_sums)
  1480   moreover
  1481   { fix i
  1482     have "emeasure M (A i) \<le> emeasure M (\<Union>i. A i)"
  1483       using measurable by (auto intro!: emeasure_mono)
  1484     then have "emeasure M (A i) = ennreal ((measure M (A i)))"
  1485       using finite by (intro emeasure_eq_ennreal_measure) (auto simp: top_unique) }
  1486   ultimately show ?thesis using finite
  1487     by (subst (asm) (2) emeasure_eq_ennreal_measure) simp_all
  1488 qed
  1489 
  1490 lemma measure_subadditive:
  1491   assumes measurable: "A \<in> sets M" "B \<in> sets M"
  1492   and fin: "emeasure M A \<noteq> \<infinity>" "emeasure M B \<noteq> \<infinity>"
  1493   shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  1494 proof -
  1495   have "emeasure M (A \<union> B) \<noteq> \<infinity>"
  1496     using emeasure_subadditive[OF measurable] fin by (auto simp: top_unique)
  1497   then show "(measure M (A \<union> B)) \<le> (measure M A) + (measure M B)"
  1498     using emeasure_subadditive[OF measurable] fin
  1499     apply simp
  1500     apply (subst (asm) (2 3 4) emeasure_eq_ennreal_measure)
  1501     apply (auto simp add: ennreal_plus[symmetric] simp del: ennreal_plus)
  1502     done
  1503 qed
  1504 
  1505 lemma measure_subadditive_finite:
  1506   assumes A: "finite I" "A`I \<subseteq> sets M" and fin: "\<And>i. i \<in> I \<Longrightarrow> emeasure M (A i) \<noteq> \<infinity>"
  1507   shows "measure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. measure M (A i))"
  1508 proof -
  1509   { have "emeasure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. emeasure M (A i))"
  1510       using emeasure_subadditive_finite[OF A] .
  1511     also have "\<dots> < \<infinity>"
  1512       using fin by (simp add: less_top A)
  1513     finally have "emeasure M (\<Union>i\<in>I. A i) \<noteq> top" by simp }
  1514   note * = this
  1515   show ?thesis
  1516     using emeasure_subadditive_finite[OF A] fin
  1517     unfolding emeasure_eq_ennreal_measure[OF *]
  1518     by (simp_all add: setsum_nonneg emeasure_eq_ennreal_measure)
  1519 qed
  1520 
  1521 lemma measure_subadditive_countably:
  1522   assumes A: "range A \<subseteq> sets M" and fin: "(\<Sum>i. emeasure M (A i)) \<noteq> \<infinity>"
  1523   shows "measure M (\<Union>i. A i) \<le> (\<Sum>i. measure M (A i))"
  1524 proof -
  1525   from fin have **: "\<And>i. emeasure M (A i) \<noteq> top"
  1526     using ennreal_suminf_lessD[of "\<lambda>i. emeasure M (A i)"] by (simp add: less_top)
  1527   { have "emeasure M (\<Union>i. A i) \<le> (\<Sum>i. emeasure M (A i))"
  1528       using emeasure_subadditive_countably[OF A] .
  1529     also have "\<dots> < \<infinity>"
  1530       using fin by (simp add: less_top)
  1531     finally have "emeasure M (\<Union>i. A i) \<noteq> top" by simp }
  1532   then have "ennreal (measure M (\<Union>i. A i)) = emeasure M (\<Union>i. A i)"
  1533     by (rule emeasure_eq_ennreal_measure[symmetric])
  1534   also have "\<dots> \<le> (\<Sum>i. emeasure M (A i))"
  1535     using emeasure_subadditive_countably[OF A] .
  1536   also have "\<dots> = ennreal (\<Sum>i. measure M (A i))"
  1537     using fin unfolding emeasure_eq_ennreal_measure[OF **]
  1538     by (subst suminf_ennreal) (auto simp: **)
  1539   finally show ?thesis
  1540     apply (rule ennreal_le_iff[THEN iffD1, rotated])
  1541     apply (intro suminf_nonneg allI measure_nonneg summable_suminf_not_top)
  1542     using fin
  1543     apply (simp add: emeasure_eq_ennreal_measure[OF **])
  1544     done
  1545 qed
  1546 
  1547 lemma measure_eq_setsum_singleton:
  1548   "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>
  1549     measure M S = (\<Sum>x\<in>S. measure M {x})"
  1550   using emeasure_eq_setsum_singleton[of S M]
  1551   by (intro measure_eq_emeasure_eq_ennreal) (auto simp: setsum_nonneg emeasure_eq_ennreal_measure)
  1552 
  1553 lemma Lim_measure_incseq:
  1554   assumes A: "range A \<subseteq> sets M" "incseq A" and fin: "emeasure M (\<Union>i. A i) \<noteq> \<infinity>"
  1555   shows "(\<lambda>i. measure M (A i)) \<longlonglongrightarrow> measure M (\<Union>i. A i)"
  1556 proof (rule tendsto_ennrealD)
  1557   have "ennreal (measure M (\<Union>i. A i)) = emeasure M (\<Union>i. A i)"
  1558     using fin by (auto simp: emeasure_eq_ennreal_measure)
  1559   moreover have "ennreal (measure M (A i)) = emeasure M (A i)" for i
  1560     using assms emeasure_mono[of "A _" "\<Union>i. A i" M]
  1561     by (intro emeasure_eq_ennreal_measure[symmetric]) (auto simp: less_top UN_upper intro: le_less_trans)
  1562   ultimately show "(\<lambda>x. ennreal (Sigma_Algebra.measure M (A x))) \<longlonglongrightarrow> ennreal (Sigma_Algebra.measure M (\<Union>i. A i))"
  1563     using A by (auto intro!: Lim_emeasure_incseq)
  1564 qed auto
  1565 
  1566 lemma Lim_measure_decseq:
  1567   assumes A: "range A \<subseteq> sets M" "decseq A" and fin: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1568   shows "(\<lambda>n. measure M (A n)) \<longlonglongrightarrow> measure M (\<Inter>i. A i)"
  1569 proof (rule tendsto_ennrealD)
  1570   have "ennreal (measure M (\<Inter>i. A i)) = emeasure M (\<Inter>i. A i)"
  1571     using fin[of 0] A emeasure_mono[of "\<Inter>i. A i" "A 0" M]
  1572     by (auto intro!: emeasure_eq_ennreal_measure[symmetric] simp: INT_lower less_top intro: le_less_trans)
  1573   moreover have "ennreal (measure M (A i)) = emeasure M (A i)" for i
  1574     using A fin[of i] by (intro emeasure_eq_ennreal_measure[symmetric]) auto
  1575   ultimately show "(\<lambda>x. ennreal (Sigma_Algebra.measure M (A x))) \<longlonglongrightarrow> ennreal (Sigma_Algebra.measure M (\<Inter>i. A i))"
  1576     using fin A by (auto intro!: Lim_emeasure_decseq)
  1577 qed auto
  1578 
  1579 subsection \<open>Measure spaces with @{term "emeasure M (space M) < \<infinity>"}\<close>
  1580 
  1581 locale finite_measure = sigma_finite_measure M for M +
  1582   assumes finite_emeasure_space: "emeasure M (space M) \<noteq> top"
  1583 
  1584 lemma finite_measureI[Pure.intro!]:
  1585   "emeasure M (space M) \<noteq> \<infinity> \<Longrightarrow> finite_measure M"
  1586   proof qed (auto intro!: exI[of _ "{space M}"])
  1587 
  1588 lemma (in finite_measure) emeasure_finite[simp, intro]: "emeasure M A \<noteq> top"
  1589   using finite_emeasure_space emeasure_space[of M A] by (auto simp: top_unique)
  1590 
  1591 lemma (in finite_measure) emeasure_eq_measure: "emeasure M A = ennreal (measure M A)"
  1592   by (intro emeasure_eq_ennreal_measure) simp
  1593 
  1594 lemma (in finite_measure) emeasure_real: "\<exists>r. 0 \<le> r \<and> emeasure M A = ennreal r"
  1595   using emeasure_finite[of A] by (cases "emeasure M A" rule: ennreal_cases) auto
  1596 
  1597 lemma (in finite_measure) bounded_measure: "measure M A \<le> measure M (space M)"
  1598   using emeasure_space[of M A] emeasure_real[of A] emeasure_real[of "space M"] by (auto simp: measure_def)
  1599 
  1600 lemma (in finite_measure) finite_measure_Diff:
  1601   assumes sets: "A \<in> sets M" "B \<in> sets M" and "B \<subseteq> A"
  1602   shows "measure M (A - B) = measure M A - measure M B"
  1603   using measure_Diff[OF _ assms] by simp
  1604 
  1605 lemma (in finite_measure) finite_measure_Union:
  1606   assumes sets: "A \<in> sets M" "B \<in> sets M" and "A \<inter> B = {}"
  1607   shows "measure M (A \<union> B) = measure M A + measure M B"
  1608   using measure_Union[OF _ _ assms] by simp
  1609 
  1610 lemma (in finite_measure) finite_measure_finite_Union:
  1611   assumes measurable: "finite S" "A`S \<subseteq> sets M" "disjoint_family_on A S"
  1612   shows "measure M (\<Union>i\<in>S. A i) = (\<Sum>i\<in>S. measure M (A i))"
  1613   using measure_finite_Union[OF assms] by simp
  1614 
  1615 lemma (in finite_measure) finite_measure_UNION:
  1616   assumes A: "range A \<subseteq> sets M" "disjoint_family A"
  1617   shows "(\<lambda>i. measure M (A i)) sums (measure M (\<Union>i. A i))"
  1618   using measure_UNION[OF A] by simp
  1619 
  1620 lemma (in finite_measure) finite_measure_mono:
  1621   assumes "A \<subseteq> B" "B \<in> sets M" shows "measure M A \<le> measure M B"
  1622   using emeasure_mono[OF assms] emeasure_real[of A] emeasure_real[of B] by (auto simp: measure_def)
  1623 
  1624 lemma (in finite_measure) finite_measure_subadditive:
  1625   assumes m: "A \<in> sets M" "B \<in> sets M"
  1626   shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  1627   using measure_subadditive[OF m] by simp
  1628 
  1629 lemma (in finite_measure) finite_measure_subadditive_finite:
  1630   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))"
  1631   using measure_subadditive_finite[OF assms] by simp
  1632 
  1633 lemma (in finite_measure) finite_measure_subadditive_countably:
  1634   "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))"
  1635   by (rule measure_subadditive_countably)
  1636      (simp_all add: ennreal_suminf_neq_top emeasure_eq_measure)
  1637 
  1638 lemma (in finite_measure) finite_measure_eq_setsum_singleton:
  1639   assumes "finite S" and *: "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
  1640   shows "measure M S = (\<Sum>x\<in>S. measure M {x})"
  1641   using measure_eq_setsum_singleton[OF assms] by simp
  1642 
  1643 lemma (in finite_measure) finite_Lim_measure_incseq:
  1644   assumes A: "range A \<subseteq> sets M" "incseq A"
  1645   shows "(\<lambda>i. measure M (A i)) \<longlonglongrightarrow> measure M (\<Union>i. A i)"
  1646   using Lim_measure_incseq[OF A] by simp
  1647 
  1648 lemma (in finite_measure) finite_Lim_measure_decseq:
  1649   assumes A: "range A \<subseteq> sets M" "decseq A"
  1650   shows "(\<lambda>n. measure M (A n)) \<longlonglongrightarrow> measure M (\<Inter>i. A i)"
  1651   using Lim_measure_decseq[OF A] by simp
  1652 
  1653 lemma (in finite_measure) finite_measure_compl:
  1654   assumes S: "S \<in> sets M"
  1655   shows "measure M (space M - S) = measure M (space M) - measure M S"
  1656   using measure_Diff[OF _ sets.top S sets.sets_into_space] S by simp
  1657 
  1658 lemma (in finite_measure) finite_measure_mono_AE:
  1659   assumes imp: "AE x in M. x \<in> A \<longrightarrow> x \<in> B" and B: "B \<in> sets M"
  1660   shows "measure M A \<le> measure M B"
  1661   using assms emeasure_mono_AE[OF imp B]
  1662   by (simp add: emeasure_eq_measure)
  1663 
  1664 lemma (in finite_measure) finite_measure_eq_AE:
  1665   assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  1666   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  1667   shows "measure M A = measure M B"
  1668   using assms emeasure_eq_AE[OF assms] by (simp add: emeasure_eq_measure)
  1669 
  1670 lemma (in finite_measure) measure_increasing: "increasing M (measure M)"
  1671   by (auto intro!: finite_measure_mono simp: increasing_def)
  1672 
  1673 lemma (in finite_measure) measure_zero_union:
  1674   assumes "s \<in> sets M" "t \<in> sets M" "measure M t = 0"
  1675   shows "measure M (s \<union> t) = measure M s"
  1676 using assms
  1677 proof -
  1678   have "measure M (s \<union> t) \<le> measure M s"
  1679     using finite_measure_subadditive[of s t] assms by auto
  1680   moreover have "measure M (s \<union> t) \<ge> measure M s"
  1681     using assms by (blast intro: finite_measure_mono)
  1682   ultimately show ?thesis by simp
  1683 qed
  1684 
  1685 lemma (in finite_measure) measure_eq_compl:
  1686   assumes "s \<in> sets M" "t \<in> sets M"
  1687   assumes "measure M (space M - s) = measure M (space M - t)"
  1688   shows "measure M s = measure M t"
  1689   using assms finite_measure_compl by auto
  1690 
  1691 lemma (in finite_measure) measure_eq_bigunion_image:
  1692   assumes "range f \<subseteq> sets M" "range g \<subseteq> sets M"
  1693   assumes "disjoint_family f" "disjoint_family g"
  1694   assumes "\<And> n :: nat. measure M (f n) = measure M (g n)"
  1695   shows "measure M (\<Union>i. f i) = measure M (\<Union>i. g i)"
  1696 using assms
  1697 proof -
  1698   have a: "(\<lambda> i. measure M (f i)) sums (measure M (\<Union>i. f i))"
  1699     by (rule finite_measure_UNION[OF assms(1,3)])
  1700   have b: "(\<lambda> i. measure M (g i)) sums (measure M (\<Union>i. g i))"
  1701     by (rule finite_measure_UNION[OF assms(2,4)])
  1702   show ?thesis using sums_unique[OF b] sums_unique[OF a] assms by simp
  1703 qed
  1704 
  1705 lemma (in finite_measure) measure_countably_zero:
  1706   assumes "range c \<subseteq> sets M"
  1707   assumes "\<And> i. measure M (c i) = 0"
  1708   shows "measure M (\<Union>i :: nat. c i) = 0"
  1709 proof (rule antisym)
  1710   show "measure M (\<Union>i :: nat. c i) \<le> 0"
  1711     using finite_measure_subadditive_countably[OF assms(1)] by (simp add: assms(2))
  1712 qed simp
  1713 
  1714 lemma (in finite_measure) measure_space_inter:
  1715   assumes events:"s \<in> sets M" "t \<in> sets M"
  1716   assumes "measure M t = measure M (space M)"
  1717   shows "measure M (s \<inter> t) = measure M s"
  1718 proof -
  1719   have "measure M ((space M - s) \<union> (space M - t)) = measure M (space M - s)"
  1720     using events assms finite_measure_compl[of "t"] by (auto intro!: measure_zero_union)
  1721   also have "(space M - s) \<union> (space M - t) = space M - (s \<inter> t)"
  1722     by blast
  1723   finally show "measure M (s \<inter> t) = measure M s"
  1724     using events by (auto intro!: measure_eq_compl[of "s \<inter> t" s])
  1725 qed
  1726 
  1727 lemma (in finite_measure) measure_equiprobable_finite_unions:
  1728   assumes s: "finite s" "\<And>x. x \<in> s \<Longrightarrow> {x} \<in> sets M"
  1729   assumes "\<And> x y. \<lbrakk>x \<in> s; y \<in> s\<rbrakk> \<Longrightarrow> measure M {x} = measure M {y}"
  1730   shows "measure M s = real (card s) * measure M {SOME x. x \<in> s}"
  1731 proof cases
  1732   assume "s \<noteq> {}"
  1733   then have "\<exists> x. x \<in> s" by blast
  1734   from someI_ex[OF this] assms
  1735   have prob_some: "\<And> x. x \<in> s \<Longrightarrow> measure M {x} = measure M {SOME y. y \<in> s}" by blast
  1736   have "measure M s = (\<Sum> x \<in> s. measure M {x})"
  1737     using finite_measure_eq_setsum_singleton[OF s] by simp
  1738   also have "\<dots> = (\<Sum> x \<in> s. measure M {SOME y. y \<in> s})" using prob_some by auto
  1739   also have "\<dots> = real (card s) * measure M {(SOME x. x \<in> s)}"
  1740     using setsum_constant assms by simp
  1741   finally show ?thesis by simp
  1742 qed simp
  1743 
  1744 lemma (in finite_measure) measure_real_sum_image_fn:
  1745   assumes "e \<in> sets M"
  1746   assumes "\<And> x. x \<in> s \<Longrightarrow> e \<inter> f x \<in> sets M"
  1747   assumes "finite s"
  1748   assumes disjoint: "\<And> x y. \<lbrakk>x \<in> s ; y \<in> s ; x \<noteq> y\<rbrakk> \<Longrightarrow> f x \<inter> f y = {}"
  1749   assumes upper: "space M \<subseteq> (\<Union>i \<in> s. f i)"
  1750   shows "measure M e = (\<Sum> x \<in> s. measure M (e \<inter> f x))"
  1751 proof -
  1752   have "e \<subseteq> (\<Union>i\<in>s. f i)"
  1753     using \<open>e \<in> sets M\<close> sets.sets_into_space upper by blast
  1754   then have e: "e = (\<Union>i \<in> s. e \<inter> f i)"
  1755     by auto
  1756   hence "measure M e = measure M (\<Union>i \<in> s. e \<inter> f i)" by simp
  1757   also have "\<dots> = (\<Sum> x \<in> s. measure M (e \<inter> f x))"
  1758   proof (rule finite_measure_finite_Union)
  1759     show "finite s" by fact
  1760     show "(\<lambda>i. e \<inter> f i)`s \<subseteq> sets M" using assms(2) by auto
  1761     show "disjoint_family_on (\<lambda>i. e \<inter> f i) s"
  1762       using disjoint by (auto simp: disjoint_family_on_def)
  1763   qed
  1764   finally show ?thesis .
  1765 qed
  1766 
  1767 lemma (in finite_measure) measure_exclude:
  1768   assumes "A \<in> sets M" "B \<in> sets M"
  1769   assumes "measure M A = measure M (space M)" "A \<inter> B = {}"
  1770   shows "measure M B = 0"
  1771   using measure_space_inter[of B A] assms by (auto simp: ac_simps)
  1772 lemma (in finite_measure) finite_measure_distr:
  1773   assumes f: "f \<in> measurable M M'"
  1774   shows "finite_measure (distr M M' f)"
  1775 proof (rule finite_measureI)
  1776   have "f -` space M' \<inter> space M = space M" using f by (auto dest: measurable_space)
  1777   with f show "emeasure (distr M M' f) (space (distr M M' f)) \<noteq> \<infinity>" by (auto simp: emeasure_distr)
  1778 qed
  1779 
  1780 lemma emeasure_gfp[consumes 1, case_names cont measurable]:
  1781   assumes sets[simp]: "\<And>s. sets (M s) = sets N"
  1782   assumes "\<And>s. finite_measure (M s)"
  1783   assumes cont: "inf_continuous F" "inf_continuous f"
  1784   assumes meas: "\<And>P. Measurable.pred N P \<Longrightarrow> Measurable.pred N (F P)"
  1785   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"
  1786   assumes bound: "\<And>P. f P \<le> f (\<lambda>s. emeasure (M s) (space (M s)))"
  1787   shows "emeasure (M s) {x\<in>space N. gfp F x} = gfp f s"
  1788 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
  1789     P="Measurable.pred N", symmetric])
  1790   interpret finite_measure "M s" for s by fact
  1791   fix C assume "decseq C" "\<And>i. Measurable.pred N (C i)"
  1792   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}))"
  1793     unfolding INF_apply[abs_def]
  1794     by (subst INF_emeasure_decseq) (auto simp: antimono_def fun_eq_iff intro!: arg_cong2[where f=emeasure])
  1795 next
  1796   show "f x \<le> (\<lambda>s. emeasure (M s) {x \<in> space N. F top x})" for x
  1797     using bound[of x] sets_eq_imp_space_eq[OF sets] by (simp add: iter)
  1798 qed (auto simp add: iter le_fun_def INF_apply[abs_def] intro!: meas cont)
  1799 
  1800 subsection \<open>Counting space\<close>
  1801 
  1802 lemma strict_monoI_Suc:
  1803   assumes ord [simp]: "(\<And>n. f n < f (Suc n))" shows "strict_mono f"
  1804   unfolding strict_mono_def
  1805 proof safe
  1806   fix n m :: nat assume "n < m" then show "f n < f m"
  1807     by (induct m) (auto simp: less_Suc_eq intro: less_trans ord)
  1808 qed
  1809 
  1810 lemma emeasure_count_space:
  1811   assumes "X \<subseteq> A" shows "emeasure (count_space A) X = (if finite X then of_nat (card X) else \<infinity>)"
  1812     (is "_ = ?M X")
  1813   unfolding count_space_def
  1814 proof (rule emeasure_measure_of_sigma)
  1815   show "X \<in> Pow A" using \<open>X \<subseteq> A\<close> by auto
  1816   show "sigma_algebra A (Pow A)" by (rule sigma_algebra_Pow)
  1817   show positive: "positive (Pow A) ?M"
  1818     by (auto simp: positive_def)
  1819   have additive: "additive (Pow A) ?M"
  1820     by (auto simp: card_Un_disjoint additive_def)
  1821 
  1822   interpret ring_of_sets A "Pow A"
  1823     by (rule ring_of_setsI) auto
  1824   show "countably_additive (Pow A) ?M"
  1825     unfolding countably_additive_iff_continuous_from_below[OF positive additive]
  1826   proof safe
  1827     fix F :: "nat \<Rightarrow> 'a set" assume "incseq F"
  1828     show "(\<lambda>i. ?M (F i)) \<longlonglongrightarrow> ?M (\<Union>i. F i)"
  1829     proof cases
  1830       assume "\<exists>i. \<forall>j\<ge>i. F i = F j"
  1831       then guess i .. note i = this
  1832       { fix j from i \<open>incseq F\<close> have "F j \<subseteq> F i"
  1833           by (cases "i \<le> j") (auto simp: incseq_def) }
  1834       then have eq: "(\<Union>i. F i) = F i"
  1835         by auto
  1836       with i show ?thesis
  1837         by (auto intro!: Lim_transform_eventually[OF _ tendsto_const] eventually_sequentiallyI[where c=i])
  1838     next
  1839       assume "\<not> (\<exists>i. \<forall>j\<ge>i. F i = F j)"
  1840       then obtain f where f: "\<And>i. i \<le> f i" "\<And>i. F i \<noteq> F (f i)" by metis
  1841       then have "\<And>i. F i \<subseteq> F (f i)" using \<open>incseq F\<close> by (auto simp: incseq_def)
  1842       with f have *: "\<And>i. F i \<subset> F (f i)" by auto
  1843 
  1844       have "incseq (\<lambda>i. ?M (F i))"
  1845         using \<open>incseq F\<close> unfolding incseq_def by (auto simp: card_mono dest: finite_subset)
  1846       then have "(\<lambda>i. ?M (F i)) \<longlonglongrightarrow> (SUP n. ?M (F n))"
  1847         by (rule LIMSEQ_SUP)
  1848 
  1849       moreover have "(SUP n. ?M (F n)) = top"
  1850       proof (rule ennreal_SUP_eq_top)
  1851         fix n :: nat show "\<exists>k::nat\<in>UNIV. of_nat n \<le> ?M (F k)"
  1852         proof (induct n)
  1853           case (Suc n)
  1854           then guess k .. note k = this
  1855           moreover have "finite (F k) \<Longrightarrow> finite (F (f k)) \<Longrightarrow> card (F k) < card (F (f k))"
  1856             using \<open>F k \<subset> F (f k)\<close> by (simp add: psubset_card_mono)
  1857           moreover have "finite (F (f k)) \<Longrightarrow> finite (F k)"
  1858             using \<open>k \<le> f k\<close> \<open>incseq F\<close> by (auto simp: incseq_def dest: finite_subset)
  1859           ultimately show ?case
  1860             by (auto intro!: exI[of _ "f k"] simp del: of_nat_Suc)
  1861         qed auto
  1862       qed
  1863 
  1864       moreover
  1865       have "inj (\<lambda>n. F ((f ^^ n) 0))"
  1866         by (intro strict_mono_imp_inj_on strict_monoI_Suc) (simp add: *)
  1867       then have 1: "infinite (range (\<lambda>i. F ((f ^^ i) 0)))"
  1868         by (rule range_inj_infinite)
  1869       have "infinite (Pow (\<Union>i. F i))"
  1870         by (rule infinite_super[OF _ 1]) auto
  1871       then have "infinite (\<Union>i. F i)"
  1872         by auto
  1873 
  1874       ultimately show ?thesis by auto
  1875     qed
  1876   qed
  1877 qed
  1878 
  1879 lemma distr_bij_count_space:
  1880   assumes f: "bij_betw f A B"
  1881   shows "distr (count_space A) (count_space B) f = count_space B"
  1882 proof (rule measure_eqI)
  1883   have f': "f \<in> measurable (count_space A) (count_space B)"
  1884     using f unfolding Pi_def bij_betw_def by auto
  1885   fix X assume "X \<in> sets (distr (count_space A) (count_space B) f)"
  1886   then have X: "X \<in> sets (count_space B)" by auto
  1887   moreover from X have "f -` X \<inter> A = the_inv_into A f ` X"
  1888     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])
  1889   moreover have "inj_on (the_inv_into A f) B"
  1890     using X f by (auto simp: bij_betw_def inj_on_the_inv_into)
  1891   with X have "inj_on (the_inv_into A f) X"
  1892     by (auto intro: subset_inj_on)
  1893   ultimately show "emeasure (distr (count_space A) (count_space B) f) X = emeasure (count_space B) X"
  1894     using f unfolding emeasure_distr[OF f' X]
  1895     by (subst (1 2) emeasure_count_space) (auto simp: card_image dest: finite_imageD)
  1896 qed simp
  1897 
  1898 lemma emeasure_count_space_finite[simp]:
  1899   "X \<subseteq> A \<Longrightarrow> finite X \<Longrightarrow> emeasure (count_space A) X = of_nat (card X)"
  1900   using emeasure_count_space[of X A] by simp
  1901 
  1902 lemma emeasure_count_space_infinite[simp]:
  1903   "X \<subseteq> A \<Longrightarrow> infinite X \<Longrightarrow> emeasure (count_space A) X = \<infinity>"
  1904   using emeasure_count_space[of X A] by simp
  1905 
  1906 lemma measure_count_space: "measure (count_space A) X = (if X \<subseteq> A then of_nat (card X) else 0)"
  1907   by (cases "finite X") (auto simp: measure_notin_sets ennreal_of_nat_eq_real_of_nat
  1908                                     measure_zero_top measure_eq_emeasure_eq_ennreal)
  1909 
  1910 lemma emeasure_count_space_eq_0:
  1911   "emeasure (count_space A) X = 0 \<longleftrightarrow> (X \<subseteq> A \<longrightarrow> X = {})"
  1912 proof cases
  1913   assume X: "X \<subseteq> A"
  1914   then show ?thesis
  1915   proof (intro iffI impI)
  1916     assume "emeasure (count_space A) X = 0"
  1917     with X show "X = {}"
  1918       by (subst (asm) emeasure_count_space) (auto split: if_split_asm)
  1919   qed simp
  1920 qed (simp add: emeasure_notin_sets)
  1921 
  1922 lemma space_empty: "space M = {} \<Longrightarrow> M = count_space {}"
  1923   by (rule measure_eqI) (simp_all add: space_empty_iff)
  1924 
  1925 lemma null_sets_count_space: "null_sets (count_space A) = { {} }"
  1926   unfolding null_sets_def by (auto simp add: emeasure_count_space_eq_0)
  1927 
  1928 lemma AE_count_space: "(AE x in count_space A. P x) \<longleftrightarrow> (\<forall>x\<in>A. P x)"
  1929   unfolding eventually_ae_filter by (auto simp add: null_sets_count_space)
  1930 
  1931 lemma sigma_finite_measure_count_space_countable:
  1932   assumes A: "countable A"
  1933   shows "sigma_finite_measure (count_space A)"
  1934   proof qed (insert A, auto intro!: exI[of _ "(\<lambda>a. {a}) ` A"])
  1935 
  1936 lemma sigma_finite_measure_count_space:
  1937   fixes A :: "'a::countable set" shows "sigma_finite_measure (count_space A)"
  1938   by (rule sigma_finite_measure_count_space_countable) auto
  1939 
  1940 lemma finite_measure_count_space:
  1941   assumes [simp]: "finite A"
  1942   shows "finite_measure (count_space A)"
  1943   by rule simp
  1944 
  1945 lemma sigma_finite_measure_count_space_finite:
  1946   assumes A: "finite A" shows "sigma_finite_measure (count_space A)"
  1947 proof -
  1948   interpret finite_measure "count_space A" using A by (rule finite_measure_count_space)
  1949   show "sigma_finite_measure (count_space A)" ..
  1950 qed
  1951 
  1952 subsection \<open>Measure restricted to space\<close>
  1953 
  1954 lemma emeasure_restrict_space:
  1955   assumes "\<Omega> \<inter> space M \<in> sets M" "A \<subseteq> \<Omega>"
  1956   shows "emeasure (restrict_space M \<Omega>) A = emeasure M A"
  1957 proof (cases "A \<in> sets M")
  1958   case True
  1959   show ?thesis
  1960   proof (rule emeasure_measure_of[OF restrict_space_def])
  1961     show "op \<inter> \<Omega> ` sets M \<subseteq> Pow (\<Omega> \<inter> space M)" "A \<in> sets (restrict_space M \<Omega>)"
  1962       using \<open>A \<subseteq> \<Omega>\<close> \<open>A \<in> sets M\<close> sets.space_closed by (auto simp: sets_restrict_space)
  1963     show "positive (sets (restrict_space M \<Omega>)) (emeasure M)"
  1964       by (auto simp: positive_def)
  1965     show "countably_additive (sets (restrict_space M \<Omega>)) (emeasure M)"
  1966     proof (rule countably_additiveI)
  1967       fix A :: "nat \<Rightarrow> _" assume "range A \<subseteq> sets (restrict_space M \<Omega>)" "disjoint_family A"
  1968       with assms have "\<And>i. A i \<in> sets M" "\<And>i. A i \<subseteq> space M" "disjoint_family A"
  1969         by (fastforce simp: sets_restrict_space_iff[OF assms(1)] image_subset_iff
  1970                       dest: sets.sets_into_space)+
  1971       then show "(\<Sum>i. emeasure M (A i)) = emeasure M (\<Union>i. A i)"
  1972         by (subst suminf_emeasure) (auto simp: disjoint_family_subset)
  1973     qed
  1974   qed
  1975 next
  1976   case False
  1977   with assms have "A \<notin> sets (restrict_space M \<Omega>)"
  1978     by (simp add: sets_restrict_space_iff)
  1979   with False show ?thesis
  1980     by (simp add: emeasure_notin_sets)
  1981 qed
  1982 
  1983 lemma measure_restrict_space:
  1984   assumes "\<Omega> \<inter> space M \<in> sets M" "A \<subseteq> \<Omega>"
  1985   shows "measure (restrict_space M \<Omega>) A = measure M A"
  1986   using emeasure_restrict_space[OF assms] by (simp add: measure_def)
  1987 
  1988 lemma AE_restrict_space_iff:
  1989   assumes "\<Omega> \<inter> space M \<in> sets M"
  1990   shows "(AE x in restrict_space M \<Omega>. P x) \<longleftrightarrow> (AE x in M. x \<in> \<Omega> \<longrightarrow> P x)"
  1991 proof -
  1992   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)"
  1993     by auto
  1994   { fix X assume X: "X \<in> sets M" "emeasure M X = 0"
  1995     then have "emeasure M (\<Omega> \<inter> space M \<inter> X) \<le> emeasure M X"
  1996       by (intro emeasure_mono) auto
  1997     then have "emeasure M (\<Omega> \<inter> space M \<inter> X) = 0"
  1998       using X by (auto intro!: antisym) }
  1999   with assms show ?thesis
  2000     unfolding eventually_ae_filter
  2001     by (auto simp add: space_restrict_space null_sets_def sets_restrict_space_iff
  2002                        emeasure_restrict_space cong: conj_cong
  2003              intro!: ex_cong[where f="\<lambda>X. (\<Omega> \<inter> space M) \<inter> X"])
  2004 qed
  2005 
  2006 lemma restrict_restrict_space:
  2007   assumes "A \<inter> space M \<in> sets M" "B \<inter> space M \<in> sets M"
  2008   shows "restrict_space (restrict_space M A) B = restrict_space M (A \<inter> B)" (is "?l = ?r")
  2009 proof (rule measure_eqI[symmetric])
  2010   show "sets ?r = sets ?l"
  2011     unfolding sets_restrict_space image_comp by (intro image_cong) auto
  2012 next
  2013   fix X assume "X \<in> sets (restrict_space M (A \<inter> B))"
  2014   then obtain Y where "Y \<in> sets M" "X = Y \<inter> A \<inter> B"
  2015     by (auto simp: sets_restrict_space)
  2016   with assms sets.Int[OF assms] show "emeasure ?r X = emeasure ?l X"
  2017     by (subst (1 2) emeasure_restrict_space)
  2018        (auto simp: space_restrict_space sets_restrict_space_iff emeasure_restrict_space ac_simps)
  2019 qed
  2020 
  2021 lemma restrict_count_space: "restrict_space (count_space B) A = count_space (A \<inter> B)"
  2022 proof (rule measure_eqI)
  2023   show "sets (restrict_space (count_space B) A) = sets (count_space (A \<inter> B))"
  2024     by (subst sets_restrict_space) auto
  2025   moreover fix X assume "X \<in> sets (restrict_space (count_space B) A)"
  2026   ultimately have "X \<subseteq> A \<inter> B" by auto
  2027   then show "emeasure (restrict_space (count_space B) A) X = emeasure (count_space (A \<inter> B)) X"
  2028     by (cases "finite X") (auto simp add: emeasure_restrict_space)
  2029 qed
  2030 
  2031 lemma sigma_finite_measure_restrict_space:
  2032   assumes "sigma_finite_measure M"
  2033   and A: "A \<in> sets M"
  2034   shows "sigma_finite_measure (restrict_space M A)"
  2035 proof -
  2036   interpret sigma_finite_measure M by fact
  2037   from sigma_finite_countable obtain C
  2038     where C: "countable C" "C \<subseteq> sets M" "(\<Union>C) = space M" "\<forall>a\<in>C. emeasure M a \<noteq> \<infinity>"
  2039     by blast
  2040   let ?C = "op \<inter> A ` C"
  2041   from C have "countable ?C" "?C \<subseteq> sets (restrict_space M A)" "(\<Union>?C) = space (restrict_space M A)"
  2042     by(auto simp add: sets_restrict_space space_restrict_space)
  2043   moreover {
  2044     fix a
  2045     assume "a \<in> ?C"
  2046     then obtain a' where "a = A \<inter> a'" "a' \<in> C" ..
  2047     then have "emeasure (restrict_space M A) a \<le> emeasure M a'"
  2048       using A C by(auto simp add: emeasure_restrict_space intro: emeasure_mono)
  2049     also have "\<dots> < \<infinity>" using C(4)[rule_format, of a'] \<open>a' \<in> C\<close> by (simp add: less_top)
  2050     finally have "emeasure (restrict_space M A) a \<noteq> \<infinity>" by simp }
  2051   ultimately show ?thesis
  2052     by unfold_locales (rule exI conjI|assumption|blast)+
  2053 qed
  2054 
  2055 lemma finite_measure_restrict_space:
  2056   assumes "finite_measure M"
  2057   and A: "A \<in> sets M"
  2058   shows "finite_measure (restrict_space M A)"
  2059 proof -
  2060   interpret finite_measure M by fact
  2061   show ?thesis
  2062     by(rule finite_measureI)(simp add: emeasure_restrict_space A space_restrict_space)
  2063 qed
  2064 
  2065 lemma restrict_distr:
  2066   assumes [measurable]: "f \<in> measurable M N"
  2067   assumes [simp]: "\<Omega> \<inter> space N \<in> sets N" and restrict: "f \<in> space M \<rightarrow> \<Omega>"
  2068   shows "restrict_space (distr M N f) \<Omega> = distr M (restrict_space N \<Omega>) f"
  2069   (is "?l = ?r")
  2070 proof (rule measure_eqI)
  2071   fix A assume "A \<in> sets (restrict_space (distr M N f) \<Omega>)"
  2072   with restrict show "emeasure ?l A = emeasure ?r A"
  2073     by (subst emeasure_distr)
  2074        (auto simp: sets_restrict_space_iff emeasure_restrict_space emeasure_distr
  2075              intro!: measurable_restrict_space2)
  2076 qed (simp add: sets_restrict_space)
  2077 
  2078 lemma measure_eqI_restrict_generator:
  2079   assumes E: "Int_stable E" "E \<subseteq> Pow \<Omega>" "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
  2080   assumes sets_eq: "sets M = sets N" and \<Omega>: "\<Omega> \<in> sets M"
  2081   assumes "sets (restrict_space M \<Omega>) = sigma_sets \<Omega> E"
  2082   assumes "sets (restrict_space N \<Omega>) = sigma_sets \<Omega> E"
  2083   assumes ae: "AE x in M. x \<in> \<Omega>" "AE x in N. x \<in> \<Omega>"
  2084   assumes A: "countable A" "A \<noteq> {}" "A \<subseteq> E" "\<Union>A = \<Omega>" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
  2085   shows "M = N"
  2086 proof (rule measure_eqI)
  2087   fix X assume X: "X \<in> sets M"
  2088   then have "emeasure M X = emeasure (restrict_space M \<Omega>) (X \<inter> \<Omega>)"
  2089     using ae \<Omega> by (auto simp add: emeasure_restrict_space intro!: emeasure_eq_AE)
  2090   also have "restrict_space M \<Omega> = restrict_space N \<Omega>"
  2091   proof (rule measure_eqI_generator_eq)
  2092     fix X assume "X \<in> E"
  2093     then show "emeasure (restrict_space M \<Omega>) X = emeasure (restrict_space N \<Omega>) X"
  2094       using E \<Omega> by (subst (1 2) emeasure_restrict_space) (auto simp: sets_eq sets_eq[THEN sets_eq_imp_space_eq])
  2095   next
  2096     show "range (from_nat_into A) \<subseteq> E" "(\<Union>i. from_nat_into A i) = \<Omega>"
  2097       using A by (auto cong del: strong_SUP_cong)
  2098   next
  2099     fix i
  2100     have "emeasure (restrict_space M \<Omega>) (from_nat_into A i) = emeasure M (from_nat_into A i)"
  2101       using A \<Omega> by (subst emeasure_restrict_space)
  2102                    (auto simp: sets_eq sets_eq[THEN sets_eq_imp_space_eq] intro: from_nat_into)
  2103     with A show "emeasure (restrict_space M \<Omega>) (from_nat_into A i) \<noteq> \<infinity>"
  2104       by (auto intro: from_nat_into)
  2105   qed fact+
  2106   also have "emeasure (restrict_space N \<Omega>) (X \<inter> \<Omega>) = emeasure N X"
  2107     using X ae \<Omega> by (auto simp add: emeasure_restrict_space sets_eq intro!: emeasure_eq_AE)
  2108   finally show "emeasure M X = emeasure N X" .
  2109 qed fact
  2110 
  2111 subsection \<open>Null measure\<close>
  2112 
  2113 definition "null_measure M = sigma (space M) (sets M)"
  2114 
  2115 lemma space_null_measure[simp]: "space (null_measure M) = space M"
  2116   by (simp add: null_measure_def)
  2117 
  2118 lemma sets_null_measure[simp, measurable_cong]: "sets (null_measure M) = sets M"
  2119   by (simp add: null_measure_def)
  2120 
  2121 lemma emeasure_null_measure[simp]: "emeasure (null_measure M) X = 0"
  2122   by (cases "X \<in> sets M", rule emeasure_measure_of)
  2123      (auto simp: positive_def countably_additive_def emeasure_notin_sets null_measure_def
  2124            dest: sets.sets_into_space)
  2125 
  2126 lemma measure_null_measure[simp]: "measure (null_measure M) X = 0"
  2127   by (intro measure_eq_emeasure_eq_ennreal) auto
  2128 
  2129 lemma null_measure_idem [simp]: "null_measure (null_measure M) = null_measure M"
  2130   by(rule measure_eqI) simp_all
  2131 
  2132 subsection \<open>Scaling a measure\<close>
  2133 
  2134 definition scale_measure :: "ennreal \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  2135 where
  2136   "scale_measure r M = measure_of (space M) (sets M) (\<lambda>A. r * emeasure M A)"
  2137 
  2138 lemma space_scale_measure: "space (scale_measure r M) = space M"
  2139   by (simp add: scale_measure_def)
  2140 
  2141 lemma sets_scale_measure [simp, measurable_cong]: "sets (scale_measure r M) = sets M"
  2142   by (simp add: scale_measure_def)
  2143 
  2144 lemma emeasure_scale_measure [simp]:
  2145   "emeasure (scale_measure r M) A = r * emeasure M A"
  2146   (is "_ = ?\<mu> A")
  2147 proof(cases "A \<in> sets M")
  2148   case True
  2149   show ?thesis unfolding scale_measure_def
  2150   proof(rule emeasure_measure_of_sigma)
  2151     show "sigma_algebra (space M) (sets M)" ..
  2152     show "positive (sets M) ?\<mu>" by (simp add: positive_def)
  2153     show "countably_additive (sets M) ?\<mu>"
  2154     proof (rule countably_additiveI)
  2155       fix A :: "nat \<Rightarrow> _"  assume *: "range A \<subseteq> sets M" "disjoint_family A"
  2156       have "(\<Sum>i. ?\<mu> (A i)) = r * (\<Sum>i. emeasure M (A i))"
  2157         by simp
  2158       also have "\<dots> = ?\<mu> (\<Union>i. A i)" using * by(simp add: suminf_emeasure)
  2159       finally show "(\<Sum>i. ?\<mu> (A i)) = ?\<mu> (\<Union>i. A i)" .
  2160     qed
  2161   qed(fact True)
  2162 qed(simp add: emeasure_notin_sets)
  2163 
  2164 lemma scale_measure_1 [simp]: "scale_measure 1 M = M"
  2165   by(rule measure_eqI) simp_all
  2166 
  2167 lemma scale_measure_0[simp]: "scale_measure 0 M = null_measure M"
  2168   by(rule measure_eqI) simp_all
  2169 
  2170 lemma measure_scale_measure [simp]: "0 \<le> r \<Longrightarrow> measure (scale_measure r M) A = r * measure M A"
  2171   using emeasure_scale_measure[of r M A]
  2172     emeasure_eq_ennreal_measure[of M A]
  2173     measure_eq_emeasure_eq_ennreal[of _ "scale_measure r M" A]
  2174   by (cases "emeasure (scale_measure r M) A = top")
  2175      (auto simp del: emeasure_scale_measure
  2176            simp: ennreal_top_eq_mult_iff ennreal_mult_eq_top_iff measure_zero_top ennreal_mult[symmetric])
  2177 
  2178 lemma scale_scale_measure [simp]:
  2179   "scale_measure r (scale_measure r' M) = scale_measure (r * r') M"
  2180   by (rule measure_eqI) (simp_all add: max_def mult.assoc)
  2181 
  2182 lemma scale_null_measure [simp]: "scale_measure r (null_measure M) = null_measure M"
  2183   by (rule measure_eqI) simp_all
  2184 
  2185 
  2186 subsection \<open>Complete lattice structure on measures\<close>
  2187 
  2188 lemma (in finite_measure) finite_measure_Diff':
  2189   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> measure M (A - B) = measure M A - measure M (A \<inter> B)"
  2190   using finite_measure_Diff[of A "A \<inter> B"] by (auto simp: Diff_Int)
  2191 
  2192 lemma (in finite_measure) finite_measure_Union':
  2193   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> measure M (A \<union> B) = measure M A + measure M (B - A)"
  2194   using finite_measure_Union[of A "B - A"] by auto
  2195 
  2196 lemma finite_unsigned_Hahn_decomposition:
  2197   assumes "finite_measure M" "finite_measure N" and [simp]: "sets N = sets M"
  2198   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)"
  2199 proof -
  2200   interpret M: finite_measure M by fact
  2201   interpret N: finite_measure N by fact
  2202 
  2203   define d where "d X = measure M X - measure N X" for X
  2204 
  2205   have [intro]: "bdd_above (d`sets M)"
  2206     using sets.sets_into_space[of _ M]
  2207     by (intro bdd_aboveI[where M="measure M (space M)"])
  2208        (auto simp: d_def field_simps subset_eq intro!: add_increasing M.finite_measure_mono)
  2209 
  2210   define \<gamma> where "\<gamma> = (SUP X:sets M. d X)"
  2211   have le_\<gamma>[intro]: "X \<in> sets M \<Longrightarrow> d X \<le> \<gamma>" for X
  2212     by (auto simp: \<gamma>_def intro!: cSUP_upper)
  2213 
  2214   have "\<exists>f. \<forall>n. f n \<in> sets M \<and> d (f n) > \<gamma> - 1 / 2^n"
  2215   proof (intro choice_iff[THEN iffD1] allI)
  2216     fix n
  2217     have "\<exists>X\<in>sets M. \<gamma> - 1 / 2^n < d X"
  2218       unfolding \<gamma>_def by (intro less_cSUP_iff[THEN iffD1]) auto
  2219     then show "\<exists>y. y \<in> sets M \<and> \<gamma> - 1 / 2 ^ n < d y"
  2220       by auto
  2221   qed
  2222   then obtain E where [measurable]: "E n \<in> sets M" and E: "d (E n) > \<gamma> - 1 / 2^n" for n
  2223     by auto
  2224 
  2225   define F where "F m n = (if m \<le> n then \<Inter>i\<in>{m..n}. E i else space M)" for m n
  2226 
  2227   have [measurable]: "m \<le> n \<Longrightarrow> F m n \<in> sets M" for m n
  2228     by (auto simp: F_def)
  2229 
  2230   have 1: "\<gamma> - 2 / 2 ^ m + 1 / 2 ^ n \<le> d (F m n)" if "m \<le> n" for m n
  2231     using that
  2232   proof (induct rule: dec_induct)
  2233     case base with E[of m] show ?case
  2234       by (simp add: F_def field_simps)
  2235   next
  2236     case (step i)
  2237     have F_Suc: "F m (Suc i) = F m i \<inter> E (Suc i)"
  2238       using \<open>m \<le> i\<close> by (auto simp: F_def le_Suc_eq)
  2239 
  2240     have "\<gamma> + (\<gamma> - 2 / 2^m + 1 / 2 ^ Suc i) \<le> (\<gamma> - 1 / 2^Suc i) + (\<gamma> - 2 / 2^m + 1 / 2^i)"
  2241       by (simp add: field_simps)
  2242     also have "\<dots> \<le> d (E (Suc i)) + d (F m i)"
  2243       using E[of "Suc i"] by (intro add_mono step) auto
  2244     also have "\<dots> = d (E (Suc i)) + d (F m i - E (Suc i)) + d (F m (Suc i))"
  2245       using \<open>m \<le> i\<close> by (simp add: d_def field_simps F_Suc M.finite_measure_Diff' N.finite_measure_Diff')
  2246     also have "\<dots> = d (E (Suc i) \<union> F m i) + d (F m (Suc i))"
  2247       using \<open>m \<le> i\<close> by (simp add: d_def field_simps M.finite_measure_Union' N.finite_measure_Union')
  2248     also have "\<dots> \<le> \<gamma> + d (F m (Suc i))"
  2249       using \<open>m \<le> i\<close> by auto
  2250     finally show ?case
  2251       by auto
  2252   qed
  2253 
  2254   define F' where "F' m = (\<Inter>i\<in>{m..}. E i)" for m
  2255   have F'_eq: "F' m = (\<Inter>i. F m (i + m))" for m
  2256     by (fastforce simp: le_iff_add[of m] F'_def F_def)
  2257 
  2258   have [measurable]: "F' m \<in> sets M" for m
  2259     by (auto simp: F'_def)
  2260 
  2261   have \<gamma>_le: "\<gamma> - 0 \<le> d (\<Union>m. F' m)"
  2262   proof (rule LIMSEQ_le)
  2263     show "(\<lambda>n. \<gamma> - 2 / 2 ^ n) \<longlonglongrightarrow> \<gamma> - 0"
  2264       by (intro tendsto_intros LIMSEQ_divide_realpow_zero) auto
  2265     have "incseq F'"
  2266       by (auto simp: incseq_def F'_def)
  2267     then show "(\<lambda>m. d (F' m)) \<longlonglongrightarrow> d (\<Union>m. F' m)"
  2268       unfolding d_def
  2269       by (intro tendsto_diff M.finite_Lim_measure_incseq N.finite_Lim_measure_incseq) auto
  2270 
  2271     have "\<gamma> - 2 / 2 ^ m + 0 \<le> d (F' m)" for m
  2272     proof (rule LIMSEQ_le)
  2273       have *: "decseq (\<lambda>n. F m (n + m))"
  2274         by (auto simp: decseq_def F_def)
  2275       show "(\<lambda>n. d (F m n)) \<longlonglongrightarrow> d (F' m)"
  2276         unfolding d_def F'_eq
  2277         by (rule LIMSEQ_offset[where k=m])
  2278            (auto intro!: tendsto_diff M.finite_Lim_measure_decseq N.finite_Lim_measure_decseq *)
  2279       show "(\<lambda>n. \<gamma> - 2 / 2 ^ m + 1 / 2 ^ n) \<longlonglongrightarrow> \<gamma> - 2 / 2 ^ m + 0"
  2280         by (intro tendsto_add LIMSEQ_divide_realpow_zero tendsto_const) auto
  2281       show "\<exists>N. \<forall>n\<ge>N. \<gamma> - 2 / 2 ^ m + 1 / 2 ^ n \<le> d (F m n)"
  2282         using 1[of m] by (intro exI[of _ m]) auto
  2283     qed
  2284     then show "\<exists>N. \<forall>n\<ge>N. \<gamma> - 2 / 2 ^ n \<le> d (F' n)"
  2285       by auto
  2286   qed
  2287 
  2288   show ?thesis
  2289   proof (safe intro!: bexI[of _ "\<Union>m. F' m"])
  2290     fix X assume [measurable]: "X \<in> sets M" and X: "X \<subseteq> (\<Union>m. F' m)"
  2291     have "d (\<Union>m. F' m) - d X = d ((\<Union>m. F' m) - X)"
  2292       using X by (auto simp: d_def M.finite_measure_Diff N.finite_measure_Diff)
  2293     also have "\<dots> \<le> \<gamma>"
  2294       by auto
  2295     finally have "0 \<le> d X"
  2296       using \<gamma>_le by auto
  2297     then show "emeasure N X \<le> emeasure M X"
  2298       by (auto simp: d_def M.emeasure_eq_measure N.emeasure_eq_measure)
  2299   next
  2300     fix X assume [measurable]: "X \<in> sets M" and X: "X \<inter> (\<Union>m. F' m) = {}"
  2301     then have "d (\<Union>m. F' m) + d X = d (X \<union> (\<Union>m. F' m))"
  2302       by (auto simp: d_def M.finite_measure_Union N.finite_measure_Union)
  2303     also have "\<dots> \<le> \<gamma>"
  2304       by auto
  2305     finally have "d X \<le> 0"
  2306       using \<gamma>_le by auto
  2307     then show "emeasure M X \<le> emeasure N X"
  2308       by (auto simp: d_def M.emeasure_eq_measure N.emeasure_eq_measure)
  2309   qed auto
  2310 qed
  2311 
  2312 lemma unsigned_Hahn_decomposition:
  2313   assumes [simp]: "sets N = sets M" and [measurable]: "A \<in> sets M"
  2314     and [simp]: "emeasure M A \<noteq> top" "emeasure N A \<noteq> top"
  2315   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)"
  2316 proof -
  2317   have "\<exists>Y\<in>sets (restrict_space M A).
  2318     (\<forall>X\<in>sets (restrict_space M A). X \<subseteq> Y \<longrightarrow> (restrict_space N A) X \<le> (restrict_space M A) X) \<and>
  2319     (\<forall>X\<in>sets (restrict_space M A). X \<inter> Y = {} \<longrightarrow> (restrict_space M A) X \<le> (restrict_space N A) X)"
  2320   proof (rule finite_unsigned_Hahn_decomposition)
  2321     show "finite_measure (restrict_space M A)" "finite_measure (restrict_space N A)"
  2322       by (auto simp: space_restrict_space emeasure_restrict_space less_top intro!: finite_measureI)
  2323   qed (simp add: sets_restrict_space)
  2324   then guess Y ..
  2325   then show ?thesis
  2326     apply (intro bexI[of _ Y] conjI ballI conjI)
  2327     apply (simp_all add: sets_restrict_space emeasure_restrict_space)
  2328     apply safe
  2329     subgoal for X Z
  2330       by (erule ballE[of _ _ X]) (auto simp add: Int_absorb1)
  2331     subgoal for X Z
  2332       by (erule ballE[of _ _ X])  (auto simp add: Int_absorb1 ac_simps)
  2333     apply auto
  2334     done
  2335 qed
  2336 
  2337 text \<open>
  2338   Define a lexicographical order on @{type measure}, in the order space, sets and measure. The parts
  2339   of the lexicographical order are point-wise ordered.
  2340 \<close>
  2341 
  2342 instantiation measure :: (type) order_bot
  2343 begin
  2344 
  2345 inductive less_eq_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
  2346   "space M \<subset> space N \<Longrightarrow> less_eq_measure M N"
  2347 | "space M = space N \<Longrightarrow> sets M \<subset> sets N \<Longrightarrow> less_eq_measure M N"
  2348 | "space M = space N \<Longrightarrow> sets M = sets N \<Longrightarrow> emeasure M \<le> emeasure N \<Longrightarrow> less_eq_measure M N"
  2349 
  2350 lemma le_measure_iff:
  2351   "M \<le> N \<longleftrightarrow> (if space M = space N then
  2352     if sets M = sets N then emeasure M \<le> emeasure N else sets M \<subseteq> sets N else space M \<subseteq> space N)"
  2353   by (auto elim: less_eq_measure.cases intro: less_eq_measure.intros)
  2354 
  2355 definition less_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
  2356   "less_measure M N \<longleftrightarrow> (M \<le> N \<and> \<not> N \<le> M)"
  2357 
  2358 definition bot_measure :: "'a measure" where
  2359   "bot_measure = sigma {} {}"
  2360 
  2361 lemma
  2362   shows space_bot[simp]: "space bot = {}"
  2363     and sets_bot[simp]: "sets bot = {{}}"
  2364     and emeasure_bot[simp]: "emeasure bot X = 0"
  2365   by (auto simp: bot_measure_def sigma_sets_empty_eq emeasure_sigma)
  2366 
  2367 instance
  2368 proof standard
  2369   show "bot \<le> a" for a :: "'a measure"
  2370     by (simp add: le_measure_iff bot_measure_def sigma_sets_empty_eq emeasure_sigma le_fun_def)
  2371 qed (auto simp: le_measure_iff less_measure_def split: if_split_asm intro: measure_eqI)
  2372 
  2373 end
  2374 
  2375 lemma le_measure: "sets M = sets N \<Longrightarrow> M \<le> N \<longleftrightarrow> (\<forall>A\<in>sets M. emeasure M A \<le> emeasure N A)"
  2376   apply (auto simp: le_measure_iff le_fun_def dest: sets_eq_imp_space_eq)
  2377   subgoal for X
  2378     by (cases "X \<in> sets M") (auto simp: emeasure_notin_sets)
  2379   done
  2380 
  2381 definition sup_measure' :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  2382 where
  2383   "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))"
  2384 
  2385 lemma assumes [simp]: "sets B = sets A"
  2386   shows space_sup_measure'[simp]: "space (sup_measure' A B) = space A"
  2387     and sets_sup_measure'[simp]: "sets (sup_measure' A B) = sets A"
  2388   using sets_eq_imp_space_eq[OF assms] by (simp_all add: sup_measure'_def)
  2389 
  2390 lemma emeasure_sup_measure':
  2391   assumes sets_eq[simp]: "sets B = sets A" and [simp, intro]: "X \<in> sets A"
  2392   shows "emeasure (sup_measure' A B) X = (SUP Y:sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  2393     (is "_ = ?S X")
  2394 proof -
  2395   note sets_eq_imp_space_eq[OF sets_eq, simp]
  2396   show ?thesis
  2397     using sup_measure'_def
  2398   proof (rule emeasure_measure_of)
  2399     let ?d = "\<lambda>X Y. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y)"
  2400     show "countably_additive (sets (sup_measure' A B)) (\<lambda>X. SUP Y : sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  2401     proof (rule countably_additiveI, goal_cases)
  2402       case (1 X)
  2403       then have [measurable]: "\<And>i. X i \<in> sets A" and "disjoint_family X"
  2404         by auto
  2405       have "(\<Sum>i. ?S (X i)) = (SUP Y:sets A. \<Sum>i. ?d (X i) Y)"
  2406       proof (rule ennreal_suminf_SUP_eq_directed)
  2407         fix J :: "nat set" and a b assume "finite J" and [measurable]: "a \<in> sets A" "b \<in> sets A"
  2408         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
  2409         proof cases
  2410           assume "emeasure A (X i) = top \<or> emeasure B (X i) = top"
  2411           then show ?thesis
  2412           proof
  2413             assume "emeasure A (X i) = top" then show ?thesis
  2414               by (intro bexI[of _ "X i"]) auto
  2415           next
  2416             assume "emeasure B (X i) = top" then show ?thesis
  2417               by (intro bexI[of _ "{}"]) auto
  2418           qed
  2419         next
  2420           assume finite: "\<not> (emeasure A (X i) = top \<or> emeasure B (X i) = top)"
  2421           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)"
  2422             using unsigned_Hahn_decomposition[of B A "X i"] by simp
  2423           then obtain Y where [measurable]: "Y \<in> sets A" and [simp]: "Y \<subseteq> X i"
  2424             and B_le_A: "\<And>C. C \<in> sets A \<Longrightarrow> C \<subseteq> Y \<Longrightarrow> B C \<le> A C"
  2425             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"
  2426             by auto
  2427 
  2428           show ?thesis
  2429           proof (intro bexI[of _ Y] ballI conjI)
  2430             fix a assume [measurable]: "a \<in> sets A"
  2431             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"
  2432               for a Y by auto
  2433             then have "?d (X i) a =
  2434               (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))"
  2435               by (subst (1 2) plus_emeasure) (auto simp: Diff_eq[symmetric])
  2436             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))"
  2437               by (intro add_mono order_refl B_le_A A_le_B) (auto simp: Diff_eq[symmetric])
  2438             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))"
  2439               by (simp add: ac_simps)
  2440             also have "\<dots> \<le> A (X i \<inter> Y) + B (X i \<inter> - Y)"
  2441               by (subst (1 2) plus_emeasure) (auto simp: Diff_eq[symmetric] *)
  2442             finally show "?d (X i) a \<le> ?d (X i) Y" .
  2443           qed auto
  2444         qed
  2445         then obtain C where [measurable]: "C i \<in> sets A" and "C i \<subseteq> X i"
  2446           and C: "\<And>a. a \<in> sets A \<Longrightarrow> ?d (X i) a \<le> ?d (X i) (C i)" for i
  2447           by metis
  2448         have *: "X i \<inter> (\<Union>i. C i) = X i \<inter> C i" for i
  2449         proof safe
  2450           fix x j assume "x \<in> X i" "x \<in> C j"
  2451           moreover have "i = j \<or> X i \<inter> X j = {}"
  2452             using \<open>disjoint_family X\<close> by (auto simp: disjoint_family_on_def)
  2453           ultimately show "x \<in> C i"
  2454             using \<open>C i \<subseteq> X i\<close> \<open>C j \<subseteq> X j\<close> by auto
  2455         qed auto
  2456         have **: "X i \<inter> - (\<Union>i. C i) = X i \<inter> - C i" for i
  2457         proof safe
  2458           fix x j assume "x \<in> X i" "x \<notin> C i" "x \<in> C j"
  2459           moreover have "i = j \<or> X i \<inter> X j = {}"
  2460             using \<open>disjoint_family X\<close> by (auto simp: disjoint_family_on_def)
  2461           ultimately show False
  2462             using \<open>C i \<subseteq> X i\<close> \<open>C j \<subseteq> X j\<close> by auto
  2463         qed auto
  2464         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"
  2465           apply (intro bexI[of _ "\<Union>i. C i"])
  2466           unfolding * **
  2467           apply (intro C ballI conjI)
  2468           apply auto
  2469           done
  2470       qed
  2471       also have "\<dots> = ?S (\<Union>i. X i)"
  2472         unfolding UN_extend_simps(4)
  2473         by (auto simp add: suminf_add[symmetric] Diff_eq[symmetric] simp del: UN_simps
  2474                  intro!: SUP_cong arg_cong2[where f="op +"] suminf_emeasure
  2475                          disjoint_family_on_bisimulation[OF \<open>disjoint_family X\<close>])
  2476       finally show "(\<Sum>i. ?S (X i)) = ?S (\<Union>i. X i)" .
  2477     qed
  2478   qed (auto dest: sets.sets_into_space simp: positive_def intro!: SUP_const)
  2479 qed
  2480 
  2481 lemma le_emeasure_sup_measure'1:
  2482   assumes "sets B = sets A" "X \<in> sets A" shows "emeasure A X \<le> emeasure (sup_measure' A B) X"
  2483   by (subst emeasure_sup_measure'[OF assms]) (auto intro!: SUP_upper2[of "X"] assms)
  2484 
  2485 lemma le_emeasure_sup_measure'2:
  2486   assumes "sets B = sets A" "X \<in> sets A" shows "emeasure B X \<le> emeasure (sup_measure' A B) X"
  2487   by (subst emeasure_sup_measure'[OF assms]) (auto intro!: SUP_upper2[of "{}"] assms)
  2488 
  2489 lemma emeasure_sup_measure'_le2:
  2490   assumes [simp]: "sets B = sets C" "sets A = sets C" and [measurable]: "X \<in> sets C"
  2491   assumes A: "\<And>Y. Y \<subseteq> X \<Longrightarrow> Y \<in> sets A \<Longrightarrow> emeasure A Y \<le> emeasure C Y"
  2492   assumes B: "\<And>Y. Y \<subseteq> X \<Longrightarrow> Y \<in> sets A \<Longrightarrow> emeasure B Y \<le> emeasure C Y"
  2493   shows "emeasure (sup_measure' A B) X \<le> emeasure C X"
  2494 proof (subst emeasure_sup_measure')
  2495   show "(SUP Y:sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y)) \<le> emeasure C X"
  2496     unfolding \<open>sets A = sets C\<close>
  2497   proof (intro SUP_least)
  2498     fix Y assume [measurable]: "Y \<in> sets C"
  2499     have [simp]: "X \<inter> Y \<union> (X - Y) = X"
  2500       by auto
  2501     have "emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y) \<le> emeasure C (X \<inter> Y) + emeasure C (X \<inter> - Y)"
  2502       by (intro add_mono A B) (auto simp: Diff_eq[symmetric])
  2503     also have "\<dots> = emeasure C X"
  2504       by (subst plus_emeasure) (auto simp: Diff_eq[symmetric])
  2505     finally show "emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y) \<le> emeasure C X" .
  2506   qed
  2507 qed simp_all
  2508 
  2509 definition sup_lexord :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b::order) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a"
  2510 where
  2511   "sup_lexord A B k s c =
  2512     (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)"
  2513 
  2514 lemma sup_lexord:
  2515   "(k A < k B \<Longrightarrow> P B) \<Longrightarrow> (k B < k A \<Longrightarrow> P A) \<Longrightarrow> (k A = k B \<Longrightarrow> P c) \<Longrightarrow>
  2516     (\<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)"
  2517   by (auto simp: sup_lexord_def)
  2518 
  2519 lemmas le_sup_lexord = sup_lexord[where P="\<lambda>a. c \<le> a" for c]
  2520 
  2521 lemma sup_lexord1: "k A = k B \<Longrightarrow> sup_lexord A B k s c = c"
  2522   by (simp add: sup_lexord_def)
  2523 
  2524 lemma sup_lexord_commute: "sup_lexord A B k s c = sup_lexord B A k s c"
  2525   by (auto simp: sup_lexord_def)
  2526 
  2527 lemma sigma_sets_le_sets_iff: "(sigma_sets (space x) \<A> \<subseteq> sets x) = (\<A> \<subseteq> sets x)"
  2528   using sets.sigma_sets_subset[of \<A> x] by auto
  2529 
  2530 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))"
  2531   by (cases "\<Omega> = space x")
  2532      (simp_all add: eq_commute[of _ "sets x"] le_measure_iff emeasure_sigma le_fun_def
  2533                     sigma_sets_superset_generator sigma_sets_le_sets_iff)
  2534 
  2535 instantiation measure :: (type) semilattice_sup
  2536 begin
  2537 
  2538 definition sup_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  2539 where
  2540   "sup_measure A B =
  2541     sup_lexord A B space (sigma (space A \<union> space B) {})
  2542       (sup_lexord A B sets (sigma (space A) (sets A \<union> sets B)) (sup_measure' A B))"
  2543 
  2544 instance
  2545 proof
  2546   fix x y z :: "'a measure"
  2547   show "x \<le> sup x y"
  2548     unfolding sup_measure_def
  2549   proof (intro le_sup_lexord)
  2550     assume "space x = space y"
  2551     then have *: "sets x \<union> sets y \<subseteq> Pow (space x)"
  2552       using sets.space_closed by auto
  2553     assume "\<not> sets y \<subseteq> sets x" "\<not> sets x \<subseteq> sets y"
  2554     then have "sets x \<subset> sets x \<union> sets y"
  2555       by auto
  2556     also have "\<dots> \<le> sigma (space x) (sets x \<union> sets y)"
  2557       by (subst sets_measure_of[OF *]) (rule sigma_sets_superset_generator)
  2558     finally show "x \<le> sigma (space x) (sets x \<union> sets y)"
  2559       by (simp add: space_measure_of[OF *] less_eq_measure.intros(2))
  2560   next
  2561     assume "\<not> space y \<subseteq> space x" "\<not> space x \<subseteq> space y"
  2562     then show "x \<le> sigma (space x \<union> space y) {}"
  2563       by (intro less_eq_measure.intros) auto
  2564   next
  2565     assume "sets x = sets y" then show "x \<le> sup_measure' x y"
  2566       by (simp add: le_measure le_emeasure_sup_measure'1)
  2567   qed (auto intro: less_eq_measure.intros)
  2568   show "y \<le> sup x y"
  2569     unfolding sup_measure_def
  2570   proof (intro le_sup_lexord)
  2571     assume **: "space x = space y"
  2572     then have *: "sets x \<union> sets y \<subseteq> Pow (space y)"
  2573       using sets.space_closed by auto
  2574     assume "\<not> sets y \<subseteq> sets x" "\<not> sets x \<subseteq> sets y"
  2575     then have "sets y \<subset> sets x \<union> sets y"
  2576       by auto
  2577     also have "\<dots> \<le> sigma (space y) (sets x \<union> sets y)"
  2578       by (subst sets_measure_of[OF *]) (rule sigma_sets_superset_generator)
  2579     finally show "y \<le> sigma (space x) (sets x \<union> sets y)"
  2580       by (simp add: ** space_measure_of[OF *] less_eq_measure.intros(2))
  2581   next
  2582     assume "\<not> space y \<subseteq> space x" "\<not> space x \<subseteq> space y"
  2583     then show "y \<le> sigma (space x \<union> space y) {}"
  2584       by (intro less_eq_measure.intros) auto
  2585   next
  2586     assume "sets x = sets y" then show "y \<le> sup_measure' x y"
  2587       by (simp add: le_measure le_emeasure_sup_measure'2)
  2588   qed (auto intro: less_eq_measure.intros)
  2589   show "x \<le> y \<Longrightarrow> z \<le> y \<Longrightarrow> sup x z \<le> y"
  2590     unfolding sup_measure_def
  2591   proof (intro sup_lexord[where P="\<lambda>x. x \<le> y"])
  2592     assume "x \<le> y" "z \<le> y" and [simp]: "space x = space z" "sets x = sets z"
  2593     from \<open>x \<le> y\<close> show "sup_measure' x z \<le> y"
  2594     proof cases
  2595       case 1 then show ?thesis
  2596         by (intro less_eq_measure.intros(1)) simp
  2597     next
  2598       case 2 then show ?thesis
  2599         by (intro less_eq_measure.intros(2)) simp_all
  2600     next
  2601       case 3 with \<open>z \<le> y\<close> \<open>x \<le> y\<close> show ?thesis
  2602         by (auto simp add: le_measure intro!: emeasure_sup_measure'_le2)
  2603     qed
  2604   next
  2605     assume **: "x \<le> y" "z \<le> y" "space x = space z" "\<not> sets z \<subseteq> sets x" "\<not> sets x \<subseteq> sets z"
  2606     then have *: "sets x \<union> sets z \<subseteq> Pow (space x)"
  2607       using sets.space_closed by auto
  2608     show "sigma (space x) (sets x \<union> sets z) \<le> y"
  2609       unfolding sigma_le_iff[OF *] using ** by (auto simp: le_measure_iff split: if_split_asm)
  2610   next
  2611     assume "x \<le> y" "z \<le> y" "\<not> space z \<subseteq> space x" "\<not> space x \<subseteq> space z"
  2612     then have "space x \<subseteq> space y" "space z \<subseteq> space y"
  2613       by (auto simp: le_measure_iff split: if_split_asm)
  2614     then show "sigma (space x \<union> space z) {} \<le> y"
  2615       by (simp add: sigma_le_iff)
  2616   qed
  2617 qed
  2618 
  2619 end
  2620 
  2621 lemma space_empty_eq_bot: "space a = {} \<longleftrightarrow> a = bot"
  2622   using space_empty[of a] by (auto intro!: measure_eqI)
  2623 
  2624 lemma sets_eq_iff_bounded: "A \<le> B \<Longrightarrow> B \<le> C \<Longrightarrow> sets A = sets C \<Longrightarrow> sets B = sets A"
  2625   by (auto dest: sets_eq_imp_space_eq simp add: le_measure_iff split: if_split_asm)
  2626 
  2627 lemma sets_sup: "sets A = sets M \<Longrightarrow> sets B = sets M \<Longrightarrow> sets (sup A B) = sets M"
  2628   by (auto simp add: sup_measure_def sup_lexord_def dest: sets_eq_imp_space_eq)
  2629 
  2630 lemma le_measureD1: "A \<le> B \<Longrightarrow> space A \<le> space B"
  2631   by (auto simp: le_measure_iff split: if_split_asm)
  2632 
  2633 lemma le_measureD2: "A \<le> B \<Longrightarrow> space A = space B \<Longrightarrow> sets A \<le> sets B"
  2634   by (auto simp: le_measure_iff split: if_split_asm)
  2635 
  2636 lemma le_measureD3: "A \<le> B \<Longrightarrow> sets A = sets B \<Longrightarrow> emeasure A X \<le> emeasure B X"
  2637   by (auto simp: le_measure_iff le_fun_def dest: sets_eq_imp_space_eq split: if_split_asm)
  2638 
  2639 lemma UN_space_closed: "UNION S sets \<subseteq> Pow (UNION S space)"
  2640   using sets.space_closed by auto
  2641 
  2642 definition Sup_lexord :: "('a \<Rightarrow> 'b::complete_lattice) \<Rightarrow> ('a set \<Rightarrow> 'a) \<Rightarrow> ('a set \<Rightarrow> 'a) \<Rightarrow> 'a set \<Rightarrow> 'a"
  2643 where
  2644   "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)"
  2645 
  2646 lemma Sup_lexord:
  2647   "(\<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>
  2648     P (Sup_lexord k c s A)"
  2649   by (auto simp: Sup_lexord_def Let_def)
  2650 
  2651 lemma Sup_lexord1:
  2652   assumes A: "A \<noteq> {}" "(\<And>a. a \<in> A \<Longrightarrow> k a = (\<Union>a\<in>A. k a))" "P (c A)"
  2653   shows "P (Sup_lexord k c s A)"
  2654   unfolding Sup_lexord_def Let_def
  2655 proof (clarsimp, safe)
  2656   show "\<forall>a\<in>A. k a \<noteq> (\<Union>x\<in>A. k x) \<Longrightarrow> P (s A)"
  2657     by (metis assms(1,2) ex_in_conv)
  2658 next
  2659   fix a assume "a \<in> A" "k a = (\<Union>x\<in>A. k x)"
  2660   then have "{a \<in> A. k a = (\<Union>x\<in>A. k x)} = {a \<in> A. k a = k a}"
  2661     by (metis A(2)[symmetric])
  2662   then show "P (c {a \<in> A. k a = (\<Union>x\<in>A. k x)})"
  2663     by (simp add: A(3))
  2664 qed
  2665 
  2666 instantiation measure :: (type) complete_lattice
  2667 begin
  2668 
  2669 interpretation sup_measure: comm_monoid_set sup "bot :: 'a measure"
  2670   by standard (auto intro!: antisym)
  2671 
  2672 lemma sup_measure_F_mono':
  2673   "finite J \<Longrightarrow> finite I \<Longrightarrow> sup_measure.F id I \<le> sup_measure.F id (I \<union> J)"
  2674 proof (induction J rule: finite_induct)
  2675   case empty then show ?case
  2676     by simp
  2677 next
  2678   case (insert i J)
  2679   show ?case
  2680   proof cases
  2681     assume "i \<in> I" with insert show ?thesis
  2682       by (auto simp: insert_absorb)
  2683   next
  2684     assume "i \<notin> I"
  2685     have "sup_measure.F id I \<le> sup_measure.F id (I \<union> J)"
  2686       by (intro insert)
  2687     also have "\<dots> \<le> sup_measure.F id (insert i (I \<union> J))"
  2688       using insert \<open>i \<notin> I\<close> by (subst sup_measure.insert) auto
  2689     finally show ?thesis
  2690       by auto
  2691   qed
  2692 qed
  2693 
  2694 lemma sup_measure_F_mono: "finite I \<Longrightarrow> J \<subseteq> I \<Longrightarrow> sup_measure.F id J \<le> sup_measure.F id I"
  2695   using sup_measure_F_mono'[of I J] by (auto simp: finite_subset Un_absorb1)
  2696 
  2697 lemma sets_sup_measure_F:
  2698   "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"
  2699   by (induction I rule: finite_ne_induct) (simp_all add: sets_sup)
  2700 
  2701 definition Sup_measure' :: "'a measure set \<Rightarrow> 'a measure"
  2702 where
  2703   "Sup_measure' M = measure_of (\<Union>a\<in>M. space a) (\<Union>a\<in>M. sets a)
  2704     (\<lambda>X. (SUP P:{P. finite P \<and> P \<subseteq> M }. sup_measure.F id P X))"
  2705 
  2706 lemma space_Sup_measure'2: "space (Sup_measure' M) = (\<Union>m\<in>M. space m)"
  2707   unfolding Sup_measure'_def by (intro space_measure_of[OF UN_space_closed])
  2708 
  2709 lemma sets_Sup_measure'2: "sets (Sup_measure' M) = sigma_sets (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m)"
  2710   unfolding Sup_measure'_def by (intro sets_measure_of[OF UN_space_closed])
  2711 
  2712 lemma sets_Sup_measure':
  2713   assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "M \<noteq> {}"
  2714   shows "sets (Sup_measure' M) = sets A"
  2715   using sets_eq[THEN sets_eq_imp_space_eq, simp] \<open>M \<noteq> {}\<close> by (simp add: Sup_measure'_def)
  2716 
  2717 lemma space_Sup_measure':
  2718   assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "M \<noteq> {}"
  2719   shows "space (Sup_measure' M) = space A"
  2720   using sets_eq[THEN sets_eq_imp_space_eq, simp] \<open>M \<noteq> {}\<close>
  2721   by (simp add: Sup_measure'_def )
  2722 
  2723 lemma emeasure_Sup_measure':
  2724   assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "X \<in> sets A" "M \<noteq> {}"
  2725   shows "emeasure (Sup_measure' M) X = (SUP P:{P. finite P \<and> P \<subseteq> M}. sup_measure.F id P X)"
  2726     (is "_ = ?S X")
  2727   using Sup_measure'_def
  2728 proof (rule emeasure_measure_of)
  2729   note sets_eq[THEN sets_eq_imp_space_eq, simp]
  2730   have *: "sets (Sup_measure' M) = sets A" "space (Sup_measure' M) = space A"
  2731     using \<open>M \<noteq> {}\<close> by (simp_all add: Sup_measure'_def)
  2732   let ?\<mu> = "sup_measure.F id"
  2733   show "countably_additive (sets (Sup_measure' M)) ?S"
  2734   proof (rule countably_additiveI, goal_cases)
  2735     case (1 F)
  2736     then have **: "range F \<subseteq> sets A"
  2737       by (auto simp: *)
  2738     show "(\<Sum>i. ?S (F i)) = ?S (\<Union>i. F i)"
  2739     proof (subst ennreal_suminf_SUP_eq_directed)
  2740       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}"
  2741       have "(i \<noteq> {} \<longrightarrow> sets (?\<mu> i) = sets A) \<and> (j \<noteq> {} \<longrightarrow> sets (?\<mu> j) = sets A) \<and>
  2742         (i \<noteq> {} \<or> j \<noteq> {} \<longrightarrow> sets (?\<mu> (i \<union> j)) = sets A)"
  2743         using ij by (intro impI sets_sup_measure_F conjI) auto
  2744       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
  2745         using ij
  2746         by (cases "i = {}"; cases "j = {}")
  2747            (auto intro!: le_measureD3 sup_measure_F_mono simp: sets_sup_measure_F
  2748                  simp del: id_apply)
  2749       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)"
  2750         by (safe intro!: bexI[of _ "i \<union> j"]) auto
  2751     next
  2752       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))"
  2753       proof (intro SUP_cong refl)
  2754         fix i assume i: "i \<in> {P. finite P \<and> P \<subseteq> M}"
  2755         show "(\<Sum>n. ?\<mu> i (F n)) = ?\<mu> i (UNION UNIV F)"
  2756         proof cases
  2757           assume "i \<noteq> {}" with i ** show ?thesis
  2758             apply (intro suminf_emeasure \<open>disjoint_family F\<close>)
  2759             apply (subst sets_sup_measure_F[OF _ _ sets_eq])
  2760             apply auto
  2761             done
  2762         qed simp
  2763       qed
  2764     qed
  2765   qed
  2766   show "positive (sets (Sup_measure' M)) ?S"
  2767     by (auto simp: positive_def bot_ennreal[symmetric])
  2768   show "X \<in> sets (Sup_measure' M)"
  2769     using assms * by auto
  2770 qed (rule UN_space_closed)
  2771 
  2772 definition Sup_measure :: "'a measure set \<Rightarrow> 'a measure"
  2773 where
  2774   "Sup_measure = Sup_lexord space (Sup_lexord sets Sup_measure'
  2775     (\<lambda>U. sigma (\<Union>u\<in>U. space u) (\<Union>u\<in>U. sets u))) (\<lambda>U. sigma (\<Union>u\<in>U. space u) {})"
  2776 
  2777 definition Inf_measure :: "'a measure set \<Rightarrow> 'a measure"
  2778 where
  2779   "Inf_measure A = Sup {x. \<forall>a\<in>A. x \<le> a}"
  2780 
  2781 definition inf_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  2782 where
  2783   "inf_measure a b = Inf {a, b}"
  2784 
  2785 definition top_measure :: "'a measure"
  2786 where
  2787   "top_measure = Inf {}"
  2788 
  2789 instance
  2790 proof
  2791   note UN_space_closed [simp]
  2792   show upper: "x \<le> Sup A" if x: "x \<in> A" for x :: "'a measure" and A
  2793     unfolding Sup_measure_def
  2794   proof (intro Sup_lexord[where P="\<lambda>y. x \<le> y"])
  2795     assume "\<And>a. a \<in> A \<Longrightarrow> space a \<noteq> (\<Union>a\<in>A. space a)"
  2796     from this[OF \<open>x \<in> A\<close>] \<open>x \<in> A\<close> show "x \<le> sigma (\<Union>a\<in>A. space a) {}"
  2797       by (intro less_eq_measure.intros) auto
  2798   next
  2799     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}"
  2800       and neq: "\<And>aa. aa \<in> S \<Longrightarrow> sets aa \<noteq> (\<Union>a\<in>S. sets a)"
  2801     have sp_a: "space a = (UNION S space)"
  2802       using \<open>a\<in>A\<close> by (auto simp: S)
  2803     show "x \<le> sigma (UNION S space) (UNION S sets)"
  2804     proof cases
  2805       assume [simp]: "space x = space a"
  2806       have "sets x \<subset> (\<Union>a\<in>S. sets a)"
  2807         using \<open>x\<in>A\<close> neq[of x] by (auto simp: S)
  2808       also have "\<dots> \<subseteq> sigma_sets (\<Union>x\<in>S. space x) (\<Union>x\<in>S. sets x)"
  2809         by (rule sigma_sets_superset_generator)
  2810       finally show ?thesis
  2811         by (intro less_eq_measure.intros(2)) (simp_all add: sp_a)
  2812     next
  2813       assume "space x \<noteq> space a"
  2814       moreover have "space x \<le> space a"
  2815         unfolding a using \<open>x\<in>A\<close> by auto
  2816       ultimately show ?thesis
  2817         by (intro less_eq_measure.intros) (simp add: less_le sp_a)
  2818     qed
  2819   next
  2820     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}"
  2821       and "b \<in> S" and b: "sets b = (\<Union>a\<in>S. sets a)" and S': "S' = {a' \<in> S. sets a' = sets b}"
  2822     then have "S' \<noteq> {}" "space b = space a"
  2823       by auto
  2824     have sets_eq: "\<And>x. x \<in> S' \<Longrightarrow> sets x = sets b"
  2825       by (auto simp: S')
  2826     note sets_eq[THEN sets_eq_imp_space_eq, simp]
  2827     have *: "sets (Sup_measure' S') = sets b" "space (Sup_measure' S') = space b"
  2828       using \<open>S' \<noteq> {}\<close> by (simp_all add: Sup_measure'_def sets_eq)
  2829     show "x \<le> Sup_measure' S'"
  2830     proof cases
  2831       assume "x \<in> S"
  2832       with \<open>b \<in> S\<close> have "space x = space b"
  2833         by (simp add: S)
  2834       show ?thesis
  2835       proof cases
  2836         assume "x \<in> S'"
  2837         show "x \<le> Sup_measure' S'"
  2838         proof (intro le_measure[THEN iffD2] ballI)
  2839           show "sets x = sets (Sup_measure' S')"
  2840             using \<open>x\<in>S'\<close> * by (simp add: S')
  2841           fix X assume "X \<in> sets x"
  2842           show "emeasure x X \<le> emeasure (Sup_measure' S') X"
  2843           proof (subst emeasure_Sup_measure'[OF _ \<open>X \<in> sets x\<close>])
  2844             show "emeasure x X \<le> (SUP P : {P. finite P \<and> P \<subseteq> S'}. emeasure (sup_measure.F id P) X)"
  2845               using \<open>x\<in>S'\<close> by (intro SUP_upper2[where i="{x}"]) auto
  2846           qed (insert \<open>x\<in>S'\<close> S', auto)
  2847         qed
  2848       next
  2849         assume "x \<notin> S'"
  2850         then have "sets x \<noteq> sets b"
  2851           using \<open>x\<in>S\<close> by (auto simp: S')
  2852         moreover have "sets x \<le> sets b"
  2853           using \<open>x\<in>S\<close> unfolding b by auto
  2854         ultimately show ?thesis
  2855           using * \<open>x \<in> S\<close>
  2856           by (intro less_eq_measure.intros(2))
  2857              (simp_all add: * \<open>space x = space b\<close> less_le)
  2858       qed
  2859     next
  2860       assume "x \<notin> S"
  2861       with \<open>x\<in>A\<close> \<open>x \<notin> S\<close> \<open>space b = space a\<close> show ?thesis
  2862         by (intro less_eq_measure.intros)
  2863            (simp_all add: * less_le a SUP_upper S)
  2864     qed
  2865   qed
  2866   show least: "Sup A \<le> x" if x: "\<And>z. z \<in> A \<Longrightarrow> z \<le> x" for x :: "'a measure" and A
  2867     unfolding Sup_measure_def
  2868   proof (intro Sup_lexord[where P="\<lambda>y. y \<le> x"])
  2869     assume "\<And>a. a \<in> A \<Longrightarrow> space a \<noteq> (\<Union>a\<in>A. space a)"
  2870     show "sigma (UNION A space) {} \<le> x"
  2871       using x[THEN le_measureD1] by (subst sigma_le_iff) auto
  2872   next
  2873     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}"
  2874       "\<And>a. a \<in> S \<Longrightarrow> sets a \<noteq> (\<Union>a\<in>S. sets a)"
  2875     have "UNION S space \<subseteq> space x"
  2876       using S le_measureD1[OF x] by auto
  2877     moreover
  2878     have "UNION S space = space a"
  2879       using \<open>a\<in>A\<close> S by auto
  2880     then have "space x = UNION S space \<Longrightarrow> UNION S sets \<subseteq> sets x"
  2881       using \<open>a \<in> A\<close> le_measureD2[OF x] by (auto simp: S)
  2882     ultimately show "sigma (UNION S space) (UNION S sets) \<le> x"
  2883       by (subst sigma_le_iff) simp_all
  2884   next
  2885     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}"
  2886       and "b \<in> S" and b: "sets b = (\<Union>a\<in>S. sets a)" and S': "S' = {a' \<in> S. sets a' = sets b}"
  2887     then have "S' \<noteq> {}" "space b = space a"
  2888       by auto
  2889     have sets_eq: "\<And>x. x \<in> S' \<Longrightarrow> sets x = sets b"
  2890       by (auto simp: S')
  2891     note sets_eq[THEN sets_eq_imp_space_eq, simp]
  2892     have *: "sets (Sup_measure' S') = sets b" "space (Sup_measure' S') = space b"
  2893       using \<open>S' \<noteq> {}\<close> by (simp_all add: Sup_measure'_def sets_eq)
  2894     show "Sup_measure' S' \<le> x"
  2895     proof cases
  2896       assume "space x = space a"
  2897       show ?thesis
  2898       proof cases
  2899         assume **: "sets x = sets b"
  2900         show ?thesis
  2901         proof (intro le_measure[THEN iffD2] ballI)
  2902           show ***: "sets (Sup_measure' S') = sets x"
  2903             by (simp add: * **)
  2904           fix X assume "X \<in> sets (Sup_measure' S')"
  2905           show "emeasure (Sup_measure' S') X \<le> emeasure x X"
  2906             unfolding ***
  2907           proof (subst emeasure_Sup_measure'[OF _ \<open>X \<in> sets (Sup_measure' S')\<close>])
  2908             show "(SUP P : {P. finite P \<and> P \<subseteq> S'}. emeasure (sup_measure.F id P) X) \<le> emeasure x X"
  2909             proof (safe intro!: SUP_least)
  2910               fix P assume P: "finite P" "P \<subseteq> S'"
  2911               show "emeasure (sup_measure.F id P) X \<le> emeasure x X"
  2912               proof cases
  2913                 assume "P = {}" then show ?thesis
  2914                   by auto
  2915               next
  2916                 assume "P \<noteq> {}"
  2917                 from P have "finite P" "P \<subseteq> A"
  2918                   unfolding S' S by (simp_all add: subset_eq)
  2919                 then have "sup_measure.F id P \<le> x"
  2920                   by (induction P) (auto simp: x)
  2921                 moreover have "sets (sup_measure.F id P) = sets x"
  2922                   using \<open>finite P\<close> \<open>P \<noteq> {}\<close> \<open>P \<subseteq> S'\<close> \<open>sets x = sets b\<close>
  2923                   by (intro sets_sup_measure_F) (auto simp: S')
  2924                 ultimately show "emeasure (sup_measure.F id P) X \<le> emeasure x X"
  2925                   by (rule le_measureD3)
  2926               qed
  2927             qed
  2928             show "m \<in> S' \<Longrightarrow> sets m = sets (Sup_measure' S')" for m
  2929               unfolding * by (simp add: S')
  2930           qed fact
  2931         qed
  2932       next
  2933         assume "sets x \<noteq> sets b"
  2934         moreover have "sets b \<le> sets x"
  2935           unfolding b S using x[THEN le_measureD2] \<open>space x = space a\<close> by auto
  2936         ultimately show "Sup_measure' S' \<le> x"
  2937           using \<open>space x = space a\<close> \<open>b \<in> S\<close>
  2938           by (intro less_eq_measure.intros(2)) (simp_all add: * S)
  2939       qed
  2940     next
  2941       assume "space x \<noteq> space a"
  2942       then have "space a < space x"
  2943         using le_measureD1[OF x[OF \<open>a\<in>A\<close>]] by auto
  2944       then show "Sup_measure' S' \<le> x"
  2945         by (intro less_eq_measure.intros) (simp add: * \<open>space b = space a\<close>)
  2946     qed
  2947   qed
  2948   show "Sup {} = (bot::'a measure)" "Inf {} = (top::'a measure)"
  2949     by (auto intro!: antisym least simp: top_measure_def)
  2950   show lower: "x \<in> A \<Longrightarrow> Inf A \<le> x" for x :: "'a measure" and A
  2951     unfolding Inf_measure_def by (intro least) auto
  2952   show greatest: "(\<And>z. z \<in> A \<Longrightarrow> x \<le> z) \<Longrightarrow> x \<le> Inf A" for x :: "'a measure" and A
  2953     unfolding Inf_measure_def by (intro upper) auto
  2954   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"
  2955     by (auto simp: inf_measure_def intro!: lower greatest)
  2956 qed
  2957 
  2958 end
  2959 
  2960 lemma sets_SUP:
  2961   assumes "\<And>x. x \<in> I \<Longrightarrow> sets (M x) = sets N"
  2962   shows "I \<noteq> {} \<Longrightarrow> sets (SUP i:I. M i) = sets N"
  2963   unfolding Sup_measure_def
  2964   using assms assms[THEN sets_eq_imp_space_eq]
  2965     sets_Sup_measure'[where A=N and M="M`I"]
  2966   by (intro Sup_lexord1[where P="\<lambda>x. sets x = sets N"]) auto
  2967 
  2968 lemma emeasure_SUP:
  2969   assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets N" "X \<in> sets N" "I \<noteq> {}"
  2970   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)"
  2971 proof -
  2972   interpret sup_measure: comm_monoid_set sup "bot :: 'b measure"
  2973     by standard (auto intro!: antisym)
  2974   have eq: "finite J \<Longrightarrow> sup_measure.F id J = (SUP i:J. i)" for J :: "'b measure set"
  2975     by (induction J rule: finite_induct) auto
  2976   have 1: "J \<noteq> {} \<Longrightarrow> J \<subseteq> I \<Longrightarrow> sets (SUP x:J. M x) = sets N" for J
  2977     by (intro sets_SUP sets) (auto )
  2978   from \<open>I \<noteq> {}\<close> obtain i where "i\<in>I" by auto
  2979   have "Sup_measure' (M`I) X = (SUP P:{P. finite P \<and> P \<subseteq> M`I}. sup_measure.F id P X)"
  2980     using sets by (intro emeasure_Sup_measure') auto
  2981   also have "Sup_measure' (M`I) = (SUP i:I. M i)"
  2982     unfolding Sup_measure_def using \<open>I \<noteq> {}\<close> sets sets(1)[THEN sets_eq_imp_space_eq]
  2983     by (intro Sup_lexord1[where P="\<lambda>x. _ = x"]) auto
  2984   also have "(SUP P:{P. finite P \<and> P \<subseteq> M`I}. sup_measure.F id P X) =
  2985     (SUP J:{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. (SUP i:J. M i) X)"
  2986   proof (intro SUP_eq)
  2987     fix J assume "J \<in> {P. finite P \<and> P \<subseteq> M`I}"
  2988     then obtain J' where J': "J' \<subseteq> I" "finite J'" and J: "J = M`J'" and "finite J"
  2989       using finite_subset_image[of J M I] by auto
  2990     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"
  2991     proof cases
  2992       assume "J' = {}" with \<open>i \<in> I\<close> show ?thesis
  2993         by (auto simp add: J)
  2994     next
  2995       assume "J' \<noteq> {}" with J J' show ?thesis
  2996         by (intro bexI[of _ "J'"]) (auto simp add: eq simp del: id_apply)
  2997     qed
  2998   next
  2999     fix J assume J: "J \<in> {P. P \<noteq> {} \<and> finite P \<and> P \<subseteq> I}"
  3000     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"
  3001       using J by (intro bexI[of _ "M`J"]) (auto simp add: eq simp del: id_apply)
  3002   qed
  3003   finally show ?thesis .
  3004 qed
  3005 
  3006 lemma emeasure_SUP_chain:
  3007   assumes sets: "\<And>i. i \<in> A \<Longrightarrow> sets (M i) = sets N" "X \<in> sets N"
  3008   assumes ch: "Complete_Partial_Order.chain op \<le> (M ` A)" and "A \<noteq> {}"
  3009   shows "emeasure (SUP i:A. M i) X = (SUP i:A. emeasure (M i) X)"
  3010 proof (subst emeasure_SUP[OF sets \<open>A \<noteq> {}\<close>])
  3011   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)"
  3012   proof (rule SUP_eq)
  3013     fix J assume "J \<in> {J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> A}"
  3014     then have J: "Complete_Partial_Order.chain op \<le> (M ` J)" "finite J" "J \<noteq> {}" and "J \<subseteq> A"
  3015       using ch[THEN chain_subset, of "M`J"] by auto
  3016     with in_chain_finite[OF J(1)] obtain j where "j \<in> J" "(SUP j:J. M j) = M j"
  3017       by auto
  3018     with \<open>J \<subseteq> A\<close> show "\<exists>j\<in>A. emeasure (SUPREMUM J M) X \<le> emeasure (M j) X"
  3019       by auto
  3020   next
  3021     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"
  3022       by (intro bexI[of _ "{j}"]) auto
  3023   qed
  3024 qed
  3025 
  3026 subsubsection \<open>Supremum of a set of $\sigma$-algebras\<close>
  3027 
  3028 lemma space_Sup_eq_UN: "space (Sup M) = (\<Union>x\<in>M. space x)"
  3029   unfolding Sup_measure_def
  3030   apply (intro Sup_lexord[where P="\<lambda>x. space x = _"])
  3031   apply (subst space_Sup_measure'2)
  3032   apply auto []
  3033   apply (subst space_measure_of[OF UN_space_closed])
  3034   apply auto
  3035   done
  3036 
  3037 lemma sets_Sup_eq:
  3038   assumes *: "\<And>m. m \<in> M \<Longrightarrow> space m = X" and "M \<noteq> {}"
  3039   shows "sets (Sup M) = sigma_sets X (\<Union>x\<in>M. sets x)"
  3040   unfolding Sup_measure_def
  3041   apply (rule Sup_lexord1)
  3042   apply fact
  3043   apply (simp add: assms)
  3044   apply (rule Sup_lexord)
  3045   subgoal premises that for a S
  3046     unfolding that(3) that(2)[symmetric]
  3047     using that(1)
  3048     apply (subst sets_Sup_measure'2)
  3049     apply (intro arg_cong2[where f=sigma_sets])
  3050     apply (auto simp: *)
  3051     done
  3052   apply (subst sets_measure_of[OF UN_space_closed])
  3053   apply (simp add:  assms)
  3054   done
  3055 
  3056 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)"
  3057   by (subst sets_Sup_eq[where X=X]) auto
  3058 
  3059 lemma Sup_lexord_rel:
  3060   assumes "\<And>i. i \<in> I \<Longrightarrow> k (A i) = k (B i)"
  3061     "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))}))"
  3062     "R (s (A`I)) (s (B`I))"
  3063   shows "R (Sup_lexord k c s (A`I)) (Sup_lexord k c s (B`I))"
  3064 proof -
  3065   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))}"
  3066     using assms(1) by auto
  3067   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))}"
  3068     by auto
  3069   ultimately show ?thesis
  3070     using assms by (auto simp: Sup_lexord_def Let_def)
  3071 qed
  3072 
  3073 lemma sets_SUP_cong:
  3074   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)"
  3075   unfolding Sup_measure_def
  3076   using eq eq[THEN sets_eq_imp_space_eq]
  3077   apply (intro Sup_lexord_rel[where R="\<lambda>x y. sets x = sets y"])
  3078   apply simp
  3079   apply simp
  3080   apply (simp add: sets_Sup_measure'2)
  3081   apply (intro arg_cong2[where f="\<lambda>x y. sets (sigma x y)"])
  3082   apply auto
  3083   done
  3084 
  3085 lemma sets_Sup_in_sets:
  3086   assumes "M \<noteq> {}"
  3087   assumes "\<And>m. m \<in> M \<Longrightarrow> space m = space N"
  3088   assumes "\<And>m. m \<in> M \<Longrightarrow> sets m \<subseteq> sets N"
  3089   shows "sets (Sup M) \<subseteq> sets N"
  3090 proof -
  3091   have *: "UNION M space = space N"
  3092     using assms by auto
  3093   show ?thesis
  3094     unfolding * using assms by (subst sets_Sup_eq[of M "space N"]) (auto intro!: sets.sigma_sets_subset)
  3095 qed
  3096 
  3097 lemma measurable_Sup1:
  3098   assumes m: "m \<in> M" and f: "f \<in> measurable m N"
  3099     and const_space: "\<And>m n. m \<in> M \<Longrightarrow> n \<in> M \<Longrightarrow> space m = space n"
  3100   shows "f \<in> measurable (Sup M) N"
  3101 proof -
  3102   have "space (Sup M) = space m"
  3103     using m by (auto simp add: space_Sup_eq_UN dest: const_space)
  3104   then show ?thesis
  3105     using m f unfolding measurable_def by (auto intro: in_sets_Sup[OF const_space])
  3106 qed
  3107 
  3108 lemma measurable_Sup2:
  3109   assumes M: "M \<noteq> {}"
  3110   assumes f: "\<And>m. m \<in> M \<Longrightarrow> f \<in> measurable N m"
  3111     and const_space: "\<And>m n. m \<in> M \<Longrightarrow> n \<in> M \<Longrightarrow> space m = space n"
  3112   shows "f \<in> measurable N (Sup M)"
  3113 proof -
  3114   from M obtain m where "m \<in> M" by auto
  3115   have space_eq: "\<And>n. n \<in> M \<Longrightarrow> space n = space m"
  3116     by (intro const_space \<open>m \<in> M\<close>)
  3117   have "f \<in> measurable N (sigma (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m))"
  3118   proof (rule measurable_measure_of)
  3119     show "f \<in> space N \<rightarrow> UNION M space"
  3120       using measurable_space[OF f] M by auto
  3121   qed (auto intro: measurable_sets f dest: sets.sets_into_space)
  3122   also have "measurable N (sigma (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m)) = measurable N (Sup M)"
  3123     apply (intro measurable_cong_sets refl)
  3124     apply (subst sets_Sup_eq[OF space_eq M])
  3125     apply simp
  3126     apply (subst sets_measure_of[OF UN_space_closed])
  3127     apply (simp add: space_eq M)
  3128     done
  3129   finally show ?thesis .
  3130 qed
  3131 
  3132 lemma sets_Sup_sigma:
  3133   assumes [simp]: "M \<noteq> {}" and M: "\<And>m. m \<in> M \<Longrightarrow> m \<subseteq> Pow \<Omega>"
  3134   shows "sets (SUP m:M. sigma \<Omega> m) = sets (sigma \<Omega> (\<Union>M))"
  3135 proof -
  3136   { fix a m assume "a \<in> sigma_sets \<Omega> m" "m \<in> M"
  3137     then have "a \<in> sigma_sets \<Omega> (\<Union>M)"
  3138      by induction (auto intro: sigma_sets.intros) }
  3139   then show "sets (SUP m:M. sigma \<Omega> m) = sets (sigma \<Omega> (\<Union>M))"
  3140     apply (subst sets_Sup_eq[where X="\<Omega>"])
  3141     apply (auto simp add: M) []
  3142     apply auto []
  3143     apply (simp add: space_measure_of_conv M Union_least)
  3144     apply (rule sigma_sets_eqI)
  3145     apply auto
  3146     done
  3147 qed
  3148 
  3149 lemma Sup_sigma:
  3150   assumes [simp]: "M \<noteq> {}" and M: "\<And>m. m \<in> M \<Longrightarrow> m \<subseteq> Pow \<Omega>"
  3151   shows "(SUP m:M. sigma \<Omega> m) = (sigma \<Omega> (\<Union>M))"
  3152 proof (intro antisym SUP_least)
  3153   have *: "\<Union>M \<subseteq> Pow \<Omega>"
  3154     using M by auto
  3155   show "sigma \<Omega> (\<Union>M) \<le> (SUP m:M. sigma \<Omega> m)"
  3156   proof (intro less_eq_measure.intros(3))
  3157     show "space (sigma \<Omega> (\<Union>M)) = space (SUP m:M. sigma \<Omega> m)"
  3158       "sets (sigma \<Omega> (\<Union>M)) = sets (SUP m:M. sigma \<Omega> m)"
  3159       using sets_Sup_sigma[OF assms] sets_Sup_sigma[OF assms, THEN sets_eq_imp_space_eq]
  3160       by auto
  3161   qed (simp add: emeasure_sigma le_fun_def)
  3162   fix m assume "m \<in> M" then show "sigma \<Omega> m \<le> sigma \<Omega> (\<Union>M)"
  3163     by (subst sigma_le_iff) (auto simp add: M *)
  3164 qed
  3165 
  3166 lemma SUP_sigma_sigma:
  3167   "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)"
  3168   using Sup_sigma[of "f`M" \<Omega>] by auto
  3169 
  3170 lemma sets_vimage_Sup_eq:
  3171   assumes *: "M \<noteq> {}" "f \<in> X \<rightarrow> Y" "\<And>m. m \<in> M \<Longrightarrow> space m = Y"
  3172   shows "sets (vimage_algebra X f (Sup M)) = sets (SUP m : M. vimage_algebra X f m)"
  3173   (is "?IS = ?SI")
  3174 proof
  3175   show "?IS \<subseteq> ?SI"
  3176     apply (intro sets_image_in_sets measurable_Sup2)
  3177     apply (simp add: space_Sup_eq_UN *)
  3178     apply (simp add: *)
  3179     apply (intro measurable_Sup1)
  3180     apply (rule imageI)
  3181     apply assumption
  3182     apply (rule measurable_vimage_algebra1)
  3183     apply (auto simp: *)
  3184     done
  3185   show "?SI \<subseteq> ?IS"
  3186     apply (intro sets_Sup_in_sets)
  3187     apply (auto simp: *) []
  3188     apply (auto simp: *) []
  3189     apply (elim imageE)
  3190     apply simp
  3191     apply (rule sets_image_in_sets)
  3192     apply simp
  3193     apply (simp add: measurable_def)
  3194     apply (simp add: * space_Sup_eq_UN sets_vimage_algebra2)
  3195     apply (auto intro: in_sets_Sup[OF *(3)])
  3196     done
  3197 qed
  3198 
  3199 lemma restrict_space_eq_vimage_algebra':
  3200   "sets (restrict_space M \<Omega>) = sets (vimage_algebra (\<Omega> \<inter> space M) (\<lambda>x. x) M)"
  3201 proof -
  3202   have *: "{A \<inter> (\<Omega> \<inter> space M) |A. A \<in> sets M} = {A \<inter> \<Omega> |A. A \<in> sets M}"
  3203     using sets.sets_into_space[of _ M] by blast
  3204 
  3205   show ?thesis
  3206     unfolding restrict_space_def
  3207     by (subst sets_measure_of)
  3208        (auto simp add: image_subset_iff sets_vimage_algebra * dest: sets.sets_into_space intro!: arg_cong2[where f=sigma_sets])
  3209 qed
  3210 
  3211 lemma sigma_le_sets:
  3212   assumes [simp]: "A \<subseteq> Pow X" shows "sets (sigma X A) \<subseteq> sets N \<longleftrightarrow> X \<in> sets N \<and> A \<subseteq> sets N"
  3213 proof
  3214   have "X \<in> sigma_sets X A" "A \<subseteq> sigma_sets X A"
  3215     by (auto intro: sigma_sets_top)
  3216   moreover assume "sets (sigma X A) \<subseteq> sets N"
  3217   ultimately show "X \<in> sets N \<and> A \<subseteq> sets N"
  3218     by auto
  3219 next
  3220   assume *: "X \<in> sets N \<and> A \<subseteq> sets N"
  3221   { fix Y assume "Y \<in> sigma_sets X A" from this * have "Y \<in> sets N"
  3222       by induction auto }
  3223   then show "sets (sigma X A) \<subseteq> sets N"
  3224     by auto
  3225 qed
  3226 
  3227 lemma measurable_iff_sets:
  3228   "f \<in> measurable M N \<longleftrightarrow> (f \<in> space M \<rightarrow> space N \<and> sets (vimage_algebra (space M) f N) \<subseteq> sets M)"
  3229 proof -
  3230   have *: "{f -` A \<inter> space M |A. A \<in> sets N} \<subseteq> Pow (space M)"
  3231     by auto
  3232   show ?thesis
  3233     unfolding measurable_def
  3234     by (auto simp add: vimage_algebra_def sigma_le_sets[OF *])
  3235 qed
  3236 
  3237 lemma sets_vimage_algebra_space: "X \<in> sets (vimage_algebra X f M)"
  3238   using sets.top[of "vimage_algebra X f M"] by simp
  3239 
  3240 lemma measurable_mono:
  3241   assumes N: "sets N' \<le> sets N" "space N = space N'"
  3242   assumes M: "sets M \<le> sets M'" "space M = space M'"
  3243   shows "measurable M N \<subseteq> measurable M' N'"
  3244   unfolding measurable_def
  3245 proof safe
  3246   fix f A assume "f \<in> space M \<rightarrow> space N" "A \<in> sets N'"
  3247   moreover assume "\<forall>y\<in>sets N. f -` y \<inter> space M \<in> sets M" note this[THEN bspec, of A]
  3248   ultimately show "f -` A \<inter> space M' \<in> sets M'"
  3249     using assms by auto
  3250 qed (insert N M, auto)
  3251 
  3252 lemma measurable_Sup_measurable:
  3253   assumes f: "f \<in> space N \<rightarrow> A"
  3254   shows "f \<in> measurable N (Sup {M. space M = A \<and> f \<in> measurable N M})"
  3255 proof (rule measurable_Sup2)
  3256   show "{M. space M = A \<and> f \<in> measurable N M} \<noteq> {}"
  3257     using f unfolding ex_in_conv[symmetric]
  3258     by (intro exI[of _ "sigma A {}"]) (auto intro!: measurable_measure_of)
  3259 qed auto
  3260 
  3261 lemma (in sigma_algebra) sigma_sets_subset':
  3262   assumes a: "a \<subseteq> M" "\<Omega>' \<in> M"
  3263   shows "sigma_sets \<Omega>' a \<subseteq> M"
  3264 proof
  3265   show "x \<in> M" if x: "x \<in> sigma_sets \<Omega>' a" for x
  3266     using x by (induct rule: sigma_sets.induct) (insert a, auto)
  3267 qed
  3268 
  3269 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)"
  3270   by (intro in_sets_Sup[where X=Y]) auto
  3271 
  3272 lemma measurable_SUP1:
  3273   "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>
  3274     f \<in> measurable (SUP i:I. M i) N"
  3275   by (auto intro: measurable_Sup1)
  3276 
  3277 lemma sets_image_in_sets':
  3278   assumes X: "X \<in> sets N"
  3279   assumes f: "\<And>A. A \<in> sets M \<Longrightarrow> f -` A \<inter> X \<in> sets N"
  3280   shows "sets (vimage_algebra X f M) \<subseteq> sets N"
  3281   unfolding sets_vimage_algebra
  3282   by (rule sets.sigma_sets_subset') (auto intro!: measurable_sets X f)
  3283 
  3284 lemma mono_vimage_algebra:
  3285   "sets M \<le> sets N \<Longrightarrow> sets (vimage_algebra X f M) \<subseteq> sets (vimage_algebra X f N)"
  3286   using sets.top[of "sigma X {f -` A \<inter> X |A. A \<in> sets N}"]
  3287   unfolding vimage_algebra_def
  3288   apply (subst (asm) space_measure_of)
  3289   apply auto []
  3290   apply (subst sigma_le_sets)
  3291   apply auto
  3292   done
  3293 
  3294 lemma mono_restrict_space: "sets M \<le> sets N \<Longrightarrow> sets (restrict_space M X) \<subseteq> sets (restrict_space N X)"
  3295   unfolding sets_restrict_space by (rule image_mono)
  3296 
  3297 lemma sets_eq_bot: "sets M = {{}} \<longleftrightarrow> M = bot"
  3298   apply safe
  3299   apply (intro measure_eqI)
  3300   apply auto
  3301   done
  3302 
  3303 lemma sets_eq_bot2: "{{}} = sets M \<longleftrightarrow> M = bot"
  3304   using sets_eq_bot[of M] by blast
  3305 
  3306 
  3307 lemma (in finite_measure) countable_support:
  3308   "countable {x. measure M {x} \<noteq> 0}"
  3309 proof cases
  3310   assume "measure M (space M) = 0"
  3311   with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
  3312     by auto
  3313   then show ?thesis
  3314     by simp
  3315 next
  3316   let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
  3317   assume "?M \<noteq> 0"
  3318   then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
  3319     using reals_Archimedean[of "?m x / ?M" for x]
  3320     by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
  3321   have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
  3322   proof (rule ccontr)
  3323     fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
  3324     then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
  3325       by (metis infinite_arbitrarily_large)
  3326     from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x"
  3327       by auto
  3328     { fix x assume "x \<in> X"
  3329       from \<open>?M \<noteq> 0\<close> *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
  3330       then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
  3331     note singleton_sets = this
  3332     have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
  3333       using \<open>?M \<noteq> 0\<close>
  3334       by (simp add: \<open>card X = Suc (Suc n)\<close> field_simps less_le)
  3335     also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
  3336       by (rule setsum_mono) fact
  3337     also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
  3338       using singleton_sets \<open>finite X\<close>
  3339       by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
  3340     finally have "?M < measure M (\<Union>x\<in>X. {x})" .
  3341     moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
  3342       using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
  3343     ultimately show False by simp
  3344   qed
  3345   show ?thesis
  3346     unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
  3347 qed
  3348 
  3349 end