src/HOL/Probability/Caratheodory.thy
author haftmann
Sun Mar 16 18:09:04 2014 +0100 (2014-03-16)
changeset 56166 9a241bc276cd
parent 55642 63beb38e9258
child 56212 3253aaf73a01
permissions -rw-r--r--
normalising simp rules for compound operators
     1 (*  Title:      HOL/Probability/Caratheodory.thy
     2     Author:     Lawrence C Paulson
     3     Author:     Johannes Hölzl, TU München
     4 *)
     5 
     6 header {*Caratheodory Extension Theorem*}
     7 
     8 theory Caratheodory
     9   imports Measure_Space
    10 begin
    11 
    12 text {*
    13   Originally from the Hurd/Coble measure theory development, translated by Lawrence Paulson.
    14 *}
    15 
    16 lemma suminf_ereal_2dimen:
    17   fixes f:: "nat \<times> nat \<Rightarrow> ereal"
    18   assumes pos: "\<And>p. 0 \<le> f p"
    19   assumes "\<And>m. g m = (\<Sum>n. f (m,n))"
    20   shows "(\<Sum>i. f (prod_decode i)) = suminf g"
    21 proof -
    22   have g_def: "g = (\<lambda>m. (\<Sum>n. f (m,n)))"
    23     using assms by (simp add: fun_eq_iff)
    24   have reindex: "\<And>B. (\<Sum>x\<in>B. f (prod_decode x)) = setsum f (prod_decode ` B)"
    25     by (simp add: setsum_reindex[OF inj_prod_decode] comp_def)
    26   { fix n
    27     let ?M = "\<lambda>f. Suc (Max (f ` prod_decode ` {..<n}))"
    28     { fix a b x assume "x < n" and [symmetric]: "(a, b) = prod_decode x"
    29       then have "a < ?M fst" "b < ?M snd"
    30         by (auto intro!: Max_ge le_imp_less_Suc image_eqI) }
    31     then have "setsum f (prod_decode ` {..<n}) \<le> setsum f ({..<?M fst} \<times> {..<?M snd})"
    32       by (auto intro!: setsum_mono3 simp: pos)
    33     then have "\<exists>a b. setsum f (prod_decode ` {..<n}) \<le> setsum f ({..<a} \<times> {..<b})" by auto }
    34   moreover
    35   { fix a b
    36     let ?M = "prod_decode ` {..<Suc (Max (prod_encode ` ({..<a} \<times> {..<b})))}"
    37     { fix a' b' assume "a' < a" "b' < b" then have "(a', b') \<in> ?M"
    38         by (auto intro!: Max_ge le_imp_less_Suc image_eqI[where x="prod_encode (a', b')"]) }
    39     then have "setsum f ({..<a} \<times> {..<b}) \<le> setsum f ?M"
    40       by (auto intro!: setsum_mono3 simp: pos) }
    41   ultimately
    42   show ?thesis unfolding g_def using pos
    43     by (auto intro!: SUPR_eq  simp: setsum_cartesian_product reindex SUP_upper2
    44                      setsum_nonneg suminf_ereal_eq_SUPR SUPR_pair
    45                      SUPR_ereal_setsum[symmetric] incseq_setsumI setsum_nonneg)
    46 qed
    47 
    48 subsection {* Measure Spaces *}
    49 
    50 definition subadditive where "subadditive M f \<longleftrightarrow>
    51   (\<forall>x\<in>M. \<forall>y\<in>M. x \<inter> y = {} \<longrightarrow> f (x \<union> y) \<le> f x + f y)"
    52 
    53 definition countably_subadditive where "countably_subadditive M f \<longleftrightarrow>
    54   (\<forall>A. range A \<subseteq> M \<longrightarrow> disjoint_family A \<longrightarrow> (\<Union>i. A i) \<in> M \<longrightarrow>
    55     (f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))))"
    56 
    57 definition lambda_system where "lambda_system \<Omega> M f = {l \<in> M.
    58   \<forall>x \<in> M. f (l \<inter> x) + f ((\<Omega> - l) \<inter> x) = f x}"
    59 
    60 definition outer_measure_space where "outer_measure_space M f \<longleftrightarrow>
    61   positive M f \<and> increasing M f \<and> countably_subadditive M f"
    62 
    63 definition measure_set where "measure_set M f X = {r.
    64   \<exists>A. range A \<subseteq> M \<and> disjoint_family A \<and> X \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
    65 
    66 lemma subadditiveD:
    67   "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"
    68   by (auto simp add: subadditive_def)
    69 
    70 subsection {* Lambda Systems *}
    71 
    72 lemma (in algebra) lambda_system_eq:
    73   shows "lambda_system \<Omega> M f = {l \<in> M. \<forall>x \<in> M. f (x \<inter> l) + f (x - l) = f x}"
    74 proof -
    75   have [simp]: "!!l x. l \<in> M \<Longrightarrow> x \<in> M \<Longrightarrow> (\<Omega> - l) \<inter> x = x - l"
    76     by (metis Int_Diff Int_absorb1 Int_commute sets_into_space)
    77   show ?thesis
    78     by (auto simp add: lambda_system_def) (metis Int_commute)+
    79 qed
    80 
    81 lemma (in algebra) lambda_system_empty:
    82   "positive M f \<Longrightarrow> {} \<in> lambda_system \<Omega> M f"
    83   by (auto simp add: positive_def lambda_system_eq)
    84 
    85 lemma lambda_system_sets:
    86   "x \<in> lambda_system \<Omega> M f \<Longrightarrow> x \<in> M"
    87   by (simp add: lambda_system_def)
    88 
    89 lemma (in algebra) lambda_system_Compl:
    90   fixes f:: "'a set \<Rightarrow> ereal"
    91   assumes x: "x \<in> lambda_system \<Omega> M f"
    92   shows "\<Omega> - x \<in> lambda_system \<Omega> M f"
    93 proof -
    94   have "x \<subseteq> \<Omega>"
    95     by (metis sets_into_space lambda_system_sets x)
    96   hence "\<Omega> - (\<Omega> - x) = x"
    97     by (metis double_diff equalityE)
    98   with x show ?thesis
    99     by (force simp add: lambda_system_def ac_simps)
   100 qed
   101 
   102 lemma (in algebra) lambda_system_Int:
   103   fixes f:: "'a set \<Rightarrow> ereal"
   104   assumes xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   105   shows "x \<inter> y \<in> lambda_system \<Omega> M f"
   106 proof -
   107   from xl yl show ?thesis
   108   proof (auto simp add: positive_def lambda_system_eq Int)
   109     fix u
   110     assume x: "x \<in> M" and y: "y \<in> M" and u: "u \<in> M"
   111        and fx: "\<forall>z\<in>M. f (z \<inter> x) + f (z - x) = f z"
   112        and fy: "\<forall>z\<in>M. f (z \<inter> y) + f (z - y) = f z"
   113     have "u - x \<inter> y \<in> M"
   114       by (metis Diff Diff_Int Un u x y)
   115     moreover
   116     have "(u - (x \<inter> y)) \<inter> y = u \<inter> y - x" by blast
   117     moreover
   118     have "u - x \<inter> y - y = u - y" by blast
   119     ultimately
   120     have ey: "f (u - x \<inter> y) = f (u \<inter> y - x) + f (u - y)" using fy
   121       by force
   122     have "f (u \<inter> (x \<inter> y)) + f (u - x \<inter> y)
   123           = (f (u \<inter> (x \<inter> y)) + f (u \<inter> y - x)) + f (u - y)"
   124       by (simp add: ey ac_simps)
   125     also have "... =  (f ((u \<inter> y) \<inter> x) + f (u \<inter> y - x)) + f (u - y)"
   126       by (simp add: Int_ac)
   127     also have "... = f (u \<inter> y) + f (u - y)"
   128       using fx [THEN bspec, of "u \<inter> y"] Int y u
   129       by force
   130     also have "... = f u"
   131       by (metis fy u)
   132     finally show "f (u \<inter> (x \<inter> y)) + f (u - x \<inter> y) = f u" .
   133   qed
   134 qed
   135 
   136 lemma (in algebra) lambda_system_Un:
   137   fixes f:: "'a set \<Rightarrow> ereal"
   138   assumes xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   139   shows "x \<union> y \<in> lambda_system \<Omega> M f"
   140 proof -
   141   have "(\<Omega> - x) \<inter> (\<Omega> - y) \<in> M"
   142     by (metis Diff_Un Un compl_sets lambda_system_sets xl yl)
   143   moreover
   144   have "x \<union> y = \<Omega> - ((\<Omega> - x) \<inter> (\<Omega> - y))"
   145     by auto (metis subsetD lambda_system_sets sets_into_space xl yl)+
   146   ultimately show ?thesis
   147     by (metis lambda_system_Compl lambda_system_Int xl yl)
   148 qed
   149 
   150 lemma (in algebra) lambda_system_algebra:
   151   "positive M f \<Longrightarrow> algebra \<Omega> (lambda_system \<Omega> M f)"
   152   apply (auto simp add: algebra_iff_Un)
   153   apply (metis lambda_system_sets set_mp sets_into_space)
   154   apply (metis lambda_system_empty)
   155   apply (metis lambda_system_Compl)
   156   apply (metis lambda_system_Un)
   157   done
   158 
   159 lemma (in algebra) lambda_system_strong_additive:
   160   assumes z: "z \<in> M" and disj: "x \<inter> y = {}"
   161       and xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   162   shows "f (z \<inter> (x \<union> y)) = f (z \<inter> x) + f (z \<inter> y)"
   163 proof -
   164   have "z \<inter> x = (z \<inter> (x \<union> y)) \<inter> x" using disj by blast
   165   moreover
   166   have "z \<inter> y = (z \<inter> (x \<union> y)) - x" using disj by blast
   167   moreover
   168   have "(z \<inter> (x \<union> y)) \<in> M"
   169     by (metis Int Un lambda_system_sets xl yl z)
   170   ultimately show ?thesis using xl yl
   171     by (simp add: lambda_system_eq)
   172 qed
   173 
   174 lemma (in algebra) lambda_system_additive: "additive (lambda_system \<Omega> M f) f"
   175 proof (auto simp add: additive_def)
   176   fix x and y
   177   assume disj: "x \<inter> y = {}"
   178      and xl: "x \<in> lambda_system \<Omega> M f" and yl: "y \<in> lambda_system \<Omega> M f"
   179   hence  "x \<in> M" "y \<in> M" by (blast intro: lambda_system_sets)+
   180   thus "f (x \<union> y) = f x + f y"
   181     using lambda_system_strong_additive [OF top disj xl yl]
   182     by (simp add: Un)
   183 qed
   184 
   185 lemma (in ring_of_sets) countably_subadditive_subadditive:
   186   assumes f: "positive M f" and cs: "countably_subadditive M f"
   187   shows  "subadditive M f"
   188 proof (auto simp add: subadditive_def)
   189   fix x y
   190   assume x: "x \<in> M" and y: "y \<in> M" and "x \<inter> y = {}"
   191   hence "disjoint_family (binaryset x y)"
   192     by (auto simp add: disjoint_family_on_def binaryset_def)
   193   hence "range (binaryset x y) \<subseteq> M \<longrightarrow>
   194          (\<Union>i. binaryset x y i) \<in> M \<longrightarrow>
   195          f (\<Union>i. binaryset x y i) \<le> (\<Sum> n. f (binaryset x y n))"
   196     using cs by (auto simp add: countably_subadditive_def)
   197   hence "{x,y,{}} \<subseteq> M \<longrightarrow> x \<union> y \<in> M \<longrightarrow>
   198          f (x \<union> y) \<le> (\<Sum> n. f (binaryset x y n))"
   199     by (simp add: range_binaryset_eq UN_binaryset_eq)
   200   thus "f (x \<union> y) \<le>  f x + f y" using f x y
   201     by (auto simp add: Un o_def suminf_binaryset_eq positive_def)
   202 qed
   203 
   204 lemma lambda_system_increasing:
   205  "increasing M f \<Longrightarrow> increasing (lambda_system \<Omega> M f) f"
   206   by (simp add: increasing_def lambda_system_def)
   207 
   208 lemma lambda_system_positive:
   209   "positive M f \<Longrightarrow> positive (lambda_system \<Omega> M f) f"
   210   by (simp add: positive_def lambda_system_def)
   211 
   212 lemma (in algebra) lambda_system_strong_sum:
   213   fixes A:: "nat \<Rightarrow> 'a set" and f :: "'a set \<Rightarrow> ereal"
   214   assumes f: "positive M f" and a: "a \<in> M"
   215       and A: "range A \<subseteq> lambda_system \<Omega> M f"
   216       and disj: "disjoint_family A"
   217   shows  "(\<Sum>i = 0..<n. f (a \<inter>A i)) = f (a \<inter> (\<Union>i\<in>{0..<n}. A i))"
   218 proof (induct n)
   219   case 0 show ?case using f by (simp add: positive_def)
   220 next
   221   case (Suc n)
   222   have 2: "A n \<inter> UNION {0..<n} A = {}" using disj
   223     by (force simp add: disjoint_family_on_def neq_iff)
   224   have 3: "A n \<in> lambda_system \<Omega> M f" using A
   225     by blast
   226   interpret l: algebra \<Omega> "lambda_system \<Omega> M f"
   227     using f by (rule lambda_system_algebra)
   228   have 4: "UNION {0..<n} A \<in> lambda_system \<Omega> M f"
   229     using A l.UNION_in_sets by simp
   230   from Suc.hyps show ?case
   231     by (simp add: atLeastLessThanSuc lambda_system_strong_additive [OF a 2 3 4])
   232 qed
   233 
   234 lemma (in sigma_algebra) lambda_system_caratheodory:
   235   assumes oms: "outer_measure_space M f"
   236       and A: "range A \<subseteq> lambda_system \<Omega> M f"
   237       and disj: "disjoint_family A"
   238   shows  "(\<Union>i. A i) \<in> lambda_system \<Omega> M f \<and> (\<Sum>i. f (A i)) = f (\<Union>i. A i)"
   239 proof -
   240   have pos: "positive M f" and inc: "increasing M f"
   241    and csa: "countably_subadditive M f"
   242     by (metis oms outer_measure_space_def)+
   243   have sa: "subadditive M f"
   244     by (metis countably_subadditive_subadditive csa pos)
   245   have A': "\<And>S. A`S \<subseteq> (lambda_system \<Omega> M f)" using A
   246     by auto
   247   interpret ls: algebra \<Omega> "lambda_system \<Omega> M f"
   248     using pos by (rule lambda_system_algebra)
   249   have A'': "range A \<subseteq> M"
   250      by (metis A image_subset_iff lambda_system_sets)
   251 
   252   have U_in: "(\<Union>i. A i) \<in> M"
   253     by (metis A'' countable_UN)
   254   have U_eq: "f (\<Union>i. A i) = (\<Sum>i. f (A i))"
   255   proof (rule antisym)
   256     show "f (\<Union>i. A i) \<le> (\<Sum>i. f (A i))"
   257       using csa[unfolded countably_subadditive_def] A'' disj U_in by auto
   258     have *: "\<And>i. 0 \<le> f (A i)" using pos A'' unfolding positive_def by auto
   259     have dis: "\<And>N. disjoint_family_on A {..<N}" by (intro disjoint_family_on_mono[OF _ disj]) auto
   260     show "(\<Sum>i. f (A i)) \<le> f (\<Union>i. A i)"
   261       using ls.additive_sum [OF lambda_system_positive[OF pos] lambda_system_additive _ A' dis]
   262       using A''
   263       by (intro suminf_bound[OF _ *]) (auto intro!: increasingD[OF inc] countable_UN)
   264   qed
   265   {
   266     fix a
   267     assume a [iff]: "a \<in> M"
   268     have "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) = f a"
   269     proof -
   270       show ?thesis
   271       proof (rule antisym)
   272         have "range (\<lambda>i. a \<inter> A i) \<subseteq> M" using A''
   273           by blast
   274         moreover
   275         have "disjoint_family (\<lambda>i. a \<inter> A i)" using disj
   276           by (auto simp add: disjoint_family_on_def)
   277         moreover
   278         have "a \<inter> (\<Union>i. A i) \<in> M"
   279           by (metis Int U_in a)
   280         ultimately
   281         have "f (a \<inter> (\<Union>i. A i)) \<le> (\<Sum>i. f (a \<inter> A i))"
   282           using csa[unfolded countably_subadditive_def, rule_format, of "(\<lambda>i. a \<inter> A i)"]
   283           by (simp add: o_def)
   284         hence "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le>
   285             (\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i))"
   286           by (rule add_right_mono)
   287         moreover
   288         have "(\<Sum>i. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   289           proof (intro suminf_bound_add allI)
   290             fix n
   291             have UNION_in: "(\<Union>i\<in>{0..<n}. A i) \<in> M"
   292               by (metis A'' UNION_in_sets)
   293             have le_fa: "f (UNION {0..<n} A \<inter> a) \<le> f a" using A''
   294               by (blast intro: increasingD [OF inc] A'' UNION_in_sets)
   295             have ls: "(\<Union>i\<in>{0..<n}. A i) \<in> lambda_system \<Omega> M f"
   296               using ls.UNION_in_sets by (simp add: A)
   297             hence eq_fa: "f a = f (a \<inter> (\<Union>i\<in>{0..<n}. A i)) + f (a - (\<Union>i\<in>{0..<n}. A i))"
   298               by (simp add: lambda_system_eq UNION_in)
   299             have "f (a - (\<Union>i. A i)) \<le> f (a - (\<Union>i\<in>{0..<n}. A i))"
   300               by (blast intro: increasingD [OF inc] UNION_in U_in)
   301             thus "(\<Sum>i<n. f (a \<inter> A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   302               by (simp add: lambda_system_strong_sum pos A disj eq_fa add_left_mono atLeast0LessThan[symmetric])
   303           next
   304             have "\<And>i. a \<inter> A i \<in> M" using A'' by auto
   305             then show "\<And>i. 0 \<le> f (a \<inter> A i)" using pos[unfolded positive_def] by auto
   306             have "\<And>i. a - (\<Union>i. A i) \<in> M" using A'' by auto
   307             then have "\<And>i. 0 \<le> f (a - (\<Union>i. A i))" using pos[unfolded positive_def] by auto
   308             then show "f (a - (\<Union>i. A i)) \<noteq> -\<infinity>" by auto
   309           qed
   310         ultimately show "f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i)) \<le> f a"
   311           by (rule order_trans)
   312       next
   313         have "f a \<le> f (a \<inter> (\<Union>i. A i) \<union> (a - (\<Union>i. A i)))"
   314           by (blast intro:  increasingD [OF inc] U_in)
   315         also have "... \<le>  f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i))"
   316           by (blast intro: subadditiveD [OF sa] U_in)
   317         finally show "f a \<le> f (a \<inter> (\<Union>i. A i)) + f (a - (\<Union>i. A i))" .
   318         qed
   319      qed
   320   }
   321   thus  ?thesis
   322     by (simp add: lambda_system_eq sums_iff U_eq U_in)
   323 qed
   324 
   325 lemma (in sigma_algebra) caratheodory_lemma:
   326   assumes oms: "outer_measure_space M f"
   327   defines "L \<equiv> lambda_system \<Omega> M f"
   328   shows "measure_space \<Omega> L f"
   329 proof -
   330   have pos: "positive M f"
   331     by (metis oms outer_measure_space_def)
   332   have alg: "algebra \<Omega> L"
   333     using lambda_system_algebra [of f, OF pos]
   334     by (simp add: algebra_iff_Un L_def)
   335   then
   336   have "sigma_algebra \<Omega> L"
   337     using lambda_system_caratheodory [OF oms]
   338     by (simp add: sigma_algebra_disjoint_iff L_def)
   339   moreover
   340   have "countably_additive L f" "positive L f"
   341     using pos lambda_system_caratheodory [OF oms]
   342     by (auto simp add: lambda_system_sets L_def countably_additive_def positive_def)
   343   ultimately
   344   show ?thesis
   345     using pos by (simp add: measure_space_def)
   346 qed
   347 
   348 lemma inf_measure_nonempty:
   349   assumes f: "positive M f" and b: "b \<in> M" and a: "a \<subseteq> b" "{} \<in> M"
   350   shows "f b \<in> measure_set M f a"
   351 proof -
   352   let ?A = "\<lambda>i::nat. (if i = 0 then b else {})"
   353   have "(\<Sum>i. f (?A i)) = (\<Sum>i<1::nat. f (?A i))"
   354     by (rule suminf_finite) (simp_all add: f[unfolded positive_def])
   355   also have "... = f b"
   356     by simp
   357   finally show ?thesis using assms
   358     by (auto intro!: exI [of _ ?A]
   359              simp: measure_set_def disjoint_family_on_def split_if_mem2 comp_def)
   360 qed
   361 
   362 lemma (in ring_of_sets) inf_measure_agrees:
   363   assumes posf: "positive M f" and ca: "countably_additive M f"
   364       and s: "s \<in> M"
   365   shows "Inf (measure_set M f s) = f s"
   366 proof (intro Inf_eqI)
   367   fix z
   368   assume z: "z \<in> measure_set M f s"
   369   from this obtain A where
   370     A: "range A \<subseteq> M" and disj: "disjoint_family A"
   371     and "s \<subseteq> (\<Union>x. A x)" and si: "(\<Sum>i. f (A i)) = z"
   372     by (auto simp add: measure_set_def comp_def)
   373   hence seq: "s = (\<Union>i. A i \<inter> s)" by blast
   374   have inc: "increasing M f"
   375     by (metis additive_increasing ca countably_additive_additive posf)
   376   have sums: "(\<Sum>i. f (A i \<inter> s)) = f (\<Union>i. A i \<inter> s)"
   377     proof (rule ca[unfolded countably_additive_def, rule_format])
   378       show "range (\<lambda>n. A n \<inter> s) \<subseteq> M" using A s
   379         by blast
   380       show "disjoint_family (\<lambda>n. A n \<inter> s)" using disj
   381         by (auto simp add: disjoint_family_on_def)
   382       show "(\<Union>i. A i \<inter> s) \<in> M" using A s
   383         by (metis UN_extend_simps(4) s seq)
   384     qed
   385   hence "f s = (\<Sum>i. f (A i \<inter> s))"
   386     using seq [symmetric] by (simp add: sums_iff)
   387   also have "... \<le> (\<Sum>i. f (A i))"
   388     proof (rule suminf_le_pos)
   389       fix n show "f (A n \<inter> s) \<le> f (A n)" using A s
   390         by (force intro: increasingD [OF inc])
   391       fix N have "A N \<inter> s \<in> M"  using A s by auto
   392       then show "0 \<le> f (A N \<inter> s)" using posf unfolding positive_def by auto
   393     qed
   394   also have "... = z" by (rule si)
   395   finally show "f s \<le> z" .
   396 qed (blast intro: inf_measure_nonempty [of _ f, OF posf s subset_refl])
   397 
   398 lemma measure_set_pos:
   399   assumes posf: "positive M f" "r \<in> measure_set M f X"
   400   shows "0 \<le> r"
   401 proof -
   402   obtain A where "range A \<subseteq> M" and r: "r = (\<Sum>i. f (A i))"
   403     using `r \<in> measure_set M f X` unfolding measure_set_def by auto
   404   then show "0 \<le> r" using posf unfolding r positive_def
   405     by (intro suminf_0_le) auto
   406 qed
   407 
   408 lemma inf_measure_pos:
   409   assumes posf: "positive M f"
   410   shows "0 \<le> Inf (measure_set M f X)"
   411 proof (rule complete_lattice_class.Inf_greatest)
   412   fix r assume "r \<in> measure_set M f X" with posf show "0 \<le> r"
   413     by (rule measure_set_pos)
   414 qed
   415 
   416 lemma inf_measure_empty:
   417   assumes posf: "positive M f" and "{} \<in> M"
   418   shows "Inf (measure_set M f {}) = 0"
   419 proof (rule antisym)
   420   show "Inf (measure_set M f {}) \<le> 0"
   421     by (metis complete_lattice_class.Inf_lower `{} \<in> M`
   422               inf_measure_nonempty[OF posf] subset_refl posf[unfolded positive_def])
   423 qed (rule inf_measure_pos[OF posf])
   424 
   425 lemma (in ring_of_sets) inf_measure_positive:
   426   assumes p: "positive M f" and "{} \<in> M"
   427   shows "positive M (\<lambda>x. Inf (measure_set M f x))"
   428 proof (unfold positive_def, intro conjI ballI)
   429   show "Inf (measure_set M f {}) = 0" using inf_measure_empty[OF assms] by auto
   430   fix A assume "A \<in> M"
   431 qed (rule inf_measure_pos[OF p])
   432 
   433 lemma (in ring_of_sets) inf_measure_increasing:
   434   assumes posf: "positive M f"
   435   shows "increasing (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   436 apply (clarsimp simp add: increasing_def)
   437 apply (rule complete_lattice_class.Inf_greatest)
   438 apply (rule complete_lattice_class.Inf_lower)
   439 apply (clarsimp simp add: measure_set_def, rule_tac x=A in exI, blast)
   440 done
   441 
   442 lemma (in ring_of_sets) inf_measure_le:
   443   assumes posf: "positive M f" and inc: "increasing M f"
   444       and x: "x \<in> {r . \<exists>A. range A \<subseteq> M \<and> s \<subseteq> (\<Union>i. A i) \<and> (\<Sum>i. f (A i)) = r}"
   445   shows "Inf (measure_set M f s) \<le> x"
   446 proof -
   447   obtain A where A: "range A \<subseteq> M" and ss: "s \<subseteq> (\<Union>i. A i)"
   448              and xeq: "(\<Sum>i. f (A i)) = x"
   449     using x by auto
   450   have dA: "range (disjointed A) \<subseteq> M"
   451     by (metis A range_disjointed_sets)
   452   have "\<forall>n. f (disjointed A n) \<le> f (A n)"
   453     by (metis increasingD [OF inc] UNIV_I dA image_subset_iff disjointed_subset A comp_def)
   454   moreover have "\<forall>i. 0 \<le> f (disjointed A i)"
   455     using posf dA unfolding positive_def by auto
   456   ultimately have sda: "(\<Sum>i. f (disjointed A i)) \<le> (\<Sum>i. f (A i))"
   457     by (blast intro!: suminf_le_pos)
   458   hence ley: "(\<Sum>i. f (disjointed A i)) \<le> x"
   459     by (metis xeq)
   460   hence y: "(\<Sum>i. f (disjointed A i)) \<in> measure_set M f s"
   461     apply (auto simp add: measure_set_def)
   462     apply (rule_tac x="disjointed A" in exI)
   463     apply (simp add: disjoint_family_disjointed UN_disjointed_eq ss dA comp_def)
   464     done
   465   show ?thesis
   466     by (blast intro: y order_trans [OF _ ley] posf complete_lattice_class.Inf_lower)
   467 qed
   468 
   469 lemma (in ring_of_sets) inf_measure_close:
   470   fixes e :: ereal
   471   assumes posf: "positive M f" and e: "0 < e" and ss: "s \<subseteq> (\<Omega>)" and "Inf (measure_set M f s) \<noteq> \<infinity>"
   472   shows "\<exists>A. range A \<subseteq> M \<and> disjoint_family A \<and> s \<subseteq> (\<Union>i. A i) \<and>
   473                (\<Sum>i. f (A i)) \<le> Inf (measure_set M f s) + e"
   474 proof -
   475   from `Inf (measure_set M f s) \<noteq> \<infinity>` have fin: "\<bar>Inf (measure_set M f s)\<bar> \<noteq> \<infinity>"
   476     using inf_measure_pos[OF posf, of s] by auto
   477   obtain l where "l \<in> measure_set M f s" "l \<le> Inf (measure_set M f s) + e"
   478     using Inf_ereal_close[OF fin e] by auto
   479   thus ?thesis
   480     by (auto intro!: exI[of _ l] simp: measure_set_def comp_def)
   481 qed
   482 
   483 lemma (in ring_of_sets) inf_measure_countably_subadditive:
   484   assumes posf: "positive M f" and inc: "increasing M f"
   485   shows "countably_subadditive (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   486 proof (simp add: countably_subadditive_def, safe)
   487   fix A :: "nat \<Rightarrow> 'a set"
   488   let ?outer = "\<lambda>B. Inf (measure_set M f B)"
   489   assume A: "range A \<subseteq> Pow (\<Omega>)"
   490      and disj: "disjoint_family A"
   491      and sb: "(\<Union>i. A i) \<subseteq> \<Omega>"
   492 
   493   { fix e :: ereal assume e: "0 < e" and "\<forall>i. ?outer (A i) \<noteq> \<infinity>"
   494     hence "\<exists>BB. \<forall>n. range (BB n) \<subseteq> M \<and> disjoint_family (BB n) \<and>
   495         A n \<subseteq> (\<Union>i. BB n i) \<and> (\<Sum>i. f (BB n i)) \<le> ?outer (A n) + e * (1/2)^(Suc n)"
   496       apply (safe intro!: choice inf_measure_close [of f, OF posf])
   497       using e sb by (auto simp: ereal_zero_less_0_iff one_ereal_def)
   498     then obtain BB
   499       where BB: "\<And>n. (range (BB n) \<subseteq> M)"
   500       and disjBB: "\<And>n. disjoint_family (BB n)"
   501       and sbBB: "\<And>n. A n \<subseteq> (\<Union>i. BB n i)"
   502       and BBle: "\<And>n. (\<Sum>i. f (BB n i)) \<le> ?outer (A n) + e * (1/2)^(Suc n)"
   503       by auto blast
   504     have sll: "(\<Sum>n. \<Sum>i. (f (BB n i))) \<le> (\<Sum>n. ?outer (A n)) + e"
   505     proof -
   506       have sum_eq_1: "(\<Sum>n. e*(1/2) ^ Suc n) = e"
   507         using suminf_half_series_ereal e
   508         by (simp add: ereal_zero_le_0_iff zero_le_divide_ereal suminf_cmult_ereal)
   509       have "\<And>n i. 0 \<le> f (BB n i)" using posf[unfolded positive_def] BB by auto
   510       then have "\<And>n. 0 \<le> (\<Sum>i. f (BB n i))" by (rule suminf_0_le)
   511       then have "(\<Sum>n. \<Sum>i. (f (BB n i))) \<le> (\<Sum>n. ?outer (A n) + e*(1/2) ^ Suc n)"
   512         by (rule suminf_le_pos[OF BBle])
   513       also have "... = (\<Sum>n. ?outer (A n)) + e"
   514         using sum_eq_1 inf_measure_pos[OF posf] e
   515         by (subst suminf_add_ereal) (auto simp add: ereal_zero_le_0_iff)
   516       finally show ?thesis .
   517     qed
   518     def C \<equiv> "(split BB) o prod_decode"
   519     have C: "!!n. C n \<in> M"
   520       apply (rule_tac p="prod_decode n" in PairE)
   521       apply (simp add: C_def)
   522       apply (metis BB subsetD rangeI)
   523       done
   524     have sbC: "(\<Union>i. A i) \<subseteq> (\<Union>i. C i)"
   525     proof (auto simp add: C_def)
   526       fix x i
   527       assume x: "x \<in> A i"
   528       with sbBB [of i] obtain j where "x \<in> BB i j"
   529         by blast
   530       thus "\<exists>i. x \<in> split BB (prod_decode i)"
   531         by (metis prod_encode_inverse prod.case)
   532     qed
   533     have "(f \<circ> C) = (f \<circ> (\<lambda>(x, y). BB x y)) \<circ> prod_decode"
   534       by (rule ext)  (auto simp add: C_def)
   535     moreover have "suminf ... = (\<Sum>n. \<Sum>i. f (BB n i))" using BBle
   536       using BB posf[unfolded positive_def]
   537       by (force intro!: suminf_ereal_2dimen simp: o_def)
   538     ultimately have Csums: "(\<Sum>i. f (C i)) = (\<Sum>n. \<Sum>i. f (BB n i))" by (simp add: o_def)
   539     have "?outer (\<Union>i. A i) \<le> (\<Sum>n. \<Sum>i. f (BB n i))"
   540       apply (rule inf_measure_le [OF posf(1) inc], auto)
   541       apply (rule_tac x="C" in exI)
   542       apply (auto simp add: C sbC Csums)
   543       done
   544     also have "... \<le> (\<Sum>n. ?outer (A n)) + e" using sll
   545       by blast
   546     finally have "?outer (\<Union>i. A i) \<le> (\<Sum>n. ?outer (A n)) + e" . }
   547   note for_finite_Inf = this
   548 
   549   show "?outer (\<Union>i. A i) \<le> (\<Sum>n. ?outer (A n))"
   550   proof cases
   551     assume "\<forall>i. ?outer (A i) \<noteq> \<infinity>"
   552     with for_finite_Inf show ?thesis
   553       by (intro ereal_le_epsilon) auto
   554   next
   555     assume "\<not> (\<forall>i. ?outer (A i) \<noteq> \<infinity>)"
   556     then have "\<exists>i. ?outer (A i) = \<infinity>"
   557       by auto
   558     then have "(\<Sum>n. ?outer (A n)) = \<infinity>"
   559       using suminf_PInfty[OF inf_measure_pos, OF posf]
   560       by metis
   561     then show ?thesis by simp
   562   qed
   563 qed
   564 
   565 lemma (in ring_of_sets) inf_measure_outer:
   566   "\<lbrakk> positive M f ; increasing M f \<rbrakk> \<Longrightarrow>
   567     outer_measure_space (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   568   using inf_measure_pos[of M f]
   569   by (simp add: outer_measure_space_def inf_measure_empty
   570                 inf_measure_increasing inf_measure_countably_subadditive positive_def)
   571 
   572 lemma (in ring_of_sets) algebra_subset_lambda_system:
   573   assumes posf: "positive M f" and inc: "increasing M f"
   574       and add: "additive M f"
   575   shows "M \<subseteq> lambda_system \<Omega> (Pow \<Omega>) (\<lambda>x. Inf (measure_set M f x))"
   576 proof (auto dest: sets_into_space
   577             simp add: algebra.lambda_system_eq [OF algebra_Pow])
   578   fix x s
   579   assume x: "x \<in> M"
   580      and s: "s \<subseteq> \<Omega>"
   581   have [simp]: "!!x. x \<in> M \<Longrightarrow> s \<inter> (\<Omega> - x) = s-x" using s
   582     by blast
   583   have "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
   584         \<le> Inf (measure_set M f s)"
   585   proof cases
   586     assume "Inf (measure_set M f s) = \<infinity>" then show ?thesis by simp
   587   next
   588     assume fin: "Inf (measure_set M f s) \<noteq> \<infinity>"
   589     then have "measure_set M f s \<noteq> {}"
   590       by (auto simp: top_ereal_def)
   591     show ?thesis
   592     proof (rule complete_lattice_class.Inf_greatest)
   593       fix r assume "r \<in> measure_set M f s"
   594       then obtain A where A: "disjoint_family A" "range A \<subseteq> M" "s \<subseteq> (\<Union>i. A i)"
   595         and r: "r = (\<Sum>i. f (A i))" unfolding measure_set_def by auto
   596       have "Inf (measure_set M f (s \<inter> x)) \<le> (\<Sum>i. f (A i \<inter> x))"
   597         unfolding measure_set_def
   598       proof (safe intro!: complete_lattice_class.Inf_lower exI[of _ "\<lambda>i. A i \<inter> x"])
   599         from A(1) show "disjoint_family (\<lambda>i. A i \<inter> x)"
   600           by (rule disjoint_family_on_bisimulation) auto
   601       qed (insert x A, auto)
   602       moreover
   603       have "Inf (measure_set M f (s - x)) \<le> (\<Sum>i. f (A i - x))"
   604         unfolding measure_set_def
   605       proof (safe intro!: complete_lattice_class.Inf_lower exI[of _ "\<lambda>i. A i - x"])
   606         from A(1) show "disjoint_family (\<lambda>i. A i - x)"
   607           by (rule disjoint_family_on_bisimulation) auto
   608       qed (insert x A, auto)
   609       ultimately have "Inf (measure_set M f (s \<inter> x)) + Inf (measure_set M f (s - x)) \<le>
   610           (\<Sum>i. f (A i \<inter> x)) + (\<Sum>i. f (A i - x))" by (rule add_mono)
   611       also have "\<dots> = (\<Sum>i. f (A i \<inter> x) + f (A i - x))"
   612         using A(2) x posf by (subst suminf_add_ereal) (auto simp: positive_def)
   613       also have "\<dots> = (\<Sum>i. f (A i))"
   614         using A x
   615         by (subst add[THEN additiveD, symmetric])
   616            (auto intro!: arg_cong[where f=suminf] arg_cong[where f=f])
   617       finally show "Inf (measure_set M f (s \<inter> x)) + Inf (measure_set M f (s - x)) \<le> r"
   618         using r by simp
   619     qed
   620   qed
   621   moreover
   622   have "Inf (measure_set M f s)
   623        \<le> Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))"
   624   proof -
   625     have "Inf (measure_set M f s) = Inf (measure_set M f ((s\<inter>x) \<union> (s-x)))"
   626       by (metis Un_Diff_Int Un_commute)
   627     also have "... \<le> Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))"
   628       apply (rule subadditiveD)
   629       apply (rule ring_of_sets.countably_subadditive_subadditive [OF ring_of_sets_Pow])
   630       apply (simp add: positive_def inf_measure_empty[OF posf] inf_measure_pos[OF posf])
   631       apply (rule inf_measure_countably_subadditive)
   632       using s by (auto intro!: posf inc)
   633     finally show ?thesis .
   634   qed
   635   ultimately
   636   show "Inf (measure_set M f (s\<inter>x)) + Inf (measure_set M f (s-x))
   637         = Inf (measure_set M f s)"
   638     by (rule order_antisym)
   639 qed
   640 
   641 lemma measure_down:
   642   "measure_space \<Omega> N \<mu> \<Longrightarrow> sigma_algebra \<Omega> M \<Longrightarrow> M \<subseteq> N \<Longrightarrow> measure_space \<Omega> M \<mu>"
   643   by (simp add: measure_space_def positive_def countably_additive_def)
   644      blast
   645 
   646 theorem (in ring_of_sets) caratheodory':
   647   assumes posf: "positive M f" and ca: "countably_additive M f"
   648   shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu> s = f s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
   649 proof -
   650   have inc: "increasing M f"
   651     by (metis additive_increasing ca countably_additive_additive posf)
   652   let ?infm = "(\<lambda>x. Inf (measure_set M f x))"
   653   def ls \<equiv> "lambda_system \<Omega> (Pow \<Omega>) ?infm"
   654   have mls: "measure_space \<Omega> ls ?infm"
   655     using sigma_algebra.caratheodory_lemma
   656             [OF sigma_algebra_Pow  inf_measure_outer [OF posf inc]]
   657     by (simp add: ls_def)
   658   hence sls: "sigma_algebra \<Omega> ls"
   659     by (simp add: measure_space_def)
   660   have "M \<subseteq> ls"
   661     by (simp add: ls_def)
   662        (metis ca posf inc countably_additive_additive algebra_subset_lambda_system)
   663   hence sgs_sb: "sigma_sets (\<Omega>) (M) \<subseteq> ls"
   664     using sigma_algebra.sigma_sets_subset [OF sls, of "M"]
   665     by simp
   666   have "measure_space \<Omega> (sigma_sets \<Omega> M) ?infm"
   667     by (rule measure_down [OF mls], rule sigma_algebra_sigma_sets)
   668        (simp_all add: sgs_sb space_closed)
   669   thus ?thesis using inf_measure_agrees [OF posf ca]
   670     by (intro exI[of _ ?infm]) auto
   671 qed
   672 
   673 subsubsection {*Alternative instances of caratheodory*}
   674 
   675 lemma (in ring_of_sets) caratheodory_empty_continuous:
   676   assumes f: "positive M f" "additive M f" and fin: "\<And>A. A \<in> M \<Longrightarrow> f A \<noteq> \<infinity>"
   677   assumes cont: "\<And>A. range A \<subseteq> M \<Longrightarrow> decseq A \<Longrightarrow> (\<Inter>i. A i) = {} \<Longrightarrow> (\<lambda>i. f (A i)) ----> 0"
   678   shows "\<exists>\<mu> :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu> s = f s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>"
   679 proof (intro caratheodory' empty_continuous_imp_countably_additive f)
   680   show "\<forall>A\<in>M. f A \<noteq> \<infinity>" using fin by auto
   681 qed (rule cont)
   682 
   683 section {* Volumes *}
   684 
   685 definition volume :: "'a set set \<Rightarrow> ('a set \<Rightarrow> ereal) \<Rightarrow> bool" where
   686   "volume M f \<longleftrightarrow>
   687   (f {} = 0) \<and> (\<forall>a\<in>M. 0 \<le> f a) \<and>
   688   (\<forall>C\<subseteq>M. disjoint C \<longrightarrow> finite C \<longrightarrow> \<Union>C \<in> M \<longrightarrow> f (\<Union>C) = (\<Sum>c\<in>C. f c))"
   689 
   690 lemma volumeI:
   691   assumes "f {} = 0"
   692   assumes "\<And>a. a \<in> M \<Longrightarrow> 0 \<le> f a"
   693   assumes "\<And>C. C \<subseteq> M \<Longrightarrow> disjoint C \<Longrightarrow> finite C \<Longrightarrow> \<Union>C \<in> M \<Longrightarrow> f (\<Union>C) = (\<Sum>c\<in>C. f c)"
   694   shows "volume M f"
   695   using assms by (auto simp: volume_def)
   696 
   697 lemma volume_positive:
   698   "volume M f \<Longrightarrow> a \<in> M \<Longrightarrow> 0 \<le> f a"
   699   by (auto simp: volume_def)
   700 
   701 lemma volume_empty:
   702   "volume M f \<Longrightarrow> f {} = 0"
   703   by (auto simp: volume_def)
   704 
   705 lemma volume_finite_additive:
   706   assumes "volume M f" 
   707   assumes A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> M" "disjoint_family_on A I" "finite I" "UNION I A \<in> M"
   708   shows "f (UNION I A) = (\<Sum>i\<in>I. f (A i))"
   709 proof -
   710   have "A`I \<subseteq> M" "disjoint (A`I)" "finite (A`I)" "\<Union>(A`I) \<in> M"
   711     using A unfolding SUP_def by (auto simp: disjoint_family_on_disjoint_image)
   712   with `volume M f` have "f (\<Union>(A`I)) = (\<Sum>a\<in>A`I. f a)"
   713     unfolding volume_def by blast
   714   also have "\<dots> = (\<Sum>i\<in>I. f (A i))"
   715   proof (subst setsum_reindex_nonzero)
   716     fix i j assume "i \<in> I" "j \<in> I" "i \<noteq> j" "A i = A j"
   717     with `disjoint_family_on A I` have "A i = {}"
   718       by (auto simp: disjoint_family_on_def)
   719     then show "f (A i) = 0"
   720       using volume_empty[OF `volume M f`] by simp
   721   qed (auto intro: `finite I`)
   722   finally show "f (UNION I A) = (\<Sum>i\<in>I. f (A i))"
   723     by simp
   724 qed
   725 
   726 lemma (in ring_of_sets) volume_additiveI:
   727   assumes pos: "\<And>a. a \<in> M \<Longrightarrow> 0 \<le> \<mu> a" 
   728   assumes [simp]: "\<mu> {} = 0"
   729   assumes add: "\<And>a b. a \<in> M \<Longrightarrow> b \<in> M \<Longrightarrow> a \<inter> b = {} \<Longrightarrow> \<mu> (a \<union> b) = \<mu> a + \<mu> b"
   730   shows "volume M \<mu>"
   731 proof (unfold volume_def, safe)
   732   fix C assume "finite C" "C \<subseteq> M" "disjoint C"
   733   then show "\<mu> (\<Union>C) = setsum \<mu> C"
   734   proof (induct C)
   735     case (insert c C)
   736     from insert(1,2,4,5) have "\<mu> (\<Union>insert c C) = \<mu> c + \<mu> (\<Union>C)"
   737       by (auto intro!: add simp: disjoint_def)
   738     with insert show ?case
   739       by (simp add: disjoint_def)
   740   qed simp
   741 qed fact+
   742 
   743 lemma (in semiring_of_sets) extend_volume:
   744   assumes "volume M \<mu>"
   745   shows "\<exists>\<mu>'. volume generated_ring \<mu>' \<and> (\<forall>a\<in>M. \<mu>' a = \<mu> a)"
   746 proof -
   747   let ?R = generated_ring
   748   have "\<forall>a\<in>?R. \<exists>m. \<exists>C\<subseteq>M. a = \<Union>C \<and> finite C \<and> disjoint C \<and> m = (\<Sum>c\<in>C. \<mu> c)"
   749     by (auto simp: generated_ring_def)
   750   from bchoice[OF this] guess \<mu>' .. note \<mu>'_spec = this
   751   
   752   { fix C assume C: "C \<subseteq> M" "finite C" "disjoint C"
   753     fix D assume D: "D \<subseteq> M" "finite D" "disjoint D"
   754     assume "\<Union>C = \<Union>D"
   755     have "(\<Sum>d\<in>D. \<mu> d) = (\<Sum>d\<in>D. \<Sum>c\<in>C. \<mu> (c \<inter> d))"
   756     proof (intro setsum_cong refl)
   757       fix d assume "d \<in> D"
   758       have Un_eq_d: "(\<Union>c\<in>C. c \<inter> d) = d"
   759         using `d \<in> D` `\<Union>C = \<Union>D` by auto
   760       moreover have "\<mu> (\<Union>c\<in>C. c \<inter> d) = (\<Sum>c\<in>C. \<mu> (c \<inter> d))"
   761       proof (rule volume_finite_additive)
   762         { fix c assume "c \<in> C" then show "c \<inter> d \<in> M"
   763             using C D `d \<in> D` by auto }
   764         show "(\<Union>a\<in>C. a \<inter> d) \<in> M"
   765           unfolding Un_eq_d using `d \<in> D` D by auto
   766         show "disjoint_family_on (\<lambda>a. a \<inter> d) C"
   767           using `disjoint C` by (auto simp: disjoint_family_on_def disjoint_def)
   768       qed fact+
   769       ultimately show "\<mu> d = (\<Sum>c\<in>C. \<mu> (c \<inter> d))" by simp
   770     qed }
   771   note split_sum = this
   772 
   773   { fix C assume C: "C \<subseteq> M" "finite C" "disjoint C"
   774     fix D assume D: "D \<subseteq> M" "finite D" "disjoint D"
   775     assume "\<Union>C = \<Union>D"
   776     with split_sum[OF C D] split_sum[OF D C]
   777     have "(\<Sum>d\<in>D. \<mu> d) = (\<Sum>c\<in>C. \<mu> c)"
   778       by (simp, subst setsum_commute, simp add: ac_simps) }
   779   note sum_eq = this
   780 
   781   { fix C assume C: "C \<subseteq> M" "finite C" "disjoint C"
   782     then have "\<Union>C \<in> ?R" by (auto simp: generated_ring_def)
   783     with \<mu>'_spec[THEN bspec, of "\<Union>C"]
   784     obtain D where
   785       D: "D \<subseteq> M" "finite D" "disjoint D" "\<Union>C = \<Union>D" and "\<mu>' (\<Union>C) = (\<Sum>d\<in>D. \<mu> d)"
   786       by blast
   787     with sum_eq[OF C D] have "\<mu>' (\<Union>C) = (\<Sum>c\<in>C. \<mu> c)" by simp }
   788   note \<mu>' = this
   789 
   790   show ?thesis
   791   proof (intro exI conjI ring_of_sets.volume_additiveI[OF generating_ring] ballI)
   792     fix a assume "a \<in> M" with \<mu>'[of "{a}"] show "\<mu>' a = \<mu> a"
   793       by (simp add: disjoint_def)
   794   next
   795     fix a assume "a \<in> ?R" then guess Ca .. note Ca = this
   796     with \<mu>'[of Ca] `volume M \<mu>`[THEN volume_positive]
   797     show "0 \<le> \<mu>' a"
   798       by (auto intro!: setsum_nonneg)
   799   next
   800     show "\<mu>' {} = 0" using \<mu>'[of "{}"] by auto
   801   next
   802     fix a assume "a \<in> ?R" then guess Ca .. note Ca = this
   803     fix b assume "b \<in> ?R" then guess Cb .. note Cb = this
   804     assume "a \<inter> b = {}"
   805     with Ca Cb have "Ca \<inter> Cb \<subseteq> {{}}" by auto
   806     then have C_Int_cases: "Ca \<inter> Cb = {{}} \<or> Ca \<inter> Cb = {}" by auto
   807 
   808     from `a \<inter> b = {}` have "\<mu>' (\<Union> (Ca \<union> Cb)) = (\<Sum>c\<in>Ca \<union> Cb. \<mu> c)"
   809       using Ca Cb by (intro \<mu>') (auto intro!: disjoint_union)
   810     also have "\<dots> = (\<Sum>c\<in>Ca \<union> Cb. \<mu> c) + (\<Sum>c\<in>Ca \<inter> Cb. \<mu> c)"
   811       using C_Int_cases volume_empty[OF `volume M \<mu>`] by (elim disjE) simp_all
   812     also have "\<dots> = (\<Sum>c\<in>Ca. \<mu> c) + (\<Sum>c\<in>Cb. \<mu> c)"
   813       using Ca Cb by (simp add: setsum_Un_Int)
   814     also have "\<dots> = \<mu>' a + \<mu>' b"
   815       using Ca Cb by (simp add: \<mu>')
   816     finally show "\<mu>' (a \<union> b) = \<mu>' a + \<mu>' b"
   817       using Ca Cb by simp
   818   qed
   819 qed
   820 
   821 section {* Caratheodory on semirings *}
   822 
   823 theorem (in semiring_of_sets) caratheodory:
   824   assumes pos: "positive M \<mu>" and ca: "countably_additive M \<mu>"
   825   shows "\<exists>\<mu>' :: 'a set \<Rightarrow> ereal. (\<forall>s \<in> M. \<mu>' s = \<mu> s) \<and> measure_space \<Omega> (sigma_sets \<Omega> M) \<mu>'"
   826 proof -
   827   have "volume M \<mu>"
   828   proof (rule volumeI)
   829     { fix a assume "a \<in> M" then show "0 \<le> \<mu> a"
   830         using pos unfolding positive_def by auto }
   831     note p = this
   832 
   833     fix C assume sets_C: "C \<subseteq> M" "\<Union>C \<in> M" and "disjoint C" "finite C"
   834     have "\<exists>F'. bij_betw F' {..<card C} C"
   835       by (rule finite_same_card_bij[OF _ `finite C`]) auto
   836     then guess F' .. note F' = this
   837     then have F': "C = F' ` {..< card C}" "inj_on F' {..< card C}"
   838       by (auto simp: bij_betw_def)
   839     { fix i j assume *: "i < card C" "j < card C" "i \<noteq> j"
   840       with F' have "F' i \<in> C" "F' j \<in> C" "F' i \<noteq> F' j"
   841         unfolding inj_on_def by auto
   842       with `disjoint C`[THEN disjointD]
   843       have "F' i \<inter> F' j = {}"
   844         by auto }
   845     note F'_disj = this
   846     def F \<equiv> "\<lambda>i. if i < card C then F' i else {}"
   847     then have "disjoint_family F"
   848       using F'_disj by (auto simp: disjoint_family_on_def)
   849     moreover from F' have "(\<Union>i. F i) = \<Union>C"
   850       by (auto simp: F_def set_eq_iff split: split_if_asm)
   851     moreover have sets_F: "\<And>i. F i \<in> M"
   852       using F' sets_C by (auto simp: F_def)
   853     moreover note sets_C
   854     ultimately have "\<mu> (\<Union>C) = (\<Sum>i. \<mu> (F i))"
   855       using ca[unfolded countably_additive_def, THEN spec, of F] by auto
   856     also have "\<dots> = (\<Sum>i<card C. \<mu> (F' i))"
   857     proof -
   858       have "(\<lambda>i. if i \<in> {..< card C} then \<mu> (F' i) else 0) sums (\<Sum>i<card C. \<mu> (F' i))"
   859         by (rule sums_If_finite_set) auto
   860       also have "(\<lambda>i. if i \<in> {..< card C} then \<mu> (F' i) else 0) = (\<lambda>i. \<mu> (F i))"
   861         using pos by (auto simp: positive_def F_def)
   862       finally show "(\<Sum>i. \<mu> (F i)) = (\<Sum>i<card C. \<mu> (F' i))"
   863         by (simp add: sums_iff)
   864     qed
   865     also have "\<dots> = (\<Sum>c\<in>C. \<mu> c)"
   866       using F'(2) by (subst (2) F') (simp add: setsum_reindex)
   867     finally show "\<mu> (\<Union>C) = (\<Sum>c\<in>C. \<mu> c)" .
   868   next
   869     show "\<mu> {} = 0"
   870       using `positive M \<mu>` by (rule positiveD1)
   871   qed
   872   from extend_volume[OF this] obtain \<mu>_r where
   873     V: "volume generated_ring \<mu>_r" "\<And>a. a \<in> M \<Longrightarrow> \<mu> a = \<mu>_r a"
   874     by auto
   875 
   876   interpret G: ring_of_sets \<Omega> generated_ring
   877     by (rule generating_ring)
   878 
   879   have pos: "positive generated_ring \<mu>_r"
   880     using V unfolding positive_def by (auto simp: positive_def intro!: volume_positive volume_empty)
   881 
   882   have "countably_additive generated_ring \<mu>_r"
   883   proof (rule countably_additiveI)
   884     fix A' :: "nat \<Rightarrow> 'a set" assume A': "range A' \<subseteq> generated_ring" "disjoint_family A'"
   885       and Un_A: "(\<Union>i. A' i) \<in> generated_ring"
   886 
   887     from generated_ringE[OF Un_A] guess C' . note C' = this
   888 
   889     { fix c assume "c \<in> C'"
   890       moreover def A \<equiv> "\<lambda>i. A' i \<inter> c"
   891       ultimately have A: "range A \<subseteq> generated_ring" "disjoint_family A"
   892         and Un_A: "(\<Union>i. A i) \<in> generated_ring"
   893         using A' C'
   894         by (auto intro!: G.Int G.finite_Union intro: generated_ringI_Basic simp: disjoint_family_on_def)
   895       from A C' `c \<in> C'` have UN_eq: "(\<Union>i. A i) = c"
   896         by (auto simp: A_def)
   897 
   898       have "\<forall>i::nat. \<exists>f::nat \<Rightarrow> 'a set. \<mu>_r (A i) = (\<Sum>j. \<mu>_r (f j)) \<and> disjoint_family f \<and> \<Union>range f = A i \<and> (\<forall>j. f j \<in> M)"
   899         (is "\<forall>i. ?P i")
   900       proof
   901         fix i
   902         from A have Ai: "A i \<in> generated_ring" by auto
   903         from generated_ringE[OF this] guess C . note C = this
   904 
   905         have "\<exists>F'. bij_betw F' {..<card C} C"
   906           by (rule finite_same_card_bij[OF _ `finite C`]) auto
   907         then guess F .. note F = this
   908         def f \<equiv> "\<lambda>i. if i < card C then F i else {}"
   909         then have f: "bij_betw f {..< card C} C"
   910           by (intro bij_betw_cong[THEN iffD1, OF _ F]) auto
   911         with C have "\<forall>j. f j \<in> M"
   912           by (auto simp: Pi_iff f_def dest!: bij_betw_imp_funcset)
   913         moreover
   914         from f C have d_f: "disjoint_family_on f {..<card C}"
   915           by (intro disjoint_image_disjoint_family_on) (auto simp: bij_betw_def)
   916         then have "disjoint_family f"
   917           by (auto simp: disjoint_family_on_def f_def)
   918         moreover
   919         have Ai_eq: "A i = (\<Union> x<card C. f x)"
   920           using f C Ai unfolding bij_betw_def by (simp add: Union_image_eq[symmetric])
   921         then have "\<Union>range f = A i"
   922           using f C Ai unfolding bij_betw_def by (auto simp: f_def)
   923         moreover 
   924         { have "(\<Sum>j. \<mu>_r (f j)) = (\<Sum>j. if j \<in> {..< card C} then \<mu>_r (f j) else 0)"
   925             using volume_empty[OF V(1)] by (auto intro!: arg_cong[where f=suminf] simp: f_def)
   926           also have "\<dots> = (\<Sum>j<card C. \<mu>_r (f j))"
   927             by (rule sums_If_finite_set[THEN sums_unique, symmetric]) simp
   928           also have "\<dots> = \<mu>_r (A i)"
   929             using C f[THEN bij_betw_imp_funcset] unfolding Ai_eq
   930             by (intro volume_finite_additive[OF V(1) _ d_f, symmetric])
   931                (auto simp: Pi_iff Ai_eq intro: generated_ringI_Basic)
   932           finally have "\<mu>_r (A i) = (\<Sum>j. \<mu>_r (f j))" .. }
   933         ultimately show "?P i"
   934           by blast
   935       qed
   936       from choice[OF this] guess f .. note f = this
   937       then have UN_f_eq: "(\<Union>i. split f (prod_decode i)) = (\<Union>i. A i)"
   938         unfolding UN_extend_simps surj_prod_decode by (auto simp: set_eq_iff)
   939 
   940       have d: "disjoint_family (\<lambda>i. split f (prod_decode i))"
   941         unfolding disjoint_family_on_def
   942       proof (intro ballI impI)
   943         fix m n :: nat assume "m \<noteq> n"
   944         then have neq: "prod_decode m \<noteq> prod_decode n"
   945           using inj_prod_decode[of UNIV] by (auto simp: inj_on_def)
   946         show "split f (prod_decode m) \<inter> split f (prod_decode n) = {}"
   947         proof cases
   948           assume "fst (prod_decode m) = fst (prod_decode n)"
   949           then show ?thesis
   950             using neq f by (fastforce simp: disjoint_family_on_def)
   951         next
   952           assume neq: "fst (prod_decode m) \<noteq> fst (prod_decode n)"
   953           have "split f (prod_decode m) \<subseteq> A (fst (prod_decode m))"
   954             "split f (prod_decode n) \<subseteq> A (fst (prod_decode n))"
   955             using f[THEN spec, of "fst (prod_decode m)"]
   956             using f[THEN spec, of "fst (prod_decode n)"]
   957             by (auto simp: set_eq_iff)
   958           with f A neq show ?thesis
   959             by (fastforce simp: disjoint_family_on_def subset_eq set_eq_iff)
   960         qed
   961       qed
   962       from f have "(\<Sum>n. \<mu>_r (A n)) = (\<Sum>n. \<mu>_r (split f (prod_decode n)))"
   963         by (intro suminf_ereal_2dimen[symmetric] positiveD2[OF pos] generated_ringI_Basic)
   964          (auto split: prod.split)
   965       also have "\<dots> = (\<Sum>n. \<mu> (split f (prod_decode n)))"
   966         using f V(2) by (auto intro!: arg_cong[where f=suminf] split: prod.split)
   967       also have "\<dots> = \<mu> (\<Union>i. split f (prod_decode i))"
   968         using f `c \<in> C'` C'
   969         by (intro ca[unfolded countably_additive_def, rule_format])
   970            (auto split: prod.split simp: UN_f_eq d UN_eq)
   971       finally have "(\<Sum>n. \<mu>_r (A' n \<inter> c)) = \<mu> c"
   972         using UN_f_eq UN_eq by (simp add: A_def) }
   973     note eq = this
   974 
   975     have "(\<Sum>n. \<mu>_r (A' n)) = (\<Sum>n. \<Sum>c\<in>C'. \<mu>_r (A' n \<inter> c))"
   976       using C' A'
   977       by (subst volume_finite_additive[symmetric, OF V(1)])
   978          (auto simp: disjoint_def disjoint_family_on_def Union_image_eq[symmetric] simp del: Sup_image_eq Union_image_eq
   979                intro!: G.Int G.finite_Union arg_cong[where f="\<lambda>X. suminf (\<lambda>i. \<mu>_r (X i))"] ext
   980                intro: generated_ringI_Basic)
   981     also have "\<dots> = (\<Sum>c\<in>C'. \<Sum>n. \<mu>_r (A' n \<inter> c))"
   982       using C' A'
   983       by (intro suminf_setsum_ereal positiveD2[OF pos] G.Int G.finite_Union)
   984          (auto intro: generated_ringI_Basic)
   985     also have "\<dots> = (\<Sum>c\<in>C'. \<mu>_r c)"
   986       using eq V C' by (auto intro!: setsum_cong)
   987     also have "\<dots> = \<mu>_r (\<Union>C')"
   988       using C' Un_A
   989       by (subst volume_finite_additive[symmetric, OF V(1)])
   990          (auto simp: disjoint_family_on_def disjoint_def Union_image_eq[symmetric] simp del: Sup_image_eq Union_image_eq 
   991                intro: generated_ringI_Basic)
   992     finally show "(\<Sum>n. \<mu>_r (A' n)) = \<mu>_r (\<Union>i. A' i)"
   993       using C' by simp
   994   qed
   995   from G.caratheodory'[OF `positive generated_ring \<mu>_r` `countably_additive generated_ring \<mu>_r`]
   996   guess \<mu>' ..
   997   with V show ?thesis
   998     unfolding sigma_sets_generated_ring_eq
   999     by (intro exI[of _ \<mu>']) (auto intro: generated_ringI_Basic)
  1000 qed
  1001 
  1002 end