src/HOL/Probability/Binary_Product_Measure.thy
author haftmann
Sat Jul 05 11:01:53 2014 +0200 (2014-07-05)
changeset 57514 bdc2c6b40bf2
parent 57447 87429bdecad5
child 58606 9c66f7c541fb
permissions -rw-r--r--
prefer ac_simps collections over separate name bindings for add and mult
     1 (*  Title:      HOL/Probability/Binary_Product_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 header {*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 sets_pair_measure:
    33   "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}"
    34   unfolding pair_measure_def using pair_measure_closed[of A B]
    35   by (rule sets_measure_of)
    36 
    37 lemma sets_pair_measure_cong[cong]:
    38   "sets M1 = sets M1' \<Longrightarrow> sets M2 = sets M2' \<Longrightarrow> sets (M1 \<Otimes>\<^sub>M M2) = sets (M1' \<Otimes>\<^sub>M M2')"
    39   unfolding sets_pair_measure by (simp cong: sets_eq_imp_space_eq)
    40 
    41 lemma pair_measureI[intro, simp, measurable]:
    42   "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (A \<Otimes>\<^sub>M B)"
    43   by (auto simp: sets_pair_measure)
    44 
    45 lemma measurable_pair_measureI:
    46   assumes 1: "f \<in> space M \<rightarrow> space M1 \<times> space M2"
    47   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"
    48   shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    49   unfolding pair_measure_def using 1 2
    50   by (intro measurable_measure_of) (auto dest: sets.sets_into_space)
    51 
    52 lemma measurable_split_replace[measurable (raw)]:
    53   "(\<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"
    54   unfolding split_beta' .
    55 
    56 lemma measurable_Pair[measurable (raw)]:
    57   assumes f: "f \<in> measurable M M1" and g: "g \<in> measurable M M2"
    58   shows "(\<lambda>x. (f x, g x)) \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    59 proof (rule measurable_pair_measureI)
    60   show "(\<lambda>x. (f x, g x)) \<in> space M \<rightarrow> space M1 \<times> space M2"
    61     using f g by (auto simp: measurable_def)
    62   fix A B assume *: "A \<in> sets M1" "B \<in> sets M2"
    63   have "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
    64     by auto
    65   also have "\<dots> \<in> sets M"
    66     by (rule sets.Int) (auto intro!: measurable_sets * f g)
    67   finally show "(\<lambda>x. (f x, g x)) -` (A \<times> B) \<inter> space M \<in> sets M" .
    68 qed
    69 
    70 lemma measurable_Pair_compose_split[measurable_dest]:
    71   assumes f: "split f \<in> measurable (M1 \<Otimes>\<^sub>M M2) N"
    72   assumes g: "g \<in> measurable M M1" and h: "h \<in> measurable M M2"
    73   shows "(\<lambda>x. f (g x) (h x)) \<in> measurable M N"
    74   using measurable_compose[OF measurable_Pair f, OF g h] by simp
    75 
    76 lemma measurable_pair:
    77   assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
    78   shows "f \<in> measurable M (M1 \<Otimes>\<^sub>M M2)"
    79   using measurable_Pair[OF assms] by simp
    80 
    81 lemma measurable_fst[intro!, simp, measurable]: "fst \<in> measurable (M1 \<Otimes>\<^sub>M M2) M1"
    82   by (auto simp: fst_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    83     measurable_def)
    84 
    85 lemma measurable_snd[intro!, simp, measurable]: "snd \<in> measurable (M1 \<Otimes>\<^sub>M M2) M2"
    86   by (auto simp: snd_vimage_eq_Times space_pair_measure sets.sets_into_space times_Int_times
    87     measurable_def)
    88 
    89 lemma 
    90   assumes f[measurable]: "f \<in> measurable M (N \<Otimes>\<^sub>M P)" 
    91   shows measurable_fst': "(\<lambda>x. fst (f x)) \<in> measurable M N"
    92     and measurable_snd': "(\<lambda>x. snd (f x)) \<in> measurable M P"
    93   by simp_all
    94 
    95 lemma
    96   assumes f[measurable]: "f \<in> measurable M N"
    97   shows measurable_fst'': "(\<lambda>x. f (fst x)) \<in> measurable (M \<Otimes>\<^sub>M P) N"
    98     and measurable_snd'': "(\<lambda>x. f (snd x)) \<in> measurable (P \<Otimes>\<^sub>M M) N"
    99   by simp_all
   100 
   101 lemma measurable_pair_iff:
   102   "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"
   103   by (auto intro: measurable_pair[of f M M1 M2]) 
   104 
   105 lemma measurable_split_conv:
   106   "(\<lambda>(x, y). f x y) \<in> measurable A B \<longleftrightarrow> (\<lambda>x. f (fst x) (snd x)) \<in> measurable A B"
   107   by (intro arg_cong2[where f="op \<in>"]) auto
   108 
   109 lemma measurable_pair_swap': "(\<lambda>(x,y). (y, x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (M2 \<Otimes>\<^sub>M M1)"
   110   by (auto intro!: measurable_Pair simp: measurable_split_conv)
   111 
   112 lemma measurable_pair_swap:
   113   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"
   114   using measurable_comp[OF measurable_Pair f] by (auto simp: measurable_split_conv comp_def)
   115 
   116 lemma measurable_pair_swap_iff:
   117   "f \<in> measurable (M2 \<Otimes>\<^sub>M M1) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable (M1 \<Otimes>\<^sub>M M2) M"
   118   by (auto dest: measurable_pair_swap)
   119 
   120 lemma measurable_Pair1': "x \<in> space M1 \<Longrightarrow> Pair x \<in> measurable M2 (M1 \<Otimes>\<^sub>M M2)"
   121   by simp
   122 
   123 lemma sets_Pair1[measurable (raw)]:
   124   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "Pair x -` A \<in> sets M2"
   125 proof -
   126   have "Pair x -` A = (if x \<in> space M1 then Pair x -` A \<inter> space M2 else {})"
   127     using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   128   also have "\<dots> \<in> sets M2"
   129     using A by (auto simp add: measurable_Pair1' intro!: measurable_sets split: split_if_asm)
   130   finally show ?thesis .
   131 qed
   132 
   133 lemma measurable_Pair2': "y \<in> space M2 \<Longrightarrow> (\<lambda>x. (x, y)) \<in> measurable M1 (M1 \<Otimes>\<^sub>M M2)"
   134   by (auto intro!: measurable_Pair)
   135 
   136 lemma sets_Pair2: assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>x. (x, y)) -` A \<in> sets M1"
   137 proof -
   138   have "(\<lambda>x. (x, y)) -` A = (if y \<in> space M2 then (\<lambda>x. (x, y)) -` A \<inter> space M1 else {})"
   139     using A[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   140   also have "\<dots> \<in> sets M1"
   141     using A by (auto simp add: measurable_Pair2' intro!: measurable_sets split: split_if_asm)
   142   finally show ?thesis .
   143 qed
   144 
   145 lemma measurable_Pair2:
   146   assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and x: "x \<in> space M1"
   147   shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   148   using measurable_comp[OF measurable_Pair1' f, OF x]
   149   by (simp add: comp_def)
   150   
   151 lemma measurable_Pair1:
   152   assumes f: "f \<in> measurable (M1 \<Otimes>\<^sub>M M2) M" and y: "y \<in> space M2"
   153   shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
   154   using measurable_comp[OF measurable_Pair2' f, OF y]
   155   by (simp add: comp_def)
   156 
   157 lemma Int_stable_pair_measure_generator: "Int_stable {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B}"
   158   unfolding Int_stable_def
   159   by safe (auto simp add: times_Int_times)
   160 
   161 lemma disjoint_family_vimageI: "disjoint_family F \<Longrightarrow> disjoint_family (\<lambda>i. f -` F i)"
   162   by (auto simp: disjoint_family_on_def)
   163 
   164 lemma (in finite_measure) finite_measure_cut_measurable:
   165   assumes [measurable]: "Q \<in> sets (N \<Otimes>\<^sub>M M)"
   166   shows "(\<lambda>x. emeasure M (Pair x -` Q)) \<in> borel_measurable N"
   167     (is "?s Q \<in> _")
   168   using Int_stable_pair_measure_generator pair_measure_closed assms
   169   unfolding sets_pair_measure
   170 proof (induct rule: sigma_sets_induct_disjoint)
   171   case (compl A)
   172   with sets.sets_into_space have "\<And>x. emeasure M (Pair x -` ((space N \<times> space M) - A)) =
   173       (if x \<in> space N then emeasure M (space M) - ?s A x else 0)"
   174     unfolding sets_pair_measure[symmetric]
   175     by (auto intro!: emeasure_compl simp: vimage_Diff sets_Pair1)
   176   with compl sets.top show ?case
   177     by (auto intro!: measurable_If simp: space_pair_measure)
   178 next
   179   case (union F)
   180   then have "\<And>x. emeasure M (Pair x -` (\<Union>i. F i)) = (\<Sum>i. ?s (F i) x)"
   181     by (simp add: suminf_emeasure disjoint_family_vimageI subset_eq vimage_UN sets_pair_measure[symmetric])
   182   with union show ?case
   183     unfolding sets_pair_measure[symmetric] by simp
   184 qed (auto simp add: if_distrib Int_def[symmetric] intro!: measurable_If)
   185 
   186 lemma (in sigma_finite_measure) measurable_emeasure_Pair:
   187   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> _")
   188 proof -
   189   from sigma_finite_disjoint guess F . note F = this
   190   then have F_sets: "\<And>i. F i \<in> sets M" by auto
   191   let ?C = "\<lambda>x i. F i \<inter> Pair x -` Q"
   192   { fix i
   193     have [simp]: "space N \<times> F i \<inter> space N \<times> space M = space N \<times> F i"
   194       using F sets.sets_into_space by auto
   195     let ?R = "density M (indicator (F i))"
   196     have "finite_measure ?R"
   197       using F by (intro finite_measureI) (auto simp: emeasure_restricted subset_eq)
   198     then have "(\<lambda>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))) \<in> borel_measurable N"
   199      by (rule finite_measure.finite_measure_cut_measurable) (auto intro: Q)
   200     moreover have "\<And>x. emeasure ?R (Pair x -` (space N \<times> space ?R \<inter> Q))
   201         = emeasure M (F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q))"
   202       using Q F_sets by (intro emeasure_restricted) (auto intro: sets_Pair1)
   203     moreover have "\<And>x. F i \<inter> Pair x -` (space N \<times> space ?R \<inter> Q) = ?C x i"
   204       using sets.sets_into_space[OF Q] by (auto simp: space_pair_measure)
   205     ultimately have "(\<lambda>x. emeasure M (?C x i)) \<in> borel_measurable N"
   206       by simp }
   207   moreover
   208   { fix x
   209     have "(\<Sum>i. emeasure M (?C x i)) = emeasure M (\<Union>i. ?C x i)"
   210     proof (intro suminf_emeasure)
   211       show "range (?C x) \<subseteq> sets M"
   212         using F `Q \<in> sets (N \<Otimes>\<^sub>M M)` by (auto intro!: sets_Pair1)
   213       have "disjoint_family F" using F by auto
   214       show "disjoint_family (?C x)"
   215         by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
   216     qed
   217     also have "(\<Union>i. ?C x i) = Pair x -` Q"
   218       using F sets.sets_into_space[OF `Q \<in> sets (N \<Otimes>\<^sub>M M)`]
   219       by (auto simp: space_pair_measure)
   220     finally have "emeasure M (Pair x -` Q) = (\<Sum>i. emeasure M (?C x i))"
   221       by simp }
   222   ultimately show ?thesis using `Q \<in> sets (N \<Otimes>\<^sub>M M)` F_sets
   223     by auto
   224 qed
   225 
   226 lemma (in sigma_finite_measure) measurable_emeasure[measurable (raw)]:
   227   assumes space: "\<And>x. x \<in> space N \<Longrightarrow> A x \<subseteq> space M"
   228   assumes A: "{x\<in>space (N \<Otimes>\<^sub>M M). snd x \<in> A (fst x)} \<in> sets (N \<Otimes>\<^sub>M M)"
   229   shows "(\<lambda>x. emeasure M (A x)) \<in> borel_measurable N"
   230 proof -
   231   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"
   232     by (auto simp: space_pair_measure)
   233   with measurable_emeasure_Pair[OF A] show ?thesis
   234     by (auto cong: measurable_cong)
   235 qed
   236 
   237 lemma (in sigma_finite_measure) emeasure_pair_measure:
   238   assumes "X \<in> sets (N \<Otimes>\<^sub>M M)"
   239   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")
   240 proof (rule emeasure_measure_of[OF pair_measure_def])
   241   show "positive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   242     by (auto simp: positive_def nn_integral_nonneg)
   243   have eq[simp]: "\<And>A x y. indicator A (x, y) = indicator (Pair x -` A) y"
   244     by (auto simp: indicator_def)
   245   show "countably_additive (sets (N \<Otimes>\<^sub>M M)) ?\<mu>"
   246   proof (rule countably_additiveI)
   247     fix F :: "nat \<Rightarrow> ('b \<times> 'a) set" assume F: "range F \<subseteq> sets (N \<Otimes>\<^sub>M M)" "disjoint_family F"
   248     from F have *: "\<And>i. F i \<in> sets (N \<Otimes>\<^sub>M M)" "(\<Union>i. F i) \<in> sets (N \<Otimes>\<^sub>M M)" by auto
   249     moreover from F have "\<And>i. (\<lambda>x. emeasure M (Pair x -` F i)) \<in> borel_measurable N"
   250       by (intro measurable_emeasure_Pair) auto
   251     moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   252       by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   253     moreover have "\<And>x. range (\<lambda>i. Pair x -` F i) \<subseteq> sets M"
   254       using F by (auto simp: sets_Pair1)
   255     ultimately show "(\<Sum>n. ?\<mu> (F n)) = ?\<mu> (\<Union>i. F i)"
   256       by (auto simp add: vimage_UN nn_integral_suminf[symmetric] suminf_emeasure subset_eq emeasure_nonneg sets_Pair1
   257                intro!: nn_integral_cong nn_integral_indicator[symmetric])
   258   qed
   259   show "{a \<times> b |a b. a \<in> sets N \<and> b \<in> sets M} \<subseteq> Pow (space N \<times> space M)"
   260     using sets.space_closed[of N] sets.space_closed[of M] by auto
   261 qed fact
   262 
   263 lemma (in sigma_finite_measure) emeasure_pair_measure_alt:
   264   assumes X: "X \<in> sets (N \<Otimes>\<^sub>M M)"
   265   shows "emeasure (N  \<Otimes>\<^sub>M M) X = (\<integral>\<^sup>+x. emeasure M (Pair x -` X) \<partial>N)"
   266 proof -
   267   have [simp]: "\<And>x y. indicator X (x, y) = indicator (Pair x -` X) y"
   268     by (auto simp: indicator_def)
   269   show ?thesis
   270     using X by (auto intro!: nn_integral_cong simp: emeasure_pair_measure sets_Pair1)
   271 qed
   272 
   273 lemma (in sigma_finite_measure) emeasure_pair_measure_Times:
   274   assumes A: "A \<in> sets N" and B: "B \<in> sets M"
   275   shows "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = emeasure N A * emeasure M B"
   276 proof -
   277   have "emeasure (N \<Otimes>\<^sub>M M) (A \<times> B) = (\<integral>\<^sup>+x. emeasure M B * indicator A x \<partial>N)"
   278     using A B by (auto intro!: nn_integral_cong simp: emeasure_pair_measure_alt)
   279   also have "\<dots> = emeasure M B * emeasure N A"
   280     using A by (simp add: emeasure_nonneg nn_integral_cmult_indicator)
   281   finally show ?thesis
   282     by (simp add: ac_simps)
   283 qed
   284 
   285 subsection {* Binary products of $\sigma$-finite emeasure spaces *}
   286 
   287 locale pair_sigma_finite = M1: sigma_finite_measure M1 + M2: sigma_finite_measure M2
   288   for M1 :: "'a measure" and M2 :: "'b measure"
   289 
   290 lemma (in pair_sigma_finite) measurable_emeasure_Pair1:
   291   "Q \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow> (\<lambda>x. emeasure M2 (Pair x -` Q)) \<in> borel_measurable M1"
   292   using M2.measurable_emeasure_Pair .
   293 
   294 lemma (in pair_sigma_finite) measurable_emeasure_Pair2:
   295   assumes Q: "Q \<in> sets (M1 \<Otimes>\<^sub>M M2)" shows "(\<lambda>y. emeasure M1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   296 proof -
   297   have "(\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   298     using Q measurable_pair_swap' by (auto intro: measurable_sets)
   299   note M1.measurable_emeasure_Pair[OF this]
   300   moreover have "\<And>y. Pair y -` ((\<lambda>(x, y). (y, x)) -` Q \<inter> space (M2 \<Otimes>\<^sub>M M1)) = (\<lambda>x. (x, y)) -` Q"
   301     using Q[THEN sets.sets_into_space] by (auto simp: space_pair_measure)
   302   ultimately show ?thesis by simp
   303 qed
   304 
   305 lemma (in pair_sigma_finite) sigma_finite_up_in_pair_measure_generator:
   306   defines "E \<equiv> {A \<times> B | A B. A \<in> sets M1 \<and> B \<in> sets M2}"
   307   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>
   308     (\<forall>i. emeasure (M1 \<Otimes>\<^sub>M M2) (F i) \<noteq> \<infinity>)"
   309 proof -
   310   from M1.sigma_finite_incseq guess F1 . note F1 = this
   311   from M2.sigma_finite_incseq guess F2 . note F2 = this
   312   from F1 F2 have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)" by auto
   313   let ?F = "\<lambda>i. F1 i \<times> F2 i"
   314   show ?thesis
   315   proof (intro exI[of _ ?F] conjI allI)
   316     show "range ?F \<subseteq> E" using F1 F2 by (auto simp: E_def) (metis range_subsetD)
   317   next
   318     have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   319     proof (intro subsetI)
   320       fix x assume "x \<in> space M1 \<times> space M2"
   321       then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"
   322         by (auto simp: space)
   323       then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"
   324         using `incseq F1` `incseq F2` unfolding incseq_def
   325         by (force split: split_max)+
   326       then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"
   327         by (intro SigmaI) (auto simp add: max.commute)
   328       then show "x \<in> (\<Union>i. ?F i)" by auto
   329     qed
   330     then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   331       using space by (auto simp: space)
   332   next
   333     fix i show "incseq (\<lambda>i. F1 i \<times> F2 i)"
   334       using `incseq F1` `incseq F2` unfolding incseq_Suc_iff by auto
   335   next
   336     fix i
   337     from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   338     with F1 F2 emeasure_nonneg[of M1 "F1 i"] emeasure_nonneg[of M2 "F2 i"]
   339     show "emeasure (M1 \<Otimes>\<^sub>M M2) (F1 i \<times> F2 i) \<noteq> \<infinity>"
   340       by (auto simp add: emeasure_pair_measure_Times)
   341   qed
   342 qed
   343 
   344 sublocale pair_sigma_finite \<subseteq> P: sigma_finite_measure "M1 \<Otimes>\<^sub>M M2"
   345 proof
   346   from M1.sigma_finite_countable guess F1 ..
   347   moreover from M2.sigma_finite_countable guess F2 ..
   348   ultimately show
   349     "\<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>)"
   350     by (intro exI[of _ "(\<lambda>(a, b). a \<times> b) ` (F1 \<times> F2)"] conjI)
   351        (auto simp: M2.emeasure_pair_measure_Times space_pair_measure set_eq_iff subset_eq
   352              dest: sets.sets_into_space)
   353 qed
   354 
   355 lemma sigma_finite_pair_measure:
   356   assumes A: "sigma_finite_measure A" and B: "sigma_finite_measure B"
   357   shows "sigma_finite_measure (A \<Otimes>\<^sub>M B)"
   358 proof -
   359   interpret A: sigma_finite_measure A by fact
   360   interpret B: sigma_finite_measure B by fact
   361   interpret AB: pair_sigma_finite A  B ..
   362   show ?thesis ..
   363 qed
   364 
   365 lemma sets_pair_swap:
   366   assumes "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   367   shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1) \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   368   using measurable_pair_swap' assms by (rule measurable_sets)
   369 
   370 lemma (in pair_sigma_finite) distr_pair_swap:
   371   "M1 \<Otimes>\<^sub>M M2 = distr (M2 \<Otimes>\<^sub>M M1) (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). (y, x))" (is "?P = ?D")
   372 proof -
   373   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   374   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   375   show ?thesis
   376   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   377     show "?E \<subseteq> Pow (space ?P)"
   378       using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   379     show "sets ?P = sigma_sets (space ?P) ?E"
   380       by (simp add: sets_pair_measure space_pair_measure)
   381     then show "sets ?D = sigma_sets (space ?P) ?E"
   382       by simp
   383   next
   384     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   385       using F by (auto simp: space_pair_measure)
   386   next
   387     fix X assume "X \<in> ?E"
   388     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
   389     have "(\<lambda>(y, x). (x, y)) -` X \<inter> space (M2 \<Otimes>\<^sub>M M1) = B \<times> A"
   390       using sets.sets_into_space[OF A] sets.sets_into_space[OF B] by (auto simp: space_pair_measure)
   391     with A B show "emeasure (M1 \<Otimes>\<^sub>M M2) X = emeasure ?D X"
   392       by (simp add: M2.emeasure_pair_measure_Times M1.emeasure_pair_measure_Times emeasure_distr
   393                     measurable_pair_swap' ac_simps)
   394   qed
   395 qed
   396 
   397 lemma (in pair_sigma_finite) emeasure_pair_measure_alt2:
   398   assumes A: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   399   shows "emeasure (M1 \<Otimes>\<^sub>M M2) A = (\<integral>\<^sup>+y. emeasure M1 ((\<lambda>x. (x, y)) -` A) \<partial>M2)"
   400     (is "_ = ?\<nu> A")
   401 proof -
   402   have [simp]: "\<And>y. (Pair y -` ((\<lambda>(x, y). (y, x)) -` A \<inter> space (M2 \<Otimes>\<^sub>M M1))) = (\<lambda>x. (x, y)) -` A"
   403     using sets.sets_into_space[OF A] by (auto simp: space_pair_measure)
   404   show ?thesis using A
   405     by (subst distr_pair_swap)
   406        (simp_all del: vimage_Int add: measurable_sets[OF measurable_pair_swap']
   407                  M1.emeasure_pair_measure_alt emeasure_distr[OF measurable_pair_swap' A])
   408 qed
   409 
   410 lemma (in pair_sigma_finite) AE_pair:
   411   assumes "AE x in (M1 \<Otimes>\<^sub>M M2). Q x"
   412   shows "AE x in M1. (AE y in M2. Q (x, y))"
   413 proof -
   414   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"
   415     using assms unfolding eventually_ae_filter by auto
   416   show ?thesis
   417   proof (rule AE_I)
   418     from N measurable_emeasure_Pair1[OF `N \<in> sets (M1 \<Otimes>\<^sub>M M2)`]
   419     show "emeasure M1 {x\<in>space M1. emeasure M2 (Pair x -` N) \<noteq> 0} = 0"
   420       by (auto simp: M2.emeasure_pair_measure_alt nn_integral_0_iff emeasure_nonneg)
   421     show "{x \<in> space M1. emeasure M2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   422       by (intro borel_measurable_ereal_neq_const measurable_emeasure_Pair1 N)
   423     { fix x assume "x \<in> space M1" "emeasure M2 (Pair x -` N) = 0"
   424       have "AE y in M2. Q (x, y)"
   425       proof (rule AE_I)
   426         show "emeasure M2 (Pair x -` N) = 0" by fact
   427         show "Pair x -` N \<in> sets M2" using N(1) by (rule sets_Pair1)
   428         show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
   429           using N `x \<in> space M1` unfolding space_pair_measure by auto
   430       qed }
   431     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}"
   432       by auto
   433   qed
   434 qed
   435 
   436 lemma (in pair_sigma_finite) AE_pair_measure:
   437   assumes "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   438   assumes ae: "AE x in M1. AE y in M2. P (x, y)"
   439   shows "AE x in M1 \<Otimes>\<^sub>M M2. P x"
   440 proof (subst AE_iff_measurable[OF _ refl])
   441   show "{x\<in>space (M1 \<Otimes>\<^sub>M M2). \<not> P x} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   442     by (rule sets.sets_Collect) fact
   443   then have "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} =
   444       (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. indicator {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} (x, y) \<partial>M2 \<partial>M1)"
   445     by (simp add: M2.emeasure_pair_measure)
   446   also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. 0 \<partial>M2 \<partial>M1)"
   447     using ae
   448     apply (safe intro!: nn_integral_cong_AE)
   449     apply (intro AE_I2)
   450     apply (safe intro!: nn_integral_cong_AE)
   451     apply auto
   452     done
   453   finally show "emeasure (M1 \<Otimes>\<^sub>M M2) {x \<in> space (M1 \<Otimes>\<^sub>M M2). \<not> P x} = 0" by simp
   454 qed
   455 
   456 lemma (in pair_sigma_finite) AE_pair_iff:
   457   "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2) \<Longrightarrow>
   458     (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))"
   459   using AE_pair[of "\<lambda>x. P (fst x) (snd x)"] AE_pair_measure[of "\<lambda>x. P (fst x) (snd x)"] by auto
   460 
   461 lemma (in pair_sigma_finite) AE_commute:
   462   assumes P: "{x\<in>space (M1 \<Otimes>\<^sub>M M2). P (fst x) (snd x)} \<in> sets (M1 \<Otimes>\<^sub>M M2)"
   463   shows "(AE x in M1. AE y in M2. P x y) \<longleftrightarrow> (AE y in M2. AE x in M1. P x y)"
   464 proof -
   465   interpret Q: pair_sigma_finite M2 M1 ..
   466   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"
   467     by auto
   468   have "{x \<in> space (M2 \<Otimes>\<^sub>M M1). P (snd x) (fst x)} =
   469     (\<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)"
   470     by (auto simp: space_pair_measure)
   471   also have "\<dots> \<in> sets (M2 \<Otimes>\<^sub>M M1)"
   472     by (intro sets_pair_swap P)
   473   finally show ?thesis
   474     apply (subst AE_pair_iff[OF P])
   475     apply (subst distr_pair_swap)
   476     apply (subst AE_distr_iff[OF measurable_pair_swap' P])
   477     apply (subst Q.AE_pair_iff)
   478     apply simp_all
   479     done
   480 qed
   481 
   482 subsection "Fubinis theorem"
   483 
   484 lemma measurable_compose_Pair1:
   485   "x \<in> space M1 \<Longrightarrow> g \<in> measurable (M1 \<Otimes>\<^sub>M M2) L \<Longrightarrow> (\<lambda>y. g (x, y)) \<in> measurable M2 L"
   486   by simp
   487 
   488 lemma (in sigma_finite_measure) borel_measurable_nn_integral_fst':
   489   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)" "\<And>x. 0 \<le> f x"
   490   shows "(\<lambda>x. \<integral>\<^sup>+ y. f (x, y) \<partial>M) \<in> borel_measurable M1"
   491 using f proof induct
   492   case (cong u v)
   493   then have "\<And>w x. w \<in> space M1 \<Longrightarrow> x \<in> space M \<Longrightarrow> u (w, x) = v (w, x)"
   494     by (auto simp: space_pair_measure)
   495   show ?case
   496     apply (subst measurable_cong)
   497     apply (rule nn_integral_cong)
   498     apply fact+
   499     done
   500 next
   501   case (set Q)
   502   have [simp]: "\<And>x y. indicator Q (x, y) = indicator (Pair x -` Q) y"
   503     by (auto simp: indicator_def)
   504   have "\<And>x. x \<in> space M1 \<Longrightarrow> emeasure M (Pair x -` Q) = \<integral>\<^sup>+ y. indicator Q (x, y) \<partial>M"
   505     by (simp add: sets_Pair1[OF set])
   506   from this measurable_emeasure_Pair[OF set] show ?case
   507     by (rule measurable_cong[THEN iffD1])
   508 qed (simp_all add: nn_integral_add nn_integral_cmult measurable_compose_Pair1
   509                    nn_integral_monotone_convergence_SUP incseq_def le_fun_def
   510               cong: measurable_cong)
   511 
   512 lemma (in sigma_finite_measure) nn_integral_fst':
   513   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)" "\<And>x. 0 \<le> f x"
   514   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 = _")
   515 using f proof induct
   516   case (cong u v)
   517   then have "?I u = ?I v"
   518     by (intro nn_integral_cong) (auto simp: space_pair_measure)
   519   with cong show ?case
   520     by (simp cong: nn_integral_cong)
   521 qed (simp_all add: emeasure_pair_measure nn_integral_cmult nn_integral_add
   522                    nn_integral_monotone_convergence_SUP
   523                    measurable_compose_Pair1 nn_integral_nonneg
   524                    borel_measurable_nn_integral_fst' nn_integral_mono incseq_def le_fun_def
   525               cong: nn_integral_cong)
   526 
   527 lemma (in sigma_finite_measure) nn_integral_fst:
   528   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M)"
   529   shows "(\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x, y) \<partial>M) \<partial>M1) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M) f"
   530   using f
   531     borel_measurable_nn_integral_fst'[of "\<lambda>x. max 0 (f x)"]
   532     nn_integral_fst'[of "\<lambda>x. max 0 (f x)"]
   533   unfolding nn_integral_max_0 by auto
   534 
   535 lemma (in sigma_finite_measure) borel_measurable_nn_integral[measurable (raw)]:
   536   "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"
   537   using borel_measurable_nn_integral_fst'[of "\<lambda>x. max 0 (split f x)" N]
   538   by (simp add: nn_integral_max_0)
   539 
   540 lemma (in pair_sigma_finite) nn_integral_snd:
   541   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   542   shows "(\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x, y) \<partial>M1) \<partial>M2) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   543 proof -
   544   note measurable_pair_swap[OF f]
   545   from M1.nn_integral_fst[OF this]
   546   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))"
   547     by simp
   548   also have "(\<integral>\<^sup>+ (x, y). f (y, x) \<partial>(M2 \<Otimes>\<^sub>M M1)) = integral\<^sup>N (M1 \<Otimes>\<^sub>M M2) f"
   549     by (subst distr_pair_swap)
   550        (auto simp: nn_integral_distr[OF measurable_pair_swap' f] intro!: nn_integral_cong)
   551   finally show ?thesis .
   552 qed
   553 
   554 lemma (in pair_sigma_finite) Fubini:
   555   assumes f: "f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   556   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)"
   557   unfolding nn_integral_snd[OF assms] M2.nn_integral_fst[OF assms] ..
   558 
   559 lemma (in pair_sigma_finite) Fubini':
   560   assumes f: "split f \<in> borel_measurable (M1 \<Otimes>\<^sub>M M2)"
   561   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)"
   562   using Fubini[OF f] by simp
   563 
   564 subsection {* Products on counting spaces, densities and distributions *}
   565 
   566 lemma sigma_sets_pair_measure_generator_finite:
   567   assumes "finite A" and "finite B"
   568   shows "sigma_sets (A \<times> B) { a \<times> b | a b. a \<subseteq> A \<and> b \<subseteq> B} = Pow (A \<times> B)"
   569   (is "sigma_sets ?prod ?sets = _")
   570 proof safe
   571   have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
   572   fix x assume subset: "x \<subseteq> A \<times> B"
   573   hence "finite x" using fin by (rule finite_subset)
   574   from this subset show "x \<in> sigma_sets ?prod ?sets"
   575   proof (induct x)
   576     case empty show ?case by (rule sigma_sets.Empty)
   577   next
   578     case (insert a x)
   579     hence "{a} \<in> sigma_sets ?prod ?sets" by auto
   580     moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
   581     ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
   582   qed
   583 next
   584   fix x a b
   585   assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"
   586   from sigma_sets_into_sp[OF _ this(1)] this(2)
   587   show "a \<in> A" and "b \<in> B" by auto
   588 qed
   589 
   590 lemma pair_measure_count_space:
   591   assumes A: "finite A" and B: "finite B"
   592   shows "count_space A \<Otimes>\<^sub>M count_space B = count_space (A \<times> B)" (is "?P = ?C")
   593 proof (rule measure_eqI)
   594   interpret A: finite_measure "count_space A" by (rule finite_measure_count_space) fact
   595   interpret B: finite_measure "count_space B" by (rule finite_measure_count_space) fact
   596   interpret P: pair_sigma_finite "count_space A" "count_space B" by default
   597   show eq: "sets ?P = sets ?C"
   598     by (simp add: sets_pair_measure sigma_sets_pair_measure_generator_finite A B)
   599   fix X assume X: "X \<in> sets ?P"
   600   with eq have X_subset: "X \<subseteq> A \<times> B" by simp
   601   with A B have fin_Pair: "\<And>x. finite (Pair x -` X)"
   602     by (intro finite_subset[OF _ B]) auto
   603   have fin_X: "finite X" using X_subset by (rule finite_subset) (auto simp: A B)
   604   show "emeasure ?P X = emeasure ?C X"
   605     apply (subst B.emeasure_pair_measure_alt[OF X])
   606     apply (subst emeasure_count_space)
   607     using X_subset apply auto []
   608     apply (simp add: fin_Pair emeasure_count_space X_subset fin_X)
   609     apply (subst nn_integral_count_space)
   610     using A apply simp
   611     apply (simp del: real_of_nat_setsum add: real_of_nat_setsum[symmetric])
   612     apply (subst card_gt_0_iff)
   613     apply (simp add: fin_Pair)
   614     apply (subst card_SigmaI[symmetric])
   615     using A apply simp
   616     using fin_Pair apply simp
   617     using X_subset apply (auto intro!: arg_cong[where f=card])
   618     done
   619 qed
   620 
   621 lemma pair_measure_density:
   622   assumes f: "f \<in> borel_measurable M1" "AE x in M1. 0 \<le> f x"
   623   assumes g: "g \<in> borel_measurable M2" "AE x in M2. 0 \<le> g x"
   624   assumes "sigma_finite_measure M2" "sigma_finite_measure (density M2 g)"
   625   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")
   626 proof (rule measure_eqI)
   627   interpret M2: sigma_finite_measure M2 by fact
   628   interpret D2: sigma_finite_measure "density M2 g" by fact
   629 
   630   fix A assume A: "A \<in> sets ?L"
   631   with f g have "(\<integral>\<^sup>+ x. f x * \<integral>\<^sup>+ y. g y * indicator A (x, y) \<partial>M2 \<partial>M1) =
   632     (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f x * g y * indicator A (x, y) \<partial>M2 \<partial>M1)"
   633     by (intro nn_integral_cong_AE)
   634        (auto simp add: nn_integral_cmult[symmetric] ac_simps)
   635   with A f g show "emeasure ?L A = emeasure ?R A"
   636     by (simp add: D2.emeasure_pair_measure emeasure_density nn_integral_density
   637                   M2.nn_integral_fst[symmetric]
   638              cong: nn_integral_cong)
   639 qed simp
   640 
   641 lemma sigma_finite_measure_distr:
   642   assumes "sigma_finite_measure (distr M N f)" and f: "f \<in> measurable M N"
   643   shows "sigma_finite_measure M"
   644 proof -
   645   interpret sigma_finite_measure "distr M N f" by fact
   646   from sigma_finite_countable guess A .. note A = this
   647   show ?thesis
   648   proof
   649     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>)"
   650       using A f
   651       by (intro exI[of _ "(\<lambda>a. f -` a \<inter> space M) ` A"])
   652          (auto simp: emeasure_distr set_eq_iff subset_eq intro: measurable_space)
   653   qed
   654 qed
   655 
   656 lemma pair_measure_distr:
   657   assumes f: "f \<in> measurable M S" and g: "g \<in> measurable N T"
   658   assumes "sigma_finite_measure (distr N T g)"
   659   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")
   660 proof (rule measure_eqI)
   661   interpret T: sigma_finite_measure "distr N T g" by fact
   662   interpret N: sigma_finite_measure N by (rule sigma_finite_measure_distr) fact+
   663 
   664   fix A assume A: "A \<in> sets ?P"
   665   with f g show "emeasure ?P A = emeasure ?D A"
   666     by (auto simp add: N.emeasure_pair_measure_alt space_pair_measure emeasure_distr
   667                        T.emeasure_pair_measure_alt nn_integral_distr
   668              intro!: nn_integral_cong arg_cong[where f="emeasure N"])
   669 qed simp
   670 
   671 lemma pair_measure_eqI:
   672   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
   673   assumes sets: "sets (M1 \<Otimes>\<^sub>M M2) = sets M"
   674   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)"
   675   shows "M1 \<Otimes>\<^sub>M M2 = M"
   676 proof -
   677   interpret M1: sigma_finite_measure M1 by fact
   678   interpret M2: sigma_finite_measure M2 by fact
   679   interpret pair_sigma_finite M1 M2 by default
   680   from sigma_finite_up_in_pair_measure_generator guess F :: "nat \<Rightarrow> ('a \<times> 'b) set" .. note F = this
   681   let ?E = "{a \<times> b |a b. a \<in> sets M1 \<and> b \<in> sets M2}"
   682   let ?P = "M1 \<Otimes>\<^sub>M M2"
   683   show ?thesis
   684   proof (rule measure_eqI_generator_eq[OF Int_stable_pair_measure_generator[of M1 M2]])
   685     show "?E \<subseteq> Pow (space ?P)"
   686       using sets.space_closed[of M1] sets.space_closed[of M2] by (auto simp: space_pair_measure)
   687     show "sets ?P = sigma_sets (space ?P) ?E"
   688       by (simp add: sets_pair_measure space_pair_measure)
   689     then show "sets M = sigma_sets (space ?P) ?E"
   690       using sets[symmetric] by simp
   691   next
   692     show "range F \<subseteq> ?E" "(\<Union>i. F i) = space ?P" "\<And>i. emeasure ?P (F i) \<noteq> \<infinity>"
   693       using F by (auto simp: space_pair_measure)
   694   next
   695     fix X assume "X \<in> ?E"
   696     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
   697     then have "emeasure ?P X = emeasure M1 A * emeasure M2 B"
   698        by (simp add: M2.emeasure_pair_measure_Times)
   699     also have "\<dots> = emeasure M (A \<times> B)"
   700       using A B emeasure by auto
   701     finally show "emeasure ?P X = emeasure M X"
   702       by simp
   703   qed
   704 qed
   705   
   706 lemma sets_pair_countable:
   707   assumes "countable S1" "countable S2"
   708   assumes M: "sets M = Pow S1" and N: "sets N = Pow S2"
   709   shows "sets (M \<Otimes>\<^sub>M N) = Pow (S1 \<times> S2)"
   710 proof auto
   711   fix x a b assume x: "x \<in> sets (M \<Otimes>\<^sub>M N)" "(a, b) \<in> x"
   712   from sets.sets_into_space[OF x(1)] x(2)
   713     sets_eq_imp_space_eq[of N "count_space S2"] sets_eq_imp_space_eq[of M "count_space S1"] M N
   714   show "a \<in> S1" "b \<in> S2"
   715     by (auto simp: space_pair_measure)
   716 next
   717   fix X assume X: "X \<subseteq> S1 \<times> S2"
   718   then have "countable X"
   719     by (metis countable_subset `countable S1` `countable S2` countable_SIGMA)
   720   have "X = (\<Union>(a, b)\<in>X. {a} \<times> {b})" by auto
   721   also have "\<dots> \<in> sets (M \<Otimes>\<^sub>M N)"
   722     using X
   723     by (safe intro!: sets.countable_UN' `countable X` subsetI pair_measureI) (auto simp: M N)
   724   finally show "X \<in> sets (M \<Otimes>\<^sub>M N)" .
   725 qed
   726 
   727 lemma pair_measure_countable:
   728   assumes "countable S1" "countable S2"
   729   shows "count_space S1 \<Otimes>\<^sub>M count_space S2 = count_space (S1 \<times> S2)"
   730 proof (rule pair_measure_eqI)
   731   show "sigma_finite_measure (count_space S1)" "sigma_finite_measure (count_space S2)"
   732     using assms by (auto intro!: sigma_finite_measure_count_space_countable)
   733   show "sets (count_space S1 \<Otimes>\<^sub>M count_space S2) = sets (count_space (S1 \<times> S2))"
   734     by (subst sets_pair_countable[OF assms]) auto
   735 next
   736   fix A B assume "A \<in> sets (count_space S1)" "B \<in> sets (count_space S2)"
   737   then show "emeasure (count_space S1) A * emeasure (count_space S2) B = 
   738     emeasure (count_space (S1 \<times> S2)) (A \<times> B)"
   739     by (subst (1 2 3) emeasure_count_space) (auto simp: finite_cartesian_product_iff)
   740 qed
   741 
   742 subsection {* Product of Borel spaces *}
   743 
   744 lemma borel_Times:
   745   fixes A :: "'a::topological_space set" and B :: "'b::topological_space set"
   746   assumes A: "A \<in> sets borel" and B: "B \<in> sets borel"
   747   shows "A \<times> B \<in> sets borel"
   748 proof -
   749   have "A \<times> B = (A\<times>UNIV) \<inter> (UNIV \<times> B)"
   750     by auto
   751   moreover
   752   { have "A \<in> sigma_sets UNIV {S. open S}" using A by (simp add: sets_borel)
   753     then have "A\<times>UNIV \<in> sets borel"
   754     proof (induct A)
   755       case (Basic S) then show ?case
   756         by (auto intro!: borel_open open_Times)
   757     next
   758       case (Compl A)
   759       moreover have *: "(UNIV - A) \<times> UNIV = UNIV - (A \<times> UNIV)"
   760         by auto
   761       ultimately show ?case
   762         unfolding * by auto
   763     next
   764       case (Union A)
   765       moreover have *: "(UNION UNIV A) \<times> UNIV = UNION UNIV (\<lambda>i. A i \<times> UNIV)"
   766         by auto
   767       ultimately show ?case
   768         unfolding * by auto
   769     qed simp }
   770   moreover
   771   { have "B \<in> sigma_sets UNIV {S. open S}" using B by (simp add: sets_borel)
   772     then have "UNIV\<times>B \<in> sets borel"
   773     proof (induct B)
   774       case (Basic S) then show ?case
   775         by (auto intro!: borel_open open_Times)
   776     next
   777       case (Compl B)
   778       moreover have *: "UNIV \<times> (UNIV - B) = UNIV - (UNIV \<times> B)"
   779         by auto
   780       ultimately show ?case
   781         unfolding * by auto
   782     next
   783       case (Union B)
   784       moreover have *: "UNIV \<times> (UNION UNIV B) = UNION UNIV (\<lambda>i. UNIV \<times> B i)"
   785         by auto
   786       ultimately show ?case
   787         unfolding * by auto
   788     qed simp }
   789   ultimately show ?thesis
   790     by auto
   791 qed
   792 
   793 lemma borel_prod: "sets (borel \<Otimes>\<^sub>M borel) =
   794     (sets borel :: ('a::second_countable_topology \<times> 'b::second_countable_topology) set set)"
   795   (is "?l = ?r")
   796 proof -
   797   obtain A :: "'a set set" where A: "countable A" "topological_basis A"
   798     by (metis ex_countable_basis)
   799   moreover obtain B :: "'b set set" where B: "countable B" "topological_basis B"
   800     by (metis ex_countable_basis)
   801   ultimately have AB: "countable ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))" "topological_basis ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))"
   802     by (auto intro!: topological_basis_prod)
   803   have "sets (borel \<Otimes>\<^sub>M borel) = sigma_sets UNIV {a \<times> b |a b. a \<in> sigma_sets UNIV A \<and> b \<in> sigma_sets UNIV B}"
   804     by (simp add: sets_pair_measure
   805        borel_eq_countable_basis[OF A] borel_eq_countable_basis[OF B])
   806   also have "\<dots> \<supseteq> sigma_sets UNIV ((\<lambda>(a, b). a \<times> b) ` (A \<times> B))" (is "... \<supseteq> ?A")
   807     by (auto intro!: sigma_sets_mono)
   808   also (xtrans) have "?A = sets borel"
   809     by (simp add: borel_eq_countable_basis[OF AB])
   810   finally have "?r \<subseteq> ?l" .
   811   moreover have "?l \<subseteq> ?r"
   812   proof (simp add: sets_pair_measure, safe intro!: sigma_sets_mono)
   813     fix A::"('a \<times> 'b) set" assume "A \<in> sigma_sets UNIV {a \<times> b |a b. a \<in> sets borel \<and> b \<in> sets borel}"
   814     then show "A \<in> sets borel"
   815       by (induct A) (auto intro!: borel_Times)
   816   qed
   817   ultimately show ?thesis by auto
   818 qed
   819 
   820 lemma borel_prod':
   821   "borel \<Otimes>\<^sub>M borel = (borel :: 
   822       ('a::second_countable_topology \<times> 'b::second_countable_topology) measure)"
   823 proof (rule measure_eqI[OF borel_prod])
   824   interpret sigma_finite_measure "borel :: 'b measure"
   825     proof qed (intro exI[of _ "{UNIV}"], auto simp: borel_def emeasure_sigma)
   826   fix X :: "('a \<times> 'b) set" assume asm: "X \<in> sets (borel \<Otimes>\<^sub>M borel)"
   827   have "UNIV \<times> UNIV \<in> sets (borel \<Otimes>\<^sub>M borel :: ('a \<times> 'b) measure)" 
   828       by (simp add: borel_prod)
   829   moreover have "emeasure (borel \<Otimes>\<^sub>M borel) (UNIV \<times> UNIV :: ('a \<times> 'b) set) = 0"
   830       by (subst emeasure_pair_measure_Times, simp_all add: borel_def emeasure_sigma)
   831   moreover have "X \<subseteq> UNIV \<times> UNIV" by auto
   832   ultimately have "emeasure (borel \<Otimes>\<^sub>M borel) X = 0" by (rule emeasure_eq_0)
   833   thus "emeasure (borel \<Otimes>\<^sub>M borel) X = emeasure borel X"
   834       by (simp add: borel_def emeasure_sigma)
   835 qed
   836 
   837 lemma finite_measure_pair_measure:
   838   assumes "finite_measure M" "finite_measure N"
   839   shows "finite_measure (N  \<Otimes>\<^sub>M M)"
   840 proof (rule finite_measureI)
   841   interpret M: finite_measure M by fact
   842   interpret N: finite_measure N by fact
   843   show "emeasure (N  \<Otimes>\<^sub>M M) (space (N  \<Otimes>\<^sub>M M)) \<noteq> \<infinity>"
   844     by (auto simp: space_pair_measure M.emeasure_pair_measure_Times)
   845 qed
   846 
   847 end