src/HOL/Probability/Binary_Product_Measure.thy
author haftmann
Fri Jun 19 07:53:35 2015 +0200 (2015-06-19)
changeset 60517 f16e4fb20652
parent 60066 14efa7f4ee7b
child 60727 53697011b03a
permissions -rw-r--r--
separate class for notions specific for integral (semi)domains, in contrast to fields where these are trivial
     1 (*  Title:      HOL/Probability/Binary_Product_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 section {*Binary product measures*}
     6 
     7 theory Binary_Product_Measure
     8 imports Nonnegative_Lebesgue_Integration
     9 begin
    10 
    11 lemma Pair_vimage_times[simp]: "Pair x -` (A \<times> B) = (if x \<in> A then B else {})"
    12   by auto
    13 
    14 lemma rev_Pair_vimage_times[simp]: "(\<lambda>x. (x, y)) -` (A \<times> B) = (if y \<in> B then A else {})"
    15   by auto
    16 
    17 subsection "Binary products"
    18 
    19 definition pair_measure (infixr "\<Otimes>\<^sub>M" 80) where
    20   "A \<Otimes>\<^sub>M B = measure_of (space A \<times> space B)
    21       {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}
    22       (\<lambda>X. \<integral>\<^sup>+x. (\<integral>\<^sup>+y. indicator X (x,y) \<partial>B) \<partial>A)"
    23 
    24 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)"
    25   using sets.space_closed[of A] sets.space_closed[of B] by auto
    26 
    27 lemma space_pair_measure:
    28   "space (A \<Otimes>\<^sub>M B) = space A \<times> space B"
    29   unfolding pair_measure_def using pair_measure_closed[of A B]
    30   by (rule space_measure_of)
    31 
    32 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)}"
    33   by (auto simp: space_pair_measure)
    34 
    35 lemma sets_pair_measure:
    36   "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}"
    37   unfolding pair_measure_def using pair_measure_closed[of A B]
    38   by (rule sets_measure_of)
    39 
    40 lemma sets_pair_in_sets:
    41   assumes N: "space A \<times> space B = space N"
    42   assumes "\<And>a b. a \<in> sets A \<Longrightarrow> b \<in> sets B \<Longrightarrow> a \<times> b \<in> sets N"
    43   shows "sets (A \<Otimes>\<^sub>M B) \<subseteq> sets N"
    44   using assms by (auto intro!: sets.sigma_sets_subset simp: sets_pair_measure N)
    45 
    46 lemma sets_pair_measure_cong[measurable_cong, cong]:
    47   "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^sub>M M2) = sets (M1' \<Otimes>\<^sub>M M2')"
    48   unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
    49 
    50 lemma pair_measureI[intro, simp, measurable]:
    51   "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^sub>M B)"
    52   by (auto simp: sets_pair_measure)
    53 
    54 lemma sets_Pair: "{x} \<in> sets M1 \<Longrightarrow> {y} \<in> sets M2 \<Longrightarrow> {(x, y)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
    55   using pair_measureI[of "{x}" M1 "{y}" M2] by simp
    56 
    57 lemma measurable_pair_measureI:
    58   assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"
    59   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"
    60   shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    61   unfolding pair_measure_def using 1 2
    62   by (intro measurable_measure_of) (auto dest: sets.sets_into_space)
    63 
    64 lemma measurable_split_replace[measurable (raw)]:
    65   "(\<lambda>x. f x (fst (g x)) (snd (g x))) \<in> measurable M N \<Longrightarrow> (\<lambda>x. split (f x) (g x)) \<in> measurable M N"
    66   unfolding split_beta' .
    67 
    68 lemma measurable_Pair[measurable (raw)]:
    69   assumes f: "f \<in> measurable M M1" and g: "g \<in> measurable M M2"
    70   shows "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    71 proof (rule measurable_pair_measureI)
    72   show "(\<lambda>x. (f x, g x)) \<in> space M \<rightarrow> space M1 \<times> space M2"
    73     using f g by (auto simp: measurable_def)
    74   fix A B assume *: "A \<in> sets M1" "B \<in> sets M2"
    75   have "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    76     by auto
    77   also have "\<dots> \<in> sets M"
    78     by (rule sets.Int) (auto intro!: measurable_sets * f g)
    79   finally show "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M \<in> sets M" .
    80 qed
    81 
    82 lemma measurable_fst[intro!, simp, measurable]: "fst \<in> measurable (M1 \<Otimes>\<^sub>M M2) M1"
    83   by (auto simp: fst_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    84     measurable_def)
    85 
    86 lemma measurable_snd[intro!, simp, measurable]: "snd \<in> measurable (M1 \<Otimes>\<^sub>M M2) M2"
    87   by (auto simp: snd_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    88     measurable_def)
    89 
    90 lemma measurable_Pair_compose_split[measurable_dest]:
    91   assumes f: "split f \<in> measurable (M1 \<Otimes>\<^sub>M M2) N"
    92   assumes g: "g \<in> measurable M M1" and h: "h \<in> measurable M M2"
    93   shows "(\<lambda>x. f (g x) (h x)) \<in> measurable M N"
    94   using measurable_compose[OF measurable_Pair f, OF g h] by simp
    95 
    96 lemma measurable_Pair1_compose[measurable_dest]:
    97   assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    98   assumes [measurable]: "h \<in> measurable N M"
    99   shows "(\<lambda>x. f (h x)) \<in> measurable N M1"
   100   using measurable_compose[OF f measurable_fst] by simp
   101 
   102 lemma measurable_Pair2_compose[measurable_dest]:
   103   assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
   104   assumes [measurable]: "h \<in> measurable N M"
   105   shows "(\<lambda>x. g (h x)) \<in> measurable N M2"
   106   using measurable_compose[OF f measurable_snd] by simp
   107 
   108 lemma measurable_pair:
   109   assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
   110   shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
   111   using measurable_Pair[OF assms] by simp
   112 
   113 lemma 
   114   assumes f[measurable]: "f \<in> measurable M (N \<Otimes>\<^sub>M P)" 
   115   shows measurable_fst': "(\<lambda>x. fst (f x)) \<in> measurable M N"
   116     and measurable_snd': "(\<lambda>x. snd (f x)) \<in> measurable M P"
   117   by simp_all
   118 
   119 lemma
   120   assumes f[measurable]: "f \<in> measurable M N"
   121   shows measurable_fst'': "(\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^sub>M P) N"
   122     and measurable_snd'': "(\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^sub>M M) N"
   123   by simp_all
   124 
   125 lemma sets_pair_eq_sets_fst_snd:
   126   "sets (A \<Otimes>\<^sub>M B) = sets (Sup_sigma {vimage_algebra (space A \<times> space B) fst A, vimage_algebra (space A \<times> space B) snd B})"
   127     (is "?P = sets (Sup_sigma {?fst, ?snd})")
   128 proof -
   129   { fix a b assume ab: "a \<in> sets A" "b \<in> sets B"
   130     then have "a \<times> b = (fst -` a \<inter> (space A \<times> space B)) \<inter> (snd -` b \<inter> (space A \<times> space B))"
   131       by (auto dest: sets.sets_into_space)
   132     also have "\<dots> \<in> sets (Sup_sigma {?fst, ?snd})"
   133       using ab by (auto intro: in_Sup_sigma in_vimage_algebra)
   134     finally have "a \<times> b \<in> sets (Sup_sigma {?fst, ?snd})" . }
   135   moreover have "sets ?fst \<subseteq> sets (A \<Otimes>\<^sub>M B)"
   136     by (rule sets_image_in_sets) (auto simp: space_pair_measure[symmetric])
   137   moreover have "sets ?snd \<subseteq> sets (A \<Otimes>\<^sub>M B)"  
   138     by (rule sets_image_in_sets) (auto simp: space_pair_measure)
   139   ultimately show ?thesis
   140     by (intro antisym[of "sets A" for A] sets_Sup_in_sets sets_pair_in_sets )
   141        (auto simp add: space_Sup_sigma space_pair_measure)
   142 qed
   143 
   144 lemma measurable_pair_iff:
   145   "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"
   146   by (auto intro: measurable_pair[of f M M1 M2]) 
   147 
   148 lemma measurable_split_conv:
   149   "(\<lambda>(x, y). f x y) \<in> measurable A B \<longleftrightarrow> (\<lambda>x. f (fst x) (snd x)) \<in> measurable A B"
   150   by (intro arg_cong2[where f="op \<in>"]) auto
   151 
   152 lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (M2 \<Otimes>\<^sub>M M1)"
   153   by (auto intro!: measurable_Pair simp: measurable_split_conv)
   154 
   155 lemma measurable_pair_swap:
   156   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"
   157   using measurable_comp[OF measurable_Pair f] by (auto simp: measurable_split_conv comp_def)
   158 
   159 lemma measurable_pair_swap_iff:
   160   "f \<in> measurable (M2 \<Otimes>\<^sub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) M"
   161   by (auto dest: measurable_pair_swap)
   162 
   163 lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^sub>M M2)"
   164   by simp
   165 
   166 lemma sets_Pair1[measurable (raw)]:
   167   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "Pair x -` A \<in> sets M2"
   168 proof -
   169   have "Pair x -` A = (if x \<in> space M1 then Pair x -` A \<inter> space M2 else {})"
   170     using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   171   also have "\<dots> \<in> sets M2"
   172     using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: split_if_asm)
   173   finally show ?thesis .
   174 qed
   175 
   176 lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^sub>M M2)"
   177   by (auto intro!: measurable_Pair)
   178 
   179 lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
   180 proof -
   181   have "(\<lambda>x. (x, y)) -` A = (if y \<in> space M2 then (\<lambda>x. (x, y)) -` A \<inter> space M1 else {})"
   182     using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   183   also have "\<dots> \<in> sets M1"
   184     using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: split_if_asm)
   185   finally show ?thesis .
   186 qed
   187 
   188 lemma measurable_Pair2:
   189   assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and x: "x \<in> space M1"
   190   shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   191   using measurable_comp[OF measurable_Pair1' f, OF x]
   192   by (simp add: comp_def)
   193   
   194 lemma measurable_Pair1:
   195   assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and y: "y \<in> space M2"
   196   shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
   197   using measurable_comp[OF measurable_Pair2' f, OF y]
   198   by (simp add: comp_def)
   199 
   200 lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
   201   unfolding Int_stable_def
   202   by safe (auto simp add: times_Int_times)
   203 
   204 lemma disjoint_family_vimageI: "disjoint_family F \<Longrightarrow> disjoint_family (\<lambda>i. f -` F i)"
   205   by (auto simp: disjoint_family_on_def)
   206 
   207 lemma (in finite_measure) finite_measure_cut_measurable:
   208   assumes [measurable]: "Q \<in> sets (N \<Otimes>\<^sub>M M)"
   209   shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N"
   210     (is "?s Q \<in> _")
   211   using Int_stable_pair_measure_generator pair_measure_closed assms
   212   unfolding sets_pair_measure
   213 proof (induct rule: sigma_sets_induct_disjoint)
   214   case (compl A)
   215   with sets.sets_into_space have "\<And>x. emeasure M (Pair x -` ((space N \<times> space M) - A)) =
   216       (if x \<in> space N then emeasure M (space M) - ?s A x else 0)"
   217     unfolding sets_pair_measure[symmetric]
   218     by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   219   with compl sets.top show ?case
   220     by (auto intro!: measurable_If simp: space_pair_measure)
   221 next
   222   case (union F)
   223   then have "\<And>x. emeasure M (Pair x -` (\<Union>i. F i)) = (\<Sum>i. ?s (F i) x)"
   224     by (simp add: suminf_emeasure disjoint_family_vimageI subset_eq vimage_UN sets_pair_measure[symmetric])
   225   with union show ?case
   226     unfolding sets_pair_measure[symmetric] by simp
   227 qed (auto simp add: if_distrib Int_def[symmetric] intro!: measurable_If)
   228 
   229 lemma (in sigma_finite_measure) measurable_emeasure_Pair:
   230   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> _")
   231 proof -
   232   from sigma_finite_disjoint guess F . note F = this
   233   then have F_sets: "\<And>i. F i \<in> sets M" by auto
   234   let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   235   { fix i
   236     have [simp]: "space N \<times> F i \<inter> space N \<times> space M = space N \<times> F i"
   237       using F sets.sets_into_space by auto
   238     let ?R = "density M (indicator (F i))"
   239     have "finite_measure ?R"
   240       using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   241     then have "(\<lambda>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))) \<in> borel_measurable N"
   242      by (rule finite_measure.finite_measure_cut_measurable) (auto intro: Q)
   243     moreover have "\<And>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))
   244         = emeasure M (F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q))"
   245       using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   246     moreover have "\<And>x. F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q) = ?C x i"
   247       using sets.sets_into_space[OF Q] by (auto simp: space_pair_measure)
   248     ultimately have "(\<lambda>x. emeasure M (?C x i)) \<in> borel_measurable N"
   249       by simp }
   250   moreover
   251   { fix x
   252     have "(\<Sum>i. emeasure M (?C x i)) = emeasure M (\<Union>i. ?C x i)"
   253     proof (intro suminf_emeasure)
   254       show "range (?C x) \<subseteq> sets M"
   255         using F `Q \<in> sets (N \<Otimes>\<^sub>M M)` by (auto intro!: sets_Pair1)
   256       have "disjoint_family F" using F by auto
   257       show "disjoint_family (?C x)"
   258         by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
   259     qed
   260     also have "(\<Union>i. ?C x i) = Pair x -` Q"
   261       using F sets.sets_into_space[OF `Q \<in> sets (N \<Otimes>\<^sub>M M)`]
   262       by (auto simp: space_pair_measure)
   263     finally have "emeasure M (Pair x -` Q) = (\<Sum>i. emeasure M (?C x i))"
   264       by simp }
   265   ultimately show ?thesis using `Q \<in> sets (N \<Otimes>\<^sub>M M)` F_sets
   266     by auto
   267 qed
   268 
   269 lemma (in sigma_finite_measure) measurable_emeasure[measurable (raw)]:
   270   assumes space: "\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M"
   271   assumes A: "{x\<in>space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M)"
   272   shows "(\<lambda>x. emeasure M (A x)) \<in> borel_measurable N"
   273 proof -
   274   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"
   275     by (auto simp: space_pair_measure)
   276   with measurable_emeasure_Pair[OF A] show ?thesis
   277     by (auto cong: measurable_cong)
   278 qed
   279 
   280 lemma (in sigma_finite_measure) emeasure_pair_measure:
   281   assumes "X \<in> sets (N \<Otimes>\<^sub>M M)"
   282   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")
   283 proof (rule emeasure_measure_of[OF pair_measure_def])
   284   show "positive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   285     by (auto simp: positive_def nn_integral_nonneg)
   286   have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   287     by (auto simp: indicator_def)
   288   show "countably_additive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   289   proof (rule countably_additiveI)
   290     fix F :: "nat \<Rightarrow> ('b \<times> 'a) set" assume F: "range F \<subseteq> sets (N \<Otimes>\<^sub>M M)" "disjoint_family F"
   291     from F have *: "\<And>i. F i \<in> sets (N \<Otimes>\<^sub>M M)" by auto
   292     moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   293       by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   294     moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M"
   295       using F by (auto simp: sets_Pair1)
   296     ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   297       by (auto simp add: nn_integral_suminf[symmetric] vimage_UN suminf_emeasure emeasure_nonneg
   298                intro!: nn_integral_cong nn_integral_indicator[symmetric])
   299   qed
   300   show "{a \<times> b |a b. a \<in> sets N \<and> b \<in> sets M} \<subseteq> Pow (space N \<times> space M)"
   301     using sets.space_closed[of N] sets.space_closed[of M] by auto
   302 qed fact
   303 
   304 lemma (in sigma_finite_measure) emeasure_pair_measure_alt:
   305   assumes X: "X \<in> sets (N \<Otimes>\<^sub>M M)"
   306   shows "emeasure (N  \<Otimes>\<^sub>M M) X = (\<integral>\<^sup>+x. emeasure M (Pair x -` X) \<partial>N)"
   307 proof -
   308   have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   309     by (auto simp: indicator_def)
   310   show ?thesis
   311     using X by (auto intro!: nn_integral_cong simp: emeasure_pair_measure sets_Pair1)
   312 qed
   313 
   314 lemma (in sigma_finite_measure) emeasure_pair_measure_Times:
   315   assumes A: "A \<in> sets N" and B: "B \<in> sets M"
   316   shows "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = emeasure N A * emeasure M B"
   317 proof -
   318   have "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = (\<integral>\<^sup>+x. emeasure M B * indicator A x \<partial>N)"
   319     using A B by (auto intro!: nn_integral_cong simp: emeasure_pair_measure_alt)
   320   also have "\<dots> = emeasure M B * emeasure N A"
   321     using A by (simp add: emeasure_nonneg nn_integral_cmult_indicator)
   322   finally show ?thesis
   323     by (simp add: ac_simps)
   324 qed
   325 
   326 subsection {* Binary products of $\sigma$-finite emeasure spaces *}
   327 
   328 locale pair_sigma_finite = M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
   329   for M1 :: "'a measure" and M2 :: "'b measure"
   330 
   331 lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
   332   "Q \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow> (\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   333   using M2.measurable_emeasure_Pair .
   334 
   335 lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
   336   assumes Q: "Q \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   337 proof -
   338   have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   339     using Q measurable_pair_swap' by (auto intro: measurable_sets)
   340   note M1.measurable_emeasure_Pair[OF this]
   341   moreover have "\<And>y. Pair y -` ((\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1)) = (\<lambda>x. (x, y)) -` Q"
   342     using Q[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   343   ultimately show ?thesis by simp
   344 qed
   345 
   346 lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
   347   defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
   348   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>
   349     (\<forall>i. emeasure (M1 \<Otimes>\<^sub>M M2) (F i) \<noteq> \<infinity>)"
   350 proof -
   351   from M1.sigma_finite_incseq guess F1 . note F1 = this
   352   from M2.sigma_finite_incseq guess F2 . note F2 = this
   353   from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   354   let ?F = "\<lambda>i. F1 i \<times> F2 i"
   355   show ?thesis
   356   proof (intro exI[of _ ?F] conjI allI)
   357     show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)
   358   next
   359     have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   360     proof (intro subsetI)
   361       fix x assume "x \<in> space M1 \<times> space M2"
   362       then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"
   363         by (auto simp: space)
   364       then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"
   365         using `incseq F1` `incseq F2` unfolding incseq_def
   366         by (force split: split_max)+
   367       then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"
   368         by (intro SigmaI) (auto simp add: max.commute)
   369       then show "x \<in> (\<Union>i. ?F i)" by auto
   370     qed
   371     then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   372       using space by (auto simp: space)
   373   next
   374     fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"
   375       using `incseq F1` `incseq F2` unfolding incseq_Suc_iff by auto
   376   next
   377     fix i
   378     from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   379     with F1 F2 emeasure_nonneg[of M1 "F1 i"] emeasure_nonneg[of M2 "F2 i"]
   380     show "emeasure (M1 \<Otimes>\<^sub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"
   381       by (auto simp add: emeasure_pair_measure_Times)
   382   qed
   383 qed
   384 
   385 sublocale pair_sigma_finite \<subseteq> P: sigma_finite_measure "M1 \<Otimes>\<^sub>M M2"
   386 proof
   387   from M1.sigma_finite_countable guess F1 ..
   388   moreover from M2.sigma_finite_countable guess F2 ..
   389   ultimately show
   390     "\<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>)"
   391     by (intro exI[of _ "(\<lambda>(a, b). a \<times> b) ` (F1 \<times> F2)"] conjI)
   392        (auto simp: M2.emeasure_pair_measure_Times space_pair_measure set_eq_iff subset_eq
   393              dest: sets.sets_into_space)
   394 qed
   395 
   396 lemma sigma_finite_pair_measure:
   397   assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"
   398   shows "sigma_finite_measure (A \<Otimes>\<^sub>M B)"
   399 proof -
   400   interpret A: sigma_finite_measure A by fact
   401   interpret B: sigma_finite_measure B by fact
   402   interpret AB: pair_sigma_finite A  B ..
   403   show ?thesis ..
   404 qed
   405 
   406 lemma sets_pair_swap:
   407   assumes "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   408   shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   409   using measurable_pair_swap' assms by (rule measurable_sets)
   410 
   411 lemma (in pair_sigma_finite) distr_pair_swap:
   412   "M1 \<Otimes>\<^sub>M M2 = distr (M2 \<Otimes>\<^sub>M M1) (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")
   413 proof -
   414   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   415   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   416   show ?thesis
   417   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   418     show "?E \<subseteq> Pow (space ?P)"
   419       using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   420     show "sets ?P = sigma_sets (space ?P) ?E"
   421       by (simp add: sets_pair_measure space_pair_measure)
   422     then show "sets ?D = sigma_sets (space ?P) ?E"
   423       by simp
   424   next
   425     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   426       using F by (auto simp: space_pair_measure)
   427   next
   428     fix X assume "X \<in> ?E"
   429     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
   430     have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^sub>M M1) = B \<times> A"
   431       using sets.sets_into_space[OF A] sets.sets_into_space[OF B] by (auto simp: space_pair_measure)
   432     with A B show "emeasure (M1 \<Otimes>\<^sub>M M2) X = emeasure ?D X"
   433       by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_pair_measure_Times emeasure_distr
   434                     measurable_pair_swap' ac_simps)
   435   qed
   436 qed
   437 
   438 lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:
   439   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   440   shows "emeasure (M1 \<Otimes>\<^sub>M M2) A = (\<integral>\<^sup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   441     (is "_ = ?\<nu> A")
   442 proof -
   443   have [simp]: "\<And>y. (Pair y -` ((\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1))) = (\<lambda>x. (x, y)) -` A"
   444     using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
   445   show ?thesis using A
   446     by (subst distr_pair_swap)
   447        (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
   448                  M1.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   449 qed
   450 
   451 lemma (in pair_sigma_finite) AE_pair:
   452   assumes "AE x in (M1 \<Otimes>\<^sub>M M2). Q x"
   453   shows "AE x in M1. (AE y in M2. Q (x, y))"
   454 proof -
   455   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"
   456     using assms unfolding eventually_ae_filter by auto
   457   show ?thesis
   458   proof (rule AE_I)
   459     from N measurable_emeasure_Pair1[OF `N \<in> sets (M1 \<Otimes>\<^sub>M M2)`]
   460     show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
   461       by (auto simp: M2.emeasure_pair_measure_alt nn_integral_0_iff emeasure_nonneg)
   462     show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   463       by (intro borel_measurable_ereal_neq_const measurable_emeasure_Pair1 N)
   464     { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
   465       have "AE y in M2. Q (x, y)"
   466       proof (rule AE_I)
   467         show "emeasure M2 (Pair x -` N) = 0" by fact
   468         show "Pair x -` N \<in> sets M2" using N(1) by (rule sets_Pair1)
   469         show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
   470           using N `x \<in> space M1` unfolding space_pair_measure by auto
   471       qed }
   472     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}"
   473       by auto
   474   qed
   475 qed
   476 
   477 lemma (in pair_sigma_finite) AE_pair_measure:
   478   assumes "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   479   assumes ae: "AE x in M1. AE y in M2. P (x, y)"
   480   shows "AE x in M1 \<Otimes>\<^sub>M M2. P x"
   481 proof (subst AE_iff_measurable[OF _ refl])
   482   show "{x\<in>space (M1 \<Otimes>\<^sub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   483     by (rule sets.sets_Collect) fact
   484   then have "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} =
   485       (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"
   486     by (simp add: M2.emeasure_pair_measure)
   487   also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. 0 \<partial>M2 \<partial>M1)"
   488     using ae
   489     apply (safe intro!: nn_integral_cong_AE)
   490     apply (intro AE_I2)
   491     apply (safe intro!: nn_integral_cong_AE)
   492     apply auto
   493     done
   494   finally show "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} = 0" by simp
   495 qed
   496 
   497 lemma (in pair_sigma_finite) AE_pair_iff:
   498   "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow>
   499     (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))"
   500   using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto
   501 
   502 lemma (in pair_sigma_finite) AE_commute:
   503   assumes P: "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   504   shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"
   505 proof -
   506   interpret Q: pair_sigma_finite M2 M1 ..
   507   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"
   508     by auto
   509   have "{x \<in> space (M2 \<Otimes>\<^sub>M M1). P (snd x) (fst x)} =
   510     (\<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)"
   511     by (auto simp: space_pair_measure)
   512   also have "\<dots> \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   513     by (intro sets_pair_swap P)
   514   finally show ?thesis
   515     apply (subst AE_pair_iff[OF P])
   516     apply (subst distr_pair_swap)
   517     apply (subst AE_distr_iff[OF measurable_pair_swap' P])
   518     apply (subst Q.AE_pair_iff)
   519     apply simp_all
   520     done
   521 qed
   522 
   523 subsection "Fubinis theorem"
   524 
   525 lemma measurable_compose_Pair1:
   526   "x \<in> space M1 \<Longrightarrow> g \<in> measurable (M1 \<Otimes>\<^sub>M M2) L \<Longrightarrow> (\<lambda>y. g (x, y)) \<in> measurable M2 L"
   527   by simp
   528 
   529 lemma (in sigma_finite_measure) borel_measurable_nn_integral_fst':
   530   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)" "\<And>x. 0 \<le> f x"
   531   shows "(\<lambda>x. \<integral>\<^sup>+ y. f (x, y) \<partial>M) \<in> borel_measurable M1"
   532 using f proof induct
   533   case (cong u v)
   534   then have "\<And>w x. w \<in> space M1 \<Longrightarrow> x \<in> space M \<Longrightarrow> u (w, x) = v (w, x)"
   535     by (auto simp: space_pair_measure)
   536   show ?case
   537     apply (subst measurable_cong)
   538     apply (rule nn_integral_cong)
   539     apply fact+
   540     done
   541 next
   542   case (set Q)
   543   have [simp]: "\<And>x y. indicator Q (x, y) = indicator (Pair x -` Q) y"
   544     by (auto simp: indicator_def)
   545   have "\<And>x. x \<in> space M1 \<Longrightarrow> emeasure M (Pair x -` Q) = \<integral>\<^sup>+ y. indicator Q (x, y) \<partial>M"
   546     by (simp add: sets_Pair1[OF set])
   547   from this measurable_emeasure_Pair[OF set] show ?case
   548     by (rule measurable_cong[THEN iffD1])
   549 qed (simp_all add: nn_integral_add nn_integral_cmult measurable_compose_Pair1
   550                    nn_integral_monotone_convergence_SUP incseq_def le_fun_def
   551               cong: measurable_cong)
   552 
   553 lemma (in sigma_finite_measure) nn_integral_fst':
   554   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)" "\<And>x. 0 \<le> f x"
   555   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 = _")
   556 using f proof induct
   557   case (cong u v)
   558   then have "?I u = ?I v"
   559     by (intro nn_integral_cong) (auto simp: space_pair_measure)
   560   with cong show ?case
   561     by (simp cong: nn_integral_cong)
   562 qed (simp_all add: emeasure_pair_measure nn_integral_cmult nn_integral_add
   563                    nn_integral_monotone_convergence_SUP
   564                    measurable_compose_Pair1 nn_integral_nonneg
   565                    borel_measurable_nn_integral_fst' nn_integral_mono incseq_def le_fun_def
   566               cong: nn_integral_cong)
   567 
   568 lemma (in sigma_finite_measure) nn_integral_fst:
   569   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
   570   shows "(\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x, y) \<partial>M) \<partial>M1) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M) f"
   571   using f
   572     borel_measurable_nn_integral_fst'[of "\<lambda>x. max 0 (f x)"]
   573     nn_integral_fst'[of "\<lambda>x. max 0 (f x)"]
   574   unfolding nn_integral_max_0 by auto
   575 
   576 lemma (in sigma_finite_measure) borel_measurable_nn_integral[measurable (raw)]:
   577   "split f \<in> borel_measurable (N \<Otimes>\<^sub>M M) \<Longrightarrow> (\<lambda>x. \<integral>\<^sup>+ y. f x y \<partial>M) \<in> borel_measurable N"
   578   using borel_measurable_nn_integral_fst'[of "\<lambda>x. max 0 (split f x)" N]
   579   by (simp add: nn_integral_max_0)
   580 
   581 lemma (in pair_sigma_finite) nn_integral_snd:
   582   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   583   shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   584 proof -
   585   note measurable_pair_swap[OF f]
   586   from M1.nn_integral_fst[OF this]
   587   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))"
   588     by simp
   589   also have "(\<integral>\<^sup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   590     by (subst distr_pair_swap)
   591        (auto simp: nn_integral_distr[OF measurable_pair_swap' f] intro!: nn_integral_cong)
   592   finally show ?thesis .
   593 qed
   594 
   595 lemma (in pair_sigma_finite) Fubini:
   596   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   597   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)"
   598   unfolding nn_integral_snd[OF assms] M2.nn_integral_fst[OF assms] ..
   599 
   600 lemma (in pair_sigma_finite) Fubini':
   601   assumes f: "split f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   602   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)"
   603   using Fubini[OF f] by simp
   604 
   605 subsection {* Products on counting spaces, densities and distributions *}
   606 
   607 lemma sigma_prod:
   608   assumes X_cover: "\<exists>E\<subseteq>A. countable E \<and> X = \<Union>E" and A: "A \<subseteq> Pow X"
   609   assumes Y_cover: "\<exists>E\<subseteq>B. countable E \<and> Y = \<Union>E" and B: "B \<subseteq> Pow Y"
   610   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}"
   611     (is "?P = ?S")
   612 proof (rule measure_eqI)
   613   have [simp]: "snd \<in> X \<times> Y \<rightarrow> Y" "fst \<in> X \<times> Y \<rightarrow> X"
   614     by auto
   615   let ?XY = "{{fst -` a \<inter> X \<times> Y | a. a \<in> A}, {snd -` b \<inter> X \<times> Y | b. b \<in> B}}"
   616   have "sets ?P = 
   617     sets (\<Squnion>\<^sub>\<sigma> xy\<in>?XY. sigma (X \<times> Y) xy)"
   618     by (simp add: vimage_algebra_sigma sets_pair_eq_sets_fst_snd A B)
   619   also have "\<dots> = sets (sigma (X \<times> Y) (\<Union>?XY))"
   620     by (intro Sup_sigma_sigma arg_cong[where f=sets]) auto
   621   also have "\<dots> = sets ?S"
   622   proof (intro arg_cong[where f=sets] sigma_eqI sigma_sets_eqI) 
   623     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)"
   624       using A B by auto
   625   next
   626     interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   627       using A B by (intro sigma_algebra_sigma_sets) auto
   628     fix Z assume "Z \<in> \<Union>?XY"
   629     then show "Z \<in> sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   630     proof safe
   631       fix a assume "a \<in> A"
   632       from Y_cover obtain E where E: "E \<subseteq> B" "countable E" and "Y = \<Union>E"
   633         by auto
   634       with `a \<in> A` A have eq: "fst -` a \<inter> X \<times> Y = (\<Union>e\<in>E. a \<times> e)"
   635         by auto
   636       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}"
   637         using `a \<in> A` E unfolding eq by (auto intro!: XY.countable_UN')
   638     next
   639       fix b assume "b \<in> B"
   640       from X_cover obtain E where E: "E \<subseteq> A" "countable E" and "X = \<Union>E"
   641         by auto
   642       with `b \<in> B` B have eq: "snd -` b \<inter> X \<times> Y = (\<Union>e\<in>E. e \<times> b)"
   643         by auto
   644       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}"
   645         using `b \<in> B` E unfolding eq by (auto intro!: XY.countable_UN')
   646     qed
   647   next
   648     fix Z assume "Z \<in> {a \<times> b |a b. a \<in> A \<and> b \<in> B}"
   649     then obtain a b where "Z = a \<times> b" and ab: "a \<in> A" "b \<in> B"
   650       by auto
   651     then have Z: "Z = (fst -` a \<inter> X \<times> Y) \<inter> (snd -` b \<inter> X \<times> Y)"
   652       using A B by auto
   653     interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) (\<Union>?XY)"
   654       by (intro sigma_algebra_sigma_sets) auto
   655     show "Z \<in> sigma_sets (X \<times> Y) (\<Union>?XY)"
   656       unfolding Z by (rule XY.Int) (blast intro: ab)+
   657   qed
   658   finally show "sets ?P = sets ?S" .
   659 next
   660   interpret finite_measure "sigma X A" for X A
   661     proof qed (simp add: emeasure_sigma)
   662   fix A assume "A \<in> sets ?P" then show "emeasure ?P A = emeasure ?S A"
   663     by (simp add: emeasure_pair_measure_alt emeasure_sigma)
   664 qed
   665 
   666 lemma sigma_sets_pair_measure_generator_finite:
   667   assumes "finite A" and "finite B"
   668   shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"
   669   (is "sigma_sets ?prod ?sets = _")
   670 proof safe
   671   have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
   672   fix x assume subset: "x \<subseteq> A \<times> B"
   673   hence "finite x" using fin by (rule finite_subset)
   674   from this subset show "x \<in> sigma_sets ?prod ?sets"
   675   proof (induct x)
   676     case empty show ?case by (rule sigma_sets.Empty)
   677   next
   678     case (insert a x)
   679     hence "{a} \<in> sigma_sets ?prod ?sets" by auto
   680     moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
   681     ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
   682   qed
   683 next
   684   fix x a b
   685   assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"
   686   from sigma_sets_into_sp[OF _ this(1)] this(2)
   687   show "a \<in> A" and "b \<in> B" by auto
   688 qed
   689 
   690 lemma borel_prod:
   691   "(borel \<Otimes>\<^sub>M borel) = (borel :: ('a::second_countable_topology \<times> 'b::second_countable_topology) measure)"
   692   (is "?P = ?B")
   693 proof -
   694   have "?B = sigma UNIV {A \<times> B | A B. open A \<and> open B}"
   695     by (rule second_countable_borel_measurable[OF open_prod_generated])
   696   also have "\<dots> = ?P"
   697     unfolding borel_def
   698     by (subst sigma_prod) (auto intro!: exI[of _ "{UNIV}"])
   699   finally show ?thesis ..
   700 qed
   701 
   702 lemma pair_measure_count_space:
   703   assumes A: "finite A" and B: "finite B"
   704   shows "count_space A \<Otimes>\<^sub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")
   705 proof (rule measure_eqI)
   706   interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact
   707   interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact
   708   interpret P: pair_sigma_finite "count_space A" "count_space B" by default
   709   show eq: "sets ?P = sets ?C"
   710     by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)
   711   fix X assume X: "X \<in> sets ?P"
   712   with eq have X_subset: "X \<subseteq> A \<times> B" by simp
   713   with A B have fin_Pair: "\<And>x. finite (Pair x -` X)"
   714     by (intro finite_subset[OF _ B]) auto
   715   have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
   716   show "emeasure ?P X = emeasure ?C X"
   717     apply (subst B.emeasure_pair_measure_alt[OF X])
   718     apply (subst emeasure_count_space)
   719     using X_subset apply auto []
   720     apply (simp add: fin_Pair emeasure_count_space X_subset fin_X)
   721     apply (subst nn_integral_count_space)
   722     using A apply simp
   723     apply (simp del: real_of_nat_setsum add: real_of_nat_setsum[symmetric])
   724     apply (subst card_gt_0_iff)
   725     apply (simp add: fin_Pair)
   726     apply (subst card_SigmaI[symmetric])
   727     using A apply simp
   728     using fin_Pair apply simp
   729     using X_subset apply (auto intro!: arg_cong[where f=card])
   730     done
   731 qed
   732 
   733 
   734 lemma emeasure_prod_count_space:
   735   assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M M)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")
   736   shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator A (x, y) \<partial>?B \<partial>?A)"
   737   by (rule emeasure_measure_of[OF pair_measure_def])
   738      (auto simp: countably_additive_def positive_def suminf_indicator nn_integral_nonneg A
   739                  nn_integral_suminf[symmetric] dest: sets.sets_into_space)
   740 
   741 lemma emeasure_prod_count_space_single[simp]: "emeasure (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) {x} = 1"
   742 proof -
   743   have [simp]: "\<And>a b x y. indicator {(a, b)} (x, y) = (indicator {a} x * indicator {b} y::ereal)"
   744     by (auto split: split_indicator)
   745   show ?thesis
   746     by (cases x)
   747        (auto simp: emeasure_prod_count_space nn_integral_cmult sets_Pair nn_integral_max_0 one_ereal_def[symmetric])
   748 qed
   749 
   750 lemma emeasure_count_space_prod_eq:
   751   fixes A :: "('a \<times> 'b) set"
   752   assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M count_space UNIV)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")
   753   shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"
   754 proof -
   755   { fix A :: "('a \<times> 'b) set" assume "countable A"
   756     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)"
   757       by (intro emeasure_UN_countable) (auto simp: sets_Pair disjoint_family_on_def)
   758     also have "\<dots> = (\<integral>\<^sup>+a. indicator A a \<partial>count_space UNIV)"
   759       by (subst nn_integral_count_space_indicator) auto
   760     finally have "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"
   761       by simp }
   762   note * = this
   763 
   764   show ?thesis
   765   proof cases
   766     assume "finite A" then show ?thesis
   767       by (intro * countable_finite)
   768   next
   769     assume "infinite A"
   770     then obtain C where "countable C" and "infinite C" and "C \<subseteq> A"
   771       by (auto dest: infinite_countable_subset')
   772     with A have "emeasure (?A \<Otimes>\<^sub>M ?B) C \<le> emeasure (?A \<Otimes>\<^sub>M ?B) A"
   773       by (intro emeasure_mono) auto
   774     also have "emeasure (?A \<Otimes>\<^sub>M ?B) C = emeasure (count_space UNIV) C"
   775       using `countable C` by (rule *)
   776     finally show ?thesis
   777       using `infinite C` `infinite A` by simp
   778   qed
   779 qed
   780 
   781 lemma nn_intergal_count_space_prod_eq':
   782   assumes [simp]: "\<And>x. 0 \<le> f x"
   783   shows "nn_integral (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) f = nn_integral (count_space UNIV) f"
   784     (is "nn_integral ?P f = _")
   785 proof cases
   786   assume cntbl: "countable {x. f x \<noteq> 0}"
   787   have [simp]: "\<And>x. ereal (real (card ({x} \<inter> {x. f x \<noteq> 0}))) = indicator {x. f x \<noteq> 0} x"
   788     by (auto split: split_indicator)
   789   have [measurable]: "\<And>y. (\<lambda>x. indicator {y} x) \<in> borel_measurable ?P"
   790     by (rule measurable_discrete_difference[of "\<lambda>x. 0" _ borel "{y}" "\<lambda>x. indicator {y} x" for y])
   791        (auto intro: sets_Pair)
   792 
   793   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)"
   794     by (auto simp add: nn_integral_cmult nn_integral_indicator' intro!: nn_integral_cong split: split_indicator)
   795   also have "\<dots> = (\<integral>\<^sup>+x. \<integral>\<^sup>+x'. f x' * indicator {x'} x \<partial>count_space {x. f x \<noteq> 0} \<partial>?P)"
   796     by (auto intro!: nn_integral_cong split: split_indicator)
   797   also have "\<dots> = (\<integral>\<^sup>+x'. \<integral>\<^sup>+x. f x' * indicator {x'} x \<partial>?P \<partial>count_space {x. f x \<noteq> 0})"
   798     by (intro nn_integral_count_space_nn_integral cntbl) auto
   799   also have "\<dots> = (\<integral>\<^sup>+x'. f x' \<partial>count_space {x. f x \<noteq> 0})"
   800     by (intro nn_integral_cong) (auto simp: nn_integral_cmult sets_Pair)
   801   finally show ?thesis
   802     by (auto simp add: nn_integral_count_space_indicator intro!: nn_integral_cong split: split_indicator)
   803 next
   804   { fix x assume "f x \<noteq> 0"
   805     with `0 \<le> f x` have "(\<exists>r. 0 < r \<and> f x = ereal r) \<or> f x = \<infinity>"
   806       by (cases "f x") (auto simp: less_le)
   807     then have "\<exists>n. ereal (1 / real (Suc n)) \<le> f x"
   808       by (auto elim!: nat_approx_posE intro!: less_imp_le) }
   809   note * = this
   810 
   811   assume cntbl: "uncountable {x. f x \<noteq> 0}"
   812   also have "{x. f x \<noteq> 0} = (\<Union>n. {x. 1/Suc n \<le> f x})"
   813     using * by auto
   814   finally obtain n where "infinite {x. 1/Suc n \<le> f x}"
   815     by (meson countableI_type countable_UN uncountable_infinite)
   816   then obtain C where C: "C \<subseteq> {x. 1/Suc n \<le> f x}" and "countable C" "infinite C"
   817     by (metis infinite_countable_subset')
   818 
   819   have [measurable]: "C \<in> sets ?P"
   820     using sets.countable[OF _ `countable C`, of ?P] by (auto simp: sets_Pair)
   821 
   822   have "(\<integral>\<^sup>+x. ereal (1/Suc n) * indicator C x \<partial>?P) \<le> nn_integral ?P f"
   823     using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])
   824   moreover have "(\<integral>\<^sup>+x. ereal (1/Suc n) * indicator C x \<partial>?P) = \<infinity>"
   825     using `infinite C` by (simp add: nn_integral_cmult emeasure_count_space_prod_eq)
   826   moreover have "(\<integral>\<^sup>+x. ereal (1/Suc n) * indicator C x \<partial>count_space UNIV) \<le> nn_integral (count_space UNIV) f"
   827     using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])
   828   moreover have "(\<integral>\<^sup>+x. ereal (1/Suc n) * indicator C x \<partial>count_space UNIV) = \<infinity>"
   829     using `infinite C` by (simp add: nn_integral_cmult)
   830   ultimately show ?thesis
   831     by simp
   832 qed
   833 
   834 lemma nn_intergal_count_space_prod_eq:
   835   "nn_integral (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) f = nn_integral (count_space UNIV) f"
   836   by (subst (1 2) nn_integral_max_0[symmetric]) (auto intro!: nn_intergal_count_space_prod_eq')
   837 
   838 lemma pair_measure_density:
   839   assumes f: "f \<in> borel_measurable M1" "AE x in M1. 0 \<le> f x"
   840   assumes g: "g \<in> borel_measurable M2" "AE x in M2. 0 \<le> g x"
   841   assumes "sigma_finite_measure M2" "sigma_finite_measure (density M2 g)"
   842   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")
   843 proof (rule measure_eqI)
   844   interpret M2: sigma_finite_measure M2 by fact
   845   interpret D2: sigma_finite_measure "density M2 g" by fact
   846 
   847   fix A assume A: "A \<in> sets ?L"
   848   with f g have "(\<integral>\<^sup>+ x. f x * \<integral>\<^sup>+ y. g y * indicator A (x, y) \<partial>M2 \<partial>M1) =
   849     (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f x * g y * indicator A (x, y) \<partial>M2 \<partial>M1)"
   850     by (intro nn_integral_cong_AE)
   851        (auto simp add: nn_integral_cmult[symmetric] ac_simps)
   852   with A f g show "emeasure ?L A = emeasure ?R A"
   853     by (simp add: D2.emeasure_pair_measure emeasure_density nn_integral_density
   854                   M2.nn_integral_fst[symmetric]
   855              cong: nn_integral_cong)
   856 qed simp
   857 
   858 lemma sigma_finite_measure_distr:
   859   assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"
   860   shows "sigma_finite_measure M"
   861 proof -
   862   interpret sigma_finite_measure "distr M N f" by fact
   863   from sigma_finite_countable guess A .. note A = this
   864   show ?thesis
   865   proof
   866     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>)"
   867       using A f
   868       by (intro exI[of _ "(\<lambda>a. f -` a \<inter> space M) ` A"])
   869          (auto simp: emeasure_distr set_eq_iff subset_eq intro: measurable_space)
   870   qed
   871 qed
   872 
   873 lemma pair_measure_distr:
   874   assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"
   875   assumes "sigma_finite_measure (distr N T g)"
   876   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")
   877 proof (rule measure_eqI)
   878   interpret T: sigma_finite_measure "distr N T g" by fact
   879   interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+
   880 
   881   fix A assume A: "A \<in> sets ?P"
   882   with f g show "emeasure ?P A = emeasure ?D A"
   883     by (auto simp add: N.emeasure_pair_measure_alt space_pair_measure emeasure_distr
   884                        T.emeasure_pair_measure_alt nn_integral_distr
   885              intro!: nn_integral_cong arg_cong[where f="emeasure N"])
   886 qed simp
   887 
   888 lemma pair_measure_eqI:
   889   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
   890   assumes sets: "sets (M1 \<Otimes>\<^sub>M M2) = sets M"
   891   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)"
   892   shows "M1 \<Otimes>\<^sub>M M2 = M"
   893 proof -
   894   interpret M1: sigma_finite_measure M1 by fact
   895   interpret M2: sigma_finite_measure M2 by fact
   896   interpret pair_sigma_finite M1 M2 by default
   897   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   898   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   899   let ?P = "M1 \<Otimes>\<^sub>M M2"
   900   show ?thesis
   901   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   902     show "?E \<subseteq> Pow (space ?P)"
   903       using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   904     show "sets ?P = sigma_sets (space ?P) ?E"
   905       by (simp add: sets_pair_measure space_pair_measure)
   906     then show "sets M = sigma_sets (space ?P) ?E"
   907       using sets[symmetric] by simp
   908   next
   909     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   910       using F by (auto simp: space_pair_measure)
   911   next
   912     fix X assume "X \<in> ?E"
   913     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
   914     then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
   915        by (simp add: M2.emeasure_pair_measure_Times)
   916     also have "\<dots> = emeasure M (A \<times> B)"
   917       using A B emeasure by auto
   918     finally show "emeasure ?P X = emeasure M X"
   919       by simp
   920   qed
   921 qed
   922   
   923 lemma sets_pair_countable:
   924   assumes "countable S1" "countable S2"
   925   assumes M: "sets M = Pow S1" and N: "sets N = Pow S2"
   926   shows "sets (M \<Otimes>\<^sub>M N) = Pow (S1 \<times> S2)"
   927 proof auto
   928   fix x a b assume x: "x \<in> sets (M \<Otimes>\<^sub>M N)" "(a, b) \<in> x"
   929   from sets.sets_into_space[OF x(1)] x(2)
   930     sets_eq_imp_space_eq[of N "count_space S2"] sets_eq_imp_space_eq[of M "count_space S1"] M N
   931   show "a \<in> S1" "b \<in> S2"
   932     by (auto simp: space_pair_measure)
   933 next
   934   fix X assume X: "X \<subseteq> S1 \<times> S2"
   935   then have "countable X"
   936     by (metis countable_subset `countable S1` `countable S2` countable_SIGMA)
   937   have "X = (\<Union>(a, b)\<in>X. {a} \<times> {b})" by auto
   938   also have "\<dots> \<in> sets (M \<Otimes>\<^sub>M N)"
   939     using X
   940     by (safe intro!: sets.countable_UN' `countable X` subsetI pair_measureI) (auto simp: M N)
   941   finally show "X \<in> sets (M \<Otimes>\<^sub>M N)" .
   942 qed
   943 
   944 lemma pair_measure_countable:
   945   assumes "countable S1" "countable S2"
   946   shows "count_space S1 \<Otimes>\<^sub>M count_space S2 = count_space (S1 \<times> S2)"
   947 proof (rule pair_measure_eqI)
   948   show "sigma_finite_measure (count_space S1)" "sigma_finite_measure (count_space S2)"
   949     using assms by (auto intro!: sigma_finite_measure_count_space_countable)
   950   show "sets (count_space S1 \<Otimes>\<^sub>M count_space S2) = sets (count_space (S1 \<times> S2))"
   951     by (subst sets_pair_countable[OF assms]) auto
   952 next
   953   fix A B assume "A \<in> sets (count_space S1)" "B \<in> sets (count_space S2)"
   954   then show "emeasure (count_space S1) A * emeasure (count_space S2) B = 
   955     emeasure (count_space (S1 \<times> S2)) (A \<times> B)"
   956     by (subst (1 2 3) emeasure_count_space) (auto simp: finite_cartesian_product_iff)
   957 qed
   958 
   959 lemma nn_integral_fst_count_space':
   960   assumes nonneg: "\<And>xy. 0 \<le> f xy"
   961   shows "(\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"
   962   (is "?lhs = ?rhs")
   963 proof(cases)
   964   assume *: "countable {xy. f xy \<noteq> 0}"
   965   let ?A = "fst ` {xy. f xy \<noteq> 0}"
   966   let ?B = "snd ` {xy. f xy \<noteq> 0}"
   967   from * have [simp]: "countable ?A" "countable ?B" by(rule countable_image)+
   968   from nonneg have f_neq_0: "\<And>xy. f xy \<noteq> 0 \<longleftrightarrow> f xy > 0"
   969     by(auto simp add: order.order_iff_strict)
   970   have "?lhs = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space ?A)"
   971     by(rule nn_integral_count_space_eq)
   972       (auto simp add: f_neq_0 nn_integral_0_iff_AE AE_count_space not_le intro: rev_image_eqI)
   973   also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space ?B \<partial>count_space ?A)"
   974     by(intro nn_integral_count_space_eq nn_integral_cong)(auto intro: rev_image_eqI)
   975   also have "\<dots> = (\<integral>\<^sup>+ xy. f xy \<partial>count_space (?A \<times> ?B))"
   976     by(subst sigma_finite_measure.nn_integral_fst)
   977       (simp_all add: sigma_finite_measure_count_space_countable pair_measure_countable)
   978   also have "\<dots> = ?rhs"
   979     by(rule nn_integral_count_space_eq)(auto intro: rev_image_eqI)
   980   finally show ?thesis .
   981 next
   982   { fix xy assume "f xy \<noteq> 0"
   983     with `0 \<le> f xy` have "(\<exists>r. 0 < r \<and> f xy = ereal r) \<or> f xy = \<infinity>"
   984       by (cases "f xy") (auto simp: less_le)
   985     then have "\<exists>n. ereal (1 / real (Suc n)) \<le> f xy"
   986       by (auto elim!: nat_approx_posE intro!: less_imp_le) }
   987   note * = this
   988 
   989   assume cntbl: "uncountable {xy. f xy \<noteq> 0}"
   990   also have "{xy. f xy \<noteq> 0} = (\<Union>n. {xy. 1/Suc n \<le> f xy})"
   991     using * by auto
   992   finally obtain n where "infinite {xy. 1/Suc n \<le> f xy}"
   993     by (meson countableI_type countable_UN uncountable_infinite)
   994   then obtain C where C: "C \<subseteq> {xy. 1/Suc n \<le> f xy}" and "countable C" "infinite C"
   995     by (metis infinite_countable_subset')
   996 
   997   have "\<infinity> = (\<integral>\<^sup>+ xy. ereal (1 / Suc n) * indicator C xy \<partial>count_space UNIV)"
   998     using \<open>infinite C\<close> by(simp add: nn_integral_cmult)
   999   also have "\<dots> \<le> ?rhs" using C
  1000     by(intro nn_integral_mono)(auto split: split_indicator simp add: nonneg)
  1001   finally have "?rhs = \<infinity>" by simp
  1002   moreover have "?lhs = \<infinity>"
  1003   proof(cases "finite (fst ` C)")
  1004     case True
  1005     then obtain x C' where x: "x \<in> fst ` C" 
  1006       and C': "C' = fst -` {x} \<inter> C"
  1007       and "infinite C'"
  1008       using \<open>infinite C\<close> by(auto elim!: inf_img_fin_domE')
  1009     from x C C' have **: "C' \<subseteq> {xy. 1 / Suc n \<le> f xy}" by auto
  1010 
  1011     from C' \<open>infinite C'\<close> have "infinite (snd ` C')"
  1012       by(auto dest!: finite_imageD simp add: inj_on_def)
  1013     then have "\<infinity> = (\<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator (snd ` C') y \<partial>count_space UNIV)"
  1014       by(simp add: nn_integral_cmult)
  1015     also have "\<dots> = (\<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV)"
  1016       by(rule nn_integral_cong)(force split: split_indicator intro: rev_image_eqI simp add: C')
  1017     also have "\<dots> = (\<integral>\<^sup>+ x'. (\<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV) * indicator {x} x' \<partial>count_space UNIV)"
  1018       by(simp add: one_ereal_def[symmetric] nn_integral_nonneg nn_integral_cmult_indicator)
  1019     also have "\<dots> \<le> (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV \<partial>count_space UNIV)"
  1020       by(rule nn_integral_mono)(simp split: split_indicator add: nn_integral_nonneg)
  1021     also have "\<dots> \<le> ?lhs" using **
  1022       by(intro nn_integral_mono)(auto split: split_indicator simp add: nonneg)
  1023     finally show ?thesis by simp
  1024   next
  1025     case False
  1026     def C' \<equiv> "fst ` C"
  1027     have "\<infinity> = \<integral>\<^sup>+ x. ereal (1 / Suc n) * indicator C' x \<partial>count_space UNIV"
  1028       using C'_def False by(simp add: nn_integral_cmult)
  1029     also have "\<dots> = \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C' x * indicator {SOME y. (x, y) \<in> C} y \<partial>count_space UNIV \<partial>count_space UNIV"
  1030       by(auto simp add: one_ereal_def[symmetric] nn_integral_cmult_indicator intro: nn_integral_cong)
  1031     also have "\<dots> \<le> \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. ereal (1 / Suc n) * indicator C (x, y) \<partial>count_space UNIV \<partial>count_space UNIV"
  1032       by(intro nn_integral_mono)(auto simp add: C'_def split: split_indicator intro: someI)
  1033     also have "\<dots> \<le> ?lhs" using C
  1034       by(intro nn_integral_mono)(auto split: split_indicator simp add: nonneg)
  1035     finally show ?thesis by simp
  1036   qed
  1037   ultimately show ?thesis by simp
  1038 qed
  1039 
  1040 lemma nn_integral_fst_count_space:
  1041   "(\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"
  1042 by(subst (2 3) nn_integral_max_0[symmetric])(rule nn_integral_fst_count_space', simp)
  1043 
  1044 lemma nn_integral_snd_count_space:
  1045   "(\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"
  1046   (is "?lhs = ?rhs")
  1047 proof -
  1048   have "?lhs = (\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. (\<lambda>(y, x). f (x, y)) (y, x) \<partial>count_space UNIV \<partial>count_space UNIV)"
  1049     by(simp)
  1050   also have "\<dots> = \<integral>\<^sup>+ yx. (\<lambda>(y, x). f (x, y)) yx \<partial>count_space UNIV"
  1051     by(rule nn_integral_fst_count_space)
  1052   also have "\<dots> = \<integral>\<^sup>+ xy. f xy \<partial>count_space ((\<lambda>(x, y). (y, x)) ` UNIV)"
  1053     by(subst nn_integral_bij_count_space[OF inj_on_imp_bij_betw, symmetric])
  1054       (simp_all add: inj_on_def split_def)
  1055   also have "\<dots> = ?rhs" by(rule nn_integral_count_space_eq) auto
  1056   finally show ?thesis .
  1057 qed
  1058 
  1059 lemma measurable_pair_measure_countable1:
  1060   assumes "countable A"
  1061   and [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N K"
  1062   shows "f \<in> measurable (count_space A \<Otimes>\<^sub>M N) K"
  1063 using _ _ assms(1)
  1064 by(rule measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A, simplified])simp_all
  1065 
  1066 subsection {* Product of Borel spaces *}
  1067 
  1068 lemma borel_Times:
  1069   fixes A :: "'a::topological_space set" and B :: "'b::topological_space set"
  1070   assumes A: "A \<in> sets borel" and B: "B \<in> sets borel"
  1071   shows "A \<times> B \<in> sets borel"
  1072 proof -
  1073   have "A \<times> B = (A\<times>UNIV) \<inter> (UNIV \<times> B)"
  1074     by auto
  1075   moreover
  1076   { have "A \<in> sigma_sets UNIV {S. open S}" using A by (simp add: sets_borel)
  1077     then have "A\<times>UNIV \<in> sets borel"
  1078     proof (induct A)
  1079       case (Basic S) then show ?case
  1080         by (auto intro!: borel_open open_Times)
  1081     next
  1082       case (Compl A)
  1083       moreover have *: "(UNIV - A) \<times> UNIV = UNIV - (A \<times> UNIV)"
  1084         by auto
  1085       ultimately show ?case
  1086         unfolding * by auto
  1087     next
  1088       case (Union A)
  1089       moreover have *: "(UNION UNIV A) \<times> UNIV = UNION UNIV (\<lambda>i. A i \<times> UNIV)"
  1090         by auto
  1091       ultimately show ?case
  1092         unfolding * by auto
  1093     qed simp }
  1094   moreover
  1095   { have "B \<in> sigma_sets UNIV {S. open S}" using B by (simp add: sets_borel)
  1096     then have "UNIV\<times>B \<in> sets borel"
  1097     proof (induct B)
  1098       case (Basic S) then show ?case
  1099         by (auto intro!: borel_open open_Times)
  1100     next
  1101       case (Compl B)
  1102       moreover have *: "UNIV \<times> (UNIV - B) = UNIV - (UNIV \<times> B)"
  1103         by auto
  1104       ultimately show ?case
  1105         unfolding * by auto
  1106     next
  1107       case (Union B)
  1108       moreover have *: "UNIV \<times> (UNION UNIV B) = UNION UNIV (\<lambda>i. UNIV \<times> B i)"
  1109         by auto
  1110       ultimately show ?case
  1111         unfolding * by auto
  1112     qed simp }
  1113   ultimately show ?thesis
  1114     by auto
  1115 qed
  1116 
  1117 lemma finite_measure_pair_measure:
  1118   assumes "finite_measure M" "finite_measure N"
  1119   shows "finite_measure (N  \<Otimes>\<^sub>M M)"
  1120 proof (rule finite_measureI)
  1121   interpret M: finite_measure M by fact
  1122   interpret N: finite_measure N by fact
  1123   show "emeasure (N  \<Otimes>\<^sub>M M) (space (N  \<Otimes>\<^sub>M M)) \<noteq> \<infinity>"
  1124     by (auto simp: space_pair_measure M.emeasure_pair_measure_Times)
  1125 qed
  1126 
  1127 end