introduce Bochner integral: generalizes Lebesgue integral from real-valued function to functions on real-normed vector spaces
authorhoelzl
Mon May 19 12:04:45 2014 +0200 (2014-05-19)
changeset 56993e5366291d6aa
parent 56992 d0e5225601d3
child 56994 8d5e5ec1cac3
introduce Bochner integral: generalizes Lebesgue integral from real-valued function to functions on real-normed vector spaces
NEWS
src/HOL/Lattices_Big.thy
src/HOL/Library/Extended_Real.thy
src/HOL/Library/Indicator_Function.thy
src/HOL/Probability/Binary_Product_Measure.thy
src/HOL/Probability/Bochner_Integration.thy
src/HOL/Probability/Borel_Space.thy
src/HOL/Probability/Complete_Measure.thy
src/HOL/Probability/Distributions.thy
src/HOL/Probability/Finite_Product_Measure.thy
src/HOL/Probability/Information.thy
src/HOL/Probability/Lebesgue_Integration.thy
src/HOL/Probability/Lebesgue_Measure.thy
src/HOL/Probability/Measurable.thy
src/HOL/Probability/Nonnegative_Lebesgue_Integration.thy
src/HOL/Probability/Probability_Measure.thy
src/HOL/Probability/Radon_Nikodym.thy
src/HOL/Probability/Sigma_Algebra.thy
     1.1 --- a/NEWS	Mon May 19 11:27:02 2014 +0200
     1.2 +++ b/NEWS	Mon May 19 12:04:45 2014 +0200
     1.3 @@ -670,6 +670,68 @@
     1.4      derivative_is_linear      ~>  has_derivative_linear
     1.5      bounded_linear_imp_linear ~>  bounded_linear.linear
     1.6  
     1.7 +* HOL-Probability:
     1.8 +  - replaced the Lebesgue integral on real numbers by the more general Bochner
     1.9 +    integral for functions into a real-normed vector space.
    1.10 +
    1.11 +    integral_zero               ~>  integral_zero / integrable_zero
    1.12 +    integral_minus              ~>  integral_minus / integrable_minus
    1.13 +    integral_add                ~>  integral_add / integrable_add
    1.14 +    integral_diff               ~>  integral_diff / integrable_diff
    1.15 +    integral_setsum             ~>  integral_setsum / integrable_setsum
    1.16 +    integral_multc              ~>  integral_mult_left / integrable_mult_left
    1.17 +    integral_cmult              ~>  integral_mult_right / integrable_mult_right
    1.18 +    integral_triangle_inequality~>  integral_norm_bound
    1.19 +    integrable_nonneg           ~>  integrableI_nonneg
    1.20 +    integral_positive           ~>  integral_nonneg_AE
    1.21 +    integrable_abs_iff          ~>  integrable_abs_cancel
    1.22 +    positive_integral_lim_INF   ~>  positive_integral_liminf
    1.23 +    lebesgue_real_affine        ~>  lborel_real_affine
    1.24 +    borel_integral_has_integral ~>  has_integral_lebesgue_integral
    1.25 +    integral_indicator          ~>
    1.26 +         integral_real_indicator / integrable_real_indicator
    1.27 +    positive_integral_fst       ~>  positive_integral_fst'
    1.28 +    positive_integral_fst_measurable ~> positive_integral_fst
    1.29 +    positive_integral_snd_measurable ~> positive_integral_snd
    1.30 +
    1.31 +    integrable_fst_measurable   ~>
    1.32 +         integral_fst / integrable_fst / AE_integrable_fst
    1.33 +
    1.34 +    integrable_snd_measurable   ~>
    1.35 +         integral_snd / integrable_snd / AE_integrable_snd
    1.36 +
    1.37 +    integral_monotone_convergence  ~>
    1.38 +         integral_monotone_convergence / integrable_monotone_convergence
    1.39 +
    1.40 +    integral_monotone_convergence_at_top  ~>
    1.41 +         integral_monotone_convergence_at_top /
    1.42 +         integrable_monotone_convergence_at_top
    1.43 +
    1.44 +    has_integral_iff_positive_integral_lebesgue  ~>
    1.45 +         has_integral_iff_has_bochner_integral_lebesgue_nonneg
    1.46 +
    1.47 +    lebesgue_integral_has_integral  ~>
    1.48 +         has_integral_integrable_lebesgue_nonneg
    1.49 +
    1.50 +    positive_integral_lebesgue_has_integral  ~>
    1.51 +         integral_has_integral_lebesgue_nonneg /
    1.52 +         integrable_has_integral_lebesgue_nonneg
    1.53 +
    1.54 +    lebesgue_integral_real_affine  ~>
    1.55 +         positive_integral_real_affine
    1.56 +
    1.57 +    has_integral_iff_positive_integral_lborel  ~>
    1.58 +         integral_has_integral_nonneg / integrable_has_integral_nonneg
    1.59 +
    1.60 +    The following theorems where removed:
    1.61 +
    1.62 +    lebesgue_integral_nonneg
    1.63 +    lebesgue_integral_uminus
    1.64 +    lebesgue_integral_cmult
    1.65 +    lebesgue_integral_multc
    1.66 +    lebesgue_integral_cmult_nonneg
    1.67 +    integral_cmul_indicator
    1.68 +    integral_real
    1.69  
    1.70  *** Scala ***
    1.71  
     2.1 --- a/src/HOL/Lattices_Big.thy	Mon May 19 11:27:02 2014 +0200
     2.2 +++ b/src/HOL/Lattices_Big.thy	Mon May 19 12:04:45 2014 +0200
     2.3 @@ -125,6 +125,9 @@
     2.4    finally show ?case .
     2.5  qed
     2.6  
     2.7 +lemma infinite: "\<not> finite A \<Longrightarrow> F A = the None"
     2.8 +  unfolding eq_fold' by (cases "finite (UNIV::'a set)") (auto intro: finite_subset fold_infinite)
     2.9 +
    2.10  end
    2.11  
    2.12  locale semilattice_order_set = binary?: semilattice_order + semilattice_set
     3.1 --- a/src/HOL/Library/Extended_Real.thy	Mon May 19 11:27:02 2014 +0200
     3.2 +++ b/src/HOL/Library/Extended_Real.thy	Mon May 19 12:04:45 2014 +0200
     3.3 @@ -453,7 +453,7 @@
     3.4  
     3.5  lemma ereal_add_strict_mono:
     3.6    fixes a b c d :: ereal
     3.7 -  assumes "a = b"
     3.8 +  assumes "a \<le> b"
     3.9      and "0 \<le> a"
    3.10      and "a \<noteq> \<infinity>"
    3.11      and "c < d"
    3.12 @@ -2022,6 +2022,22 @@
    3.13       by auto
    3.14  qed (auto simp add: image_Union image_Int)
    3.15  
    3.16 +
    3.17 +lemma eventually_finite:
    3.18 +  fixes x :: ereal
    3.19 +  assumes "\<bar>x\<bar> \<noteq> \<infinity>" "(f ---> x) F"
    3.20 +  shows "eventually (\<lambda>x. \<bar>f x\<bar> \<noteq> \<infinity>) F"
    3.21 +proof -
    3.22 +  have "(f ---> ereal (real x)) F"
    3.23 +    using assms by (cases x) auto
    3.24 +  then have "eventually (\<lambda>x. f x \<in> ereal ` UNIV) F"
    3.25 +    by (rule topological_tendstoD) (auto intro: open_ereal)
    3.26 +  also have "(\<lambda>x. f x \<in> ereal ` UNIV) = (\<lambda>x. \<bar>f x\<bar> \<noteq> \<infinity>)"
    3.27 +    by auto
    3.28 +  finally show ?thesis .
    3.29 +qed
    3.30 +
    3.31 +
    3.32  lemma open_ereal_def:
    3.33    "open A \<longleftrightarrow> open (ereal -` A) \<and> (\<infinity> \<in> A \<longrightarrow> (\<exists>x. {ereal x <..} \<subseteq> A)) \<and> (-\<infinity> \<in> A \<longrightarrow> (\<exists>x. {..<ereal x} \<subseteq> A))"
    3.34    (is "open A \<longleftrightarrow> ?rhs")
    3.35 @@ -2265,6 +2281,27 @@
    3.36    shows "a * b = a * c \<longleftrightarrow> (\<bar>a\<bar> = \<infinity> \<and> 0 < b * c) \<or> a = 0 \<or> b = c"
    3.37    by (cases rule: ereal3_cases[of a b c]) (simp_all add: zero_less_mult_iff)
    3.38  
    3.39 +lemma tendsto_add_ereal:
    3.40 +  fixes x y :: ereal
    3.41 +  assumes x: "\<bar>x\<bar> \<noteq> \<infinity>" and y: "\<bar>y\<bar> \<noteq> \<infinity>"
    3.42 +  assumes f: "(f ---> x) F" and g: "(g ---> y) F"
    3.43 +  shows "((\<lambda>x. f x + g x) ---> x + y) F"
    3.44 +proof -
    3.45 +  from x obtain r where x': "x = ereal r" by (cases x) auto
    3.46 +  with f have "((\<lambda>i. real (f i)) ---> r) F" by simp
    3.47 +  moreover
    3.48 +  from y obtain p where y': "y = ereal p" by (cases y) auto
    3.49 +  with g have "((\<lambda>i. real (g i)) ---> p) F" by simp
    3.50 +  ultimately have "((\<lambda>i. real (f i) + real (g i)) ---> r + p) F"
    3.51 +    by (rule tendsto_add)
    3.52 +  moreover
    3.53 +  from eventually_finite[OF x f] eventually_finite[OF y g]
    3.54 +  have "eventually (\<lambda>x. f x + g x = ereal (real (f x) + real (g x))) F"
    3.55 +    by eventually_elim auto
    3.56 +  ultimately show ?thesis
    3.57 +    by (simp add: x' y' cong: filterlim_cong)
    3.58 +qed
    3.59 +
    3.60  lemma ereal_inj_affinity:
    3.61    fixes m t :: ereal
    3.62    assumes "\<bar>m\<bar> \<noteq> \<infinity>"
     4.1 --- a/src/HOL/Library/Indicator_Function.thy	Mon May 19 11:27:02 2014 +0200
     4.2 +++ b/src/HOL/Library/Indicator_Function.thy	Mon May 19 12:04:45 2014 +0200
     4.3 @@ -5,7 +5,7 @@
     4.4  header {* Indicator Function *}
     4.5  
     4.6  theory Indicator_Function
     4.7 -imports Main
     4.8 +imports Complex_Main
     4.9  begin
    4.10  
    4.11  definition "indicator S x = (if x \<in> S then 1 else 0)"
    4.12 @@ -65,4 +65,9 @@
    4.13    using setsum_mult_indicator[OF assms, of "%x. 1::nat"]
    4.14    unfolding card_eq_setsum by simp
    4.15  
    4.16 +lemma setsum_indicator_scaleR[simp]:
    4.17 +  "finite A \<Longrightarrow>
    4.18 +    (\<Sum>x \<in> A. indicator (B x) (g x) *\<^sub>R f x) = (\<Sum>x \<in> {x\<in>A. g x \<in> B x}. f x::'a::real_vector)"
    4.19 +  using assms by (auto intro!: setsum_mono_zero_cong_right split: split_if_asm simp: indicator_def)
    4.20 +
    4.21  end
    4.22 \ No newline at end of file
     5.1 --- a/src/HOL/Probability/Binary_Product_Measure.thy	Mon May 19 11:27:02 2014 +0200
     5.2 +++ b/src/HOL/Probability/Binary_Product_Measure.thy	Mon May 19 12:04:45 2014 +0200
     5.3 @@ -5,7 +5,7 @@
     5.4  header {*Binary product measures*}
     5.5  
     5.6  theory Binary_Product_Measure
     5.7 -imports Lebesgue_Integration
     5.8 +imports Nonnegative_Lebesgue_Integration
     5.9  begin
    5.10  
    5.11  lemma Pair_vimage_times[simp]: "Pair x -` (A \<times> B) = (if x \<in> A then B else {})"
    5.12 @@ -510,7 +510,7 @@
    5.13                     positive_integral_monotone_convergence_SUP incseq_def le_fun_def
    5.14                cong: measurable_cong)
    5.15  
    5.16 -lemma (in sigma_finite_measure) positive_integral_fst:
    5.17 +lemma (in sigma_finite_measure) positive_integral_fst':
    5.18    assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)" "\<And>x. 0 \<le> f x"
    5.19    shows "(\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>M \<partial>M1) = integral\<^sup>P (M1 \<Otimes>\<^sub>M M) f" (is "?I f = _")
    5.20  using f proof induct
    5.21 @@ -525,30 +525,25 @@
    5.22                     borel_measurable_positive_integral_fst' positive_integral_mono incseq_def le_fun_def
    5.23                cong: positive_integral_cong)
    5.24  
    5.25 -lemma (in sigma_finite_measure) positive_integral_fst_measurable:
    5.26 +lemma (in sigma_finite_measure) positive_integral_fst:
    5.27    assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
    5.28 -  shows "(\<lambda>x. \<integral>\<^sup>+ y. f (x, y) \<partial>M) \<in> borel_measurable M1"
    5.29 -      (is "?C f \<in> borel_measurable M1")
    5.30 -    and "(\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x, y) \<partial>M) \<partial>M1) = integral\<^sup>P (M1 \<Otimes>\<^sub>M M) f"
    5.31 +  shows "(\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x, y) \<partial>M) \<partial>M1) = integral\<^sup>P (M1 \<Otimes>\<^sub>M M) f"
    5.32    using f
    5.33      borel_measurable_positive_integral_fst'[of "\<lambda>x. max 0 (f x)"]
    5.34 -    positive_integral_fst[of "\<lambda>x. max 0 (f x)"]
    5.35 +    positive_integral_fst'[of "\<lambda>x. max 0 (f x)"]
    5.36    unfolding positive_integral_max_0 by auto
    5.37  
    5.38  lemma (in sigma_finite_measure) borel_measurable_positive_integral[measurable (raw)]:
    5.39    "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M) \<Longrightarrow> (\<lambda>x. \<integral>\<^sup>+ y. f x y \<partial>M) \<in> borel_measurable N"
    5.40 -  using positive_integral_fst_measurable(1)[of "split f" N] by simp
    5.41 +  using borel_measurable_positive_integral_fst'[of "\<lambda>x. max 0 (split f x)" N]
    5.42 +  by (simp add: positive_integral_max_0)
    5.43  
    5.44 -lemma (in sigma_finite_measure) borel_measurable_lebesgue_integral[measurable (raw)]:
    5.45 -  "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M) \<Longrightarrow> (\<lambda>x. \<integral> y. f x y \<partial>M) \<in> borel_measurable N"
    5.46 -  by (simp add: lebesgue_integral_def)
    5.47 -
    5.48 -lemma (in pair_sigma_finite) positive_integral_snd_measurable:
    5.49 +lemma (in pair_sigma_finite) positive_integral_snd:
    5.50    assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
    5.51    shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>P (M1 \<Otimes>\<^sub>M M2) f"
    5.52  proof -
    5.53    note measurable_pair_swap[OF f]
    5.54 -  from M1.positive_integral_fst_measurable[OF this]
    5.55 +  from M1.positive_integral_fst[OF this]
    5.56    have "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^sup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^sub>M M1))"
    5.57      by simp
    5.58    also have "(\<integral>\<^sup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>P (M1 \<Otimes>\<^sub>M M2) f"
    5.59 @@ -560,113 +555,7 @@
    5.60  lemma (in pair_sigma_finite) Fubini:
    5.61    assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
    5.62    shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x, y) \<partial>M2) \<partial>M1)"
    5.63 -  unfolding positive_integral_snd_measurable[OF assms]
    5.64 -  unfolding M2.positive_integral_fst_measurable[OF assms] ..
    5.65 -
    5.66 -lemma (in pair_sigma_finite) integrable_product_swap:
    5.67 -  assumes "integrable (M1 \<Otimes>\<^sub>M M2) f"
    5.68 -  shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x))"
    5.69 -proof -
    5.70 -  interpret Q: pair_sigma_finite M2 M1 by default
    5.71 -  have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
    5.72 -  show ?thesis unfolding *
    5.73 -    by (rule integrable_distr[OF measurable_pair_swap'])
    5.74 -       (simp add: distr_pair_swap[symmetric] assms)
    5.75 -qed
    5.76 -
    5.77 -lemma (in pair_sigma_finite) integrable_product_swap_iff:
    5.78 -  "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable (M1 \<Otimes>\<^sub>M M2) f"
    5.79 -proof -
    5.80 -  interpret Q: pair_sigma_finite M2 M1 by default
    5.81 -  from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
    5.82 -  show ?thesis by auto
    5.83 -qed
    5.84 -
    5.85 -lemma (in pair_sigma_finite) integral_product_swap:
    5.86 -  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
    5.87 -  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
    5.88 -proof -
    5.89 -  have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
    5.90 -  show ?thesis unfolding *
    5.91 -    by (simp add: integral_distr[symmetric, OF measurable_pair_swap' f] distr_pair_swap[symmetric])
    5.92 -qed
    5.93 -
    5.94 -lemma (in pair_sigma_finite) integrable_fst_measurable:
    5.95 -  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) f"
    5.96 -  shows "AE x in M1. integrable M2 (\<lambda> y. f (x, y))" (is "?AE")
    5.97 -    and "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f" (is "?INT")
    5.98 -proof -
    5.99 -  have f_borel: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   5.100 -    using f by auto
   5.101 -  let ?pf = "\<lambda>x. ereal (f x)" and ?nf = "\<lambda>x. ereal (- f x)"
   5.102 -  have
   5.103 -    borel: "?nf \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)""?pf \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" and
   5.104 -    int: "integral\<^sup>P (M1 \<Otimes>\<^sub>M M2) ?nf \<noteq> \<infinity>" "integral\<^sup>P (M1 \<Otimes>\<^sub>M M2) ?pf \<noteq> \<infinity>"
   5.105 -    using assms by auto
   5.106 -  have "(\<integral>\<^sup>+x. (\<integral>\<^sup>+y. ereal (f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
   5.107 -     "(\<integral>\<^sup>+x. (\<integral>\<^sup>+y. ereal (- f (x, y)) \<partial>M2) \<partial>M1) \<noteq> \<infinity>"
   5.108 -    using borel[THEN M2.positive_integral_fst_measurable(1)] int
   5.109 -    unfolding borel[THEN M2.positive_integral_fst_measurable(2)] by simp_all
   5.110 -  with borel[THEN M2.positive_integral_fst_measurable(1)]
   5.111 -  have AE_pos: "AE x in M1. (\<integral>\<^sup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
   5.112 -    "AE x in M1. (\<integral>\<^sup>+y. ereal (- f (x, y)) \<partial>M2) \<noteq> \<infinity>"
   5.113 -    by (auto intro!: positive_integral_PInf_AE )
   5.114 -  then have AE: "AE x in M1. \<bar>\<integral>\<^sup>+y. ereal (f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
   5.115 -    "AE x in M1. \<bar>\<integral>\<^sup>+y. ereal (- f (x, y)) \<partial>M2\<bar> \<noteq> \<infinity>"
   5.116 -    by (auto simp: positive_integral_positive)
   5.117 -  from AE_pos show ?AE using assms
   5.118 -    by (simp add: measurable_Pair2[OF f_borel] integrable_def)
   5.119 -  { fix f have "(\<integral>\<^sup>+ x. - \<integral>\<^sup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = (\<integral>\<^sup>+x. 0 \<partial>M1)"
   5.120 -      using positive_integral_positive
   5.121 -      by (intro positive_integral_cong_pos) (auto simp: ereal_uminus_le_reorder)
   5.122 -    then have "(\<integral>\<^sup>+ x. - \<integral>\<^sup>+ y. ereal (f x y) \<partial>M2 \<partial>M1) = 0" by simp }
   5.123 -  note this[simp]
   5.124 -  { fix f assume borel: "(\<lambda>x. ereal (f x)) \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   5.125 -      and int: "integral\<^sup>P (M1 \<Otimes>\<^sub>M M2) (\<lambda>x. ereal (f x)) \<noteq> \<infinity>"
   5.126 -      and AE: "AE x in M1. (\<integral>\<^sup>+y. ereal (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
   5.127 -    have "integrable M1 (\<lambda>x. real (\<integral>\<^sup>+y. ereal (f (x, y)) \<partial>M2))" (is "integrable M1 ?f")
   5.128 -    proof (intro integrable_def[THEN iffD2] conjI)
   5.129 -      show "?f \<in> borel_measurable M1"
   5.130 -        using borel by (auto intro!: M2.positive_integral_fst_measurable)
   5.131 -      have "(\<integral>\<^sup>+x. ereal (?f x) \<partial>M1) = (\<integral>\<^sup>+x. (\<integral>\<^sup>+y. ereal (f (x, y))  \<partial>M2) \<partial>M1)"
   5.132 -        using AE positive_integral_positive[of M2]
   5.133 -        by (auto intro!: positive_integral_cong_AE simp: ereal_real)
   5.134 -      then show "(\<integral>\<^sup>+x. ereal (?f x) \<partial>M1) \<noteq> \<infinity>"
   5.135 -        using M2.positive_integral_fst_measurable[OF borel] int by simp
   5.136 -      have "(\<integral>\<^sup>+x. ereal (- ?f x) \<partial>M1) = (\<integral>\<^sup>+x. 0 \<partial>M1)"
   5.137 -        by (intro positive_integral_cong_pos)
   5.138 -           (simp add: positive_integral_positive real_of_ereal_pos)
   5.139 -      then show "(\<integral>\<^sup>+x. ereal (- ?f x) \<partial>M1) \<noteq> \<infinity>" by simp
   5.140 -    qed }
   5.141 -  with this[OF borel(1) int(1) AE_pos(2)] this[OF borel(2) int(2) AE_pos(1)]
   5.142 -  show ?INT
   5.143 -    unfolding lebesgue_integral_def[of "M1 \<Otimes>\<^sub>M M2"] lebesgue_integral_def[of M2]
   5.144 -      borel[THEN M2.positive_integral_fst_measurable(2), symmetric]
   5.145 -    using AE[THEN integral_real]
   5.146 -    by simp
   5.147 -qed
   5.148 -
   5.149 -lemma (in pair_sigma_finite) integrable_snd_measurable:
   5.150 -  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) f"
   5.151 -  shows "AE y in M2. integrable M1 (\<lambda>x. f (x, y))" (is "?AE")
   5.152 -    and "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f" (is "?INT")
   5.153 -proof -
   5.154 -  interpret Q: pair_sigma_finite M2 M1 by default
   5.155 -  have Q_int: "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x, y). f (y, x))"
   5.156 -    using f unfolding integrable_product_swap_iff .
   5.157 -  show ?INT
   5.158 -    using Q.integrable_fst_measurable(2)[OF Q_int]
   5.159 -    using integral_product_swap[of f] f by auto
   5.160 -  show ?AE
   5.161 -    using Q.integrable_fst_measurable(1)[OF Q_int]
   5.162 -    by simp
   5.163 -qed
   5.164 -
   5.165 -lemma (in pair_sigma_finite) Fubini_integral:
   5.166 -  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) f"
   5.167 -  shows "(\<integral>y. (\<integral>x. f (x, y) \<partial>M1) \<partial>M2) = (\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1)"
   5.168 -  unfolding integrable_snd_measurable[OF assms]
   5.169 -  unfolding integrable_fst_measurable[OF assms] ..
   5.170 +  unfolding positive_integral_snd[OF assms] M2.positive_integral_fst[OF assms] ..
   5.171  
   5.172  section {* Products on counting spaces, densities and distributions *}
   5.173  
   5.174 @@ -741,7 +630,7 @@
   5.175         (auto simp add: positive_integral_cmult[symmetric] ac_simps)
   5.176    with A f g show "emeasure ?L A = emeasure ?R A"
   5.177      by (simp add: D2.emeasure_pair_measure emeasure_density positive_integral_density
   5.178 -                  M2.positive_integral_fst_measurable(2)[symmetric]
   5.179 +                  M2.positive_integral_fst[symmetric]
   5.180               cong: positive_integral_cong)
   5.181  qed simp
   5.182  
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/HOL/Probability/Bochner_Integration.thy	Mon May 19 12:04:45 2014 +0200
     6.3 @@ -0,0 +1,2615 @@
     6.4 +(*  Title:      HOL/Probability/Bochner_Integration.thy
     6.5 +    Author:     Johannes Hölzl, TU München
     6.6 +*)
     6.7 +
     6.8 +header {* Bochner Integration for Vector-Valued Functions *}
     6.9 +
    6.10 +theory Bochner_Integration
    6.11 +  imports Finite_Product_Measure
    6.12 +begin
    6.13 +
    6.14 +text {*
    6.15 +
    6.16 +In the following development of the Bochner integral we use second countable topologies instead
    6.17 +of separable spaces. A second countable topology is also separable.
    6.18 +
    6.19 +*}
    6.20 +
    6.21 +lemma borel_measurable_implies_sequence_metric:
    6.22 +  fixes f :: "'a \<Rightarrow> 'b :: {metric_space, second_countable_topology}"
    6.23 +  assumes [measurable]: "f \<in> borel_measurable M"
    6.24 +  shows "\<exists>F. (\<forall>i. simple_function M (F i)) \<and> (\<forall>x\<in>space M. (\<lambda>i. F i x) ----> f x) \<and>
    6.25 +    (\<forall>i. \<forall>x\<in>space M. dist (F i x) z \<le> 2 * dist (f x) z)"
    6.26 +proof -
    6.27 +  obtain D :: "'b set" where "countable D" and D: "\<And>X. open X \<Longrightarrow> X \<noteq> {} \<Longrightarrow> \<exists>d\<in>D. d \<in> X"
    6.28 +    by (erule countable_dense_setE)
    6.29 +
    6.30 +  def e \<equiv> "from_nat_into D"
    6.31 +  { fix n x
    6.32 +    obtain d where "d \<in> D" and d: "d \<in> ball x (1 / Suc n)"
    6.33 +      using D[of "ball x (1 / Suc n)"] by auto
    6.34 +    from `d \<in> D` D[of UNIV] `countable D` obtain i where "d = e i"
    6.35 +      unfolding e_def by (auto dest: from_nat_into_surj)
    6.36 +    with d have "\<exists>i. dist x (e i) < 1 / Suc n"
    6.37 +      by auto }
    6.38 +  note e = this
    6.39 +
    6.40 +  def A \<equiv> "\<lambda>m n. {x\<in>space M. dist (f x) (e n) < 1 / (Suc m) \<and> 1 / (Suc m) \<le> dist (f x) z}"
    6.41 +  def B \<equiv> "\<lambda>m. disjointed (A m)"
    6.42 +  
    6.43 +  def m \<equiv> "\<lambda>N x. Max {m::nat. m \<le> N \<and> x \<in> (\<Union>n\<le>N. B m n)}"
    6.44 +  def F \<equiv> "\<lambda>N::nat. \<lambda>x. if (\<exists>m\<le>N. x \<in> (\<Union>n\<le>N. B m n)) \<and> (\<exists>n\<le>N. x \<in> B (m N x) n) 
    6.45 +    then e (LEAST n. x \<in> B (m N x) n) else z"
    6.46 +
    6.47 +  have B_imp_A[intro, simp]: "\<And>x m n. x \<in> B m n \<Longrightarrow> x \<in> A m n"
    6.48 +    using disjointed_subset[of "A m" for m] unfolding B_def by auto
    6.49 +
    6.50 +  { fix m
    6.51 +    have "\<And>n. A m n \<in> sets M"
    6.52 +      by (auto simp: A_def)
    6.53 +    then have "\<And>n. B m n \<in> sets M"
    6.54 +      using sets.range_disjointed_sets[of "A m" M] by (auto simp: B_def) }
    6.55 +  note this[measurable]
    6.56 +
    6.57 +  { fix N i x assume "\<exists>m\<le>N. x \<in> (\<Union>n\<le>N. B m n)"
    6.58 +    then have "m N x \<in> {m::nat. m \<le> N \<and> x \<in> (\<Union>n\<le>N. B m n)}"
    6.59 +      unfolding m_def by (intro Max_in) auto
    6.60 +    then have "m N x \<le> N" "\<exists>n\<le>N. x \<in> B (m N x) n"
    6.61 +      by auto }
    6.62 +  note m = this
    6.63 +
    6.64 +  { fix j N i x assume "j \<le> N" "i \<le> N" "x \<in> B j i"
    6.65 +    then have "j \<le> m N x"
    6.66 +      unfolding m_def by (intro Max_ge) auto }
    6.67 +  note m_upper = this
    6.68 +
    6.69 +  show ?thesis
    6.70 +    unfolding simple_function_def
    6.71 +  proof (safe intro!: exI[of _ F])
    6.72 +    have [measurable]: "\<And>i. F i \<in> borel_measurable M"
    6.73 +      unfolding F_def m_def by measurable
    6.74 +    show "\<And>x i. F i -` {x} \<inter> space M \<in> sets M"
    6.75 +      by measurable
    6.76 +
    6.77 +    { fix i
    6.78 +      { fix n x assume "x \<in> B (m i x) n"
    6.79 +        then have "(LEAST n. x \<in> B (m i x) n) \<le> n"
    6.80 +          by (intro Least_le)
    6.81 +        also assume "n \<le> i" 
    6.82 +        finally have "(LEAST n. x \<in> B (m i x) n) \<le> i" . }
    6.83 +      then have "F i ` space M \<subseteq> {z} \<union> e ` {.. i}"
    6.84 +        by (auto simp: F_def)
    6.85 +      then show "finite (F i ` space M)"
    6.86 +        by (rule finite_subset) auto }
    6.87 +    
    6.88 +    { fix N i n x assume "i \<le> N" "n \<le> N" "x \<in> B i n"
    6.89 +      then have 1: "\<exists>m\<le>N. x \<in> (\<Union> n\<le>N. B m n)" by auto
    6.90 +      from m[OF this] obtain n where n: "m N x \<le> N" "n \<le> N" "x \<in> B (m N x) n" by auto
    6.91 +      moreover
    6.92 +      def L \<equiv> "LEAST n. x \<in> B (m N x) n"
    6.93 +      have "dist (f x) (e L) < 1 / Suc (m N x)"
    6.94 +      proof -
    6.95 +        have "x \<in> B (m N x) L"
    6.96 +          using n(3) unfolding L_def by (rule LeastI)
    6.97 +        then have "x \<in> A (m N x) L"
    6.98 +          by auto
    6.99 +        then show ?thesis
   6.100 +          unfolding A_def by simp
   6.101 +      qed
   6.102 +      ultimately have "dist (f x) (F N x) < 1 / Suc (m N x)"
   6.103 +        by (auto simp add: F_def L_def) }
   6.104 +    note * = this
   6.105 +
   6.106 +    fix x assume "x \<in> space M"
   6.107 +    show "(\<lambda>i. F i x) ----> f x"
   6.108 +    proof cases
   6.109 +      assume "f x = z"
   6.110 +      then have "\<And>i n. x \<notin> A i n"
   6.111 +        unfolding A_def by auto
   6.112 +      then have "\<And>i. F i x = z"
   6.113 +        by (auto simp: F_def)
   6.114 +      then show ?thesis
   6.115 +        using `f x = z` by auto
   6.116 +    next
   6.117 +      assume "f x \<noteq> z"
   6.118 +
   6.119 +      show ?thesis
   6.120 +      proof (rule tendstoI)
   6.121 +        fix e :: real assume "0 < e"
   6.122 +        with `f x \<noteq> z` obtain n where "1 / Suc n < e" "1 / Suc n < dist (f x) z"
   6.123 +          by (metis dist_nz order_less_trans neq_iff nat_approx_posE)
   6.124 +        with `x\<in>space M` `f x \<noteq> z` have "x \<in> (\<Union>i. B n i)"
   6.125 +          unfolding A_def B_def UN_disjointed_eq using e by auto
   6.126 +        then obtain i where i: "x \<in> B n i" by auto
   6.127 +
   6.128 +        show "eventually (\<lambda>i. dist (F i x) (f x) < e) sequentially"
   6.129 +          using eventually_ge_at_top[of "max n i"]
   6.130 +        proof eventually_elim
   6.131 +          fix j assume j: "max n i \<le> j"
   6.132 +          with i have "dist (f x) (F j x) < 1 / Suc (m j x)"
   6.133 +            by (intro *[OF _ _ i]) auto
   6.134 +          also have "\<dots> \<le> 1 / Suc n"
   6.135 +            using j m_upper[OF _ _ i]
   6.136 +            by (auto simp: field_simps)
   6.137 +          also note `1 / Suc n < e`
   6.138 +          finally show "dist (F j x) (f x) < e"
   6.139 +            by (simp add: less_imp_le dist_commute)
   6.140 +        qed
   6.141 +      qed
   6.142 +    qed
   6.143 +    fix i 
   6.144 +    { fix n m assume "x \<in> A n m"
   6.145 +      then have "dist (e m) (f x) + dist (f x) z \<le> 2 * dist (f x) z"
   6.146 +        unfolding A_def by (auto simp: dist_commute)
   6.147 +      also have "dist (e m) z \<le> dist (e m) (f x) + dist (f x) z"
   6.148 +        by (rule dist_triangle)
   6.149 +      finally (xtrans) have "dist (e m) z \<le> 2 * dist (f x) z" . }
   6.150 +    then show "dist (F i x) z \<le> 2 * dist (f x) z"
   6.151 +      unfolding F_def
   6.152 +      apply auto
   6.153 +      apply (rule LeastI2)
   6.154 +      apply auto
   6.155 +      done
   6.156 +  qed
   6.157 +qed
   6.158 +
   6.159 +lemma real_indicator: "real (indicator A x :: ereal) = indicator A x"
   6.160 +  unfolding indicator_def by auto
   6.161 +
   6.162 +lemma split_indicator_asm:
   6.163 +  "P (indicator S x) \<longleftrightarrow> \<not> ((x \<in> S \<and> \<not> P 1) \<or> (x \<notin> S \<and> \<not> P 0))"
   6.164 +  unfolding indicator_def by auto
   6.165 +
   6.166 +lemma
   6.167 +  fixes f :: "'a \<Rightarrow> 'b::semiring_1" assumes "finite A"
   6.168 +  shows setsum_mult_indicator[simp]: "(\<Sum>x \<in> A. f x * indicator (B x) (g x)) = (\<Sum>x\<in>{x\<in>A. g x \<in> B x}. f x)"
   6.169 +  and setsum_indicator_mult[simp]: "(\<Sum>x \<in> A. indicator (B x) (g x) * f x) = (\<Sum>x\<in>{x\<in>A. g x \<in> B x}. f x)"
   6.170 +  unfolding indicator_def
   6.171 +  using assms by (auto intro!: setsum_mono_zero_cong_right split: split_if_asm)
   6.172 +
   6.173 +lemma borel_measurable_induct_real[consumes 2, case_names set mult add seq]:
   6.174 +  fixes P :: "('a \<Rightarrow> real) \<Rightarrow> bool"
   6.175 +  assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
   6.176 +  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   6.177 +  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
   6.178 +  assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x = 0 \<or> v x = 0) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
   6.179 +  assumes seq: "\<And>U. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow> (\<And>i x. 0 \<le> U i x) \<Longrightarrow> (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. U i x) ----> u x) \<Longrightarrow> P u"
   6.180 +  shows "P u"
   6.181 +proof -
   6.182 +  have "(\<lambda>x. ereal (u x)) \<in> borel_measurable M" using u by auto
   6.183 +  from borel_measurable_implies_simple_function_sequence'[OF this]
   6.184 +  obtain U where U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
   6.185 +    sup: "\<And>x. (SUP i. U i x) = max 0 (ereal (u x))" and nn: "\<And>i x. 0 \<le> U i x"
   6.186 +    by blast
   6.187 +
   6.188 +  def U' \<equiv> "\<lambda>i x. indicator (space M) x * real (U i x)"
   6.189 +  then have U'_sf[measurable]: "\<And>i. simple_function M (U' i)"
   6.190 +    using U by (auto intro!: simple_function_compose1[where g=real])
   6.191 +
   6.192 +  show "P u"
   6.193 +  proof (rule seq)
   6.194 +    fix i show "U' i \<in> borel_measurable M" "\<And>x. 0 \<le> U' i x"
   6.195 +      using U nn by (auto
   6.196 +          intro: borel_measurable_simple_function 
   6.197 +          intro!: borel_measurable_real_of_ereal real_of_ereal_pos borel_measurable_times
   6.198 +          simp: U'_def zero_le_mult_iff)
   6.199 +    show "incseq U'"
   6.200 +      using U(2,3) nn
   6.201 +      by (auto simp: incseq_def le_fun_def image_iff eq_commute U'_def indicator_def
   6.202 +               intro!: real_of_ereal_positive_mono)
   6.203 +  next
   6.204 +    fix x assume x: "x \<in> space M"
   6.205 +    have "(\<lambda>i. U i x) ----> (SUP i. U i x)"
   6.206 +      using U(2) by (intro LIMSEQ_SUP) (auto simp: incseq_def le_fun_def)
   6.207 +    moreover have "(\<lambda>i. U i x) = (\<lambda>i. ereal (U' i x))"
   6.208 +      using x nn U(3) by (auto simp: fun_eq_iff U'_def ereal_real image_iff eq_commute)
   6.209 +    moreover have "(SUP i. U i x) = ereal (u x)"
   6.210 +      using sup u(2) by (simp add: max_def)
   6.211 +    ultimately show "(\<lambda>i. U' i x) ----> u x" 
   6.212 +      by simp
   6.213 +  next
   6.214 +    fix i
   6.215 +    have "U' i ` space M \<subseteq> real ` (U i ` space M)" "finite (U i ` space M)"
   6.216 +      unfolding U'_def using U(1) by (auto dest: simple_functionD)
   6.217 +    then have fin: "finite (U' i ` space M)"
   6.218 +      by (metis finite_subset finite_imageI)
   6.219 +    moreover have "\<And>z. {y. U' i z = y \<and> y \<in> U' i ` space M \<and> z \<in> space M} = (if z \<in> space M then {U' i z} else {})"
   6.220 +      by auto
   6.221 +    ultimately have U': "(\<lambda>z. \<Sum>y\<in>U' i`space M. y * indicator {x\<in>space M. U' i x = y} z) = U' i"
   6.222 +      by (simp add: U'_def fun_eq_iff)
   6.223 +    have "\<And>x. x \<in> U' i ` space M \<Longrightarrow> 0 \<le> x"
   6.224 +      using nn by (auto simp: U'_def real_of_ereal_pos)
   6.225 +    with fin have "P (\<lambda>z. \<Sum>y\<in>U' i`space M. y * indicator {x\<in>space M. U' i x = y} z)"
   6.226 +    proof induct
   6.227 +      case empty from set[of "{}"] show ?case
   6.228 +        by (simp add: indicator_def[abs_def])
   6.229 +    next
   6.230 +      case (insert x F)
   6.231 +      then show ?case
   6.232 +        by (auto intro!: add mult set setsum_nonneg split: split_indicator split_indicator_asm
   6.233 +                 simp del: setsum_mult_indicator simp: setsum_nonneg_eq_0_iff )
   6.234 +    qed
   6.235 +    with U' show "P (U' i)" by simp
   6.236 +  qed
   6.237 +qed
   6.238 +
   6.239 +lemma scaleR_cong_right:
   6.240 +  fixes x :: "'a :: real_vector"
   6.241 +  shows "(x \<noteq> 0 \<Longrightarrow> r = p) \<Longrightarrow> r *\<^sub>R x = p *\<^sub>R x"
   6.242 +  by (cases "x = 0") auto
   6.243 +
   6.244 +inductive simple_bochner_integrable :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b::real_vector) \<Rightarrow> bool" for M f where
   6.245 +  "simple_function M f \<Longrightarrow> emeasure M {y\<in>space M. f y \<noteq> 0} \<noteq> \<infinity> \<Longrightarrow>
   6.246 +    simple_bochner_integrable M f"
   6.247 +
   6.248 +lemma simple_bochner_integrable_compose2:
   6.249 +  assumes p_0: "p 0 0 = 0"
   6.250 +  shows "simple_bochner_integrable M f \<Longrightarrow> simple_bochner_integrable M g \<Longrightarrow>
   6.251 +    simple_bochner_integrable M (\<lambda>x. p (f x) (g x))"
   6.252 +proof (safe intro!: simple_bochner_integrable.intros elim!: simple_bochner_integrable.cases del: notI)
   6.253 +  assume sf: "simple_function M f" "simple_function M g"
   6.254 +  then show "simple_function M (\<lambda>x. p (f x) (g x))"
   6.255 +    by (rule simple_function_compose2)
   6.256 +
   6.257 +  from sf have [measurable]:
   6.258 +      "f \<in> measurable M (count_space UNIV)"
   6.259 +      "g \<in> measurable M (count_space UNIV)"
   6.260 +    by (auto intro: measurable_simple_function)
   6.261 +
   6.262 +  assume fin: "emeasure M {y \<in> space M. f y \<noteq> 0} \<noteq> \<infinity>" "emeasure M {y \<in> space M. g y \<noteq> 0} \<noteq> \<infinity>"
   6.263 +   
   6.264 +  have "emeasure M {x\<in>space M. p (f x) (g x) \<noteq> 0} \<le>
   6.265 +      emeasure M ({x\<in>space M. f x \<noteq> 0} \<union> {x\<in>space M. g x \<noteq> 0})"
   6.266 +    by (intro emeasure_mono) (auto simp: p_0)
   6.267 +  also have "\<dots> \<le> emeasure M {x\<in>space M. f x \<noteq> 0} + emeasure M {x\<in>space M. g x \<noteq> 0}"
   6.268 +    by (intro emeasure_subadditive) auto
   6.269 +  finally show "emeasure M {y \<in> space M. p (f y) (g y) \<noteq> 0} \<noteq> \<infinity>"
   6.270 +    using fin by auto
   6.271 +qed
   6.272 +
   6.273 +lemma simple_function_finite_support:
   6.274 +  assumes f: "simple_function M f" and fin: "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>" and nn: "\<And>x. 0 \<le> f x"
   6.275 +  shows "emeasure M {x\<in>space M. f x \<noteq> 0} \<noteq> \<infinity>"
   6.276 +proof cases
   6.277 +  from f have meas[measurable]: "f \<in> borel_measurable M"
   6.278 +    by (rule borel_measurable_simple_function)
   6.279 +
   6.280 +  assume non_empty: "\<exists>x\<in>space M. f x \<noteq> 0"
   6.281 +
   6.282 +  def m \<equiv> "Min (f`space M - {0})"
   6.283 +  have "m \<in> f`space M - {0}"
   6.284 +    unfolding m_def using f non_empty by (intro Min_in) (auto simp: simple_function_def)
   6.285 +  then have m: "0 < m"
   6.286 +    using nn by (auto simp: less_le)
   6.287 +
   6.288 +  from m have "m * emeasure M {x\<in>space M. 0 \<noteq> f x} = 
   6.289 +    (\<integral>\<^sup>+x. m * indicator {x\<in>space M. 0 \<noteq> f x} x \<partial>M)"
   6.290 +    using f by (intro positive_integral_cmult_indicator[symmetric]) auto
   6.291 +  also have "\<dots> \<le> (\<integral>\<^sup>+x. f x \<partial>M)"
   6.292 +    using AE_space
   6.293 +  proof (intro positive_integral_mono_AE, eventually_elim)
   6.294 +    fix x assume "x \<in> space M"
   6.295 +    with nn show "m * indicator {x \<in> space M. 0 \<noteq> f x} x \<le> f x"
   6.296 +      using f by (auto split: split_indicator simp: simple_function_def m_def)
   6.297 +  qed
   6.298 +  also note `\<dots> < \<infinity>`
   6.299 +  finally show ?thesis
   6.300 +    using m by auto 
   6.301 +next
   6.302 +  assume "\<not> (\<exists>x\<in>space M. f x \<noteq> 0)"
   6.303 +  with nn have *: "{x\<in>space M. f x \<noteq> 0} = {}"
   6.304 +    by auto
   6.305 +  show ?thesis unfolding * by simp
   6.306 +qed
   6.307 +
   6.308 +lemma simple_bochner_integrableI_bounded:
   6.309 +  assumes f: "simple_function M f" and fin: "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
   6.310 +  shows "simple_bochner_integrable M f"
   6.311 +proof
   6.312 +  have "emeasure M {y \<in> space M. ereal (norm (f y)) \<noteq> 0} \<noteq> \<infinity>"
   6.313 +  proof (rule simple_function_finite_support)
   6.314 +    show "simple_function M (\<lambda>x. ereal (norm (f x)))"
   6.315 +      using f by (rule simple_function_compose1)
   6.316 +    show "(\<integral>\<^sup>+ y. ereal (norm (f y)) \<partial>M) < \<infinity>" by fact
   6.317 +  qed simp
   6.318 +  then show "emeasure M {y \<in> space M. f y \<noteq> 0} \<noteq> \<infinity>" by simp
   6.319 +qed fact
   6.320 +
   6.321 +definition simple_bochner_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b::real_vector) \<Rightarrow> 'b" where
   6.322 +  "simple_bochner_integral M f = (\<Sum>y\<in>f`space M. measure M {x\<in>space M. f x = y} *\<^sub>R y)"
   6.323 +
   6.324 +lemma simple_bochner_integral_partition:
   6.325 +  assumes f: "simple_bochner_integrable M f" and g: "simple_function M g"
   6.326 +  assumes sub: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow> g x = g y \<Longrightarrow> f x = f y"
   6.327 +  assumes v: "\<And>x. x \<in> space M \<Longrightarrow> f x = v (g x)"
   6.328 +  shows "simple_bochner_integral M f = (\<Sum>y\<in>g ` space M. measure M {x\<in>space M. g x = y} *\<^sub>R v y)"
   6.329 +    (is "_ = ?r")
   6.330 +proof -
   6.331 +  from f g have [simp]: "finite (f`space M)" "finite (g`space M)"
   6.332 +    by (auto simp: simple_function_def elim: simple_bochner_integrable.cases)
   6.333 +
   6.334 +  from f have [measurable]: "f \<in> measurable M (count_space UNIV)"
   6.335 +    by (auto intro: measurable_simple_function elim: simple_bochner_integrable.cases)
   6.336 +
   6.337 +  from g have [measurable]: "g \<in> measurable M (count_space UNIV)"
   6.338 +    by (auto intro: measurable_simple_function elim: simple_bochner_integrable.cases)
   6.339 +
   6.340 +  { fix y assume "y \<in> space M"
   6.341 +    then have "f ` space M \<inter> {i. \<exists>x\<in>space M. i = f x \<and> g y = g x} = {v (g y)}"
   6.342 +      by (auto cong: sub simp: v[symmetric]) }
   6.343 +  note eq = this
   6.344 +
   6.345 +  have "simple_bochner_integral M f =
   6.346 +    (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M. 
   6.347 +      if \<exists>x\<in>space M. y = f x \<and> z = g x then measure M {x\<in>space M. g x = z} else 0) *\<^sub>R y)"
   6.348 +    unfolding simple_bochner_integral_def
   6.349 +  proof (safe intro!: setsum_cong scaleR_cong_right)
   6.350 +    fix y assume y: "y \<in> space M" "f y \<noteq> 0"
   6.351 +    have [simp]: "g ` space M \<inter> {z. \<exists>x\<in>space M. f y = f x \<and> z = g x} = 
   6.352 +        {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
   6.353 +      by auto
   6.354 +    have eq:"{x \<in> space M. f x = f y} =
   6.355 +        (\<Union>i\<in>{z. \<exists>x\<in>space M. f y = f x \<and> z = g x}. {x \<in> space M. g x = i})"
   6.356 +      by (auto simp: eq_commute cong: sub rev_conj_cong)
   6.357 +    have "finite (g`space M)" by simp
   6.358 +    then have "finite {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
   6.359 +      by (rule rev_finite_subset) auto
   6.360 +    moreover
   6.361 +    { fix x assume "x \<in> space M" "f x = f y"
   6.362 +      then have "x \<in> space M" "f x \<noteq> 0"
   6.363 +        using y by auto
   6.364 +      then have "emeasure M {y \<in> space M. g y = g x} \<le> emeasure M {y \<in> space M. f y \<noteq> 0}"
   6.365 +        by (auto intro!: emeasure_mono cong: sub)
   6.366 +      then have "emeasure M {xa \<in> space M. g xa = g x} < \<infinity>"
   6.367 +        using f by (auto simp: simple_bochner_integrable.simps) }
   6.368 +    ultimately
   6.369 +    show "measure M {x \<in> space M. f x = f y} =
   6.370 +      (\<Sum>z\<in>g ` space M. if \<exists>x\<in>space M. f y = f x \<and> z = g x then measure M {x \<in> space M. g x = z} else 0)"
   6.371 +      apply (simp add: setsum_cases eq)
   6.372 +      apply (subst measure_finite_Union[symmetric])
   6.373 +      apply (auto simp: disjoint_family_on_def)
   6.374 +      done
   6.375 +  qed
   6.376 +  also have "\<dots> = (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M. 
   6.377 +      if \<exists>x\<in>space M. y = f x \<and> z = g x then measure M {x\<in>space M. g x = z} *\<^sub>R y else 0))"
   6.378 +    by (auto intro!: setsum_cong simp: scaleR_setsum_left)
   6.379 +  also have "\<dots> = ?r"
   6.380 +    by (subst setsum_commute)
   6.381 +       (auto intro!: setsum_cong simp: setsum_cases scaleR_setsum_right[symmetric] eq)
   6.382 +  finally show "simple_bochner_integral M f = ?r" .
   6.383 +qed
   6.384 +
   6.385 +lemma simple_bochner_integral_add:
   6.386 +  assumes f: "simple_bochner_integrable M f" and g: "simple_bochner_integrable M g"
   6.387 +  shows "simple_bochner_integral M (\<lambda>x. f x + g x) =
   6.388 +    simple_bochner_integral M f + simple_bochner_integral M g"
   6.389 +proof -
   6.390 +  from f g have "simple_bochner_integral M (\<lambda>x. f x + g x) =
   6.391 +    (\<Sum>y\<in>(\<lambda>x. (f x, g x)) ` space M. measure M {x \<in> space M. (f x, g x) = y} *\<^sub>R (fst y + snd y))"
   6.392 +    by (intro simple_bochner_integral_partition)
   6.393 +       (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   6.394 +  moreover from f g have "simple_bochner_integral M f =
   6.395 +    (\<Sum>y\<in>(\<lambda>x. (f x, g x)) ` space M. measure M {x \<in> space M. (f x, g x) = y} *\<^sub>R fst y)"
   6.396 +    by (intro simple_bochner_integral_partition)
   6.397 +       (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   6.398 +  moreover from f g have "simple_bochner_integral M g =
   6.399 +    (\<Sum>y\<in>(\<lambda>x. (f x, g x)) ` space M. measure M {x \<in> space M. (f x, g x) = y} *\<^sub>R snd y)"
   6.400 +    by (intro simple_bochner_integral_partition)
   6.401 +       (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   6.402 +  ultimately show ?thesis
   6.403 +    by (simp add: setsum_addf[symmetric] scaleR_add_right)
   6.404 +qed
   6.405 +
   6.406 +lemma (in linear) simple_bochner_integral_linear:
   6.407 +  assumes g: "simple_bochner_integrable M g"
   6.408 +  shows "simple_bochner_integral M (\<lambda>x. f (g x)) = f (simple_bochner_integral M g)"
   6.409 +proof -
   6.410 +  from g have "simple_bochner_integral M (\<lambda>x. f (g x)) =
   6.411 +    (\<Sum>y\<in>g ` space M. measure M {x \<in> space M. g x = y} *\<^sub>R f y)"
   6.412 +    by (intro simple_bochner_integral_partition)
   6.413 +       (auto simp: simple_bochner_integrable_compose2[where p="\<lambda>x y. f x"] zero
   6.414 +             elim: simple_bochner_integrable.cases)
   6.415 +  also have "\<dots> = f (simple_bochner_integral M g)"
   6.416 +    by (simp add: simple_bochner_integral_def setsum scaleR)
   6.417 +  finally show ?thesis .
   6.418 +qed
   6.419 +
   6.420 +lemma simple_bochner_integral_minus:
   6.421 +  assumes f: "simple_bochner_integrable M f"
   6.422 +  shows "simple_bochner_integral M (\<lambda>x. - f x) = - simple_bochner_integral M f"
   6.423 +proof -
   6.424 +  interpret linear uminus by unfold_locales auto
   6.425 +  from f show ?thesis
   6.426 +    by (rule simple_bochner_integral_linear)
   6.427 +qed
   6.428 +
   6.429 +lemma simple_bochner_integral_diff:
   6.430 +  assumes f: "simple_bochner_integrable M f" and g: "simple_bochner_integrable M g"
   6.431 +  shows "simple_bochner_integral M (\<lambda>x. f x - g x) =
   6.432 +    simple_bochner_integral M f - simple_bochner_integral M g"
   6.433 +  unfolding diff_conv_add_uminus using f g
   6.434 +  by (subst simple_bochner_integral_add)
   6.435 +     (auto simp: simple_bochner_integral_minus simple_bochner_integrable_compose2[where p="\<lambda>x y. - y"])
   6.436 +
   6.437 +lemma simple_bochner_integral_norm_bound:
   6.438 +  assumes f: "simple_bochner_integrable M f"
   6.439 +  shows "norm (simple_bochner_integral M f) \<le> simple_bochner_integral M (\<lambda>x. norm (f x))"
   6.440 +proof -
   6.441 +  have "norm (simple_bochner_integral M f) \<le> 
   6.442 +    (\<Sum>y\<in>f ` space M. norm (measure M {x \<in> space M. f x = y} *\<^sub>R y))"
   6.443 +    unfolding simple_bochner_integral_def by (rule norm_setsum)
   6.444 +  also have "\<dots> = (\<Sum>y\<in>f ` space M. measure M {x \<in> space M. f x = y} *\<^sub>R norm y)"
   6.445 +    by (simp add: measure_nonneg)
   6.446 +  also have "\<dots> = simple_bochner_integral M (\<lambda>x. norm (f x))"
   6.447 +    using f
   6.448 +    by (intro simple_bochner_integral_partition[symmetric])
   6.449 +       (auto intro: f simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   6.450 +  finally show ?thesis .
   6.451 +qed
   6.452 +
   6.453 +lemma simple_bochner_integral_eq_positive_integral:
   6.454 +  assumes f: "simple_bochner_integrable M f" "\<And>x. 0 \<le> f x"
   6.455 +  shows "simple_bochner_integral M f = (\<integral>\<^sup>+x. f x \<partial>M)"
   6.456 +proof -
   6.457 +  { fix x y z have "(x \<noteq> 0 \<Longrightarrow> y = z) \<Longrightarrow> ereal x * y = ereal x * z"
   6.458 +      by (cases "x = 0") (auto simp: zero_ereal_def[symmetric]) }
   6.459 +  note ereal_cong_mult = this
   6.460 +
   6.461 +  have [measurable]: "f \<in> borel_measurable M"
   6.462 +    using f(1) by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   6.463 +
   6.464 +  { fix y assume y: "y \<in> space M" "f y \<noteq> 0"
   6.465 +    have "ereal (measure M {x \<in> space M. f x = f y}) = emeasure M {x \<in> space M. f x = f y}"
   6.466 +    proof (rule emeasure_eq_ereal_measure[symmetric])
   6.467 +      have "emeasure M {x \<in> space M. f x = f y} \<le> emeasure M {x \<in> space M. f x \<noteq> 0}"
   6.468 +        using y by (intro emeasure_mono) auto
   6.469 +      with f show "emeasure M {x \<in> space M. f x = f y} \<noteq> \<infinity>"
   6.470 +        by (auto simp: simple_bochner_integrable.simps)
   6.471 +    qed
   6.472 +    moreover have "{x \<in> space M. f x = f y} = (\<lambda>x. ereal (f x)) -` {ereal (f y)} \<inter> space M"
   6.473 +      by auto
   6.474 +    ultimately have "ereal (measure M {x \<in> space M. f x = f y}) =
   6.475 +          emeasure M ((\<lambda>x. ereal (f x)) -` {ereal (f y)} \<inter> space M)" by simp }
   6.476 +  with f have "simple_bochner_integral M f = (\<integral>\<^sup>Sx. f x \<partial>M)"
   6.477 +    unfolding simple_integral_def
   6.478 +    by (subst simple_bochner_integral_partition[OF f(1), where g="\<lambda>x. ereal (f x)" and v=real])
   6.479 +       (auto intro: f simple_function_compose1 elim: simple_bochner_integrable.cases
   6.480 +             intro!: setsum_cong ereal_cong_mult
   6.481 +             simp: setsum_ereal[symmetric] times_ereal.simps(1)[symmetric] mult_ac
   6.482 +             simp del: setsum_ereal times_ereal.simps(1))
   6.483 +  also have "\<dots> = (\<integral>\<^sup>+x. f x \<partial>M)"
   6.484 +    using f
   6.485 +    by (intro positive_integral_eq_simple_integral[symmetric])
   6.486 +       (auto simp: simple_function_compose1 simple_bochner_integrable.simps)
   6.487 +  finally show ?thesis .
   6.488 +qed
   6.489 +
   6.490 +lemma simple_bochner_integral_bounded:
   6.491 +  fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector, second_countable_topology}"
   6.492 +  assumes f[measurable]: "f \<in> borel_measurable M"
   6.493 +  assumes s: "simple_bochner_integrable M s" and t: "simple_bochner_integrable M t"
   6.494 +  shows "ereal (norm (simple_bochner_integral M s - simple_bochner_integral M t)) \<le>
   6.495 +    (\<integral>\<^sup>+ x. norm (f x - s x) \<partial>M) + (\<integral>\<^sup>+ x. norm (f x - t x) \<partial>M)"
   6.496 +    (is "ereal (norm (?s - ?t)) \<le> ?S + ?T")
   6.497 +proof -
   6.498 +  have [measurable]: "s \<in> borel_measurable M" "t \<in> borel_measurable M"
   6.499 +    using s t by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   6.500 +
   6.501 +  have "ereal (norm (?s - ?t)) = norm (simple_bochner_integral M (\<lambda>x. s x - t x))"
   6.502 +    using s t by (subst simple_bochner_integral_diff) auto
   6.503 +  also have "\<dots> \<le> simple_bochner_integral M (\<lambda>x. norm (s x - t x))"
   6.504 +    using simple_bochner_integrable_compose2[of "op -" M "s" "t"] s t
   6.505 +    by (auto intro!: simple_bochner_integral_norm_bound)
   6.506 +  also have "\<dots> = (\<integral>\<^sup>+x. norm (s x - t x) \<partial>M)"
   6.507 +    using simple_bochner_integrable_compose2[of "\<lambda>x y. norm (x - y)" M "s" "t"] s t
   6.508 +    by (auto intro!: simple_bochner_integral_eq_positive_integral)
   6.509 +  also have "\<dots> \<le> (\<integral>\<^sup>+x. ereal (norm (f x - s x)) + ereal (norm (f x - t x)) \<partial>M)"
   6.510 +    by (auto intro!: positive_integral_mono)
   6.511 +       (metis (erased, hide_lams) add_diff_cancel_left add_diff_eq diff_add_eq order_trans
   6.512 +              norm_minus_commute norm_triangle_ineq4 order_refl)
   6.513 +  also have "\<dots> = ?S + ?T"
   6.514 +   by (rule positive_integral_add) auto
   6.515 +  finally show ?thesis .
   6.516 +qed
   6.517 +
   6.518 +inductive has_bochner_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b::{real_normed_vector, second_countable_topology} \<Rightarrow> bool"
   6.519 +  for M f x where
   6.520 +  "f \<in> borel_measurable M \<Longrightarrow>
   6.521 +    (\<And>i. simple_bochner_integrable M (s i)) \<Longrightarrow>
   6.522 +    (\<lambda>i. \<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) ----> 0 \<Longrightarrow>
   6.523 +    (\<lambda>i. simple_bochner_integral M (s i)) ----> x \<Longrightarrow>
   6.524 +    has_bochner_integral M f x"
   6.525 +
   6.526 +lemma has_bochner_integral_cong:
   6.527 +  assumes "M = N" "\<And>x. x \<in> space N \<Longrightarrow> f x = g x" "x = y"
   6.528 +  shows "has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral N g y"
   6.529 +  unfolding has_bochner_integral.simps assms(1,3)
   6.530 +  using assms(2) by (simp cong: measurable_cong_strong positive_integral_cong_strong)
   6.531 +
   6.532 +lemma has_bochner_integral_cong_AE:
   6.533 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow>
   6.534 +    has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral M g x"
   6.535 +  unfolding has_bochner_integral.simps
   6.536 +  by (intro arg_cong[where f=Ex] ext conj_cong rev_conj_cong refl arg_cong[where f="\<lambda>x. x ----> 0"]
   6.537 +            positive_integral_cong_AE)
   6.538 +     auto
   6.539 +
   6.540 +lemma borel_measurable_has_bochner_integral[measurable_dest]:
   6.541 +  "has_bochner_integral M f x \<Longrightarrow> f \<in> borel_measurable M"
   6.542 +  by (auto elim: has_bochner_integral.cases)
   6.543 +
   6.544 +lemma has_bochner_integral_simple_bochner_integrable:
   6.545 +  "simple_bochner_integrable M f \<Longrightarrow> has_bochner_integral M f (simple_bochner_integral M f)"
   6.546 +  by (rule has_bochner_integral.intros[where s="\<lambda>_. f"])
   6.547 +     (auto intro: borel_measurable_simple_function 
   6.548 +           elim: simple_bochner_integrable.cases
   6.549 +           simp: zero_ereal_def[symmetric])
   6.550 +
   6.551 +lemma has_bochner_integral_real_indicator:
   6.552 +  assumes [measurable]: "A \<in> sets M" and A: "emeasure M A < \<infinity>"
   6.553 +  shows "has_bochner_integral M (indicator A) (measure M A)"
   6.554 +proof -
   6.555 +  have sbi: "simple_bochner_integrable M (indicator A::'a \<Rightarrow> real)"
   6.556 +  proof
   6.557 +    have "{y \<in> space M. (indicator A y::real) \<noteq> 0} = A"
   6.558 +      using sets.sets_into_space[OF `A\<in>sets M`] by (auto split: split_indicator)
   6.559 +    then show "emeasure M {y \<in> space M. (indicator A y::real) \<noteq> 0} \<noteq> \<infinity>"
   6.560 +      using A by auto
   6.561 +  qed (rule simple_function_indicator assms)+
   6.562 +  moreover have "simple_bochner_integral M (indicator A) = measure M A"
   6.563 +    using simple_bochner_integral_eq_positive_integral[OF sbi] A
   6.564 +    by (simp add: ereal_indicator emeasure_eq_ereal_measure)
   6.565 +  ultimately show ?thesis
   6.566 +    by (metis has_bochner_integral_simple_bochner_integrable)
   6.567 +qed
   6.568 +
   6.569 +lemma has_bochner_integral_add:
   6.570 +  "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M g y \<Longrightarrow>
   6.571 +    has_bochner_integral M (\<lambda>x. f x + g x) (x + y)"
   6.572 +proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   6.573 +  fix sf sg
   6.574 +  assume f_sf: "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - sf i x) \<partial>M) ----> 0"
   6.575 +  assume g_sg: "(\<lambda>i. \<integral>\<^sup>+ x. norm (g x - sg i x) \<partial>M) ----> 0"
   6.576 +
   6.577 +  assume sf: "\<forall>i. simple_bochner_integrable M (sf i)"
   6.578 +    and sg: "\<forall>i. simple_bochner_integrable M (sg i)"
   6.579 +  then have [measurable]: "\<And>i. sf i \<in> borel_measurable M" "\<And>i. sg i \<in> borel_measurable M"
   6.580 +    by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   6.581 +  assume [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   6.582 +
   6.583 +  show "\<And>i. simple_bochner_integrable M (\<lambda>x. sf i x + sg i x)"
   6.584 +    using sf sg by (simp add: simple_bochner_integrable_compose2)
   6.585 +
   6.586 +  show "(\<lambda>i. \<integral>\<^sup>+ x. (norm (f x + g x - (sf i x + sg i x))) \<partial>M) ----> 0"
   6.587 +    (is "?f ----> 0")
   6.588 +  proof (rule tendsto_sandwich)
   6.589 +    show "eventually (\<lambda>n. 0 \<le> ?f n) sequentially" "(\<lambda>_. 0) ----> 0"
   6.590 +      by (auto simp: positive_integral_positive)
   6.591 +    show "eventually (\<lambda>i. ?f i \<le> (\<integral>\<^sup>+ x. (norm (f x - sf i x)) \<partial>M) + \<integral>\<^sup>+ x. (norm (g x - sg i x)) \<partial>M) sequentially"
   6.592 +      (is "eventually (\<lambda>i. ?f i \<le> ?g i) sequentially")
   6.593 +    proof (intro always_eventually allI)
   6.594 +      fix i have "?f i \<le> (\<integral>\<^sup>+ x. (norm (f x - sf i x)) + ereal (norm (g x - sg i x)) \<partial>M)"
   6.595 +        by (auto intro!: positive_integral_mono norm_diff_triangle_ineq)
   6.596 +      also have "\<dots> = ?g i"
   6.597 +        by (intro positive_integral_add) auto
   6.598 +      finally show "?f i \<le> ?g i" .
   6.599 +    qed
   6.600 +    show "?g ----> 0"
   6.601 +      using tendsto_add_ereal[OF _ _ f_sf g_sg] by simp
   6.602 +  qed
   6.603 +qed (auto simp: simple_bochner_integral_add tendsto_add)
   6.604 +
   6.605 +lemma has_bochner_integral_bounded_linear:
   6.606 +  assumes "bounded_linear T"
   6.607 +  shows "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M (\<lambda>x. T (f x)) (T x)"
   6.608 +proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   6.609 +  interpret T: bounded_linear T by fact
   6.610 +  have [measurable]: "T \<in> borel_measurable borel"
   6.611 +    by (intro borel_measurable_continuous_on1 T.continuous_on continuous_on_id)
   6.612 +  assume [measurable]: "f \<in> borel_measurable M"
   6.613 +  then show "(\<lambda>x. T (f x)) \<in> borel_measurable M"
   6.614 +    by auto
   6.615 +
   6.616 +  fix s assume f_s: "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M) ----> 0"
   6.617 +  assume s: "\<forall>i. simple_bochner_integrable M (s i)"
   6.618 +  then show "\<And>i. simple_bochner_integrable M (\<lambda>x. T (s i x))"
   6.619 +    by (auto intro: simple_bochner_integrable_compose2 T.zero)
   6.620 +
   6.621 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   6.622 +    using s by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   6.623 +
   6.624 +  obtain K where K: "K > 0" "\<And>x i. norm (T (f x) - T (s i x)) \<le> norm (f x - s i x) * K"
   6.625 +    using T.pos_bounded by (auto simp: T.diff[symmetric])
   6.626 +
   6.627 +  show "(\<lambda>i. \<integral>\<^sup>+ x. norm (T (f x) - T (s i x)) \<partial>M) ----> 0"
   6.628 +    (is "?f ----> 0")
   6.629 +  proof (rule tendsto_sandwich)
   6.630 +    show "eventually (\<lambda>n. 0 \<le> ?f n) sequentially" "(\<lambda>_. 0) ----> 0"
   6.631 +      by (auto simp: positive_integral_positive)
   6.632 +
   6.633 +    show "eventually (\<lambda>i. ?f i \<le> K * (\<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M)) sequentially"
   6.634 +      (is "eventually (\<lambda>i. ?f i \<le> ?g i) sequentially")
   6.635 +    proof (intro always_eventually allI)
   6.636 +      fix i have "?f i \<le> (\<integral>\<^sup>+ x. ereal K * norm (f x - s i x) \<partial>M)"
   6.637 +        using K by (intro positive_integral_mono) (auto simp: mult_ac)
   6.638 +      also have "\<dots> = ?g i"
   6.639 +        using K by (intro positive_integral_cmult) auto
   6.640 +      finally show "?f i \<le> ?g i" .
   6.641 +    qed
   6.642 +    show "?g ----> 0"
   6.643 +      using ereal_lim_mult[OF f_s, of "ereal K"] by simp
   6.644 +  qed
   6.645 +
   6.646 +  assume "(\<lambda>i. simple_bochner_integral M (s i)) ----> x"
   6.647 +  with s show "(\<lambda>i. simple_bochner_integral M (\<lambda>x. T (s i x))) ----> T x"
   6.648 +    by (auto intro!: T.tendsto simp: T.simple_bochner_integral_linear)
   6.649 +qed
   6.650 +
   6.651 +lemma has_bochner_integral_zero[intro]: "has_bochner_integral M (\<lambda>x. 0) 0"
   6.652 +  by (auto intro!: has_bochner_integral.intros[where s="\<lambda>_ _. 0"]
   6.653 +           simp: zero_ereal_def[symmetric] simple_bochner_integrable.simps
   6.654 +                 simple_bochner_integral_def image_constant_conv)
   6.655 +
   6.656 +lemma has_bochner_integral_scaleR_left[intro]:
   6.657 +  "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x *\<^sub>R c) (x *\<^sub>R c)"
   6.658 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_scaleR_left])
   6.659 +
   6.660 +lemma has_bochner_integral_scaleR_right[intro]:
   6.661 +  "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c *\<^sub>R f x) (c *\<^sub>R x)"
   6.662 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_scaleR_right])
   6.663 +
   6.664 +lemma has_bochner_integral_mult_left[intro]:
   6.665 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   6.666 +  shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x * c) (x * c)"
   6.667 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_mult_left])
   6.668 +
   6.669 +lemma has_bochner_integral_mult_right[intro]:
   6.670 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   6.671 +  shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c * f x) (c * x)"
   6.672 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_mult_right])
   6.673 +
   6.674 +lemmas has_bochner_integral_divide = 
   6.675 +  has_bochner_integral_bounded_linear[OF bounded_linear_divide]
   6.676 +
   6.677 +lemma has_bochner_integral_divide_zero[intro]:
   6.678 +  fixes c :: "_::{real_normed_field, field_inverse_zero, second_countable_topology}"
   6.679 +  shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x / c) (x / c)"
   6.680 +  using has_bochner_integral_divide by (cases "c = 0") auto
   6.681 +
   6.682 +lemma has_bochner_integral_inner_left[intro]:
   6.683 +  "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x \<bullet> c) (x \<bullet> c)"
   6.684 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_inner_left])
   6.685 +
   6.686 +lemma has_bochner_integral_inner_right[intro]:
   6.687 +  "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c \<bullet> f x) (c \<bullet> x)"
   6.688 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_inner_right])
   6.689 +
   6.690 +lemmas has_bochner_integral_minus =
   6.691 +  has_bochner_integral_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
   6.692 +lemmas has_bochner_integral_Re =
   6.693 +  has_bochner_integral_bounded_linear[OF bounded_linear_Re]
   6.694 +lemmas has_bochner_integral_Im =
   6.695 +  has_bochner_integral_bounded_linear[OF bounded_linear_Im]
   6.696 +lemmas has_bochner_integral_cnj =
   6.697 +  has_bochner_integral_bounded_linear[OF bounded_linear_cnj]
   6.698 +lemmas has_bochner_integral_of_real =
   6.699 +  has_bochner_integral_bounded_linear[OF bounded_linear_of_real]
   6.700 +lemmas has_bochner_integral_fst =
   6.701 +  has_bochner_integral_bounded_linear[OF bounded_linear_fst]
   6.702 +lemmas has_bochner_integral_snd =
   6.703 +  has_bochner_integral_bounded_linear[OF bounded_linear_snd]
   6.704 +
   6.705 +lemma has_bochner_integral_indicator:
   6.706 +  "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   6.707 +    has_bochner_integral M (\<lambda>x. indicator A x *\<^sub>R c) (measure M A *\<^sub>R c)"
   6.708 +  by (intro has_bochner_integral_scaleR_left has_bochner_integral_real_indicator)
   6.709 +
   6.710 +lemma has_bochner_integral_diff:
   6.711 +  "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M g y \<Longrightarrow>
   6.712 +    has_bochner_integral M (\<lambda>x. f x - g x) (x - y)"
   6.713 +  unfolding diff_conv_add_uminus
   6.714 +  by (intro has_bochner_integral_add has_bochner_integral_minus)
   6.715 +
   6.716 +lemma has_bochner_integral_setsum:
   6.717 +  "(\<And>i. i \<in> I \<Longrightarrow> has_bochner_integral M (f i) (x i)) \<Longrightarrow>
   6.718 +    has_bochner_integral M (\<lambda>x. \<Sum>i\<in>I. f i x) (\<Sum>i\<in>I. x i)"
   6.719 +  by (induct I rule: infinite_finite_induct)
   6.720 +     (auto intro: has_bochner_integral_zero has_bochner_integral_add)
   6.721 +
   6.722 +lemma has_bochner_integral_implies_finite_norm:
   6.723 +  "has_bochner_integral M f x \<Longrightarrow> (\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
   6.724 +proof (elim has_bochner_integral.cases)
   6.725 +  fix s v
   6.726 +  assume [measurable]: "f \<in> borel_measurable M" and s: "\<And>i. simple_bochner_integrable M (s i)" and
   6.727 +    lim_0: "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0"
   6.728 +  from order_tendstoD[OF lim_0, of "\<infinity>"]
   6.729 +  obtain i where f_s_fin: "(\<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) < \<infinity>"
   6.730 +    by (metis (mono_tags, lifting) eventually_False_sequentially eventually_elim1
   6.731 +              less_ereal.simps(4) zero_ereal_def)
   6.732 +
   6.733 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   6.734 +    using s by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   6.735 +
   6.736 +  def m \<equiv> "if space M = {} then 0 else Max ((\<lambda>x. norm (s i x))`space M)"
   6.737 +  have "finite (s i ` space M)"
   6.738 +    using s by (auto simp: simple_function_def simple_bochner_integrable.simps)
   6.739 +  then have "finite (norm ` s i ` space M)"
   6.740 +    by (rule finite_imageI)
   6.741 +  then have "\<And>x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> m" "0 \<le> m"
   6.742 +    by (auto simp: m_def image_comp comp_def Max_ge_iff)
   6.743 +  then have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) \<le> (\<integral>\<^sup>+x. ereal m * indicator {x\<in>space M. s i x \<noteq> 0} x \<partial>M)"
   6.744 +    by (auto split: split_indicator intro!: Max_ge positive_integral_mono simp:)
   6.745 +  also have "\<dots> < \<infinity>"
   6.746 +    using s by (subst positive_integral_cmult_indicator) (auto simp: `0 \<le> m` simple_bochner_integrable.simps)
   6.747 +  finally have s_fin: "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) < \<infinity>" .
   6.748 +
   6.749 +  have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x - s i x)) + ereal (norm (s i x)) \<partial>M)"
   6.750 +    by (auto intro!: positive_integral_mono) (metis add_commute norm_triangle_sub)
   6.751 +  also have "\<dots> = (\<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) + (\<integral>\<^sup>+x. norm (s i x) \<partial>M)"
   6.752 +    by (rule positive_integral_add) auto
   6.753 +  also have "\<dots> < \<infinity>"
   6.754 +    using s_fin f_s_fin by auto
   6.755 +  finally show "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>" .
   6.756 +qed
   6.757 +
   6.758 +lemma has_bochner_integral_norm_bound:
   6.759 +  assumes i: "has_bochner_integral M f x"
   6.760 +  shows "norm x \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   6.761 +using assms proof
   6.762 +  fix s assume
   6.763 +    x: "(\<lambda>i. simple_bochner_integral M (s i)) ----> x" (is "?s ----> x") and
   6.764 +    s[simp]: "\<And>i. simple_bochner_integrable M (s i)" and
   6.765 +    lim: "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0" and
   6.766 +    f[measurable]: "f \<in> borel_measurable M"
   6.767 +
   6.768 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   6.769 +    using s by (auto simp: simple_bochner_integrable.simps intro: borel_measurable_simple_function)
   6.770 +
   6.771 +  show "norm x \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   6.772 +  proof (rule LIMSEQ_le)
   6.773 +    show "(\<lambda>i. ereal (norm (?s i))) ----> norm x"
   6.774 +      using x by (intro tendsto_intros lim_ereal[THEN iffD2])
   6.775 +    show "\<exists>N. \<forall>n\<ge>N. norm (?s n) \<le> (\<integral>\<^sup>+x. norm (f x - s n x) \<partial>M) + (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   6.776 +      (is "\<exists>N. \<forall>n\<ge>N. _ \<le> ?t n")
   6.777 +    proof (intro exI allI impI)
   6.778 +      fix n
   6.779 +      have "ereal (norm (?s n)) \<le> simple_bochner_integral M (\<lambda>x. norm (s n x))"
   6.780 +        by (auto intro!: simple_bochner_integral_norm_bound)
   6.781 +      also have "\<dots> = (\<integral>\<^sup>+x. norm (s n x) \<partial>M)"
   6.782 +        by (intro simple_bochner_integral_eq_positive_integral)
   6.783 +           (auto intro: s simple_bochner_integrable_compose2)
   6.784 +      also have "\<dots> \<le> (\<integral>\<^sup>+x. ereal (norm (f x - s n x)) + norm (f x) \<partial>M)"
   6.785 +        by (auto intro!: positive_integral_mono)
   6.786 +           (metis add_commute norm_minus_commute norm_triangle_sub)
   6.787 +      also have "\<dots> = ?t n" 
   6.788 +        by (rule positive_integral_add) auto
   6.789 +      finally show "norm (?s n) \<le> ?t n" .
   6.790 +    qed
   6.791 +    have "?t ----> 0 + (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
   6.792 +      using has_bochner_integral_implies_finite_norm[OF i]
   6.793 +      by (intro tendsto_add_ereal tendsto_const lim) auto
   6.794 +    then show "?t ----> \<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M"
   6.795 +      by simp
   6.796 +  qed
   6.797 +qed
   6.798 +
   6.799 +lemma has_bochner_integral_eq:
   6.800 +  "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M f y \<Longrightarrow> x = y"
   6.801 +proof (elim has_bochner_integral.cases)
   6.802 +  assume f[measurable]: "f \<in> borel_measurable M"
   6.803 +
   6.804 +  fix s t
   6.805 +  assume "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M) ----> 0" (is "?S ----> 0")
   6.806 +  assume "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - t i x) \<partial>M) ----> 0" (is "?T ----> 0")
   6.807 +  assume s: "\<And>i. simple_bochner_integrable M (s i)"
   6.808 +  assume t: "\<And>i. simple_bochner_integrable M (t i)"
   6.809 +
   6.810 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M" "\<And>i. t i \<in> borel_measurable M"
   6.811 +    using s t by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   6.812 +
   6.813 +  let ?s = "\<lambda>i. simple_bochner_integral M (s i)"
   6.814 +  let ?t = "\<lambda>i. simple_bochner_integral M (t i)"
   6.815 +  assume "?s ----> x" "?t ----> y"
   6.816 +  then have "(\<lambda>i. norm (?s i - ?t i)) ----> norm (x - y)"
   6.817 +    by (intro tendsto_intros)
   6.818 +  moreover
   6.819 +  have "(\<lambda>i. ereal (norm (?s i - ?t i))) ----> ereal 0"
   6.820 +  proof (rule tendsto_sandwich)
   6.821 +    show "eventually (\<lambda>i. 0 \<le> ereal (norm (?s i - ?t i))) sequentially" "(\<lambda>_. 0) ----> ereal 0"
   6.822 +      by (auto simp: positive_integral_positive zero_ereal_def[symmetric])
   6.823 +
   6.824 +    show "eventually (\<lambda>i. norm (?s i - ?t i) \<le> ?S i + ?T i) sequentially"
   6.825 +      by (intro always_eventually allI simple_bochner_integral_bounded s t f)
   6.826 +    show "(\<lambda>i. ?S i + ?T i) ----> ereal 0"
   6.827 +      using tendsto_add_ereal[OF _ _ `?S ----> 0` `?T ----> 0`]
   6.828 +      by (simp add: zero_ereal_def[symmetric])
   6.829 +  qed
   6.830 +  then have "(\<lambda>i. norm (?s i - ?t i)) ----> 0"
   6.831 +    by simp
   6.832 +  ultimately have "norm (x - y) = 0"
   6.833 +    by (rule LIMSEQ_unique)
   6.834 +  then show "x = y" by simp
   6.835 +qed
   6.836 +
   6.837 +lemma has_bochner_integralI_AE:
   6.838 +  assumes f: "has_bochner_integral M f x"
   6.839 +    and g: "g \<in> borel_measurable M"
   6.840 +    and ae: "AE x in M. f x = g x"
   6.841 +  shows "has_bochner_integral M g x"
   6.842 +  using f
   6.843 +proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   6.844 +  fix s assume "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0"
   6.845 +  also have "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) = (\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (g x - s i x)) \<partial>M)"
   6.846 +    using ae
   6.847 +    by (intro ext positive_integral_cong_AE, eventually_elim) simp
   6.848 +  finally show "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (g x - s i x)) \<partial>M) ----> 0" .
   6.849 +qed (auto intro: g)
   6.850 +
   6.851 +lemma has_bochner_integral_eq_AE:
   6.852 +  assumes f: "has_bochner_integral M f x"
   6.853 +    and g: "has_bochner_integral M g y"
   6.854 +    and ae: "AE x in M. f x = g x"
   6.855 +  shows "x = y"
   6.856 +proof -
   6.857 +  from assms have "has_bochner_integral M g x"
   6.858 +    by (auto intro: has_bochner_integralI_AE)
   6.859 +  from this g show "x = y"
   6.860 +    by (rule has_bochner_integral_eq)
   6.861 +qed
   6.862 +
   6.863 +inductive integrable for M f where
   6.864 +  "has_bochner_integral M f x \<Longrightarrow> integrable M f"
   6.865 +
   6.866 +definition lebesgue_integral ("integral\<^sup>L") where
   6.867 +  "integral\<^sup>L M f = (THE x. has_bochner_integral M f x)"
   6.868 +
   6.869 +syntax
   6.870 +  "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> 'a measure \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
   6.871 +
   6.872 +translations
   6.873 +  "\<integral> x. f \<partial>M" == "CONST lebesgue_integral M (\<lambda>x. f)"
   6.874 +
   6.875 +lemma has_bochner_integral_integral_eq: "has_bochner_integral M f x \<Longrightarrow> integral\<^sup>L M f = x"
   6.876 +  by (metis the_equality has_bochner_integral_eq lebesgue_integral_def)
   6.877 +
   6.878 +lemma has_bochner_integral_integrable:
   6.879 +  "integrable M f \<Longrightarrow> has_bochner_integral M f (integral\<^sup>L M f)"
   6.880 +  by (auto simp: has_bochner_integral_integral_eq integrable.simps)
   6.881 +
   6.882 +lemma has_bochner_integral_iff:
   6.883 +  "has_bochner_integral M f x \<longleftrightarrow> integrable M f \<and> integral\<^sup>L M f = x"
   6.884 +  by (metis has_bochner_integral_integrable has_bochner_integral_integral_eq integrable.intros)
   6.885 +
   6.886 +lemma simple_bochner_integrable_eq_integral:
   6.887 +  "simple_bochner_integrable M f \<Longrightarrow> simple_bochner_integral M f = integral\<^sup>L M f"
   6.888 +  using has_bochner_integral_simple_bochner_integrable[of M f]
   6.889 +  by (simp add: has_bochner_integral_integral_eq)
   6.890 +
   6.891 +lemma not_integrable_integral_eq: "\<not> integrable M f \<Longrightarrow> integral\<^sup>L M f = (THE x. False)"
   6.892 +  unfolding integrable.simps lebesgue_integral_def by (auto intro!: arg_cong[where f=The])
   6.893 +
   6.894 +lemma integral_eq_cases:
   6.895 +  "integrable M f \<longleftrightarrow> integrable N g \<Longrightarrow>
   6.896 +    (integrable M f \<Longrightarrow> integrable N g \<Longrightarrow> integral\<^sup>L M f = integral\<^sup>L N g) \<Longrightarrow>
   6.897 +    integral\<^sup>L M f = integral\<^sup>L N g"
   6.898 +  by (metis not_integrable_integral_eq)
   6.899 +
   6.900 +lemma borel_measurable_integrable[measurable_dest]: "integrable M f \<Longrightarrow> f \<in> borel_measurable M"
   6.901 +  by (auto elim: integrable.cases has_bochner_integral.cases)
   6.902 +
   6.903 +lemma integrable_cong:
   6.904 +  "M = N \<Longrightarrow> (\<And>x. x \<in> space N \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable N g"
   6.905 +  using assms by (simp cong: has_bochner_integral_cong add: integrable.simps)
   6.906 +
   6.907 +lemma integrable_cong_AE:
   6.908 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. f x = g x \<Longrightarrow>
   6.909 +    integrable M f \<longleftrightarrow> integrable M g"
   6.910 +  unfolding integrable.simps
   6.911 +  by (intro has_bochner_integral_cong_AE arg_cong[where f=Ex] ext)
   6.912 +
   6.913 +lemma integral_cong:
   6.914 +  "M = N \<Longrightarrow> (\<And>x. x \<in> space N \<Longrightarrow> f x = g x) \<Longrightarrow> integral\<^sup>L M f = integral\<^sup>L N g"
   6.915 +  using assms by (simp cong: has_bochner_integral_cong add: lebesgue_integral_def)
   6.916 +
   6.917 +lemma integral_cong_AE:
   6.918 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. f x = g x \<Longrightarrow>
   6.919 +    integral\<^sup>L M f = integral\<^sup>L M g"
   6.920 +  unfolding lebesgue_integral_def
   6.921 +  by (intro has_bochner_integral_cong_AE arg_cong[where f=The] ext)
   6.922 +
   6.923 +lemma integrable_add[simp, intro]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> integrable M (\<lambda>x. f x + g x)"
   6.924 +  by (auto simp: integrable.simps intro: has_bochner_integral_add)
   6.925 +
   6.926 +lemma integrable_zero[simp, intro]: "integrable M (\<lambda>x. 0)"
   6.927 +  by (metis has_bochner_integral_zero integrable.simps) 
   6.928 +
   6.929 +lemma integrable_setsum[simp, intro]: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow> integrable M (\<lambda>x. \<Sum>i\<in>I. f i x)"
   6.930 +  by (metis has_bochner_integral_setsum integrable.simps) 
   6.931 +
   6.932 +lemma integrable_indicator[simp, intro]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   6.933 +  integrable M (\<lambda>x. indicator A x *\<^sub>R c)"
   6.934 +  by (metis has_bochner_integral_indicator integrable.simps) 
   6.935 +
   6.936 +lemma integrable_real_indicator[simp, intro]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   6.937 +  integrable M (indicator A :: 'a \<Rightarrow> real)"
   6.938 +  by (metis has_bochner_integral_real_indicator integrable.simps)
   6.939 +
   6.940 +lemma integrable_diff[simp, intro]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> integrable M (\<lambda>x. f x - g x)"
   6.941 +  by (auto simp: integrable.simps intro: has_bochner_integral_diff)
   6.942 +  
   6.943 +lemma integrable_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. T (f x))"
   6.944 +  by (auto simp: integrable.simps intro: has_bochner_integral_bounded_linear)
   6.945 +
   6.946 +lemma integrable_scaleR_left[simp, intro]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x *\<^sub>R c)"
   6.947 +  unfolding integrable.simps by fastforce
   6.948 +
   6.949 +lemma integrable_scaleR_right[simp, intro]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c *\<^sub>R f x)"
   6.950 +  unfolding integrable.simps by fastforce
   6.951 +
   6.952 +lemma integrable_mult_left[simp, intro]:
   6.953 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   6.954 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x * c)"
   6.955 +  unfolding integrable.simps by fastforce
   6.956 +
   6.957 +lemma integrable_mult_right[simp, intro]:
   6.958 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   6.959 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c * f x)"
   6.960 +  unfolding integrable.simps by fastforce
   6.961 +
   6.962 +lemma integrable_divide_zero[simp, intro]:
   6.963 +  fixes c :: "_::{real_normed_field, field_inverse_zero, second_countable_topology}"
   6.964 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x / c)"
   6.965 +  unfolding integrable.simps by fastforce
   6.966 +
   6.967 +lemma integrable_inner_left[simp, intro]:
   6.968 +  "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x \<bullet> c)"
   6.969 +  unfolding integrable.simps by fastforce
   6.970 +
   6.971 +lemma integrable_inner_right[simp, intro]:
   6.972 +  "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c \<bullet> f x)"
   6.973 +  unfolding integrable.simps by fastforce
   6.974 +
   6.975 +lemmas integrable_minus[simp, intro] =
   6.976 +  integrable_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
   6.977 +lemmas integrable_divide[simp, intro] =
   6.978 +  integrable_bounded_linear[OF bounded_linear_divide]
   6.979 +lemmas integrable_Re[simp, intro] =
   6.980 +  integrable_bounded_linear[OF bounded_linear_Re]
   6.981 +lemmas integrable_Im[simp, intro] =
   6.982 +  integrable_bounded_linear[OF bounded_linear_Im]
   6.983 +lemmas integrable_cnj[simp, intro] =
   6.984 +  integrable_bounded_linear[OF bounded_linear_cnj]
   6.985 +lemmas integrable_of_real[simp, intro] =
   6.986 +  integrable_bounded_linear[OF bounded_linear_of_real]
   6.987 +lemmas integrable_fst[simp, intro] =
   6.988 +  integrable_bounded_linear[OF bounded_linear_fst]
   6.989 +lemmas integrable_snd[simp, intro] =
   6.990 +  integrable_bounded_linear[OF bounded_linear_snd]
   6.991 +
   6.992 +lemma integral_zero[simp]: "integral\<^sup>L M (\<lambda>x. 0) = 0"
   6.993 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_zero)
   6.994 +
   6.995 +lemma integral_add[simp]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow>
   6.996 +    integral\<^sup>L M (\<lambda>x. f x + g x) = integral\<^sup>L M f + integral\<^sup>L M g"
   6.997 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_add has_bochner_integral_integrable)
   6.998 +
   6.999 +lemma integral_diff[simp]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow>
  6.1000 +    integral\<^sup>L M (\<lambda>x. f x - g x) = integral\<^sup>L M f - integral\<^sup>L M g"
  6.1001 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_diff has_bochner_integral_integrable)
  6.1002 +
  6.1003 +lemma integral_setsum[simp]: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow>
  6.1004 +  integral\<^sup>L M (\<lambda>x. \<Sum>i\<in>I. f i x) = (\<Sum>i\<in>I. integral\<^sup>L M (f i))"
  6.1005 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_setsum has_bochner_integral_integrable)
  6.1006 +
  6.1007 +lemma integral_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow>
  6.1008 +    integral\<^sup>L M (\<lambda>x. T (f x)) = T (integral\<^sup>L M f)"
  6.1009 +  by (metis has_bochner_integral_bounded_linear has_bochner_integral_integrable has_bochner_integral_integral_eq)
  6.1010 +
  6.1011 +lemma integral_indicator[simp]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
  6.1012 +  integral\<^sup>L M (\<lambda>x. indicator A x *\<^sub>R c) = measure M A *\<^sub>R c"
  6.1013 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_indicator has_bochner_integral_integrable)
  6.1014 +
  6.1015 +lemma integral_real_indicator[simp]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
  6.1016 +  integral\<^sup>L M (indicator A :: 'a \<Rightarrow> real) = measure M A"
  6.1017 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_real_indicator has_bochner_integral_integrable)
  6.1018 +
  6.1019 +lemma integral_scaleR_left[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x *\<^sub>R c \<partial>M) = integral\<^sup>L M f *\<^sub>R c"
  6.1020 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_scaleR_left)
  6.1021 +
  6.1022 +lemma integral_scaleR_right[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c *\<^sub>R f x \<partial>M) = c *\<^sub>R integral\<^sup>L M f"
  6.1023 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_scaleR_right)
  6.1024 +
  6.1025 +lemma integral_mult_left[simp]:
  6.1026 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  6.1027 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x * c \<partial>M) = integral\<^sup>L M f * c"
  6.1028 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_mult_left)
  6.1029 +
  6.1030 +lemma integral_mult_right[simp]:
  6.1031 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  6.1032 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c * f x \<partial>M) = c * integral\<^sup>L M f"
  6.1033 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_mult_right)
  6.1034 +
  6.1035 +lemma integral_inner_left[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x \<bullet> c \<partial>M) = integral\<^sup>L M f \<bullet> c"
  6.1036 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_inner_left)
  6.1037 +
  6.1038 +lemma integral_inner_right[simp]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c \<bullet> f x \<partial>M) = c \<bullet> integral\<^sup>L M f"
  6.1039 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_inner_right)
  6.1040 +
  6.1041 +lemma integral_divide_zero[simp]:
  6.1042 +  fixes c :: "_::{real_normed_field, field_inverse_zero, second_countable_topology}"
  6.1043 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integral\<^sup>L M (\<lambda>x. f x / c) = integral\<^sup>L M f / c"
  6.1044 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_divide_zero)
  6.1045 +
  6.1046 +lemmas integral_minus[simp] =
  6.1047 +  integral_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
  6.1048 +lemmas integral_divide[simp] =
  6.1049 +  integral_bounded_linear[OF bounded_linear_divide]
  6.1050 +lemmas integral_Re[simp] =
  6.1051 +  integral_bounded_linear[OF bounded_linear_Re]
  6.1052 +lemmas integral_Im[simp] =
  6.1053 +  integral_bounded_linear[OF bounded_linear_Im]
  6.1054 +lemmas integral_cnj[simp] =
  6.1055 +  integral_bounded_linear[OF bounded_linear_cnj]
  6.1056 +lemmas integral_of_real[simp] =
  6.1057 +  integral_bounded_linear[OF bounded_linear_of_real]
  6.1058 +lemmas integral_fst[simp] =
  6.1059 +  integral_bounded_linear[OF bounded_linear_fst]
  6.1060 +lemmas integral_snd[simp] =
  6.1061 +  integral_bounded_linear[OF bounded_linear_snd]
  6.1062 +
  6.1063 +lemma integral_norm_bound_ereal:
  6.1064 +  "integrable M f \<Longrightarrow> norm (integral\<^sup>L M f) \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
  6.1065 +  by (metis has_bochner_integral_integrable has_bochner_integral_norm_bound)
  6.1066 +
  6.1067 +lemma integrableI_sequence:
  6.1068 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1069 +  assumes f[measurable]: "f \<in> borel_measurable M"
  6.1070 +  assumes s: "\<And>i. simple_bochner_integrable M (s i)"
  6.1071 +  assumes lim: "(\<lambda>i. \<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) ----> 0" (is "?S ----> 0")
  6.1072 +  shows "integrable M f"
  6.1073 +proof -
  6.1074 +  let ?s = "\<lambda>n. simple_bochner_integral M (s n)"
  6.1075 +
  6.1076 +  have "\<exists>x. ?s ----> x"
  6.1077 +    unfolding convergent_eq_cauchy
  6.1078 +  proof (rule metric_CauchyI)
  6.1079 +    fix e :: real assume "0 < e"
  6.1080 +    then have "0 < ereal (e / 2)" by auto
  6.1081 +    from order_tendstoD(2)[OF lim this]
  6.1082 +    obtain M where M: "\<And>n. M \<le> n \<Longrightarrow> ?S n < e / 2"
  6.1083 +      by (auto simp: eventually_sequentially)
  6.1084 +    show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (?s m) (?s n) < e"
  6.1085 +    proof (intro exI allI impI)
  6.1086 +      fix m n assume m: "M \<le> m" and n: "M \<le> n"
  6.1087 +      have "?S n \<noteq> \<infinity>"
  6.1088 +        using M[OF n] by auto
  6.1089 +      have "norm (?s n - ?s m) \<le> ?S n + ?S m"
  6.1090 +        by (intro simple_bochner_integral_bounded s f)
  6.1091 +      also have "\<dots> < ereal (e / 2) + e / 2"
  6.1092 +        using ereal_add_strict_mono[OF less_imp_le[OF M[OF n]] _ `?S n \<noteq> \<infinity>` M[OF m]]
  6.1093 +        by (auto simp: positive_integral_positive)
  6.1094 +      also have "\<dots> = e" by simp
  6.1095 +      finally show "dist (?s n) (?s m) < e"
  6.1096 +        by (simp add: dist_norm)
  6.1097 +    qed
  6.1098 +  qed
  6.1099 +  then obtain x where "?s ----> x" ..
  6.1100 +  show ?thesis
  6.1101 +    by (rule, rule) fact+
  6.1102 +qed
  6.1103 +
  6.1104 +lemma positive_integral_dominated_convergence_norm:
  6.1105 +  fixes u' :: "_ \<Rightarrow> _::{real_normed_vector, second_countable_topology}"
  6.1106 +  assumes [measurable]:
  6.1107 +       "\<And>i. u i \<in> borel_measurable M" "u' \<in> borel_measurable M" "w \<in> borel_measurable M"
  6.1108 +    and bound: "\<And>j. AE x in M. norm (u j x) \<le> w x"
  6.1109 +    and w: "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
  6.1110 +    and u': "AE x in M. (\<lambda>i. u i x) ----> u' x"
  6.1111 +  shows "(\<lambda>i. (\<integral>\<^sup>+x. norm (u' x - u i x) \<partial>M)) ----> 0"
  6.1112 +proof -
  6.1113 +  have "AE x in M. \<forall>j. norm (u j x) \<le> w x"
  6.1114 +    unfolding AE_all_countable by rule fact
  6.1115 +  with u' have bnd: "AE x in M. \<forall>j. norm (u' x - u j x) \<le> 2 * w x"
  6.1116 +  proof (eventually_elim, intro allI)
  6.1117 +    fix i x assume "(\<lambda>i. u i x) ----> u' x" "\<forall>j. norm (u j x) \<le> w x" "\<forall>j. norm (u j x) \<le> w x"
  6.1118 +    then have "norm (u' x) \<le> w x" "norm (u i x) \<le> w x"
  6.1119 +      by (auto intro: LIMSEQ_le_const2 tendsto_norm)
  6.1120 +    then have "norm (u' x) + norm (u i x) \<le> 2 * w x"
  6.1121 +      by simp
  6.1122 +    also have "norm (u' x - u i x) \<le> norm (u' x) + norm (u i x)"
  6.1123 +      by (rule norm_triangle_ineq4)
  6.1124 +    finally (xtrans) show "norm (u' x - u i x) \<le> 2 * w x" .
  6.1125 +  qed
  6.1126 +  
  6.1127 +  have "(\<lambda>i. (\<integral>\<^sup>+x. norm (u' x - u i x) \<partial>M)) ----> (\<integral>\<^sup>+x. 0 \<partial>M)"
  6.1128 +  proof (rule positive_integral_dominated_convergence)  
  6.1129 +    show "(\<integral>\<^sup>+x. 2 * w x \<partial>M) < \<infinity>"
  6.1130 +      by (rule positive_integral_mult_bounded_inf[OF _ w, of 2]) auto
  6.1131 +    show "AE x in M. (\<lambda>i. ereal (norm (u' x - u i x))) ----> 0"
  6.1132 +      using u' 
  6.1133 +    proof eventually_elim
  6.1134 +      fix x assume "(\<lambda>i. u i x) ----> u' x"
  6.1135 +      from tendsto_diff[OF tendsto_const[of "u' x"] this]
  6.1136 +      show "(\<lambda>i. ereal (norm (u' x - u i x))) ----> 0"
  6.1137 +        by (simp add: zero_ereal_def tendsto_norm_zero_iff)
  6.1138 +    qed
  6.1139 +  qed (insert bnd, auto)
  6.1140 +  then show ?thesis by simp
  6.1141 +qed
  6.1142 +
  6.1143 +lemma integrableI_bounded:
  6.1144 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1145 +  assumes f[measurable]: "f \<in> borel_measurable M" and fin: "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  6.1146 +  shows "integrable M f"
  6.1147 +proof -
  6.1148 +  from borel_measurable_implies_sequence_metric[OF f, of 0] obtain s where
  6.1149 +    s: "\<And>i. simple_function M (s i)" and
  6.1150 +    pointwise: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) ----> f x" and
  6.1151 +    bound: "\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)"
  6.1152 +    by (simp add: norm_conv_dist) metis
  6.1153 +  
  6.1154 +  show ?thesis
  6.1155 +  proof (rule integrableI_sequence)
  6.1156 +    { fix i
  6.1157 +      have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) \<le> (\<integral>\<^sup>+x. 2 * ereal (norm (f x)) \<partial>M)"
  6.1158 +        by (intro positive_integral_mono) (simp add: bound)
  6.1159 +      also have "\<dots> = 2 * (\<integral>\<^sup>+x. ereal (norm (f x)) \<partial>M)"
  6.1160 +        by (rule positive_integral_cmult) auto
  6.1161 +      finally have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) < \<infinity>"
  6.1162 +        using fin by auto }
  6.1163 +    note fin_s = this
  6.1164 +
  6.1165 +    show "\<And>i. simple_bochner_integrable M (s i)"
  6.1166 +      by (rule simple_bochner_integrableI_bounded) fact+
  6.1167 +
  6.1168 +    show "(\<lambda>i. \<integral>\<^sup>+ x. ereal (norm (f x - s i x)) \<partial>M) ----> 0"
  6.1169 +    proof (rule positive_integral_dominated_convergence_norm)
  6.1170 +      show "\<And>j. AE x in M. norm (s j x) \<le> 2 * norm (f x)"
  6.1171 +        using bound by auto
  6.1172 +      show "\<And>i. s i \<in> borel_measurable M" "(\<lambda>x. 2 * norm (f x)) \<in> borel_measurable M"
  6.1173 +        using s by (auto intro: borel_measurable_simple_function)
  6.1174 +      show "(\<integral>\<^sup>+ x. ereal (2 * norm (f x)) \<partial>M) < \<infinity>"
  6.1175 +        using fin unfolding times_ereal.simps(1)[symmetric] by (subst positive_integral_cmult) auto
  6.1176 +      show "AE x in M. (\<lambda>i. s i x) ----> f x"
  6.1177 +        using pointwise by auto
  6.1178 +    qed fact
  6.1179 +  qed fact
  6.1180 +qed
  6.1181 +
  6.1182 +lemma integrableI_nonneg:
  6.1183 +  fixes f :: "'a \<Rightarrow> real"
  6.1184 +  assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>"
  6.1185 +  shows "integrable M f"
  6.1186 +proof -
  6.1187 +  have "(\<integral>\<^sup>+x. norm (f x) \<partial>M) = (\<integral>\<^sup>+x. f x \<partial>M)"
  6.1188 +    using assms by (intro positive_integral_cong_AE) auto
  6.1189 +  then show ?thesis
  6.1190 +    using assms by (intro integrableI_bounded) auto
  6.1191 +qed
  6.1192 +
  6.1193 +lemma integrable_iff_bounded:
  6.1194 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1195 +  shows "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and> (\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  6.1196 +  using integrableI_bounded[of f M] has_bochner_integral_implies_finite_norm[of M f]
  6.1197 +  unfolding integrable.simps has_bochner_integral.simps[abs_def] by auto
  6.1198 +
  6.1199 +lemma integrable_bound:
  6.1200 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1201 +    and g :: "'a \<Rightarrow> 'c::{banach, second_countable_topology}"
  6.1202 +  shows "integrable M f \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. norm (g x) \<le> norm (f x)) \<Longrightarrow>
  6.1203 +    integrable M g"
  6.1204 +  unfolding integrable_iff_bounded
  6.1205 +proof safe
  6.1206 +  assume "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  6.1207 +  assume "AE x in M. norm (g x) \<le> norm (f x)"
  6.1208 +  then have "(\<integral>\<^sup>+ x. ereal (norm (g x)) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
  6.1209 +    by  (intro positive_integral_mono_AE) auto
  6.1210 +  also assume "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>"
  6.1211 +  finally show "(\<integral>\<^sup>+ x. ereal (norm (g x)) \<partial>M) < \<infinity>" .
  6.1212 +qed 
  6.1213 +
  6.1214 +lemma integrable_abs[simp, intro]:
  6.1215 +  fixes f :: "'a \<Rightarrow> real"
  6.1216 +  assumes [measurable]: "integrable M f" shows "integrable M (\<lambda>x. \<bar>f x\<bar>)"
  6.1217 +  using assms by (rule integrable_bound) auto
  6.1218 +
  6.1219 +lemma integrable_norm[simp, intro]:
  6.1220 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1221 +  assumes [measurable]: "integrable M f" shows "integrable M (\<lambda>x. norm (f x))"
  6.1222 +  using assms by (rule integrable_bound) auto
  6.1223 +  
  6.1224 +lemma integrable_norm_cancel:
  6.1225 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1226 +  assumes [measurable]: "integrable M (\<lambda>x. norm (f x))" "f \<in> borel_measurable M" shows "integrable M f"
  6.1227 +  using assms by (rule integrable_bound) auto
  6.1228 +
  6.1229 +lemma integrable_abs_cancel:
  6.1230 +  fixes f :: "'a \<Rightarrow> real"
  6.1231 +  assumes [measurable]: "integrable M (\<lambda>x. \<bar>f x\<bar>)" "f \<in> borel_measurable M" shows "integrable M f"
  6.1232 +  using assms by (rule integrable_bound) auto
  6.1233 +
  6.1234 +lemma integrable_max[simp, intro]:
  6.1235 +  fixes f :: "'a \<Rightarrow> real"
  6.1236 +  assumes fg[measurable]: "integrable M f" "integrable M g"
  6.1237 +  shows "integrable M (\<lambda>x. max (f x) (g x))"
  6.1238 +  using integrable_add[OF integrable_norm[OF fg(1)] integrable_norm[OF fg(2)]]
  6.1239 +  by (rule integrable_bound) auto
  6.1240 +
  6.1241 +lemma integrable_min[simp, intro]:
  6.1242 +  fixes f :: "'a \<Rightarrow> real"
  6.1243 +  assumes fg[measurable]: "integrable M f" "integrable M g"
  6.1244 +  shows "integrable M (\<lambda>x. min (f x) (g x))"
  6.1245 +  using integrable_add[OF integrable_norm[OF fg(1)] integrable_norm[OF fg(2)]]
  6.1246 +  by (rule integrable_bound) auto
  6.1247 +
  6.1248 +lemma integral_minus_iff[simp]:
  6.1249 +  "integrable M (\<lambda>x. - f x ::'a::{banach, second_countable_topology}) \<longleftrightarrow> integrable M f"
  6.1250 +  unfolding integrable_iff_bounded
  6.1251 +  by (auto intro: borel_measurable_uminus[of "\<lambda>x. - f x" M, simplified])
  6.1252 +
  6.1253 +lemma integrable_indicator_iff:
  6.1254 +  "integrable M (indicator A::_ \<Rightarrow> real) \<longleftrightarrow> A \<inter> space M \<in> sets M \<and> emeasure M (A \<inter> space M) < \<infinity>"
  6.1255 +  by (simp add: integrable_iff_bounded borel_measurable_indicator_iff ereal_indicator positive_integral_indicator'
  6.1256 +           cong: conj_cong)
  6.1257 +
  6.1258 +lemma integral_dominated_convergence:
  6.1259 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and w :: "'a \<Rightarrow> real"
  6.1260 +  assumes "f \<in> borel_measurable M" "\<And>i. s i \<in> borel_measurable M" "integrable M w"
  6.1261 +  assumes lim: "AE x in M. (\<lambda>i. s i x) ----> f x"
  6.1262 +  assumes bound: "\<And>i. AE x in M. norm (s i x) \<le> w x"
  6.1263 +  shows "integrable M f"
  6.1264 +    and "\<And>i. integrable M (s i)"
  6.1265 +    and "(\<lambda>i. integral\<^sup>L M (s i)) ----> integral\<^sup>L M f"
  6.1266 +proof -
  6.1267 +  have "AE x in M. 0 \<le> w x"
  6.1268 +    using bound[of 0] by eventually_elim (auto intro: norm_ge_zero order_trans)
  6.1269 +  then have "(\<integral>\<^sup>+x. w x \<partial>M) = (\<integral>\<^sup>+x. norm (w x) \<partial>M)"
  6.1270 +    by (intro positive_integral_cong_AE) auto
  6.1271 +  with `integrable M w` have w: "w \<in> borel_measurable M" "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
  6.1272 +    unfolding integrable_iff_bounded by auto
  6.1273 +
  6.1274 +  show int_s: "\<And>i. integrable M (s i)"
  6.1275 +    unfolding integrable_iff_bounded
  6.1276 +  proof
  6.1277 +    fix i 
  6.1278 +    have "(\<integral>\<^sup>+ x. ereal (norm (s i x)) \<partial>M) \<le> (\<integral>\<^sup>+x. w x \<partial>M)"
  6.1279 +      using bound by (intro positive_integral_mono_AE) auto
  6.1280 +    with w show "(\<integral>\<^sup>+ x. ereal (norm (s i x)) \<partial>M) < \<infinity>" by auto
  6.1281 +  qed fact
  6.1282 +
  6.1283 +  have all_bound: "AE x in M. \<forall>i. norm (s i x) \<le> w x"
  6.1284 +    using bound unfolding AE_all_countable by auto
  6.1285 +
  6.1286 +  show int_f: "integrable M f"
  6.1287 +    unfolding integrable_iff_bounded
  6.1288 +  proof
  6.1289 +    have "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) \<le> (\<integral>\<^sup>+x. w x \<partial>M)"
  6.1290 +      using all_bound lim
  6.1291 +    proof (intro positive_integral_mono_AE, eventually_elim)
  6.1292 +      fix x assume "\<forall>i. norm (s i x) \<le> w x" "(\<lambda>i. s i x) ----> f x"
  6.1293 +      then show "ereal (norm (f x)) \<le> ereal (w x)"
  6.1294 +        by (intro LIMSEQ_le_const2[where X="\<lambda>i. ereal (norm (s i x))"] tendsto_intros lim_ereal[THEN iffD2]) auto
  6.1295 +    qed
  6.1296 +    with w show "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>" by auto
  6.1297 +  qed fact
  6.1298 +
  6.1299 +  have "(\<lambda>n. ereal (norm (integral\<^sup>L M (s n) - integral\<^sup>L M f))) ----> ereal 0" (is "?d ----> ereal 0")
  6.1300 +  proof (rule tendsto_sandwich)
  6.1301 +    show "eventually (\<lambda>n. ereal 0 \<le> ?d n) sequentially" "(\<lambda>_. ereal 0) ----> ereal 0" by auto
  6.1302 +    show "eventually (\<lambda>n. ?d n \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)) sequentially"
  6.1303 +    proof (intro always_eventually allI)
  6.1304 +      fix n
  6.1305 +      have "?d n = norm (integral\<^sup>L M (\<lambda>x. s n x - f x))"
  6.1306 +        using int_f int_s by simp
  6.1307 +      also have "\<dots> \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)"
  6.1308 +        by (intro int_f int_s integrable_diff integral_norm_bound_ereal)
  6.1309 +      finally show "?d n \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)" .
  6.1310 +    qed
  6.1311 +    show "(\<lambda>n. \<integral>\<^sup>+x. norm (s n x - f x) \<partial>M) ----> ereal 0"
  6.1312 +      unfolding zero_ereal_def[symmetric]
  6.1313 +      apply (subst norm_minus_commute)
  6.1314 +    proof (rule positive_integral_dominated_convergence_norm[where w=w])
  6.1315 +      show "\<And>n. s n \<in> borel_measurable M"
  6.1316 +        using int_s unfolding integrable_iff_bounded by auto
  6.1317 +    qed fact+
  6.1318 +  qed
  6.1319 +  then have "(\<lambda>n. integral\<^sup>L M (s n) - integral\<^sup>L M f) ----> 0"
  6.1320 +    unfolding lim_ereal tendsto_norm_zero_iff .
  6.1321 +  from tendsto_add[OF this tendsto_const[of "integral\<^sup>L M f"]]
  6.1322 +  show "(\<lambda>i. integral\<^sup>L M (s i)) ----> integral\<^sup>L M f"  by simp
  6.1323 +qed
  6.1324 +
  6.1325 +lemma integrable_mult_left_iff:
  6.1326 +  fixes f :: "'a \<Rightarrow> real"
  6.1327 +  shows "integrable M (\<lambda>x. c * f x) \<longleftrightarrow> c = 0 \<or> integrable M f"
  6.1328 +  using integrable_mult_left[of c M f] integrable_mult_left[of "1 / c" M "\<lambda>x. c * f x"]
  6.1329 +  by (cases "c = 0") auto
  6.1330 +
  6.1331 +lemma positive_integral_eq_integral:
  6.1332 +  assumes f: "integrable M f"
  6.1333 +  assumes nonneg: "AE x in M. 0 \<le> f x" 
  6.1334 +  shows "(\<integral>\<^sup>+ x. f x \<partial>M) = integral\<^sup>L M f"
  6.1335 +proof -
  6.1336 +  { fix f :: "'a \<Rightarrow> real" assume f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" "integrable M f"
  6.1337 +    then have "(\<integral>\<^sup>+ x. f x \<partial>M) = integral\<^sup>L M f"
  6.1338 +    proof (induct rule: borel_measurable_induct_real)
  6.1339 +      case (set A) then show ?case
  6.1340 +        by (simp add: integrable_indicator_iff ereal_indicator emeasure_eq_ereal_measure)
  6.1341 +    next
  6.1342 +      case (mult f c) then show ?case
  6.1343 +        unfolding times_ereal.simps(1)[symmetric]
  6.1344 +        by (subst positive_integral_cmult)
  6.1345 +           (auto simp add: integrable_mult_left_iff zero_ereal_def[symmetric])
  6.1346 +    next
  6.1347 +      case (add g f)
  6.1348 +      then have "integrable M f" "integrable M g"
  6.1349 +        by (auto intro!: integrable_bound[OF add(8)])
  6.1350 +      with add show ?case
  6.1351 +        unfolding plus_ereal.simps(1)[symmetric]
  6.1352 +        by (subst positive_integral_add) auto
  6.1353 +    next
  6.1354 +      case (seq s)
  6.1355 +      { fix i x assume "x \<in> space M" with seq(4) have "s i x \<le> f x"
  6.1356 +          by (intro LIMSEQ_le_const[OF seq(5)] exI[of _ i]) (auto simp: incseq_def le_fun_def) }
  6.1357 +      note s_le_f = this
  6.1358 +
  6.1359 +      show ?case
  6.1360 +      proof (rule LIMSEQ_unique)
  6.1361 +        show "(\<lambda>i. ereal (integral\<^sup>L M (s i))) ----> ereal (integral\<^sup>L M f)"
  6.1362 +          unfolding lim_ereal
  6.1363 +        proof (rule integral_dominated_convergence[where w=f])
  6.1364 +          show "integrable M f" by fact
  6.1365 +          from s_le_f seq show "\<And>i. AE x in M. norm (s i x) \<le> f x"
  6.1366 +            by auto
  6.1367 +        qed (insert seq, auto)
  6.1368 +        have int_s: "\<And>i. integrable M (s i)"
  6.1369 +          using seq f s_le_f by (intro integrable_bound[OF f(3)]) auto
  6.1370 +        have "(\<lambda>i. \<integral>\<^sup>+ x. s i x \<partial>M) ----> \<integral>\<^sup>+ x. f x \<partial>M"
  6.1371 +          using seq s_le_f f
  6.1372 +          by (intro positive_integral_dominated_convergence[where w=f])
  6.1373 +             (auto simp: integrable_iff_bounded)
  6.1374 +        also have "(\<lambda>i. \<integral>\<^sup>+x. s i x \<partial>M) = (\<lambda>i. \<integral>x. s i x \<partial>M)"
  6.1375 +          using seq int_s by simp
  6.1376 +        finally show "(\<lambda>i. \<integral>x. s i x \<partial>M) ----> \<integral>\<^sup>+x. f x \<partial>M"
  6.1377 +          by simp
  6.1378 +      qed
  6.1379 +    qed }
  6.1380 +  from this[of "\<lambda>x. max 0 (f x)"] assms have "(\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) = integral\<^sup>L M (\<lambda>x. max 0 (f x))"
  6.1381 +    by simp
  6.1382 +  also have "\<dots> = integral\<^sup>L M f"
  6.1383 +    using assms by (auto intro!: integral_cong_AE)
  6.1384 +  also have "(\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) = (\<integral>\<^sup>+ x. f x \<partial>M)"
  6.1385 +    using assms by (auto intro!: positive_integral_cong_AE simp: max_def)
  6.1386 +  finally show ?thesis .
  6.1387 +qed
  6.1388 +
  6.1389 +lemma integral_norm_bound:
  6.1390 +  fixes f :: "_ \<Rightarrow> 'a :: {banach, second_countable_topology}"
  6.1391 +  shows "integrable M f \<Longrightarrow> norm (integral\<^sup>L M f) \<le> (\<integral>x. norm (f x) \<partial>M)"
  6.1392 +  using positive_integral_eq_integral[of M "\<lambda>x. norm (f x)"]
  6.1393 +  using integral_norm_bound_ereal[of M f] by simp
  6.1394 +  
  6.1395 +lemma integral_eq_positive_integral:
  6.1396 +  "integrable M f \<Longrightarrow> (\<And>x. 0 \<le> f x) \<Longrightarrow> integral\<^sup>L M f = real (\<integral>\<^sup>+ x. ereal (f x) \<partial>M)"
  6.1397 +  by (subst positive_integral_eq_integral) auto
  6.1398 +  
  6.1399 +lemma integrableI_simple_bochner_integrable:
  6.1400 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1401 +  shows "simple_bochner_integrable M f \<Longrightarrow> integrable M f"
  6.1402 +  by (intro integrableI_sequence[where s="\<lambda>_. f"] borel_measurable_simple_function)
  6.1403 +     (auto simp: zero_ereal_def[symmetric] simple_bochner_integrable.simps)
  6.1404 +
  6.1405 +lemma integrable_induct[consumes 1, case_names base add lim, induct pred: integrable]:
  6.1406 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1407 +  assumes "integrable M f"
  6.1408 +  assumes base: "\<And>A c. A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow> P (\<lambda>x. indicator A x *\<^sub>R c)"
  6.1409 +  assumes add: "\<And>f g. integrable M f \<Longrightarrow> P f \<Longrightarrow> integrable M g \<Longrightarrow> P g \<Longrightarrow> P (\<lambda>x. f x + g x)"
  6.1410 +  assumes lim: "\<And>f s. (\<And>i. integrable M (s i)) \<Longrightarrow> (\<And>i. P (s i)) \<Longrightarrow>
  6.1411 +   (\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) ----> f x) \<Longrightarrow>
  6.1412 +   (\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)) \<Longrightarrow> integrable M f \<Longrightarrow> P f"
  6.1413 +  shows "P f"
  6.1414 +proof -
  6.1415 +  from `integrable M f` have f: "f \<in> borel_measurable M" "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  6.1416 +    unfolding integrable_iff_bounded by auto
  6.1417 +  from borel_measurable_implies_sequence_metric[OF f(1)]
  6.1418 +  obtain s where s: "\<And>i. simple_function M (s i)" "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) ----> f x"
  6.1419 +    "\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)"
  6.1420 +    unfolding norm_conv_dist by metis
  6.1421 +
  6.1422 +  { fix f A 
  6.1423 +    have [simp]: "P (\<lambda>x. 0)"
  6.1424 +      using base[of "{}" undefined] by simp
  6.1425 +    have "(\<And>i::'b. i \<in> A \<Longrightarrow> integrable M (f i::'a \<Rightarrow> 'b)) \<Longrightarrow>
  6.1426 +    (\<And>i. i \<in> A \<Longrightarrow> P (f i)) \<Longrightarrow> P (\<lambda>x. \<Sum>i\<in>A. f i x)"
  6.1427 +    by (induct A rule: infinite_finite_induct) (auto intro!: add) }
  6.1428 +  note setsum = this
  6.1429 +
  6.1430 +  def s' \<equiv> "\<lambda>i z. indicator (space M) z *\<^sub>R s i z"
  6.1431 +  then have s'_eq_s: "\<And>i x. x \<in> space M \<Longrightarrow> s' i x = s i x"
  6.1432 +    by simp
  6.1433 +
  6.1434 +  have sf[measurable]: "\<And>i. simple_function M (s' i)"
  6.1435 +    unfolding s'_def using s(1)
  6.1436 +    by (intro simple_function_compose2[where h="op *\<^sub>R"] simple_function_indicator) auto
  6.1437 +
  6.1438 +  { fix i 
  6.1439 +    have "\<And>z. {y. s' i z = y \<and> y \<in> s' i ` space M \<and> y \<noteq> 0 \<and> z \<in> space M} =
  6.1440 +        (if z \<in> space M \<and> s' i z \<noteq> 0 then {s' i z} else {})"
  6.1441 +      by (auto simp add: s'_def split: split_indicator)
  6.1442 +    then have "\<And>z. s' i = (\<lambda>z. \<Sum>y\<in>s' i`space M - {0}. indicator {x\<in>space M. s' i x = y} z *\<^sub>R y)"
  6.1443 +      using sf by (auto simp: fun_eq_iff simple_function_def s'_def) }
  6.1444 +  note s'_eq = this
  6.1445 +
  6.1446 +  show "P f"
  6.1447 +  proof (rule lim)
  6.1448 +    fix i
  6.1449 +
  6.1450 +    have "(\<integral>\<^sup>+x. norm (s' i x) \<partial>M) \<le> (\<integral>\<^sup>+x. 2 * ereal (norm (f x)) \<partial>M)"
  6.1451 +      using s by (intro positive_integral_mono) (auto simp: s'_eq_s)
  6.1452 +    also have "\<dots> < \<infinity>"
  6.1453 +      using f by (subst positive_integral_cmult) auto
  6.1454 +    finally have sbi: "simple_bochner_integrable M (s' i)"
  6.1455 +      using sf by (intro simple_bochner_integrableI_bounded) auto
  6.1456 +    then show "integrable M (s' i)"
  6.1457 +      by (rule integrableI_simple_bochner_integrable)
  6.1458 +
  6.1459 +    { fix x assume"x \<in> space M" "s' i x \<noteq> 0"
  6.1460 +      then have "emeasure M {y \<in> space M. s' i y = s' i x} \<le> emeasure M {y \<in> space M. s' i y \<noteq> 0}"
  6.1461 +        by (intro emeasure_mono) auto
  6.1462 +      also have "\<dots> < \<infinity>"
  6.1463 +        using sbi by (auto elim: simple_bochner_integrable.cases)
  6.1464 +      finally have "emeasure M {y \<in> space M. s' i y = s' i x} \<noteq> \<infinity>" by simp }
  6.1465 +    then show "P (s' i)"
  6.1466 +      by (subst s'_eq) (auto intro!: setsum base)
  6.1467 +
  6.1468 +    fix x assume "x \<in> space M" with s show "(\<lambda>i. s' i x) ----> f x"
  6.1469 +      by (simp add: s'_eq_s)
  6.1470 +    show "norm (s' i x) \<le> 2 * norm (f x)"
  6.1471 +      using `x \<in> space M` s by (simp add: s'_eq_s)
  6.1472 +  qed fact
  6.1473 +qed
  6.1474 +
  6.1475 +lemma integral_nonneg_AE:
  6.1476 +  fixes f :: "'a \<Rightarrow> real"
  6.1477 +  assumes [measurable]: "integrable M f" "AE x in M. 0 \<le> f x"
  6.1478 +  shows "0 \<le> integral\<^sup>L M f"
  6.1479 +proof -
  6.1480 +  have "0 \<le> ereal (integral\<^sup>L M (\<lambda>x. max 0 (f x)))"
  6.1481 +    by (subst integral_eq_positive_integral)
  6.1482 +       (auto intro: real_of_ereal_pos positive_integral_positive integrable_max assms)
  6.1483 +  also have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) = integral\<^sup>L M f"
  6.1484 +    using assms(2) by (intro integral_cong_AE assms integrable_max) auto
  6.1485 +  finally show ?thesis
  6.1486 +    by simp
  6.1487 +qed
  6.1488 +
  6.1489 +lemma integral_eq_zero_AE:
  6.1490 +  "f \<in> borel_measurable M \<Longrightarrow> (AE x in M. f x = 0) \<Longrightarrow> integral\<^sup>L M f = 0"
  6.1491 +  using integral_cong_AE[of f M "\<lambda>_. 0"] by simp
  6.1492 +
  6.1493 +lemma integral_nonneg_eq_0_iff_AE:
  6.1494 +  fixes f :: "_ \<Rightarrow> real"
  6.1495 +  assumes f[measurable]: "integrable M f" and nonneg: "AE x in M. 0 \<le> f x"
  6.1496 +  shows "integral\<^sup>L M f = 0 \<longleftrightarrow> (AE x in M. f x = 0)"
  6.1497 +proof
  6.1498 +  assume "integral\<^sup>L M f = 0"
  6.1499 +  then have "integral\<^sup>P M f = 0"
  6.1500 +    using positive_integral_eq_integral[OF f nonneg] by simp
  6.1501 +  then have "AE x in M. ereal (f x) \<le> 0"
  6.1502 +    by (simp add: positive_integral_0_iff_AE)
  6.1503 +  with nonneg show "AE x in M. f x = 0"
  6.1504 +    by auto
  6.1505 +qed (auto simp add: integral_eq_zero_AE)
  6.1506 +
  6.1507 +lemma integral_mono_AE:
  6.1508 +  fixes f :: "'a \<Rightarrow> real"
  6.1509 +  assumes "integrable M f" "integrable M g" "AE x in M. f x \<le> g x"
  6.1510 +  shows "integral\<^sup>L M f \<le> integral\<^sup>L M g"
  6.1511 +proof -
  6.1512 +  have "0 \<le> integral\<^sup>L M (\<lambda>x. g x - f x)"
  6.1513 +    using assms by (intro integral_nonneg_AE integrable_diff assms) auto
  6.1514 +  also have "\<dots> = integral\<^sup>L M g - integral\<^sup>L M f"
  6.1515 +    by (intro integral_diff assms)
  6.1516 +  finally show ?thesis by simp
  6.1517 +qed
  6.1518 +
  6.1519 +lemma integral_mono:
  6.1520 +  fixes f :: "'a \<Rightarrow> real"
  6.1521 +  shows "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x \<le> g x) \<Longrightarrow> 
  6.1522 +    integral\<^sup>L M f \<le> integral\<^sup>L M g"
  6.1523 +  by (intro integral_mono_AE) auto
  6.1524 +
  6.1525 +section {* Measure spaces with an associated density *}
  6.1526 +
  6.1527 +lemma integrable_density:
  6.1528 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  6.1529 +  assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  6.1530 +    and nn: "AE x in M. 0 \<le> g x"
  6.1531 +  shows "integrable (density M g) f \<longleftrightarrow> integrable M (\<lambda>x. g x *\<^sub>R f x)"
  6.1532 +  unfolding integrable_iff_bounded using nn
  6.1533 +  apply (simp add: positive_integral_density )
  6.1534 +  apply (intro arg_cong2[where f="op ="] refl positive_integral_cong_AE)
  6.1535 +  apply auto
  6.1536 +  done
  6.1537 +
  6.1538 +lemma integral_density:
  6.1539 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  6.1540 +  assumes f: "f \<in> borel_measurable M"
  6.1541 +    and g[measurable]: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  6.1542 +  shows "integral\<^sup>L (density M g) f = integral\<^sup>L M (\<lambda>x. g x *\<^sub>R f x)"
  6.1543 +proof (rule integral_eq_cases)
  6.1544 +  assume "integrable (density M g) f"
  6.1545 +  then show ?thesis
  6.1546 +  proof induct
  6.1547 +    case (base A c)
  6.1548 +    then have [measurable]: "A \<in> sets M" by auto
  6.1549 +  
  6.1550 +    have int: "integrable M (\<lambda>x. g x * indicator A x)"
  6.1551 +      using g base integrable_density[of "indicator A :: 'a \<Rightarrow> real" M g] by simp
  6.1552 +    then have "integral\<^sup>L M (\<lambda>x. g x * indicator A x) = (\<integral>\<^sup>+ x. ereal (g x * indicator A x) \<partial>M)"
  6.1553 +      using g by (subst positive_integral_eq_integral) auto
  6.1554 +    also have "\<dots> = (\<integral>\<^sup>+ x. ereal (g x) * indicator A x \<partial>M)"
  6.1555 +      by (intro positive_integral_cong) (auto split: split_indicator)
  6.1556 +    also have "\<dots> = emeasure (density M g) A"
  6.1557 +      by (rule emeasure_density[symmetric]) auto
  6.1558 +    also have "\<dots> = ereal (measure (density M g) A)"
  6.1559 +      using base by (auto intro: emeasure_eq_ereal_measure)
  6.1560 +    also have "\<dots> = integral\<^sup>L (density M g) (indicator A)"
  6.1561 +      using base by simp
  6.1562 +    finally show ?case
  6.1563 +      using base by (simp add: int)
  6.1564 +  next
  6.1565 +    case (add f h)
  6.1566 +    then have [measurable]: "f \<in> borel_measurable M" "h \<in> borel_measurable M"
  6.1567 +      by (auto dest!: borel_measurable_integrable)
  6.1568 +    from add g show ?case
  6.1569 +      by (simp add: scaleR_add_right integrable_density)
  6.1570 +  next
  6.1571 +    case (lim f s)
  6.1572 +    have [measurable]: "f \<in> borel_measurable M" "\<And>i. s i \<in> borel_measurable M"
  6.1573 +      using lim(1,5)[THEN borel_measurable_integrable] by auto
  6.1574 +  
  6.1575 +    show ?case
  6.1576 +    proof (rule LIMSEQ_unique)
  6.1577 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. g x *\<^sub>R s i x)) ----> integral\<^sup>L M (\<lambda>x. g x *\<^sub>R f x)"
  6.1578 +      proof (rule integral_dominated_convergence(3))
  6.1579 +        show "integrable M (\<lambda>x. 2 * norm (g x *\<^sub>R f x))"
  6.1580 +          by (intro integrable_mult_right integrable_norm integrable_density[THEN iffD1] lim g) auto
  6.1581 +        show "AE x in M. (\<lambda>i. g x *\<^sub>R s i x) ----> g x *\<^sub>R f x"
  6.1582 +          using lim(3) by (auto intro!: tendsto_scaleR AE_I2[of M])
  6.1583 +        show "\<And>i. AE x in M. norm (g x *\<^sub>R s i x) \<le> 2 * norm (g x *\<^sub>R f x)"
  6.1584 +          using lim(4) g by (auto intro!: AE_I2[of M] mult_left_mono simp: field_simps)
  6.1585 +      qed auto
  6.1586 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. g x *\<^sub>R s i x)) ----> integral\<^sup>L (density M g) f"
  6.1587 +        unfolding lim(2)[symmetric]
  6.1588 +        by (rule integral_dominated_convergence(3)[where w="\<lambda>x. 2 * norm (f x)"])
  6.1589 +           (insert lim(3-5), auto intro: integrable_norm)
  6.1590 +    qed
  6.1591 +  qed
  6.1592 +qed (simp add: f g integrable_density)
  6.1593 +
  6.1594 +lemma
  6.1595 +  fixes g :: "'a \<Rightarrow> real"
  6.1596 +  assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "g \<in> borel_measurable M"
  6.1597 +  shows integral_real_density: "integral\<^sup>L (density M f) g = (\<integral> x. f x * g x \<partial>M)"
  6.1598 +    and integrable_real_density: "integrable (density M f) g \<longleftrightarrow> integrable M (\<lambda>x. f x * g x)"
  6.1599 +  using assms integral_density[of g M f] integrable_density[of g M f] by auto
  6.1600 +
  6.1601 +lemma has_bochner_integral_density:
  6.1602 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  6.1603 +  shows "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. 0 \<le> g x) \<Longrightarrow>
  6.1604 +    has_bochner_integral M (\<lambda>x. g x *\<^sub>R f x) x \<Longrightarrow> has_bochner_integral (density M g) f x"
  6.1605 +  by (simp add: has_bochner_integral_iff integrable_density integral_density)
  6.1606 +
  6.1607 +subsection {* Distributions *}
  6.1608 +
  6.1609 +lemma integrable_distr_eq:
  6.1610 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1611 +  assumes [measurable]: "g \<in> measurable M N" "f \<in> borel_measurable N"
  6.1612 +  shows "integrable (distr M N g) f \<longleftrightarrow> integrable M (\<lambda>x. f (g x))"
  6.1613 +  unfolding integrable_iff_bounded by (simp_all add: positive_integral_distr)
  6.1614 +
  6.1615 +lemma integrable_distr:
  6.1616 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1617 +  shows "T \<in> measurable M M' \<Longrightarrow> integrable (distr M M' T) f \<Longrightarrow> integrable M (\<lambda>x. f (T x))"
  6.1618 +  by (subst integrable_distr_eq[symmetric, where g=T])
  6.1619 +     (auto dest: borel_measurable_integrable)
  6.1620 +
  6.1621 +lemma integral_distr:
  6.1622 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1623 +  assumes g[measurable]: "g \<in> measurable M N" and f: "f \<in> borel_measurable N"
  6.1624 +  shows "integral\<^sup>L (distr M N g) f = integral\<^sup>L M (\<lambda>x. f (g x))"
  6.1625 +proof (rule integral_eq_cases)
  6.1626 +  assume "integrable (distr M N g) f"
  6.1627 +  then show ?thesis
  6.1628 +  proof induct
  6.1629 +    case (base A c)
  6.1630 +    then have [measurable]: "A \<in> sets N" by auto
  6.1631 +    from base have int: "integrable (distr M N g) (\<lambda>a. indicator A a *\<^sub>R c)"
  6.1632 +      by (intro integrable_indicator)
  6.1633 +  
  6.1634 +    have "integral\<^sup>L (distr M N g) (\<lambda>a. indicator A a *\<^sub>R c) = measure (distr M N g) A *\<^sub>R c"
  6.1635 +      using base by (subst integral_indicator) auto
  6.1636 +    also have "\<dots> = measure M (g -` A \<inter> space M) *\<^sub>R c"
  6.1637 +      by (subst measure_distr) auto
  6.1638 +    also have "\<dots> = integral\<^sup>L M (\<lambda>a. indicator (g -` A \<inter> space M) a *\<^sub>R c)"
  6.1639 +      using base by (subst integral_indicator) (auto simp: emeasure_distr)
  6.1640 +    also have "\<dots> = integral\<^sup>L M (\<lambda>a. indicator A (g a) *\<^sub>R c)"
  6.1641 +      using int base by (intro integral_cong_AE) (auto simp: emeasure_distr split: split_indicator)
  6.1642 +    finally show ?case .
  6.1643 +  next
  6.1644 +    case (add f h)
  6.1645 +    then have [measurable]: "f \<in> borel_measurable N" "h \<in> borel_measurable N"
  6.1646 +      by (auto dest!: borel_measurable_integrable)
  6.1647 +    from add g show ?case
  6.1648 +      by (simp add: scaleR_add_right integrable_distr_eq)
  6.1649 +  next
  6.1650 +    case (lim f s)
  6.1651 +    have [measurable]: "f \<in> borel_measurable N" "\<And>i. s i \<in> borel_measurable N"
  6.1652 +      using lim(1,5)[THEN borel_measurable_integrable] by auto
  6.1653 +  
  6.1654 +    show ?case
  6.1655 +    proof (rule LIMSEQ_unique)
  6.1656 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. s i (g x))) ----> integral\<^sup>L M (\<lambda>x. f (g x))"
  6.1657 +      proof (rule integral_dominated_convergence(3))
  6.1658 +        show "integrable M (\<lambda>x. 2 * norm (f (g x)))"
  6.1659 +          using lim by (auto intro!: integrable_norm simp: integrable_distr_eq) 
  6.1660 +        show "AE x in M. (\<lambda>i. s i (g x)) ----> f (g x)"
  6.1661 +          using lim(3) g[THEN measurable_space] by auto
  6.1662 +        show "\<And>i. AE x in M. norm (s i (g x)) \<le> 2 * norm (f (g x))"
  6.1663 +          using lim(4) g[THEN measurable_space] by auto
  6.1664 +      qed auto
  6.1665 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. s i (g x))) ----> integral\<^sup>L (distr M N g) f"
  6.1666 +        unfolding lim(2)[symmetric]
  6.1667 +        by (rule integral_dominated_convergence(3)[where w="\<lambda>x. 2 * norm (f x)"])
  6.1668 +           (insert lim(3-5), auto intro: integrable_norm)
  6.1669 +    qed
  6.1670 +  qed
  6.1671 +qed (simp add: f g integrable_distr_eq)
  6.1672 +
  6.1673 +lemma has_bochner_integral_distr:
  6.1674 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1675 +  shows "f \<in> borel_measurable N \<Longrightarrow> g \<in> measurable M N \<Longrightarrow>
  6.1676 +    has_bochner_integral M (\<lambda>x. f (g x)) x \<Longrightarrow> has_bochner_integral (distr M N g) f x"
  6.1677 +  by (simp add: has_bochner_integral_iff integrable_distr_eq integral_distr)
  6.1678 +
  6.1679 +section {* Lebesgue integration on @{const count_space} *}
  6.1680 +
  6.1681 +lemma integrable_count_space:
  6.1682 +  fixes f :: "'a \<Rightarrow> 'b::{banach,second_countable_topology}"
  6.1683 +  shows "finite X \<Longrightarrow> integrable (count_space X) f"
  6.1684 +  by (auto simp: positive_integral_count_space integrable_iff_bounded)
  6.1685 +
  6.1686 +lemma measure_count_space[simp]:
  6.1687 +  "B \<subseteq> A \<Longrightarrow> finite B \<Longrightarrow> measure (count_space A) B = card B"
  6.1688 +  unfolding measure_def by (subst emeasure_count_space ) auto
  6.1689 +
  6.1690 +lemma lebesgue_integral_count_space_finite_support:
  6.1691 +  assumes f: "finite {a\<in>A. f a \<noteq> 0}"
  6.1692 +  shows "(\<integral>x. f x \<partial>count_space A) = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. f a)"
  6.1693 +proof -
  6.1694 +  have eq: "\<And>x. x \<in> A \<Longrightarrow> (\<Sum>a | x = a \<and> a \<in> A \<and> f a \<noteq> 0. f a) = (\<Sum>x\<in>{x}. f x)"
  6.1695 +    by (intro setsum_mono_zero_cong_left) auto
  6.1696 +    
  6.1697 +  have "(\<integral>x. f x \<partial>count_space A) = (\<integral>x. (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. indicator {a} x *\<^sub>R f a) \<partial>count_space A)"
  6.1698 +    by (intro integral_cong refl) (simp add: f eq)
  6.1699 +  also have "\<dots> = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. measure (count_space A) {a} *\<^sub>R f a)"
  6.1700 +    by (subst integral_setsum) (auto intro!: setsum_cong)
  6.1701 +  finally show ?thesis
  6.1702 +    by auto
  6.1703 +qed
  6.1704 +
  6.1705 +lemma lebesgue_integral_count_space_finite: "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
  6.1706 +  by (subst lebesgue_integral_count_space_finite_support)
  6.1707 +     (auto intro!: setsum_mono_zero_cong_left)
  6.1708 +
  6.1709 +section {* Point measure *}
  6.1710 +
  6.1711 +lemma lebesgue_integral_point_measure_finite:
  6.1712 +  fixes g :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1713 +  shows "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow>
  6.1714 +    integral\<^sup>L (point_measure A f) g = (\<Sum>a\<in>A. f a *\<^sub>R g a)"
  6.1715 +  by (simp add: lebesgue_integral_count_space_finite AE_count_space integral_density point_measure_def)
  6.1716 +
  6.1717 +lemma integrable_point_measure_finite:
  6.1718 +  fixes g :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and f :: "'a \<Rightarrow> real"
  6.1719 +  shows "finite A \<Longrightarrow> integrable (point_measure A f) g"
  6.1720 +  unfolding point_measure_def
  6.1721 +  apply (subst density_ereal_max_0)
  6.1722 +  apply (subst integrable_density)
  6.1723 +  apply (auto simp: AE_count_space integrable_count_space)
  6.1724 +  done
  6.1725 +
  6.1726 +subsection {* Legacy lemmas for the real-valued Lebesgue integral\<^sup>L *}
  6.1727 +
  6.1728 +lemma real_lebesgue_integral_def:
  6.1729 +  assumes f: "integrable M f"
  6.1730 +  shows "integral\<^sup>L M f = real (\<integral>\<^sup>+x. f x \<partial>M) - real (\<integral>\<^sup>+x. - f x \<partial>M)"
  6.1731 +proof -
  6.1732 +  have "integral\<^sup>L M f = integral\<^sup>L M (\<lambda>x. max 0 (f x) - max 0 (- f x))"
  6.1733 +    by (auto intro!: arg_cong[where f="integral\<^sup>L M"])
  6.1734 +  also have "\<dots> = integral\<^sup>L M (\<lambda>x. max 0 (f x)) - integral\<^sup>L M (\<lambda>x. max 0 (- f x))"
  6.1735 +    by (intro integral_diff integrable_max integrable_minus integrable_zero f)
  6.1736 +  also have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) = real (\<integral>\<^sup>+x. max 0 (f x) \<partial>M)"
  6.1737 +    by (subst integral_eq_positive_integral[symmetric]) (auto intro!: integrable_max f)
  6.1738 +  also have "integral\<^sup>L M (\<lambda>x. max 0 (- f x)) = real (\<integral>\<^sup>+x. max 0 (- f x) \<partial>M)"
  6.1739 +    by (subst integral_eq_positive_integral[symmetric]) (auto intro!: integrable_max f)
  6.1740 +  also have "(\<lambda>x. ereal (max 0 (f x))) = (\<lambda>x. max 0 (ereal (f x)))"
  6.1741 +    by (auto simp: max_def)
  6.1742 +  also have "(\<lambda>x. ereal (max 0 (- f x))) = (\<lambda>x. max 0 (- ereal (f x)))"
  6.1743 +    by (auto simp: max_def)
  6.1744 +  finally show ?thesis
  6.1745 +    unfolding positive_integral_max_0 .
  6.1746 +qed
  6.1747 +
  6.1748 +lemma real_integrable_def:
  6.1749 +  "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  6.1750 +    (\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  6.1751 +  unfolding integrable_iff_bounded
  6.1752 +proof (safe del: notI)
  6.1753 +  assume *: "(\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M) < \<infinity>"
  6.1754 +  have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
  6.1755 +    by (intro positive_integral_mono) auto
  6.1756 +  also note *
  6.1757 +  finally show "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>"
  6.1758 +    by simp
  6.1759 +  have "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (norm (f x)) \<partial>M)"
  6.1760 +    by (intro positive_integral_mono) auto
  6.1761 +  also note *
  6.1762 +  finally show "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  6.1763 +    by simp
  6.1764 +next
  6.1765 +  assume [measurable]: "f \<in> borel_measurable M"
  6.1766 +  assume fin: "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  6.1767 +  have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) = (\<integral>\<^sup>+ x. max 0 (ereal (f x)) + max 0 (ereal (- f x)) \<partial>M)"
  6.1768 +    by (intro positive_integral_cong) (auto simp: max_def)
  6.1769 +  also have"\<dots> = (\<integral>\<^sup>+ x. max 0 (ereal (f x)) \<partial>M) + (\<integral>\<^sup>+ x. max 0 (ereal (- f x)) \<partial>M)"
  6.1770 +    by (intro positive_integral_add) auto
  6.1771 +  also have "\<dots> < \<infinity>"
  6.1772 +    using fin by (auto simp: positive_integral_max_0)
  6.1773 +  finally show "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) < \<infinity>" .
  6.1774 +qed
  6.1775 +
  6.1776 +lemma integrableD[dest]:
  6.1777 +  assumes "integrable M f"
  6.1778 +  shows "f \<in> borel_measurable M" "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
  6.1779 +  using assms unfolding real_integrable_def by auto
  6.1780 +
  6.1781 +lemma integrableE:
  6.1782 +  assumes "integrable M f"
  6.1783 +  obtains r q where
  6.1784 +    "(\<integral>\<^sup>+x. ereal (f x)\<partial>M) = ereal r"
  6.1785 +    "(\<integral>\<^sup>+x. ereal (-f x)\<partial>M) = ereal q"
  6.1786 +    "f \<in> borel_measurable M" "integral\<^sup>L M f = r - q"
  6.1787 +  using assms unfolding real_integrable_def real_lebesgue_integral_def[OF assms]
  6.1788 +  using positive_integral_positive[of M "\<lambda>x. ereal (f x)"]
  6.1789 +  using positive_integral_positive[of M "\<lambda>x. ereal (-f x)"]
  6.1790 +  by (cases rule: ereal2_cases[of "(\<integral>\<^sup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^sup>+x. ereal (f x)\<partial>M)"]) auto
  6.1791 +
  6.1792 +lemma integral_monotone_convergence_nonneg:
  6.1793 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  6.1794 +  assumes i: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  6.1795 +    and pos: "\<And>i. AE x in M. 0 \<le> f i x"
  6.1796 +    and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
  6.1797 +    and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) ----> x"
  6.1798 +    and u: "u \<in> borel_measurable M"
  6.1799 +  shows "integrable M u"
  6.1800 +  and "integral\<^sup>L M u = x"
  6.1801 +proof -
  6.1802 +  have "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^sup>+ x. ereal (f n x) \<partial>M))"
  6.1803 +  proof (subst positive_integral_monotone_convergence_SUP_AE[symmetric])
  6.1804 +    fix i
  6.1805 +    from mono pos show "AE x in M. ereal (f i x) \<le> ereal (f (Suc i) x) \<and> 0 \<le> ereal (f i x)"
  6.1806 +      by eventually_elim (auto simp: mono_def)
  6.1807 +    show "(\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
  6.1808 +      using i by auto
  6.1809 +  next
  6.1810 +    show "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = \<integral>\<^sup>+ x. (SUP i. ereal (f i x)) \<partial>M"
  6.1811 +      apply (rule positive_integral_cong_AE)
  6.1812 +      using lim mono
  6.1813 +      by eventually_elim (simp add: SUP_eq_LIMSEQ[THEN iffD2])
  6.1814 +  qed
  6.1815 +  also have "\<dots> = ereal x"
  6.1816 +    using mono i unfolding positive_integral_eq_integral[OF i pos]
  6.1817 +    by (subst SUP_eq_LIMSEQ) (auto simp: mono_def intro!: integral_mono_AE ilim)
  6.1818 +  finally have "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = ereal x" .
  6.1819 +  moreover have "(\<integral>\<^sup>+ x. ereal (- u x) \<partial>M) = 0"
  6.1820 +  proof (subst positive_integral_0_iff_AE)
  6.1821 +    show "(\<lambda>x. ereal (- u x)) \<in> borel_measurable M"
  6.1822 +      using u by auto
  6.1823 +    from mono pos[of 0] lim show "AE x in M. ereal (- u x) \<le> 0"
  6.1824 +    proof eventually_elim
  6.1825 +      fix x assume "mono (\<lambda>n. f n x)" "0 \<le> f 0 x" "(\<lambda>i. f i x) ----> u x"
  6.1826 +      then show "ereal (- u x) \<le> 0"
  6.1827 +        using incseq_le[of "\<lambda>n. f n x" "u x" 0] by (simp add: mono_def incseq_def)
  6.1828 +    qed
  6.1829 +  qed
  6.1830 +  ultimately show "integrable M u" "integral\<^sup>L M u = x"
  6.1831 +    by (auto simp: real_integrable_def real_lebesgue_integral_def u)
  6.1832 +qed
  6.1833 +
  6.1834 +lemma
  6.1835 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  6.1836 +  assumes f: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  6.1837 +  and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
  6.1838 +  and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) ----> x"
  6.1839 +  and u: "u \<in> borel_measurable M"
  6.1840 +  shows integrable_monotone_convergence: "integrable M u"
  6.1841 +    and integral_monotone_convergence: "integral\<^sup>L M u = x"
  6.1842 +    and has_bochner_integral_monotone_convergence: "has_bochner_integral M u x"
  6.1843 +proof -
  6.1844 +  have 1: "\<And>i. integrable M (\<lambda>x. f i x - f 0 x)"
  6.1845 +    using f by auto
  6.1846 +  have 2: "AE x in M. mono (\<lambda>n. f n x - f 0 x)"
  6.1847 +    using mono by (auto simp: mono_def le_fun_def)
  6.1848 +  have 3: "\<And>n. AE x in M. 0 \<le> f n x - f 0 x"
  6.1849 +    using mono by (auto simp: field_simps mono_def le_fun_def)
  6.1850 +  have 4: "AE x in M. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
  6.1851 +    using lim by (auto intro!: tendsto_diff)
  6.1852 +  have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x \<partial>M)) ----> x - integral\<^sup>L M (f 0)"
  6.1853 +    using f ilim by (auto intro!: tendsto_diff)
  6.1854 +  have 6: "(\<lambda>x. u x - f 0 x) \<in> borel_measurable M"
  6.1855 +    using f[of 0] u by auto
  6.1856 +  note diff = integral_monotone_convergence_nonneg[OF 1 2 3 4 5 6]
  6.1857 +  have "integrable M (\<lambda>x. (u x - f 0 x) + f 0 x)"
  6.1858 +    using diff(1) f by (rule integrable_add)
  6.1859 +  with diff(2) f show "integrable M u" "integral\<^sup>L M u = x"
  6.1860 +    by auto
  6.1861 +  then show "has_bochner_integral M u x"
  6.1862 +    by (metis has_bochner_integral_integrable)
  6.1863 +qed
  6.1864 +
  6.1865 +lemma integral_norm_eq_0_iff:
  6.1866 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.1867 +  assumes f[measurable]: "integrable M f"
  6.1868 +  shows "(\<integral>x. norm (f x) \<partial>M) = 0 \<longleftrightarrow> emeasure M {x\<in>space M. f x \<noteq> 0} = 0"
  6.1869 +proof -
  6.1870 +  have "(\<integral>\<^sup>+x. norm (f x) \<partial>M) = (\<integral>x. norm (f x) \<partial>M)"
  6.1871 +    using f by (intro positive_integral_eq_integral integrable_norm) auto
  6.1872 +  then have "(\<integral>x. norm (f x) \<partial>M) = 0 \<longleftrightarrow> (\<integral>\<^sup>+x. norm (f x) \<partial>M) = 0"
  6.1873 +    by simp
  6.1874 +  also have "\<dots> \<longleftrightarrow> emeasure M {x\<in>space M. ereal (norm (f x)) \<noteq> 0} = 0"
  6.1875 +    by (intro positive_integral_0_iff) auto
  6.1876 +  finally show ?thesis
  6.1877 +    by simp
  6.1878 +qed
  6.1879 +
  6.1880 +lemma integral_0_iff:
  6.1881 +  fixes f :: "'a \<Rightarrow> real"
  6.1882 +  shows "integrable M f \<Longrightarrow> (\<integral>x. abs (f x) \<partial>M) = 0 \<longleftrightarrow> emeasure M {x\<in>space M. f x \<noteq> 0} = 0"
  6.1883 +  using integral_norm_eq_0_iff[of M f] by simp
  6.1884 +
  6.1885 +lemma (in finite_measure) lebesgue_integral_const[simp]:
  6.1886 +  "(\<integral>x. a \<partial>M) = measure M (space M) *\<^sub>R a"
  6.1887 +  using integral_indicator[of "space M" M a]
  6.1888 +  by (simp del: integral_indicator integral_scaleR_left cong: integral_cong)
  6.1889 +
  6.1890 +lemma (in finite_measure) integrable_const[intro!, simp]: "integrable M (\<lambda>x. a)"
  6.1891 +  using integrable_indicator[of "space M" M a] by (simp cong: integrable_cong)
  6.1892 +
  6.1893 +lemma (in finite_measure) integrable_const_bound:
  6.1894 +  fixes f :: "'a \<Rightarrow> 'b::{banach,second_countable_topology}"
  6.1895 +  shows "AE x in M. norm (f x) \<le> B \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> integrable M f"
  6.1896 +  apply (rule integrable_bound[OF integrable_const[of B], of f])
  6.1897 +  apply assumption
  6.1898 +  apply (cases "0 \<le> B")
  6.1899 +  apply auto
  6.1900 +  done
  6.1901 +
  6.1902 +lemma (in finite_measure) integral_less_AE:
  6.1903 +  fixes X Y :: "'a \<Rightarrow> real"
  6.1904 +  assumes int: "integrable M X" "integrable M Y"
  6.1905 +  assumes A: "(emeasure M) A \<noteq> 0" "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  6.1906 +  assumes gt: "AE x in M. X x \<le> Y x"
  6.1907 +  shows "integral\<^sup>L M X < integral\<^sup>L M Y"
  6.1908 +proof -
  6.1909 +  have "integral\<^sup>L M X \<le> integral\<^sup>L M Y"
  6.1910 +    using gt int by (intro integral_mono_AE) auto
  6.1911 +  moreover
  6.1912 +  have "integral\<^sup>L M X \<noteq> integral\<^sup>L M Y"
  6.1913 +  proof
  6.1914 +    assume eq: "integral\<^sup>L M X = integral\<^sup>L M Y"
  6.1915 +    have "integral\<^sup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^sup>L M (\<lambda>x. Y x - X x)"
  6.1916 +      using gt int by (intro integral_cong_AE) auto
  6.1917 +    also have "\<dots> = 0"
  6.1918 +      using eq int by simp
  6.1919 +    finally have "(emeasure M) {x \<in> space M. Y x - X x \<noteq> 0} = 0"
  6.1920 +      using int by (simp add: integral_0_iff)
  6.1921 +    moreover
  6.1922 +    have "(\<integral>\<^sup>+x. indicator A x \<partial>M) \<le> (\<integral>\<^sup>+x. indicator {x \<in> space M. Y x - X x \<noteq> 0} x \<partial>M)"
  6.1923 +      using A by (intro positive_integral_mono_AE) auto
  6.1924 +    then have "(emeasure M) A \<le> (emeasure M) {x \<in> space M. Y x - X x \<noteq> 0}"
  6.1925 +      using int A by (simp add: integrable_def)
  6.1926 +    ultimately have "emeasure M A = 0"
  6.1927 +      using emeasure_nonneg[of M A] by simp
  6.1928 +    with `(emeasure M) A \<noteq> 0` show False by auto
  6.1929 +  qed
  6.1930 +  ultimately show ?thesis by auto
  6.1931 +qed
  6.1932 +
  6.1933 +lemma (in finite_measure) integral_less_AE_space:
  6.1934 +  fixes X Y :: "'a \<Rightarrow> real"
  6.1935 +  assumes int: "integrable M X" "integrable M Y"
  6.1936 +  assumes gt: "AE x in M. X x < Y x" "emeasure M (space M) \<noteq> 0"
  6.1937 +  shows "integral\<^sup>L M X < integral\<^sup>L M Y"
  6.1938 +  using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
  6.1939 +
  6.1940 +(* GENERALIZE to banach, sct *)
  6.1941 +lemma integral_sums:
  6.1942 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  6.1943 +  assumes integrable[measurable]: "\<And>i. integrable M (f i)"
  6.1944 +  and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
  6.1945 +  and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  6.1946 +  shows "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
  6.1947 +  and "(\<lambda>i. integral\<^sup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is ?integral)
  6.1948 +proof -
  6.1949 +  have "\<forall>x\<in>space M. \<exists>w. (\<lambda>i. \<bar>f i x\<bar>) sums w"
  6.1950 +    using summable unfolding summable_def by auto
  6.1951 +  from bchoice[OF this]
  6.1952 +  obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
  6.1953 +  then have w_borel: "w \<in> borel_measurable M" unfolding sums_def
  6.1954 +    by (rule borel_measurable_LIMSEQ) auto
  6.1955 +
  6.1956 +  let ?w = "\<lambda>y. if y \<in> space M then w y else 0"
  6.1957 +
  6.1958 +  obtain x where abs_sum: "(\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M)) sums x"
  6.1959 +    using sums unfolding summable_def ..
  6.1960 +
  6.1961 +  have 1: "\<And>n. integrable M (\<lambda>x. \<Sum>i<n. f i x)"
  6.1962 +    using integrable by auto
  6.1963 +
  6.1964 +  have 2: "\<And>j. AE x in M. norm (\<Sum>i<j. f i x) \<le> ?w x"
  6.1965 +    using AE_space
  6.1966 +  proof eventually_elim
  6.1967 +    fix j x assume [simp]: "x \<in> space M"
  6.1968 +    have "\<bar>\<Sum>i<j. f i x\<bar> \<le> (\<Sum>i<j. \<bar>f i x\<bar>)" by (rule setsum_abs)
  6.1969 +    also have "\<dots> \<le> w x" using w[of x] setsum_le_suminf[of "\<lambda>i. \<bar>f i x\<bar>"] unfolding sums_iff by auto
  6.1970 +    finally show "norm (\<Sum>i<j. f i x) \<le> ?w x" by simp
  6.1971 +  qed
  6.1972 +
  6.1973 +  have 3: "integrable M ?w"
  6.1974 +  proof (rule integrable_monotone_convergence(1))
  6.1975 +    let ?F = "\<lambda>n y. (\<Sum>i<n. \<bar>f i y\<bar>)"
  6.1976 +    let ?w' = "\<lambda>n y. if y \<in> space M then ?F n y else 0"
  6.1977 +    have "\<And>n. integrable M (?F n)"
  6.1978 +      using integrable by (auto intro!: integrable_abs)
  6.1979 +    thus "\<And>n. integrable M (?w' n)" by (simp cong: integrable_cong)
  6.1980 +    show "AE x in M. mono (\<lambda>n. ?w' n x)"
  6.1981 +      by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
  6.1982 +    show "AE x in M. (\<lambda>n. ?w' n x) ----> ?w x"
  6.1983 +        using w by (simp_all add: tendsto_const sums_def)
  6.1984 +    have *: "\<And>n. integral\<^sup>L M (?w' n) = (\<Sum>i< n. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
  6.1985 +      using integrable by (simp add: integrable_abs cong: integral_cong)
  6.1986 +    from abs_sum
  6.1987 +    show "(\<lambda>i. integral\<^sup>L M (?w' i)) ----> x" unfolding * sums_def .
  6.1988 +  qed (simp add: w_borel measurable_If_set)
  6.1989 +
  6.1990 +  from summable[THEN summable_rabs_cancel]
  6.1991 +  have 4: "AE x in M. (\<lambda>n. \<Sum>i<n. f i x) ----> (\<Sum>i. f i x)"
  6.1992 +    by (auto intro: summable_LIMSEQ)
  6.1993 +
  6.1994 +  note int = integral_dominated_convergence(1,3)[OF _ _ 3 4 2]
  6.1995 +
  6.1996 +  from int show "integrable M ?S" by simp
  6.1997 +
  6.1998 +  show ?integral unfolding sums_def integral_setsum(1)[symmetric, OF integrable]
  6.1999 +    using int(2) by simp
  6.2000 +qed
  6.2001 +
  6.2002 +lemma integrable_mult_indicator:
  6.2003 +  fixes f :: "'a \<Rightarrow> real"
  6.2004 +  shows "A \<in> sets M \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. f x * indicator A x)"
  6.2005 +  by (rule integrable_bound[where f="\<lambda>x. \<bar>f x\<bar>"])
  6.2006 +     (auto intro: integrable_abs split: split_indicator)
  6.2007 +
  6.2008 +lemma tendsto_integral_at_top:
  6.2009 +  fixes f :: "real \<Rightarrow> real"
  6.2010 +  assumes M: "sets M = sets borel" and f[measurable]: "integrable M f"
  6.2011 +  shows "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> \<integral> x. f x \<partial>M) at_top"
  6.2012 +proof -
  6.2013 +  have M_measure[simp]: "borel_measurable M = borel_measurable borel"
  6.2014 +    using M by (simp add: sets_eq_imp_space_eq measurable_def)
  6.2015 +  { fix f :: "real \<Rightarrow> real" assume f: "integrable M f" "\<And>x. 0 \<le> f x"
  6.2016 +    then have [measurable]: "f \<in> borel_measurable borel"
  6.2017 +      by (simp add: real_integrable_def)
  6.2018 +    have "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> \<integral> x. f x \<partial>M) at_top"
  6.2019 +    proof (rule tendsto_at_topI_sequentially)
  6.2020 +      have int: "\<And>n. integrable M (\<lambda>x. f x * indicator {.. n} x)"
  6.2021 +        by (rule integrable_mult_indicator) (auto simp: M f)
  6.2022 +      show "(\<lambda>n. \<integral> x. f x * indicator {..real n} x \<partial>M) ----> integral\<^sup>L M f"
  6.2023 +      proof (rule integral_dominated_convergence)
  6.2024 +        { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
  6.2025 +            by (rule eventually_sequentiallyI[of "natceiling x"])
  6.2026 +               (auto split: split_indicator simp: natceiling_le_eq) }
  6.2027 +        from filterlim_cong[OF refl refl this]
  6.2028 +        show "AE x in M. (\<lambda>n. f x * indicator {..real n} x) ----> f x"
  6.2029 +          by (simp add: tendsto_const)
  6.2030 +        show "\<And>j. AE x in M. norm (f x * indicator {.. j} x) \<le> f x"
  6.2031 +          using f(2) by (intro AE_I2) (auto split: split_indicator)
  6.2032 +      qed (simp | fact)+
  6.2033 +      show "mono (\<lambda>y. \<integral> x. f x * indicator {..y} x \<partial>M)"
  6.2034 +        by (intro monoI integral_mono int) (auto split: split_indicator intro: f)
  6.2035 +    qed }
  6.2036 +  note nonneg = this
  6.2037 +  let ?P = "\<lambda>y. \<integral> x. max 0 (f x) * indicator {..y} x \<partial>M"
  6.2038 +  let ?N = "\<lambda>y. \<integral> x. max 0 (- f x) * indicator {..y} x \<partial>M"
  6.2039 +  let ?p = "integral\<^sup>L M (\<lambda>x. max 0 (f x))"
  6.2040 +  let ?n = "integral\<^sup>L M (\<lambda>x. max 0 (- f x))"
  6.2041 +  have "(?P ---> ?p) at_top" "(?N ---> ?n) at_top"
  6.2042 +    by (auto intro!: nonneg integrable_max f)
  6.2043 +  note tendsto_diff[OF this]
  6.2044 +  also have "(\<lambda>y. ?P y - ?N y) = (\<lambda>y. \<integral> x. f x * indicator {..y} x \<partial>M)"
  6.2045 +    by (subst integral_diff[symmetric])
  6.2046 +       (auto intro!: integrable_mult_indicator integrable_max f integral_cong
  6.2047 +             simp: M split: split_max)
  6.2048 +  also have "?p - ?n = integral\<^sup>L M f"
  6.2049 +    by (subst integral_diff[symmetric])
  6.2050 +       (auto intro!: integrable_max f integral_cong simp: M split: split_max)
  6.2051 +  finally show ?thesis .
  6.2052 +qed
  6.2053 +
  6.2054 +lemma
  6.2055 +  fixes f :: "real \<Rightarrow> real"
  6.2056 +  assumes M: "sets M = sets borel"
  6.2057 +  assumes nonneg: "AE x in M. 0 \<le> f x"
  6.2058 +  assumes borel: "f \<in> borel_measurable borel"
  6.2059 +  assumes int: "\<And>y. integrable M (\<lambda>x. f x * indicator {.. y} x)"
  6.2060 +  assumes conv: "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> x) at_top"
  6.2061 +  shows has_bochner_integral_monotone_convergence_at_top: "has_bochner_integral M f x"
  6.2062 +    and integrable_monotone_convergence_at_top: "integrable M f"
  6.2063 +    and integral_monotone_convergence_at_top:"integral\<^sup>L M f = x"
  6.2064 +proof -
  6.2065 +  from nonneg have "AE x in M. mono (\<lambda>n::nat. f x * indicator {..real n} x)"
  6.2066 +    by (auto split: split_indicator intro!: monoI)
  6.2067 +  { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
  6.2068 +      by (rule eventually_sequentiallyI[of "natceiling x"])
  6.2069 +         (auto split: split_indicator simp: natceiling_le_eq) }
  6.2070 +  from filterlim_cong[OF refl refl this]
  6.2071 +  have "AE x in M. (\<lambda>i. f x * indicator {..real i} x) ----> f x"
  6.2072 +    by (simp add: tendsto_const)
  6.2073 +  have "(\<lambda>i. \<integral> x. f x * indicator {..real i} x \<partial>M) ----> x"
  6.2074 +    using conv filterlim_real_sequentially by (rule filterlim_compose)
  6.2075 +  have M_measure[simp]: "borel_measurable M = borel_measurable borel"
  6.2076 +    using M by (simp add: sets_eq_imp_space_eq measurable_def)
  6.2077 +  have "f \<in> borel_measurable M"
  6.2078 +    using borel by simp
  6.2079 +  show "has_bochner_integral M f x"
  6.2080 +    by (rule has_bochner_integral_monotone_convergence) fact+
  6.2081 +  then show "integrable M f" "integral\<^sup>L M f = x"
  6.2082 +    by (auto simp: _has_bochner_integral_iff)
  6.2083 +qed
  6.2084 +
  6.2085 +
  6.2086 +section "Lebesgue integration on countable spaces"
  6.2087 +
  6.2088 +lemma integral_on_countable:
  6.2089 +  fixes f :: "real \<Rightarrow> real"
  6.2090 +  assumes f: "f \<in> borel_measurable M"
  6.2091 +  and bij: "bij_betw enum S (f ` space M)"
  6.2092 +  and enum_zero: "enum ` (-S) \<subseteq> {0}"
  6.2093 +  and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> (emeasure M) (f -` {x} \<inter> space M) \<noteq> \<infinity>"
  6.2094 +  and abs_summable: "summable (\<lambda>r. \<bar>enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))\<bar>)"
  6.2095 +  shows "integrable M f"
  6.2096 +  and "(\<lambda>r. enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))) sums integral\<^sup>L M f" (is ?sums)
  6.2097 +proof -
  6.2098 +  let ?A = "\<lambda>r. f -` {enum r} \<inter> space M"
  6.2099 +  let ?F = "\<lambda>r x. enum r * indicator (?A r) x"
  6.2100 +  have enum_eq: "\<And>r. enum r * real ((emeasure M) (?A r)) = integral\<^sup>L M (?F r)"
  6.2101 +    using f fin by (simp add: measure_def cong: disj_cong)
  6.2102 +
  6.2103 +  { fix x assume "x \<in> space M"
  6.2104 +    hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
  6.2105 +    then obtain i where "i\<in>S" "enum i = f x" by auto
  6.2106 +    have F: "\<And>j. ?F j x = (if j = i then f x else 0)"
  6.2107 +    proof cases
  6.2108 +      fix j assume "j = i"
  6.2109 +      thus "?thesis j" using `x \<in> space M` `enum i = f x` by (simp add: indicator_def)
  6.2110 +    next
  6.2111 +      fix j assume "j \<noteq> i"
  6.2112 +      show "?thesis j" using bij `i \<in> S` `j \<noteq> i` `enum i = f x` enum_zero
  6.2113 +        by (cases "j \<in> S") (auto simp add: indicator_def bij_betw_def inj_on_def)
  6.2114 +    qed
  6.2115 +    hence F_abs: "\<And>j. \<bar>if j = i then f x else 0\<bar> = (if j = i then \<bar>f x\<bar> else 0)" by auto
  6.2116 +    have "(\<lambda>i. ?F i x) sums f x"
  6.2117 +         "(\<lambda>i. \<bar>?F i x\<bar>) sums \<bar>f x\<bar>"
  6.2118 +      by (auto intro!: sums_single simp: F F_abs) }
  6.2119 +  note F_sums_f = this(1) and F_abs_sums_f = this(2)
  6.2120 +
  6.2121 +  have int_f: "integral\<^sup>L M f = (\<integral>x. (\<Sum>r. ?F r x) \<partial>M)" "integrable M f = integrable M (\<lambda>x. \<Sum>r. ?F r x)"
  6.2122 +    using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
  6.2123 +
  6.2124 +  { fix r
  6.2125 +    have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
  6.2126 +      by (auto simp: indicator_def intro!: integral_cong)
  6.2127 +    also have "\<dots> = \<bar>enum r\<bar> * real ((emeasure M) (?A r))"
  6.2128 +      using f fin by (simp add: measure_def cong: disj_cong)
  6.2129 +    finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real ((emeasure M) (?A r))\<bar>"
  6.2130 +      using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
  6.2131 +  note int_abs_F = this
  6.2132 +
  6.2133 +  have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
  6.2134 +    using f fin by auto
  6.2135 +
  6.2136 +  have 2: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>?F i x\<bar>)"
  6.2137 +    using F_abs_sums_f unfolding sums_iff by auto
  6.2138 +
  6.2139 +  from integral_sums(2)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  6.2140 +  show ?sums unfolding enum_eq int_f by simp
  6.2141 +
  6.2142 +  from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
  6.2143 +  show "integrable M f" unfolding int_f by simp
  6.2144 +qed
  6.2145 +
  6.2146 +subsection {* Product measure *}
  6.2147 +
  6.2148 +lemma (in sigma_finite_measure) borel_measurable_lebesgue_integrable[measurable (raw)]:
  6.2149 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2150 +  assumes [measurable]: "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  6.2151 +  shows "Measurable.pred N (\<lambda>x. integrable M (f x))"
  6.2152 +proof -
  6.2153 +  have [simp]: "\<And>x. x \<in> space N \<Longrightarrow> integrable M (f x) \<longleftrightarrow> (\<integral>\<^sup>+y. norm (f x y) \<partial>M) < \<infinity>"
  6.2154 +    unfolding integrable_iff_bounded by simp
  6.2155 +  show ?thesis
  6.2156 +    by (simp cong: measurable_cong)
  6.2157 +qed
  6.2158 +
  6.2159 +lemma (in sigma_finite_measure) measurable_measure[measurable (raw)]:
  6.2160 +  "(\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M) \<Longrightarrow>
  6.2161 +    {x \<in> space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M) \<Longrightarrow>
  6.2162 +    (\<lambda>x. measure M (A x)) \<in> borel_measurable N"
  6.2163 +  unfolding measure_def by (intro measurable_emeasure borel_measurable_real_of_ereal)
  6.2164 +
  6.2165 +lemma Collect_subset [simp]: "{x\<in>A. P x} \<subseteq> A" by auto
  6.2166 +
  6.2167 +lemma (in sigma_finite_measure) borel_measurable_lebesgue_integral[measurable (raw)]:
  6.2168 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2169 +  assumes f[measurable]: "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  6.2170 +  shows "(\<lambda>x. \<integral>y. f x y \<partial>M) \<in> borel_measurable N"
  6.2171 +proof -
  6.2172 +  from borel_measurable_implies_sequence_metric[OF f, of 0] guess s ..
  6.2173 +  then have s: "\<And>i. simple_function (N \<Otimes>\<^sub>M M) (s i)"
  6.2174 +    "\<And>x y. x \<in> space N \<Longrightarrow> y \<in> space M \<Longrightarrow> (\<lambda>i. s i (x, y)) ----> f x y"
  6.2175 +    "\<And>i x y. x \<in> space N \<Longrightarrow> y \<in> space M \<Longrightarrow> norm (s i (x, y)) \<le> 2 * norm (f x y)"
  6.2176 +    by (auto simp: space_pair_measure norm_conv_dist)
  6.2177 +
  6.2178 +  have [measurable]: "\<And>i. s i \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  6.2179 +    by (rule borel_measurable_simple_function) fact
  6.2180 +
  6.2181 +  have "\<And>i. s i \<in> measurable (N \<Otimes>\<^sub>M M) (count_space UNIV)"
  6.2182 +    by (rule measurable_simple_function) fact
  6.2183 +
  6.2184 +  def f' \<equiv> "\<lambda>i x. if integrable M (f x)
  6.2185 +    then simple_bochner_integral M (\<lambda>y. s i (x, y))
  6.2186 +    else (THE x. False)"
  6.2187 +
  6.2188 +  { fix i x assume "x \<in> space N"
  6.2189 +    then have "simple_bochner_integral M (\<lambda>y. s i (x, y)) =
  6.2190 +      (\<Sum>z\<in>s i ` (space N \<times> space M). measure M {y \<in> space M. s i (x, y) = z} *\<^sub>R z)"
  6.2191 +      using s(1)[THEN simple_functionD(1)]
  6.2192 +      unfolding simple_bochner_integral_def
  6.2193 +      by (intro setsum_mono_zero_cong_left)
  6.2194 +         (auto simp: eq_commute space_pair_measure image_iff cong: conj_cong) }
  6.2195 +  note eq = this
  6.2196 +
  6.2197 +  show ?thesis
  6.2198 +  proof (rule borel_measurable_LIMSEQ_metric)
  6.2199 +    fix i show "f' i \<in> borel_measurable N"
  6.2200 +      unfolding f'_def by (simp_all add: eq cong: measurable_cong if_cong)
  6.2201 +  next
  6.2202 +    fix x assume x: "x \<in> space N"
  6.2203 +    { assume int_f: "integrable M (f x)"
  6.2204 +      have int_2f: "integrable M (\<lambda>y. 2 * norm (f x y))"
  6.2205 +        by (intro integrable_norm integrable_mult_right int_f)
  6.2206 +      have "(\<lambda>i. integral\<^sup>L M (\<lambda>y. s i (x, y))) ----> integral\<^sup>L M (f x)"
  6.2207 +      proof (rule integral_dominated_convergence)
  6.2208 +        from int_f show "f x \<in> borel_measurable M" by auto
  6.2209 +        show "\<And>i. (\<lambda>y. s i (x, y)) \<in> borel_measurable M"
  6.2210 +          using x by simp
  6.2211 +        show "AE xa in M. (\<lambda>i. s i (x, xa)) ----> f x xa"
  6.2212 +          using x s(2) by auto
  6.2213 +        show "\<And>i. AE xa in M. norm (s i (x, xa)) \<le> 2 * norm (f x xa)"
  6.2214 +          using x s(3) by auto
  6.2215 +      qed fact
  6.2216 +      moreover
  6.2217 +      { fix i
  6.2218 +        have "simple_bochner_integrable M (\<lambda>y. s i (x, y))"
  6.2219 +        proof (rule simple_bochner_integrableI_bounded)
  6.2220 +          have "(\<lambda>y. s i (x, y)) ` space M \<subseteq> s i ` (space N \<times> space M)"
  6.2221 +            using x by auto
  6.2222 +          then show "simple_function M (\<lambda>y. s i (x, y))"
  6.2223 +            using simple_functionD(1)[OF s(1), of i] x
  6.2224 +            by (intro simple_function_borel_measurable)
  6.2225 +               (auto simp: space_pair_measure dest: finite_subset)
  6.2226 +          have "(\<integral>\<^sup>+ y. ereal (norm (s i (x, y))) \<partial>M) \<le> (\<integral>\<^sup>+ y. 2 * norm (f x y) \<partial>M)"
  6.2227 +            using x s by (intro positive_integral_mono) auto
  6.2228 +          also have "(\<integral>\<^sup>+ y. 2 * norm (f x y) \<partial>M) < \<infinity>"
  6.2229 +            using int_2f by (simp add: integrable_iff_bounded)
  6.2230 +          finally show "(\<integral>\<^sup>+ xa. ereal (norm (s i (x, xa))) \<partial>M) < \<infinity>" .
  6.2231 +        qed
  6.2232 +        then have "integral\<^sup>L M (\<lambda>y. s i (x, y)) = simple_bochner_integral M (\<lambda>y. s i (x, y))"
  6.2233 +          by (rule simple_bochner_integrable_eq_integral[symmetric]) }
  6.2234 +      ultimately have "(\<lambda>i. simple_bochner_integral M (\<lambda>y. s i (x, y))) ----> integral\<^sup>L M (f x)"
  6.2235 +        by simp }
  6.2236 +    then 
  6.2237 +    show "(\<lambda>i. f' i x) ----> integral\<^sup>L M (f x)"
  6.2238 +      unfolding f'_def
  6.2239 +      by (cases "integrable M (f x)") (simp_all add: not_integrable_integral_eq tendsto_const)
  6.2240 +  qed
  6.2241 +qed
  6.2242 +
  6.2243 +lemma (in pair_sigma_finite) integrable_product_swap:
  6.2244 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2245 +  assumes "integrable (M1 \<Otimes>\<^sub>M M2) f"
  6.2246 +  shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x))"
  6.2247 +proof -
  6.2248 +  interpret Q: pair_sigma_finite M2 M1 by default
  6.2249 +  have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  6.2250 +  show ?thesis unfolding *
  6.2251 +    by (rule integrable_distr[OF measurable_pair_swap'])
  6.2252 +       (simp add: distr_pair_swap[symmetric] assms)
  6.2253 +qed
  6.2254 +
  6.2255 +lemma (in pair_sigma_finite) integrable_product_swap_iff:
  6.2256 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2257 +  shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable (M1 \<Otimes>\<^sub>M M2) f"
  6.2258 +proof -
  6.2259 +  interpret Q: pair_sigma_finite M2 M1 by default
  6.2260 +  from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
  6.2261 +  show ?thesis by auto
  6.2262 +qed
  6.2263 +
  6.2264 +lemma (in pair_sigma_finite) integral_product_swap:
  6.2265 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2266 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  6.2267 +  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  6.2268 +proof -
  6.2269 +  have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  6.2270 +  show ?thesis unfolding *
  6.2271 +    by (simp add: integral_distr[symmetric, OF measurable_pair_swap' f] distr_pair_swap[symmetric])
  6.2272 +qed
  6.2273 +
  6.2274 +lemma (in pair_sigma_finite) emeasure_pair_measure_finite:
  6.2275 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" and finite: "emeasure (M1 \<Otimes>\<^sub>M M2) A < \<infinity>"
  6.2276 +  shows "AE x in M1. emeasure M2 {y\<in>space M2. (x, y) \<in> A} < \<infinity>"
  6.2277 +proof -
  6.2278 +  from M2.emeasure_pair_measure_alt[OF A] finite
  6.2279 +  have "(\<integral>\<^sup>+ x. emeasure M2 (Pair x -` A) \<partial>M1) \<noteq> \<infinity>"
  6.2280 +    by simp
  6.2281 +  then have "AE x in M1. emeasure M2 (Pair x -` A) \<noteq> \<infinity>"
  6.2282 +    by (rule positive_integral_PInf_AE[rotated]) (intro M2.measurable_emeasure_Pair A)
  6.2283 +  moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> Pair x -` A = {y\<in>space M2. (x, y) \<in> A}"
  6.2284 +    using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
  6.2285 +  ultimately show ?thesis by auto
  6.2286 +qed
  6.2287 +
  6.2288 +lemma (in pair_sigma_finite) AE_integrable_fst':
  6.2289 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2290 +  assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  6.2291 +  shows "AE x in M1. integrable M2 (\<lambda>y. f (x, y))"
  6.2292 +proof -
  6.2293 +  have "(\<integral>\<^sup>+x. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<partial>M1) = (\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2))"
  6.2294 +    by (rule M2.positive_integral_fst) simp
  6.2295 +  also have "(\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2)) \<noteq> \<infinity>"
  6.2296 +    using f unfolding integrable_iff_bounded by simp
  6.2297 +  finally have "AE x in M1. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
  6.2298 +    by (intro positive_integral_PInf_AE M2.borel_measurable_positive_integral )
  6.2299 +       (auto simp: measurable_split_conv)
  6.2300 +  with AE_space show ?thesis
  6.2301 +    by eventually_elim
  6.2302 +       (auto simp: integrable_iff_bounded measurable_compose[OF _ borel_measurable_integrable[OF f]])
  6.2303 +qed
  6.2304 +
  6.2305 +lemma (in pair_sigma_finite) integrable_fst':
  6.2306 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2307 +  assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  6.2308 +  shows "integrable M1 (\<lambda>x. \<integral>y. f (x, y) \<partial>M2)"
  6.2309 +  unfolding integrable_iff_bounded
  6.2310 +proof
  6.2311 +  show "(\<lambda>x. \<integral> y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
  6.2312 +    by (rule M2.borel_measurable_lebesgue_integral) simp
  6.2313 +  have "(\<integral>\<^sup>+ x. ereal (norm (\<integral> y. f (x, y) \<partial>M2)) \<partial>M1) \<le> (\<integral>\<^sup>+x. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<partial>M1)"
  6.2314 +    using AE_integrable_fst'[OF f] by (auto intro!: positive_integral_mono_AE integral_norm_bound_ereal)
  6.2315 +  also have "(\<integral>\<^sup>+x. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<partial>M1) = (\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2))"
  6.2316 +    by (rule M2.positive_integral_fst) simp
  6.2317 +  also have "(\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2)) < \<infinity>"
  6.2318 +    using f unfolding integrable_iff_bounded by simp
  6.2319 +  finally show "(\<integral>\<^sup>+ x. ereal (norm (\<integral> y. f (x, y) \<partial>M2)) \<partial>M1) < \<infinity>" .
  6.2320 +qed
  6.2321 +
  6.2322 +lemma (in pair_sigma_finite) integral_fst':
  6.2323 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2324 +  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  6.2325 +  shows "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  6.2326 +using f proof induct
  6.2327 +  case (base A c)
  6.2328 +  have A[measurable]: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" by fact
  6.2329 +
  6.2330 +  have eq: "\<And>x y. x \<in> space M1 \<Longrightarrow> indicator A (x, y) = indicator {y\<in>space M2. (x, y) \<in> A} y"
  6.2331 +    using sets.sets_into_space[OF A] by (auto split: split_indicator simp: space_pair_measure)
  6.2332 +
  6.2333 +  have int_A: "integrable (M1 \<Otimes>\<^sub>M M2) (indicator A :: _ \<Rightarrow> real)"
  6.2334 +    using base by (rule integrable_real_indicator)
  6.2335 +
  6.2336 +  have "(\<integral> x. \<integral> y. indicator A (x, y) *\<^sub>R c \<partial>M2 \<partial>M1) = (\<integral>x. measure M2 {y\<in>space M2. (x, y) \<in> A} *\<^sub>R c \<partial>M1)"
  6.2337 +  proof (intro integral_cong_AE, simp, simp)
  6.2338 +    from AE_integrable_fst'[OF int_A] AE_space
  6.2339 +    show "AE x in M1. (\<integral>y. indicator A (x, y) *\<^sub>R c \<partial>M2) = measure M2 {y\<in>space M2. (x, y) \<in> A} *\<^sub>R c"
  6.2340 +      by eventually_elim (simp add: eq integrable_indicator_iff)
  6.2341 +  qed
  6.2342 +  also have "\<dots> = measure (M1 \<Otimes>\<^sub>M M2) A *\<^sub>R c"
  6.2343 +  proof (subst integral_scaleR_left)
  6.2344 +    have "(\<integral>\<^sup>+x. ereal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1) =
  6.2345 +      (\<integral>\<^sup>+x. emeasure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1)"
  6.2346 +      using emeasure_pair_measure_finite[OF base]
  6.2347 +      by (intro positive_integral_cong_AE, eventually_elim) (simp add: emeasure_eq_ereal_measure)
  6.2348 +    also have "\<dots> = emeasure (M1 \<Otimes>\<^sub>M M2) A"
  6.2349 +      using sets.sets_into_space[OF A]
  6.2350 +      by (subst M2.emeasure_pair_measure_alt)
  6.2351 +         (auto intro!: positive_integral_cong arg_cong[where f="emeasure M2"] simp: space_pair_measure)
  6.2352 +    finally have *: "(\<integral>\<^sup>+x. ereal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1) = emeasure (M1 \<Otimes>\<^sub>M M2) A" .
  6.2353 +
  6.2354 +    from base * show "integrable M1 (\<lambda>x. measure M2 {y \<in> space M2. (x, y) \<in> A})"
  6.2355 +      by (simp add: measure_nonneg integrable_iff_bounded)
  6.2356 +    then have "(\<integral>x. measure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1) = 
  6.2357 +      (\<integral>\<^sup>+x. ereal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1)"
  6.2358 +      by (rule positive_integral_eq_integral[symmetric]) (simp add: measure_nonneg)
  6.2359 +    also note *
  6.2360 +    finally show "(\<integral>x. measure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1) *\<^sub>R c = measure (M1 \<Otimes>\<^sub>M M2) A *\<^sub>R c"
  6.2361 +      using base by (simp add: emeasure_eq_ereal_measure)
  6.2362 +  qed
  6.2363 +  also have "\<dots> = (\<integral> a. indicator A a *\<^sub>R c \<partial>(M1 \<Otimes>\<^sub>M M2))"
  6.2364 +    using base by simp
  6.2365 +  finally show ?case .
  6.2366 +next
  6.2367 +  case (add f g)
  6.2368 +  then have [measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" "g \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  6.2369 +    by auto
  6.2370 +  have "(\<integral> x. \<integral> y. f (x, y) + g (x, y) \<partial>M2 \<partial>M1) = 
  6.2371 +    (\<integral> x. (\<integral> y. f (x, y) \<partial>M2) + (\<integral> y. g (x, y) \<partial>M2) \<partial>M1)"
  6.2372 +    apply (rule integral_cong_AE)
  6.2373 +    apply simp_all
  6.2374 +    using AE_integrable_fst'[OF add(1)] AE_integrable_fst'[OF add(3)]
  6.2375 +    apply eventually_elim
  6.2376 +    apply simp
  6.2377 +    done 
  6.2378 +  also have "\<dots> = (\<integral> x. f x \<partial>(M1 \<Otimes>\<^sub>M M2)) + (\<integral> x. g x \<partial>(M1 \<Otimes>\<^sub>M M2))"
  6.2379 +    using integrable_fst'[OF add(1)] integrable_fst'[OF add(3)] add(2,4) by simp
  6.2380 +  finally show ?case
  6.2381 +    using add by simp
  6.2382 +next
  6.2383 +  case (lim f s)
  6.2384 +  then have [measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" "\<And>i. s i \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  6.2385 +    by auto
  6.2386 +  
  6.2387 +  show ?case
  6.2388 +  proof (rule LIMSEQ_unique)
  6.2389 +    show "(\<lambda>i. integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (s i)) ----> integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  6.2390 +    proof (rule integral_dominated_convergence)
  6.2391 +      show "integrable (M1 \<Otimes>\<^sub>M M2) (\<lambda>x. 2 * norm (f x))"
  6.2392 +        using lim(5) by (auto intro: integrable_norm)
  6.2393 +    qed (insert lim, auto)
  6.2394 +    have "(\<lambda>i. \<integral> x. \<integral> y. s i (x, y) \<partial>M2 \<partial>M1) ----> \<integral> x. \<integral> y. f (x, y) \<partial>M2 \<partial>M1"
  6.2395 +    proof (rule integral_dominated_convergence)
  6.2396 +      have "AE x in M1. \<forall>i. integrable M2 (\<lambda>y. s i (x, y))"
  6.2397 +        unfolding AE_all_countable using AE_integrable_fst'[OF lim(1)] ..
  6.2398 +      with AE_space AE_integrable_fst'[OF lim(5)]
  6.2399 +      show "AE x in M1. (\<lambda>i. \<integral> y. s i (x, y) \<partial>M2) ----> \<integral> y. f (x, y) \<partial>M2"
  6.2400 +      proof eventually_elim
  6.2401 +        fix x assume x: "x \<in> space M1" and
  6.2402 +          s: "\<forall>i. integrable M2 (\<lambda>y. s i (x, y))" and f: "integrable M2 (\<lambda>y. f (x, y))"
  6.2403 +        show "(\<lambda>i. \<integral> y. s i (x, y) \<partial>M2) ----> \<integral> y. f (x, y) \<partial>M2"
  6.2404 +        proof (rule integral_dominated_convergence)
  6.2405 +          show "integrable M2 (\<lambda>y. 2 * norm (f (x, y)))"
  6.2406 +             using f by (auto intro: integrable_norm)
  6.2407 +          show "AE xa in M2. (\<lambda>i. s i (x, xa)) ----> f (x, xa)"
  6.2408 +            using x lim(3) by (auto simp: space_pair_measure)
  6.2409 +          show "\<And>i. AE xa in M2. norm (s i (x, xa)) \<le> 2 * norm (f (x, xa))"
  6.2410 +            using x lim(4) by (auto simp: space_pair_measure)
  6.2411 +        qed (insert x, measurable)
  6.2412 +      qed
  6.2413 +      show "integrable M1 (\<lambda>x. (\<integral> y. 2 * norm (f (x, y)) \<partial>M2))"
  6.2414 +        by (intro integrable_mult_right integrable_norm integrable_fst' lim)
  6.2415 +      fix i show "AE x in M1. norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral> y. 2 * norm (f (x, y)) \<partial>M2)"
  6.2416 +        using AE_space AE_integrable_fst'[OF lim(1), of i] AE_integrable_fst'[OF lim(5)]
  6.2417 +      proof eventually_elim 
  6.2418 +        fix x assume x: "x \<in> space M1"
  6.2419 +          and s: "integrable M2 (\<lambda>y. s i (x, y))" and f: "integrable M2 (\<lambda>y. f (x, y))"
  6.2420 +        from s have "norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral>\<^sup>+y. norm (s i (x, y)) \<partial>M2)"
  6.2421 +          by (rule integral_norm_bound_ereal)
  6.2422 +        also have "\<dots> \<le> (\<integral>\<^sup>+y. 2 * norm (f (x, y)) \<partial>M2)"
  6.2423 +          using x lim by (auto intro!: positive_integral_mono simp: space_pair_measure)
  6.2424 +        also have "\<dots> = (\<integral>y. 2 * norm (f (x, y)) \<partial>M2)"
  6.2425 +          using f by (intro positive_integral_eq_integral) auto
  6.2426 +        finally show "norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral> y. 2 * norm (f (x, y)) \<partial>M2)"
  6.2427 +          by simp
  6.2428 +      qed
  6.2429 +    qed simp_all
  6.2430 +    then show "(\<lambda>i. integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (s i)) ----> \<integral> x. \<integral> y. f (x, y) \<partial>M2 \<partial>M1"
  6.2431 +      using lim by simp
  6.2432 +  qed
  6.2433 +qed
  6.2434 +
  6.2435 +lemma (in pair_sigma_finite)
  6.2436 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2437 +  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) (split f)"
  6.2438 +  shows AE_integrable_fst: "AE x in M1. integrable M2 (\<lambda>y. f x y)" (is "?AE")
  6.2439 +    and integrable_fst: "integrable M1 (\<lambda>x. \<integral>y. f x y \<partial>M2)" (is "?INT")
  6.2440 +    and integral_fst: "(\<integral>x. (\<integral>y. f x y \<partial>M2) \<partial>M1) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). f x y)" (is "?EQ")
  6.2441 +  using AE_integrable_fst'[OF f] integrable_fst'[OF f] integral_fst'[OF f] by auto
  6.2442 +
  6.2443 +lemma (in pair_sigma_finite)
  6.2444 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2445 +  assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) (split f)"
  6.2446 +  shows AE_integrable_snd: "AE y in M2. integrable M1 (\<lambda>x. f x y)" (is "?AE")
  6.2447 +    and integrable_snd: "integrable M2 (\<lambda>y. \<integral>x. f x y \<partial>M1)" (is "?INT")
  6.2448 +    and integral_snd: "(\<integral>y. (\<integral>x. f x y \<partial>M1) \<partial>M2) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (split f)" (is "?EQ")
  6.2449 +proof -
  6.2450 +  interpret Q: pair_sigma_finite M2 M1 by default
  6.2451 +  have Q_int: "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x, y). f y x)"
  6.2452 +    using f unfolding integrable_product_swap_iff[symmetric] by simp
  6.2453 +  show ?AE  using Q.AE_integrable_fst'[OF Q_int] by simp
  6.2454 +  show ?INT using Q.integrable_fst'[OF Q_int] by simp
  6.2455 +  show ?EQ using Q.integral_fst'[OF Q_int]
  6.2456 +    using integral_product_swap[of "split f"] by simp
  6.2457 +qed
  6.2458 +
  6.2459 +lemma (in pair_sigma_finite) Fubini_integral:
  6.2460 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _ :: {banach, second_countable_topology}"
  6.2461 +  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) (split f)"
  6.2462 +  shows "(\<integral>y. (\<integral>x. f x y \<partial>M1) \<partial>M2) = (\<integral>x. (\<integral>y. f x y \<partial>M2) \<partial>M1)"
  6.2463 +  unfolding integral_snd[OF assms] integral_fst[OF assms] ..
  6.2464 +
  6.2465 +lemma (in product_sigma_finite) product_integral_singleton:
  6.2466 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2467 +  shows "f \<in> borel_measurable (M i) \<Longrightarrow> (\<integral>x. f (x i) \<partial>Pi\<^sub>M {i} M) = integral\<^sup>L (M i) f"
  6.2468 +  apply (subst distr_singleton[symmetric])
  6.2469 +  apply (subst integral_distr)
  6.2470 +  apply simp_all
  6.2471 +  done
  6.2472 +
  6.2473 +lemma (in product_sigma_finite) product_integral_fold:
  6.2474 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2475 +  assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  6.2476 +  and f: "integrable (Pi\<^sub>M (I \<union> J) M) f"
  6.2477 +  shows "integral\<^sup>L (Pi\<^sub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I J (x, y)) \<partial>Pi\<^sub>M J M) \<partial>Pi\<^sub>M I M)"
  6.2478 +proof -
  6.2479 +  interpret I: finite_product_sigma_finite M I by default fact
  6.2480 +  interpret J: finite_product_sigma_finite M J by default fact
  6.2481 +  have "finite (I \<union> J)" using fin by auto
  6.2482 +  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
  6.2483 +  interpret P: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" by default
  6.2484 +  let ?M = "merge I J"
  6.2485 +  let ?f = "\<lambda>x. f (?M x)"
  6.2486 +  from f have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (I \<union> J) M)"
  6.2487 +    by auto
  6.2488 +  have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
  6.2489 +    using measurable_comp[OF measurable_merge f_borel] by (simp add: comp_def)
  6.2490 +  have f_int: "integrable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) ?f"
  6.2491 +    by (rule integrable_distr[OF measurable_merge]) (simp add: distr_merge[OF IJ fin] f)
  6.2492 +  show ?thesis
  6.2493 +    apply (subst distr_merge[symmetric, OF IJ fin])
  6.2494 +    apply (subst integral_distr[OF measurable_merge f_borel])
  6.2495 +    apply (subst P.integral_fst'[symmetric, OF f_int])
  6.2496 +    apply simp
  6.2497 +    done
  6.2498 +qed
  6.2499 +
  6.2500 +lemma (in product_sigma_finite) product_integral_insert:
  6.2501 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  6.2502 +  assumes I: "finite I" "i \<notin> I"
  6.2503 +    and f: "integrable (Pi\<^sub>M (insert i I) M) f"
  6.2504 +  shows "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = (\<integral>x. (\<integral>y. f (x(i:=y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
  6.2505 +proof -
  6.2506 +  have "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = integral\<^sup>L (Pi\<^sub>M (I \<union> {i}) M) f"
  6.2507 +    by simp
  6.2508 +  also have "\<dots> = (\<integral>x. (\<integral>y. f (merge I {i} (x,y)) \<partial>Pi\<^sub>M {i} M) \<partial>Pi\<^sub>M I M)"
  6.2509 +    using f I by (intro product_integral_fold) auto
  6.2510 +  also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
  6.2511 +  proof (rule integral_cong[OF refl], subst product_integral_singleton[symmetric])
  6.2512 +    fix x assume x: "x \<in> space (Pi\<^sub>M I M)"
  6.2513 +    have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  6.2514 +      using f by auto
  6.2515 +    show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
  6.2516 +      using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
  6.2517 +      unfolding comp_def .
  6.2518 +    from x I show "(\<integral> y. f (merge I {i} (x,y)) \<partial>Pi\<^sub>M {i} M) = (\<integral> xa. f (x(i := xa i)) \<partial>Pi\<^sub>M {i} M)"
  6.2519 +      by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def PiE_def)
  6.2520 +  qed
  6.2521 +  finally show ?thesis .
  6.2522 +qed
  6.2523 +
  6.2524 +lemma (in product_sigma_finite) product_integrable_setprod:
  6.2525 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> _::{real_normed_field,banach,second_countable_topology}"
  6.2526 +  assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  6.2527 +  shows "integrable (Pi\<^sub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
  6.2528 +proof (unfold integrable_iff_bounded, intro conjI)
  6.2529 +  interpret finite_product_sigma_finite M I by default fact
  6.2530 +  show "?f \<in> borel_measurable (Pi\<^sub>M I M)"
  6.2531 +    using assms by simp
  6.2532 +  have "(\<integral>\<^sup>+ x. ereal (norm (\<Prod>i\<in>I. f i (x i))) \<partial>Pi\<^sub>M I M) = 
  6.2533 +      (\<integral>\<^sup>+ x. (\<Prod>i\<in>I. ereal (norm (f i (x i)))) \<partial>Pi\<^sub>M I M)"
  6.2534 +    by (simp add: setprod_norm setprod_ereal)
  6.2535 +  also have "\<dots> = (\<Prod>i\<in>I. \<integral>\<^sup>+ x. ereal (norm (f i x)) \<partial>M i)"
  6.2536 +    using assms by (intro product_positive_integral_setprod) auto
  6.2537 +  also have "\<dots> < \<infinity>"
  6.2538 +    using integrable by (simp add: setprod_PInf positive_integral_positive integrable_iff_bounded)
  6.2539 +  finally show "(\<integral>\<^sup>+ x. ereal (norm (\<Prod>i\<in>I. f i (x i))) \<partial>Pi\<^sub>M I M) < \<infinity>" .
  6.2540 +qed
  6.2541 +
  6.2542 +lemma (in product_sigma_finite) product_integral_setprod:
  6.2543 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> _::{real_normed_field,banach,second_countable_topology}"
  6.2544 +  assumes "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  6.2545 +  shows "(\<integral>x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^sub>M I M) = (\<Prod>i\<in>I. integral\<^sup>L (M i) (f i))"
  6.2546 +using assms proof induct
  6.2547 +  case empty
  6.2548 +  interpret finite_measure "Pi\<^sub>M {} M"
  6.2549 +    by rule (simp add: space_PiM)
  6.2550 +  show ?case by (simp add: space_PiM measure_def)
  6.2551 +next
  6.2552 +  case (insert i I)
  6.2553 +  then have iI: "finite (insert i I)" by auto
  6.2554 +  then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  6.2555 +    integrable (Pi\<^sub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  6.2556 +    by (intro product_integrable_setprod insert(4)) (auto intro: finite_subset)
  6.2557 +  interpret I: finite_product_sigma_finite M I by default fact
  6.2558 +  have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  6.2559 +    using `i \<notin> I` by (auto intro!: setprod_cong)
  6.2560 +  show ?case
  6.2561 +    unfolding product_integral_insert[OF insert(1,2) prod[OF subset_refl]]
  6.2562 +    by (simp add: * insert prod subset_insertI)
  6.2563 +qed
  6.2564 +
  6.2565 +lemma integrable_subalgebra:
  6.2566 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.2567 +  assumes borel: "f \<in> borel_measurable N"
  6.2568 +  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  6.2569 +  shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  6.2570 +proof -
  6.2571 +  have "f \<in> borel_measurable M"
  6.2572 +    using assms by (auto simp: measurable_def)
  6.2573 +  with assms show ?thesis
  6.2574 +    using assms by (auto simp: integrable_iff_bounded positive_integral_subalgebra)
  6.2575 +qed
  6.2576 +
  6.2577 +lemma integral_subalgebra:
  6.2578 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  6.2579 +  assumes borel: "f \<in> borel_measurable N"
  6.2580 +  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  6.2581 +  shows "integral\<^sup>L N f = integral\<^sup>L M f"
  6.2582 +proof cases
  6.2583 +  assume "integrable N f"
  6.2584 +  then show ?thesis
  6.2585 +  proof induct
  6.2586 +    case base with assms show ?case by (auto simp: subset_eq measure_def)
  6.2587 +  next
  6.2588 +    case (add f g)
  6.2589 +    then have "(\<integral> a. f a + g a \<partial>N) = integral\<^sup>L M f + integral\<^sup>L M g"
  6.2590 +      by simp
  6.2591 +    also have "\<dots> = (\<integral> a. f a + g a \<partial>M)"
  6.2592 +      using add integrable_subalgebra[OF _ N, of f] integrable_subalgebra[OF _ N, of g] by simp
  6.2593 +    finally show ?case .
  6.2594 +  next
  6.2595 +    case (lim f s)
  6.2596 +    then have M: "\<And>i. integrable M (s i)" "integrable M f"
  6.2597 +      using integrable_subalgebra[OF _ N, of f] integrable_subalgebra[OF _ N, of "s i" for i] by simp_all
  6.2598 +    show ?case
  6.2599 +    proof (intro LIMSEQ_unique)
  6.2600 +      show "(\<lambda>i. integral\<^sup>L N (s i)) ----> integral\<^sup>L N f"
  6.2601 +        apply (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  6.2602 +        using lim
  6.2603 +        apply auto
  6.2604 +        done
  6.2605 +      show "(\<lambda>i. integral\<^sup>L N (s i)) ----> integral\<^sup>L M f"
  6.2606 +        unfolding lim
  6.2607 +        apply (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  6.2608 +        using lim M N(2)
  6.2609 +        apply auto
  6.2610 +        done
  6.2611 +    qed
  6.2612 +  qed
  6.2613 +qed (simp add: not_integrable_integral_eq integrable_subalgebra[OF assms])
  6.2614 +
  6.2615 +hide_const simple_bochner_integral
  6.2616 +hide_const simple_bochner_integrable
  6.2617 +
  6.2618 +end
     7.1 --- a/src/HOL/Probability/Borel_Space.thy	Mon May 19 11:27:02 2014 +0200
     7.2 +++ b/src/HOL/Probability/Borel_Space.thy	Mon May 19 12:04:45 2014 +0200
     7.3 @@ -655,6 +655,9 @@
     7.4  
     7.5  subsection "Borel measurable operators"
     7.6  
     7.7 +lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
     7.8 +  by (intro borel_measurable_continuous_on1 continuous_intros)
     7.9 +
    7.10  lemma borel_measurable_uminus[measurable (raw)]:
    7.11    fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
    7.12    assumes g: "g \<in> borel_measurable M"
    7.13 @@ -820,20 +823,6 @@
    7.14  lemma borel_measurable_exp[measurable]: "exp \<in> borel_measurable borel"
    7.15    by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
    7.16  
    7.17 -lemma measurable_count_space_eq2_countable:
    7.18 -  fixes f :: "'a => 'c::countable"
    7.19 -  shows "f \<in> measurable M (count_space A) \<longleftrightarrow> (f \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M))"
    7.20 -proof -
    7.21 -  { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
    7.22 -    then have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)"
    7.23 -      by auto
    7.24 -    moreover assume "\<And>a. a\<in>A \<Longrightarrow> f -` {a} \<inter> space M \<in> sets M"
    7.25 -    ultimately have "f -` X \<inter> space M \<in> sets M"
    7.26 -      using `X \<subseteq> A` by (simp add: subset_eq del: UN_simps) }
    7.27 -  then show ?thesis
    7.28 -    unfolding measurable_def by auto
    7.29 -qed
    7.30 -
    7.31  lemma measurable_real_floor[measurable]:
    7.32    "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
    7.33  proof -
    7.34 @@ -1166,6 +1155,37 @@
    7.35    ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
    7.36  qed
    7.37  
    7.38 +lemma borel_measurable_LIMSEQ_metric:
    7.39 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
    7.40 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
    7.41 +  assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) ----> g x"
    7.42 +  shows "g \<in> borel_measurable M"
    7.43 +  unfolding borel_eq_closed
    7.44 +proof (safe intro!: measurable_measure_of)
    7.45 +  fix A :: "'b set" assume "closed A" 
    7.46 +
    7.47 +  have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
    7.48 +  proof (rule borel_measurable_LIMSEQ)
    7.49 +    show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) ----> infdist (g x) A"
    7.50 +      by (intro tendsto_infdist lim)
    7.51 +    show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
    7.52 +      by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
    7.53 +        continuous_at_imp_continuous_on ballI continuous_infdist isCont_ident) auto
    7.54 +  qed
    7.55 +
    7.56 +  show "g -` A \<inter> space M \<in> sets M"
    7.57 +  proof cases
    7.58 +    assume "A \<noteq> {}"
    7.59 +    then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
    7.60 +      using `closed A` by (simp add: in_closed_iff_infdist_zero)
    7.61 +    then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
    7.62 +      by auto
    7.63 +    also have "\<dots> \<in> sets M"
    7.64 +      by measurable
    7.65 +    finally show ?thesis .
    7.66 +  qed simp
    7.67 +qed auto
    7.68 +
    7.69  lemma sets_Collect_Cauchy[measurable]: 
    7.70    fixes f :: "nat \<Rightarrow> 'a => real"
    7.71    assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
     8.1 --- a/src/HOL/Probability/Complete_Measure.thy	Mon May 19 11:27:02 2014 +0200
     8.2 +++ b/src/HOL/Probability/Complete_Measure.thy	Mon May 19 12:04:45 2014 +0200
     8.3 @@ -3,7 +3,7 @@
     8.4  *)
     8.5  
     8.6  theory Complete_Measure
     8.7 -imports Lebesgue_Integration
     8.8 +imports Bochner_Integration
     8.9  begin
    8.10  
    8.11  definition
     9.1 --- a/src/HOL/Probability/Distributions.thy	Mon May 19 11:27:02 2014 +0200
     9.2 +++ b/src/HOL/Probability/Distributions.thy	Mon May 19 12:04:45 2014 +0200
     9.3 @@ -162,8 +162,8 @@
     9.4    by (auto intro!: exponential_distributedI simp: one_ereal_def emeasure_eq_measure)
     9.5  
     9.6  lemma borel_integral_x_exp:
     9.7 -  "(\<integral>x. x * exp (- x) * indicator {0::real ..} x \<partial>lborel) = 1"
     9.8 -proof (rule integral_monotone_convergence)
     9.9 +  "has_bochner_integral lborel (\<lambda>x. x * exp (- x) * indicator {0::real ..} x) 1"
    9.10 +proof (rule has_bochner_integral_monotone_convergence)
    9.11    let ?f = "\<lambda>i x. x * exp (- x) * indicator {0::real .. i} x"
    9.12    have "eventually (\<lambda>b::real. 0 \<le> b) at_top"
    9.13      by (rule eventually_ge_at_top)
    9.14 @@ -172,7 +172,7 @@
    9.15     fix b :: real assume [simp]: "0 \<le> b"
    9.16      have "(\<integral>x. (exp (-x)) * indicator {0 .. b} x \<partial>lborel) - (integral\<^sup>L lborel (?f b)) = 
    9.17        (\<integral>x. (exp (-x) - x * exp (-x)) * indicator {0 .. b} x \<partial>lborel)"
    9.18 -      by (subst integral_diff(2)[symmetric])
    9.19 +      by (subst integral_diff[symmetric])
    9.20           (auto intro!: borel_integrable_atLeastAtMost integral_cong split: split_indicator)
    9.21      also have "\<dots> = b * exp (-b) - 0 * exp (- 0)"
    9.22      proof (rule integral_FTC_atLeastAtMost)
    9.23 @@ -217,9 +217,11 @@
    9.24      using `0 < l`
    9.25      by (auto split: split_indicator simp: zero_le_mult_iff exponential_density_def)
    9.26    from borel_integral_x_exp `0 < l`
    9.27 -  show "(\<integral> x. exponential_density l x * x \<partial>lborel) = 1 / l"
    9.28 -    by (subst (asm) lebesgue_integral_real_affine[of "l" _ 0])
    9.29 -       (simp_all add: borel_measurable_exp nonzero_eq_divide_eq ac_simps)
    9.30 +  have "has_bochner_integral lborel (\<lambda>x. exponential_density l x * x) (1 / l)"
    9.31 +    by (subst (asm) lborel_has_bochner_integral_real_affine_iff[of l _ _ 0])
    9.32 +       (simp_all add: field_simps)
    9.33 +  then show "(\<integral> x. exponential_density l x * x \<partial>lborel) = 1 / l"
    9.34 +    by (metis has_bochner_integral_integral_eq)
    9.35  qed simp
    9.36  
    9.37  subsection {* Uniform distribution *}
    9.38 @@ -356,7 +358,9 @@
    9.39      uniform_distributed_bounds[of X a b]
    9.40      uniform_distributed_measure[of X a b]
    9.41      distributed_measurable[of M lborel X]
    9.42 -  by (auto intro!: uniform_distrI_borel_atLeastAtMost simp: one_ereal_def emeasure_eq_measure)
    9.43 +  by (auto intro!: uniform_distrI_borel_atLeastAtMost 
    9.44 +              simp: one_ereal_def emeasure_eq_measure
    9.45 +              simp del: measure_lborel)
    9.46  
    9.47  lemma (in prob_space) uniform_distributed_expectation:
    9.48    fixes a b :: real
    10.1 --- a/src/HOL/Probability/Finite_Product_Measure.thy	Mon May 19 11:27:02 2014 +0200
    10.2 +++ b/src/HOL/Probability/Finite_Product_Measure.thy	Mon May 19 12:04:45 2014 +0200
    10.3 @@ -782,7 +782,7 @@
    10.4    show ?thesis
    10.5      apply (subst distr_merge[OF IJ, symmetric])
    10.6      apply (subst positive_integral_distr[OF measurable_merge f])
    10.7 -    apply (subst J.positive_integral_fst_measurable(2)[symmetric, OF P_borel])
    10.8 +    apply (subst J.positive_integral_fst[symmetric, OF P_borel])
    10.9      apply simp
   10.10      done
   10.11  qed
   10.12 @@ -859,17 +859,6 @@
   10.13      done
   10.14  qed (simp add: space_PiM)
   10.15  
   10.16 -lemma (in product_sigma_finite) product_integral_singleton:
   10.17 -  assumes f: "f \<in> borel_measurable (M i)"
   10.18 -  shows "(\<integral>x. f (x i) \<partial>Pi\<^sub>M {i} M) = integral\<^sup>L (M i) f"
   10.19 -proof -
   10.20 -  interpret I: finite_product_sigma_finite M "{i}" by default simp
   10.21 -  have *: "(\<lambda>x. ereal (f x)) \<in> borel_measurable (M i)"
   10.22 -    "(\<lambda>x. ereal (- f x)) \<in> borel_measurable (M i)"
   10.23 -    using assms by auto
   10.24 -  show ?thesis
   10.25 -    unfolding lebesgue_integral_def *[THEN product_positive_integral_singleton] ..
   10.26 -qed
   10.27  lemma (in product_sigma_finite) distr_component:
   10.28    "distr (M i) (Pi\<^sub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^sub>M {i} M" (is "?D = ?P")
   10.29  proof (intro measure_eqI[symmetric])
   10.30 @@ -888,32 +877,6 @@
   10.31    finally show "emeasure (Pi\<^sub>M {i} M) A = emeasure ?D A" .
   10.32  qed simp
   10.33  
   10.34 -lemma (in product_sigma_finite) product_integral_fold:
   10.35 -  assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   10.36 -  and f: "integrable (Pi\<^sub>M (I \<union> J) M) f"
   10.37 -  shows "integral\<^sup>L (Pi\<^sub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I J (x, y)) \<partial>Pi\<^sub>M J M) \<partial>Pi\<^sub>M I M)"
   10.38 -proof -
   10.39 -  interpret I: finite_product_sigma_finite M I by default fact
   10.40 -  interpret J: finite_product_sigma_finite M J by default fact
   10.41 -  have "finite (I \<union> J)" using fin by auto
   10.42 -  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   10.43 -  interpret P: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" by default
   10.44 -  let ?M = "merge I J"
   10.45 -  let ?f = "\<lambda>x. f (?M x)"
   10.46 -  from f have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (I \<union> J) M)"
   10.47 -    by auto
   10.48 -  have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
   10.49 -    using measurable_comp[OF measurable_merge f_borel] by (simp add: comp_def)
   10.50 -  have f_int: "integrable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) ?f"
   10.51 -    by (rule integrable_distr[OF measurable_merge]) (simp add: distr_merge[OF IJ fin] f)
   10.52 -  show ?thesis
   10.53 -    apply (subst distr_merge[symmetric, OF IJ fin])
   10.54 -    apply (subst integral_distr[OF measurable_merge f_borel])
   10.55 -    apply (subst P.integrable_fst_measurable(2)[symmetric, OF f_int])
   10.56 -    apply simp
   10.57 -    done
   10.58 -qed
   10.59 -
   10.60  lemma (in product_sigma_finite)
   10.61    assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^sub>M (I \<union> J) M)"
   10.62    shows emeasure_fold_integral:
   10.63 @@ -939,82 +902,6 @@
   10.64      by (simp add: vimage_comp comp_def space_pair_measure cong: measurable_cong)
   10.65  qed
   10.66  
   10.67 -lemma (in product_sigma_finite) product_integral_insert:
   10.68 -  assumes I: "finite I" "i \<notin> I"
   10.69 -    and f: "integrable (Pi\<^sub>M (insert i I) M) f"
   10.70 -  shows "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = (\<integral>x. (\<integral>y. f (x(i:=y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
   10.71 -proof -
   10.72 -  have "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = integral\<^sup>L (Pi\<^sub>M (I \<union> {i}) M) f"
   10.73 -    by simp
   10.74 -  also have "\<dots> = (\<integral>x. (\<integral>y. f (merge I {i} (x,y)) \<partial>Pi\<^sub>M {i} M) \<partial>Pi\<^sub>M I M)"
   10.75 -    using f I by (intro product_integral_fold) auto
   10.76 -  also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
   10.77 -  proof (rule integral_cong, subst product_integral_singleton[symmetric])
   10.78 -    fix x assume x: "x \<in> space (Pi\<^sub>M I M)"
   10.79 -    have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
   10.80 -      using f by auto
   10.81 -    show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
   10.82 -      using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
   10.83 -      unfolding comp_def .
   10.84 -    from x I show "(\<integral> y. f (merge I {i} (x,y)) \<partial>Pi\<^sub>M {i} M) = (\<integral> xa. f (x(i := xa i)) \<partial>Pi\<^sub>M {i} M)"
   10.85 -      by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def PiE_def)
   10.86 -  qed
   10.87 -  finally show ?thesis .
   10.88 -qed
   10.89 -
   10.90 -lemma (in product_sigma_finite) product_integrable_setprod:
   10.91 -  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   10.92 -  assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   10.93 -  shows "integrable (Pi\<^sub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
   10.94 -proof -
   10.95 -  interpret finite_product_sigma_finite M I by default fact
   10.96 -  have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
   10.97 -    using integrable unfolding integrable_def by auto
   10.98 -  have borel: "?f \<in> borel_measurable (Pi\<^sub>M I M)"
   10.99 -    using measurable_comp[OF measurable_component_singleton[of _ I M] f] by (auto simp: comp_def)
  10.100 -  moreover have "integrable (Pi\<^sub>M I M) (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
  10.101 -  proof (unfold integrable_def, intro conjI)
  10.102 -    show "(\<lambda>x. abs (?f x)) \<in> borel_measurable (Pi\<^sub>M I M)"
  10.103 -      using borel by auto
  10.104 -    have "(\<integral>\<^sup>+x. ereal (abs (?f x)) \<partial>Pi\<^sub>M I M) = (\<integral>\<^sup>+x. (\<Prod>i\<in>I. ereal (abs (f i (x i)))) \<partial>Pi\<^sub>M I M)"
  10.105 -      by (simp add: setprod_ereal abs_setprod)
  10.106 -    also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^sup>+x. ereal (abs (f i x)) \<partial>M i))"
  10.107 -      using f by (subst product_positive_integral_setprod) auto
  10.108 -    also have "\<dots> < \<infinity>"
  10.109 -      using integrable[THEN integrable_abs]
  10.110 -      by (simp add: setprod_PInf integrable_def positive_integral_positive)
  10.111 -    finally show "(\<integral>\<^sup>+x. ereal (abs (?f x)) \<partial>(Pi\<^sub>M I M)) \<noteq> \<infinity>" by auto
  10.112 -    have "(\<integral>\<^sup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^sub>M I M)) = (\<integral>\<^sup>+x. 0 \<partial>(Pi\<^sub>M I M))"
  10.113 -      by (intro positive_integral_cong_pos) auto
  10.114 -    then show "(\<integral>\<^sup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^sub>M I M)) \<noteq> \<infinity>" by simp
  10.115 -  qed
  10.116 -  ultimately show ?thesis
  10.117 -    by (rule integrable_abs_iff[THEN iffD1])
  10.118 -qed
  10.119 -
  10.120 -lemma (in product_sigma_finite) product_integral_setprod:
  10.121 -  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
  10.122 -  assumes "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  10.123 -  shows "(\<integral>x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^sub>M I M) = (\<Prod>i\<in>I. integral\<^sup>L (M i) (f i))"
  10.124 -using assms proof induct
  10.125 -  case empty
  10.126 -  interpret finite_measure "Pi\<^sub>M {} M"
  10.127 -    by rule (simp add: space_PiM)
  10.128 -  show ?case by (simp add: space_PiM measure_def)
  10.129 -next
  10.130 -  case (insert i I)
  10.131 -  then have iI: "finite (insert i I)" by auto
  10.132 -  then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  10.133 -    integrable (Pi\<^sub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  10.134 -    by (intro product_integrable_setprod insert(4)) (auto intro: finite_subset)
  10.135 -  interpret I: finite_product_sigma_finite M I by default fact
  10.136 -  have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  10.137 -    using `i \<notin> I` by (auto intro!: setprod_cong)
  10.138 -  show ?case
  10.139 -    unfolding product_integral_insert[OF insert(1,2) prod[OF subset_refl]]
  10.140 -    by (simp add: * insert integral_multc integral_cmult[OF prod] subset_insertI)
  10.141 -qed
  10.142 -
  10.143  lemma sets_Collect_single:
  10.144    "i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> { x \<in> space (Pi\<^sub>M I M). x i \<in> A } \<in> sets (Pi\<^sub>M I M)"
  10.145    by simp
    11.1 --- a/src/HOL/Probability/Information.thy	Mon May 19 11:27:02 2014 +0200
    11.2 +++ b/src/HOL/Probability/Information.thy	Mon May 19 12:04:45 2014 +0200
    11.3 @@ -79,37 +79,26 @@
    11.4  definition
    11.5    "KL_divergence b M N = integral\<^sup>L N (entropy_density b M N)"
    11.6  
    11.7 -lemma (in information_space) measurable_entropy_density:
    11.8 -  assumes ac: "absolutely_continuous M N" "sets N = events"
    11.9 -  shows "entropy_density b M N \<in> borel_measurable M"
   11.10 -proof -
   11.11 -  from borel_measurable_RN_deriv[OF ac] b_gt_1 show ?thesis
   11.12 -    unfolding entropy_density_def by auto
   11.13 -qed
   11.14 -
   11.15 -lemma borel_measurable_RN_deriv_density[measurable (raw)]:
   11.16 -  "f \<in> borel_measurable M \<Longrightarrow> RN_deriv M (density M f) \<in> borel_measurable M"
   11.17 -  using borel_measurable_RN_deriv_density[of "\<lambda>x. max 0 (f x )" M]
   11.18 -  by (simp add: density_max_0[symmetric])
   11.19 +lemma measurable_entropy_density[measurable]: "entropy_density b M N \<in> borel_measurable M"
   11.20 +  unfolding entropy_density_def by auto
   11.21  
   11.22  lemma (in sigma_finite_measure) KL_density:
   11.23    fixes f :: "'a \<Rightarrow> real"
   11.24    assumes "1 < b"
   11.25 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
   11.26 +  assumes f[measurable]: "f \<in> borel_measurable M" and nn: "AE x in M. 0 \<le> f x"
   11.27    shows "KL_divergence b M (density M f) = (\<integral>x. f x * log b (f x) \<partial>M)"
   11.28    unfolding KL_divergence_def
   11.29 -proof (subst integral_density)
   11.30 -  show "entropy_density b M (density M (\<lambda>x. ereal (f x))) \<in> borel_measurable M"
   11.31 +proof (subst integral_real_density)
   11.32 +  show [measurable]: "entropy_density b M (density M (\<lambda>x. ereal (f x))) \<in> borel_measurable M"
   11.33      using f
   11.34      by (auto simp: comp_def entropy_density_def)
   11.35    have "density M (RN_deriv M (density M f)) = density M f"
   11.36 -    using f by (intro density_RN_deriv_density) auto
   11.37 +    using f nn by (intro density_RN_deriv_density) auto
   11.38    then have eq: "AE x in M. RN_deriv M (density M f) x = f x"
   11.39 -    using f
   11.40 -    by (intro density_unique)
   11.41 -       (auto intro!: borel_measurable_log borel_measurable_RN_deriv_density simp: RN_deriv_density_nonneg)
   11.42 +    using f nn by (intro density_unique) (auto simp: RN_deriv_nonneg)
   11.43    show "(\<integral>x. f x * entropy_density b M (density M (\<lambda>x. ereal (f x))) x \<partial>M) = (\<integral>x. f x * log b (f x) \<partial>M)"
   11.44      apply (intro integral_cong_AE)
   11.45 +    apply measurable
   11.46      using eq
   11.47      apply eventually_elim
   11.48      apply (auto simp: entropy_density_def)
   11.49 @@ -161,8 +150,10 @@
   11.50    then have D_pos: "(\<integral>\<^sup>+ x. ereal (D x) \<partial>M) = 1"
   11.51      using N.emeasure_space_1 by simp
   11.52  
   11.53 -  have "integrable M D" "integral\<^sup>L M D = 1"
   11.54 -    using D D_pos D_neg unfolding integrable_def lebesgue_integral_def by simp_all
   11.55 +  have "integrable M D"
   11.56 +    using D D_pos D_neg unfolding real_integrable_def real_lebesgue_integral_def by simp_all
   11.57 +  then have "integral\<^sup>L M D = 1"
   11.58 +    using D D_pos D_neg by (simp add: real_lebesgue_integral_def)
   11.59  
   11.60    have "0 \<le> 1 - measure M ?D_set"
   11.61      using prob_le_1 by (auto simp: field_simps)
   11.62 @@ -172,10 +163,9 @@
   11.63    also have "\<dots> < (\<integral> x. D x * (ln b * log b (D x)) \<partial>M)"
   11.64    proof (rule integral_less_AE)
   11.65      show "integrable M (\<lambda>x. D x - indicator ?D_set x)"
   11.66 -      using `integrable M D`
   11.67 -      by (intro integral_diff integral_indicator) auto
   11.68 +      using `integrable M D` by auto
   11.69    next
   11.70 -    from integral_cmult(1)[OF int, of "ln b"]
   11.71 +    from integrable_mult_left(1)[OF int, of "ln b"]
   11.72      show "integrable M (\<lambda>x. D x * (ln b * log b (D x)))" 
   11.73        by (simp add: ac_simps)
   11.74    next
   11.75 @@ -242,7 +232,7 @@
   11.76    also have "\<dots> = (\<integral> x. ln b * (D x * log b (D x)) \<partial>M)"
   11.77      by (simp add: ac_simps)
   11.78    also have "\<dots> = ln b * (\<integral> x. D x * log b (D x) \<partial>M)"
   11.79 -    using int by (rule integral_cmult)
   11.80 +    using int by simp
   11.81    finally show ?thesis
   11.82      using b_gt_1 D by (subst KL_density) (auto simp: zero_less_mult_iff)
   11.83  qed
   11.84 @@ -260,7 +250,7 @@
   11.85    qed
   11.86    then have "AE x in M. log b (real (RN_deriv M M x)) = 0"
   11.87      by (elim AE_mp) simp
   11.88 -  from integral_cong_AE[OF this]
   11.89 +  from integral_cong_AE[OF _ _ this]
   11.90    have "integral\<^sup>L M (entropy_density b M M) = 0"
   11.91      by (simp add: entropy_density_def comp_def)
   11.92    then show "KL_divergence b M M = 0"
   11.93 @@ -291,7 +281,7 @@
   11.94    have "N = density M (RN_deriv M N)"
   11.95      using ac by (rule density_RN_deriv[symmetric])
   11.96    also have "\<dots> = density M D"
   11.97 -    using borel_measurable_RN_deriv[OF ac] D by (auto intro!: density_cong)
   11.98 +    using D by (auto intro!: density_cong)
   11.99    finally have N: "N = density M D" .
  11.100  
  11.101    from absolutely_continuous_AE[OF ac(2,1) D(2)] D b_gt_1 ac measurable_entropy_density
  11.102 @@ -299,7 +289,7 @@
  11.103      by (intro integrable_cong_AE[THEN iffD2, OF _ _ _ int])
  11.104         (auto simp: N entropy_density_def)
  11.105    with D b_gt_1 have "integrable M (\<lambda>x. D x * log b (D x))"
  11.106 -    by (subst integral_density(2)[symmetric]) (auto simp: N[symmetric] comp_def)
  11.107 +    by (subst integrable_real_density[symmetric]) (auto simp: N[symmetric] comp_def)
  11.108    with `prob_space N` D show ?thesis
  11.109      unfolding N
  11.110      by (intro KL_eq_0_iff_eq) auto
  11.111 @@ -335,7 +325,7 @@
  11.112        using f g by (auto simp: AE_density)
  11.113      show "integrable (density M f) (\<lambda>x. g x / f x * log b (g x / f x))"
  11.114        using `1 < b` f g ac
  11.115 -      by (subst integral_density)
  11.116 +      by (subst integrable_density)
  11.117           (auto intro!: integrable_cong_AE[THEN iffD2, OF _ _ _ int] measurable_If)
  11.118    qed
  11.119    also have "\<dots> = KL_divergence b (density M f) (density M g)"
  11.120 @@ -414,7 +404,7 @@
  11.121    "distributed M N X f \<Longrightarrow> g \<in> borel_measurable N \<Longrightarrow>
  11.122      integrable N (\<lambda>x. f x * g x) \<longleftrightarrow> integrable M (\<lambda>x. g (X x))"
  11.123    by (auto simp: distributed_real_AE
  11.124 -                    distributed_distr_eq_density[symmetric] integral_density[symmetric] integrable_distr_eq)
  11.125 +                    distributed_distr_eq_density[symmetric] integrable_real_density[symmetric] integrable_distr_eq)
  11.126    
  11.127  lemma distributed_transform_integrable:
  11.128    assumes Px: "distributed M N X Px"
  11.129 @@ -431,8 +421,9 @@
  11.130    finally show ?thesis .
  11.131  qed
  11.132  
  11.133 -lemma integrable_cong_AE_imp: "integrable M g \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (AE x in M. g x = f x) \<Longrightarrow> integrable M f"
  11.134 -  using integrable_cong_AE by blast
  11.135 +lemma integrable_cong_AE_imp:
  11.136 +  "integrable M g \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (AE x in M. g x = f x) \<Longrightarrow> integrable M f"
  11.137 +  using integrable_cong_AE[of f M g] by (auto simp: eq_commute)
  11.138  
  11.139  lemma (in information_space) finite_entropy_integrable:
  11.140    "finite_entropy S X Px \<Longrightarrow> integrable S (\<lambda>x. Px x * log b (Px x))"
  11.141 @@ -485,7 +476,7 @@
  11.142  
  11.143      show ac: "absolutely_continuous P Q" by (simp add: absolutely_continuous_def)
  11.144      then have ed: "entropy_density b P Q \<in> borel_measurable P"
  11.145 -      by (rule P.measurable_entropy_density) simp
  11.146 +      by simp
  11.147  
  11.148      have "AE x in P. 1 = RN_deriv P Q x"
  11.149      proof (rule P.RN_deriv_unique)
  11.150 @@ -494,13 +485,15 @@
  11.151      qed auto
  11.152      then have ae_0: "AE x in P. entropy_density b P Q x = 0"
  11.153        by eventually_elim (auto simp: entropy_density_def)
  11.154 -    then have "integrable P (entropy_density b P Q) \<longleftrightarrow> integrable Q (\<lambda>x. 0)"
  11.155 +    then have "integrable P (entropy_density b P Q) \<longleftrightarrow> integrable Q (\<lambda>x. 0::real)"
  11.156        using ed unfolding `Q = P` by (intro integrable_cong_AE) auto
  11.157      then show "integrable Q (entropy_density b P Q)" by simp
  11.158  
  11.159 -    show "mutual_information b S T X Y = 0"
  11.160 +    from ae_0 have "mutual_information b S T X Y = (\<integral>x. 0 \<partial>P)"
  11.161        unfolding mutual_information_def KL_divergence_def P_def[symmetric] Q_def[symmetric] `Q = P`
  11.162 -      using ae_0 by (simp cong: integral_cong_AE) }
  11.163 +      by (intro integral_cong_AE) auto
  11.164 +    then show "mutual_information b S T X Y = 0"
  11.165 +      by simp }
  11.166  
  11.167    { assume ac: "absolutely_continuous P Q"
  11.168      assume int: "integrable Q (entropy_density b P Q)"
  11.169 @@ -722,8 +715,8 @@
  11.170  lemma (in information_space)
  11.171    fixes Pxy :: "'b \<times> 'c \<Rightarrow> real" and Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
  11.172    assumes "sigma_finite_measure S" "sigma_finite_measure T"
  11.173 -  assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
  11.174 -  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.175 +  assumes Px[measurable]: "distributed M S X Px" and Py[measurable]: "distributed M T Y Py"
  11.176 +  assumes Pxy[measurable]: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.177    assumes ae: "AE x in S. AE y in T. Pxy (x, y) = Px x * Py y"
  11.178    shows mutual_information_eq_0: "mutual_information b S T X Y = 0"
  11.179  proof -
  11.180 @@ -743,7 +736,7 @@
  11.181    ultimately have "AE x in S \<Otimes>\<^sub>M T. Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x))) = 0"
  11.182      by eventually_elim simp
  11.183    then have "(\<integral>x. Pxy x * log b (Pxy x / (Px (fst x) * Py (snd x))) \<partial>(S \<Otimes>\<^sub>M T)) = (\<integral>x. 0 \<partial>(S \<Otimes>\<^sub>M T))"
  11.184 -    by (rule integral_cong_AE)
  11.185 +    by (intro integral_cong_AE) auto
  11.186    then show ?thesis
  11.187      by (subst mutual_information_distr[OF assms(1-5)]) simp
  11.188  qed
  11.189 @@ -810,7 +803,7 @@
  11.190  
  11.191  lemma (in information_space)
  11.192    fixes X :: "'a \<Rightarrow> 'b"
  11.193 -  assumes X: "distributed M MX X f"
  11.194 +  assumes X[measurable]: "distributed M MX X f"
  11.195    shows entropy_distr: "entropy b MX X = - (\<integral>x. f x * log b (f x) \<partial>MX)" (is ?eq)
  11.196  proof -
  11.197    note D = distributed_measurable[OF X] distributed_borel_measurable[OF X] distributed_AE[OF X]
  11.198 @@ -825,18 +818,15 @@
  11.199      apply auto
  11.200      done
  11.201  
  11.202 -  have int_eq: "- (\<integral> x. log b (f x) \<partial>distr M MX X) = - (\<integral> x. f x * log b (f x) \<partial>MX)"
  11.203 +  have int_eq: "(\<integral> x. f x * log b (f x) \<partial>MX) = (\<integral> x. log b (f x) \<partial>distr M MX X)"
  11.204      unfolding distributed_distr_eq_density[OF X]
  11.205      using D
  11.206      by (subst integral_density)
  11.207         (auto simp: borel_measurable_ereal_iff)
  11.208  
  11.209    show ?eq
  11.210 -    unfolding entropy_def KL_divergence_def entropy_density_def comp_def
  11.211 -    apply (subst integral_cong_AE)
  11.212 -    apply (rule ae_eq)
  11.213 -    apply (rule int_eq)
  11.214 -    done
  11.215 +    unfolding entropy_def KL_divergence_def entropy_density_def comp_def int_eq neg_equal_iff_equal
  11.216 +    using ae_eq by (intro integral_cong_AE) auto
  11.217  qed
  11.218  
  11.219  lemma (in prob_space) distributed_imp_emeasure_nonzero:
  11.220 @@ -861,7 +851,7 @@
  11.221  
  11.222  lemma (in information_space) entropy_le:
  11.223    fixes Px :: "'b \<Rightarrow> real" and MX :: "'b measure"
  11.224 -  assumes X: "distributed M MX X Px"
  11.225 +  assumes X[measurable]: "distributed M MX X Px"
  11.226    and fin: "emeasure MX {x \<in> space MX. Px x \<noteq> 0} \<noteq> \<infinity>"
  11.227    and int: "integrable MX (\<lambda>x. - Px x * log b (Px x))"
  11.228    shows "entropy b MX X \<le> log b (measure MX {x \<in> space MX. Px x \<noteq> 0})"
  11.229 @@ -873,7 +863,7 @@
  11.230    have " - log b (measure MX {x \<in> space MX. Px x \<noteq> 0}) = 
  11.231      - log b (\<integral> x. indicator {x \<in> space MX. Px x \<noteq> 0} x \<partial>MX)"
  11.232      using Px fin
  11.233 -    by (subst integral_indicator) (auto simp: measure_def borel_measurable_ereal_iff)
  11.234 +    by (subst integral_real_indicator) (auto simp: measure_def borel_measurable_ereal_iff)
  11.235    also have "- log b (\<integral> x. indicator {x \<in> space MX. Px x \<noteq> 0} x \<partial>MX) = - log b (\<integral> x. 1 / Px x \<partial>distr M MX X)"
  11.236      unfolding distributed_distr_eq_density[OF X] using Px
  11.237      apply (intro arg_cong[where f="log b"] arg_cong[where f=uminus])
  11.238 @@ -889,7 +879,7 @@
  11.239        by (intro positive_integral_cong) (auto split: split_max)
  11.240      then show "integrable (distr M MX X) (\<lambda>x. 1 / Px x)"
  11.241        unfolding distributed_distr_eq_density[OF X] using Px
  11.242 -      by (auto simp: positive_integral_density integrable_def borel_measurable_ereal_iff fin positive_integral_max_0
  11.243 +      by (auto simp: positive_integral_density real_integrable_def borel_measurable_ereal_iff fin positive_integral_max_0
  11.244                cong: positive_integral_cong)
  11.245      have "integrable MX (\<lambda>x. Px x * log b (1 / Px x)) =
  11.246        integrable MX (\<lambda>x. - Px x * log b (Px x))"
  11.247 @@ -900,7 +890,7 @@
  11.248      then show "integrable (distr M MX X) (\<lambda>x. - log b (1 / Px x))"
  11.249        unfolding distributed_distr_eq_density[OF X]
  11.250        using Px int
  11.251 -      by (subst integral_density) (auto simp: borel_measurable_ereal_iff)
  11.252 +      by (subst integrable_real_density) (auto simp: borel_measurable_ereal_iff)
  11.253    qed (auto simp: minus_log_convex[OF b_gt_1])
  11.254    also have "\<dots> = (\<integral> x. log b (Px x) \<partial>distr M MX X)"
  11.255      unfolding distributed_distr_eq_density[OF X] using Px
  11.256 @@ -940,11 +930,11 @@
  11.257    have eq: "(\<integral> x. indicator A x / measure MX A * log b (indicator A x / measure MX A) \<partial>MX) =
  11.258      (\<integral> x. (- log b (measure MX A) / measure MX A) * indicator A x \<partial>MX)"
  11.259      using measure_nonneg[of MX A] uniform_distributed_params[OF X]
  11.260 -    by (auto intro!: integral_cong split: split_indicator simp: log_divide_eq)
  11.261 +    by (intro integral_cong) (auto split: split_indicator simp: log_divide_eq)
  11.262    show "- (\<integral> x. indicator A x / measure MX A * log b (indicator A x / measure MX A) \<partial>MX) =
  11.263      log b (measure MX A)"
  11.264      unfolding eq using uniform_distributed_params[OF X]
  11.265 -    by (subst lebesgue_integral_cmult) (auto simp: measure_def)
  11.266 +    by (subst integral_mult_right) (auto simp: measure_def)
  11.267  qed
  11.268  
  11.269  lemma (in information_space) entropy_simple_distributed:
  11.270 @@ -1068,7 +1058,7 @@
  11.271      unfolding conditional_mutual_information_def
  11.272      apply (subst mi_eq)
  11.273      apply (subst mutual_information_distr[OF S TP Px Pyz Pxyz])
  11.274 -    apply (subst integral_diff(2)[symmetric])
  11.275 +    apply (subst integral_diff[symmetric])
  11.276      apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
  11.277      done
  11.278  
  11.279 @@ -1104,7 +1094,7 @@
  11.280      apply auto
  11.281      done
  11.282    also have "\<dots> = (\<integral>\<^sup>+(y, z). \<integral>\<^sup>+ x. ereal (Pxz (x, z)) * ereal (Pyz (y, z) / Pz z) \<partial>S \<partial>T \<Otimes>\<^sub>M P)"
  11.283 -    by (subst STP.positive_integral_snd_measurable[symmetric]) (auto simp add: split_beta')
  11.284 +    by (subst STP.positive_integral_snd[symmetric]) (auto simp add: split_beta')
  11.285    also have "\<dots> = (\<integral>\<^sup>+x. ereal (Pyz x) * 1 \<partial>T \<Otimes>\<^sub>M P)"
  11.286      apply (rule positive_integral_cong_AE)
  11.287      using aeX1 aeX2 aeX3 distributed_AE[OF Pyz] AE_space
  11.288 @@ -1123,7 +1113,7 @@
  11.289    also have "\<dots> < \<infinity>" by simp
  11.290    finally have fin: "(\<integral>\<^sup>+ x. ?f x \<partial>?P) \<noteq> \<infinity>" by simp
  11.291  
  11.292 -  have pos: "(\<integral>\<^sup>+ x. ?f x \<partial>?P) \<noteq> 0"
  11.293 +  have pos: "(\<integral>\<^sup>+x. ?f x \<partial>?P) \<noteq> 0"
  11.294      apply (subst positive_integral_density)
  11.295      apply simp
  11.296      apply (rule distributed_AE[OF Pxyz])
  11.297 @@ -1131,7 +1121,7 @@
  11.298      apply (simp add: split_beta')
  11.299    proof
  11.300      let ?g = "\<lambda>x. ereal (if Pxyz x = 0 then 0 else Pxz (fst x, snd (snd x)) * Pyz (snd x) / Pz (snd (snd x)))"
  11.301 -    assume "(\<integral>\<^sup>+ x. ?g x \<partial>(S \<Otimes>\<^sub>M T \<Otimes>\<^sub>M P)) = 0"
  11.302 +    assume "(\<integral>\<^sup>+x. ?g x \<partial>(S \<Otimes>\<^sub>M T \<Otimes>\<^sub>M P)) = 0"
  11.303      then have "AE x in S \<Otimes>\<^sub>M T \<Otimes>\<^sub>M P. ?g x \<le> 0"
  11.304        by (intro positive_integral_0_iff_AE[THEN iffD1]) auto
  11.305      then have "AE x in S \<Otimes>\<^sub>M T \<Otimes>\<^sub>M P. Pxyz x = 0"
  11.306 @@ -1154,19 +1144,32 @@
  11.307      done
  11.308  
  11.309    have I3: "integrable (S \<Otimes>\<^sub>M T \<Otimes>\<^sub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
  11.310 -    apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ integral_diff(1)[OF I1 I2]])
  11.311 +    apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ integrable_diff[OF I1 I2]])
  11.312      using ae
  11.313      apply (auto simp: split_beta')
  11.314      done
  11.315  
  11.316    have "- log b 1 \<le> - log b (integral\<^sup>L ?P ?f)"
  11.317    proof (intro le_imp_neg_le log_le[OF b_gt_1])
  11.318 -    show "0 < integral\<^sup>L ?P ?f"
  11.319 -      using neg pos fin positive_integral_positive[of ?P ?f]
  11.320 -      by (cases "(\<integral>\<^sup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def less_le split_beta')
  11.321 -    show "integral\<^sup>L ?P ?f \<le> 1"
  11.322 -      using neg le1 fin positive_integral_positive[of ?P ?f]
  11.323 -      by (cases "(\<integral>\<^sup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def split_beta' one_ereal_def)
  11.324 +    have If: "integrable ?P ?f"
  11.325 +      unfolding real_integrable_def
  11.326 +    proof (intro conjI)
  11.327 +      from neg show "(\<integral>\<^sup>+ x. - ?f x \<partial>?P) \<noteq> \<infinity>"
  11.328 +        by simp
  11.329 +      from fin show "(\<integral>\<^sup>+ x. ?f x \<partial>?P) \<noteq> \<infinity>"
  11.330 +        by simp
  11.331 +    qed simp
  11.332 +    then have "(\<integral>\<^sup>+ x. ?f x \<partial>?P) = (\<integral>x. ?f x \<partial>?P)"
  11.333 +      apply (rule positive_integral_eq_integral)
  11.334 +      apply (subst AE_density)
  11.335 +      apply simp
  11.336 +      using ae5 ae6 ae7 ae8
  11.337 +      apply eventually_elim
  11.338 +      apply auto
  11.339 +      done
  11.340 +    with positive_integral_positive[of ?P ?f] pos le1
  11.341 +    show "0 < (\<integral>x. ?f x \<partial>?P)" "(\<integral>x. ?f x \<partial>?P) \<le> 1"
  11.342 +      by (simp_all add: one_ereal_def)
  11.343    qed
  11.344    also have "- log b (integral\<^sup>L ?P ?f) \<le> (\<integral> x. - log b (?f x) \<partial>?P)"
  11.345    proof (rule P.jensens_inequality[where a=0 and b=1 and I="{0<..}"])
  11.346 @@ -1175,10 +1178,10 @@
  11.347        using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  11.348        by eventually_elim (auto)
  11.349      show "integrable ?P ?f"
  11.350 -      unfolding integrable_def 
  11.351 +      unfolding real_integrable_def 
  11.352        using fin neg by (auto simp: split_beta')
  11.353      show "integrable ?P (\<lambda>x. - log b (?f x))"
  11.354 -      apply (subst integral_density)
  11.355 +      apply (subst integrable_real_density)
  11.356        apply simp
  11.357        apply (auto intro!: distributed_real_AE[OF Pxyz]) []
  11.358        apply simp
  11.359 @@ -1192,13 +1195,12 @@
  11.360    qed (auto simp: b_gt_1 minus_log_convex)
  11.361    also have "\<dots> = conditional_mutual_information b S T P X Y Z"
  11.362      unfolding `?eq`
  11.363 -    apply (subst integral_density)
  11.364 +    apply (subst integral_real_density)
  11.365      apply simp
  11.366      apply (auto intro!: distributed_real_AE[OF Pxyz]) []
  11.367      apply simp
  11.368      apply (intro integral_cong_AE)
  11.369      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  11.370 -    apply eventually_elim
  11.371      apply (auto simp: log_divide_eq zero_less_mult_iff zero_less_divide_iff field_simps)
  11.372      done
  11.373    finally show ?nonneg
  11.374 @@ -1316,7 +1318,7 @@
  11.375      unfolding conditional_mutual_information_def
  11.376      apply (subst mi_eq)
  11.377      apply (subst mutual_information_distr[OF S TP Px Pyz Pxyz])
  11.378 -    apply (subst integral_diff(2)[symmetric])
  11.379 +    apply (subst integral_diff[symmetric])
  11.380      apply (auto intro!: integral_cong_AE simp: split_beta' simp del: integral_diff)
  11.381      done
  11.382  
  11.383 @@ -1349,8 +1351,7 @@
  11.384      apply auto
  11.385      done
  11.386    also have "\<dots> = (\<integral>\<^sup>+(y, z). \<integral>\<^sup>+ x. ereal (Pxz (x, z)) * ereal (Pyz (y, z) / Pz z) \<partial>S \<partial>T \<Otimes>\<^sub>M P)"
  11.387 -    by (subst STP.positive_integral_snd_measurable[symmetric])
  11.388 -       (auto simp add: split_beta')
  11.389 +    by (subst STP.positive_integral_snd[symmetric]) (auto simp add: split_beta')
  11.390    also have "\<dots> = (\<integral>\<^sup>+x. ereal (Pyz x) * 1 \<partial>T \<Otimes>\<^sub>M P)"
  11.391      apply (rule positive_integral_cong_AE)
  11.392      using aeX1 aeX2 aeX3 distributed_AE[OF Pyz] AE_space
  11.393 @@ -1399,19 +1400,32 @@
  11.394      done
  11.395  
  11.396    have I3: "integrable (S \<Otimes>\<^sub>M T \<Otimes>\<^sub>M P) (\<lambda>(x, y, z). Pxyz (x, y, z) * log b (Pxyz (x, y, z) / (Pxz (x, z) * (Pyz (y,z) / Pz z))))"
  11.397 -    apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ integral_diff(1)[OF I1 I2]])
  11.398 +    apply (rule integrable_cong_AE[THEN iffD1, OF _ _ _ integrable_diff[OF I1 I2]])
  11.399      using ae
  11.400      apply (auto simp: split_beta')
  11.401      done
  11.402  
  11.403    have "- log b 1 \<le> - log b (integral\<^sup>L ?P ?f)"
  11.404    proof (intro le_imp_neg_le log_le[OF b_gt_1])
  11.405 -    show "0 < integral\<^sup>L ?P ?f"
  11.406 -      using neg pos fin positive_integral_positive[of ?P ?f]
  11.407 -      by (cases "(\<integral>\<^sup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def less_le split_beta')
  11.408 -    show "integral\<^sup>L ?P ?f \<le> 1"
  11.409 -      using neg le1 fin positive_integral_positive[of ?P ?f]
  11.410 -      by (cases "(\<integral>\<^sup>+ x. ?f x \<partial>?P)") (auto simp add: lebesgue_integral_def split_beta' one_ereal_def)
  11.411 +    have If: "integrable ?P ?f"
  11.412 +      unfolding real_integrable_def
  11.413 +    proof (intro conjI)
  11.414 +      from neg show "(\<integral>\<^sup>+ x. - ?f x \<partial>?P) \<noteq> \<infinity>"
  11.415 +        by simp
  11.416 +      from fin show "(\<integral>\<^sup>+ x. ?f x \<partial>?P) \<noteq> \<infinity>"
  11.417 +        by simp
  11.418 +    qed simp
  11.419 +    then have "(\<integral>\<^sup>+ x. ?f x \<partial>?P) = (\<integral>x. ?f x \<partial>?P)"
  11.420 +      apply (rule positive_integral_eq_integral)
  11.421 +      apply (subst AE_density)
  11.422 +      apply simp
  11.423 +      using ae5 ae6 ae7 ae8
  11.424 +      apply eventually_elim
  11.425 +      apply auto
  11.426 +      done
  11.427 +    with positive_integral_positive[of ?P ?f] pos le1
  11.428 +    show "0 < (\<integral>x. ?f x \<partial>?P)" "(\<integral>x. ?f x \<partial>?P) \<le> 1"
  11.429 +      by (simp_all add: one_ereal_def)
  11.430    qed
  11.431    also have "- log b (integral\<^sup>L ?P ?f) \<le> (\<integral> x. - log b (?f x) \<partial>?P)"
  11.432    proof (rule P.jensens_inequality[where a=0 and b=1 and I="{0<..}"])
  11.433 @@ -1420,10 +1434,10 @@
  11.434        using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  11.435        by eventually_elim (auto)
  11.436      show "integrable ?P ?f"
  11.437 -      unfolding integrable_def
  11.438 +      unfolding real_integrable_def
  11.439        using fin neg by (auto simp: split_beta')
  11.440      show "integrable ?P (\<lambda>x. - log b (?f x))"
  11.441 -      apply (subst integral_density)
  11.442 +      apply (subst integrable_real_density)
  11.443        apply simp
  11.444        apply (auto intro!: distributed_real_AE[OF Pxyz]) []
  11.445        apply simp
  11.446 @@ -1437,13 +1451,12 @@
  11.447    qed (auto simp: b_gt_1 minus_log_convex)
  11.448    also have "\<dots> = conditional_mutual_information b S T P X Y Z"
  11.449      unfolding `?eq`
  11.450 -    apply (subst integral_density)
  11.451 +    apply (subst integral_real_density)
  11.452      apply simp
  11.453      apply (auto intro!: distributed_real_AE[OF Pxyz]) []
  11.454      apply simp
  11.455      apply (intro integral_cong_AE)
  11.456      using ae1 ae2 ae3 ae4 ae5 ae6 ae7 ae8 Pxyz[THEN distributed_real_AE]
  11.457 -    apply eventually_elim
  11.458      apply (auto simp: log_divide_eq zero_less_mult_iff zero_less_divide_iff field_simps)
  11.459      done
  11.460    finally show ?nonneg
  11.461 @@ -1532,7 +1545,7 @@
  11.462    "\<H>(X | Y) \<equiv> conditional_entropy b (count_space (X`space M)) (count_space (Y`space M)) X Y"
  11.463  
  11.464  lemma (in information_space) conditional_entropy_generic_eq:
  11.465 -  fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
  11.466 +  fixes Pxy :: "_ \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
  11.467    assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  11.468    assumes Py[measurable]: "distributed M T Y Py"
  11.469    assumes Pxy[measurable]: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.470 @@ -1552,19 +1565,15 @@
  11.471      unfolding AE_density[OF distributed_borel_measurable, OF Pxy]
  11.472      unfolding distributed_distr_eq_density[OF Py]
  11.473      apply (rule ST.AE_pair_measure)
  11.474 -    apply (auto intro!: sets.sets_Collect borel_measurable_eq measurable_compose[OF _ distributed_real_measurable[OF Py]]
  11.475 -                        distributed_real_measurable[OF Pxy] distributed_real_AE[OF Py]
  11.476 -                        borel_measurable_real_of_ereal measurable_compose[OF _ borel_measurable_RN_deriv_density])
  11.477 +    apply auto
  11.478      using distributed_RN_deriv[OF Py]
  11.479      apply auto
  11.480      done    
  11.481    ultimately
  11.482    have "conditional_entropy b S T X Y = - (\<integral>x. Pxy x * log b (Pxy x / Py (snd x)) \<partial>(S \<Otimes>\<^sub>M T))"
  11.483      unfolding conditional_entropy_def neg_equal_iff_equal
  11.484 -    apply (subst integral_density(1)[symmetric])
  11.485 -    apply (auto simp: distributed_real_measurable[OF Pxy] distributed_real_AE[OF Pxy]
  11.486 -                      measurable_compose[OF _ distributed_real_measurable[OF Py]]
  11.487 -                      distributed_distr_eq_density[OF Pxy]
  11.488 +    apply (subst integral_real_density[symmetric])
  11.489 +    apply (auto simp: distributed_real_AE[OF Pxy] distributed_distr_eq_density[OF Pxy]
  11.490                  intro!: integral_cong_AE)
  11.491      done
  11.492    then show ?thesis by (simp add: split_beta')
  11.493 @@ -1573,8 +1582,8 @@
  11.494  lemma (in information_space) conditional_entropy_eq_entropy:
  11.495    fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real"
  11.496    assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  11.497 -  assumes Py: "distributed M T Y Py"
  11.498 -  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.499 +  assumes Py[measurable]: "distributed M T Y Py"
  11.500 +  assumes Pxy[measurable]: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.501    assumes I1: "integrable (S \<Otimes>\<^sub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
  11.502    assumes I2: "integrable (S \<Otimes>\<^sub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
  11.503    shows "conditional_entropy b S T X Y = entropy b (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) - entropy b T Y"
  11.504 @@ -1606,7 +1615,6 @@
  11.505      unfolding conditional_entropy_generic_eq[OF S T Py Pxy] neg_equal_iff_equal
  11.506      apply (intro integral_cong_AE)
  11.507      using ae
  11.508 -    apply eventually_elim
  11.509      apply auto
  11.510      done
  11.511    also have "\<dots> = - (\<integral>x. Pxy x * log b (Pxy x) \<partial>(S \<Otimes>\<^sub>M T)) - - (\<integral>x.  Pxy x * log b (Py (snd x)) \<partial>(S \<Otimes>\<^sub>M T))"
  11.512 @@ -1702,8 +1710,8 @@
  11.513  lemma (in information_space) mutual_information_eq_entropy_conditional_entropy_distr:
  11.514    fixes Px :: "'b \<Rightarrow> real" and Py :: "'c \<Rightarrow> real" and Pxy :: "('b \<times> 'c) \<Rightarrow> real"
  11.515    assumes S: "sigma_finite_measure S" and T: "sigma_finite_measure T"
  11.516 -  assumes Px: "distributed M S X Px" and Py: "distributed M T Y Py"
  11.517 -  assumes Pxy: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.518 +  assumes Px[measurable]: "distributed M S X Px" and Py[measurable]: "distributed M T Y Py"
  11.519 +  assumes Pxy[measurable]: "distributed M (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) Pxy"
  11.520    assumes Ix: "integrable(S \<Otimes>\<^sub>M T) (\<lambda>x. Pxy x * log b (Px (fst x)))"
  11.521    assumes Iy: "integrable(S \<Otimes>\<^sub>M T) (\<lambda>x. Pxy x * log b (Py (snd x)))"
  11.522    assumes Ixy: "integrable(S \<Otimes>\<^sub>M T) (\<lambda>x. Pxy x * log b (Pxy x))"
  11.523 @@ -1758,13 +1766,13 @@
  11.524    have "entropy b S X + entropy b T Y - entropy b (S \<Otimes>\<^sub>M T) (\<lambda>x. (X x, Y x)) = integral\<^sup>L (S \<Otimes>\<^sub>M T) ?f"
  11.525      unfolding X Y XY
  11.526      apply (subst integral_diff)
  11.527 -    apply (intro integral_diff Ixy Ix Iy)+
  11.528 +    apply (intro integrable_diff Ixy Ix Iy)+
  11.529      apply (subst integral_diff)
  11.530 -    apply (intro integral_diff Ixy Ix Iy)+
  11.531 +    apply (intro Ixy Ix Iy)+
  11.532      apply (simp add: field_simps)
  11.533      done
  11.534    also have "\<dots> = integral\<^sup>L (S \<Otimes>\<^sub>M T) ?g"
  11.535 -    using `AE x in _. ?f x = ?g x` by (rule integral_cong_AE)
  11.536 +    using `AE x in _. ?f x = ?g x` by (intro integral_cong_AE) auto
  11.537    also have "\<dots> = mutual_information b S T X Y"
  11.538      by (rule mutual_information_distr[OF S T Px Py Pxy, symmetric])
  11.539    finally show ?thesis ..
    12.1 --- a/src/HOL/Probability/Lebesgue_Integration.thy	Mon May 19 11:27:02 2014 +0200
    12.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
    12.3 @@ -1,2900 +0,0 @@
    12.4 -(*  Title:      HOL/Probability/Lebesgue_Integration.thy
    12.5 -    Author:     Johannes Hölzl, TU München
    12.6 -    Author:     Armin Heller, TU München
    12.7 -*)
    12.8 -
    12.9 -header {*Lebesgue Integration*}
   12.10 -
   12.11 -theory Lebesgue_Integration
   12.12 -  imports Measure_Space Borel_Space
   12.13 -begin
   12.14 -
   12.15 -lemma indicator_less_ereal[simp]:
   12.16 -  "indicator A x \<le> (indicator B x::ereal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
   12.17 -  by (simp add: indicator_def not_le)
   12.18 -
   12.19 -section "Simple function"
   12.20 -
   12.21 -text {*
   12.22 -
   12.23 -Our simple functions are not restricted to positive real numbers. Instead
   12.24 -they are just functions with a finite range and are measurable when singleton
   12.25 -sets are measurable.
   12.26 -
   12.27 -*}
   12.28 -
   12.29 -definition "simple_function M g \<longleftrightarrow>
   12.30 -    finite (g ` space M) \<and>
   12.31 -    (\<forall>x \<in> g ` space M. g -` {x} \<inter> space M \<in> sets M)"
   12.32 -
   12.33 -lemma simple_functionD:
   12.34 -  assumes "simple_function M g"
   12.35 -  shows "finite (g ` space M)" and "g -` X \<inter> space M \<in> sets M"
   12.36 -proof -
   12.37 -  show "finite (g ` space M)"
   12.38 -    using assms unfolding simple_function_def by auto
   12.39 -  have "g -` X \<inter> space M = g -` (X \<inter> g`space M) \<inter> space M" by auto
   12.40 -  also have "\<dots> = (\<Union>x\<in>X \<inter> g`space M. g-`{x} \<inter> space M)" by auto
   12.41 -  finally show "g -` X \<inter> space M \<in> sets M" using assms
   12.42 -    by (auto simp del: UN_simps simp: simple_function_def)
   12.43 -qed
   12.44 -
   12.45 -lemma measurable_simple_function[measurable_dest]:
   12.46 -  "simple_function M f \<Longrightarrow> f \<in> measurable M (count_space UNIV)"
   12.47 -  unfolding simple_function_def measurable_def
   12.48 -proof safe
   12.49 -  fix A assume "finite (f ` space M)" "\<forall>x\<in>f ` space M. f -` {x} \<inter> space M \<in> sets M"
   12.50 -  then have "(\<Union>x\<in>f ` space M. if x \<in> A then f -` {x} \<inter> space M else {}) \<in> sets M"
   12.51 -    by (intro sets.finite_UN) auto
   12.52 -  also have "(\<Union>x\<in>f ` space M. if x \<in> A then f -` {x} \<inter> space M else {}) = f -` A \<inter> space M"
   12.53 -    by (auto split: split_if_asm)
   12.54 -  finally show "f -` A \<inter> space M \<in> sets M" .
   12.55 -qed simp
   12.56 -
   12.57 -lemma borel_measurable_simple_function:
   12.58 -  "simple_function M f \<Longrightarrow> f \<in> borel_measurable M"
   12.59 -  by (auto dest!: measurable_simple_function simp: measurable_def)
   12.60 -
   12.61 -lemma simple_function_measurable2[intro]:
   12.62 -  assumes "simple_function M f" "simple_function M g"
   12.63 -  shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
   12.64 -proof -
   12.65 -  have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
   12.66 -    by auto
   12.67 -  then show ?thesis using assms[THEN simple_functionD(2)] by auto
   12.68 -qed
   12.69 -
   12.70 -lemma simple_function_indicator_representation:
   12.71 -  fixes f ::"'a \<Rightarrow> ereal"
   12.72 -  assumes f: "simple_function M f" and x: "x \<in> space M"
   12.73 -  shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
   12.74 -  (is "?l = ?r")
   12.75 -proof -
   12.76 -  have "?r = (\<Sum>y \<in> f ` space M.
   12.77 -    (if y = f x then y * indicator (f -` {y} \<inter> space M) x else 0))"
   12.78 -    by (auto intro!: setsum_cong2)
   12.79 -  also have "... =  f x *  indicator (f -` {f x} \<inter> space M) x"
   12.80 -    using assms by (auto dest: simple_functionD simp: setsum_delta)
   12.81 -  also have "... = f x" using x by (auto simp: indicator_def)
   12.82 -  finally show ?thesis by auto
   12.83 -qed
   12.84 -
   12.85 -lemma simple_function_notspace:
   12.86 -  "simple_function M (\<lambda>x. h x * indicator (- space M) x::ereal)" (is "simple_function M ?h")
   12.87 -proof -
   12.88 -  have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
   12.89 -  hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
   12.90 -  have "?h -` {0} \<inter> space M = space M" by auto
   12.91 -  thus ?thesis unfolding simple_function_def by auto
   12.92 -qed
   12.93 -
   12.94 -lemma simple_function_cong:
   12.95 -  assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
   12.96 -  shows "simple_function M f \<longleftrightarrow> simple_function M g"
   12.97 -proof -
   12.98 -  have "f ` space M = g ` space M"
   12.99 -    "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
  12.100 -    using assms by (auto intro!: image_eqI)
  12.101 -  thus ?thesis unfolding simple_function_def using assms by simp
  12.102 -qed
  12.103 -
  12.104 -lemma simple_function_cong_algebra:
  12.105 -  assumes "sets N = sets M" "space N = space M"
  12.106 -  shows "simple_function M f \<longleftrightarrow> simple_function N f"
  12.107 -  unfolding simple_function_def assms ..
  12.108 -
  12.109 -lemma simple_function_borel_measurable:
  12.110 -  fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
  12.111 -  assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
  12.112 -  shows "simple_function M f"
  12.113 -  using assms unfolding simple_function_def
  12.114 -  by (auto intro: borel_measurable_vimage)
  12.115 -
  12.116 -lemma simple_function_eq_measurable:
  12.117 -  fixes f :: "'a \<Rightarrow> ereal"
  12.118 -  shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> measurable M (count_space UNIV)"
  12.119 -  using simple_function_borel_measurable[of f] measurable_simple_function[of M f]
  12.120 -  by (fastforce simp: simple_function_def)
  12.121 -
  12.122 -lemma simple_function_const[intro, simp]:
  12.123 -  "simple_function M (\<lambda>x. c)"
  12.124 -  by (auto intro: finite_subset simp: simple_function_def)
  12.125 -lemma simple_function_compose[intro, simp]:
  12.126 -  assumes "simple_function M f"
  12.127 -  shows "simple_function M (g \<circ> f)"
  12.128 -  unfolding simple_function_def
  12.129 -proof safe
  12.130 -  show "finite ((g \<circ> f) ` space M)"
  12.131 -    using assms unfolding simple_function_def by (auto simp: image_comp [symmetric])
  12.132 -next
  12.133 -  fix x assume "x \<in> space M"
  12.134 -  let ?G = "g -` {g (f x)} \<inter> (f`space M)"
  12.135 -  have *: "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M =
  12.136 -    (\<Union>x\<in>?G. f -` {x} \<inter> space M)" by auto
  12.137 -  show "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M \<in> sets M"
  12.138 -    using assms unfolding simple_function_def *
  12.139 -    by (rule_tac sets.finite_UN) auto
  12.140 -qed
  12.141 -
  12.142 -lemma simple_function_indicator[intro, simp]:
  12.143 -  assumes "A \<in> sets M"
  12.144 -  shows "simple_function M (indicator A)"
  12.145 -proof -
  12.146 -  have "indicator A ` space M \<subseteq> {0, 1}" (is "?S \<subseteq> _")
  12.147 -    by (auto simp: indicator_def)
  12.148 -  hence "finite ?S" by (rule finite_subset) simp
  12.149 -  moreover have "- A \<inter> space M = space M - A" by auto
  12.150 -  ultimately show ?thesis unfolding simple_function_def
  12.151 -    using assms by (auto simp: indicator_def [abs_def])
  12.152 -qed
  12.153 -
  12.154 -lemma simple_function_Pair[intro, simp]:
  12.155 -  assumes "simple_function M f"
  12.156 -  assumes "simple_function M g"
  12.157 -  shows "simple_function M (\<lambda>x. (f x, g x))" (is "simple_function M ?p")
  12.158 -  unfolding simple_function_def
  12.159 -proof safe
  12.160 -  show "finite (?p ` space M)"
  12.161 -    using assms unfolding simple_function_def
  12.162 -    by (rule_tac finite_subset[of _ "f`space M \<times> g`space M"]) auto
  12.163 -next
  12.164 -  fix x assume "x \<in> space M"
  12.165 -  have "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M =
  12.166 -      (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)"
  12.167 -    by auto
  12.168 -  with `x \<in> space M` show "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M \<in> sets M"
  12.169 -    using assms unfolding simple_function_def by auto
  12.170 -qed
  12.171 -
  12.172 -lemma simple_function_compose1:
  12.173 -  assumes "simple_function M f"
  12.174 -  shows "simple_function M (\<lambda>x. g (f x))"
  12.175 -  using simple_function_compose[OF assms, of g]
  12.176 -  by (simp add: comp_def)
  12.177 -
  12.178 -lemma simple_function_compose2:
  12.179 -  assumes "simple_function M f" and "simple_function M g"
  12.180 -  shows "simple_function M (\<lambda>x. h (f x) (g x))"
  12.181 -proof -
  12.182 -  have "simple_function M ((\<lambda>(x, y). h x y) \<circ> (\<lambda>x. (f x, g x)))"
  12.183 -    using assms by auto
  12.184 -  thus ?thesis by (simp_all add: comp_def)
  12.185 -qed
  12.186 -
  12.187 -lemmas simple_function_add[intro, simp] = simple_function_compose2[where h="op +"]
  12.188 -  and simple_function_diff[intro, simp] = simple_function_compose2[where h="op -"]
  12.189 -  and simple_function_uminus[intro, simp] = simple_function_compose[where g="uminus"]
  12.190 -  and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
  12.191 -  and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
  12.192 -  and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
  12.193 -  and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
  12.194 -
  12.195 -lemma simple_function_setsum[intro, simp]:
  12.196 -  assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
  12.197 -  shows "simple_function M (\<lambda>x. \<Sum>i\<in>P. f i x)"
  12.198 -proof cases
  12.199 -  assume "finite P" from this assms show ?thesis by induct auto
  12.200 -qed auto
  12.201 -
  12.202 -lemma simple_function_ereal[intro, simp]: 
  12.203 -  fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
  12.204 -  shows "simple_function M (\<lambda>x. ereal (f x))"
  12.205 -  by (auto intro!: simple_function_compose1[OF sf])
  12.206 -
  12.207 -lemma simple_function_real_of_nat[intro, simp]: 
  12.208 -  fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
  12.209 -  shows "simple_function M (\<lambda>x. real (f x))"
  12.210 -  by (auto intro!: simple_function_compose1[OF sf])
  12.211 -
  12.212 -lemma borel_measurable_implies_simple_function_sequence:
  12.213 -  fixes u :: "'a \<Rightarrow> ereal"
  12.214 -  assumes u: "u \<in> borel_measurable M"
  12.215 -  shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
  12.216 -             (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
  12.217 -proof -
  12.218 -  def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
  12.219 -  { fix x j have "f x j \<le> j * 2 ^ j" unfolding f_def
  12.220 -    proof (split split_if, intro conjI impI)
  12.221 -      assume "\<not> real j \<le> u x"
  12.222 -      then have "natfloor (real (u x) * 2 ^ j) \<le> natfloor (j * 2 ^ j)"
  12.223 -         by (cases "u x") (auto intro!: natfloor_mono)
  12.224 -      moreover have "real (natfloor (j * 2 ^ j)) \<le> j * 2^j"
  12.225 -        by (intro real_natfloor_le) auto
  12.226 -      ultimately show "natfloor (real (u x) * 2 ^ j) \<le> j * 2 ^ j"
  12.227 -        unfolding real_of_nat_le_iff by auto
  12.228 -    qed auto }
  12.229 -  note f_upper = this
  12.230 -
  12.231 -  have real_f:
  12.232 -    "\<And>i x. real (f x i) = (if real i \<le> u x then i * 2 ^ i else real (natfloor (real (u x) * 2 ^ i)))"
  12.233 -    unfolding f_def by auto
  12.234 -
  12.235 -  let ?g = "\<lambda>j x. real (f x j) / 2^j :: ereal"
  12.236 -  show ?thesis
  12.237 -  proof (intro exI[of _ ?g] conjI allI ballI)
  12.238 -    fix i
  12.239 -    have "simple_function M (\<lambda>x. real (f x i))"
  12.240 -    proof (intro simple_function_borel_measurable)
  12.241 -      show "(\<lambda>x. real (f x i)) \<in> borel_measurable M"
  12.242 -        using u by (auto simp: real_f)
  12.243 -      have "(\<lambda>x. real (f x i))`space M \<subseteq> real`{..i*2^i}"
  12.244 -        using f_upper[of _ i] by auto
  12.245 -      then show "finite ((\<lambda>x. real (f x i))`space M)"
  12.246 -        by (rule finite_subset) auto
  12.247 -    qed
  12.248 -    then show "simple_function M (?g i)"
  12.249 -      by (auto intro: simple_function_ereal simple_function_div)
  12.250 -  next
  12.251 -    show "incseq ?g"
  12.252 -    proof (intro incseq_ereal incseq_SucI le_funI)
  12.253 -      fix x and i :: nat
  12.254 -      have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
  12.255 -      proof ((split split_if)+, intro conjI impI)
  12.256 -        assume "ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
  12.257 -        then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
  12.258 -          by (cases "u x") (auto intro!: le_natfloor)
  12.259 -      next
  12.260 -        assume "\<not> ereal (real i) \<le> u x" "ereal (real (Suc i)) \<le> u x"
  12.261 -        then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
  12.262 -          by (cases "u x") auto
  12.263 -      next
  12.264 -        assume "\<not> ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
  12.265 -        have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
  12.266 -          by simp
  12.267 -        also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
  12.268 -        proof cases
  12.269 -          assume "0 \<le> u x" then show ?thesis
  12.270 -            by (intro le_mult_natfloor) 
  12.271 -        next
  12.272 -          assume "\<not> 0 \<le> u x" then show ?thesis
  12.273 -            by (cases "u x") (auto simp: natfloor_neg mult_nonpos_nonneg)
  12.274 -        qed
  12.275 -        also have "\<dots> = natfloor (real (u x) * 2 ^ Suc i)"
  12.276 -          by (simp add: ac_simps)
  12.277 -        finally show "natfloor (real (u x) * 2 ^ i) * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)" .
  12.278 -      qed simp
  12.279 -      then show "?g i x \<le> ?g (Suc i) x"
  12.280 -        by (auto simp: field_simps)
  12.281 -    qed
  12.282 -  next
  12.283 -    fix x show "(SUP i. ?g i x) = max 0 (u x)"
  12.284 -    proof (rule SUP_eqI)
  12.285 -      fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
  12.286 -        by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
  12.287 -                                     mult_nonpos_nonneg)
  12.288 -    next
  12.289 -      fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
  12.290 -      have "\<And>i. 0 \<le> ?g i x" by auto
  12.291 -      from order_trans[OF this *] have "0 \<le> y" by simp
  12.292 -      show "max 0 (u x) \<le> y"
  12.293 -      proof (cases y)
  12.294 -        case (real r)
  12.295 -        with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
  12.296 -        from reals_Archimedean2[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
  12.297 -        then have "\<exists>p. max 0 (u x) = ereal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
  12.298 -        then guess p .. note ux = this
  12.299 -        obtain m :: nat where m: "p < real m" using reals_Archimedean2 ..
  12.300 -        have "p \<le> r"
  12.301 -        proof (rule ccontr)
  12.302 -          assume "\<not> p \<le> r"
  12.303 -          with LIMSEQ_inverse_realpow_zero[unfolded LIMSEQ_iff, rule_format, of 2 "p - r"]
  12.304 -          obtain N where "\<forall>n\<ge>N. r * 2^n < p * 2^n - 1" by (auto simp: field_simps)
  12.305 -          then have "r * 2^max N m < p * 2^max N m - 1" by simp
  12.306 -          moreover
  12.307 -          have "real (natfloor (p * 2 ^ max N m)) \<le> r * 2 ^ max N m"
  12.308 -            using *[of "max N m"] m unfolding real_f using ux
  12.309 -            by (cases "0 \<le> u x") (simp_all add: max_def split: split_if_asm)
  12.310 -          then have "p * 2 ^ max N m - 1 < r * 2 ^ max N m"
  12.311 -            by (metis real_natfloor_gt_diff_one less_le_trans)
  12.312 -          ultimately show False by auto
  12.313 -        qed
  12.314 -        then show "max 0 (u x) \<le> y" using real ux by simp
  12.315 -      qed (insert `0 \<le> y`, auto)
  12.316 -    qed
  12.317 -  qed auto
  12.318 -qed
  12.319 -
  12.320 -lemma borel_measurable_implies_simple_function_sequence':
  12.321 -  fixes u :: "'a \<Rightarrow> ereal"
  12.322 -  assumes u: "u \<in> borel_measurable M"
  12.323 -  obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
  12.324 -    "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
  12.325 -  using borel_measurable_implies_simple_function_sequence[OF u] by auto
  12.326 -
  12.327 -lemma simple_function_induct[consumes 1, case_names cong set mult add, induct set: simple_function]:
  12.328 -  fixes u :: "'a \<Rightarrow> ereal"
  12.329 -  assumes u: "simple_function M u"
  12.330 -  assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
  12.331 -  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
  12.332 -  assumes mult: "\<And>u c. P u \<Longrightarrow> P (\<lambda>x. c * u x)"
  12.333 -  assumes add: "\<And>u v. P u \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
  12.334 -  shows "P u"
  12.335 -proof (rule cong)
  12.336 -  from AE_space show "AE x in M. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x"
  12.337 -  proof eventually_elim
  12.338 -    fix x assume x: "x \<in> space M"
  12.339 -    from simple_function_indicator_representation[OF u x]
  12.340 -    show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
  12.341 -  qed
  12.342 -next
  12.343 -  from u have "finite (u ` space M)"
  12.344 -    unfolding simple_function_def by auto
  12.345 -  then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
  12.346 -  proof induct
  12.347 -    case empty show ?case
  12.348 -      using set[of "{}"] by (simp add: indicator_def[abs_def])
  12.349 -  qed (auto intro!: add mult set simple_functionD u)
  12.350 -next
  12.351 -  show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
  12.352 -    apply (subst simple_function_cong)
  12.353 -    apply (rule simple_function_indicator_representation[symmetric])
  12.354 -    apply (auto intro: u)
  12.355 -    done
  12.356 -qed fact
  12.357 -
  12.358 -lemma simple_function_induct_nn[consumes 2, case_names cong set mult add]:
  12.359 -  fixes u :: "'a \<Rightarrow> ereal"
  12.360 -  assumes u: "simple_function M u" and nn: "\<And>x. 0 \<le> u x"
  12.361 -  assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
  12.362 -  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
  12.363 -  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
  12.364 -  assumes add: "\<And>u v. simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> simple_function M v \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
  12.365 -  shows "P u"
  12.366 -proof -
  12.367 -  show ?thesis
  12.368 -  proof (rule cong)
  12.369 -    fix x assume x: "x \<in> space M"
  12.370 -    from simple_function_indicator_representation[OF u x]
  12.371 -    show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
  12.372 -  next
  12.373 -    show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
  12.374 -      apply (subst simple_function_cong)
  12.375 -      apply (rule simple_function_indicator_representation[symmetric])
  12.376 -      apply (auto intro: u)
  12.377 -      done
  12.378 -  next
  12.379 -    from u nn have "finite (u ` space M)" "\<And>x. x \<in> u ` space M \<Longrightarrow> 0 \<le> x"
  12.380 -      unfolding simple_function_def by auto
  12.381 -    then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
  12.382 -    proof induct
  12.383 -      case empty show ?case
  12.384 -        using set[of "{}"] by (simp add: indicator_def[abs_def])
  12.385 -    qed (auto intro!: add mult set simple_functionD u setsum_nonneg
  12.386 -       simple_function_setsum)
  12.387 -  qed fact
  12.388 -qed
  12.389 -
  12.390 -lemma borel_measurable_induct[consumes 2, case_names cong set mult add seq, induct set: borel_measurable]:
  12.391 -  fixes u :: "'a \<Rightarrow> ereal"
  12.392 -  assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
  12.393 -  assumes cong: "\<And>f g. f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P g \<Longrightarrow> P f"
  12.394 -  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
  12.395 -  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
  12.396 -  assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
  12.397 -  assumes seq: "\<And>U. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow>  (\<And>i x. 0 \<le> U i x) \<Longrightarrow>  (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> P (SUP i. U i)"
  12.398 -  shows "P u"
  12.399 -  using u
  12.400 -proof (induct rule: borel_measurable_implies_simple_function_sequence')
  12.401 -  fix U assume U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
  12.402 -    sup: "\<And>x. (SUP i. U i x) = max 0 (u x)" and nn: "\<And>i x. 0 \<le> U i x"
  12.403 -  have u_eq: "u = (SUP i. U i)"
  12.404 -    using nn u sup by (auto simp: max_def)
  12.405 -  
  12.406 -  from U have "\<And>i. U i \<in> borel_measurable M"
  12.407 -    by (simp add: borel_measurable_simple_function)
  12.408 -
  12.409 -  show "P u"
  12.410 -    unfolding u_eq
  12.411 -  proof (rule seq)
  12.412 -    fix i show "P (U i)"
  12.413 -      using `simple_function M (U i)` nn
  12.414 -      by (induct rule: simple_function_induct_nn)
  12.415 -         (auto intro: set mult add cong dest!: borel_measurable_simple_function)
  12.416 -  qed fact+
  12.417 -qed
  12.418 -
  12.419 -lemma simple_function_If_set:
  12.420 -  assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
  12.421 -  shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
  12.422 -proof -
  12.423 -  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
  12.424 -  show ?thesis unfolding simple_function_def
  12.425 -  proof safe
  12.426 -    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
  12.427 -    from finite_subset[OF this] assms
  12.428 -    show "finite (?IF ` space M)" unfolding simple_function_def by auto
  12.429 -  next
  12.430 -    fix x assume "x \<in> space M"
  12.431 -    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
  12.432 -      then ((F (f x) \<inter> (A \<inter> space M)) \<union> (G (f x) - (G (f x) \<inter> (A \<inter> space M))))
  12.433 -      else ((F (g x) \<inter> (A \<inter> space M)) \<union> (G (g x) - (G (g x) \<inter> (A \<inter> space M)))))"
  12.434 -      using sets.sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
  12.435 -    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
  12.436 -      unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
  12.437 -    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
  12.438 -  qed
  12.439 -qed
  12.440 -
  12.441 -lemma simple_function_If:
  12.442 -  assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
  12.443 -  shows "simple_function M (\<lambda>x. if P x then f x else g x)"
  12.444 -proof -
  12.445 -  have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
  12.446 -  with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
  12.447 -qed
  12.448 -
  12.449 -lemma simple_function_subalgebra:
  12.450 -  assumes "simple_function N f"
  12.451 -  and N_subalgebra: "sets N \<subseteq> sets M" "space N = space M"
  12.452 -  shows "simple_function M f"
  12.453 -  using assms unfolding simple_function_def by auto
  12.454 -
  12.455 -lemma simple_function_comp:
  12.456 -  assumes T: "T \<in> measurable M M'"
  12.457 -    and f: "simple_function M' f"
  12.458 -  shows "simple_function M (\<lambda>x. f (T x))"
  12.459 -proof (intro simple_function_def[THEN iffD2] conjI ballI)
  12.460 -  have "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
  12.461 -    using T unfolding measurable_def by auto
  12.462 -  then show "finite ((\<lambda>x. f (T x)) ` space M)"
  12.463 -    using f unfolding simple_function_def by (auto intro: finite_subset)
  12.464 -  fix i assume i: "i \<in> (\<lambda>x. f (T x)) ` space M"
  12.465 -  then have "i \<in> f ` space M'"
  12.466 -    using T unfolding measurable_def by auto
  12.467 -  then have "f -` {i} \<inter> space M' \<in> sets M'"
  12.468 -    using f unfolding simple_function_def by auto
  12.469 -  then have "T -` (f -` {i} \<inter> space M') \<inter> space M \<in> sets M"
  12.470 -    using T unfolding measurable_def by auto
  12.471 -  also have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
  12.472 -    using T unfolding measurable_def by auto
  12.473 -  finally show "(\<lambda>x. f (T x)) -` {i} \<inter> space M \<in> sets M" .
  12.474 -qed
  12.475 -
  12.476 -section "Simple integral"
  12.477 -
  12.478 -definition simple_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^sup>S") where
  12.479 -  "integral\<^sup>S M f = (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M))"
  12.480 -
  12.481 -syntax
  12.482 -  "_simple_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^sup>S _. _ \<partial>_" [60,61] 110)
  12.483 -
  12.484 -translations
  12.485 -  "\<integral>\<^sup>S x. f \<partial>M" == "CONST simple_integral M (%x. f)"
  12.486 -
  12.487 -lemma simple_integral_cong:
  12.488 -  assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
  12.489 -  shows "integral\<^sup>S M f = integral\<^sup>S M g"
  12.490 -proof -
  12.491 -  have "f ` space M = g ` space M"
  12.492 -    "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
  12.493 -    using assms by (auto intro!: image_eqI)
  12.494 -  thus ?thesis unfolding simple_integral_def by simp
  12.495 -qed
  12.496 -
  12.497 -lemma simple_integral_const[simp]:
  12.498 -  "(\<integral>\<^sup>Sx. c \<partial>M) = c * (emeasure M) (space M)"
  12.499 -proof (cases "space M = {}")
  12.500 -  case True thus ?thesis unfolding simple_integral_def by simp
  12.501 -next
  12.502 -  case False hence "(\<lambda>x. c) ` space M = {c}" by auto
  12.503 -  thus ?thesis unfolding simple_integral_def by simp
  12.504 -qed
  12.505 -
  12.506 -lemma simple_function_partition:
  12.507 -  assumes f: "simple_function M f" and g: "simple_function M g"
  12.508 -  assumes sub: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow> g x = g y \<Longrightarrow> f x = f y"
  12.509 -  assumes v: "\<And>x. x \<in> space M \<Longrightarrow> f x = v (g x)"
  12.510 -  shows "integral\<^sup>S M f = (\<Sum>y\<in>g ` space M. v y * emeasure M {x\<in>space M. g x = y})"
  12.511 -    (is "_ = ?r")
  12.512 -proof -
  12.513 -  from f g have [simp]: "finite (f`space M)" "finite (g`space M)"
  12.514 -    by (auto simp: simple_function_def)
  12.515 -  from f g have [measurable]: "f \<in> measurable M (count_space UNIV)" "g \<in> measurable M (count_space UNIV)"
  12.516 -    by (auto intro: measurable_simple_function)
  12.517 -
  12.518 -  { fix y assume "y \<in> space M"
  12.519 -    then have "f ` space M \<inter> {i. \<exists>x\<in>space M. i = f x \<and> g y = g x} = {v (g y)}"
  12.520 -      by (auto cong: sub simp: v[symmetric]) }
  12.521 -  note eq = this
  12.522 -
  12.523 -  have "integral\<^sup>S M f =
  12.524 -    (\<Sum>y\<in>f`space M. y * (\<Sum>z\<in>g`space M. 
  12.525 -      if \<exists>x\<in>space M. y = f x \<and> z = g x then emeasure M {x\<in>space M. g x = z} else 0))"
  12.526 -    unfolding simple_integral_def
  12.527 -  proof (safe intro!: setsum_cong ereal_left_mult_cong)
  12.528 -    fix y assume y: "y \<in> space M" "f y \<noteq> 0"
  12.529 -    have [simp]: "g ` space M \<inter> {z. \<exists>x\<in>space M. f y = f x \<and> z = g x} = 
  12.530 -        {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
  12.531 -      by auto
  12.532 -    have eq:"(\<Union>i\<in>{z. \<exists>x\<in>space M. f y = f x \<and> z = g x}. {x \<in> space M. g x = i}) =
  12.533 -        f -` {f y} \<inter> space M"
  12.534 -      by (auto simp: eq_commute cong: sub rev_conj_cong)
  12.535 -    have "finite (g`space M)" by simp
  12.536 -    then have "finite {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
  12.537 -      by (rule rev_finite_subset) auto
  12.538 -    then show "emeasure M (f -` {f y} \<inter> space M) =
  12.539 -      (\<Sum>z\<in>g ` space M. if \<exists>x\<in>space M. f y = f x \<and> z = g x then emeasure M {x \<in> space M. g x = z} else 0)"
  12.540 -      apply (simp add: setsum_cases)
  12.541 -      apply (subst setsum_emeasure)
  12.542 -      apply (auto simp: disjoint_family_on_def eq)
  12.543 -      done
  12.544 -  qed
  12.545 -  also have "\<dots> = (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M. 
  12.546 -      if \<exists>x\<in>space M. y = f x \<and> z = g x then y * emeasure M {x\<in>space M. g x = z} else 0))"
  12.547 -    by (auto intro!: setsum_cong simp: setsum_ereal_right_distrib emeasure_nonneg)
  12.548 -  also have "\<dots> = ?r"
  12.549 -    by (subst setsum_commute)
  12.550 -       (auto intro!: setsum_cong simp: setsum_cases scaleR_setsum_right[symmetric] eq)
  12.551 -  finally show "integral\<^sup>S M f = ?r" .
  12.552 -qed
  12.553 -
  12.554 -lemma simple_integral_add[simp]:
  12.555 -  assumes f: "simple_function M f" and "\<And>x. 0 \<le> f x" and g: "simple_function M g" and "\<And>x. 0 \<le> g x"
  12.556 -  shows "(\<integral>\<^sup>Sx. f x + g x \<partial>M) = integral\<^sup>S M f + integral\<^sup>S M g"
  12.557 -proof -
  12.558 -  have "(\<integral>\<^sup>Sx. f x + g x \<partial>M) =
  12.559 -    (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. (fst y + snd y) * emeasure M {x\<in>space M. (f x, g x) = y})"
  12.560 -    by (intro simple_function_partition) (auto intro: f g)
  12.561 -  also have "\<dots> = (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. fst y * emeasure M {x\<in>space M. (f x, g x) = y}) +
  12.562 -    (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. snd y * emeasure M {x\<in>space M. (f x, g x) = y})"
  12.563 -    using assms(2,4) by (auto intro!: setsum_cong ereal_left_distrib simp: setsum_addf[symmetric])
  12.564 -  also have "(\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. fst y * emeasure M {x\<in>space M. (f x, g x) = y}) = (\<integral>\<^sup>Sx. f x \<partial>M)"
  12.565 -    by (intro simple_function_partition[symmetric]) (auto intro: f g)
  12.566 -  also have "(\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. snd y * emeasure M {x\<in>space M. (f x, g x) = y}) = (\<integral>\<^sup>Sx. g x \<partial>M)"
  12.567 -    by (intro simple_function_partition[symmetric]) (auto intro: f g)
  12.568 -  finally show ?thesis .
  12.569 -qed
  12.570 -
  12.571 -lemma simple_integral_setsum[simp]:
  12.572 -  assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
  12.573 -  assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
  12.574 -  shows "(\<integral>\<^sup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^sup>S M (f i))"
  12.575 -proof cases
  12.576 -  assume "finite P"
  12.577 -  from this assms show ?thesis
  12.578 -    by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
  12.579 -qed auto
  12.580 -
  12.581 -lemma simple_integral_mult[simp]:
  12.582 -  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
  12.583 -  shows "(\<integral>\<^sup>Sx. c * f x \<partial>M) = c * integral\<^sup>S M f"
  12.584 -proof -
  12.585 -  have "(\<integral>\<^sup>Sx. c * f x \<partial>M) = (\<Sum>y\<in>f ` space M. (c * y) * emeasure M {x\<in>space M. f x = y})"
  12.586 -    using f by (intro simple_function_partition) auto
  12.587 -  also have "\<dots> = c * integral\<^sup>S M f"
  12.588 -    using f unfolding simple_integral_def
  12.589 -    by (subst setsum_ereal_right_distrib) (auto simp: emeasure_nonneg mult_assoc Int_def conj_commute)
  12.590 -  finally show ?thesis .
  12.591 -qed
  12.592 -
  12.593 -lemma simple_integral_mono_AE:
  12.594 -  assumes f[measurable]: "simple_function M f" and g[measurable]: "simple_function M g"
  12.595 -  and mono: "AE x in M. f x \<le> g x"
  12.596 -  shows "integral\<^sup>S M f \<le> integral\<^sup>S M g"
  12.597 -proof -
  12.598 -  let ?\<mu> = "\<lambda>P. emeasure M {x\<in>space M. P x}"
  12.599 -  have "integral\<^sup>S M f = (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. fst y * ?\<mu> (\<lambda>x. (f x, g x) = y))"
  12.600 -    using f g by (intro simple_function_partition) auto
  12.601 -  also have "\<dots> \<le> (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. snd y * ?\<mu> (\<lambda>x. (f x, g x) = y))"
  12.602 -  proof (clarsimp intro!: setsum_mono)
  12.603 -    fix x assume "x \<in> space M"
  12.604 -    let ?M = "?\<mu> (\<lambda>y. f y = f x \<and> g y = g x)"
  12.605 -    show "f x * ?M \<le> g x * ?M"
  12.606 -    proof cases
  12.607 -      assume "?M \<noteq> 0"
  12.608 -      then have "0 < ?M"
  12.609 -        by (simp add: less_le emeasure_nonneg)
  12.610 -      also have "\<dots> \<le> ?\<mu> (\<lambda>y. f x \<le> g x)"
  12.611 -        using mono by (intro emeasure_mono_AE) auto
  12.612 -      finally have "\<not> \<not> f x \<le> g x"
  12.613 -        by (intro notI) auto
  12.614 -      then show ?thesis
  12.615 -        by (intro ereal_mult_right_mono) auto
  12.616 -    qed simp
  12.617 -  qed
  12.618 -  also have "\<dots> = integral\<^sup>S M g"
  12.619 -    using f g by (intro simple_function_partition[symmetric]) auto
  12.620 -  finally show ?thesis .
  12.621 -qed
  12.622 -
  12.623 -lemma simple_integral_mono:
  12.624 -  assumes "simple_function M f" and "simple_function M g"
  12.625 -  and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
  12.626 -  shows "integral\<^sup>S M f \<le> integral\<^sup>S M g"
  12.627 -  using assms by (intro simple_integral_mono_AE) auto
  12.628 -
  12.629 -lemma simple_integral_cong_AE:
  12.630 -  assumes "simple_function M f" and "simple_function M g"
  12.631 -  and "AE x in M. f x = g x"
  12.632 -  shows "integral\<^sup>S M f = integral\<^sup>S M g"
  12.633 -  using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
  12.634 -
  12.635 -lemma simple_integral_cong':
  12.636 -  assumes sf: "simple_function M f" "simple_function M g"
  12.637 -  and mea: "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0"
  12.638 -  shows "integral\<^sup>S M f = integral\<^sup>S M g"
  12.639 -proof (intro simple_integral_cong_AE sf AE_I)
  12.640 -  show "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0" by fact
  12.641 -  show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
  12.642 -    using sf[THEN borel_measurable_simple_function] by auto
  12.643 -qed simp
  12.644 -
  12.645 -lemma simple_integral_indicator:
  12.646 -  assumes A: "A \<in> sets M"
  12.647 -  assumes f: "simple_function M f"
  12.648 -  shows "(\<integral>\<^sup>Sx. f x * indicator A x \<partial>M) =
  12.649 -    (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M \<inter> A))"
  12.650 -proof -
  12.651 -  have eq: "(\<lambda>x. (f x, indicator A x)) ` space M \<inter> {x. snd x = 1} = (\<lambda>x. (f x, 1::ereal))`A"
  12.652 -    using A[THEN sets.sets_into_space] by (auto simp: indicator_def image_iff split: split_if_asm)
  12.653 -  have eq2: "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
  12.654 -    by (auto simp: image_iff)
  12.655 -
  12.656 -  have "(\<integral>\<^sup>Sx. f x * indicator A x \<partial>M) =
  12.657 -    (\<Sum>y\<in>(\<lambda>x. (f x, indicator A x))`space M. (fst y * snd y) * emeasure M {x\<in>space M. (f x, indicator A x) = y})"
  12.658 -    using assms by (intro simple_function_partition) auto
  12.659 -  also have "\<dots> = (\<Sum>y\<in>(\<lambda>x. (f x, indicator A x::ereal))`space M.
  12.660 -    if snd y = 1 then fst y * emeasure M (f -` {fst y} \<inter> space M \<inter> A) else 0)"
  12.661 -    by (auto simp: indicator_def split: split_if_asm intro!: arg_cong2[where f="op *"] arg_cong2[where f=emeasure] setsum_cong)
  12.662 -  also have "\<dots> = (\<Sum>y\<in>(\<lambda>x. (f x, 1::ereal))`A. fst y * emeasure M (f -` {fst y} \<inter> space M \<inter> A))"
  12.663 -    using assms by (subst setsum_cases) (auto intro!: simple_functionD(1) simp: eq)
  12.664 -  also have "\<dots> = (\<Sum>y\<in>fst`(\<lambda>x. (f x, 1::ereal))`A. y * emeasure M (f -` {y} \<inter> space M \<inter> A))"
  12.665 -    by (subst setsum_reindex[where f=fst]) (auto simp: inj_on_def)
  12.666 -  also have "\<dots> = (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M \<inter> A))"
  12.667 -    using A[THEN sets.sets_into_space]
  12.668 -    by (intro setsum_mono_zero_cong_left simple_functionD f) (auto simp: image_comp comp_def eq2)
  12.669 -  finally show ?thesis .
  12.670 -qed
  12.671 -
  12.672 -lemma simple_integral_indicator_only[simp]:
  12.673 -  assumes "A \<in> sets M"
  12.674 -  shows "integral\<^sup>S M (indicator A) = emeasure M A"
  12.675 -  using simple_integral_indicator[OF assms, of "\<lambda>x. 1"] sets.sets_into_space[OF assms]
  12.676 -  by (simp_all add: image_constant_conv Int_absorb1 split: split_if_asm)
  12.677 -
  12.678 -lemma simple_integral_null_set:
  12.679 -  assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets M"
  12.680 -  shows "(\<integral>\<^sup>Sx. u x * indicator N x \<partial>M) = 0"
  12.681 -proof -
  12.682 -  have "AE x in M. indicator N x = (0 :: ereal)"
  12.683 -    using `N \<in> null_sets M` by (auto simp: indicator_def intro!: AE_I[of _ _ N])
  12.684 -  then have "(\<integral>\<^sup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^sup>Sx. 0 \<partial>M)"
  12.685 -    using assms apply (intro simple_integral_cong_AE) by auto
  12.686 -  then show ?thesis by simp
  12.687 -qed
  12.688 -
  12.689 -lemma simple_integral_cong_AE_mult_indicator:
  12.690 -  assumes sf: "simple_function M f" and eq: "AE x in M. x \<in> S" and "S \<in> sets M"
  12.691 -  shows "integral\<^sup>S M f = (\<integral>\<^sup>Sx. f x * indicator S x \<partial>M)"
  12.692 -  using assms by (intro simple_integral_cong_AE) auto
  12.693 -
  12.694 -lemma simple_integral_cmult_indicator:
  12.695 -  assumes A: "A \<in> sets M"
  12.696 -  shows "(\<integral>\<^sup>Sx. c * indicator A x \<partial>M) = c * emeasure M A"
  12.697 -  using simple_integral_mult[OF simple_function_indicator[OF A]]
  12.698 -  unfolding simple_integral_indicator_only[OF A] by simp
  12.699 -
  12.700 -lemma simple_integral_positive:
  12.701 -  assumes f: "simple_function M f" and ae: "AE x in M. 0 \<le> f x"
  12.702 -  shows "0 \<le> integral\<^sup>S M f"
  12.703 -proof -
  12.704 -  have "integral\<^sup>S M (\<lambda>x. 0) \<le> integral\<^sup>S M f"
  12.705 -    using simple_integral_mono_AE[OF _ f ae] by auto
  12.706 -  then show ?thesis by simp
  12.707 -qed
  12.708 -
  12.709 -section "Continuous positive integration"
  12.710 -
  12.711 -definition positive_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^sup>P") where
  12.712 -  "integral\<^sup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^sup>S M g)"
  12.713 -
  12.714 -syntax
  12.715 -  "_positive_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^sup>+ _. _ \<partial>_" [60,61] 110)
  12.716 -
  12.717 -translations
  12.718 -  "\<integral>\<^sup>+ x. f \<partial>M" == "CONST positive_integral M (%x. f)"
  12.719 -
  12.720 -lemma positive_integral_positive:
  12.721 -  "0 \<le> integral\<^sup>P M f"
  12.722 -  by (auto intro!: SUP_upper2[of "\<lambda>x. 0"] simp: positive_integral_def le_fun_def)
  12.723 -
  12.724 -lemma positive_integral_not_MInfty[simp]: "integral\<^sup>P M f \<noteq> -\<infinity>"
  12.725 -  using positive_integral_positive[of M f] by auto
  12.726 -
  12.727 -lemma positive_integral_def_finite:
  12.728 -  "integral\<^sup>P M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^sup>S M g)"
  12.729 -    (is "_ = SUPREMUM ?A ?f")
  12.730 -  unfolding positive_integral_def
  12.731 -proof (safe intro!: antisym SUP_least)
  12.732 -  fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
  12.733 -  let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
  12.734 -  note gM = g(1)[THEN borel_measurable_simple_function]
  12.735 -  have \<mu>_G_pos: "0 \<le> (emeasure M) ?G" using gM by auto
  12.736 -  let ?g = "\<lambda>y x. if g x = \<infinity> then y else max 0 (g x)"
  12.737 -  from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
  12.738 -    apply (safe intro!: simple_function_max simple_function_If)
  12.739 -    apply (force simp: max_def le_fun_def split: split_if_asm)+
  12.740 -    done
  12.741 -  show "integral\<^sup>S M g \<le> SUPREMUM ?A ?f"
  12.742 -  proof cases
  12.743 -    have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
  12.744 -    assume "(emeasure M) ?G = 0"
  12.745 -    with gM have "AE x in M. x \<notin> ?G"
  12.746 -      by (auto simp add: AE_iff_null intro!: null_setsI)
  12.747 -    with gM g show ?thesis
  12.748 -      by (intro SUP_upper2[OF g0] simple_integral_mono_AE)
  12.749 -         (auto simp: max_def intro!: simple_function_If)
  12.750 -  next
  12.751 -    assume \<mu>_G: "(emeasure M) ?G \<noteq> 0"
  12.752 -    have "SUPREMUM ?A (integral\<^sup>S M) = \<infinity>"
  12.753 -    proof (intro SUP_PInfty)
  12.754 -      fix n :: nat
  12.755 -      let ?y = "ereal (real n) / (if (emeasure M) ?G = \<infinity> then 1 else (emeasure M) ?G)"
  12.756 -      have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>_G \<mu>_G_pos by (auto simp: ereal_divide_eq)
  12.757 -      then have "?g ?y \<in> ?A" by (rule g_in_A)
  12.758 -      have "real n \<le> ?y * (emeasure M) ?G"
  12.759 -        using \<mu>_G \<mu>_G_pos by (cases "(emeasure M) ?G") (auto simp: field_simps)
  12.760 -      also have "\<dots> = (\<integral>\<^sup>Sx. ?y * indicator ?G x \<partial>M)"
  12.761 -        using `0 \<le> ?y` `?g ?y \<in> ?A` gM
  12.762 -        by (subst simple_integral_cmult_indicator) auto
  12.763 -      also have "\<dots> \<le> integral\<^sup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
  12.764 -        by (intro simple_integral_mono) auto
  12.765 -      finally show "\<exists>i\<in>?A. real n \<le> integral\<^sup>S M i"
  12.766 -        using `?g ?y \<in> ?A` by blast
  12.767 -    qed
  12.768 -    then show ?thesis by simp
  12.769 -  qed
  12.770 -qed (auto intro: SUP_upper)
  12.771 -
  12.772 -lemma positive_integral_mono_AE:
  12.773 -  assumes ae: "AE x in M. u x \<le> v x" shows "integral\<^sup>P M u \<le> integral\<^sup>P M v"
  12.774 -  unfolding positive_integral_def
  12.775 -proof (safe intro!: SUP_mono)
  12.776 -  fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
  12.777 -  from ae[THEN AE_E] guess N . note N = this
  12.778 -  then have ae_N: "AE x in M. x \<notin> N" by (auto intro: AE_not_in)
  12.779 -  let ?n = "\<lambda>x. n x * indicator (space M - N) x"
  12.780 -  have "AE x in M. n x \<le> ?n x" "simple_function M ?n"
  12.781 -    using n N ae_N by auto
  12.782 -  moreover
  12.783 -  { fix x have "?n x \<le> max 0 (v x)"
  12.784 -    proof cases
  12.785 -      assume x: "x \<in> space M - N"
  12.786 -      with N have "u x \<le> v x" by auto
  12.787 -      with n(2)[THEN le_funD, of x] x show ?thesis
  12.788 -        by (auto simp: max_def split: split_if_asm)
  12.789 -    qed simp }
  12.790 -  then have "?n \<le> max 0 \<circ> v" by (auto simp: le_funI)
  12.791 -  moreover have "integral\<^sup>S M n \<le> integral\<^sup>S M ?n"
  12.792 -    using ae_N N n by (auto intro!: simple_integral_mono_AE)
  12.793 -  ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^sup>S M n \<le> integral\<^sup>S M m"
  12.794 -    by force
  12.795 -qed
  12.796 -
  12.797 -lemma positive_integral_mono:
  12.798 -  "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^sup>P M u \<le> integral\<^sup>P M v"
  12.799 -  by (auto intro: positive_integral_mono_AE)
  12.800 -
  12.801 -lemma positive_integral_cong_AE:
  12.802 -  "AE x in M. u x = v x \<Longrightarrow> integral\<^sup>P M u = integral\<^sup>P M v"
  12.803 -  by (auto simp: eq_iff intro!: positive_integral_mono_AE)
  12.804 -
  12.805 -lemma positive_integral_cong:
  12.806 -  "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^sup>P M u = integral\<^sup>P M v"
  12.807 -  by (auto intro: positive_integral_cong_AE)
  12.808 -
  12.809 -lemma positive_integral_eq_simple_integral:
  12.810 -  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^sup>P M f = integral\<^sup>S M f"
  12.811 -proof -
  12.812 -  let ?f = "\<lambda>x. f x * indicator (space M) x"
  12.813 -  have f': "simple_function M ?f" using f by auto
  12.814 -  with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
  12.815 -    by (auto simp: fun_eq_iff max_def split: split_indicator)
  12.816 -  have "integral\<^sup>P M ?f \<le> integral\<^sup>S M ?f" using f'
  12.817 -    by (force intro!: SUP_least simple_integral_mono simp: le_fun_def positive_integral_def)
  12.818 -  moreover have "integral\<^sup>S M ?f \<le> integral\<^sup>P M ?f"
  12.819 -    unfolding positive_integral_def
  12.820 -    using f' by (auto intro!: SUP_upper)
  12.821 -  ultimately show ?thesis
  12.822 -    by (simp cong: positive_integral_cong simple_integral_cong)
  12.823 -qed
  12.824 -
  12.825 -lemma positive_integral_eq_simple_integral_AE:
  12.826 -  assumes f: "simple_function M f" "AE x in M. 0 \<le> f x" shows "integral\<^sup>P M f = integral\<^sup>S M f"
  12.827 -proof -
  12.828 -  have "AE x in M. f x = max 0 (f x)" using f by (auto split: split_max)
  12.829 -  with f have "integral\<^sup>P M f = integral\<^sup>S M (\<lambda>x. max 0 (f x))"
  12.830 -    by (simp cong: positive_integral_cong_AE simple_integral_cong_AE
  12.831 -             add: positive_integral_eq_simple_integral)
  12.832 -  with assms show ?thesis
  12.833 -    by (auto intro!: simple_integral_cong_AE split: split_max)
  12.834 -qed
  12.835 -
  12.836 -lemma positive_integral_SUP_approx:
  12.837 -  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  12.838 -  and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
  12.839 -  shows "integral\<^sup>S M u \<le> (SUP i. integral\<^sup>P M (f i))" (is "_ \<le> ?S")
  12.840 -proof (rule ereal_le_mult_one_interval)
  12.841 -  have "0 \<le> (SUP i. integral\<^sup>P M (f i))"
  12.842 -    using f(3) by (auto intro!: SUP_upper2 positive_integral_positive)
  12.843 -  then show "(SUP i. integral\<^sup>P M (f i)) \<noteq> -\<infinity>" by auto
  12.844 -  have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
  12.845 -    using u(3) by auto
  12.846 -  fix a :: ereal assume "0 < a" "a < 1"
  12.847 -  hence "a \<noteq> 0" by auto
  12.848 -  let ?B = "\<lambda>i. {x \<in> space M. a * u x \<le> f i x}"
  12.849 -  have B: "\<And>i. ?B i \<in> sets M"
  12.850 -    using f `simple_function M u`[THEN borel_measurable_simple_function] by auto
  12.851 -
  12.852 -  let ?uB = "\<lambda>i x. u x * indicator (?B i) x"
  12.853 -
  12.854 -  { fix i have "?B i \<subseteq> ?B (Suc i)"
  12.855 -    proof safe
  12.856 -      fix i x assume "a * u x \<le> f i x"
  12.857 -      also have "\<dots> \<le> f (Suc i) x"
  12.858 -        using `incseq f`[THEN incseq_SucD] unfolding le_fun_def by auto
  12.859 -      finally show "a * u x \<le> f (Suc i) x" .
  12.860 -    qed }
  12.861 -  note B_mono = this
  12.862 -
  12.863 -  note B_u = sets.Int[OF u(1)[THEN simple_functionD(2)] B]
  12.864 -
  12.865 -  let ?B' = "\<lambda>i n. (u -` {i} \<inter> space M) \<inter> ?B n"
  12.866 -  have measure_conv: "\<And>i. (emeasure M) (u -` {i} \<inter> space M) = (SUP n. (emeasure M) (?B' i n))"
  12.867 -  proof -
  12.868 -    fix i
  12.869 -    have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
  12.870 -    have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
  12.871 -    have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
  12.872 -    proof safe
  12.873 -      fix x i assume x: "x \<in> space M"
  12.874 -      show "x \<in> (\<Union>i. ?B' (u x) i)"
  12.875 -      proof cases
  12.876 -        assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
  12.877 -      next
  12.878 -        assume "u x \<noteq> 0"
  12.879 -        with `a < 1` u_range[OF `x \<in> space M`]
  12.880 -        have "a * u x < 1 * u x"
  12.881 -          by (intro ereal_mult_strict_right_mono) (auto simp: image_iff)
  12.882 -        also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def)
  12.883 -        finally obtain i where "a * u x < f i x" unfolding SUP_def
  12.884 -          by (auto simp add: less_SUP_iff)
  12.885 -        hence "a * u x \<le> f i x" by auto
  12.886 -        thus ?thesis using `x \<in> space M` by auto
  12.887 -      qed
  12.888 -    qed
  12.889 -    then show "?thesis i" using SUP_emeasure_incseq[OF 1 2] by simp
  12.890 -  qed
  12.891 -
  12.892 -  have "integral\<^sup>S M u = (SUP i. integral\<^sup>S M (?uB i))"
  12.893 -    unfolding simple_integral_indicator[OF B `simple_function M u`]
  12.894 -  proof (subst SUP_ereal_setsum, safe)
  12.895 -    fix x n assume "x \<in> space M"
  12.896 -    with u_range show "incseq (\<lambda>i. u x * (emeasure M) (?B' (u x) i))" "\<And>i. 0 \<le> u x * (emeasure M) (?B' (u x) i)"
  12.897 -      using B_mono B_u by (auto intro!: emeasure_mono ereal_mult_left_mono incseq_SucI simp: ereal_zero_le_0_iff)
  12.898 -  next
  12.899 -    show "integral\<^sup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * (emeasure M) (?B' i n))"
  12.900 -      using measure_conv u_range B_u unfolding simple_integral_def
  12.901 -      by (auto intro!: setsum_cong SUP_ereal_cmult [symmetric])
  12.902 -  qed
  12.903 -  moreover
  12.904 -  have "a * (SUP i. integral\<^sup>S M (?uB i)) \<le> ?S"
  12.905 -    apply (subst SUP_ereal_cmult [symmetric])
  12.906 -  proof (safe intro!: SUP_mono bexI)
  12.907 -    fix i
  12.908 -    have "a * integral\<^sup>S M (?uB i) = (\<integral>\<^sup>Sx. a * ?uB i x \<partial>M)"
  12.909 -      using B `simple_function M u` u_range
  12.910 -      by (subst simple_integral_mult) (auto split: split_indicator)
  12.911 -    also have "\<dots> \<le> integral\<^sup>P M (f i)"
  12.912 -    proof -
  12.913 -      have *: "simple_function M (\<lambda>x. a * ?uB i x)" using B `0 < a` u(1) by auto
  12.914 -      show ?thesis using f(3) * u_range `0 < a`
  12.915 -        by (subst positive_integral_eq_simple_integral[symmetric])
  12.916 -           (auto intro!: positive_integral_mono split: split_indicator)
  12.917 -    qed
  12.918 -    finally show "a * integral\<^sup>S M (?uB i) \<le> integral\<^sup>P M (f i)"
  12.919 -      by auto
  12.920 -  next
  12.921 -    fix i show "0 \<le> \<integral>\<^sup>S x. ?uB i x \<partial>M" using B `0 < a` u(1) u_range
  12.922 -      by (intro simple_integral_positive) (auto split: split_indicator)
  12.923 -  qed (insert `0 < a`, auto)
  12.924 -  ultimately show "a * integral\<^sup>S M u \<le> ?S" by simp
  12.925 -qed
  12.926 -
  12.927 -lemma incseq_positive_integral:
  12.928 -  assumes "incseq f" shows "incseq (\<lambda>i. integral\<^sup>P M (f i))"
  12.929 -proof -
  12.930 -  have "\<And>i x. f i x \<le> f (Suc i) x"
  12.931 -    using assms by (auto dest!: incseq_SucD simp: le_fun_def)
  12.932 -  then show ?thesis
  12.933 -    by (auto intro!: incseq_SucI positive_integral_mono)
  12.934 -qed
  12.935 -
  12.936 -text {* Beppo-Levi monotone convergence theorem *}
  12.937 -lemma positive_integral_monotone_convergence_SUP:
  12.938 -  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
  12.939 -  shows "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^sup>P M (f i))"
  12.940 -proof (rule antisym)
  12.941 -  show "(SUP j. integral\<^sup>P M (f j)) \<le> (\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M)"
  12.942 -    by (auto intro!: SUP_least SUP_upper positive_integral_mono)
  12.943 -next
  12.944 -  show "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^sup>P M (f j))"
  12.945 -    unfolding positive_integral_def_finite[of _ "\<lambda>x. SUP i. f i x"]
  12.946 -  proof (safe intro!: SUP_least)
  12.947 -    fix g assume g: "simple_function M g"
  12.948 -      and *: "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
  12.949 -    then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
  12.950 -      using f by (auto intro!: SUP_upper2)
  12.951 -    with * show "integral\<^sup>S M g \<le> (SUP j. integral\<^sup>P M (f j))"
  12.952 -      by (intro  positive_integral_SUP_approx[OF f g _ g'])
  12.953 -         (auto simp: le_fun_def max_def)
  12.954 -  qed
  12.955 -qed
  12.956 -
  12.957 -lemma positive_integral_monotone_convergence_SUP_AE:
  12.958 -  assumes f: "\<And>i. AE x in M. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
  12.959 -  shows "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^sup>P M (f i))"
  12.960 -proof -
  12.961 -  from f have "AE x in M. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
  12.962 -    by (simp add: AE_all_countable)
  12.963 -  from this[THEN AE_E] guess N . note N = this
  12.964 -  let ?f = "\<lambda>i x. if x \<in> space M - N then f i x else 0"
  12.965 -  have f_eq: "AE x in M. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ _ N])
  12.966 -  then have "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^sup>+ x. (SUP i. ?f i x) \<partial>M)"
  12.967 -    by (auto intro!: positive_integral_cong_AE)
  12.968 -  also have "\<dots> = (SUP i. (\<integral>\<^sup>+ x. ?f i x \<partial>M))"
  12.969 -  proof (rule positive_integral_monotone_convergence_SUP)
  12.970 -    show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
  12.971 -    { fix i show "(\<lambda>x. if x \<in> space M - N then f i x else 0) \<in> borel_measurable M"
  12.972 -        using f N(3) by (intro measurable_If_set) auto
  12.973 -      fix x show "0 \<le> ?f i x"
  12.974 -        using N(1) by auto }
  12.975 -  qed
  12.976 -  also have "\<dots> = (SUP i. (\<integral>\<^sup>+ x. f i x \<partial>M))"
  12.977 -    using f_eq by (force intro!: arg_cong[where f="SUPREMUM UNIV"] positive_integral_cong_AE ext)
  12.978 -  finally show ?thesis .
  12.979 -qed
  12.980 -
  12.981 -lemma positive_integral_monotone_convergence_SUP_AE_incseq:
  12.982 -  assumes f: "incseq f" "\<And>i. AE x in M. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
  12.983 -  shows "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^sup>P M (f i))"
  12.984 -  using f[unfolded incseq_Suc_iff le_fun_def]
  12.985 -  by (intro positive_integral_monotone_convergence_SUP_AE[OF _ borel])
  12.986 -     auto
  12.987 -
  12.988 -lemma positive_integral_monotone_convergence_simple:
  12.989 -  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
  12.990 -  shows "(SUP i. integral\<^sup>S M (f i)) = (\<integral>\<^sup>+x. (SUP i. f i x) \<partial>M)"
  12.991 -  using assms unfolding positive_integral_monotone_convergence_SUP[OF f(1)
  12.992 -    f(3)[THEN borel_measurable_simple_function] f(2)]
  12.993 -  by (auto intro!: positive_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPREMUM UNIV"] ext)
  12.994 -
  12.995 -lemma positive_integral_max_0:
  12.996 -  "(\<integral>\<^sup>+x. max 0 (f x) \<partial>M) = integral\<^sup>P M f"
  12.997 -  by (simp add: le_fun_def positive_integral_def)
  12.998 -
  12.999 -lemma positive_integral_cong_pos:
 12.1000 -  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
 12.1001 -  shows "integral\<^sup>P M f = integral\<^sup>P M g"
 12.1002 -proof -
 12.1003 -  have "integral\<^sup>P M (\<lambda>x. max 0 (f x)) = integral\<^sup>P M (\<lambda>x. max 0 (g x))"
 12.1004 -  proof (intro positive_integral_cong)
 12.1005 -    fix x assume "x \<in> space M"
 12.1006 -    from assms[OF this] show "max 0 (f x) = max 0 (g x)"
 12.1007 -      by (auto split: split_max)
 12.1008 -  qed
 12.1009 -  then show ?thesis by (simp add: positive_integral_max_0)
 12.1010 -qed
 12.1011 -
 12.1012 -lemma SUP_simple_integral_sequences:
 12.1013 -  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
 12.1014 -  and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
 12.1015 -  and eq: "AE x in M. (SUP i. f i x) = (SUP i. g i x)"
 12.1016 -  shows "(SUP i. integral\<^sup>S M (f i)) = (SUP i. integral\<^sup>S M (g i))"
 12.1017 -    (is "SUPREMUM _ ?F = SUPREMUM _ ?G")
 12.1018 -proof -
 12.1019 -  have "(SUP i. integral\<^sup>S M (f i)) = (\<integral>\<^sup>+x. (SUP i. f i x) \<partial>M)"
 12.1020 -    using f by (rule positive_integral_monotone_convergence_simple)
 12.1021 -  also have "\<dots> = (\<integral>\<^sup>+x. (SUP i. g i x) \<partial>M)"
 12.1022 -    unfolding eq[THEN positive_integral_cong_AE] ..
 12.1023 -  also have "\<dots> = (SUP i. ?G i)"
 12.1024 -    using g by (rule positive_integral_monotone_convergence_simple[symmetric])
 12.1025 -  finally show ?thesis by simp
 12.1026 -qed
 12.1027 -
 12.1028 -lemma positive_integral_const[simp]:
 12.1029 -  "0 \<le> c \<Longrightarrow> (\<integral>\<^sup>+ x. c \<partial>M) = c * (emeasure M) (space M)"
 12.1030 -  by (subst positive_integral_eq_simple_integral) auto
 12.1031 -
 12.1032 -lemma positive_integral_linear:
 12.1033 -  assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
 12.1034 -  and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
 12.1035 -  shows "(\<integral>\<^sup>+ x. a * f x + g x \<partial>M) = a * integral\<^sup>P M f + integral\<^sup>P M g"
 12.1036 -    (is "integral\<^sup>P M ?L = _")
 12.1037 -proof -
 12.1038 -  from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess u .
 12.1039 -  note u = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
 12.1040 -  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess v .
 12.1041 -  note v = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
 12.1042 -  let ?L' = "\<lambda>i x. a * u i x + v i x"
 12.1043 -
 12.1044 -  have "?L \<in> borel_measurable M" using assms by auto
 12.1045 -  from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
 12.1046 -  note l = positive_integral_monotone_convergence_simple[OF this(2,5,1)] this
 12.1047 -
 12.1048 -  have inc: "incseq (\<lambda>i. a * integral\<^sup>S M (u i))" "incseq (\<lambda>i. integral\<^sup>S M (v i))"
 12.1049 -    using u v `0 \<le> a`
 12.1050 -    by (auto simp: incseq_Suc_iff le_fun_def
 12.1051 -             intro!: add_mono ereal_mult_left_mono simple_integral_mono)
 12.1052 -  have pos: "\<And>i. 0 \<le> integral\<^sup>S M (u i)" "\<And>i. 0 \<le> integral\<^sup>S M (v i)" "\<And>i. 0 \<le> a * integral\<^sup>S M (u i)"
 12.1053 -    using u v `0 \<le> a` by (auto simp: simple_integral_positive)
 12.1054 -  { fix i from pos[of i] have "a * integral\<^sup>S M (u i) \<noteq> -\<infinity>" "integral\<^sup>S M (v i) \<noteq> -\<infinity>"
 12.1055 -      by (auto split: split_if_asm) }
 12.1056 -  note not_MInf = this
 12.1057 -
 12.1058 -  have l': "(SUP i. integral\<^sup>S M (l i)) = (SUP i. integral\<^sup>S M (?L' i))"
 12.1059 -  proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
 12.1060 -    show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
 12.1061 -      using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
 12.1062 -      by (auto intro!: add_mono ereal_mult_left_mono)
 12.1063 -    { fix x
 12.1064 -      { fix i have "a * u i x \<noteq> -\<infinity>" "v i x \<noteq> -\<infinity>" "u i x \<noteq> -\<infinity>" using `0 \<le> a` u(6)[of i x] v(6)[of i x]
 12.1065 -          by auto }
 12.1066 -      then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
 12.1067 -        using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
 12.1068 -        by (subst SUP_ereal_cmult [symmetric, OF u(6) `0 \<le> a`])
 12.1069 -           (auto intro!: SUP_ereal_add
 12.1070 -                 simp: incseq_Suc_iff le_fun_def add_mono ereal_mult_left_mono) }
 12.1071 -    then show "AE x in M. (SUP i. l i x) = (SUP i. ?L' i x)"
 12.1072 -      unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
 12.1073 -      by (intro AE_I2) (auto split: split_max)
 12.1074 -  qed
 12.1075 -  also have "\<dots> = (SUP i. a * integral\<^sup>S M (u i) + integral\<^sup>S M (v i))"
 12.1076 -    using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPREMUM UNIV"] ext)
 12.1077 -  finally have "(\<integral>\<^sup>+ x. max 0 (a * f x + g x) \<partial>M) = a * (\<integral>\<^sup>+x. max 0 (f x) \<partial>M) + (\<integral>\<^sup>+x. max 0 (g x) \<partial>M)"
 12.1078 -    unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
 12.1079 -    unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
 12.1080 -    apply (subst SUP_ereal_cmult [symmetric, OF pos(1) `0 \<le> a`])
 12.1081 -    apply (subst SUP_ereal_add [symmetric, OF inc not_MInf]) .
 12.1082 -  then show ?thesis by (simp add: positive_integral_max_0)
 12.1083 -qed
 12.1084 -
 12.1085 -lemma positive_integral_cmult:
 12.1086 -  assumes f: "f \<in> borel_measurable M" "0 \<le> c"
 12.1087 -  shows "(\<integral>\<^sup>+ x. c * f x \<partial>M) = c * integral\<^sup>P M f"
 12.1088 -proof -
 12.1089 -  have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
 12.1090 -    by (auto split: split_max simp: ereal_zero_le_0_iff)
 12.1091 -  have "(\<integral>\<^sup>+ x. c * f x \<partial>M) = (\<integral>\<^sup>+ x. c * max 0 (f x) \<partial>M)"
 12.1092 -    by (simp add: positive_integral_max_0)
 12.1093 -  then show ?thesis
 12.1094 -    using positive_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" _ "\<lambda>x. 0"] f
 12.1095 -    by (auto simp: positive_integral_max_0)
 12.1096 -qed
 12.1097 -
 12.1098 -lemma positive_integral_multc:
 12.1099 -  assumes "f \<in> borel_measurable M" "0 \<le> c"
 12.1100 -  shows "(\<integral>\<^sup>+ x. f x * c \<partial>M) = integral\<^sup>P M f * c"
 12.1101 -  unfolding mult_commute[of _ c] positive_integral_cmult[OF assms] by simp
 12.1102 -
 12.1103 -lemma positive_integral_indicator[simp]:
 12.1104 -  "A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+ x. indicator A x\<partial>M) = (emeasure M) A"
 12.1105 -  by (subst positive_integral_eq_simple_integral)
 12.1106 -     (auto simp: simple_integral_indicator)
 12.1107 -
 12.1108 -lemma positive_integral_cmult_indicator:
 12.1109 -  "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+ x. c * indicator A x \<partial>M) = c * (emeasure M) A"
 12.1110 -  by (subst positive_integral_eq_simple_integral)
 12.1111 -     (auto simp: simple_function_indicator simple_integral_indicator)
 12.1112 -
 12.1113 -lemma positive_integral_indicator':
 12.1114 -  assumes [measurable]: "A \<inter> space M \<in> sets M"
 12.1115 -  shows "(\<integral>\<^sup>+ x. indicator A x \<partial>M) = emeasure M (A \<inter> space M)"
 12.1116 -proof -
 12.1117 -  have "(\<integral>\<^sup>+ x. indicator A x \<partial>M) = (\<integral>\<^sup>+ x. indicator (A \<inter> space M) x \<partial>M)"
 12.1118 -    by (intro positive_integral_cong) (simp split: split_indicator)
 12.1119 -  also have "\<dots> = emeasure M (A \<inter> space M)"
 12.1120 -    by simp
 12.1121 -  finally show ?thesis .
 12.1122 -qed
 12.1123 -
 12.1124 -lemma positive_integral_add:
 12.1125 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
 12.1126 -  and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
 12.1127 -  shows "(\<integral>\<^sup>+ x. f x + g x \<partial>M) = integral\<^sup>P M f + integral\<^sup>P M g"
 12.1128 -proof -
 12.1129 -  have ae: "AE x in M. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
 12.1130 -    using assms by (auto split: split_max)
 12.1131 -  have "(\<integral>\<^sup>+ x. f x + g x \<partial>M) = (\<integral>\<^sup>+ x. max 0 (f x + g x) \<partial>M)"
 12.1132 -    by (simp add: positive_integral_max_0)
 12.1133 -  also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
 12.1134 -    unfolding ae[THEN positive_integral_cong_AE] ..
 12.1135 -  also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^sup>+ x. max 0 (g x) \<partial>M)"
 12.1136 -    using positive_integral_linear[of "\<lambda>x. max 0 (f x)" _ 1 "\<lambda>x. max 0 (g x)"] f g
 12.1137 -    by auto
 12.1138 -  finally show ?thesis
 12.1139 -    by (simp add: positive_integral_max_0)
 12.1140 -qed
 12.1141 -
 12.1142 -lemma positive_integral_setsum:
 12.1143 -  assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x in M. 0 \<le> f i x"
 12.1144 -  shows "(\<integral>\<^sup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^sup>P M (f i))"
 12.1145 -proof cases
 12.1146 -  assume f: "finite P"
 12.1147 -  from assms have "AE x in M. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
 12.1148 -  from f this assms(1) show ?thesis
 12.1149 -  proof induct
 12.1150 -    case (insert i P)
 12.1151 -    then have "f i \<in> borel_measurable M" "AE x in M. 0 \<le> f i x"
 12.1152 -      "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x in M. 0 \<le> (\<Sum>i\<in>P. f i x)"
 12.1153 -      by (auto intro!: setsum_nonneg)
 12.1154 -    from positive_integral_add[OF this]
 12.1155 -    show ?case using insert by auto
 12.1156 -  qed simp
 12.1157 -qed simp
 12.1158 -
 12.1159 -lemma positive_integral_Markov_inequality:
 12.1160 -  assumes u: "u \<in> borel_measurable M" "AE x in M. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c"
 12.1161 -  shows "(emeasure M) ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^sup>+ x. u x * indicator A x \<partial>M)"
 12.1162 -    (is "(emeasure M) ?A \<le> _ * ?PI")
 12.1163 -proof -
 12.1164 -  have "?A \<in> sets M"
 12.1165 -    using `A \<in> sets M` u by auto
 12.1166 -  hence "(emeasure M) ?A = (\<integral>\<^sup>+ x. indicator ?A x \<partial>M)"
 12.1167 -    using positive_integral_indicator by simp
 12.1168 -  also have "\<dots> \<le> (\<integral>\<^sup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
 12.1169 -    by (auto intro!: positive_integral_mono_AE
 12.1170 -      simp: indicator_def ereal_zero_le_0_iff)
 12.1171 -  also have "\<dots> = c * (\<integral>\<^sup>+ x. u x * indicator A x \<partial>M)"
 12.1172 -    using assms
 12.1173 -    by (auto intro!: positive_integral_cmult simp: ereal_zero_le_0_iff)
 12.1174 -  finally show ?thesis .
 12.1175 -qed
 12.1176 -
 12.1177 -lemma positive_integral_noteq_infinite:
 12.1178 -  assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
 12.1179 -  and "integral\<^sup>P M g \<noteq> \<infinity>"
 12.1180 -  shows "AE x in M. g x \<noteq> \<infinity>"
 12.1181 -proof (rule ccontr)
 12.1182 -  assume c: "\<not> (AE x in M. g x \<noteq> \<infinity>)"
 12.1183 -  have "(emeasure M) {x\<in>space M. g x = \<infinity>} \<noteq> 0"
 12.1184 -    using c g by (auto simp add: AE_iff_null)
 12.1185 -  moreover have "0 \<le> (emeasure M) {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
 12.1186 -  ultimately have "0 < (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
 12.1187 -  then have "\<infinity> = \<infinity> * (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
 12.1188 -  also have "\<dots> \<le> (\<integral>\<^sup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
 12.1189 -    using g by (subst positive_integral_cmult_indicator) auto
 12.1190 -  also have "\<dots> \<le> integral\<^sup>P M g"
 12.1191 -    using assms by (auto intro!: positive_integral_mono_AE simp: indicator_def)
 12.1192 -  finally show False using `integral\<^sup>P M g \<noteq> \<infinity>` by auto
 12.1193 -qed
 12.1194 -
 12.1195 -lemma positive_integral_PInf:
 12.1196 -  assumes f: "f \<in> borel_measurable M"
 12.1197 -  and not_Inf: "integral\<^sup>P M f \<noteq> \<infinity>"
 12.1198 -  shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
 12.1199 -proof -
 12.1200 -  have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) = (\<integral>\<^sup>+ x. \<infinity> * indicator (f -` {\<infinity>} \<inter> space M) x \<partial>M)"
 12.1201 -    using f by (subst positive_integral_cmult_indicator) (auto simp: measurable_sets)
 12.1202 -  also have "\<dots> \<le> integral\<^sup>P M (\<lambda>x. max 0 (f x))"
 12.1203 -    by (auto intro!: positive_integral_mono simp: indicator_def max_def)
 12.1204 -  finally have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) \<le> integral\<^sup>P M f"
 12.1205 -    by (simp add: positive_integral_max_0)
 12.1206 -  moreover have "0 \<le> (emeasure M) (f -` {\<infinity>} \<inter> space M)"
 12.1207 -    by (rule emeasure_nonneg)
 12.1208 -  ultimately show ?thesis
 12.1209 -    using assms by (auto split: split_if_asm)
 12.1210 -qed
 12.1211 -
 12.1212 -lemma positive_integral_PInf_AE:
 12.1213 -  assumes "f \<in> borel_measurable M" "integral\<^sup>P M f \<noteq> \<infinity>" shows "AE x in M. f x \<noteq> \<infinity>"
 12.1214 -proof (rule AE_I)
 12.1215 -  show "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
 12.1216 -    by (rule positive_integral_PInf[OF assms])
 12.1217 -  show "f -` {\<infinity>} \<inter> space M \<in> sets M"
 12.1218 -    using assms by (auto intro: borel_measurable_vimage)
 12.1219 -qed auto
 12.1220 -
 12.1221 -lemma simple_integral_PInf:
 12.1222 -  assumes "simple_function M f" "\<And>x. 0 \<le> f x"
 12.1223 -  and "integral\<^sup>S M f \<noteq> \<infinity>"
 12.1224 -  shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
 12.1225 -proof (rule positive_integral_PInf)
 12.1226 -  show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
 12.1227 -  show "integral\<^sup>P M f \<noteq> \<infinity>"
 12.1228 -    using assms by (simp add: positive_integral_eq_simple_integral)
 12.1229 -qed
 12.1230 -
 12.1231 -lemma positive_integral_diff:
 12.1232 -  assumes f: "f \<in> borel_measurable M"
 12.1233 -  and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
 12.1234 -  and fin: "integral\<^sup>P M g \<noteq> \<infinity>"
 12.1235 -  and mono: "AE x in M. g x \<le> f x"
 12.1236 -  shows "(\<integral>\<^sup>+ x. f x - g x \<partial>M) = integral\<^sup>P M f - integral\<^sup>P M g"
 12.1237 -proof -
 12.1238 -  have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x in M. 0 \<le> f x - g x"
 12.1239 -    using assms by (auto intro: ereal_diff_positive)
 12.1240 -  have pos_f: "AE x in M. 0 \<le> f x" using mono g by auto
 12.1241 -  { fix a b :: ereal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
 12.1242 -      by (cases rule: ereal2_cases[of a b]) auto }
 12.1243 -  note * = this
 12.1244 -  then have "AE x in M. f x = f x - g x + g x"
 12.1245 -    using mono positive_integral_noteq_infinite[OF g fin] assms by auto
 12.1246 -  then have **: "integral\<^sup>P M f = (\<integral>\<^sup>+x. f x - g x \<partial>M) + integral\<^sup>P M g"
 12.1247 -    unfolding positive_integral_add[OF diff g, symmetric]
 12.1248 -    by (rule positive_integral_cong_AE)
 12.1249 -  show ?thesis unfolding **
 12.1250 -    using fin positive_integral_positive[of M g]
 12.1251 -    by (cases rule: ereal2_cases[of "\<integral>\<^sup>+ x. f x - g x \<partial>M" "integral\<^sup>P M g"]) auto
 12.1252 -qed
 12.1253 -
 12.1254 -lemma positive_integral_suminf:
 12.1255 -  assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> f i x"
 12.1256 -  shows "(\<integral>\<^sup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^sup>P M (f i))"
 12.1257 -proof -
 12.1258 -  have all_pos: "AE x in M. \<forall>i. 0 \<le> f i x"
 12.1259 -    using assms by (auto simp: AE_all_countable)
 12.1260 -  have "(\<Sum>i. integral\<^sup>P M (f i)) = (SUP n. \<Sum>i<n. integral\<^sup>P M (f i))"
 12.1261 -    using positive_integral_positive by (rule suminf_ereal_eq_SUP)
 12.1262 -  also have "\<dots> = (SUP n. \<integral>\<^sup>+x. (\<Sum>i<n. f i x) \<partial>M)"
 12.1263 -    unfolding positive_integral_setsum[OF f] ..
 12.1264 -  also have "\<dots> = \<integral>\<^sup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
 12.1265 -    by (intro positive_integral_monotone_convergence_SUP_AE[symmetric])
 12.1266 -       (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
 12.1267 -  also have "\<dots> = \<integral>\<^sup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
 12.1268 -    by (intro positive_integral_cong_AE) (auto simp: suminf_ereal_eq_SUP)
 12.1269 -  finally show ?thesis by simp
 12.1270 -qed
 12.1271 -
 12.1272 -text {* Fatou's lemma: convergence theorem on limes inferior *}
 12.1273 -lemma positive_integral_lim_INF:
 12.1274 -  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
 12.1275 -  assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> u i x"
 12.1276 -  shows "(\<integral>\<^sup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^sup>P M (u n))"
 12.1277 -proof -
 12.1278 -  have pos: "AE x in M. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
 12.1279 -  have "(\<integral>\<^sup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
 12.1280 -    (SUP n. \<integral>\<^sup>+ x. (INF i:{n..}. u i x) \<partial>M)"
 12.1281 -    unfolding liminf_SUP_INF using pos u
 12.1282 -    by (intro positive_integral_monotone_convergence_SUP_AE)
 12.1283 -       (elim AE_mp, auto intro!: AE_I2 intro: INF_greatest INF_superset_mono)
 12.1284 -  also have "\<dots> \<le> liminf (\<lambda>n. integral\<^sup>P M (u n))"
 12.1285 -    unfolding liminf_SUP_INF
 12.1286 -    by (auto intro!: SUP_mono exI INF_greatest positive_integral_mono INF_lower)
 12.1287 -  finally show ?thesis .
 12.1288 -qed
 12.1289 -
 12.1290 -lemma positive_integral_null_set:
 12.1291 -  assumes "N \<in> null_sets M" shows "(\<integral>\<^sup>+ x. u x * indicator N x \<partial>M) = 0"
 12.1292 -proof -
 12.1293 -  have "(\<integral>\<^sup>+ x. u x * indicator N x \<partial>M) = (\<integral>\<^sup>+ x. 0 \<partial>M)"
 12.1294 -  proof (intro positive_integral_cong_AE AE_I)
 12.1295 -    show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
 12.1296 -      by (auto simp: indicator_def)
 12.1297 -    show "(emeasure M) N = 0" "N \<in> sets M"
 12.1298 -      using assms by auto
 12.1299 -  qed
 12.1300 -  then show ?thesis by simp
 12.1301 -qed
 12.1302 -
 12.1303 -lemma positive_integral_0_iff:
 12.1304 -  assumes u: "u \<in> borel_measurable M" and pos: "AE x in M. 0 \<le> u x"
 12.1305 -  shows "integral\<^sup>P M u = 0 \<longleftrightarrow> emeasure M {x\<in>space M. u x \<noteq> 0} = 0"
 12.1306 -    (is "_ \<longleftrightarrow> (emeasure M) ?A = 0")
 12.1307 -proof -
 12.1308 -  have u_eq: "(\<integral>\<^sup>+ x. u x * indicator ?A x \<partial>M) = integral\<^sup>P M u"
 12.1309 -    by (auto intro!: positive_integral_cong simp: indicator_def)
 12.1310 -  show ?thesis
 12.1311 -  proof
 12.1312 -    assume "(emeasure M) ?A = 0"
 12.1313 -    with positive_integral_null_set[of ?A M u] u
 12.1314 -    show "integral\<^sup>P M u = 0" by (simp add: u_eq null_sets_def)
 12.1315 -  next
 12.1316 -    { fix r :: ereal and n :: nat assume gt_1: "1 \<le> real n * r"
 12.1317 -      then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_ereal_def)
 12.1318 -      then have "0 \<le> r" by (auto simp add: ereal_zero_less_0_iff) }
 12.1319 -    note gt_1 = this
 12.1320 -    assume *: "integral\<^sup>P M u = 0"
 12.1321 -    let ?M = "\<lambda>n. {x \<in> space M. 1 \<le> real (n::nat) * u x}"
 12.1322 -    have "0 = (SUP n. (emeasure M) (?M n \<inter> ?A))"
 12.1323 -    proof -
 12.1324 -      { fix n :: nat
 12.1325 -        from positive_integral_Markov_inequality[OF u pos, of ?A "ereal (real n)"]
 12.1326 -        have "(emeasure M) (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
 12.1327 -        moreover have "0 \<le> (emeasure M) (?M n \<inter> ?A)" using u by auto
 12.1328 -        ultimately have "(emeasure M) (?M n \<inter> ?A) = 0" by auto }
 12.1329 -      thus ?thesis by simp
 12.1330 -    qed
 12.1331 -    also have "\<dots> = (emeasure M) (\<Union>n. ?M n \<inter> ?A)"
 12.1332 -    proof (safe intro!: SUP_emeasure_incseq)
 12.1333 -      fix n show "?M n \<inter> ?A \<in> sets M"
 12.1334 -        using u by (auto intro!: sets.Int)
 12.1335 -    next
 12.1336 -      show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
 12.1337 -      proof (safe intro!: incseq_SucI)
 12.1338 -        fix n :: nat and x
 12.1339 -        assume *: "1 \<le> real n * u x"
 12.1340 -        also from gt_1[OF *] have "real n * u x \<le> real (Suc n) * u x"
 12.1341 -          using `0 \<le> u x` by (auto intro!: ereal_mult_right_mono)
 12.1342 -        finally show "1 \<le> real (Suc n) * u x" by auto
 12.1343 -      qed
 12.1344 -    qed
 12.1345 -    also have "\<dots> = (emeasure M) {x\<in>space M. 0 < u x}"
 12.1346 -    proof (safe intro!: arg_cong[where f="(emeasure M)"] dest!: gt_1)
 12.1347 -      fix x assume "0 < u x" and [simp, intro]: "x \<in> space M"
 12.1348 -      show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
 12.1349 -      proof (cases "u x")
 12.1350 -        case (real r) with `0 < u x` have "0 < r" by auto
 12.1351 -        obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
 12.1352 -        hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
 12.1353 -        hence "1 \<le> real j * r" using real `0 < r` by auto
 12.1354 -        thus ?thesis using `0 < r` real by (auto simp: one_ereal_def)
 12.1355 -      qed (insert `0 < u x`, auto)
 12.1356 -    qed auto
 12.1357 -    finally have "(emeasure M) {x\<in>space M. 0 < u x} = 0" by simp
 12.1358 -    moreover
 12.1359 -    from pos have "AE x in M. \<not> (u x < 0)" by auto
 12.1360 -    then have "(emeasure M) {x\<in>space M. u x < 0} = 0"
 12.1361 -      using AE_iff_null[of M] u by auto
 12.1362 -    moreover have "(emeasure M) {x\<in>space M. u x \<noteq> 0} = (emeasure M) {x\<in>space M. u x < 0} + (emeasure M) {x\<in>space M. 0 < u x}"
 12.1363 -      using u by (subst plus_emeasure) (auto intro!: arg_cong[where f="emeasure M"])
 12.1364 -    ultimately show "(emeasure M) ?A = 0" by simp
 12.1365 -  qed
 12.1366 -qed
 12.1367 -
 12.1368 -lemma positive_integral_0_iff_AE:
 12.1369 -  assumes u: "u \<in> borel_measurable M"
 12.1370 -  shows "integral\<^sup>P M u = 0 \<longleftrightarrow> (AE x in M. u x \<le> 0)"
 12.1371 -proof -
 12.1372 -  have sets: "{x\<in>space M. max 0 (u x) \<noteq> 0} \<in> sets M"
 12.1373 -    using u by auto
 12.1374 -  from positive_integral_0_iff[of "\<lambda>x. max 0 (u x)"]
 12.1375 -  have "integral\<^sup>P M u = 0 \<longleftrightarrow> (AE x in M. max 0 (u x) = 0)"
 12.1376 -    unfolding positive_integral_max_0
 12.1377 -    using AE_iff_null[OF sets] u by auto
 12.1378 -  also have "\<dots> \<longleftrightarrow> (AE x in M. u x \<le> 0)" by (auto split: split_max)
 12.1379 -  finally show ?thesis .
 12.1380 -qed
 12.1381 -
 12.1382 -lemma AE_iff_positive_integral: 
 12.1383 -  "{x\<in>space M. P x} \<in> sets M \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> integral\<^sup>P M (indicator {x. \<not> P x}) = 0"
 12.1384 -  by (subst positive_integral_0_iff_AE) (auto simp: one_ereal_def zero_ereal_def
 12.1385 -    sets.sets_Collect_neg indicator_def[abs_def] measurable_If)
 12.1386 -
 12.1387 -lemma positive_integral_const_If:
 12.1388 -  "(\<integral>\<^sup>+x. a \<partial>M) = (if 0 \<le> a then a * (emeasure M) (space M) else 0)"
 12.1389 -  by (auto intro!: positive_integral_0_iff_AE[THEN iffD2])
 12.1390 -
 12.1391 -lemma positive_integral_subalgebra:
 12.1392 -  assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
 12.1393 -  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
 12.1394 -  shows "integral\<^sup>P N f = integral\<^sup>P M f"
 12.1395 -proof -
 12.1396 -  have [simp]: "\<And>f :: 'a \<Rightarrow> ereal. f \<in> borel_measurable N \<Longrightarrow> f \<in> borel_measurable M"
 12.1397 -    using N by (auto simp: measurable_def)
 12.1398 -  have [simp]: "\<And>P. (AE x in N. P x) \<Longrightarrow> (AE x in M. P x)"
 12.1399 -    using N by (auto simp add: eventually_ae_filter null_sets_def)
 12.1400 -  have [simp]: "\<And>A. A \<in> sets N \<Longrightarrow> A \<in> sets M"
 12.1401 -    using N by auto
 12.1402 -  from f show ?thesis
 12.1403 -    apply induct
 12.1404 -    apply (simp_all add: positive_integral_add positive_integral_cmult positive_integral_monotone_convergence_SUP N)
 12.1405 -    apply (auto intro!: positive_integral_cong cong: positive_integral_cong simp: N(2)[symmetric])
 12.1406 -    done
 12.1407 -qed
 12.1408 -
 12.1409 -lemma positive_integral_nat_function:
 12.1410 -  fixes f :: "'a \<Rightarrow> nat"
 12.1411 -  assumes "f \<in> measurable M (count_space UNIV)"
 12.1412 -  shows "(\<integral>\<^sup>+x. ereal (of_nat (f x)) \<partial>M) = (\<Sum>t. emeasure M {x\<in>space M. t < f x})"
 12.1413 -proof -
 12.1414 -  def F \<equiv> "\<lambda>i. {x\<in>space M. i < f x}"
 12.1415 -  with assms have [measurable]: "\<And>i. F i \<in> sets M"
 12.1416 -    by auto
 12.1417 -
 12.1418 -  { fix x assume "x \<in> space M"
 12.1419 -    have "(\<lambda>i. if i < f x then 1 else 0) sums (of_nat (f x)::real)"
 12.1420 -      using sums_If_finite[of "\<lambda>i. i < f x" "\<lambda>_. 1::real"] by simp
 12.1421 -    then have "(\<lambda>i. ereal(if i < f x then 1 else 0)) sums (ereal(of_nat(f x)))"
 12.1422 -      unfolding sums_ereal .
 12.1423 -    moreover have "\<And>i. ereal (if i < f x then 1 else 0) = indicator (F i) x"
 12.1424 -      using `x \<in> space M` by (simp add: one_ereal_def F_def)
 12.1425 -    ultimately have "ereal(of_nat(f x)) = (\<Sum>i. indicator (F i) x)"
 12.1426 -      by (simp add: sums_iff) }
 12.1427 -  then have "(\<integral>\<^sup>+x. ereal (of_nat (f x)) \<partial>M) = (\<integral>\<^sup>+x. (\<Sum>i. indicator (F i) x) \<partial>M)"
 12.1428 -    by (simp cong: positive_integral_cong)
 12.1429 -  also have "\<dots> = (\<Sum>i. emeasure M (F i))"
 12.1430 -    by (simp add: positive_integral_suminf)
 12.1431 -  finally show ?thesis
 12.1432 -    by (simp add: F_def)
 12.1433 -qed
 12.1434 -
 12.1435 -section "Lebesgue Integral"
 12.1436 -
 12.1437 -definition integrable :: "'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> bool" where
 12.1438 -  "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
 12.1439 -    (\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
 12.1440 -
 12.1441 -lemma borel_measurable_integrable[measurable_dest]:
 12.1442 -  "integrable M f \<Longrightarrow> f \<in> borel_measurable M"
 12.1443 -  by (auto simp: integrable_def)
 12.1444 -
 12.1445 -lemma integrableD[dest]:
 12.1446 -  assumes "integrable M f"
 12.1447 -  shows "f \<in> borel_measurable M" "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>"
 12.1448 -  using assms unfolding integrable_def by auto
 12.1449 -
 12.1450 -definition lebesgue_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> real) \<Rightarrow> real" ("integral\<^sup>L") where
 12.1451 -  "integral\<^sup>L M f = real ((\<integral>\<^sup>+ x. ereal (f x) \<partial>M)) - real ((\<integral>\<^sup>+ x. ereal (- f x) \<partial>M))"
 12.1452 -
 12.1453 -syntax
 12.1454 -  "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> 'a measure \<Rightarrow> real" ("\<integral> _. _ \<partial>_" [60,61] 110)
 12.1455 -
 12.1456 -translations
 12.1457 -  "\<integral> x. f \<partial>M" == "CONST lebesgue_integral M (%x. f)"
 12.1458 -
 12.1459 -lemma integrableE:
 12.1460 -  assumes "integrable M f"
 12.1461 -  obtains r q where
 12.1462 -    "(\<integral>\<^sup>+x. ereal (f x)\<partial>M) = ereal r"
 12.1463 -    "(\<integral>\<^sup>+x. ereal (-f x)\<partial>M) = ereal q"
 12.1464 -    "f \<in> borel_measurable M" "integral\<^sup>L M f = r - q"
 12.1465 -  using assms unfolding integrable_def lebesgue_integral_def
 12.1466 -  using positive_integral_positive[of M "\<lambda>x. ereal (f x)"]
 12.1467 -  using positive_integral_positive[of M "\<lambda>x. ereal (-f x)"]
 12.1468 -  by (cases rule: ereal2_cases[of "(\<integral>\<^sup>+x. ereal (-f x)\<partial>M)" "(\<integral>\<^sup>+x. ereal (f x)\<partial>M)"]) auto
 12.1469 -
 12.1470 -lemma integral_cong:
 12.1471 -  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x = g x"
 12.1472 -  shows "integral\<^sup>L M f = integral\<^sup>L M g"
 12.1473 -  using assms by (simp cong: positive_integral_cong add: lebesgue_integral_def)
 12.1474 -
 12.1475 -lemma integral_cong_AE:
 12.1476 -  assumes cong: "AE x in M. f x = g x"
 12.1477 -  shows "integral\<^sup>L M f = integral\<^sup>L M g"
 12.1478 -proof -
 12.1479 -  have *: "AE x in M. ereal (f x) = ereal (g x)"
 12.1480 -    "AE x in M. ereal (- f x) = ereal (- g x)" using cong by auto
 12.1481 -  show ?thesis
 12.1482 -    unfolding *[THEN positive_integral_cong_AE] lebesgue_integral_def ..
 12.1483 -qed
 12.1484 -
 12.1485 -lemma integrable_cong_AE:
 12.1486 -  assumes borel: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
 12.1487 -  assumes "AE x in M. f x = g x"
 12.1488 -  shows "integrable M f = integrable M g"
 12.1489 -proof -
 12.1490 -  have "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) = (\<integral>\<^sup>+ x. ereal (g x) \<partial>M)"
 12.1491 -    "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) = (\<integral>\<^sup>+ x. ereal (- g x) \<partial>M)"
 12.1492 -    using assms by (auto intro!: positive_integral_cong_AE)
 12.1493 -  with assms show ?thesis
 12.1494 -    by (auto simp: integrable_def)
 12.1495 -qed
 12.1496 -
 12.1497 -lemma integrable_cong:
 12.1498 -  "(\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable M g"
 12.1499 -  by (simp cong: positive_integral_cong measurable_cong add: integrable_def)
 12.1500 -
 12.1501 -lemma integral_mono_AE:
 12.1502 -  assumes fg: "integrable M f" "integrable M g"
 12.1503 -  and mono: "AE t in M. f t \<le> g t"
 12.1504 -  shows "integral\<^sup>L M f \<le> integral\<^sup>L M g"
 12.1505 -proof -
 12.1506 -  have "AE x in M. ereal (f x) \<le> ereal (g x)"
 12.1507 -    using mono by auto
 12.1508 -  moreover have "AE x in M. ereal (- g x) \<le> ereal (- f x)"
 12.1509 -    using mono by auto
 12.1510 -  ultimately show ?thesis using fg
 12.1511 -    by (auto intro!: add_mono positive_integral_mono_AE real_of_ereal_positive_mono
 12.1512 -             simp: positive_integral_positive lebesgue_integral_def algebra_simps)
 12.1513 -qed
 12.1514 -
 12.1515 -lemma integral_mono:
 12.1516 -  assumes "integrable M f" "integrable M g" "\<And>t. t \<in> space M \<Longrightarrow> f t \<le> g t"
 12.1517 -  shows "integral\<^sup>L M f \<le> integral\<^sup>L M g"
 12.1518 -  using assms by (auto intro: integral_mono_AE)
 12.1519 -
 12.1520 -lemma positive_integral_eq_integral:
 12.1521 -  assumes f: "integrable M f"
 12.1522 -  assumes nonneg: "AE x in M. 0 \<le> f x" 
 12.1523 -  shows "(\<integral>\<^sup>+ x. ereal (f x) \<partial>M) = integral\<^sup>L M f"
 12.1524 -proof -
 12.1525 -  have "(\<integral>\<^sup>+ x. max 0 (ereal (- f x)) \<partial>M) = (\<integral>\<^sup>+ x. 0 \<partial>M)"
 12.1526 -    using nonneg by (intro positive_integral_cong_AE) (auto split: split_max)
 12.1527 -  with f positive_integral_positive show ?thesis
 12.1528 -    by (cases "\<integral>\<^sup>+ x. ereal (f x) \<partial>M")
 12.1529 -       (auto simp add: lebesgue_integral_def positive_integral_max_0 integrable_def)
 12.1530 -qed
 12.1531 -  
 12.1532 -lemma integral_eq_positive_integral:
 12.1533 -  assumes f: "\<And>x. 0 \<le> f x"
 12.1534 -  shows "integral\<^sup>L M f = real (\<integral>\<^sup>+ x. ereal (f x) \<partial>M)"
 12.1535 -proof -
 12.1536 -  { fix x have "max 0 (ereal (- f x)) = 0" using f[of x] by (simp split: split_max) }
 12.1537 -  then have "0 = (\<integral>\<^sup>+ x. max 0 (ereal (- f x)) \<partial>M)" by simp
 12.1538 -  also have "\<dots> = (\<integral>\<^sup>+ x. ereal (- f x) \<partial>M)" unfolding positive_integral_max_0 ..
 12.1539 -  finally show ?thesis
 12.1540 -    unfolding lebesgue_integral_def by simp
 12.1541 -qed
 12.1542 -
 12.1543 -lemma integral_minus[intro, simp]:
 12.1544 -  assumes "integrable M f"
 12.1545 -  shows "integrable M (\<lambda>x. - f x)" "(\<integral>x. - f x \<partial>M) = - integral\<^sup>L M f"
 12.1546 -  using assms by (auto simp: integrable_def lebesgue_integral_def)
 12.1547 -
 12.1548 -lemma integral_minus_iff[simp]:
 12.1549 -  "integrable M (\<lambda>x. - f x) \<longleftrightarrow> integrable M f"
 12.1550 -proof
 12.1551 -  assume "integrable M (\<lambda>x. - f x)"
 12.1552 -  then have "integrable M (\<lambda>x. - (- f x))"
 12.1553 -    by (rule integral_minus)
 12.1554 -  then show "integrable M f" by simp
 12.1555 -qed (rule integral_minus)
 12.1556 -
 12.1557 -lemma integral_of_positive_diff:
 12.1558 -  assumes integrable: "integrable M u" "integrable M v"
 12.1559 -  and f_def: "\<And>x. f x = u x - v x" and pos: "\<And>x. 0 \<le> u x" "\<And>x. 0 \<le> v x"
 12.1560 -  shows "integrable M f" and "integral\<^sup>L M f = integral\<^sup>L M u - integral\<^sup>L M v"
 12.1561 -proof -
 12.1562 -  let ?f = "\<lambda>x. max 0 (ereal (f x))"
 12.1563 -  let ?mf = "\<lambda>x. max 0 (ereal (- f x))"
 12.1564 -  let ?u = "\<lambda>x. max 0 (ereal (u x))"
 12.1565 -  let ?v = "\<lambda>x. max 0 (ereal (v x))"
 12.1566 -
 12.1567 -  from borel_measurable_diff[of u M v] integrable
 12.1568 -  have f_borel: "?f \<in> borel_measurable M" and
 12.1569 -    mf_borel: "?mf \<in> borel_measurable M" and
 12.1570 -    v_borel: "?v \<in> borel_measurable M" and
 12.1571 -    u_borel: "?u \<in> borel_measurable M" and
 12.1572 -    "f \<in> borel_measurable M"
 12.1573 -    by (auto simp: f_def[symmetric] integrable_def)
 12.1574 -
 12.1575 -  have "(\<integral>\<^sup>+ x. ereal (u x - v x) \<partial>M) \<le> integral\<^sup>P M ?u"
 12.1576 -    using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
 12.1577 -  moreover have "(\<integral>\<^sup>+ x. ereal (v x - u x) \<partial>M) \<le> integral\<^sup>P M ?v"
 12.1578 -    using pos by (auto intro!: positive_integral_mono simp: positive_integral_max_0)
 12.1579 -  ultimately show f: "integrable M f"
 12.1580 -    using `integrable M u` `integrable M v` `f \<in> borel_measurable M`
 12.1581 -    by (auto simp: integrable_def f_def positive_integral_max_0)
 12.1582 -
 12.1583 -  have "\<And>x. ?u x + ?mf x = ?v x + ?f x"
 12.1584 -    unfolding f_def using pos by (simp split: split_max)
 12.1585 -  then have "(\<integral>\<^sup>+ x. ?u x + ?mf x \<partial>M) = (\<integral>\<^sup>+ x. ?v x + ?f x \<partial>M)" by simp
 12.1586 -  then have "real (integral\<^sup>P M ?u + integral\<^sup>P M ?mf) =
 12.1587 -      real (integral\<^sup>P M ?v + integral\<^sup>P M ?f)"
 12.1588 -    using positive_integral_add[OF u_borel _ mf_borel]
 12.1589 -    using positive_integral_add[OF v_borel _ f_borel]
 12.1590 -    by auto
 12.1591 -  then show "integral\<^sup>L M f = integral\<^sup>L M u - integral\<^sup>L M v"
 12.1592 -    unfolding positive_integral_max_0
 12.1593 -    unfolding pos[THEN integral_eq_positive_integral]
 12.1594 -    using integrable f by (auto elim!: integrableE)
 12.1595 -qed
 12.1596 -
 12.1597 -lemma integral_linear:
 12.1598 -  assumes "integrable M f" "integrable M g" and "0 \<le> a"
 12.1599 -  shows "integrable M (\<lambda>t. a * f t + g t)"
 12.1600 -  and "(\<integral> t. a * f t + g t \<partial>M) = a * integral\<^sup>L M f + integral\<^sup>L M g" (is ?EQ)
 12.1601 -proof -
 12.1602 -  let ?f = "\<lambda>x. max 0 (ereal (f x))"
 12.1603 -  let ?g = "\<lambda>x. max 0 (ereal (g x))"
 12.1604 -  let ?mf = "\<lambda>x. max 0 (ereal (- f x))"
 12.1605 -  let ?mg = "\<lambda>x. max 0 (ereal (- g x))"
 12.1606 -  let ?p = "\<lambda>t. max 0 (a * f t) + max 0 (g t)"
 12.1607 -  let ?n = "\<lambda>t. max 0 (- (a * f t)) + max 0 (- g t)"
 12.1608 -
 12.1609 -  from assms have linear:
 12.1610 -    "(\<integral>\<^sup>+ x. ereal a * ?f x + ?g x \<partial>M) = ereal a * integral\<^sup>P M ?f + integral\<^sup>P M ?g"
 12.1611 -    "(\<integral>\<^sup>+ x. ereal a * ?mf x + ?mg x \<partial>M) = ereal a * integral\<^sup>P M ?mf + integral\<^sup>P M ?mg"
 12.1612 -    by (auto intro!: positive_integral_linear simp: integrable_def)
 12.1613 -
 12.1614 -  have *: "(\<integral>\<^sup>+x. ereal (- ?p x) \<partial>M) = 0" "(\<integral>\<^sup>+x. ereal (- ?n x) \<partial>M) = 0"
 12.1615 -    using `0 \<le> a` assms by (auto simp: positive_integral_0_iff_AE integrable_def)
 12.1616 -  have **: "\<And>x. ereal a * ?f x + ?g x = max 0 (ereal (?p x))"
 12.1617 -           "\<And>x. ereal a * ?mf x + ?mg x = max 0 (ereal (?n x))"
 12.1618 -    using `0 \<le> a` by (auto split: split_max simp: zero_le_mult_iff mult_le_0_iff)
 12.1619 -
 12.1620 -  have "integrable M ?p" "integrable M ?n"
 12.1621 -      "\<And>t. a * f t + g t = ?p t - ?n t" "\<And>t. 0 \<le> ?p t" "\<And>t. 0 \<le> ?n t"
 12.1622 -    using linear assms unfolding integrable_def ** *
 12.1623 -    by (auto simp: positive_integral_max_0)
 12.1624 -  note diff = integral_of_positive_diff[OF this]
 12.1625 -
 12.1626 -  show "integrable M (\<lambda>t. a * f t + g t)" by (rule diff)
 12.1627 -  from assms linear show ?EQ
 12.1628 -    unfolding diff(2) ** positive_integral_max_0
 12.1629 -    unfolding lebesgue_integral_def *
 12.1630 -    by (auto elim!: integrableE simp: field_simps)
 12.1631 -qed
 12.1632 -
 12.1633 -lemma integral_add[simp, intro]:
 12.1634 -  assumes "integrable M f" "integrable M g"
 12.1635 -  shows "integrable M (\<lambda>t. f t + g t)"
 12.1636 -  and "(\<integral> t. f t + g t \<partial>M) = integral\<^sup>L M f + integral\<^sup>L M g"
 12.1637 -  using assms integral_linear[where a=1] by auto
 12.1638 -
 12.1639 -lemma integral_zero[simp, intro]:
 12.1640 -  shows "integrable M (\<lambda>x. 0)" "(\<integral> x.0 \<partial>M) = 0"
 12.1641 -  unfolding integrable_def lebesgue_integral_def
 12.1642 -  by auto
 12.1643 -
 12.1644 -lemma lebesgue_integral_uminus:
 12.1645 -    "(\<integral>x. - f x \<partial>M) = - integral\<^sup>L M f"
 12.1646 -  unfolding lebesgue_integral_def by simp
 12.1647 -
 12.1648 -lemma lebesgue_integral_cmult_nonneg:
 12.1649 -  assumes f: "f \<in> borel_measurable M" and "0 \<le> c"
 12.1650 -  shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^sup>L M f"
 12.1651 -proof -
 12.1652 -  { have "real (ereal c * integral\<^sup>P M (\<lambda>x. max 0 (ereal (f x)))) =
 12.1653 -      real (integral\<^sup>P M (\<lambda>x. ereal c * max 0 (ereal (f x))))"
 12.1654 -      using f `0 \<le> c` by (subst positive_integral_cmult) auto
 12.1655 -    also have "\<dots> = real (integral\<^sup>P M (\<lambda>x. max 0 (ereal (c * f x))))"
 12.1656 -      using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def zero_le_mult_iff)
 12.1657 -    finally have "real (integral\<^sup>P M (\<lambda>x. ereal (c * f x))) = c * real (integral\<^sup>P M (\<lambda>x. ereal (f x)))"
 12.1658 -      by (simp add: positive_integral_max_0) }
 12.1659 -  moreover
 12.1660 -  { have "real (ereal c * integral\<^sup>P M (\<lambda>x. max 0 (ereal (- f x)))) =
 12.1661 -      real (integral\<^sup>P M (\<lambda>x. ereal c * max 0 (ereal (- f x))))"
 12.1662 -      using f `0 \<le> c` by (subst positive_integral_cmult) auto
 12.1663 -    also have "\<dots> = real (integral\<^sup>P M (\<lambda>x. max 0 (ereal (- c * f x))))"
 12.1664 -      using `0 \<le> c` by (auto intro!: arg_cong[where f=real] positive_integral_cong simp: max_def mult_le_0_iff)
 12.1665 -    finally have "real (integral\<^sup>P M (\<lambda>x. ereal (- c * f x))) = c * real (integral\<^sup>P M (\<lambda>x. ereal (- f x)))"
 12.1666 -      by (simp add: positive_integral_max_0) }
 12.1667 -  ultimately show ?thesis
 12.1668 -    by (simp add: lebesgue_integral_def field_simps)
 12.1669 -qed
 12.1670 -
 12.1671 -lemma lebesgue_integral_cmult:
 12.1672 -  assumes f: "f \<in> borel_measurable M"
 12.1673 -  shows "(\<integral>x. c * f x \<partial>M) = c * integral\<^sup>L M f"
 12.1674 -proof (cases rule: linorder_le_cases)
 12.1675 -  assume "0 \<le> c" with f show ?thesis by (rule lebesgue_integral_cmult_nonneg)
 12.1676 -next
 12.1677 -  assume "c \<le> 0"
 12.1678 -  with lebesgue_integral_cmult_nonneg[OF f, of "-c"]
 12.1679 -  show ?thesis
 12.1680 -    by (simp add: lebesgue_integral_def)
 12.1681 -qed
 12.1682 -
 12.1683 -lemma lebesgue_integral_multc:
 12.1684 -  "f \<in> borel_measurable M \<Longrightarrow> (\<integral>x. f x * c \<partial>M) = integral\<^sup>L M f * c"
 12.1685 -  using lebesgue_integral_cmult[of f M c] by (simp add: ac_simps)
 12.1686 -
 12.1687 -lemma integral_multc:
 12.1688 -  "integrable M f \<Longrightarrow> (\<integral> x. f x * c \<partial>M) = integral\<^sup>L M f * c"
 12.1689 -  by (simp add: lebesgue_integral_multc)
 12.1690 -
 12.1691 -lemma integral_cmult[simp, intro]:
 12.1692 -  assumes "integrable M f"
 12.1693 -  shows "integrable M (\<lambda>t. a * f t)" (is ?P)
 12.1694 -  and "(\<integral> t. a * f t \<partial>M) = a * integral\<^sup>L M f" (is ?I)
 12.1695 -proof -
 12.1696 -  have "integrable M (\<lambda>t. a * f t) \<and> (\<integral> t. a * f t \<partial>M) = a * integral\<^sup>L M f"
 12.1697 -  proof (cases rule: le_cases)
 12.1698 -    assume "0 \<le> a" show ?thesis
 12.1699 -      using integral_linear[OF assms integral_zero(1) `0 \<le> a`]
 12.1700 -      by simp
 12.1701 -  next
 12.1702 -    assume "a \<le> 0" hence "0 \<le> - a" by auto
 12.1703 -    have *: "\<And>t. - a * t + 0 = (-a) * t" by simp
 12.1704 -    show ?thesis using integral_linear[OF assms integral_zero(1) `0 \<le> - a`]
 12.1705 -        integral_minus(1)[of M "\<lambda>t. - a * f t"]
 12.1706 -      unfolding * integral_zero by simp
 12.1707 -  qed
 12.1708 -  thus ?P ?I by auto
 12.1709 -qed
 12.1710 -
 12.1711 -lemma integral_diff[simp, intro]:
 12.1712 -  assumes f: "integrable M f" and g: "integrable M g"
 12.1713 -  shows "integrable M (\<lambda>t. f t - g t)"
 12.1714 -  and "(\<integral> t. f t - g t \<partial>M) = integral\<^sup>L M f - integral\<^sup>L M g"
 12.1715 -  using integral_add[OF f integral_minus(1)[OF g]]
 12.1716 -  unfolding integral_minus(2)[OF g]
 12.1717 -  by auto
 12.1718 -
 12.1719 -lemma integral_indicator[simp, intro]:
 12.1720 -  assumes "A \<in> sets M" and "(emeasure M) A \<noteq> \<infinity>"
 12.1721 -  shows "integral\<^sup>L M (indicator A) = real (emeasure M A)" (is ?int)
 12.1722 -  and "integrable M (indicator A)" (is ?able)
 12.1723 -proof -
 12.1724 -  from `A \<in> sets M` have *:
 12.1725 -    "\<And>x. ereal (indicator A x) = indicator A x"
 12.1726 -    "(\<integral>\<^sup>+x. ereal (- indicator A x) \<partial>M) = 0"
 12.1727 -    by (auto split: split_indicator simp: positive_integral_0_iff_AE one_ereal_def)
 12.1728 -  show ?int ?able
 12.1729 -    using assms unfolding lebesgue_integral_def integrable_def
 12.1730 -    by (auto simp: *)
 12.1731 -qed
 12.1732 -
 12.1733 -lemma integral_cmul_indicator:
 12.1734 -  assumes "A \<in> sets M" and "c \<noteq> 0 \<Longrightarrow> (emeasure M) A \<noteq> \<infinity>"
 12.1735 -  shows "integrable M (\<lambda>x. c * indicator A x)" (is ?P)
 12.1736 -  and "(\<integral>x. c * indicator A x \<partial>M) = c * real ((emeasure M) A)" (is ?I)
 12.1737 -proof -
 12.1738 -  show ?P
 12.1739 -  proof (cases "c = 0")
 12.1740 -    case False with assms show ?thesis by simp
 12.1741 -  qed simp
 12.1742 -
 12.1743 -  show ?I
 12.1744 -  proof (cases "c = 0")
 12.1745 -    case False with assms show ?thesis by simp
 12.1746 -  qed simp
 12.1747 -qed
 12.1748 -
 12.1749 -lemma integral_setsum[simp, intro]:
 12.1750 -  assumes "\<And>n. n \<in> S \<Longrightarrow> integrable M (f n)"
 12.1751 -  shows "(\<integral>x. (\<Sum> i \<in> S. f i x) \<partial>M) = (\<Sum> i \<in> S. integral\<^sup>L M (f i))" (is "?int S")
 12.1752 -    and "integrable M (\<lambda>x. \<Sum> i \<in> S. f i x)" (is "?I S")
 12.1753 -proof -
 12.1754 -  have "?int S \<and> ?I S"
 12.1755 -  proof (cases "finite S")
 12.1756 -    assume "finite S"
 12.1757 -    from this assms show ?thesis by (induct S) simp_all
 12.1758 -  qed simp
 12.1759 -  thus "?int S" and "?I S" by auto
 12.1760 -qed
 12.1761 -
 12.1762 -lemma integrable_bound:
 12.1763 -  assumes "integrable M f" and f: "AE x in M. \<bar>g x\<bar> \<le> f x"
 12.1764 -  assumes borel: "g \<in> borel_measurable M"
 12.1765 -  shows "integrable M g"
 12.1766 -proof -
 12.1767 -  have "(\<integral>\<^sup>+ x. ereal (g x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal \<bar>g x\<bar> \<partial>M)"
 12.1768 -    by (auto intro!: positive_integral_mono)
 12.1769 -  also have "\<dots> \<le> (\<integral>\<^sup>+ x. ereal (f x) \<partial>M)"
 12.1770 -    using f by (auto intro!: positive_integral_mono_AE)
 12.1771 -  also have "\<dots> < \<infinity>"
 12.1772 -    using `integrable M f` unfolding integrable_def by auto
 12.1773 -  finally have pos: "(\<integral>\<^sup>+ x. ereal (g x) \<partial>M) < \<infinity>" .
 12.1774 -
 12.1775 -  have "(\<integral>\<^sup>+ x. ereal (- g x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ereal (\<bar>g x\<bar>) \<partial>M)"
 12.1776 -    by (auto intro!: positive_integral_mono)
 12.1777 -  also have "\<dots> \<le> (\<integral>\<^sup>+ x. ereal (f x) \<partial>M)"
 12.1778 -    using f by (auto intro!: positive_integral_mono_AE)
 12.1779 -  also have "\<dots> < \<infinity>"
 12.1780 -    using `integrable M f` unfolding integrable_def by auto
 12.1781 -  finally have neg: "(\<integral>\<^sup>+ x. ereal (- g x) \<partial>M) < \<infinity>" .
 12.1782 -
 12.1783 -  from neg pos borel show ?thesis
 12.1784 -    unfolding integrable_def by auto
 12.1785 -qed
 12.1786 -
 12.1787 -lemma integrable_abs:
 12.1788 -  assumes f[measurable]: "integrable M f"
 12.1789 -  shows "integrable M (\<lambda> x. \<bar>f x\<bar>)"
 12.1790 -proof -
 12.1791 -  from assms have *: "(\<integral>\<^sup>+x. ereal (- \<bar>f x\<bar>)\<partial>M) = 0"
 12.1792 -    "\<And>x. ereal \<bar>f x\<bar> = max 0 (ereal (f x)) + max 0 (ereal (- f x))"
 12.1793 -    by (auto simp: integrable_def positive_integral_0_iff_AE split: split_max)
 12.1794 -  with assms show ?thesis
 12.1795 -    by (simp add: positive_integral_add positive_integral_max_0 integrable_def)
 12.1796 -qed
 12.1797 -
 12.1798 -lemma integral_subalgebra:
 12.1799 -  assumes borel: "f \<in> borel_measurable N"
 12.1800 -  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
 12.1801 -  shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
 12.1802 -    and "integral\<^sup>L N f = integral\<^sup>L M f" (is ?I)
 12.1803 -proof -
 12.1804 -  have "(\<integral>\<^sup>+ x. max 0 (ereal (f x)) \<partial>N) = (\<integral>\<^sup>+ x. max 0 (ereal (f x)) \<partial>M)"
 12.1805 -       "(\<integral>\<^sup>+ x. max 0 (ereal (- f x)) \<partial>N) = (\<integral>\<^sup>+ x. max 0 (ereal (- f x)) \<partial>M)"
 12.1806 -    using borel by (auto intro!: positive_integral_subalgebra N)
 12.1807 -  moreover have "f \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable N"
 12.1808 -    using assms unfolding measurable_def by auto
 12.1809 -  ultimately show ?P ?I
 12.1810 -    by (auto simp: integrable_def lebesgue_integral_def positive_integral_max_0)
 12.1811 -qed
 12.1812 -
 12.1813 -lemma lebesgue_integral_nonneg:
 12.1814 -  assumes ae: "(AE x in M. 0 \<le> f x)" shows "0 \<le> integral\<^sup>L M f"
 12.1815 -proof -
 12.1816 -  have "(\<integral>\<^sup>+x. max 0 (ereal (- f x)) \<partial>M) = (\<integral>\<^sup>+x. 0 \<partial>M)"
 12.1817 -    using ae by (intro positive_integral_cong_AE) (auto simp: max_def)
 12.1818 -  then show ?thesis
 12.1819 -    by (auto simp: lebesgue_integral_def positive_integral_max_0
 12.1820 -             intro!: real_of_ereal_pos positive_integral_positive)
 12.1821 -qed
 12.1822 -
 12.1823 -lemma integrable_abs_iff:
 12.1824 -  "f \<in> borel_measurable M \<Longrightarrow> integrable M (\<lambda> x. \<bar>f x\<bar>) \<longleftrightarrow> integrable M f"
 12.1825 -  by (auto intro!: integrable_bound[where g=f] integrable_abs)
 12.1826 -
 12.1827 -lemma integrable_max:
 12.1828 -  assumes int: "integrable M f" "integrable M g"
 12.1829 -  shows "integrable M (\<lambda> x. max (f x) (g x))"
 12.1830 -proof (rule integrable_bound)
 12.1831 -  show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
 12.1832 -    using int by (simp add: integrable_abs)
 12.1833 -  show "(\<lambda>x. max (f x) (g x)) \<in> borel_measurable M"
 12.1834 -    using int unfolding integrable_def by auto
 12.1835 -qed auto
 12.1836 -
 12.1837 -lemma integrable_min:
 12.1838 -  assumes int: "integrable M f" "integrable M g"
 12.1839 -  shows "integrable M (\<lambda> x. min (f x) (g x))"
 12.1840 -proof (rule integrable_bound)
 12.1841 -  show "integrable M (\<lambda>x. \<bar>f x\<bar> + \<bar>g x\<bar>)"
 12.1842 -    using int by (simp add: integrable_abs)
 12.1843 -  show "(\<lambda>x. min (f x) (g x)) \<in> borel_measurable M"
 12.1844 -    using int unfolding integrable_def by auto
 12.1845 -qed auto
 12.1846 -
 12.1847 -lemma integral_triangle_inequality:
 12.1848 -  assumes "integrable M f"
 12.1849 -  shows "\<bar>integral\<^sup>L M f\<bar> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
 12.1850 -proof -
 12.1851 -  have "\<bar>integral\<^sup>L M f\<bar> = max (integral\<^sup>L M f) (- integral\<^sup>L M f)" by auto
 12.1852 -  also have "\<dots> \<le> (\<integral>x. \<bar>f x\<bar> \<partial>M)"
 12.1853 -      using assms integral_minus(2)[of M f, symmetric]
 12.1854 -      by (auto intro!: integral_mono integrable_abs simp del: integral_minus)
 12.1855 -  finally show ?thesis .
 12.1856 -qed
 12.1857 -
 12.1858 -lemma integrable_nonneg:
 12.1859 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "(\<integral>\<^sup>+ x. f x \<partial>M) \<noteq> \<infinity>"
 12.1860 -  shows "integrable M f"
 12.1861 -  unfolding integrable_def
 12.1862 -proof (intro conjI f)
 12.1863 -  have "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) = 0"
 12.1864 -    using f by (subst positive_integral_0_iff_AE) auto
 12.1865 -  then show "(\<integral>\<^sup>+ x. ereal (- f x) \<partial>M) \<noteq> \<infinity>" by simp
 12.1866 -qed
 12.1867 -
 12.1868 -lemma integral_positive:
 12.1869 -  assumes "integrable M f" "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
 12.1870 -  shows "0 \<le> integral\<^sup>L M f"
 12.1871 -proof -
 12.1872 -  have "0 = (\<integral>x. 0 \<partial>M)" by auto
 12.1873 -  also have "\<dots> \<le> integral\<^sup>L M f"
 12.1874 -    using assms by (rule integral_mono[OF integral_zero(1)])
 12.1875 -  finally show ?thesis .
 12.1876 -qed
 12.1877 -
 12.1878 -lemma integral_monotone_convergence_pos:
 12.1879 -  assumes i: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
 12.1880 -    and pos: "\<And>i. AE x in M. 0 \<le> f i x"
 12.1881 -    and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
 12.1882 -    and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) ----> x"
 12.1883 -    and u: "u \<in> borel_measurable M"
 12.1884 -  shows "integrable M u"
 12.1885 -  and "integral\<^sup>L M u = x"
 12.1886 -proof -
 12.1887 -  have "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = (SUP n. (\<integral>\<^sup>+ x. ereal (f n x) \<partial>M))"
 12.1888 -  proof (subst positive_integral_monotone_convergence_SUP_AE[symmetric])
 12.1889 -    fix i
 12.1890 -    from mono pos show "AE x in M. ereal (f i x) \<le> ereal (f (Suc i) x) \<and> 0 \<le> ereal (f i x)"
 12.1891 -      by eventually_elim (auto simp: mono_def)
 12.1892 -    show "(\<lambda>x. ereal (f i x)) \<in> borel_measurable M"
 12.1893 -      using i by auto
 12.1894 -  next
 12.1895 -    show "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = \<integral>\<^sup>+ x. (SUP i. ereal (f i x)) \<partial>M"
 12.1896 -      apply (rule positive_integral_cong_AE)
 12.1897 -      using lim mono
 12.1898 -      by eventually_elim (simp add: SUP_eq_LIMSEQ[THEN iffD2])
 12.1899 -  qed
 12.1900 -  also have "\<dots> = ereal x"
 12.1901 -    using mono i unfolding positive_integral_eq_integral[OF i pos]
 12.1902 -    by (subst SUP_eq_LIMSEQ) (auto simp: mono_def intro!: integral_mono_AE ilim)
 12.1903 -  finally have "(\<integral>\<^sup>+ x. ereal (u x) \<partial>M) = ereal x" .
 12.1904 -  moreover have "(\<integral>\<^sup>+ x. ereal (- u x) \<partial>M) = 0"
 12.1905 -  proof (subst positive_integral_0_iff_AE)
 12.1906 -    show "(\<lambda>x. ereal (- u x)) \<in> borel_measurable M"
 12.1907 -      using u by auto
 12.1908 -    from mono pos[of 0] lim show "AE x in M. ereal (- u x) \<le> 0"
 12.1909 -    proof eventually_elim
 12.1910 -      fix x assume "mono (\<lambda>n. f n x)" "0 \<le> f 0 x" "(\<lambda>i. f i x) ----> u x"
 12.1911 -      then show "ereal (- u x) \<le> 0"
 12.1912 -        using incseq_le[of "\<lambda>n. f n x" "u x" 0] by (simp add: mono_def incseq_def)
 12.1913 -    qed
 12.1914 -  qed
 12.1915 -  ultimately show "integrable M u" "integral\<^sup>L M u = x"
 12.1916 -    by (auto simp: integrable_def lebesgue_integral_def u)
 12.1917 -qed
 12.1918 -
 12.1919 -lemma integral_monotone_convergence:
 12.1920 -  assumes f: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
 12.1921 -  and lim: "AE x in M. (\<lambda>i. f i x) ----> u x"
 12.1922 -  and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) ----> x"
 12.1923 -  and u: "u \<in> borel_measurable M"
 12.1924 -  shows "integrable M u"
 12.1925 -  and "integral\<^sup>L M u = x"
 12.1926 -proof -
 12.1927 -  have 1: "\<And>i. integrable M (\<lambda>x. f i x - f 0 x)"
 12.1928 -    using f by auto
 12.1929 -  have 2: "AE x in M. mono (\<lambda>n. f n x - f 0 x)"
 12.1930 -    using mono by (auto simp: mono_def le_fun_def)
 12.1931 -  have 3: "\<And>n. AE x in M. 0 \<le> f n x - f 0 x"
 12.1932 -    using mono by (auto simp: field_simps mono_def le_fun_def)
 12.1933 -  have 4: "AE x in M. (\<lambda>i. f i x - f 0 x) ----> u x - f 0 x"
 12.1934 -    using lim by (auto intro!: tendsto_diff)
 12.1935 -  have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x \<partial>M)) ----> x - integral\<^sup>L M (f 0)"
 12.1936 -    using f ilim by (auto intro!: tendsto_diff)
 12.1937 -  have 6: "(\<lambda>x. u x - f 0 x) \<in> borel_measurable M"
 12.1938 -    using f[of 0] u by auto
 12.1939 -  note diff = integral_monotone_convergence_pos[OF 1 2 3 4 5 6]
 12.1940 -  have "integrable M (\<lambda>x. (u x - f 0 x) + f 0 x)"
 12.1941 -    using diff(1) f by (rule integral_add(1))
 12.1942 -  with diff(2) f show "integrable M u" "integral\<^sup>L M u = x"
 12.1943 -    by auto
 12.1944 -qed
 12.1945 -
 12.1946 -lemma integral_0_iff:
 12.1947 -  assumes "integrable M f"
 12.1948 -  shows "(\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> (emeasure M) {x\<in>space M. f x \<noteq> 0} = 0"
 12.1949 -proof -
 12.1950 -  have *: "(\<integral>\<^sup>+x. ereal (- \<bar>f x\<bar>) \<partial>M) = 0"
 12.1951 -    using assms by (auto simp: positive_integral_0_iff_AE integrable_def)
 12.1952 -  have "integrable M (\<lambda>x. \<bar>f x\<bar>)" using assms by (rule integrable_abs)
 12.1953 -  hence "(\<lambda>x. ereal (\<bar>f x\<bar>)) \<in> borel_measurable M"
 12.1954 -    "(\<integral>\<^sup>+ x. ereal \<bar>f x\<bar> \<partial>M) \<noteq> \<infinity>" unfolding integrable_def by auto
 12.1955 -  from positive_integral_0_iff[OF this(1)] this(2)
 12.1956 -  show ?thesis unfolding lebesgue_integral_def *
 12.1957 -    using positive_integral_positive[of M "\<lambda>x. ereal \<bar>f x\<bar>"]
 12.1958 -    by (auto simp add: real_of_ereal_eq_0)
 12.1959 -qed
 12.1960 -
 12.1961 -lemma integral_real:
 12.1962 -  "AE x in M. \<bar>f x\<bar> \<noteq> \<infinity> \<Longrightarrow> (\<integral>x. real (f x) \<partial>M) = real (integral\<^sup>P M f) - real (integral\<^sup>P M (\<lambda>x. - f x))"
 12.1963 -  using assms unfolding lebesgue_integral_def
 12.1964 -  by (subst (1 2) positive_integral_cong_AE) (auto simp add: ereal_real)
 12.1965 -
 12.1966 -lemma (in finite_measure) lebesgue_integral_const[simp]:
 12.1967 -  shows "integrable M (\<lambda>x. a)"
 12.1968 -  and  "(\<integral>x. a \<partial>M) = a * measure M (space M)"
 12.1969 -proof -
 12.1970 -  { fix a :: real assume "0 \<le> a"
 12.1971 -    then have "(\<integral>\<^sup>+ x. ereal a \<partial>M) = ereal a * (emeasure M) (space M)"
 12.1972 -      by (subst positive_integral_const) auto
 12.1973 -    moreover
 12.1974 -    from `0 \<le> a` have "(\<integral>\<^sup>+ x. ereal (-a) \<partial>M) = 0"
 12.1975 -      by (subst positive_integral_0_iff_AE) auto
 12.1976 -    ultimately have "integrable M (\<lambda>x. a)" by (auto simp: integrable_def) }
 12.1977 -  note * = this
 12.1978 -  show "integrable M (\<lambda>x. a)"
 12.1979 -  proof cases
 12.1980 -    assume "0 \<le> a" with * show ?thesis .
 12.1981 -  next
 12.1982 -    assume "\<not> 0 \<le> a"
 12.1983 -    then have "0 \<le> -a" by auto
 12.1984 -    from *[OF this] show ?thesis by simp
 12.1985 -  qed
 12.1986 -  show "(\<integral>x. a \<partial>M) = a * measure M (space M)"
 12.1987 -    by (simp add: lebesgue_integral_def positive_integral_const_If emeasure_eq_measure)
 12.1988 -qed
 12.1989 -
 12.1990 -lemma (in finite_measure) integrable_const_bound:
 12.1991 -  assumes "AE x in M. \<bar>f x\<bar> \<le> B" and "f \<in> borel_measurable M" shows "integrable M f"
 12.1992 -  by (auto intro: integrable_bound[where f="\<lambda>x. B"] lebesgue_integral_const assms)
 12.1993 -
 12.1994 -lemma (in finite_measure) integral_less_AE:
 12.1995 -  assumes int: "integrable M X" "integrable M Y"
 12.1996 -  assumes A: "(emeasure M) A \<noteq> 0" "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> X x \<noteq> Y x"
 12.1997 -  assumes gt: "AE x in M. X x \<le> Y x"
 12.1998 -  shows "integral\<^sup>L M X < integral\<^sup>L M Y"
 12.1999 -proof -
 12.2000 -  have "integral\<^sup>L M X \<le> integral\<^sup>L M Y"
 12.2001 -    using gt int by (intro integral_mono_AE) auto
 12.2002 -  moreover
 12.2003 -  have "integral\<^sup>L M X \<noteq> integral\<^sup>L M Y"
 12.2004 -  proof
 12.2005 -    assume eq: "integral\<^sup>L M X = integral\<^sup>L M Y"
 12.2006 -    have "integral\<^sup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^sup>L M (\<lambda>x. Y x - X x)"
 12.2007 -      using gt by (intro integral_cong_AE) auto
 12.2008 -    also have "\<dots> = 0"
 12.2009 -      using eq int by simp
 12.2010 -    finally have "(emeasure M) {x \<in> space M. Y x - X x \<noteq> 0} = 0"
 12.2011 -      using int by (simp add: integral_0_iff)
 12.2012 -    moreover
 12.2013 -    have "(\<integral>\<^sup>+x. indicator A x \<partial>M) \<le> (\<integral>\<^sup>+x. indicator {x \<in> space M. Y x - X x \<noteq> 0} x \<partial>M)"
 12.2014 -      using A by (intro positive_integral_mono_AE) auto
 12.2015 -    then have "(emeasure M) A \<le> (emeasure M) {x \<in> space M. Y x - X x \<noteq> 0}"
 12.2016 -      using int A by (simp add: integrable_def)
 12.2017 -    ultimately have "emeasure M A = 0"
 12.2018 -      using emeasure_nonneg[of M A] by simp
 12.2019 -    with `(emeasure M) A \<noteq> 0` show False by auto
 12.2020 -  qed
 12.2021 -  ultimately show ?thesis by auto
 12.2022 -qed
 12.2023 -
 12.2024 -lemma (in finite_measure) integral_less_AE_space:
 12.2025 -  assumes int: "integrable M X" "integrable M Y"
 12.2026 -  assumes gt: "AE x in M. X x < Y x" "(emeasure M) (space M) \<noteq> 0"
 12.2027 -  shows "integral\<^sup>L M X < integral\<^sup>L M Y"
 12.2028 -  using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
 12.2029 -
 12.2030 -lemma integral_dominated_convergence:
 12.2031 -  assumes u[measurable]: "\<And>i. integrable M (u i)" and bound: "\<And>j. AE x in M. \<bar>u j x\<bar> \<le> w x"
 12.2032 -  and w[measurable]: "integrable M w"
 12.2033 -  and u': "AE x in M. (\<lambda>i. u i x) ----> u' x"
 12.2034 -  and [measurable]: "u' \<in> borel_measurable M"
 12.2035 -  shows "integrable M u'"
 12.2036 -  and "(\<lambda>i. (\<integral>x. \<bar>u i x - u' x\<bar> \<partial>M)) ----> 0" (is "?lim_diff")
 12.2037 -  and "(\<lambda>i. integral\<^sup>L M (u i)) ----> integral\<^sup>L M u'" (is ?lim)
 12.2038 -proof -
 12.2039 -  have all_bound: "AE x in M. \<forall>j. \<bar>u j x\<bar> \<le> w x"
 12.2040 -    using bound by (auto simp: AE_all_countable)
 12.2041 -  with u' have u'_bound: "AE x in M. \<bar>u' x\<bar> \<le> w x"
 12.2042 -    by eventually_elim (auto intro: LIMSEQ_le_const2 tendsto_rabs)
 12.2043 -
 12.2044 -  from bound[of 0] have w_pos: "AE x in M. 0 \<le> w x"
 12.2045 -    by eventually_elim auto
 12.2046 -
 12.2047 -  show "integrable M u'"
 12.2048 -    by (rule integrable_bound) fact+
 12.2049 -
 12.2050 -  let ?diff = "\<lambda>n x. 2 * w x - \<bar>u n x - u' x\<bar>"
 12.2051 -  have diff: "\<And>n. integrable M (\<lambda>x. \<bar>u n x - u' x\<bar>)"
 12.2052 -    using w u `integrable M u'` by (auto intro!: integrable_abs)
 12.2053 -
 12.2054 -  from u'_bound all_bound
 12.2055 -  have diff_less_2w: "AE x in M. \<forall>j. \<bar>u j x - u' x\<bar> \<le> 2 * w x"
 12.2056 -  proof (eventually_elim, intro allI)
 12.2057 -    fix x j assume *: "\<bar>u' x\<bar> \<le> w x" "\<forall>j. \<bar>u j x\<bar> \<le> w x"
 12.2058 -    then have "\<bar>u j x - u' x\<bar> \<le> \<bar>u j x\<bar> + \<bar>u' x\<bar>" by auto
 12.2059 -    also have "\<dots> \<le> w x + w x"
 12.2060 -      using * by (intro add_mono) auto
 12.2061 -    finally show "\<bar>u j x - u' x\<bar> \<le> 2 * w x" by simp
 12.2062 -  qed
 12.2063 -
 12.2064 -  have PI_diff: "\<And>n. (\<integral>\<^sup>+ x. ereal (?diff n x) \<partial>M) =
 12.2065 -    (\<integral>\<^sup>+ x. ereal (2 * w x) \<partial>M) - (\<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
 12.2066 -    using diff w diff_less_2w w_pos
 12.2067 -    by (subst positive_integral_diff[symmetric])
 12.2068 -       (auto simp: integrable_def intro!: positive_integral_cong_AE)
 12.2069 -
 12.2070 -  have "integrable M (\<lambda>x. 2 * w x)"
 12.2071 -    using w by auto
 12.2072 -  hence I2w_fin: "(\<integral>\<^sup>+ x. ereal (2 * w x) \<partial>M) \<noteq> \<infinity>" and
 12.2073 -    borel_2w: "(\<lambda>x. ereal (2 * w x)) \<in> borel_measurable M"
 12.2074 -    unfolding integrable_def by auto
 12.2075 -
 12.2076 -  have "limsup (\<lambda>n. \<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = 0" (is "limsup ?f = 0")
 12.2077 -  proof cases
 12.2078 -    assume eq_0: "(\<integral>\<^sup>+ x. max 0 (ereal (2 * w x)) \<partial>M) = 0" (is "?wx = 0")
 12.2079 -    { fix n
 12.2080 -      have "?f n \<le> ?wx" (is "integral\<^sup>P M ?f' \<le> _")
 12.2081 -        using diff_less_2w unfolding positive_integral_max_0
 12.2082 -        by (intro positive_integral_mono_AE) auto
 12.2083 -      then have "?f n = 0"
 12.2084 -        using positive_integral_positive[of M ?f'] eq_0 by auto }
 12.2085 -    then show ?thesis by (simp add: Limsup_const)
 12.2086 -  next
 12.2087 -    assume neq_0: "(\<integral>\<^sup>+ x. max 0 (ereal (2 * w x)) \<partial>M) \<noteq> 0" (is "?wx \<noteq> 0")
 12.2088 -    have "0 = limsup (\<lambda>n. 0 :: ereal)" by (simp add: Limsup_const)
 12.2089 -    also have "\<dots> \<le> limsup (\<lambda>n. \<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
 12.2090 -      by (simp add: Limsup_mono  positive_integral_positive)
 12.2091 -    finally have pos: "0 \<le> limsup (\<lambda>n. \<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)" .
 12.2092 -    have "?wx = (\<integral>\<^sup>+ x. liminf (\<lambda>n. max 0 (ereal (?diff n x))) \<partial>M)"
 12.2093 -      using u'
 12.2094 -    proof (intro positive_integral_cong_AE, eventually_elim)
 12.2095 -      fix x assume u': "(\<lambda>i. u i x) ----> u' x"
 12.2096 -      show "max 0 (ereal (2 * w x)) = liminf (\<lambda>n. max 0 (ereal (?diff n x)))"
 12.2097 -        unfolding ereal_max_0
 12.2098 -      proof (rule lim_imp_Liminf[symmetric], unfold lim_ereal)
 12.2099 -        have "(\<lambda>i. ?diff i x) ----> 2 * w x - \<bar>u' x - u' x\<bar>"
 12.2100 -          using u' by (safe intro!: tendsto_intros)
 12.2101 -        then show "(\<lambda>i. max 0 (?diff i x)) ----> max 0 (2 * w x)"
 12.2102 -          by (auto intro!: tendsto_max)
 12.2103 -      qed (rule trivial_limit_sequentially)
 12.2104 -    qed
 12.2105 -    also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^sup>+ x. max 0 (ereal (?diff n x)) \<partial>M)"
 12.2106 -      using w u unfolding integrable_def
 12.2107 -      by (intro positive_integral_lim_INF) (auto intro!: positive_integral_lim_INF)
 12.2108 -    also have "\<dots> = (\<integral>\<^sup>+ x. ereal (2 * w x) \<partial>M) -
 12.2109 -        limsup (\<lambda>n. \<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"
 12.2110 -      unfolding PI_diff positive_integral_max_0
 12.2111 -      using positive_integral_positive[of M "\<lambda>x. ereal (2 * w x)"]
 12.2112 -      by (subst liminf_ereal_cminus) auto
 12.2113 -    finally show ?thesis
 12.2114 -      using neq_0 I2w_fin positive_integral_positive[of M "\<lambda>x. ereal (2 * w x)"] pos
 12.2115 -      unfolding positive_integral_max_0
 12.2116 -      by (cases rule: ereal2_cases[of "\<integral>\<^sup>+ x. ereal (2 * w x) \<partial>M" "limsup (\<lambda>n. \<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M)"])
 12.2117 -         auto
 12.2118 -  qed
 12.2119 -
 12.2120 -  have "liminf ?f \<le> limsup ?f"
 12.2121 -    by (intro Liminf_le_Limsup trivial_limit_sequentially)
 12.2122 -  moreover
 12.2123 -  { have "0 = liminf (\<lambda>n. 0 :: ereal)" by (simp add: Liminf_const)
 12.2124 -    also have "\<dots> \<le> liminf ?f"
 12.2125 -      by (simp add: Liminf_mono positive_integral_positive)
 12.2126 -    finally have "0 \<le> liminf ?f" . }
 12.2127 -  ultimately have liminf_limsup_eq: "liminf ?f = ereal 0" "limsup ?f = ereal 0"
 12.2128 -    using `limsup ?f = 0` by auto
 12.2129 -  have "\<And>n. (\<integral>\<^sup>+ x. ereal \<bar>u n x - u' x\<bar> \<partial>M) = ereal (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)"
 12.2130 -    using diff positive_integral_positive[of M]
 12.2131 -    by (subst integral_eq_positive_integral[of _ M]) (auto simp: ereal_real integrable_def)
 12.2132 -  then show ?lim_diff
 12.2133 -    using Liminf_eq_Limsup[OF trivial_limit_sequentially liminf_limsup_eq]
 12.2134 -    by simp
 12.2135 -
 12.2136 -  show ?lim
 12.2137 -  proof (rule LIMSEQ_I)
 12.2138 -    fix r :: real assume "0 < r"
 12.2139 -    from LIMSEQ_D[OF `?lim_diff` this]
 12.2140 -    obtain N where N: "\<And>n. N \<le> n \<Longrightarrow> (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M) < r"
 12.2141 -      using diff by (auto simp: integral_positive)
 12.2142 -
 12.2143 -    show "\<exists>N. \<forall>n\<ge>N. norm (integral\<^sup>L M (u n) - integral\<^sup>L M u') < r"
 12.2144 -    proof (safe intro!: exI[of _ N])
 12.2145 -      fix n assume "N \<le> n"
 12.2146 -      have "\<bar>integral\<^sup>L M (u n) - integral\<^sup>L M u'\<bar> = \<bar>(\<integral>x. u n x - u' x \<partial>M)\<bar>"
 12.2147 -        using u `integrable M u'` by auto
 12.2148 -      also have "\<dots> \<le> (\<integral>x. \<bar>u n x - u' x\<bar> \<partial>M)" using u `integrable M u'`
 12.2149 -        by (rule_tac integral_triangle_inequality) auto
 12.2150 -      also note N[OF `N \<le> n`]
 12.2151 -      finally show "norm (integral\<^sup>L M (u n) - integral\<^sup>L M u') < r" by simp
 12.2152 -    qed
 12.2153 -  qed
 12.2154 -qed
 12.2155 -
 12.2156 -lemma integral_sums:
 12.2157 -  assumes integrable[measurable]: "\<And>i. integrable M (f i)"
 12.2158 -  and summable: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>f i x\<bar>)"
 12.2159 -  and sums: "summable (\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
 12.2160 -  shows "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
 12.2161 -  and "(\<lambda>i. integral\<^sup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is ?integral)
 12.2162 -proof -
 12.2163 -  have "\<forall>x\<in>space M. \<exists>w. (\<lambda>i. \<bar>f i x\<bar>) sums w"
 12.2164 -    using summable unfolding summable_def by auto
 12.2165 -  from bchoice[OF this]
 12.2166 -  obtain w where w: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. \<bar>f i x\<bar>) sums w x" by auto
 12.2167 -  then have w_borel: "w \<in> borel_measurable M" unfolding sums_def
 12.2168 -    by (rule borel_measurable_LIMSEQ) auto
 12.2169 -
 12.2170 -  let ?w = "\<lambda>y. if y \<in> space M then w y else 0"
 12.2171 -
 12.2172 -  obtain x where abs_sum: "(\<lambda>i. (\<integral>x. \<bar>f i x\<bar> \<partial>M)) sums x"
 12.2173 -    using sums unfolding summable_def ..
 12.2174 -
 12.2175 -  have 1: "\<And>n. integrable M (\<lambda>x. \<Sum>i<n. f i x)"
 12.2176 -    using integrable by auto
 12.2177 -
 12.2178 -  have 2: "\<And>j. AE x in M. \<bar>\<Sum>i<j. f i x\<bar> \<le> ?w x"
 12.2179 -    using AE_space
 12.2180 -  proof eventually_elim
 12.2181 -    fix j x assume [simp]: "x \<in> space M"
 12.2182 -    have "\<bar>\<Sum>i<j. f i x\<bar> \<le> (\<Sum>i<j. \<bar>f i x\<bar>)" by (rule setsum_abs)
 12.2183 -    also have "\<dots> \<le> w x" using w[of x] setsum_le_suminf[of "\<lambda>i. \<bar>f i x\<bar>"] unfolding sums_iff by auto
 12.2184 -    finally show "\<bar>\<Sum>i<j. f i x\<bar> \<le> ?w x" by simp
 12.2185 -  qed
 12.2186 -
 12.2187 -  have 3: "integrable M ?w"
 12.2188 -  proof (rule integral_monotone_convergence(1))
 12.2189 -    let ?F = "\<lambda>n y. (\<Sum>i<n. \<bar>f i y\<bar>)"
 12.2190 -    let ?w' = "\<lambda>n y. if y \<in> space M then ?F n y else 0"
 12.2191 -    have "\<And>n. integrable M (?F n)"
 12.2192 -      using integrable by (auto intro!: integrable_abs)
 12.2193 -    thus "\<And>n. integrable M (?w' n)" by (simp cong: integrable_cong)
 12.2194 -    show "AE x in M. mono (\<lambda>n. ?w' n x)"
 12.2195 -      by (auto simp: mono_def le_fun_def intro!: setsum_mono2)
 12.2196 -    show "AE x in M. (\<lambda>n. ?w' n x) ----> ?w x"
 12.2197 -        using w by (simp_all add: tendsto_const sums_def)
 12.2198 -    have *: "\<And>n. integral\<^sup>L M (?w' n) = (\<Sum>i< n. (\<integral>x. \<bar>f i x\<bar> \<partial>M))"
 12.2199 -      using integrable by (simp add: integrable_abs cong: integral_cong)
 12.2200 -    from abs_sum
 12.2201 -    show "(\<lambda>i. integral\<^sup>L M (?w' i)) ----> x" unfolding * sums_def .
 12.2202 -  qed (simp add: w_borel measurable_If_set)
 12.2203 -
 12.2204 -  from summable[THEN summable_rabs_cancel]
 12.2205 -  have 4: "AE x in M. (\<lambda>n. \<Sum>i<n. f i x) ----> (\<Sum>i. f i x)"
 12.2206 -    by (auto intro: summable_LIMSEQ)
 12.2207 -
 12.2208 -  note int = integral_dominated_convergence(1,3)[OF 1 2 3 4
 12.2209 -    borel_measurable_suminf[OF integrableD(1)[OF integrable]]]
 12.2210 -
 12.2211 -  from int show "integrable M ?S" by simp
 12.2212 -
 12.2213 -  show ?integral unfolding sums_def integral_setsum(1)[symmetric, OF integrable]
 12.2214 -    using int(2) by simp
 12.2215 -qed
 12.2216 -
 12.2217 -lemma integrable_mult_indicator:
 12.2218 -  "A \<in> sets M \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. f x * indicator A x)"
 12.2219 -  by (rule integrable_bound[where f="\<lambda>x. \<bar>f x\<bar>"])
 12.2220 -     (auto intro: integrable_abs split: split_indicator)
 12.2221 -
 12.2222 -lemma tendsto_integral_at_top:
 12.2223 -  fixes M :: "real measure"
 12.2224 -  assumes M: "sets M = sets borel" and f[measurable]: "integrable M f"
 12.2225 -  shows "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> \<integral> x. f x \<partial>M) at_top"
 12.2226 -proof -
 12.2227 -  have M_measure[simp]: "borel_measurable M = borel_measurable borel"
 12.2228 -    using M by (simp add: sets_eq_imp_space_eq measurable_def)
 12.2229 -  { fix f assume f: "integrable M f" "\<And>x. 0 \<le> f x"
 12.2230 -    then have [measurable]: "f \<in> borel_measurable borel"
 12.2231 -      by (simp add: integrable_def)
 12.2232 -    have "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> \<integral> x. f x \<partial>M) at_top"
 12.2233 -    proof (rule tendsto_at_topI_sequentially)
 12.2234 -      have "\<And>j. AE x in M. \<bar>f x * indicator {.. j} x\<bar> \<le> f x"
 12.2235 -        using f(2) by (intro AE_I2) (auto split: split_indicator)
 12.2236 -      have int: "\<And>n. integrable M (\<lambda>x. f x * indicator {.. n} x)"
 12.2237 -        by (rule integrable_mult_indicator) (auto simp: M f)
 12.2238 -      show "(\<lambda>n. \<integral> x. f x * indicator {..real n} x \<partial>M) ----> integral\<^sup>L M f"
 12.2239 -      proof (rule integral_dominated_convergence)
 12.2240 -        { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
 12.2241 -            by (rule eventually_sequentiallyI[of "natceiling x"])
 12.2242 -               (auto split: split_indicator simp: natceiling_le_eq) }
 12.2243 -        from filterlim_cong[OF refl refl this]
 12.2244 -        show "AE x in M. (\<lambda>n. f x * indicator {..real n} x) ----> f x"
 12.2245 -          by (simp add: tendsto_const)
 12.2246 -      qed (fact+, simp)
 12.2247 -      show "mono (\<lambda>y. \<integral> x. f x * indicator {..y} x \<partial>M)"
 12.2248 -        by (intro monoI integral_mono int) (auto split: split_indicator intro: f)
 12.2249 -    qed }
 12.2250 -  note nonneg = this
 12.2251 -  let ?P = "\<lambda>y. \<integral> x. max 0 (f x) * indicator {..y} x \<partial>M"
 12.2252 -  let ?N = "\<lambda>y. \<integral> x. max 0 (- f x) * indicator {..y} x \<partial>M"
 12.2253 -  let ?p = "integral\<^sup>L M (\<lambda>x. max 0 (f x))"
 12.2254 -  let ?n = "integral\<^sup>L M (\<lambda>x. max 0 (- f x))"
 12.2255 -  have "(?P ---> ?p) at_top" "(?N ---> ?n) at_top"
 12.2256 -    by (auto intro!: nonneg integrable_max f)
 12.2257 -  note tendsto_diff[OF this]
 12.2258 -  also have "(\<lambda>y. ?P y - ?N y) = (\<lambda>y. \<integral> x. f x * indicator {..y} x \<partial>M)"
 12.2259 -    by (subst integral_diff(2)[symmetric])
 12.2260 -       (auto intro!: integrable_mult_indicator integrable_max f integral_cong ext
 12.2261 -             simp: M split: split_max)
 12.2262 -  also have "?p - ?n = integral\<^sup>L M f"
 12.2263 -    by (subst integral_diff(2)[symmetric])
 12.2264 -       (auto intro!: integrable_max f integral_cong ext simp: M split: split_max)
 12.2265 -  finally show ?thesis .
 12.2266 -qed
 12.2267 -
 12.2268 -lemma integral_monotone_convergence_at_top:
 12.2269 -  fixes M :: "real measure"
 12.2270 -  assumes M: "sets M = sets borel"
 12.2271 -  assumes nonneg: "AE x in M. 0 \<le> f x"
 12.2272 -  assumes borel: "f \<in> borel_measurable borel"
 12.2273 -  assumes int: "\<And>y. integrable M (\<lambda>x. f x * indicator {.. y} x)"
 12.2274 -  assumes conv: "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) ---> x) at_top"
 12.2275 -  shows "integrable M f" "integral\<^sup>L M f = x"
 12.2276 -proof -
 12.2277 -  from nonneg have "AE x in M. mono (\<lambda>n::nat. f x * indicator {..real n} x)"
 12.2278 -    by (auto split: split_indicator intro!: monoI)
 12.2279 -  { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
 12.2280 -      by (rule eventually_sequentiallyI[of "natceiling x"])
 12.2281 -         (auto split: split_indicator simp: natceiling_le_eq) }
 12.2282 -  from filterlim_cong[OF refl refl this]
 12.2283 -  have "AE x in M. (\<lambda>i. f x * indicator {..real i} x) ----> f x"
 12.2284 -    by (simp add: tendsto_const)
 12.2285 -  have "(\<lambda>i. \<integral> x. f x * indicator {..real i} x \<partial>M) ----> x"
 12.2286 -    using conv filterlim_real_sequentially by (rule filterlim_compose)
 12.2287 -  have M_measure[simp]: "borel_measurable M = borel_measurable borel"
 12.2288 -    using M by (simp add: sets_eq_imp_space_eq measurable_def)
 12.2289 -  have "f \<in> borel_measurable M"
 12.2290 -    using borel by simp
 12.2291 -  show "integrable M f"
 12.2292 -    by (rule integral_monotone_convergence) fact+
 12.2293 -  show "integral\<^sup>L M f = x"
 12.2294 -    by (rule integral_monotone_convergence) fact+
 12.2295 -qed
 12.2296 -
 12.2297 -
 12.2298 -section "Lebesgue integration on countable spaces"
 12.2299 -
 12.2300 -lemma integral_on_countable:
 12.2301 -  assumes f: "f \<in> borel_measurable M"
 12.2302 -  and bij: "bij_betw enum S (f ` space M)"
 12.2303 -  and enum_zero: "enum ` (-S) \<subseteq> {0}"
 12.2304 -  and fin: "\<And>x. x \<noteq> 0 \<Longrightarrow> (emeasure M) (f -` {x} \<inter> space M) \<noteq> \<infinity>"
 12.2305 -  and abs_summable: "summable (\<lambda>r. \<bar>enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))\<bar>)"
 12.2306 -  shows "integrable M f"
 12.2307 -  and "(\<lambda>r. enum r * real ((emeasure M) (f -` {enum r} \<inter> space M))) sums integral\<^sup>L M f" (is ?sums)
 12.2308 -proof -
 12.2309 -  let ?A = "\<lambda>r. f -` {enum r} \<inter> space M"
 12.2310 -  let ?F = "\<lambda>r x. enum r * indicator (?A r) x"
 12.2311 -  have enum_eq: "\<And>r. enum r * real ((emeasure M) (?A r)) = integral\<^sup>L M (?F r)"
 12.2312 -    using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
 12.2313 -
 12.2314 -  { fix x assume "x \<in> space M"
 12.2315 -    hence "f x \<in> enum ` S" using bij unfolding bij_betw_def by auto
 12.2316 -    then obtain i where "i\<in>S" "enum i = f x" by auto
 12.2317 -    have F: "\<And>j. ?F j x = (if j = i then f x else 0)"
 12.2318 -    proof cases
 12.2319 -      fix j assume "j = i"
 12.2320 -      thus "?thesis j" using `x \<in> space M` `enum i = f x` by (simp add: indicator_def)
 12.2321 -    next
 12.2322 -      fix j assume "j \<noteq> i"
 12.2323 -      show "?thesis j" using bij `i \<in> S` `j \<noteq> i` `enum i = f x` enum_zero
 12.2324 -        by (cases "j \<in> S") (auto simp add: indicator_def bij_betw_def inj_on_def)
 12.2325 -    qed
 12.2326 -    hence F_abs: "\<And>j. \<bar>if j = i then f x else 0\<bar> = (if j = i then \<bar>f x\<bar> else 0)" by auto
 12.2327 -    have "(\<lambda>i. ?F i x) sums f x"
 12.2328 -         "(\<lambda>i. \<bar>?F i x\<bar>) sums \<bar>f x\<bar>"
 12.2329 -      by (auto intro!: sums_single simp: F F_abs) }
 12.2330 -  note F_sums_f = this(1) and F_abs_sums_f = this(2)
 12.2331 -
 12.2332 -  have int_f: "integral\<^sup>L M f = (\<integral>x. (\<Sum>r. ?F r x) \<partial>M)" "integrable M f = integrable M (\<lambda>x. \<Sum>r. ?F r x)"
 12.2333 -    using F_sums_f by (auto intro!: integral_cong integrable_cong simp: sums_iff)
 12.2334 -
 12.2335 -  { fix r
 12.2336 -    have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = (\<integral>x. \<bar>enum r\<bar> * indicator (?A r) x \<partial>M)"
 12.2337 -      by (auto simp: indicator_def intro!: integral_cong)
 12.2338 -    also have "\<dots> = \<bar>enum r\<bar> * real ((emeasure M) (?A r))"
 12.2339 -      using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
 12.2340 -    finally have "(\<integral>x. \<bar>?F r x\<bar> \<partial>M) = \<bar>enum r * real ((emeasure M) (?A r))\<bar>"
 12.2341 -      using f by (subst (2) abs_mult_pos[symmetric]) (auto intro!: real_of_ereal_pos measurable_sets) }
 12.2342 -  note int_abs_F = this
 12.2343 -
 12.2344 -  have 1: "\<And>i. integrable M (\<lambda>x. ?F i x)"
 12.2345 -    using f fin by (simp add: borel_measurable_vimage integral_cmul_indicator)
 12.2346 -
 12.2347 -  have 2: "\<And>x. x \<in> space M \<Longrightarrow> summable (\<lambda>i. \<bar>?F i x\<bar>)"
 12.2348 -    using F_abs_sums_f unfolding sums_iff by auto
 12.2349 -
 12.2350 -  from integral_sums(2)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
 12.2351 -  show ?sums unfolding enum_eq int_f by simp
 12.2352 -
 12.2353 -  from integral_sums(1)[OF 1 2, unfolded int_abs_F, OF _ abs_summable]
 12.2354 -  show "integrable M f" unfolding int_f by simp
 12.2355 -qed
 12.2356 -
 12.2357 -section {* Distributions *}
 12.2358 -
 12.2359 -lemma positive_integral_distr':
 12.2360 -  assumes T: "T \<in> measurable M M'"
 12.2361 -  and f: "f \<in> borel_measurable (distr M M' T)" "\<And>x. 0 \<le> f x"
 12.2362 -  shows "integral\<^sup>P (distr M M' T) f = (\<integral>\<^sup>+ x. f (T x) \<partial>M)"
 12.2363 -  using f 
 12.2364 -proof induct
 12.2365 -  case (cong f g)
 12.2366 -  with T show ?case
 12.2367 -    apply (subst positive_integral_cong[of _ f g])
 12.2368 -    apply simp
 12.2369 -    apply (subst positive_integral_cong[of _ "\<lambda>x. f (T x)" "\<lambda>x. g (T x)"])
 12.2370 -    apply (simp add: measurable_def Pi_iff)
 12.2371 -    apply simp
 12.2372 -    done
 12.2373 -next
 12.2374 -  case (set A)
 12.2375 -  then have eq: "\<And>x. x \<in> space M \<Longrightarrow> indicator A (T x) = indicator (T -` A \<inter> space M) x"
 12.2376 -    by (auto simp: indicator_def)
 12.2377 -  from set T show ?case
 12.2378 -    by (subst positive_integral_cong[OF eq])
 12.2379 -       (auto simp add: emeasure_distr intro!: positive_integral_indicator[symmetric] measurable_sets)
 12.2380 -qed (simp_all add: measurable_compose[OF T] T positive_integral_cmult positive_integral_add
 12.2381 -                   positive_integral_monotone_convergence_SUP le_fun_def incseq_def)
 12.2382 -
 12.2383 -lemma positive_integral_distr:
 12.2384 -  "T \<in> measurable M M' \<Longrightarrow> f \<in> borel_measurable M' \<Longrightarrow> integral\<^sup>P (distr M M' T) f = (\<integral>\<^sup>+ x. f (T x) \<partial>M)"
 12.2385 -  by (subst (1 2) positive_integral_max_0[symmetric])
 12.2386 -     (simp add: positive_integral_distr')
 12.2387 -
 12.2388 -lemma integral_distr:
 12.2389 -  "T \<in> measurable M M' \<Longrightarrow> f \<in> borel_measurable M' \<Longrightarrow> integral\<^sup>L (distr M M' T) f = (\<integral> x. f (T x) \<partial>M)"
 12.2390 -  unfolding lebesgue_integral_def
 12.2391 -  by (subst (1 2) positive_integral_distr) auto
 12.2392 -
 12.2393 -lemma integrable_distr_eq:
 12.2394 -  "T \<in> measurable M M' \<Longrightarrow> f \<in> borel_measurable M' \<Longrightarrow> integrable (distr M M' T) f \<longleftrightarrow> integrable M (\<lambda>x. f (T x))"
 12.2395 -  unfolding integrable_def 
 12.2396 -  by (subst (1 2) positive_integral_distr) (auto simp: comp_def)
 12.2397 -
 12.2398 -lemma integrable_distr:
 12.2399 -  "T \<in> measurable M M' \<Longrightarrow> integrable (distr M M' T) f \<Longrightarrow> integrable M (\<lambda>x. f (T x))"
 12.2400 -  by (subst integrable_distr_eq[symmetric]) auto
 12.2401 -
 12.2402 -section {* Lebesgue integration on @{const count_space} *}
 12.2403 -
 12.2404 -lemma simple_function_count_space[simp]:
 12.2405 -  "simple_function (count_space A) f \<longleftrightarrow> finite (f ` A)"
 12.2406 -  unfolding simple_function_def by simp
 12.2407 -
 12.2408 -lemma positive_integral_count_space:
 12.2409 -  assumes A: "finite {a\<in>A. 0 < f a}"
 12.2410 -  shows "integral\<^sup>P (count_space A) f = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
 12.2411 -proof -
 12.2412 -  have *: "(\<integral>\<^sup>+x. max 0 (f x) \<partial>count_space A) =
 12.2413 -    (\<integral>\<^sup>+ x. (\<Sum>a|a\<in>A \<and> 0 < f a. f a * indicator {a} x) \<partial>count_space A)"
 12.2414 -    by (auto intro!: positive_integral_cong
 12.2415 -             simp add: indicator_def if_distrib setsum_cases[OF A] max_def le_less)
 12.2416 -  also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. \<integral>\<^sup>+ x. f a * indicator {a} x \<partial>count_space A)"
 12.2417 -    by (subst positive_integral_setsum)
 12.2418 -       (simp_all add: AE_count_space ereal_zero_le_0_iff less_imp_le)
 12.2419 -  also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
 12.2420 -    by (auto intro!: setsum_cong simp: positive_integral_cmult_indicator one_ereal_def[symmetric])
 12.2421 -  finally show ?thesis by (simp add: positive_integral_max_0)
 12.2422 -qed
 12.2423 -
 12.2424 -lemma integrable_count_space:
 12.2425 -  "finite X \<Longrightarrow> integrable (count_space X) f"
 12.2426 -  by (auto simp: positive_integral_count_space integrable_def)
 12.2427 -
 12.2428 -lemma positive_integral_count_space_finite:
 12.2429 -    "finite A \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>count_space A) = (\<Sum>a\<in>A. max 0 (f a))"
 12.2430 -  by (subst positive_integral_max_0[symmetric])
 12.2431 -     (auto intro!: setsum_mono_zero_left simp: positive_integral_count_space less_le)
 12.2432 -
 12.2433 -lemma lebesgue_integral_count_space_finite_support:
 12.2434 -  assumes f: "finite {a\<in>A. f a \<noteq> 0}" shows "(\<integral>x. f x \<partial>count_space A) = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. f a)"
 12.2435 -proof -
 12.2436 -  have *: "\<And>r::real. 0 < max 0 r \<longleftrightarrow> 0 < r" "\<And>x. max 0 (ereal x) = ereal (max 0 x)"
 12.2437 -    "\<And>a. a \<in> A \<and> 0 < f a \<Longrightarrow> max 0 (f a) = f a"
 12.2438 -    "\<And>a. a \<in> A \<and> f a < 0 \<Longrightarrow> max 0 (- f a) = - f a"
 12.2439 -    "{a \<in> A. f a \<noteq> 0} = {a \<in> A. 0 < f a} \<union> {a \<in> A. f a < 0}"
 12.2440 -    "({a \<in> A. 0 < f a} \<inter> {a \<in> A. f a < 0}) = {}"
 12.2441 -    by (auto split: split_max)
 12.2442 -  have "finite {a \<in> A. 0 < f a}" "finite {a \<in> A. f a < 0}"
 12.2443 -    by (auto intro: finite_subset[OF _ f])
 12.2444 -  then show ?thesis
 12.2445 -    unfolding lebesgue_integral_def
 12.2446 -    apply (subst (1 2) positive_integral_max_0[symmetric])
 12.2447 -    apply (subst (1 2) positive_integral_count_space)
 12.2448 -    apply (auto simp add: * setsum_negf setsum_Un
 12.2449 -                simp del: ereal_max)
 12.2450 -    done
 12.2451 -qed
 12.2452 -
 12.2453 -lemma lebesgue_integral_count_space_finite:
 12.2454 -    "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
 12.2455 -  apply (auto intro!: setsum_mono_zero_left
 12.2456 -              simp: positive_integral_count_space_finite lebesgue_integral_def)
 12.2457 -  apply (subst (1 2)  setsum_real_of_ereal[symmetric])
 12.2458 -  apply (auto simp: max_def setsum_subtractf[symmetric] intro!: setsum_cong)
 12.2459 -  done
 12.2460 -
 12.2461 -lemma emeasure_UN_countable:
 12.2462 -  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> sets M" and I: "countable I" 
 12.2463 -  assumes disj: "disjoint_family_on X I"
 12.2464 -  shows "emeasure M (UNION I X) = (\<integral>\<^sup>+i. emeasure M (X i) \<partial>count_space I)"
 12.2465 -proof cases
 12.2466 -  assume "finite I" with sets disj show ?thesis
 12.2467 -    by (subst setsum_emeasure[symmetric])
 12.2468 -       (auto intro!: setsum_cong simp add: max_def subset_eq positive_integral_count_space_finite emeasure_nonneg)
 12.2469 -next
 12.2470 -  assume f: "\<not> finite I"
 12.2471 -  then have [intro]: "I \<noteq> {}" by auto
 12.2472 -  from from_nat_into_inj_infinite[OF I f] from_nat_into[OF this] disj
 12.2473 -  have disj2: "disjoint_family (\<lambda>i. X (from_nat_into I i))"
 12.2474 -    unfolding disjoint_family_on_def by metis
 12.2475 -
 12.2476 -  from f have "bij_betw (from_nat_into I) UNIV I"
 12.2477 -    using bij_betw_from_nat_into[OF I] by simp
 12.2478 -  then have "(\<Union>i\<in>I. X i) = (\<Union>i. (X \<circ> from_nat_into I) i)"
 12.2479 -    unfolding SUP_def image_comp [symmetric] by (simp add: bij_betw_def)
 12.2480 -  then have "emeasure M (UNION I X) = emeasure M (\<Union>i. X (from_nat_into I i))"
 12.2481 -    by simp
 12.2482 -  also have "\<dots> = (\<Sum>i. emeasure M (X (from_nat_into I i)))"
 12.2483 -    by (intro suminf_emeasure[symmetric] disj disj2) (auto intro!: sets from_nat_into[OF `I \<noteq> {}`])
 12.2484 -  also have "\<dots> = (\<Sum>n. \<integral>\<^sup>+i. emeasure M (X i) * indicator {from_nat_into I n} i \<partial>count_space I)"
 12.2485 -  proof (intro arg_cong[where f=suminf] ext)
 12.2486 -    fix i
 12.2487 -    have eq: "{a \<in> I. 0 < emeasure M (X a) * indicator {from_nat_into I i} a}
 12.2488 -     = (if 0 < emeasure M (X (from_nat_into I i)) then {from_nat_into I i} else {})"
 12.2489 -     using ereal_0_less_1
 12.2490 -     by (auto simp: ereal_zero_less_0_iff indicator_def from_nat_into `I \<noteq> {}` simp del: ereal_0_less_1)
 12.2491 -    have "(\<integral>\<^sup>+ ia. emeasure M (X ia) * indicator {from_nat_into I i} ia \<partial>count_space I) =
 12.2492 -      (if 0 < emeasure M (X (from_nat_into I i)) then emeasure M (X (from_nat_into I i)) else 0)"
 12.2493 -      by (subst positive_integral_count_space) (simp_all add: eq)
 12.2494 -    also have "\<dots> = emeasure M (X (from_nat_into I i))"
 12.2495 -      by (simp add: less_le emeasure_nonneg)
 12.2496 -    finally show "emeasure M (X (from_nat_into I i)) =
 12.2497 -         \<integral>\<^sup>+ ia. emeasure M (X ia) * indicator {from_nat_into I i} ia \<partial>count_space I" ..
 12.2498 -  qed
 12.2499 -  also have "\<dots> = (\<integral>\<^sup>+i. emeasure M (X i) \<partial>count_space I)"
 12.2500 -    apply (subst positive_integral_suminf[symmetric])
 12.2501 -    apply (auto simp: emeasure_nonneg intro!: positive_integral_cong)
 12.2502 -  proof -
 12.2503 -    fix x assume "x \<in> I"
 12.2504 -    then have "(\<Sum>i. emeasure M (X x) * indicator {from_nat_into I i} x) = (\<Sum>i\<in>{to_nat_on I x}. emeasure M (X x) * indicator {from_nat_into I i} x)"
 12.2505 -      by (intro suminf_finite) (auto simp: indicator_def I f)
 12.2506 -    also have "\<dots> = emeasure M (X x)"
 12.2507 -      by (simp add: I f `x\<in>I`)
 12.2508 -    finally show "(\<Sum>i. emeasure M (X x) * indicator {from_nat_into I i} x) = emeasure M (X x)" .
 12.2509 -  qed
 12.2510 -  finally show ?thesis .
 12.2511 -qed
 12.2512 -
 12.2513 -section {* Measures with Restricted Space *}
 12.2514 -
 12.2515 -lemma positive_integral_restrict_space:
 12.2516 -  assumes \<Omega>: "\<Omega> \<in> sets M" and f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" "\<And>x. x \<in> space M - \<Omega> \<Longrightarrow> f x = 0"
 12.2517 -  shows "positive_integral (restrict_space M \<Omega>) f = positive_integral M f"
 12.2518 -using f proof (induct rule: borel_measurable_induct)
 12.2519 -  case (cong f g) then show ?case
 12.2520 -    using positive_integral_cong[of M f g] positive_integral_cong[of "restrict_space M \<Omega>" f g]
 12.2521 -      sets.sets_into_space[OF `\<Omega> \<in> sets M`]
 12.2522 -    by (simp add: subset_eq space_restrict_space)
 12.2523 -next
 12.2524 -  case (set A)
 12.2525 -  then have "A \<subseteq> \<Omega>"
 12.2526 -    unfolding indicator_eq_0_iff by (auto dest: sets.sets_into_space)
 12.2527 -  with set `\<Omega> \<in> sets M` sets.sets_into_space[OF `\<Omega> \<in> sets M`] show ?case
 12.2528 -    by (subst positive_integral_indicator')
 12.2529 -       (auto simp add: sets_restrict_space_iff space_restrict_space
 12.2530 -                  emeasure_restrict_space Int_absorb2
 12.2531 -                dest: sets.sets_into_space)
 12.2532 -next
 12.2533 -  case (mult f c) then show ?case
 12.2534 -    by (cases "c = 0") (simp_all add: measurable_restrict_space1 \<Omega> positive_integral_cmult)
 12.2535 -next
 12.2536 -  case (add f g) then show ?case
 12.2537 -    by (simp add: measurable_restrict_space1 \<Omega> positive_integral_add ereal_add_nonneg_eq_0_iff)
 12.2538 -next
 12.2539 -  case (seq F) then show ?case
 12.2540 -    by (auto simp add: SUP_eq_iff measurable_restrict_space1 \<Omega> positive_integral_monotone_convergence_SUP)
 12.2541 -qed
 12.2542 -
 12.2543 -section {* Measure spaces with an associated density *}
 12.2544 -
 12.2545 -definition density :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
 12.2546 -  "density M f = measure_of (space M) (sets M) (\<lambda>A. \<integral>\<^sup>+ x. f x * indicator A x \<partial>M)"
 12.2547 -
 12.2548 -lemma 
 12.2549 -  shows sets_density[simp]: "sets (density M f) = sets M"
 12.2550 -    and space_density[simp]: "space (density M f) = space M"
 12.2551 -  by (auto simp: density_def)
 12.2552 -
 12.2553 -(* FIXME: add conversion to simplify space, sets and measurable *)
 12.2554 -lemma space_density_imp[measurable_dest]:
 12.2555 -  "\<And>x M f. x \<in> space (density M f) \<Longrightarrow> x \<in> space M" by auto
 12.2556 -
 12.2557 -lemma 
 12.2558 -  shows measurable_density_eq1[simp]: "g \<in> measurable (density Mg f) Mg' \<longleftrightarrow> g \<in> measurable Mg Mg'"
 12.2559 -    and measurable_density_eq2[simp]: "h \<in> measurable Mh (density Mh' f) \<longleftrightarrow> h \<in> measurable Mh Mh'"
 12.2560 -    and simple_function_density_eq[simp]: "simple_function (density Mu f) u \<longleftrightarrow> simple_function Mu u"
 12.2561 -  unfolding measurable_def simple_function_def by simp_all
 12.2562 -
 12.2563 -lemma density_cong: "f \<in> borel_measurable M \<Longrightarrow> f' \<in> borel_measurable M \<Longrightarrow>
 12.2564 -  (AE x in M. f x = f' x) \<Longrightarrow> density M f = density M f'"
 12.2565 -  unfolding density_def by (auto intro!: measure_of_eq positive_integral_cong_AE sets.space_closed)
 12.2566 -
 12.2567 -lemma density_max_0: "density M f = density M (\<lambda>x. max 0 (f x))"
 12.2568 -proof -
 12.2569 -  have "\<And>x A. max 0 (f x) * indicator A x = max 0 (f x * indicator A x)"
 12.2570 -    by (auto simp: indicator_def)
 12.2571 -  then show ?thesis
 12.2572 -    unfolding density_def by (simp add: positive_integral_max_0)
 12.2573 -qed
 12.2574 -
 12.2575 -lemma density_ereal_max_0: "density M (\<lambda>x. ereal (f x)) = density M (\<lambda>x. ereal (max 0 (f x)))"
 12.2576 -  by (subst density_max_0) (auto intro!: arg_cong[where f="density M"] split: split_max)
 12.2577 -
 12.2578 -lemma emeasure_density:
 12.2579 -  assumes f[measurable]: "f \<in> borel_measurable M" and A[measurable]: "A \<in> sets M"
 12.2580 -  shows "emeasure (density M f) A = (\<integral>\<^sup>+ x. f x * indicator A x \<partial>M)"
 12.2581 -    (is "_ = ?\<mu> A")
 12.2582 -  unfolding density_def
 12.2583 -proof (rule emeasure_measure_of_sigma)
 12.2584 -  show "sigma_algebra (space M) (sets M)" ..
 12.2585 -  show "positive (sets M) ?\<mu>"
 12.2586 -    using f by (auto simp: positive_def intro!: positive_integral_positive)
 12.2587 -  have \<mu>_eq: "?\<mu> = (\<lambda>A. \<integral>\<^sup>+ x. max 0 (f x) * indicator A x \<partial>M)" (is "?\<mu> = ?\<mu>'")
 12.2588 -    apply (subst positive_integral_max_0[symmetric])
 12.2589 -    apply (intro ext positive_integral_cong_AE AE_I2)
 12.2590 -    apply (auto simp: indicator_def)
 12.2591 -    done
 12.2592 -  show "countably_additive (sets M) ?\<mu>"
 12.2593 -    unfolding \<mu>_eq
 12.2594 -  proof (intro countably_additiveI)
 12.2595 -    fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M"
 12.2596 -    then have "\<And>i. A i \<in> sets M" by auto
 12.2597 -    then have *: "\<And>i. (\<lambda>x. max 0 (f x) * indicator (A i) x) \<in> borel_measurable M"
 12.2598 -      by (auto simp: set_eq_iff)
 12.2599 -    assume disj: "disjoint_family A"
 12.2600 -    have "(\<Sum>n. ?\<mu>' (A n)) = (\<integral>\<^sup>+ x. (\<Sum>n. max 0 (f x) * indicator (A n) x) \<partial>M)"
 12.2601 -      using f * by (simp add: positive_integral_suminf)
 12.2602 -    also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) * (\<Sum>n. indicator (A n) x) \<partial>M)" using f
 12.2603 -      by (auto intro!: suminf_cmult_ereal positive_integral_cong_AE)
 12.2604 -    also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) * indicator (\<Union>n. A n) x \<partial>M)"
 12.2605 -      unfolding suminf_indicator[OF disj] ..
 12.2606 -    finally show "(\<Sum>n. ?\<mu>' (A n)) = ?\<mu>' (\<Union>x. A x)" by simp
 12.2607 -  qed
 12.2608 -qed fact
 12.2609 -
 12.2610 -lemma null_sets_density_iff:
 12.2611 -  assumes f: "f \<in> borel_measurable M"
 12.2612 -  shows "A \<in> null_sets (density M f) \<longleftrightarrow> A \<in> sets M \<and> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
 12.2613 -proof -
 12.2614 -  { assume "A \<in> sets M"
 12.2615 -    have eq: "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. max 0 (f x) * indicator A x \<partial>M)"
 12.2616 -      apply (subst positive_integral_max_0[symmetric])
 12.2617 -      apply (intro positive_integral_cong)
 12.2618 -      apply (auto simp: indicator_def)
 12.2619 -      done
 12.2620 -    have "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> 
 12.2621 -      emeasure M {x \<in> space M. max 0 (f x) * indicator A x \<noteq> 0} = 0"
 12.2622 -      unfolding eq
 12.2623 -      using f `A \<in> sets M`
 12.2624 -      by (intro positive_integral_0_iff) auto
 12.2625 -    also have "\<dots> \<longleftrightarrow> (AE x in M. max 0 (f x) * indicator A x = 0)"
 12.2626 -      using f `A \<in> sets M`
 12.2627 -      by (intro AE_iff_measurable[OF _ refl, symmetric]) auto
 12.2628 -    also have "(AE x in M. max 0 (f x) * indicator A x = 0) \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
 12.2629 -      by (auto simp add: indicator_def max_def split: split_if_asm)
 12.2630 -    finally have "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)" . }
 12.2631 -  with f show ?thesis
 12.2632 -    by (simp add: null_sets_def emeasure_density cong: conj_cong)
 12.2633 -qed
 12.2634 -
 12.2635 -lemma AE_density:
 12.2636 -  assumes f: "f \<in> borel_measurable M"
 12.2637 -  shows "(AE x in density M f. P x) \<longleftrightarrow> (AE x in M. 0 < f x \<longrightarrow> P x)"
 12.2638 -proof
 12.2639 -  assume "AE x in density M f. P x"
 12.2640 -  with f obtain N where "{x \<in> space M. \<not> P x} \<subseteq> N" "N \<in> sets M" and ae: "AE x in M. x \<in> N \<longrightarrow> f x \<le> 0"
 12.2641 -    by (auto simp: eventually_ae_filter null_sets_density_iff)
 12.2642 -  then have "AE x in M. x \<notin> N \<longrightarrow> P x" by auto
 12.2643 -  with ae show "AE x in M. 0 < f x \<longrightarrow> P x"
 12.2644 -    by (rule eventually_elim2) auto
 12.2645 -next
 12.2646 -  fix N assume ae: "AE x in M. 0 < f x \<longrightarrow> P x"
 12.2647 -  then obtain N where "{x \<in> space M. \<not> (0 < f x \<longrightarrow> P x)} \<subseteq> N" "N \<in> null_sets M"
 12.2648 -    by (auto simp: eventually_ae_filter)
 12.2649 -  then have *: "{x \<in> space (density M f). \<not> P x} \<subseteq> N \<union> {x\<in>space M. \<not> 0 < f x}"
 12.2650 -    "N \<union> {x\<in>space M. \<not> 0 < f x} \<in> sets M" and ae2: "AE x in M. x \<notin> N"
 12.2651 -    using f by (auto simp: subset_eq intro!: sets.sets_Collect_neg AE_not_in)
 12.2652 -  show "AE x in density M f. P x"
 12.2653 -    using ae2
 12.2654 -    unfolding eventually_ae_filter[of _ "density M f"] Bex_def null_sets_density_iff[OF f]
 12.2655 -    by (intro exI[of _ "N \<union> {x\<in>space M. \<not> 0 < f x}"] conjI *)
 12.2656 -       (auto elim: eventually_elim2)
 12.2657 -qed
 12.2658 -
 12.2659 -lemma positive_integral_density':
 12.2660 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
 12.2661 -  assumes g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
 12.2662 -  shows "integral\<^sup>P (density M f) g = (\<integral>\<^sup>+ x. f x * g x \<partial>M)"
 12.2663 -using g proof induct
 12.2664 -  case (cong u v)
 12.2665 -  then show ?case
 12.2666 -    apply (subst positive_integral_cong[OF cong(3)])
 12.2667 -    apply (simp_all cong: positive_integral_cong)
 12.2668 -    done
 12.2669 -next
 12.2670 -  case (set A) then show ?case
 12.2671 -    by (simp add: emeasure_density f)
 12.2672 -next
 12.2673 -  case (mult u c)
 12.2674 -  moreover have "\<And>x. f x * (c * u x) = c * (f x * u x)" by (simp add: field_simps)
 12.2675 -  ultimately show ?case
 12.2676 -    using f by (simp add: positive_integral_cmult)
 12.2677 -next
 12.2678 -  case (add u v)
 12.2679 -  then have "\<And>x. f x * (v x + u x) = f x * v x + f x * u x"
 12.2680 -    by (simp add: ereal_right_distrib)
 12.2681 -  with add f show ?case
 12.2682 -    by (auto simp add: positive_integral_add ereal_zero_le_0_iff intro!: positive_integral_add[symmetric])
 12.2683 -next
 12.2684 -  case (seq U)
 12.2685 -  from f(2) have eq: "AE x in M. f x * (SUP i. U i x) = (SUP i. f x * U i x)"
 12.2686 -    by eventually_elim (simp add: SUP_ereal_cmult seq)
 12.2687 -  from seq f show ?case
 12.2688 -    apply (simp add: positive_integral_monotone_convergence_SUP)
 12.2689 -    apply (subst positive_integral_cong_AE[OF eq])
 12.2690 -    apply (subst positive_integral_monotone_convergence_SUP_AE)
 12.2691 -    apply (auto simp: incseq_def le_fun_def intro!: ereal_mult_left_mono)
 12.2692 -    done
 12.2693 -qed
 12.2694 -
 12.2695 -lemma positive_integral_density:
 12.2696 -  "f \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow> g' \<in> borel_measurable M \<Longrightarrow> 
 12.2697 -    integral\<^sup>P (density M f) g' = (\<integral>\<^sup>+ x. f x * g' x \<partial>M)"
 12.2698 -  by (subst (1 2) positive_integral_max_0[symmetric])
 12.2699 -     (auto intro!: positive_integral_cong_AE
 12.2700 -           simp: measurable_If max_def ereal_zero_le_0_iff positive_integral_density')
 12.2701 -
 12.2702 -lemma integral_density:
 12.2703 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
 12.2704 -    and g: "g \<in> borel_measurable M"
 12.2705 -  shows "integral\<^sup>L (density M f) g = (\<integral> x. f x * g x \<partial>M)"
 12.2706 -    and "integrable (density M f) g \<longleftrightarrow> integrable M (\<lambda>x. f x * g x)"
 12.2707 -  unfolding lebesgue_integral_def integrable_def using f g
 12.2708 -  by (auto simp: positive_integral_density)
 12.2709 -
 12.2710 -lemma emeasure_restricted:
 12.2711 -  assumes S: "S \<in> sets M" and X: "X \<in> sets M"
 12.2712 -  shows "emeasure (density M (indicator S)) X = emeasure M (S \<inter> X)"
 12.2713 -proof -
 12.2714 -  have "emeasure (density M (indicator S)) X = (\<integral>\<^sup>+x. indicator S x * indicator X x \<partial>M)"
 12.2715 -    using S X by (simp add: emeasure_density)
 12.2716 -  also have "\<dots> = (\<integral>\<^sup>+x. indicator (S \<inter> X) x \<partial>M)"
 12.2717 -    by (auto intro!: positive_integral_cong simp: indicator_def)
 12.2718 -  also have "\<dots> = emeasure M (S \<inter> X)"
 12.2719 -    using S X by (simp add: sets.Int)
 12.2720 -  finally show ?thesis .
 12.2721 -qed
 12.2722 -
 12.2723 -lemma measure_restricted:
 12.2724 -  "S \<in> sets M \<Longrightarrow> X \<in> sets M \<Longrightarrow> measure (density M (indicator S)) X = measure M (S \<inter> X)"
 12.2725 -  by (simp add: emeasure_restricted measure_def)
 12.2726 -
 12.2727 -lemma (in finite_measure) finite_measure_restricted:
 12.2728 -  "S \<in> sets M \<Longrightarrow> finite_measure (density M (indicator S))"
 12.2729 -  by default (simp add: emeasure_restricted)
 12.2730 -
 12.2731 -lemma emeasure_density_const:
 12.2732 -  "A \<in> sets M \<Longrightarrow> 0 \<le> c \<Longrightarrow> emeasure (density M (\<lambda>_. c)) A = c * emeasure M A"
 12.2733 -  by (auto simp: positive_integral_cmult_indicator emeasure_density)
 12.2734 -
 12.2735 -lemma measure_density_const:
 12.2736 -  "A \<in> sets M \<Longrightarrow> 0 < c \<Longrightarrow> c \<noteq> \<infinity> \<Longrightarrow> measure (density M (\<lambda>_. c)) A = real c * measure M A"
 12.2737 -  by (auto simp: emeasure_density_const measure_def)
 12.2738 -
 12.2739 -lemma density_density_eq:
 12.2740 -   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow>
 12.2741 -   density (density M f) g = density M (\<lambda>x. f x * g x)"
 12.2742 -  by (auto intro!: measure_eqI simp: emeasure_density positive_integral_density ac_simps)
 12.2743 -
 12.2744 -lemma distr_density_distr:
 12.2745 -  assumes T: "T \<in> measurable M M'" and T': "T' \<in> measurable M' M"
 12.2746 -    and inv: "\<forall>x\<in>space M. T' (T x) = x"
 12.2747 -  assumes f: "f \<in> borel_measurable M'"
 12.2748 -  shows "distr (density (distr M M' T) f) M T' = density M (f \<circ> T)" (is "?R = ?L")
 12.2749 -proof (rule measure_eqI)
 12.2750 -  fix A assume A: "A \<in> sets ?R"
 12.2751 -  { fix x assume "x \<in> space M"
 12.2752 -    with sets.sets_into_space[OF A]
 12.2753 -    have "indicator (T' -` A \<inter> space M') (T x) = (indicator A x :: ereal)"
 12.2754 -      using T inv by (auto simp: indicator_def measurable_space) }
 12.2755 -  with A T T' f show "emeasure ?R A = emeasure ?L A"
 12.2756 -    by (simp add: measurable_comp emeasure_density emeasure_distr
 12.2757 -                  positive_integral_distr measurable_sets cong: positive_integral_cong)
 12.2758 -qed simp
 12.2759 -
 12.2760 -lemma density_density_divide:
 12.2761 -  fixes f g :: "'a \<Rightarrow> real"
 12.2762 -  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
 12.2763 -  assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
 12.2764 -  assumes ac: "AE x in M. f x = 0 \<longrightarrow> g x = 0"
 12.2765 -  shows "density (density M f) (\<lambda>x. g x / f x) = density M g"
 12.2766 -proof -
 12.2767 -  have "density M g = density M (\<lambda>x. f x * (g x / f x))"
 12.2768 -    using f g ac by (auto intro!: density_cong measurable_If)
 12.2769 -  then show ?thesis
 12.2770 -    using f g by (subst density_density_eq) auto
 12.2771 -qed
 12.2772 -
 12.2773 -section {* Point measure *}
 12.2774 -
 12.2775 -definition point_measure :: "'a set \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
 12.2776 -  "point_measure A f = density (count_space A) f"
 12.2777 -
 12.2778 -lemma
 12.2779 -  shows space_point_measure: "space (point_measure A f) = A"
 12.2780 -    and sets_point_measure: "sets (point_measure A f) = Pow A"
 12.2781 -  by (auto simp: point_measure_def)
 12.2782 -
 12.2783 -lemma measurable_point_measure_eq1[simp]:
 12.2784 -  "g \<in> measurable (point_measure A f) M \<longleftrightarrow> g \<in> A \<rightarrow> space M"
 12.2785 -  unfolding point_measure_def by simp
 12.2786 -
 12.2787 -lemma measurable_point_measure_eq2_finite[simp]:
 12.2788 -  "finite A \<Longrightarrow>
 12.2789 -   g \<in> measurable M (point_measure A f) \<longleftrightarrow>
 12.2790 -    (g \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. g -` {a} \<inter> space M \<in> sets M))"
 12.2791 -  unfolding point_measure_def by (simp add: measurable_count_space_eq2)
 12.2792 -
 12.2793 -lemma simple_function_point_measure[simp]:
 12.2794 -  "simple_function (point_measure A f) g \<longleftrightarrow> finite (g ` A)"
 12.2795 -  by (simp add: point_measure_def)
 12.2796 -
 12.2797 -lemma emeasure_point_measure:
 12.2798 -  assumes A: "finite {a\<in>X. 0 < f a}" "X \<subseteq> A"
 12.2799 -  shows "emeasure (point_measure A f) X = (\<Sum>a|a\<in>X \<and> 0 < f a. f a)"
 12.2800 -proof -
 12.2801 -  have "{a. (a \<in> X \<longrightarrow> a \<in> A \<and> 0 < f a) \<and> a \<in> X} = {a\<in>X. 0 < f a}"