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