src/HOL/Analysis/Binary_Product_Measure.thy
changeset 63627 6ddb43c6b711
parent 63626 44ce6b524ff3
child 64008 17a20ca86d62
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Analysis/Binary_Product_Measure.thy	Mon Aug 08 14:13:14 2016 +0200
     1.3 @@ -0,0 +1,1110 @@
     1.4 +(*  Title:      HOL/Analysis/Binary_Product_Measure.thy
     1.5 +    Author:     Johannes Hölzl, TU München
     1.6 +*)
     1.7 +
     1.8 +section \<open>Binary product measures\<close>
     1.9 +
    1.10 +theory Binary_Product_Measure
    1.11 +imports Nonnegative_Lebesgue_Integration
    1.12 +begin
    1.13 +
    1.14 +lemma Pair_vimage_times[simp]: "Pair x -` (A \<times> B) = (if x \<in> A then B else {})"
    1.15 +  by auto
    1.16 +
    1.17 +lemma rev_Pair_vimage_times[simp]: "(\<lambda>x. (x, y)) -` (A \<times> B) = (if y \<in> B then A else {})"
    1.18 +  by auto
    1.19 +
    1.20 +subsection "Binary products"
    1.21 +
    1.22 +definition pair_measure (infixr "\<Otimes>\<^sub>M" 80) where
    1.23 +  "A \<Otimes>\<^sub>M B = measure_of (space A \<times> space B)
    1.24 +      {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}
    1.25 +      (\<lambda>X. \<integral>\<^sup>+x. (\<integral>\<^sup>+y. indicator X (x,y) \<partial>B) \<partial>A)"
    1.26 +
    1.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)"
    1.28 +  using sets.space_closed[of A] sets.space_closed[of B] by auto
    1.29 +
    1.30 +lemma space_pair_measure:
    1.31 +  "space (A \<Otimes>\<^sub>M B) = space A \<times> space B"
    1.32 +  unfolding pair_measure_def using pair_measure_closed[of A B]
    1.33 +  by (rule space_measure_of)
    1.34 +
    1.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)}"
    1.36 +  by (auto simp: space_pair_measure)
    1.37 +
    1.38 +lemma sets_pair_measure:
    1.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}"
    1.40 +  unfolding pair_measure_def using pair_measure_closed[of A B]
    1.41 +  by (rule sets_measure_of)
    1.42 +
    1.43 +lemma sets_pair_measure_cong[measurable_cong, cong]:
    1.44 +  "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^sub>M M2) = sets (M1' \<Otimes>\<^sub>M M2')"
    1.45 +  unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
    1.46 +
    1.47 +lemma pair_measureI[intro, simp, measurable]:
    1.48 +  "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^sub>M B)"
    1.49 +  by (auto simp: sets_pair_measure)
    1.50 +
    1.51 +lemma sets_Pair: "{x} \<in> sets M1 \<Longrightarrow> {y} \<in> sets M2 \<Longrightarrow> {(x, y)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
    1.52 +  using pair_measureI[of "{x}" M1 "{y}" M2] by simp
    1.53 +
    1.54 +lemma measurable_pair_measureI:
    1.55 +  assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"
    1.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"
    1.57 +  shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    1.58 +  unfolding pair_measure_def using 1 2
    1.59 +  by (intro measurable_measure_of) (auto dest: sets.sets_into_space)
    1.60 +
    1.61 +lemma measurable_split_replace[measurable (raw)]:
    1.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"
    1.63 +  unfolding split_beta' .
    1.64 +
    1.65 +lemma measurable_Pair[measurable (raw)]:
    1.66 +  assumes f: "f \<in> measurable M M1" and g: "g \<in> measurable M M2"
    1.67 +  shows "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    1.68 +proof (rule measurable_pair_measureI)
    1.69 +  show "(\<lambda>x. (f x, g x)) \<in> space M \<rightarrow> space M1 \<times> space M2"
    1.70 +    using f g by (auto simp: measurable_def)
    1.71 +  fix A B assume *: "A \<in> sets M1" "B \<in> sets M2"
    1.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)"
    1.73 +    by auto
    1.74 +  also have "\<dots> \<in> sets M"
    1.75 +    by (rule sets.Int) (auto intro!: measurable_sets * f g)
    1.76 +  finally show "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M \<in> sets M" .
    1.77 +qed
    1.78 +
    1.79 +lemma measurable_fst[intro!, simp, measurable]: "fst \<in> measurable (M1 \<Otimes>\<^sub>M M2) M1"
    1.80 +  by (auto simp: fst_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    1.81 +    measurable_def)
    1.82 +
    1.83 +lemma measurable_snd[intro!, simp, measurable]: "snd \<in> measurable (M1 \<Otimes>\<^sub>M M2) M2"
    1.84 +  by (auto simp: snd_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    1.85 +    measurable_def)
    1.86 +
    1.87 +lemma measurable_Pair_compose_split[measurable_dest]:
    1.88 +  assumes f: "case_prod f \<in> measurable (M1 \<Otimes>\<^sub>M M2) N"
    1.89 +  assumes g: "g \<in> measurable M M1" and h: "h \<in> measurable M M2"
    1.90 +  shows "(\<lambda>x. f (g x) (h x)) \<in> measurable M N"
    1.91 +  using measurable_compose[OF measurable_Pair f, OF g h] by simp
    1.92 +
    1.93 +lemma measurable_Pair1_compose[measurable_dest]:
    1.94 +  assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    1.95 +  assumes [measurable]: "h \<in> measurable N M"
    1.96 +  shows "(\<lambda>x. f (h x)) \<in> measurable N M1"
    1.97 +  using measurable_compose[OF f measurable_fst] by simp
    1.98 +
    1.99 +lemma measurable_Pair2_compose[measurable_dest]:
   1.100 +  assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
   1.101 +  assumes [measurable]: "h \<in> measurable N M"
   1.102 +  shows "(\<lambda>x. g (h x)) \<in> measurable N M2"
   1.103 +  using measurable_compose[OF f measurable_snd] by simp
   1.104 +
   1.105 +lemma measurable_pair:
   1.106 +  assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
   1.107 +  shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
   1.108 +  using measurable_Pair[OF assms] by simp
   1.109 +
   1.110 +lemma
   1.111 +  assumes f[measurable]: "f \<in> measurable M (N \<Otimes>\<^sub>M P)"
   1.112 +  shows measurable_fst': "(\<lambda>x. fst (f x)) \<in> measurable M N"
   1.113 +    and measurable_snd': "(\<lambda>x. snd (f x)) \<in> measurable M P"
   1.114 +  by simp_all
   1.115 +
   1.116 +lemma
   1.117 +  assumes f[measurable]: "f \<in> measurable M N"
   1.118 +  shows measurable_fst'': "(\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^sub>M P) N"
   1.119 +    and measurable_snd'': "(\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^sub>M M) N"
   1.120 +  by simp_all
   1.121 +
   1.122 +lemma sets_pair_in_sets:
   1.123 +  assumes "\<And>a b. a \<in> sets A \<Longrightarrow> b \<in> sets B \<Longrightarrow> a \<times> b \<in> sets N"
   1.124 +  shows "sets (A \<Otimes>\<^sub>M B) \<subseteq> sets N"
   1.125 +  unfolding sets_pair_measure
   1.126 +  by (intro sets.sigma_sets_subset') (auto intro!: assms)
   1.127 +
   1.128 +lemma sets_pair_eq_sets_fst_snd:
   1.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})"
   1.130 +    (is "?P = sets (Sup {?fst, ?snd})")
   1.131 +proof -
   1.132 +  { fix a b assume ab: "a \<in> sets A" "b \<in> sets B"
   1.133 +    then have "a \<times> b = (fst -` a \<inter> (space A \<times> space B)) \<inter> (snd -` b \<inter> (space A \<times> space B))"
   1.134 +      by (auto dest: sets.sets_into_space)
   1.135 +    also have "\<dots> \<in> sets (Sup {?fst, ?snd})"
   1.136 +      apply (rule sets.Int)
   1.137 +      apply (rule in_sets_Sup)
   1.138 +      apply auto []
   1.139 +      apply (rule insertI1)
   1.140 +      apply (auto intro: ab in_vimage_algebra) []
   1.141 +      apply (rule in_sets_Sup)
   1.142 +      apply auto []
   1.143 +      apply (rule insertI2)
   1.144 +      apply (auto intro: ab in_vimage_algebra)
   1.145 +      done
   1.146 +    finally have "a \<times> b \<in> sets (Sup {?fst, ?snd})" . }
   1.147 +  moreover have "sets ?fst \<subseteq> sets (A \<Otimes>\<^sub>M B)"
   1.148 +    by (rule sets_image_in_sets) (auto simp: space_pair_measure[symmetric])
   1.149 +  moreover have "sets ?snd \<subseteq> sets (A \<Otimes>\<^sub>M B)"
   1.150 +    by (rule sets_image_in_sets) (auto simp: space_pair_measure)
   1.151 +  ultimately show ?thesis
   1.152 +    apply (intro antisym[of "sets A" for A] sets_Sup_in_sets sets_pair_in_sets)
   1.153 +    apply simp
   1.154 +    apply simp
   1.155 +    apply simp
   1.156 +    apply (elim disjE)
   1.157 +    apply (simp add: space_pair_measure)
   1.158 +    apply (simp add: space_pair_measure)
   1.159 +    apply (auto simp add: space_pair_measure)
   1.160 +    done
   1.161 +qed
   1.162 +
   1.163 +lemma measurable_pair_iff:
   1.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"
   1.165 +  by (auto intro: measurable_pair[of f M M1 M2])
   1.166 +
   1.167 +lemma measurable_split_conv:
   1.168 +  "(\<lambda>(x, y). f x y) \<in> measurable A B \<longleftrightarrow> (\<lambda>x. f (fst x) (snd x)) \<in> measurable A B"
   1.169 +  by (intro arg_cong2[where f="op \<in>"]) auto
   1.170 +
   1.171 +lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (M2 \<Otimes>\<^sub>M M1)"
   1.172 +  by (auto intro!: measurable_Pair simp: measurable_split_conv)
   1.173 +
   1.174 +lemma measurable_pair_swap:
   1.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"
   1.176 +  using measurable_comp[OF measurable_Pair f] by (auto simp: measurable_split_conv comp_def)
   1.177 +
   1.178 +lemma measurable_pair_swap_iff:
   1.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"
   1.180 +  by (auto dest: measurable_pair_swap)
   1.181 +
   1.182 +lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^sub>M M2)"
   1.183 +  by simp
   1.184 +
   1.185 +lemma sets_Pair1[measurable (raw)]:
   1.186 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "Pair x -` A \<in> sets M2"
   1.187 +proof -
   1.188 +  have "Pair x -` A = (if x \<in> space M1 then Pair x -` A \<inter> space M2 else {})"
   1.189 +    using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   1.190 +  also have "\<dots> \<in> sets M2"
   1.191 +    using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: if_split_asm)
   1.192 +  finally show ?thesis .
   1.193 +qed
   1.194 +
   1.195 +lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^sub>M M2)"
   1.196 +  by (auto intro!: measurable_Pair)
   1.197 +
   1.198 +lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
   1.199 +proof -
   1.200 +  have "(\<lambda>x. (x, y)) -` A = (if y \<in> space M2 then (\<lambda>x. (x, y)) -` A \<inter> space M1 else {})"
   1.201 +    using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   1.202 +  also have "\<dots> \<in> sets M1"
   1.203 +    using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: if_split_asm)
   1.204 +  finally show ?thesis .
   1.205 +qed
   1.206 +
   1.207 +lemma measurable_Pair2:
   1.208 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and x: "x \<in> space M1"
   1.209 +  shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   1.210 +  using measurable_comp[OF measurable_Pair1' f, OF x]
   1.211 +  by (simp add: comp_def)
   1.212 +
   1.213 +lemma measurable_Pair1:
   1.214 +  assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and y: "y \<in> space M2"
   1.215 +  shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
   1.216 +  using measurable_comp[OF measurable_Pair2' f, OF y]
   1.217 +  by (simp add: comp_def)
   1.218 +
   1.219 +lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
   1.220 +  unfolding Int_stable_def
   1.221 +  by safe (auto simp add: times_Int_times)
   1.222 +
   1.223 +lemma (in finite_measure) finite_measure_cut_measurable:
   1.224 +  assumes [measurable]: "Q \<in> sets (N \<Otimes>\<^sub>M M)"
   1.225 +  shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N"
   1.226 +    (is "?s Q \<in> _")
   1.227 +  using Int_stable_pair_measure_generator pair_measure_closed assms
   1.228 +  unfolding sets_pair_measure
   1.229 +proof (induct rule: sigma_sets_induct_disjoint)
   1.230 +  case (compl A)
   1.231 +  with sets.sets_into_space have "\<And>x. emeasure M (Pair x -` ((space N \<times> space M) - A)) =
   1.232 +      (if x \<in> space N then emeasure M (space M) - ?s A x else 0)"
   1.233 +    unfolding sets_pair_measure[symmetric]
   1.234 +    by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   1.235 +  with compl sets.top show ?case
   1.236 +    by (auto intro!: measurable_If simp: space_pair_measure)
   1.237 +next
   1.238 +  case (union F)
   1.239 +  then have "\<And>x. emeasure M (Pair x -` (\<Union>i. F i)) = (\<Sum>i. ?s (F i) x)"
   1.240 +    by (simp add: suminf_emeasure disjoint_family_on_vimageI subset_eq vimage_UN sets_pair_measure[symmetric])
   1.241 +  with union show ?case
   1.242 +    unfolding sets_pair_measure[symmetric] by simp
   1.243 +qed (auto simp add: if_distrib Int_def[symmetric] intro!: measurable_If)
   1.244 +
   1.245 +lemma (in sigma_finite_measure) measurable_emeasure_Pair:
   1.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> _")
   1.247 +proof -
   1.248 +  from sigma_finite_disjoint guess F . note F = this
   1.249 +  then have F_sets: "\<And>i. F i \<in> sets M" by auto
   1.250 +  let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   1.251 +  { fix i
   1.252 +    have [simp]: "space N \<times> F i \<inter> space N \<times> space M = space N \<times> F i"
   1.253 +      using F sets.sets_into_space by auto
   1.254 +    let ?R = "density M (indicator (F i))"
   1.255 +    have "finite_measure ?R"
   1.256 +      using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   1.257 +    then have "(\<lambda>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))) \<in> borel_measurable N"
   1.258 +     by (rule finite_measure.finite_measure_cut_measurable) (auto intro: Q)
   1.259 +    moreover have "\<And>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))
   1.260 +        = emeasure M (F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q))"
   1.261 +      using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   1.262 +    moreover have "\<And>x. F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q) = ?C x i"
   1.263 +      using sets.sets_into_space[OF Q] by (auto simp: space_pair_measure)
   1.264 +    ultimately have "(\<lambda>x. emeasure M (?C x i)) \<in> borel_measurable N"
   1.265 +      by simp }
   1.266 +  moreover
   1.267 +  { fix x
   1.268 +    have "(\<Sum>i. emeasure M (?C x i)) = emeasure M (\<Union>i. ?C x i)"
   1.269 +    proof (intro suminf_emeasure)
   1.270 +      show "range (?C x) \<subseteq> sets M"
   1.271 +        using F \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close> by (auto intro!: sets_Pair1)
   1.272 +      have "disjoint_family F" using F by auto
   1.273 +      show "disjoint_family (?C x)"
   1.274 +        by (rule disjoint_family_on_bisimulation[OF \<open>disjoint_family F\<close>]) auto
   1.275 +    qed
   1.276 +    also have "(\<Union>i. ?C x i) = Pair x -` Q"
   1.277 +      using F sets.sets_into_space[OF \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close>]
   1.278 +      by (auto simp: space_pair_measure)
   1.279 +    finally have "emeasure M (Pair x -` Q) = (\<Sum>i. emeasure M (?C x i))"
   1.280 +      by simp }
   1.281 +  ultimately show ?thesis using \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close> F_sets
   1.282 +    by auto
   1.283 +qed
   1.284 +
   1.285 +lemma (in sigma_finite_measure) measurable_emeasure[measurable (raw)]:
   1.286 +  assumes space: "\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M"
   1.287 +  assumes A: "{x\<in>space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M)"
   1.288 +  shows "(\<lambda>x. emeasure M (A x)) \<in> borel_measurable N"
   1.289 +proof -
   1.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"
   1.291 +    by (auto simp: space_pair_measure)
   1.292 +  with measurable_emeasure_Pair[OF A] show ?thesis
   1.293 +    by (auto cong: measurable_cong)
   1.294 +qed
   1.295 +
   1.296 +lemma (in sigma_finite_measure) emeasure_pair_measure:
   1.297 +  assumes "X \<in> sets (N \<Otimes>\<^sub>M M)"
   1.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")
   1.299 +proof (rule emeasure_measure_of[OF pair_measure_def])
   1.300 +  show "positive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   1.301 +    by (auto simp: positive_def)
   1.302 +  have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   1.303 +    by (auto simp: indicator_def)
   1.304 +  show "countably_additive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   1.305 +  proof (rule countably_additiveI)
   1.306 +    fix F :: "nat \<Rightarrow> ('b \<times> 'a) set" assume F: "range F \<subseteq> sets (N \<Otimes>\<^sub>M M)" "disjoint_family F"
   1.307 +    from F have *: "\<And>i. F i \<in> sets (N \<Otimes>\<^sub>M M)" by auto
   1.308 +    moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   1.309 +      by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   1.310 +    moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M"
   1.311 +      using F by (auto simp: sets_Pair1)
   1.312 +    ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   1.313 +      by (auto simp add: nn_integral_suminf[symmetric] vimage_UN suminf_emeasure
   1.314 +               intro!: nn_integral_cong nn_integral_indicator[symmetric])
   1.315 +  qed
   1.316 +  show "{a \<times> b |a b. a \<in> sets N \<and> b \<in> sets M} \<subseteq> Pow (space N \<times> space M)"
   1.317 +    using sets.space_closed[of N] sets.space_closed[of M] by auto
   1.318 +qed fact
   1.319 +
   1.320 +lemma (in sigma_finite_measure) emeasure_pair_measure_alt:
   1.321 +  assumes X: "X \<in> sets (N \<Otimes>\<^sub>M M)"
   1.322 +  shows "emeasure (N  \<Otimes>\<^sub>M M) X = (\<integral>\<^sup>+x. emeasure M (Pair x -` X) \<partial>N)"
   1.323 +proof -
   1.324 +  have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   1.325 +    by (auto simp: indicator_def)
   1.326 +  show ?thesis
   1.327 +    using X by (auto intro!: nn_integral_cong simp: emeasure_pair_measure sets_Pair1)
   1.328 +qed
   1.329 +
   1.330 +lemma (in sigma_finite_measure) emeasure_pair_measure_Times:
   1.331 +  assumes A: "A \<in> sets N" and B: "B \<in> sets M"
   1.332 +  shows "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = emeasure N A * emeasure M B"
   1.333 +proof -
   1.334 +  have "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = (\<integral>\<^sup>+x. emeasure M B * indicator A x \<partial>N)"
   1.335 +    using A B by (auto intro!: nn_integral_cong simp: emeasure_pair_measure_alt)
   1.336 +  also have "\<dots> = emeasure M B * emeasure N A"
   1.337 +    using A by (simp add: nn_integral_cmult_indicator)
   1.338 +  finally show ?thesis
   1.339 +    by (simp add: ac_simps)
   1.340 +qed
   1.341 +
   1.342 +subsection \<open>Binary products of $\sigma$-finite emeasure spaces\<close>
   1.343 +
   1.344 +locale pair_sigma_finite = M1?: sigma_finite_measure M1 + M2?: sigma_finite_measure M2
   1.345 +  for M1 :: "'a measure" and M2 :: "'b measure"
   1.346 +
   1.347 +lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
   1.348 +  "Q \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow> (\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   1.349 +  using M2.measurable_emeasure_Pair .
   1.350 +
   1.351 +lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
   1.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"
   1.353 +proof -
   1.354 +  have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   1.355 +    using Q measurable_pair_swap' by (auto intro: measurable_sets)
   1.356 +  note M1.measurable_emeasure_Pair[OF this]
   1.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"
   1.358 +    using Q[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   1.359 +  ultimately show ?thesis by simp
   1.360 +qed
   1.361 +
   1.362 +lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
   1.363 +  defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
   1.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>
   1.365 +    (\<forall>i. emeasure (M1 \<Otimes>\<^sub>M M2) (F i) \<noteq> \<infinity>)"
   1.366 +proof -
   1.367 +  from M1.sigma_finite_incseq guess F1 . note F1 = this
   1.368 +  from M2.sigma_finite_incseq guess F2 . note F2 = this
   1.369 +  from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   1.370 +  let ?F = "\<lambda>i. F1 i \<times> F2 i"
   1.371 +  show ?thesis
   1.372 +  proof (intro exI[of _ ?F] conjI allI)
   1.373 +    show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)
   1.374 +  next
   1.375 +    have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   1.376 +    proof (intro subsetI)
   1.377 +      fix x assume "x \<in> space M1 \<times> space M2"
   1.378 +      then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"
   1.379 +        by (auto simp: space)
   1.380 +      then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"
   1.381 +        using \<open>incseq F1\<close> \<open>incseq F2\<close> unfolding incseq_def
   1.382 +        by (force split: split_max)+
   1.383 +      then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"
   1.384 +        by (intro SigmaI) (auto simp add: max.commute)
   1.385 +      then show "x \<in> (\<Union>i. ?F i)" by auto
   1.386 +    qed
   1.387 +    then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   1.388 +      using space by (auto simp: space)
   1.389 +  next
   1.390 +    fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"
   1.391 +      using \<open>incseq F1\<close> \<open>incseq F2\<close> unfolding incseq_Suc_iff by auto
   1.392 +  next
   1.393 +    fix i
   1.394 +    from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   1.395 +    with F1 F2 show "emeasure (M1 \<Otimes>\<^sub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"
   1.396 +      by (auto simp add: emeasure_pair_measure_Times ennreal_mult_eq_top_iff)
   1.397 +  qed
   1.398 +qed
   1.399 +
   1.400 +sublocale pair_sigma_finite \<subseteq> P?: sigma_finite_measure "M1 \<Otimes>\<^sub>M M2"
   1.401 +proof
   1.402 +  from M1.sigma_finite_countable guess F1 ..
   1.403 +  moreover from M2.sigma_finite_countable guess F2 ..
   1.404 +  ultimately show
   1.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>)"
   1.406 +    by (intro exI[of _ "(\<lambda>(a, b). a \<times> b) ` (F1 \<times> F2)"] conjI)
   1.407 +       (auto simp: M2.emeasure_pair_measure_Times space_pair_measure set_eq_iff subset_eq ennreal_mult_eq_top_iff)
   1.408 +qed
   1.409 +
   1.410 +lemma sigma_finite_pair_measure:
   1.411 +  assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"
   1.412 +  shows "sigma_finite_measure (A \<Otimes>\<^sub>M B)"
   1.413 +proof -
   1.414 +  interpret A: sigma_finite_measure A by fact
   1.415 +  interpret B: sigma_finite_measure B by fact
   1.416 +  interpret AB: pair_sigma_finite A  B ..
   1.417 +  show ?thesis ..
   1.418 +qed
   1.419 +
   1.420 +lemma sets_pair_swap:
   1.421 +  assumes "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   1.422 +  shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   1.423 +  using measurable_pair_swap' assms by (rule measurable_sets)
   1.424 +
   1.425 +lemma (in pair_sigma_finite) distr_pair_swap:
   1.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")
   1.427 +proof -
   1.428 +  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   1.429 +  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   1.430 +  show ?thesis
   1.431 +  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   1.432 +    show "?E \<subseteq> Pow (space ?P)"
   1.433 +      using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   1.434 +    show "sets ?P = sigma_sets (space ?P) ?E"
   1.435 +      by (simp add: sets_pair_measure space_pair_measure)
   1.436 +    then show "sets ?D = sigma_sets (space ?P) ?E"
   1.437 +      by simp
   1.438 +  next
   1.439 +    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   1.440 +      using F by (auto simp: space_pair_measure)
   1.441 +  next
   1.442 +    fix X assume "X \<in> ?E"
   1.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
   1.444 +    have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^sub>M M1) = B \<times> A"
   1.445 +      using sets.sets_into_space[OF A] sets.sets_into_space[OF B] by (auto simp: space_pair_measure)
   1.446 +    with A B show "emeasure (M1 \<Otimes>\<^sub>M M2) X = emeasure ?D X"
   1.447 +      by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_pair_measure_Times emeasure_distr
   1.448 +                    measurable_pair_swap' ac_simps)
   1.449 +  qed
   1.450 +qed
   1.451 +
   1.452 +lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:
   1.453 +  assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   1.454 +  shows "emeasure (M1 \<Otimes>\<^sub>M M2) A = (\<integral>\<^sup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   1.455 +    (is "_ = ?\<nu> A")
   1.456 +proof -
   1.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"
   1.458 +    using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
   1.459 +  show ?thesis using A
   1.460 +    by (subst distr_pair_swap)
   1.461 +       (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
   1.462 +                 M1.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   1.463 +qed
   1.464 +
   1.465 +lemma (in pair_sigma_finite) AE_pair:
   1.466 +  assumes "AE x in (M1 \<Otimes>\<^sub>M M2). Q x"
   1.467 +  shows "AE x in M1. (AE y in M2. Q (x, y))"
   1.468 +proof -
   1.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"
   1.470 +    using assms unfolding eventually_ae_filter by auto
   1.471 +  show ?thesis
   1.472 +  proof (rule AE_I)
   1.473 +    from N measurable_emeasure_Pair1[OF \<open>N \<in> sets (M1 \<Otimes>\<^sub>M M2)\<close>]
   1.474 +    show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
   1.475 +      by (auto simp: M2.emeasure_pair_measure_alt nn_integral_0_iff)
   1.476 +    show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   1.477 +      by (intro borel_measurable_eq measurable_emeasure_Pair1 N sets.sets_Collect_neg N) simp
   1.478 +    { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
   1.479 +      have "AE y in M2. Q (x, y)"
   1.480 +      proof (rule AE_I)
   1.481 +        show "emeasure M2 (Pair x -` N) = 0" by fact
   1.482 +        show "Pair x -` N \<in> sets M2" using N(1) by (rule sets_Pair1)
   1.483 +        show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
   1.484 +          using N \<open>x \<in> space M1\<close> unfolding space_pair_measure by auto
   1.485 +      qed }
   1.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}"
   1.487 +      by auto
   1.488 +  qed
   1.489 +qed
   1.490 +
   1.491 +lemma (in pair_sigma_finite) AE_pair_measure:
   1.492 +  assumes "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   1.493 +  assumes ae: "AE x in M1. AE y in M2. P (x, y)"
   1.494 +  shows "AE x in M1 \<Otimes>\<^sub>M M2. P x"
   1.495 +proof (subst AE_iff_measurable[OF _ refl])
   1.496 +  show "{x\<in>space (M1 \<Otimes>\<^sub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   1.497 +    by (rule sets.sets_Collect) fact
   1.498 +  then have "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} =
   1.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)"
   1.500 +    by (simp add: M2.emeasure_pair_measure)
   1.501 +  also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. 0 \<partial>M2 \<partial>M1)"
   1.502 +    using ae
   1.503 +    apply (safe intro!: nn_integral_cong_AE)
   1.504 +    apply (intro AE_I2)
   1.505 +    apply (safe intro!: nn_integral_cong_AE)
   1.506 +    apply auto
   1.507 +    done
   1.508 +  finally show "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} = 0" by simp
   1.509 +qed
   1.510 +
   1.511 +lemma (in pair_sigma_finite) AE_pair_iff:
   1.512 +  "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow>
   1.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))"
   1.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
   1.515 +
   1.516 +lemma (in pair_sigma_finite) AE_commute:
   1.517 +  assumes P: "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   1.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)"
   1.519 +proof -
   1.520 +  interpret Q: pair_sigma_finite M2 M1 ..
   1.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"
   1.522 +    by auto
   1.523 +  have "{x \<in> space (M2 \<Otimes>\<^sub>M M1). P (snd x) (fst x)} =
   1.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)"
   1.525 +    by (auto simp: space_pair_measure)
   1.526 +  also have "\<dots> \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   1.527 +    by (intro sets_pair_swap P)
   1.528 +  finally show ?thesis
   1.529 +    apply (subst AE_pair_iff[OF P])
   1.530 +    apply (subst distr_pair_swap)
   1.531 +    apply (subst AE_distr_iff[OF measurable_pair_swap' P])
   1.532 +    apply (subst Q.AE_pair_iff)
   1.533 +    apply simp_all
   1.534 +    done
   1.535 +qed
   1.536 +
   1.537 +subsection "Fubinis theorem"
   1.538 +
   1.539 +lemma measurable_compose_Pair1:
   1.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"
   1.541 +  by simp
   1.542 +
   1.543 +lemma (in sigma_finite_measure) borel_measurable_nn_integral_fst:
   1.544 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
   1.545 +  shows "(\<lambda>x. \<integral>\<^sup>+ y. f (x, y) \<partial>M) \<in> borel_measurable M1"
   1.546 +using f proof induct
   1.547 +  case (cong u v)
   1.548 +  then have "\<And>w x. w \<in> space M1 \<Longrightarrow> x \<in> space M \<Longrightarrow> u (w, x) = v (w, x)"
   1.549 +    by (auto simp: space_pair_measure)
   1.550 +  show ?case
   1.551 +    apply (subst measurable_cong)
   1.552 +    apply (rule nn_integral_cong)
   1.553 +    apply fact+
   1.554 +    done
   1.555 +next
   1.556 +  case (set Q)
   1.557 +  have [simp]: "\<And>x y. indicator Q (x, y) = indicator (Pair x -` Q) y"
   1.558 +    by (auto simp: indicator_def)
   1.559 +  have "\<And>x. x \<in> space M1 \<Longrightarrow> emeasure M (Pair x -` Q) = \<integral>\<^sup>+ y. indicator Q (x, y) \<partial>M"
   1.560 +    by (simp add: sets_Pair1[OF set])
   1.561 +  from this measurable_emeasure_Pair[OF set] show ?case
   1.562 +    by (rule measurable_cong[THEN iffD1])
   1.563 +qed (simp_all add: nn_integral_add nn_integral_cmult measurable_compose_Pair1
   1.564 +                   nn_integral_monotone_convergence_SUP incseq_def le_fun_def
   1.565 +              cong: measurable_cong)
   1.566 +
   1.567 +lemma (in sigma_finite_measure) nn_integral_fst:
   1.568 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
   1.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 = _")
   1.570 +using f proof induct
   1.571 +  case (cong u v)
   1.572 +  then have "?I u = ?I v"
   1.573 +    by (intro nn_integral_cong) (auto simp: space_pair_measure)
   1.574 +  with cong show ?case
   1.575 +    by (simp cong: nn_integral_cong)
   1.576 +qed (simp_all add: emeasure_pair_measure nn_integral_cmult nn_integral_add
   1.577 +                   nn_integral_monotone_convergence_SUP measurable_compose_Pair1
   1.578 +                   borel_measurable_nn_integral_fst nn_integral_mono incseq_def le_fun_def
   1.579 +              cong: nn_integral_cong)
   1.580 +
   1.581 +lemma (in sigma_finite_measure) borel_measurable_nn_integral[measurable (raw)]:
   1.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"
   1.583 +  using borel_measurable_nn_integral_fst[of "case_prod f" N] by simp
   1.584 +
   1.585 +lemma (in pair_sigma_finite) nn_integral_snd:
   1.586 +  assumes f[measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   1.587 +  shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   1.588 +proof -
   1.589 +  note measurable_pair_swap[OF f]
   1.590 +  from M1.nn_integral_fst[OF this]
   1.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))"
   1.592 +    by simp
   1.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"
   1.594 +    by (subst distr_pair_swap) (auto simp add: nn_integral_distr intro!: nn_integral_cong)
   1.595 +  finally show ?thesis .
   1.596 +qed
   1.597 +
   1.598 +lemma (in pair_sigma_finite) Fubini:
   1.599 +  assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   1.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)"
   1.601 +  unfolding nn_integral_snd[OF assms] M2.nn_integral_fst[OF assms] ..
   1.602 +
   1.603 +lemma (in pair_sigma_finite) Fubini':
   1.604 +  assumes f: "case_prod f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   1.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)"
   1.606 +  using Fubini[OF f] by simp
   1.607 +
   1.608 +subsection \<open>Products on counting spaces, densities and distributions\<close>
   1.609 +
   1.610 +lemma sigma_prod:
   1.611 +  assumes X_cover: "\<exists>E\<subseteq>A. countable E \<and> X = \<Union>E" and A: "A \<subseteq> Pow X"
   1.612 +  assumes Y_cover: "\<exists>E\<subseteq>B. countable E \<and> Y = \<Union>E" and B: "B \<subseteq> Pow Y"
   1.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}"
   1.614 +    (is "?P = ?S")
   1.615 +proof (rule measure_eqI)
   1.616 +  have [simp]: "snd \<in> X \<times> Y \<rightarrow> Y" "fst \<in> X \<times> Y \<rightarrow> X"
   1.617 +    by auto
   1.618 +  let ?XY = "{{fst -` a \<inter> X \<times> Y | a. a \<in> A}, {snd -` b \<inter> X \<times> Y | b. b \<in> B}}"
   1.619 +  have "sets ?P = sets (SUP xy:?XY. sigma (X \<times> Y) xy)"
   1.620 +    by (simp add: vimage_algebra_sigma sets_pair_eq_sets_fst_snd A B)
   1.621 +  also have "\<dots> = sets (sigma (X \<times> Y) (\<Union>?XY))"
   1.622 +    by (intro Sup_sigma arg_cong[where f=sets]) auto
   1.623 +  also have "\<dots> = sets ?S"
   1.624 +  proof (intro arg_cong[where f=sets] sigma_eqI sigma_sets_eqI)
   1.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)"
   1.626 +      using A B by auto
   1.627 +  next
   1.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}"
   1.629 +      using A B by (intro sigma_algebra_sigma_sets) auto
   1.630 +    fix Z assume "Z \<in> \<Union>?XY"
   1.631 +    then show "Z \<in> sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   1.632 +    proof safe
   1.633 +      fix a assume "a \<in> A"
   1.634 +      from Y_cover obtain E where E: "E \<subseteq> B" "countable E" and "Y = \<Union>E"
   1.635 +        by auto
   1.636 +      with \<open>a \<in> A\<close> A have eq: "fst -` a \<inter> X \<times> Y = (\<Union>e\<in>E. a \<times> e)"
   1.637 +        by auto
   1.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}"
   1.639 +        using \<open>a \<in> A\<close> E unfolding eq by (auto intro!: XY.countable_UN')
   1.640 +    next
   1.641 +      fix b assume "b \<in> B"
   1.642 +      from X_cover obtain E where E: "E \<subseteq> A" "countable E" and "X = \<Union>E"
   1.643 +        by auto
   1.644 +      with \<open>b \<in> B\<close> B have eq: "snd -` b \<inter> X \<times> Y = (\<Union>e\<in>E. e \<times> b)"
   1.645 +        by auto
   1.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}"
   1.647 +        using \<open>b \<in> B\<close> E unfolding eq by (auto intro!: XY.countable_UN')
   1.648 +    qed
   1.649 +  next
   1.650 +    fix Z assume "Z \<in> {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   1.651 +    then obtain a b where "Z = a \<times> b" and ab: "a \<in> A" "b \<in> B"
   1.652 +      by auto
   1.653 +    then have Z: "Z = (fst -` a \<inter> X \<times> Y) \<inter> (snd -` b \<inter> X \<times> Y)"
   1.654 +      using A B by auto
   1.655 +    interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) (\<Union>?XY)"
   1.656 +      by (intro sigma_algebra_sigma_sets) auto
   1.657 +    show "Z \<in> sigma_sets (X \<times> Y) (\<Union>?XY)"
   1.658 +      unfolding Z by (rule XY.Int) (blast intro: ab)+
   1.659 +  qed
   1.660 +  finally show "sets ?P = sets ?S" .
   1.661 +next
   1.662 +  interpret finite_measure "sigma X A" for X A
   1.663 +    proof qed (simp add: emeasure_sigma)
   1.664 +  fix A assume "A \<in> sets ?P" then show "emeasure ?P A = emeasure ?S A"
   1.665 +    by (simp add: emeasure_pair_measure_alt emeasure_sigma)
   1.666 +qed
   1.667 +
   1.668 +lemma sigma_sets_pair_measure_generator_finite:
   1.669 +  assumes "finite A" and "finite B"
   1.670 +  shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"
   1.671 +  (is "sigma_sets ?prod ?sets = _")
   1.672 +proof safe
   1.673 +  have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
   1.674 +  fix x assume subset: "x \<subseteq> A \<times> B"
   1.675 +  hence "finite x" using fin by (rule finite_subset)
   1.676 +  from this subset show "x \<in> sigma_sets ?prod ?sets"
   1.677 +  proof (induct x)
   1.678 +    case empty show ?case by (rule sigma_sets.Empty)
   1.679 +  next
   1.680 +    case (insert a x)
   1.681 +    hence "{a} \<in> sigma_sets ?prod ?sets" by auto
   1.682 +    moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
   1.683 +    ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
   1.684 +  qed
   1.685 +next
   1.686 +  fix x a b
   1.687 +  assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"
   1.688 +  from sigma_sets_into_sp[OF _ this(1)] this(2)
   1.689 +  show "a \<in> A" and "b \<in> B" by auto
   1.690 +qed
   1.691 +
   1.692 +lemma borel_prod:
   1.693 +  "(borel \<Otimes>\<^sub>M borel) = (borel :: ('a::second_countable_topology \<times> 'b::second_countable_topology) measure)"
   1.694 +  (is "?P = ?B")
   1.695 +proof -
   1.696 +  have "?B = sigma UNIV {A \<times> B | A B. open A \<and> open B}"
   1.697 +    by (rule second_countable_borel_measurable[OF open_prod_generated])
   1.698 +  also have "\<dots> = ?P"
   1.699 +    unfolding borel_def
   1.700 +    by (subst sigma_prod) (auto intro!: exI[of _ "{UNIV}"])
   1.701 +  finally show ?thesis ..
   1.702 +qed
   1.703 +
   1.704 +lemma pair_measure_count_space:
   1.705 +  assumes A: "finite A" and B: "finite B"
   1.706 +  shows "count_space A \<Otimes>\<^sub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")
   1.707 +proof (rule measure_eqI)
   1.708 +  interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact
   1.709 +  interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact
   1.710 +  interpret P: pair_sigma_finite "count_space A" "count_space B" ..
   1.711 +  show eq: "sets ?P = sets ?C"
   1.712 +    by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)
   1.713 +  fix X assume X: "X \<in> sets ?P"
   1.714 +  with eq have X_subset: "X \<subseteq> A \<times> B" by simp
   1.715 +  with A B have fin_Pair: "\<And>x. finite (Pair x -` X)"
   1.716 +    by (intro finite_subset[OF _ B]) auto
   1.717 +  have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
   1.718 +  have pos_card: "(0::ennreal) < of_nat (card (Pair x -` X)) \<longleftrightarrow> Pair x -` X \<noteq> {}" for x
   1.719 +    by (auto simp: card_eq_0_iff fin_Pair) blast
   1.720 +
   1.721 +  show "emeasure ?P X = emeasure ?C X"
   1.722 +    using X_subset A fin_Pair fin_X
   1.723 +    apply (subst B.emeasure_pair_measure_alt[OF X])
   1.724 +    apply (subst emeasure_count_space)
   1.725 +    apply (auto simp add: emeasure_count_space nn_integral_count_space
   1.726 +                          pos_card of_nat_setsum[symmetric] card_SigmaI[symmetric]
   1.727 +                simp del: of_nat_setsum card_SigmaI
   1.728 +                intro!: arg_cong[where f=card])
   1.729 +    done
   1.730 +qed
   1.731 +
   1.732 +
   1.733 +lemma emeasure_prod_count_space:
   1.734 +  assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M M)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")
   1.735 +  shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator A (x, y) \<partial>?B \<partial>?A)"
   1.736 +  by (rule emeasure_measure_of[OF pair_measure_def])
   1.737 +     (auto simp: countably_additive_def positive_def suminf_indicator A
   1.738 +                 nn_integral_suminf[symmetric] dest: sets.sets_into_space)
   1.739 +
   1.740 +lemma emeasure_prod_count_space_single[simp]: "emeasure (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) {x} = 1"
   1.741 +proof -
   1.742 +  have [simp]: "\<And>a b x y. indicator {(a, b)} (x, y) = (indicator {a} x * indicator {b} y::ennreal)"
   1.743 +    by (auto split: split_indicator)
   1.744 +  show ?thesis
   1.745 +    by (cases x) (auto simp: emeasure_prod_count_space nn_integral_cmult sets_Pair)
   1.746 +qed
   1.747 +
   1.748 +lemma emeasure_count_space_prod_eq:
   1.749 +  fixes A :: "('a \<times> 'b) set"
   1.750 +  assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M count_space UNIV)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")
   1.751 +  shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"
   1.752 +proof -
   1.753 +  { fix A :: "('a \<times> 'b) set" assume "countable A"
   1.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)"
   1.755 +      by (intro emeasure_UN_countable) (auto simp: sets_Pair disjoint_family_on_def)
   1.756 +    also have "\<dots> = (\<integral>\<^sup>+a. indicator A a \<partial>count_space UNIV)"
   1.757 +      by (subst nn_integral_count_space_indicator) auto
   1.758 +    finally have "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"
   1.759 +      by simp }
   1.760 +  note * = this
   1.761 +
   1.762 +  show ?thesis
   1.763 +  proof cases
   1.764 +    assume "finite A" then show ?thesis
   1.765 +      by (intro * countable_finite)
   1.766 +  next
   1.767 +    assume "infinite A"
   1.768 +    then obtain C where "countable C" and "infinite C" and "C \<subseteq> A"
   1.769 +      by (auto dest: infinite_countable_subset')
   1.770 +    with A have "emeasure (?A \<Otimes>\<^sub>M ?B) C \<le> emeasure (?A \<Otimes>\<^sub>M ?B) A"
   1.771 +      by (intro emeasure_mono) auto
   1.772 +    also have "emeasure (?A \<Otimes>\<^sub>M ?B) C = emeasure (count_space UNIV) C"
   1.773 +      using \<open>countable C\<close> by (rule *)
   1.774 +    finally show ?thesis
   1.775 +      using \<open>infinite C\<close> \<open>infinite A\<close> by (simp add: top_unique)
   1.776 +  qed
   1.777 +qed
   1.778 +
   1.779 +lemma nn_integral_count_space_prod_eq:
   1.780 +  "nn_integral (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) f = nn_integral (count_space UNIV) f"
   1.781 +    (is "nn_integral ?P f = _")
   1.782 +proof cases
   1.783 +  assume cntbl: "countable {x. f x \<noteq> 0}"
   1.784 +  have [simp]: "\<And>x. card ({x} \<inter> {x. f x \<noteq> 0}) = (indicator {x. f x \<noteq> 0} x::ennreal)"
   1.785 +    by (auto split: split_indicator)
   1.786 +  have [measurable]: "\<And>y. (\<lambda>x. indicator {y} x) \<in> borel_measurable ?P"
   1.787 +    by (rule measurable_discrete_difference[of "\<lambda>x. 0" _ borel "{y}" "\<lambda>x. indicator {y} x" for y])
   1.788 +       (auto intro: sets_Pair)
   1.789 +
   1.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)"
   1.791 +    by (auto simp add: nn_integral_cmult nn_integral_indicator' intro!: nn_integral_cong split: split_indicator)
   1.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)"
   1.793 +    by (auto intro!: nn_integral_cong split: split_indicator)
   1.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})"
   1.795 +    by (intro nn_integral_count_space_nn_integral cntbl) auto
   1.796 +  also have "\<dots> = (\<integral>\<^sup>+x'. f x' \<partial>count_space {x. f x \<noteq> 0})"
   1.797 +    by (intro nn_integral_cong) (auto simp: nn_integral_cmult sets_Pair)
   1.798 +  finally show ?thesis
   1.799 +    by (auto simp add: nn_integral_count_space_indicator intro!: nn_integral_cong split: split_indicator)
   1.800 +next
   1.801 +  { fix x assume "f x \<noteq> 0"
   1.802 +    then have "(\<exists>r\<ge>0. 0 < r \<and> f x = ennreal r) \<or> f x = \<infinity>"
   1.803 +      by (cases "f x" rule: ennreal_cases) (auto simp: less_le)
   1.804 +    then have "\<exists>n. ennreal (1 / real (Suc n)) \<le> f x"
   1.805 +      by (auto elim!: nat_approx_posE intro!: less_imp_le) }
   1.806 +  note * = this
   1.807 +
   1.808 +  assume cntbl: "uncountable {x. f x \<noteq> 0}"
   1.809 +  also have "{x. f x \<noteq> 0} = (\<Union>n. {x. 1/Suc n \<le> f x})"
   1.810 +    using * by auto
   1.811 +  finally obtain n where "infinite {x. 1/Suc n \<le> f x}"
   1.812 +    by (meson countableI_type countable_UN uncountable_infinite)
   1.813 +  then obtain C where C: "C \<subseteq> {x. 1/Suc n \<le> f x}" and "countable C" "infinite C"
   1.814 +    by (metis infinite_countable_subset')
   1.815 +
   1.816 +  have [measurable]: "C \<in> sets ?P"
   1.817 +    using sets.countable[OF _ \<open>countable C\<close>, of ?P] by (auto simp: sets_Pair)
   1.818 +
   1.819 +  have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>?P) \<le> nn_integral ?P f"
   1.820 +    using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])
   1.821 +  moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>?P) = \<infinity>"
   1.822 +    using \<open>infinite C\<close> by (simp add: nn_integral_cmult emeasure_count_space_prod_eq ennreal_mult_top)
   1.823 +  moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>count_space UNIV) \<le> nn_integral (count_space UNIV) f"
   1.824 +    using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])
   1.825 +  moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>count_space UNIV) = \<infinity>"
   1.826 +    using \<open>infinite C\<close> by (simp add: nn_integral_cmult ennreal_mult_top)
   1.827 +  ultimately show ?thesis
   1.828 +    by (simp add: top_unique)
   1.829 +qed
   1.830 +
   1.831 +lemma pair_measure_density:
   1.832 +  assumes f: "f \<in> borel_measurable M1"
   1.833 +  assumes g: "g \<in> borel_measurable M2"
   1.834 +  assumes "sigma_finite_measure M2" "sigma_finite_measure (density M2 g)"
   1.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")
   1.836 +proof (rule measure_eqI)
   1.837 +  interpret M2: sigma_finite_measure M2 by fact
   1.838 +  interpret D2: sigma_finite_measure "density M2 g" by fact
   1.839 +
   1.840 +  fix A assume A: "A \<in> sets ?L"
   1.841 +  with f g have "(\<integral>\<^sup>+ x. f x * \<integral>\<^sup>+ y. g y * indicator A (x, y) \<partial>M2 \<partial>M1) =
   1.842 +    (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f x * g y * indicator A (x, y) \<partial>M2 \<partial>M1)"
   1.843 +    by (intro nn_integral_cong_AE)
   1.844 +       (auto simp add: nn_integral_cmult[symmetric] ac_simps)
   1.845 +  with A f g show "emeasure ?L A = emeasure ?R A"
   1.846 +    by (simp add: D2.emeasure_pair_measure emeasure_density nn_integral_density
   1.847 +                  M2.nn_integral_fst[symmetric]
   1.848 +             cong: nn_integral_cong)
   1.849 +qed simp
   1.850 +
   1.851 +lemma sigma_finite_measure_distr:
   1.852 +  assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"
   1.853 +  shows "sigma_finite_measure M"
   1.854 +proof -
   1.855 +  interpret sigma_finite_measure "distr M N f" by fact
   1.856 +  from sigma_finite_countable guess A .. note A = this
   1.857 +  show ?thesis
   1.858 +  proof
   1.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>)"
   1.860 +      using A f
   1.861 +      by (intro exI[of _ "(\<lambda>a. f -` a \<inter> space M) ` A"])
   1.862 +         (auto simp: emeasure_distr set_eq_iff subset_eq intro: measurable_space)
   1.863 +  qed
   1.864 +qed
   1.865 +
   1.866 +lemma pair_measure_distr:
   1.867 +  assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"
   1.868 +  assumes "sigma_finite_measure (distr N T g)"
   1.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")
   1.870 +proof (rule measure_eqI)
   1.871 +  interpret T: sigma_finite_measure "distr N T g" by fact
   1.872 +  interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+
   1.873 +
   1.874 +  fix A assume A: "A \<in> sets ?P"
   1.875 +  with f g show "emeasure ?P A = emeasure ?D A"
   1.876 +    by (auto simp add: N.emeasure_pair_measure_alt space_pair_measure emeasure_distr
   1.877 +                       T.emeasure_pair_measure_alt nn_integral_distr
   1.878 +             intro!: nn_integral_cong arg_cong[where f="emeasure N"])
   1.879 +qed simp
   1.880 +
   1.881 +lemma pair_measure_eqI:
   1.882 +  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
   1.883 +  assumes sets: "sets (M1 \<Otimes>\<^sub>M M2) = sets M"
   1.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)"
   1.885 +  shows "M1 \<Otimes>\<^sub>M M2 = M"
   1.886 +proof -
   1.887 +  interpret M1: sigma_finite_measure M1 by fact
   1.888 +  interpret M2: sigma_finite_measure M2 by fact
   1.889 +  interpret pair_sigma_finite M1 M2 ..
   1.890 +  from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   1.891 +  let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   1.892 +  let ?P = "M1 \<Otimes>\<^sub>M M2"
   1.893 +  show ?thesis
   1.894 +  proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   1.895 +    show "?E \<subseteq> Pow (space ?P)"
   1.896 +      using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   1.897 +    show "sets ?P = sigma_sets (space ?P) ?E"
   1.898 +      by (simp add: sets_pair_measure space_pair_measure)
   1.899 +    then show "sets M = sigma_sets (space ?P) ?E"
   1.900 +      using sets[symmetric] by simp
   1.901 +  next
   1.902 +    show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   1.903 +      using F by (auto simp: space_pair_measure)
   1.904 +  next
   1.905 +    fix X assume "X \<in> ?E"
   1.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
   1.907 +    then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
   1.908 +       by (simp add: M2.emeasure_pair_measure_Times)
   1.909 +    also have "\<dots> = emeasure M (A \<times> B)"
   1.910 +      using A B emeasure by auto
   1.911 +    finally show "emeasure ?P X = emeasure M X"
   1.912 +      by simp
   1.913 +  qed
   1.914 +qed
   1.915 +
   1.916 +lemma sets_pair_countable:
   1.917 +  assumes "countable S1" "countable S2"
   1.918 +  assumes M: "sets M = Pow S1" and N: "sets N = Pow S2"
   1.919 +  shows "sets (M \<Otimes>\<^sub>M N) = Pow (S1 \<times> S2)"
   1.920 +proof auto
   1.921 +  fix x a b assume x: "x \<in> sets (M \<Otimes>\<^sub>M N)" "(a, b) \<in> x"
   1.922 +  from sets.sets_into_space[OF x(1)] x(2)
   1.923 +    sets_eq_imp_space_eq[of N "count_space S2"] sets_eq_imp_space_eq[of M "count_space S1"] M N
   1.924 +  show "a \<in> S1" "b \<in> S2"
   1.925 +    by (auto simp: space_pair_measure)
   1.926 +next
   1.927 +  fix X assume X: "X \<subseteq> S1 \<times> S2"
   1.928 +  then have "countable X"
   1.929 +    by (metis countable_subset \<open>countable S1\<close> \<open>countable S2\<close> countable_SIGMA)
   1.930 +  have "X = (\<Union>(a, b)\<in>X. {a} \<times> {b})" by auto
   1.931 +  also have "\<dots> \<in> sets (M \<Otimes>\<^sub>M N)"
   1.932 +    using X
   1.933 +    by (safe intro!: sets.countable_UN' \<open>countable X\<close> subsetI pair_measureI) (auto simp: M N)
   1.934 +  finally show "X \<in> sets (M \<Otimes>\<^sub>M N)" .
   1.935 +qed
   1.936 +
   1.937 +lemma pair_measure_countable:
   1.938 +  assumes "countable S1" "countable S2"
   1.939 +  shows "count_space S1 \<Otimes>\<^sub>M count_space S2 = count_space (S1 \<times> S2)"
   1.940 +proof (rule pair_measure_eqI)
   1.941 +  show "sigma_finite_measure (count_space S1)" "sigma_finite_measure (count_space S2)"
   1.942 +    using assms by (auto intro!: sigma_finite_measure_count_space_countable)
   1.943 +  show "sets (count_space S1 \<Otimes>\<^sub>M count_space S2) = sets (count_space (S1 \<times> S2))"
   1.944 +    by (subst sets_pair_countable[OF assms]) auto
   1.945 +next
   1.946 +  fix A B assume "A \<in> sets (count_space S1)" "B \<in> sets (count_space S2)"
   1.947 +  then show "emeasure (count_space S1) A * emeasure (count_space S2) B =
   1.948 +    emeasure (count_space (S1 \<times> S2)) (A \<times> B)"
   1.949 +    by (subst (1 2 3) emeasure_count_space) (auto simp: finite_cartesian_product_iff ennreal_mult_top ennreal_top_mult)
   1.950 +qed
   1.951 +
   1.952 +lemma nn_integral_fst_count_space:
   1.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"
   1.954 +  (is "?lhs = ?rhs")
   1.955 +proof(cases)
   1.956 +  assume *: "countable {xy. f xy \<noteq> 0}"
   1.957 +  let ?A = "fst ` {xy. f xy \<noteq> 0}"
   1.958 +  let ?B = "snd ` {xy. f xy \<noteq> 0}"
   1.959 +  from * have [simp]: "countable ?A" "countable ?B" by(rule countable_image)+
   1.960 +  have "?lhs = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space ?A)"
   1.961 +    by(rule nn_integral_count_space_eq)
   1.962 +      (auto simp add: nn_integral_0_iff_AE AE_count_space not_le intro: rev_image_eqI)
   1.963 +  also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space ?B \<partial>count_space ?A)"
   1.964 +    by(intro nn_integral_count_space_eq nn_integral_cong)(auto intro: rev_image_eqI)
   1.965 +  also have "\<dots> = (\<integral>\<^sup>+ xy. f xy \<partial>count_space (?A \<times> ?B))"
   1.966 +    by(subst sigma_finite_measure.nn_integral_fst)
   1.967 +      (simp_all add: sigma_finite_measure_count_space_countable pair_measure_countable)
   1.968 +  also have "\<dots> = ?rhs"
   1.969 +    by(rule nn_integral_count_space_eq)(auto intro: rev_image_eqI)
   1.970 +  finally show ?thesis .
   1.971 +next
   1.972 +  { fix xy assume "f xy \<noteq> 0"
   1.973 +    then have "(\<exists>r\<ge>0. 0 < r \<and> f xy = ennreal r) \<or> f xy = \<infinity>"
   1.974 +      by (cases "f xy" rule: ennreal_cases) (auto simp: less_le)
   1.975 +    then have "\<exists>n. ennreal (1 / real (Suc n)) \<le> f xy"
   1.976 +      by (auto elim!: nat_approx_posE intro!: less_imp_le) }
   1.977 +  note * = this
   1.978 +
   1.979 +  assume cntbl: "uncountable {xy. f xy \<noteq> 0}"
   1.980 +  also have "{xy. f xy \<noteq> 0} = (\<Union>n. {xy. 1/Suc n \<le> f xy})"
   1.981 +    using * by auto
   1.982 +  finally obtain n where "infinite {xy. 1/Suc n \<le> f xy}"
   1.983 +    by (meson countableI_type countable_UN uncountable_infinite)
   1.984 +  then obtain C where C: "C \<subseteq> {xy. 1/Suc n \<le> f xy}" and "countable C" "infinite C"
   1.985 +    by (metis infinite_countable_subset')
   1.986 +
   1.987 +  have "\<infinity> = (\<integral>\<^sup>+ xy. ennreal (1 / Suc n) * indicator C xy \<partial>count_space UNIV)"
   1.988 +    using \<open>infinite C\<close> by(simp add: nn_integral_cmult ennreal_mult_top)
   1.989 +  also have "\<dots> \<le> ?rhs" using C
   1.990 +    by(intro nn_integral_mono)(auto split: split_indicator)
   1.991 +  finally have "?rhs = \<infinity>" by (simp add: top_unique)
   1.992 +  moreover have "?lhs = \<infinity>"
   1.993 +  proof(cases "finite (fst ` C)")
   1.994 +    case True
   1.995 +    then obtain x C' where x: "x \<in> fst ` C"
   1.996 +      and C': "C' = fst -` {x} \<inter> C"
   1.997 +      and "infinite C'"
   1.998 +      using \<open>infinite C\<close> by(auto elim!: inf_img_fin_domE')
   1.999 +    from x C C' have **: "C' \<subseteq> {xy. 1 / Suc n \<le> f xy}" by auto
  1.1000 +
  1.1001 +    from C' \<open>infinite C'\<close> have "infinite (snd ` C')"
  1.1002 +      by(auto dest!: finite_imageD simp add: inj_on_def)
  1.1003 +    then have "\<infinity> = (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator (snd ` C') y \<partial>count_space UNIV)"
  1.1004 +      by(simp add: nn_integral_cmult ennreal_mult_top)
  1.1005 +    also have "\<dots> = (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV)"
  1.1006 +      by(rule nn_integral_cong)(force split: split_indicator intro: rev_image_eqI simp add: C')
  1.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)"
  1.1008 +      by(simp add: one_ereal_def[symmetric])
  1.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)"
  1.1010 +      by(rule nn_integral_mono)(simp split: split_indicator)
  1.1011 +    also have "\<dots> \<le> ?lhs" using **
  1.1012 +      by(intro nn_integral_mono)(auto split: split_indicator)
  1.1013 +    finally show ?thesis by (simp add: top_unique)
  1.1014 +  next
  1.1015 +    case False
  1.1016 +    define C' where "C' = fst ` C"
  1.1017 +    have "\<infinity> = \<integral>\<^sup>+ x. ennreal (1 / Suc n) * indicator C' x \<partial>count_space UNIV"
  1.1018 +      using C'_def False by(simp add: nn_integral_cmult ennreal_mult_top)
  1.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"
  1.1020 +      by(auto simp add: one_ereal_def[symmetric] max_def intro: nn_integral_cong)
  1.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"
  1.1022 +      by(intro nn_integral_mono)(auto simp add: C'_def split: split_indicator intro: someI)
  1.1023 +    also have "\<dots> \<le> ?lhs" using C
  1.1024 +      by(intro nn_integral_mono)(auto split: split_indicator)
  1.1025 +    finally show ?thesis by (simp add: top_unique)
  1.1026 +  qed
  1.1027 +  ultimately show ?thesis by simp
  1.1028 +qed
  1.1029 +
  1.1030 +lemma nn_integral_snd_count_space:
  1.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"
  1.1032 +  (is "?lhs = ?rhs")
  1.1033 +proof -
  1.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)"
  1.1035 +    by(simp)
  1.1036 +  also have "\<dots> = \<integral>\<^sup>+ yx. (\<lambda>(y, x). f (x, y)) yx \<partial>count_space UNIV"
  1.1037 +    by(rule nn_integral_fst_count_space)
  1.1038 +  also have "\<dots> = \<integral>\<^sup>+ xy. f xy \<partial>count_space ((\<lambda>(x, y). (y, x)) ` UNIV)"
  1.1039 +    by(subst nn_integral_bij_count_space[OF inj_on_imp_bij_betw, symmetric])
  1.1040 +      (simp_all add: inj_on_def split_def)
  1.1041 +  also have "\<dots> = ?rhs" by(rule nn_integral_count_space_eq) auto
  1.1042 +  finally show ?thesis .
  1.1043 +qed
  1.1044 +
  1.1045 +lemma measurable_pair_measure_countable1:
  1.1046 +  assumes "countable A"
  1.1047 +  and [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N K"
  1.1048 +  shows "f \<in> measurable (count_space A \<Otimes>\<^sub>M N) K"
  1.1049 +using _ _ assms(1)
  1.1050 +by(rule measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A, simplified])simp_all
  1.1051 +
  1.1052 +subsection \<open>Product of Borel spaces\<close>
  1.1053 +
  1.1054 +lemma borel_Times:
  1.1055 +  fixes A :: "'a::topological_space set" and B :: "'b::topological_space set"
  1.1056 +  assumes A: "A \<in> sets borel" and B: "B \<in> sets borel"
  1.1057 +  shows "A \<times> B \<in> sets borel"
  1.1058 +proof -
  1.1059 +  have "A \<times> B = (A\<times>UNIV) \<inter> (UNIV \<times> B)"
  1.1060 +    by auto
  1.1061 +  moreover
  1.1062 +  { have "A \<in> sigma_sets UNIV {S. open S}" using A by (simp add: sets_borel)
  1.1063 +    then have "A\<times>UNIV \<in> sets borel"
  1.1064 +    proof (induct A)
  1.1065 +      case (Basic S) then show ?case
  1.1066 +        by (auto intro!: borel_open open_Times)
  1.1067 +    next
  1.1068 +      case (Compl A)
  1.1069 +      moreover have *: "(UNIV - A) \<times> UNIV = UNIV - (A \<times> UNIV)"
  1.1070 +        by auto
  1.1071 +      ultimately show ?case
  1.1072 +        unfolding * by auto
  1.1073 +    next
  1.1074 +      case (Union A)
  1.1075 +      moreover have *: "(UNION UNIV A) \<times> UNIV = UNION UNIV (\<lambda>i. A i \<times> UNIV)"
  1.1076 +        by auto
  1.1077 +      ultimately show ?case
  1.1078 +        unfolding * by auto
  1.1079 +    qed simp }
  1.1080 +  moreover
  1.1081 +  { have "B \<in> sigma_sets UNIV {S. open S}" using B by (simp add: sets_borel)
  1.1082 +    then have "UNIV\<times>B \<in> sets borel"
  1.1083 +    proof (induct B)
  1.1084 +      case (Basic S) then show ?case
  1.1085 +        by (auto intro!: borel_open open_Times)
  1.1086 +    next
  1.1087 +      case (Compl B)
  1.1088 +      moreover have *: "UNIV \<times> (UNIV - B) = UNIV - (UNIV \<times> B)"
  1.1089 +        by auto
  1.1090 +      ultimately show ?case
  1.1091 +        unfolding * by auto
  1.1092 +    next
  1.1093 +      case (Union B)
  1.1094 +      moreover have *: "UNIV \<times> (UNION UNIV B) = UNION UNIV (\<lambda>i. UNIV \<times> B i)"
  1.1095 +        by auto
  1.1096 +      ultimately show ?case
  1.1097 +        unfolding * by auto
  1.1098 +    qed simp }
  1.1099 +  ultimately show ?thesis
  1.1100 +    by auto
  1.1101 +qed
  1.1102 +
  1.1103 +lemma finite_measure_pair_measure:
  1.1104 +  assumes "finite_measure M" "finite_measure N"
  1.1105 +  shows "finite_measure (N  \<Otimes>\<^sub>M M)"
  1.1106 +proof (rule finite_measureI)
  1.1107 +  interpret M: finite_measure M by fact
  1.1108 +  interpret N: finite_measure N by fact
  1.1109 +  show "emeasure (N  \<Otimes>\<^sub>M M) (space (N  \<Otimes>\<^sub>M M)) \<noteq> \<infinity>"
  1.1110 +    by (auto simp: space_pair_measure M.emeasure_pair_measure_Times ennreal_mult_eq_top_iff)
  1.1111 +qed
  1.1112 +
  1.1113 +end