rename HOL-Multivariate_Analysis to HOL-Analysis.
authorhoelzl
Mon Aug 08 14:13:14 2016 +0200 (2016-08-08)
changeset 636276ddb43c6b711
parent 63626 44ce6b524ff3
child 63631 2edc8da89edc
child 63633 2accfb71e33b
rename HOL-Multivariate_Analysis to HOL-Analysis.
NEWS
src/HOL/Analysis/Analysis.thy
src/HOL/Analysis/Binary_Product_Measure.thy
src/HOL/Analysis/Bochner_Integration.thy
src/HOL/Analysis/Borel_Space.thy
src/HOL/Analysis/Bounded_Continuous_Function.thy
src/HOL/Analysis/Bounded_Linear_Function.thy
src/HOL/Analysis/Brouwer_Fixpoint.thy
src/HOL/Analysis/Caratheodory.thy
src/HOL/Analysis/Cartesian_Euclidean_Space.thy
src/HOL/Analysis/Cauchy_Integral_Theorem.thy
src/HOL/Analysis/Complete_Measure.thy
src/HOL/Analysis/Complex_Analysis_Basics.thy
src/HOL/Analysis/Complex_Transcendental.thy
src/HOL/Analysis/Conformal_Mappings.thy
src/HOL/Analysis/Continuous_Extension.thy
src/HOL/Analysis/Convex_Euclidean_Space.thy
src/HOL/Analysis/Derivative.thy
src/HOL/Analysis/Determinants.thy
src/HOL/Analysis/Embed_Measure.thy
src/HOL/Analysis/Euclidean_Space.thy
src/HOL/Analysis/Extended_Real_Limits.thy
src/HOL/Analysis/Fashoda_Theorem.thy
src/HOL/Analysis/Finite_Cartesian_Product.thy
src/HOL/Analysis/Finite_Product_Measure.thy
src/HOL/Analysis/Gamma_Function.thy
src/HOL/Analysis/Generalised_Binomial_Theorem.thy
src/HOL/Analysis/Harmonic_Numbers.thy
src/HOL/Analysis/Henstock_Kurzweil_Integration.thy
src/HOL/Analysis/Homeomorphism.thy
src/HOL/Analysis/Integral_Test.thy
src/HOL/Analysis/Interval_Integral.thy
src/HOL/Analysis/L2_Norm.thy
src/HOL/Analysis/Lebesgue_Integral_Substitution.thy
src/HOL/Analysis/Lebesgue_Measure.thy
src/HOL/Analysis/Linear_Algebra.thy
src/HOL/Analysis/Measurable.thy
src/HOL/Analysis/Measure_Space.thy
src/HOL/Analysis/Nonnegative_Lebesgue_Integration.thy
src/HOL/Analysis/Norm_Arith.thy
src/HOL/Analysis/Operator_Norm.thy
src/HOL/Analysis/Ordered_Euclidean_Space.thy
src/HOL/Analysis/Path_Connected.thy
src/HOL/Analysis/Poly_Roots.thy
src/HOL/Analysis/Polytope.thy
src/HOL/Analysis/Radon_Nikodym.thy
src/HOL/Analysis/Regularity.thy
src/HOL/Analysis/Set_Integral.thy
src/HOL/Analysis/Sigma_Algebra.thy
src/HOL/Analysis/Summation_Tests.thy
src/HOL/Analysis/Topology_Euclidean_Space.thy
src/HOL/Analysis/Uniform_Limit.thy
src/HOL/Analysis/Weierstrass_Theorems.thy
src/HOL/Analysis/document/root.tex
src/HOL/Analysis/ex/Approximations.thy
src/HOL/Analysis/measurable.ML
src/HOL/Analysis/normarith.ML
src/HOL/Deriv.thy
src/HOL/Library/Extended_Real.thy
src/HOL/Multivariate_Analysis/Binary_Product_Measure.thy
src/HOL/Multivariate_Analysis/Bochner_Integration.thy
src/HOL/Multivariate_Analysis/Borel_Space.thy
src/HOL/Multivariate_Analysis/Bounded_Continuous_Function.thy
src/HOL/Multivariate_Analysis/Bounded_Linear_Function.thy
src/HOL/Multivariate_Analysis/Brouwer_Fixpoint.thy
src/HOL/Multivariate_Analysis/Caratheodory.thy
src/HOL/Multivariate_Analysis/Cartesian_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Cauchy_Integral_Theorem.thy
src/HOL/Multivariate_Analysis/Complete_Measure.thy
src/HOL/Multivariate_Analysis/Complex_Analysis_Basics.thy
src/HOL/Multivariate_Analysis/Complex_Transcendental.thy
src/HOL/Multivariate_Analysis/Conformal_Mappings.thy
src/HOL/Multivariate_Analysis/Continuous_Extension.thy
src/HOL/Multivariate_Analysis/Convex_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Derivative.thy
src/HOL/Multivariate_Analysis/Determinants.thy
src/HOL/Multivariate_Analysis/Embed_Measure.thy
src/HOL/Multivariate_Analysis/Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Extended_Real_Limits.thy
src/HOL/Multivariate_Analysis/Fashoda_Theorem.thy
src/HOL/Multivariate_Analysis/Finite_Cartesian_Product.thy
src/HOL/Multivariate_Analysis/Finite_Product_Measure.thy
src/HOL/Multivariate_Analysis/Gamma_Function.thy
src/HOL/Multivariate_Analysis/Generalised_Binomial_Theorem.thy
src/HOL/Multivariate_Analysis/Harmonic_Numbers.thy
src/HOL/Multivariate_Analysis/Henstock_Kurzweil_Integration.thy
src/HOL/Multivariate_Analysis/Homeomorphism.thy
src/HOL/Multivariate_Analysis/Integral_Test.thy
src/HOL/Multivariate_Analysis/Interval_Integral.thy
src/HOL/Multivariate_Analysis/L2_Norm.thy
src/HOL/Multivariate_Analysis/Lebesgue_Integral_Substitution.thy
src/HOL/Multivariate_Analysis/Lebesgue_Measure.thy
src/HOL/Multivariate_Analysis/Linear_Algebra.thy
src/HOL/Multivariate_Analysis/Measurable.thy
src/HOL/Multivariate_Analysis/Measure_Space.thy
src/HOL/Multivariate_Analysis/Multivariate_Analysis.thy
src/HOL/Multivariate_Analysis/Nonnegative_Lebesgue_Integration.thy
src/HOL/Multivariate_Analysis/Norm_Arith.thy
src/HOL/Multivariate_Analysis/Operator_Norm.thy
src/HOL/Multivariate_Analysis/Ordered_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Path_Connected.thy
src/HOL/Multivariate_Analysis/Poly_Roots.thy
src/HOL/Multivariate_Analysis/Polytope.thy
src/HOL/Multivariate_Analysis/Radon_Nikodym.thy
src/HOL/Multivariate_Analysis/Regularity.thy
src/HOL/Multivariate_Analysis/Set_Integral.thy
src/HOL/Multivariate_Analysis/Sigma_Algebra.thy
src/HOL/Multivariate_Analysis/Summation_Tests.thy
src/HOL/Multivariate_Analysis/Topology_Euclidean_Space.thy
src/HOL/Multivariate_Analysis/Uniform_Limit.thy
src/HOL/Multivariate_Analysis/Weierstrass_Theorems.thy
src/HOL/Multivariate_Analysis/document/root.tex
src/HOL/Multivariate_Analysis/ex/Approximations.thy
src/HOL/Multivariate_Analysis/measurable.ML
src/HOL/Multivariate_Analysis/normarith.ML
src/HOL/Probability/Discrete_Topology.thy
src/HOL/Probability/Probability_Measure.thy
src/HOL/ROOT
src/HOL/ex/Gauge_Integration.thy
     1.1 --- a/NEWS	Fri Aug 05 18:34:57 2016 +0200
     1.2 +++ b/NEWS	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -190,6 +190,13 @@
     1.4  
     1.5  *** HOL ***
     1.6  
     1.7 +* Renamed session HOL-Multivariate_Analysis to HOL-Analysis.
     1.8 +
     1.9 +* Moved measure theory from HOL-Probability to HOL-Analysis. When importing
    1.10 +HOL-Analysis some theorems need additional name spaces prefixes due to name
    1.11 +clashes.
    1.12 +INCOMPATIBILITY.
    1.13 +
    1.14  * Number_Theory: algebraic foundation for primes: Introduction of 
    1.15  predicates "is_prime", "irreducible", a "prime_factorization" 
    1.16  function, the "factorial_ring" typeclass with instance proofs for 
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/src/HOL/Analysis/Analysis.thy	Mon Aug 08 14:13:14 2016 +0200
     2.3 @@ -0,0 +1,20 @@
     2.4 +theory Analysis
     2.5 +imports
     2.6 +  Regularity
     2.7 +  Lebesgue_Integral_Substitution
     2.8 +  Embed_Measure
     2.9 +  Complete_Measure
    2.10 +  Radon_Nikodym
    2.11 +  Fashoda_Theorem
    2.12 +  Determinants
    2.13 +  Homeomorphism
    2.14 +  Bounded_Continuous_Function
    2.15 +  Weierstrass_Theorems
    2.16 +  Polytope
    2.17 +  Poly_Roots
    2.18 +  Conformal_Mappings
    2.19 +  Generalised_Binomial_Theorem
    2.20 +  Gamma_Function
    2.21 +begin
    2.22 +
    2.23 +end
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/src/HOL/Analysis/Binary_Product_Measure.thy	Mon Aug 08 14:13:14 2016 +0200
     3.3 @@ -0,0 +1,1110 @@
     3.4 +(*  Title:      HOL/Analysis/Binary_Product_Measure.thy
     3.5 +    Author:     Johannes Hölzl, TU München
     3.6 +*)
     3.7 +
     3.8 +section \<open>Binary product measures\<close>
     3.9 +
    3.10 +theory Binary_Product_Measure
    3.11 +imports Nonnegative_Lebesgue_Integration
    3.12 +begin
    3.13 +
    3.14 +lemma Pair_vimage_times[simp]: "Pair x -` (A \<times> B) = (if x \<in> A then B else {})"
    3.15 +  by auto
    3.16 +
    3.17 +lemma rev_Pair_vimage_times[simp]: "(\<lambda>x. (x, y)) -` (A \<times> B) = (if y \<in> B then A else {})"
    3.18 +  by auto
    3.19 +
    3.20 +subsection "Binary products"
    3.21 +
    3.22 +definition pair_measure (infixr "\<Otimes>\<^sub>M" 80) where
    3.23 +  "A \<Otimes>\<^sub>M B = measure_of (space A \<times> space B)
    3.24 +      {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}
    3.25 +      (\<lambda>X. \<integral>\<^sup>+x. (\<integral>\<^sup>+y. indicator X (x,y) \<partial>B) \<partial>A)"
    3.26 +
    3.27 +lemma pair_measure_closed: "{a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B} \<subseteq> Pow (space A \<times> space B)"
    3.28 +  using sets.space_closed[of A] sets.space_closed[of B] by auto
    3.29 +
    3.30 +lemma space_pair_measure:
    3.31 +  "space (A \<Otimes>\<^sub>M B) = space A \<times> space B"
    3.32 +  unfolding pair_measure_def using pair_measure_closed[of A B]
    3.33 +  by (rule space_measure_of)
    3.34 +
    3.35 +lemma SIGMA_Collect_eq: "(SIGMA x:space M. {y\<in>space N. P x y}) = {x\<in>space (M \<Otimes>\<^sub>M N). P (fst x) (snd x)}"
    3.36 +  by (auto simp: space_pair_measure)
    3.37 +
    3.38 +lemma sets_pair_measure:
    3.39 +  "sets (A \<Otimes>\<^sub>M B) = sigma_sets (space A \<times> space B) {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
    3.40 +  unfolding pair_measure_def using pair_measure_closed[of A B]
    3.41 +  by (rule sets_measure_of)
    3.42 +
    3.43 +lemma sets_pair_measure_cong[measurable_cong, cong]:
    3.44 +  "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^sub>M M2) = sets (M1' \<Otimes>\<^sub>M M2')"
    3.45 +  unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
    3.46 +
    3.47 +lemma pair_measureI[intro, simp, measurable]:
    3.48 +  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^sub>M B)"
    3.49 +  by (auto simp: sets_pair_measure)
    3.50 +
    3.51 +lemma sets_Pair: "{x} \<in> sets M1 \<Longrightarrow> {y} \<in> sets M2 \<Longrightarrow> {(x, y)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
    3.52 +  using pair_measureI[of "{x}" M1 "{y}" M2] by simp
    3.53 +
    3.54 +lemma measurable_pair_measureI:
    3.55 +  assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"
    3.56 +  assumes 2: "\<And>A B. A \<in> sets M1 \<Longrightarrow> B \<in> sets M2 \<Longrightarrow> f -` (A \<times> B) \<inter> space M \<in> sets M"
    3.57 +  shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    3.58 +  unfolding pair_measure_def using 1 2
    3.59 +  by (intro measurable_measure_of) (auto dest: sets.sets_into_space)
    3.60 +
    3.61 +lemma measurable_split_replace[measurable (raw)]:
    3.62 +  "(\<lambda>x. f x (fst (g x)) (snd (g x))) \<in> measurable M N \<Longrightarrow> (\<lambda>x. case_prod (f x) (g x)) \<in> measurable M N"
    3.63 +  unfolding split_beta' .
    3.64 +
    3.65 +lemma measurable_Pair[measurable (raw)]:
    3.66 +  assumes f: "f \<in> measurable M M1" and g: "g \<in> measurable M M2"
    3.67 +  shows "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    3.68 +proof (rule measurable_pair_measureI)
    3.69 +  show "(\<lambda>x. (f x, g x)) \<in> space M \<rightarrow> space M1 \<times> space M2"
    3.70 +    using f g by (auto simp: measurable_def)
    3.71 +  fix A B assume *: "A \<in> sets M1" "B \<in> sets M2"
    3.72 +  have "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    3.73 +    by auto
    3.74 +  also have "\<dots> \<in> sets M"
    3.75 +    by (rule sets.Int) (auto intro!: measurable_sets * f g)
    3.76 +  finally show "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M \<in> sets M" .
    3.77 +qed
    3.78 +
    3.79 +lemma measurable_fst[intro!, simp, measurable]: "fst \<in> measurable (M1 \<Otimes>\<^sub>M M2) M1"
    3.80 +  by (auto simp: fst_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    3.81 +    measurable_def)
    3.82 +
    3.83 +lemma measurable_snd[intro!, simp, measurable]: "snd \<in> measurable (M1 \<Otimes>\<^sub>M M2) M2"
    3.84 +  by (auto simp: snd_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    3.85 +    measurable_def)
    3.86 +
    3.87 +lemma measurable_Pair_compose_split[measurable_dest]:
    3.88 +  assumes f: "case_prod f \<in> measurable (M1 \<Otimes>\<^sub>M M2) N"
    3.89 +  assumes g: "g \<in> measurable M M1" and h: "h \<in> measurable M M2"
    3.90 +  shows "(\<lambda>x. f (g x) (h x)) \<in> measurable M N"
    3.91 +  using measurable_compose[OF measurable_Pair f, OF g h] by simp
    3.92 +
    3.93 +lemma measurable_Pair1_compose[measurable_dest]:
    3.94 +  assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    3.95 +  assumes [measurable]: "h \<in> measurable N M"
    3.96 +  shows "(\<lambda>x. f (h x)) \<in> measurable N M1"
    3.97 +  using measurable_compose[OF f measurable_fst] by simp
    3.98 +
    3.99 +lemma measurable_Pair2_compose[measurable_dest]:
   3.100 +  assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
   3.101 +  assumes [measurable]: "h \<in> measurable N M"
   3.102 +  shows "(\<lambda>x. g (h x)) \<in> measurable N M2"
   3.103 +  using measurable_compose[OF f measurable_snd] by simp
   3.104 +
   3.105 +lemma measurable_pair:
   3.106 +  assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
   3.107 +  shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
   3.108 +  using measurable_Pair[OF assms] by simp
   3.109 +
   3.110 +lemma
   3.111 +  assumes f[measurable]: "f \<in> measurable M (N \<Otimes>\<^sub>M P)"
   3.112 +  shows measurable_fst': "(\<lambda>x. fst (f x)) \<in> measurable M N"
   3.113 +    and measurable_snd': "(\<lambda>x. snd (f x)) \<in> measurable M P"
   3.114 +  by simp_all
   3.115 +
   3.116 +lemma
   3.117 +  assumes f[measurable]: "f \<in> measurable M N"
   3.118 +  shows measurable_fst'': "(\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^sub>M P) N"
   3.119 +    and measurable_snd'': "(\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^sub>M M) N"
   3.120 +  by simp_all
   3.121 +
   3.122 +lemma sets_pair_in_sets:
   3.123 +  assumes "\<And>a b. a \<in> sets A \<Longrightarrow> b \<in> sets B \<Longrightarrow> a \<times> b \<in> sets N"
   3.124 +  shows "sets (A \<Otimes>\<^sub>M B) \<subseteq> sets N"
   3.125 +  unfolding sets_pair_measure
   3.126 +  by (intro sets.sigma_sets_subset') (auto intro!: assms)
   3.127 +
   3.128 +lemma sets_pair_eq_sets_fst_snd:
   3.129 +  "sets (A \<Otimes>\<^sub>M B) = sets (Sup {vimage_algebra (space A \<times> space B) fst A, vimage_algebra (space A \<times> space B) snd B})"
   3.130 +    (is "?P = sets (Sup {?fst, ?snd})")
   3.131 +proof -
   3.132 +  { fix a b assume ab: "a \<in> sets A" "b \<in> sets B"
   3.133 +    then have "a \<times> b = (fst -` a \<inter> (space A \<times> space B)) \<inter> (snd -` b \<inter> (space A \<times> space B))"
   3.134 +      by (auto dest: sets.sets_into_space)
   3.135 +    also have "\<dots> \<in> sets (Sup {?fst, ?snd})"
   3.136 +      apply (rule sets.Int)
   3.137 +      apply (rule in_sets_Sup)
   3.138 +      apply auto []
   3.139 +      apply (rule insertI1)
   3.140 +      apply (auto intro: ab in_vimage_algebra) []
   3.141 +      apply (rule in_sets_Sup)
   3.142 +      apply auto []
   3.143 +      apply (rule insertI2)
   3.144 +      apply (auto intro: ab in_vimage_algebra)
   3.145 +      done
   3.146 +    finally have "a \<times> b \<in> sets (Sup {?fst, ?snd})" . }
   3.147 +  moreover have "sets ?fst \<subseteq> sets (A \<Otimes>\<^sub>M B)"
   3.148 +    by (rule sets_image_in_sets) (auto simp: space_pair_measure[symmetric])
   3.149 +  moreover have "sets ?snd \<subseteq> sets (A \<Otimes>\<^sub>M B)"
   3.150 +    by (rule sets_image_in_sets) (auto simp: space_pair_measure)
   3.151 +  ultimately show ?thesis
   3.152 +    apply (intro antisym[of "sets A" for A] sets_Sup_in_sets sets_pair_in_sets)
   3.153 +    apply simp
   3.154 +    apply simp
   3.155 +    apply simp
   3.156 +    apply (elim disjE)
   3.157 +    apply (simp add: space_pair_measure)
   3.158 +    apply (simp add: space_pair_measure)
   3.159 +    apply (auto simp add: space_pair_measure)
   3.160 +    done
   3.161 +qed
   3.162 +
   3.163 +lemma measurable_pair_iff:
   3.164 +  "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2) \<longleftrightarrow> (fst \<circ> f) \<in> measurable M M1 \<and> (snd \<circ> f) \<in> measurable M M2"
   3.165 +  by (auto intro: measurable_pair[of f M M1 M2])
   3.166 +
   3.167 +lemma measurable_split_conv:
   3.168 +  "(\<lambda>(x, y). f x y) \<in> measurable A B \<longleftrightarrow> (\<lambda>x. f (fst x) (snd x)) \<in> measurable A B"
   3.169 +  by (intro arg_cong2[where f="op \<in>"]) auto
   3.170 +
   3.171 +lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (M2 \<Otimes>\<^sub>M M1)"
   3.172 +  by (auto intro!: measurable_Pair simp: measurable_split_conv)
   3.173 +
   3.174 +lemma measurable_pair_swap:
   3.175 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" shows "(\<lambda>(x,y). f (y, x)) \<in> measurable (M2 \<Otimes>\<^sub>M M1) M"
   3.176 +  using measurable_comp[OF measurable_Pair f] by (auto simp: measurable_split_conv comp_def)
   3.177 +
   3.178 +lemma measurable_pair_swap_iff:
   3.179 +  "f \<in> measurable (M2 \<Otimes>\<^sub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) M"
   3.180 +  by (auto dest: measurable_pair_swap)
   3.181 +
   3.182 +lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^sub>M M2)"
   3.183 +  by simp
   3.184 +
   3.185 +lemma sets_Pair1[measurable (raw)]:
   3.186 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "Pair x -` A \<in> sets M2"
   3.187 +proof -
   3.188 +  have "Pair x -` A = (if x \<in> space M1 then Pair x -` A \<inter> space M2 else {})"
   3.189 +    using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   3.190 +  also have "\<dots> \<in> sets M2"
   3.191 +    using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: if_split_asm)
   3.192 +  finally show ?thesis .
   3.193 +qed
   3.194 +
   3.195 +lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^sub>M M2)"
   3.196 +  by (auto intro!: measurable_Pair)
   3.197 +
   3.198 +lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
   3.199 +proof -
   3.200 +  have "(\<lambda>x. (x, y)) -` A = (if y \<in> space M2 then (\<lambda>x. (x, y)) -` A \<inter> space M1 else {})"
   3.201 +    using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   3.202 +  also have "\<dots> \<in> sets M1"
   3.203 +    using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: if_split_asm)
   3.204 +  finally show ?thesis .
   3.205 +qed
   3.206 +
   3.207 +lemma measurable_Pair2:
   3.208 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and x: "x \<in> space M1"
   3.209 +  shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   3.210 +  using measurable_comp[OF measurable_Pair1' f, OF x]
   3.211 +  by (simp add: comp_def)
   3.212 +
   3.213 +lemma measurable_Pair1:
   3.214 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and y: "y \<in> space M2"
   3.215 +  shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
   3.216 +  using measurable_comp[OF measurable_Pair2' f, OF y]
   3.217 +  by (simp add: comp_def)
   3.218 +
   3.219 +lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
   3.220 +  unfolding Int_stable_def
   3.221 +  by safe (auto simp add: times_Int_times)
   3.222 +
   3.223 +lemma (in finite_measure) finite_measure_cut_measurable:
   3.224 +  assumes [measurable]: "Q \<in> sets (N \<Otimes>\<^sub>M M)"
   3.225 +  shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N"
   3.226 +    (is "?s Q \<in> _")
   3.227 +  using Int_stable_pair_measure_generator pair_measure_closed assms
   3.228 +  unfolding sets_pair_measure
   3.229 +proof (induct rule: sigma_sets_induct_disjoint)
   3.230 +  case (compl A)
   3.231 +  with sets.sets_into_space have "\<And>x. emeasure M (Pair x -` ((space N \<times> space M) - A)) =
   3.232 +      (if x \<in> space N then emeasure M (space M) - ?s A x else 0)"
   3.233 +    unfolding sets_pair_measure[symmetric]
   3.234 +    by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   3.235 +  with compl sets.top show ?case
   3.236 +    by (auto intro!: measurable_If simp: space_pair_measure)
   3.237 +next
   3.238 +  case (union F)
   3.239 +  then have "\<And>x. emeasure M (Pair x -` (\<Union>i. F i)) = (\<Sum>i. ?s (F i) x)"
   3.240 +    by (simp add: suminf_emeasure disjoint_family_on_vimageI subset_eq vimage_UN sets_pair_measure[symmetric])
   3.241 +  with union show ?case
   3.242 +    unfolding sets_pair_measure[symmetric] by simp
   3.243 +qed (auto simp add: if_distrib Int_def[symmetric] intro!: measurable_If)
   3.244 +
   3.245 +lemma (in sigma_finite_measure) measurable_emeasure_Pair:
   3.246 +  assumes Q: "Q \<in> sets (N \<Otimes>\<^sub>M M)" shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N" (is "?s Q \<in> _")
   3.247 +proof -
   3.248 +  from sigma_finite_disjoint guess F . note F = this
   3.249 +  then have F_sets: "\<And>i. F i \<in> sets M" by auto
   3.250 +  let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   3.251 +  { fix i
   3.252 +    have [simp]: "space N \<times> F i \<inter> space N \<times> space M = space N \<times> F i"
   3.253 +      using F sets.sets_into_space by auto
   3.254 +    let ?R = "density M (indicator (F i))"
   3.255 +    have "finite_measure ?R"
   3.256 +      using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   3.257 +    then have "(\<lambda>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))) \<in> borel_measurable N"
   3.258 +     by (rule finite_measure.finite_measure_cut_measurable) (auto intro: Q)
   3.259 +    moreover have "\<And>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))
   3.260 +        = emeasure M (F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q))"
   3.261 +      using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   3.262 +    moreover have "\<And>x. F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q) = ?C x i"
   3.263 +      using sets.sets_into_space[OF Q] by (auto simp: space_pair_measure)
   3.264 +    ultimately have "(\<lambda>x. emeasure M (?C x i)) \<in> borel_measurable N"
   3.265 +      by simp }
   3.266 +  moreover
   3.267 +  { fix x
   3.268 +    have "(\<Sum>i. emeasure M (?C x i)) = emeasure M (\<Union>i. ?C x i)"
   3.269 +    proof (intro suminf_emeasure)
   3.270 +      show "range (?C x) \<subseteq> sets M"
   3.271 +        using F \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close> by (auto intro!: sets_Pair1)
   3.272 +      have "disjoint_family F" using F by auto
   3.273 +      show "disjoint_family (?C x)"
   3.274 +        by (rule disjoint_family_on_bisimulation[OF \<open>disjoint_family F\<close>]) auto
   3.275 +    qed
   3.276 +    also have "(\<Union>i. ?C x i) = Pair x -` Q"
   3.277 +      using F sets.sets_into_space[OF \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close>]
   3.278 +      by (auto simp: space_pair_measure)
   3.279 +    finally have "emeasure M (Pair x -` Q) = (\<Sum>i. emeasure M (?C x i))"
   3.280 +      by simp }
   3.281 +  ultimately show ?thesis using \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close> F_sets
   3.282 +    by auto
   3.283 +qed
   3.284 +
   3.285 +lemma (in sigma_finite_measure) measurable_emeasure[measurable (raw)]:
   3.286 +  assumes space: "\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M"
   3.287 +  assumes A: "{x\<in>space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M)"
   3.288 +  shows "(\<lambda>x. emeasure M (A x)) \<in> borel_measurable N"
   3.289 +proof -
   3.290 +  from space have "\<And>x. x \<in> space N \<Longrightarrow> Pair x -` {x \<in> space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} = A x"
   3.291 +    by (auto simp: space_pair_measure)
   3.292 +  with measurable_emeasure_Pair[OF A] show ?thesis
   3.293 +    by (auto cong: measurable_cong)
   3.294 +qed
   3.295 +
   3.296 +lemma (in sigma_finite_measure) emeasure_pair_measure:
   3.297 +  assumes "X \<in> sets (N \<Otimes>\<^sub>M M)"
   3.298 +  shows "emeasure (N \<Otimes>\<^sub>M M) X = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator X (x, y) \<partial>M \<partial>N)" (is "_ = ?\<mu> X")
   3.299 +proof (rule emeasure_measure_of[OF pair_measure_def])
   3.300 +  show "positive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   3.301 +    by (auto simp: positive_def)
   3.302 +  have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   3.303 +    by (auto simp: indicator_def)
   3.304 +  show "countably_additive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   3.305 +  proof (rule countably_additiveI)
   3.306 +    fix F :: "nat \<Rightarrow> ('b \<times> 'a) set" assume F: "range F \<subseteq> sets (N \<Otimes>\<^sub>M M)" "disjoint_family F"
   3.307 +    from F have *: "\<And>i. F i \<in> sets (N \<Otimes>\<^sub>M M)" by auto
   3.308 +    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   3.309 +      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   3.310 +    moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M"
   3.311 +      using F by (auto simp: sets_Pair1)
   3.312 +    ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   3.313 +      by (auto simp add: nn_integral_suminf[symmetric] vimage_UN suminf_emeasure
   3.314 +               intro!: nn_integral_cong nn_integral_indicator[symmetric])
   3.315 +  qed
   3.316 +  show "{a \<times> b |a b. a \<in> sets N \<and> b \<in> sets M} \<subseteq> Pow (space N \<times> space M)"
   3.317 +    using sets.space_closed[of N] sets.space_closed[of M] by auto
   3.318 +qed fact
   3.319 +
   3.320 +lemma (in sigma_finite_measure) emeasure_pair_measure_alt:
   3.321 +  assumes X: "X \<in> sets (N \<Otimes>\<^sub>M M)"
   3.322 +  shows "emeasure (N  \<Otimes>\<^sub>M M) X = (\<integral>\<^sup>+x. emeasure M (Pair x -` X) \<partial>N)"
   3.323 +proof -
   3.324 +  have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   3.325 +    by (auto simp: indicator_def)
   3.326 +  show ?thesis
   3.327 +    using X by (auto intro!: nn_integral_cong simp: emeasure_pair_measure sets_Pair1)
   3.328 +qed
   3.329 +
   3.330 +lemma (in sigma_finite_measure) emeasure_pair_measure_Times:
   3.331 +  assumes A: "A \<in> sets N" and B: "B \<in> sets M"
   3.332 +  shows "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = emeasure N A * emeasure M B"
   3.333 +proof -
   3.334 +  have "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = (\<integral>\<^sup>+x. emeasure M B * indicator A x \<partial>N)"
   3.335 +    using A B by (auto intro!: nn_integral_cong simp: emeasure_pair_measure_alt)
   3.336 +  also have "\<dots> = emeasure M B * emeasure N A"
   3.337 +    using A by (simp add: nn_integral_cmult_indicator)
   3.338 +  finally show ?thesis
   3.339 +    by (simp add: ac_simps)
   3.340 +qed
   3.341 +
   3.342 +subsection \<open>Binary products of $\sigma$-finite emeasure spaces\<close>
   3.343 +
   3.344 +locale pair_sigma_finite = M1?: sigma_finite_measure M1 + M2?: sigma_finite_measure M2
   3.345 +  for M1 :: "'a measure" and M2 :: "'b measure"
   3.346 +
   3.347 +lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
   3.348 +  "Q \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow> (\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   3.349 +  using M2.measurable_emeasure_Pair .
   3.350 +
   3.351 +lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
   3.352 +  assumes Q: "Q \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   3.353 +proof -
   3.354 +  have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   3.355 +    using Q measurable_pair_swap' by (auto intro: measurable_sets)
   3.356 +  note M1.measurable_emeasure_Pair[OF this]
   3.357 +  moreover have "\<And>y. Pair y -` ((\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1)) = (\<lambda>x. (x, y)) -` Q"
   3.358 +    using Q[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   3.359 +  ultimately show ?thesis by simp
   3.360 +qed
   3.361 +
   3.362 +lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
   3.363 +  defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
   3.364 +  shows "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> E \<and> incseq F \<and> (\<Union>i. F i) = space M1 \<times> space M2 \<and>
   3.365 +    (\<forall>i. emeasure (M1 \<Otimes>\<^sub>M M2) (F i) \<noteq> \<infinity>)"
   3.366 +proof -
   3.367 +  from M1.sigma_finite_incseq guess F1 . note F1 = this
   3.368 +  from M2.sigma_finite_incseq guess F2 . note F2 = this
   3.369 +  from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   3.370 +  let ?F = "\<lambda>i. F1 i \<times> F2 i"
   3.371 +  show ?thesis
   3.372 +  proof (intro exI[of _ ?F] conjI allI)
   3.373 +    show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)
   3.374 +  next
   3.375 +    have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   3.376 +    proof (intro subsetI)
   3.377 +      fix x assume "x \<in> space M1 \<times> space M2"
   3.378 +      then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"
   3.379 +        by (auto simp: space)
   3.380 +      then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"
   3.381 +        using \<open>incseq F1\<close> \<open>incseq F2\<close> unfolding incseq_def
   3.382 +        by (force split: split_max)+
   3.383 +      then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"
   3.384 +        by (intro SigmaI) (auto simp add: max.commute)
   3.385 +      then show "x \<in> (\<Union>i. ?F i)" by auto
   3.386 +    qed
   3.387 +    then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   3.388 +      using space by (auto simp: space)
   3.389 +  next
   3.390 +    fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"
   3.391 +      using \<open>incseq F1\<close> \<open>incseq F2\<close> unfolding incseq_Suc_iff by auto
   3.392 +  next
   3.393 +    fix i
   3.394 +    from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   3.395 +    with F1 F2 show "emeasure (M1 \<Otimes>\<^sub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"
   3.396 +      by (auto simp add: emeasure_pair_measure_Times ennreal_mult_eq_top_iff)
   3.397 +  qed
   3.398 +qed
   3.399 +
   3.400 +sublocale pair_sigma_finite \<subseteq> P?: sigma_finite_measure "M1 \<Otimes>\<^sub>M M2"
   3.401 +proof
   3.402 +  from M1.sigma_finite_countable guess F1 ..
   3.403 +  moreover from M2.sigma_finite_countable guess F2 ..
   3.404 +  ultimately show
   3.405 +    "\<exists>A. countable A \<and> A \<subseteq> sets (M1 \<Otimes>\<^sub>M M2) \<and> \<Union>A = space (M1 \<Otimes>\<^sub>M M2) \<and> (\<forall>a\<in>A. emeasure (M1 \<Otimes>\<^sub>M M2) a \<noteq> \<infinity>)"
   3.406 +    by (intro exI[of _ "(\<lambda>(a, b). a \<times> b) ` (F1 \<times> F2)"] conjI)
   3.407 +       (auto simp: M2.emeasure_pair_measure_Times space_pair_measure set_eq_iff subset_eq ennreal_mult_eq_top_iff)
   3.408 +qed
   3.409 +
   3.410 +lemma sigma_finite_pair_measure:
   3.411 +  assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"
   3.412 +  shows "sigma_finite_measure (A \<Otimes>\<^sub>M B)"
   3.413 +proof -
   3.414 +  interpret A: sigma_finite_measure A by fact
   3.415 +  interpret B: sigma_finite_measure B by fact
   3.416 +  interpret AB: pair_sigma_finite A  B ..
   3.417 +  show ?thesis ..
   3.418 +qed
   3.419 +
   3.420 +lemma sets_pair_swap:
   3.421 +  assumes "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   3.422 +  shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   3.423 +  using measurable_pair_swap' assms by (rule measurable_sets)
   3.424 +
   3.425 +lemma (in pair_sigma_finite) distr_pair_swap:
   3.426 +  "M1 \<Otimes>\<^sub>M M2 = distr (M2 \<Otimes>\<^sub>M M1) (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")
   3.427 +proof -
   3.428 +  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   3.429 +  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   3.430 +  show ?thesis
   3.431 +  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   3.432 +    show "?E \<subseteq> Pow (space ?P)"
   3.433 +      using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   3.434 +    show "sets ?P = sigma_sets (space ?P) ?E"
   3.435 +      by (simp add: sets_pair_measure space_pair_measure)
   3.436 +    then show "sets ?D = sigma_sets (space ?P) ?E"
   3.437 +      by simp
   3.438 +  next
   3.439 +    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   3.440 +      using F by (auto simp: space_pair_measure)
   3.441 +  next
   3.442 +    fix X assume "X \<in> ?E"
   3.443 +    then obtain A B where X[simp]: "X = A \<times> B" and A: "A \<in> sets M1" and B: "B \<in> sets M2" by auto
   3.444 +    have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^sub>M M1) = B \<times> A"
   3.445 +      using sets.sets_into_space[OF A] sets.sets_into_space[OF B] by (auto simp: space_pair_measure)
   3.446 +    with A B show "emeasure (M1 \<Otimes>\<^sub>M M2) X = emeasure ?D X"
   3.447 +      by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_pair_measure_Times emeasure_distr
   3.448 +                    measurable_pair_swap' ac_simps)
   3.449 +  qed
   3.450 +qed
   3.451 +
   3.452 +lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:
   3.453 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   3.454 +  shows "emeasure (M1 \<Otimes>\<^sub>M M2) A = (\<integral>\<^sup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   3.455 +    (is "_ = ?\<nu> A")
   3.456 +proof -
   3.457 +  have [simp]: "\<And>y. (Pair y -` ((\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1))) = (\<lambda>x. (x, y)) -` A"
   3.458 +    using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
   3.459 +  show ?thesis using A
   3.460 +    by (subst distr_pair_swap)
   3.461 +       (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
   3.462 +                 M1.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   3.463 +qed
   3.464 +
   3.465 +lemma (in pair_sigma_finite) AE_pair:
   3.466 +  assumes "AE x in (M1 \<Otimes>\<^sub>M M2). Q x"
   3.467 +  shows "AE x in M1. (AE y in M2. Q (x, y))"
   3.468 +proof -
   3.469 +  obtain N where N: "N \<in> sets (M1 \<Otimes>\<^sub>M M2)" "emeasure (M1 \<Otimes>\<^sub>M M2) N = 0" "{x\<in>space (M1 \<Otimes>\<^sub>M M2). \<not> Q x} \<subseteq> N"
   3.470 +    using assms unfolding eventually_ae_filter by auto
   3.471 +  show ?thesis
   3.472 +  proof (rule AE_I)
   3.473 +    from N measurable_emeasure_Pair1[OF \<open>N \<in> sets (M1 \<Otimes>\<^sub>M M2)\<close>]
   3.474 +    show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
   3.475 +      by (auto simp: M2.emeasure_pair_measure_alt nn_integral_0_iff)
   3.476 +    show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   3.477 +      by (intro borel_measurable_eq measurable_emeasure_Pair1 N sets.sets_Collect_neg N) simp
   3.478 +    { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
   3.479 +      have "AE y in M2. Q (x, y)"
   3.480 +      proof (rule AE_I)
   3.481 +        show "emeasure M2 (Pair x -` N) = 0" by fact
   3.482 +        show "Pair x -` N \<in> sets M2" using N(1) by (rule sets_Pair1)
   3.483 +        show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
   3.484 +          using N \<open>x \<in> space M1\<close> unfolding space_pair_measure by auto
   3.485 +      qed }
   3.486 +    then show "{x \<in> space M1. \<not> (AE y in M2. Q (x, y))} \<subseteq> {x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0}"
   3.487 +      by auto
   3.488 +  qed
   3.489 +qed
   3.490 +
   3.491 +lemma (in pair_sigma_finite) AE_pair_measure:
   3.492 +  assumes "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   3.493 +  assumes ae: "AE x in M1. AE y in M2. P (x, y)"
   3.494 +  shows "AE x in M1 \<Otimes>\<^sub>M M2. P x"
   3.495 +proof (subst AE_iff_measurable[OF _ refl])
   3.496 +  show "{x\<in>space (M1 \<Otimes>\<^sub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   3.497 +    by (rule sets.sets_Collect) fact
   3.498 +  then have "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} =
   3.499 +      (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"
   3.500 +    by (simp add: M2.emeasure_pair_measure)
   3.501 +  also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. 0 \<partial>M2 \<partial>M1)"
   3.502 +    using ae
   3.503 +    apply (safe intro!: nn_integral_cong_AE)
   3.504 +    apply (intro AE_I2)
   3.505 +    apply (safe intro!: nn_integral_cong_AE)
   3.506 +    apply auto
   3.507 +    done
   3.508 +  finally show "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} = 0" by simp
   3.509 +qed
   3.510 +
   3.511 +lemma (in pair_sigma_finite) AE_pair_iff:
   3.512 +  "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow>
   3.513 +    (AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE x in (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x))"
   3.514 +  using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto
   3.515 +
   3.516 +lemma (in pair_sigma_finite) AE_commute:
   3.517 +  assumes P: "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   3.518 +  shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"
   3.519 +proof -
   3.520 +  interpret Q: pair_sigma_finite M2 M1 ..
   3.521 +  have [simp]: "\<And>x. (fst (case x of (x, y) \<Rightarrow> (y, x))) = snd x" "\<And>x. (snd (case x of (x, y) \<Rightarrow> (y, x))) = fst x"
   3.522 +    by auto
   3.523 +  have "{x \<in> space (M2 \<Otimes>\<^sub>M M1). P (snd x) (fst x)} =
   3.524 +    (\<lambda>(x, y). (y, x)) -` {x \<in> space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<inter> space (M2 \<Otimes>\<^sub>M M1)"
   3.525 +    by (auto simp: space_pair_measure)
   3.526 +  also have "\<dots> \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   3.527 +    by (intro sets_pair_swap P)
   3.528 +  finally show ?thesis
   3.529 +    apply (subst AE_pair_iff[OF P])
   3.530 +    apply (subst distr_pair_swap)
   3.531 +    apply (subst AE_distr_iff[OF measurable_pair_swap' P])
   3.532 +    apply (subst Q.AE_pair_iff)
   3.533 +    apply simp_all
   3.534 +    done
   3.535 +qed
   3.536 +
   3.537 +subsection "Fubinis theorem"
   3.538 +
   3.539 +lemma measurable_compose_Pair1:
   3.540 +  "x \<in> space M1 \<Longrightarrow> g \<in> measurable (M1 \<Otimes>\<^sub>M M2) L \<Longrightarrow> (\<lambda>y. g (x, y)) \<in> measurable M2 L"
   3.541 +  by simp
   3.542 +
   3.543 +lemma (in sigma_finite_measure) borel_measurable_nn_integral_fst:
   3.544 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
   3.545 +  shows "(\<lambda>x. \<integral>\<^sup>+ y. f (x, y) \<partial>M) \<in> borel_measurable M1"
   3.546 +using f proof induct
   3.547 +  case (cong u v)
   3.548 +  then have "\<And>w x. w \<in> space M1 \<Longrightarrow> x \<in> space M \<Longrightarrow> u (w, x) = v (w, x)"
   3.549 +    by (auto simp: space_pair_measure)
   3.550 +  show ?case
   3.551 +    apply (subst measurable_cong)
   3.552 +    apply (rule nn_integral_cong)
   3.553 +    apply fact+
   3.554 +    done
   3.555 +next
   3.556 +  case (set Q)
   3.557 +  have [simp]: "\<And>x y. indicator Q (x, y) = indicator (Pair x -` Q) y"
   3.558 +    by (auto simp: indicator_def)
   3.559 +  have "\<And>x. x \<in> space M1 \<Longrightarrow> emeasure M (Pair x -` Q) = \<integral>\<^sup>+ y. indicator Q (x, y) \<partial>M"
   3.560 +    by (simp add: sets_Pair1[OF set])
   3.561 +  from this measurable_emeasure_Pair[OF set] show ?case
   3.562 +    by (rule measurable_cong[THEN iffD1])
   3.563 +qed (simp_all add: nn_integral_add nn_integral_cmult measurable_compose_Pair1
   3.564 +                   nn_integral_monotone_convergence_SUP incseq_def le_fun_def
   3.565 +              cong: measurable_cong)
   3.566 +
   3.567 +lemma (in sigma_finite_measure) nn_integral_fst:
   3.568 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
   3.569 +  shows "(\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>M \<partial>M1) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M) f" (is "?I f = _")
   3.570 +using f proof induct
   3.571 +  case (cong u v)
   3.572 +  then have "?I u = ?I v"
   3.573 +    by (intro nn_integral_cong) (auto simp: space_pair_measure)
   3.574 +  with cong show ?case
   3.575 +    by (simp cong: nn_integral_cong)
   3.576 +qed (simp_all add: emeasure_pair_measure nn_integral_cmult nn_integral_add
   3.577 +                   nn_integral_monotone_convergence_SUP measurable_compose_Pair1
   3.578 +                   borel_measurable_nn_integral_fst nn_integral_mono incseq_def le_fun_def
   3.579 +              cong: nn_integral_cong)
   3.580 +
   3.581 +lemma (in sigma_finite_measure) borel_measurable_nn_integral[measurable (raw)]:
   3.582 +  "case_prod f \<in> borel_measurable (N \<Otimes>\<^sub>M M) \<Longrightarrow> (\<lambda>x. \<integral>\<^sup>+ y. f x y \<partial>M) \<in> borel_measurable N"
   3.583 +  using borel_measurable_nn_integral_fst[of "case_prod f" N] by simp
   3.584 +
   3.585 +lemma (in pair_sigma_finite) nn_integral_snd:
   3.586 +  assumes f[measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   3.587 +  shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   3.588 +proof -
   3.589 +  note measurable_pair_swap[OF f]
   3.590 +  from M1.nn_integral_fst[OF this]
   3.591 +  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))"
   3.592 +    by simp
   3.593 +  also have "(\<integral>\<^sup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   3.594 +    by (subst distr_pair_swap) (auto simp add: nn_integral_distr intro!: nn_integral_cong)
   3.595 +  finally show ?thesis .
   3.596 +qed
   3.597 +
   3.598 +lemma (in pair_sigma_finite) Fubini:
   3.599 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   3.600 +  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)"
   3.601 +  unfolding nn_integral_snd[OF assms] M2.nn_integral_fst[OF assms] ..
   3.602 +
   3.603 +lemma (in pair_sigma_finite) Fubini':
   3.604 +  assumes f: "case_prod f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   3.605 +  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)"
   3.606 +  using Fubini[OF f] by simp
   3.607 +
   3.608 +subsection \<open>Products on counting spaces, densities and distributions\<close>
   3.609 +
   3.610 +lemma sigma_prod:
   3.611 +  assumes X_cover: "\<exists>E\<subseteq>A. countable E \<and> X = \<Union>E" and A: "A \<subseteq> Pow X"
   3.612 +  assumes Y_cover: "\<exists>E\<subseteq>B. countable E \<and> Y = \<Union>E" and B: "B \<subseteq> Pow Y"
   3.613 +  shows "sigma X A \<Otimes>\<^sub>M sigma Y B = sigma (X \<times> Y) {a \<times> b | a b. a \<in> A \<and> b \<in> B}"
   3.614 +    (is "?P = ?S")
   3.615 +proof (rule measure_eqI)
   3.616 +  have [simp]: "snd \<in> X \<times> Y \<rightarrow> Y" "fst \<in> X \<times> Y \<rightarrow> X"
   3.617 +    by auto
   3.618 +  let ?XY = "{{fst -` a \<inter> X \<times> Y | a. a \<in> A}, {snd -` b \<inter> X \<times> Y | b. b \<in> B}}"
   3.619 +  have "sets ?P = sets (SUP xy:?XY. sigma (X \<times> Y) xy)"
   3.620 +    by (simp add: vimage_algebra_sigma sets_pair_eq_sets_fst_snd A B)
   3.621 +  also have "\<dots> = sets (sigma (X \<times> Y) (\<Union>?XY))"
   3.622 +    by (intro Sup_sigma arg_cong[where f=sets]) auto
   3.623 +  also have "\<dots> = sets ?S"
   3.624 +  proof (intro arg_cong[where f=sets] sigma_eqI sigma_sets_eqI)
   3.625 +    show "\<Union>?XY \<subseteq> Pow (X \<times> Y)" "{a \<times> b |a b. a \<in> A \<and> b \<in> B} \<subseteq> Pow (X \<times> Y)"
   3.626 +      using A B by auto
   3.627 +  next
   3.628 +    interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   3.629 +      using A B by (intro sigma_algebra_sigma_sets) auto
   3.630 +    fix Z assume "Z \<in> \<Union>?XY"
   3.631 +    then show "Z \<in> sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   3.632 +    proof safe
   3.633 +      fix a assume "a \<in> A"
   3.634 +      from Y_cover obtain E where E: "E \<subseteq> B" "countable E" and "Y = \<Union>E"
   3.635 +        by auto
   3.636 +      with \<open>a \<in> A\<close> A have eq: "fst -` a \<inter> X \<times> Y = (\<Union>e\<in>E. a \<times> e)"
   3.637 +        by auto
   3.638 +      show "fst -` a \<inter> X \<times> Y \<in> sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   3.639 +        using \<open>a \<in> A\<close> E unfolding eq by (auto intro!: XY.countable_UN')
   3.640 +    next
   3.641 +      fix b assume "b \<in> B"
   3.642 +      from X_cover obtain E where E: "E \<subseteq> A" "countable E" and "X = \<Union>E"
   3.643 +        by auto
   3.644 +      with \<open>b \<in> B\<close> B have eq: "snd -` b \<inter> X \<times> Y = (\<Union>e\<in>E. e \<times> b)"
   3.645 +        by auto
   3.646 +      show "snd -` b \<inter> X \<times> Y \<in> sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   3.647 +        using \<open>b \<in> B\<close> E unfolding eq by (auto intro!: XY.countable_UN')
   3.648 +    qed
   3.649 +  next
   3.650 +    fix Z assume "Z \<in> {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   3.651 +    then obtain a b where "Z = a \<times> b" and ab: "a \<in> A" "b \<in> B"
   3.652 +      by auto
   3.653 +    then have Z: "Z = (fst -` a \<inter> X \<times> Y) \<inter> (snd -` b \<inter> X \<times> Y)"
   3.654 +      using A B by auto
   3.655 +    interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) (\<Union>?XY)"
   3.656 +      by (intro sigma_algebra_sigma_sets) auto
   3.657 +    show "Z \<in> sigma_sets (X \<times> Y) (\<Union>?XY)"
   3.658 +      unfolding Z by (rule XY.Int) (blast intro: ab)+
   3.659 +  qed
   3.660 +  finally show "sets ?P = sets ?S" .
   3.661 +next
   3.662 +  interpret finite_measure "sigma X A" for X A
   3.663 +    proof qed (simp add: emeasure_sigma)
   3.664 +  fix A assume "A \<in> sets ?P" then show "emeasure ?P A = emeasure ?S A"
   3.665 +    by (simp add: emeasure_pair_measure_alt emeasure_sigma)
   3.666 +qed
   3.667 +
   3.668 +lemma sigma_sets_pair_measure_generator_finite:
   3.669 +  assumes "finite A" and "finite B"
   3.670 +  shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"
   3.671 +  (is "sigma_sets ?prod ?sets = _")
   3.672 +proof safe
   3.673 +  have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
   3.674 +  fix x assume subset: "x \<subseteq> A \<times> B"
   3.675 +  hence "finite x" using fin by (rule finite_subset)
   3.676 +  from this subset show "x \<in> sigma_sets ?prod ?sets"
   3.677 +  proof (induct x)
   3.678 +    case empty show ?case by (rule sigma_sets.Empty)
   3.679 +  next
   3.680 +    case (insert a x)
   3.681 +    hence "{a} \<in> sigma_sets ?prod ?sets" by auto
   3.682 +    moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
   3.683 +    ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
   3.684 +  qed
   3.685 +next
   3.686 +  fix x a b
   3.687 +  assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"
   3.688 +  from sigma_sets_into_sp[OF _ this(1)] this(2)
   3.689 +  show "a \<in> A" and "b \<in> B" by auto
   3.690 +qed
   3.691 +
   3.692 +lemma borel_prod:
   3.693 +  "(borel \<Otimes>\<^sub>M borel) = (borel :: ('a::second_countable_topology \<times> 'b::second_countable_topology) measure)"
   3.694 +  (is "?P = ?B")
   3.695 +proof -
   3.696 +  have "?B = sigma UNIV {A \<times> B | A B. open A \<and> open B}"
   3.697 +    by (rule second_countable_borel_measurable[OF open_prod_generated])
   3.698 +  also have "\<dots> = ?P"
   3.699 +    unfolding borel_def
   3.700 +    by (subst sigma_prod) (auto intro!: exI[of _ "{UNIV}"])
   3.701 +  finally show ?thesis ..
   3.702 +qed
   3.703 +
   3.704 +lemma pair_measure_count_space:
   3.705 +  assumes A: "finite A" and B: "finite B"
   3.706 +  shows "count_space A \<Otimes>\<^sub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")
   3.707 +proof (rule measure_eqI)
   3.708 +  interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact
   3.709 +  interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact
   3.710 +  interpret P: pair_sigma_finite "count_space A" "count_space B" ..
   3.711 +  show eq: "sets ?P = sets ?C"
   3.712 +    by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)
   3.713 +  fix X assume X: "X \<in> sets ?P"
   3.714 +  with eq have X_subset: "X \<subseteq> A \<times> B" by simp
   3.715 +  with A B have fin_Pair: "\<And>x. finite (Pair x -` X)"
   3.716 +    by (intro finite_subset[OF _ B]) auto
   3.717 +  have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
   3.718 +  have pos_card: "(0::ennreal) < of_nat (card (Pair x -` X)) \<longleftrightarrow> Pair x -` X \<noteq> {}" for x
   3.719 +    by (auto simp: card_eq_0_iff fin_Pair) blast
   3.720 +
   3.721 +  show "emeasure ?P X = emeasure ?C X"
   3.722 +    using X_subset A fin_Pair fin_X
   3.723 +    apply (subst B.emeasure_pair_measure_alt[OF X])
   3.724 +    apply (subst emeasure_count_space)
   3.725 +    apply (auto simp add: emeasure_count_space nn_integral_count_space
   3.726 +                          pos_card of_nat_setsum[symmetric] card_SigmaI[symmetric]
   3.727 +                simp del: of_nat_setsum card_SigmaI
   3.728 +                intro!: arg_cong[where f=card])
   3.729 +    done
   3.730 +qed
   3.731 +
   3.732 +
   3.733 +lemma emeasure_prod_count_space:
   3.734 +  assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M M)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")
   3.735 +  shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator A (x, y) \<partial>?B \<partial>?A)"
   3.736 +  by (rule emeasure_measure_of[OF pair_measure_def])
   3.737 +     (auto simp: countably_additive_def positive_def suminf_indicator A
   3.738 +                 nn_integral_suminf[symmetric] dest: sets.sets_into_space)
   3.739 +
   3.740 +lemma emeasure_prod_count_space_single[simp]: "emeasure (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) {x} = 1"
   3.741 +proof -
   3.742 +  have [simp]: "\<And>a b x y. indicator {(a, b)} (x, y) = (indicator {a} x * indicator {b} y::ennreal)"
   3.743 +    by (auto split: split_indicator)
   3.744 +  show ?thesis
   3.745 +    by (cases x) (auto simp: emeasure_prod_count_space nn_integral_cmult sets_Pair)
   3.746 +qed
   3.747 +
   3.748 +lemma emeasure_count_space_prod_eq:
   3.749 +  fixes A :: "('a \<times> 'b) set"
   3.750 +  assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M count_space UNIV)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")
   3.751 +  shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"
   3.752 +proof -
   3.753 +  { fix A :: "('a \<times> 'b) set" assume "countable A"
   3.754 +    then have "emeasure (?A \<Otimes>\<^sub>M ?B) (\<Union>a\<in>A. {a}) = (\<integral>\<^sup>+a. emeasure (?A \<Otimes>\<^sub>M ?B) {a} \<partial>count_space A)"
   3.755 +      by (intro emeasure_UN_countable) (auto simp: sets_Pair disjoint_family_on_def)
   3.756 +    also have "\<dots> = (\<integral>\<^sup>+a. indicator A a \<partial>count_space UNIV)"
   3.757 +      by (subst nn_integral_count_space_indicator) auto
   3.758 +    finally have "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"
   3.759 +      by simp }
   3.760 +  note * = this
   3.761 +
   3.762 +  show ?thesis
   3.763 +  proof cases
   3.764 +    assume "finite A" then show ?thesis
   3.765 +      by (intro * countable_finite)
   3.766 +  next
   3.767 +    assume "infinite A"
   3.768 +    then obtain C where "countable C" and "infinite C" and "C \<subseteq> A"
   3.769 +      by (auto dest: infinite_countable_subset')
   3.770 +    with A have "emeasure (?A \<Otimes>\<^sub>M ?B) C \<le> emeasure (?A \<Otimes>\<^sub>M ?B) A"
   3.771 +      by (intro emeasure_mono) auto
   3.772 +    also have "emeasure (?A \<Otimes>\<^sub>M ?B) C = emeasure (count_space UNIV) C"
   3.773 +      using \<open>countable C\<close> by (rule *)
   3.774 +    finally show ?thesis
   3.775 +      using \<open>infinite C\<close> \<open>infinite A\<close> by (simp add: top_unique)
   3.776 +  qed
   3.777 +qed
   3.778 +
   3.779 +lemma nn_integral_count_space_prod_eq:
   3.780 +  "nn_integral (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) f = nn_integral (count_space UNIV) f"
   3.781 +    (is "nn_integral ?P f = _")
   3.782 +proof cases
   3.783 +  assume cntbl: "countable {x. f x \<noteq> 0}"
   3.784 +  have [simp]: "\<And>x. card ({x} \<inter> {x. f x \<noteq> 0}) = (indicator {x. f x \<noteq> 0} x::ennreal)"
   3.785 +    by (auto split: split_indicator)
   3.786 +  have [measurable]: "\<And>y. (\<lambda>x. indicator {y} x) \<in> borel_measurable ?P"
   3.787 +    by (rule measurable_discrete_difference[of "\<lambda>x. 0" _ borel "{y}" "\<lambda>x. indicator {y} x" for y])
   3.788 +       (auto intro: sets_Pair)
   3.789 +
   3.790 +  have "(\<integral>\<^sup>+x. f x \<partial>?P) = (\<integral>\<^sup>+x. \<integral>\<^sup>+x'. f x * indicator {x} x' \<partial>count_space {x. f x \<noteq> 0} \<partial>?P)"
   3.791 +    by (auto simp add: nn_integral_cmult nn_integral_indicator' intro!: nn_integral_cong split: split_indicator)
   3.792 +  also have "\<dots> = (\<integral>\<^sup>+x. \<integral>\<^sup>+x'. f x' * indicator {x'} x \<partial>count_space {x. f x \<noteq> 0} \<partial>?P)"
   3.793 +    by (auto intro!: nn_integral_cong split: split_indicator)
   3.794 +  also have "\<dots> = (\<integral>\<^sup>+x'. \<integral>\<^sup>+x. f x' * indicator {x'} x \<partial>?P \<partial>count_space {x. f x \<noteq> 0})"
   3.795 +    by (intro nn_integral_count_space_nn_integral cntbl) auto
   3.796 +  also have "\<dots> = (\<integral>\<^sup>+x'. f x' \<partial>count_space {x. f x \<noteq> 0})"
   3.797 +    by (intro nn_integral_cong) (auto simp: nn_integral_cmult sets_Pair)
   3.798 +  finally show ?thesis
   3.799 +    by (auto simp add: nn_integral_count_space_indicator intro!: nn_integral_cong split: split_indicator)
   3.800 +next
   3.801 +  { fix x assume "f x \<noteq> 0"
   3.802 +    then have "(\<exists>r\<ge>0. 0 < r \<and> f x = ennreal r) \<or> f x = \<infinity>"
   3.803 +      by (cases "f x" rule: ennreal_cases) (auto simp: less_le)
   3.804 +    then have "\<exists>n. ennreal (1 / real (Suc n)) \<le> f x"
   3.805 +      by (auto elim!: nat_approx_posE intro!: less_imp_le) }
   3.806 +  note * = this
   3.807 +
   3.808 +  assume cntbl: "uncountable {x. f x \<noteq> 0}"
   3.809 +  also have "{x. f x \<noteq> 0} = (\<Union>n. {x. 1/Suc n \<le> f x})"
   3.810 +    using * by auto
   3.811 +  finally obtain n where "infinite {x. 1/Suc n \<le> f x}"
   3.812 +    by (meson countableI_type countable_UN uncountable_infinite)
   3.813 +  then obtain C where C: "C \<subseteq> {x. 1/Suc n \<le> f x}" and "countable C" "infinite C"
   3.814 +    by (metis infinite_countable_subset')
   3.815 +
   3.816 +  have [measurable]: "C \<in> sets ?P"
   3.817 +    using sets.countable[OF _ \<open>countable C\<close>, of ?P] by (auto simp: sets_Pair)
   3.818 +
   3.819 +  have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>?P) \<le> nn_integral ?P f"
   3.820 +    using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])
   3.821 +  moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>?P) = \<infinity>"
   3.822 +    using \<open>infinite C\<close> by (simp add: nn_integral_cmult emeasure_count_space_prod_eq ennreal_mult_top)
   3.823 +  moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>count_space UNIV) \<le> nn_integral (count_space UNIV) f"
   3.824 +    using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])
   3.825 +  moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>count_space UNIV) = \<infinity>"
   3.826 +    using \<open>infinite C\<close> by (simp add: nn_integral_cmult ennreal_mult_top)
   3.827 +  ultimately show ?thesis
   3.828 +    by (simp add: top_unique)
   3.829 +qed
   3.830 +
   3.831 +lemma pair_measure_density:
   3.832 +  assumes f: "f \<in> borel_measurable M1"
   3.833 +  assumes g: "g \<in> borel_measurable M2"
   3.834 +  assumes "sigma_finite_measure M2" "sigma_finite_measure (density M2 g)"
   3.835 +  shows "density M1 f \<Otimes>\<^sub>M density M2 g = density (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x,y). f x * g y)" (is "?L = ?R")
   3.836 +proof (rule measure_eqI)
   3.837 +  interpret M2: sigma_finite_measure M2 by fact
   3.838 +  interpret D2: sigma_finite_measure "density M2 g" by fact
   3.839 +
   3.840 +  fix A assume A: "A \<in> sets ?L"
   3.841 +  with f g have "(\<integral>\<^sup>+ x. f x * \<integral>\<^sup>+ y. g y * indicator A (x, y) \<partial>M2 \<partial>M1) =
   3.842 +    (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f x * g y * indicator A (x, y) \<partial>M2 \<partial>M1)"
   3.843 +    by (intro nn_integral_cong_AE)
   3.844 +       (auto simp add: nn_integral_cmult[symmetric] ac_simps)
   3.845 +  with A f g show "emeasure ?L A = emeasure ?R A"
   3.846 +    by (simp add: D2.emeasure_pair_measure emeasure_density nn_integral_density
   3.847 +                  M2.nn_integral_fst[symmetric]
   3.848 +             cong: nn_integral_cong)
   3.849 +qed simp
   3.850 +
   3.851 +lemma sigma_finite_measure_distr:
   3.852 +  assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"
   3.853 +  shows "sigma_finite_measure M"
   3.854 +proof -
   3.855 +  interpret sigma_finite_measure "distr M N f" by fact
   3.856 +  from sigma_finite_countable guess A .. note A = this
   3.857 +  show ?thesis
   3.858 +  proof
   3.859 +    show "\<exists>A. countable A \<and> A \<subseteq> sets M \<and> \<Union>A = space M \<and> (\<forall>a\<in>A. emeasure M a \<noteq> \<infinity>)"
   3.860 +      using A f
   3.861 +      by (intro exI[of _ "(\<lambda>a. f -` a \<inter> space M) ` A"])
   3.862 +         (auto simp: emeasure_distr set_eq_iff subset_eq intro: measurable_space)
   3.863 +  qed
   3.864 +qed
   3.865 +
   3.866 +lemma pair_measure_distr:
   3.867 +  assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"
   3.868 +  assumes "sigma_finite_measure (distr N T g)"
   3.869 +  shows "distr M S f \<Otimes>\<^sub>M distr N T g = distr (M \<Otimes>\<^sub>M N) (S \<Otimes>\<^sub>M T) (\<lambda>(x, y). (f x, g y))" (is "?P = ?D")
   3.870 +proof (rule measure_eqI)
   3.871 +  interpret T: sigma_finite_measure "distr N T g" by fact
   3.872 +  interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+
   3.873 +
   3.874 +  fix A assume A: "A \<in> sets ?P"
   3.875 +  with f g show "emeasure ?P A = emeasure ?D A"
   3.876 +    by (auto simp add: N.emeasure_pair_measure_alt space_pair_measure emeasure_distr
   3.877 +                       T.emeasure_pair_measure_alt nn_integral_distr
   3.878 +             intro!: nn_integral_cong arg_cong[where f="emeasure N"])
   3.879 +qed simp
   3.880 +
   3.881 +lemma pair_measure_eqI:
   3.882 +  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
   3.883 +  assumes sets: "sets (M1 \<Otimes>\<^sub>M M2) = sets M"
   3.884 +  assumes emeasure: "\<And>A B. A \<in> sets M1 \<Longrightarrow> B \<in> sets M2 \<Longrightarrow> emeasure M1 A * emeasure M2 B = emeasure M (A \<times> B)"
   3.885 +  shows "M1 \<Otimes>\<^sub>M M2 = M"
   3.886 +proof -
   3.887 +  interpret M1: sigma_finite_measure M1 by fact
   3.888 +  interpret M2: sigma_finite_measure M2 by fact
   3.889 +  interpret pair_sigma_finite M1 M2 ..
   3.890 +  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   3.891 +  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   3.892 +  let ?P = "M1 \<Otimes>\<^sub>M M2"
   3.893 +  show ?thesis
   3.894 +  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   3.895 +    show "?E \<subseteq> Pow (space ?P)"
   3.896 +      using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   3.897 +    show "sets ?P = sigma_sets (space ?P) ?E"
   3.898 +      by (simp add: sets_pair_measure space_pair_measure)
   3.899 +    then show "sets M = sigma_sets (space ?P) ?E"
   3.900 +      using sets[symmetric] by simp
   3.901 +  next
   3.902 +    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   3.903 +      using F by (auto simp: space_pair_measure)
   3.904 +  next
   3.905 +    fix X assume "X \<in> ?E"
   3.906 +    then obtain A B where X[simp]: "X = A \<times> B" and A: "A \<in> sets M1" and B: "B \<in> sets M2" by auto
   3.907 +    then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
   3.908 +       by (simp add: M2.emeasure_pair_measure_Times)
   3.909 +    also have "\<dots> = emeasure M (A \<times> B)"
   3.910 +      using A B emeasure by auto
   3.911 +    finally show "emeasure ?P X = emeasure M X"
   3.912 +      by simp
   3.913 +  qed
   3.914 +qed
   3.915 +
   3.916 +lemma sets_pair_countable:
   3.917 +  assumes "countable S1" "countable S2"
   3.918 +  assumes M: "sets M = Pow S1" and N: "sets N = Pow S2"
   3.919 +  shows "sets (M \<Otimes>\<^sub>M N) = Pow (S1 \<times> S2)"
   3.920 +proof auto
   3.921 +  fix x a b assume x: "x \<in> sets (M \<Otimes>\<^sub>M N)" "(a, b) \<in> x"
   3.922 +  from sets.sets_into_space[OF x(1)] x(2)
   3.923 +    sets_eq_imp_space_eq[of N "count_space S2"] sets_eq_imp_space_eq[of M "count_space S1"] M N
   3.924 +  show "a \<in> S1" "b \<in> S2"
   3.925 +    by (auto simp: space_pair_measure)
   3.926 +next
   3.927 +  fix X assume X: "X \<subseteq> S1 \<times> S2"
   3.928 +  then have "countable X"
   3.929 +    by (metis countable_subset \<open>countable S1\<close> \<open>countable S2\<close> countable_SIGMA)
   3.930 +  have "X = (\<Union>(a, b)\<in>X. {a} \<times> {b})" by auto
   3.931 +  also have "\<dots> \<in> sets (M \<Otimes>\<^sub>M N)"
   3.932 +    using X
   3.933 +    by (safe intro!: sets.countable_UN' \<open>countable X\<close> subsetI pair_measureI) (auto simp: M N)
   3.934 +  finally show "X \<in> sets (M \<Otimes>\<^sub>M N)" .
   3.935 +qed
   3.936 +
   3.937 +lemma pair_measure_countable:
   3.938 +  assumes "countable S1" "countable S2"
   3.939 +  shows "count_space S1 \<Otimes>\<^sub>M count_space S2 = count_space (S1 \<times> S2)"
   3.940 +proof (rule pair_measure_eqI)
   3.941 +  show "sigma_finite_measure (count_space S1)" "sigma_finite_measure (count_space S2)"
   3.942 +    using assms by (auto intro!: sigma_finite_measure_count_space_countable)
   3.943 +  show "sets (count_space S1 \<Otimes>\<^sub>M count_space S2) = sets (count_space (S1 \<times> S2))"
   3.944 +    by (subst sets_pair_countable[OF assms]) auto
   3.945 +next
   3.946 +  fix A B assume "A \<in> sets (count_space S1)" "B \<in> sets (count_space S2)"
   3.947 +  then show "emeasure (count_space S1) A * emeasure (count_space S2) B =
   3.948 +    emeasure (count_space (S1 \<times> S2)) (A \<times> B)"
   3.949 +    by (subst (1 2 3) emeasure_count_space) (auto simp: finite_cartesian_product_iff ennreal_mult_top ennreal_top_mult)
   3.950 +qed
   3.951 +
   3.952 +lemma nn_integral_fst_count_space:
   3.953 +  "(\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"
   3.954 +  (is "?lhs = ?rhs")
   3.955 +proof(cases)
   3.956 +  assume *: "countable {xy. f xy \<noteq> 0}"
   3.957 +  let ?A = "fst ` {xy. f xy \<noteq> 0}"
   3.958 +  let ?B = "snd ` {xy. f xy \<noteq> 0}"
   3.959 +  from * have [simp]: "countable ?A" "countable ?B" by(rule countable_image)+
   3.960 +  have "?lhs = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space ?A)"
   3.961 +    by(rule nn_integral_count_space_eq)
   3.962 +      (auto simp add: nn_integral_0_iff_AE AE_count_space not_le intro: rev_image_eqI)
   3.963 +  also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space ?B \<partial>count_space ?A)"
   3.964 +    by(intro nn_integral_count_space_eq nn_integral_cong)(auto intro: rev_image_eqI)
   3.965 +  also have "\<dots> = (\<integral>\<^sup>+ xy. f xy \<partial>count_space (?A \<times> ?B))"
   3.966 +    by(subst sigma_finite_measure.nn_integral_fst)
   3.967 +      (simp_all add: sigma_finite_measure_count_space_countable pair_measure_countable)
   3.968 +  also have "\<dots> = ?rhs"
   3.969 +    by(rule nn_integral_count_space_eq)(auto intro: rev_image_eqI)
   3.970 +  finally show ?thesis .
   3.971 +next
   3.972 +  { fix xy assume "f xy \<noteq> 0"
   3.973 +    then have "(\<exists>r\<ge>0. 0 < r \<and> f xy = ennreal r) \<or> f xy = \<infinity>"
   3.974 +      by (cases "f xy" rule: ennreal_cases) (auto simp: less_le)
   3.975 +    then have "\<exists>n. ennreal (1 / real (Suc n)) \<le> f xy"
   3.976 +      by (auto elim!: nat_approx_posE intro!: less_imp_le) }
   3.977 +  note * = this
   3.978 +
   3.979 +  assume cntbl: "uncountable {xy. f xy \<noteq> 0}"
   3.980 +  also have "{xy. f xy \<noteq> 0} = (\<Union>n. {xy. 1/Suc n \<le> f xy})"
   3.981 +    using * by auto
   3.982 +  finally obtain n where "infinite {xy. 1/Suc n \<le> f xy}"
   3.983 +    by (meson countableI_type countable_UN uncountable_infinite)
   3.984 +  then obtain C where C: "C \<subseteq> {xy. 1/Suc n \<le> f xy}" and "countable C" "infinite C"
   3.985 +    by (metis infinite_countable_subset')
   3.986 +
   3.987 +  have "\<infinity> = (\<integral>\<^sup>+ xy. ennreal (1 / Suc n) * indicator C xy \<partial>count_space UNIV)"
   3.988 +    using \<open>infinite C\<close> by(simp add: nn_integral_cmult ennreal_mult_top)
   3.989 +  also have "\<dots> \<le> ?rhs" using C
   3.990 +    by(intro nn_integral_mono)(auto split: split_indicator)
   3.991 +  finally have "?rhs = \<infinity>" by (simp add: top_unique)
   3.992 +  moreover have "?lhs = \<infinity>"
   3.993 +  proof(cases "finite (fst ` C)")
   3.994 +    case True
   3.995 +    then obtain x C' where x: "x \<in> fst ` C"
   3.996 +      and C': "C' = fst -` {x} \<inter> C"
   3.997 +      and "infinite C'"
   3.998 +      using \<open>infinite C\<close> by(auto elim!: inf_img_fin_domE')
   3.999 +    from x C C' have **: "C' \<subseteq> {xy. 1 / Suc n \<le> f xy}" by auto
  3.1000 +
  3.1001 +    from C' \<open>infinite C'\<close> have "infinite (snd ` C')"
  3.1002 +      by(auto dest!: finite_imageD simp add: inj_on_def)
  3.1003 +    then have "\<infinity> = (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator (snd ` C') y \<partial>count_space UNIV)"
  3.1004 +      by(simp add: nn_integral_cmult ennreal_mult_top)
  3.1005 +    also have "\<dots> = (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV)"
  3.1006 +      by(rule nn_integral_cong)(force split: split_indicator intro: rev_image_eqI simp add: C')
  3.1007 +    also have "\<dots> = (\<integral>\<^sup>+ x'. (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV) * indicator {x} x' \<partial>count_space UNIV)"
  3.1008 +      by(simp add: one_ereal_def[symmetric])
  3.1009 +    also have "\<dots> \<le> (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV \<partial>count_space UNIV)"
  3.1010 +      by(rule nn_integral_mono)(simp split: split_indicator)
  3.1011 +    also have "\<dots> \<le> ?lhs" using **
  3.1012 +      by(intro nn_integral_mono)(auto split: split_indicator)
  3.1013 +    finally show ?thesis by (simp add: top_unique)
  3.1014 +  next
  3.1015 +    case False
  3.1016 +    define C' where "C' = fst ` C"
  3.1017 +    have "\<infinity> = \<integral>\<^sup>+ x. ennreal (1 / Suc n) * indicator C' x \<partial>count_space UNIV"
  3.1018 +      using C'_def False by(simp add: nn_integral_cmult ennreal_mult_top)
  3.1019 +    also have "\<dots> = \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C' x * indicator {SOME y. (x, y) \<in> C} y \<partial>count_space UNIV \<partial>count_space UNIV"
  3.1020 +      by(auto simp add: one_ereal_def[symmetric] max_def intro: nn_integral_cong)
  3.1021 +    also have "\<dots> \<le> \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C (x, y) \<partial>count_space UNIV \<partial>count_space UNIV"
  3.1022 +      by(intro nn_integral_mono)(auto simp add: C'_def split: split_indicator intro: someI)
  3.1023 +    also have "\<dots> \<le> ?lhs" using C
  3.1024 +      by(intro nn_integral_mono)(auto split: split_indicator)
  3.1025 +    finally show ?thesis by (simp add: top_unique)
  3.1026 +  qed
  3.1027 +  ultimately show ?thesis by simp
  3.1028 +qed
  3.1029 +
  3.1030 +lemma nn_integral_snd_count_space:
  3.1031 +  "(\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"
  3.1032 +  (is "?lhs = ?rhs")
  3.1033 +proof -
  3.1034 +  have "?lhs = (\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. (\<lambda>(y, x). f (x, y)) (y, x) \<partial>count_space UNIV \<partial>count_space UNIV)"
  3.1035 +    by(simp)
  3.1036 +  also have "\<dots> = \<integral>\<^sup>+ yx. (\<lambda>(y, x). f (x, y)) yx \<partial>count_space UNIV"
  3.1037 +    by(rule nn_integral_fst_count_space)
  3.1038 +  also have "\<dots> = \<integral>\<^sup>+ xy. f xy \<partial>count_space ((\<lambda>(x, y). (y, x)) ` UNIV)"
  3.1039 +    by(subst nn_integral_bij_count_space[OF inj_on_imp_bij_betw, symmetric])
  3.1040 +      (simp_all add: inj_on_def split_def)
  3.1041 +  also have "\<dots> = ?rhs" by(rule nn_integral_count_space_eq) auto
  3.1042 +  finally show ?thesis .
  3.1043 +qed
  3.1044 +
  3.1045 +lemma measurable_pair_measure_countable1:
  3.1046 +  assumes "countable A"
  3.1047 +  and [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N K"
  3.1048 +  shows "f \<in> measurable (count_space A \<Otimes>\<^sub>M N) K"
  3.1049 +using _ _ assms(1)
  3.1050 +by(rule measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A, simplified])simp_all
  3.1051 +
  3.1052 +subsection \<open>Product of Borel spaces\<close>
  3.1053 +
  3.1054 +lemma borel_Times:
  3.1055 +  fixes A :: "'a::topological_space set" and B :: "'b::topological_space set"
  3.1056 +  assumes A: "A \<in> sets borel" and B: "B \<in> sets borel"
  3.1057 +  shows "A \<times> B \<in> sets borel"
  3.1058 +proof -
  3.1059 +  have "A \<times> B = (A\<times>UNIV) \<inter> (UNIV \<times> B)"
  3.1060 +    by auto
  3.1061 +  moreover
  3.1062 +  { have "A \<in> sigma_sets UNIV {S. open S}" using A by (simp add: sets_borel)
  3.1063 +    then have "A\<times>UNIV \<in> sets borel"
  3.1064 +    proof (induct A)
  3.1065 +      case (Basic S) then show ?case
  3.1066 +        by (auto intro!: borel_open open_Times)
  3.1067 +    next
  3.1068 +      case (Compl A)
  3.1069 +      moreover have *: "(UNIV - A) \<times> UNIV = UNIV - (A \<times> UNIV)"
  3.1070 +        by auto
  3.1071 +      ultimately show ?case
  3.1072 +        unfolding * by auto
  3.1073 +    next
  3.1074 +      case (Union A)
  3.1075 +      moreover have *: "(UNION UNIV A) \<times> UNIV = UNION UNIV (\<lambda>i. A i \<times> UNIV)"
  3.1076 +        by auto
  3.1077 +      ultimately show ?case
  3.1078 +        unfolding * by auto
  3.1079 +    qed simp }
  3.1080 +  moreover
  3.1081 +  { have "B \<in> sigma_sets UNIV {S. open S}" using B by (simp add: sets_borel)
  3.1082 +    then have "UNIV\<times>B \<in> sets borel"
  3.1083 +    proof (induct B)
  3.1084 +      case (Basic S) then show ?case
  3.1085 +        by (auto intro!: borel_open open_Times)
  3.1086 +    next
  3.1087 +      case (Compl B)
  3.1088 +      moreover have *: "UNIV \<times> (UNIV - B) = UNIV - (UNIV \<times> B)"
  3.1089 +        by auto
  3.1090 +      ultimately show ?case
  3.1091 +        unfolding * by auto
  3.1092 +    next
  3.1093 +      case (Union B)
  3.1094 +      moreover have *: "UNIV \<times> (UNION UNIV B) = UNION UNIV (\<lambda>i. UNIV \<times> B i)"
  3.1095 +        by auto
  3.1096 +      ultimately show ?case
  3.1097 +        unfolding * by auto
  3.1098 +    qed simp }
  3.1099 +  ultimately show ?thesis
  3.1100 +    by auto
  3.1101 +qed
  3.1102 +
  3.1103 +lemma finite_measure_pair_measure:
  3.1104 +  assumes "finite_measure M" "finite_measure N"
  3.1105 +  shows "finite_measure (N  \<Otimes>\<^sub>M M)"
  3.1106 +proof (rule finite_measureI)
  3.1107 +  interpret M: finite_measure M by fact
  3.1108 +  interpret N: finite_measure N by fact
  3.1109 +  show "emeasure (N  \<Otimes>\<^sub>M M) (space (N  \<Otimes>\<^sub>M M)) \<noteq> \<infinity>"
  3.1110 +    by (auto simp: space_pair_measure M.emeasure_pair_measure_Times ennreal_mult_eq_top_iff)
  3.1111 +qed
  3.1112 +
  3.1113 +end
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/HOL/Analysis/Bochner_Integration.thy	Mon Aug 08 14:13:14 2016 +0200
     4.3 @@ -0,0 +1,3066 @@
     4.4 +(*  Title:      HOL/Analysis/Bochner_Integration.thy
     4.5 +    Author:     Johannes Hölzl, TU München
     4.6 +*)
     4.7 +
     4.8 +section \<open>Bochner Integration for Vector-Valued Functions\<close>
     4.9 +
    4.10 +theory Bochner_Integration
    4.11 +  imports Finite_Product_Measure
    4.12 +begin
    4.13 +
    4.14 +text \<open>
    4.15 +
    4.16 +In the following development of the Bochner integral we use second countable topologies instead
    4.17 +of separable spaces. A second countable topology is also separable.
    4.18 +
    4.19 +\<close>
    4.20 +
    4.21 +lemma borel_measurable_implies_sequence_metric:
    4.22 +  fixes f :: "'a \<Rightarrow> 'b :: {metric_space, second_countable_topology}"
    4.23 +  assumes [measurable]: "f \<in> borel_measurable M"
    4.24 +  shows "\<exists>F. (\<forall>i. simple_function M (F i)) \<and> (\<forall>x\<in>space M. (\<lambda>i. F i x) \<longlonglongrightarrow> f x) \<and>
    4.25 +    (\<forall>i. \<forall>x\<in>space M. dist (F i x) z \<le> 2 * dist (f x) z)"
    4.26 +proof -
    4.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"
    4.28 +    by (erule countable_dense_setE)
    4.29 +
    4.30 +  define e where "e = from_nat_into D"
    4.31 +  { fix n x
    4.32 +    obtain d where "d \<in> D" and d: "d \<in> ball x (1 / Suc n)"
    4.33 +      using D[of "ball x (1 / Suc n)"] by auto
    4.34 +    from \<open>d \<in> D\<close> D[of UNIV] \<open>countable D\<close> obtain i where "d = e i"
    4.35 +      unfolding e_def by (auto dest: from_nat_into_surj)
    4.36 +    with d have "\<exists>i. dist x (e i) < 1 / Suc n"
    4.37 +      by auto }
    4.38 +  note e = this
    4.39 +
    4.40 +  define A where [abs_def]: "A m n =
    4.41 +    {x\<in>space M. dist (f x) (e n) < 1 / (Suc m) \<and> 1 / (Suc m) \<le> dist (f x) z}" for m n
    4.42 +  define B where [abs_def]: "B m = disjointed (A m)" for m
    4.43 +
    4.44 +  define m where [abs_def]: "m N x = Max {m. m \<le> N \<and> x \<in> (\<Union>n\<le>N. B m n)}" for N x
    4.45 +  define F where [abs_def]: "F N x =
    4.46 +    (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)
    4.47 +     then e (LEAST n. x \<in> B (m N x) n) else z)" for N x
    4.48 +
    4.49 +  have B_imp_A[intro, simp]: "\<And>x m n. x \<in> B m n \<Longrightarrow> x \<in> A m n"
    4.50 +    using disjointed_subset[of "A m" for m] unfolding B_def by auto
    4.51 +
    4.52 +  { fix m
    4.53 +    have "\<And>n. A m n \<in> sets M"
    4.54 +      by (auto simp: A_def)
    4.55 +    then have "\<And>n. B m n \<in> sets M"
    4.56 +      using sets.range_disjointed_sets[of "A m" M] by (auto simp: B_def) }
    4.57 +  note this[measurable]
    4.58 +
    4.59 +  { fix N i x assume "\<exists>m\<le>N. x \<in> (\<Union>n\<le>N. B m n)"
    4.60 +    then have "m N x \<in> {m::nat. m \<le> N \<and> x \<in> (\<Union>n\<le>N. B m n)}"
    4.61 +      unfolding m_def by (intro Max_in) auto
    4.62 +    then have "m N x \<le> N" "\<exists>n\<le>N. x \<in> B (m N x) n"
    4.63 +      by auto }
    4.64 +  note m = this
    4.65 +
    4.66 +  { fix j N i x assume "j \<le> N" "i \<le> N" "x \<in> B j i"
    4.67 +    then have "j \<le> m N x"
    4.68 +      unfolding m_def by (intro Max_ge) auto }
    4.69 +  note m_upper = this
    4.70 +
    4.71 +  show ?thesis
    4.72 +    unfolding simple_function_def
    4.73 +  proof (safe intro!: exI[of _ F])
    4.74 +    have [measurable]: "\<And>i. F i \<in> borel_measurable M"
    4.75 +      unfolding F_def m_def by measurable
    4.76 +    show "\<And>x i. F i -` {x} \<inter> space M \<in> sets M"
    4.77 +      by measurable
    4.78 +
    4.79 +    { fix i
    4.80 +      { fix n x assume "x \<in> B (m i x) n"
    4.81 +        then have "(LEAST n. x \<in> B (m i x) n) \<le> n"
    4.82 +          by (intro Least_le)
    4.83 +        also assume "n \<le> i"
    4.84 +        finally have "(LEAST n. x \<in> B (m i x) n) \<le> i" . }
    4.85 +      then have "F i ` space M \<subseteq> {z} \<union> e ` {.. i}"
    4.86 +        by (auto simp: F_def)
    4.87 +      then show "finite (F i ` space M)"
    4.88 +        by (rule finite_subset) auto }
    4.89 +
    4.90 +    { fix N i n x assume "i \<le> N" "n \<le> N" "x \<in> B i n"
    4.91 +      then have 1: "\<exists>m\<le>N. x \<in> (\<Union>n\<le>N. B m n)" by auto
    4.92 +      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
    4.93 +      moreover
    4.94 +      define L where "L = (LEAST n. x \<in> B (m N x) n)"
    4.95 +      have "dist (f x) (e L) < 1 / Suc (m N x)"
    4.96 +      proof -
    4.97 +        have "x \<in> B (m N x) L"
    4.98 +          using n(3) unfolding L_def by (rule LeastI)
    4.99 +        then have "x \<in> A (m N x) L"
   4.100 +          by auto
   4.101 +        then show ?thesis
   4.102 +          unfolding A_def by simp
   4.103 +      qed
   4.104 +      ultimately have "dist (f x) (F N x) < 1 / Suc (m N x)"
   4.105 +        by (auto simp add: F_def L_def) }
   4.106 +    note * = this
   4.107 +
   4.108 +    fix x assume "x \<in> space M"
   4.109 +    show "(\<lambda>i. F i x) \<longlonglongrightarrow> f x"
   4.110 +    proof cases
   4.111 +      assume "f x = z"
   4.112 +      then have "\<And>i n. x \<notin> A i n"
   4.113 +        unfolding A_def by auto
   4.114 +      then have "\<And>i. F i x = z"
   4.115 +        by (auto simp: F_def)
   4.116 +      then show ?thesis
   4.117 +        using \<open>f x = z\<close> by auto
   4.118 +    next
   4.119 +      assume "f x \<noteq> z"
   4.120 +
   4.121 +      show ?thesis
   4.122 +      proof (rule tendstoI)
   4.123 +        fix e :: real assume "0 < e"
   4.124 +        with \<open>f x \<noteq> z\<close> obtain n where "1 / Suc n < e" "1 / Suc n < dist (f x) z"
   4.125 +          by (metis dist_nz order_less_trans neq_iff nat_approx_posE)
   4.126 +        with \<open>x\<in>space M\<close> \<open>f x \<noteq> z\<close> have "x \<in> (\<Union>i. B n i)"
   4.127 +          unfolding A_def B_def UN_disjointed_eq using e by auto
   4.128 +        then obtain i where i: "x \<in> B n i" by auto
   4.129 +
   4.130 +        show "eventually (\<lambda>i. dist (F i x) (f x) < e) sequentially"
   4.131 +          using eventually_ge_at_top[of "max n i"]
   4.132 +        proof eventually_elim
   4.133 +          fix j assume j: "max n i \<le> j"
   4.134 +          with i have "dist (f x) (F j x) < 1 / Suc (m j x)"
   4.135 +            by (intro *[OF _ _ i]) auto
   4.136 +          also have "\<dots> \<le> 1 / Suc n"
   4.137 +            using j m_upper[OF _ _ i]
   4.138 +            by (auto simp: field_simps)
   4.139 +          also note \<open>1 / Suc n < e\<close>
   4.140 +          finally show "dist (F j x) (f x) < e"
   4.141 +            by (simp add: less_imp_le dist_commute)
   4.142 +        qed
   4.143 +      qed
   4.144 +    qed
   4.145 +    fix i
   4.146 +    { fix n m assume "x \<in> A n m"
   4.147 +      then have "dist (e m) (f x) + dist (f x) z \<le> 2 * dist (f x) z"
   4.148 +        unfolding A_def by (auto simp: dist_commute)
   4.149 +      also have "dist (e m) z \<le> dist (e m) (f x) + dist (f x) z"
   4.150 +        by (rule dist_triangle)
   4.151 +      finally (xtrans) have "dist (e m) z \<le> 2 * dist (f x) z" . }
   4.152 +    then show "dist (F i x) z \<le> 2 * dist (f x) z"
   4.153 +      unfolding F_def
   4.154 +      apply auto
   4.155 +      apply (rule LeastI2)
   4.156 +      apply auto
   4.157 +      done
   4.158 +  qed
   4.159 +qed
   4.160 +
   4.161 +lemma
   4.162 +  fixes f :: "'a \<Rightarrow> 'b::semiring_1" assumes "finite A"
   4.163 +  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)"
   4.164 +  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)"
   4.165 +  unfolding indicator_def
   4.166 +  using assms by (auto intro!: setsum.mono_neutral_cong_right split: if_split_asm)
   4.167 +
   4.168 +lemma borel_measurable_induct_real[consumes 2, case_names set mult add seq]:
   4.169 +  fixes P :: "('a \<Rightarrow> real) \<Rightarrow> bool"
   4.170 +  assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
   4.171 +  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
   4.172 +  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)"
   4.173 +  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)"
   4.174 +  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) \<longlonglongrightarrow> u x) \<Longrightarrow> P u"
   4.175 +  shows "P u"
   4.176 +proof -
   4.177 +  have "(\<lambda>x. ennreal (u x)) \<in> borel_measurable M" using u by auto
   4.178 +  from borel_measurable_implies_simple_function_sequence'[OF this]
   4.179 +  obtain U where U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i x. U i x < top" and
   4.180 +    sup: "\<And>x. (SUP i. U i x) = ennreal (u x)"
   4.181 +    by blast
   4.182 +
   4.183 +  define U' where [abs_def]: "U' i x = indicator (space M) x * enn2real (U i x)" for i x
   4.184 +  then have U'_sf[measurable]: "\<And>i. simple_function M (U' i)"
   4.185 +    using U by (auto intro!: simple_function_compose1[where g=enn2real])
   4.186 +
   4.187 +  show "P u"
   4.188 +  proof (rule seq)
   4.189 +    show U': "U' i \<in> borel_measurable M" "\<And>x. 0 \<le> U' i x" for i
   4.190 +      using U by (auto
   4.191 +          intro: borel_measurable_simple_function
   4.192 +          intro!: borel_measurable_enn2real borel_measurable_times
   4.193 +          simp: U'_def zero_le_mult_iff enn2real_nonneg)
   4.194 +    show "incseq U'"
   4.195 +      using U(2,3)
   4.196 +      by (auto simp: incseq_def le_fun_def image_iff eq_commute U'_def indicator_def enn2real_mono)
   4.197 +
   4.198 +    fix x assume x: "x \<in> space M"
   4.199 +    have "(\<lambda>i. U i x) \<longlonglongrightarrow> (SUP i. U i x)"
   4.200 +      using U(2) by (intro LIMSEQ_SUP) (auto simp: incseq_def le_fun_def)
   4.201 +    moreover have "(\<lambda>i. U i x) = (\<lambda>i. ennreal (U' i x))"
   4.202 +      using x U(3) by (auto simp: fun_eq_iff U'_def image_iff eq_commute)
   4.203 +    moreover have "(SUP i. U i x) = ennreal (u x)"
   4.204 +      using sup u(2) by (simp add: max_def)
   4.205 +    ultimately show "(\<lambda>i. U' i x) \<longlonglongrightarrow> u x"
   4.206 +      using u U' by simp
   4.207 +  next
   4.208 +    fix i
   4.209 +    have "U' i ` space M \<subseteq> enn2real ` (U i ` space M)" "finite (U i ` space M)"
   4.210 +      unfolding U'_def using U(1) by (auto dest: simple_functionD)
   4.211 +    then have fin: "finite (U' i ` space M)"
   4.212 +      by (metis finite_subset finite_imageI)
   4.213 +    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 {})"
   4.214 +      by auto
   4.215 +    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"
   4.216 +      by (simp add: U'_def fun_eq_iff)
   4.217 +    have "\<And>x. x \<in> U' i ` space M \<Longrightarrow> 0 \<le> x"
   4.218 +      by (auto simp: U'_def enn2real_nonneg)
   4.219 +    with fin have "P (\<lambda>z. \<Sum>y\<in>U' i`space M. y * indicator {x\<in>space M. U' i x = y} z)"
   4.220 +    proof induct
   4.221 +      case empty from set[of "{}"] show ?case
   4.222 +        by (simp add: indicator_def[abs_def])
   4.223 +    next
   4.224 +      case (insert x F)
   4.225 +      then show ?case
   4.226 +        by (auto intro!: add mult set setsum_nonneg split: split_indicator split_indicator_asm
   4.227 +                 simp del: setsum_mult_indicator simp: setsum_nonneg_eq_0_iff)
   4.228 +    qed
   4.229 +    with U' show "P (U' i)" by simp
   4.230 +  qed
   4.231 +qed
   4.232 +
   4.233 +lemma scaleR_cong_right:
   4.234 +  fixes x :: "'a :: real_vector"
   4.235 +  shows "(x \<noteq> 0 \<Longrightarrow> r = p) \<Longrightarrow> r *\<^sub>R x = p *\<^sub>R x"
   4.236 +  by (cases "x = 0") auto
   4.237 +
   4.238 +inductive simple_bochner_integrable :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b::real_vector) \<Rightarrow> bool" for M f where
   4.239 +  "simple_function M f \<Longrightarrow> emeasure M {y\<in>space M. f y \<noteq> 0} \<noteq> \<infinity> \<Longrightarrow>
   4.240 +    simple_bochner_integrable M f"
   4.241 +
   4.242 +lemma simple_bochner_integrable_compose2:
   4.243 +  assumes p_0: "p 0 0 = 0"
   4.244 +  shows "simple_bochner_integrable M f \<Longrightarrow> simple_bochner_integrable M g \<Longrightarrow>
   4.245 +    simple_bochner_integrable M (\<lambda>x. p (f x) (g x))"
   4.246 +proof (safe intro!: simple_bochner_integrable.intros elim!: simple_bochner_integrable.cases del: notI)
   4.247 +  assume sf: "simple_function M f" "simple_function M g"
   4.248 +  then show "simple_function M (\<lambda>x. p (f x) (g x))"
   4.249 +    by (rule simple_function_compose2)
   4.250 +
   4.251 +  from sf have [measurable]:
   4.252 +      "f \<in> measurable M (count_space UNIV)"
   4.253 +      "g \<in> measurable M (count_space UNIV)"
   4.254 +    by (auto intro: measurable_simple_function)
   4.255 +
   4.256 +  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>"
   4.257 +
   4.258 +  have "emeasure M {x\<in>space M. p (f x) (g x) \<noteq> 0} \<le>
   4.259 +      emeasure M ({x\<in>space M. f x \<noteq> 0} \<union> {x\<in>space M. g x \<noteq> 0})"
   4.260 +    by (intro emeasure_mono) (auto simp: p_0)
   4.261 +  also have "\<dots> \<le> emeasure M {x\<in>space M. f x \<noteq> 0} + emeasure M {x\<in>space M. g x \<noteq> 0}"
   4.262 +    by (intro emeasure_subadditive) auto
   4.263 +  finally show "emeasure M {y \<in> space M. p (f y) (g y) \<noteq> 0} \<noteq> \<infinity>"
   4.264 +    using fin by (auto simp: top_unique)
   4.265 +qed
   4.266 +
   4.267 +lemma simple_function_finite_support:
   4.268 +  assumes f: "simple_function M f" and fin: "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>" and nn: "\<And>x. 0 \<le> f x"
   4.269 +  shows "emeasure M {x\<in>space M. f x \<noteq> 0} \<noteq> \<infinity>"
   4.270 +proof cases
   4.271 +  from f have meas[measurable]: "f \<in> borel_measurable M"
   4.272 +    by (rule borel_measurable_simple_function)
   4.273 +
   4.274 +  assume non_empty: "\<exists>x\<in>space M. f x \<noteq> 0"
   4.275 +
   4.276 +  define m where "m = Min (f`space M - {0})"
   4.277 +  have "m \<in> f`space M - {0}"
   4.278 +    unfolding m_def using f non_empty by (intro Min_in) (auto simp: simple_function_def)
   4.279 +  then have m: "0 < m"
   4.280 +    using nn by (auto simp: less_le)
   4.281 +
   4.282 +  from m have "m * emeasure M {x\<in>space M. 0 \<noteq> f x} =
   4.283 +    (\<integral>\<^sup>+x. m * indicator {x\<in>space M. 0 \<noteq> f x} x \<partial>M)"
   4.284 +    using f by (intro nn_integral_cmult_indicator[symmetric]) auto
   4.285 +  also have "\<dots> \<le> (\<integral>\<^sup>+x. f x \<partial>M)"
   4.286 +    using AE_space
   4.287 +  proof (intro nn_integral_mono_AE, eventually_elim)
   4.288 +    fix x assume "x \<in> space M"
   4.289 +    with nn show "m * indicator {x \<in> space M. 0 \<noteq> f x} x \<le> f x"
   4.290 +      using f by (auto split: split_indicator simp: simple_function_def m_def)
   4.291 +  qed
   4.292 +  also note \<open>\<dots> < \<infinity>\<close>
   4.293 +  finally show ?thesis
   4.294 +    using m by (auto simp: ennreal_mult_less_top)
   4.295 +next
   4.296 +  assume "\<not> (\<exists>x\<in>space M. f x \<noteq> 0)"
   4.297 +  with nn have *: "{x\<in>space M. f x \<noteq> 0} = {}"
   4.298 +    by auto
   4.299 +  show ?thesis unfolding * by simp
   4.300 +qed
   4.301 +
   4.302 +lemma simple_bochner_integrableI_bounded:
   4.303 +  assumes f: "simple_function M f" and fin: "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
   4.304 +  shows "simple_bochner_integrable M f"
   4.305 +proof
   4.306 +  have "emeasure M {y \<in> space M. ennreal (norm (f y)) \<noteq> 0} \<noteq> \<infinity>"
   4.307 +  proof (rule simple_function_finite_support)
   4.308 +    show "simple_function M (\<lambda>x. ennreal (norm (f x)))"
   4.309 +      using f by (rule simple_function_compose1)
   4.310 +    show "(\<integral>\<^sup>+ y. ennreal (norm (f y)) \<partial>M) < \<infinity>" by fact
   4.311 +  qed simp
   4.312 +  then show "emeasure M {y \<in> space M. f y \<noteq> 0} \<noteq> \<infinity>" by simp
   4.313 +qed fact
   4.314 +
   4.315 +definition simple_bochner_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b::real_vector) \<Rightarrow> 'b" where
   4.316 +  "simple_bochner_integral M f = (\<Sum>y\<in>f`space M. measure M {x\<in>space M. f x = y} *\<^sub>R y)"
   4.317 +
   4.318 +lemma simple_bochner_integral_partition:
   4.319 +  assumes f: "simple_bochner_integrable M f" and g: "simple_function M g"
   4.320 +  assumes sub: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow> g x = g y \<Longrightarrow> f x = f y"
   4.321 +  assumes v: "\<And>x. x \<in> space M \<Longrightarrow> f x = v (g x)"
   4.322 +  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)"
   4.323 +    (is "_ = ?r")
   4.324 +proof -
   4.325 +  from f g have [simp]: "finite (f`space M)" "finite (g`space M)"
   4.326 +    by (auto simp: simple_function_def elim: simple_bochner_integrable.cases)
   4.327 +
   4.328 +  from f have [measurable]: "f \<in> measurable M (count_space UNIV)"
   4.329 +    by (auto intro: measurable_simple_function elim: simple_bochner_integrable.cases)
   4.330 +
   4.331 +  from g have [measurable]: "g \<in> measurable M (count_space UNIV)"
   4.332 +    by (auto intro: measurable_simple_function elim: simple_bochner_integrable.cases)
   4.333 +
   4.334 +  { fix y assume "y \<in> space M"
   4.335 +    then have "f ` space M \<inter> {i. \<exists>x\<in>space M. i = f x \<and> g y = g x} = {v (g y)}"
   4.336 +      by (auto cong: sub simp: v[symmetric]) }
   4.337 +  note eq = this
   4.338 +
   4.339 +  have "simple_bochner_integral M f =
   4.340 +    (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M.
   4.341 +      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)"
   4.342 +    unfolding simple_bochner_integral_def
   4.343 +  proof (safe intro!: setsum.cong scaleR_cong_right)
   4.344 +    fix y assume y: "y \<in> space M" "f y \<noteq> 0"
   4.345 +    have [simp]: "g ` space M \<inter> {z. \<exists>x\<in>space M. f y = f x \<and> z = g x} =
   4.346 +        {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
   4.347 +      by auto
   4.348 +    have eq:"{x \<in> space M. f x = f y} =
   4.349 +        (\<Union>i\<in>{z. \<exists>x\<in>space M. f y = f x \<and> z = g x}. {x \<in> space M. g x = i})"
   4.350 +      by (auto simp: eq_commute cong: sub rev_conj_cong)
   4.351 +    have "finite (g`space M)" by simp
   4.352 +    then have "finite {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
   4.353 +      by (rule rev_finite_subset) auto
   4.354 +    moreover
   4.355 +    { fix x assume "x \<in> space M" "f x = f y"
   4.356 +      then have "x \<in> space M" "f x \<noteq> 0"
   4.357 +        using y by auto
   4.358 +      then have "emeasure M {y \<in> space M. g y = g x} \<le> emeasure M {y \<in> space M. f y \<noteq> 0}"
   4.359 +        by (auto intro!: emeasure_mono cong: sub)
   4.360 +      then have "emeasure M {xa \<in> space M. g xa = g x} < \<infinity>"
   4.361 +        using f by (auto simp: simple_bochner_integrable.simps less_top) }
   4.362 +    ultimately
   4.363 +    show "measure M {x \<in> space M. f x = f y} =
   4.364 +      (\<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)"
   4.365 +      apply (simp add: setsum.If_cases eq)
   4.366 +      apply (subst measure_finite_Union[symmetric])
   4.367 +      apply (auto simp: disjoint_family_on_def less_top)
   4.368 +      done
   4.369 +  qed
   4.370 +  also have "\<dots> = (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M.
   4.371 +      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))"
   4.372 +    by (auto intro!: setsum.cong simp: scaleR_setsum_left)
   4.373 +  also have "\<dots> = ?r"
   4.374 +    by (subst setsum.commute)
   4.375 +       (auto intro!: setsum.cong simp: setsum.If_cases scaleR_setsum_right[symmetric] eq)
   4.376 +  finally show "simple_bochner_integral M f = ?r" .
   4.377 +qed
   4.378 +
   4.379 +lemma simple_bochner_integral_add:
   4.380 +  assumes f: "simple_bochner_integrable M f" and g: "simple_bochner_integrable M g"
   4.381 +  shows "simple_bochner_integral M (\<lambda>x. f x + g x) =
   4.382 +    simple_bochner_integral M f + simple_bochner_integral M g"
   4.383 +proof -
   4.384 +  from f g have "simple_bochner_integral M (\<lambda>x. f x + g x) =
   4.385 +    (\<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))"
   4.386 +    by (intro simple_bochner_integral_partition)
   4.387 +       (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   4.388 +  moreover from f g have "simple_bochner_integral M f =
   4.389 +    (\<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)"
   4.390 +    by (intro simple_bochner_integral_partition)
   4.391 +       (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   4.392 +  moreover from f g have "simple_bochner_integral M g =
   4.393 +    (\<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)"
   4.394 +    by (intro simple_bochner_integral_partition)
   4.395 +       (auto simp: simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   4.396 +  ultimately show ?thesis
   4.397 +    by (simp add: setsum.distrib[symmetric] scaleR_add_right)
   4.398 +qed
   4.399 +
   4.400 +lemma (in linear) simple_bochner_integral_linear:
   4.401 +  assumes g: "simple_bochner_integrable M g"
   4.402 +  shows "simple_bochner_integral M (\<lambda>x. f (g x)) = f (simple_bochner_integral M g)"
   4.403 +proof -
   4.404 +  from g have "simple_bochner_integral M (\<lambda>x. f (g x)) =
   4.405 +    (\<Sum>y\<in>g ` space M. measure M {x \<in> space M. g x = y} *\<^sub>R f y)"
   4.406 +    by (intro simple_bochner_integral_partition)
   4.407 +       (auto simp: simple_bochner_integrable_compose2[where p="\<lambda>x y. f x"] zero
   4.408 +             elim: simple_bochner_integrable.cases)
   4.409 +  also have "\<dots> = f (simple_bochner_integral M g)"
   4.410 +    by (simp add: simple_bochner_integral_def setsum scaleR)
   4.411 +  finally show ?thesis .
   4.412 +qed
   4.413 +
   4.414 +lemma simple_bochner_integral_minus:
   4.415 +  assumes f: "simple_bochner_integrable M f"
   4.416 +  shows "simple_bochner_integral M (\<lambda>x. - f x) = - simple_bochner_integral M f"
   4.417 +proof -
   4.418 +  interpret linear uminus by unfold_locales auto
   4.419 +  from f show ?thesis
   4.420 +    by (rule simple_bochner_integral_linear)
   4.421 +qed
   4.422 +
   4.423 +lemma simple_bochner_integral_diff:
   4.424 +  assumes f: "simple_bochner_integrable M f" and g: "simple_bochner_integrable M g"
   4.425 +  shows "simple_bochner_integral M (\<lambda>x. f x - g x) =
   4.426 +    simple_bochner_integral M f - simple_bochner_integral M g"
   4.427 +  unfolding diff_conv_add_uminus using f g
   4.428 +  by (subst simple_bochner_integral_add)
   4.429 +     (auto simp: simple_bochner_integral_minus simple_bochner_integrable_compose2[where p="\<lambda>x y. - y"])
   4.430 +
   4.431 +lemma simple_bochner_integral_norm_bound:
   4.432 +  assumes f: "simple_bochner_integrable M f"
   4.433 +  shows "norm (simple_bochner_integral M f) \<le> simple_bochner_integral M (\<lambda>x. norm (f x))"
   4.434 +proof -
   4.435 +  have "norm (simple_bochner_integral M f) \<le>
   4.436 +    (\<Sum>y\<in>f ` space M. norm (measure M {x \<in> space M. f x = y} *\<^sub>R y))"
   4.437 +    unfolding simple_bochner_integral_def by (rule norm_setsum)
   4.438 +  also have "\<dots> = (\<Sum>y\<in>f ` space M. measure M {x \<in> space M. f x = y} *\<^sub>R norm y)"
   4.439 +    by simp
   4.440 +  also have "\<dots> = simple_bochner_integral M (\<lambda>x. norm (f x))"
   4.441 +    using f
   4.442 +    by (intro simple_bochner_integral_partition[symmetric])
   4.443 +       (auto intro: f simple_bochner_integrable_compose2 elim: simple_bochner_integrable.cases)
   4.444 +  finally show ?thesis .
   4.445 +qed
   4.446 +
   4.447 +lemma simple_bochner_integral_nonneg[simp]:
   4.448 +  fixes f :: "'a \<Rightarrow> real"
   4.449 +  shows "(\<And>x. 0 \<le> f x) \<Longrightarrow> 0 \<le> simple_bochner_integral M f"
   4.450 +  by (simp add: setsum_nonneg simple_bochner_integral_def)
   4.451 +
   4.452 +lemma simple_bochner_integral_eq_nn_integral:
   4.453 +  assumes f: "simple_bochner_integrable M f" "\<And>x. 0 \<le> f x"
   4.454 +  shows "simple_bochner_integral M f = (\<integral>\<^sup>+x. f x \<partial>M)"
   4.455 +proof -
   4.456 +  { fix x y z have "(x \<noteq> 0 \<Longrightarrow> y = z) \<Longrightarrow> ennreal x * y = ennreal x * z"
   4.457 +      by (cases "x = 0") (auto simp: zero_ennreal_def[symmetric]) }
   4.458 +  note ennreal_cong_mult = this
   4.459 +
   4.460 +  have [measurable]: "f \<in> borel_measurable M"
   4.461 +    using f(1) by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   4.462 +
   4.463 +  { fix y assume y: "y \<in> space M" "f y \<noteq> 0"
   4.464 +    have "ennreal (measure M {x \<in> space M. f x = f y}) = emeasure M {x \<in> space M. f x = f y}"
   4.465 +    proof (rule emeasure_eq_ennreal_measure[symmetric])
   4.466 +      have "emeasure M {x \<in> space M. f x = f y} \<le> emeasure M {x \<in> space M. f x \<noteq> 0}"
   4.467 +        using y by (intro emeasure_mono) auto
   4.468 +      with f show "emeasure M {x \<in> space M. f x = f y} \<noteq> top"
   4.469 +        by (auto simp: simple_bochner_integrable.simps top_unique)
   4.470 +    qed
   4.471 +    moreover have "{x \<in> space M. f x = f y} = (\<lambda>x. ennreal (f x)) -` {ennreal (f y)} \<inter> space M"
   4.472 +      using f by auto
   4.473 +    ultimately have "ennreal (measure M {x \<in> space M. f x = f y}) =
   4.474 +          emeasure M ((\<lambda>x. ennreal (f x)) -` {ennreal (f y)} \<inter> space M)" by simp }
   4.475 +  with f have "simple_bochner_integral M f = (\<integral>\<^sup>Sx. f x \<partial>M)"
   4.476 +    unfolding simple_integral_def
   4.477 +    by (subst simple_bochner_integral_partition[OF f(1), where g="\<lambda>x. ennreal (f x)" and v=enn2real])
   4.478 +       (auto intro: f simple_function_compose1 elim: simple_bochner_integrable.cases
   4.479 +             intro!: setsum.cong ennreal_cong_mult
   4.480 +             simp: setsum_ennreal[symmetric] ac_simps ennreal_mult
   4.481 +             simp del: setsum_ennreal)
   4.482 +  also have "\<dots> = (\<integral>\<^sup>+x. f x \<partial>M)"
   4.483 +    using f
   4.484 +    by (intro nn_integral_eq_simple_integral[symmetric])
   4.485 +       (auto simp: simple_function_compose1 simple_bochner_integrable.simps)
   4.486 +  finally show ?thesis .
   4.487 +qed
   4.488 +
   4.489 +lemma simple_bochner_integral_bounded:
   4.490 +  fixes f :: "'a \<Rightarrow> 'b::{real_normed_vector, second_countable_topology}"
   4.491 +  assumes f[measurable]: "f \<in> borel_measurable M"
   4.492 +  assumes s: "simple_bochner_integrable M s" and t: "simple_bochner_integrable M t"
   4.493 +  shows "ennreal (norm (simple_bochner_integral M s - simple_bochner_integral M t)) \<le>
   4.494 +    (\<integral>\<^sup>+ x. norm (f x - s x) \<partial>M) + (\<integral>\<^sup>+ x. norm (f x - t x) \<partial>M)"
   4.495 +    (is "ennreal (norm (?s - ?t)) \<le> ?S + ?T")
   4.496 +proof -
   4.497 +  have [measurable]: "s \<in> borel_measurable M" "t \<in> borel_measurable M"
   4.498 +    using s t by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   4.499 +
   4.500 +  have "ennreal (norm (?s - ?t)) = norm (simple_bochner_integral M (\<lambda>x. s x - t x))"
   4.501 +    using s t by (subst simple_bochner_integral_diff) auto
   4.502 +  also have "\<dots> \<le> simple_bochner_integral M (\<lambda>x. norm (s x - t x))"
   4.503 +    using simple_bochner_integrable_compose2[of "op -" M "s" "t"] s t
   4.504 +    by (auto intro!: simple_bochner_integral_norm_bound)
   4.505 +  also have "\<dots> = (\<integral>\<^sup>+x. norm (s x - t x) \<partial>M)"
   4.506 +    using simple_bochner_integrable_compose2[of "\<lambda>x y. norm (x - y)" M "s" "t"] s t
   4.507 +    by (auto intro!: simple_bochner_integral_eq_nn_integral)
   4.508 +  also have "\<dots> \<le> (\<integral>\<^sup>+x. ennreal (norm (f x - s x)) + ennreal (norm (f x - t x)) \<partial>M)"
   4.509 +    by (auto intro!: nn_integral_mono simp: ennreal_plus[symmetric] simp del: ennreal_plus)
   4.510 +       (metis (erased, hide_lams) add_diff_cancel_left add_diff_eq diff_add_eq order_trans
   4.511 +              norm_minus_commute norm_triangle_ineq4 order_refl)
   4.512 +  also have "\<dots> = ?S + ?T"
   4.513 +   by (rule nn_integral_add) auto
   4.514 +  finally show ?thesis .
   4.515 +qed
   4.516 +
   4.517 +inductive has_bochner_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b) \<Rightarrow> 'b::{real_normed_vector, second_countable_topology} \<Rightarrow> bool"
   4.518 +  for M f x where
   4.519 +  "f \<in> borel_measurable M \<Longrightarrow>
   4.520 +    (\<And>i. simple_bochner_integrable M (s i)) \<Longrightarrow>
   4.521 +    (\<lambda>i. \<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) \<longlonglongrightarrow> 0 \<Longrightarrow>
   4.522 +    (\<lambda>i. simple_bochner_integral M (s i)) \<longlonglongrightarrow> x \<Longrightarrow>
   4.523 +    has_bochner_integral M f x"
   4.524 +
   4.525 +lemma has_bochner_integral_cong:
   4.526 +  assumes "M = N" "\<And>x. x \<in> space N \<Longrightarrow> f x = g x" "x = y"
   4.527 +  shows "has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral N g y"
   4.528 +  unfolding has_bochner_integral.simps assms(1,3)
   4.529 +  using assms(2) by (simp cong: measurable_cong_strong nn_integral_cong_strong)
   4.530 +
   4.531 +lemma has_bochner_integral_cong_AE:
   4.532 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow>
   4.533 +    has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral M g x"
   4.534 +  unfolding has_bochner_integral.simps
   4.535 +  by (intro arg_cong[where f=Ex] ext conj_cong rev_conj_cong refl arg_cong[where f="\<lambda>x. x \<longlonglongrightarrow> 0"]
   4.536 +            nn_integral_cong_AE)
   4.537 +     auto
   4.538 +
   4.539 +lemma borel_measurable_has_bochner_integral:
   4.540 +  "has_bochner_integral M f x \<Longrightarrow> f \<in> borel_measurable M"
   4.541 +  by (rule has_bochner_integral.cases)
   4.542 +
   4.543 +lemma borel_measurable_has_bochner_integral'[measurable_dest]:
   4.544 +  "has_bochner_integral M f x \<Longrightarrow> g \<in> measurable N M \<Longrightarrow> (\<lambda>x. f (g x)) \<in> borel_measurable N"
   4.545 +  using borel_measurable_has_bochner_integral[measurable] by measurable
   4.546 +
   4.547 +lemma has_bochner_integral_simple_bochner_integrable:
   4.548 +  "simple_bochner_integrable M f \<Longrightarrow> has_bochner_integral M f (simple_bochner_integral M f)"
   4.549 +  by (rule has_bochner_integral.intros[where s="\<lambda>_. f"])
   4.550 +     (auto intro: borel_measurable_simple_function
   4.551 +           elim: simple_bochner_integrable.cases
   4.552 +           simp: zero_ennreal_def[symmetric])
   4.553 +
   4.554 +lemma has_bochner_integral_real_indicator:
   4.555 +  assumes [measurable]: "A \<in> sets M" and A: "emeasure M A < \<infinity>"
   4.556 +  shows "has_bochner_integral M (indicator A) (measure M A)"
   4.557 +proof -
   4.558 +  have sbi: "simple_bochner_integrable M (indicator A::'a \<Rightarrow> real)"
   4.559 +  proof
   4.560 +    have "{y \<in> space M. (indicator A y::real) \<noteq> 0} = A"
   4.561 +      using sets.sets_into_space[OF \<open>A\<in>sets M\<close>] by (auto split: split_indicator)
   4.562 +    then show "emeasure M {y \<in> space M. (indicator A y::real) \<noteq> 0} \<noteq> \<infinity>"
   4.563 +      using A by auto
   4.564 +  qed (rule simple_function_indicator assms)+
   4.565 +  moreover have "simple_bochner_integral M (indicator A) = measure M A"
   4.566 +    using simple_bochner_integral_eq_nn_integral[OF sbi] A
   4.567 +    by (simp add: ennreal_indicator emeasure_eq_ennreal_measure)
   4.568 +  ultimately show ?thesis
   4.569 +    by (metis has_bochner_integral_simple_bochner_integrable)
   4.570 +qed
   4.571 +
   4.572 +lemma has_bochner_integral_add[intro]:
   4.573 +  "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M g y \<Longrightarrow>
   4.574 +    has_bochner_integral M (\<lambda>x. f x + g x) (x + y)"
   4.575 +proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   4.576 +  fix sf sg
   4.577 +  assume f_sf: "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - sf i x) \<partial>M) \<longlonglongrightarrow> 0"
   4.578 +  assume g_sg: "(\<lambda>i. \<integral>\<^sup>+ x. norm (g x - sg i x) \<partial>M) \<longlonglongrightarrow> 0"
   4.579 +
   4.580 +  assume sf: "\<forall>i. simple_bochner_integrable M (sf i)"
   4.581 +    and sg: "\<forall>i. simple_bochner_integrable M (sg i)"
   4.582 +  then have [measurable]: "\<And>i. sf i \<in> borel_measurable M" "\<And>i. sg i \<in> borel_measurable M"
   4.583 +    by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   4.584 +  assume [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
   4.585 +
   4.586 +  show "\<And>i. simple_bochner_integrable M (\<lambda>x. sf i x + sg i x)"
   4.587 +    using sf sg by (simp add: simple_bochner_integrable_compose2)
   4.588 +
   4.589 +  show "(\<lambda>i. \<integral>\<^sup>+ x. (norm (f x + g x - (sf i x + sg i x))) \<partial>M) \<longlonglongrightarrow> 0"
   4.590 +    (is "?f \<longlonglongrightarrow> 0")
   4.591 +  proof (rule tendsto_sandwich)
   4.592 +    show "eventually (\<lambda>n. 0 \<le> ?f n) sequentially" "(\<lambda>_. 0) \<longlonglongrightarrow> 0"
   4.593 +      by auto
   4.594 +    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"
   4.595 +      (is "eventually (\<lambda>i. ?f i \<le> ?g i) sequentially")
   4.596 +    proof (intro always_eventually allI)
   4.597 +      fix i have "?f i \<le> (\<integral>\<^sup>+ x. (norm (f x - sf i x)) + ennreal (norm (g x - sg i x)) \<partial>M)"
   4.598 +        by (auto intro!: nn_integral_mono norm_diff_triangle_ineq
   4.599 +                 simp del: ennreal_plus simp add: ennreal_plus[symmetric])
   4.600 +      also have "\<dots> = ?g i"
   4.601 +        by (intro nn_integral_add) auto
   4.602 +      finally show "?f i \<le> ?g i" .
   4.603 +    qed
   4.604 +    show "?g \<longlonglongrightarrow> 0"
   4.605 +      using tendsto_add[OF f_sf g_sg] by simp
   4.606 +  qed
   4.607 +qed (auto simp: simple_bochner_integral_add tendsto_add)
   4.608 +
   4.609 +lemma has_bochner_integral_bounded_linear:
   4.610 +  assumes "bounded_linear T"
   4.611 +  shows "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M (\<lambda>x. T (f x)) (T x)"
   4.612 +proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   4.613 +  interpret T: bounded_linear T by fact
   4.614 +  have [measurable]: "T \<in> borel_measurable borel"
   4.615 +    by (intro borel_measurable_continuous_on1 T.continuous_on continuous_on_id)
   4.616 +  assume [measurable]: "f \<in> borel_measurable M"
   4.617 +  then show "(\<lambda>x. T (f x)) \<in> borel_measurable M"
   4.618 +    by auto
   4.619 +
   4.620 +  fix s assume f_s: "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M) \<longlonglongrightarrow> 0"
   4.621 +  assume s: "\<forall>i. simple_bochner_integrable M (s i)"
   4.622 +  then show "\<And>i. simple_bochner_integrable M (\<lambda>x. T (s i x))"
   4.623 +    by (auto intro: simple_bochner_integrable_compose2 T.zero)
   4.624 +
   4.625 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   4.626 +    using s by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   4.627 +
   4.628 +  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"
   4.629 +    using T.pos_bounded by (auto simp: T.diff[symmetric])
   4.630 +
   4.631 +  show "(\<lambda>i. \<integral>\<^sup>+ x. norm (T (f x) - T (s i x)) \<partial>M) \<longlonglongrightarrow> 0"
   4.632 +    (is "?f \<longlonglongrightarrow> 0")
   4.633 +  proof (rule tendsto_sandwich)
   4.634 +    show "eventually (\<lambda>n. 0 \<le> ?f n) sequentially" "(\<lambda>_. 0) \<longlonglongrightarrow> 0"
   4.635 +      by auto
   4.636 +
   4.637 +    show "eventually (\<lambda>i. ?f i \<le> K * (\<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M)) sequentially"
   4.638 +      (is "eventually (\<lambda>i. ?f i \<le> ?g i) sequentially")
   4.639 +    proof (intro always_eventually allI)
   4.640 +      fix i have "?f i \<le> (\<integral>\<^sup>+ x. ennreal K * norm (f x - s i x) \<partial>M)"
   4.641 +        using K by (intro nn_integral_mono) (auto simp: ac_simps ennreal_mult[symmetric])
   4.642 +      also have "\<dots> = ?g i"
   4.643 +        using K by (intro nn_integral_cmult) auto
   4.644 +      finally show "?f i \<le> ?g i" .
   4.645 +    qed
   4.646 +    show "?g \<longlonglongrightarrow> 0"
   4.647 +      using ennreal_tendsto_cmult[OF _ f_s] by simp
   4.648 +  qed
   4.649 +
   4.650 +  assume "(\<lambda>i. simple_bochner_integral M (s i)) \<longlonglongrightarrow> x"
   4.651 +  with s show "(\<lambda>i. simple_bochner_integral M (\<lambda>x. T (s i x))) \<longlonglongrightarrow> T x"
   4.652 +    by (auto intro!: T.tendsto simp: T.simple_bochner_integral_linear)
   4.653 +qed
   4.654 +
   4.655 +lemma has_bochner_integral_zero[intro]: "has_bochner_integral M (\<lambda>x. 0) 0"
   4.656 +  by (auto intro!: has_bochner_integral.intros[where s="\<lambda>_ _. 0"]
   4.657 +           simp: zero_ennreal_def[symmetric] simple_bochner_integrable.simps
   4.658 +                 simple_bochner_integral_def image_constant_conv)
   4.659 +
   4.660 +lemma has_bochner_integral_scaleR_left[intro]:
   4.661 +  "(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)"
   4.662 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_scaleR_left])
   4.663 +
   4.664 +lemma has_bochner_integral_scaleR_right[intro]:
   4.665 +  "(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)"
   4.666 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_scaleR_right])
   4.667 +
   4.668 +lemma has_bochner_integral_mult_left[intro]:
   4.669 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   4.670 +  shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x * c) (x * c)"
   4.671 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_mult_left])
   4.672 +
   4.673 +lemma has_bochner_integral_mult_right[intro]:
   4.674 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   4.675 +  shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c * f x) (c * x)"
   4.676 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_mult_right])
   4.677 +
   4.678 +lemmas has_bochner_integral_divide =
   4.679 +  has_bochner_integral_bounded_linear[OF bounded_linear_divide]
   4.680 +
   4.681 +lemma has_bochner_integral_divide_zero[intro]:
   4.682 +  fixes c :: "_::{real_normed_field, field, second_countable_topology}"
   4.683 +  shows "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x / c) (x / c)"
   4.684 +  using has_bochner_integral_divide by (cases "c = 0") auto
   4.685 +
   4.686 +lemma has_bochner_integral_inner_left[intro]:
   4.687 +  "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. f x \<bullet> c) (x \<bullet> c)"
   4.688 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_inner_left])
   4.689 +
   4.690 +lemma has_bochner_integral_inner_right[intro]:
   4.691 +  "(c \<noteq> 0 \<Longrightarrow> has_bochner_integral M f x) \<Longrightarrow> has_bochner_integral M (\<lambda>x. c \<bullet> f x) (c \<bullet> x)"
   4.692 +  by (cases "c = 0") (auto simp add: has_bochner_integral_bounded_linear[OF bounded_linear_inner_right])
   4.693 +
   4.694 +lemmas has_bochner_integral_minus =
   4.695 +  has_bochner_integral_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
   4.696 +lemmas has_bochner_integral_Re =
   4.697 +  has_bochner_integral_bounded_linear[OF bounded_linear_Re]
   4.698 +lemmas has_bochner_integral_Im =
   4.699 +  has_bochner_integral_bounded_linear[OF bounded_linear_Im]
   4.700 +lemmas has_bochner_integral_cnj =
   4.701 +  has_bochner_integral_bounded_linear[OF bounded_linear_cnj]
   4.702 +lemmas has_bochner_integral_of_real =
   4.703 +  has_bochner_integral_bounded_linear[OF bounded_linear_of_real]
   4.704 +lemmas has_bochner_integral_fst =
   4.705 +  has_bochner_integral_bounded_linear[OF bounded_linear_fst]
   4.706 +lemmas has_bochner_integral_snd =
   4.707 +  has_bochner_integral_bounded_linear[OF bounded_linear_snd]
   4.708 +
   4.709 +lemma has_bochner_integral_indicator:
   4.710 +  "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   4.711 +    has_bochner_integral M (\<lambda>x. indicator A x *\<^sub>R c) (measure M A *\<^sub>R c)"
   4.712 +  by (intro has_bochner_integral_scaleR_left has_bochner_integral_real_indicator)
   4.713 +
   4.714 +lemma has_bochner_integral_diff:
   4.715 +  "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M g y \<Longrightarrow>
   4.716 +    has_bochner_integral M (\<lambda>x. f x - g x) (x - y)"
   4.717 +  unfolding diff_conv_add_uminus
   4.718 +  by (intro has_bochner_integral_add has_bochner_integral_minus)
   4.719 +
   4.720 +lemma has_bochner_integral_setsum:
   4.721 +  "(\<And>i. i \<in> I \<Longrightarrow> has_bochner_integral M (f i) (x i)) \<Longrightarrow>
   4.722 +    has_bochner_integral M (\<lambda>x. \<Sum>i\<in>I. f i x) (\<Sum>i\<in>I. x i)"
   4.723 +  by (induct I rule: infinite_finite_induct) auto
   4.724 +
   4.725 +lemma has_bochner_integral_implies_finite_norm:
   4.726 +  "has_bochner_integral M f x \<Longrightarrow> (\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
   4.727 +proof (elim has_bochner_integral.cases)
   4.728 +  fix s v
   4.729 +  assume [measurable]: "f \<in> borel_measurable M" and s: "\<And>i. simple_bochner_integrable M (s i)" and
   4.730 +    lim_0: "(\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) \<partial>M) \<longlonglongrightarrow> 0"
   4.731 +  from order_tendstoD[OF lim_0, of "\<infinity>"]
   4.732 +  obtain i where f_s_fin: "(\<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) \<partial>M) < \<infinity>"
   4.733 +    by (auto simp: eventually_sequentially)
   4.734 +
   4.735 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   4.736 +    using s by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   4.737 +
   4.738 +  define m where "m = (if space M = {} then 0 else Max ((\<lambda>x. norm (s i x))`space M))"
   4.739 +  have "finite (s i ` space M)"
   4.740 +    using s by (auto simp: simple_function_def simple_bochner_integrable.simps)
   4.741 +  then have "finite (norm ` s i ` space M)"
   4.742 +    by (rule finite_imageI)
   4.743 +  then have "\<And>x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> m" "0 \<le> m"
   4.744 +    by (auto simp: m_def image_comp comp_def Max_ge_iff)
   4.745 +  then have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) \<le> (\<integral>\<^sup>+x. ennreal m * indicator {x\<in>space M. s i x \<noteq> 0} x \<partial>M)"
   4.746 +    by (auto split: split_indicator intro!: Max_ge nn_integral_mono simp:)
   4.747 +  also have "\<dots> < \<infinity>"
   4.748 +    using s by (subst nn_integral_cmult_indicator) (auto simp: \<open>0 \<le> m\<close> simple_bochner_integrable.simps ennreal_mult_less_top less_top)
   4.749 +  finally have s_fin: "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) < \<infinity>" .
   4.750 +
   4.751 +  have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) + ennreal (norm (s i x)) \<partial>M)"
   4.752 +    by (auto intro!: nn_integral_mono simp del: ennreal_plus simp add: ennreal_plus[symmetric])
   4.753 +       (metis add.commute norm_triangle_sub)
   4.754 +  also have "\<dots> = (\<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) + (\<integral>\<^sup>+x. norm (s i x) \<partial>M)"
   4.755 +    by (rule nn_integral_add) auto
   4.756 +  also have "\<dots> < \<infinity>"
   4.757 +    using s_fin f_s_fin by auto
   4.758 +  finally show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>" .
   4.759 +qed
   4.760 +
   4.761 +lemma has_bochner_integral_norm_bound:
   4.762 +  assumes i: "has_bochner_integral M f x"
   4.763 +  shows "norm x \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   4.764 +using assms proof
   4.765 +  fix s assume
   4.766 +    x: "(\<lambda>i. simple_bochner_integral M (s i)) \<longlonglongrightarrow> x" (is "?s \<longlonglongrightarrow> x") and
   4.767 +    s[simp]: "\<And>i. simple_bochner_integrable M (s i)" and
   4.768 +    lim: "(\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) \<partial>M) \<longlonglongrightarrow> 0" and
   4.769 +    f[measurable]: "f \<in> borel_measurable M"
   4.770 +
   4.771 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M"
   4.772 +    using s by (auto simp: simple_bochner_integrable.simps intro: borel_measurable_simple_function)
   4.773 +
   4.774 +  show "norm x \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
   4.775 +  proof (rule LIMSEQ_le)
   4.776 +    show "(\<lambda>i. ennreal (norm (?s i))) \<longlonglongrightarrow> norm x"
   4.777 +      using x by (auto simp: tendsto_ennreal_iff intro: tendsto_intros)
   4.778 +    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)"
   4.779 +      (is "\<exists>N. \<forall>n\<ge>N. _ \<le> ?t n")
   4.780 +    proof (intro exI allI impI)
   4.781 +      fix n
   4.782 +      have "ennreal (norm (?s n)) \<le> simple_bochner_integral M (\<lambda>x. norm (s n x))"
   4.783 +        by (auto intro!: simple_bochner_integral_norm_bound)
   4.784 +      also have "\<dots> = (\<integral>\<^sup>+x. norm (s n x) \<partial>M)"
   4.785 +        by (intro simple_bochner_integral_eq_nn_integral)
   4.786 +           (auto intro: s simple_bochner_integrable_compose2)
   4.787 +      also have "\<dots> \<le> (\<integral>\<^sup>+x. ennreal (norm (f x - s n x)) + norm (f x) \<partial>M)"
   4.788 +        by (auto intro!: nn_integral_mono simp del: ennreal_plus simp add: ennreal_plus[symmetric])
   4.789 +           (metis add.commute norm_minus_commute norm_triangle_sub)
   4.790 +      also have "\<dots> = ?t n"
   4.791 +        by (rule nn_integral_add) auto
   4.792 +      finally show "norm (?s n) \<le> ?t n" .
   4.793 +    qed
   4.794 +    have "?t \<longlonglongrightarrow> 0 + (\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M)"
   4.795 +      using has_bochner_integral_implies_finite_norm[OF i]
   4.796 +      by (intro tendsto_add tendsto_const lim)
   4.797 +    then show "?t \<longlonglongrightarrow> \<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M"
   4.798 +      by simp
   4.799 +  qed
   4.800 +qed
   4.801 +
   4.802 +lemma has_bochner_integral_eq:
   4.803 +  "has_bochner_integral M f x \<Longrightarrow> has_bochner_integral M f y \<Longrightarrow> x = y"
   4.804 +proof (elim has_bochner_integral.cases)
   4.805 +  assume f[measurable]: "f \<in> borel_measurable M"
   4.806 +
   4.807 +  fix s t
   4.808 +  assume "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - s i x) \<partial>M) \<longlonglongrightarrow> 0" (is "?S \<longlonglongrightarrow> 0")
   4.809 +  assume "(\<lambda>i. \<integral>\<^sup>+ x. norm (f x - t i x) \<partial>M) \<longlonglongrightarrow> 0" (is "?T \<longlonglongrightarrow> 0")
   4.810 +  assume s: "\<And>i. simple_bochner_integrable M (s i)"
   4.811 +  assume t: "\<And>i. simple_bochner_integrable M (t i)"
   4.812 +
   4.813 +  have [measurable]: "\<And>i. s i \<in> borel_measurable M" "\<And>i. t i \<in> borel_measurable M"
   4.814 +    using s t by (auto intro: borel_measurable_simple_function elim: simple_bochner_integrable.cases)
   4.815 +
   4.816 +  let ?s = "\<lambda>i. simple_bochner_integral M (s i)"
   4.817 +  let ?t = "\<lambda>i. simple_bochner_integral M (t i)"
   4.818 +  assume "?s \<longlonglongrightarrow> x" "?t \<longlonglongrightarrow> y"
   4.819 +  then have "(\<lambda>i. norm (?s i - ?t i)) \<longlonglongrightarrow> norm (x - y)"
   4.820 +    by (intro tendsto_intros)
   4.821 +  moreover
   4.822 +  have "(\<lambda>i. ennreal (norm (?s i - ?t i))) \<longlonglongrightarrow> ennreal 0"
   4.823 +  proof (rule tendsto_sandwich)
   4.824 +    show "eventually (\<lambda>i. 0 \<le> ennreal (norm (?s i - ?t i))) sequentially" "(\<lambda>_. 0) \<longlonglongrightarrow> ennreal 0"
   4.825 +      by auto
   4.826 +
   4.827 +    show "eventually (\<lambda>i. norm (?s i - ?t i) \<le> ?S i + ?T i) sequentially"
   4.828 +      by (intro always_eventually allI simple_bochner_integral_bounded s t f)
   4.829 +    show "(\<lambda>i. ?S i + ?T i) \<longlonglongrightarrow> ennreal 0"
   4.830 +      using tendsto_add[OF \<open>?S \<longlonglongrightarrow> 0\<close> \<open>?T \<longlonglongrightarrow> 0\<close>] by simp
   4.831 +  qed
   4.832 +  then have "(\<lambda>i. norm (?s i - ?t i)) \<longlonglongrightarrow> 0"
   4.833 +    by (simp add: ennreal_0[symmetric] del: ennreal_0)
   4.834 +  ultimately have "norm (x - y) = 0"
   4.835 +    by (rule LIMSEQ_unique)
   4.836 +  then show "x = y" by simp
   4.837 +qed
   4.838 +
   4.839 +lemma has_bochner_integralI_AE:
   4.840 +  assumes f: "has_bochner_integral M f x"
   4.841 +    and g: "g \<in> borel_measurable M"
   4.842 +    and ae: "AE x in M. f x = g x"
   4.843 +  shows "has_bochner_integral M g x"
   4.844 +  using f
   4.845 +proof (safe intro!: has_bochner_integral.intros elim!: has_bochner_integral.cases)
   4.846 +  fix s assume "(\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) \<partial>M) \<longlonglongrightarrow> 0"
   4.847 +  also have "(\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) \<partial>M) = (\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (g x - s i x)) \<partial>M)"
   4.848 +    using ae
   4.849 +    by (intro ext nn_integral_cong_AE, eventually_elim) simp
   4.850 +  finally show "(\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (g x - s i x)) \<partial>M) \<longlonglongrightarrow> 0" .
   4.851 +qed (auto intro: g)
   4.852 +
   4.853 +lemma has_bochner_integral_eq_AE:
   4.854 +  assumes f: "has_bochner_integral M f x"
   4.855 +    and g: "has_bochner_integral M g y"
   4.856 +    and ae: "AE x in M. f x = g x"
   4.857 +  shows "x = y"
   4.858 +proof -
   4.859 +  from assms have "has_bochner_integral M g x"
   4.860 +    by (auto intro: has_bochner_integralI_AE)
   4.861 +  from this g show "x = y"
   4.862 +    by (rule has_bochner_integral_eq)
   4.863 +qed
   4.864 +
   4.865 +lemma simple_bochner_integrable_restrict_space:
   4.866 +  fixes f :: "_ \<Rightarrow> 'b::real_normed_vector"
   4.867 +  assumes \<Omega>: "\<Omega> \<inter> space M \<in> sets M"
   4.868 +  shows "simple_bochner_integrable (restrict_space M \<Omega>) f \<longleftrightarrow>
   4.869 +    simple_bochner_integrable M (\<lambda>x. indicator \<Omega> x *\<^sub>R f x)"
   4.870 +  by (simp add: simple_bochner_integrable.simps space_restrict_space
   4.871 +    simple_function_restrict_space[OF \<Omega>] emeasure_restrict_space[OF \<Omega>] Collect_restrict
   4.872 +    indicator_eq_0_iff conj_ac)
   4.873 +
   4.874 +lemma simple_bochner_integral_restrict_space:
   4.875 +  fixes f :: "_ \<Rightarrow> 'b::real_normed_vector"
   4.876 +  assumes \<Omega>: "\<Omega> \<inter> space M \<in> sets M"
   4.877 +  assumes f: "simple_bochner_integrable (restrict_space M \<Omega>) f"
   4.878 +  shows "simple_bochner_integral (restrict_space M \<Omega>) f =
   4.879 +    simple_bochner_integral M (\<lambda>x. indicator \<Omega> x *\<^sub>R f x)"
   4.880 +proof -
   4.881 +  have "finite ((\<lambda>x. indicator \<Omega> x *\<^sub>R f x)`space M)"
   4.882 +    using f simple_bochner_integrable_restrict_space[OF \<Omega>, of f]
   4.883 +    by (simp add: simple_bochner_integrable.simps simple_function_def)
   4.884 +  then show ?thesis
   4.885 +    by (auto simp: space_restrict_space measure_restrict_space[OF \<Omega>(1)] le_infI2
   4.886 +                   simple_bochner_integral_def Collect_restrict
   4.887 +             split: split_indicator split_indicator_asm
   4.888 +             intro!: setsum.mono_neutral_cong_left arg_cong2[where f=measure])
   4.889 +qed
   4.890 +
   4.891 +context
   4.892 +  notes [[inductive_internals]]
   4.893 +begin
   4.894 +
   4.895 +inductive integrable for M f where
   4.896 +  "has_bochner_integral M f x \<Longrightarrow> integrable M f"
   4.897 +
   4.898 +end
   4.899 +
   4.900 +definition lebesgue_integral ("integral\<^sup>L") where
   4.901 +  "integral\<^sup>L M f = (if \<exists>x. has_bochner_integral M f x then THE x. has_bochner_integral M f x else 0)"
   4.902 +
   4.903 +syntax
   4.904 +  "_lebesgue_integral" :: "pttrn \<Rightarrow> real \<Rightarrow> 'a measure \<Rightarrow> real" ("\<integral>((2 _./ _)/ \<partial>_)" [60,61] 110)
   4.905 +
   4.906 +translations
   4.907 +  "\<integral> x. f \<partial>M" == "CONST lebesgue_integral M (\<lambda>x. f)"
   4.908 +
   4.909 +syntax
   4.910 +  "_ascii_lebesgue_integral" :: "pttrn \<Rightarrow> 'a measure \<Rightarrow> real \<Rightarrow> real" ("(3LINT (1_)/|(_)./ _)" [0,110,60] 60)
   4.911 +
   4.912 +translations
   4.913 +  "LINT x|M. f" == "CONST lebesgue_integral M (\<lambda>x. f)"
   4.914 +
   4.915 +lemma has_bochner_integral_integral_eq: "has_bochner_integral M f x \<Longrightarrow> integral\<^sup>L M f = x"
   4.916 +  by (metis the_equality has_bochner_integral_eq lebesgue_integral_def)
   4.917 +
   4.918 +lemma has_bochner_integral_integrable:
   4.919 +  "integrable M f \<Longrightarrow> has_bochner_integral M f (integral\<^sup>L M f)"
   4.920 +  by (auto simp: has_bochner_integral_integral_eq integrable.simps)
   4.921 +
   4.922 +lemma has_bochner_integral_iff:
   4.923 +  "has_bochner_integral M f x \<longleftrightarrow> integrable M f \<and> integral\<^sup>L M f = x"
   4.924 +  by (metis has_bochner_integral_integrable has_bochner_integral_integral_eq integrable.intros)
   4.925 +
   4.926 +lemma simple_bochner_integrable_eq_integral:
   4.927 +  "simple_bochner_integrable M f \<Longrightarrow> simple_bochner_integral M f = integral\<^sup>L M f"
   4.928 +  using has_bochner_integral_simple_bochner_integrable[of M f]
   4.929 +  by (simp add: has_bochner_integral_integral_eq)
   4.930 +
   4.931 +lemma not_integrable_integral_eq: "\<not> integrable M f \<Longrightarrow> integral\<^sup>L M f = 0"
   4.932 +  unfolding integrable.simps lebesgue_integral_def by (auto intro!: arg_cong[where f=The])
   4.933 +
   4.934 +lemma integral_eq_cases:
   4.935 +  "integrable M f \<longleftrightarrow> integrable N g \<Longrightarrow>
   4.936 +    (integrable M f \<Longrightarrow> integrable N g \<Longrightarrow> integral\<^sup>L M f = integral\<^sup>L N g) \<Longrightarrow>
   4.937 +    integral\<^sup>L M f = integral\<^sup>L N g"
   4.938 +  by (metis not_integrable_integral_eq)
   4.939 +
   4.940 +lemma borel_measurable_integrable[measurable_dest]: "integrable M f \<Longrightarrow> f \<in> borel_measurable M"
   4.941 +  by (auto elim: integrable.cases has_bochner_integral.cases)
   4.942 +
   4.943 +lemma borel_measurable_integrable'[measurable_dest]:
   4.944 +  "integrable M f \<Longrightarrow> g \<in> measurable N M \<Longrightarrow> (\<lambda>x. f (g x)) \<in> borel_measurable N"
   4.945 +  using borel_measurable_integrable[measurable] by measurable
   4.946 +
   4.947 +lemma integrable_cong:
   4.948 +  "M = N \<Longrightarrow> (\<And>x. x \<in> space N \<Longrightarrow> f x = g x) \<Longrightarrow> integrable M f \<longleftrightarrow> integrable N g"
   4.949 +  by (simp cong: has_bochner_integral_cong add: integrable.simps)
   4.950 +
   4.951 +lemma integrable_cong_AE:
   4.952 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. f x = g x \<Longrightarrow>
   4.953 +    integrable M f \<longleftrightarrow> integrable M g"
   4.954 +  unfolding integrable.simps
   4.955 +  by (intro has_bochner_integral_cong_AE arg_cong[where f=Ex] ext)
   4.956 +
   4.957 +lemma integral_cong:
   4.958 +  "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"
   4.959 +  by (simp cong: has_bochner_integral_cong cong del: if_weak_cong add: lebesgue_integral_def)
   4.960 +
   4.961 +lemma integral_cong_AE:
   4.962 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. f x = g x \<Longrightarrow>
   4.963 +    integral\<^sup>L M f = integral\<^sup>L M g"
   4.964 +  unfolding lebesgue_integral_def
   4.965 +  by (rule arg_cong[where x="has_bochner_integral M f"]) (intro has_bochner_integral_cong_AE ext)
   4.966 +
   4.967 +lemma integrable_add[simp, intro]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> integrable M (\<lambda>x. f x + g x)"
   4.968 +  by (auto simp: integrable.simps)
   4.969 +
   4.970 +lemma integrable_zero[simp, intro]: "integrable M (\<lambda>x. 0)"
   4.971 +  by (metis has_bochner_integral_zero integrable.simps)
   4.972 +
   4.973 +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)"
   4.974 +  by (metis has_bochner_integral_setsum integrable.simps)
   4.975 +
   4.976 +lemma integrable_indicator[simp, intro]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   4.977 +  integrable M (\<lambda>x. indicator A x *\<^sub>R c)"
   4.978 +  by (metis has_bochner_integral_indicator integrable.simps)
   4.979 +
   4.980 +lemma integrable_real_indicator[simp, intro]: "A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow>
   4.981 +  integrable M (indicator A :: 'a \<Rightarrow> real)"
   4.982 +  by (metis has_bochner_integral_real_indicator integrable.simps)
   4.983 +
   4.984 +lemma integrable_diff[simp, intro]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> integrable M (\<lambda>x. f x - g x)"
   4.985 +  by (auto simp: integrable.simps intro: has_bochner_integral_diff)
   4.986 +
   4.987 +lemma integrable_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. T (f x))"
   4.988 +  by (auto simp: integrable.simps intro: has_bochner_integral_bounded_linear)
   4.989 +
   4.990 +lemma integrable_scaleR_left[simp, intro]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x *\<^sub>R c)"
   4.991 +  unfolding integrable.simps by fastforce
   4.992 +
   4.993 +lemma integrable_scaleR_right[simp, intro]: "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c *\<^sub>R f x)"
   4.994 +  unfolding integrable.simps by fastforce
   4.995 +
   4.996 +lemma integrable_mult_left[simp, intro]:
   4.997 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
   4.998 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x * c)"
   4.999 +  unfolding integrable.simps by fastforce
  4.1000 +
  4.1001 +lemma integrable_mult_right[simp, intro]:
  4.1002 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  4.1003 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c * f x)"
  4.1004 +  unfolding integrable.simps by fastforce
  4.1005 +
  4.1006 +lemma integrable_divide_zero[simp, intro]:
  4.1007 +  fixes c :: "_::{real_normed_field, field, second_countable_topology}"
  4.1008 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x / c)"
  4.1009 +  unfolding integrable.simps by fastforce
  4.1010 +
  4.1011 +lemma integrable_inner_left[simp, intro]:
  4.1012 +  "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. f x \<bullet> c)"
  4.1013 +  unfolding integrable.simps by fastforce
  4.1014 +
  4.1015 +lemma integrable_inner_right[simp, intro]:
  4.1016 +  "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> integrable M (\<lambda>x. c \<bullet> f x)"
  4.1017 +  unfolding integrable.simps by fastforce
  4.1018 +
  4.1019 +lemmas integrable_minus[simp, intro] =
  4.1020 +  integrable_bounded_linear[OF bounded_linear_minus[OF bounded_linear_ident]]
  4.1021 +lemmas integrable_divide[simp, intro] =
  4.1022 +  integrable_bounded_linear[OF bounded_linear_divide]
  4.1023 +lemmas integrable_Re[simp, intro] =
  4.1024 +  integrable_bounded_linear[OF bounded_linear_Re]
  4.1025 +lemmas integrable_Im[simp, intro] =
  4.1026 +  integrable_bounded_linear[OF bounded_linear_Im]
  4.1027 +lemmas integrable_cnj[simp, intro] =
  4.1028 +  integrable_bounded_linear[OF bounded_linear_cnj]
  4.1029 +lemmas integrable_of_real[simp, intro] =
  4.1030 +  integrable_bounded_linear[OF bounded_linear_of_real]
  4.1031 +lemmas integrable_fst[simp, intro] =
  4.1032 +  integrable_bounded_linear[OF bounded_linear_fst]
  4.1033 +lemmas integrable_snd[simp, intro] =
  4.1034 +  integrable_bounded_linear[OF bounded_linear_snd]
  4.1035 +
  4.1036 +lemma integral_zero[simp]: "integral\<^sup>L M (\<lambda>x. 0) = 0"
  4.1037 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_zero)
  4.1038 +
  4.1039 +lemma integral_add[simp]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow>
  4.1040 +    integral\<^sup>L M (\<lambda>x. f x + g x) = integral\<^sup>L M f + integral\<^sup>L M g"
  4.1041 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_add has_bochner_integral_integrable)
  4.1042 +
  4.1043 +lemma integral_diff[simp]: "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow>
  4.1044 +    integral\<^sup>L M (\<lambda>x. f x - g x) = integral\<^sup>L M f - integral\<^sup>L M g"
  4.1045 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_diff has_bochner_integral_integrable)
  4.1046 +
  4.1047 +lemma integral_setsum: "(\<And>i. i \<in> I \<Longrightarrow> integrable M (f i)) \<Longrightarrow>
  4.1048 +  integral\<^sup>L M (\<lambda>x. \<Sum>i\<in>I. f i x) = (\<Sum>i\<in>I. integral\<^sup>L M (f i))"
  4.1049 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_setsum has_bochner_integral_integrable)
  4.1050 +
  4.1051 +lemma integral_setsum'[simp]: "(\<And>i. i \<in> I =simp=> integrable M (f i)) \<Longrightarrow>
  4.1052 +  integral\<^sup>L M (\<lambda>x. \<Sum>i\<in>I. f i x) = (\<Sum>i\<in>I. integral\<^sup>L M (f i))"
  4.1053 +  unfolding simp_implies_def by (rule integral_setsum)
  4.1054 +
  4.1055 +lemma integral_bounded_linear: "bounded_linear T \<Longrightarrow> integrable M f \<Longrightarrow>
  4.1056 +    integral\<^sup>L M (\<lambda>x. T (f x)) = T (integral\<^sup>L M f)"
  4.1057 +  by (metis has_bochner_integral_bounded_linear has_bochner_integral_integrable has_bochner_integral_integral_eq)
  4.1058 +
  4.1059 +lemma integral_bounded_linear':
  4.1060 +  assumes T: "bounded_linear T" and T': "bounded_linear T'"
  4.1061 +  assumes *: "\<not> (\<forall>x. T x = 0) \<Longrightarrow> (\<forall>x. T' (T x) = x)"
  4.1062 +  shows "integral\<^sup>L M (\<lambda>x. T (f x)) = T (integral\<^sup>L M f)"
  4.1063 +proof cases
  4.1064 +  assume "(\<forall>x. T x = 0)" then show ?thesis
  4.1065 +    by simp
  4.1066 +next
  4.1067 +  assume **: "\<not> (\<forall>x. T x = 0)"
  4.1068 +  show ?thesis
  4.1069 +  proof cases
  4.1070 +    assume "integrable M f" with T show ?thesis
  4.1071 +      by (rule integral_bounded_linear)
  4.1072 +  next
  4.1073 +    assume not: "\<not> integrable M f"
  4.1074 +    moreover have "\<not> integrable M (\<lambda>x. T (f x))"
  4.1075 +    proof
  4.1076 +      assume "integrable M (\<lambda>x. T (f x))"
  4.1077 +      from integrable_bounded_linear[OF T' this] not *[OF **]
  4.1078 +      show False
  4.1079 +        by auto
  4.1080 +    qed
  4.1081 +    ultimately show ?thesis
  4.1082 +      using T by (simp add: not_integrable_integral_eq linear_simps)
  4.1083 +  qed
  4.1084 +qed
  4.1085 +
  4.1086 +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"
  4.1087 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_scaleR_left)
  4.1088 +
  4.1089 +lemma integral_scaleR_right[simp]: "(\<integral> x. c *\<^sub>R f x \<partial>M) = c *\<^sub>R integral\<^sup>L M f"
  4.1090 +  by (rule integral_bounded_linear'[OF bounded_linear_scaleR_right bounded_linear_scaleR_right[of "1 / c"]]) simp
  4.1091 +
  4.1092 +lemma integral_mult_left[simp]:
  4.1093 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  4.1094 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. f x * c \<partial>M) = integral\<^sup>L M f * c"
  4.1095 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_mult_left)
  4.1096 +
  4.1097 +lemma integral_mult_right[simp]:
  4.1098 +  fixes c :: "_::{real_normed_algebra,second_countable_topology}"
  4.1099 +  shows "(c \<noteq> 0 \<Longrightarrow> integrable M f) \<Longrightarrow> (\<integral> x. c * f x \<partial>M) = c * integral\<^sup>L M f"
  4.1100 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_mult_right)
  4.1101 +
  4.1102 +lemma integral_mult_left_zero[simp]:
  4.1103 +  fixes c :: "_::{real_normed_field,second_countable_topology}"
  4.1104 +  shows "(\<integral> x. f x * c \<partial>M) = integral\<^sup>L M f * c"
  4.1105 +  by (rule integral_bounded_linear'[OF bounded_linear_mult_left bounded_linear_mult_left[of "1 / c"]]) simp
  4.1106 +
  4.1107 +lemma integral_mult_right_zero[simp]:
  4.1108 +  fixes c :: "_::{real_normed_field,second_countable_topology}"
  4.1109 +  shows "(\<integral> x. c * f x \<partial>M) = c * integral\<^sup>L M f"
  4.1110 +  by (rule integral_bounded_linear'[OF bounded_linear_mult_right bounded_linear_mult_right[of "1 / c"]]) simp
  4.1111 +
  4.1112 +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"
  4.1113 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_inner_left)
  4.1114 +
  4.1115 +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"
  4.1116 +  by (intro has_bochner_integral_integral_eq has_bochner_integral_integrable has_bochner_integral_inner_right)
  4.1117 +
  4.1118 +lemma integral_divide_zero[simp]:
  4.1119 +  fixes c :: "_::{real_normed_field, field, second_countable_topology}"
  4.1120 +  shows "integral\<^sup>L M (\<lambda>x. f x / c) = integral\<^sup>L M f / c"
  4.1121 +  by (rule integral_bounded_linear'[OF bounded_linear_divide bounded_linear_mult_left[of c]]) simp
  4.1122 +
  4.1123 +lemma integral_minus[simp]: "integral\<^sup>L M (\<lambda>x. - f x) = - integral\<^sup>L M f"
  4.1124 +  by (rule integral_bounded_linear'[OF bounded_linear_minus[OF bounded_linear_ident] bounded_linear_minus[OF bounded_linear_ident]]) simp
  4.1125 +
  4.1126 +lemma integral_complex_of_real[simp]: "integral\<^sup>L M (\<lambda>x. complex_of_real (f x)) = of_real (integral\<^sup>L M f)"
  4.1127 +  by (rule integral_bounded_linear'[OF bounded_linear_of_real bounded_linear_Re]) simp
  4.1128 +
  4.1129 +lemma integral_cnj[simp]: "integral\<^sup>L M (\<lambda>x. cnj (f x)) = cnj (integral\<^sup>L M f)"
  4.1130 +  by (rule integral_bounded_linear'[OF bounded_linear_cnj bounded_linear_cnj]) simp
  4.1131 +
  4.1132 +lemmas integral_divide[simp] =
  4.1133 +  integral_bounded_linear[OF bounded_linear_divide]
  4.1134 +lemmas integral_Re[simp] =
  4.1135 +  integral_bounded_linear[OF bounded_linear_Re]
  4.1136 +lemmas integral_Im[simp] =
  4.1137 +  integral_bounded_linear[OF bounded_linear_Im]
  4.1138 +lemmas integral_of_real[simp] =
  4.1139 +  integral_bounded_linear[OF bounded_linear_of_real]
  4.1140 +lemmas integral_fst[simp] =
  4.1141 +  integral_bounded_linear[OF bounded_linear_fst]
  4.1142 +lemmas integral_snd[simp] =
  4.1143 +  integral_bounded_linear[OF bounded_linear_snd]
  4.1144 +
  4.1145 +lemma integral_norm_bound_ennreal:
  4.1146 +  "integrable M f \<Longrightarrow> norm (integral\<^sup>L M f) \<le> (\<integral>\<^sup>+x. norm (f x) \<partial>M)"
  4.1147 +  by (metis has_bochner_integral_integrable has_bochner_integral_norm_bound)
  4.1148 +
  4.1149 +lemma integrableI_sequence:
  4.1150 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1151 +  assumes f[measurable]: "f \<in> borel_measurable M"
  4.1152 +  assumes s: "\<And>i. simple_bochner_integrable M (s i)"
  4.1153 +  assumes lim: "(\<lambda>i. \<integral>\<^sup>+x. norm (f x - s i x) \<partial>M) \<longlonglongrightarrow> 0" (is "?S \<longlonglongrightarrow> 0")
  4.1154 +  shows "integrable M f"
  4.1155 +proof -
  4.1156 +  let ?s = "\<lambda>n. simple_bochner_integral M (s n)"
  4.1157 +
  4.1158 +  have "\<exists>x. ?s \<longlonglongrightarrow> x"
  4.1159 +    unfolding convergent_eq_cauchy
  4.1160 +  proof (rule metric_CauchyI)
  4.1161 +    fix e :: real assume "0 < e"
  4.1162 +    then have "0 < ennreal (e / 2)" by auto
  4.1163 +    from order_tendstoD(2)[OF lim this]
  4.1164 +    obtain M where M: "\<And>n. M \<le> n \<Longrightarrow> ?S n < e / 2"
  4.1165 +      by (auto simp: eventually_sequentially)
  4.1166 +    show "\<exists>M. \<forall>m\<ge>M. \<forall>n\<ge>M. dist (?s m) (?s n) < e"
  4.1167 +    proof (intro exI allI impI)
  4.1168 +      fix m n assume m: "M \<le> m" and n: "M \<le> n"
  4.1169 +      have "?S n \<noteq> \<infinity>"
  4.1170 +        using M[OF n] by auto
  4.1171 +      have "norm (?s n - ?s m) \<le> ?S n + ?S m"
  4.1172 +        by (intro simple_bochner_integral_bounded s f)
  4.1173 +      also have "\<dots> < ennreal (e / 2) + e / 2"
  4.1174 +        by (intro add_strict_mono M n m)
  4.1175 +      also have "\<dots> = e" using \<open>0<e\<close> by (simp del: ennreal_plus add: ennreal_plus[symmetric])
  4.1176 +      finally show "dist (?s n) (?s m) < e"
  4.1177 +        using \<open>0<e\<close> by (simp add: dist_norm ennreal_less_iff)
  4.1178 +    qed
  4.1179 +  qed
  4.1180 +  then obtain x where "?s \<longlonglongrightarrow> x" ..
  4.1181 +  show ?thesis
  4.1182 +    by (rule, rule) fact+
  4.1183 +qed
  4.1184 +
  4.1185 +lemma nn_integral_dominated_convergence_norm:
  4.1186 +  fixes u' :: "_ \<Rightarrow> _::{real_normed_vector, second_countable_topology}"
  4.1187 +  assumes [measurable]:
  4.1188 +       "\<And>i. u i \<in> borel_measurable M" "u' \<in> borel_measurable M" "w \<in> borel_measurable M"
  4.1189 +    and bound: "\<And>j. AE x in M. norm (u j x) \<le> w x"
  4.1190 +    and w: "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
  4.1191 +    and u': "AE x in M. (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
  4.1192 +  shows "(\<lambda>i. (\<integral>\<^sup>+x. norm (u' x - u i x) \<partial>M)) \<longlonglongrightarrow> 0"
  4.1193 +proof -
  4.1194 +  have "AE x in M. \<forall>j. norm (u j x) \<le> w x"
  4.1195 +    unfolding AE_all_countable by rule fact
  4.1196 +  with u' have bnd: "AE x in M. \<forall>j. norm (u' x - u j x) \<le> 2 * w x"
  4.1197 +  proof (eventually_elim, intro allI)
  4.1198 +    fix i x assume "(\<lambda>i. u i x) \<longlonglongrightarrow> u' x" "\<forall>j. norm (u j x) \<le> w x" "\<forall>j. norm (u j x) \<le> w x"
  4.1199 +    then have "norm (u' x) \<le> w x" "norm (u i x) \<le> w x"
  4.1200 +      by (auto intro: LIMSEQ_le_const2 tendsto_norm)
  4.1201 +    then have "norm (u' x) + norm (u i x) \<le> 2 * w x"
  4.1202 +      by simp
  4.1203 +    also have "norm (u' x - u i x) \<le> norm (u' x) + norm (u i x)"
  4.1204 +      by (rule norm_triangle_ineq4)
  4.1205 +    finally (xtrans) show "norm (u' x - u i x) \<le> 2 * w x" .
  4.1206 +  qed
  4.1207 +  have w_nonneg: "AE x in M. 0 \<le> w x"
  4.1208 +    using bound[of 0] by (auto intro: order_trans[OF norm_ge_zero])
  4.1209 +
  4.1210 +  have "(\<lambda>i. (\<integral>\<^sup>+x. norm (u' x - u i x) \<partial>M)) \<longlonglongrightarrow> (\<integral>\<^sup>+x. 0 \<partial>M)"
  4.1211 +  proof (rule nn_integral_dominated_convergence)
  4.1212 +    show "(\<integral>\<^sup>+x. 2 * w x \<partial>M) < \<infinity>"
  4.1213 +      by (rule nn_integral_mult_bounded_inf[OF _ w, of 2]) (insert w_nonneg, auto simp: ennreal_mult )
  4.1214 +    show "AE x in M. (\<lambda>i. ennreal (norm (u' x - u i x))) \<longlonglongrightarrow> 0"
  4.1215 +      using u'
  4.1216 +    proof eventually_elim
  4.1217 +      fix x assume "(\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
  4.1218 +      from tendsto_diff[OF tendsto_const[of "u' x"] this]
  4.1219 +      show "(\<lambda>i. ennreal (norm (u' x - u i x))) \<longlonglongrightarrow> 0"
  4.1220 +        by (simp add: tendsto_norm_zero_iff ennreal_0[symmetric] del: ennreal_0)
  4.1221 +    qed
  4.1222 +  qed (insert bnd w_nonneg, auto)
  4.1223 +  then show ?thesis by simp
  4.1224 +qed
  4.1225 +
  4.1226 +lemma integrableI_bounded:
  4.1227 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1228 +  assumes f[measurable]: "f \<in> borel_measurable M" and fin: "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  4.1229 +  shows "integrable M f"
  4.1230 +proof -
  4.1231 +  from borel_measurable_implies_sequence_metric[OF f, of 0] obtain s where
  4.1232 +    s: "\<And>i. simple_function M (s i)" and
  4.1233 +    pointwise: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) \<longlonglongrightarrow> f x" and
  4.1234 +    bound: "\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)"
  4.1235 +    by simp metis
  4.1236 +
  4.1237 +  show ?thesis
  4.1238 +  proof (rule integrableI_sequence)
  4.1239 +    { fix i
  4.1240 +      have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) \<le> (\<integral>\<^sup>+x. ennreal (2 * norm (f x)) \<partial>M)"
  4.1241 +        by (intro nn_integral_mono) (simp add: bound)
  4.1242 +      also have "\<dots> = 2 * (\<integral>\<^sup>+x. ennreal (norm (f x)) \<partial>M)"
  4.1243 +        by (simp add: ennreal_mult nn_integral_cmult)
  4.1244 +      also have "\<dots> < top"
  4.1245 +        using fin by (simp add: ennreal_mult_less_top)
  4.1246 +      finally have "(\<integral>\<^sup>+x. norm (s i x) \<partial>M) < \<infinity>"
  4.1247 +        by simp }
  4.1248 +    note fin_s = this
  4.1249 +
  4.1250 +    show "\<And>i. simple_bochner_integrable M (s i)"
  4.1251 +      by (rule simple_bochner_integrableI_bounded) fact+
  4.1252 +
  4.1253 +    show "(\<lambda>i. \<integral>\<^sup>+ x. ennreal (norm (f x - s i x)) \<partial>M) \<longlonglongrightarrow> 0"
  4.1254 +    proof (rule nn_integral_dominated_convergence_norm)
  4.1255 +      show "\<And>j. AE x in M. norm (s j x) \<le> 2 * norm (f x)"
  4.1256 +        using bound by auto
  4.1257 +      show "\<And>i. s i \<in> borel_measurable M" "(\<lambda>x. 2 * norm (f x)) \<in> borel_measurable M"
  4.1258 +        using s by (auto intro: borel_measurable_simple_function)
  4.1259 +      show "(\<integral>\<^sup>+ x. ennreal (2 * norm (f x)) \<partial>M) < \<infinity>"
  4.1260 +        using fin by (simp add: nn_integral_cmult ennreal_mult ennreal_mult_less_top)
  4.1261 +      show "AE x in M. (\<lambda>i. s i x) \<longlonglongrightarrow> f x"
  4.1262 +        using pointwise by auto
  4.1263 +    qed fact
  4.1264 +  qed fact
  4.1265 +qed
  4.1266 +
  4.1267 +lemma integrableI_bounded_set:
  4.1268 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1269 +  assumes [measurable]: "A \<in> sets M" "f \<in> borel_measurable M"
  4.1270 +  assumes finite: "emeasure M A < \<infinity>"
  4.1271 +    and bnd: "AE x in M. x \<in> A \<longrightarrow> norm (f x) \<le> B"
  4.1272 +    and null: "AE x in M. x \<notin> A \<longrightarrow> f x = 0"
  4.1273 +  shows "integrable M f"
  4.1274 +proof (rule integrableI_bounded)
  4.1275 +  { fix x :: 'b have "norm x \<le> B \<Longrightarrow> 0 \<le> B"
  4.1276 +      using norm_ge_zero[of x] by arith }
  4.1277 +  with bnd null have "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) \<le> (\<integral>\<^sup>+ x. ennreal (max 0 B) * indicator A x \<partial>M)"
  4.1278 +    by (intro nn_integral_mono_AE) (auto split: split_indicator split_max)
  4.1279 +  also have "\<dots> < \<infinity>"
  4.1280 +    using finite by (subst nn_integral_cmult_indicator) (auto simp: ennreal_mult_less_top)
  4.1281 +  finally show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>" .
  4.1282 +qed simp
  4.1283 +
  4.1284 +lemma integrableI_bounded_set_indicator:
  4.1285 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1286 +  shows "A \<in> sets M \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow>
  4.1287 +    emeasure M A < \<infinity> \<Longrightarrow> (AE x in M. x \<in> A \<longrightarrow> norm (f x) \<le> B) \<Longrightarrow>
  4.1288 +    integrable M (\<lambda>x. indicator A x *\<^sub>R f x)"
  4.1289 +  by (rule integrableI_bounded_set[where A=A]) auto
  4.1290 +
  4.1291 +lemma integrableI_nonneg:
  4.1292 +  fixes f :: "'a \<Rightarrow> real"
  4.1293 +  assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>"
  4.1294 +  shows "integrable M f"
  4.1295 +proof -
  4.1296 +  have "(\<integral>\<^sup>+x. norm (f x) \<partial>M) = (\<integral>\<^sup>+x. f x \<partial>M)"
  4.1297 +    using assms by (intro nn_integral_cong_AE) auto
  4.1298 +  then show ?thesis
  4.1299 +    using assms by (intro integrableI_bounded) auto
  4.1300 +qed
  4.1301 +
  4.1302 +lemma integrable_iff_bounded:
  4.1303 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1304 +  shows "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and> (\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  4.1305 +  using integrableI_bounded[of f M] has_bochner_integral_implies_finite_norm[of M f]
  4.1306 +  unfolding integrable.simps has_bochner_integral.simps[abs_def] by auto
  4.1307 +
  4.1308 +lemma integrable_bound:
  4.1309 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1310 +    and g :: "'a \<Rightarrow> 'c::{banach, second_countable_topology}"
  4.1311 +  shows "integrable M f \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. norm (g x) \<le> norm (f x)) \<Longrightarrow>
  4.1312 +    integrable M g"
  4.1313 +  unfolding integrable_iff_bounded
  4.1314 +proof safe
  4.1315 +  assume "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  4.1316 +  assume "AE x in M. norm (g x) \<le> norm (f x)"
  4.1317 +  then have "(\<integral>\<^sup>+ x. ennreal (norm (g x)) \<partial>M) \<le> (\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M)"
  4.1318 +    by  (intro nn_integral_mono_AE) auto
  4.1319 +  also assume "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>"
  4.1320 +  finally show "(\<integral>\<^sup>+ x. ennreal (norm (g x)) \<partial>M) < \<infinity>" .
  4.1321 +qed
  4.1322 +
  4.1323 +lemma integrable_mult_indicator:
  4.1324 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1325 +  shows "A \<in> sets M \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. indicator A x *\<^sub>R f x)"
  4.1326 +  by (rule integrable_bound[of M f]) (auto split: split_indicator)
  4.1327 +
  4.1328 +lemma integrable_real_mult_indicator:
  4.1329 +  fixes f :: "'a \<Rightarrow> real"
  4.1330 +  shows "A \<in> sets M \<Longrightarrow> integrable M f \<Longrightarrow> integrable M (\<lambda>x. f x * indicator A x)"
  4.1331 +  using integrable_mult_indicator[of A M f] by (simp add: mult_ac)
  4.1332 +
  4.1333 +lemma integrable_abs[simp, intro]:
  4.1334 +  fixes f :: "'a \<Rightarrow> real"
  4.1335 +  assumes [measurable]: "integrable M f" shows "integrable M (\<lambda>x. \<bar>f x\<bar>)"
  4.1336 +  using assms by (rule integrable_bound) auto
  4.1337 +
  4.1338 +lemma integrable_norm[simp, intro]:
  4.1339 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1340 +  assumes [measurable]: "integrable M f" shows "integrable M (\<lambda>x. norm (f x))"
  4.1341 +  using assms by (rule integrable_bound) auto
  4.1342 +
  4.1343 +lemma integrable_norm_cancel:
  4.1344 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1345 +  assumes [measurable]: "integrable M (\<lambda>x. norm (f x))" "f \<in> borel_measurable M" shows "integrable M f"
  4.1346 +  using assms by (rule integrable_bound) auto
  4.1347 +
  4.1348 +lemma integrable_norm_iff:
  4.1349 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1350 +  shows "f \<in> borel_measurable M \<Longrightarrow> integrable M (\<lambda>x. norm (f x)) \<longleftrightarrow> integrable M f"
  4.1351 +  by (auto intro: integrable_norm_cancel)
  4.1352 +
  4.1353 +lemma integrable_abs_cancel:
  4.1354 +  fixes f :: "'a \<Rightarrow> real"
  4.1355 +  assumes [measurable]: "integrable M (\<lambda>x. \<bar>f x\<bar>)" "f \<in> borel_measurable M" shows "integrable M f"
  4.1356 +  using assms by (rule integrable_bound) auto
  4.1357 +
  4.1358 +lemma integrable_abs_iff:
  4.1359 +  fixes f :: "'a \<Rightarrow> real"
  4.1360 +  shows "f \<in> borel_measurable M \<Longrightarrow> integrable M (\<lambda>x. \<bar>f x\<bar>) \<longleftrightarrow> integrable M f"
  4.1361 +  by (auto intro: integrable_abs_cancel)
  4.1362 +
  4.1363 +lemma integrable_max[simp, intro]:
  4.1364 +  fixes f :: "'a \<Rightarrow> real"
  4.1365 +  assumes fg[measurable]: "integrable M f" "integrable M g"
  4.1366 +  shows "integrable M (\<lambda>x. max (f x) (g x))"
  4.1367 +  using integrable_add[OF integrable_norm[OF fg(1)] integrable_norm[OF fg(2)]]
  4.1368 +  by (rule integrable_bound) auto
  4.1369 +
  4.1370 +lemma integrable_min[simp, intro]:
  4.1371 +  fixes f :: "'a \<Rightarrow> real"
  4.1372 +  assumes fg[measurable]: "integrable M f" "integrable M g"
  4.1373 +  shows "integrable M (\<lambda>x. min (f x) (g x))"
  4.1374 +  using integrable_add[OF integrable_norm[OF fg(1)] integrable_norm[OF fg(2)]]
  4.1375 +  by (rule integrable_bound) auto
  4.1376 +
  4.1377 +lemma integral_minus_iff[simp]:
  4.1378 +  "integrable M (\<lambda>x. - f x ::'a::{banach, second_countable_topology}) \<longleftrightarrow> integrable M f"
  4.1379 +  unfolding integrable_iff_bounded
  4.1380 +  by (auto intro: borel_measurable_uminus[of "\<lambda>x. - f x" M, simplified])
  4.1381 +
  4.1382 +lemma integrable_indicator_iff:
  4.1383 +  "integrable M (indicator A::_ \<Rightarrow> real) \<longleftrightarrow> A \<inter> space M \<in> sets M \<and> emeasure M (A \<inter> space M) < \<infinity>"
  4.1384 +  by (simp add: integrable_iff_bounded borel_measurable_indicator_iff ennreal_indicator nn_integral_indicator'
  4.1385 +           cong: conj_cong)
  4.1386 +
  4.1387 +lemma integral_indicator[simp]: "integral\<^sup>L M (indicator A) = measure M (A \<inter> space M)"
  4.1388 +proof cases
  4.1389 +  assume *: "A \<inter> space M \<in> sets M \<and> emeasure M (A \<inter> space M) < \<infinity>"
  4.1390 +  have "integral\<^sup>L M (indicator A) = integral\<^sup>L M (indicator (A \<inter> space M))"
  4.1391 +    by (intro integral_cong) (auto split: split_indicator)
  4.1392 +  also have "\<dots> = measure M (A \<inter> space M)"
  4.1393 +    using * by (intro has_bochner_integral_integral_eq has_bochner_integral_real_indicator) auto
  4.1394 +  finally show ?thesis .
  4.1395 +next
  4.1396 +  assume *: "\<not> (A \<inter> space M \<in> sets M \<and> emeasure M (A \<inter> space M) < \<infinity>)"
  4.1397 +  have "integral\<^sup>L M (indicator A) = integral\<^sup>L M (indicator (A \<inter> space M) :: _ \<Rightarrow> real)"
  4.1398 +    by (intro integral_cong) (auto split: split_indicator)
  4.1399 +  also have "\<dots> = 0"
  4.1400 +    using * by (subst not_integrable_integral_eq) (auto simp: integrable_indicator_iff)
  4.1401 +  also have "\<dots> = measure M (A \<inter> space M)"
  4.1402 +    using * by (auto simp: measure_def emeasure_notin_sets not_less top_unique)
  4.1403 +  finally show ?thesis .
  4.1404 +qed
  4.1405 +
  4.1406 +lemma integrable_discrete_difference:
  4.1407 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1408 +  assumes X: "countable X"
  4.1409 +  assumes null: "\<And>x. x \<in> X \<Longrightarrow> emeasure M {x} = 0"
  4.1410 +  assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  4.1411 +  assumes eq: "\<And>x. x \<in> space M \<Longrightarrow> x \<notin> X \<Longrightarrow> f x = g x"
  4.1412 +  shows "integrable M f \<longleftrightarrow> integrable M g"
  4.1413 +  unfolding integrable_iff_bounded
  4.1414 +proof (rule conj_cong)
  4.1415 +  { assume "f \<in> borel_measurable M" then have "g \<in> borel_measurable M"
  4.1416 +      by (rule measurable_discrete_difference[where X=X]) (auto simp: assms) }
  4.1417 +  moreover
  4.1418 +  { assume "g \<in> borel_measurable M" then have "f \<in> borel_measurable M"
  4.1419 +      by (rule measurable_discrete_difference[where X=X]) (auto simp: assms) }
  4.1420 +  ultimately show "f \<in> borel_measurable M \<longleftrightarrow> g \<in> borel_measurable M" ..
  4.1421 +next
  4.1422 +  have "AE x in M. x \<notin> X"
  4.1423 +    by (rule AE_discrete_difference) fact+
  4.1424 +  then have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) = (\<integral>\<^sup>+ x. norm (g x) \<partial>M)"
  4.1425 +    by (intro nn_integral_cong_AE) (auto simp: eq)
  4.1426 +  then show "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) < \<infinity> \<longleftrightarrow> (\<integral>\<^sup>+ x. norm (g x) \<partial>M) < \<infinity>"
  4.1427 +    by simp
  4.1428 +qed
  4.1429 +
  4.1430 +lemma integral_discrete_difference:
  4.1431 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1432 +  assumes X: "countable X"
  4.1433 +  assumes null: "\<And>x. x \<in> X \<Longrightarrow> emeasure M {x} = 0"
  4.1434 +  assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  4.1435 +  assumes eq: "\<And>x. x \<in> space M \<Longrightarrow> x \<notin> X \<Longrightarrow> f x = g x"
  4.1436 +  shows "integral\<^sup>L M f = integral\<^sup>L M g"
  4.1437 +proof (rule integral_eq_cases)
  4.1438 +  show eq: "integrable M f \<longleftrightarrow> integrable M g"
  4.1439 +    by (rule integrable_discrete_difference[where X=X]) fact+
  4.1440 +
  4.1441 +  assume f: "integrable M f"
  4.1442 +  show "integral\<^sup>L M f = integral\<^sup>L M g"
  4.1443 +  proof (rule integral_cong_AE)
  4.1444 +    show "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  4.1445 +      using f eq by (auto intro: borel_measurable_integrable)
  4.1446 +
  4.1447 +    have "AE x in M. x \<notin> X"
  4.1448 +      by (rule AE_discrete_difference) fact+
  4.1449 +    with AE_space show "AE x in M. f x = g x"
  4.1450 +      by eventually_elim fact
  4.1451 +  qed
  4.1452 +qed
  4.1453 +
  4.1454 +lemma has_bochner_integral_discrete_difference:
  4.1455 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1456 +  assumes X: "countable X"
  4.1457 +  assumes null: "\<And>x. x \<in> X \<Longrightarrow> emeasure M {x} = 0"
  4.1458 +  assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M"
  4.1459 +  assumes eq: "\<And>x. x \<in> space M \<Longrightarrow> x \<notin> X \<Longrightarrow> f x = g x"
  4.1460 +  shows "has_bochner_integral M f x \<longleftrightarrow> has_bochner_integral M g x"
  4.1461 +  using integrable_discrete_difference[of X M f g, OF assms]
  4.1462 +  using integral_discrete_difference[of X M f g, OF assms]
  4.1463 +  by (metis has_bochner_integral_iff)
  4.1464 +
  4.1465 +lemma
  4.1466 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and w :: "'a \<Rightarrow> real"
  4.1467 +  assumes "f \<in> borel_measurable M" "\<And>i. s i \<in> borel_measurable M" "integrable M w"
  4.1468 +  assumes lim: "AE x in M. (\<lambda>i. s i x) \<longlonglongrightarrow> f x"
  4.1469 +  assumes bound: "\<And>i. AE x in M. norm (s i x) \<le> w x"
  4.1470 +  shows integrable_dominated_convergence: "integrable M f"
  4.1471 +    and integrable_dominated_convergence2: "\<And>i. integrable M (s i)"
  4.1472 +    and integral_dominated_convergence: "(\<lambda>i. integral\<^sup>L M (s i)) \<longlonglongrightarrow> integral\<^sup>L M f"
  4.1473 +proof -
  4.1474 +  have w_nonneg: "AE x in M. 0 \<le> w x"
  4.1475 +    using bound[of 0] by eventually_elim (auto intro: norm_ge_zero order_trans)
  4.1476 +  then have "(\<integral>\<^sup>+x. w x \<partial>M) = (\<integral>\<^sup>+x. norm (w x) \<partial>M)"
  4.1477 +    by (intro nn_integral_cong_AE) auto
  4.1478 +  with \<open>integrable M w\<close> have w: "w \<in> borel_measurable M" "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
  4.1479 +    unfolding integrable_iff_bounded by auto
  4.1480 +
  4.1481 +  show int_s: "\<And>i. integrable M (s i)"
  4.1482 +    unfolding integrable_iff_bounded
  4.1483 +  proof
  4.1484 +    fix i
  4.1485 +    have "(\<integral>\<^sup>+ x. ennreal (norm (s i x)) \<partial>M) \<le> (\<integral>\<^sup>+x. w x \<partial>M)"
  4.1486 +      using bound[of i] w_nonneg by (intro nn_integral_mono_AE) auto
  4.1487 +    with w show "(\<integral>\<^sup>+ x. ennreal (norm (s i x)) \<partial>M) < \<infinity>" by auto
  4.1488 +  qed fact
  4.1489 +
  4.1490 +  have all_bound: "AE x in M. \<forall>i. norm (s i x) \<le> w x"
  4.1491 +    using bound unfolding AE_all_countable by auto
  4.1492 +
  4.1493 +  show int_f: "integrable M f"
  4.1494 +    unfolding integrable_iff_bounded
  4.1495 +  proof
  4.1496 +    have "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) \<le> (\<integral>\<^sup>+x. w x \<partial>M)"
  4.1497 +      using all_bound lim w_nonneg
  4.1498 +    proof (intro nn_integral_mono_AE, eventually_elim)
  4.1499 +      fix x assume "\<forall>i. norm (s i x) \<le> w x" "(\<lambda>i. s i x) \<longlonglongrightarrow> f x" "0 \<le> w x"
  4.1500 +      then show "ennreal (norm (f x)) \<le> ennreal (w x)"
  4.1501 +        by (intro LIMSEQ_le_const2[where X="\<lambda>i. ennreal (norm (s i x))"]) (auto intro: tendsto_intros)
  4.1502 +    qed
  4.1503 +    with w show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>" by auto
  4.1504 +  qed fact
  4.1505 +
  4.1506 +  have "(\<lambda>n. ennreal (norm (integral\<^sup>L M (s n) - integral\<^sup>L M f))) \<longlonglongrightarrow> ennreal 0" (is "?d \<longlonglongrightarrow> ennreal 0")
  4.1507 +  proof (rule tendsto_sandwich)
  4.1508 +    show "eventually (\<lambda>n. ennreal 0 \<le> ?d n) sequentially" "(\<lambda>_. ennreal 0) \<longlonglongrightarrow> ennreal 0" by auto
  4.1509 +    show "eventually (\<lambda>n. ?d n \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)) sequentially"
  4.1510 +    proof (intro always_eventually allI)
  4.1511 +      fix n
  4.1512 +      have "?d n = norm (integral\<^sup>L M (\<lambda>x. s n x - f x))"
  4.1513 +        using int_f int_s by simp
  4.1514 +      also have "\<dots> \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)"
  4.1515 +        by (intro int_f int_s integrable_diff integral_norm_bound_ennreal)
  4.1516 +      finally show "?d n \<le> (\<integral>\<^sup>+x. norm (s n x - f x) \<partial>M)" .
  4.1517 +    qed
  4.1518 +    show "(\<lambda>n. \<integral>\<^sup>+x. norm (s n x - f x) \<partial>M) \<longlonglongrightarrow> ennreal 0"
  4.1519 +      unfolding ennreal_0
  4.1520 +      apply (subst norm_minus_commute)
  4.1521 +    proof (rule nn_integral_dominated_convergence_norm[where w=w])
  4.1522 +      show "\<And>n. s n \<in> borel_measurable M"
  4.1523 +        using int_s unfolding integrable_iff_bounded by auto
  4.1524 +    qed fact+
  4.1525 +  qed
  4.1526 +  then have "(\<lambda>n. integral\<^sup>L M (s n) - integral\<^sup>L M f) \<longlonglongrightarrow> 0"
  4.1527 +    by (simp add: tendsto_norm_zero_iff del: ennreal_0)
  4.1528 +  from tendsto_add[OF this tendsto_const[of "integral\<^sup>L M f"]]
  4.1529 +  show "(\<lambda>i. integral\<^sup>L M (s i)) \<longlonglongrightarrow> integral\<^sup>L M f"  by simp
  4.1530 +qed
  4.1531 +
  4.1532 +context
  4.1533 +  fixes s :: "real \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}" and w :: "'a \<Rightarrow> real"
  4.1534 +    and f :: "'a \<Rightarrow> 'b" and M
  4.1535 +  assumes "f \<in> borel_measurable M" "\<And>t. s t \<in> borel_measurable M" "integrable M w"
  4.1536 +  assumes lim: "AE x in M. ((\<lambda>i. s i x) \<longlongrightarrow> f x) at_top"
  4.1537 +  assumes bound: "\<forall>\<^sub>F i in at_top. AE x in M. norm (s i x) \<le> w x"
  4.1538 +begin
  4.1539 +
  4.1540 +lemma integral_dominated_convergence_at_top: "((\<lambda>t. integral\<^sup>L M (s t)) \<longlongrightarrow> integral\<^sup>L M f) at_top"
  4.1541 +proof (rule tendsto_at_topI_sequentially)
  4.1542 +  fix X :: "nat \<Rightarrow> real" assume X: "filterlim X at_top sequentially"
  4.1543 +  from filterlim_iff[THEN iffD1, OF this, rule_format, OF bound]
  4.1544 +  obtain N where w: "\<And>n. N \<le> n \<Longrightarrow> AE x in M. norm (s (X n) x) \<le> w x"
  4.1545 +    by (auto simp: eventually_sequentially)
  4.1546 +
  4.1547 +  show "(\<lambda>n. integral\<^sup>L M (s (X n))) \<longlonglongrightarrow> integral\<^sup>L M f"
  4.1548 +  proof (rule LIMSEQ_offset, rule integral_dominated_convergence)
  4.1549 +    show "AE x in M. norm (s (X (n + N)) x) \<le> w x" for n
  4.1550 +      by (rule w) auto
  4.1551 +    show "AE x in M. (\<lambda>n. s (X (n + N)) x) \<longlonglongrightarrow> f x"
  4.1552 +      using lim
  4.1553 +    proof eventually_elim
  4.1554 +      fix x assume "((\<lambda>i. s i x) \<longlongrightarrow> f x) at_top"
  4.1555 +      then show "(\<lambda>n. s (X (n + N)) x) \<longlonglongrightarrow> f x"
  4.1556 +        by (intro LIMSEQ_ignore_initial_segment filterlim_compose[OF _ X])
  4.1557 +    qed
  4.1558 +  qed fact+
  4.1559 +qed
  4.1560 +
  4.1561 +lemma integrable_dominated_convergence_at_top: "integrable M f"
  4.1562 +proof -
  4.1563 +  from bound obtain N where w: "\<And>n. N \<le> n \<Longrightarrow> AE x in M. norm (s n x) \<le> w x"
  4.1564 +    by (auto simp: eventually_at_top_linorder)
  4.1565 +  show ?thesis
  4.1566 +  proof (rule integrable_dominated_convergence)
  4.1567 +    show "AE x in M. norm (s (N + i) x) \<le> w x" for i :: nat
  4.1568 +      by (intro w) auto
  4.1569 +    show "AE x in M. (\<lambda>i. s (N + real i) x) \<longlonglongrightarrow> f x"
  4.1570 +      using lim
  4.1571 +    proof eventually_elim
  4.1572 +      fix x assume "((\<lambda>i. s i x) \<longlongrightarrow> f x) at_top"
  4.1573 +      then show "(\<lambda>n. s (N + n) x) \<longlonglongrightarrow> f x"
  4.1574 +        by (rule filterlim_compose)
  4.1575 +           (auto intro!: filterlim_tendsto_add_at_top filterlim_real_sequentially)
  4.1576 +    qed
  4.1577 +  qed fact+
  4.1578 +qed
  4.1579 +
  4.1580 +end
  4.1581 +
  4.1582 +lemma integrable_mult_left_iff:
  4.1583 +  fixes f :: "'a \<Rightarrow> real"
  4.1584 +  shows "integrable M (\<lambda>x. c * f x) \<longleftrightarrow> c = 0 \<or> integrable M f"
  4.1585 +  using integrable_mult_left[of c M f] integrable_mult_left[of "1 / c" M "\<lambda>x. c * f x"]
  4.1586 +  by (cases "c = 0") auto
  4.1587 +
  4.1588 +lemma integrableI_nn_integral_finite:
  4.1589 +  assumes [measurable]: "f \<in> borel_measurable M"
  4.1590 +    and nonneg: "AE x in M. 0 \<le> f x"
  4.1591 +    and finite: "(\<integral>\<^sup>+x. f x \<partial>M) = ennreal x"
  4.1592 +  shows "integrable M f"
  4.1593 +proof (rule integrableI_bounded)
  4.1594 +  have "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) = (\<integral>\<^sup>+ x. ennreal (f x) \<partial>M)"
  4.1595 +    using nonneg by (intro nn_integral_cong_AE) auto
  4.1596 +  with finite show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>"
  4.1597 +    by auto
  4.1598 +qed simp
  4.1599 +
  4.1600 +lemma integral_nonneg_AE:
  4.1601 +  fixes f :: "'a \<Rightarrow> real"
  4.1602 +  assumes nonneg: "AE x in M. 0 \<le> f x"
  4.1603 +  shows "0 \<le> integral\<^sup>L M f"
  4.1604 +proof cases
  4.1605 +  assume f: "integrable M f"
  4.1606 +  then have [measurable]: "f \<in> M \<rightarrow>\<^sub>M borel"
  4.1607 +    by auto
  4.1608 +  have "(\<lambda>x. max 0 (f x)) \<in> M \<rightarrow>\<^sub>M borel" "\<And>x. 0 \<le> max 0 (f x)" "integrable M (\<lambda>x. max 0 (f x))"
  4.1609 +    using f by auto
  4.1610 +  from this have "0 \<le> integral\<^sup>L M (\<lambda>x. max 0 (f x))"
  4.1611 +  proof (induction rule: borel_measurable_induct_real)
  4.1612 +    case (add f g)
  4.1613 +    then have "integrable M f" "integrable M g"
  4.1614 +      by (auto intro!: integrable_bound[OF add.prems])
  4.1615 +    with add show ?case
  4.1616 +      by (simp add: nn_integral_add)
  4.1617 +  next
  4.1618 +    case (seq U)
  4.1619 +    show ?case
  4.1620 +    proof (rule LIMSEQ_le_const)
  4.1621 +      have U_le: "x \<in> space M \<Longrightarrow> U i x \<le> max 0 (f x)" for x i
  4.1622 +        using seq by (intro incseq_le) (auto simp: incseq_def le_fun_def)
  4.1623 +      with seq nonneg show "(\<lambda>i. integral\<^sup>L M (U i)) \<longlonglongrightarrow> LINT x|M. max 0 (f x)"
  4.1624 +        by (intro integral_dominated_convergence) auto
  4.1625 +      have "integrable M (U i)" for i
  4.1626 +        using seq.prems by (rule integrable_bound) (insert U_le seq, auto)
  4.1627 +      with seq show "\<exists>N. \<forall>n\<ge>N. 0 \<le> integral\<^sup>L M (U n)"
  4.1628 +        by auto
  4.1629 +    qed
  4.1630 +  qed (auto simp: measure_nonneg integrable_mult_left_iff)
  4.1631 +  also have "\<dots> = integral\<^sup>L M f"
  4.1632 +    using nonneg by (auto intro!: integral_cong_AE)
  4.1633 +  finally show ?thesis .
  4.1634 +qed (simp add: not_integrable_integral_eq)
  4.1635 +
  4.1636 +lemma integral_nonneg[simp]:
  4.1637 +  fixes f :: "'a \<Rightarrow> real"
  4.1638 +  shows "(\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> integral\<^sup>L M f"
  4.1639 +  by (intro integral_nonneg_AE) auto
  4.1640 +
  4.1641 +lemma nn_integral_eq_integral:
  4.1642 +  assumes f: "integrable M f"
  4.1643 +  assumes nonneg: "AE x in M. 0 \<le> f x"
  4.1644 +  shows "(\<integral>\<^sup>+ x. f x \<partial>M) = integral\<^sup>L M f"
  4.1645 +proof -
  4.1646 +  { fix f :: "'a \<Rightarrow> real" assume f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" "integrable M f"
  4.1647 +    then have "(\<integral>\<^sup>+ x. f x \<partial>M) = integral\<^sup>L M f"
  4.1648 +    proof (induct rule: borel_measurable_induct_real)
  4.1649 +      case (set A) then show ?case
  4.1650 +        by (simp add: integrable_indicator_iff ennreal_indicator emeasure_eq_ennreal_measure)
  4.1651 +    next
  4.1652 +      case (mult f c) then show ?case
  4.1653 +        by (auto simp add: integrable_mult_left_iff nn_integral_cmult ennreal_mult integral_nonneg_AE)
  4.1654 +    next
  4.1655 +      case (add g f)
  4.1656 +      then have "integrable M f" "integrable M g"
  4.1657 +        by (auto intro!: integrable_bound[OF add.prems])
  4.1658 +      with add show ?case
  4.1659 +        by (simp add: nn_integral_add integral_nonneg_AE)
  4.1660 +    next
  4.1661 +      case (seq U)
  4.1662 +      show ?case
  4.1663 +      proof (rule LIMSEQ_unique)
  4.1664 +        have U_le_f: "x \<in> space M \<Longrightarrow> U i x \<le> f x" for x i
  4.1665 +          using seq by (intro incseq_le) (auto simp: incseq_def le_fun_def)
  4.1666 +        have int_U: "\<And>i. integrable M (U i)"
  4.1667 +          using seq f U_le_f by (intro integrable_bound[OF f(3)]) auto
  4.1668 +        from U_le_f seq have "(\<lambda>i. integral\<^sup>L M (U i)) \<longlonglongrightarrow> integral\<^sup>L M f"
  4.1669 +          by (intro integral_dominated_convergence) auto
  4.1670 +        then show "(\<lambda>i. ennreal (integral\<^sup>L M (U i))) \<longlonglongrightarrow> ennreal (integral\<^sup>L M f)"
  4.1671 +          using seq f int_U by (simp add: f integral_nonneg_AE)
  4.1672 +        have "(\<lambda>i. \<integral>\<^sup>+ x. U i x \<partial>M) \<longlonglongrightarrow> \<integral>\<^sup>+ x. f x \<partial>M"
  4.1673 +          using seq U_le_f f
  4.1674 +          by (intro nn_integral_dominated_convergence[where w=f]) (auto simp: integrable_iff_bounded)
  4.1675 +        then show "(\<lambda>i. \<integral>x. U i x \<partial>M) \<longlonglongrightarrow> \<integral>\<^sup>+x. f x \<partial>M"
  4.1676 +          using seq int_U by simp
  4.1677 +      qed
  4.1678 +    qed }
  4.1679 +  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))"
  4.1680 +    by simp
  4.1681 +  also have "\<dots> = integral\<^sup>L M f"
  4.1682 +    using assms by (auto intro!: integral_cong_AE simp: integral_nonneg_AE)
  4.1683 +  also have "(\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) = (\<integral>\<^sup>+ x. f x \<partial>M)"
  4.1684 +    using assms by (auto intro!: nn_integral_cong_AE simp: max_def)
  4.1685 +  finally show ?thesis .
  4.1686 +qed
  4.1687 +
  4.1688 +lemma
  4.1689 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> 'a :: {banach, second_countable_topology}"
  4.1690 +  assumes integrable[measurable]: "\<And>i. integrable M (f i)"
  4.1691 +  and summable: "AE x in M. summable (\<lambda>i. norm (f i x))"
  4.1692 +  and sums: "summable (\<lambda>i. (\<integral>x. norm (f i x) \<partial>M))"
  4.1693 +  shows integrable_suminf: "integrable M (\<lambda>x. (\<Sum>i. f i x))" (is "integrable M ?S")
  4.1694 +    and sums_integral: "(\<lambda>i. integral\<^sup>L M (f i)) sums (\<integral>x. (\<Sum>i. f i x) \<partial>M)" (is "?f sums ?x")
  4.1695 +    and integral_suminf: "(\<integral>x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^sup>L M (f i))"
  4.1696 +    and summable_integral: "summable (\<lambda>i. integral\<^sup>L M (f i))"
  4.1697 +proof -
  4.1698 +  have 1: "integrable M (\<lambda>x. \<Sum>i. norm (f i x))"
  4.1699 +  proof (rule integrableI_bounded)
  4.1700 +    have "(\<integral>\<^sup>+ x. ennreal (norm (\<Sum>i. norm (f i x))) \<partial>M) = (\<integral>\<^sup>+ x. (\<Sum>i. ennreal (norm (f i x))) \<partial>M)"
  4.1701 +      apply (intro nn_integral_cong_AE)
  4.1702 +      using summable
  4.1703 +      apply eventually_elim
  4.1704 +      apply (simp add: suminf_nonneg ennreal_suminf_neq_top)
  4.1705 +      done
  4.1706 +    also have "\<dots> = (\<Sum>i. \<integral>\<^sup>+ x. norm (f i x) \<partial>M)"
  4.1707 +      by (intro nn_integral_suminf) auto
  4.1708 +    also have "\<dots> = (\<Sum>i. ennreal (\<integral>x. norm (f i x) \<partial>M))"
  4.1709 +      by (intro arg_cong[where f=suminf] ext nn_integral_eq_integral integrable_norm integrable) auto
  4.1710 +    finally show "(\<integral>\<^sup>+ x. ennreal (norm (\<Sum>i. norm (f i x))) \<partial>M) < \<infinity>"
  4.1711 +      by (simp add: sums ennreal_suminf_neq_top less_top[symmetric] integral_nonneg_AE)
  4.1712 +  qed simp
  4.1713 +
  4.1714 +  have 2: "AE x in M. (\<lambda>n. \<Sum>i<n. f i x) \<longlonglongrightarrow> (\<Sum>i. f i x)"
  4.1715 +    using summable by eventually_elim (auto intro: summable_LIMSEQ summable_norm_cancel)
  4.1716 +
  4.1717 +  have 3: "\<And>j. AE x in M. norm (\<Sum>i<j. f i x) \<le> (\<Sum>i. norm (f i x))"
  4.1718 +    using summable
  4.1719 +  proof eventually_elim
  4.1720 +    fix j x assume [simp]: "summable (\<lambda>i. norm (f i x))"
  4.1721 +    have "norm (\<Sum>i<j. f i x) \<le> (\<Sum>i<j. norm (f i x))" by (rule norm_setsum)
  4.1722 +    also have "\<dots> \<le> (\<Sum>i. norm (f i x))"
  4.1723 +      using setsum_le_suminf[of "\<lambda>i. norm (f i x)"] unfolding sums_iff by auto
  4.1724 +    finally show "norm (\<Sum>i<j. f i x) \<le> (\<Sum>i. norm (f i x))" by simp
  4.1725 +  qed
  4.1726 +
  4.1727 +  note ibl = integrable_dominated_convergence[OF _ _ 1 2 3]
  4.1728 +  note int = integral_dominated_convergence[OF _ _ 1 2 3]
  4.1729 +
  4.1730 +  show "integrable M ?S"
  4.1731 +    by (rule ibl) measurable
  4.1732 +
  4.1733 +  show "?f sums ?x" unfolding sums_def
  4.1734 +    using int by (simp add: integrable)
  4.1735 +  then show "?x = suminf ?f" "summable ?f"
  4.1736 +    unfolding sums_iff by auto
  4.1737 +qed
  4.1738 +
  4.1739 +lemma integral_norm_bound:
  4.1740 +  fixes f :: "_ \<Rightarrow> 'a :: {banach, second_countable_topology}"
  4.1741 +  shows "integrable M f \<Longrightarrow> norm (integral\<^sup>L M f) \<le> (\<integral>x. norm (f x) \<partial>M)"
  4.1742 +  using nn_integral_eq_integral[of M "\<lambda>x. norm (f x)"]
  4.1743 +  using integral_norm_bound_ennreal[of M f] by (simp add: integral_nonneg_AE)
  4.1744 +
  4.1745 +lemma integral_eq_nn_integral:
  4.1746 +  assumes [measurable]: "f \<in> borel_measurable M"
  4.1747 +  assumes nonneg: "AE x in M. 0 \<le> f x"
  4.1748 +  shows "integral\<^sup>L M f = enn2real (\<integral>\<^sup>+ x. ennreal (f x) \<partial>M)"
  4.1749 +proof cases
  4.1750 +  assume *: "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) = \<infinity>"
  4.1751 +  also have "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) = (\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M)"
  4.1752 +    using nonneg by (intro nn_integral_cong_AE) auto
  4.1753 +  finally have "\<not> integrable M f"
  4.1754 +    by (auto simp: integrable_iff_bounded)
  4.1755 +  then show ?thesis
  4.1756 +    by (simp add: * not_integrable_integral_eq)
  4.1757 +next
  4.1758 +  assume "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) \<noteq> \<infinity>"
  4.1759 +  then have "integrable M f"
  4.1760 +    by (cases "\<integral>\<^sup>+ x. ennreal (f x) \<partial>M" rule: ennreal_cases)
  4.1761 +       (auto intro!: integrableI_nn_integral_finite assms)
  4.1762 +  from nn_integral_eq_integral[OF this] nonneg show ?thesis
  4.1763 +    by (simp add: integral_nonneg_AE)
  4.1764 +qed
  4.1765 +
  4.1766 +lemma enn2real_nn_integral_eq_integral:
  4.1767 +  assumes eq: "AE x in M. f x = ennreal (g x)" and nn: "AE x in M. 0 \<le> g x"
  4.1768 +    and fin: "(\<integral>\<^sup>+x. f x \<partial>M) < top"
  4.1769 +    and [measurable]: "g \<in> M \<rightarrow>\<^sub>M borel"
  4.1770 +  shows "enn2real (\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>x. g x \<partial>M)"
  4.1771 +proof -
  4.1772 +  have "ennreal (enn2real (\<integral>\<^sup>+x. f x \<partial>M)) = (\<integral>\<^sup>+x. f x \<partial>M)"
  4.1773 +    using fin by (intro ennreal_enn2real) auto
  4.1774 +  also have "\<dots> = (\<integral>\<^sup>+x. g x \<partial>M)"
  4.1775 +    using eq by (rule nn_integral_cong_AE)
  4.1776 +  also have "\<dots> = (\<integral>x. g x \<partial>M)"
  4.1777 +  proof (rule nn_integral_eq_integral)
  4.1778 +    show "integrable M g"
  4.1779 +    proof (rule integrableI_bounded)
  4.1780 +      have "(\<integral>\<^sup>+ x. ennreal (norm (g x)) \<partial>M) = (\<integral>\<^sup>+ x. f x \<partial>M)"
  4.1781 +        using eq nn by (auto intro!: nn_integral_cong_AE elim!: eventually_elim2)
  4.1782 +      also note fin
  4.1783 +      finally show "(\<integral>\<^sup>+ x. ennreal (norm (g x)) \<partial>M) < \<infinity>"
  4.1784 +        by simp
  4.1785 +    qed simp
  4.1786 +  qed fact
  4.1787 +  finally show ?thesis
  4.1788 +    using nn by (simp add: integral_nonneg_AE)
  4.1789 +qed
  4.1790 +
  4.1791 +lemma has_bochner_integral_nn_integral:
  4.1792 +  assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "0 \<le> x"
  4.1793 +  assumes "(\<integral>\<^sup>+x. f x \<partial>M) = ennreal x"
  4.1794 +  shows "has_bochner_integral M f x"
  4.1795 +  unfolding has_bochner_integral_iff
  4.1796 +  using assms by (auto simp: assms integral_eq_nn_integral intro: integrableI_nn_integral_finite)
  4.1797 +
  4.1798 +lemma integrableI_simple_bochner_integrable:
  4.1799 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1800 +  shows "simple_bochner_integrable M f \<Longrightarrow> integrable M f"
  4.1801 +  by (intro integrableI_sequence[where s="\<lambda>_. f"] borel_measurable_simple_function)
  4.1802 +     (auto simp: zero_ennreal_def[symmetric] simple_bochner_integrable.simps)
  4.1803 +
  4.1804 +lemma integrable_induct[consumes 1, case_names base add lim, induct pred: integrable]:
  4.1805 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1806 +  assumes "integrable M f"
  4.1807 +  assumes base: "\<And>A c. A \<in> sets M \<Longrightarrow> emeasure M A < \<infinity> \<Longrightarrow> P (\<lambda>x. indicator A x *\<^sub>R c)"
  4.1808 +  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)"
  4.1809 +  assumes lim: "\<And>f s. (\<And>i. integrable M (s i)) \<Longrightarrow> (\<And>i. P (s i)) \<Longrightarrow>
  4.1810 +   (\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) \<longlonglongrightarrow> f x) \<Longrightarrow>
  4.1811 +   (\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)) \<Longrightarrow> integrable M f \<Longrightarrow> P f"
  4.1812 +  shows "P f"
  4.1813 +proof -
  4.1814 +  from \<open>integrable M f\<close> have f: "f \<in> borel_measurable M" "(\<integral>\<^sup>+x. norm (f x) \<partial>M) < \<infinity>"
  4.1815 +    unfolding integrable_iff_bounded by auto
  4.1816 +  from borel_measurable_implies_sequence_metric[OF f(1)]
  4.1817 +  obtain s where s: "\<And>i. simple_function M (s i)" "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. s i x) \<longlonglongrightarrow> f x"
  4.1818 +    "\<And>i x. x \<in> space M \<Longrightarrow> norm (s i x) \<le> 2 * norm (f x)"
  4.1819 +    unfolding norm_conv_dist by metis
  4.1820 +
  4.1821 +  { fix f A
  4.1822 +    have [simp]: "P (\<lambda>x. 0)"
  4.1823 +      using base[of "{}" undefined] by simp
  4.1824 +    have "(\<And>i::'b. i \<in> A \<Longrightarrow> integrable M (f i::'a \<Rightarrow> 'b)) \<Longrightarrow>
  4.1825 +    (\<And>i. i \<in> A \<Longrightarrow> P (f i)) \<Longrightarrow> P (\<lambda>x. \<Sum>i\<in>A. f i x)"
  4.1826 +    by (induct A rule: infinite_finite_induct) (auto intro!: add) }
  4.1827 +  note setsum = this
  4.1828 +
  4.1829 +  define s' where [abs_def]: "s' i z = indicator (space M) z *\<^sub>R s i z" for i z
  4.1830 +  then have s'_eq_s: "\<And>i x. x \<in> space M \<Longrightarrow> s' i x = s i x"
  4.1831 +    by simp
  4.1832 +
  4.1833 +  have sf[measurable]: "\<And>i. simple_function M (s' i)"
  4.1834 +    unfolding s'_def using s(1)
  4.1835 +    by (intro simple_function_compose2[where h="op *\<^sub>R"] simple_function_indicator) auto
  4.1836 +
  4.1837 +  { fix i
  4.1838 +    have "\<And>z. {y. s' i z = y \<and> y \<in> s' i ` space M \<and> y \<noteq> 0 \<and> z \<in> space M} =
  4.1839 +        (if z \<in> space M \<and> s' i z \<noteq> 0 then {s' i z} else {})"
  4.1840 +      by (auto simp add: s'_def split: split_indicator)
  4.1841 +    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)"
  4.1842 +      using sf by (auto simp: fun_eq_iff simple_function_def s'_def) }
  4.1843 +  note s'_eq = this
  4.1844 +
  4.1845 +  show "P f"
  4.1846 +  proof (rule lim)
  4.1847 +    fix i
  4.1848 +
  4.1849 +    have "(\<integral>\<^sup>+x. norm (s' i x) \<partial>M) \<le> (\<integral>\<^sup>+x. ennreal (2 * norm (f x)) \<partial>M)"
  4.1850 +      using s by (intro nn_integral_mono) (auto simp: s'_eq_s)
  4.1851 +    also have "\<dots> < \<infinity>"
  4.1852 +      using f by (simp add: nn_integral_cmult ennreal_mult_less_top ennreal_mult)
  4.1853 +    finally have sbi: "simple_bochner_integrable M (s' i)"
  4.1854 +      using sf by (intro simple_bochner_integrableI_bounded) auto
  4.1855 +    then show "integrable M (s' i)"
  4.1856 +      by (rule integrableI_simple_bochner_integrable)
  4.1857 +
  4.1858 +    { fix x assume"x \<in> space M" "s' i x \<noteq> 0"
  4.1859 +      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}"
  4.1860 +        by (intro emeasure_mono) auto
  4.1861 +      also have "\<dots> < \<infinity>"
  4.1862 +        using sbi by (auto elim: simple_bochner_integrable.cases simp: less_top)
  4.1863 +      finally have "emeasure M {y \<in> space M. s' i y = s' i x} \<noteq> \<infinity>" by simp }
  4.1864 +    then show "P (s' i)"
  4.1865 +      by (subst s'_eq) (auto intro!: setsum base simp: less_top)
  4.1866 +
  4.1867 +    fix x assume "x \<in> space M" with s show "(\<lambda>i. s' i x) \<longlonglongrightarrow> f x"
  4.1868 +      by (simp add: s'_eq_s)
  4.1869 +    show "norm (s' i x) \<le> 2 * norm (f x)"
  4.1870 +      using \<open>x \<in> space M\<close> s by (simp add: s'_eq_s)
  4.1871 +  qed fact
  4.1872 +qed
  4.1873 +
  4.1874 +lemma integral_eq_zero_AE:
  4.1875 +  "(AE x in M. f x = 0) \<Longrightarrow> integral\<^sup>L M f = 0"
  4.1876 +  using integral_cong_AE[of f M "\<lambda>_. 0"]
  4.1877 +  by (cases "integrable M f") (simp_all add: not_integrable_integral_eq)
  4.1878 +
  4.1879 +lemma integral_nonneg_eq_0_iff_AE:
  4.1880 +  fixes f :: "_ \<Rightarrow> real"
  4.1881 +  assumes f[measurable]: "integrable M f" and nonneg: "AE x in M. 0 \<le> f x"
  4.1882 +  shows "integral\<^sup>L M f = 0 \<longleftrightarrow> (AE x in M. f x = 0)"
  4.1883 +proof
  4.1884 +  assume "integral\<^sup>L M f = 0"
  4.1885 +  then have "integral\<^sup>N M f = 0"
  4.1886 +    using nn_integral_eq_integral[OF f nonneg] by simp
  4.1887 +  then have "AE x in M. ennreal (f x) \<le> 0"
  4.1888 +    by (simp add: nn_integral_0_iff_AE)
  4.1889 +  with nonneg show "AE x in M. f x = 0"
  4.1890 +    by auto
  4.1891 +qed (auto simp add: integral_eq_zero_AE)
  4.1892 +
  4.1893 +lemma integral_mono_AE:
  4.1894 +  fixes f :: "'a \<Rightarrow> real"
  4.1895 +  assumes "integrable M f" "integrable M g" "AE x in M. f x \<le> g x"
  4.1896 +  shows "integral\<^sup>L M f \<le> integral\<^sup>L M g"
  4.1897 +proof -
  4.1898 +  have "0 \<le> integral\<^sup>L M (\<lambda>x. g x - f x)"
  4.1899 +    using assms by (intro integral_nonneg_AE integrable_diff assms) auto
  4.1900 +  also have "\<dots> = integral\<^sup>L M g - integral\<^sup>L M f"
  4.1901 +    by (intro integral_diff assms)
  4.1902 +  finally show ?thesis by simp
  4.1903 +qed
  4.1904 +
  4.1905 +lemma integral_mono:
  4.1906 +  fixes f :: "'a \<Rightarrow> real"
  4.1907 +  shows "integrable M f \<Longrightarrow> integrable M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x \<le> g x) \<Longrightarrow>
  4.1908 +    integral\<^sup>L M f \<le> integral\<^sup>L M g"
  4.1909 +  by (intro integral_mono_AE) auto
  4.1910 +
  4.1911 +lemma (in finite_measure) integrable_measure:
  4.1912 +  assumes I: "disjoint_family_on X I" "countable I"
  4.1913 +  shows "integrable (count_space I) (\<lambda>i. measure M (X i))"
  4.1914 +proof -
  4.1915 +  have "(\<integral>\<^sup>+i. measure M (X i) \<partial>count_space I) = (\<integral>\<^sup>+i. measure M (if X i \<in> sets M then X i else {}) \<partial>count_space I)"
  4.1916 +    by (auto intro!: nn_integral_cong measure_notin_sets)
  4.1917 +  also have "\<dots> = measure M (\<Union>i\<in>I. if X i \<in> sets M then X i else {})"
  4.1918 +    using I unfolding emeasure_eq_measure[symmetric]
  4.1919 +    by (subst emeasure_UN_countable) (auto simp: disjoint_family_on_def)
  4.1920 +  finally show ?thesis
  4.1921 +    by (auto intro!: integrableI_bounded)
  4.1922 +qed
  4.1923 +
  4.1924 +lemma integrableI_real_bounded:
  4.1925 +  assumes f: "f \<in> borel_measurable M" and ae: "AE x in M. 0 \<le> f x" and fin: "integral\<^sup>N M f < \<infinity>"
  4.1926 +  shows "integrable M f"
  4.1927 +proof (rule integrableI_bounded)
  4.1928 +  have "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) = \<integral>\<^sup>+ x. ennreal (f x) \<partial>M"
  4.1929 +    using ae by (auto intro: nn_integral_cong_AE)
  4.1930 +  also note fin
  4.1931 +  finally show "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>" .
  4.1932 +qed fact
  4.1933 +
  4.1934 +lemma integral_real_bounded:
  4.1935 +  assumes "0 \<le> r" "integral\<^sup>N M f \<le> ennreal r"
  4.1936 +  shows "integral\<^sup>L M f \<le> r"
  4.1937 +proof cases
  4.1938 +  assume [simp]: "integrable M f"
  4.1939 +
  4.1940 +  have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) = integral\<^sup>N M (\<lambda>x. max 0 (f x))"
  4.1941 +    by (intro nn_integral_eq_integral[symmetric]) auto
  4.1942 +  also have "\<dots> = integral\<^sup>N M f"
  4.1943 +    by (intro nn_integral_cong) (simp add: max_def ennreal_neg)
  4.1944 +  also have "\<dots> \<le> r"
  4.1945 +    by fact
  4.1946 +  finally have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) \<le> r"
  4.1947 +    using \<open>0 \<le> r\<close> by simp
  4.1948 +
  4.1949 +  moreover have "integral\<^sup>L M f \<le> integral\<^sup>L M (\<lambda>x. max 0 (f x))"
  4.1950 +    by (rule integral_mono_AE) auto
  4.1951 +  ultimately show ?thesis
  4.1952 +    by simp
  4.1953 +next
  4.1954 +  assume "\<not> integrable M f" then show ?thesis
  4.1955 +    using \<open>0 \<le> r\<close> by (simp add: not_integrable_integral_eq)
  4.1956 +qed
  4.1957 +
  4.1958 +subsection \<open>Restricted measure spaces\<close>
  4.1959 +
  4.1960 +lemma integrable_restrict_space:
  4.1961 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1962 +  assumes \<Omega>[simp]: "\<Omega> \<inter> space M \<in> sets M"
  4.1963 +  shows "integrable (restrict_space M \<Omega>) f \<longleftrightarrow> integrable M (\<lambda>x. indicator \<Omega> x *\<^sub>R f x)"
  4.1964 +  unfolding integrable_iff_bounded
  4.1965 +    borel_measurable_restrict_space_iff[OF \<Omega>]
  4.1966 +    nn_integral_restrict_space[OF \<Omega>]
  4.1967 +  by (simp add: ac_simps ennreal_indicator ennreal_mult)
  4.1968 +
  4.1969 +lemma integral_restrict_space:
  4.1970 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.1971 +  assumes \<Omega>[simp]: "\<Omega> \<inter> space M \<in> sets M"
  4.1972 +  shows "integral\<^sup>L (restrict_space M \<Omega>) f = integral\<^sup>L M (\<lambda>x. indicator \<Omega> x *\<^sub>R f x)"
  4.1973 +proof (rule integral_eq_cases)
  4.1974 +  assume "integrable (restrict_space M \<Omega>) f"
  4.1975 +  then show ?thesis
  4.1976 +  proof induct
  4.1977 +    case (base A c) then show ?case
  4.1978 +      by (simp add: indicator_inter_arith[symmetric] sets_restrict_space_iff
  4.1979 +                    emeasure_restrict_space Int_absorb1 measure_restrict_space)
  4.1980 +  next
  4.1981 +    case (add g f) then show ?case
  4.1982 +      by (simp add: scaleR_add_right integrable_restrict_space)
  4.1983 +  next
  4.1984 +    case (lim f s)
  4.1985 +    show ?case
  4.1986 +    proof (rule LIMSEQ_unique)
  4.1987 +      show "(\<lambda>i. integral\<^sup>L (restrict_space M \<Omega>) (s i)) \<longlonglongrightarrow> integral\<^sup>L (restrict_space M \<Omega>) f"
  4.1988 +        using lim by (intro integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"]) simp_all
  4.1989 +
  4.1990 +      show "(\<lambda>i. integral\<^sup>L (restrict_space M \<Omega>) (s i)) \<longlonglongrightarrow> (\<integral> x. indicator \<Omega> x *\<^sub>R f x \<partial>M)"
  4.1991 +        unfolding lim
  4.1992 +        using lim
  4.1993 +        by (intro integral_dominated_convergence[where w="\<lambda>x. 2 * norm (indicator \<Omega> x *\<^sub>R f x)"])
  4.1994 +           (auto simp add: space_restrict_space integrable_restrict_space simp del: norm_scaleR
  4.1995 +                 split: split_indicator)
  4.1996 +    qed
  4.1997 +  qed
  4.1998 +qed (simp add: integrable_restrict_space)
  4.1999 +
  4.2000 +lemma integral_empty:
  4.2001 +  assumes "space M = {}"
  4.2002 +  shows "integral\<^sup>L M f = 0"
  4.2003 +proof -
  4.2004 +  have "(\<integral> x. f x \<partial>M) = (\<integral> x. 0 \<partial>M)"
  4.2005 +    by(rule integral_cong)(simp_all add: assms)
  4.2006 +  thus ?thesis by simp
  4.2007 +qed
  4.2008 +
  4.2009 +subsection \<open>Measure spaces with an associated density\<close>
  4.2010 +
  4.2011 +lemma integrable_density:
  4.2012 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  4.2013 +  assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  4.2014 +    and nn: "AE x in M. 0 \<le> g x"
  4.2015 +  shows "integrable (density M g) f \<longleftrightarrow> integrable M (\<lambda>x. g x *\<^sub>R f x)"
  4.2016 +  unfolding integrable_iff_bounded using nn
  4.2017 +  apply (simp add: nn_integral_density less_top[symmetric])
  4.2018 +  apply (intro arg_cong2[where f="op ="] refl nn_integral_cong_AE)
  4.2019 +  apply (auto simp: ennreal_mult)
  4.2020 +  done
  4.2021 +
  4.2022 +lemma integral_density:
  4.2023 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  4.2024 +  assumes f: "f \<in> borel_measurable M"
  4.2025 +    and g[measurable]: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
  4.2026 +  shows "integral\<^sup>L (density M g) f = integral\<^sup>L M (\<lambda>x. g x *\<^sub>R f x)"
  4.2027 +proof (rule integral_eq_cases)
  4.2028 +  assume "integrable (density M g) f"
  4.2029 +  then show ?thesis
  4.2030 +  proof induct
  4.2031 +    case (base A c)
  4.2032 +    then have [measurable]: "A \<in> sets M" by auto
  4.2033 +
  4.2034 +    have int: "integrable M (\<lambda>x. g x * indicator A x)"
  4.2035 +      using g base integrable_density[of "indicator A :: 'a \<Rightarrow> real" M g] by simp
  4.2036 +    then have "integral\<^sup>L M (\<lambda>x. g x * indicator A x) = (\<integral>\<^sup>+ x. ennreal (g x * indicator A x) \<partial>M)"
  4.2037 +      using g by (subst nn_integral_eq_integral) auto
  4.2038 +    also have "\<dots> = (\<integral>\<^sup>+ x. ennreal (g x) * indicator A x \<partial>M)"
  4.2039 +      by (intro nn_integral_cong) (auto split: split_indicator)
  4.2040 +    also have "\<dots> = emeasure (density M g) A"
  4.2041 +      by (rule emeasure_density[symmetric]) auto
  4.2042 +    also have "\<dots> = ennreal (measure (density M g) A)"
  4.2043 +      using base by (auto intro: emeasure_eq_ennreal_measure)
  4.2044 +    also have "\<dots> = integral\<^sup>L (density M g) (indicator A)"
  4.2045 +      using base by simp
  4.2046 +    finally show ?case
  4.2047 +      using base g
  4.2048 +      apply (simp add: int integral_nonneg_AE)
  4.2049 +      apply (subst (asm) ennreal_inj)
  4.2050 +      apply (auto intro!: integral_nonneg_AE)
  4.2051 +      done
  4.2052 +  next
  4.2053 +    case (add f h)
  4.2054 +    then have [measurable]: "f \<in> borel_measurable M" "h \<in> borel_measurable M"
  4.2055 +      by (auto dest!: borel_measurable_integrable)
  4.2056 +    from add g show ?case
  4.2057 +      by (simp add: scaleR_add_right integrable_density)
  4.2058 +  next
  4.2059 +    case (lim f s)
  4.2060 +    have [measurable]: "f \<in> borel_measurable M" "\<And>i. s i \<in> borel_measurable M"
  4.2061 +      using lim(1,5)[THEN borel_measurable_integrable] by auto
  4.2062 +
  4.2063 +    show ?case
  4.2064 +    proof (rule LIMSEQ_unique)
  4.2065 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. g x *\<^sub>R s i x)) \<longlonglongrightarrow> integral\<^sup>L M (\<lambda>x. g x *\<^sub>R f x)"
  4.2066 +      proof (rule integral_dominated_convergence)
  4.2067 +        show "integrable M (\<lambda>x. 2 * norm (g x *\<^sub>R f x))"
  4.2068 +          by (intro integrable_mult_right integrable_norm integrable_density[THEN iffD1] lim g) auto
  4.2069 +        show "AE x in M. (\<lambda>i. g x *\<^sub>R s i x) \<longlonglongrightarrow> g x *\<^sub>R f x"
  4.2070 +          using lim(3) by (auto intro!: tendsto_scaleR AE_I2[of M])
  4.2071 +        show "\<And>i. AE x in M. norm (g x *\<^sub>R s i x) \<le> 2 * norm (g x *\<^sub>R f x)"
  4.2072 +          using lim(4) g by (auto intro!: AE_I2[of M] mult_left_mono simp: field_simps)
  4.2073 +      qed auto
  4.2074 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. g x *\<^sub>R s i x)) \<longlonglongrightarrow> integral\<^sup>L (density M g) f"
  4.2075 +        unfolding lim(2)[symmetric]
  4.2076 +        by (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  4.2077 +           (insert lim(3-5), auto)
  4.2078 +    qed
  4.2079 +  qed
  4.2080 +qed (simp add: f g integrable_density)
  4.2081 +
  4.2082 +lemma
  4.2083 +  fixes g :: "'a \<Rightarrow> real"
  4.2084 +  assumes "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x" "g \<in> borel_measurable M"
  4.2085 +  shows integral_real_density: "integral\<^sup>L (density M f) g = (\<integral> x. f x * g x \<partial>M)"
  4.2086 +    and integrable_real_density: "integrable (density M f) g \<longleftrightarrow> integrable M (\<lambda>x. f x * g x)"
  4.2087 +  using assms integral_density[of g M f] integrable_density[of g M f] by auto
  4.2088 +
  4.2089 +lemma has_bochner_integral_density:
  4.2090 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and g :: "'a \<Rightarrow> real"
  4.2091 +  shows "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (AE x in M. 0 \<le> g x) \<Longrightarrow>
  4.2092 +    has_bochner_integral M (\<lambda>x. g x *\<^sub>R f x) x \<Longrightarrow> has_bochner_integral (density M g) f x"
  4.2093 +  by (simp add: has_bochner_integral_iff integrable_density integral_density)
  4.2094 +
  4.2095 +subsection \<open>Distributions\<close>
  4.2096 +
  4.2097 +lemma integrable_distr_eq:
  4.2098 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.2099 +  assumes [measurable]: "g \<in> measurable M N" "f \<in> borel_measurable N"
  4.2100 +  shows "integrable (distr M N g) f \<longleftrightarrow> integrable M (\<lambda>x. f (g x))"
  4.2101 +  unfolding integrable_iff_bounded by (simp_all add: nn_integral_distr)
  4.2102 +
  4.2103 +lemma integrable_distr:
  4.2104 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.2105 +  shows "T \<in> measurable M M' \<Longrightarrow> integrable (distr M M' T) f \<Longrightarrow> integrable M (\<lambda>x. f (T x))"
  4.2106 +  by (subst integrable_distr_eq[symmetric, where g=T])
  4.2107 +     (auto dest: borel_measurable_integrable)
  4.2108 +
  4.2109 +lemma integral_distr:
  4.2110 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.2111 +  assumes g[measurable]: "g \<in> measurable M N" and f: "f \<in> borel_measurable N"
  4.2112 +  shows "integral\<^sup>L (distr M N g) f = integral\<^sup>L M (\<lambda>x. f (g x))"
  4.2113 +proof (rule integral_eq_cases)
  4.2114 +  assume "integrable (distr M N g) f"
  4.2115 +  then show ?thesis
  4.2116 +  proof induct
  4.2117 +    case (base A c)
  4.2118 +    then have [measurable]: "A \<in> sets N" by auto
  4.2119 +    from base have int: "integrable (distr M N g) (\<lambda>a. indicator A a *\<^sub>R c)"
  4.2120 +      by (intro integrable_indicator)
  4.2121 +
  4.2122 +    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"
  4.2123 +      using base by auto
  4.2124 +    also have "\<dots> = measure M (g -` A \<inter> space M) *\<^sub>R c"
  4.2125 +      by (subst measure_distr) auto
  4.2126 +    also have "\<dots> = integral\<^sup>L M (\<lambda>a. indicator (g -` A \<inter> space M) a *\<^sub>R c)"
  4.2127 +      using base by (auto simp: emeasure_distr)
  4.2128 +    also have "\<dots> = integral\<^sup>L M (\<lambda>a. indicator A (g a) *\<^sub>R c)"
  4.2129 +      using int base by (intro integral_cong_AE) (auto simp: emeasure_distr split: split_indicator)
  4.2130 +    finally show ?case .
  4.2131 +  next
  4.2132 +    case (add f h)
  4.2133 +    then have [measurable]: "f \<in> borel_measurable N" "h \<in> borel_measurable N"
  4.2134 +      by (auto dest!: borel_measurable_integrable)
  4.2135 +    from add g show ?case
  4.2136 +      by (simp add: scaleR_add_right integrable_distr_eq)
  4.2137 +  next
  4.2138 +    case (lim f s)
  4.2139 +    have [measurable]: "f \<in> borel_measurable N" "\<And>i. s i \<in> borel_measurable N"
  4.2140 +      using lim(1,5)[THEN borel_measurable_integrable] by auto
  4.2141 +
  4.2142 +    show ?case
  4.2143 +    proof (rule LIMSEQ_unique)
  4.2144 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. s i (g x))) \<longlonglongrightarrow> integral\<^sup>L M (\<lambda>x. f (g x))"
  4.2145 +      proof (rule integral_dominated_convergence)
  4.2146 +        show "integrable M (\<lambda>x. 2 * norm (f (g x)))"
  4.2147 +          using lim by (auto simp: integrable_distr_eq)
  4.2148 +        show "AE x in M. (\<lambda>i. s i (g x)) \<longlonglongrightarrow> f (g x)"
  4.2149 +          using lim(3) g[THEN measurable_space] by auto
  4.2150 +        show "\<And>i. AE x in M. norm (s i (g x)) \<le> 2 * norm (f (g x))"
  4.2151 +          using lim(4) g[THEN measurable_space] by auto
  4.2152 +      qed auto
  4.2153 +      show "(\<lambda>i. integral\<^sup>L M (\<lambda>x. s i (g x))) \<longlonglongrightarrow> integral\<^sup>L (distr M N g) f"
  4.2154 +        unfolding lim(2)[symmetric]
  4.2155 +        by (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  4.2156 +           (insert lim(3-5), auto)
  4.2157 +    qed
  4.2158 +  qed
  4.2159 +qed (simp add: f g integrable_distr_eq)
  4.2160 +
  4.2161 +lemma has_bochner_integral_distr:
  4.2162 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.2163 +  shows "f \<in> borel_measurable N \<Longrightarrow> g \<in> measurable M N \<Longrightarrow>
  4.2164 +    has_bochner_integral M (\<lambda>x. f (g x)) x \<Longrightarrow> has_bochner_integral (distr M N g) f x"
  4.2165 +  by (simp add: has_bochner_integral_iff integrable_distr_eq integral_distr)
  4.2166 +
  4.2167 +subsection \<open>Lebesgue integration on @{const count_space}\<close>
  4.2168 +
  4.2169 +lemma integrable_count_space:
  4.2170 +  fixes f :: "'a \<Rightarrow> 'b::{banach,second_countable_topology}"
  4.2171 +  shows "finite X \<Longrightarrow> integrable (count_space X) f"
  4.2172 +  by (auto simp: nn_integral_count_space integrable_iff_bounded)
  4.2173 +
  4.2174 +lemma measure_count_space[simp]:
  4.2175 +  "B \<subseteq> A \<Longrightarrow> finite B \<Longrightarrow> measure (count_space A) B = card B"
  4.2176 +  unfolding measure_def by (subst emeasure_count_space ) auto
  4.2177 +
  4.2178 +lemma lebesgue_integral_count_space_finite_support:
  4.2179 +  assumes f: "finite {a\<in>A. f a \<noteq> 0}"
  4.2180 +  shows "(\<integral>x. f x \<partial>count_space A) = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. f a)"
  4.2181 +proof -
  4.2182 +  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)"
  4.2183 +    by (intro setsum.mono_neutral_cong_left) auto
  4.2184 +
  4.2185 +  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)"
  4.2186 +    by (intro integral_cong refl) (simp add: f eq)
  4.2187 +  also have "\<dots> = (\<Sum>a | a \<in> A \<and> f a \<noteq> 0. measure (count_space A) {a} *\<^sub>R f a)"
  4.2188 +    by (subst integral_setsum) (auto intro!: setsum.cong)
  4.2189 +  finally show ?thesis
  4.2190 +    by auto
  4.2191 +qed
  4.2192 +
  4.2193 +lemma lebesgue_integral_count_space_finite: "finite A \<Longrightarrow> (\<integral>x. f x \<partial>count_space A) = (\<Sum>a\<in>A. f a)"
  4.2194 +  by (subst lebesgue_integral_count_space_finite_support)
  4.2195 +     (auto intro!: setsum.mono_neutral_cong_left)
  4.2196 +
  4.2197 +lemma integrable_count_space_nat_iff:
  4.2198 +  fixes f :: "nat \<Rightarrow> _::{banach,second_countable_topology}"
  4.2199 +  shows "integrable (count_space UNIV) f \<longleftrightarrow> summable (\<lambda>x. norm (f x))"
  4.2200 +  by (auto simp add: integrable_iff_bounded nn_integral_count_space_nat ennreal_suminf_neq_top
  4.2201 +           intro:  summable_suminf_not_top)
  4.2202 +
  4.2203 +lemma sums_integral_count_space_nat:
  4.2204 +  fixes f :: "nat \<Rightarrow> _::{banach,second_countable_topology}"
  4.2205 +  assumes *: "integrable (count_space UNIV) f"
  4.2206 +  shows "f sums (integral\<^sup>L (count_space UNIV) f)"
  4.2207 +proof -
  4.2208 +  let ?f = "\<lambda>n i. indicator {n} i *\<^sub>R f i"
  4.2209 +  have f': "\<And>n i. ?f n i = indicator {n} i *\<^sub>R f n"
  4.2210 +    by (auto simp: fun_eq_iff split: split_indicator)
  4.2211 +
  4.2212 +  have "(\<lambda>i. \<integral>n. ?f i n \<partial>count_space UNIV) sums \<integral> n. (\<Sum>i. ?f i n) \<partial>count_space UNIV"
  4.2213 +  proof (rule sums_integral)
  4.2214 +    show "\<And>i. integrable (count_space UNIV) (?f i)"
  4.2215 +      using * by (intro integrable_mult_indicator) auto
  4.2216 +    show "AE n in count_space UNIV. summable (\<lambda>i. norm (?f i n))"
  4.2217 +      using summable_finite[of "{n}" "\<lambda>i. norm (?f i n)" for n] by simp
  4.2218 +    show "summable (\<lambda>i. \<integral> n. norm (?f i n) \<partial>count_space UNIV)"
  4.2219 +      using * by (subst f') (simp add: integrable_count_space_nat_iff)
  4.2220 +  qed
  4.2221 +  also have "(\<integral> n. (\<Sum>i. ?f i n) \<partial>count_space UNIV) = (\<integral>n. f n \<partial>count_space UNIV)"
  4.2222 +    using suminf_finite[of "{n}" "\<lambda>i. ?f i n" for n] by (auto intro!: integral_cong)
  4.2223 +  also have "(\<lambda>i. \<integral>n. ?f i n \<partial>count_space UNIV) = f"
  4.2224 +    by (subst f') simp
  4.2225 +  finally show ?thesis .
  4.2226 +qed
  4.2227 +
  4.2228 +lemma integral_count_space_nat:
  4.2229 +  fixes f :: "nat \<Rightarrow> _::{banach,second_countable_topology}"
  4.2230 +  shows "integrable (count_space UNIV) f \<Longrightarrow> integral\<^sup>L (count_space UNIV) f = (\<Sum>x. f x)"
  4.2231 +  using sums_integral_count_space_nat by (rule sums_unique)
  4.2232 +
  4.2233 +subsection \<open>Point measure\<close>
  4.2234 +
  4.2235 +lemma lebesgue_integral_point_measure_finite:
  4.2236 +  fixes g :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.2237 +  shows "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow>
  4.2238 +    integral\<^sup>L (point_measure A f) g = (\<Sum>a\<in>A. f a *\<^sub>R g a)"
  4.2239 +  by (simp add: lebesgue_integral_count_space_finite AE_count_space integral_density point_measure_def)
  4.2240 +
  4.2241 +lemma integrable_point_measure_finite:
  4.2242 +  fixes g :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}" and f :: "'a \<Rightarrow> real"
  4.2243 +  shows "finite A \<Longrightarrow> integrable (point_measure A f) g"
  4.2244 +  unfolding point_measure_def
  4.2245 +  apply (subst density_cong[where f'="\<lambda>x. ennreal (max 0 (f x))"])
  4.2246 +  apply (auto split: split_max simp: ennreal_neg)
  4.2247 +  apply (subst integrable_density)
  4.2248 +  apply (auto simp: AE_count_space integrable_count_space)
  4.2249 +  done
  4.2250 +
  4.2251 +subsection \<open>Lebesgue integration on @{const null_measure}\<close>
  4.2252 +
  4.2253 +lemma has_bochner_integral_null_measure_iff[iff]:
  4.2254 +  "has_bochner_integral (null_measure M) f 0 \<longleftrightarrow> f \<in> borel_measurable M"
  4.2255 +  by (auto simp add: has_bochner_integral.simps simple_bochner_integral_def[abs_def]
  4.2256 +           intro!: exI[of _ "\<lambda>n x. 0"] simple_bochner_integrable.intros)
  4.2257 +
  4.2258 +lemma integrable_null_measure_iff[iff]: "integrable (null_measure M) f \<longleftrightarrow> f \<in> borel_measurable M"
  4.2259 +  by (auto simp add: integrable.simps)
  4.2260 +
  4.2261 +lemma integral_null_measure[simp]: "integral\<^sup>L (null_measure M) f = 0"
  4.2262 +  by (cases "integrable (null_measure M) f")
  4.2263 +     (auto simp add: not_integrable_integral_eq has_bochner_integral_integral_eq)
  4.2264 +
  4.2265 +subsection \<open>Legacy lemmas for the real-valued Lebesgue integral\<close>
  4.2266 +
  4.2267 +lemma real_lebesgue_integral_def:
  4.2268 +  assumes f[measurable]: "integrable M f"
  4.2269 +  shows "integral\<^sup>L M f = enn2real (\<integral>\<^sup>+x. f x \<partial>M) - enn2real (\<integral>\<^sup>+x. ennreal (- f x) \<partial>M)"
  4.2270 +proof -
  4.2271 +  have "integral\<^sup>L M f = integral\<^sup>L M (\<lambda>x. max 0 (f x) - max 0 (- f x))"
  4.2272 +    by (auto intro!: arg_cong[where f="integral\<^sup>L M"])
  4.2273 +  also have "\<dots> = integral\<^sup>L M (\<lambda>x. max 0 (f x)) - integral\<^sup>L M (\<lambda>x. max 0 (- f x))"
  4.2274 +    by (intro integral_diff integrable_max integrable_minus integrable_zero f)
  4.2275 +  also have "integral\<^sup>L M (\<lambda>x. max 0 (f x)) = enn2real (\<integral>\<^sup>+x. ennreal (f x) \<partial>M)"
  4.2276 +    by (subst integral_eq_nn_integral) (auto intro!: arg_cong[where f=enn2real] nn_integral_cong simp: max_def ennreal_neg)
  4.2277 +  also have "integral\<^sup>L M (\<lambda>x. max 0 (- f x)) = enn2real (\<integral>\<^sup>+x. ennreal (- f x) \<partial>M)"
  4.2278 +    by (subst integral_eq_nn_integral) (auto intro!: arg_cong[where f=enn2real] nn_integral_cong simp: max_def ennreal_neg)
  4.2279 +  finally show ?thesis .
  4.2280 +qed
  4.2281 +
  4.2282 +lemma real_integrable_def:
  4.2283 +  "integrable M f \<longleftrightarrow> f \<in> borel_measurable M \<and>
  4.2284 +    (\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) \<noteq> \<infinity> \<and> (\<integral>\<^sup>+ x. ennreal (- f x) \<partial>M) \<noteq> \<infinity>"
  4.2285 +  unfolding integrable_iff_bounded
  4.2286 +proof (safe del: notI)
  4.2287 +  assume *: "(\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M) < \<infinity>"
  4.2288 +  have "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M)"
  4.2289 +    by (intro nn_integral_mono) auto
  4.2290 +  also note *
  4.2291 +  finally show "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) \<noteq> \<infinity>"
  4.2292 +    by simp
  4.2293 +  have "(\<integral>\<^sup>+ x. ennreal (- f x) \<partial>M) \<le> (\<integral>\<^sup>+ x. ennreal (norm (f x)) \<partial>M)"
  4.2294 +    by (intro nn_integral_mono) auto
  4.2295 +  also note *
  4.2296 +  finally show "(\<integral>\<^sup>+ x. ennreal (- f x) \<partial>M) \<noteq> \<infinity>"
  4.2297 +    by simp
  4.2298 +next
  4.2299 +  assume [measurable]: "f \<in> borel_measurable M"
  4.2300 +  assume fin: "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ennreal (- f x) \<partial>M) \<noteq> \<infinity>"
  4.2301 +  have "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) = (\<integral>\<^sup>+ x. ennreal (f x) + ennreal (- f x) \<partial>M)"
  4.2302 +    by (intro nn_integral_cong) (auto simp: abs_real_def ennreal_neg)
  4.2303 +  also have"\<dots> = (\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) + (\<integral>\<^sup>+ x. ennreal (- f x) \<partial>M)"
  4.2304 +    by (intro nn_integral_add) auto
  4.2305 +  also have "\<dots> < \<infinity>"
  4.2306 +    using fin by (auto simp: less_top)
  4.2307 +  finally show "(\<integral>\<^sup>+ x. norm (f x) \<partial>M) < \<infinity>" .
  4.2308 +qed
  4.2309 +
  4.2310 +lemma integrableD[dest]:
  4.2311 +  assumes "integrable M f"
  4.2312 +  shows "f \<in> borel_measurable M" "(\<integral>\<^sup>+ x. ennreal (f x) \<partial>M) \<noteq> \<infinity>" "(\<integral>\<^sup>+ x. ennreal (- f x) \<partial>M) \<noteq> \<infinity>"
  4.2313 +  using assms unfolding real_integrable_def by auto
  4.2314 +
  4.2315 +lemma integrableE:
  4.2316 +  assumes "integrable M f"
  4.2317 +  obtains r q where
  4.2318 +    "(\<integral>\<^sup>+x. ennreal (f x)\<partial>M) = ennreal r"
  4.2319 +    "(\<integral>\<^sup>+x. ennreal (-f x)\<partial>M) = ennreal q"
  4.2320 +    "f \<in> borel_measurable M" "integral\<^sup>L M f = r - q"
  4.2321 +  using assms unfolding real_integrable_def real_lebesgue_integral_def[OF assms]
  4.2322 +  by (cases rule: ennreal2_cases[of "(\<integral>\<^sup>+x. ennreal (-f x)\<partial>M)" "(\<integral>\<^sup>+x. ennreal (f x)\<partial>M)"]) auto
  4.2323 +
  4.2324 +lemma integral_monotone_convergence_nonneg:
  4.2325 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  4.2326 +  assumes i: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  4.2327 +    and pos: "\<And>i. AE x in M. 0 \<le> f i x"
  4.2328 +    and lim: "AE x in M. (\<lambda>i. f i x) \<longlonglongrightarrow> u x"
  4.2329 +    and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) \<longlonglongrightarrow> x"
  4.2330 +    and u: "u \<in> borel_measurable M"
  4.2331 +  shows "integrable M u"
  4.2332 +  and "integral\<^sup>L M u = x"
  4.2333 +proof -
  4.2334 +  have nn: "AE x in M. \<forall>i. 0 \<le> f i x"
  4.2335 +    using pos unfolding AE_all_countable by auto
  4.2336 +  with lim have u_nn: "AE x in M. 0 \<le> u x"
  4.2337 +    by eventually_elim (auto intro: LIMSEQ_le_const)
  4.2338 +  have [simp]: "0 \<le> x"
  4.2339 +    by (intro LIMSEQ_le_const[OF ilim] allI exI impI integral_nonneg_AE pos)
  4.2340 +  have "(\<integral>\<^sup>+ x. ennreal (u x) \<partial>M) = (SUP n. (\<integral>\<^sup>+ x. ennreal (f n x) \<partial>M))"
  4.2341 +  proof (subst nn_integral_monotone_convergence_SUP_AE[symmetric])
  4.2342 +    fix i
  4.2343 +    from mono nn show "AE x in M. ennreal (f i x) \<le> ennreal (f (Suc i) x)"
  4.2344 +      by eventually_elim (auto simp: mono_def)
  4.2345 +    show "(\<lambda>x. ennreal (f i x)) \<in> borel_measurable M"
  4.2346 +      using i by auto
  4.2347 +  next
  4.2348 +    show "(\<integral>\<^sup>+ x. ennreal (u x) \<partial>M) = \<integral>\<^sup>+ x. (SUP i. ennreal (f i x)) \<partial>M"
  4.2349 +      apply (rule nn_integral_cong_AE)
  4.2350 +      using lim mono nn u_nn
  4.2351 +      apply eventually_elim
  4.2352 +      apply (simp add: LIMSEQ_unique[OF _ LIMSEQ_SUP] incseq_def)
  4.2353 +      done
  4.2354 +  qed
  4.2355 +  also have "\<dots> = ennreal x"
  4.2356 +    using mono i nn unfolding nn_integral_eq_integral[OF i pos]
  4.2357 +    by (subst LIMSEQ_unique[OF LIMSEQ_SUP]) (auto simp: mono_def integral_nonneg_AE pos intro!: integral_mono_AE ilim)
  4.2358 +  finally have "(\<integral>\<^sup>+ x. ennreal (u x) \<partial>M) = ennreal x" .
  4.2359 +  moreover have "(\<integral>\<^sup>+ x. ennreal (- u x) \<partial>M) = 0"
  4.2360 +    using u u_nn by (subst nn_integral_0_iff_AE) (auto simp add: ennreal_neg)
  4.2361 +  ultimately show "integrable M u" "integral\<^sup>L M u = x"
  4.2362 +    by (auto simp: real_integrable_def real_lebesgue_integral_def u)
  4.2363 +qed
  4.2364 +
  4.2365 +lemma
  4.2366 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  4.2367 +  assumes f: "\<And>i. integrable M (f i)" and mono: "AE x in M. mono (\<lambda>n. f n x)"
  4.2368 +  and lim: "AE x in M. (\<lambda>i. f i x) \<longlonglongrightarrow> u x"
  4.2369 +  and ilim: "(\<lambda>i. integral\<^sup>L M (f i)) \<longlonglongrightarrow> x"
  4.2370 +  and u: "u \<in> borel_measurable M"
  4.2371 +  shows integrable_monotone_convergence: "integrable M u"
  4.2372 +    and integral_monotone_convergence: "integral\<^sup>L M u = x"
  4.2373 +    and has_bochner_integral_monotone_convergence: "has_bochner_integral M u x"
  4.2374 +proof -
  4.2375 +  have 1: "\<And>i. integrable M (\<lambda>x. f i x - f 0 x)"
  4.2376 +    using f by auto
  4.2377 +  have 2: "AE x in M. mono (\<lambda>n. f n x - f 0 x)"
  4.2378 +    using mono by (auto simp: mono_def le_fun_def)
  4.2379 +  have 3: "\<And>n. AE x in M. 0 \<le> f n x - f 0 x"
  4.2380 +    using mono by (auto simp: field_simps mono_def le_fun_def)
  4.2381 +  have 4: "AE x in M. (\<lambda>i. f i x - f 0 x) \<longlonglongrightarrow> u x - f 0 x"
  4.2382 +    using lim by (auto intro!: tendsto_diff)
  4.2383 +  have 5: "(\<lambda>i. (\<integral>x. f i x - f 0 x \<partial>M)) \<longlonglongrightarrow> x - integral\<^sup>L M (f 0)"
  4.2384 +    using f ilim by (auto intro!: tendsto_diff)
  4.2385 +  have 6: "(\<lambda>x. u x - f 0 x) \<in> borel_measurable M"
  4.2386 +    using f[of 0] u by auto
  4.2387 +  note diff = integral_monotone_convergence_nonneg[OF 1 2 3 4 5 6]
  4.2388 +  have "integrable M (\<lambda>x. (u x - f 0 x) + f 0 x)"
  4.2389 +    using diff(1) f by (rule integrable_add)
  4.2390 +  with diff(2) f show "integrable M u" "integral\<^sup>L M u = x"
  4.2391 +    by auto
  4.2392 +  then show "has_bochner_integral M u x"
  4.2393 +    by (metis has_bochner_integral_integrable)
  4.2394 +qed
  4.2395 +
  4.2396 +lemma integral_norm_eq_0_iff:
  4.2397 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.2398 +  assumes f[measurable]: "integrable M f"
  4.2399 +  shows "(\<integral>x. norm (f x) \<partial>M) = 0 \<longleftrightarrow> emeasure M {x\<in>space M. f x \<noteq> 0} = 0"
  4.2400 +proof -
  4.2401 +  have "(\<integral>\<^sup>+x. norm (f x) \<partial>M) = (\<integral>x. norm (f x) \<partial>M)"
  4.2402 +    using f by (intro nn_integral_eq_integral integrable_norm) auto
  4.2403 +  then have "(\<integral>x. norm (f x) \<partial>M) = 0 \<longleftrightarrow> (\<integral>\<^sup>+x. norm (f x) \<partial>M) = 0"
  4.2404 +    by simp
  4.2405 +  also have "\<dots> \<longleftrightarrow> emeasure M {x\<in>space M. ennreal (norm (f x)) \<noteq> 0} = 0"
  4.2406 +    by (intro nn_integral_0_iff) auto
  4.2407 +  finally show ?thesis
  4.2408 +    by simp
  4.2409 +qed
  4.2410 +
  4.2411 +lemma integral_0_iff:
  4.2412 +  fixes f :: "'a \<Rightarrow> real"
  4.2413 +  shows "integrable M f \<Longrightarrow> (\<integral>x. \<bar>f x\<bar> \<partial>M) = 0 \<longleftrightarrow> emeasure M {x\<in>space M. f x \<noteq> 0} = 0"
  4.2414 +  using integral_norm_eq_0_iff[of M f] by simp
  4.2415 +
  4.2416 +lemma (in finite_measure) integrable_const[intro!, simp]: "integrable M (\<lambda>x. a)"
  4.2417 +  using integrable_indicator[of "space M" M a] by (simp cong: integrable_cong add: less_top[symmetric])
  4.2418 +
  4.2419 +lemma lebesgue_integral_const[simp]:
  4.2420 +  fixes a :: "'a :: {banach, second_countable_topology}"
  4.2421 +  shows "(\<integral>x. a \<partial>M) = measure M (space M) *\<^sub>R a"
  4.2422 +proof -
  4.2423 +  { assume "emeasure M (space M) = \<infinity>" "a \<noteq> 0"
  4.2424 +    then have ?thesis
  4.2425 +      by (auto simp add: not_integrable_integral_eq ennreal_mult_less_top measure_def integrable_iff_bounded) }
  4.2426 +  moreover
  4.2427 +  { assume "a = 0" then have ?thesis by simp }
  4.2428 +  moreover
  4.2429 +  { assume "emeasure M (space M) \<noteq> \<infinity>"
  4.2430 +    interpret finite_measure M
  4.2431 +      proof qed fact
  4.2432 +    have "(\<integral>x. a \<partial>M) = (\<integral>x. indicator (space M) x *\<^sub>R a \<partial>M)"
  4.2433 +      by (intro integral_cong) auto
  4.2434 +    also have "\<dots> = measure M (space M) *\<^sub>R a"
  4.2435 +      by (simp add: less_top[symmetric])
  4.2436 +    finally have ?thesis . }
  4.2437 +  ultimately show ?thesis by blast
  4.2438 +qed
  4.2439 +
  4.2440 +lemma (in finite_measure) integrable_const_bound:
  4.2441 +  fixes f :: "'a \<Rightarrow> 'b::{banach,second_countable_topology}"
  4.2442 +  shows "AE x in M. norm (f x) \<le> B \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> integrable M f"
  4.2443 +  apply (rule integrable_bound[OF integrable_const[of B], of f])
  4.2444 +  apply assumption
  4.2445 +  apply (cases "0 \<le> B")
  4.2446 +  apply auto
  4.2447 +  done
  4.2448 +
  4.2449 +lemma integral_indicator_finite_real:
  4.2450 +  fixes f :: "'a \<Rightarrow> real"
  4.2451 +  assumes [simp]: "finite A"
  4.2452 +  assumes [measurable]: "\<And>a. a \<in> A \<Longrightarrow> {a} \<in> sets M"
  4.2453 +  assumes finite: "\<And>a. a \<in> A \<Longrightarrow> emeasure M {a} < \<infinity>"
  4.2454 +  shows "(\<integral>x. f x * indicator A x \<partial>M) = (\<Sum>a\<in>A. f a * measure M {a})"
  4.2455 +proof -
  4.2456 +  have "(\<integral>x. f x * indicator A x \<partial>M) = (\<integral>x. (\<Sum>a\<in>A. f a * indicator {a} x) \<partial>M)"
  4.2457 +  proof (intro integral_cong refl)
  4.2458 +    fix x show "f x * indicator A x = (\<Sum>a\<in>A. f a * indicator {a} x)"
  4.2459 +      by (auto split: split_indicator simp: eq_commute[of x] cong: conj_cong)
  4.2460 +  qed
  4.2461 +  also have "\<dots> = (\<Sum>a\<in>A. f a * measure M {a})"
  4.2462 +    using finite by (subst integral_setsum) (auto simp add: integrable_mult_left_iff)
  4.2463 +  finally show ?thesis .
  4.2464 +qed
  4.2465 +
  4.2466 +lemma (in finite_measure) ennreal_integral_real:
  4.2467 +  assumes [measurable]: "f \<in> borel_measurable M"
  4.2468 +  assumes ae: "AE x in M. f x \<le> ennreal B" "0 \<le> B"
  4.2469 +  shows "ennreal (\<integral>x. enn2real (f x) \<partial>M) = (\<integral>\<^sup>+x. f x \<partial>M)"
  4.2470 +proof (subst nn_integral_eq_integral[symmetric])
  4.2471 +  show "integrable M (\<lambda>x. enn2real (f x))"
  4.2472 +    using ae by (intro integrable_const_bound[where B=B]) (auto simp: enn2real_leI enn2real_nonneg)
  4.2473 +  show "(\<integral>\<^sup>+ x. ennreal (enn2real (f x)) \<partial>M) = integral\<^sup>N M f"
  4.2474 +    using ae by (intro nn_integral_cong_AE) (auto simp: le_less_trans[OF _ ennreal_less_top])
  4.2475 +qed (auto simp: enn2real_nonneg)
  4.2476 +
  4.2477 +lemma (in finite_measure) integral_less_AE:
  4.2478 +  fixes X Y :: "'a \<Rightarrow> real"
  4.2479 +  assumes int: "integrable M X" "integrable M Y"
  4.2480 +  assumes A: "(emeasure M) A \<noteq> 0" "A \<in> sets M" "AE x in M. x \<in> A \<longrightarrow> X x \<noteq> Y x"
  4.2481 +  assumes gt: "AE x in M. X x \<le> Y x"
  4.2482 +  shows "integral\<^sup>L M X < integral\<^sup>L M Y"
  4.2483 +proof -
  4.2484 +  have "integral\<^sup>L M X \<le> integral\<^sup>L M Y"
  4.2485 +    using gt int by (intro integral_mono_AE) auto
  4.2486 +  moreover
  4.2487 +  have "integral\<^sup>L M X \<noteq> integral\<^sup>L M Y"
  4.2488 +  proof
  4.2489 +    assume eq: "integral\<^sup>L M X = integral\<^sup>L M Y"
  4.2490 +    have "integral\<^sup>L M (\<lambda>x. \<bar>Y x - X x\<bar>) = integral\<^sup>L M (\<lambda>x. Y x - X x)"
  4.2491 +      using gt int by (intro integral_cong_AE) auto
  4.2492 +    also have "\<dots> = 0"
  4.2493 +      using eq int by simp
  4.2494 +    finally have "(emeasure M) {x \<in> space M. Y x - X x \<noteq> 0} = 0"
  4.2495 +      using int by (simp add: integral_0_iff)
  4.2496 +    moreover
  4.2497 +    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)"
  4.2498 +      using A by (intro nn_integral_mono_AE) auto
  4.2499 +    then have "(emeasure M) A \<le> (emeasure M) {x \<in> space M. Y x - X x \<noteq> 0}"
  4.2500 +      using int A by (simp add: integrable_def)
  4.2501 +    ultimately have "emeasure M A = 0"
  4.2502 +      by simp
  4.2503 +    with \<open>(emeasure M) A \<noteq> 0\<close> show False by auto
  4.2504 +  qed
  4.2505 +  ultimately show ?thesis by auto
  4.2506 +qed
  4.2507 +
  4.2508 +lemma (in finite_measure) integral_less_AE_space:
  4.2509 +  fixes X Y :: "'a \<Rightarrow> real"
  4.2510 +  assumes int: "integrable M X" "integrable M Y"
  4.2511 +  assumes gt: "AE x in M. X x < Y x" "emeasure M (space M) \<noteq> 0"
  4.2512 +  shows "integral\<^sup>L M X < integral\<^sup>L M Y"
  4.2513 +  using gt by (intro integral_less_AE[OF int, where A="space M"]) auto
  4.2514 +
  4.2515 +lemma tendsto_integral_at_top:
  4.2516 +  fixes f :: "real \<Rightarrow> 'a::{banach, second_countable_topology}"
  4.2517 +  assumes [measurable_cong]: "sets M = sets borel" and f[measurable]: "integrable M f"
  4.2518 +  shows "((\<lambda>y. \<integral> x. indicator {.. y} x *\<^sub>R f x \<partial>M) \<longlongrightarrow> \<integral> x. f x \<partial>M) at_top"
  4.2519 +proof (rule tendsto_at_topI_sequentially)
  4.2520 +  fix X :: "nat \<Rightarrow> real" assume "filterlim X at_top sequentially"
  4.2521 +  show "(\<lambda>n. \<integral>x. indicator {..X n} x *\<^sub>R f x \<partial>M) \<longlonglongrightarrow> integral\<^sup>L M f"
  4.2522 +  proof (rule integral_dominated_convergence)
  4.2523 +    show "integrable M (\<lambda>x. norm (f x))"
  4.2524 +      by (rule integrable_norm) fact
  4.2525 +    show "AE x in M. (\<lambda>n. indicator {..X n} x *\<^sub>R f x) \<longlonglongrightarrow> f x"
  4.2526 +    proof
  4.2527 +      fix x
  4.2528 +      from \<open>filterlim X at_top sequentially\<close>
  4.2529 +      have "eventually (\<lambda>n. x \<le> X n) sequentially"
  4.2530 +        unfolding filterlim_at_top_ge[where c=x] by auto
  4.2531 +      then show "(\<lambda>n. indicator {..X n} x *\<^sub>R f x) \<longlonglongrightarrow> f x"
  4.2532 +        by (intro Lim_eventually) (auto split: split_indicator elim!: eventually_mono)
  4.2533 +    qed
  4.2534 +    fix n show "AE x in M. norm (indicator {..X n} x *\<^sub>R f x) \<le> norm (f x)"
  4.2535 +      by (auto split: split_indicator)
  4.2536 +  qed auto
  4.2537 +qed
  4.2538 +
  4.2539 +lemma
  4.2540 +  fixes f :: "real \<Rightarrow> real"
  4.2541 +  assumes M: "sets M = sets borel"
  4.2542 +  assumes nonneg: "AE x in M. 0 \<le> f x"
  4.2543 +  assumes borel: "f \<in> borel_measurable borel"
  4.2544 +  assumes int: "\<And>y. integrable M (\<lambda>x. f x * indicator {.. y} x)"
  4.2545 +  assumes conv: "((\<lambda>y. \<integral> x. f x * indicator {.. y} x \<partial>M) \<longlongrightarrow> x) at_top"
  4.2546 +  shows has_bochner_integral_monotone_convergence_at_top: "has_bochner_integral M f x"
  4.2547 +    and integrable_monotone_convergence_at_top: "integrable M f"
  4.2548 +    and integral_monotone_convergence_at_top:"integral\<^sup>L M f = x"
  4.2549 +proof -
  4.2550 +  from nonneg have "AE x in M. mono (\<lambda>n::nat. f x * indicator {..real n} x)"
  4.2551 +    by (auto split: split_indicator intro!: monoI)
  4.2552 +  { fix x have "eventually (\<lambda>n. f x * indicator {..real n} x = f x) sequentially"
  4.2553 +      by (rule eventually_sequentiallyI[of "nat \<lceil>x\<rceil>"])
  4.2554 +         (auto split: split_indicator simp: nat_le_iff ceiling_le_iff) }
  4.2555 +  from filterlim_cong[OF refl refl this]
  4.2556 +  have "AE x in M. (\<lambda>i. f x * indicator {..real i} x) \<longlonglongrightarrow> f x"
  4.2557 +    by simp
  4.2558 +  have "(\<lambda>i. \<integral> x. f x * indicator {..real i} x \<partial>M) \<longlonglongrightarrow> x"
  4.2559 +    using conv filterlim_real_sequentially by (rule filterlim_compose)
  4.2560 +  have M_measure[simp]: "borel_measurable M = borel_measurable borel"
  4.2561 +    using M by (simp add: sets_eq_imp_space_eq measurable_def)
  4.2562 +  have "f \<in> borel_measurable M"
  4.2563 +    using borel by simp
  4.2564 +  show "has_bochner_integral M f x"
  4.2565 +    by (rule has_bochner_integral_monotone_convergence) fact+
  4.2566 +  then show "integrable M f" "integral\<^sup>L M f = x"
  4.2567 +    by (auto simp: _has_bochner_integral_iff)
  4.2568 +qed
  4.2569 +
  4.2570 +subsection \<open>Product measure\<close>
  4.2571 +
  4.2572 +lemma (in sigma_finite_measure) borel_measurable_lebesgue_integrable[measurable (raw)]:
  4.2573 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2574 +  assumes [measurable]: "case_prod f \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  4.2575 +  shows "Measurable.pred N (\<lambda>x. integrable M (f x))"
  4.2576 +proof -
  4.2577 +  have [simp]: "\<And>x. x \<in> space N \<Longrightarrow> integrable M (f x) \<longleftrightarrow> (\<integral>\<^sup>+y. norm (f x y) \<partial>M) < \<infinity>"
  4.2578 +    unfolding integrable_iff_bounded by simp
  4.2579 +  show ?thesis
  4.2580 +    by (simp cong: measurable_cong)
  4.2581 +qed
  4.2582 +
  4.2583 +lemma Collect_subset [simp]: "{x\<in>A. P x} \<subseteq> A" by auto
  4.2584 +
  4.2585 +lemma (in sigma_finite_measure) measurable_measure[measurable (raw)]:
  4.2586 +  "(\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M) \<Longrightarrow>
  4.2587 +    {x \<in> space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M) \<Longrightarrow>
  4.2588 +    (\<lambda>x. measure M (A x)) \<in> borel_measurable N"
  4.2589 +  unfolding measure_def by (intro measurable_emeasure borel_measurable_enn2real) auto
  4.2590 +
  4.2591 +lemma (in sigma_finite_measure) borel_measurable_lebesgue_integral[measurable (raw)]:
  4.2592 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2593 +  assumes f[measurable]: "case_prod f \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  4.2594 +  shows "(\<lambda>x. \<integral>y. f x y \<partial>M) \<in> borel_measurable N"
  4.2595 +proof -
  4.2596 +  from borel_measurable_implies_sequence_metric[OF f, of 0] guess s ..
  4.2597 +  then have s: "\<And>i. simple_function (N \<Otimes>\<^sub>M M) (s i)"
  4.2598 +    "\<And>x y. x \<in> space N \<Longrightarrow> y \<in> space M \<Longrightarrow> (\<lambda>i. s i (x, y)) \<longlonglongrightarrow> f x y"
  4.2599 +    "\<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)"
  4.2600 +    by (auto simp: space_pair_measure)
  4.2601 +
  4.2602 +  have [measurable]: "\<And>i. s i \<in> borel_measurable (N \<Otimes>\<^sub>M M)"
  4.2603 +    by (rule borel_measurable_simple_function) fact
  4.2604 +
  4.2605 +  have "\<And>i. s i \<in> measurable (N \<Otimes>\<^sub>M M) (count_space UNIV)"
  4.2606 +    by (rule measurable_simple_function) fact
  4.2607 +
  4.2608 +  define f' where [abs_def]: "f' i x =
  4.2609 +    (if integrable M (f x) then simple_bochner_integral M (\<lambda>y. s i (x, y)) else 0)" for i x
  4.2610 +
  4.2611 +  { fix i x assume "x \<in> space N"
  4.2612 +    then have "simple_bochner_integral M (\<lambda>y. s i (x, y)) =
  4.2613 +      (\<Sum>z\<in>s i ` (space N \<times> space M). measure M {y \<in> space M. s i (x, y) = z} *\<^sub>R z)"
  4.2614 +      using s(1)[THEN simple_functionD(1)]
  4.2615 +      unfolding simple_bochner_integral_def
  4.2616 +      by (intro setsum.mono_neutral_cong_left)
  4.2617 +         (auto simp: eq_commute space_pair_measure image_iff cong: conj_cong) }
  4.2618 +  note eq = this
  4.2619 +
  4.2620 +  show ?thesis
  4.2621 +  proof (rule borel_measurable_LIMSEQ_metric)
  4.2622 +    fix i show "f' i \<in> borel_measurable N"
  4.2623 +      unfolding f'_def by (simp_all add: eq cong: measurable_cong if_cong)
  4.2624 +  next
  4.2625 +    fix x assume x: "x \<in> space N"
  4.2626 +    { assume int_f: "integrable M (f x)"
  4.2627 +      have int_2f: "integrable M (\<lambda>y. 2 * norm (f x y))"
  4.2628 +        by (intro integrable_norm integrable_mult_right int_f)
  4.2629 +      have "(\<lambda>i. integral\<^sup>L M (\<lambda>y. s i (x, y))) \<longlonglongrightarrow> integral\<^sup>L M (f x)"
  4.2630 +      proof (rule integral_dominated_convergence)
  4.2631 +        from int_f show "f x \<in> borel_measurable M" by auto
  4.2632 +        show "\<And>i. (\<lambda>y. s i (x, y)) \<in> borel_measurable M"
  4.2633 +          using x by simp
  4.2634 +        show "AE xa in M. (\<lambda>i. s i (x, xa)) \<longlonglongrightarrow> f x xa"
  4.2635 +          using x s(2) by auto
  4.2636 +        show "\<And>i. AE xa in M. norm (s i (x, xa)) \<le> 2 * norm (f x xa)"
  4.2637 +          using x s(3) by auto
  4.2638 +      qed fact
  4.2639 +      moreover
  4.2640 +      { fix i
  4.2641 +        have "simple_bochner_integrable M (\<lambda>y. s i (x, y))"
  4.2642 +        proof (rule simple_bochner_integrableI_bounded)
  4.2643 +          have "(\<lambda>y. s i (x, y)) ` space M \<subseteq> s i ` (space N \<times> space M)"
  4.2644 +            using x by auto
  4.2645 +          then show "simple_function M (\<lambda>y. s i (x, y))"
  4.2646 +            using simple_functionD(1)[OF s(1), of i] x
  4.2647 +            by (intro simple_function_borel_measurable)
  4.2648 +               (auto simp: space_pair_measure dest: finite_subset)
  4.2649 +          have "(\<integral>\<^sup>+ y. ennreal (norm (s i (x, y))) \<partial>M) \<le> (\<integral>\<^sup>+ y. 2 * norm (f x y) \<partial>M)"
  4.2650 +            using x s by (intro nn_integral_mono) auto
  4.2651 +          also have "(\<integral>\<^sup>+ y. 2 * norm (f x y) \<partial>M) < \<infinity>"
  4.2652 +            using int_2f by (simp add: integrable_iff_bounded)
  4.2653 +          finally show "(\<integral>\<^sup>+ xa. ennreal (norm (s i (x, xa))) \<partial>M) < \<infinity>" .
  4.2654 +        qed
  4.2655 +        then have "integral\<^sup>L M (\<lambda>y. s i (x, y)) = simple_bochner_integral M (\<lambda>y. s i (x, y))"
  4.2656 +          by (rule simple_bochner_integrable_eq_integral[symmetric]) }
  4.2657 +      ultimately have "(\<lambda>i. simple_bochner_integral M (\<lambda>y. s i (x, y))) \<longlonglongrightarrow> integral\<^sup>L M (f x)"
  4.2658 +        by simp }
  4.2659 +    then
  4.2660 +    show "(\<lambda>i. f' i x) \<longlonglongrightarrow> integral\<^sup>L M (f x)"
  4.2661 +      unfolding f'_def
  4.2662 +      by (cases "integrable M (f x)") (simp_all add: not_integrable_integral_eq)
  4.2663 +  qed
  4.2664 +qed
  4.2665 +
  4.2666 +lemma (in pair_sigma_finite) integrable_product_swap:
  4.2667 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2668 +  assumes "integrable (M1 \<Otimes>\<^sub>M M2) f"
  4.2669 +  shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x))"
  4.2670 +proof -
  4.2671 +  interpret Q: pair_sigma_finite M2 M1 ..
  4.2672 +  have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  4.2673 +  show ?thesis unfolding *
  4.2674 +    by (rule integrable_distr[OF measurable_pair_swap'])
  4.2675 +       (simp add: distr_pair_swap[symmetric] assms)
  4.2676 +qed
  4.2677 +
  4.2678 +lemma (in pair_sigma_finite) integrable_product_swap_iff:
  4.2679 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2680 +  shows "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow> integrable (M1 \<Otimes>\<^sub>M M2) f"
  4.2681 +proof -
  4.2682 +  interpret Q: pair_sigma_finite M2 M1 ..
  4.2683 +  from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
  4.2684 +  show ?thesis by auto
  4.2685 +qed
  4.2686 +
  4.2687 +lemma (in pair_sigma_finite) integral_product_swap:
  4.2688 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2689 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  4.2690 +  shows "(\<integral>(x,y). f (y,x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  4.2691 +proof -
  4.2692 +  have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
  4.2693 +  show ?thesis unfolding *
  4.2694 +    by (simp add: integral_distr[symmetric, OF measurable_pair_swap' f] distr_pair_swap[symmetric])
  4.2695 +qed
  4.2696 +
  4.2697 +lemma (in pair_sigma_finite) Fubini_integrable:
  4.2698 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2699 +  assumes f[measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  4.2700 +    and integ1: "integrable M1 (\<lambda>x. \<integral> y. norm (f (x, y)) \<partial>M2)"
  4.2701 +    and integ2: "AE x in M1. integrable M2 (\<lambda>y. f (x, y))"
  4.2702 +  shows "integrable (M1 \<Otimes>\<^sub>M M2) f"
  4.2703 +proof (rule integrableI_bounded)
  4.2704 +  have "(\<integral>\<^sup>+ p. norm (f p) \<partial>(M1 \<Otimes>\<^sub>M M2)) = (\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. norm (f (x, y)) \<partial>M2) \<partial>M1)"
  4.2705 +    by (simp add: M2.nn_integral_fst [symmetric])
  4.2706 +  also have "\<dots> = (\<integral>\<^sup>+ x. \<bar>\<integral>y. norm (f (x, y)) \<partial>M2\<bar> \<partial>M1)"
  4.2707 +    apply (intro nn_integral_cong_AE)
  4.2708 +    using integ2
  4.2709 +  proof eventually_elim
  4.2710 +    fix x assume "integrable M2 (\<lambda>y. f (x, y))"
  4.2711 +    then have f: "integrable M2 (\<lambda>y. norm (f (x, y)))"
  4.2712 +      by simp
  4.2713 +    then have "(\<integral>\<^sup>+y. ennreal (norm (f (x, y))) \<partial>M2) = ennreal (LINT y|M2. norm (f (x, y)))"
  4.2714 +      by (rule nn_integral_eq_integral) simp
  4.2715 +    also have "\<dots> = ennreal \<bar>LINT y|M2. norm (f (x, y))\<bar>"
  4.2716 +      using f by simp
  4.2717 +    finally show "(\<integral>\<^sup>+y. ennreal (norm (f (x, y))) \<partial>M2) = ennreal \<bar>LINT y|M2. norm (f (x, y))\<bar>" .
  4.2718 +  qed
  4.2719 +  also have "\<dots> < \<infinity>"
  4.2720 +    using integ1 by (simp add: integrable_iff_bounded integral_nonneg_AE)
  4.2721 +  finally show "(\<integral>\<^sup>+ p. norm (f p) \<partial>(M1 \<Otimes>\<^sub>M M2)) < \<infinity>" .
  4.2722 +qed fact
  4.2723 +
  4.2724 +lemma (in pair_sigma_finite) emeasure_pair_measure_finite:
  4.2725 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" and finite: "emeasure (M1 \<Otimes>\<^sub>M M2) A < \<infinity>"
  4.2726 +  shows "AE x in M1. emeasure M2 {y\<in>space M2. (x, y) \<in> A} < \<infinity>"
  4.2727 +proof -
  4.2728 +  from M2.emeasure_pair_measure_alt[OF A] finite
  4.2729 +  have "(\<integral>\<^sup>+ x. emeasure M2 (Pair x -` A) \<partial>M1) \<noteq> \<infinity>"
  4.2730 +    by simp
  4.2731 +  then have "AE x in M1. emeasure M2 (Pair x -` A) \<noteq> \<infinity>"
  4.2732 +    by (rule nn_integral_PInf_AE[rotated]) (intro M2.measurable_emeasure_Pair A)
  4.2733 +  moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> Pair x -` A = {y\<in>space M2. (x, y) \<in> A}"
  4.2734 +    using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
  4.2735 +  ultimately show ?thesis by (auto simp: less_top)
  4.2736 +qed
  4.2737 +
  4.2738 +lemma (in pair_sigma_finite) AE_integrable_fst':
  4.2739 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2740 +  assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  4.2741 +  shows "AE x in M1. integrable M2 (\<lambda>y. f (x, y))"
  4.2742 +proof -
  4.2743 +  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))"
  4.2744 +    by (rule M2.nn_integral_fst) simp
  4.2745 +  also have "(\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2)) \<noteq> \<infinity>"
  4.2746 +    using f unfolding integrable_iff_bounded by simp
  4.2747 +  finally have "AE x in M1. (\<integral>\<^sup>+y. norm (f (x, y)) \<partial>M2) \<noteq> \<infinity>"
  4.2748 +    by (intro nn_integral_PInf_AE M2.borel_measurable_nn_integral )
  4.2749 +       (auto simp: measurable_split_conv)
  4.2750 +  with AE_space show ?thesis
  4.2751 +    by eventually_elim
  4.2752 +       (auto simp: integrable_iff_bounded measurable_compose[OF _ borel_measurable_integrable[OF f]] less_top)
  4.2753 +qed
  4.2754 +
  4.2755 +lemma (in pair_sigma_finite) integrable_fst':
  4.2756 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2757 +  assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  4.2758 +  shows "integrable M1 (\<lambda>x. \<integral>y. f (x, y) \<partial>M2)"
  4.2759 +  unfolding integrable_iff_bounded
  4.2760 +proof
  4.2761 +  show "(\<lambda>x. \<integral> y. f (x, y) \<partial>M2) \<in> borel_measurable M1"
  4.2762 +    by (rule M2.borel_measurable_lebesgue_integral) simp
  4.2763 +  have "(\<integral>\<^sup>+ x. ennreal (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)"
  4.2764 +    using AE_integrable_fst'[OF f] by (auto intro!: nn_integral_mono_AE integral_norm_bound_ennreal)
  4.2765 +  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))"
  4.2766 +    by (rule M2.nn_integral_fst) simp
  4.2767 +  also have "(\<integral>\<^sup>+x. norm (f x) \<partial>(M1 \<Otimes>\<^sub>M M2)) < \<infinity>"
  4.2768 +    using f unfolding integrable_iff_bounded by simp
  4.2769 +  finally show "(\<integral>\<^sup>+ x. ennreal (norm (\<integral> y. f (x, y) \<partial>M2)) \<partial>M1) < \<infinity>" .
  4.2770 +qed
  4.2771 +
  4.2772 +lemma (in pair_sigma_finite) integral_fst':
  4.2773 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2774 +  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) f"
  4.2775 +  shows "(\<integral>x. (\<integral>y. f (x, y) \<partial>M2) \<partial>M1) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  4.2776 +using f proof induct
  4.2777 +  case (base A c)
  4.2778 +  have A[measurable]: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" by fact
  4.2779 +
  4.2780 +  have eq: "\<And>x y. x \<in> space M1 \<Longrightarrow> indicator A (x, y) = indicator {y\<in>space M2. (x, y) \<in> A} y"
  4.2781 +    using sets.sets_into_space[OF A] by (auto split: split_indicator simp: space_pair_measure)
  4.2782 +
  4.2783 +  have int_A: "integrable (M1 \<Otimes>\<^sub>M M2) (indicator A :: _ \<Rightarrow> real)"
  4.2784 +    using base by (rule integrable_real_indicator)
  4.2785 +
  4.2786 +  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)"
  4.2787 +  proof (intro integral_cong_AE, simp, simp)
  4.2788 +    from AE_integrable_fst'[OF int_A] AE_space
  4.2789 +    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"
  4.2790 +      by eventually_elim (simp add: eq integrable_indicator_iff)
  4.2791 +  qed
  4.2792 +  also have "\<dots> = measure (M1 \<Otimes>\<^sub>M M2) A *\<^sub>R c"
  4.2793 +  proof (subst integral_scaleR_left)
  4.2794 +    have "(\<integral>\<^sup>+x. ennreal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1) =
  4.2795 +      (\<integral>\<^sup>+x. emeasure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1)"
  4.2796 +      using emeasure_pair_measure_finite[OF base]
  4.2797 +      by (intro nn_integral_cong_AE, eventually_elim) (simp add: emeasure_eq_ennreal_measure)
  4.2798 +    also have "\<dots> = emeasure (M1 \<Otimes>\<^sub>M M2) A"
  4.2799 +      using sets.sets_into_space[OF A]
  4.2800 +      by (subst M2.emeasure_pair_measure_alt)
  4.2801 +         (auto intro!: nn_integral_cong arg_cong[where f="emeasure M2"] simp: space_pair_measure)
  4.2802 +    finally have *: "(\<integral>\<^sup>+x. ennreal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1) = emeasure (M1 \<Otimes>\<^sub>M M2) A" .
  4.2803 +
  4.2804 +    from base * show "integrable M1 (\<lambda>x. measure M2 {y \<in> space M2. (x, y) \<in> A})"
  4.2805 +      by (simp add: integrable_iff_bounded)
  4.2806 +    then have "(\<integral>x. measure M2 {y \<in> space M2. (x, y) \<in> A} \<partial>M1) =
  4.2807 +      (\<integral>\<^sup>+x. ennreal (measure M2 {y \<in> space M2. (x, y) \<in> A}) \<partial>M1)"
  4.2808 +      by (rule nn_integral_eq_integral[symmetric]) simp
  4.2809 +    also note *
  4.2810 +    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"
  4.2811 +      using base by (simp add: emeasure_eq_ennreal_measure)
  4.2812 +  qed
  4.2813 +  also have "\<dots> = (\<integral> a. indicator A a *\<^sub>R c \<partial>(M1 \<Otimes>\<^sub>M M2))"
  4.2814 +    using base by simp
  4.2815 +  finally show ?case .
  4.2816 +next
  4.2817 +  case (add f g)
  4.2818 +  then have [measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" "g \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  4.2819 +    by auto
  4.2820 +  have "(\<integral> x. \<integral> y. f (x, y) + g (x, y) \<partial>M2 \<partial>M1) =
  4.2821 +    (\<integral> x. (\<integral> y. f (x, y) \<partial>M2) + (\<integral> y. g (x, y) \<partial>M2) \<partial>M1)"
  4.2822 +    apply (rule integral_cong_AE)
  4.2823 +    apply simp_all
  4.2824 +    using AE_integrable_fst'[OF add(1)] AE_integrable_fst'[OF add(3)]
  4.2825 +    apply eventually_elim
  4.2826 +    apply simp
  4.2827 +    done
  4.2828 +  also have "\<dots> = (\<integral> x. f x \<partial>(M1 \<Otimes>\<^sub>M M2)) + (\<integral> x. g x \<partial>(M1 \<Otimes>\<^sub>M M2))"
  4.2829 +    using integrable_fst'[OF add(1)] integrable_fst'[OF add(3)] add(2,4) by simp
  4.2830 +  finally show ?case
  4.2831 +    using add by simp
  4.2832 +next
  4.2833 +  case (lim f s)
  4.2834 +  then have [measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)" "\<And>i. s i \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
  4.2835 +    by auto
  4.2836 +
  4.2837 +  show ?case
  4.2838 +  proof (rule LIMSEQ_unique)
  4.2839 +    show "(\<lambda>i. integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (s i)) \<longlonglongrightarrow> integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) f"
  4.2840 +    proof (rule integral_dominated_convergence)
  4.2841 +      show "integrable (M1 \<Otimes>\<^sub>M M2) (\<lambda>x. 2 * norm (f x))"
  4.2842 +        using lim(5) by auto
  4.2843 +    qed (insert lim, auto)
  4.2844 +    have "(\<lambda>i. \<integral> x. \<integral> y. s i (x, y) \<partial>M2 \<partial>M1) \<longlonglongrightarrow> \<integral> x. \<integral> y. f (x, y) \<partial>M2 \<partial>M1"
  4.2845 +    proof (rule integral_dominated_convergence)
  4.2846 +      have "AE x in M1. \<forall>i. integrable M2 (\<lambda>y. s i (x, y))"
  4.2847 +        unfolding AE_all_countable using AE_integrable_fst'[OF lim(1)] ..
  4.2848 +      with AE_space AE_integrable_fst'[OF lim(5)]
  4.2849 +      show "AE x in M1. (\<lambda>i. \<integral> y. s i (x, y) \<partial>M2) \<longlonglongrightarrow> \<integral> y. f (x, y) \<partial>M2"
  4.2850 +      proof eventually_elim
  4.2851 +        fix x assume x: "x \<in> space M1" and
  4.2852 +          s: "\<forall>i. integrable M2 (\<lambda>y. s i (x, y))" and f: "integrable M2 (\<lambda>y. f (x, y))"
  4.2853 +        show "(\<lambda>i. \<integral> y. s i (x, y) \<partial>M2) \<longlonglongrightarrow> \<integral> y. f (x, y) \<partial>M2"
  4.2854 +        proof (rule integral_dominated_convergence)
  4.2855 +          show "integrable M2 (\<lambda>y. 2 * norm (f (x, y)))"
  4.2856 +             using f by auto
  4.2857 +          show "AE xa in M2. (\<lambda>i. s i (x, xa)) \<longlonglongrightarrow> f (x, xa)"
  4.2858 +            using x lim(3) by (auto simp: space_pair_measure)
  4.2859 +          show "\<And>i. AE xa in M2. norm (s i (x, xa)) \<le> 2 * norm (f (x, xa))"
  4.2860 +            using x lim(4) by (auto simp: space_pair_measure)
  4.2861 +        qed (insert x, measurable)
  4.2862 +      qed
  4.2863 +      show "integrable M1 (\<lambda>x. (\<integral> y. 2 * norm (f (x, y)) \<partial>M2))"
  4.2864 +        by (intro integrable_mult_right integrable_norm integrable_fst' lim)
  4.2865 +      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)"
  4.2866 +        using AE_space AE_integrable_fst'[OF lim(1), of i] AE_integrable_fst'[OF lim(5)]
  4.2867 +      proof eventually_elim
  4.2868 +        fix x assume x: "x \<in> space M1"
  4.2869 +          and s: "integrable M2 (\<lambda>y. s i (x, y))" and f: "integrable M2 (\<lambda>y. f (x, y))"
  4.2870 +        from s have "norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral>\<^sup>+y. norm (s i (x, y)) \<partial>M2)"
  4.2871 +          by (rule integral_norm_bound_ennreal)
  4.2872 +        also have "\<dots> \<le> (\<integral>\<^sup>+y. 2 * norm (f (x, y)) \<partial>M2)"
  4.2873 +          using x lim by (auto intro!: nn_integral_mono simp: space_pair_measure)
  4.2874 +        also have "\<dots> = (\<integral>y. 2 * norm (f (x, y)) \<partial>M2)"
  4.2875 +          using f by (intro nn_integral_eq_integral) auto
  4.2876 +        finally show "norm (\<integral> y. s i (x, y) \<partial>M2) \<le> (\<integral> y. 2 * norm (f (x, y)) \<partial>M2)"
  4.2877 +          by simp
  4.2878 +      qed
  4.2879 +    qed simp_all
  4.2880 +    then show "(\<lambda>i. integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (s i)) \<longlonglongrightarrow> \<integral> x. \<integral> y. f (x, y) \<partial>M2 \<partial>M1"
  4.2881 +      using lim by simp
  4.2882 +  qed
  4.2883 +qed
  4.2884 +
  4.2885 +lemma (in pair_sigma_finite)
  4.2886 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2887 +  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) (case_prod f)"
  4.2888 +  shows AE_integrable_fst: "AE x in M1. integrable M2 (\<lambda>y. f x y)" (is "?AE")
  4.2889 +    and integrable_fst: "integrable M1 (\<lambda>x. \<integral>y. f x y \<partial>M2)" (is "?INT")
  4.2890 +    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")
  4.2891 +  using AE_integrable_fst'[OF f] integrable_fst'[OF f] integral_fst'[OF f] by auto
  4.2892 +
  4.2893 +lemma (in pair_sigma_finite)
  4.2894 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2895 +  assumes f[measurable]: "integrable (M1 \<Otimes>\<^sub>M M2) (case_prod f)"
  4.2896 +  shows AE_integrable_snd: "AE y in M2. integrable M1 (\<lambda>x. f x y)" (is "?AE")
  4.2897 +    and integrable_snd: "integrable M2 (\<lambda>y. \<integral>x. f x y \<partial>M1)" (is "?INT")
  4.2898 +    and integral_snd: "(\<integral>y. (\<integral>x. f x y \<partial>M1) \<partial>M2) = integral\<^sup>L (M1 \<Otimes>\<^sub>M M2) (case_prod f)" (is "?EQ")
  4.2899 +proof -
  4.2900 +  interpret Q: pair_sigma_finite M2 M1 ..
  4.2901 +  have Q_int: "integrable (M2 \<Otimes>\<^sub>M M1) (\<lambda>(x, y). f y x)"
  4.2902 +    using f unfolding integrable_product_swap_iff[symmetric] by simp
  4.2903 +  show ?AE  using Q.AE_integrable_fst'[OF Q_int] by simp
  4.2904 +  show ?INT using Q.integrable_fst'[OF Q_int] by simp
  4.2905 +  show ?EQ using Q.integral_fst'[OF Q_int]
  4.2906 +    using integral_product_swap[of "case_prod f"] by simp
  4.2907 +qed
  4.2908 +
  4.2909 +lemma (in pair_sigma_finite) Fubini_integral:
  4.2910 +  fixes f :: "_ \<Rightarrow> _ \<Rightarrow> _ :: {banach, second_countable_topology}"
  4.2911 +  assumes f: "integrable (M1 \<Otimes>\<^sub>M M2) (case_prod f)"
  4.2912 +  shows "(\<integral>y. (\<integral>x. f x y \<partial>M1) \<partial>M2) = (\<integral>x. (\<integral>y. f x y \<partial>M2) \<partial>M1)"
  4.2913 +  unfolding integral_snd[OF assms] integral_fst[OF assms] ..
  4.2914 +
  4.2915 +lemma (in product_sigma_finite) product_integral_singleton:
  4.2916 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2917 +  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"
  4.2918 +  apply (subst distr_singleton[symmetric])
  4.2919 +  apply (subst integral_distr)
  4.2920 +  apply simp_all
  4.2921 +  done
  4.2922 +
  4.2923 +lemma (in product_sigma_finite) product_integral_fold:
  4.2924 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2925 +  assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  4.2926 +  and f: "integrable (Pi\<^sub>M (I \<union> J) M) f"
  4.2927 +  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)"
  4.2928 +proof -
  4.2929 +  interpret I: finite_product_sigma_finite M I by standard fact
  4.2930 +  interpret J: finite_product_sigma_finite M J by standard fact
  4.2931 +  have "finite (I \<union> J)" using fin by auto
  4.2932 +  interpret IJ: finite_product_sigma_finite M "I \<union> J" by standard fact
  4.2933 +  interpret P: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" ..
  4.2934 +  let ?M = "merge I J"
  4.2935 +  let ?f = "\<lambda>x. f (?M x)"
  4.2936 +  from f have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (I \<union> J) M)"
  4.2937 +    by auto
  4.2938 +  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)"
  4.2939 +    using measurable_comp[OF measurable_merge f_borel] by (simp add: comp_def)
  4.2940 +  have f_int: "integrable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) ?f"
  4.2941 +    by (rule integrable_distr[OF measurable_merge]) (simp add: distr_merge[OF IJ fin] f)
  4.2942 +  show ?thesis
  4.2943 +    apply (subst distr_merge[symmetric, OF IJ fin])
  4.2944 +    apply (subst integral_distr[OF measurable_merge f_borel])
  4.2945 +    apply (subst P.integral_fst'[symmetric, OF f_int])
  4.2946 +    apply simp
  4.2947 +    done
  4.2948 +qed
  4.2949 +
  4.2950 +lemma (in product_sigma_finite) product_integral_insert:
  4.2951 +  fixes f :: "_ \<Rightarrow> _::{banach, second_countable_topology}"
  4.2952 +  assumes I: "finite I" "i \<notin> I"
  4.2953 +    and f: "integrable (Pi\<^sub>M (insert i I) M) f"
  4.2954 +  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)"
  4.2955 +proof -
  4.2956 +  have "integral\<^sup>L (Pi\<^sub>M (insert i I) M) f = integral\<^sup>L (Pi\<^sub>M (I \<union> {i}) M) f"
  4.2957 +    by simp
  4.2958 +  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)"
  4.2959 +    using f I by (intro product_integral_fold) auto
  4.2960 +  also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^sub>M I M)"
  4.2961 +  proof (rule integral_cong[OF refl], subst product_integral_singleton[symmetric])
  4.2962 +    fix x assume x: "x \<in> space (Pi\<^sub>M I M)"
  4.2963 +    have f_borel: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  4.2964 +      using f by auto
  4.2965 +    show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
  4.2966 +      using measurable_comp[OF measurable_component_update f_borel, OF x \<open>i \<notin> I\<close>]
  4.2967 +      unfolding comp_def .
  4.2968 +    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)"
  4.2969 +      by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def PiE_def)
  4.2970 +  qed
  4.2971 +  finally show ?thesis .
  4.2972 +qed
  4.2973 +
  4.2974 +lemma (in product_sigma_finite) product_integrable_setprod:
  4.2975 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> _::{real_normed_field,banach,second_countable_topology}"
  4.2976 +  assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  4.2977 +  shows "integrable (Pi\<^sub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
  4.2978 +proof (unfold integrable_iff_bounded, intro conjI)
  4.2979 +  interpret finite_product_sigma_finite M I by standard fact
  4.2980 +
  4.2981 +  show "?f \<in> borel_measurable (Pi\<^sub>M I M)"
  4.2982 +    using assms by simp
  4.2983 +  have "(\<integral>\<^sup>+ x. ennreal (norm (\<Prod>i\<in>I. f i (x i))) \<partial>Pi\<^sub>M I M) =
  4.2984 +      (\<integral>\<^sup>+ x. (\<Prod>i\<in>I. ennreal (norm (f i (x i)))) \<partial>Pi\<^sub>M I M)"
  4.2985 +    by (simp add: setprod_norm setprod_ennreal)
  4.2986 +  also have "\<dots> = (\<Prod>i\<in>I. \<integral>\<^sup>+ x. ennreal (norm (f i x)) \<partial>M i)"
  4.2987 +    using assms by (intro product_nn_integral_setprod) auto
  4.2988 +  also have "\<dots> < \<infinity>"
  4.2989 +    using integrable by (simp add: less_top[symmetric] ennreal_setprod_eq_top integrable_iff_bounded)
  4.2990 +  finally show "(\<integral>\<^sup>+ x. ennreal (norm (\<Prod>i\<in>I. f i (x i))) \<partial>Pi\<^sub>M I M) < \<infinity>" .
  4.2991 +qed
  4.2992 +
  4.2993 +lemma (in product_sigma_finite) product_integral_setprod:
  4.2994 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> _::{real_normed_field,banach,second_countable_topology}"
  4.2995 +  assumes "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  4.2996 +  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))"
  4.2997 +using assms proof induct
  4.2998 +  case empty
  4.2999 +  interpret finite_measure "Pi\<^sub>M {} M"
  4.3000 +    by rule (simp add: space_PiM)
  4.3001 +  show ?case by (simp add: space_PiM measure_def)
  4.3002 +next
  4.3003 +  case (insert i I)
  4.3004 +  then have iI: "finite (insert i I)" by auto
  4.3005 +  then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  4.3006 +    integrable (Pi\<^sub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  4.3007 +    by (intro product_integrable_setprod insert(4)) (auto intro: finite_subset)
  4.3008 +  interpret I: finite_product_sigma_finite M I by standard fact
  4.3009 +  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))"
  4.3010 +    using \<open>i \<notin> I\<close> by (auto intro!: setprod.cong)
  4.3011 +  show ?case
  4.3012 +    unfolding product_integral_insert[OF insert(1,2) prod[OF subset_refl]]
  4.3013 +    by (simp add: * insert prod subset_insertI)
  4.3014 +qed
  4.3015 +
  4.3016 +lemma integrable_subalgebra:
  4.3017 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.3018 +  assumes borel: "f \<in> borel_measurable N"
  4.3019 +  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  4.3020 +  shows "integrable N f \<longleftrightarrow> integrable M f" (is ?P)
  4.3021 +proof -
  4.3022 +  have "f \<in> borel_measurable M"
  4.3023 +    using assms by (auto simp: measurable_def)
  4.3024 +  with assms show ?thesis
  4.3025 +    using assms by (auto simp: integrable_iff_bounded nn_integral_subalgebra)
  4.3026 +qed
  4.3027 +
  4.3028 +lemma integral_subalgebra:
  4.3029 +  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  4.3030 +  assumes borel: "f \<in> borel_measurable N"
  4.3031 +  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
  4.3032 +  shows "integral\<^sup>L N f = integral\<^sup>L M f"
  4.3033 +proof cases
  4.3034 +  assume "integrable N f"
  4.3035 +  then show ?thesis
  4.3036 +  proof induct
  4.3037 +    case base with assms show ?case by (auto simp: subset_eq measure_def)
  4.3038 +  next
  4.3039 +    case (add f g)
  4.3040 +    then have "(\<integral> a. f a + g a \<partial>N) = integral\<^sup>L M f + integral\<^sup>L M g"
  4.3041 +      by simp
  4.3042 +    also have "\<dots> = (\<integral> a. f a + g a \<partial>M)"
  4.3043 +      using add integrable_subalgebra[OF _ N, of f] integrable_subalgebra[OF _ N, of g] by simp
  4.3044 +    finally show ?case .
  4.3045 +  next
  4.3046 +    case (lim f s)
  4.3047 +    then have M: "\<And>i. integrable M (s i)" "integrable M f"
  4.3048 +      using integrable_subalgebra[OF _ N, of f] integrable_subalgebra[OF _ N, of "s i" for i] by simp_all
  4.3049 +    show ?case
  4.3050 +    proof (intro LIMSEQ_unique)
  4.3051 +      show "(\<lambda>i. integral\<^sup>L N (s i)) \<longlonglongrightarrow> integral\<^sup>L N f"
  4.3052 +        apply (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  4.3053 +        using lim
  4.3054 +        apply auto
  4.3055 +        done
  4.3056 +      show "(\<lambda>i. integral\<^sup>L N (s i)) \<longlonglongrightarrow> integral\<^sup>L M f"
  4.3057 +        unfolding lim
  4.3058 +        apply (rule integral_dominated_convergence[where w="\<lambda>x. 2 * norm (f x)"])
  4.3059 +        using lim M N(2)
  4.3060 +        apply auto
  4.3061 +        done
  4.3062 +    qed
  4.3063 +  qed
  4.3064 +qed (simp add: not_integrable_integral_eq integrable_subalgebra[OF assms])
  4.3065 +
  4.3066 +hide_const (open) simple_bochner_integral
  4.3067 +hide_const (open) simple_bochner_integrable
  4.3068 +
  4.3069 +end
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/HOL/Analysis/Borel_Space.thy	Mon Aug 08 14:13:14 2016 +0200
     5.3 @@ -0,0 +1,1915 @@
     5.4 +(*  Title:      HOL/Analysis/Borel_Space.thy
     5.5 +    Author:     Johannes Hölzl, TU München
     5.6 +    Author:     Armin Heller, TU München
     5.7 +*)
     5.8 +
     5.9 +section \<open>Borel spaces\<close>
    5.10 +
    5.11 +theory Borel_Space
    5.12 +imports
    5.13 +  Measurable Derivative Ordered_Euclidean_Space Extended_Real_Limits
    5.14 +begin
    5.15 +
    5.16 +lemma sets_Collect_eventually_sequentially[measurable]:
    5.17 +  "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
    5.18 +  unfolding eventually_sequentially by simp
    5.19 +
    5.20 +lemma topological_basis_trivial: "topological_basis {A. open A}"
    5.21 +  by (auto simp: topological_basis_def)
    5.22 +
    5.23 +lemma open_prod_generated: "open = generate_topology {A \<times> B | A B. open A \<and> open B}"
    5.24 +proof -
    5.25 +  have "{A \<times> B :: ('a \<times> 'b) set | A B. open A \<and> open B} = ((\<lambda>(a, b). a \<times> b) ` ({A. open A} \<times> {A. open A}))"
    5.26 +    by auto
    5.27 +  then show ?thesis
    5.28 +    by (auto intro: topological_basis_prod topological_basis_trivial topological_basis_imp_subbasis)
    5.29 +qed
    5.30 +
    5.31 +definition "mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r \<le> s \<longrightarrow> f r \<le> f s"
    5.32 +
    5.33 +lemma mono_onI:
    5.34 +  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r \<le> s \<Longrightarrow> f r \<le> f s) \<Longrightarrow> mono_on f A"
    5.35 +  unfolding mono_on_def by simp
    5.36 +
    5.37 +lemma mono_onD:
    5.38 +  "\<lbrakk>mono_on f A; r \<in> A; s \<in> A; r \<le> s\<rbrakk> \<Longrightarrow> f r \<le> f s"
    5.39 +  unfolding mono_on_def by simp
    5.40 +
    5.41 +lemma mono_imp_mono_on: "mono f \<Longrightarrow> mono_on f A"
    5.42 +  unfolding mono_def mono_on_def by auto
    5.43 +
    5.44 +lemma mono_on_subset: "mono_on f A \<Longrightarrow> B \<subseteq> A \<Longrightarrow> mono_on f B"
    5.45 +  unfolding mono_on_def by auto
    5.46 +
    5.47 +definition "strict_mono_on f A \<equiv> \<forall>r s. r \<in> A \<and> s \<in> A \<and> r < s \<longrightarrow> f r < f s"
    5.48 +
    5.49 +lemma strict_mono_onI:
    5.50 +  "(\<And>r s. r \<in> A \<Longrightarrow> s \<in> A \<Longrightarrow> r < s \<Longrightarrow> f r < f s) \<Longrightarrow> strict_mono_on f A"
    5.51 +  unfolding strict_mono_on_def by simp
    5.52 +
    5.53 +lemma strict_mono_onD:
    5.54 +  "\<lbrakk>strict_mono_on f A; r \<in> A; s \<in> A; r < s\<rbrakk> \<Longrightarrow> f r < f s"
    5.55 +  unfolding strict_mono_on_def by simp
    5.56 +
    5.57 +lemma mono_on_greaterD:
    5.58 +  assumes "mono_on g A" "x \<in> A" "y \<in> A" "g x > (g (y::_::linorder) :: _ :: linorder)"
    5.59 +  shows "x > y"
    5.60 +proof (rule ccontr)
    5.61 +  assume "\<not>x > y"
    5.62 +  hence "x \<le> y" by (simp add: not_less)
    5.63 +  from assms(1-3) and this have "g x \<le> g y" by (rule mono_onD)
    5.64 +  with assms(4) show False by simp
    5.65 +qed
    5.66 +
    5.67 +lemma strict_mono_inv:
    5.68 +  fixes f :: "('a::linorder) \<Rightarrow> ('b::linorder)"
    5.69 +  assumes "strict_mono f" and "surj f" and inv: "\<And>x. g (f x) = x"
    5.70 +  shows "strict_mono g"
    5.71 +proof
    5.72 +  fix x y :: 'b assume "x < y"
    5.73 +  from \<open>surj f\<close> obtain x' y' where [simp]: "x = f x'" "y = f y'" by blast
    5.74 +  with \<open>x < y\<close> and \<open>strict_mono f\<close> have "x' < y'" by (simp add: strict_mono_less)
    5.75 +  with inv show "g x < g y" by simp
    5.76 +qed
    5.77 +
    5.78 +lemma strict_mono_on_imp_inj_on:
    5.79 +  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> (_ :: preorder)) A"
    5.80 +  shows "inj_on f A"
    5.81 +proof (rule inj_onI)
    5.82 +  fix x y assume "x \<in> A" "y \<in> A" "f x = f y"
    5.83 +  thus "x = y"
    5.84 +    by (cases x y rule: linorder_cases)
    5.85 +       (auto dest: strict_mono_onD[OF assms, of x y] strict_mono_onD[OF assms, of y x])
    5.86 +qed
    5.87 +
    5.88 +lemma strict_mono_on_leD:
    5.89 +  assumes "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A" "x \<in> A" "y \<in> A" "x \<le> y"
    5.90 +  shows "f x \<le> f y"
    5.91 +proof (insert le_less_linear[of y x], elim disjE)
    5.92 +  assume "x < y"
    5.93 +  with assms have "f x < f y" by (rule_tac strict_mono_onD[OF assms(1)]) simp_all
    5.94 +  thus ?thesis by (rule less_imp_le)
    5.95 +qed (insert assms, simp)
    5.96 +
    5.97 +lemma strict_mono_on_eqD:
    5.98 +  fixes f :: "(_ :: linorder) \<Rightarrow> (_ :: preorder)"
    5.99 +  assumes "strict_mono_on f A" "f x = f y" "x \<in> A" "y \<in> A"
   5.100 +  shows "y = x"
   5.101 +  using assms by (rule_tac linorder_cases[of x y]) (auto dest: strict_mono_onD)
   5.102 +
   5.103 +lemma mono_on_imp_deriv_nonneg:
   5.104 +  assumes mono: "mono_on f A" and deriv: "(f has_real_derivative D) (at x)"
   5.105 +  assumes "x \<in> interior A"
   5.106 +  shows "D \<ge> 0"
   5.107 +proof (rule tendsto_le_const)
   5.108 +  let ?A' = "(\<lambda>y. y - x) ` interior A"
   5.109 +  from deriv show "((\<lambda>h. (f (x + h) - f x) / h) \<longlongrightarrow> D) (at 0)"
   5.110 +      by (simp add: field_has_derivative_at has_field_derivative_def)
   5.111 +  from mono have mono': "mono_on f (interior A)" by (rule mono_on_subset) (rule interior_subset)
   5.112 +
   5.113 +  show "eventually (\<lambda>h. (f (x + h) - f x) / h \<ge> 0) (at 0)"
   5.114 +  proof (subst eventually_at_topological, intro exI conjI ballI impI)
   5.115 +    have "open (interior A)" by simp
   5.116 +    hence "open (op + (-x) ` interior A)" by (rule open_translation)
   5.117 +    also have "(op + (-x) ` interior A) = ?A'" by auto
   5.118 +    finally show "open ?A'" .
   5.119 +  next
   5.120 +    from \<open>x \<in> interior A\<close> show "0 \<in> ?A'" by auto
   5.121 +  next
   5.122 +    fix h assume "h \<in> ?A'"
   5.123 +    hence "x + h \<in> interior A" by auto
   5.124 +    with mono' and \<open>x \<in> interior A\<close> show "(f (x + h) - f x) / h \<ge> 0"
   5.125 +      by (cases h rule: linorder_cases[of _ 0])
   5.126 +         (simp_all add: divide_nonpos_neg divide_nonneg_pos mono_onD field_simps)
   5.127 +  qed
   5.128 +qed simp
   5.129 +
   5.130 +lemma strict_mono_on_imp_mono_on:
   5.131 +  "strict_mono_on (f :: (_ :: linorder) \<Rightarrow> _ :: preorder) A \<Longrightarrow> mono_on f A"
   5.132 +  by (rule mono_onI, rule strict_mono_on_leD)
   5.133 +
   5.134 +lemma mono_on_ctble_discont:
   5.135 +  fixes f :: "real \<Rightarrow> real"
   5.136 +  fixes A :: "real set"
   5.137 +  assumes "mono_on f A"
   5.138 +  shows "countable {a\<in>A. \<not> continuous (at a within A) f}"
   5.139 +proof -
   5.140 +  have mono: "\<And>x y. x \<in> A \<Longrightarrow> y \<in> A \<Longrightarrow> x \<le> y \<Longrightarrow> f x \<le> f y"
   5.141 +    using \<open>mono_on f A\<close> by (simp add: mono_on_def)
   5.142 +  have "\<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}. \<exists>q :: nat \<times> rat.
   5.143 +      (fst q = 0 \<and> of_rat (snd q) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd q))) \<or>
   5.144 +      (fst q = 1 \<and> of_rat (snd q) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd q)))"
   5.145 +  proof (clarsimp simp del: One_nat_def)
   5.146 +    fix a assume "a \<in> A" assume "\<not> continuous (at a within A) f"
   5.147 +    thus "\<exists>q1 q2.
   5.148 +            q1 = 0 \<and> real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2) \<or>
   5.149 +            q1 = 1 \<and> f a < real_of_rat q2 \<and> (\<forall>x\<in>A. a < x \<longrightarrow> real_of_rat q2 < f x)"
   5.150 +    proof (auto simp add: continuous_within order_tendsto_iff eventually_at)
   5.151 +      fix l assume "l < f a"
   5.152 +      then obtain q2 where q2: "l < of_rat q2" "of_rat q2 < f a"
   5.153 +        using of_rat_dense by blast
   5.154 +      assume * [rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> l < f x"
   5.155 +      from q2 have "real_of_rat q2 < f a \<and> (\<forall>x\<in>A. x < a \<longrightarrow> f x < real_of_rat q2)"
   5.156 +      proof auto
   5.157 +        fix x assume "x \<in> A" "x < a"
   5.158 +        with q2 *[of "a - x"] show "f x < real_of_rat q2"
   5.159 +          apply (auto simp add: dist_real_def not_less)
   5.160 +          apply (subgoal_tac "f x \<le> f xa")
   5.161 +          by (auto intro: mono)
   5.162 +      qed
   5.163 +      thus ?thesis by auto
   5.164 +    next
   5.165 +      fix u assume "u > f a"
   5.166 +      then obtain q2 where q2: "f a < of_rat q2" "of_rat q2 < u"
   5.167 +        using of_rat_dense by blast
   5.168 +      assume *[rule_format]: "\<forall>d>0. \<exists>x\<in>A. x \<noteq> a \<and> dist x a < d \<and> \<not> u > f x"
   5.169 +      from q2 have "real_of_rat q2 > f a \<and> (\<forall>x\<in>A. x > a \<longrightarrow> f x > real_of_rat q2)"
   5.170 +      proof auto
   5.171 +        fix x assume "x \<in> A" "x > a"
   5.172 +        with q2 *[of "x - a"] show "f x > real_of_rat q2"
   5.173 +          apply (auto simp add: dist_real_def)
   5.174 +          apply (subgoal_tac "f x \<ge> f xa")
   5.175 +          by (auto intro: mono)
   5.176 +      qed
   5.177 +      thus ?thesis by auto
   5.178 +    qed
   5.179 +  qed
   5.180 +  hence "\<exists>g :: real \<Rightarrow> nat \<times> rat . \<forall>a \<in> {a\<in>A. \<not> continuous (at a within A) f}.
   5.181 +      (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (\<forall>x \<in> A. x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   5.182 +      (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (\<forall>x \<in> A. x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   5.183 +    by (rule bchoice)
   5.184 +  then guess g ..
   5.185 +  hence g: "\<And>a x. a \<in> A \<Longrightarrow> \<not> continuous (at a within A) f \<Longrightarrow> x \<in> A \<Longrightarrow>
   5.186 +      (fst (g a) = 0 \<and> of_rat (snd (g a)) < f a \<and> (x < a \<longrightarrow> f x < of_rat (snd (g a)))) |
   5.187 +      (fst (g a) = 1 \<and> of_rat (snd (g a)) > f a \<and> (x > a \<longrightarrow> f x > of_rat (snd (g a))))"
   5.188 +    by auto
   5.189 +  have "inj_on g {a\<in>A. \<not> continuous (at a within A) f}"
   5.190 +  proof (auto simp add: inj_on_def)
   5.191 +    fix w z
   5.192 +    assume 1: "w \<in> A" and 2: "\<not> continuous (at w within A) f" and
   5.193 +           3: "z \<in> A" and 4: "\<not> continuous (at z within A) f" and
   5.194 +           5: "g w = g z"
   5.195 +    from g [OF 1 2 3] g [OF 3 4 1] 5
   5.196 +    show "w = z" by auto
   5.197 +  qed
   5.198 +  thus ?thesis
   5.199 +    by (rule countableI')
   5.200 +qed
   5.201 +
   5.202 +lemma mono_on_ctble_discont_open:
   5.203 +  fixes f :: "real \<Rightarrow> real"
   5.204 +  fixes A :: "real set"
   5.205 +  assumes "open A" "mono_on f A"
   5.206 +  shows "countable {a\<in>A. \<not>isCont f a}"
   5.207 +proof -
   5.208 +  have "{a\<in>A. \<not>isCont f a} = {a\<in>A. \<not>(continuous (at a within A) f)}"
   5.209 +    by (auto simp add: continuous_within_open [OF _ \<open>open A\<close>])
   5.210 +  thus ?thesis
   5.211 +    apply (elim ssubst)
   5.212 +    by (rule mono_on_ctble_discont, rule assms)
   5.213 +qed
   5.214 +
   5.215 +lemma mono_ctble_discont:
   5.216 +  fixes f :: "real \<Rightarrow> real"
   5.217 +  assumes "mono f"
   5.218 +  shows "countable {a. \<not> isCont f a}"
   5.219 +using assms mono_on_ctble_discont [of f UNIV] unfolding mono_on_def mono_def by auto
   5.220 +
   5.221 +lemma has_real_derivative_imp_continuous_on:
   5.222 +  assumes "\<And>x. x \<in> A \<Longrightarrow> (f has_real_derivative f' x) (at x)"
   5.223 +  shows "continuous_on A f"
   5.224 +  apply (intro differentiable_imp_continuous_on, unfold differentiable_on_def)
   5.225 +  apply (intro ballI Deriv.differentiableI)
   5.226 +  apply (rule has_field_derivative_subset[OF assms])
   5.227 +  apply simp_all
   5.228 +  done
   5.229 +
   5.230 +lemma closure_contains_Sup:
   5.231 +  fixes S :: "real set"
   5.232 +  assumes "S \<noteq> {}" "bdd_above S"
   5.233 +  shows "Sup S \<in> closure S"
   5.234 +proof-
   5.235 +  have "Inf (uminus ` S) \<in> closure (uminus ` S)"
   5.236 +      using assms by (intro closure_contains_Inf) auto
   5.237 +  also have "Inf (uminus ` S) = -Sup S" by (simp add: Inf_real_def)
   5.238 +  also have "closure (uminus ` S) = uminus ` closure S"
   5.239 +      by (rule sym, intro closure_injective_linear_image) (auto intro: linearI)
   5.240 +  finally show ?thesis by auto
   5.241 +qed
   5.242 +
   5.243 +lemma closed_contains_Sup:
   5.244 +  fixes S :: "real set"
   5.245 +  shows "S \<noteq> {} \<Longrightarrow> bdd_above S \<Longrightarrow> closed S \<Longrightarrow> Sup S \<in> S"
   5.246 +  by (subst closure_closed[symmetric], assumption, rule closure_contains_Sup)
   5.247 +
   5.248 +lemma deriv_nonneg_imp_mono:
   5.249 +  assumes deriv: "\<And>x. x \<in> {a..b} \<Longrightarrow> (g has_real_derivative g' x) (at x)"
   5.250 +  assumes nonneg: "\<And>x. x \<in> {a..b} \<Longrightarrow> g' x \<ge> 0"
   5.251 +  assumes ab: "a \<le> b"
   5.252 +  shows "g a \<le> g b"
   5.253 +proof (cases "a < b")
   5.254 +  assume "a < b"
   5.255 +  from deriv have "\<forall>x. x \<ge> a \<and> x \<le> b \<longrightarrow> (g has_real_derivative g' x) (at x)" by simp
   5.256 +  from MVT2[OF \<open>a < b\<close> this] and deriv
   5.257 +    obtain \<xi> where \<xi>_ab: "\<xi> > a" "\<xi> < b" and g_ab: "g b - g a = (b - a) * g' \<xi>" by blast
   5.258 +  from \<xi>_ab ab nonneg have "(b - a) * g' \<xi> \<ge> 0" by simp
   5.259 +  with g_ab show ?thesis by simp
   5.260 +qed (insert ab, simp)
   5.261 +
   5.262 +lemma continuous_interval_vimage_Int:
   5.263 +  assumes "continuous_on {a::real..b} g" and mono: "\<And>x y. a \<le> x \<Longrightarrow> x \<le> y \<Longrightarrow> y \<le> b \<Longrightarrow> g x \<le> g y"
   5.264 +  assumes "a \<le> b" "(c::real) \<le> d" "{c..d} \<subseteq> {g a..g b}"
   5.265 +  obtains c' d' where "{a..b} \<inter> g -` {c..d} = {c'..d'}" "c' \<le> d'" "g c' = c" "g d' = d"
   5.266 +proof-
   5.267 +  let ?A = "{a..b} \<inter> g -` {c..d}"
   5.268 +  from IVT'[of g a c b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   5.269 +  obtain c'' where c'': "c'' \<in> ?A" "g c'' = c" by auto
   5.270 +  from IVT'[of g a d b, OF _ _ \<open>a \<le> b\<close> assms(1)] assms(4,5)
   5.271 +  obtain d'' where d'': "d'' \<in> ?A" "g d'' = d" by auto
   5.272 +  hence [simp]: "?A \<noteq> {}" by blast
   5.273 +
   5.274 +  define c' where "c' = Inf ?A"
   5.275 +  define d' where "d' = Sup ?A"
   5.276 +  have "?A \<subseteq> {c'..d'}" unfolding c'_def d'_def
   5.277 +    by (intro subsetI) (auto intro: cInf_lower cSup_upper)
   5.278 +  moreover from assms have "closed ?A"
   5.279 +    using continuous_on_closed_vimage[of "{a..b}" g] by (subst Int_commute) simp
   5.280 +  hence c'd'_in_set: "c' \<in> ?A" "d' \<in> ?A" unfolding c'_def d'_def
   5.281 +    by ((intro closed_contains_Inf closed_contains_Sup, simp_all)[])+
   5.282 +  hence "{c'..d'} \<subseteq> ?A" using assms
   5.283 +    by (intro subsetI)
   5.284 +       (auto intro!: order_trans[of c "g c'" "g x" for x] order_trans[of "g x" "g d'" d for x]
   5.285 +             intro!: mono)
   5.286 +  moreover have "c' \<le> d'" using c'd'_in_set(2) unfolding c'_def by (intro cInf_lower) auto
   5.287 +  moreover have "g c' \<le> c" "g d' \<ge> d"
   5.288 +    apply (insert c'' d'' c'd'_in_set)
   5.289 +    apply (subst c''(2)[symmetric])
   5.290 +    apply (auto simp: c'_def intro!: mono cInf_lower c'') []
   5.291 +    apply (subst d''(2)[symmetric])
   5.292 +    apply (auto simp: d'_def intro!: mono cSup_upper d'') []
   5.293 +    done
   5.294 +  with c'd'_in_set have "g c' = c" "g d' = d" by auto
   5.295 +  ultimately show ?thesis using that by blast
   5.296 +qed
   5.297 +
   5.298 +subsection \<open>Generic Borel spaces\<close>
   5.299 +
   5.300 +definition (in topological_space) borel :: "'a measure" where
   5.301 +  "borel = sigma UNIV {S. open S}"
   5.302 +
   5.303 +abbreviation "borel_measurable M \<equiv> measurable M borel"
   5.304 +
   5.305 +lemma in_borel_measurable:
   5.306 +   "f \<in> borel_measurable M \<longleftrightarrow>
   5.307 +    (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
   5.308 +  by (auto simp add: measurable_def borel_def)
   5.309 +
   5.310 +lemma in_borel_measurable_borel:
   5.311 +   "f \<in> borel_measurable M \<longleftrightarrow>
   5.312 +    (\<forall>S \<in> sets borel.
   5.313 +      f -` S \<inter> space M \<in> sets M)"
   5.314 +  by (auto simp add: measurable_def borel_def)
   5.315 +
   5.316 +lemma space_borel[simp]: "space borel = UNIV"
   5.317 +  unfolding borel_def by auto
   5.318 +
   5.319 +lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
   5.320 +  unfolding borel_def by auto
   5.321 +
   5.322 +lemma sets_borel: "sets borel = sigma_sets UNIV {S. open S}"
   5.323 +  unfolding borel_def by (rule sets_measure_of) simp
   5.324 +
   5.325 +lemma measurable_sets_borel:
   5.326 +    "\<lbrakk>f \<in> measurable borel M; A \<in> sets M\<rbrakk> \<Longrightarrow> f -` A \<in> sets borel"
   5.327 +  by (drule (1) measurable_sets) simp
   5.328 +
   5.329 +lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
   5.330 +  unfolding borel_def pred_def by auto
   5.331 +
   5.332 +lemma borel_open[measurable (raw generic)]:
   5.333 +  assumes "open A" shows "A \<in> sets borel"
   5.334 +proof -
   5.335 +  have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
   5.336 +  thus ?thesis unfolding borel_def by auto
   5.337 +qed
   5.338 +
   5.339 +lemma borel_closed[measurable (raw generic)]:
   5.340 +  assumes "closed A" shows "A \<in> sets borel"
   5.341 +proof -
   5.342 +  have "space borel - (- A) \<in> sets borel"
   5.343 +    using assms unfolding closed_def by (blast intro: borel_open)
   5.344 +  thus ?thesis by simp
   5.345 +qed
   5.346 +
   5.347 +lemma borel_singleton[measurable]:
   5.348 +  "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
   5.349 +  unfolding insert_def by (rule sets.Un) auto
   5.350 +
   5.351 +lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
   5.352 +  unfolding Compl_eq_Diff_UNIV by simp
   5.353 +
   5.354 +lemma borel_measurable_vimage:
   5.355 +  fixes f :: "'a \<Rightarrow> 'x::t2_space"
   5.356 +  assumes borel[measurable]: "f \<in> borel_measurable M"
   5.357 +  shows "f -` {x} \<inter> space M \<in> sets M"
   5.358 +  by simp
   5.359 +
   5.360 +lemma borel_measurableI:
   5.361 +  fixes f :: "'a \<Rightarrow> 'x::topological_space"
   5.362 +  assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
   5.363 +  shows "f \<in> borel_measurable M"
   5.364 +  unfolding borel_def
   5.365 +proof (rule measurable_measure_of, simp_all)
   5.366 +  fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
   5.367 +    using assms[of S] by simp
   5.368 +qed
   5.369 +
   5.370 +lemma borel_measurable_const:
   5.371 +  "(\<lambda>x. c) \<in> borel_measurable M"
   5.372 +  by auto
   5.373 +
   5.374 +lemma borel_measurable_indicator:
   5.375 +  assumes A: "A \<in> sets M"
   5.376 +  shows "indicator A \<in> borel_measurable M"
   5.377 +  unfolding indicator_def [abs_def] using A
   5.378 +  by (auto intro!: measurable_If_set)
   5.379 +
   5.380 +lemma borel_measurable_count_space[measurable (raw)]:
   5.381 +  "f \<in> borel_measurable (count_space S)"
   5.382 +  unfolding measurable_def by auto
   5.383 +
   5.384 +lemma borel_measurable_indicator'[measurable (raw)]:
   5.385 +  assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
   5.386 +  shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
   5.387 +  unfolding indicator_def[abs_def]
   5.388 +  by (auto intro!: measurable_If)
   5.389 +
   5.390 +lemma borel_measurable_indicator_iff:
   5.391 +  "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   5.392 +    (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   5.393 +proof
   5.394 +  assume "?I \<in> borel_measurable M"
   5.395 +  then have "?I -` {1} \<inter> space M \<in> sets M"
   5.396 +    unfolding measurable_def by auto
   5.397 +  also have "?I -` {1} \<inter> space M = A \<inter> space M"
   5.398 +    unfolding indicator_def [abs_def] by auto
   5.399 +  finally show "A \<inter> space M \<in> sets M" .
   5.400 +next
   5.401 +  assume "A \<inter> space M \<in> sets M"
   5.402 +  moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   5.403 +    (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   5.404 +    by (intro measurable_cong) (auto simp: indicator_def)
   5.405 +  ultimately show "?I \<in> borel_measurable M" by auto
   5.406 +qed
   5.407 +
   5.408 +lemma borel_measurable_subalgebra:
   5.409 +  assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   5.410 +  shows "f \<in> borel_measurable M"
   5.411 +  using assms unfolding measurable_def by auto
   5.412 +
   5.413 +lemma borel_measurable_restrict_space_iff_ereal:
   5.414 +  fixes f :: "'a \<Rightarrow> ereal"
   5.415 +  assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   5.416 +  shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   5.417 +    (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   5.418 +  by (subst measurable_restrict_space_iff)
   5.419 +     (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   5.420 +
   5.421 +lemma borel_measurable_restrict_space_iff_ennreal:
   5.422 +  fixes f :: "'a \<Rightarrow> ennreal"
   5.423 +  assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   5.424 +  shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   5.425 +    (\<lambda>x. f x * indicator \<Omega> x) \<in> borel_measurable M"
   5.426 +  by (subst measurable_restrict_space_iff)
   5.427 +     (auto simp: indicator_def if_distrib[where f="\<lambda>x. a * x" for a] cong del: if_weak_cong)
   5.428 +
   5.429 +lemma borel_measurable_restrict_space_iff:
   5.430 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_vector"
   5.431 +  assumes \<Omega>[measurable, simp]: "\<Omega> \<inter> space M \<in> sets M"
   5.432 +  shows "f \<in> borel_measurable (restrict_space M \<Omega>) \<longleftrightarrow>
   5.433 +    (\<lambda>x. indicator \<Omega> x *\<^sub>R f x) \<in> borel_measurable M"
   5.434 +  by (subst measurable_restrict_space_iff)
   5.435 +     (auto simp: indicator_def if_distrib[where f="\<lambda>x. x *\<^sub>R a" for a] ac_simps
   5.436 +       cong del: if_weak_cong)
   5.437 +
   5.438 +lemma cbox_borel[measurable]: "cbox a b \<in> sets borel"
   5.439 +  by (auto intro: borel_closed)
   5.440 +
   5.441 +lemma box_borel[measurable]: "box a b \<in> sets borel"
   5.442 +  by (auto intro: borel_open)
   5.443 +
   5.444 +lemma borel_compact: "compact (A::'a::t2_space set) \<Longrightarrow> A \<in> sets borel"
   5.445 +  by (auto intro: borel_closed dest!: compact_imp_closed)
   5.446 +
   5.447 +lemma borel_sigma_sets_subset:
   5.448 +  "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   5.449 +  using sets.sigma_sets_subset[of A borel] by simp
   5.450 +
   5.451 +lemma borel_eq_sigmaI1:
   5.452 +  fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   5.453 +  assumes borel_eq: "borel = sigma UNIV X"
   5.454 +  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   5.455 +  assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   5.456 +  shows "borel = sigma UNIV (F ` A)"
   5.457 +  unfolding borel_def
   5.458 +proof (intro sigma_eqI antisym)
   5.459 +  have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   5.460 +    unfolding borel_def by simp
   5.461 +  also have "\<dots> = sigma_sets UNIV X"
   5.462 +    unfolding borel_eq by simp
   5.463 +  also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   5.464 +    using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   5.465 +  finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   5.466 +  show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   5.467 +    unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   5.468 +qed auto
   5.469 +
   5.470 +lemma borel_eq_sigmaI2:
   5.471 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   5.472 +    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   5.473 +  assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   5.474 +  assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   5.475 +  assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   5.476 +  shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   5.477 +  using assms
   5.478 +  by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   5.479 +
   5.480 +lemma borel_eq_sigmaI3:
   5.481 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   5.482 +  assumes borel_eq: "borel = sigma UNIV X"
   5.483 +  assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   5.484 +  assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   5.485 +  shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   5.486 +  using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   5.487 +
   5.488 +lemma borel_eq_sigmaI4:
   5.489 +  fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   5.490 +    and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   5.491 +  assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   5.492 +  assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   5.493 +  assumes F: "\<And>i. F i \<in> sets borel"
   5.494 +  shows "borel = sigma UNIV (range F)"
   5.495 +  using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   5.496 +
   5.497 +lemma borel_eq_sigmaI5:
   5.498 +  fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   5.499 +  assumes borel_eq: "borel = sigma UNIV (range G)"
   5.500 +  assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   5.501 +  assumes F: "\<And>i j. F i j \<in> sets borel"
   5.502 +  shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   5.503 +  using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   5.504 +
   5.505 +lemma second_countable_borel_measurable:
   5.506 +  fixes X :: "'a::second_countable_topology set set"
   5.507 +  assumes eq: "open = generate_topology X"
   5.508 +  shows "borel = sigma UNIV X"
   5.509 +  unfolding borel_def
   5.510 +proof (intro sigma_eqI sigma_sets_eqI)
   5.511 +  interpret X: sigma_algebra UNIV "sigma_sets UNIV X"
   5.512 +    by (rule sigma_algebra_sigma_sets) simp
   5.513 +
   5.514 +  fix S :: "'a set" assume "S \<in> Collect open"
   5.515 +  then have "generate_topology X S"
   5.516 +    by (auto simp: eq)
   5.517 +  then show "S \<in> sigma_sets UNIV X"
   5.518 +  proof induction
   5.519 +    case (UN K)
   5.520 +    then have K: "\<And>k. k \<in> K \<Longrightarrow> open k"
   5.521 +      unfolding eq by auto
   5.522 +    from ex_countable_basis obtain B :: "'a set set" where
   5.523 +      B:  "\<And>b. b \<in> B \<Longrightarrow> open b" "\<And>X. open X \<Longrightarrow> \<exists>b\<subseteq>B. (\<Union>b) = X" and "countable B"
   5.524 +      by (auto simp: topological_basis_def)
   5.525 +    from B(2)[OF K] obtain m where m: "\<And>k. k \<in> K \<Longrightarrow> m k \<subseteq> B" "\<And>k. k \<in> K \<Longrightarrow> (\<Union>m k) = k"
   5.526 +      by metis
   5.527 +    define U where "U = (\<Union>k\<in>K. m k)"
   5.528 +    with m have "countable U"
   5.529 +      by (intro countable_subset[OF _ \<open>countable B\<close>]) auto
   5.530 +    have "\<Union>U = (\<Union>A\<in>U. A)" by simp
   5.531 +    also have "\<dots> = \<Union>K"
   5.532 +      unfolding U_def UN_simps by (simp add: m)
   5.533 +    finally have "\<Union>U = \<Union>K" .
   5.534 +
   5.535 +    have "\<forall>b\<in>U. \<exists>k\<in>K. b \<subseteq> k"
   5.536 +      using m by (auto simp: U_def)
   5.537 +    then obtain u where u: "\<And>b. b \<in> U \<Longrightarrow> u b \<in> K" and "\<And>b. b \<in> U \<Longrightarrow> b \<subseteq> u b"
   5.538 +      by metis
   5.539 +    then have "(\<Union>b\<in>U. u b) \<subseteq> \<Union>K" "\<Union>U \<subseteq> (\<Union>b\<in>U. u b)"
   5.540 +      by auto
   5.541 +    then have "\<Union>K = (\<Union>b\<in>U. u b)"
   5.542 +      unfolding \<open>\<Union>U = \<Union>K\<close> by auto
   5.543 +    also have "\<dots> \<in> sigma_sets UNIV X"
   5.544 +      using u UN by (intro X.countable_UN' \<open>countable U\<close>) auto
   5.545 +    finally show "\<Union>K \<in> sigma_sets UNIV X" .
   5.546 +  qed auto
   5.547 +qed (auto simp: eq intro: generate_topology.Basis)
   5.548 +
   5.549 +lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   5.550 +  unfolding borel_def
   5.551 +proof (intro sigma_eqI sigma_sets_eqI, safe)
   5.552 +  fix x :: "'a set" assume "open x"
   5.553 +  hence "x = UNIV - (UNIV - x)" by auto
   5.554 +  also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   5.555 +    by (force intro: sigma_sets.Compl simp: \<open>open x\<close>)
   5.556 +  finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   5.557 +next
   5.558 +  fix x :: "'a set" assume "closed x"
   5.559 +  hence "x = UNIV - (UNIV - x)" by auto
   5.560 +  also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   5.561 +    by (force intro: sigma_sets.Compl simp: \<open>closed x\<close>)
   5.562 +  finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   5.563 +qed simp_all
   5.564 +
   5.565 +lemma borel_eq_countable_basis:
   5.566 +  fixes B::"'a::topological_space set set"
   5.567 +  assumes "countable B"
   5.568 +  assumes "topological_basis B"
   5.569 +  shows "borel = sigma UNIV B"
   5.570 +  unfolding borel_def
   5.571 +proof (intro sigma_eqI sigma_sets_eqI, safe)
   5.572 +  interpret countable_basis using assms by unfold_locales
   5.573 +  fix X::"'a set" assume "open X"
   5.574 +  from open_countable_basisE[OF this] guess B' . note B' = this
   5.575 +  then show "X \<in> sigma_sets UNIV B"
   5.576 +    by (blast intro: sigma_sets_UNION \<open>countable B\<close> countable_subset)
   5.577 +next
   5.578 +  fix b assume "b \<in> B"
   5.579 +  hence "open b" by (rule topological_basis_open[OF assms(2)])
   5.580 +  thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   5.581 +qed simp_all
   5.582 +
   5.583 +lemma borel_measurable_continuous_on_restrict:
   5.584 +  fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   5.585 +  assumes f: "continuous_on A f"
   5.586 +  shows "f \<in> borel_measurable (restrict_space borel A)"
   5.587 +proof (rule borel_measurableI)
   5.588 +  fix S :: "'b set" assume "open S"
   5.589 +  with f obtain T where "f -` S \<inter> A = T \<inter> A" "open T"
   5.590 +    by (metis continuous_on_open_invariant)
   5.591 +  then show "f -` S \<inter> space (restrict_space borel A) \<in> sets (restrict_space borel A)"
   5.592 +    by (force simp add: sets_restrict_space space_restrict_space)
   5.593 +qed
   5.594 +
   5.595 +lemma borel_measurable_continuous_on1: "continuous_on UNIV f \<Longrightarrow> f \<in> borel_measurable borel"
   5.596 +  by (drule borel_measurable_continuous_on_restrict) simp
   5.597 +
   5.598 +lemma borel_measurable_continuous_on_if:
   5.599 +  "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> continuous_on (- A) g \<Longrightarrow>
   5.600 +    (\<lambda>x. if x \<in> A then f x else g x) \<in> borel_measurable borel"
   5.601 +  by (auto simp add: measurable_If_restrict_space_iff Collect_neg_eq
   5.602 +           intro!: borel_measurable_continuous_on_restrict)
   5.603 +
   5.604 +lemma borel_measurable_continuous_countable_exceptions:
   5.605 +  fixes f :: "'a::t1_space \<Rightarrow> 'b::topological_space"
   5.606 +  assumes X: "countable X"
   5.607 +  assumes "continuous_on (- X) f"
   5.608 +  shows "f \<in> borel_measurable borel"
   5.609 +proof (rule measurable_discrete_difference[OF _ X])
   5.610 +  have "X \<in> sets borel"
   5.611 +    by (rule sets.countable[OF _ X]) auto
   5.612 +  then show "(\<lambda>x. if x \<in> X then undefined else f x) \<in> borel_measurable borel"
   5.613 +    by (intro borel_measurable_continuous_on_if assms continuous_intros)
   5.614 +qed auto
   5.615 +
   5.616 +lemma borel_measurable_continuous_on:
   5.617 +  assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   5.618 +  shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   5.619 +  using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   5.620 +
   5.621 +lemma borel_measurable_continuous_on_indicator:
   5.622 +  fixes f g :: "'a::topological_space \<Rightarrow> 'b::real_normed_vector"
   5.623 +  shows "A \<in> sets borel \<Longrightarrow> continuous_on A f \<Longrightarrow> (\<lambda>x. indicator A x *\<^sub>R f x) \<in> borel_measurable borel"
   5.624 +  by (subst borel_measurable_restrict_space_iff[symmetric])
   5.625 +     (auto intro: borel_measurable_continuous_on_restrict)
   5.626 +
   5.627 +lemma borel_measurable_Pair[measurable (raw)]:
   5.628 +  fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   5.629 +  assumes f[measurable]: "f \<in> borel_measurable M"
   5.630 +  assumes g[measurable]: "g \<in> borel_measurable M"
   5.631 +  shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   5.632 +proof (subst borel_eq_countable_basis)
   5.633 +  let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   5.634 +  let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   5.635 +  let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   5.636 +  show "countable ?P" "topological_basis ?P"
   5.637 +    by (auto intro!: countable_basis topological_basis_prod is_basis)
   5.638 +
   5.639 +  show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   5.640 +  proof (rule measurable_measure_of)
   5.641 +    fix S assume "S \<in> ?P"
   5.642 +    then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   5.643 +    then have borel: "open b" "open c"
   5.644 +      by (auto intro: is_basis topological_basis_open)
   5.645 +    have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   5.646 +      unfolding S by auto
   5.647 +    also have "\<dots> \<in> sets M"
   5.648 +      using borel by simp
   5.649 +    finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   5.650 +  qed auto
   5.651 +qed
   5.652 +
   5.653 +lemma borel_measurable_continuous_Pair:
   5.654 +  fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   5.655 +  assumes [measurable]: "f \<in> borel_measurable M"
   5.656 +  assumes [measurable]: "g \<in> borel_measurable M"
   5.657 +  assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   5.658 +  shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   5.659 +proof -
   5.660 +  have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   5.661 +  show ?thesis
   5.662 +    unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   5.663 +qed
   5.664 +
   5.665 +subsection \<open>Borel spaces on order topologies\<close>
   5.666 +
   5.667 +lemma [measurable]:
   5.668 +  fixes a b :: "'a::linorder_topology"
   5.669 +  shows lessThan_borel: "{..< a} \<in> sets borel"
   5.670 +    and greaterThan_borel: "{a <..} \<in> sets borel"
   5.671 +    and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   5.672 +    and atMost_borel: "{..a} \<in> sets borel"
   5.673 +    and atLeast_borel: "{a..} \<in> sets borel"
   5.674 +    and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   5.675 +    and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   5.676 +    and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   5.677 +  unfolding greaterThanAtMost_def atLeastLessThan_def
   5.678 +  by (blast intro: borel_open borel_closed open_lessThan open_greaterThan open_greaterThanLessThan
   5.679 +                   closed_atMost closed_atLeast closed_atLeastAtMost)+
   5.680 +
   5.681 +lemma borel_Iio:
   5.682 +  "borel = sigma UNIV (range lessThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   5.683 +  unfolding second_countable_borel_measurable[OF open_generated_order]
   5.684 +proof (intro sigma_eqI sigma_sets_eqI)
   5.685 +  from countable_dense_setE guess D :: "'a set" . note D = this
   5.686 +
   5.687 +  interpret L: sigma_algebra UNIV "sigma_sets UNIV (range lessThan)"
   5.688 +    by (rule sigma_algebra_sigma_sets) simp
   5.689 +
   5.690 +  fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   5.691 +  then obtain y where "A = {y <..} \<or> A = {..< y}"
   5.692 +    by blast
   5.693 +  then show "A \<in> sigma_sets UNIV (range lessThan)"
   5.694 +  proof
   5.695 +    assume A: "A = {y <..}"
   5.696 +    show ?thesis
   5.697 +    proof cases
   5.698 +      assume "\<forall>x>y. \<exists>d. y < d \<and> d < x"
   5.699 +      with D(2)[of "{y <..< x}" for x] have "\<forall>x>y. \<exists>d\<in>D. y < d \<and> d < x"
   5.700 +        by (auto simp: set_eq_iff)
   5.701 +      then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. y < d}. {..< d})"
   5.702 +        by (auto simp: A) (metis less_asym)
   5.703 +      also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   5.704 +        using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   5.705 +      finally show ?thesis .
   5.706 +    next
   5.707 +      assume "\<not> (\<forall>x>y. \<exists>d. y < d \<and> d < x)"
   5.708 +      then obtain x where "y < x"  "\<And>d. y < d \<Longrightarrow> \<not> d < x"
   5.709 +        by auto
   5.710 +      then have "A = UNIV - {..< x}"
   5.711 +        unfolding A by (auto simp: not_less[symmetric])
   5.712 +      also have "\<dots> \<in> sigma_sets UNIV (range lessThan)"
   5.713 +        by auto
   5.714 +      finally show ?thesis .
   5.715 +    qed
   5.716 +  qed auto
   5.717 +qed auto
   5.718 +
   5.719 +lemma borel_Ioi:
   5.720 +  "borel = sigma UNIV (range greaterThan :: 'a::{linorder_topology, second_countable_topology} set set)"
   5.721 +  unfolding second_countable_borel_measurable[OF open_generated_order]
   5.722 +proof (intro sigma_eqI sigma_sets_eqI)
   5.723 +  from countable_dense_setE guess D :: "'a set" . note D = this
   5.724 +
   5.725 +  interpret L: sigma_algebra UNIV "sigma_sets UNIV (range greaterThan)"
   5.726 +    by (rule sigma_algebra_sigma_sets) simp
   5.727 +
   5.728 +  fix A :: "'a set" assume "A \<in> range lessThan \<union> range greaterThan"
   5.729 +  then obtain y where "A = {y <..} \<or> A = {..< y}"
   5.730 +    by blast
   5.731 +  then show "A \<in> sigma_sets UNIV (range greaterThan)"
   5.732 +  proof
   5.733 +    assume A: "A = {..< y}"
   5.734 +    show ?thesis
   5.735 +    proof cases
   5.736 +      assume "\<forall>x<y. \<exists>d. x < d \<and> d < y"
   5.737 +      with D(2)[of "{x <..< y}" for x] have "\<forall>x<y. \<exists>d\<in>D. x < d \<and> d < y"
   5.738 +        by (auto simp: set_eq_iff)
   5.739 +      then have "A = UNIV - (\<Inter>d\<in>{d\<in>D. d < y}. {d <..})"
   5.740 +        by (auto simp: A) (metis less_asym)
   5.741 +      also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   5.742 +        using D(1) by (intro L.Diff L.top L.countable_INT'') auto
   5.743 +      finally show ?thesis .
   5.744 +    next
   5.745 +      assume "\<not> (\<forall>x<y. \<exists>d. x < d \<and> d < y)"
   5.746 +      then obtain x where "x < y"  "\<And>d. y > d \<Longrightarrow> x \<ge> d"
   5.747 +        by (auto simp: not_less[symmetric])
   5.748 +      then have "A = UNIV - {x <..}"
   5.749 +        unfolding A Compl_eq_Diff_UNIV[symmetric] by auto
   5.750 +      also have "\<dots> \<in> sigma_sets UNIV (range greaterThan)"
   5.751 +        by auto
   5.752 +      finally show ?thesis .
   5.753 +    qed
   5.754 +  qed auto
   5.755 +qed auto
   5.756 +
   5.757 +lemma borel_measurableI_less:
   5.758 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   5.759 +  shows "(\<And>y. {x\<in>space M. f x < y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   5.760 +  unfolding borel_Iio
   5.761 +  by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   5.762 +
   5.763 +lemma borel_measurableI_greater:
   5.764 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   5.765 +  shows "(\<And>y. {x\<in>space M. y < f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   5.766 +  unfolding borel_Ioi
   5.767 +  by (rule measurable_measure_of) (auto simp: Int_def conj_commute)
   5.768 +
   5.769 +lemma borel_measurableI_le:
   5.770 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   5.771 +  shows "(\<And>y. {x\<in>space M. f x \<le> y} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   5.772 +  by (rule borel_measurableI_greater) (auto simp: not_le[symmetric])
   5.773 +
   5.774 +lemma borel_measurableI_ge:
   5.775 +  fixes f :: "'a \<Rightarrow> 'b::{linorder_topology, second_countable_topology}"
   5.776 +  shows "(\<And>y. {x\<in>space M. y \<le> f x} \<in> sets M) \<Longrightarrow> f \<in> borel_measurable M"
   5.777 +  by (rule borel_measurableI_less) (auto simp: not_le[symmetric])
   5.778 +
   5.779 +lemma borel_measurable_less[measurable]:
   5.780 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   5.781 +  assumes "f \<in> borel_measurable M"
   5.782 +  assumes "g \<in> borel_measurable M"
   5.783 +  shows "{w \<in> space M. f w < g w} \<in> sets M"
   5.784 +proof -
   5.785 +  have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
   5.786 +    by auto
   5.787 +  also have "\<dots> \<in> sets M"
   5.788 +    by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
   5.789 +              continuous_intros)
   5.790 +  finally show ?thesis .
   5.791 +qed
   5.792 +
   5.793 +lemma
   5.794 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
   5.795 +  assumes f[measurable]: "f \<in> borel_measurable M"
   5.796 +  assumes g[measurable]: "g \<in> borel_measurable M"
   5.797 +  shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   5.798 +    and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   5.799 +    and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   5.800 +  unfolding eq_iff not_less[symmetric]
   5.801 +  by measurable
   5.802 +
   5.803 +lemma borel_measurable_SUP[measurable (raw)]:
   5.804 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   5.805 +  assumes [simp]: "countable I"
   5.806 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   5.807 +  shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   5.808 +  by (rule borel_measurableI_greater) (simp add: less_SUP_iff)
   5.809 +
   5.810 +lemma borel_measurable_INF[measurable (raw)]:
   5.811 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> _::{complete_linorder, linorder_topology, second_countable_topology}"
   5.812 +  assumes [simp]: "countable I"
   5.813 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   5.814 +  shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   5.815 +  by (rule borel_measurableI_less) (simp add: INF_less_iff)
   5.816 +
   5.817 +lemma borel_measurable_cSUP[measurable (raw)]:
   5.818 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   5.819 +  assumes [simp]: "countable I"
   5.820 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   5.821 +  assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_above ((\<lambda>i. F i x) ` I)"
   5.822 +  shows "(\<lambda>x. SUP i:I. F i x) \<in> borel_measurable M"
   5.823 +proof cases
   5.824 +  assume "I = {}" then show ?thesis
   5.825 +    unfolding \<open>I = {}\<close> image_empty by simp
   5.826 +next
   5.827 +  assume "I \<noteq> {}"
   5.828 +  show ?thesis
   5.829 +  proof (rule borel_measurableI_le)
   5.830 +    fix y
   5.831 +    have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} \<in> sets M"
   5.832 +      by measurable
   5.833 +    also have "{x \<in> space M. \<forall>i\<in>I. F i x \<le> y} = {x \<in> space M. (SUP i:I. F i x) \<le> y}"
   5.834 +      by (simp add: cSUP_le_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   5.835 +    finally show "{x \<in> space M. (SUP i:I. F i x) \<le>  y} \<in> sets M"  .
   5.836 +  qed
   5.837 +qed
   5.838 +
   5.839 +lemma borel_measurable_cINF[measurable (raw)]:
   5.840 +  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> 'a::{conditionally_complete_linorder, linorder_topology, second_countable_topology}"
   5.841 +  assumes [simp]: "countable I"
   5.842 +  assumes [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
   5.843 +  assumes bdd: "\<And>x. x \<in> space M \<Longrightarrow> bdd_below ((\<lambda>i. F i x) ` I)"
   5.844 +  shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
   5.845 +proof cases
   5.846 +  assume "I = {}" then show ?thesis
   5.847 +    unfolding \<open>I = {}\<close> image_empty by simp
   5.848 +next
   5.849 +  assume "I \<noteq> {}"
   5.850 +  show ?thesis
   5.851 +  proof (rule borel_measurableI_ge)
   5.852 +    fix y
   5.853 +    have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} \<in> sets M"
   5.854 +      by measurable
   5.855 +    also have "{x \<in> space M. \<forall>i\<in>I. y \<le> F i x} = {x \<in> space M. y \<le> (INF i:I. F i x)}"
   5.856 +      by (simp add: le_cINF_iff \<open>I \<noteq> {}\<close> bdd cong: conj_cong)
   5.857 +    finally show "{x \<in> space M. y \<le> (INF i:I. F i x)} \<in> sets M"  .
   5.858 +  qed
   5.859 +qed
   5.860 +
   5.861 +lemma borel_measurable_lfp[consumes 1, case_names continuity step]:
   5.862 +  fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   5.863 +  assumes "sup_continuous F"
   5.864 +  assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   5.865 +  shows "lfp F \<in> borel_measurable M"
   5.866 +proof -
   5.867 +  { fix i have "((F ^^ i) bot) \<in> borel_measurable M"
   5.868 +      by (induct i) (auto intro!: *) }
   5.869 +  then have "(\<lambda>x. SUP i. (F ^^ i) bot x) \<in> borel_measurable M"
   5.870 +    by measurable
   5.871 +  also have "(\<lambda>x. SUP i. (F ^^ i) bot x) = (SUP i. (F ^^ i) bot)"
   5.872 +    by auto
   5.873 +  also have "(SUP i. (F ^^ i) bot) = lfp F"
   5.874 +    by (rule sup_continuous_lfp[symmetric]) fact
   5.875 +  finally show ?thesis .
   5.876 +qed
   5.877 +
   5.878 +lemma borel_measurable_gfp[consumes 1, case_names continuity step]:
   5.879 +  fixes F :: "('a \<Rightarrow> 'b) \<Rightarrow> ('a \<Rightarrow> 'b::{complete_linorder, linorder_topology, second_countable_topology})"
   5.880 +  assumes "inf_continuous F"
   5.881 +  assumes *: "\<And>f. f \<in> borel_measurable M \<Longrightarrow> F f \<in> borel_measurable M"
   5.882 +  shows "gfp F \<in> borel_measurable M"
   5.883 +proof -
   5.884 +  { fix i have "((F ^^ i) top) \<in> borel_measurable M"
   5.885 +      by (induct i) (auto intro!: * simp: bot_fun_def) }
   5.886 +  then have "(\<lambda>x. INF i. (F ^^ i) top x) \<in> borel_measurable M"
   5.887 +    by measurable
   5.888 +  also have "(\<lambda>x. INF i. (F ^^ i) top x) = (INF i. (F ^^ i) top)"
   5.889 +    by auto
   5.890 +  also have "\<dots> = gfp F"
   5.891 +    by (rule inf_continuous_gfp[symmetric]) fact
   5.892 +  finally show ?thesis .
   5.893 +qed
   5.894 +
   5.895 +lemma borel_measurable_max[measurable (raw)]:
   5.896 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   5.897 +  by (rule borel_measurableI_less) simp
   5.898 +
   5.899 +lemma borel_measurable_min[measurable (raw)]:
   5.900 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   5.901 +  by (rule borel_measurableI_greater) simp
   5.902 +
   5.903 +lemma borel_measurable_Min[measurable (raw)]:
   5.904 +  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. Min ((\<lambda>i. f i x)`I) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   5.905 +proof (induct I rule: finite_induct)
   5.906 +  case (insert i I) then show ?case
   5.907 +    by (cases "I = {}") auto
   5.908 +qed auto
   5.909 +
   5.910 +lemma borel_measurable_Max[measurable (raw)]:
   5.911 +  "finite I \<Longrightarrow> (\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable M) \<Longrightarrow> (\<lambda>x. Max ((\<lambda>i. f i x)`I) :: 'b::{second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   5.912 +proof (induct I rule: finite_induct)
   5.913 +  case (insert i I) then show ?case
   5.914 +    by (cases "I = {}") auto
   5.915 +qed auto
   5.916 +
   5.917 +lemma borel_measurable_sup[measurable (raw)]:
   5.918 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. sup (g x) (f x) :: 'b::{lattice, second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   5.919 +  unfolding sup_max by measurable
   5.920 +
   5.921 +lemma borel_measurable_inf[measurable (raw)]:
   5.922 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. inf (g x) (f x) :: 'b::{lattice, second_countable_topology, linorder_topology}) \<in> borel_measurable M"
   5.923 +  unfolding inf_min by measurable
   5.924 +
   5.925 +lemma [measurable (raw)]:
   5.926 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   5.927 +  assumes "\<And>i. f i \<in> borel_measurable M"
   5.928 +  shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   5.929 +    and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
   5.930 +  unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
   5.931 +
   5.932 +lemma measurable_convergent[measurable (raw)]:
   5.933 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   5.934 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   5.935 +  shows "Measurable.pred M (\<lambda>x. convergent (\<lambda>i. f i x))"
   5.936 +  unfolding convergent_ereal by measurable
   5.937 +
   5.938 +lemma sets_Collect_convergent[measurable]:
   5.939 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   5.940 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   5.941 +  shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
   5.942 +  by measurable
   5.943 +
   5.944 +lemma borel_measurable_lim[measurable (raw)]:
   5.945 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   5.946 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
   5.947 +  shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
   5.948 +proof -
   5.949 +  have "\<And>x. lim (\<lambda>i. f i x) = (if convergent (\<lambda>i. f i x) then limsup (\<lambda>i. f i x) else (THE i. False))"
   5.950 +    by (simp add: lim_def convergent_def convergent_limsup_cl)
   5.951 +  then show ?thesis
   5.952 +    by simp
   5.953 +qed
   5.954 +
   5.955 +lemma borel_measurable_LIMSEQ_order:
   5.956 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology}"
   5.957 +  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
   5.958 +  and u: "\<And>i. u i \<in> borel_measurable M"
   5.959 +  shows "u' \<in> borel_measurable M"
   5.960 +proof -
   5.961 +  have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
   5.962 +    using u' by (simp add: lim_imp_Liminf[symmetric])
   5.963 +  with u show ?thesis by (simp cong: measurable_cong)
   5.964 +qed
   5.965 +
   5.966 +subsection \<open>Borel spaces on topological monoids\<close>
   5.967 +
   5.968 +lemma borel_measurable_add[measurable (raw)]:
   5.969 +  fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, topological_monoid_add}"
   5.970 +  assumes f: "f \<in> borel_measurable M"
   5.971 +  assumes g: "g \<in> borel_measurable M"
   5.972 +  shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   5.973 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   5.974 +
   5.975 +lemma borel_measurable_setsum[measurable (raw)]:
   5.976 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, topological_comm_monoid_add}"
   5.977 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   5.978 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   5.979 +proof cases
   5.980 +  assume "finite S"
   5.981 +  thus ?thesis using assms by induct auto
   5.982 +qed simp
   5.983 +
   5.984 +lemma borel_measurable_suminf_order[measurable (raw)]:
   5.985 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{complete_linorder, second_countable_topology, linorder_topology, topological_comm_monoid_add}"
   5.986 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
   5.987 +  shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
   5.988 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
   5.989 +
   5.990 +subsection \<open>Borel spaces on Euclidean spaces\<close>
   5.991 +
   5.992 +lemma borel_measurable_inner[measurable (raw)]:
   5.993 +  fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
   5.994 +  assumes "f \<in> borel_measurable M"
   5.995 +  assumes "g \<in> borel_measurable M"
   5.996 +  shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
   5.997 +  using assms
   5.998 +  by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   5.999 +
  5.1000 +notation
  5.1001 +  eucl_less (infix "<e" 50)
  5.1002 +
  5.1003 +lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
  5.1004 +  and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
  5.1005 +  by auto
  5.1006 +
  5.1007 +lemma eucl_ivals[measurable]:
  5.1008 +  fixes a b :: "'a::ordered_euclidean_space"
  5.1009 +  shows "{x. x <e a} \<in> sets borel"
  5.1010 +    and "{x. a <e x} \<in> sets borel"
  5.1011 +    and "{..a} \<in> sets borel"
  5.1012 +    and "{a..} \<in> sets borel"
  5.1013 +    and "{a..b} \<in> sets borel"
  5.1014 +    and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
  5.1015 +    and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
  5.1016 +  unfolding box_oc box_co
  5.1017 +  by (auto intro: borel_open borel_closed)
  5.1018 +
  5.1019 +lemma
  5.1020 +  fixes i :: "'a::{second_countable_topology, real_inner}"
  5.1021 +  shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
  5.1022 +    and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
  5.1023 +    and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
  5.1024 +    and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
  5.1025 +  by simp_all
  5.1026 +
  5.1027 +lemma borel_eq_box:
  5.1028 +  "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a :: euclidean_space set))"
  5.1029 +    (is "_ = ?SIGMA")
  5.1030 +proof (rule borel_eq_sigmaI1[OF borel_def])
  5.1031 +  fix M :: "'a set" assume "M \<in> {S. open S}"
  5.1032 +  then have "open M" by simp
  5.1033 +  show "M \<in> ?SIGMA"
  5.1034 +    apply (subst open_UNION_box[OF \<open>open M\<close>])
  5.1035 +    apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
  5.1036 +    apply (auto intro: countable_rat)
  5.1037 +    done
  5.1038 +qed (auto simp: box_def)
  5.1039 +
  5.1040 +lemma halfspace_gt_in_halfspace:
  5.1041 +  assumes i: "i \<in> A"
  5.1042 +  shows "{x::'a. a < x \<bullet> i} \<in>
  5.1043 +    sigma_sets UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
  5.1044 +  (is "?set \<in> ?SIGMA")
  5.1045 +proof -
  5.1046 +  interpret sigma_algebra UNIV ?SIGMA
  5.1047 +    by (intro sigma_algebra_sigma_sets) simp_all
  5.1048 +  have *: "?set = (\<Union>n. UNIV - {x::'a. x \<bullet> i < a + 1 / real (Suc n)})"
  5.1049 +  proof (safe, simp_all add: not_less del: of_nat_Suc)
  5.1050 +    fix x :: 'a assume "a < x \<bullet> i"
  5.1051 +    with reals_Archimedean[of "x \<bullet> i - a"]
  5.1052 +    obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
  5.1053 +      by (auto simp: field_simps)
  5.1054 +    then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
  5.1055 +      by (blast intro: less_imp_le)
  5.1056 +  next
  5.1057 +    fix x n
  5.1058 +    have "a < a + 1 / real (Suc n)" by auto
  5.1059 +    also assume "\<dots> \<le> x"
  5.1060 +    finally show "a < x" .
  5.1061 +  qed
  5.1062 +  show "?set \<in> ?SIGMA" unfolding *
  5.1063 +    by (auto intro!: Diff sigma_sets_Inter i)
  5.1064 +qed
  5.1065 +
  5.1066 +lemma borel_eq_halfspace_less:
  5.1067 +  "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
  5.1068 +  (is "_ = ?SIGMA")
  5.1069 +proof (rule borel_eq_sigmaI2[OF borel_eq_box])
  5.1070 +  fix a b :: 'a
  5.1071 +  have "box a b = {x\<in>space ?SIGMA. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"
  5.1072 +    by (auto simp: box_def)
  5.1073 +  also have "\<dots> \<in> sets ?SIGMA"
  5.1074 +    by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
  5.1075 +       (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
  5.1076 +  finally show "box a b \<in> sets ?SIGMA" .
  5.1077 +qed auto
  5.1078 +
  5.1079 +lemma borel_eq_halfspace_le:
  5.1080 +  "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
  5.1081 +  (is "_ = ?SIGMA")
  5.1082 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  5.1083 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  5.1084 +  then have i: "i \<in> Basis" by auto
  5.1085 +  have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
  5.1086 +  proof (safe, simp_all del: of_nat_Suc)
  5.1087 +    fix x::'a assume *: "x\<bullet>i < a"
  5.1088 +    with reals_Archimedean[of "a - x\<bullet>i"]
  5.1089 +    obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
  5.1090 +      by (auto simp: field_simps)
  5.1091 +    then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
  5.1092 +      by (blast intro: less_imp_le)
  5.1093 +  next
  5.1094 +    fix x::'a and n
  5.1095 +    assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
  5.1096 +    also have "\<dots> < a" by auto
  5.1097 +    finally show "x\<bullet>i < a" .
  5.1098 +  qed
  5.1099 +  show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  5.1100 +    by (intro sets.countable_UN) (auto intro: i)
  5.1101 +qed auto
  5.1102 +
  5.1103 +lemma borel_eq_halfspace_ge:
  5.1104 +  "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
  5.1105 +  (is "_ = ?SIGMA")
  5.1106 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
  5.1107 +  fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
  5.1108 +  have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
  5.1109 +  show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
  5.1110 +    using i by (intro sets.compl_sets) auto
  5.1111 +qed auto
  5.1112 +
  5.1113 +lemma borel_eq_halfspace_greater:
  5.1114 +  "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
  5.1115 +  (is "_ = ?SIGMA")
  5.1116 +proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
  5.1117 +  fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
  5.1118 +  then have i: "i \<in> Basis" by auto
  5.1119 +  have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
  5.1120 +  show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  5.1121 +    by (intro sets.compl_sets) (auto intro: i)
  5.1122 +qed auto
  5.1123 +
  5.1124 +lemma borel_eq_atMost:
  5.1125 +  "borel = sigma UNIV (range (\<lambda>a. {..a::'a::ordered_euclidean_space}))"
  5.1126 +  (is "_ = ?SIGMA")
  5.1127 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  5.1128 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  5.1129 +  then have "i \<in> Basis" by auto
  5.1130 +  then have *: "{x::'a. x\<bullet>i \<le> a} = (\<Union>k::nat. {.. (\<Sum>n\<in>Basis. (if n = i then a else real k)*\<^sub>R n)})"
  5.1131 +  proof (safe, simp_all add: eucl_le[where 'a='a] split: if_split_asm)
  5.1132 +    fix x :: 'a
  5.1133 +    from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
  5.1134 +    then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
  5.1135 +      by (subst (asm) Max_le_iff) auto
  5.1136 +    then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
  5.1137 +      by (auto intro!: exI[of _ k])
  5.1138 +  qed
  5.1139 +  show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
  5.1140 +    by (intro sets.countable_UN) auto
  5.1141 +qed auto
  5.1142 +
  5.1143 +lemma borel_eq_greaterThan:
  5.1144 +  "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. a <e x}))"
  5.1145 +  (is "_ = ?SIGMA")
  5.1146 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
  5.1147 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  5.1148 +  then have i: "i \<in> Basis" by auto
  5.1149 +  have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
  5.1150 +  also have *: "{x::'a. a < x\<bullet>i} =
  5.1151 +      (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
  5.1152 +  proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  5.1153 +    fix x :: 'a
  5.1154 +    from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
  5.1155 +    guess k::nat .. note k = this
  5.1156 +    { fix i :: 'a assume "i \<in> Basis"
  5.1157 +      then have "-x\<bullet>i < real k"
  5.1158 +        using k by (subst (asm) Max_less_iff) auto
  5.1159 +      then have "- real k < x\<bullet>i" by simp }
  5.1160 +    then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
  5.1161 +      by (auto intro!: exI[of _ k])
  5.1162 +  qed
  5.1163 +  finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
  5.1164 +    apply (simp only:)
  5.1165 +    apply (intro sets.countable_UN sets.Diff)
  5.1166 +    apply (auto intro: sigma_sets_top)
  5.1167 +    done
  5.1168 +qed auto
  5.1169 +
  5.1170 +lemma borel_eq_lessThan:
  5.1171 +  "borel = sigma UNIV (range (\<lambda>a::'a::ordered_euclidean_space. {x. x <e a}))"
  5.1172 +  (is "_ = ?SIGMA")
  5.1173 +proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
  5.1174 +  fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
  5.1175 +  then have i: "i \<in> Basis" by auto
  5.1176 +  have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
  5.1177 +  also have *: "{x::'a. x\<bullet>i < a} = (\<Union>k::nat. {x. x <e (\<Sum>n\<in>Basis. (if n = i then a else real k) *\<^sub>R n)})" using \<open>i\<in> Basis\<close>
  5.1178 +  proof (safe, simp_all add: eucl_less_def split: if_split_asm)
  5.1179 +    fix x :: 'a
  5.1180 +    from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
  5.1181 +    guess k::nat .. note k = this
  5.1182 +    { fix i :: 'a assume "i \<in> Basis"
  5.1183 +      then have "x\<bullet>i < real k"
  5.1184 +        using k by (subst (asm) Max_less_iff) auto
  5.1185 +      then have "x\<bullet>i < real k" by simp }
  5.1186 +    then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
  5.1187 +      by (auto intro!: exI[of _ k])
  5.1188 +  qed
  5.1189 +  finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
  5.1190 +    apply (simp only:)
  5.1191 +    apply (intro sets.countable_UN sets.Diff)
  5.1192 +    apply (auto intro: sigma_sets_top )
  5.1193 +    done
  5.1194 +qed auto
  5.1195 +
  5.1196 +lemma borel_eq_atLeastAtMost:
  5.1197 +  "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} ::'a::ordered_euclidean_space set))"
  5.1198 +  (is "_ = ?SIGMA")
  5.1199 +proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  5.1200 +  fix a::'a
  5.1201 +  have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
  5.1202 +  proof (safe, simp_all add: eucl_le[where 'a='a])
  5.1203 +    fix x :: 'a
  5.1204 +    from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
  5.1205 +    guess k::nat .. note k = this
  5.1206 +    { fix i :: 'a assume "i \<in> Basis"
  5.1207 +      with k have "- x\<bullet>i \<le> real k"
  5.1208 +        by (subst (asm) Max_le_iff) (auto simp: field_simps)
  5.1209 +      then have "- real k \<le> x\<bullet>i" by simp }
  5.1210 +    then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
  5.1211 +      by (auto intro!: exI[of _ k])
  5.1212 +  qed
  5.1213 +  show "{..a} \<in> ?SIGMA" unfolding *
  5.1214 +    by (intro sets.countable_UN)
  5.1215 +       (auto intro!: sigma_sets_top)
  5.1216 +qed auto
  5.1217 +
  5.1218 +lemma borel_set_induct[consumes 1, case_names empty interval compl union]:
  5.1219 +  assumes "A \<in> sets borel"
  5.1220 +  assumes empty: "P {}" and int: "\<And>a b. a \<le> b \<Longrightarrow> P {a..b}" and compl: "\<And>A. A \<in> sets borel \<Longrightarrow> P A \<Longrightarrow> P (-A)" and
  5.1221 +          un: "\<And>f. disjoint_family f \<Longrightarrow> (\<And>i. f i \<in> sets borel) \<Longrightarrow>  (\<And>i. P (f i)) \<Longrightarrow> P (\<Union>i::nat. f i)"
  5.1222 +  shows "P (A::real set)"
  5.1223 +proof-
  5.1224 +  let ?G = "range (\<lambda>(a,b). {a..b::real})"
  5.1225 +  have "Int_stable ?G" "?G \<subseteq> Pow UNIV" "A \<in> sigma_sets UNIV ?G"
  5.1226 +      using assms(1) by (auto simp add: borel_eq_atLeastAtMost Int_stable_def)
  5.1227 +  thus ?thesis
  5.1228 +  proof (induction rule: sigma_sets_induct_disjoint)
  5.1229 +    case (union f)
  5.1230 +      from union.hyps(2) have "\<And>i. f i \<in> sets borel" by (auto simp: borel_eq_atLeastAtMost)
  5.1231 +      with union show ?case by (auto intro: un)
  5.1232 +  next
  5.1233 +    case (basic A)
  5.1234 +    then obtain a b where "A = {a .. b}" by auto
  5.1235 +    then show ?case
  5.1236 +      by (cases "a \<le> b") (auto intro: int empty)
  5.1237 +  qed (auto intro: empty compl simp: Compl_eq_Diff_UNIV[symmetric] borel_eq_atLeastAtMost)
  5.1238 +qed
  5.1239 +
  5.1240 +lemma borel_sigma_sets_Ioc: "borel = sigma UNIV (range (\<lambda>(a, b). {a <.. b::real}))"
  5.1241 +proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
  5.1242 +  fix i :: real
  5.1243 +  have "{..i} = (\<Union>j::nat. {-j <.. i})"
  5.1244 +    by (auto simp: minus_less_iff reals_Archimedean2)
  5.1245 +  also have "\<dots> \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))"
  5.1246 +    by (intro sets.countable_nat_UN) auto
  5.1247 +  finally show "{..i} \<in> sets (sigma UNIV (range (\<lambda>(i, j). {i<..j})))" .
  5.1248 +qed simp
  5.1249 +
  5.1250 +lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
  5.1251 +  by (simp add: eucl_less_def lessThan_def)
  5.1252 +
  5.1253 +lemma borel_eq_atLeastLessThan:
  5.1254 +  "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
  5.1255 +proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
  5.1256 +  have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
  5.1257 +  fix x :: real
  5.1258 +  have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
  5.1259 +    by (auto simp: move_uminus real_arch_simple)
  5.1260 +  then show "{y. y <e x} \<in> ?SIGMA"
  5.1261 +    by (auto intro: sigma_sets.intros(2-) simp: eucl_lessThan)
  5.1262 +qed auto
  5.1263 +
  5.1264 +lemma borel_measurable_halfspacesI:
  5.1265 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  5.1266 +  assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
  5.1267 +  and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
  5.1268 +  shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
  5.1269 +proof safe
  5.1270 +  fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
  5.1271 +  then show "S a i \<in> sets M" unfolding assms
  5.1272 +    by (auto intro!: measurable_sets simp: assms(1))
  5.1273 +next
  5.1274 +  assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
  5.1275 +  then show "f \<in> borel_measurable M"
  5.1276 +    by (auto intro!: measurable_measure_of simp: S_eq F)
  5.1277 +qed
  5.1278 +
  5.1279 +lemma borel_measurable_iff_halfspace_le:
  5.1280 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  5.1281 +  shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i \<le> a} \<in> sets M)"
  5.1282 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
  5.1283 +
  5.1284 +lemma borel_measurable_iff_halfspace_less:
  5.1285 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  5.1286 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i < a} \<in> sets M)"
  5.1287 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
  5.1288 +
  5.1289 +lemma borel_measurable_iff_halfspace_ge:
  5.1290 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  5.1291 +  shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a \<le> f w \<bullet> i} \<in> sets M)"
  5.1292 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
  5.1293 +
  5.1294 +lemma borel_measurable_iff_halfspace_greater:
  5.1295 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  5.1296 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a < f w \<bullet> i} \<in> sets M)"
  5.1297 +  by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
  5.1298 +
  5.1299 +lemma borel_measurable_iff_le:
  5.1300 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
  5.1301 +  using borel_measurable_iff_halfspace_le[where 'c=real] by simp
  5.1302 +
  5.1303 +lemma borel_measurable_iff_less:
  5.1304 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
  5.1305 +  using borel_measurable_iff_halfspace_less[where 'c=real] by simp
  5.1306 +
  5.1307 +lemma borel_measurable_iff_ge:
  5.1308 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
  5.1309 +  using borel_measurable_iff_halfspace_ge[where 'c=real]
  5.1310 +  by simp
  5.1311 +
  5.1312 +lemma borel_measurable_iff_greater:
  5.1313 +  "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
  5.1314 +  using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
  5.1315 +
  5.1316 +lemma borel_measurable_euclidean_space:
  5.1317 +  fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
  5.1318 +  shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
  5.1319 +proof safe
  5.1320 +  assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
  5.1321 +  then show "f \<in> borel_measurable M"
  5.1322 +    by (subst borel_measurable_iff_halfspace_le) auto
  5.1323 +qed auto
  5.1324 +
  5.1325 +subsection "Borel measurable operators"
  5.1326 +
  5.1327 +lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
  5.1328 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1329 +
  5.1330 +lemma borel_measurable_sgn [measurable]: "(sgn::'a::real_normed_vector \<Rightarrow> 'a) \<in> borel_measurable borel"
  5.1331 +  by (rule borel_measurable_continuous_countable_exceptions[where X="{0}"])
  5.1332 +     (auto intro!: continuous_on_sgn continuous_on_id)
  5.1333 +
  5.1334 +lemma borel_measurable_uminus[measurable (raw)]:
  5.1335 +  fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  5.1336 +  assumes g: "g \<in> borel_measurable M"
  5.1337 +  shows "(\<lambda>x. - g x) \<in> borel_measurable M"
  5.1338 +  by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
  5.1339 +
  5.1340 +lemma borel_measurable_diff[measurable (raw)]:
  5.1341 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  5.1342 +  assumes f: "f \<in> borel_measurable M"
  5.1343 +  assumes g: "g \<in> borel_measurable M"
  5.1344 +  shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  5.1345 +  using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
  5.1346 +
  5.1347 +lemma borel_measurable_times[measurable (raw)]:
  5.1348 +  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
  5.1349 +  assumes f: "f \<in> borel_measurable M"
  5.1350 +  assumes g: "g \<in> borel_measurable M"
  5.1351 +  shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  5.1352 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  5.1353 +
  5.1354 +lemma borel_measurable_setprod[measurable (raw)]:
  5.1355 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
  5.1356 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  5.1357 +  shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  5.1358 +proof cases
  5.1359 +  assume "finite S"
  5.1360 +  thus ?thesis using assms by induct auto
  5.1361 +qed simp
  5.1362 +
  5.1363 +lemma borel_measurable_dist[measurable (raw)]:
  5.1364 +  fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
  5.1365 +  assumes f: "f \<in> borel_measurable M"
  5.1366 +  assumes g: "g \<in> borel_measurable M"
  5.1367 +  shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
  5.1368 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  5.1369 +
  5.1370 +lemma borel_measurable_scaleR[measurable (raw)]:
  5.1371 +  fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
  5.1372 +  assumes f: "f \<in> borel_measurable M"
  5.1373 +  assumes g: "g \<in> borel_measurable M"
  5.1374 +  shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
  5.1375 +  using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
  5.1376 +
  5.1377 +lemma affine_borel_measurable_vector:
  5.1378 +  fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
  5.1379 +  assumes "f \<in> borel_measurable M"
  5.1380 +  shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
  5.1381 +proof (rule borel_measurableI)
  5.1382 +  fix S :: "'x set" assume "open S"
  5.1383 +  show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
  5.1384 +  proof cases
  5.1385 +    assume "b \<noteq> 0"
  5.1386 +    with \<open>open S\<close> have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
  5.1387 +      using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
  5.1388 +      by (auto simp: algebra_simps)
  5.1389 +    hence "?S \<in> sets borel" by auto
  5.1390 +    moreover
  5.1391 +    from \<open>b \<noteq> 0\<close> have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
  5.1392 +      apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
  5.1393 +    ultimately show ?thesis using assms unfolding in_borel_measurable_borel
  5.1394 +      by auto
  5.1395 +  qed simp
  5.1396 +qed
  5.1397 +
  5.1398 +lemma borel_measurable_const_scaleR[measurable (raw)]:
  5.1399 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
  5.1400 +  using affine_borel_measurable_vector[of f M 0 b] by simp
  5.1401 +
  5.1402 +lemma borel_measurable_const_add[measurable (raw)]:
  5.1403 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
  5.1404 +  using affine_borel_measurable_vector[of f M a 1] by simp
  5.1405 +
  5.1406 +lemma borel_measurable_inverse[measurable (raw)]:
  5.1407 +  fixes f :: "'a \<Rightarrow> 'b::real_normed_div_algebra"
  5.1408 +  assumes f: "f \<in> borel_measurable M"
  5.1409 +  shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
  5.1410 +  apply (rule measurable_compose[OF f])
  5.1411 +  apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  5.1412 +  apply (auto intro!: continuous_on_inverse continuous_on_id)
  5.1413 +  done
  5.1414 +
  5.1415 +lemma borel_measurable_divide[measurable (raw)]:
  5.1416 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
  5.1417 +    (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_div_algebra}) \<in> borel_measurable M"
  5.1418 +  by (simp add: divide_inverse)
  5.1419 +
  5.1420 +lemma borel_measurable_abs[measurable (raw)]:
  5.1421 +  "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  5.1422 +  unfolding abs_real_def by simp
  5.1423 +
  5.1424 +lemma borel_measurable_nth[measurable (raw)]:
  5.1425 +  "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  5.1426 +  by (simp add: cart_eq_inner_axis)
  5.1427 +
  5.1428 +lemma convex_measurable:
  5.1429 +  fixes A :: "'a :: euclidean_space set"
  5.1430 +  shows "X \<in> borel_measurable M \<Longrightarrow> X ` space M \<subseteq> A \<Longrightarrow> open A \<Longrightarrow> convex_on A q \<Longrightarrow>
  5.1431 +    (\<lambda>x. q (X x)) \<in> borel_measurable M"
  5.1432 +  by (rule measurable_compose[where f=X and N="restrict_space borel A"])
  5.1433 +     (auto intro!: borel_measurable_continuous_on_restrict convex_on_continuous measurable_restrict_space2)
  5.1434 +
  5.1435 +lemma borel_measurable_ln[measurable (raw)]:
  5.1436 +  assumes f: "f \<in> borel_measurable M"
  5.1437 +  shows "(\<lambda>x. ln (f x :: real)) \<in> borel_measurable M"
  5.1438 +  apply (rule measurable_compose[OF f])
  5.1439 +  apply (rule borel_measurable_continuous_countable_exceptions[of "{0}"])
  5.1440 +  apply (auto intro!: continuous_on_ln continuous_on_id)
  5.1441 +  done
  5.1442 +
  5.1443 +lemma borel_measurable_log[measurable (raw)]:
  5.1444 +  "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
  5.1445 +  unfolding log_def by auto
  5.1446 +
  5.1447 +lemma borel_measurable_exp[measurable]:
  5.1448 +  "(exp::'a::{real_normed_field,banach}\<Rightarrow>'a) \<in> borel_measurable borel"
  5.1449 +  by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
  5.1450 +
  5.1451 +lemma measurable_real_floor[measurable]:
  5.1452 +  "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  5.1453 +proof -
  5.1454 +  have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real_of_int a \<le> x \<and> x < real_of_int (a + 1))"
  5.1455 +    by (auto intro: floor_eq2)
  5.1456 +  then show ?thesis
  5.1457 +    by (auto simp: vimage_def measurable_count_space_eq2_countable)
  5.1458 +qed
  5.1459 +
  5.1460 +lemma measurable_real_ceiling[measurable]:
  5.1461 +  "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
  5.1462 +  unfolding ceiling_def[abs_def] by simp
  5.1463 +
  5.1464 +lemma borel_measurable_real_floor: "(\<lambda>x::real. real_of_int \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
  5.1465 +  by simp
  5.1466 +
  5.1467 +lemma borel_measurable_root [measurable]: "root n \<in> borel_measurable borel"
  5.1468 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1469 +
  5.1470 +lemma borel_measurable_sqrt [measurable]: "sqrt \<in> borel_measurable borel"
  5.1471 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1472 +
  5.1473 +lemma borel_measurable_power [measurable (raw)]:
  5.1474 +  fixes f :: "_ \<Rightarrow> 'b::{power,real_normed_algebra}"
  5.1475 +  assumes f: "f \<in> borel_measurable M"
  5.1476 +  shows "(\<lambda>x. (f x) ^ n) \<in> borel_measurable M"
  5.1477 +  by (intro borel_measurable_continuous_on [OF _ f] continuous_intros)
  5.1478 +
  5.1479 +lemma borel_measurable_Re [measurable]: "Re \<in> borel_measurable borel"
  5.1480 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1481 +
  5.1482 +lemma borel_measurable_Im [measurable]: "Im \<in> borel_measurable borel"
  5.1483 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1484 +
  5.1485 +lemma borel_measurable_of_real [measurable]: "(of_real :: _ \<Rightarrow> (_::real_normed_algebra)) \<in> borel_measurable borel"
  5.1486 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1487 +
  5.1488 +lemma borel_measurable_sin [measurable]: "(sin :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  5.1489 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1490 +
  5.1491 +lemma borel_measurable_cos [measurable]: "(cos :: _ \<Rightarrow> (_::{real_normed_field,banach})) \<in> borel_measurable borel"
  5.1492 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1493 +
  5.1494 +lemma borel_measurable_arctan [measurable]: "arctan \<in> borel_measurable borel"
  5.1495 +  by (intro borel_measurable_continuous_on1 continuous_intros)
  5.1496 +
  5.1497 +lemma borel_measurable_complex_iff:
  5.1498 +  "f \<in> borel_measurable M \<longleftrightarrow>
  5.1499 +    (\<lambda>x. Re (f x)) \<in> borel_measurable M \<and> (\<lambda>x. Im (f x)) \<in> borel_measurable M"
  5.1500 +  apply auto
  5.1501 +  apply (subst fun_complex_eq)
  5.1502 +  apply (intro borel_measurable_add)
  5.1503 +  apply auto
  5.1504 +  done
  5.1505 +
  5.1506 +subsection "Borel space on the extended reals"
  5.1507 +
  5.1508 +lemma borel_measurable_ereal[measurable (raw)]:
  5.1509 +  assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  5.1510 +  using continuous_on_ereal f by (rule borel_measurable_continuous_on) (rule continuous_on_id)
  5.1511 +
  5.1512 +lemma borel_measurable_real_of_ereal[measurable (raw)]:
  5.1513 +  fixes f :: "'a \<Rightarrow> ereal"
  5.1514 +  assumes f: "f \<in> borel_measurable M"
  5.1515 +  shows "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M"
  5.1516 +  apply (rule measurable_compose[OF f])
  5.1517 +  apply (rule borel_measurable_continuous_countable_exceptions[of "{\<infinity>, -\<infinity> }"])
  5.1518 +  apply (auto intro: continuous_on_real simp: Compl_eq_Diff_UNIV)
  5.1519 +  done
  5.1520 +
  5.1521 +lemma borel_measurable_ereal_cases:
  5.1522 +  fixes f :: "'a \<Rightarrow> ereal"
  5.1523 +  assumes f: "f \<in> borel_measurable M"
  5.1524 +  assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x)))) \<in> borel_measurable M"
  5.1525 +  shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
  5.1526 +proof -
  5.1527 +  let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real_of_ereal (f x)))"
  5.1528 +  { fix x have "H (f x) = ?F x" by (cases "f x") auto }
  5.1529 +  with f H show ?thesis by simp
  5.1530 +qed
  5.1531 +
  5.1532 +lemma
  5.1533 +  fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
  5.1534 +  shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  5.1535 +    and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
  5.1536 +    and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  5.1537 +  by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
  5.1538 +
  5.1539 +lemma borel_measurable_uminus_eq_ereal[simp]:
  5.1540 +  "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  5.1541 +proof
  5.1542 +  assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  5.1543 +qed auto
  5.1544 +
  5.1545 +lemma set_Collect_ereal2:
  5.1546 +  fixes f g :: "'a \<Rightarrow> ereal"
  5.1547 +  assumes f: "f \<in> borel_measurable M"
  5.1548 +  assumes g: "g \<in> borel_measurable M"
  5.1549 +  assumes H: "{x \<in> space M. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))} \<in> sets M"
  5.1550 +    "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
  5.1551 +    "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
  5.1552 +    "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
  5.1553 +    "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
  5.1554 +  shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
  5.1555 +proof -
  5.1556 +  let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = -\<infinity> then H y (-\<infinity>) else H y (ereal (real_of_ereal (g x)))"
  5.1557 +  let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = -\<infinity> then ?G (-\<infinity>) x else ?G (ereal (real_of_ereal (f x))) x"
  5.1558 +  { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  5.1559 +  note * = this
  5.1560 +  from assms show ?thesis
  5.1561 +    by (subst *) (simp del: space_borel split del: if_split)
  5.1562 +qed
  5.1563 +
  5.1564 +lemma borel_measurable_ereal_iff:
  5.1565 +  shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  5.1566 +proof
  5.1567 +  assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  5.1568 +  from borel_measurable_real_of_ereal[OF this]
  5.1569 +  show "f \<in> borel_measurable M" by auto
  5.1570 +qed auto
  5.1571 +
  5.1572 +lemma borel_measurable_erealD[measurable_dest]:
  5.1573 +  "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<Longrightarrow> g \<in> measurable N M \<Longrightarrow> (\<lambda>x. f (g x)) \<in> borel_measurable N"
  5.1574 +  unfolding borel_measurable_ereal_iff by simp
  5.1575 +
  5.1576 +lemma borel_measurable_ereal_iff_real:
  5.1577 +  fixes f :: "'a \<Rightarrow> ereal"
  5.1578 +  shows "f \<in> borel_measurable M \<longleftrightarrow>
  5.1579 +    ((\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
  5.1580 +proof safe
  5.1581 +  assume *: "(\<lambda>x. real_of_ereal (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
  5.1582 +  have "f -` {\<infinity>} \<inter> space M = {x\<in>space M. f x = \<infinity>}" "f -` {-\<infinity>} \<inter> space M = {x\<in>space M. f x = -\<infinity>}" by auto
  5.1583 +  with * have **: "{x\<in>space M. f x = \<infinity>} \<in> sets M" "{x\<in>space M. f x = -\<infinity>} \<in> sets M" by simp_all
  5.1584 +  let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real_of_ereal (f x))"
  5.1585 +  have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  5.1586 +  also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
  5.1587 +  finally show "f \<in> borel_measurable M" .
  5.1588 +qed simp_all
  5.1589 +
  5.1590 +lemma borel_measurable_ereal_iff_Iio:
  5.1591 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  5.1592 +  by (auto simp: borel_Iio measurable_iff_measure_of)
  5.1593 +
  5.1594 +lemma borel_measurable_ereal_iff_Ioi:
  5.1595 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  5.1596 +  by (auto simp: borel_Ioi measurable_iff_measure_of)
  5.1597 +
  5.1598 +lemma vimage_sets_compl_iff:
  5.1599 +  "f -` A \<inter> space M \<in> sets M \<longleftrightarrow> f -` (- A) \<inter> space M \<in> sets M"
  5.1600 +proof -
  5.1601 +  { fix A assume "f -` A \<inter> space M \<in> sets M"
  5.1602 +    moreover have "f -` (- A) \<inter> space M = space M - f -` A \<inter> space M" by auto
  5.1603 +    ultimately have "f -` (- A) \<inter> space M \<in> sets M" by auto }
  5.1604 +  from this[of A] this[of "-A"] show ?thesis
  5.1605 +    by (metis double_complement)
  5.1606 +qed
  5.1607 +
  5.1608 +lemma borel_measurable_iff_Iic_ereal:
  5.1609 +  "(f::'a\<Rightarrow>ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  5.1610 +  unfolding borel_measurable_ereal_iff_Ioi vimage_sets_compl_iff[where A="{a <..}" for a] by simp
  5.1611 +
  5.1612 +lemma borel_measurable_iff_Ici_ereal:
  5.1613 +  "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  5.1614 +  unfolding borel_measurable_ereal_iff_Iio vimage_sets_compl_iff[where A="{..< a}" for a] by simp
  5.1615 +
  5.1616 +lemma borel_measurable_ereal2:
  5.1617 +  fixes f g :: "'a \<Rightarrow> ereal"
  5.1618 +  assumes f: "f \<in> borel_measurable M"
  5.1619 +  assumes g: "g \<in> borel_measurable M"
  5.1620 +  assumes H: "(\<lambda>x. H (ereal (real_of_ereal (f x))) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  5.1621 +    "(\<lambda>x. H (-\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  5.1622 +    "(\<lambda>x. H (\<infinity>) (ereal (real_of_ereal (g x)))) \<in> borel_measurable M"
  5.1623 +    "(\<lambda>x. H (ereal (real_of_ereal (f x))) (-\<infinity>)) \<in> borel_measurable M"
  5.1624 +    "(\<lambda>x. H (ereal (real_of_ereal (f x))) (\<infinity>)) \<in> borel_measurable M"
  5.1625 +  shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  5.1626 +proof -
  5.1627 +  let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = - \<infinity> then H y (-\<infinity>) else H y (ereal (real_of_ereal (g x)))"
  5.1628 +  let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = - \<infinity> then ?G (-\<infinity>) x else ?G (ereal (real_of_ereal (f x))) x"
  5.1629 +  { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  5.1630 +  note * = this
  5.1631 +  from assms show ?thesis unfolding * by simp
  5.1632 +qed
  5.1633 +
  5.1634 +lemma [measurable(raw)]:
  5.1635 +  fixes f :: "'a \<Rightarrow> ereal"
  5.1636 +  assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  5.1637 +  shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  5.1638 +    and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  5.1639 +  by (simp_all add: borel_measurable_ereal2)
  5.1640 +
  5.1641 +lemma [measurable(raw)]:
  5.1642 +  fixes f g :: "'a \<Rightarrow> ereal"
  5.1643 +  assumes "f \<in> borel_measurable M"
  5.1644 +  assumes "g \<in> borel_measurable M"
  5.1645 +  shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  5.1646 +    and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  5.1647 +  using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  5.1648 +
  5.1649 +lemma borel_measurable_ereal_setsum[measurable (raw)]:
  5.1650 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  5.1651 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  5.1652 +  shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  5.1653 +  using assms by (induction S rule: infinite_finite_induct) auto
  5.1654 +
  5.1655 +lemma borel_measurable_ereal_setprod[measurable (raw)]:
  5.1656 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  5.1657 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  5.1658 +  shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  5.1659 +  using assms by (induction S rule: infinite_finite_induct) auto
  5.1660 +
  5.1661 +lemma borel_measurable_extreal_suminf[measurable (raw)]:
  5.1662 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  5.1663 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  5.1664 +  shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  5.1665 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  5.1666 +
  5.1667 +subsection "Borel space on the extended non-negative reals"
  5.1668 +
  5.1669 +text \<open> @{type ennreal} is a topological monoid, so no rules for plus are required, also all order
  5.1670 +  statements are usually done on type classes. \<close>
  5.1671 +
  5.1672 +lemma measurable_enn2ereal[measurable]: "enn2ereal \<in> borel \<rightarrow>\<^sub>M borel"
  5.1673 +  by (intro borel_measurable_continuous_on1 continuous_on_enn2ereal)
  5.1674 +
  5.1675 +lemma measurable_e2ennreal[measurable]: "e2ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  5.1676 +  by (intro borel_measurable_continuous_on1 continuous_on_e2ennreal)
  5.1677 +
  5.1678 +lemma borel_measurable_enn2real[measurable (raw)]:
  5.1679 +  "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. enn2real (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  5.1680 +  unfolding enn2real_def[abs_def] by measurable
  5.1681 +
  5.1682 +definition [simp]: "is_borel f M \<longleftrightarrow> f \<in> borel_measurable M"
  5.1683 +
  5.1684 +lemma is_borel_transfer[transfer_rule]: "rel_fun (rel_fun op = pcr_ennreal) op = is_borel is_borel"
  5.1685 +  unfolding is_borel_def[abs_def]
  5.1686 +proof (safe intro!: rel_funI ext dest!: rel_fun_eq_pcr_ennreal[THEN iffD1])
  5.1687 +  fix f and M :: "'a measure"
  5.1688 +  show "f \<in> borel_measurable M" if f: "enn2ereal \<circ> f \<in> borel_measurable M"
  5.1689 +    using measurable_compose[OF f measurable_e2ennreal] by simp
  5.1690 +qed simp
  5.1691 +
  5.1692 +context
  5.1693 +  includes ennreal.lifting
  5.1694 +begin
  5.1695 +
  5.1696 +lemma measurable_ennreal[measurable]: "ennreal \<in> borel \<rightarrow>\<^sub>M borel"
  5.1697 +  unfolding is_borel_def[symmetric]
  5.1698 +  by transfer simp
  5.1699 +
  5.1700 +lemma borel_measurable_ennreal_iff[simp]:
  5.1701 +  assumes [simp]: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> f x"
  5.1702 +  shows "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel \<longleftrightarrow> f \<in> M \<rightarrow>\<^sub>M borel"
  5.1703 +proof safe
  5.1704 +  assume "(\<lambda>x. ennreal (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  5.1705 +  then have "(\<lambda>x. enn2real (ennreal (f x))) \<in> M \<rightarrow>\<^sub>M borel"
  5.1706 +    by measurable
  5.1707 +  then show "f \<in> M \<rightarrow>\<^sub>M borel"
  5.1708 +    by (rule measurable_cong[THEN iffD1, rotated]) auto
  5.1709 +qed measurable
  5.1710 +
  5.1711 +lemma borel_measurable_times_ennreal[measurable (raw)]:
  5.1712 +  fixes f g :: "'a \<Rightarrow> ennreal"
  5.1713 +  shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x * g x) \<in> M \<rightarrow>\<^sub>M borel"
  5.1714 +  unfolding is_borel_def[symmetric] by transfer simp
  5.1715 +
  5.1716 +lemma borel_measurable_inverse_ennreal[measurable (raw)]:
  5.1717 +  fixes f :: "'a \<Rightarrow> ennreal"
  5.1718 +  shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. inverse (f x)) \<in> M \<rightarrow>\<^sub>M borel"
  5.1719 +  unfolding is_borel_def[symmetric] by transfer simp
  5.1720 +
  5.1721 +lemma borel_measurable_divide_ennreal[measurable (raw)]:
  5.1722 +  fixes f :: "'a \<Rightarrow> ennreal"
  5.1723 +  shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x / g x) \<in> M \<rightarrow>\<^sub>M borel"
  5.1724 +  unfolding divide_ennreal_def by simp
  5.1725 +
  5.1726 +lemma borel_measurable_minus_ennreal[measurable (raw)]:
  5.1727 +  fixes f :: "'a \<Rightarrow> ennreal"
  5.1728 +  shows "f \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M borel \<Longrightarrow> (\<lambda>x. f x - g x) \<in> M \<rightarrow>\<^sub>M borel"
  5.1729 +  unfolding is_borel_def[symmetric] by transfer simp
  5.1730 +
  5.1731 +lemma borel_measurable_setprod_ennreal[measurable (raw)]:
  5.1732 +  fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ennreal"
  5.1733 +  assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  5.1734 +  shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  5.1735 +  using assms by (induction S rule: infinite_finite_induct) auto
  5.1736 +
  5.1737 +end
  5.1738 +
  5.1739 +hide_const (open) is_borel
  5.1740 +
  5.1741 +subsection \<open>LIMSEQ is borel measurable\<close>
  5.1742 +
  5.1743 +lemma borel_measurable_LIMSEQ_real:
  5.1744 +  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  5.1745 +  assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) \<longlonglongrightarrow> u' x"
  5.1746 +  and u: "\<And>i. u i \<in> borel_measurable M"
  5.1747 +  shows "u' \<in> borel_measurable M"
  5.1748 +proof -
  5.1749 +  have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  5.1750 +    using u' by (simp add: lim_imp_Liminf)
  5.1751 +  moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  5.1752 +    by auto
  5.1753 +  ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  5.1754 +qed
  5.1755 +
  5.1756 +lemma borel_measurable_LIMSEQ_metric:
  5.1757 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
  5.1758 +  assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  5.1759 +  assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) \<longlonglongrightarrow> g x"
  5.1760 +  shows "g \<in> borel_measurable M"
  5.1761 +  unfolding borel_eq_closed
  5.1762 +proof (safe intro!: measurable_measure_of)
  5.1763 +  fix A :: "'b set" assume "closed A"
  5.1764 +
  5.1765 +  have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
  5.1766 +  proof (rule borel_measurable_LIMSEQ_real)
  5.1767 +    show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) \<longlonglongrightarrow> infdist (g x) A"
  5.1768 +      by (intro tendsto_infdist lim)
  5.1769 +    show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
  5.1770 +      by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
  5.1771 +        continuous_at_imp_continuous_on ballI continuous_infdist continuous_ident) auto
  5.1772 +  qed
  5.1773 +
  5.1774 +  show "g -` A \<inter> space M \<in> sets M"
  5.1775 +  proof cases
  5.1776 +    assume "A \<noteq> {}"
  5.1777 +    then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
  5.1778 +      using \<open>closed A\<close> by (simp add: in_closed_iff_infdist_zero)
  5.1779 +    then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
  5.1780 +      by auto
  5.1781 +    also have "\<dots> \<in> sets M"
  5.1782 +      by measurable
  5.1783 +    finally show ?thesis .
  5.1784 +  qed simp
  5.1785 +qed auto
  5.1786 +
  5.1787 +lemma sets_Collect_Cauchy[measurable]:
  5.1788 +  fixes f :: "nat \<Rightarrow> 'a => 'b::{metric_space, second_countable_topology}"
  5.1789 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  5.1790 +  shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  5.1791 +  unfolding metric_Cauchy_iff2 using f by auto
  5.1792 +
  5.1793 +lemma borel_measurable_lim_metric[measurable (raw)]:
  5.1794 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  5.1795 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  5.1796 +  shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  5.1797 +proof -
  5.1798 +  define u' where "u' x = lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)" for x
  5.1799 +  then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  5.1800 +    by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  5.1801 +  have "u' \<in> borel_measurable M"
  5.1802 +  proof (rule borel_measurable_LIMSEQ_metric)
  5.1803 +    fix x
  5.1804 +    have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  5.1805 +      by (cases "Cauchy (\<lambda>i. f i x)")
  5.1806 +         (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  5.1807 +    then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) \<longlonglongrightarrow> u' x"
  5.1808 +      unfolding u'_def
  5.1809 +      by (rule convergent_LIMSEQ_iff[THEN iffD1])
  5.1810 +  qed measurable
  5.1811 +  then show ?thesis
  5.1812 +    unfolding * by measurable
  5.1813 +qed
  5.1814 +
  5.1815 +lemma borel_measurable_suminf[measurable (raw)]:
  5.1816 +  fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b::{banach, second_countable_topology}"
  5.1817 +  assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  5.1818 +  shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  5.1819 +  unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  5.1820 +
  5.1821 +lemma Collect_closed_imp_pred_borel: "closed {x. P x} \<Longrightarrow> Measurable.pred borel P"
  5.1822 +  by (simp add: pred_def)
  5.1823 +
  5.1824 +(* Proof by Jeremy Avigad and Luke Serafin *)
  5.1825 +lemma isCont_borel_pred[measurable]:
  5.1826 +  fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  5.1827 +  shows "Measurable.pred borel (isCont f)"
  5.1828 +proof (subst measurable_cong)
  5.1829 +  let ?I = "\<lambda>j. inverse(real (Suc j))"
  5.1830 +  show "isCont f x = (\<forall>i. \<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i)" for x
  5.1831 +    unfolding continuous_at_eps_delta
  5.1832 +  proof safe
  5.1833 +    fix i assume "\<forall>e>0. \<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  5.1834 +    moreover have "0 < ?I i / 2"
  5.1835 +      by simp
  5.1836 +    ultimately obtain d where d: "0 < d" "\<And>y. dist x y < d \<Longrightarrow> dist (f y) (f x) < ?I i / 2"
  5.1837 +      by (metis dist_commute)
  5.1838 +    then obtain j where j: "?I j < d"
  5.1839 +      by (metis reals_Archimedean)
  5.1840 +
  5.1841 +    show "\<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i"
  5.1842 +    proof (safe intro!: exI[where x=j])
  5.1843 +      fix y z assume *: "dist x y < ?I j" "dist x z < ?I j"
  5.1844 +      have "dist (f y) (f z) \<le> dist (f y) (f x) + dist (f z) (f x)"
  5.1845 +        by (rule dist_triangle2)
  5.1846 +      also have "\<dots> < ?I i / 2 + ?I i / 2"
  5.1847 +        by (intro add_strict_mono d less_trans[OF _ j] *)
  5.1848 +      also have "\<dots> \<le> ?I i"
  5.1849 +        by (simp add: field_simps of_nat_Suc)
  5.1850 +      finally show "dist (f y) (f z) \<le> ?I i"
  5.1851 +        by simp
  5.1852 +    qed
  5.1853 +  next
  5.1854 +    fix e::real assume "0 < e"
  5.1855 +    then obtain n where n: "?I n < e"
  5.1856 +      by (metis reals_Archimedean)
  5.1857 +    assume "\<forall>i. \<exists>j. \<forall>y z. dist x y < ?I j \<and> dist x z < ?I j \<longrightarrow> dist (f y) (f z) \<le> ?I i"
  5.1858 +    from this[THEN spec, of "Suc n"]
  5.1859 +    obtain j where j: "\<And>y z. dist x y < ?I j \<Longrightarrow> dist x z < ?I j \<Longrightarrow> dist (f y) (f z) \<le> ?I (Suc n)"
  5.1860 +      by auto
  5.1861 +
  5.1862 +    show "\<exists>d>0. \<forall>y. dist y x < d \<longrightarrow> dist (f y) (f x) < e"
  5.1863 +    proof (safe intro!: exI[of _ "?I j"])
  5.1864 +      fix y assume "dist y x < ?I j"
  5.1865 +      then have "dist (f y) (f x) \<le> ?I (Suc n)"
  5.1866 +        by (intro j) (auto simp: dist_commute)
  5.1867 +      also have "?I (Suc n) < ?I n"
  5.1868 +        by simp
  5.1869 +      also note n
  5.1870 +      finally show "dist (f y) (f x) < e" .
  5.1871 +    qed simp
  5.1872 +  qed
  5.1873 +qed (intro pred_intros_countable closed_Collect_all closed_Collect_le open_Collect_less
  5.1874 +           Collect_closed_imp_pred_borel closed_Collect_imp open_Collect_conj continuous_intros)
  5.1875 +
  5.1876 +lemma isCont_borel:
  5.1877 +  fixes f :: "'b::metric_space \<Rightarrow> 'a::metric_space"
  5.1878 +  shows "{x. isCont f x} \<in> sets borel"
  5.1879 +  by simp
  5.1880 +
  5.1881 +lemma is_real_interval:
  5.1882 +  assumes S: "is_interval S"
  5.1883 +  shows "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or> S = {a<..} \<or> S = {a..} \<or>
  5.1884 +    S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}"
  5.1885 +  using S unfolding is_interval_1 by (blast intro: interval_cases)
  5.1886 +
  5.1887 +lemma real_interval_borel_measurable:
  5.1888 +  assumes "is_interval (S::real set)"
  5.1889 +  shows "S \<in> sets borel"
  5.1890 +proof -
  5.1891 +  from assms is_real_interval have "\<exists>a b::real. S = {} \<or> S = UNIV \<or> S = {..<b} \<or> S = {..b} \<or>
  5.1892 +    S = {a<..} \<or> S = {a..} \<or> S = {a<..<b} \<or> S = {a<..b} \<or> S = {a..<b} \<or> S = {a..b}" by auto
  5.1893 +  then guess a ..
  5.1894 +  then guess b ..
  5.1895 +  thus ?thesis
  5.1896 +    by auto
  5.1897 +qed
  5.1898 +
  5.1899 +lemma borel_measurable_mono_on_fnc:
  5.1900 +  fixes f :: "real \<Rightarrow> real" and A :: "real set"
  5.1901 +  assumes "mono_on f A"
  5.1902 +  shows "f \<in> borel_measurable (restrict_space borel A)"
  5.1903 +  apply (rule measurable_restrict_countable[OF mono_on_ctble_discont[OF assms]])
  5.1904 +  apply (auto intro!: image_eqI[where x="{x}" for x] simp: sets_restrict_space)
  5.1905 +  apply (auto simp add: sets_restrict_restrict_space continuous_on_eq_continuous_within
  5.1906 +              cong: measurable_cong_sets
  5.1907 +              intro!: borel_measurable_continuous_on_restrict intro: continuous_within_subset)
  5.1908 +  done
  5.1909 +
  5.1910 +lemma borel_measurable_mono:
  5.1911 +  fixes f :: "real \<Rightarrow> real"
  5.1912 +  shows "mono f \<Longrightarrow> f \<in> borel_measurable borel"
  5.1913 +  using borel_measurable_mono_on_fnc[of f UNIV] by (simp add: mono_def mono_on_def)
  5.1914 +
  5.1915 +no_notation
  5.1916 +  eucl_less (infix "<e" 50)
  5.1917 +
  5.1918 +end
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/src/HOL/Analysis/Bounded_Continuous_Function.thy	Mon Aug 08 14:13:14 2016 +0200
     6.3 @@ -0,0 +1,518 @@
     6.4 +section \<open>Bounded Continuous Functions\<close>
     6.5 +
     6.6 +theory Bounded_Continuous_Function
     6.7 +imports Henstock_Kurzweil_Integration
     6.8 +begin
     6.9 +
    6.10 +subsection \<open>Definition\<close>
    6.11 +
    6.12 +definition bcontfun :: "('a::topological_space \<Rightarrow> 'b::metric_space) set"
    6.13 +  where "bcontfun = {f. continuous_on UNIV f \<and> bounded (range f)}"
    6.14 +
    6.15 +typedef (overloaded) ('a, 'b) bcontfun =
    6.16 +    "bcontfun :: ('a::topological_space \<Rightarrow> 'b::metric_space) set"
    6.17 +  by (auto simp: bcontfun_def intro: continuous_intros simp: bounded_def)
    6.18 +
    6.19 +lemma bcontfunE:
    6.20 +  assumes "f \<in> bcontfun"
    6.21 +  obtains y where "continuous_on UNIV f" "\<And>x. dist (f x) u \<le> y"
    6.22 +  using assms unfolding bcontfun_def
    6.23 +  by (metis (lifting) bounded_any_center dist_commute mem_Collect_eq rangeI)
    6.24 +
    6.25 +lemma bcontfunE':
    6.26 +  assumes "f \<in> bcontfun"
    6.27 +  obtains y where "continuous_on UNIV f" "\<And>x. dist (f x) undefined \<le> y"
    6.28 +  using assms bcontfunE
    6.29 +  by metis
    6.30 +
    6.31 +lemma bcontfunI: "continuous_on UNIV f \<Longrightarrow> (\<And>x. dist (f x) u \<le> b) \<Longrightarrow> f \<in> bcontfun"
    6.32 +  unfolding bcontfun_def
    6.33 +  by (metis (lifting, no_types) bounded_def dist_commute mem_Collect_eq rangeE)
    6.34 +
    6.35 +lemma bcontfunI': "continuous_on UNIV f \<Longrightarrow> (\<And>x. dist (f x) undefined \<le> b) \<Longrightarrow> f \<in> bcontfun"
    6.36 +  using bcontfunI by metis
    6.37 +
    6.38 +lemma continuous_on_Rep_bcontfun[intro, simp]: "continuous_on T (Rep_bcontfun x)"
    6.39 +  using Rep_bcontfun[of x]
    6.40 +  by (auto simp: bcontfun_def intro: continuous_on_subset)
    6.41 +
    6.42 +(* TODO: Generalize to uniform spaces? *)
    6.43 +instantiation bcontfun :: (topological_space, metric_space) metric_space
    6.44 +begin
    6.45 +
    6.46 +definition dist_bcontfun :: "('a, 'b) bcontfun \<Rightarrow> ('a, 'b) bcontfun \<Rightarrow> real"
    6.47 +  where "dist_bcontfun f g = (SUP x. dist (Rep_bcontfun f x) (Rep_bcontfun g x))"
    6.48 +
    6.49 +definition uniformity_bcontfun :: "(('a, 'b) bcontfun \<times> ('a, 'b) bcontfun) filter"
    6.50 +  where "uniformity_bcontfun = (INF e:{0 <..}. principal {(x, y). dist x y < e})"
    6.51 +
    6.52 +definition open_bcontfun :: "('a, 'b) bcontfun set \<Rightarrow> bool"
    6.53 +  where "open_bcontfun S = (\<forall>x\<in>S. \<forall>\<^sub>F (x', y) in uniformity. x' = x \<longrightarrow> y \<in> S)"
    6.54 +
    6.55 +lemma dist_bounded:
    6.56 +  fixes f :: "('a, 'b) bcontfun"
    6.57 +  shows "dist (Rep_bcontfun f x) (Rep_bcontfun g x) \<le> dist f g"
    6.58 +proof -
    6.59 +  have "Rep_bcontfun f \<in> bcontfun" by (rule Rep_bcontfun)
    6.60 +  from bcontfunE'[OF this] obtain y where y:
    6.61 +    "continuous_on UNIV (Rep_bcontfun f)"
    6.62 +    "\<And>x. dist (Rep_bcontfun f x) undefined \<le> y"
    6.63 +    by auto
    6.64 +  have "Rep_bcontfun g \<in> bcontfun" by (rule Rep_bcontfun)
    6.65 +  from bcontfunE'[OF this] obtain z where z:
    6.66 +    "continuous_on UNIV (Rep_bcontfun g)"
    6.67 +    "\<And>x. dist (Rep_bcontfun g x) undefined \<le> z"
    6.68 +    by auto
    6.69 +  show ?thesis
    6.70 +    unfolding dist_bcontfun_def
    6.71 +  proof (intro cSUP_upper bdd_aboveI2)
    6.72 +    fix x
    6.73 +    have "dist (Rep_bcontfun f x)