author nipkow
Sat Dec 29 15:43:53 2018 +0100 (9 months ago)
changeset 69529 4ab9657b3257
parent 69517 dc20f278e8f3
child 69541 d466e0a639e4
permissions -rw-r--r--
capitalize proper names in lemma names
     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 *)
     7 section \<open>Measure Spaces\<close>
     9 theory Measure_Space
    10 imports
    11   Measurable "HOL-Library.Extended_Nonnegative_Real"
    12 begin
    14 subsection%unimportant "Relate extended reals and the indicator function"
    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: sum.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
    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
    44 lemma sum_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 sum.If_cases[OF \<open>finite P\<close>])
    55 qed
    57 text \<open>
    58   The type for emeasure spaces is already defined in @{theory "HOL-Analysis.Sigma_Algebra"}, as it
    59   is also used to represent sigma algebras (with an arbitrary emeasure).
    60 \<close>
    62 subsection%unimportant "Extend binary sets"
    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
    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)
    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)
    94 subsection%unimportant \<open>Properties of a premeasure @{term \<mu>}\<close>
    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 "HOL-Analysis.Sigma_Algebra"}.
    99 \<close>
   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)"
   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)
   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))))"
   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
   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)"
   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)"
   137 lemma positiveD1: "positive M f \<Longrightarrow> f {} = 0" by (auto simp: positive_def)
   139 lemma positiveD_empty:
   140   "positive M f \<Longrightarrow> f {} = 0"
   141   by (auto simp add: positive_def)
   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)
   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)
   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)
   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
   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
   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
   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
   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
   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)
   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"
   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
   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)
   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)
   299   have disj_not_empty: "disjoint_family_on F {i. F i \<noteq> {}}"
   300     using disj by (auto simp: disjoint_family_on_def)
   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 sum.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
   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>(A ` UNIV) \<in> M \<longrightarrow> (\<Sum>i. f (A i)) = f (\<Union>(A ` UNIV))"
   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
   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>"
   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
   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[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
   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
   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
   445 subsection%unimportant \<open>Properties of @{const emeasure}\<close>
   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)
   450 lemma emeasure_empty[simp, intro]: "emeasure M {} = 0"
   451   using emeasure_positive[of M] by (simp add: positive_def)
   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])
   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)
   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)
   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
   468 lemma emeasure_additive: "additive (sets M) (emeasure M)"
   469   by (metis sets.countably_additive_additive emeasure_positive emeasure_countably_additive)
   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] ..
   475 lemma emeasure_Union:
   476   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> emeasure M (A \<union> B) = emeasure M A + emeasure M (B - A)"
   477   using plus_emeasure[of A M "B - A"] by auto
   479 lemma sum_emeasure:
   480   "F`I \<subseteq> sets M \<Longrightarrow> disjoint_family_on F I \<Longrightarrow> finite I \<Longrightarrow>
   481     (\<Sum>i\<in>I. emeasure M (F i)) = emeasure M (\<Union>i\<in>I. F i)"
   482   by (metis sets.additive_sum emeasure_positive emeasure_additive)
   484 lemma emeasure_mono:
   485   "a \<subseteq> b \<Longrightarrow> b \<in> sets M \<Longrightarrow> emeasure M a \<le> emeasure M b"
   486   by (metis zero_le sets.additive_increasing emeasure_additive emeasure_notin_sets emeasure_positive increasingD)
   488 lemma emeasure_space:
   489   "emeasure M A \<le> emeasure M (space M)"
   490   by (metis emeasure_mono emeasure_notin_sets sets.sets_into_space zero_le)
   492 lemma emeasure_Diff:
   493   assumes finite: "emeasure M B \<noteq> \<infinity>"
   494   and [measurable]: "A \<in> sets M" "B \<in> sets M" and "B \<subseteq> A"
   495   shows "emeasure M (A - B) = emeasure M A - emeasure M B"
   496 proof -
   497   have "(A - B) \<union> B = A" using \<open>B \<subseteq> A\<close> by auto
   498   then have "emeasure M A = emeasure M ((A - B) \<union> B)" by simp
   499   also have "\<dots> = emeasure M (A - B) + emeasure M B"
   500     by (subst plus_emeasure[symmetric]) auto
   501   finally show "emeasure M (A - B) = emeasure M A - emeasure M B"
   502     using finite by simp
   503 qed
   505 lemma emeasure_compl:
   506   "s \<in> sets M \<Longrightarrow> emeasure M s \<noteq> \<infinity> \<Longrightarrow> emeasure M (space M - s) = emeasure M (space M) - emeasure M s"
   507   by (rule emeasure_Diff) (auto dest: sets.sets_into_space)
   509 lemma Lim_emeasure_incseq:
   510   "range A \<subseteq> sets M \<Longrightarrow> incseq A \<Longrightarrow> (\<lambda>i. (emeasure M (A i))) \<longlonglongrightarrow> emeasure M (\<Union>i. A i)"
   511   using emeasure_countably_additive
   512   by (auto simp add: sets.countably_additive_iff_continuous_from_below emeasure_positive
   513     emeasure_additive)
   515 lemma incseq_emeasure:
   516   assumes "range B \<subseteq> sets M" "incseq B"
   517   shows "incseq (\<lambda>i. emeasure M (B i))"
   518   using assms by (auto simp: incseq_def intro!: emeasure_mono)
   520 lemma SUP_emeasure_incseq:
   521   assumes A: "range A \<subseteq> sets M" "incseq A"
   522   shows "(SUP n. emeasure M (A n)) = emeasure M (\<Union>i. A i)"
   523   using LIMSEQ_SUP[OF incseq_emeasure, OF A] Lim_emeasure_incseq[OF A]
   524   by (simp add: LIMSEQ_unique)
   526 lemma decseq_emeasure:
   527   assumes "range B \<subseteq> sets M" "decseq B"
   528   shows "decseq (\<lambda>i. emeasure M (B i))"
   529   using assms by (auto simp: decseq_def intro!: emeasure_mono)
   531 lemma INF_emeasure_decseq:
   532   assumes A: "range A \<subseteq> sets M" and "decseq A"
   533   and finite: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   534   shows "(INF n. emeasure M (A n)) = emeasure M (\<Inter>i. A i)"
   535 proof -
   536   have le_MI: "emeasure M (\<Inter>i. A i) \<le> emeasure M (A 0)"
   537     using A by (auto intro!: emeasure_mono)
   538   hence *: "emeasure M (\<Inter>i. A i) \<noteq> \<infinity>" using finite[of 0] by (auto simp: top_unique)
   540   have "emeasure M (A 0) - (INF n. emeasure M (A n)) = (SUP n. emeasure M (A 0) - emeasure M (A n))"
   541     by (simp add: ennreal_INF_const_minus)
   542   also have "\<dots> = (SUP n. emeasure M (A 0 - A n))"
   543     using A finite \<open>decseq A\<close>[unfolded decseq_def] by (subst emeasure_Diff) auto
   544   also have "\<dots> = emeasure M (\<Union>i. A 0 - A i)"
   545   proof (rule SUP_emeasure_incseq)
   546     show "range (\<lambda>n. A 0 - A n) \<subseteq> sets M"
   547       using A by auto
   548     show "incseq (\<lambda>n. A 0 - A n)"
   549       using \<open>decseq A\<close> by (auto simp add: incseq_def decseq_def)
   550   qed
   551   also have "\<dots> = emeasure M (A 0) - emeasure M (\<Inter>i. A i)"
   552     using A finite * by (simp, subst emeasure_Diff) auto
   553   finally show ?thesis
   554     by (rule ennreal_minus_cancel[rotated 3])
   555        (insert finite A, auto intro: INF_lower emeasure_mono)
   556 qed
   558 lemma INF_emeasure_decseq':
   559   assumes A: "\<And>i. A i \<in> sets M" and "decseq A"
   560   and finite: "\<exists>i. emeasure M (A i) \<noteq> \<infinity>"
   561   shows "(INF n. emeasure M (A n)) = emeasure M (\<Inter>i. A i)"
   562 proof -
   563   from finite obtain i where i: "emeasure M (A i) < \<infinity>"
   564     by (auto simp: less_top)
   565   have fin: "i \<le> j \<Longrightarrow> emeasure M (A j) < \<infinity>" for j
   566     by (rule le_less_trans[OF emeasure_mono i]) (auto intro!: decseqD[OF \<open>decseq A\<close>] A)
   568   have "(INF n. emeasure M (A n)) = (INF n. emeasure M (A (n + i)))"
   569   proof (rule INF_eq)
   570     show "\<exists>j\<in>UNIV. emeasure M (A (j + i)) \<le> emeasure M (A i')" for i'
   571       by (intro bexI[of _ i'] emeasure_mono decseqD[OF \<open>decseq A\<close>] A) auto
   572   qed auto
   573   also have "\<dots> = emeasure M (INF n. (A (n + i)))"
   574     using A \<open>decseq A\<close> fin by (intro INF_emeasure_decseq) (auto simp: decseq_def less_top)
   575   also have "(INF n. (A (n + i))) = (INF n. A n)"
   576     by (meson INF_eq UNIV_I assms(2) decseqD le_add1)
   577   finally show ?thesis .
   578 qed
   580 lemma emeasure_INT_decseq_subset:
   581   fixes F :: "nat \<Rightarrow> 'a set"
   582   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"
   583   assumes F_sets[measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M"
   584     and fin: "\<And>i. i \<in> I \<Longrightarrow> emeasure M (F i) \<noteq> \<infinity>"
   585   shows "emeasure M (\<Inter>i\<in>I. F i) = (INF i\<in>I. emeasure M (F i))"
   586 proof cases
   587   assume "finite I"
   588   have "(\<Inter>i\<in>I. F i) = F (Max I)"
   589     using I \<open>finite I\<close> by (intro antisym INF_lower INF_greatest F) auto
   590   moreover have "(INF i\<in>I. emeasure M (F i)) = emeasure M (F (Max I))"
   591     using I \<open>finite I\<close> by (intro antisym INF_lower INF_greatest F emeasure_mono) auto
   592   ultimately show ?thesis
   593     by simp
   594 next
   595   assume "infinite I"
   596   define L where "L n = (LEAST i. i \<in> I \<and> i \<ge> n)" for n
   597   have L: "L n \<in> I \<and> n \<le> L n" for n
   598     unfolding L_def
   599   proof (rule LeastI_ex)
   600     show "\<exists>x. x \<in> I \<and> n \<le> x"
   601       using \<open>infinite I\<close> finite_subset[of I "{..< n}"]
   602       by (rule_tac ccontr) (auto simp: not_le)
   603   qed
   604   have L_eq[simp]: "i \<in> I \<Longrightarrow> L i = i" for i
   605     unfolding L_def by (intro Least_equality) auto
   606   have L_mono: "i \<le> j \<Longrightarrow> L i \<le> L j" for i j
   607     using L[of j] unfolding L_def by (intro Least_le) (auto simp: L_def)
   609   have "emeasure M (\<Inter>i. F (L i)) = (INF i. emeasure M (F (L i)))"
   610   proof (intro INF_emeasure_decseq[symmetric])
   611     show "decseq (\<lambda>i. F (L i))"
   612       using L by (intro antimonoI F L_mono) auto
   613   qed (insert L fin, auto)
   614   also have "\<dots> = (INF i\<in>I. emeasure M (F i))"
   615   proof (intro antisym INF_greatest)
   616     show "i \<in> I \<Longrightarrow> (INF i. emeasure M (F (L i))) \<le> emeasure M (F i)" for i
   617       by (intro INF_lower2[of i]) auto
   618   qed (insert L, auto intro: INF_lower)
   619   also have "(\<Inter>i. F (L i)) = (\<Inter>i\<in>I. F i)"
   620   proof (intro antisym INF_greatest)
   621     show "i \<in> I \<Longrightarrow> (\<Inter>i. F (L i)) \<subseteq> F i" for i
   622       by (intro INF_lower2[of i]) auto
   623   qed (insert L, auto)
   624   finally show ?thesis .
   625 qed
   627 lemma Lim_emeasure_decseq:
   628   assumes A: "range A \<subseteq> sets M" "decseq A" and fin: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   629   shows "(\<lambda>i. emeasure M (A i)) \<longlonglongrightarrow> emeasure M (\<Inter>i. A i)"
   630   using LIMSEQ_INF[OF decseq_emeasure, OF A]
   631   using INF_emeasure_decseq[OF A fin] by simp
   633 lemma emeasure_lfp'[consumes 1, case_names cont measurable]:
   634   assumes "P M"
   635   assumes cont: "sup_continuous F"
   636   assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
   637   shows "emeasure M {x\<in>space M. lfp F x} = (SUP i. emeasure M {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   638 proof -
   639   have "emeasure M {x\<in>space M. lfp F x} = emeasure M (\<Union>i. {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   640     using sup_continuous_lfp[OF cont] by (auto simp add: bot_fun_def intro!: arg_cong2[where f=emeasure])
   641   moreover { fix i from \<open>P M\<close> have "{x\<in>space M. (F ^^ i) (\<lambda>x. False) x} \<in> sets M"
   642     by (induct i arbitrary: M) (auto simp add: pred_def[symmetric] intro: *) }
   643   moreover have "incseq (\<lambda>i. {x\<in>space M. (F ^^ i) (\<lambda>x. False) x})"
   644   proof (rule incseq_SucI)
   645     fix i
   646     have "(F ^^ i) (\<lambda>x. False) \<le> (F ^^ (Suc i)) (\<lambda>x. False)"
   647     proof (induct i)
   648       case 0 show ?case by (simp add: le_fun_def)
   649     next
   650       case Suc thus ?case using monoD[OF sup_continuous_mono[OF cont] Suc] by auto
   651     qed
   652     then show "{x \<in> space M. (F ^^ i) (\<lambda>x. False) x} \<subseteq> {x \<in> space M. (F ^^ Suc i) (\<lambda>x. False) x}"
   653       by auto
   654   qed
   655   ultimately show ?thesis
   656     by (subst SUP_emeasure_incseq) auto
   657 qed
   659 lemma emeasure_lfp:
   660   assumes [simp]: "\<And>s. sets (M s) = sets N"
   661   assumes cont: "sup_continuous F" "sup_continuous f"
   662   assumes meas: "\<And>P. Measurable.pred N P \<Longrightarrow> Measurable.pred N (F P)"
   663   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"
   664   shows "emeasure (M s) {x\<in>space N. lfp F x} = lfp f s"
   665 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])
   666   fix C assume "incseq C" "\<And>i. Measurable.pred N (C i)"
   667   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}))"
   668     unfolding SUP_apply[abs_def]
   669     by (subst SUP_emeasure_incseq) (auto simp: mono_def fun_eq_iff intro!: arg_cong2[where f=emeasure])
   670 qed (auto simp add: iter le_fun_def SUP_apply[abs_def] intro!: meas cont)
   672 lemma emeasure_subadditive_finite:
   673   "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))"
   674   by (rule sets.subadditive[OF emeasure_positive emeasure_additive]) auto
   676 lemma emeasure_subadditive:
   677   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> emeasure M (A \<union> B) \<le> emeasure M A + emeasure M B"
   678   using emeasure_subadditive_finite[of "{True, False}" "\<lambda>True \<Rightarrow> A | False \<Rightarrow> B" M] by simp
   680 lemma emeasure_subadditive_countably:
   681   assumes "range f \<subseteq> sets M"
   682   shows "emeasure M (\<Union>i. f i) \<le> (\<Sum>i. emeasure M (f i))"
   683 proof -
   684   have "emeasure M (\<Union>i. f i) = emeasure M (\<Union>i. disjointed f i)"
   685     unfolding UN_disjointed_eq ..
   686   also have "\<dots> = (\<Sum>i. emeasure M (disjointed f i))"
   687     using sets.range_disjointed_sets[OF assms] suminf_emeasure[of "disjointed f"]
   688     by (simp add:  disjoint_family_disjointed comp_def)
   689   also have "\<dots> \<le> (\<Sum>i. emeasure M (f i))"
   690     using sets.range_disjointed_sets[OF assms] assms
   691     by (auto intro!: suminf_le emeasure_mono disjointed_subset)
   692   finally show ?thesis .
   693 qed
   695 lemma emeasure_insert:
   696   assumes sets: "{x} \<in> sets M" "A \<in> sets M" and "x \<notin> A"
   697   shows "emeasure M (insert x A) = emeasure M {x} + emeasure M A"
   698 proof -
   699   have "{x} \<inter> A = {}" using \<open>x \<notin> A\<close> by auto
   700   from plus_emeasure[OF sets this] show ?thesis by simp
   701 qed
   703 lemma emeasure_insert_ne:
   704   "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"
   705   by (rule emeasure_insert)
   707 lemma emeasure_eq_sum_singleton:
   708   assumes "finite S" "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
   709   shows "emeasure M S = (\<Sum>x\<in>S. emeasure M {x})"
   710   using sum_emeasure[of "\<lambda>x. {x}" S M] assms
   711   by (auto simp: disjoint_family_on_def subset_eq)
   713 lemma sum_emeasure_cover:
   714   assumes "finite S" and "A \<in> sets M" and br_in_M: "B ` S \<subseteq> sets M"
   715   assumes A: "A \<subseteq> (\<Union>i\<in>S. B i)"
   716   assumes disj: "disjoint_family_on B S"
   717   shows "emeasure M A = (\<Sum>i\<in>S. emeasure M (A \<inter> (B i)))"
   718 proof -
   719   have "(\<Sum>i\<in>S. emeasure M (A \<inter> (B i))) = emeasure M (\<Union>i\<in>S. A \<inter> (B i))"
   720   proof (rule sum_emeasure)
   721     show "disjoint_family_on (\<lambda>i. A \<inter> B i) S"
   722       using \<open>disjoint_family_on B S\<close>
   723       unfolding disjoint_family_on_def by auto
   724   qed (insert assms, auto)
   725   also have "(\<Union>i\<in>S. A \<inter> (B i)) = A"
   726     using A by auto
   727   finally show ?thesis by simp
   728 qed
   730 lemma emeasure_eq_0:
   731   "N \<in> sets M \<Longrightarrow> emeasure M N = 0 \<Longrightarrow> K \<subseteq> N \<Longrightarrow> emeasure M K = 0"
   732   by (metis emeasure_mono order_eq_iff zero_le)
   734 lemma emeasure_UN_eq_0:
   735   assumes "\<And>i::nat. emeasure M (N i) = 0" and "range N \<subseteq> sets M"
   736   shows "emeasure M (\<Union>i. N i) = 0"
   737 proof -
   738   have "emeasure M (\<Union>i. N i) \<le> 0"
   739     using emeasure_subadditive_countably[OF assms(2)] assms(1) by simp
   740   then show ?thesis
   741     by (auto intro: antisym zero_le)
   742 qed
   744 lemma measure_eqI_finite:
   745   assumes [simp]: "sets M = Pow A" "sets N = Pow A" and "finite A"
   746   assumes eq: "\<And>a. a \<in> A \<Longrightarrow> emeasure M {a} = emeasure N {a}"
   747   shows "M = N"
   748 proof (rule measure_eqI)
   749   fix X assume "X \<in> sets M"
   750   then have X: "X \<subseteq> A" by auto
   751   then have "emeasure M X = (\<Sum>a\<in>X. emeasure M {a})"
   752     using \<open>finite A\<close> by (subst emeasure_eq_sum_singleton) (auto dest: finite_subset)
   753   also have "\<dots> = (\<Sum>a\<in>X. emeasure N {a})"
   754     using X eq by (auto intro!: sum.cong)
   755   also have "\<dots> = emeasure N X"
   756     using X \<open>finite A\<close> by (subst emeasure_eq_sum_singleton) (auto dest: finite_subset)
   757   finally show "emeasure M X = emeasure N X" .
   758 qed simp
   760 lemma measure_eqI_generator_eq:
   761   fixes M N :: "'a measure" and E :: "'a set set" and A :: "nat \<Rightarrow> 'a set"
   762   assumes "Int_stable E" "E \<subseteq> Pow \<Omega>"
   763   and eq: "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
   764   and M: "sets M = sigma_sets \<Omega> E"
   765   and N: "sets N = sigma_sets \<Omega> E"
   766   and A: "range A \<subseteq> E" "(\<Union>i. A i) = \<Omega>" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
   767   shows "M = N"
   768 proof -
   769   let ?\<mu>  = "emeasure M" and ?\<nu> = "emeasure N"
   770   interpret S: sigma_algebra \<Omega> "sigma_sets \<Omega> E" by (rule sigma_algebra_sigma_sets) fact
   771   have "space M = \<Omega>"
   772     using[of M] sets.space_closed[of M] S.space_closed \<open>sets M = sigma_sets \<Omega> E\<close>
   773     by blast
   775   { fix F D assume "F \<in> E" and "?\<mu> F \<noteq> \<infinity>"
   776     then have [intro]: "F \<in> sigma_sets \<Omega> E" by auto
   777     have "?\<nu> F \<noteq> \<infinity>" using \<open>?\<mu> F \<noteq> \<infinity>\<close> \<open>F \<in> E\<close> eq by simp
   778     assume "D \<in> sets M"
   779     with \<open>Int_stable E\<close> \<open>E \<subseteq> Pow \<Omega>\<close> have "emeasure M (F \<inter> D) = emeasure N (F \<inter> D)"
   780       unfolding M
   781     proof (induct rule: sigma_sets_induct_disjoint)
   782       case (basic A)
   783       then have "F \<inter> A \<in> E" using \<open>Int_stable E\<close> \<open>F \<in> E\<close> by (auto simp: Int_stable_def)
   784       then show ?case using eq by auto
   785     next
   786       case empty then show ?case by simp
   787     next
   788       case (compl A)
   789       then have **: "F \<inter> (\<Omega> - A) = F - (F \<inter> A)"
   790         and [intro]: "F \<inter> A \<in> sigma_sets \<Omega> E"
   791         using \<open>F \<in> E\<close> S.sets_into_space by (auto simp: M)
   792       have "?\<nu> (F \<inter> A) \<le> ?\<nu> F" by (auto intro!: emeasure_mono simp: M N)
   793       then have "?\<nu> (F \<inter> A) \<noteq> \<infinity>" using \<open>?\<nu> F \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   794       have "?\<mu> (F \<inter> A) \<le> ?\<mu> F" by (auto intro!: emeasure_mono simp: M N)
   795       then have "?\<mu> (F \<inter> A) \<noteq> \<infinity>" using \<open>?\<mu> F \<noteq> \<infinity>\<close> by (auto simp: top_unique)
   796       then have "?\<mu> (F \<inter> (\<Omega> - A)) = ?\<mu> F - ?\<mu> (F \<inter> A)" unfolding **
   797         using \<open>F \<inter> A \<in> sigma_sets \<Omega> E\<close> by (auto intro!: emeasure_Diff simp: M N)
   798       also have "\<dots> = ?\<nu> F - ?\<nu> (F \<inter> A)" using eq \<open>F \<in> E\<close> compl by simp
   799       also have "\<dots> = ?\<nu> (F \<inter> (\<Omega> - A))" unfolding **
   800         using \<open>F \<inter> A \<in> sigma_sets \<Omega> E\<close> \<open>?\<nu> (F \<inter> A) \<noteq> \<infinity>\<close>
   801         by (auto intro!: emeasure_Diff[symmetric] simp: M N)
   802       finally show ?case
   803         using \<open>space M = \<Omega>\<close> by auto
   804     next
   805       case (union A)
   806       then have "?\<mu> (\<Union>x. F \<inter> A x) = ?\<nu> (\<Union>x. F \<inter> A x)"
   807         by (subst (1 2) suminf_emeasure[symmetric]) (auto simp: disjoint_family_on_def subset_eq M N)
   808       with A show ?case
   809         by auto
   810     qed }
   811   note * = this
   812   show "M = N"
   813   proof (rule measure_eqI)
   814     show "sets M = sets N"
   815       using M N by simp
   816     have [simp, intro]: "\<And>i. A i \<in> sets M"
   817       using A(1) by (auto simp: subset_eq M)
   818     fix F assume "F \<in> sets M"
   819     let ?D = "disjointed (\<lambda>i. F \<inter> A i)"
   820     from \<open>space M = \<Omega>\<close> have F_eq: "F = (\<Union>i. ?D i)"
   821       using \<open>F \<in> sets M\<close>[THEN sets.sets_into_space] A(2)[symmetric] by (auto simp: UN_disjointed_eq)
   822     have [simp, intro]: "\<And>i. ?D i \<in> sets M"
   823       using sets.range_disjointed_sets[of "\<lambda>i. F \<inter> A i" M] \<open>F \<in> sets M\<close>
   824       by (auto simp: subset_eq)
   825     have "disjoint_family ?D"
   826       by (auto simp: disjoint_family_disjointed)
   827     moreover
   828     have "(\<Sum>i. emeasure M (?D i)) = (\<Sum>i. emeasure N (?D i))"
   829     proof (intro arg_cong[where f=suminf] ext)
   830       fix i
   831       have "A i \<inter> ?D i = ?D i"
   832         by (auto simp: disjointed_def)
   833       then show "emeasure M (?D i) = emeasure N (?D i)"
   834         using *[of "A i" "?D i", OF _ A(3)] A(1) by auto
   835     qed
   836     ultimately show "emeasure M F = emeasure N F"
   837       by (simp add: image_subset_iff \<open>sets M = sets N\<close>[symmetric] F_eq[symmetric] suminf_emeasure)
   838   qed
   839 qed
   841 lemma space_empty: "space M = {} \<Longrightarrow> M = count_space {}"
   842   by (rule measure_eqI) (simp_all add: space_empty_iff)
   844 lemma measure_eqI_generator_eq_countable:
   845   fixes M N :: "'a measure" and E :: "'a set set" and A :: "'a set set"
   846   assumes E: "Int_stable E" "E \<subseteq> Pow \<Omega>" "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
   847     and sets: "sets M = sigma_sets \<Omega> E" "sets N = sigma_sets \<Omega> E"
   848   and A: "A \<subseteq> E" "(\<Union>A) = \<Omega>" "countable A" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
   849   shows "M = N"
   850 proof cases
   851   assume "\<Omega> = {}"
   852   have *: "sigma_sets \<Omega> E = sets (sigma \<Omega> E)"
   853     using E(2) by simp
   854   have "space M = \<Omega>" "space N = \<Omega>"
   855     using sets E(2) unfolding * by (auto dest: sets_eq_imp_space_eq simp del: sets_measure_of)
   856   then show "M = N"
   857     unfolding \<open>\<Omega> = {}\<close> by (auto dest: space_empty)
   858 next
   859   assume "\<Omega> \<noteq> {}" with \<open>\<Union>A = \<Omega>\<close> have "A \<noteq> {}" by auto
   860   from this \<open>countable A\<close> have rng: "range (from_nat_into A) = A"
   861     by (rule range_from_nat_into)
   862   show "M = N"
   863   proof (rule measure_eqI_generator_eq[OF E sets])
   864     show "range (from_nat_into A) \<subseteq> E"
   865       unfolding rng using \<open>A \<subseteq> E\<close> .
   866     show "(\<Union>i. from_nat_into A i) = \<Omega>"
   867       unfolding rng using \<open>\<Union>A = \<Omega>\<close> .
   868     show "emeasure M (from_nat_into A i) \<noteq> \<infinity>" for i
   869       using rng by (intro A) auto
   870   qed
   871 qed
   873 lemma measure_of_of_measure: "measure_of (space M) (sets M) (emeasure M) = M"
   874 proof (intro measure_eqI emeasure_measure_of_sigma)
   875   show "sigma_algebra (space M) (sets M)" ..
   876   show "positive (sets M) (emeasure M)"
   877     by (simp add: positive_def)
   878   show "countably_additive (sets M) (emeasure M)"
   879     by (simp add: emeasure_countably_additive)
   880 qed simp_all
   882 subsection \<open>\<open>\<mu>\<close>-null sets\<close>
   884 definition%important null_sets :: "'a measure \<Rightarrow> 'a set set" where
   885   "null_sets M = {N\<in>sets M. emeasure M N = 0}"
   887 lemma null_setsD1[dest]: "A \<in> null_sets M \<Longrightarrow> emeasure M A = 0"
   888   by (simp add: null_sets_def)
   890 lemma null_setsD2[dest]: "A \<in> null_sets M \<Longrightarrow> A \<in> sets M"
   891   unfolding null_sets_def by simp
   893 lemma null_setsI[intro]: "emeasure M A = 0 \<Longrightarrow> A \<in> sets M \<Longrightarrow> A \<in> null_sets M"
   894   unfolding null_sets_def by simp
   896 interpretation null_sets: ring_of_sets "space M" "null_sets M" for M
   897 proof (rule ring_of_setsI)
   898   show "null_sets M \<subseteq> Pow (space M)"
   899     using sets.sets_into_space by auto
   900   show "{} \<in> null_sets M"
   901     by auto
   902   fix A B assume null_sets: "A \<in> null_sets M" "B \<in> null_sets M"
   903   then have sets: "A \<in> sets M" "B \<in> sets M"
   904     by auto
   905   then have *: "emeasure M (A \<union> B) \<le> emeasure M A + emeasure M B"
   906     "emeasure M (A - B) \<le> emeasure M A"
   907     by (auto intro!: emeasure_subadditive emeasure_mono)
   908   then have "emeasure M B = 0" "emeasure M A = 0"
   909     using null_sets by auto
   910   with sets * show "A - B \<in> null_sets M" "A \<union> B \<in> null_sets M"
   911     by (auto intro!: antisym zero_le)
   912 qed
   914 lemma UN_from_nat_into:
   915   assumes I: "countable I" "I \<noteq> {}"
   916   shows "(\<Union>i\<in>I. N i) = (\<Union>i. N (from_nat_into I i))"
   917 proof -
   918   have "(\<Union>i\<in>I. N i) = \<Union>(N ` range (from_nat_into I))"
   919     using I by simp
   920   also have "\<dots> = (\<Union>i. (N \<circ> from_nat_into I) i)"
   921     by simp
   922   finally show ?thesis by simp
   923 qed
   925 lemma null_sets_UN':
   926   assumes "countable I"
   927   assumes "\<And>i. i \<in> I \<Longrightarrow> N i \<in> null_sets M"
   928   shows "(\<Union>i\<in>I. N i) \<in> null_sets M"
   929 proof cases
   930   assume "I = {}" then show ?thesis by simp
   931 next
   932   assume "I \<noteq> {}"
   933   show ?thesis
   934   proof (intro conjI CollectI null_setsI)
   935     show "(\<Union>i\<in>I. N i) \<in> sets M"
   936       using assms by (intro sets.countable_UN') auto
   937     have "emeasure M (\<Union>i\<in>I. N i) \<le> (\<Sum>n. emeasure M (N (from_nat_into I n)))"
   938       unfolding UN_from_nat_into[OF \<open>countable I\<close> \<open>I \<noteq> {}\<close>]
   939       using assms \<open>I \<noteq> {}\<close> by (intro emeasure_subadditive_countably) (auto intro: from_nat_into)
   940     also have "(\<lambda>n. emeasure M (N (from_nat_into I n))) = (\<lambda>_. 0)"
   941       using assms \<open>I \<noteq> {}\<close> by (auto intro: from_nat_into)
   942     finally show "emeasure M (\<Union>i\<in>I. N i) = 0"
   943       by (intro antisym zero_le) simp
   944   qed
   945 qed
   947 lemma null_sets_UN[intro]:
   948   "(\<And>i::'i::countable. N i \<in> null_sets M) \<Longrightarrow> (\<Union>i. N i) \<in> null_sets M"
   949   by (rule null_sets_UN') auto
   951 lemma null_set_Int1:
   952   assumes "B \<in> null_sets M" "A \<in> sets M" shows "A \<inter> B \<in> null_sets M"
   953 proof (intro CollectI conjI null_setsI)
   954   show "emeasure M (A \<inter> B) = 0" using assms
   955     by (intro emeasure_eq_0[of B _ "A \<inter> B"]) auto
   956 qed (insert assms, auto)
   958 lemma null_set_Int2:
   959   assumes "B \<in> null_sets M" "A \<in> sets M" shows "B \<inter> A \<in> null_sets M"
   960   using assms by (subst Int_commute) (rule null_set_Int1)
   962 lemma emeasure_Diff_null_set:
   963   assumes "B \<in> null_sets M" "A \<in> sets M"
   964   shows "emeasure M (A - B) = emeasure M A"
   965 proof -
   966   have *: "A - B = (A - (A \<inter> B))" by auto
   967   have "A \<inter> B \<in> null_sets M" using assms by (rule null_set_Int1)
   968   then show ?thesis
   969     unfolding * using assms
   970     by (subst emeasure_Diff) auto
   971 qed
   973 lemma null_set_Diff:
   974   assumes "B \<in> null_sets M" "A \<in> sets M" shows "B - A \<in> null_sets M"
   975 proof (intro CollectI conjI null_setsI)
   976   show "emeasure M (B - A) = 0" using assms by (intro emeasure_eq_0[of B _ "B - A"]) auto
   977 qed (insert assms, auto)
   979 lemma emeasure_Un_null_set:
   980   assumes "A \<in> sets M" "B \<in> null_sets M"
   981   shows "emeasure M (A \<union> B) = emeasure M A"
   982 proof -
   983   have *: "A \<union> B = A \<union> (B - A)" by auto
   984   have "B - A \<in> null_sets M" using assms(2,1) by (rule null_set_Diff)
   985   then show ?thesis
   986     unfolding * using assms
   987     by (subst plus_emeasure[symmetric]) auto
   988 qed
   990 subsection \<open>The almost everywhere filter (i.e.\ quantifier)\<close>
   992 definition%important ae_filter :: "'a measure \<Rightarrow> 'a filter" where
   993   "ae_filter M = (INF N\<in>null_sets M. principal (space M - N))"
   995 abbreviation almost_everywhere :: "'a measure \<Rightarrow> ('a \<Rightarrow> bool) \<Rightarrow> bool" where
   996   "almost_everywhere M P \<equiv> eventually P (ae_filter M)"
   998 syntax
   999   "_almost_everywhere" :: "pttrn \<Rightarrow> 'a \<Rightarrow> bool \<Rightarrow> bool" ("AE _ in _. _" [0,0,10] 10)
  1001 translations
  1002   "AE x in M. P" \<rightleftharpoons> "CONST almost_everywhere M (\<lambda>x. P)"
  1004 abbreviation
  1005   "set_almost_everywhere A M P \<equiv> AE x in M. x \<in> A \<longrightarrow> P x"
  1007 syntax
  1008   "_set_almost_everywhere" :: "pttrn \<Rightarrow> 'a set \<Rightarrow> 'a \<Rightarrow> bool \<Rightarrow> bool"
  1009   ("AE _\<in>_ in _./ _" [0,0,0,10] 10)
  1011 translations
  1012   "AE x\<in>A in M. P" \<rightleftharpoons> "CONST set_almost_everywhere A M (\<lambda>x. P)"
  1014 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)"
  1015   unfolding ae_filter_def by (subst eventually_INF_base) (auto simp: eventually_principal subset_eq)
  1017 lemma AE_I':
  1018   "N \<in> null_sets M \<Longrightarrow> {x\<in>space M. \<not> P x} \<subseteq> N \<Longrightarrow> (AE x in M. P x)"
  1019   unfolding eventually_ae_filter by auto
  1021 lemma AE_iff_null:
  1022   assumes "{x\<in>space M. \<not> P x} \<in> sets M" (is "?P \<in> sets M")
  1023   shows "(AE x in M. P x) \<longleftrightarrow> {x\<in>space M. \<not> P x} \<in> null_sets M"
  1024 proof
  1025   assume "AE x in M. P x" then obtain N where N: "N \<in> sets M" "?P \<subseteq> N" "emeasure M N = 0"
  1026     unfolding eventually_ae_filter by auto
  1027   have "emeasure M ?P \<le> emeasure M N"
  1028     using assms N(1,2) by (auto intro: emeasure_mono)
  1029   then have "emeasure M ?P = 0"
  1030     unfolding \<open>emeasure M N = 0\<close> by auto
  1031   then show "?P \<in> null_sets M" using assms by auto
  1032 next
  1033   assume "?P \<in> null_sets M" with assms show "AE x in M. P x" by (auto intro: AE_I')
  1034 qed
  1036 lemma AE_iff_null_sets:
  1037   "N \<in> sets M \<Longrightarrow> N \<in> null_sets M \<longleftrightarrow> (AE x in M. x \<notin> N)"
  1038   using Int_absorb1[OF sets.sets_into_space, of N M]
  1039   by (subst AE_iff_null) (auto simp: Int_def[symmetric])
  1041 lemma AE_not_in:
  1042   "N \<in> null_sets M \<Longrightarrow> AE x in M. x \<notin> N"
  1043   by (metis AE_iff_null_sets null_setsD2)
  1045 lemma AE_iff_measurable:
  1046   "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"
  1047   using AE_iff_null[of _ P] by auto
  1049 lemma AE_E[consumes 1]:
  1050   assumes "AE x in M. P x"
  1051   obtains N where "{x \<in> space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
  1052   using assms unfolding eventually_ae_filter by auto
  1054 lemma AE_E2:
  1055   assumes "AE x in M. P x" "{x\<in>space M. P x} \<in> sets M"
  1056   shows "emeasure M {x\<in>space M. \<not> P x} = 0" (is "emeasure M ?P = 0")
  1057 proof -
  1058   have "{x\<in>space M. \<not> P x} = space M - {x\<in>space M. P x}" by auto
  1059   with AE_iff_null[of M P] assms show ?thesis by auto
  1060 qed
  1062 lemma AE_E3:
  1063   assumes "AE x in M. P x"
  1064   obtains N where "\<And>x. x \<in> space M - N \<Longrightarrow> P x" "N \<in> null_sets M"
  1065 using assms unfolding eventually_ae_filter by auto
  1067 lemma AE_I:
  1068   assumes "{x \<in> space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
  1069   shows "AE x in M. P x"
  1070   using assms unfolding eventually_ae_filter by auto
  1072 lemma AE_mp[elim!]:
  1073   assumes AE_P: "AE x in M. P x" and AE_imp: "AE x in M. P x \<longrightarrow> Q x"
  1074   shows "AE x in M. Q x"
  1075 proof -
  1076   from AE_P obtain A where P: "{x\<in>space M. \<not> P x} \<subseteq> A"
  1077     and A: "A \<in> sets M" "emeasure M A = 0"
  1078     by (auto elim!: AE_E)
  1080   from AE_imp obtain B where imp: "{x\<in>space M. P x \<and> \<not> Q x} \<subseteq> B"
  1081     and B: "B \<in> sets M" "emeasure M B = 0"
  1082     by (auto elim!: AE_E)
  1084   show ?thesis
  1085   proof (intro AE_I)
  1086     have "emeasure M (A \<union> B) \<le> 0"
  1087       using emeasure_subadditive[of A M B] A B by auto
  1088     then show "A \<union> B \<in> sets M" "emeasure M (A \<union> B) = 0"
  1089       using A B by auto
  1090     show "{x\<in>space M. \<not> Q x} \<subseteq> A \<union> B"
  1091       using P imp by auto
  1092   qed
  1093 qed
  1095 text \<open>The next lemma is convenient to combine with a lemma whose conclusion is of the
  1096 form \<open>AE x in M. P x = Q x\<close>: for such a lemma, there is no \verb+[symmetric]+ variant,
  1097 but using \<open>AE_symmetric[OF...]\<close> will replace it.\<close>
  1099 (* depricated replace by laws about eventually *)
  1100 lemma
  1101   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"
  1102     and AE_disjI1: "AE x in M. P x \<Longrightarrow> AE x in M. P x \<or> Q x"
  1103     and AE_disjI2: "AE x in M. Q x \<Longrightarrow> AE x in M. P x \<or> Q x"
  1104     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"
  1105     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)"
  1106   by auto
  1108 lemma AE_symmetric:
  1109   assumes "AE x in M. P x = Q x"
  1110   shows "AE x in M. Q x = P x"
  1111   using assms by auto
  1113 lemma AE_impI:
  1114   "(P \<Longrightarrow> AE x in M. Q x) \<Longrightarrow> AE x in M. P \<longrightarrow> Q x"
  1115   by (cases P) auto
  1117 lemma AE_measure:
  1118   assumes AE: "AE x in M. P x" and sets: "{x\<in>space M. P x} \<in> sets M" (is "?P \<in> sets M")
  1119   shows "emeasure M {x\<in>space M. P x} = emeasure M (space M)"
  1120 proof -
  1121   from AE_E[OF AE] guess N . note N = this
  1122   with sets have "emeasure M (space M) \<le> emeasure M (?P \<union> N)"
  1123     by (intro emeasure_mono) auto
  1124   also have "\<dots> \<le> emeasure M ?P + emeasure M N"
  1125     using sets N by (intro emeasure_subadditive) auto
  1126   also have "\<dots> = emeasure M ?P" using N by simp
  1127   finally show "emeasure M ?P = emeasure M (space M)"
  1128     using emeasure_space[of M "?P"] by auto
  1129 qed
  1131 lemma AE_space: "AE x in M. x \<in> space M"
  1132   by (rule AE_I[where N="{}"]) auto
  1134 lemma AE_I2[simp, intro]:
  1135   "(\<And>x. x \<in> space M \<Longrightarrow> P x) \<Longrightarrow> AE x in M. P x"
  1136   using AE_space by force
  1138 lemma AE_Ball_mp:
  1139   "\<forall>x\<in>space M. P x \<Longrightarrow> AE x in M. P x \<longrightarrow> Q x \<Longrightarrow> AE x in M. Q x"
  1140   by auto
  1142 lemma AE_cong[cong]:
  1143   "(\<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)"
  1144   by auto
  1146 lemma AE_cong_strong: "M = N \<Longrightarrow> (\<And>x. x \<in> space N =simp=> P x = Q x) \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> (AE x in N. Q x)"
  1147   by (auto simp: simp_implies_def)
  1149 lemma AE_all_countable:
  1150   "(AE x in M. \<forall>i. P i x) \<longleftrightarrow> (\<forall>i::'i::countable. AE x in M. P i x)"
  1151 proof
  1152   assume "\<forall>i. AE x in M. P i x"
  1153   from this[unfolded eventually_ae_filter Bex_def, THEN choice]
  1154   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
  1155   have "{x\<in>space M. \<not> (\<forall>i. P i x)} \<subseteq> (\<Union>i. {x\<in>space M. \<not> P i x})" by auto
  1156   also have "\<dots> \<subseteq> (\<Union>i. N i)" using N by auto
  1157   finally have "{x\<in>space M. \<not> (\<forall>i. P i x)} \<subseteq> (\<Union>i. N i)" .
  1158   moreover from N have "(\<Union>i. N i) \<in> null_sets M"
  1159     by (intro null_sets_UN) auto
  1160   ultimately show "AE x in M. \<forall>i. P i x"
  1161     unfolding eventually_ae_filter by auto
  1162 qed auto
  1164 lemma AE_ball_countable:
  1165   assumes [intro]: "countable X"
  1166   shows "(AE x in M. \<forall>y\<in>X. P x y) \<longleftrightarrow> (\<forall>y\<in>X. AE x in M. P x y)"
  1167 proof
  1168   assume "\<forall>y\<in>X. AE x in M. P x y"
  1169   from this[unfolded eventually_ae_filter Bex_def, THEN bchoice]
  1170   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"
  1171     by auto
  1172   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})"
  1173     by auto
  1174   also have "\<dots> \<subseteq> (\<Union>y\<in>X. N y)"
  1175     using N by auto
  1176   finally have "{x\<in>space M. \<not> (\<forall>y\<in>X. P x y)} \<subseteq> (\<Union>y\<in>X. N y)" .
  1177   moreover from N have "(\<Union>y\<in>X. N y) \<in> null_sets M"
  1178     by (intro null_sets_UN') auto
  1179   ultimately show "AE x in M. \<forall>y\<in>X. P x y"
  1180     unfolding eventually_ae_filter by auto
  1181 qed auto
  1183 lemma AE_ball_countable':
  1184   "(\<And>N. N \<in> I \<Longrightarrow> AE x in M. P N x) \<Longrightarrow> countable I \<Longrightarrow> AE x in M. \<forall>N \<in> I. P N x"
  1185   unfolding AE_ball_countable by simp
  1187 lemma pairwise_alt: "pairwise R S \<longleftrightarrow> (\<forall>x\<in>S. \<forall>y\<in>S-{x}. R x y)"
  1188   by (auto simp add: pairwise_def)
  1190 lemma AE_pairwise: "countable F \<Longrightarrow> pairwise (\<lambda>A B. AE x in M. R x A B) F \<longleftrightarrow> (AE x in M. pairwise (R x) F)"
  1191   unfolding pairwise_alt by (simp add: AE_ball_countable)
  1193 lemma AE_discrete_difference:
  1194   assumes X: "countable X"
  1195   assumes null: "\<And>x. x \<in> X \<Longrightarrow> emeasure M {x} = 0"
  1196   assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  1197   shows "AE x in M. x \<notin> X"
  1198 proof -
  1199   have "(\<Union>x\<in>X. {x}) \<in> null_sets M"
  1200     using assms by (intro null_sets_UN') auto
  1201   from AE_not_in[OF this] show "AE x in M. x \<notin> X"
  1202     by auto
  1203 qed
  1205 lemma AE_finite_all:
  1206   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)"
  1207   using f by induct auto
  1209 lemma AE_finite_allI:
  1210   assumes "finite S"
  1211   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"
  1212   using AE_finite_all[OF \<open>finite S\<close>] by auto
  1214 lemma emeasure_mono_AE:
  1215   assumes imp: "AE x in M. x \<in> A \<longrightarrow> x \<in> B"
  1216     and B: "B \<in> sets M"
  1217   shows "emeasure M A \<le> emeasure M B"
  1218 proof cases
  1219   assume A: "A \<in> sets M"
  1220   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"
  1221     by (auto simp: eventually_ae_filter)
  1222   have "emeasure M A = emeasure M (A - N)"
  1223     using N A by (subst emeasure_Diff_null_set) auto
  1224   also have "emeasure M (A - N) \<le> emeasure M (B - N)"
  1225     using N A B sets.sets_into_space by (auto intro!: emeasure_mono)
  1226   also have "emeasure M (B - N) = emeasure M B"
  1227     using N B by (subst emeasure_Diff_null_set) auto
  1228   finally show ?thesis .
  1229 qed (simp add: emeasure_notin_sets)
  1231 lemma emeasure_eq_AE:
  1232   assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  1233   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  1234   shows "emeasure M A = emeasure M B"
  1235   using assms by (safe intro!: antisym emeasure_mono_AE) auto
  1237 lemma emeasure_Collect_eq_AE:
  1238   "AE x in M. P x \<longleftrightarrow> Q x \<Longrightarrow> Measurable.pred M Q \<Longrightarrow> Measurable.pred M P \<Longrightarrow>
  1239    emeasure M {x\<in>space M. P x} = emeasure M {x\<in>space M. Q x}"
  1240    by (intro emeasure_eq_AE) auto
  1242 lemma emeasure_eq_0_AE: "AE x in M. \<not> P x \<Longrightarrow> emeasure M {x\<in>space M. P x} = 0"
  1243   using AE_iff_measurable[OF _ refl, of M "\<lambda>x. \<not> P x"]
  1244   by (cases "{x\<in>space M. P x} \<in> sets M") (simp_all add: emeasure_notin_sets)
  1246 lemma emeasure_0_AE:
  1247   assumes "emeasure M (space M) = 0"
  1248   shows "AE x in M. P x"
  1249 using eventually_ae_filter assms by blast
  1251 lemma emeasure_add_AE:
  1252   assumes [measurable]: "A \<in> sets M" "B \<in> sets M" "C \<in> sets M"
  1253   assumes 1: "AE x in M. x \<in> C \<longleftrightarrow> x \<in> A \<or> x \<in> B"
  1254   assumes 2: "AE x in M. \<not> (x \<in> A \<and> x \<in> B)"
  1255   shows "emeasure M C = emeasure M A + emeasure M B"
  1256 proof -
  1257   have "emeasure M C = emeasure M (A \<union> B)"
  1258     by (rule emeasure_eq_AE) (insert 1, auto)
  1259   also have "\<dots> = emeasure M A + emeasure M (B - A)"
  1260     by (subst plus_emeasure) auto
  1261   also have "emeasure M (B - A) = emeasure M B"
  1262     by (rule emeasure_eq_AE) (insert 2, auto)
  1263   finally show ?thesis .
  1264 qed
  1266 subsection \<open>\<open>\<sigma>\<close>-finite Measures\<close>
  1268 locale%important sigma_finite_measure =
  1269   fixes M :: "'a measure"
  1270   assumes sigma_finite_countable:
  1271     "\<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>)"
  1273 lemma (in sigma_finite_measure) sigma_finite:
  1274   obtains A :: "nat \<Rightarrow> 'a set"
  1275   where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1276 proof -
  1277   obtain A :: "'a set set" where
  1278     [simp]: "countable A" and
  1279     A: "A \<subseteq> sets M" "(\<Union>A) = space M" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
  1280     using sigma_finite_countable by metis
  1281   show thesis
  1282   proof cases
  1283     assume "A = {}" with \<open>(\<Union>A) = space M\<close> show thesis
  1284       by (intro that[of "\<lambda>_. {}"]) auto
  1285   next
  1286     assume "A \<noteq> {}"
  1287     show thesis
  1288     proof
  1289       show "range (from_nat_into A) \<subseteq> sets M"
  1290         using \<open>A \<noteq> {}\<close> A by auto
  1291       have "(\<Union>i. from_nat_into A i) = \<Union>A"
  1292         using range_from_nat_into[OF \<open>A \<noteq> {}\<close> \<open>countable A\<close>] by auto
  1293       with A show "(\<Union>i. from_nat_into A i) = space M"
  1294         by auto
  1295     qed (intro A from_nat_into \<open>A \<noteq> {}\<close>)
  1296   qed
  1297 qed
  1299 lemma (in sigma_finite_measure) sigma_finite_disjoint:
  1300   obtains A :: "nat \<Rightarrow> 'a set"
  1301   where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "disjoint_family A"
  1302 proof -
  1303   obtain A :: "nat \<Rightarrow> 'a set" where
  1304     range: "range A \<subseteq> sets M" and
  1305     space: "(\<Union>i. A i) = space M" and
  1306     measure: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1307     using sigma_finite by blast
  1308   show thesis
  1309   proof (rule that[of "disjointed A"])
  1310     show "range (disjointed A) \<subseteq> sets M"
  1311       by (rule sets.range_disjointed_sets[OF range])
  1312     show "(\<Union>i. disjointed A i) = space M"
  1313       and "disjoint_family (disjointed A)"
  1314       using disjoint_family_disjointed UN_disjointed_eq[of A] space range
  1315       by auto
  1316     show "emeasure M (disjointed A i) \<noteq> \<infinity>" for i
  1317     proof -
  1318       have "emeasure M (disjointed A i) \<le> emeasure M (A i)"
  1319         using range disjointed_subset[of A i] by (auto intro!: emeasure_mono)
  1320       then show ?thesis using measure[of i] by (auto simp: top_unique)
  1321     qed
  1322   qed
  1323 qed
  1325 lemma (in sigma_finite_measure) sigma_finite_incseq:
  1326   obtains A :: "nat \<Rightarrow> 'a set"
  1327   where "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "incseq A"
  1328 proof -
  1329   obtain F :: "nat \<Rightarrow> 'a set" where
  1330     F: "range F \<subseteq> sets M" "(\<Union>i. F i) = space M" "\<And>i. emeasure M (F i) \<noteq> \<infinity>"
  1331     using sigma_finite by blast
  1332   show thesis
  1333   proof (rule that[of "\<lambda>n. \<Union>i\<le>n. F i"])
  1334     show "range (\<lambda>n. \<Union>i\<le>n. F i) \<subseteq> sets M"
  1335       using F by (force simp: incseq_def)
  1336     show "(\<Union>n. \<Union>i\<le>n. F i) = space M"
  1337     proof -
  1338       from F have "\<And>x. x \<in> space M \<Longrightarrow> \<exists>i. x \<in> F i" by auto
  1339       with F show ?thesis by fastforce
  1340     qed
  1341     show "emeasure M (\<Union>i\<le>n. F i) \<noteq> \<infinity>" for n
  1342     proof -
  1343       have "emeasure M (\<Union>i\<le>n. F i) \<le> (\<Sum>i\<le>n. emeasure M (F i))"
  1344         using F by (auto intro!: emeasure_subadditive_finite)
  1345       also have "\<dots> < \<infinity>"
  1346         using F by (auto simp: sum_Pinfty less_top)
  1347       finally show ?thesis by simp
  1348     qed
  1349     show "incseq (\<lambda>n. \<Union>i\<le>n. F i)"
  1350       by (force simp: incseq_def)
  1351   qed
  1352 qed
  1354 lemma (in sigma_finite_measure) approx_PInf_emeasure_with_finite:
  1355   fixes C::real
  1356   assumes W_meas: "W \<in> sets M"
  1357       and W_inf: "emeasure M W = \<infinity>"
  1358   obtains Z where "Z \<in> sets M" "Z \<subseteq> W" "emeasure M Z < \<infinity>" "emeasure M Z > C"
  1359 proof -
  1360   obtain A :: "nat \<Rightarrow> 'a set"
  1361     where A: "range A \<subseteq> sets M" "(\<Union>i. A i) = space M" "\<And>i. emeasure M (A i) \<noteq> \<infinity>" "incseq A"
  1362     using sigma_finite_incseq by blast
  1363   define B where "B = (\<lambda>i. W \<inter> A i)"
  1364   have B_meas: "\<And>i. B i \<in> sets M" using W_meas \<open>range A \<subseteq> sets M\<close> B_def by blast
  1365   have b: "\<And>i. B i \<subseteq> W" using B_def by blast
  1367   { fix i
  1368     have "emeasure M (B i) \<le> emeasure M (A i)"
  1369       using A by (intro emeasure_mono) (auto simp: B_def)
  1370     also have "emeasure M (A i) < \<infinity>"
  1371       using \<open>\<And>i. emeasure M (A i) \<noteq> \<infinity>\<close> by (simp add: less_top)
  1372     finally have "emeasure M (B i) < \<infinity>" . }
  1373   note c = this
  1375   have "W = (\<Union>i. B i)" using B_def \<open>(\<Union>i. A i) = space M\<close> W_meas by auto
  1376   moreover have "incseq B" using B_def \<open>incseq A\<close> by (simp add: incseq_def subset_eq)
  1377   ultimately have "(\<lambda>i. emeasure M (B i)) \<longlonglongrightarrow> emeasure M W" using W_meas B_meas
  1378     by (simp add: B_meas Lim_emeasure_incseq image_subset_iff)
  1379   then have "(\<lambda>i. emeasure M (B i)) \<longlonglongrightarrow> \<infinity>" using W_inf by simp
  1380   from order_tendstoD(1)[OF this, of C]
  1381   obtain i where d: "emeasure M (B i) > C"
  1382     by (auto simp: eventually_sequentially)
  1383   have "B i \<in> sets M" "B i \<subseteq> W" "emeasure M (B i) < \<infinity>" "emeasure M (B i) > C"
  1384     using B_meas b c d by auto
  1385   then show ?thesis using that by blast
  1386 qed
  1388 subsection \<open>Measure space induced by distribution of @{const measurable}-functions\<close>
  1390 definition%important distr :: "'a measure \<Rightarrow> 'b measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b measure" where
  1391   "distr M N f = measure_of (space N) (sets N) (\<lambda>A. emeasure M (f -` A \<inter> space M))"
  1393 lemma
  1394   shows sets_distr[simp, measurable_cong]: "sets (distr M N f) = sets N"
  1395     and space_distr[simp]: "space (distr M N f) = space N"
  1396   by (auto simp: distr_def)
  1398 lemma
  1399   shows measurable_distr_eq1[simp]: "measurable (distr Mf Nf f) Mf' = measurable Nf Mf'"
  1400     and measurable_distr_eq2[simp]: "measurable Mg' (distr Mg Ng g) = measurable Mg' Ng"
  1401   by (auto simp: measurable_def)
  1403 lemma distr_cong:
  1404   "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"
  1405   using sets_eq_imp_space_eq[of N L] by (simp add: distr_def Int_def cong: rev_conj_cong)
  1407 lemma emeasure_distr:
  1408   fixes f :: "'a \<Rightarrow> 'b"
  1409   assumes f: "f \<in> measurable M N" and A: "A \<in> sets N"
  1410   shows "emeasure (distr M N f) A = emeasure M (f -` A \<inter> space M)" (is "_ = ?\<mu> A")
  1411   unfolding distr_def
  1412 proof (rule emeasure_measure_of_sigma)
  1413   show "positive (sets N) ?\<mu>"
  1414     by (auto simp: positive_def)
  1416   show "countably_additive (sets N) ?\<mu>"
  1417   proof (intro countably_additiveI)
  1418     fix A :: "nat \<Rightarrow> 'b set" assume "range A \<subseteq> sets N" "disjoint_family A"
  1419     then have A: "\<And>i. A i \<in> sets N" "(\<Union>i. A i) \<in> sets N" by auto
  1420     then have *: "range (\<lambda>i. f -` (A i) \<inter> space M) \<subseteq> sets M"
  1421       using f by (auto simp: measurable_def)
  1422     moreover have "(\<Union>i. f -`  A i \<inter> space M) \<in> sets M"
  1423       using * by blast
  1424     moreover have **: "disjoint_family (\<lambda>i. f -` A i \<inter> space M)"
  1425       using \<open>disjoint_family A\<close> by (auto simp: disjoint_family_on_def)
  1426     ultimately show "(\<Sum>i. ?\<mu> (A i)) = ?\<mu> (\<Union>i. A i)"
  1427       using suminf_emeasure[OF _ **] A f
  1428       by (auto simp: comp_def vimage_UN)
  1429   qed
  1430   show "sigma_algebra (space N) (sets N)" ..
  1431 qed fact
  1433 lemma emeasure_Collect_distr:
  1434   assumes X[measurable]: "X \<in> measurable M N" "Measurable.pred N P"
  1435   shows "emeasure (distr M N X) {x\<in>space N. P x} = emeasure M {x\<in>space M. P (X x)}"
  1436   by (subst emeasure_distr)
  1437      (auto intro!: arg_cong2[where f=emeasure] X(1)[THEN measurable_space])
  1439 lemma emeasure_lfp2[consumes 1, case_names cont f measurable]:
  1440   assumes "P M"
  1441   assumes cont: "sup_continuous F"
  1442   assumes f: "\<And>M. P M \<Longrightarrow> f \<in> measurable M' M"
  1443   assumes *: "\<And>M A. P M \<Longrightarrow> (\<And>N. P N \<Longrightarrow> Measurable.pred N A) \<Longrightarrow> Measurable.pred M (F A)"
  1444   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)})"
  1445 proof (subst (1 2) emeasure_Collect_distr[symmetric, where X=f])
  1446   show "f \<in> measurable M' M"  "f \<in> measurable M' M"
  1447     using f[OF \<open>P M\<close>] by auto
  1448   { fix i show "Measurable.pred M ((F ^^ i) (\<lambda>x. False))"
  1449     using \<open>P M\<close> by (induction i arbitrary: M) (auto intro!: *) }
  1450   show "Measurable.pred M (lfp F)"
  1451     using \<open>P M\<close> cont * by (rule measurable_lfp_coinduct[of P])
  1453   have "emeasure (distr M' M f) {x \<in> space (distr M' M f). lfp F x} =
  1454     (SUP i. emeasure (distr M' M f) {x \<in> space (distr M' M f). (F ^^ i) (\<lambda>x. False) x})"
  1455     using \<open>P M\<close>
  1456   proof (coinduction arbitrary: M rule: emeasure_lfp')
  1457     case (measurable A N) then have "\<And>N. P N \<Longrightarrow> Measurable.pred (distr M' N f) A"
  1458       by metis
  1459     then have "\<And>N. P N \<Longrightarrow> Measurable.pred N A"
  1460       by simp
  1461     with \<open>P N\<close>[THEN *] show ?case
  1462       by auto
  1463   qed fact
  1464   then show "emeasure (distr M' M f) {x \<in> space M. lfp F x} =
  1465     (SUP i. emeasure (distr M' M f) {x \<in> space M. (F ^^ i) (\<lambda>x. False) x})"
  1466    by simp
  1467 qed
  1469 lemma distr_id[simp]: "distr N N (\<lambda>x. x) = N"
  1470   by (rule measure_eqI) (auto simp: emeasure_distr)
  1472 lemma distr_id2: "sets M = sets N \<Longrightarrow> distr N M (\<lambda>x. x) = N"
  1473   by (rule measure_eqI) (auto simp: emeasure_distr)
  1475 lemma measure_distr:
  1476   "f \<in> measurable M N \<Longrightarrow> S \<in> sets N \<Longrightarrow> measure (distr M N f) S = measure M (f -` S \<inter> space M)"
  1477   by (simp add: emeasure_distr measure_def)
  1479 lemma distr_cong_AE:
  1480   assumes 1: "M = K" "sets N = sets L" and
  1481     2: "(AE x in M. f x = g x)" and "f \<in> measurable M N" and "g \<in> measurable K L"
  1482   shows "distr M N f = distr K L g"
  1483 proof (rule measure_eqI)
  1484   fix A assume "A \<in> sets (distr M N f)"
  1485   with assms show "emeasure (distr M N f) A = emeasure (distr K L g) A"
  1486     by (auto simp add: emeasure_distr intro!: emeasure_eq_AE measurable_sets)
  1487 qed (insert 1, simp)
  1489 lemma AE_distrD:
  1490   assumes f: "f \<in> measurable M M'"
  1491     and AE: "AE x in distr M M' f. P x"
  1492   shows "AE x in M. P (f x)"
  1493 proof -
  1494   from AE[THEN AE_E] guess N .
  1495   with f show ?thesis
  1496     unfolding eventually_ae_filter
  1497     by (intro bexI[of _ "f -` N \<inter> space M"])
  1498        (auto simp: emeasure_distr measurable_def)
  1499 qed
  1501 lemma AE_distr_iff:
  1502   assumes f[measurable]: "f \<in> measurable M N" and P[measurable]: "{x \<in> space N. P x} \<in> sets N"
  1503   shows "(AE x in distr M N f. P x) \<longleftrightarrow> (AE x in M. P (f x))"
  1504 proof (subst (1 2) AE_iff_measurable[OF _ refl])
  1505   have "f -` {x\<in>space N. \<not> P x} \<inter> space M = {x \<in> space M. \<not> P (f x)}"
  1506     using f[THEN measurable_space] by auto
  1507   then show "(emeasure (distr M N f) {x \<in> space (distr M N f). \<not> P x} = 0) =
  1508     (emeasure M {x \<in> space M. \<not> P (f x)} = 0)"
  1509     by (simp add: emeasure_distr)
  1510 qed auto
  1512 lemma null_sets_distr_iff:
  1513   "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"
  1514   by (auto simp add: null_sets_def emeasure_distr)
  1516 proposition distr_distr:
  1517   "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)"
  1518   by (auto simp add: emeasure_distr measurable_space
  1519            intro!: arg_cong[where f="emeasure M"] measure_eqI)
  1521 subsection%unimportant \<open>Real measure values\<close>
  1523 lemma ring_of_finite_sets: "ring_of_sets (space M) {A\<in>sets M. emeasure M A \<noteq> top}"
  1524 proof (rule ring_of_setsI)
  1525   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>
  1526     a \<union> b \<in> {A \<in> sets M. emeasure M A \<noteq> top}" for a b
  1527     using emeasure_subadditive[of a M b] by (auto simp: top_unique)
  1529   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>
  1530     a - b \<in> {A \<in> sets M. emeasure M A \<noteq> top}" for a b
  1531     using emeasure_mono[of "a - b" a M] by (auto simp: top_unique)
  1532 qed (auto dest: sets.sets_into_space)
  1534 lemma measure_nonneg[simp]: "0 \<le> measure M A"
  1535   unfolding measure_def by auto
  1537 lemma measure_nonneg' [simp]: "\<not> measure M A < 0"
  1538   using measure_nonneg not_le by blast
  1540 lemma zero_less_measure_iff: "0 < measure M A \<longleftrightarrow> measure M A \<noteq> 0"
  1541   using measure_nonneg[of M A] by (auto simp add: le_less)
  1543 lemma measure_le_0_iff: "measure M X \<le> 0 \<longleftrightarrow> measure M X = 0"
  1544   using measure_nonneg[of M X] by linarith
  1546 lemma measure_empty[simp]: "measure M {} = 0"
  1547   unfolding measure_def by (simp add: zero_ennreal.rep_eq)
  1549 lemma emeasure_eq_ennreal_measure:
  1550   "emeasure M A \<noteq> top \<Longrightarrow> emeasure M A = ennreal (measure M A)"
  1551   by (cases "emeasure M A" rule: ennreal_cases) (auto simp: measure_def)
  1553 lemma measure_zero_top: "emeasure M A = top \<Longrightarrow> measure M A = 0"
  1554   by (simp add: measure_def enn2ereal_top)
  1556 lemma measure_eq_emeasure_eq_ennreal: "0 \<le> x \<Longrightarrow> emeasure M A = ennreal x \<Longrightarrow> measure M A = x"
  1557   using emeasure_eq_ennreal_measure[of M A]
  1558   by (cases "A \<in> M") (auto simp: measure_notin_sets emeasure_notin_sets)
  1560 lemma enn2real_plus:"a < top \<Longrightarrow> b < top \<Longrightarrow> enn2real (a + b) = enn2real a + enn2real b"
  1561   by (simp add: enn2real_def plus_ennreal.rep_eq real_of_ereal_add less_top
  1562            del: real_of_ereal_enn2ereal)
  1564 lemma measure_eq_AE:
  1565   assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  1566   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  1567   shows "measure M A = measure M B"
  1568   using assms emeasure_eq_AE[OF assms] by (simp add: measure_def)
  1570 lemma measure_Union:
  1571   "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>
  1572     measure M (A \<union> B) = measure M A + measure M B"
  1573   by (simp add: measure_def plus_emeasure[symmetric] enn2real_plus less_top)
  1575 lemma disjoint_family_on_insert:
  1576   "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"
  1577   by (fastforce simp: disjoint_family_on_def)
  1579 lemma measure_finite_Union:
  1580   "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>
  1581     measure M (\<Union>i\<in>S. A i) = (\<Sum>i\<in>S. measure M (A i))"
  1582   by (induction S rule: finite_induct)
  1583      (auto simp: disjoint_family_on_insert measure_Union sum_emeasure[symmetric] sets.countable_UN'[OF countable_finite])
  1585 lemma measure_Diff:
  1586   assumes finite: "emeasure M A \<noteq> \<infinity>"
  1587   and measurable: "A \<in> sets M" "B \<in> sets M" "B \<subseteq> A"
  1588   shows "measure M (A - B) = measure M A - measure M B"
  1589 proof -
  1590   have "emeasure M (A - B) \<le> emeasure M A" "emeasure M B \<le> emeasure M A"
  1591     using measurable by (auto intro!: emeasure_mono)
  1592   hence "measure M ((A - B) \<union> B) = measure M (A - B) + measure M B"
  1593     using measurable finite by (rule_tac measure_Union) (auto simp: top_unique)
  1594   thus ?thesis using \<open>B \<subseteq> A\<close> by (auto simp: Un_absorb2)
  1595 qed
  1597 lemma measure_UNION:
  1598   assumes measurable: "range A \<subseteq> sets M" "disjoint_family A"
  1599   assumes finite: "emeasure M (\<Union>i. A i) \<noteq> \<infinity>"
  1600   shows "(\<lambda>i. measure M (A i)) sums (measure M (\<Union>i. A i))"
  1601 proof -
  1602   have "(\<lambda>i. emeasure M (A i)) sums (emeasure M (\<Union>i. A i))"
  1603     unfolding suminf_emeasure[OF measurable, symmetric] by (simp add: summable_sums)
  1604   moreover
  1605   { fix i
  1606     have "emeasure M (A i) \<le> emeasure M (\<Union>i. A i)"
  1607       using measurable by (auto intro!: emeasure_mono)
  1608     then have "emeasure M (A i) = ennreal ((measure M (A i)))"
  1609       using finite by (intro emeasure_eq_ennreal_measure) (auto simp: top_unique) }
  1610   ultimately show ?thesis using finite
  1611     by (subst (asm) (2) emeasure_eq_ennreal_measure) simp_all
  1612 qed
  1614 lemma measure_subadditive:
  1615   assumes measurable: "A \<in> sets M" "B \<in> sets M"
  1616   and fin: "emeasure M A \<noteq> \<infinity>" "emeasure M B \<noteq> \<infinity>"
  1617   shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  1618 proof -
  1619   have "emeasure M (A \<union> B) \<noteq> \<infinity>"
  1620     using emeasure_subadditive[OF measurable] fin by (auto simp: top_unique)
  1621   then show "(measure M (A \<union> B)) \<le> (measure M A) + (measure M B)"
  1622     using emeasure_subadditive[OF measurable] fin
  1623     apply simp
  1624     apply (subst (asm) (2 3 4) emeasure_eq_ennreal_measure)
  1625     apply (auto simp flip: ennreal_plus)
  1626     done
  1627 qed
  1629 lemma measure_subadditive_finite:
  1630   assumes A: "finite I" "A`I \<subseteq> sets M" and fin: "\<And>i. i \<in> I \<Longrightarrow> emeasure M (A i) \<noteq> \<infinity>"
  1631   shows "measure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. measure M (A i))"
  1632 proof -
  1633   { have "emeasure M (\<Union>i\<in>I. A i) \<le> (\<Sum>i\<in>I. emeasure M (A i))"
  1634       using emeasure_subadditive_finite[OF A] .
  1635     also have "\<dots> < \<infinity>"
  1636       using fin by (simp add: less_top A)
  1637     finally have "emeasure M (\<Union>i\<in>I. A i) \<noteq> top" by simp }
  1638   note * = this
  1639   show ?thesis
  1640     using emeasure_subadditive_finite[OF A] fin
  1641     unfolding emeasure_eq_ennreal_measure[OF *]
  1642     by (simp_all add: sum_nonneg emeasure_eq_ennreal_measure)
  1643 qed
  1645 lemma measure_subadditive_countably:
  1646   assumes A: "range A \<subseteq> sets M" and fin: "(\<Sum>i. emeasure M (A i)) \<noteq> \<infinity>"
  1647   shows "measure M (\<Union>i. A i) \<le> (\<Sum>i. measure M (A i))"
  1648 proof -
  1649   from fin have **: "\<And>i. emeasure M (A i) \<noteq> top"
  1650     using ennreal_suminf_lessD[of "\<lambda>i. emeasure M (A i)"] by (simp add: less_top)
  1651   { have "emeasure M (\<Union>i. A i) \<le> (\<Sum>i. emeasure M (A i))"
  1652       using emeasure_subadditive_countably[OF A] .
  1653     also have "\<dots> < \<infinity>"
  1654       using fin by (simp add: less_top)
  1655     finally have "emeasure M (\<Union>i. A i) \<noteq> top" by simp }
  1656   then have "ennreal (measure M (\<Union>i. A i)) = emeasure M (\<Union>i. A i)"
  1657     by (rule emeasure_eq_ennreal_measure[symmetric])
  1658   also have "\<dots> \<le> (\<Sum>i. emeasure M (A i))"
  1659     using emeasure_subadditive_countably[OF A] .
  1660   also have "\<dots> = ennreal (\<Sum>i. measure M (A i))"
  1661     using fin unfolding emeasure_eq_ennreal_measure[OF **]
  1662     by (subst suminf_ennreal) (auto simp: **)
  1663   finally show ?thesis
  1664     apply (rule ennreal_le_iff[THEN iffD1, rotated])
  1665     apply (intro suminf_nonneg allI measure_nonneg summable_suminf_not_top)
  1666     using fin
  1667     apply (simp add: emeasure_eq_ennreal_measure[OF **])
  1668     done
  1669 qed
  1671 lemma measure_Un_null_set: "A \<in> sets M \<Longrightarrow> B \<in> null_sets M \<Longrightarrow> measure M (A \<union> B) = measure M A"
  1672   by (simp add: measure_def emeasure_Un_null_set)
  1674 lemma measure_Diff_null_set: "A \<in> sets M \<Longrightarrow> B \<in> null_sets M \<Longrightarrow> measure M (A - B) = measure M A"
  1675   by (simp add: measure_def emeasure_Diff_null_set)
  1677 lemma measure_eq_sum_singleton:
  1678   "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>
  1679     measure M S = (\<Sum>x\<in>S. measure M {x})"
  1680   using emeasure_eq_sum_singleton[of S M]
  1681   by (intro measure_eq_emeasure_eq_ennreal) (auto simp: sum_nonneg emeasure_eq_ennreal_measure)
  1683 lemma Lim_measure_incseq:
  1684   assumes A: "range A \<subseteq> sets M" "incseq A" and fin: "emeasure M (\<Union>i. A i) \<noteq> \<infinity>"
  1685   shows "(\<lambda>i. measure M (A i)) \<longlonglongrightarrow> measure M (\<Union>i. A i)"
  1686 proof (rule tendsto_ennrealD)
  1687   have "ennreal (measure M (\<Union>i. A i)) = emeasure M (\<Union>i. A i)"
  1688     using fin by (auto simp: emeasure_eq_ennreal_measure)
  1689   moreover have "ennreal (measure M (A i)) = emeasure M (A i)" for i
  1690     using assms emeasure_mono[of "A _" "\<Union>i. A i" M]
  1691     by (intro emeasure_eq_ennreal_measure[symmetric]) (auto simp: less_top UN_upper intro: le_less_trans)
  1692   ultimately show "(\<lambda>x. ennreal (measure M (A x))) \<longlonglongrightarrow> ennreal (measure M (\<Union>i. A i))"
  1693     using A by (auto intro!: Lim_emeasure_incseq)
  1694 qed auto
  1696 lemma Lim_measure_decseq:
  1697   assumes A: "range A \<subseteq> sets M" "decseq A" and fin: "\<And>i. emeasure M (A i) \<noteq> \<infinity>"
  1698   shows "(\<lambda>n. measure M (A n)) \<longlonglongrightarrow> measure M (\<Inter>i. A i)"
  1699 proof (rule tendsto_ennrealD)
  1700   have "ennreal (measure M (\<Inter>i. A i)) = emeasure M (\<Inter>i. A i)"
  1701     using fin[of 0] A emeasure_mono[of "\<Inter>i. A i" "A 0" M]
  1702     by (auto intro!: emeasure_eq_ennreal_measure[symmetric] simp: INT_lower less_top intro: le_less_trans)
  1703   moreover have "ennreal (measure M (A i)) = emeasure M (A i)" for i
  1704     using A fin[of i] by (intro emeasure_eq_ennreal_measure[symmetric]) auto
  1705   ultimately show "(\<lambda>x. ennreal (measure M (A x))) \<longlonglongrightarrow> ennreal (measure M (\<Inter>i. A i))"
  1706     using fin A by (auto intro!: Lim_emeasure_decseq)
  1707 qed auto
  1709 subsection \<open>Set of measurable sets with finite measure\<close>
  1711 definition%important fmeasurable :: "'a measure \<Rightarrow> 'a set set"
  1712 where
  1713   "fmeasurable M = {A\<in>sets M. emeasure M A < \<infinity>}"
  1715 lemma fmeasurableD[dest, measurable_dest]: "A \<in> fmeasurable M \<Longrightarrow> A \<in> sets M"
  1716   by (auto simp: fmeasurable_def)
  1718 lemma fmeasurableD2: "A \<in> fmeasurable M \<Longrightarrow> emeasure M A \<noteq> top"
  1719   by (auto simp: fmeasurable_def)
  1721 lemma fmeasurableI: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow> A \<in> fmeasurable M"
  1722   by (auto simp: fmeasurable_def)
  1724 lemma fmeasurableI_null_sets: "A \<in> null_sets M \<Longrightarrow> A \<in> fmeasurable M"
  1725   by (auto simp: fmeasurable_def)
  1727 lemma fmeasurableI2: "A \<in> fmeasurable M \<Longrightarrow> B \<subseteq> A \<Longrightarrow> B \<in> sets M \<Longrightarrow> B \<in> fmeasurable M"
  1728   using emeasure_mono[of B A M] by (auto simp: fmeasurable_def)
  1730 lemma measure_mono_fmeasurable:
  1731   "A \<subseteq> B \<Longrightarrow> A \<in> sets M \<Longrightarrow> B \<in> fmeasurable M \<Longrightarrow> measure M A \<le> measure M B"
  1732   by (auto simp: measure_def fmeasurable_def intro!: emeasure_mono enn2real_mono)
  1734 lemma emeasure_eq_measure2: "A \<in> fmeasurable M \<Longrightarrow> emeasure M A = measure M A"
  1735   by (simp add: emeasure_eq_ennreal_measure fmeasurable_def less_top)
  1737 interpretation fmeasurable: ring_of_sets "space M" "fmeasurable M"
  1738 proof (rule ring_of_setsI)
  1739   show "fmeasurable M \<subseteq> Pow (space M)" "{} \<in> fmeasurable M"
  1740     by (auto simp: fmeasurable_def dest: sets.sets_into_space)
  1741   fix a b assume *: "a \<in> fmeasurable M" "b \<in> fmeasurable M"
  1742   then have "emeasure M (a \<union> b) \<le> emeasure M a + emeasure M b"
  1743     by (intro emeasure_subadditive) auto
  1744   also have "\<dots> < top"
  1745     using * by (auto simp: fmeasurable_def)
  1746   finally show  "a \<union> b \<in> fmeasurable M"
  1747     using * by (auto intro: fmeasurableI)
  1748   show "a - b \<in> fmeasurable M"
  1749     using emeasure_mono[of "a - b" a M] * by (auto simp: fmeasurable_def)
  1750 qed
  1752 subsection\<open>Measurable sets formed by unions and intersections\<close>
  1754 lemma fmeasurable_Diff: "A \<in> fmeasurable M \<Longrightarrow> B \<in> sets M \<Longrightarrow> A - B \<in> fmeasurable M"
  1755   using fmeasurableI2[of A M "A - B"] by auto
  1757 lemma fmeasurable_Int_fmeasurable:
  1758    "\<lbrakk>S \<in> fmeasurable M; T \<in> sets M\<rbrakk> \<Longrightarrow> (S \<inter> T) \<in> fmeasurable M"
  1759   by (meson fmeasurableD fmeasurableI2 inf_le1 sets.Int)
  1761 lemma fmeasurable_UN:
  1762   assumes "countable I" "\<And>i. i \<in> I \<Longrightarrow> F i \<subseteq> A" "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M" "A \<in> fmeasurable M"
  1763   shows "(\<Union>i\<in>I. F i) \<in> fmeasurable M"
  1764 proof (rule fmeasurableI2)
  1765   show "A \<in> fmeasurable M" "(\<Union>i\<in>I. F i) \<subseteq> A" using assms by auto
  1766   show "(\<Union>i\<in>I. F i) \<in> sets M"
  1767     using assms by (intro sets.countable_UN') auto
  1768 qed
  1770 lemma fmeasurable_INT:
  1771   assumes "countable I" "i \<in> I" "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M" "F i \<in> fmeasurable M"
  1772   shows "(\<Inter>i\<in>I. F i) \<in> fmeasurable M"
  1773 proof (rule fmeasurableI2)
  1774   show "F i \<in> fmeasurable M" "(\<Inter>i\<in>I. F i) \<subseteq> F i"
  1775     using assms by auto
  1776   show "(\<Inter>i\<in>I. F i) \<in> sets M"
  1777     using assms by (intro sets.countable_INT') auto
  1778 qed
  1780 lemma measurable_measure_Diff:
  1781   assumes "A \<in> fmeasurable M" "B \<in> sets M" "B \<subseteq> A"
  1782   shows "measure M (A - B) = measure M A - measure M B"
  1783   by (simp add: assms fmeasurableD fmeasurableD2 measure_Diff)
  1785 lemma measurable_Un_null_set:
  1786   assumes "B \<in> null_sets M"
  1787   shows "(A \<union> B \<in> fmeasurable M \<and> A \<in> sets M) \<longleftrightarrow> A \<in> fmeasurable M"
  1788   using assms  by (fastforce simp add: fmeasurable.Un fmeasurableI_null_sets intro: fmeasurableI2)
  1790 lemma measurable_Diff_null_set:
  1791   assumes "B \<in> null_sets M"
  1792   shows "(A - B) \<in> fmeasurable M \<and> A \<in> sets M \<longleftrightarrow> A \<in> fmeasurable M"
  1793   using assms
  1794   by (metis Un_Diff_cancel2 fmeasurable.Diff fmeasurableD fmeasurableI_null_sets measurable_Un_null_set)
  1796 lemma fmeasurable_Diff_D:
  1797     assumes m: "T - S \<in> fmeasurable M" "S \<in> fmeasurable M" and sub: "S \<subseteq> T"
  1798     shows "T \<in> fmeasurable M"
  1799 proof -
  1800   have "T = S \<union> (T - S)"
  1801     using assms by blast
  1802   then show ?thesis
  1803     by (metis m fmeasurable.Un)
  1804 qed
  1806 lemma measure_Un2:
  1807   "A \<in> fmeasurable M \<Longrightarrow> B \<in> fmeasurable M \<Longrightarrow> measure M (A \<union> B) = measure M A + measure M (B - A)"
  1808   using measure_Union[of M A "B - A"] by (auto simp: fmeasurableD2 fmeasurable.Diff)
  1810 lemma measure_Un3:
  1811   assumes "A \<in> fmeasurable M" "B \<in> fmeasurable M"
  1812   shows "measure M (A \<union> B) = measure M A + measure M B - measure M (A \<inter> B)"
  1813 proof -
  1814   have "measure M (A \<union> B) = measure M A + measure M (B - A)"
  1815     using assms by (rule measure_Un2)
  1816   also have "B - A = B - (A \<inter> B)"
  1817     by auto
  1818   also have "measure M (B - (A \<inter> B)) = measure M B - measure M (A \<inter> B)"
  1819     using assms by (intro measure_Diff) (auto simp: fmeasurable_def)
  1820   finally show ?thesis
  1821     by simp
  1822 qed
  1824 lemma measure_Un_AE:
  1825   "AE x in M. x \<notin> A \<or> x \<notin> B \<Longrightarrow> A \<in> fmeasurable M \<Longrightarrow> B \<in> fmeasurable M \<Longrightarrow>
  1826   measure M (A \<union> B) = measure M A + measure M B"
  1827   by (subst measure_Un2) (auto intro!: measure_eq_AE)
  1829 lemma measure_UNION_AE:
  1830   assumes I: "finite I"
  1831   shows "(\<And>i. i \<in> I \<Longrightarrow> F i \<in> fmeasurable M) \<Longrightarrow> pairwise (\<lambda>i j. AE x in M. x \<notin> F i \<or> x \<notin> F j) I \<Longrightarrow>
  1832     measure M (\<Union>i\<in>I. F i) = (\<Sum>i\<in>I. measure M (F i))"
  1833   unfolding AE_pairwise[OF countable_finite, OF I]
  1834   using I
  1835 proof (induction I rule: finite_induct)
  1836   case (insert x I)
  1837   have "measure M (F x \<union> \<Union>(F ` I)) = measure M (F x) + measure M (\<Union>(F ` I))"
  1838     by (rule measure_Un_AE) (use insert in \<open>auto simp: pairwise_insert\<close>)
  1839     with insert show ?case
  1840       by (simp add: pairwise_insert )
  1841 qed simp
  1843 lemma measure_UNION':
  1844   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i \<in> fmeasurable M) \<Longrightarrow> pairwise (\<lambda>i j. disjnt (F i) (F j)) I \<Longrightarrow>
  1845     measure M (\<Union>i\<in>I. F i) = (\<Sum>i\<in>I. measure M (F i))"
  1846   by (intro measure_UNION_AE) (auto simp: disjnt_def elim!: pairwise_mono intro!: always_eventually)
  1848 lemma measure_Union_AE:
  1849   "finite F \<Longrightarrow> (\<And>S. S \<in> F \<Longrightarrow> S \<in> fmeasurable M) \<Longrightarrow> pairwise (\<lambda>S T. AE x in M. x \<notin> S \<or> x \<notin> T) F \<Longrightarrow>
  1850     measure M (\<Union>F) = (\<Sum>S\<in>F. measure M S)"
  1851   using measure_UNION_AE[of F "\<lambda>x. x" M] by simp
  1853 lemma measure_Union':
  1854   "finite F \<Longrightarrow> (\<And>S. S \<in> F \<Longrightarrow> S \<in> fmeasurable M) \<Longrightarrow> pairwise disjnt F \<Longrightarrow> measure M (\<Union>F) = (\<Sum>S\<in>F. measure M S)"
  1855   using measure_UNION'[of F "\<lambda>x. x" M] by simp
  1857 lemma measure_Un_le:
  1858   assumes "A \<in> sets M" "B \<in> sets M" shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  1859 proof cases
  1860   assume "A \<in> fmeasurable M \<and> B \<in> fmeasurable M"
  1861   with measure_subadditive[of A M B] assms show ?thesis
  1862     by (auto simp: fmeasurableD2)
  1863 next
  1864   assume "\<not> (A \<in> fmeasurable M \<and> B \<in> fmeasurable M)"
  1865   then have "A \<union> B \<notin> fmeasurable M"
  1866     using fmeasurableI2[of "A \<union> B" M A] fmeasurableI2[of "A \<union> B" M B] assms by auto
  1867   with assms show ?thesis
  1868     by (auto simp: fmeasurable_def measure_def less_top[symmetric])
  1869 qed
  1871 lemma measure_UNION_le:
  1872   "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets M) \<Longrightarrow> measure M (\<Union>i\<in>I. F i) \<le> (\<Sum>i\<in>I. measure M (F i))"
  1873 proof (induction I rule: finite_induct)
  1874   case (insert i I)
  1875   then have "measure M (\<Union>i\<in>insert i I. F i) \<le> measure M (F i) + measure M (\<Union>i\<in>I. F i)"
  1876     by (auto intro!: measure_Un_le)
  1877   also have "measure M (\<Union>i\<in>I. F i) \<le> (\<Sum>i\<in>I. measure M (F i))"
  1878     using insert by auto
  1879   finally show ?case
  1880     using insert by simp
  1881 qed simp
  1883 lemma measure_Union_le:
  1884   "finite F \<Longrightarrow> (\<And>S. S \<in> F \<Longrightarrow> S \<in> sets M) \<Longrightarrow> measure M (\<Union>F) \<le> (\<Sum>S\<in>F. measure M S)"
  1885   using measure_UNION_le[of F "\<lambda>x. x" M] by simp
  1887 text\<open>Version for indexed union over a countable set\<close>
  1888 lemma
  1889   assumes "countable I" and I: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> fmeasurable M"
  1890     and bound: "\<And>I'. I' \<subseteq> I \<Longrightarrow> finite I' \<Longrightarrow> measure M (\<Union>i\<in>I'. A i) \<le> B"
  1891   shows fmeasurable_UN_bound: "(\<Union>i\<in>I. A i) \<in> fmeasurable M" (is ?fm)
  1892     and measure_UN_bound: "measure M (\<Union>i\<in>I. A i) \<le> B" (is ?m)
  1893 proof -
  1894   have "B \<ge> 0"
  1895     using bound by force
  1896   have "?fm \<and> ?m"
  1897   proof cases
  1898     assume "I = {}"
  1899     with \<open>B \<ge> 0\<close> show ?thesis
  1900       by simp
  1901   next
  1902     assume "I \<noteq> {}"
  1903     have "(\<Union>i\<in>I. A i) = (\<Union>i. (\<Union>n\<le>i. A (from_nat_into I n)))"
  1904       by (subst range_from_nat_into[symmetric, OF \<open>I \<noteq> {}\<close> \<open>countable I\<close>]) auto
  1905     then have "emeasure M (\<Union>i\<in>I. A i) = emeasure M (\<Union>i. (\<Union>n\<le>i. A (from_nat_into I n)))" by simp
  1906     also have "\<dots> = (SUP i. emeasure M (\<Union>n\<le>i. A (from_nat_into I n)))"
  1907       using I \<open>I \<noteq> {}\<close>[THEN from_nat_into] by (intro SUP_emeasure_incseq[symmetric]) (fastforce simp: incseq_Suc_iff)+
  1908     also have "\<dots> \<le> B"
  1909     proof (intro SUP_least)
  1910       fix i :: nat
  1911       have "emeasure M (\<Union>n\<le>i. A (from_nat_into I n)) = measure M (\<Union>n\<le>i. A (from_nat_into I n))"
  1912         using I \<open>I \<noteq> {}\<close>[THEN from_nat_into] by (intro emeasure_eq_measure2 fmeasurable.finite_UN) auto
  1913       also have "\<dots> = measure M (\<Union>n\<in>from_nat_into I ` {..i}. A n)"
  1914         by simp
  1915       also have "\<dots> \<le> B"
  1916         by (intro ennreal_leI bound) (auto intro:  from_nat_into[OF \<open>I \<noteq> {}\<close>])
  1917       finally show "emeasure M (\<Union>n\<le>i. A (from_nat_into I n)) \<le> ennreal B" .
  1918     qed
  1919     finally have *: "emeasure M (\<Union>i\<in>I. A i) \<le> B" .
  1920     then have ?fm
  1921       using I \<open>countable I\<close> by (intro fmeasurableI conjI) (auto simp: less_top[symmetric] top_unique)
  1922     with * \<open>0\<le>B\<close> show ?thesis
  1923       by (simp add: emeasure_eq_measure2)
  1924   qed
  1925   then show ?fm ?m by auto
  1926 qed
  1928 text\<open>Version for big union of a countable set\<close>
  1929 lemma
  1930   assumes "countable \<D>"
  1931     and meas: "\<And>D. D \<in> \<D> \<Longrightarrow> D \<in> fmeasurable M"
  1932     and bound:  "\<And>\<E>. \<lbrakk>\<E> \<subseteq> \<D>; finite \<E>\<rbrakk> \<Longrightarrow> measure M (\<Union>\<E>) \<le> B"
  1933  shows fmeasurable_Union_bound: "\<Union>\<D> \<in> fmeasurable M"  (is ?fm)
  1934     and measure_Union_bound: "measure M (\<Union>\<D>) \<le> B"     (is ?m)
  1935 proof -
  1936   have "B \<ge> 0"
  1937     using bound by force
  1938   have "?fm \<and> ?m"
  1939   proof (cases "\<D> = {}")
  1940     case True
  1941     with \<open>B \<ge> 0\<close> show ?thesis
  1942       by auto
  1943   next
  1944     case False
  1945     then obtain D :: "nat \<Rightarrow> 'a set" where D: "\<D> = range D"
  1946       using \<open>countable \<D>\<close> uncountable_def by force
  1947       have 1: "\<And>i. D i \<in> fmeasurable M"
  1948         by (simp add: D meas)
  1949       have 2: "\<And>I'. finite I' \<Longrightarrow> measure M (\<Union>x\<in>I'. D x) \<le> B"
  1950         by (simp add: D bound image_subset_iff)
  1951       show ?thesis
  1952         unfolding D
  1953         by (intro conjI fmeasurable_UN_bound [OF _ 1 2] measure_UN_bound [OF _ 1 2]) auto
  1954     qed
  1955     then show ?fm ?m by auto
  1956 qed
  1958 text\<open>Version for indexed union over the type of naturals\<close>
  1959 lemma
  1960   fixes S :: "nat \<Rightarrow> 'a set"
  1961   assumes S: "\<And>i. S i \<in> fmeasurable M" and B: "\<And>n. measure M (\<Union>i\<le>n. S i) \<le> B"
  1962   shows fmeasurable_countable_Union: "(\<Union>i. S i) \<in> fmeasurable M"
  1963     and measure_countable_Union_le: "measure M (\<Union>i. S i) \<le> B"
  1964 proof -
  1965   have mB: "measure M (\<Union>i\<in>I. S i) \<le> B" if "finite I" for I
  1966   proof -
  1967     have "(\<Union>i\<in>I. S i) \<subseteq> (\<Union>i\<le>Max I. S i)"
  1968       using Max_ge that by force
  1969     then have "measure M (\<Union>i\<in>I. S i) \<le> measure M (\<Union>i \<le> Max I. S i)"
  1970       by (rule measure_mono_fmeasurable) (use S in \<open>blast+\<close>)
  1971     then show ?thesis
  1972       using B order_trans by blast
  1973   qed
  1974   show "(\<Union>i. S i) \<in> fmeasurable M"
  1975     by (auto intro: fmeasurable_UN_bound [OF _ S mB])
  1976   show "measure M (\<Union>n. S n) \<le> B"
  1977     by (auto intro: measure_UN_bound [OF _ S mB])
  1978 qed
  1980 lemma measure_diff_le_measure_setdiff:
  1981   assumes "S \<in> fmeasurable M" "T \<in> fmeasurable M"
  1982   shows "measure M S - measure M T \<le> measure M (S - T)"
  1983 proof -
  1984   have "measure M S \<le> measure M ((S - T) \<union> T)"
  1985     by (simp add: assms fmeasurable.Un fmeasurableD measure_mono_fmeasurable)
  1986   also have "\<dots> \<le> measure M (S - T) + measure M T"
  1987     using assms by (blast intro: measure_Un_le)
  1988   finally show ?thesis
  1989     by (simp add: algebra_simps)
  1990 qed
  1992 lemma suminf_exist_split2:
  1993   fixes f :: "nat \<Rightarrow> 'a::real_normed_vector"
  1994   assumes "summable f"
  1995   shows "(\<lambda>n. (\<Sum>k. f(k+n))) \<longlonglongrightarrow> 0"
  1996 by (subst lim_sequentially, auto simp add: dist_norm suminf_exist_split[OF _ assms])
  1998 lemma emeasure_union_summable:
  1999   assumes [measurable]: "\<And>n. A n \<in> sets M"
  2000     and "\<And>n. emeasure M (A n) < \<infinity>" "summable (\<lambda>n. measure M (A n))"
  2001   shows "emeasure M (\<Union>n. A n) < \<infinity>" "emeasure M (\<Union>n. A n) \<le> (\<Sum>n. measure M (A n))"
  2002 proof -
  2003   define B where "B = (\<lambda>N. (\<Union>n\<in>{..<N}. A n))"
  2004   have [measurable]: "B N \<in> sets M" for N unfolding B_def by auto
  2005   have "(\<lambda>N. emeasure M (B N)) \<longlonglongrightarrow> emeasure M (\<Union>N. B N)"
  2006     apply (rule Lim_emeasure_incseq) unfolding B_def by (auto simp add: SUP_subset_mono incseq_def)
  2007   moreover have "emeasure M (B N) \<le> ennreal (\<Sum>n. measure M (A n))" for N
  2008   proof -
  2009     have *: "(\<Sum>n\<in>{..<N}. measure M (A n)) \<le> (\<Sum>n. measure M (A n))"
  2010       using assms(3) measure_nonneg sum_le_suminf by blast
  2012     have "emeasure M (B N) \<le> (\<Sum>n\<in>{..<N}. emeasure M (A n))"
  2013       unfolding B_def by (rule emeasure_subadditive_finite, auto)
  2014     also have "... = (\<Sum>n\<in>{..<N}. ennreal(measure M (A n)))"
  2015       using assms(2) by (simp add: emeasure_eq_ennreal_measure less_top)
  2016     also have "... = ennreal (\<Sum>n\<in>{..<N}. measure M (A n))"
  2017       by auto
  2018     also have "... \<le> ennreal (\<Sum>n. measure M (A n))"
  2019       using * by (auto simp: ennreal_leI)
  2020     finally show ?thesis by simp
  2021   qed
  2022   ultimately have "emeasure M (\<Union>N. B N) \<le> ennreal (\<Sum>n. measure M (A n))"
  2023     by (simp add: Lim_bounded)
  2024   then show "emeasure M (\<Union>n. A n) \<le> (\<Sum>n. measure M (A n))"
  2025     unfolding B_def by (metis UN_UN_flatten UN_lessThan_UNIV)
  2026   then show "emeasure M (\<Union>n. A n) < \<infinity>"
  2027     by (auto simp: less_top[symmetric] top_unique)
  2028 qed
  2030 lemma borel_cantelli_limsup1:
  2031   assumes [measurable]: "\<And>n. A n \<in> sets M"
  2032     and "\<And>n. emeasure M (A n) < \<infinity>" "summable (\<lambda>n. measure M (A n))"
  2033   shows "limsup A \<in> null_sets M"
  2034 proof -
  2035   have "emeasure M (limsup A) \<le> 0"
  2036   proof (rule LIMSEQ_le_const)
  2037     have "(\<lambda>n. (\<Sum>k. measure M (A (k+n)))) \<longlonglongrightarrow> 0" by (rule suminf_exist_split2[OF assms(3)])
  2038     then show "(\<lambda>n. ennreal (\<Sum>k. measure M (A (k+n)))) \<longlonglongrightarrow> 0"
  2039       unfolding ennreal_0[symmetric] by (intro tendsto_ennrealI)
  2040     have "emeasure M (limsup A) \<le> (\<Sum>k. measure M (A (k+n)))" for n
  2041     proof -
  2042       have I: "(\<Union>k\<in>{n..}. A k) = (\<Union>k. A (k+n))" by (auto, metis le_add_diff_inverse2, fastforce)
  2043       have "emeasure M (limsup A) \<le> emeasure M (\<Union>k\<in>{n..}. A k)"
  2044         by (rule emeasure_mono, auto simp add: limsup_INF_SUP)
  2045       also have "... = emeasure M (\<Union>k. A (k+n))"
  2046         using I by auto
  2047       also have "... \<le> (\<Sum>k. measure M (A (k+n)))"
  2048         apply (rule emeasure_union_summable)
  2049         using assms summable_ignore_initial_segment[OF assms(3), of n] by auto
  2050       finally show ?thesis by simp
  2051     qed
  2052     then show "\<exists>N. \<forall>n\<ge>N. emeasure M (limsup A) \<le> (\<Sum>k. measure M (A (k+n)))"
  2053       by auto
  2054   qed
  2055   then show ?thesis using assms(1) measurable_limsup by auto
  2056 qed
  2058 lemma borel_cantelli_AE1:
  2059   assumes [measurable]: "\<And>n. A n \<in> sets M"
  2060     and "\<And>n. emeasure M (A n) < \<infinity>" "summable (\<lambda>n. measure M (A n))"
  2061   shows "AE x in M. eventually (\<lambda>n. x \<in> space M - A n) sequentially"
  2062 proof -
  2063   have "AE x in M. x \<notin> limsup A"
  2064     using borel_cantelli_limsup1[OF assms] unfolding eventually_ae_filter by auto
  2065   moreover
  2066   {
  2067     fix x assume "x \<notin> limsup A"
  2068     then obtain N where "x \<notin> (\<Union>n\<in>{N..}. A n)" unfolding limsup_INF_SUP by blast
  2069     then have "eventually (\<lambda>n. x \<notin> A n) sequentially" using eventually_sequentially by auto
  2070   }
  2071   ultimately show ?thesis by auto
  2072 qed
  2074 subsection \<open>Measure spaces with @{term "emeasure M (space M) < \<infinity>"}\<close>
  2076 locale%important finite_measure = sigma_finite_measure M for M +
  2077   assumes finite_emeasure_space: "emeasure M (space M) \<noteq> top"
  2079 lemma finite_measureI[Pure.intro!]:
  2080   "emeasure M (space M) \<noteq> \<infinity> \<Longrightarrow> finite_measure M"
  2081   proof qed (auto intro!: exI[of _ "{space M}"])
  2083 lemma (in finite_measure) emeasure_finite[simp, intro]: "emeasure M A \<noteq> top"
  2084   using finite_emeasure_space emeasure_space[of M A] by (auto simp: top_unique)
  2086 lemma (in finite_measure) fmeasurable_eq_sets: "fmeasurable M = sets M"
  2087   by (auto simp: fmeasurable_def less_top[symmetric])
  2089 lemma (in finite_measure) emeasure_eq_measure: "emeasure M A = ennreal (measure M A)"
  2090   by (intro emeasure_eq_ennreal_measure) simp
  2092 lemma (in finite_measure) emeasure_real: "\<exists>r. 0 \<le> r \<and> emeasure M A = ennreal r"
  2093   using emeasure_finite[of A] by (cases "emeasure M A" rule: ennreal_cases) auto
  2095 lemma (in finite_measure) bounded_measure: "measure M A \<le> measure M (space M)"
  2096   using emeasure_space[of M A] emeasure_real[of A] emeasure_real[of "space M"] by (auto simp: measure_def)
  2098 lemma (in finite_measure) finite_measure_Diff:
  2099   assumes sets: "A \<in> sets M" "B \<in> sets M" and "B \<subseteq> A"
  2100   shows "measure M (A - B) = measure M A - measure M B"
  2101   using measure_Diff[OF _ assms] by simp
  2103 lemma (in finite_measure) finite_measure_Union:
  2104   assumes sets: "A \<in> sets M" "B \<in> sets M" and "A \<inter> B = {}"
  2105   shows "measure M (A \<union> B) = measure M A + measure M B"
  2106   using measure_Union[OF _ _ assms] by simp
  2108 lemma (in finite_measure) finite_measure_finite_Union:
  2109   assumes measurable: "finite S" "A`S \<subseteq> sets M" "disjoint_family_on A S"
  2110   shows "measure M (\<Union>i\<in>S. A i) = (\<Sum>i\<in>S. measure M (A i))"
  2111   using measure_finite_Union[OF assms] by simp
  2113 lemma (in finite_measure) finite_measure_UNION:
  2114   assumes A: "range A \<subseteq> sets M" "disjoint_family A"
  2115   shows "(\<lambda>i. measure M (A i)) sums (measure M (\<Union>i. A i))"
  2116   using measure_UNION[OF A] by simp
  2118 lemma (in finite_measure) finite_measure_mono:
  2119   assumes "A \<subseteq> B" "B \<in> sets M" shows "measure M A \<le> measure M B"
  2120   using emeasure_mono[OF assms] emeasure_real[of A] emeasure_real[of B] by (auto simp: measure_def)
  2122 lemma (in finite_measure) finite_measure_subadditive:
  2123   assumes m: "A \<in> sets M" "B \<in> sets M"
  2124   shows "measure M (A \<union> B) \<le> measure M A + measure M B"
  2125   using measure_subadditive[OF m] by simp
  2127 lemma (in finite_measure) finite_measure_subadditive_finite:
  2128   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))"
  2129   using measure_subadditive_finite[OF assms] by simp
  2131 lemma (in finite_measure) finite_measure_subadditive_countably:
  2132   "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))"
  2133   by (rule measure_subadditive_countably)
  2134      (simp_all add: ennreal_suminf_neq_top emeasure_eq_measure)
  2136 lemma (in finite_measure) finite_measure_eq_sum_singleton:
  2137   assumes "finite S" and *: "\<And>x. x \<in> S \<Longrightarrow> {x} \<in> sets M"
  2138   shows "measure M S = (\<Sum>x\<in>S. measure M {x})"
  2139   using measure_eq_sum_singleton[OF assms] by simp
  2141 lemma (in finite_measure) finite_Lim_measure_incseq:
  2142   assumes A: "range A \<subseteq> sets M" "incseq A"
  2143   shows "(\<lambda>i. measure M (A i)) \<longlonglongrightarrow> measure M (\<Union>i. A i)"
  2144   using Lim_measure_incseq[OF A] by simp
  2146 lemma (in finite_measure) finite_Lim_measure_decseq:
  2147   assumes A: "range A \<subseteq> sets M" "decseq A"
  2148   shows "(\<lambda>n. measure M (A n)) \<longlonglongrightarrow> measure M (\<Inter>i. A i)"
  2149   using Lim_measure_decseq[OF A] by simp
  2151 lemma (in finite_measure) finite_measure_compl:
  2152   assumes S: "S \<in> sets M"
  2153   shows "measure M (space M - S) = measure M (space M) - measure M S"
  2154   using measure_Diff[OF _ S sets.sets_into_space] S by simp
  2156 lemma (in finite_measure) finite_measure_mono_AE:
  2157   assumes imp: "AE x in M. x \<in> A \<longrightarrow> x \<in> B" and B: "B \<in> sets M"
  2158   shows "measure M A \<le> measure M B"
  2159   using assms emeasure_mono_AE[OF imp B]
  2160   by (simp add: emeasure_eq_measure)
  2162 lemma (in finite_measure) finite_measure_eq_AE:
  2163   assumes iff: "AE x in M. x \<in> A \<longleftrightarrow> x \<in> B"
  2164   assumes A: "A \<in> sets M" and B: "B \<in> sets M"
  2165   shows "measure M A = measure M B"
  2166   using assms emeasure_eq_AE[OF assms] by (simp add: emeasure_eq_measure)
  2168 lemma (in finite_measure) measure_increasing: "increasing M (measure M)"
  2169   by (auto intro!: finite_measure_mono simp: increasing_def)
  2171 lemma (in finite_measure) measure_zero_union:
  2172   assumes "s \<in> sets M" "t \<in> sets M" "measure M t = 0"
  2173   shows "measure M (s \<union> t) = measure M s"
  2174 using assms
  2175 proof -
  2176   have "measure M (s \<union> t) \<le> measure M s"
  2177     using finite_measure_subadditive[of s t] assms by auto
  2178   moreover have "measure M (s \<union> t) \<ge> measure M s"
  2179     using assms by (blast intro: finite_measure_mono)
  2180   ultimately show ?thesis by simp
  2181 qed
  2183 lemma (in finite_measure) measure_eq_compl:
  2184   assumes "s \<in> sets M" "t \<in> sets M"
  2185   assumes "measure M (space M - s) = measure M (space M - t)"
  2186   shows "measure M s = measure M t"
  2187   using assms finite_measure_compl by auto
  2189 lemma (in finite_measure) measure_eq_bigunion_image:
  2190   assumes "range f \<subseteq> sets M" "range g \<subseteq> sets M"
  2191   assumes "disjoint_family f" "disjoint_family g"
  2192   assumes "\<And> n :: nat. measure M (f n) = measure M (g n)"
  2193   shows "measure M (\<Union>i. f i) = measure M (\<Union>i. g i)"
  2194 using assms
  2195 proof -
  2196   have a: "(\<lambda> i. measure M (f i)) sums (measure M (\<Union>i. f i))"
  2197     by (rule finite_measure_UNION[OF assms(1,3)])
  2198   have b: "(\<lambda> i. measure M (g i)) sums (measure M (\<Union>i. g i))"
  2199     by (rule finite_measure_UNION[OF assms(2,4)])
  2200   show ?thesis using sums_unique[OF b] sums_unique[OF a] assms by simp
  2201 qed
  2203 lemma (in finite_measure) measure_countably_zero:
  2204   assumes "range c \<subseteq> sets M"
  2205   assumes "\<And> i. measure M (c i) = 0"
  2206   shows "measure M (\<Union>i :: nat. c i) = 0"
  2207 proof (rule antisym)
  2208   show "measure M (\<Union>i :: nat. c i) \<le> 0"
  2209     using finite_measure_subadditive_countably[OF assms(1)] by (simp add: assms(2))
  2210 qed simp
  2212 lemma (in finite_measure) measure_space_inter:
  2213   assumes events:"s \<in> sets M" "t \<in> sets M"
  2214   assumes "measure M t = measure M (space M)"
  2215   shows "measure M (s \<inter> t) = measure M s"
  2216 proof -
  2217   have "measure M ((space M - s) \<union> (space M - t)) = measure M (space M - s)"
  2218     using events assms finite_measure_compl[of "t"] by (auto intro!: measure_zero_union)
  2219   also have "(space M - s) \<union> (space M - t) = space M - (s \<inter> t)"
  2220     by blast
  2221   finally show "measure M (s \<inter> t) = measure M s"
  2222     using events by (auto intro!: measure_eq_compl[of "s \<inter> t" s])
  2223 qed
  2225 lemma (in finite_measure) measure_equiprobable_finite_unions:
  2226   assumes s: "finite s" "\<And>x. x \<in> s \<Longrightarrow> {x} \<in> sets M"
  2227   assumes "\<And> x y. \<lbrakk>x \<in> s; y \<in> s\<rbrakk> \<Longrightarrow> measure M {x} = measure M {y}"
  2228   shows "measure M s = real (card s) * measure M {SOME x. x \<in> s}"
  2229 proof cases
  2230   assume "s \<noteq> {}"
  2231   then have "\<exists> x. x \<in> s" by blast
  2232   from someI_ex[OF this] assms
  2233   have prob_some: "\<And> x. x \<in> s \<Longrightarrow> measure M {x} = measure M {SOME y. y \<in> s}" by blast
  2234   have "measure M s = (\<Sum> x \<in> s. measure M {x})"
  2235     using finite_measure_eq_sum_singleton[OF s] by simp
  2236   also have "\<dots> = (\<Sum> x \<in> s. measure M {SOME y. y \<in> s})" using prob_some by auto
  2237   also have "\<dots> = real (card s) * measure M {(SOME x. x \<in> s)}"
  2238     using sum_constant assms by simp
  2239   finally show ?thesis by simp
  2240 qed simp
  2242 lemma (in finite_measure) measure_real_sum_image_fn:
  2243   assumes "e \<in> sets M"
  2244   assumes "\<And> x. x \<in> s \<Longrightarrow> e \<inter> f x \<in> sets M"
  2245   assumes "finite s"
  2246   assumes disjoint: "\<And> x y. \<lbrakk>x \<in> s ; y \<in> s ; x \<noteq> y\<rbrakk> \<Longrightarrow> f x \<inter> f y = {}"
  2247   assumes upper: "space M \<subseteq> (\<Union>i \<in> s. f i)"
  2248   shows "measure M e = (\<Sum> x \<in> s. measure M (e \<inter> f x))"
  2249 proof -
  2250   have "e \<subseteq> (\<Union>i\<in>s. f i)"
  2251     using \<open>e \<in> sets M\<close> sets.sets_into_space upper by blast
  2252   then have e: "e = (\<Union>i \<in> s. e \<inter> f i)"
  2253     by auto
  2254   hence "measure M e = measure M (\<Union>i \<in> s. e \<inter> f i)" by simp
  2255   also have "\<dots> = (\<Sum> x \<in> s. measure M (e \<inter> f x))"
  2256   proof (rule finite_measure_finite_Union)
  2257     show "finite s" by fact
  2258     show "(\<lambda>i. e \<inter> f i)`s \<subseteq> sets M" using assms(2) by auto
  2259     show "disjoint_family_on (\<lambda>i. e \<inter> f i) s"
  2260       using disjoint by (auto simp: disjoint_family_on_def)
  2261   qed
  2262   finally show ?thesis .
  2263 qed
  2265 lemma (in finite_measure) measure_exclude:
  2266   assumes "A \<in> sets M" "B \<in> sets M"
  2267   assumes "measure M A = measure M (space M)" "A \<inter> B = {}"
  2268   shows "measure M B = 0"
  2269   using measure_space_inter[of B A] assms by (auto simp: ac_simps)
  2270 lemma (in finite_measure) finite_measure_distr:
  2271   assumes f: "f \<in> measurable M M'"
  2272   shows "finite_measure (distr M M' f)"
  2273 proof (rule finite_measureI)
  2274   have "f -` space M' \<inter> space M = space M" using f by (auto dest: measurable_space)
  2275   with f show "emeasure (distr M M' f) (space (distr M M' f)) \<noteq> \<infinity>" by (auto simp: emeasure_distr)
  2276 qed
  2278 lemma emeasure_gfp[consumes 1, case_names cont measurable]:
  2279   assumes sets[simp]: "\<And>s. sets (M s) = sets N"
  2280   assumes "\<And>s. finite_measure (M s)"
  2281   assumes cont: "inf_continuous F" "inf_continuous f"
  2282   assumes meas: "\<And>P. Measurable.pred N P \<Longrightarrow> Measurable.pred N (F P)"
  2283   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"
  2284   assumes bound: "\<And>P. f P \<le> f (\<lambda>s. emeasure (M s) (space (M s)))"
  2285   shows "emeasure (M s) {x\<in>space N. gfp F x} = gfp f s"
  2286 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
  2287     P="Measurable.pred N", symmetric])
  2288   interpret finite_measure "M s" for s by fact
  2289   fix C assume "decseq C" "\<And>i. Measurable.pred N (C i)"
  2290   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}))"
  2291     unfolding INF_apply[abs_def]
  2292     by (subst INF_emeasure_decseq) (auto simp: antimono_def fun_eq_iff intro!: arg_cong2[where f=emeasure])
  2293 next
  2294   show "f x \<le> (\<lambda>s. emeasure (M s) {x \<in> space N. F top x})" for x
  2295     using bound[of x] sets_eq_imp_space_eq[OF sets] by (simp add: iter)
  2296 qed (auto simp add: iter le_fun_def INF_apply[abs_def] intro!: meas cont)
  2298 subsection%unimportant \<open>Counting space\<close>
  2300 lemma strict_monoI_Suc:
  2301   assumes ord [simp]: "(\<And>n. f n < f (Suc n))" shows "strict_mono f"
  2302   unfolding strict_mono_def
  2303 proof safe
  2304   fix n m :: nat assume "n < m" then show "f n < f m"
  2305     by (induct m) (auto simp: less_Suc_eq intro: less_trans ord)
  2306 qed
  2308 lemma emeasure_count_space:
  2309   assumes "X \<subseteq> A" shows "emeasure (count_space A) X = (if finite X then of_nat (card X) else \<infinity>)"
  2310     (is "_ = ?M X")
  2311   unfolding count_space_def
  2312 proof (rule emeasure_measure_of_sigma)
  2313   show "X \<in> Pow A" using \<open>X \<subseteq> A\<close> by auto
  2314   show "sigma_algebra A (Pow A)" by (rule sigma_algebra_Pow)
  2315   show positive: "positive (Pow A) ?M"
  2316     by (auto simp: positive_def)
  2317   have additive: "additive (Pow A) ?M"
  2318     by (auto simp: card_Un_disjoint additive_def)
  2320   interpret ring_of_sets A "Pow A"
  2321     by (rule ring_of_setsI) auto
  2322   show "countably_additive (Pow A) ?M"
  2323     unfolding countably_additive_iff_continuous_from_below[OF positive additive]
  2324   proof safe
  2325     fix F :: "nat \<Rightarrow> 'a set" assume "incseq F"
  2326     show "(\<lambda>i. ?M (F i)) \<longlonglongrightarrow> ?M (\<Union>i. F i)"
  2327     proof cases
  2328       assume "\<exists>i. \<forall>j\<ge>i. F i = F j"
  2329       then guess i .. note i = this
  2330       { fix j from i \<open>incseq F\<close> have "F j \<subseteq> F i"
  2331           by (cases "i \<le> j") (auto simp: incseq_def) }
  2332       then have eq: "(\<Union>i. F i) = F i"
  2333         by auto
  2334       with i show ?thesis
  2335         by (auto intro!: Lim_transform_eventually[OF _ tendsto_const] eventually_sequentiallyI[where c=i])
  2336     next
  2337       assume "\<not> (\<exists>i. \<forall>j\<ge>i. F i = F j)"
  2338       then obtain f where f: "\<And>i. i \<le> f i" "\<And>i. F i \<noteq> F (f i)" by metis
  2339       then have "\<And>i. F i \<subseteq> F (f i)" using \<open>incseq F\<close> by (auto simp: incseq_def)
  2340       with f have *: "\<And>i. F i \<subset> F (f i)" by auto
  2342       have "incseq (\<lambda>i. ?M (F i))"
  2343         using \<open>incseq F\<close> unfolding incseq_def by (auto simp: card_mono dest: finite_subset)
  2344       then have "(\<lambda>i. ?M (F i)) \<longlonglongrightarrow> (SUP n. ?M (F n))"
  2345         by (rule LIMSEQ_SUP)
  2347       moreover have "(SUP n. ?M (F n)) = top"
  2348       proof (rule ennreal_SUP_eq_top)
  2349         fix n :: nat show "\<exists>k::nat\<in>UNIV. of_nat n \<le> ?M (F k)"
  2350         proof (induct n)
  2351           case (Suc n)
  2352           then guess k .. note k = this
  2353           moreover have "finite (F k) \<Longrightarrow> finite (F (f k)) \<Longrightarrow> card (F k) < card (F (f k))"
  2354             using \<open>F k \<subset> F (f k)\<close> by (simp add: psubset_card_mono)
  2355           moreover have "finite (F (f k)) \<Longrightarrow> finite (F k)"
  2356             using \<open>k \<le> f k\<close> \<open>incseq F\<close> by (auto simp: incseq_def dest: finite_subset)
  2357           ultimately show ?case
  2358             by (auto intro!: exI[of _ "f k"] simp del: of_nat_Suc)
  2359         qed auto
  2360       qed
  2362       moreover
  2363       have "inj (\<lambda>n. F ((f ^^ n) 0))"
  2364         by (intro strict_mono_imp_inj_on strict_monoI_Suc) (simp add: *)
  2365       then have 1: "infinite (range (\<lambda>i. F ((f ^^ i) 0)))"
  2366         by (rule range_inj_infinite)
  2367       have "infinite (Pow (\<Union>i. F i))"
  2368         by (rule infinite_super[OF _ 1]) auto
  2369       then have "infinite (\<Union>i. F i)"
  2370         by auto
  2372       ultimately show ?thesis by auto
  2373     qed
  2374   qed
  2375 qed
  2377 lemma distr_bij_count_space:
  2378   assumes f: "bij_betw f A B"
  2379   shows "distr (count_space A) (count_space B) f = count_space B"
  2380 proof (rule measure_eqI)
  2381   have f': "f \<in> measurable (count_space A) (count_space B)"
  2382     using f unfolding Pi_def bij_betw_def by auto
  2383   fix X assume "X \<in> sets (distr (count_space A) (count_space B) f)"
  2384   then have X: "X \<in> sets (count_space B)" by auto
  2385   moreover from X have "f -` X \<inter> A = the_inv_into A f ` X"
  2386     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])
  2387   moreover have "inj_on (the_inv_into A f) B"
  2388     using X f by (auto simp: bij_betw_def inj_on_the_inv_into)
  2389   with X have "inj_on (the_inv_into A f) X"
  2390     by (auto intro: subset_inj_on)
  2391   ultimately show "emeasure (distr (count_space A) (count_space B) f) X = emeasure (count_space B) X"
  2392     using f unfolding emeasure_distr[OF f' X]
  2393     by (subst (1 2) emeasure_count_space) (auto simp: card_image dest: finite_imageD)
  2394 qed simp
  2396 lemma emeasure_count_space_finite[simp]:
  2397   "X \<subseteq> A \<Longrightarrow> finite X \<Longrightarrow> emeasure (count_space A) X = of_nat (card X)"
  2398   using emeasure_count_space[of X A] by simp
  2400 lemma emeasure_count_space_infinite[simp]:
  2401   "X \<subseteq> A \<Longrightarrow> infinite X \<Longrightarrow> emeasure (count_space A) X = \<infinity>"
  2402   using emeasure_count_space[of X A] by simp
  2404 lemma measure_count_space: "measure (count_space A) X = (if X \<subseteq> A then of_nat (card X) else 0)"
  2405   by (cases "finite X") (auto simp: measure_notin_sets ennreal_of_nat_eq_real_of_nat
  2406                                     measure_zero_top measure_eq_emeasure_eq_ennreal)
  2408 lemma emeasure_count_space_eq_0:
  2409   "emeasure (count_space A) X = 0 \<longleftrightarrow> (X \<subseteq> A \<longrightarrow> X = {})"
  2410 proof cases
  2411   assume X: "X \<subseteq> A"
  2412   then show ?thesis
  2413   proof (intro iffI impI)
  2414     assume "emeasure (count_space A) X = 0"
  2415     with X show "X = {}"
  2416       by (subst (asm) emeasure_count_space) (auto split: if_split_asm)
  2417   qed simp
  2418 qed (simp add: emeasure_notin_sets)
  2420 lemma null_sets_count_space: "null_sets (count_space A) = { {} }"
  2421   unfolding null_sets_def by (auto simp add: emeasure_count_space_eq_0)
  2423 lemma AE_count_space: "(AE x in count_space A. P x) \<longleftrightarrow> (\<forall>x\<in>A. P x)"
  2424   unfolding eventually_ae_filter by (auto simp add: null_sets_count_space)
  2426 lemma sigma_finite_measure_count_space_countable:
  2427   assumes A: "countable A"
  2428   shows "sigma_finite_measure (count_space A)"
  2429   proof qed (insert A, auto intro!: exI[of _ "(\<lambda>a. {a}) ` A"])
  2431 lemma sigma_finite_measure_count_space:
  2432   fixes A :: "'a::countable set" shows "sigma_finite_measure (count_space A)"
  2433   by (rule sigma_finite_measure_count_space_countable) auto
  2435 lemma finite_measure_count_space:
  2436   assumes [simp]: "finite A"
  2437   shows "finite_measure (count_space A)"
  2438   by rule simp
  2440 lemma sigma_finite_measure_count_space_finite:
  2441   assumes A: "finite A" shows "sigma_finite_measure (count_space A)"
  2442 proof -
  2443   interpret finite_measure "count_space A" using A by (rule finite_measure_count_space)
  2444   show "sigma_finite_measure (count_space A)" ..
  2445 qed
  2447 subsection%unimportant \<open>Measure restricted to space\<close>
  2449 lemma emeasure_restrict_space:
  2450   assumes "\<Omega> \<inter> space M \<in> sets M" "A \<subseteq> \<Omega>"
  2451   shows "emeasure (restrict_space M \<Omega>) A = emeasure M A"
  2452 proof (cases "A \<in> sets M")
  2453   case True
  2454   show ?thesis
  2455   proof (rule emeasure_measure_of[OF restrict_space_def])
  2456     show "(\<inter>) \<Omega> ` sets M \<subseteq> Pow (\<Omega> \<inter> space M)" "A \<in> sets (restrict_space M \<Omega>)"
  2457       using \<open>A \<subseteq> \<Omega>\<close> \<open>A \<in> sets M\<close> sets.space_closed by (auto simp: sets_restrict_space)
  2458     show "positive (sets (restrict_space M \<Omega>)) (emeasure M)"
  2459       by (auto simp: positive_def)
  2460     show "countably_additive (sets (restrict_space M \<Omega>)) (emeasure M)"
  2461     proof (rule countably_additiveI)
  2462       fix A :: "nat \<Rightarrow> _" assume "range A \<subseteq> sets (restrict_space M \<Omega>)" "disjoint_family A"
  2463       with assms have "\<And>i. A i \<in> sets M" "\<And>i. A i \<subseteq> space M" "disjoint_family A"
  2464         by (fastforce simp: sets_restrict_space_iff[OF assms(1)] image_subset_iff
  2465                       dest: sets.sets_into_space)+
  2466       then show "(\<Sum>i. emeasure M (A i)) = emeasure M (\<Union>i. A i)"
  2467         by (subst suminf_emeasure) (auto simp: disjoint_family_subset)
  2468     qed
  2469   qed
  2470 next
  2471   case False
  2472   with assms have "A \<notin> sets (restrict_space M \<Omega>)"
  2473     by (simp add: sets_restrict_space_iff)
  2474   with False show ?thesis
  2475     by (simp add: emeasure_notin_sets)
  2476 qed
  2478 lemma measure_restrict_space:
  2479   assumes "\<Omega> \<inter> space M \<in> sets M" "A \<subseteq> \<Omega>"
  2480   shows "measure (restrict_space M \<Omega>) A = measure M A"
  2481   using emeasure_restrict_space[OF assms] by (simp add: measure_def)
  2483 lemma AE_restrict_space_iff:
  2484   assumes "\<Omega> \<inter> space M \<in> sets M"
  2485   shows "(AE x in restrict_space M \<Omega>. P x) \<longleftrightarrow> (AE x in M. x \<in> \<Omega> \<longrightarrow> P x)"
  2486 proof -
  2487   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)"
  2488     by auto
  2489   { fix X assume X: "X \<in> sets M" "emeasure M X = 0"
  2490     then have "emeasure M (\<Omega> \<inter> space M \<inter> X) \<le> emeasure M X"
  2491       by (intro emeasure_mono) auto
  2492     then have "emeasure M (\<Omega> \<inter> space M \<inter> X) = 0"
  2493       using X by (auto intro!: antisym) }
  2494   with assms show ?thesis
  2495     unfolding eventually_ae_filter
  2496     by (auto simp add: space_restrict_space null_sets_def sets_restrict_space_iff
  2497                        emeasure_restrict_space cong: conj_cong
  2498              intro!: ex_cong[where f="\<lambda>X. (\<Omega> \<inter> space M) \<inter> X"])
  2499 qed
  2501 lemma restrict_restrict_space:
  2502   assumes "A \<inter> space M \<in> sets M" "B \<inter> space M \<in> sets M"
  2503   shows "restrict_space (restrict_space M A) B = restrict_space M (A \<inter> B)" (is "?l = ?r")
  2504 proof (rule measure_eqI[symmetric])
  2505   show "sets ?r = sets ?l"
  2506     unfolding sets_restrict_space image_comp by (intro image_cong) auto
  2507 next
  2508   fix X assume "X \<in> sets (restrict_space M (A \<inter> B))"
  2509   then obtain Y where "Y \<in> sets M" "X = Y \<inter> A \<inter> B"
  2510     by (auto simp: sets_restrict_space)
  2511   with assms sets.Int[OF assms] show "emeasure ?r X = emeasure ?l X"
  2512     by (subst (1 2) emeasure_restrict_space)
  2513        (auto simp: space_restrict_space sets_restrict_space_iff emeasure_restrict_space ac_simps)
  2514 qed
  2516 lemma restrict_count_space: "restrict_space (count_space B) A = count_space (A \<inter> B)"
  2517 proof (rule measure_eqI)
  2518   show "sets (restrict_space (count_space B) A) = sets (count_space (A \<inter> B))"
  2519     by (subst sets_restrict_space) auto
  2520   moreover fix X assume "X \<in> sets (restrict_space (count_space B) A)"
  2521   ultimately have "X \<subseteq> A \<inter> B" by auto
  2522   then show "emeasure (restrict_space (count_space B) A) X = emeasure (count_space (A \<inter> B)) X"
  2523     by (cases "finite X") (auto simp add: emeasure_restrict_space)
  2524 qed
  2526 lemma sigma_finite_measure_restrict_space:
  2527   assumes "sigma_finite_measure M"
  2528   and A: "A \<in> sets M"
  2529   shows "sigma_finite_measure (restrict_space M A)"
  2530 proof -
  2531   interpret sigma_finite_measure M by fact
  2532   from sigma_finite_countable obtain C
  2533     where C: "countable C" "C \<subseteq> sets M" "(\<Union>C) = space M" "\<forall>a\<in>C. emeasure M a \<noteq> \<infinity>"
  2534     by blast
  2535   let ?C = "(\<inter>) A ` C"
  2536   from C have "countable ?C" "?C \<subseteq> sets (restrict_space M A)" "(\<Union>?C) = space (restrict_space M A)"
  2537     by(auto simp add: sets_restrict_space space_restrict_space)
  2538   moreover {
  2539     fix a
  2540     assume "a \<in> ?C"
  2541     then obtain a' where "a = A \<inter> a'" "a' \<in> C" ..
  2542     then have "emeasure (restrict_space M A) a \<le> emeasure M a'"
  2543       using A C by(auto simp add: emeasure_restrict_space intro: emeasure_mono)
  2544     also have "\<dots> < \<infinity>" using C(4)[rule_format, of a'] \<open>a' \<in> C\<close> by (simp add: less_top)
  2545     finally have "emeasure (restrict_space M A) a \<noteq> \<infinity>" by simp }
  2546   ultimately show ?thesis
  2547     by unfold_locales (rule exI conjI|assumption|blast)+
  2548 qed
  2550 lemma finite_measure_restrict_space:
  2551   assumes "finite_measure M"
  2552   and A: "A \<in> sets M"
  2553   shows "finite_measure (restrict_space M A)"
  2554 proof -
  2555   interpret finite_measure M by fact
  2556   show ?thesis
  2557     by(rule finite_measureI)(simp add: emeasure_restrict_space A space_restrict_space)
  2558 qed
  2560 lemma restrict_distr:
  2561   assumes [measurable]: "f \<in> measurable M N"
  2562   assumes [simp]: "\<Omega> \<inter> space N \<in> sets N" and restrict: "f \<in> space M \<rightarrow> \<Omega>"
  2563   shows "restrict_space (distr M N f) \<Omega> = distr M (restrict_space N \<Omega>) f"
  2564   (is "?l = ?r")
  2565 proof (rule measure_eqI)
  2566   fix A assume "A \<in> sets (restrict_space (distr M N f) \<Omega>)"
  2567   with restrict show "emeasure ?l A = emeasure ?r A"
  2568     by (subst emeasure_distr)
  2569        (auto simp: sets_restrict_space_iff emeasure_restrict_space emeasure_distr
  2570              intro!: measurable_restrict_space2)
  2571 qed (simp add: sets_restrict_space)
  2573 lemma measure_eqI_restrict_generator:
  2574   assumes E: "Int_stable E" "E \<subseteq> Pow \<Omega>" "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
  2575   assumes sets_eq: "sets M = sets N" and \<Omega>: "\<Omega> \<in> sets M"
  2576   assumes "sets (restrict_space M \<Omega>) = sigma_sets \<Omega> E"
  2577   assumes "sets (restrict_space N \<Omega>) = sigma_sets \<Omega> E"
  2578   assumes ae: "AE x in M. x \<in> \<Omega>" "AE x in N. x \<in> \<Omega>"
  2579   assumes A: "countable A" "A \<noteq> {}" "A \<subseteq> E" "\<Union>A = \<Omega>" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
  2580   shows "M = N"
  2581 proof (rule measure_eqI)
  2582   fix X assume X: "X \<in> sets M"
  2583   then have "emeasure M X = emeasure (restrict_space M \<Omega>) (X \<inter> \<Omega>)"
  2584     using ae \<Omega> by (auto simp add: emeasure_restrict_space intro!: emeasure_eq_AE)
  2585   also have "restrict_space M \<Omega> = restrict_space N \<Omega>"
  2586   proof (rule measure_eqI_generator_eq)
  2587     fix X assume "X \<in> E"
  2588     then show "emeasure (restrict_space M \<Omega>) X = emeasure (restrict_space N \<Omega>) X"
  2589       using E \<Omega> by (subst (1 2) emeasure_restrict_space) (auto simp: sets_eq sets_eq[THEN sets_eq_imp_space_eq])
  2590   next
  2591     show "range (from_nat_into A) \<subseteq> E" "(\<Union>i. from_nat_into A i) = \<Omega>"
  2592       using A by (auto cong del: SUP_cong_strong)
  2593   next
  2594     fix i
  2595     have "emeasure (restrict_space M \<Omega>) (from_nat_into A i) = emeasure M (from_nat_into A i)"
  2596       using A \<Omega> by (subst emeasure_restrict_space)
  2597                    (auto simp: sets_eq sets_eq[THEN sets_eq_imp_space_eq] intro: from_nat_into)
  2598     with A show "emeasure (restrict_space M \<Omega>) (from_nat_into A i) \<noteq> \<infinity>"
  2599       by (auto intro: from_nat_into)
  2600   qed fact+
  2601   also have "emeasure (restrict_space N \<Omega>) (X \<inter> \<Omega>) = emeasure N X"
  2602     using X ae \<Omega> by (auto simp add: emeasure_restrict_space sets_eq intro!: emeasure_eq_AE)
  2603   finally show "emeasure M X = emeasure N X" .
  2604 qed fact
  2606 subsection%unimportant \<open>Null measure\<close>
  2608 definition "null_measure M = sigma (space M) (sets M)"
  2610 lemma space_null_measure[simp]: "space (null_measure M) = space M"
  2611   by (simp add: null_measure_def)
  2613 lemma sets_null_measure[simp, measurable_cong]: "sets (null_measure M) = sets M"
  2614   by (simp add: null_measure_def)
  2616 lemma emeasure_null_measure[simp]: "emeasure (null_measure M) X = 0"
  2617   by (cases "X \<in> sets M", rule emeasure_measure_of)
  2618      (auto simp: positive_def countably_additive_def emeasure_notin_sets null_measure_def
  2619            dest: sets.sets_into_space)
  2621 lemma measure_null_measure[simp]: "measure (null_measure M) X = 0"
  2622   by (intro measure_eq_emeasure_eq_ennreal) auto
  2624 lemma null_measure_idem [simp]: "null_measure (null_measure M) = null_measure M"
  2625   by(rule measure_eqI) simp_all
  2627 subsection \<open>Scaling a measure\<close>
  2629 definition%important scale_measure :: "ennreal \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  2630 where
  2631   "scale_measure r M = measure_of (space M) (sets M) (\<lambda>A. r * emeasure M A)"
  2633 lemma space_scale_measure: "space (scale_measure r M) = space M"
  2634   by (simp add: scale_measure_def)
  2636 lemma sets_scale_measure [simp, measurable_cong]: "sets (scale_measure r M) = sets M"
  2637   by (simp add: scale_measure_def)
  2639 lemma emeasure_scale_measure [simp]:
  2640   "emeasure (scale_measure r M) A = r * emeasure M A"
  2641   (is "_ = ?\<mu> A")
  2642 proof(cases "A \<in> sets M")
  2643   case True
  2644   show ?thesis unfolding scale_measure_def
  2645   proof(rule emeasure_measure_of_sigma)
  2646     show "sigma_algebra (space M) (sets M)" ..
  2647     show "positive (sets M) ?\<mu>" by (simp add: positive_def)
  2648     show "countably_additive (sets M) ?\<mu>"
  2649     proof (rule countably_additiveI)
  2650       fix A :: "nat \<Rightarrow> _"  assume *: "range A \<subseteq> sets M" "disjoint_family A"
  2651       have "(\<Sum>i. ?\<mu> (A i)) = r * (\<Sum>i. emeasure M (A i))"
  2652         by simp
  2653       also have "\<dots> = ?\<mu> (\<Union>i. A i)" using * by(simp add: suminf_emeasure)
  2654       finally show "(\<Sum>i. ?\<mu> (A i)) = ?\<mu> (\<Union>i. A i)" .
  2655     qed
  2656   qed(fact True)
  2657 qed(simp add: emeasure_notin_sets)
  2659 lemma scale_measure_1 [simp]: "scale_measure 1 M = M"
  2660   by(rule measure_eqI) simp_all
  2662 lemma scale_measure_0[simp]: "scale_measure 0 M = null_measure M"
  2663   by(rule measure_eqI) simp_all
  2665 lemma measure_scale_measure [simp]: "0 \<le> r \<Longrightarrow> measure (scale_measure r M) A = r * measure M A"
  2666   using emeasure_scale_measure[of r M A]
  2667     emeasure_eq_ennreal_measure[of M A]
  2668     measure_eq_emeasure_eq_ennreal[of _ "scale_measure r M" A]
  2669   by (cases "emeasure (scale_measure r M) A = top")
  2670      (auto simp del: emeasure_scale_measure
  2671            simp: ennreal_top_eq_mult_iff ennreal_mult_eq_top_iff measure_zero_top ennreal_mult[symmetric])
  2673 lemma scale_scale_measure [simp]:
  2674   "scale_measure r (scale_measure r' M) = scale_measure (r * r') M"
  2675   by (rule measure_eqI) (simp_all add: max_def mult.assoc)
  2677 lemma scale_null_measure [simp]: "scale_measure r (null_measure M) = null_measure M"
  2678   by (rule measure_eqI) simp_all
  2681 subsection \<open>Complete lattice structure on measures\<close>
  2683 lemma (in finite_measure) finite_measure_Diff':
  2684   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> measure M (A - B) = measure M A - measure M (A \<inter> B)"
  2685   using finite_measure_Diff[of A "A \<inter> B"] by (auto simp: Diff_Int)
  2687 lemma (in finite_measure) finite_measure_Union':
  2688   "A \<in> sets M \<Longrightarrow> B \<in> sets M \<Longrightarrow> measure M (A \<union> B) = measure M A + measure M (B - A)"
  2689   using finite_measure_Union[of A "B - A"] by auto
  2691 lemma finite_unsigned_Hahn_decomposition:
  2692   assumes "finite_measure M" "finite_measure N" and [simp]: "sets N = sets M"
  2693   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)"
  2694 proof -
  2695   interpret M: finite_measure M by fact
  2696   interpret N: finite_measure N by fact
  2698   define d where "d X = measure M X - measure N X" for X
  2700   have [intro]: "bdd_above (d`sets M)"
  2701     using sets.sets_into_space[of _ M]
  2702     by (intro bdd_aboveI[where M="measure M (space M)"])
  2703        (auto simp: d_def field_simps subset_eq intro!: add_increasing M.finite_measure_mono)
  2705   define \<gamma> where "\<gamma> = (SUP X\<in>sets M. d X)"
  2706   have le_\<gamma>[intro]: "X \<in> sets M \<Longrightarrow> d X \<le> \<gamma>" for X
  2707     by (auto simp: \<gamma>_def intro!: cSUP_upper)
  2709   have "\<exists>f. \<forall>n. f n \<in> sets M \<and> d (f n) > \<gamma> - 1 / 2^n"
  2710   proof (intro choice_iff[THEN iffD1] allI)
  2711     fix n
  2712     have "\<exists>X\<in>sets M. \<gamma> - 1 / 2^n < d X"
  2713       unfolding \<gamma>_def by (intro less_cSUP_iff[THEN iffD1]) auto
  2714     then show "\<exists>y. y \<in> sets M \<and> \<gamma> - 1 / 2 ^ n < d y"
  2715       by auto
  2716   qed
  2717   then obtain E where [measurable]: "E n \<in> sets M" and E: "d (E n) > \<gamma> - 1 / 2^n" for n
  2718     by auto
  2720   define F where "F m n = (if m \<le> n then \<Inter>i\<in>{m..n}. E i else space M)" for m n
  2722   have [measurable]: "m \<le> n \<Longrightarrow> F m n \<in> sets M" for m n
  2723     by (auto simp: F_def)
  2725   have 1: "\<gamma> - 2 / 2 ^ m + 1 / 2 ^ n \<le> d (F m n)" if "m \<le> n" for m n
  2726     using that
  2727   proof (induct rule: dec_induct)
  2728     case base with E[of m] show ?case
  2729       by (simp add: F_def field_simps)
  2730   next
  2731     case (step i)
  2732     have F_Suc: "F m (Suc i) = F m i \<inter> E (Suc i)"
  2733       using \<open>m \<le> i\<close> by (auto simp: F_def le_Suc_eq)
  2735     have "\<gamma> + (\<gamma> - 2 / 2^m + 1 / 2 ^ Suc i) \<le> (\<gamma> - 1 / 2^Suc i) + (\<gamma> - 2 / 2^m + 1 / 2^i)"
  2736       by (simp add: field_simps)
  2737     also have "\<dots> \<le> d (E (Suc i)) + d (F m i)"
  2738       using E[of "Suc i"] by (intro add_mono step) auto
  2739     also have "\<dots> = d (E (Suc i)) + d (F m i - E (Suc i)) + d (F m (Suc i))"
  2740       using \<open>m \<le> i\<close> by (simp add: d_def field_simps F_Suc M.finite_measure_Diff' N.finite_measure_Diff')
  2741     also have "\<dots> = d (E (Suc i) \<union> F m i) + d (F m (Suc i))"
  2742       using \<open>m \<le> i\<close> by (simp add: d_def field_simps M.finite_measure_Union' N.finite_measure_Union')
  2743     also have "\<dots> \<le> \<gamma> + d (F m (Suc i))"
  2744       using \<open>m \<le> i\<close> by auto
  2745     finally show ?case
  2746       by auto
  2747   qed
  2749   define F' where "F' m = (\<Inter>i\<in>{m..}. E i)" for m
  2750   have F'_eq: "F' m = (\<Inter>i. F m (i + m))" for m
  2751     by (fastforce simp: le_iff_add[of m] F'_def F_def)
  2753   have [measurable]: "F' m \<in> sets M" for m
  2754     by (auto simp: F'_def)
  2756   have \<gamma>_le: "\<gamma> - 0 \<le> d (\<Union>m. F' m)"
  2757   proof (rule LIMSEQ_le)
  2758     show "(\<lambda>n. \<gamma> - 2 / 2 ^ n) \<longlonglongrightarrow> \<gamma> - 0"
  2759       by (intro tendsto_intros LIMSEQ_divide_realpow_zero) auto
  2760     have "incseq F'"
  2761       by (auto simp: incseq_def F'_def)
  2762     then show "(\<lambda>m. d (F' m)) \<longlonglongrightarrow> d (\<Union>m. F' m)"
  2763       unfolding d_def
  2764       by (intro tendsto_diff M.finite_Lim_measure_incseq N.finite_Lim_measure_incseq) auto
  2766     have "\<gamma> - 2 / 2 ^ m + 0 \<le> d (F' m)" for m
  2767     proof (rule LIMSEQ_le)
  2768       have *: "decseq (\<lambda>n. F m (n + m))"
  2769         by (auto simp: decseq_def F_def)
  2770       show "(\<lambda>n. d (F m n)) \<longlonglongrightarrow> d (F' m)"
  2771         unfolding d_def F'_eq
  2772         by (rule LIMSEQ_offset[where k=m])
  2773            (auto intro!: tendsto_diff M.finite_Lim_measure_decseq N.finite_Lim_measure_decseq *)
  2774       show "(\<lambda>n. \<gamma> - 2 / 2 ^ m + 1 / 2 ^ n) \<longlonglongrightarrow> \<gamma> - 2 / 2 ^ m + 0"
  2775         by (intro tendsto_add LIMSEQ_divide_realpow_zero tendsto_const) auto
  2776       show "\<exists>N. \<forall>n\<ge>N. \<gamma> - 2 / 2 ^ m + 1 / 2 ^ n \<le> d (F m n)"
  2777         using 1[of m] by (intro exI[of _ m]) auto
  2778     qed
  2779     then show "\<exists>N. \<forall>n\<ge>N. \<gamma> - 2 / 2 ^ n \<le> d (F' n)"
  2780       by auto
  2781   qed
  2783   show ?thesis
  2784   proof (safe intro!: bexI[of _ "\<Union>m. F' m"])
  2785     fix X assume [measurable]: "X \<in> sets M" and X: "X \<subseteq> (\<Union>m. F' m)"
  2786     have "d (\<Union>m. F' m) - d X = d ((\<Union>m. F' m) - X)"
  2787       using X by (auto simp: d_def M.finite_measure_Diff N.finite_measure_Diff)
  2788     also have "\<dots> \<le> \<gamma>"
  2789       by auto
  2790     finally have "0 \<le> d X"
  2791       using \<gamma>_le by auto
  2792     then show "emeasure N X \<le> emeasure M X"
  2793       by (auto simp: d_def M.emeasure_eq_measure N.emeasure_eq_measure)
  2794   next
  2795     fix X assume [measurable]: "X \<in> sets M" and X: "X \<inter> (\<Union>m. F' m) = {}"
  2796     then have "d (\<Union>m. F' m) + d X = d (X \<union> (\<Union>m. F' m))"
  2797       by (auto simp: d_def M.finite_measure_Union N.finite_measure_Union)
  2798     also have "\<dots> \<le> \<gamma>"
  2799       by auto
  2800     finally have "d X \<le> 0"
  2801       using \<gamma>_le by auto
  2802     then show "emeasure M X \<le> emeasure N X"
  2803       by (auto simp: d_def M.emeasure_eq_measure N.emeasure_eq_measure)
  2804   qed auto
  2805 qed
  2807 proposition unsigned_Hahn_decomposition:
  2808   assumes [simp]: "sets N = sets M" and [measurable]: "A \<in> sets M"
  2809     and [simp]: "emeasure M A \<noteq> top" "emeasure N A \<noteq> top"
  2810   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)"
  2811 proof -
  2812   have "\<exists>Y\<in>sets (restrict_space M A).
  2813     (\<forall>X\<in>sets (restrict_space M A). X \<subseteq> Y \<longrightarrow> (restrict_space N A) X \<le> (restrict_space M A) X) \<and>
  2814     (\<forall>X\<in>sets (restrict_space M A). X \<inter> Y = {} \<longrightarrow> (restrict_space M A) X \<le> (restrict_space N A) X)"
  2815   proof (rule finite_unsigned_Hahn_decomposition)
  2816     show "finite_measure (restrict_space M A)" "finite_measure (restrict_space N A)"
  2817       by (auto simp: space_restrict_space emeasure_restrict_space less_top intro!: finite_measureI)
  2818   qed (simp add: sets_restrict_space)
  2819   then guess Y ..
  2820   then show ?thesis
  2821     apply (intro bexI[of _ Y] conjI ballI conjI)
  2822     apply (simp_all add: sets_restrict_space emeasure_restrict_space)
  2823     apply safe
  2824     subgoal for X Z
  2825       by (erule ballE[of _ _ X]) (auto simp add: Int_absorb1)
  2826     subgoal for X Z
  2827       by (erule ballE[of _ _ X])  (auto simp add: Int_absorb1 ac_simps)
  2828     apply auto
  2829     done
  2830 qed
  2832 text%important \<open>
  2833   Define a lexicographical order on @{type measure}, in the order space, sets and measure. The parts
  2834   of the lexicographical order are point-wise ordered.
  2835 \<close>
  2837 instantiation measure :: (type) order_bot
  2838 begin
  2840 inductive less_eq_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
  2841   "space M \<subset> space N \<Longrightarrow> less_eq_measure M N"
  2842 | "space M = space N \<Longrightarrow> sets M \<subset> sets N \<Longrightarrow> less_eq_measure M N"
  2843 | "space M = space N \<Longrightarrow> sets M = sets N \<Longrightarrow> emeasure M \<le> emeasure N \<Longrightarrow> less_eq_measure M N"
  2845 lemma le_measure_iff:
  2846   "M \<le> N \<longleftrightarrow> (if space M = space N then
  2847     if sets M = sets N then emeasure M \<le> emeasure N else sets M \<subseteq> sets N else space M \<subseteq> space N)"
  2848   by (auto elim: less_eq_measure.cases intro: less_eq_measure.intros)
  2850 definition%important less_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> bool" where
  2851   "less_measure M N \<longleftrightarrow> (M \<le> N \<and> \<not> N \<le> M)"
  2853 definition%important bot_measure :: "'a measure" where
  2854   "bot_measure = sigma {} {}"
  2856 lemma
  2857   shows space_bot[simp]: "space bot = {}"
  2858     and sets_bot[simp]: "sets bot = {{}}"
  2859     and emeasure_bot[simp]: "emeasure bot X = 0"
  2860   by (auto simp: bot_measure_def sigma_sets_empty_eq emeasure_sigma)
  2862 instance
  2863 proof standard
  2864   show "bot \<le> a" for a :: "'a measure"
  2865     by (simp add: le_measure_iff bot_measure_def sigma_sets_empty_eq emeasure_sigma le_fun_def)
  2866 qed (auto simp: le_measure_iff less_measure_def split: if_split_asm intro: measure_eqI)
  2868 end
  2870 proposition le_measure: "sets M = sets N \<Longrightarrow> M \<le> N \<longleftrightarrow> (\<forall>A\<in>sets M. emeasure M A \<le> emeasure N A)"
  2871   apply -
  2872   apply (auto simp: le_measure_iff le_fun_def dest: sets_eq_imp_space_eq)
  2873   subgoal for X
  2874     by (cases "X \<in> sets M") (auto simp: emeasure_notin_sets)
  2875   done
  2877 definition%important sup_measure' :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  2878 where
  2879   "sup_measure' A B = measure_of (space A) (sets A) (\<lambda>X. SUP Y\<in>sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  2881 lemma assumes [simp]: "sets B = sets A"
  2882   shows space_sup_measure'[simp]: "space (sup_measure' A B) = space A"
  2883     and sets_sup_measure'[simp]: "sets (sup_measure' A B) = sets A"
  2884   using sets_eq_imp_space_eq[OF assms] by (simp_all add: sup_measure'_def)
  2886 lemma emeasure_sup_measure':
  2887   assumes sets_eq[simp]: "sets B = sets A" and [simp, intro]: "X \<in> sets A"
  2888   shows "emeasure (sup_measure' A B) X = (SUP Y\<in>sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  2889     (is "_ = ?S X")
  2890 proof -
  2891   note sets_eq_imp_space_eq[OF sets_eq, simp]
  2892   show ?thesis
  2893     using sup_measure'_def
  2894   proof (rule emeasure_measure_of)
  2895     let ?d = "\<lambda>X Y. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y)"
  2896     show "countably_additive (sets (sup_measure' A B)) (\<lambda>X. SUP Y \<in> sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y))"
  2897     proof (rule countably_additiveI, goal_cases)
  2898       case (1 X)
  2899       then have [measurable]: "\<And>i. X i \<in> sets A" and "disjoint_family X"
  2900         by auto
  2901       have "(\<Sum>i. ?S (X i)) = (SUP Y\<in>sets A. \<Sum>i. ?d (X i) Y)"
  2902       proof (rule ennreal_suminf_SUP_eq_directed)
  2903         fix J :: "nat set" and a b assume "finite J" and [measurable]: "a \<in> sets A" "b \<in> sets A"
  2904         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
  2905         proof cases
  2906           assume "emeasure A (X i) = top \<or> emeasure B (X i) = top"
  2907           then show ?thesis
  2908           proof
  2909             assume "emeasure A (X i) = top" then show ?thesis
  2910               by (intro bexI[of _ "X i"]) auto
  2911           next
  2912             assume "emeasure B (X i) = top" then show ?thesis
  2913               by (intro bexI[of _ "{}"]) auto
  2914           qed
  2915         next
  2916           assume finite: "\<not> (emeasure A (X i) = top \<or> emeasure B (X i) = top)"
  2917           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)"
  2918             using unsigned_Hahn_decomposition[of B A "X i"] by simp
  2919           then obtain Y where [measurable]: "Y \<in> sets A" and [simp]: "Y \<subseteq> X i"
  2920             and B_le_A: "\<And>C. C \<in> sets A \<Longrightarrow> C \<subseteq> Y \<Longrightarrow> B C \<le> A C"
  2921             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"
  2922             by auto
  2924           show ?thesis
  2925           proof (intro bexI[of _ Y] ballI conjI)
  2926             fix a assume [measurable]: "a \<in> sets A"
  2927             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"
  2928               for a Y by auto
  2929             then have "?d (X i) a =
  2930               (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))"
  2931               by (subst (1 2) plus_emeasure) (auto simp: Diff_eq[symmetric])
  2932             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))"
  2933               by (intro add_mono order_refl B_le_A A_le_B) (auto simp: Diff_eq[symmetric])
  2934             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))"
  2935               by (simp add: ac_simps)
  2936             also have "\<dots> \<le> A (X i \<inter> Y) + B (X i \<inter> - Y)"
  2937               by (subst (1 2) plus_emeasure) (auto simp: Diff_eq[symmetric] *)
  2938             finally show "?d (X i) a \<le> ?d (X i) Y" .
  2939           qed auto
  2940         qed
  2941         then obtain C where [measurable]: "C i \<in> sets A" and "C i \<subseteq> X i"
  2942           and C: "\<And>a. a \<in> sets A \<Longrightarrow> ?d (X i) a \<le> ?d (X i) (C i)" for i
  2943           by metis
  2944         have *: "X i \<inter> (\<Union>i. C i) = X i \<inter> C i" for i
  2945         proof safe
  2946           fix x j assume "x \<in> X i" "x \<in> C j"
  2947           moreover have "i = j \<or> X i \<inter> X j = {}"
  2948             using \<open>disjoint_family X\<close> by (auto simp: disjoint_family_on_def)
  2949           ultimately show "x \<in> C i"
  2950             using \<open>C i \<subseteq> X i\<close> \<open>C j \<subseteq> X j\<close> by auto
  2951         qed auto
  2952         have **: "X i \<inter> - (\<Union>i. C i) = X i \<inter> - C i" for i
  2953         proof safe
  2954           fix x j assume "x \<in> X i" "x \<notin> C i" "x \<in> C j"
  2955           moreover have "i = j \<or> X i \<inter> X j = {}"
  2956             using \<open>disjoint_family X\<close> by (auto simp: disjoint_family_on_def)
  2957           ultimately show False
  2958             using \<open>C i \<subseteq> X i\<close> \<open>C j \<subseteq> X j\<close> by auto
  2959         qed auto
  2960         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"
  2961           apply (intro bexI[of _ "\<Union>i. C i"])
  2962           unfolding * **
  2963           apply (intro C ballI conjI)
  2964           apply auto
  2965           done
  2966       qed
  2967       also have "\<dots> = ?S (\<Union>i. X i)"
  2968         unfolding UN_extend_simps(4)
  2969         by (auto simp add: suminf_add[symmetric] Diff_eq[symmetric] simp del: UN_simps
  2970                  intro!: SUP_cong arg_cong2[where f="(+)"] suminf_emeasure
  2971                          disjoint_family_on_bisimulation[OF \<open>disjoint_family X\<close>])
  2972       finally show "(\<Sum>i. ?S (X i)) = ?S (\<Union>i. X i)" .
  2973     qed
  2974   qed (auto dest: sets.sets_into_space simp: positive_def intro!: SUP_const)
  2975 qed
  2977 lemma le_emeasure_sup_measure'1:
  2978   assumes "sets B = sets A" "X \<in> sets A" shows "emeasure A X \<le> emeasure (sup_measure' A B) X"
  2979   by (subst emeasure_sup_measure'[OF assms]) (auto intro!: SUP_upper2[of "X"] assms)
  2981 lemma le_emeasure_sup_measure'2:
  2982   assumes "sets B = sets A" "X \<in> sets A" shows "emeasure B X \<le> emeasure (sup_measure' A B) X"
  2983   by (subst emeasure_sup_measure'[OF assms]) (auto intro!: SUP_upper2[of "{}"] assms)
  2985 lemma emeasure_sup_measure'_le2:
  2986   assumes [simp]: "sets B = sets C" "sets A = sets C" and [measurable]: "X \<in> sets C"
  2987   assumes A: "\<And>Y. Y \<subseteq> X \<Longrightarrow> Y \<in> sets A \<Longrightarrow> emeasure A Y \<le> emeasure C Y"
  2988   assumes B: "\<And>Y. Y \<subseteq> X \<Longrightarrow> Y \<in> sets A \<Longrightarrow> emeasure B Y \<le> emeasure C Y"
  2989   shows "emeasure (sup_measure' A B) X \<le> emeasure C X"
  2990 proof (subst emeasure_sup_measure')
  2991   show "(SUP Y\<in>sets A. emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y)) \<le> emeasure C X"
  2992     unfolding \<open>sets A = sets C\<close>
  2993   proof (intro SUP_least)
  2994     fix Y assume [measurable]: "Y \<in> sets C"
  2995     have [simp]: "X \<inter> Y \<union> (X - Y) = X"
  2996       by auto
  2997     have "emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y) \<le> emeasure C (X \<inter> Y) + emeasure C (X \<inter> - Y)"
  2998       by (intro add_mono A B) (auto simp: Diff_eq[symmetric])
  2999     also have "\<dots> = emeasure C X"
  3000       by (subst plus_emeasure) (auto simp: Diff_eq[symmetric])
  3001     finally show "emeasure A (X \<inter> Y) + emeasure B (X \<inter> - Y) \<le> emeasure C X" .
  3002   qed
  3003 qed simp_all
  3005 definition%important sup_lexord :: "'a \<Rightarrow> 'a \<Rightarrow> ('a \<Rightarrow> 'b::order) \<Rightarrow> 'a \<Rightarrow> 'a \<Rightarrow> 'a"
  3006 where
  3007   "sup_lexord A B k s c =
  3008     (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)"
  3010 lemma sup_lexord:
  3011   "(k A < k B \<Longrightarrow> P B) \<Longrightarrow> (k B < k A \<Longrightarrow> P A) \<Longrightarrow> (k A = k B \<Longrightarrow> P c) \<Longrightarrow>
  3012     (\<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)"
  3013   by (auto simp: sup_lexord_def)
  3015 lemmas le_sup_lexord = sup_lexord[where P="\<lambda>a. c \<le> a" for c]
  3017 lemma sup_lexord1: "k A = k B \<Longrightarrow> sup_lexord A B k s c = c"
  3018   by (simp add: sup_lexord_def)
  3020 lemma sup_lexord_commute: "sup_lexord A B k s c = sup_lexord B A k s c"
  3021   by (auto simp: sup_lexord_def)
  3023 lemma sigma_sets_le_sets_iff: "(sigma_sets (space x) \<A> \<subseteq> sets x) = (\<A> \<subseteq> sets x)"
  3024   using sets.sigma_sets_subset[of \<A> x] by auto
  3026 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))"
  3027   by (cases "\<Omega> = space x")
  3028      (simp_all add: eq_commute[of _ "sets x"] le_measure_iff emeasure_sigma le_fun_def
  3029                     sigma_sets_superset_generator sigma_sets_le_sets_iff)
  3031 instantiation measure :: (type) semilattice_sup
  3032 begin
  3034 definition%important sup_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  3035 where
  3036   "sup_measure A B =
  3037     sup_lexord A B space (sigma (space A \<union> space B) {})
  3038       (sup_lexord A B sets (sigma (space A) (sets A \<union> sets B)) (sup_measure' A B))"
  3040 instance
  3041 proof
  3042   fix x y z :: "'a measure"
  3043   show "x \<le> sup x y"
  3044     unfolding sup_measure_def
  3045   proof (intro le_sup_lexord)
  3046     assume "space x = space y"
  3047     then have *: "sets x \<union> sets y \<subseteq> Pow (space x)"
  3048       using sets.space_closed by auto
  3049     assume "\<not> sets y \<subseteq> sets x" "\<not> sets x \<subseteq> sets y"
  3050     then have "sets x \<subset> sets x \<union> sets y"
  3051       by auto
  3052     also have "\<dots> \<le> sigma (space x) (sets x \<union> sets y)"
  3053       by (subst sets_measure_of[OF *]) (rule sigma_sets_superset_generator)
  3054     finally show "x \<le> sigma (space x) (sets x \<union> sets y)"
  3055       by (simp add: space_measure_of[OF *] less_eq_measure.intros(2))
  3056   next
  3057     assume "\<not> space y \<subseteq> space x" "\<not> space x \<subseteq> space y"
  3058     then show "x \<le> sigma (space x \<union> space y) {}"
  3059       by (intro less_eq_measure.intros) auto
  3060   next
  3061     assume "sets x = sets y" then show "x \<le> sup_measure' x y"
  3062       by (simp add: le_measure le_emeasure_sup_measure'1)
  3063   qed (auto intro: less_eq_measure.intros)
  3064   show "y \<le> sup x y"
  3065     unfolding sup_measure_def
  3066   proof (intro le_sup_lexord)
  3067     assume **: "space x = space y"
  3068     then have *: "sets x \<union> sets y \<subseteq> Pow (space y)"
  3069       using sets.space_closed by auto
  3070     assume "\<not> sets y \<subseteq> sets x" "\<not> sets x \<subseteq> sets y"
  3071     then have "sets y \<subset> sets x \<union> sets y"
  3072       by auto
  3073     also have "\<dots> \<le> sigma (space y) (sets x \<union> sets y)"
  3074       by (subst sets_measure_of[OF *]) (rule sigma_sets_superset_generator)
  3075     finally show "y \<le> sigma (space x) (sets x \<union> sets y)"
  3076       by (simp add: ** space_measure_of[OF *] less_eq_measure.intros(2))
  3077   next
  3078     assume "\<not> space y \<subseteq> space x" "\<not> space x \<subseteq> space y"
  3079     then show "y \<le> sigma (space x \<union> space y) {}"
  3080       by (intro less_eq_measure.intros) auto
  3081   next
  3082     assume "sets x = sets y" then show "y \<le> sup_measure' x y"
  3083       by (simp add: le_measure le_emeasure_sup_measure'2)
  3084   qed (auto intro: less_eq_measure.intros)
  3085   show "x \<le> y \<Longrightarrow> z \<le> y \<Longrightarrow> sup x z \<le> y"
  3086     unfolding sup_measure_def
  3087   proof (intro sup_lexord[where P="\<lambda>x. x \<le> y"])
  3088     assume "x \<le> y" "z \<le> y" and [simp]: "space x = space z" "sets x = sets z"
  3089     from \<open>x \<le> y\<close> show "sup_measure' x z \<le> y"
  3090     proof cases
  3091       case 1 then show ?thesis
  3092         by (intro less_eq_measure.intros(1)) simp
  3093     next
  3094       case 2 then show ?thesis
  3095         by (intro less_eq_measure.intros(2)) simp_all
  3096     next
  3097       case 3 with \<open>z \<le> y\<close> \<open>x \<le> y\<close> show ?thesis
  3098         by (auto simp add: le_measure intro!: emeasure_sup_measure'_le2)
  3099     qed
  3100   next
  3101     assume **: "x \<le> y" "z \<le> y" "space x = space z" "\<not> sets z \<subseteq> sets x" "\<not> sets x \<subseteq> sets z"
  3102     then have *: "sets x \<union> sets z \<subseteq> Pow (space x)"
  3103       using sets.space_closed by auto
  3104     show "sigma (space x) (sets x \<union> sets z) \<le> y"
  3105       unfolding sigma_le_iff[OF *] using ** by (auto simp: le_measure_iff split: if_split_asm)
  3106   next
  3107     assume "x \<le> y" "z \<le> y" "\<not> space z \<subseteq> space x" "\<not> space x \<subseteq> space z"
  3108     then have "space x \<subseteq> space y" "space z \<subseteq> space y"
  3109       by (auto simp: le_measure_iff split: if_split_asm)
  3110     then show "sigma (space x \<union> space z) {} \<le> y"
  3111       by (simp add: sigma_le_iff)
  3112   qed
  3113 qed
  3115 end
  3117 lemma space_empty_eq_bot: "space a = {} \<longleftrightarrow> a = bot"
  3118   using space_empty[of a] by (auto intro!: measure_eqI)
  3120 lemma sets_eq_iff_bounded: "A \<le> B \<Longrightarrow> B \<le> C \<Longrightarrow> sets A = sets C \<Longrightarrow> sets B = sets A"
  3121   by (auto dest: sets_eq_imp_space_eq simp add: le_measure_iff split: if_split_asm)
  3123 lemma sets_sup: "sets A = sets M \<Longrightarrow> sets B = sets M \<Longrightarrow> sets (sup A B) = sets M"
  3124   by (auto simp add: sup_measure_def sup_lexord_def dest: sets_eq_imp_space_eq)
  3126 lemma le_measureD1: "A \<le> B \<Longrightarrow> space A \<le> space B"
  3127   by (auto simp: le_measure_iff split: if_split_asm)
  3129 lemma le_measureD2: "A \<le> B \<Longrightarrow> space A = space B \<Longrightarrow> sets A \<le> sets B"
  3130   by (auto simp: le_measure_iff split: if_split_asm)
  3132 lemma le_measureD3: "A \<le> B \<Longrightarrow> sets A = sets B \<Longrightarrow> emeasure A X \<le> emeasure B X"
  3133   by (auto simp: le_measure_iff le_fun_def dest: sets_eq_imp_space_eq split: if_split_asm)
  3135 lemma UN_space_closed: "\<Union>(sets ` S) \<subseteq> Pow (\<Union>(space ` S))"
  3136   using sets.space_closed by auto
  3138 definition Sup_lexord :: "('a \<Rightarrow> 'b::complete_lattice) \<Rightarrow> ('a set \<Rightarrow> 'a) \<Rightarrow> ('a set \<Rightarrow> 'a) \<Rightarrow> 'a set \<Rightarrow> 'a"
  3139 where
  3140   "Sup_lexord k c s A = (let U = (SUP a\<in>A. k a) in if \<exists>a\<in>A. k a = U then c {a\<in>A. k a = U} else s A)"
  3142 lemma Sup_lexord:
  3143   "(\<And>a S. a \<in> A \<Longrightarrow> k a = (SUP a\<in>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\<in>A. k a)) \<Longrightarrow> P (s A)) \<Longrightarrow>
  3144     P (Sup_lexord k c s A)"
  3145   by (auto simp: Sup_lexord_def Let_def)
  3147 lemma Sup_lexord1:
  3148   assumes A: "A \<noteq> {}" "(\<And>a. a \<in> A \<Longrightarrow> k a = (\<Union>a\<in>A. k a))" "P (c A)"
  3149   shows "P (Sup_lexord k c s A)"
  3150   unfolding Sup_lexord_def Let_def
  3151 proof (clarsimp, safe)
  3152   show "\<forall>a\<in>A. k a \<noteq> (\<Union>x\<in>A. k x) \<Longrightarrow> P (s A)"
  3153     by (metis assms(1,2) ex_in_conv)
  3154 next
  3155   fix a assume "a \<in> A" "k a = (\<Union>x\<in>A. k x)"
  3156   then have "{a \<in> A. k a = (\<Union>x\<in>A. k x)} = {a \<in> A. k a = k a}"
  3157     by (metis A(2)[symmetric])
  3158   then show "P (c {a \<in> A. k a = (\<Union>x\<in>A. k x)})"
  3159     by (simp add: A(3))
  3160 qed
  3162 instantiation measure :: (type) complete_lattice
  3163 begin
  3165 interpretation sup_measure: comm_monoid_set sup "bot :: 'a measure"
  3166   by standard (auto intro!: antisym)
  3168 lemma sup_measure_F_mono':
  3169   "finite J \<Longrightarrow> finite I \<Longrightarrow> sup_measure.F id I \<le> sup_measure.F id (I \<union> J)"
  3170 proof (induction J rule: finite_induct)
  3171   case empty then show ?case
  3172     by simp
  3173 next
  3174   case (insert i J)
  3175   show ?case
  3176   proof cases
  3177     assume "i \<in> I" with insert show ?thesis
  3178       by (auto simp: insert_absorb)
  3179   next
  3180     assume "i \<notin> I"
  3181     have "sup_measure.F id I \<le> sup_measure.F id (I \<union> J)"
  3182       by (intro insert)
  3183     also have "\<dots> \<le> sup_measure.F id (insert i (I \<union> J))"
  3184       using insert \<open>i \<notin> I\<close> by (subst sup_measure.insert) auto
  3185     finally show ?thesis
  3186       by auto
  3187   qed
  3188 qed
  3190 lemma sup_measure_F_mono: "finite I \<Longrightarrow> J \<subseteq> I \<Longrightarrow> sup_measure.F id J \<le> sup_measure.F id I"
  3191   using sup_measure_F_mono'[of I J] by (auto simp: finite_subset Un_absorb1)
  3193 lemma sets_sup_measure_F:
  3194   "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"
  3195   by (induction I rule: finite_ne_induct) (simp_all add: sets_sup)
  3197 definition%important Sup_measure' :: "'a measure set \<Rightarrow> 'a measure"
  3198 where
  3199   "Sup_measure' M = measure_of (\<Union>a\<in>M. space a) (\<Union>a\<in>M. sets a)
  3200     (\<lambda>X. (SUP P\<in>{P. finite P \<and> P \<subseteq> M }. sup_measure.F id P X))"
  3202 lemma space_Sup_measure'2: "space (Sup_measure' M) = (\<Union>m\<in>M. space m)"
  3203   unfolding Sup_measure'_def by (intro space_measure_of[OF UN_space_closed])
  3205 lemma sets_Sup_measure'2: "sets (Sup_measure' M) = sigma_sets (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m)"
  3206   unfolding Sup_measure'_def by (intro sets_measure_of[OF UN_space_closed])
  3208 lemma sets_Sup_measure':
  3209   assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "M \<noteq> {}"
  3210   shows "sets (Sup_measure' M) = sets A"
  3211   using sets_eq[THEN sets_eq_imp_space_eq, simp] \<open>M \<noteq> {}\<close> by (simp add: Sup_measure'_def)
  3213 lemma space_Sup_measure':
  3214   assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "M \<noteq> {}"
  3215   shows "space (Sup_measure' M) = space A"
  3216   using sets_eq[THEN sets_eq_imp_space_eq, simp] \<open>M \<noteq> {}\<close>
  3217   by (simp add: Sup_measure'_def )
  3219 lemma emeasure_Sup_measure':
  3220   assumes sets_eq[simp]: "\<And>m. m \<in> M \<Longrightarrow> sets m = sets A" and "X \<in> sets A" "M \<noteq> {}"
  3221   shows "emeasure (Sup_measure' M) X = (SUP P\<in>{P. finite P \<and> P \<subseteq> M}. sup_measure.F id P X)"
  3222     (is "_ = ?S X")
  3223   using Sup_measure'_def
  3224 proof (rule emeasure_measure_of)
  3225   note sets_eq[THEN sets_eq_imp_space_eq, simp]
  3226   have *: "sets (Sup_measure' M) = sets A" "space (Sup_measure' M) = space A"
  3227     using \<open>M \<noteq> {}\<close> by (simp_all add: Sup_measure'_def)
  3228   let ?\<mu> = "sup_measure.F id"
  3229   show "countably_additive (sets (Sup_measure' M)) ?S"
  3230   proof (rule countably_additiveI, goal_cases)
  3231     case (1 F)
  3232     then have **: "range F \<subseteq> sets A"
  3233       by (auto simp: *)
  3234     show "(\<Sum>i. ?S (F i)) = ?S (\<Union>i. F i)"
  3235     proof (subst ennreal_suminf_SUP_eq_directed)
  3236       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}"
  3237       have "(i \<noteq> {} \<longrightarrow> sets (?\<mu> i) = sets A) \<and> (j \<noteq> {} \<longrightarrow> sets (?\<mu> j) = sets A) \<and>
  3238         (i \<noteq> {} \<or> j \<noteq> {} \<longrightarrow> sets (?\<mu> (i \<union> j)) = sets A)"
  3239         using ij by (intro impI sets_sup_measure_F conjI) auto
  3240       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
  3241         using ij
  3242         by (cases "i = {}"; cases "j = {}")
  3243            (auto intro!: le_measureD3 sup_measure_F_mono simp: sets_sup_measure_F
  3244                  simp del: id_apply)
  3245       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)"
  3246         by (safe intro!: bexI[of _ "i \<union> j"]) auto
  3247     next
  3248       show "(SUP P \<in> {P. finite P \<and> P \<subseteq> M}. \<Sum>n. ?\<mu> P (F n)) = (SUP P \<in> {P. finite P \<and> P \<subseteq> M}. ?\<mu> P (\<Union>(F ` UNIV)))"
  3249       proof (intro SUP_cong refl)
  3250         fix i assume i: "i \<in> {P. finite P \<and> P \<subseteq> M}"
  3251         show "(\<Sum>n. ?\<mu> i (F n)) = ?\<mu> i (\<Union>(F ` UNIV))"
  3252         proof cases
  3253           assume "i \<noteq> {}" with i ** show ?thesis
  3254             apply (intro suminf_emeasure \<open>disjoint_family F\<close>)
  3255             apply (subst sets_sup_measure_F[OF _ _ sets_eq])
  3256             apply auto
  3257             done
  3258         qed simp
  3259       qed
  3260     qed
  3261   qed
  3262   show "positive (sets (Sup_measure' M)) ?S"
  3263     by (auto simp: positive_def bot_ennreal[symmetric])
  3264   show "X \<in> sets (Sup_measure' M)"
  3265     using assms * by auto
  3266 qed (rule UN_space_closed)
  3268 definition%important Sup_measure :: "'a measure set \<Rightarrow> 'a measure"
  3269 where
  3270   "Sup_measure = Sup_lexord space (Sup_lexord sets Sup_measure'
  3271     (\<lambda>U. sigma (\<Union>u\<in>U. space u) (\<Union>u\<in>U. sets u))) (\<lambda>U. sigma (\<Union>u\<in>U. space u) {})"
  3273 definition%important Inf_measure :: "'a measure set \<Rightarrow> 'a measure"
  3274 where
  3275   "Inf_measure A = Sup {x. \<forall>a\<in>A. x \<le> a}"
  3277 definition%important inf_measure :: "'a measure \<Rightarrow> 'a measure \<Rightarrow> 'a measure"
  3278 where
  3279   "inf_measure a b = Inf {a, b}"
  3281 definition%important top_measure :: "'a measure"
  3282 where
  3283   "top_measure = Inf {}"
  3285 instance
  3286 proof
  3287   note UN_space_closed [simp]
  3288   show upper: "x \<le> Sup A" if x: "x \<in> A" for x :: "'a measure" and A
  3289     unfolding Sup_measure_def
  3290   proof (intro Sup_lexord[where P="\<lambda>y. x \<le> y"])
  3291     assume "\<And>a. a \<in> A \<Longrightarrow> space a \<noteq> (\<Union>a\<in>A. space a)"
  3292     from this[OF \<open>x \<in> A\<close>] \<open>x \<in> A\<close> show "x \<le> sigma (\<Union>a\<in>A. space a) {}"
  3293       by (intro less_eq_measure.intros) auto
  3294   next
  3295     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}"
  3296       and neq: "\<And>aa. aa \<in> S \<Longrightarrow> sets aa \<noteq> (\<Union>a\<in>S. sets a)"
  3297     have sp_a: "space a = (\<Union>(space ` S))"
  3298       using \<open>a\<in>A\<close> by (auto simp: S)
  3299     show "x \<le> sigma (\<Union>(space ` S)) (\<Union>(sets ` S))"
  3300     proof cases
  3301       assume [simp]: "space x = space a"
  3302       have "sets x \<subset> (\<Union>a\<in>S. sets a)"
  3303         using \<open>x\<in>A\<close> neq[of x] by (auto simp: S)
  3304       also have "\<dots> \<subseteq> sigma_sets (\<Union>x\<in>S. space x) (\<Union>x\<in>S. sets x)"
  3305         by (rule sigma_sets_superset_generator)
  3306       finally show ?thesis
  3307         by (intro less_eq_measure.intros(2)) (simp_all add: sp_a)
  3308     next
  3309       assume "space x \<noteq> space a"
  3310       moreover have "space x \<le> space a"
  3311         unfolding a using \<open>x\<in>A\<close> by auto
  3312       ultimately show ?thesis
  3313         by (intro less_eq_measure.intros) (simp add: less_le sp_a)
  3314     qed
  3315   next
  3316     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}"
  3317       and "b \<in> S" and b: "sets b = (\<Union>a\<in>S. sets a)" and S': "S' = {a' \<in> S. sets a' = sets b}"
  3318     then have "S' \<noteq> {}" "space b = space a"
  3319       by auto
  3320     have sets_eq: "\<And>x. x \<in> S' \<Longrightarrow> sets x = sets b"
  3321       by (auto simp: S')
  3322     note sets_eq[THEN sets_eq_imp_space_eq, simp]
  3323     have *: "sets (Sup_measure' S') = sets b" "space (Sup_measure' S') = space b"
  3324       using \<open>S' \<noteq> {}\<close> by (simp_all add: Sup_measure'_def sets_eq)
  3325     show "x \<le> Sup_measure' S'"
  3326     proof cases
  3327       assume "x \<in> S"
  3328       with \<open>b \<in> S\<close> have "space x = space b"
  3329         by (simp add: S)
  3330       show ?thesis
  3331       proof cases
  3332         assume "x \<in> S'"
  3333         show "x \<le> Sup_measure' S'"
  3334         proof (intro le_measure[THEN iffD2] ballI)
  3335           show "sets x = sets (Sup_measure' S')"
  3336             using \<open>x\<in>S'\<close> * by (simp add: S')
  3337           fix X assume "X \<in> sets x"
  3338           show "emeasure x X \<le> emeasure (Sup_measure' S') X"
  3339           proof (subst emeasure_Sup_measure'[OF _ \<open>X \<in> sets x\<close>])
  3340             show "emeasure x X \<le> (SUP P \<in> {P. finite P \<and> P \<subseteq> S'}. emeasure (sup_measure.F id P) X)"
  3341               using \<open>x\<in>S'\<close> by (intro SUP_upper2[where i="{x}"]) auto
  3342           qed (insert \<open>x\<in>S'\<close> S', auto)
  3343         qed
  3344       next
  3345         assume "x \<notin> S'"
  3346         then have "sets x \<noteq> sets b"
  3347           using \<open>x\<in>S\<close> by (auto simp: S')
  3348         moreover have "sets x \<le> sets b"
  3349           using \<open>x\<in>S\<close> unfolding b by auto
  3350         ultimately show ?thesis
  3351           using * \<open>x \<in> S\<close>
  3352           by (intro less_eq_measure.intros(2))
  3353              (simp_all add: * \<open>space x = space b\<close> less_le)
  3354       qed
  3355     next
  3356       assume "x \<notin> S"
  3357       with \<open>x\<in>A\<close> \<open>x \<notin> S\<close> \<open>space b = space a\<close> show ?thesis
  3358         by (intro less_eq_measure.intros)
  3359            (simp_all add: * less_le a SUP_upper S)
  3360     qed
  3361   qed
  3362   show least: "Sup A \<le> x" if x: "\<And>z. z \<in> A \<Longrightarrow> z \<le> x" for x :: "'a measure" and A
  3363     unfolding Sup_measure_def
  3364   proof (intro Sup_lexord[where P="\<lambda>y. y \<le> x"])
  3365     assume "\<And>a. a \<in> A \<Longrightarrow> space a \<noteq> (\<Union>a\<in>A. space a)"
  3366     show "sigma (\<Union>(space ` A)) {} \<le> x"
  3367       using x[THEN le_measureD1] by (subst sigma_le_iff) auto
  3368   next
  3369     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}"
  3370       "\<And>a. a \<in> S \<Longrightarrow> sets a \<noteq> (\<Union>a\<in>S. sets a)"
  3371     have "\<Union>(space ` S) \<subseteq> space x"
  3372       using S le_measureD1[OF x] by auto
  3373     moreover
  3374     have "\<Union>(space ` S) = space a"
  3375       using \<open>a\<in>A\<close> S by auto
  3376     then have "space x = \<Union>(space ` S) \<Longrightarrow> \<Union>(sets ` S) \<subseteq> sets x"
  3377       using \<open>a \<in> A\<close> le_measureD2[OF x] by (auto simp: S)
  3378     ultimately show "sigma (\<Union>(space ` S)) (\<Union>(sets ` S)) \<le> x"
  3379       by (subst sigma_le_iff) simp_all
  3380   next
  3381     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}"
  3382       and "b \<in> S" and b: "sets b = (\<Union>a\<in>S. sets a)" and S': "S' = {a' \<in> S. sets a' = sets b}"
  3383     then have "S' \<noteq> {}" "space b = space a"
  3384       by auto
  3385     have sets_eq: "\<And>x. x \<in> S' \<Longrightarrow> sets x = sets b"
  3386       by (auto simp: S')
  3387     note sets_eq[THEN sets_eq_imp_space_eq, simp]
  3388     have *: "sets (Sup_measure' S') = sets b" "space (Sup_measure' S') = space b"
  3389       using \<open>S' \<noteq> {}\<close> by (simp_all add: Sup_measure'_def sets_eq)
  3390     show "Sup_measure' S' \<le> x"
  3391     proof cases
  3392       assume "space x = space a"
  3393       show ?thesis
  3394       proof cases
  3395         assume **: "sets x = sets b"
  3396         show ?thesis
  3397         proof (intro le_measure[THEN iffD2] ballI)
  3398           show ***: "sets (Sup_measure' S') = sets x"
  3399             by (simp add: * **)
  3400           fix X assume "X \<in> sets (Sup_measure' S')"
  3401           show "emeasure (Sup_measure' S') X \<le> emeasure x X"
  3402             unfolding ***
  3403           proof (subst emeasure_Sup_measure'[OF _ \<open>X \<in> sets (Sup_measure' S')\<close>])
  3404             show "(SUP P \<in> {P. finite P \<and> P \<subseteq> S'}. emeasure (sup_measure.F id P) X) \<le> emeasure x X"
  3405             proof (safe intro!: SUP_least)
  3406               fix P assume P: "finite P" "P \<subseteq> S'"
  3407               show "emeasure (sup_measure.F id P) X \<le> emeasure x X"
  3408               proof cases
  3409                 assume "P = {}" then show ?thesis
  3410                   by auto
  3411               next
  3412                 assume "P \<noteq> {}"
  3413                 from P have "finite P" "P \<subseteq> A"
  3414                   unfolding S' S by (simp_all add: subset_eq)
  3415                 then have "sup_measure.F id P \<le> x"
  3416                   by (induction P) (auto simp: x)
  3417                 moreover have "sets (sup_measure.F id P) = sets x"
  3418                   using \<open>finite P\<close> \<open>P \<noteq> {}\<close> \<open>P \<subseteq> S'\<close> \<open>sets x = sets b\<close>
  3419                   by (intro sets_sup_measure_F) (auto simp: S')
  3420                 ultimately show "emeasure (sup_measure.F id P) X \<le> emeasure x X"
  3421                   by (rule le_measureD3)
  3422               qed
  3423             qed
  3424             show "m \<in> S' \<Longrightarrow> sets m = sets (Sup_measure' S')" for m
  3425               unfolding * by (simp add: S')
  3426           qed fact
  3427         qed
  3428       next
  3429         assume "sets x \<noteq> sets b"
  3430         moreover have "sets b \<le> sets x"
  3431           unfolding b S using x[THEN le_measureD2] \<open>space x = space a\<close> by auto
  3432         ultimately show "Sup_measure' S' \<le> x"
  3433           using \<open>space x = space a\<close> \<open>b \<in> S\<close>
  3434           by (intro less_eq_measure.intros(2)) (simp_all add: * S)
  3435       qed
  3436     next
  3437       assume "space x \<noteq> space a"
  3438       then have "space a < space x"
  3439         using le_measureD1[OF x[OF \<open>a\<in>A\<close>]] by auto
  3440       then show "Sup_measure' S' \<le> x"
  3441         by (intro less_eq_measure.intros) (simp add: * \<open>space b = space a\<close>)
  3442     qed
  3443   qed
  3444   show "Sup {} = (bot::'a measure)" "Inf {} = (top::'a measure)"
  3445     by (auto intro!: antisym least simp: top_measure_def)
  3446   show lower: "x \<in> A \<Longrightarrow> Inf A \<le> x" for x :: "'a measure" and A
  3447     unfolding Inf_measure_def by (intro least) auto
  3448   show greatest: "(\<And>z. z \<in> A \<Longrightarrow> x \<le> z) \<Longrightarrow> x \<le> Inf A" for x :: "'a measure" and A
  3449     unfolding Inf_measure_def by (intro upper) auto
  3450   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"
  3451     by (auto simp: inf_measure_def intro!: lower greatest)
  3452 qed
  3454 end
  3456 lemma sets_SUP:
  3457   assumes "\<And>x. x \<in> I \<Longrightarrow> sets (M x) = sets N"
  3458   shows "I \<noteq> {} \<Longrightarrow> sets (SUP i\<in>I. M i) = sets N"
  3459   unfolding Sup_measure_def
  3460   using assms assms[THEN sets_eq_imp_space_eq]
  3461     sets_Sup_measure'[where A=N and M="M`I"]
  3462   by (intro Sup_lexord1[where P="\<lambda>x. sets x = sets N"]) auto
  3464 lemma emeasure_SUP:
  3465   assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets N" "X \<in> sets N" "I \<noteq> {}"
  3466   shows "emeasure (SUP i\<in>I. M i) X = (SUP J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emeasure (SUP i\<in>J. M i) X)"
  3467 proof -
  3468   interpret sup_measure: comm_monoid_set sup "bot :: 'b measure"
  3469     by standard (auto intro!: antisym)
  3470   have eq: "finite J \<Longrightarrow> sup_measure.F id J = (SUP i\<in>J. i)" for J :: "'b measure set"
  3471     by (induction J rule: finite_induct) auto
  3472   have 1: "J \<noteq> {} \<Longrightarrow> J \<subseteq> I \<Longrightarrow> sets (SUP x\<in>J. M x) = sets N" for J
  3473     by (intro sets_SUP sets) (auto )
  3474   from \<open>I \<noteq> {}\<close> obtain i where "i\<in>I" by auto
  3475   have "Sup_measure' (M`I) X = (SUP P\<in>{P. finite P \<and> P \<subseteq> M`I}. sup_measure.F id P X)"
  3476     using sets by (intro emeasure_Sup_measure') auto
  3477   also have "Sup_measure' (M`I) = (SUP i\<in>I. M i)"
  3478     unfolding Sup_measure_def using \<open>I \<noteq> {}\<close> sets sets(1)[THEN sets_eq_imp_space_eq]
  3479     by (intro Sup_lexord1[where P="\<lambda>x. _ = x"]) auto
  3480   also have "(SUP P\<in>{P. finite P \<and> P \<subseteq> M`I}. sup_measure.F id P X) =
  3481     (SUP J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. (SUP i\<in>J. M i) X)"
  3482   proof (intro SUP_eq)
  3483     fix J assume "J \<in> {P. finite P \<and> P \<subseteq> M`I}"
  3484     then obtain J' where J': "J' \<subseteq> I" "finite J'" and J: "J = M`J'" and "finite J"
  3485       using finite_subset_image[of J M I] by auto
  3486     show "\<exists>j\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. sup_measure.F id J X \<le> (SUP i\<in>j. M i) X"
  3487     proof cases
  3488       assume "J' = {}" with \<open>i \<in> I\<close> show ?thesis
  3489         by (auto simp add: J)
  3490     next
  3491       assume "J' \<noteq> {}" with J J' show ?thesis
  3492         by (intro bexI[of _ "J'"]) (auto simp add: eq simp del: id_apply)
  3493     qed
  3494   next
  3495     fix J assume J: "J \<in> {P. P \<noteq> {} \<and> finite P \<and> P \<subseteq> I}"
  3496     show "\<exists>J'\<in>{J. finite J \<and> J \<subseteq> M`I}. (SUP i\<in>J. M i) X \<le> sup_measure.F id J' X"
  3497       using J by (intro bexI[of _ "M`J"]) (auto simp add: eq simp del: id_apply)
  3498   qed
  3499   finally show ?thesis .
  3500 qed
  3502 lemma emeasure_SUP_chain:
  3503   assumes sets: "\<And>i. i \<in> A \<Longrightarrow> sets (M i) = sets N" "X \<in> sets N"
  3504   assumes ch: "Complete_Partial_Order.chain (\<le>) (M ` A)" and "A \<noteq> {}"
  3505   shows "emeasure (SUP i\<in>A. M i) X = (SUP i\<in>A. emeasure (M i) X)"
  3506 proof (subst emeasure_SUP[OF sets \<open>A \<noteq> {}\<close>])
  3507   show "(SUP J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> A}. emeasure (Sup (M ` J)) X) = (SUP i\<in>A. emeasure (M i) X)"
  3508   proof (rule SUP_eq)
  3509     fix J assume "J \<in> {J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> A}"
  3510     then have J: "Complete_Partial_Order.chain (\<le>) (M ` J)" "finite J" "J \<noteq> {}" and "J \<subseteq> A"
  3511       using ch[THEN chain_subset, of "M`J"] by auto
  3512     with in_chain_finite[OF J(1)] obtain j where "j \<in> J" "(SUP j\<in>J. M j) = M j"
  3513       by auto
  3514     with \<open>J \<subseteq> A\<close> show "\<exists>j\<in>A. emeasure (Sup (M ` J)) X \<le> emeasure (M j) X"
  3515       by auto
  3516   next
  3517     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 (Sup (M ` i)) X"
  3518       by (intro bexI[of _ "{j}"]) auto
  3519   qed
  3520 qed
  3522 subsubsection%unimportant \<open>Supremum of a set of $\sigma$-algebras\<close>
  3524 lemma space_Sup_eq_UN: "space (Sup M) = (\<Union>x\<in>M. space x)"
  3525   unfolding Sup_measure_def
  3526   apply (intro Sup_lexord[where P="\<lambda>x. space x = _"])
  3527   apply (subst space_Sup_measure'2)
  3528   apply auto []
  3529   apply (subst space_measure_of[OF UN_space_closed])
  3530   apply auto
  3531   done
  3533 lemma sets_Sup_eq:
  3534   assumes *: "\<And>m. m \<in> M \<Longrightarrow> space m = X" and "M \<noteq> {}"
  3535   shows "sets (Sup M) = sigma_sets X (\<Union>x\<in>M. sets x)"
  3536   unfolding Sup_measure_def
  3537   apply (rule Sup_lexord1)
  3538   apply fact
  3539   apply (simp add: assms)
  3540   apply (rule Sup_lexord)
  3541   subgoal premises that for a S
  3542     unfolding that(3) that(2)[symmetric]
  3543     using that(1)
  3544     apply (subst sets_Sup_measure'2)
  3545     apply (intro arg_cong2[where f=sigma_sets])
  3546     apply (auto simp: *)
  3547     done
  3548   apply (subst sets_measure_of[OF UN_space_closed])
  3549   apply (simp add:  assms)
  3550   done
  3552 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)"
  3553   by (subst sets_Sup_eq[where X=X]) auto
  3555 lemma Sup_lexord_rel:
  3556   assumes "\<And>i. i \<in> I \<Longrightarrow> k (A i) = k (B i)"
  3557     "R (c (A ` {a \<in> I. k (B a) = (SUP x\<in>I. k (B x))})) (c (B ` {a \<in> I. k (B a) = (SUP x\<in>I. k (B x))}))"
  3558     "R (s (A`I)) (s (B`I))"
  3559   shows "R (Sup_lexord k c s (A`I)) (Sup_lexord k c s (B`I))"
  3560 proof -
  3561   have "A ` {a \<in> I. k (B a) = (SUP x\<in>I. k (B x))} =  {a \<in> A ` I. k a = (SUP x\<in>I. k (B x))}"
  3562     using assms(1) by auto
  3563   moreover have "B ` {a \<in> I. k (B a) = (SUP x\<in>I. k (B x))} =  {a \<in> B ` I. k a = (SUP x\<in>I. k (B x))}"
  3564     by auto
  3565   ultimately show ?thesis
  3566     using assms by (auto simp: Sup_lexord_def Let_def)
  3567 qed
  3569 lemma sets_SUP_cong:
  3570   assumes eq: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i)" shows "sets (SUP i\<in>I. M i) = sets (SUP i\<in>I. N i)"
  3571   unfolding Sup_measure_def
  3572   using eq eq[THEN sets_eq_imp_space_eq]
  3573   apply (intro Sup_lexord_rel[where R="\<lambda>x y. sets x = sets y"])
  3574   apply simp
  3575   apply simp
  3576   apply (simp add: sets_Sup_measure'2)
  3577   apply (intro arg_cong2[where f="\<lambda>x y. sets (sigma x y)"])
  3578   apply auto
  3579   done
  3581 lemma sets_Sup_in_sets:
  3582   assumes "M \<noteq> {}"
  3583   assumes "\<And>m. m \<in> M \<Longrightarrow> space m = space N"
  3584   assumes "\<And>m. m \<in> M \<Longrightarrow> sets m \<subseteq> sets N"
  3585   shows "sets (Sup M) \<subseteq> sets N"
  3586 proof -
  3587   have *: "\<Union>(space ` M) = space N"
  3588     using assms by auto
  3589   show ?thesis
  3590     unfolding * using assms by (subst sets_Sup_eq[of M "space N"]) (auto intro!: sets.sigma_sets_subset)
  3591 qed
  3593 lemma measurable_Sup1:
  3594   assumes m: "m \<in> M" and f: "f \<in> measurable m N"
  3595     and const_space: "\<And>m n. m \<in> M \<Longrightarrow> n \<in> M \<Longrightarrow> space m = space n"
  3596   shows "f \<in> measurable (Sup M) N"
  3597 proof -
  3598   have "space (Sup M) = space m"
  3599     using m by (auto simp add: space_Sup_eq_UN dest: const_space)
  3600   then show ?thesis
  3601     using m f unfolding measurable_def by (auto intro: in_sets_Sup[OF const_space])
  3602 qed
  3604 lemma measurable_Sup2:
  3605   assumes M: "M \<noteq> {}"
  3606   assumes f: "\<And>m. m \<in> M \<Longrightarrow> f \<in> measurable N m"
  3607     and const_space: "\<And>m n. m \<in> M \<Longrightarrow> n \<in> M \<Longrightarrow> space m = space n"
  3608   shows "f \<in> measurable N (Sup M)"
  3609 proof -
  3610   from M obtain m where "m \<in> M" by auto
  3611   have space_eq: "\<And>n. n \<in> M \<Longrightarrow> space n = space m"
  3612     by (intro const_space \<open>m \<in> M\<close>)
  3613   have "f \<in> measurable N (sigma (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m))"
  3614   proof (rule measurable_measure_of)
  3615     show "f \<in> space N \<rightarrow> \<Union>(space ` M)"
  3616       using measurable_space[OF f] M by auto
  3617   qed (auto intro: measurable_sets f dest: sets.sets_into_space)
  3618   also have "measurable N (sigma (\<Union>m\<in>M. space m) (\<Union>m\<in>M. sets m)) = measurable N (Sup M)"
  3619     apply (intro measurable_cong_sets refl)
  3620     apply (subst sets_Sup_eq[OF space_eq M])
  3621     apply simp
  3622     apply (subst sets_measure_of[OF UN_space_closed])
  3623     apply (simp add: space_eq M)
  3624     done
  3625   finally show ?thesis .
  3626 qed
  3628 lemma measurable_SUP2:
  3629   "I \<noteq> {} \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f \<in> measurable N (M i)) \<Longrightarrow>
  3630     (\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> space (M i) = space (M j)) \<Longrightarrow> f \<in> measurable N (SUP i\<in>I. M i)"
  3631   by (auto intro!: measurable_Sup2)
  3633 lemma sets_Sup_sigma:
  3634   assumes [simp]: "M \<noteq> {}" and M: "\<And>m. m \<in> M \<Longrightarrow> m \<subseteq> Pow \<Omega>"
  3635   shows "sets (SUP m\<in>M. sigma \<Omega> m) = sets (sigma \<Omega> (\<Union>M))"
  3636 proof -
  3637   { fix a m assume "a \<in> sigma_sets \<Omega> m" "m \<in> M"
  3638     then have "a \<in> sigma_sets \<Omega> (\<Union>M)"
  3639      by induction (auto intro: sigma_sets.intros(2-)) }
  3640   then show "sets (SUP m\<in>M. sigma \<Omega> m) = sets (sigma \<Omega> (\<Union>M))"
  3641     apply (subst sets_Sup_eq[where X="\<Omega>"])
  3642     apply (auto simp add: M) []
  3643     apply auto []
  3644     apply (simp add: space_measure_of_conv M Union_least)
  3645     apply (rule sigma_sets_eqI)
  3646     apply auto
  3647     done
  3648 qed
  3650 lemma Sup_sigma:
  3651   assumes [simp]: "M \<noteq> {}" and M: "\<And>m. m \<in> M \<Longrightarrow> m \<subseteq> Pow \<Omega>"
  3652   shows "(SUP m\<in>M. sigma \<Omega> m) = (sigma \<Omega> (\<Union>M))"
  3653 proof (intro antisym SUP_least)
  3654   have *: "\<Union>M \<subseteq> Pow \<Omega>"
  3655     using M by auto
  3656   show "sigma \<Omega> (\<Union>M) \<le> (SUP m\<in>M. sigma \<Omega> m)"
  3657   proof (intro less_eq_measure.intros(3))
  3658     show "space (sigma \<Omega> (\<Union>M)) = space (SUP m\<in>M. sigma \<Omega> m)"
  3659       "sets (sigma \<Omega> (\<Union>M)) = sets (SUP m\<in>M. sigma \<Omega> m)"
  3660       using sets_Sup_sigma[OF assms] sets_Sup_sigma[OF assms, THEN sets_eq_imp_space_eq]
  3661       by auto
  3662   qed (simp add: emeasure_sigma le_fun_def)
  3663   fix m assume "m \<in> M" then show "sigma \<Omega> m \<le> sigma \<Omega> (\<Union>M)"
  3664     by (subst sigma_le_iff) (auto simp add: M *)
  3665 qed
  3667 lemma SUP_sigma_sigma:
  3668   "M \<noteq> {} \<Longrightarrow> (\<And>m. m \<in> M \<Longrightarrow> f m \<subseteq> Pow \<Omega>) \<Longrightarrow> (SUP m\<in>M. sigma \<Omega> (f m)) = sigma \<Omega> (\<Union>m\<in>M. f m)"
  3669   using Sup_sigma[of "f`M" \<Omega>] by auto
  3671 lemma sets_vimage_Sup_eq:
  3672   assumes *: "M \<noteq> {}" "f \<in> X \<rightarrow> Y" "\<And>m. m \<in> M \<Longrightarrow> space m = Y"
  3673   shows "sets (vimage_algebra X f (Sup M)) = sets (SUP m \<in> M. vimage_algebra X f m)"
  3674   (is "?IS = ?SI")
  3675 proof
  3676   show "?IS \<subseteq> ?SI"
  3677     apply (intro sets_image_in_sets measurable_Sup2)
  3678     apply (simp add: space_Sup_eq_UN *)
  3679     apply (simp add: *)
  3680     apply (intro measurable_Sup1)
  3681     apply (rule imageI)
  3682     apply assumption
  3683     apply (rule measurable_vimage_algebra1)
  3684     apply (auto simp: *)
  3685     done
  3686   show "?SI \<subseteq> ?IS"
  3687     apply (intro sets_Sup_in_sets)
  3688     apply (auto simp: *) []
  3689     apply (auto simp: *) []
  3690     apply (elim imageE)
  3691     apply simp
  3692     apply (rule sets_image_in_sets)
  3693     apply simp
  3694     apply (simp add: measurable_def)
  3695     apply (simp add: * space_Sup_eq_UN sets_vimage_algebra2)
  3696     apply (auto intro: in_sets_Sup[OF *(3)])
  3697     done
  3698 qed
  3700 lemma restrict_space_eq_vimage_algebra':
  3701   "sets (restrict_space M \<Omega>) = sets (vimage_algebra (\<Omega> \<inter> space M) (\<lambda>x. x) M)"
  3702 proof -
  3703   have *: "{A \<inter> (\<Omega> \<inter> space M) |A. A \<in> sets M} = {A \<inter> \<Omega> |A. A \<in> sets M}"
  3704     using sets.sets_into_space[of _ M] by blast
  3706   show ?thesis
  3707     unfolding restrict_space_def
  3708     by (subst sets_measure_of)
  3709        (auto simp add: image_subset_iff sets_vimage_algebra * dest: sets.sets_into_space intro!: arg_cong2[where f=sigma_sets])
  3710 qed
  3712 lemma sigma_le_sets:
  3713   assumes [simp]: "A \<subseteq> Pow X" shows "sets (sigma X A) \<subseteq> sets N \<longleftrightarrow> X \<in> sets N \<and> A \<subseteq> sets N"
  3714 proof
  3715   have "X \<in> sigma_sets X A" "A \<subseteq> sigma_sets X A"
  3716     by (auto intro: sigma_sets_top)
  3717   moreover assume "sets (sigma X A) \<subseteq> sets N"
  3718   ultimately show "X \<in> sets N \<and> A \<subseteq> sets N"
  3719     by auto
  3720 next
  3721   assume *: "X \<in> sets N \<and> A \<subseteq> sets N"
  3722   { fix Y assume "Y \<in> sigma_sets X A" from this * have "Y \<in> sets N"
  3723       by induction auto }
  3724   then show "sets (sigma X A) \<subseteq> sets N"
  3725     by auto
  3726 qed
  3728 lemma measurable_iff_sets:
  3729   "f \<in> measurable M N \<longleftrightarrow> (f \<in> space M \<rightarrow> space N \<and> sets (vimage_algebra (space M) f N) \<subseteq> sets M)"
  3730 proof -
  3731   have *: "{f -` A \<inter> space M |A. A \<in> sets N} \<subseteq> Pow (space M)"
  3732     by auto
  3733   show ?thesis
  3734     unfolding measurable_def
  3735     by (auto simp add: vimage_algebra_def sigma_le_sets[OF *])
  3736 qed
  3738 lemma sets_vimage_algebra_space: "X \<in> sets (vimage_algebra X f M)"
  3739   using[of "vimage_algebra X f M"] by simp
  3741 lemma measurable_mono:
  3742   assumes N: "sets N' \<le> sets N" "space N = space N'"
  3743   assumes M: "sets M \<le> sets M'" "space M = space M'"
  3744   shows "measurable M N \<subseteq> measurable M' N'"
  3745   unfolding measurable_def
  3746 proof safe
  3747   fix f A assume "f \<in> space M \<rightarrow> space N" "A \<in> sets N'"
  3748   moreover assume "\<forall>y\<in>sets N. f -` y \<inter> space M \<in> sets M" note this[THEN bspec, of A]
  3749   ultimately show "f -` A \<inter> space M' \<in> sets M'"
  3750     using assms by auto
  3751 qed (insert N M, auto)
  3753 lemma measurable_Sup_measurable:
  3754   assumes f: "f \<in> space N \<rightarrow> A"
  3755   shows "f \<in> measurable N (Sup {M. space M = A \<and> f \<in> measurable N M})"
  3756 proof (rule measurable_Sup2)
  3757   show "{M. space M = A \<and> f \<in> measurable N M} \<noteq> {}"
  3758     using f unfolding ex_in_conv[symmetric]
  3759     by (intro exI[of _ "sigma A {}"]) (auto intro!: measurable_measure_of)
  3760 qed auto
  3762 lemma (in sigma_algebra) sigma_sets_subset':
  3763   assumes a: "a \<subseteq> M" "\<Omega>' \<in> M"
  3764   shows "sigma_sets \<Omega>' a \<subseteq> M"
  3765 proof
  3766   show "x \<in> M" if x: "x \<in> sigma_sets \<Omega>' a" for x
  3767     using x by (induct rule: sigma_sets.induct) (insert a, auto)
  3768 qed
  3770 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\<in>I. M i)"
  3771   by (intro in_sets_Sup[where X=Y]) auto
  3773 lemma measurable_SUP1:
  3774   "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>
  3775     f \<in> measurable (SUP i\<in>I. M i) N"
  3776   by (auto intro: measurable_Sup1)
  3778 lemma sets_image_in_sets':
  3779   assumes X: "X \<in> sets N"
  3780   assumes f: "\<And>A. A \<in> sets M \<Longrightarrow> f -` A \<inter> X \<in> sets N"
  3781   shows "sets (vimage_algebra X f M) \<subseteq> sets N"
  3782   unfolding sets_vimage_algebra
  3783   by (rule sets.sigma_sets_subset') (auto intro!: measurable_sets X f)
  3785 lemma mono_vimage_algebra:
  3786   "sets M \<le> sets N \<Longrightarrow> sets (vimage_algebra X f M) \<subseteq> sets (vimage_algebra X f N)"
  3787   using[of "sigma X {f -` A \<inter> X |A. A \<in> sets N}"]
  3788   unfolding vimage_algebra_def
  3789   apply (subst (asm) space_measure_of)
  3790   apply auto []
  3791   apply (subst sigma_le_sets)
  3792   apply auto
  3793   done
  3795 lemma mono_restrict_space: "sets M \<le> sets N \<Longrightarrow> sets (restrict_space M X) \<subseteq> sets (restrict_space N X)"
  3796   unfolding sets_restrict_space by (rule image_mono)
  3798 lemma sets_eq_bot: "sets M = {{}} \<longleftrightarrow> M = bot"
  3799   apply safe
  3800   apply (intro measure_eqI)
  3801   apply auto
  3802   done
  3804 lemma sets_eq_bot2: "{{}} = sets M \<longleftrightarrow> M = bot"
  3805   using sets_eq_bot[of M] by blast
  3808 lemma (in finite_measure) countable_support:
  3809   "countable {x. measure M {x} \<noteq> 0}"
  3810 proof cases
  3811   assume "measure M (space M) = 0"
  3812   with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
  3813     by auto
  3814   then show ?thesis
  3815     by simp
  3816 next
  3817   let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
  3818   assume "?M \<noteq> 0"
  3819   then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
  3820     using reals_Archimedean[of "?m x / ?M" for x]
  3821     by (auto simp: field_simps not_le[symmetric] divide_le_0_iff measure_le_0_iff)
  3822   have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
  3823   proof (rule ccontr)
  3824     fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
  3825     then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
  3826       by (metis infinite_arbitrarily_large)
  3827     from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x"
  3828       by auto
  3829     { fix x assume "x \<in> X"
  3830       from \<open>?M \<noteq> 0\<close> *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
  3831       then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
  3832     note singleton_sets = this
  3833     have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
  3834       using \<open>?M \<noteq> 0\<close>
  3835       by (simp add: \<open>card X = Suc (Suc n)\<close> field_simps less_le)
  3836     also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
  3837       by (rule sum_mono) fact
  3838     also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
  3839       using singleton_sets \<open>finite X\<close>
  3840       by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
  3841     finally have "?M < measure M (\<Union>x\<in>X. {x})" .
  3842     moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
  3843       using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
  3844     ultimately show False by simp
  3845   qed
  3846   show ?thesis
  3847     unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
  3848 qed
  3850 end