src/HOL/Analysis/Binary_Product_Measure.thy
 author wenzelm Sun Sep 18 20:33:48 2016 +0200 (2016-09-18) changeset 63915 bab633745c7f parent 63627 6ddb43c6b711 child 64008 17a20ca86d62 permissions -rw-r--r--
tuned proofs;
     1 (*  Title:      HOL/Analysis/Binary_Product_Measure.thy

     2     Author:     Johannes Hölzl, TU München

     3 *)

     4

     5 section \<open>Binary product measures\<close>

     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_measure_cong[measurable_cong, cong]:

    41   "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^sub>M M2) = sets (M1' \<Otimes>\<^sub>M M2')"

    42   unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)

    43

    44 lemma pair_measureI[intro, simp, measurable]:

    45   "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^sub>M B)"

    46   by (auto simp: sets_pair_measure)

    47

    48 lemma sets_Pair: "{x} \<in> sets M1 \<Longrightarrow> {y} \<in> sets M2 \<Longrightarrow> {(x, y)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"

    49   using pair_measureI[of "{x}" M1 "{y}" M2] by simp

    50

    51 lemma measurable_pair_measureI:

    52   assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"

    53   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"

    54   shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"

    55   unfolding pair_measure_def using 1 2

    56   by (intro measurable_measure_of) (auto dest: sets.sets_into_space)

    57

    58 lemma measurable_split_replace[measurable (raw)]:

    59   "(\<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"

    60   unfolding split_beta' .

    61

    62 lemma measurable_Pair[measurable (raw)]:

    63   assumes f: "f \<in> measurable M M1" and g: "g \<in> measurable M M2"

    64   shows "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"

    65 proof (rule measurable_pair_measureI)

    66   show "(\<lambda>x. (f x, g x)) \<in> space M \<rightarrow> space M1 \<times> space M2"

    67     using f g by (auto simp: measurable_def)

    68   fix A B assume *: "A \<in> sets M1" "B \<in> sets M2"

    69   have "(\<lambda>x. (f x, g x)) - (A \<times> B) \<inter> space M = (f - A \<inter> space M) \<inter> (g - B \<inter> space M)"

    70     by auto

    71   also have "\<dots> \<in> sets M"

    72     by (rule sets.Int) (auto intro!: measurable_sets * f g)

    73   finally show "(\<lambda>x. (f x, g x)) - (A \<times> B) \<inter> space M \<in> sets M" .

    74 qed

    75

    76 lemma measurable_fst[intro!, simp, measurable]: "fst \<in> measurable (M1 \<Otimes>\<^sub>M M2) M1"

    77   by (auto simp: fst_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times

    78     measurable_def)

    79

    80 lemma measurable_snd[intro!, simp, measurable]: "snd \<in> measurable (M1 \<Otimes>\<^sub>M M2) M2"

    81   by (auto simp: snd_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times

    82     measurable_def)

    83

    84 lemma measurable_Pair_compose_split[measurable_dest]:

    85   assumes f: "case_prod f \<in> measurable (M1 \<Otimes>\<^sub>M M2) N"

    86   assumes g: "g \<in> measurable M M1" and h: "h \<in> measurable M M2"

    87   shows "(\<lambda>x. f (g x) (h x)) \<in> measurable M N"

    88   using measurable_compose[OF measurable_Pair f, OF g h] by simp

    89

    90 lemma measurable_Pair1_compose[measurable_dest]:

    91   assumes f: "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"

    92   assumes [measurable]: "h \<in> measurable N M"

    93   shows "(\<lambda>x. f (h x)) \<in> measurable N M1"

    94   using measurable_compose[OF f measurable_fst] by simp

    95

    96 lemma measurable_Pair2_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. g (h x)) \<in> measurable N M2"

   100   using measurable_compose[OF f measurable_snd] by simp

   101

   102 lemma measurable_pair:

   103   assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"

   104   shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"

   105   using measurable_Pair[OF assms] by simp

   106

   107 lemma

   108   assumes f[measurable]: "f \<in> measurable M (N \<Otimes>\<^sub>M P)"

   109   shows measurable_fst': "(\<lambda>x. fst (f x)) \<in> measurable M N"

   110     and measurable_snd': "(\<lambda>x. snd (f x)) \<in> measurable M P"

   111   by simp_all

   112

   113 lemma

   114   assumes f[measurable]: "f \<in> measurable M N"

   115   shows measurable_fst'': "(\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^sub>M P) N"

   116     and measurable_snd'': "(\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^sub>M M) N"

   117   by simp_all

   118

   119 lemma sets_pair_in_sets:

   120   assumes "\<And>a b. a \<in> sets A \<Longrightarrow> b \<in> sets B \<Longrightarrow> a \<times> b \<in> sets N"

   121   shows "sets (A \<Otimes>\<^sub>M B) \<subseteq> sets N"

   122   unfolding sets_pair_measure

   123   by (intro sets.sigma_sets_subset') (auto intro!: assms)

   124

   125 lemma sets_pair_eq_sets_fst_snd:

   126   "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})"

   127     (is "?P = sets (Sup {?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 {?fst, ?snd})"

   133       apply (rule sets.Int)

   134       apply (rule in_sets_Sup)

   135       apply auto []

   136       apply (rule insertI1)

   137       apply (auto intro: ab in_vimage_algebra) []

   138       apply (rule in_sets_Sup)

   139       apply auto []

   140       apply (rule insertI2)

   141       apply (auto intro: ab in_vimage_algebra)

   142       done

   143     finally have "a \<times> b \<in> sets (Sup {?fst, ?snd})" . }

   144   moreover have "sets ?fst \<subseteq> sets (A \<Otimes>\<^sub>M B)"

   145     by (rule sets_image_in_sets) (auto simp: space_pair_measure[symmetric])

   146   moreover have "sets ?snd \<subseteq> sets (A \<Otimes>\<^sub>M B)"

   147     by (rule sets_image_in_sets) (auto simp: space_pair_measure)

   148   ultimately show ?thesis

   149     apply (intro antisym[of "sets A" for A] sets_Sup_in_sets sets_pair_in_sets)

   150     apply simp

   151     apply simp

   152     apply simp

   153     apply (elim disjE)

   154     apply (simp add: space_pair_measure)

   155     apply (simp add: space_pair_measure)

   156     apply (auto simp add: space_pair_measure)

   157     done

   158 qed

   159

   160 lemma measurable_pair_iff:

   161   "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"

   162   by (auto intro: measurable_pair[of f M M1 M2])

   163

   164 lemma measurable_split_conv:

   165   "(\<lambda>(x, y). f x y) \<in> measurable A B \<longleftrightarrow> (\<lambda>x. f (fst x) (snd x)) \<in> measurable A B"

   166   by (intro arg_cong2[where f="op \<in>"]) auto

   167

   168 lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (M2 \<Otimes>\<^sub>M M1)"

   169   by (auto intro!: measurable_Pair simp: measurable_split_conv)

   170

   171 lemma measurable_pair_swap:

   172   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"

   173   using measurable_comp[OF measurable_Pair f] by (auto simp: measurable_split_conv comp_def)

   174

   175 lemma measurable_pair_swap_iff:

   176   "f \<in> measurable (M2 \<Otimes>\<^sub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) M"

   177   by (auto dest: measurable_pair_swap)

   178

   179 lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^sub>M M2)"

   180   by simp

   181

   182 lemma sets_Pair1[measurable (raw)]:

   183   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "Pair x - A \<in> sets M2"

   184 proof -

   185   have "Pair x - A = (if x \<in> space M1 then Pair x - A \<inter> space M2 else {})"

   186     using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)

   187   also have "\<dots> \<in> sets M2"

   188     using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: if_split_asm)

   189   finally show ?thesis .

   190 qed

   191

   192 lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^sub>M M2)"

   193   by (auto intro!: measurable_Pair)

   194

   195 lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>x. (x, y)) - A \<in> sets M1"

   196 proof -

   197   have "(\<lambda>x. (x, y)) - A = (if y \<in> space M2 then (\<lambda>x. (x, y)) - A \<inter> space M1 else {})"

   198     using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)

   199   also have "\<dots> \<in> sets M1"

   200     using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: if_split_asm)

   201   finally show ?thesis .

   202 qed

   203

   204 lemma measurable_Pair2:

   205   assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and x: "x \<in> space M1"

   206   shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"

   207   using measurable_comp[OF measurable_Pair1' f, OF x]

   208   by (simp add: comp_def)

   209

   210 lemma measurable_Pair1:

   211   assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and y: "y \<in> space M2"

   212   shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"

   213   using measurable_comp[OF measurable_Pair2' f, OF y]

   214   by (simp add: comp_def)

   215

   216 lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"

   217   unfolding Int_stable_def

   218   by safe (auto simp add: times_Int_times)

   219

   220 lemma (in finite_measure) finite_measure_cut_measurable:

   221   assumes [measurable]: "Q \<in> sets (N \<Otimes>\<^sub>M M)"

   222   shows "(\<lambda>x. emeasure M (Pair x - Q)) \<in> borel_measurable N"

   223     (is "?s Q \<in> _")

   224   using Int_stable_pair_measure_generator pair_measure_closed assms

   225   unfolding sets_pair_measure

   226 proof (induct rule: sigma_sets_induct_disjoint)

   227   case (compl A)

   228   with sets.sets_into_space have "\<And>x. emeasure M (Pair x - ((space N \<times> space M) - A)) =

   229       (if x \<in> space N then emeasure M (space M) - ?s A x else 0)"

   230     unfolding sets_pair_measure[symmetric]

   231     by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)

   232   with compl sets.top show ?case

   233     by (auto intro!: measurable_If simp: space_pair_measure)

   234 next

   235   case (union F)

   236   then have "\<And>x. emeasure M (Pair x - (\<Union>i. F i)) = (\<Sum>i. ?s (F i) x)"

   237     by (simp add: suminf_emeasure disjoint_family_on_vimageI subset_eq vimage_UN sets_pair_measure[symmetric])

   238   with union show ?case

   239     unfolding sets_pair_measure[symmetric] by simp

   240 qed (auto simp add: if_distrib Int_def[symmetric] intro!: measurable_If)

   241

   242 lemma (in sigma_finite_measure) measurable_emeasure_Pair:

   243   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> _")

   244 proof -

   245   from sigma_finite_disjoint guess F . note F = this

   246   then have F_sets: "\<And>i. F i \<in> sets M" by auto

   247   let ?C = "\<lambda>x i. F i \<inter> Pair x - Q"

   248   { fix i

   249     have [simp]: "space N \<times> F i \<inter> space N \<times> space M = space N \<times> F i"

   250       using F sets.sets_into_space by auto

   251     let ?R = "density M (indicator (F i))"

   252     have "finite_measure ?R"

   253       using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)

   254     then have "(\<lambda>x. emeasure ?R (Pair x - (space N \<times> space ?R \<inter> Q))) \<in> borel_measurable N"

   255      by (rule finite_measure.finite_measure_cut_measurable) (auto intro: Q)

   256     moreover have "\<And>x. emeasure ?R (Pair x - (space N \<times> space ?R \<inter> Q))

   257         = emeasure M (F i \<inter> Pair x - (space N \<times> space ?R \<inter> Q))"

   258       using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)

   259     moreover have "\<And>x. F i \<inter> Pair x - (space N \<times> space ?R \<inter> Q) = ?C x i"

   260       using sets.sets_into_space[OF Q] by (auto simp: space_pair_measure)

   261     ultimately have "(\<lambda>x. emeasure M (?C x i)) \<in> borel_measurable N"

   262       by simp }

   263   moreover

   264   { fix x

   265     have "(\<Sum>i. emeasure M (?C x i)) = emeasure M (\<Union>i. ?C x i)"

   266     proof (intro suminf_emeasure)

   267       show "range (?C x) \<subseteq> sets M"

   268         using F \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close> by (auto intro!: sets_Pair1)

   269       have "disjoint_family F" using F by auto

   270       show "disjoint_family (?C x)"

   271         by (rule disjoint_family_on_bisimulation[OF \<open>disjoint_family F\<close>]) auto

   272     qed

   273     also have "(\<Union>i. ?C x i) = Pair x - Q"

   274       using F sets.sets_into_space[OF \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close>]

   275       by (auto simp: space_pair_measure)

   276     finally have "emeasure M (Pair x - Q) = (\<Sum>i. emeasure M (?C x i))"

   277       by simp }

   278   ultimately show ?thesis using \<open>Q \<in> sets (N \<Otimes>\<^sub>M M)\<close> F_sets

   279     by auto

   280 qed

   281

   282 lemma (in sigma_finite_measure) measurable_emeasure[measurable (raw)]:

   283   assumes space: "\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M"

   284   assumes A: "{x\<in>space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M)"

   285   shows "(\<lambda>x. emeasure M (A x)) \<in> borel_measurable N"

   286 proof -

   287   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"

   288     by (auto simp: space_pair_measure)

   289   with measurable_emeasure_Pair[OF A] show ?thesis

   290     by (auto cong: measurable_cong)

   291 qed

   292

   293 lemma (in sigma_finite_measure) emeasure_pair_measure:

   294   assumes "X \<in> sets (N \<Otimes>\<^sub>M M)"

   295   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")

   296 proof (rule emeasure_measure_of[OF pair_measure_def])

   297   show "positive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"

   298     by (auto simp: positive_def)

   299   have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x - A) y"

   300     by (auto simp: indicator_def)

   301   show "countably_additive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"

   302   proof (rule countably_additiveI)

   303     fix F :: "nat \<Rightarrow> ('b \<times> 'a) set" assume F: "range F \<subseteq> sets (N \<Otimes>\<^sub>M M)" "disjoint_family F"

   304     from F have *: "\<And>i. F i \<in> sets (N \<Otimes>\<^sub>M M)" by auto

   305     moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x - F i)"

   306       by (intro disjoint_family_on_bisimulation[OF F(2)]) auto

   307     moreover have "\<And>x. range (\<lambda>i. Pair x - F i) \<subseteq> sets M"

   308       using F by (auto simp: sets_Pair1)

   309     ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"

   310       by (auto simp add: nn_integral_suminf[symmetric] vimage_UN suminf_emeasure

   311                intro!: nn_integral_cong nn_integral_indicator[symmetric])

   312   qed

   313   show "{a \<times> b |a b. a \<in> sets N \<and> b \<in> sets M} \<subseteq> Pow (space N \<times> space M)"

   314     using sets.space_closed[of N] sets.space_closed[of M] by auto

   315 qed fact

   316

   317 lemma (in sigma_finite_measure) emeasure_pair_measure_alt:

   318   assumes X: "X \<in> sets (N \<Otimes>\<^sub>M M)"

   319   shows "emeasure (N  \<Otimes>\<^sub>M M) X = (\<integral>\<^sup>+x. emeasure M (Pair x - X) \<partial>N)"

   320 proof -

   321   have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x - X) y"

   322     by (auto simp: indicator_def)

   323   show ?thesis

   324     using X by (auto intro!: nn_integral_cong simp: emeasure_pair_measure sets_Pair1)

   325 qed

   326

   327 lemma (in sigma_finite_measure) emeasure_pair_measure_Times:

   328   assumes A: "A \<in> sets N" and B: "B \<in> sets M"

   329   shows "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = emeasure N A * emeasure M B"

   330 proof -

   331   have "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = (\<integral>\<^sup>+x. emeasure M B * indicator A x \<partial>N)"

   332     using A B by (auto intro!: nn_integral_cong simp: emeasure_pair_measure_alt)

   333   also have "\<dots> = emeasure M B * emeasure N A"

   334     using A by (simp add: nn_integral_cmult_indicator)

   335   finally show ?thesis

   336     by (simp add: ac_simps)

   337 qed

   338

   339 subsection \<open>Binary products of $\sigma$-finite emeasure spaces\<close>

   340

   341 locale pair_sigma_finite = M1?: sigma_finite_measure M1 + M2?: sigma_finite_measure M2

   342   for M1 :: "'a measure" and M2 :: "'b measure"

   343

   344 lemma (in pair_sigma_finite) measurable_emeasure_Pair1:

   345   "Q \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow> (\<lambda>x. emeasure M2 (Pair x - Q)) \<in> borel_measurable M1"

   346   using M2.measurable_emeasure_Pair .

   347

   348 lemma (in pair_sigma_finite) measurable_emeasure_Pair2:

   349   assumes Q: "Q \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) - Q)) \<in> borel_measurable M2"

   350 proof -

   351   have "(\<lambda>(x, y). (y, x)) - Q \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"

   352     using Q measurable_pair_swap' by (auto intro: measurable_sets)

   353   note M1.measurable_emeasure_Pair[OF this]

   354   moreover have "\<And>y. Pair y - ((\<lambda>(x, y). (y, x)) - Q \<inter> space (M2 \<Otimes>\<^sub>M M1)) = (\<lambda>x. (x, y)) - Q"

   355     using Q[THEN sets.sets_into_space] by (auto simp: space_pair_measure)

   356   ultimately show ?thesis by simp

   357 qed

   358

   359 lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:

   360   defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"

   361   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>

   362     (\<forall>i. emeasure (M1 \<Otimes>\<^sub>M M2) (F i) \<noteq> \<infinity>)"

   363 proof -

   364   from M1.sigma_finite_incseq guess F1 . note F1 = this

   365   from M2.sigma_finite_incseq guess F2 . note F2 = this

   366   from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto

   367   let ?F = "\<lambda>i. F1 i \<times> F2 i"

   368   show ?thesis

   369   proof (intro exI[of _ ?F] conjI allI)

   370     show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)

   371   next

   372     have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"

   373     proof (intro subsetI)

   374       fix x assume "x \<in> space M1 \<times> space M2"

   375       then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"

   376         by (auto simp: space)

   377       then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"

   378         using \<open>incseq F1\<close> \<open>incseq F2\<close> unfolding incseq_def

   379         by (force split: split_max)+

   380       then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"

   381         by (intro SigmaI) (auto simp add: max.commute)

   382       then show "x \<in> (\<Union>i. ?F i)" by auto

   383     qed

   384     then show "(\<Union>i. ?F i) = space M1 \<times> space M2"

   385       using space by (auto simp: space)

   386   next

   387     fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"

   388       using \<open>incseq F1\<close> \<open>incseq F2\<close> unfolding incseq_Suc_iff by auto

   389   next

   390     fix i

   391     from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto

   392     with F1 F2 show "emeasure (M1 \<Otimes>\<^sub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"

   393       by (auto simp add: emeasure_pair_measure_Times ennreal_mult_eq_top_iff)

   394   qed

   395 qed

   396

   397 sublocale pair_sigma_finite \<subseteq> P?: sigma_finite_measure "M1 \<Otimes>\<^sub>M M2"

   398 proof

   399   from M1.sigma_finite_countable guess F1 ..

   400   moreover from M2.sigma_finite_countable guess F2 ..

   401   ultimately show

   402     "\<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>)"

   403     by (intro exI[of _ "(\<lambda>(a, b). a \<times> b)  (F1 \<times> F2)"] conjI)

   404        (auto simp: M2.emeasure_pair_measure_Times space_pair_measure set_eq_iff subset_eq ennreal_mult_eq_top_iff)

   405 qed

   406

   407 lemma sigma_finite_pair_measure:

   408   assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"

   409   shows "sigma_finite_measure (A \<Otimes>\<^sub>M B)"

   410 proof -

   411   interpret A: sigma_finite_measure A by fact

   412   interpret B: sigma_finite_measure B by fact

   413   interpret AB: pair_sigma_finite A  B ..

   414   show ?thesis ..

   415 qed

   416

   417 lemma sets_pair_swap:

   418   assumes "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"

   419   shows "(\<lambda>(x, y). (y, x)) - A \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"

   420   using measurable_pair_swap' assms by (rule measurable_sets)

   421

   422 lemma (in pair_sigma_finite) distr_pair_swap:

   423   "M1 \<Otimes>\<^sub>M M2 = distr (M2 \<Otimes>\<^sub>M M1) (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")

   424 proof -

   425   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this

   426   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"

   427   show ?thesis

   428   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])

   429     show "?E \<subseteq> Pow (space ?P)"

   430       using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)

   431     show "sets ?P = sigma_sets (space ?P) ?E"

   432       by (simp add: sets_pair_measure space_pair_measure)

   433     then show "sets ?D = sigma_sets (space ?P) ?E"

   434       by simp

   435   next

   436     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"

   437       using F by (auto simp: space_pair_measure)

   438   next

   439     fix X assume "X \<in> ?E"

   440     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

   441     have "(\<lambda>(y, x). (x, y)) - X \<inter> space (M2 \<Otimes>\<^sub>M M1) = B \<times> A"

   442       using sets.sets_into_space[OF A] sets.sets_into_space[OF B] by (auto simp: space_pair_measure)

   443     with A B show "emeasure (M1 \<Otimes>\<^sub>M M2) X = emeasure ?D X"

   444       by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_pair_measure_Times emeasure_distr

   445                     measurable_pair_swap' ac_simps)

   446   qed

   447 qed

   448

   449 lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:

   450   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"

   451   shows "emeasure (M1 \<Otimes>\<^sub>M M2) A = (\<integral>\<^sup>+y. emeasure M1 ((\<lambda>x. (x, y)) - A) \<partial>M2)"

   452     (is "_ = ?\<nu> A")

   453 proof -

   454   have [simp]: "\<And>y. (Pair y - ((\<lambda>(x, y). (y, x)) - A \<inter> space (M2 \<Otimes>\<^sub>M M1))) = (\<lambda>x. (x, y)) - A"

   455     using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)

   456   show ?thesis using A

   457     by (subst distr_pair_swap)

   458        (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']

   459                  M1.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])

   460 qed

   461

   462 lemma (in pair_sigma_finite) AE_pair:

   463   assumes "AE x in (M1 \<Otimes>\<^sub>M M2). Q x"

   464   shows "AE x in M1. (AE y in M2. Q (x, y))"

   465 proof -

   466   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"

   467     using assms unfolding eventually_ae_filter by auto

   468   show ?thesis

   469   proof (rule AE_I)

   470     from N measurable_emeasure_Pair1[OF \<open>N \<in> sets (M1 \<Otimes>\<^sub>M M2)\<close>]

   471     show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x - N) \<noteq> 0} = 0"

   472       by (auto simp: M2.emeasure_pair_measure_alt nn_integral_0_iff)

   473     show "{x \<in> space M1. emeasure M2 (Pair x - N) \<noteq> 0} \<in> sets M1"

   474       by (intro borel_measurable_eq measurable_emeasure_Pair1 N sets.sets_Collect_neg N) simp

   475     { fix x assume "x \<in> space M1" "emeasure M2 (Pair x - N) = 0"

   476       have "AE y in M2. Q (x, y)"

   477       proof (rule AE_I)

   478         show "emeasure M2 (Pair x - N) = 0" by fact

   479         show "Pair x - N \<in> sets M2" using N(1) by (rule sets_Pair1)

   480         show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x - N"

   481           using N \<open>x \<in> space M1\<close> unfolding space_pair_measure by auto

   482       qed }

   483     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}"

   484       by auto

   485   qed

   486 qed

   487

   488 lemma (in pair_sigma_finite) AE_pair_measure:

   489   assumes "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"

   490   assumes ae: "AE x in M1. AE y in M2. P (x, y)"

   491   shows "AE x in M1 \<Otimes>\<^sub>M M2. P x"

   492 proof (subst AE_iff_measurable[OF _ refl])

   493   show "{x\<in>space (M1 \<Otimes>\<^sub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"

   494     by (rule sets.sets_Collect) fact

   495   then have "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} =

   496       (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"

   497     by (simp add: M2.emeasure_pair_measure)

   498   also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. 0 \<partial>M2 \<partial>M1)"

   499     using ae

   500     apply (safe intro!: nn_integral_cong_AE)

   501     apply (intro AE_I2)

   502     apply (safe intro!: nn_integral_cong_AE)

   503     apply auto

   504     done

   505   finally show "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} = 0" by simp

   506 qed

   507

   508 lemma (in pair_sigma_finite) AE_pair_iff:

   509   "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow>

   510     (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))"

   511   using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto

   512

   513 lemma (in pair_sigma_finite) AE_commute:

   514   assumes P: "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"

   515   shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"

   516 proof -

   517   interpret Q: pair_sigma_finite M2 M1 ..

   518   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"

   519     by auto

   520   have "{x \<in> space (M2 \<Otimes>\<^sub>M M1). P (snd x) (fst x)} =

   521     (\<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)"

   522     by (auto simp: space_pair_measure)

   523   also have "\<dots> \<in> sets (M2 \<Otimes>\<^sub>M M1)"

   524     by (intro sets_pair_swap P)

   525   finally show ?thesis

   526     apply (subst AE_pair_iff[OF P])

   527     apply (subst distr_pair_swap)

   528     apply (subst AE_distr_iff[OF measurable_pair_swap' P])

   529     apply (subst Q.AE_pair_iff)

   530     apply simp_all

   531     done

   532 qed

   533

   534 subsection "Fubinis theorem"

   535

   536 lemma measurable_compose_Pair1:

   537   "x \<in> space M1 \<Longrightarrow> g \<in> measurable (M1 \<Otimes>\<^sub>M M2) L \<Longrightarrow> (\<lambda>y. g (x, y)) \<in> measurable M2 L"

   538   by simp

   539

   540 lemma (in sigma_finite_measure) borel_measurable_nn_integral_fst:

   541   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"

   542   shows "(\<lambda>x. \<integral>\<^sup>+ y. f (x, y) \<partial>M) \<in> borel_measurable M1"

   543 using f proof induct

   544   case (cong u v)

   545   then have "\<And>w x. w \<in> space M1 \<Longrightarrow> x \<in> space M \<Longrightarrow> u (w, x) = v (w, x)"

   546     by (auto simp: space_pair_measure)

   547   show ?case

   548     apply (subst measurable_cong)

   549     apply (rule nn_integral_cong)

   550     apply fact+

   551     done

   552 next

   553   case (set Q)

   554   have [simp]: "\<And>x y. indicator Q (x, y) = indicator (Pair x - Q) y"

   555     by (auto simp: indicator_def)

   556   have "\<And>x. x \<in> space M1 \<Longrightarrow> emeasure M (Pair x - Q) = \<integral>\<^sup>+ y. indicator Q (x, y) \<partial>M"

   557     by (simp add: sets_Pair1[OF set])

   558   from this measurable_emeasure_Pair[OF set] show ?case

   559     by (rule measurable_cong[THEN iffD1])

   560 qed (simp_all add: nn_integral_add nn_integral_cmult measurable_compose_Pair1

   561                    nn_integral_monotone_convergence_SUP incseq_def le_fun_def

   562               cong: measurable_cong)

   563

   564 lemma (in sigma_finite_measure) nn_integral_fst:

   565   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"

   566   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 = _")

   567 using f proof induct

   568   case (cong u v)

   569   then have "?I u = ?I v"

   570     by (intro nn_integral_cong) (auto simp: space_pair_measure)

   571   with cong show ?case

   572     by (simp cong: nn_integral_cong)

   573 qed (simp_all add: emeasure_pair_measure nn_integral_cmult nn_integral_add

   574                    nn_integral_monotone_convergence_SUP measurable_compose_Pair1

   575                    borel_measurable_nn_integral_fst nn_integral_mono incseq_def le_fun_def

   576               cong: nn_integral_cong)

   577

   578 lemma (in sigma_finite_measure) borel_measurable_nn_integral[measurable (raw)]:

   579   "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"

   580   using borel_measurable_nn_integral_fst[of "case_prod f" N] by simp

   581

   582 lemma (in pair_sigma_finite) nn_integral_snd:

   583   assumes f[measurable]: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"

   584   shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"

   585 proof -

   586   note measurable_pair_swap[OF f]

   587   from M1.nn_integral_fst[OF this]

   588   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))"

   589     by simp

   590   also have "(\<integral>\<^sup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"

   591     by (subst distr_pair_swap) (auto simp add: nn_integral_distr 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: "case_prod 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 \<open>Products on counting spaces, densities and distributions\<close>

   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 = sets (SUP xy:?XY. sigma (X \<times> Y) xy)"

   617     by (simp add: vimage_algebra_sigma sets_pair_eq_sets_fst_snd A B)

   618   also have "\<dots> = sets (sigma (X \<times> Y) (\<Union>?XY))"

   619     by (intro Sup_sigma arg_cong[where f=sets]) auto

   620   also have "\<dots> = sets ?S"

   621   proof (intro arg_cong[where f=sets] sigma_eqI sigma_sets_eqI)

   622     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)"

   623       using A B by auto

   624   next

   625     interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"

   626       using A B by (intro sigma_algebra_sigma_sets) auto

   627     fix Z assume "Z \<in> \<Union>?XY"

   628     then show "Z \<in> sigma_sets (X \<times> Y) {a \<times> b |a b. a \<in> A \<and> b \<in> B}"

   629     proof safe

   630       fix a assume "a \<in> A"

   631       from Y_cover obtain E where E: "E \<subseteq> B" "countable E" and "Y = \<Union>E"

   632         by auto

   633       with \<open>a \<in> A\<close> A have eq: "fst - a \<inter> X \<times> Y = (\<Union>e\<in>E. a \<times> e)"

   634         by auto

   635       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}"

   636         using \<open>a \<in> A\<close> E unfolding eq by (auto intro!: XY.countable_UN')

   637     next

   638       fix b assume "b \<in> B"

   639       from X_cover obtain E where E: "E \<subseteq> A" "countable E" and "X = \<Union>E"

   640         by auto

   641       with \<open>b \<in> B\<close> B have eq: "snd - b \<inter> X \<times> Y = (\<Union>e\<in>E. e \<times> b)"

   642         by auto

   643       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}"

   644         using \<open>b \<in> B\<close> E unfolding eq by (auto intro!: XY.countable_UN')

   645     qed

   646   next

   647     fix Z assume "Z \<in> {a \<times> b |a b. a \<in> A \<and> b \<in> B}"

   648     then obtain a b where "Z = a \<times> b" and ab: "a \<in> A" "b \<in> B"

   649       by auto

   650     then have Z: "Z = (fst - a \<inter> X \<times> Y) \<inter> (snd - b \<inter> X \<times> Y)"

   651       using A B by auto

   652     interpret XY: sigma_algebra "X \<times> Y" "sigma_sets (X \<times> Y) (\<Union>?XY)"

   653       by (intro sigma_algebra_sigma_sets) auto

   654     show "Z \<in> sigma_sets (X \<times> Y) (\<Union>?XY)"

   655       unfolding Z by (rule XY.Int) (blast intro: ab)+

   656   qed

   657   finally show "sets ?P = sets ?S" .

   658 next

   659   interpret finite_measure "sigma X A" for X A

   660     proof qed (simp add: emeasure_sigma)

   661   fix A assume "A \<in> sets ?P" then show "emeasure ?P A = emeasure ?S A"

   662     by (simp add: emeasure_pair_measure_alt emeasure_sigma)

   663 qed

   664

   665 lemma sigma_sets_pair_measure_generator_finite:

   666   assumes "finite A" and "finite B"

   667   shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"

   668   (is "sigma_sets ?prod ?sets = _")

   669 proof safe

   670   have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)

   671   fix x assume subset: "x \<subseteq> A \<times> B"

   672   hence "finite x" using fin by (rule finite_subset)

   673   from this subset show "x \<in> sigma_sets ?prod ?sets"

   674   proof (induct x)

   675     case empty show ?case by (rule sigma_sets.Empty)

   676   next

   677     case (insert a x)

   678     hence "{a} \<in> sigma_sets ?prod ?sets" by auto

   679     moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto

   680     ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)

   681   qed

   682 next

   683   fix x a b

   684   assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"

   685   from sigma_sets_into_sp[OF _ this(1)] this(2)

   686   show "a \<in> A" and "b \<in> B" by auto

   687 qed

   688

   689 lemma borel_prod:

   690   "(borel \<Otimes>\<^sub>M borel) = (borel :: ('a::second_countable_topology \<times> 'b::second_countable_topology) measure)"

   691   (is "?P = ?B")

   692 proof -

   693   have "?B = sigma UNIV {A \<times> B | A B. open A \<and> open B}"

   694     by (rule second_countable_borel_measurable[OF open_prod_generated])

   695   also have "\<dots> = ?P"

   696     unfolding borel_def

   697     by (subst sigma_prod) (auto intro!: exI[of _ "{UNIV}"])

   698   finally show ?thesis ..

   699 qed

   700

   701 lemma pair_measure_count_space:

   702   assumes A: "finite A" and B: "finite B"

   703   shows "count_space A \<Otimes>\<^sub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")

   704 proof (rule measure_eqI)

   705   interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact

   706   interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact

   707   interpret P: pair_sigma_finite "count_space A" "count_space B" ..

   708   show eq: "sets ?P = sets ?C"

   709     by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)

   710   fix X assume X: "X \<in> sets ?P"

   711   with eq have X_subset: "X \<subseteq> A \<times> B" by simp

   712   with A B have fin_Pair: "\<And>x. finite (Pair x - X)"

   713     by (intro finite_subset[OF _ B]) auto

   714   have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)

   715   have pos_card: "(0::ennreal) < of_nat (card (Pair x - X)) \<longleftrightarrow> Pair x - X \<noteq> {}" for x

   716     by (auto simp: card_eq_0_iff fin_Pair) blast

   717

   718   show "emeasure ?P X = emeasure ?C X"

   719     using X_subset A fin_Pair fin_X

   720     apply (subst B.emeasure_pair_measure_alt[OF X])

   721     apply (subst emeasure_count_space)

   722     apply (auto simp add: emeasure_count_space nn_integral_count_space

   723                           pos_card of_nat_setsum[symmetric] card_SigmaI[symmetric]

   724                 simp del: of_nat_setsum card_SigmaI

   725                 intro!: arg_cong[where f=card])

   726     done

   727 qed

   728

   729

   730 lemma emeasure_prod_count_space:

   731   assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M M)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")

   732   shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator A (x, y) \<partial>?B \<partial>?A)"

   733   by (rule emeasure_measure_of[OF pair_measure_def])

   734      (auto simp: countably_additive_def positive_def suminf_indicator A

   735                  nn_integral_suminf[symmetric] dest: sets.sets_into_space)

   736

   737 lemma emeasure_prod_count_space_single[simp]: "emeasure (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) {x} = 1"

   738 proof -

   739   have [simp]: "\<And>a b x y. indicator {(a, b)} (x, y) = (indicator {a} x * indicator {b} y::ennreal)"

   740     by (auto split: split_indicator)

   741   show ?thesis

   742     by (cases x) (auto simp: emeasure_prod_count_space nn_integral_cmult sets_Pair)

   743 qed

   744

   745 lemma emeasure_count_space_prod_eq:

   746   fixes A :: "('a \<times> 'b) set"

   747   assumes A: "A \<in> sets (count_space UNIV \<Otimes>\<^sub>M count_space UNIV)" (is "A \<in> sets (?A \<Otimes>\<^sub>M ?B)")

   748   shows "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"

   749 proof -

   750   { fix A :: "('a \<times> 'b) set" assume "countable A"

   751     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)"

   752       by (intro emeasure_UN_countable) (auto simp: sets_Pair disjoint_family_on_def)

   753     also have "\<dots> = (\<integral>\<^sup>+a. indicator A a \<partial>count_space UNIV)"

   754       by (subst nn_integral_count_space_indicator) auto

   755     finally have "emeasure (?A \<Otimes>\<^sub>M ?B) A = emeasure (count_space UNIV) A"

   756       by simp }

   757   note * = this

   758

   759   show ?thesis

   760   proof cases

   761     assume "finite A" then show ?thesis

   762       by (intro * countable_finite)

   763   next

   764     assume "infinite A"

   765     then obtain C where "countable C" and "infinite C" and "C \<subseteq> A"

   766       by (auto dest: infinite_countable_subset')

   767     with A have "emeasure (?A \<Otimes>\<^sub>M ?B) C \<le> emeasure (?A \<Otimes>\<^sub>M ?B) A"

   768       by (intro emeasure_mono) auto

   769     also have "emeasure (?A \<Otimes>\<^sub>M ?B) C = emeasure (count_space UNIV) C"

   770       using \<open>countable C\<close> by (rule *)

   771     finally show ?thesis

   772       using \<open>infinite C\<close> \<open>infinite A\<close> by (simp add: top_unique)

   773   qed

   774 qed

   775

   776 lemma nn_integral_count_space_prod_eq:

   777   "nn_integral (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) f = nn_integral (count_space UNIV) f"

   778     (is "nn_integral ?P f = _")

   779 proof cases

   780   assume cntbl: "countable {x. f x \<noteq> 0}"

   781   have [simp]: "\<And>x. card ({x} \<inter> {x. f x \<noteq> 0}) = (indicator {x. f x \<noteq> 0} x::ennreal)"

   782     by (auto split: split_indicator)

   783   have [measurable]: "\<And>y. (\<lambda>x. indicator {y} x) \<in> borel_measurable ?P"

   784     by (rule measurable_discrete_difference[of "\<lambda>x. 0" _ borel "{y}" "\<lambda>x. indicator {y} x" for y])

   785        (auto intro: sets_Pair)

   786

   787   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)"

   788     by (auto simp add: nn_integral_cmult nn_integral_indicator' intro!: nn_integral_cong split: split_indicator)

   789   also have "\<dots> = (\<integral>\<^sup>+x. \<integral>\<^sup>+x'. f x' * indicator {x'} x \<partial>count_space {x. f x \<noteq> 0} \<partial>?P)"

   790     by (auto intro!: nn_integral_cong split: split_indicator)

   791   also have "\<dots> = (\<integral>\<^sup>+x'. \<integral>\<^sup>+x. f x' * indicator {x'} x \<partial>?P \<partial>count_space {x. f x \<noteq> 0})"

   792     by (intro nn_integral_count_space_nn_integral cntbl) auto

   793   also have "\<dots> = (\<integral>\<^sup>+x'. f x' \<partial>count_space {x. f x \<noteq> 0})"

   794     by (intro nn_integral_cong) (auto simp: nn_integral_cmult sets_Pair)

   795   finally show ?thesis

   796     by (auto simp add: nn_integral_count_space_indicator intro!: nn_integral_cong split: split_indicator)

   797 next

   798   { fix x assume "f x \<noteq> 0"

   799     then have "(\<exists>r\<ge>0. 0 < r \<and> f x = ennreal r) \<or> f x = \<infinity>"

   800       by (cases "f x" rule: ennreal_cases) (auto simp: less_le)

   801     then have "\<exists>n. ennreal (1 / real (Suc n)) \<le> f x"

   802       by (auto elim!: nat_approx_posE intro!: less_imp_le) }

   803   note * = this

   804

   805   assume cntbl: "uncountable {x. f x \<noteq> 0}"

   806   also have "{x. f x \<noteq> 0} = (\<Union>n. {x. 1/Suc n \<le> f x})"

   807     using * by auto

   808   finally obtain n where "infinite {x. 1/Suc n \<le> f x}"

   809     by (meson countableI_type countable_UN uncountable_infinite)

   810   then obtain C where C: "C \<subseteq> {x. 1/Suc n \<le> f x}" and "countable C" "infinite C"

   811     by (metis infinite_countable_subset')

   812

   813   have [measurable]: "C \<in> sets ?P"

   814     using sets.countable[OF _ \<open>countable C\<close>, of ?P] by (auto simp: sets_Pair)

   815

   816   have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>?P) \<le> nn_integral ?P f"

   817     using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])

   818   moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>?P) = \<infinity>"

   819     using \<open>infinite C\<close> by (simp add: nn_integral_cmult emeasure_count_space_prod_eq ennreal_mult_top)

   820   moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>count_space UNIV) \<le> nn_integral (count_space UNIV) f"

   821     using C by (intro nn_integral_mono) (auto split: split_indicator simp: zero_ereal_def[symmetric])

   822   moreover have "(\<integral>\<^sup>+x. ennreal (1/Suc n) * indicator C x \<partial>count_space UNIV) = \<infinity>"

   823     using \<open>infinite C\<close> by (simp add: nn_integral_cmult ennreal_mult_top)

   824   ultimately show ?thesis

   825     by (simp add: top_unique)

   826 qed

   827

   828 lemma pair_measure_density:

   829   assumes f: "f \<in> borel_measurable M1"

   830   assumes g: "g \<in> borel_measurable M2"

   831   assumes "sigma_finite_measure M2" "sigma_finite_measure (density M2 g)"

   832   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")

   833 proof (rule measure_eqI)

   834   interpret M2: sigma_finite_measure M2 by fact

   835   interpret D2: sigma_finite_measure "density M2 g" by fact

   836

   837   fix A assume A: "A \<in> sets ?L"

   838   with f g have "(\<integral>\<^sup>+ x. f x * \<integral>\<^sup>+ y. g y * indicator A (x, y) \<partial>M2 \<partial>M1) =

   839     (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f x * g y * indicator A (x, y) \<partial>M2 \<partial>M1)"

   840     by (intro nn_integral_cong_AE)

   841        (auto simp add: nn_integral_cmult[symmetric] ac_simps)

   842   with A f g show "emeasure ?L A = emeasure ?R A"

   843     by (simp add: D2.emeasure_pair_measure emeasure_density nn_integral_density

   844                   M2.nn_integral_fst[symmetric]

   845              cong: nn_integral_cong)

   846 qed simp

   847

   848 lemma sigma_finite_measure_distr:

   849   assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"

   850   shows "sigma_finite_measure M"

   851 proof -

   852   interpret sigma_finite_measure "distr M N f" by fact

   853   from sigma_finite_countable guess A .. note A = this

   854   show ?thesis

   855   proof

   856     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>)"

   857       using A f

   858       by (intro exI[of _ "(\<lambda>a. f - a \<inter> space M)  A"])

   859          (auto simp: emeasure_distr set_eq_iff subset_eq intro: measurable_space)

   860   qed

   861 qed

   862

   863 lemma pair_measure_distr:

   864   assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"

   865   assumes "sigma_finite_measure (distr N T g)"

   866   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")

   867 proof (rule measure_eqI)

   868   interpret T: sigma_finite_measure "distr N T g" by fact

   869   interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+

   870

   871   fix A assume A: "A \<in> sets ?P"

   872   with f g show "emeasure ?P A = emeasure ?D A"

   873     by (auto simp add: N.emeasure_pair_measure_alt space_pair_measure emeasure_distr

   874                        T.emeasure_pair_measure_alt nn_integral_distr

   875              intro!: nn_integral_cong arg_cong[where f="emeasure N"])

   876 qed simp

   877

   878 lemma pair_measure_eqI:

   879   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"

   880   assumes sets: "sets (M1 \<Otimes>\<^sub>M M2) = sets M"

   881   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)"

   882   shows "M1 \<Otimes>\<^sub>M M2 = M"

   883 proof -

   884   interpret M1: sigma_finite_measure M1 by fact

   885   interpret M2: sigma_finite_measure M2 by fact

   886   interpret pair_sigma_finite M1 M2 ..

   887   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this

   888   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"

   889   let ?P = "M1 \<Otimes>\<^sub>M M2"

   890   show ?thesis

   891   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])

   892     show "?E \<subseteq> Pow (space ?P)"

   893       using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)

   894     show "sets ?P = sigma_sets (space ?P) ?E"

   895       by (simp add: sets_pair_measure space_pair_measure)

   896     then show "sets M = sigma_sets (space ?P) ?E"

   897       using sets[symmetric] by simp

   898   next

   899     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"

   900       using F by (auto simp: space_pair_measure)

   901   next

   902     fix X assume "X \<in> ?E"

   903     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

   904     then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"

   905        by (simp add: M2.emeasure_pair_measure_Times)

   906     also have "\<dots> = emeasure M (A \<times> B)"

   907       using A B emeasure by auto

   908     finally show "emeasure ?P X = emeasure M X"

   909       by simp

   910   qed

   911 qed

   912

   913 lemma sets_pair_countable:

   914   assumes "countable S1" "countable S2"

   915   assumes M: "sets M = Pow S1" and N: "sets N = Pow S2"

   916   shows "sets (M \<Otimes>\<^sub>M N) = Pow (S1 \<times> S2)"

   917 proof auto

   918   fix x a b assume x: "x \<in> sets (M \<Otimes>\<^sub>M N)" "(a, b) \<in> x"

   919   from sets.sets_into_space[OF x(1)] x(2)

   920     sets_eq_imp_space_eq[of N "count_space S2"] sets_eq_imp_space_eq[of M "count_space S1"] M N

   921   show "a \<in> S1" "b \<in> S2"

   922     by (auto simp: space_pair_measure)

   923 next

   924   fix X assume X: "X \<subseteq> S1 \<times> S2"

   925   then have "countable X"

   926     by (metis countable_subset \<open>countable S1\<close> \<open>countable S2\<close> countable_SIGMA)

   927   have "X = (\<Union>(a, b)\<in>X. {a} \<times> {b})" by auto

   928   also have "\<dots> \<in> sets (M \<Otimes>\<^sub>M N)"

   929     using X

   930     by (safe intro!: sets.countable_UN' \<open>countable X\<close> subsetI pair_measureI) (auto simp: M N)

   931   finally show "X \<in> sets (M \<Otimes>\<^sub>M N)" .

   932 qed

   933

   934 lemma pair_measure_countable:

   935   assumes "countable S1" "countable S2"

   936   shows "count_space S1 \<Otimes>\<^sub>M count_space S2 = count_space (S1 \<times> S2)"

   937 proof (rule pair_measure_eqI)

   938   show "sigma_finite_measure (count_space S1)" "sigma_finite_measure (count_space S2)"

   939     using assms by (auto intro!: sigma_finite_measure_count_space_countable)

   940   show "sets (count_space S1 \<Otimes>\<^sub>M count_space S2) = sets (count_space (S1 \<times> S2))"

   941     by (subst sets_pair_countable[OF assms]) auto

   942 next

   943   fix A B assume "A \<in> sets (count_space S1)" "B \<in> sets (count_space S2)"

   944   then show "emeasure (count_space S1) A * emeasure (count_space S2) B =

   945     emeasure (count_space (S1 \<times> S2)) (A \<times> B)"

   946     by (subst (1 2 3) emeasure_count_space) (auto simp: finite_cartesian_product_iff ennreal_mult_top ennreal_top_mult)

   947 qed

   948

   949 lemma nn_integral_fst_count_space:

   950   "(\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"

   951   (is "?lhs = ?rhs")

   952 proof(cases)

   953   assume *: "countable {xy. f xy \<noteq> 0}"

   954   let ?A = "fst  {xy. f xy \<noteq> 0}"

   955   let ?B = "snd  {xy. f xy \<noteq> 0}"

   956   from * have [simp]: "countable ?A" "countable ?B" by(rule countable_image)+

   957   have "?lhs = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space UNIV \<partial>count_space ?A)"

   958     by(rule nn_integral_count_space_eq)

   959       (auto simp add: nn_integral_0_iff_AE AE_count_space not_le intro: rev_image_eqI)

   960   also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f (x, y) \<partial>count_space ?B \<partial>count_space ?A)"

   961     by(intro nn_integral_count_space_eq nn_integral_cong)(auto intro: rev_image_eqI)

   962   also have "\<dots> = (\<integral>\<^sup>+ xy. f xy \<partial>count_space (?A \<times> ?B))"

   963     by(subst sigma_finite_measure.nn_integral_fst)

   964       (simp_all add: sigma_finite_measure_count_space_countable pair_measure_countable)

   965   also have "\<dots> = ?rhs"

   966     by(rule nn_integral_count_space_eq)(auto intro: rev_image_eqI)

   967   finally show ?thesis .

   968 next

   969   { fix xy assume "f xy \<noteq> 0"

   970     then have "(\<exists>r\<ge>0. 0 < r \<and> f xy = ennreal r) \<or> f xy = \<infinity>"

   971       by (cases "f xy" rule: ennreal_cases) (auto simp: less_le)

   972     then have "\<exists>n. ennreal (1 / real (Suc n)) \<le> f xy"

   973       by (auto elim!: nat_approx_posE intro!: less_imp_le) }

   974   note * = this

   975

   976   assume cntbl: "uncountable {xy. f xy \<noteq> 0}"

   977   also have "{xy. f xy \<noteq> 0} = (\<Union>n. {xy. 1/Suc n \<le> f xy})"

   978     using * by auto

   979   finally obtain n where "infinite {xy. 1/Suc n \<le> f xy}"

   980     by (meson countableI_type countable_UN uncountable_infinite)

   981   then obtain C where C: "C \<subseteq> {xy. 1/Suc n \<le> f xy}" and "countable C" "infinite C"

   982     by (metis infinite_countable_subset')

   983

   984   have "\<infinity> = (\<integral>\<^sup>+ xy. ennreal (1 / Suc n) * indicator C xy \<partial>count_space UNIV)"

   985     using \<open>infinite C\<close> by(simp add: nn_integral_cmult ennreal_mult_top)

   986   also have "\<dots> \<le> ?rhs" using C

   987     by(intro nn_integral_mono)(auto split: split_indicator)

   988   finally have "?rhs = \<infinity>" by (simp add: top_unique)

   989   moreover have "?lhs = \<infinity>"

   990   proof(cases "finite (fst  C)")

   991     case True

   992     then obtain x C' where x: "x \<in> fst  C"

   993       and C': "C' = fst - {x} \<inter> C"

   994       and "infinite C'"

   995       using \<open>infinite C\<close> by(auto elim!: inf_img_fin_domE')

   996     from x C C' have **: "C' \<subseteq> {xy. 1 / Suc n \<le> f xy}" by auto

   997

   998     from C' \<open>infinite C'\<close> have "infinite (snd  C')"

   999       by(auto dest!: finite_imageD simp add: inj_on_def)

  1000     then have "\<infinity> = (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator (snd  C') y \<partial>count_space UNIV)"

  1001       by(simp add: nn_integral_cmult ennreal_mult_top)

  1002     also have "\<dots> = (\<integral>\<^sup>+ y. ennreal (1 / Suc n) * indicator C' (x, y) \<partial>count_space UNIV)"

  1003       by(rule nn_integral_cong)(force split: split_indicator intro: rev_image_eqI simp add: C')

  1004     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)"

  1005       by(simp add: one_ereal_def[symmetric])

  1006     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)"

  1007       by(rule nn_integral_mono)(simp split: split_indicator)

  1008     also have "\<dots> \<le> ?lhs" using **

  1009       by(intro nn_integral_mono)(auto split: split_indicator)

  1010     finally show ?thesis by (simp add: top_unique)

  1011   next

  1012     case False

  1013     define C' where "C' = fst  C"

  1014     have "\<infinity> = \<integral>\<^sup>+ x. ennreal (1 / Suc n) * indicator C' x \<partial>count_space UNIV"

  1015       using C'_def False by(simp add: nn_integral_cmult ennreal_mult_top)

  1016     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"

  1017       by(auto simp add: one_ereal_def[symmetric] max_def intro: nn_integral_cong)

  1018     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"

  1019       by(intro nn_integral_mono)(auto simp add: C'_def split: split_indicator intro: someI)

  1020     also have "\<dots> \<le> ?lhs" using C

  1021       by(intro nn_integral_mono)(auto split: split_indicator)

  1022     finally show ?thesis by (simp add: top_unique)

  1023   qed

  1024   ultimately show ?thesis by simp

  1025 qed

  1026

  1027 lemma nn_integral_snd_count_space:

  1028   "(\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. f (x, y) \<partial>count_space UNIV \<partial>count_space UNIV) = integral\<^sup>N (count_space UNIV) f"

  1029   (is "?lhs = ?rhs")

  1030 proof -

  1031   have "?lhs = (\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. (\<lambda>(y, x). f (x, y)) (y, x) \<partial>count_space UNIV \<partial>count_space UNIV)"

  1032     by(simp)

  1033   also have "\<dots> = \<integral>\<^sup>+ yx. (\<lambda>(y, x). f (x, y)) yx \<partial>count_space UNIV"

  1034     by(rule nn_integral_fst_count_space)

  1035   also have "\<dots> = \<integral>\<^sup>+ xy. f xy \<partial>count_space ((\<lambda>(x, y). (y, x))  UNIV)"

  1036     by(subst nn_integral_bij_count_space[OF inj_on_imp_bij_betw, symmetric])

  1037       (simp_all add: inj_on_def split_def)

  1038   also have "\<dots> = ?rhs" by(rule nn_integral_count_space_eq) auto

  1039   finally show ?thesis .

  1040 qed

  1041

  1042 lemma measurable_pair_measure_countable1:

  1043   assumes "countable A"

  1044   and [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N K"

  1045   shows "f \<in> measurable (count_space A \<Otimes>\<^sub>M N) K"

  1046 using _ _ assms(1)

  1047 by(rule measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A, simplified])simp_all

  1048

  1049 subsection \<open>Product of Borel spaces\<close>

  1050

  1051 lemma borel_Times:

  1052   fixes A :: "'a::topological_space set" and B :: "'b::topological_space set"

  1053   assumes A: "A \<in> sets borel" and B: "B \<in> sets borel"

  1054   shows "A \<times> B \<in> sets borel"

  1055 proof -

  1056   have "A \<times> B = (A\<times>UNIV) \<inter> (UNIV \<times> B)"

  1057     by auto

  1058   moreover

  1059   { have "A \<in> sigma_sets UNIV {S. open S}" using A by (simp add: sets_borel)

  1060     then have "A\<times>UNIV \<in> sets borel"

  1061     proof (induct A)

  1062       case (Basic S) then show ?case

  1063         by (auto intro!: borel_open open_Times)

  1064     next

  1065       case (Compl A)

  1066       moreover have *: "(UNIV - A) \<times> UNIV = UNIV - (A \<times> UNIV)"

  1067         by auto

  1068       ultimately show ?case

  1069         unfolding * by auto

  1070     next

  1071       case (Union A)

  1072       moreover have *: "(UNION UNIV A) \<times> UNIV = UNION UNIV (\<lambda>i. A i \<times> UNIV)"

  1073         by auto

  1074       ultimately show ?case

  1075         unfolding * by auto

  1076     qed simp }

  1077   moreover

  1078   { have "B \<in> sigma_sets UNIV {S. open S}" using B by (simp add: sets_borel)

  1079     then have "UNIV\<times>B \<in> sets borel"

  1080     proof (induct B)

  1081       case (Basic S) then show ?case

  1082         by (auto intro!: borel_open open_Times)

  1083     next

  1084       case (Compl B)

  1085       moreover have *: "UNIV \<times> (UNIV - B) = UNIV - (UNIV \<times> B)"

  1086         by auto

  1087       ultimately show ?case

  1088         unfolding * by auto

  1089     next

  1090       case (Union B)

  1091       moreover have *: "UNIV \<times> (UNION UNIV B) = UNION UNIV (\<lambda>i. UNIV \<times> B i)"

  1092         by auto

  1093       ultimately show ?case

  1094         unfolding * by auto

  1095     qed simp }

  1096   ultimately show ?thesis

  1097     by auto

  1098 qed

  1099

  1100 lemma finite_measure_pair_measure:

  1101   assumes "finite_measure M" "finite_measure N"

  1102   shows "finite_measure (N  \<Otimes>\<^sub>M M)"

  1103 proof (rule finite_measureI)

  1104   interpret M: finite_measure M by fact

  1105   interpret N: finite_measure N by fact

  1106   show "emeasure (N  \<Otimes>\<^sub>M M) (space (N  \<Otimes>\<^sub>M M)) \<noteq> \<infinity>"

  1107     by (auto simp: space_pair_measure M.emeasure_pair_measure_Times ennreal_mult_eq_top_iff)

  1108 qed

  1109

  1110 end
`