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