src/HOL/Probability/Product_Measure.thy
author hoelzl
Mon Jan 24 22:29:50 2011 +0100 (2011-01-24)
changeset 41661 baf1964bc468
parent 41659 a5d1b2df5e97
child 41689 3e39b0e730d6
permissions -rw-r--r--
use pre-image measure, instead of image
     1 theory Product_Measure
     2 imports Lebesgue_Integration
     3 begin
     4 
     5 lemma times_Int_times: "A \<times> B \<inter> C \<times> D = (A \<inter> C) \<times> (B \<inter> D)"
     6   by auto
     7 
     8 lemma Pair_vimage_times[simp]: "\<And>A B x. Pair x -` (A \<times> B) = (if x \<in> A then B else {})"
     9   by auto
    10 
    11 lemma rev_Pair_vimage_times[simp]: "\<And>A B y. (\<lambda>x. (x, y)) -` (A \<times> B) = (if y \<in> B then A else {})"
    12   by auto
    13 
    14 lemma case_prod_distrib: "f (case x of (x, y) \<Rightarrow> g x y) = (case x of (x, y) \<Rightarrow> f (g x y))"
    15   by (cases x) simp
    16 
    17 lemma split_beta': "(\<lambda>(x,y). f x y) = (\<lambda>x. f (fst x) (snd x))"
    18   by (auto simp: fun_eq_iff)
    19 
    20 abbreviation
    21   "Pi\<^isub>E A B \<equiv> Pi A B \<inter> extensional A"
    22 
    23 abbreviation
    24   funcset_extensional :: "['a set, 'b set] => ('a => 'b) set"
    25     (infixr "->\<^isub>E" 60) where
    26   "A ->\<^isub>E B \<equiv> Pi\<^isub>E A (%_. B)"
    27 
    28 notation (xsymbols)
    29   funcset_extensional  (infixr "\<rightarrow>\<^isub>E" 60)
    30 
    31 lemma extensional_empty[simp]: "extensional {} = {\<lambda>x. undefined}"
    32   by safe (auto simp add: extensional_def fun_eq_iff)
    33 
    34 lemma extensional_insert[intro, simp]:
    35   assumes "a \<in> extensional (insert i I)"
    36   shows "a(i := b) \<in> extensional (insert i I)"
    37   using assms unfolding extensional_def by auto
    38 
    39 lemma extensional_Int[simp]:
    40   "extensional I \<inter> extensional I' = extensional (I \<inter> I')"
    41   unfolding extensional_def by auto
    42 
    43 definition
    44   "merge I x J y = (\<lambda>i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
    45 
    46 lemma merge_apply[simp]:
    47   "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    48   "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    49   "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    50   "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    51   "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I x J y i = undefined"
    52   unfolding merge_def by auto
    53 
    54 lemma merge_commute:
    55   "I \<inter> J = {} \<Longrightarrow> merge I x J y = merge J y I x"
    56   by (auto simp: merge_def intro!: ext)
    57 
    58 lemma Pi_cancel_merge_range[simp]:
    59   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    60   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    61   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    62   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    63   by (auto simp: Pi_def)
    64 
    65 lemma Pi_cancel_merge[simp]:
    66   "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    67   "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    68   "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    69   "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    70   by (auto simp: Pi_def)
    71 
    72 lemma extensional_merge[simp]: "merge I x J y \<in> extensional (I \<union> J)"
    73   by (auto simp: extensional_def)
    74 
    75 lemma restrict_Pi_cancel: "restrict x I \<in> Pi I A \<longleftrightarrow> x \<in> Pi I A"
    76   by (auto simp: restrict_def Pi_def)
    77 
    78 lemma restrict_merge[simp]:
    79   "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    80   "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    81   "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    82   "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    83   by (auto simp: restrict_def intro!: ext)
    84 
    85 lemma extensional_insert_undefined[intro, simp]:
    86   assumes "a \<in> extensional (insert i I)"
    87   shows "a(i := undefined) \<in> extensional I"
    88   using assms unfolding extensional_def by auto
    89 
    90 lemma extensional_insert_cancel[intro, simp]:
    91   assumes "a \<in> extensional I"
    92   shows "a \<in> extensional (insert i I)"
    93   using assms unfolding extensional_def by auto
    94 
    95 lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I x {i} y = restrict (x(i := y i)) (insert i I)"
    96   unfolding merge_def by (auto simp: fun_eq_iff)
    97 
    98 lemma Pi_Int: "Pi I E \<inter> Pi I F = (\<Pi> i\<in>I. E i \<inter> F i)"
    99   by auto
   100 
   101 lemma PiE_Int: "(Pi\<^isub>E I A) \<inter> (Pi\<^isub>E I B) = Pi\<^isub>E I (\<lambda>x. A x \<inter> B x)"
   102   by auto
   103 
   104 lemma Pi_cancel_fupd_range[simp]: "i \<notin> I \<Longrightarrow> x \<in> Pi I (B(i := b)) \<longleftrightarrow> x \<in> Pi I B"
   105   by (auto simp: Pi_def)
   106 
   107 lemma Pi_split_insert_domain[simp]: "x \<in> Pi (insert i I) X \<longleftrightarrow> x \<in> Pi I X \<and> x i \<in> X i"
   108   by (auto simp: Pi_def)
   109 
   110 lemma Pi_split_domain[simp]: "x \<in> Pi (I \<union> J) X \<longleftrightarrow> x \<in> Pi I X \<and> x \<in> Pi J X"
   111   by (auto simp: Pi_def)
   112 
   113 lemma Pi_cancel_fupd[simp]: "i \<notin> I \<Longrightarrow> x(i := a) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
   114   by (auto simp: Pi_def)
   115 
   116 lemma restrict_vimage:
   117   assumes "I \<inter> J = {}"
   118   shows "(\<lambda>x. (restrict x I, restrict x J)) -` (Pi\<^isub>E I E \<times> Pi\<^isub>E J F) = Pi (I \<union> J) (merge I E J F)"
   119   using assms by (auto simp: restrict_Pi_cancel)
   120 
   121 lemma merge_vimage:
   122   assumes "I \<inter> J = {}"
   123   shows "(\<lambda>(x,y). merge I x J y) -` Pi\<^isub>E (I \<union> J) E = Pi I E \<times> Pi J E"
   124   using assms by (auto simp: restrict_Pi_cancel)
   125 
   126 lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
   127   by (auto simp: restrict_def intro!: ext)
   128 
   129 lemma merge_restrict[simp]:
   130   "merge I (restrict x I) J y = merge I x J y"
   131   "merge I x J (restrict y J) = merge I x J y"
   132   unfolding merge_def by (auto intro!: ext)
   133 
   134 lemma merge_x_x_eq_restrict[simp]:
   135   "merge I x J x = restrict x (I \<union> J)"
   136   unfolding merge_def by (auto intro!: ext)
   137 
   138 lemma Pi_fupd_iff: "i \<in> I \<Longrightarrow> f \<in> Pi I (B(i := A)) \<longleftrightarrow> f \<in> Pi (I - {i}) B \<and> f i \<in> A"
   139   apply auto
   140   apply (drule_tac x=x in Pi_mem)
   141   apply (simp_all split: split_if_asm)
   142   apply (drule_tac x=i in Pi_mem)
   143   apply (auto dest!: Pi_mem)
   144   done
   145 
   146 lemma Pi_UN:
   147   fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
   148   assumes "finite I" and mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
   149   shows "(\<Union>n. Pi I (A n)) = (\<Pi> i\<in>I. \<Union>n. A n i)"
   150 proof (intro set_eqI iffI)
   151   fix f assume "f \<in> (\<Pi> i\<in>I. \<Union>n. A n i)"
   152   then have "\<forall>i\<in>I. \<exists>n. f i \<in> A n i" by auto
   153   from bchoice[OF this] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
   154   obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
   155     using `finite I` finite_nat_set_iff_bounded_le[of "n`I"] by auto
   156   have "f \<in> Pi I (A k)"
   157   proof (intro Pi_I)
   158     fix i assume "i \<in> I"
   159     from mono[OF this, of "n i" k] k[OF this] n[OF this]
   160     show "f i \<in> A k i" by auto
   161   qed
   162   then show "f \<in> (\<Union>n. Pi I (A n))" by auto
   163 qed auto
   164 
   165 lemma PiE_cong:
   166   assumes "\<And>i. i\<in>I \<Longrightarrow> A i = B i"
   167   shows "Pi\<^isub>E I A = Pi\<^isub>E I B"
   168   using assms by (auto intro!: Pi_cong)
   169 
   170 lemma restrict_upd[simp]:
   171   "i \<notin> I \<Longrightarrow> (restrict f I)(i := y) = restrict (f(i := y)) (insert i I)"
   172   by (auto simp: fun_eq_iff)
   173 
   174 section "Binary products"
   175 
   176 definition
   177   "pair_algebra A B = \<lparr> space = space A \<times> space B,
   178                            sets = {a \<times> b | a b. a \<in> sets A \<and> b \<in> sets B} \<rparr>"
   179 
   180 locale pair_sigma_algebra = M1: sigma_algebra M1 + M2: sigma_algebra M2
   181   for M1 M2
   182 
   183 abbreviation (in pair_sigma_algebra)
   184   "E \<equiv> pair_algebra M1 M2"
   185 
   186 abbreviation (in pair_sigma_algebra)
   187   "P \<equiv> sigma E"
   188 
   189 sublocale pair_sigma_algebra \<subseteq> sigma_algebra P
   190   using M1.sets_into_space M2.sets_into_space
   191   by (force simp: pair_algebra_def intro!: sigma_algebra_sigma)
   192 
   193 lemma pair_algebraI[intro, simp]:
   194   "x \<in> sets A \<Longrightarrow> y \<in> sets B \<Longrightarrow> x \<times> y \<in> sets (pair_algebra A B)"
   195   by (auto simp add: pair_algebra_def)
   196 
   197 lemma space_pair_algebra:
   198   "space (pair_algebra A B) = space A \<times> space B"
   199   by (simp add: pair_algebra_def)
   200 
   201 lemma sets_pair_algebra: "sets (pair_algebra N M) = (\<lambda>(x, y). x \<times> y) ` (sets N \<times> sets M)"
   202   unfolding pair_algebra_def by auto
   203 
   204 lemma pair_algebra_sets_into_space:
   205   assumes "sets M \<subseteq> Pow (space M)" "sets N \<subseteq> Pow (space N)"
   206   shows "sets (pair_algebra M N) \<subseteq> Pow (space (pair_algebra M N))"
   207   using assms by (auto simp: pair_algebra_def)
   208 
   209 lemma pair_algebra_Int_snd:
   210   assumes "sets S1 \<subseteq> Pow (space S1)"
   211   shows "pair_algebra S1 (algebra.restricted_space S2 A) =
   212          algebra.restricted_space (pair_algebra S1 S2) (space S1 \<times> A)"
   213   (is "?L = ?R")
   214 proof (intro algebra.equality set_eqI iffI)
   215   fix X assume "X \<in> sets ?L"
   216   then obtain A1 A2 where X: "X = A1 \<times> (A \<inter> A2)" and "A1 \<in> sets S1" "A2 \<in> sets S2"
   217     by (auto simp: pair_algebra_def)
   218   then show "X \<in> sets ?R" unfolding pair_algebra_def
   219     using assms apply simp by (intro image_eqI[of _ _ "A1 \<times> A2"]) auto
   220 next
   221   fix X assume "X \<in> sets ?R"
   222   then obtain A1 A2 where "X = space S1 \<times> A \<inter> A1 \<times> A2" "A1 \<in> sets S1" "A2 \<in> sets S2"
   223     by (auto simp: pair_algebra_def)
   224   moreover then have "X = A1 \<times> (A \<inter> A2)"
   225     using assms by auto
   226   ultimately show "X \<in> sets ?L"
   227     unfolding pair_algebra_def by auto
   228 qed (auto simp add: pair_algebra_def)
   229 
   230 lemma (in pair_sigma_algebra)
   231   shows measurable_fst[intro!, simp]:
   232     "fst \<in> measurable P M1" (is ?fst)
   233   and measurable_snd[intro!, simp]:
   234     "snd \<in> measurable P M2" (is ?snd)
   235 proof -
   236   { fix X assume "X \<in> sets M1"
   237     then have "\<exists>X1\<in>sets M1. \<exists>X2\<in>sets M2. fst -` X \<inter> space M1 \<times> space M2 = X1 \<times> X2"
   238       apply - apply (rule bexI[of _ X]) apply (rule bexI[of _ "space M2"])
   239       using M1.sets_into_space by force+ }
   240   moreover
   241   { fix X assume "X \<in> sets M2"
   242     then have "\<exists>X1\<in>sets M1. \<exists>X2\<in>sets M2. snd -` X \<inter> space M1 \<times> space M2 = X1 \<times> X2"
   243       apply - apply (rule bexI[of _ "space M1"]) apply (rule bexI[of _ X])
   244       using M2.sets_into_space by force+ }
   245   ultimately have "?fst \<and> ?snd"
   246     by (fastsimp simp: measurable_def sets_sigma space_pair_algebra
   247                  intro!: sigma_sets.Basic)
   248   then show ?fst ?snd by auto
   249 qed
   250 
   251 lemma (in pair_sigma_algebra) measurable_pair_iff:
   252   assumes "sigma_algebra M"
   253   shows "f \<in> measurable M P \<longleftrightarrow>
   254     (fst \<circ> f) \<in> measurable M M1 \<and> (snd \<circ> f) \<in> measurable M M2"
   255 proof -
   256   interpret M: sigma_algebra M by fact
   257   from assms show ?thesis
   258   proof (safe intro!: measurable_comp[where b=P])
   259     assume f: "(fst \<circ> f) \<in> measurable M M1" and s: "(snd \<circ> f) \<in> measurable M M2"
   260     show "f \<in> measurable M P"
   261     proof (rule M.measurable_sigma)
   262       show "sets (pair_algebra M1 M2) \<subseteq> Pow (space E)"
   263         unfolding pair_algebra_def using M1.sets_into_space M2.sets_into_space by auto
   264       show "f \<in> space M \<rightarrow> space E"
   265         using f s by (auto simp: mem_Times_iff measurable_def comp_def space_sigma space_pair_algebra)
   266       fix A assume "A \<in> sets E"
   267       then obtain B C where "B \<in> sets M1" "C \<in> sets M2" "A = B \<times> C"
   268         unfolding pair_algebra_def by auto
   269       moreover have "(fst \<circ> f) -` B \<inter> space M \<in> sets M"
   270         using f `B \<in> sets M1` unfolding measurable_def by auto
   271       moreover have "(snd \<circ> f) -` C \<inter> space M \<in> sets M"
   272         using s `C \<in> sets M2` unfolding measurable_def by auto
   273       moreover have "f -` A \<inter> space M = ((fst \<circ> f) -` B \<inter> space M) \<inter> ((snd \<circ> f) -` C \<inter> space M)"
   274         unfolding `A = B \<times> C` by (auto simp: vimage_Times)
   275       ultimately show "f -` A \<inter> space M \<in> sets M" by auto
   276     qed
   277   qed
   278 qed
   279 
   280 lemma (in pair_sigma_algebra) measurable_pair:
   281   assumes "sigma_algebra M"
   282   assumes "(fst \<circ> f) \<in> measurable M M1" "(snd \<circ> f) \<in> measurable M M2"
   283   shows "f \<in> measurable M P"
   284   unfolding measurable_pair_iff[OF assms(1)] using assms(2,3) by simp
   285 
   286 lemma pair_algebraE:
   287   assumes "X \<in> sets (pair_algebra M1 M2)"
   288   obtains A B where "X = A \<times> B" "A \<in> sets M1" "B \<in> sets M2"
   289   using assms unfolding pair_algebra_def by auto
   290 
   291 lemma (in pair_sigma_algebra) pair_algebra_swap:
   292   "(\<lambda>X. (\<lambda>(x,y). (y,x)) -` X \<inter> space M2 \<times> space M1) ` sets E = sets (pair_algebra M2 M1)"
   293 proof (safe elim!: pair_algebraE)
   294   fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   295   moreover then have "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space M2 \<times> space M1 = B \<times> A"
   296     using M1.sets_into_space M2.sets_into_space by auto
   297   ultimately show "(\<lambda>(x, y). (y, x)) -` (A \<times> B) \<inter> space M2 \<times> space M1 \<in> sets (pair_algebra M2 M1)"
   298     by (auto intro: pair_algebraI)
   299 next
   300   fix A B assume "A \<in> sets M1" "B \<in> sets M2"
   301   then show "B \<times> A \<in> (\<lambda>X. (\<lambda>(x, y). (y, x)) -` X \<inter> space M2 \<times> space M1) ` sets E"
   302     using M1.sets_into_space M2.sets_into_space
   303     by (auto intro!: image_eqI[where x="A \<times> B"] pair_algebraI)
   304 qed
   305 
   306 lemma (in pair_sigma_algebra) sets_pair_sigma_algebra_swap:
   307   assumes Q: "Q \<in> sets P"
   308   shows "(\<lambda>(x,y). (y, x)) ` Q \<in> sets (sigma (pair_algebra M2 M1))" (is "_ \<in> sets ?Q")
   309 proof -
   310   have *: "(\<lambda>(x,y). (y, x)) \<in> space M2 \<times> space M1 \<rightarrow> (space M1 \<times> space M2)"
   311        "sets (pair_algebra M1 M2) \<subseteq> Pow (space M1 \<times> space M2)"
   312     using M1.sets_into_space M2.sets_into_space by (auto elim!: pair_algebraE)
   313   from Q sets_into_space show ?thesis
   314     by (auto intro!: image_eqI[where x=Q]
   315              simp: pair_algebra_swap[symmetric] sets_sigma
   316                    sigma_sets_vimage[OF *] space_pair_algebra)
   317 qed
   318 
   319 lemma (in pair_sigma_algebra) pair_sigma_algebra_swap_measurable:
   320   shows "(\<lambda>(x,y). (y, x)) \<in> measurable P (sigma (pair_algebra M2 M1))"
   321     (is "?f \<in> measurable ?P ?Q")
   322   unfolding measurable_def
   323 proof (intro CollectI conjI Pi_I ballI)
   324   fix x assume "x \<in> space ?P" then show "(case x of (x, y) \<Rightarrow> (y, x)) \<in> space ?Q"
   325     unfolding pair_algebra_def by auto
   326 next
   327   fix A assume "A \<in> sets ?Q"
   328   interpret Q: pair_sigma_algebra M2 M1 by default
   329   have "?f -` A \<inter> space ?P = (\<lambda>(x,y). (y, x)) ` A"
   330     using Q.sets_into_space `A \<in> sets ?Q` by (auto simp: pair_algebra_def)
   331   with Q.sets_pair_sigma_algebra_swap[OF `A \<in> sets ?Q`]
   332   show "?f -` A \<inter> space ?P \<in> sets ?P" by simp
   333 qed
   334 
   335 lemma (in pair_sigma_algebra) measurable_cut_fst:
   336   assumes "Q \<in> sets P" shows "Pair x -` Q \<in> sets M2"
   337 proof -
   338   let ?Q' = "{Q. Q \<subseteq> space P \<and> Pair x -` Q \<in> sets M2}"
   339   let ?Q = "\<lparr> space = space P, sets = ?Q' \<rparr>"
   340   interpret Q: sigma_algebra ?Q
   341     proof qed (auto simp: vimage_UN vimage_Diff space_pair_algebra)
   342   have "sets E \<subseteq> sets ?Q"
   343     using M1.sets_into_space M2.sets_into_space
   344     by (auto simp: pair_algebra_def space_pair_algebra)
   345   then have "sets P \<subseteq> sets ?Q"
   346     by (subst pair_algebra_def, intro Q.sets_sigma_subset)
   347        (simp_all add: pair_algebra_def)
   348   with assms show ?thesis by auto
   349 qed
   350 
   351 lemma (in pair_sigma_algebra) measurable_cut_snd:
   352   assumes Q: "Q \<in> sets P" shows "(\<lambda>x. (x, y)) -` Q \<in> sets M1" (is "?cut Q \<in> sets M1")
   353 proof -
   354   interpret Q: pair_sigma_algebra M2 M1 by default
   355   have "Pair y -` (\<lambda>(x, y). (y, x)) ` Q = (\<lambda>x. (x, y)) -` Q" by auto
   356   with Q.measurable_cut_fst[OF sets_pair_sigma_algebra_swap[OF Q], of y]
   357   show ?thesis by simp
   358 qed
   359 
   360 lemma (in pair_sigma_algebra) measurable_pair_image_snd:
   361   assumes m: "f \<in> measurable P M" and "x \<in> space M1"
   362   shows "(\<lambda>y. f (x, y)) \<in> measurable M2 M"
   363   unfolding measurable_def
   364 proof (intro CollectI conjI Pi_I ballI)
   365   fix y assume "y \<in> space M2" with `f \<in> measurable P M` `x \<in> space M1`
   366   show "f (x, y) \<in> space M" unfolding measurable_def pair_algebra_def by auto
   367 next
   368   fix A assume "A \<in> sets M"
   369   then have "Pair x -` (f -` A \<inter> space P) \<in> sets M2" (is "?C \<in> _")
   370     using `f \<in> measurable P M`
   371     by (intro measurable_cut_fst) (auto simp: measurable_def)
   372   also have "?C = (\<lambda>y. f (x, y)) -` A \<inter> space M2"
   373     using `x \<in> space M1` by (auto simp: pair_algebra_def)
   374   finally show "(\<lambda>y. f (x, y)) -` A \<inter> space M2 \<in> sets M2" .
   375 qed
   376 
   377 lemma (in pair_sigma_algebra) measurable_pair_image_fst:
   378   assumes m: "f \<in> measurable P M" and "y \<in> space M2"
   379   shows "(\<lambda>x. f (x, y)) \<in> measurable M1 M"
   380 proof -
   381   interpret Q: pair_sigma_algebra M2 M1 by default
   382   from Q.measurable_pair_image_snd[OF measurable_comp `y \<in> space M2`,
   383                                       OF Q.pair_sigma_algebra_swap_measurable m]
   384   show ?thesis by simp
   385 qed
   386 
   387 lemma (in pair_sigma_algebra) Int_stable_pair_algebra: "Int_stable E"
   388   unfolding Int_stable_def
   389 proof (intro ballI)
   390   fix A B assume "A \<in> sets E" "B \<in> sets E"
   391   then obtain A1 A2 B1 B2 where "A = A1 \<times> A2" "B = B1 \<times> B2"
   392     "A1 \<in> sets M1" "A2 \<in> sets M2" "B1 \<in> sets M1" "B2 \<in> sets M2"
   393     unfolding pair_algebra_def by auto
   394   then show "A \<inter> B \<in> sets E"
   395     by (auto simp add: times_Int_times pair_algebra_def)
   396 qed
   397 
   398 lemma finite_measure_cut_measurable:
   399   fixes M1 :: "'a algebra" and M2 :: "'b algebra"
   400   assumes "sigma_finite_measure M1 \<mu>1" "finite_measure M2 \<mu>2"
   401   assumes "Q \<in> sets (sigma (pair_algebra M1 M2))"
   402   shows "(\<lambda>x. \<mu>2 (Pair x -` Q)) \<in> borel_measurable M1"
   403     (is "?s Q \<in> _")
   404 proof -
   405   interpret M1: sigma_finite_measure M1 \<mu>1 by fact
   406   interpret M2: finite_measure M2 \<mu>2 by fact
   407   interpret pair_sigma_algebra M1 M2 by default
   408   have [intro]: "sigma_algebra M1" by fact
   409   have [intro]: "sigma_algebra M2" by fact
   410   let ?D = "\<lparr> space = space P, sets = {A\<in>sets P. ?s A \<in> borel_measurable M1}  \<rparr>"
   411   note space_pair_algebra[simp]
   412   interpret dynkin_system ?D
   413   proof (intro dynkin_systemI)
   414     fix A assume "A \<in> sets ?D" then show "A \<subseteq> space ?D"
   415       using sets_into_space by simp
   416   next
   417     from top show "space ?D \<in> sets ?D"
   418       by (auto simp add: if_distrib intro!: M1.measurable_If)
   419   next
   420     fix A assume "A \<in> sets ?D"
   421     with sets_into_space have "\<And>x. \<mu>2 (Pair x -` (space M1 \<times> space M2 - A)) =
   422         (if x \<in> space M1 then \<mu>2 (space M2) - ?s A x else 0)"
   423       by (auto intro!: M2.finite_measure_compl measurable_cut_fst
   424                simp: vimage_Diff)
   425     with `A \<in> sets ?D` top show "space ?D - A \<in> sets ?D"
   426       by (auto intro!: Diff M1.measurable_If M1.borel_measurable_pextreal_diff)
   427   next
   428     fix F :: "nat \<Rightarrow> ('a\<times>'b) set" assume "disjoint_family F" "range F \<subseteq> sets ?D"
   429     moreover then have "\<And>x. \<mu>2 (\<Union>i. Pair x -` F i) = (\<Sum>\<^isub>\<infinity> i. ?s (F i) x)"
   430       by (intro M2.measure_countably_additive[symmetric])
   431          (auto intro!: measurable_cut_fst simp: disjoint_family_on_def)
   432     ultimately show "(\<Union>i. F i) \<in> sets ?D"
   433       by (auto simp: vimage_UN intro!: M1.borel_measurable_psuminf)
   434   qed
   435   have "P = ?D"
   436   proof (intro dynkin_lemma)
   437     show "Int_stable E" by (rule Int_stable_pair_algebra)
   438     from M1.sets_into_space have "\<And>A. A \<in> sets M1 \<Longrightarrow> {x \<in> space M1. x \<in> A} = A"
   439       by auto
   440     then show "sets E \<subseteq> sets ?D"
   441       by (auto simp: pair_algebra_def sets_sigma if_distrib
   442                intro: sigma_sets.Basic intro!: M1.measurable_If)
   443   qed auto
   444   with `Q \<in> sets P` have "Q \<in> sets ?D" by simp
   445   then show "?s Q \<in> borel_measurable M1" by simp
   446 qed
   447 
   448 subsection {* Binary products of $\sigma$-finite measure spaces *}
   449 
   450 locale pair_sigma_finite = M1: sigma_finite_measure M1 \<mu>1 + M2: sigma_finite_measure M2 \<mu>2
   451   for M1 \<mu>1 M2 \<mu>2
   452 
   453 sublocale pair_sigma_finite \<subseteq> pair_sigma_algebra M1 M2
   454   by default
   455 
   456 lemma (in pair_sigma_finite) measure_cut_measurable_fst:
   457   assumes "Q \<in> sets P" shows "(\<lambda>x. \<mu>2 (Pair x -` Q)) \<in> borel_measurable M1" (is "?s Q \<in> _")
   458 proof -
   459   have [intro]: "sigma_algebra M1" and [intro]: "sigma_algebra M2" by default+
   460   have M1: "sigma_finite_measure M1 \<mu>1" by default
   461 
   462   from M2.disjoint_sigma_finite guess F .. note F = this
   463   let "?C x i" = "F i \<inter> Pair x -` Q"
   464   { fix i
   465     let ?R = "M2.restricted_space (F i)"
   466     have [simp]: "space M1 \<times> F i \<inter> space M1 \<times> space M2 = space M1 \<times> F i"
   467       using F M2.sets_into_space by auto
   468     have "(\<lambda>x. \<mu>2 (Pair x -` (space M1 \<times> F i \<inter> Q))) \<in> borel_measurable M1"
   469     proof (intro finite_measure_cut_measurable[OF M1])
   470       show "finite_measure (M2.restricted_space (F i)) \<mu>2"
   471         using F by (intro M2.restricted_to_finite_measure) auto
   472       have "space M1 \<times> F i \<in> sets P"
   473         using M1.top F by blast
   474       from sigma_sets_Int[symmetric,
   475         OF this[unfolded pair_sigma_algebra_def sets_sigma]]
   476       show "(space M1 \<times> F i) \<inter> Q \<in> sets (sigma (pair_algebra M1 ?R))"
   477         using `Q \<in> sets P`
   478         using pair_algebra_Int_snd[OF M1.space_closed, of "F i" M2]
   479         by (auto simp: pair_algebra_def sets_sigma)
   480     qed
   481     moreover have "\<And>x. Pair x -` (space M1 \<times> F i \<inter> Q) = ?C x i"
   482       using `Q \<in> sets P` sets_into_space by (auto simp: space_pair_algebra)
   483     ultimately have "(\<lambda>x. \<mu>2 (?C x i)) \<in> borel_measurable M1"
   484       by simp }
   485   moreover
   486   { fix x
   487     have "(\<Sum>\<^isub>\<infinity>i. \<mu>2 (?C x i)) = \<mu>2 (\<Union>i. ?C x i)"
   488     proof (intro M2.measure_countably_additive)
   489       show "range (?C x) \<subseteq> sets M2"
   490         using F `Q \<in> sets P` by (auto intro!: M2.Int measurable_cut_fst)
   491       have "disjoint_family F" using F by auto
   492       show "disjoint_family (?C x)"
   493         by (rule disjoint_family_on_bisimulation[OF `disjoint_family F`]) auto
   494     qed
   495     also have "(\<Union>i. ?C x i) = Pair x -` Q"
   496       using F sets_into_space `Q \<in> sets P`
   497       by (auto simp: space_pair_algebra)
   498     finally have "\<mu>2 (Pair x -` Q) = (\<Sum>\<^isub>\<infinity>i. \<mu>2 (?C x i))"
   499       by simp }
   500   ultimately show ?thesis
   501     by (auto intro!: M1.borel_measurable_psuminf)
   502 qed
   503 
   504 lemma (in pair_sigma_finite) measure_cut_measurable_snd:
   505   assumes "Q \<in> sets P" shows "(\<lambda>y. \<mu>1 ((\<lambda>x. (x, y)) -` Q)) \<in> borel_measurable M2"
   506 proof -
   507   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   508   have [simp]: "\<And>y. (Pair y -` (\<lambda>(x, y). (y, x)) ` Q) = (\<lambda>x. (x, y)) -` Q"
   509     by auto
   510   note sets_pair_sigma_algebra_swap[OF assms]
   511   from Q.measure_cut_measurable_fst[OF this]
   512   show ?thesis by simp
   513 qed
   514 
   515 lemma (in pair_sigma_algebra) pair_sigma_algebra_measurable:
   516   assumes "f \<in> measurable P M"
   517   shows "(\<lambda>(x,y). f (y, x)) \<in> measurable (sigma (pair_algebra M2 M1)) M"
   518 proof -
   519   interpret Q: pair_sigma_algebra M2 M1 by default
   520   have *: "(\<lambda>(x,y). f (y, x)) = f \<circ> (\<lambda>(x,y). (y, x))" by (simp add: fun_eq_iff)
   521   show ?thesis
   522     using Q.pair_sigma_algebra_swap_measurable assms
   523     unfolding * by (rule measurable_comp)
   524 qed
   525 
   526 definition (in pair_sigma_finite)
   527   "pair_measure A = M1.positive_integral (\<lambda>x.
   528     M2.positive_integral (\<lambda>y. indicator A (x, y)))"
   529 
   530 lemma (in pair_sigma_finite) pair_measure_alt:
   531   assumes "A \<in> sets P"
   532   shows "pair_measure A = M1.positive_integral (\<lambda>x. \<mu>2 (Pair x -` A))"
   533   unfolding pair_measure_def
   534 proof (rule M1.positive_integral_cong)
   535   fix x assume "x \<in> space M1"
   536   have *: "\<And>y. indicator A (x, y) = (indicator (Pair x -` A) y :: pextreal)"
   537     unfolding indicator_def by auto
   538   show "M2.positive_integral (\<lambda>y. indicator A (x, y)) = \<mu>2 (Pair x -` A)"
   539     unfolding *
   540     apply (subst M2.positive_integral_indicator)
   541     apply (rule measurable_cut_fst[OF assms])
   542     by simp
   543 qed
   544 
   545 lemma (in pair_sigma_finite) pair_measure_times:
   546   assumes A: "A \<in> sets M1" and "B \<in> sets M2"
   547   shows "pair_measure (A \<times> B) = \<mu>1 A * \<mu>2 B"
   548 proof -
   549   from assms have "pair_measure (A \<times> B) =
   550       M1.positive_integral (\<lambda>x. \<mu>2 B * indicator A x)"
   551     by (auto intro!: M1.positive_integral_cong simp: pair_measure_alt)
   552   with assms show ?thesis
   553     by (simp add: M1.positive_integral_cmult_indicator ac_simps)
   554 qed
   555 
   556 lemma (in pair_sigma_finite) sigma_finite_up_in_pair_algebra:
   557   "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets E \<and> F \<up> space E \<and>
   558     (\<forall>i. pair_measure (F i) \<noteq> \<omega>)"
   559 proof -
   560   obtain F1 :: "nat \<Rightarrow> 'a set" and F2 :: "nat \<Rightarrow> 'b set" where
   561     F1: "range F1 \<subseteq> sets M1" "F1 \<up> space M1" "\<And>i. \<mu>1 (F1 i) \<noteq> \<omega>" and
   562     F2: "range F2 \<subseteq> sets M2" "F2 \<up> space M2" "\<And>i. \<mu>2 (F2 i) \<noteq> \<omega>"
   563     using M1.sigma_finite_up M2.sigma_finite_up by auto
   564   then have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)"
   565     unfolding isoton_def by auto
   566   let ?F = "\<lambda>i. F1 i \<times> F2 i"
   567   show ?thesis unfolding isoton_def space_pair_algebra
   568   proof (intro exI[of _ ?F] conjI allI)
   569     show "range ?F \<subseteq> sets E" using F1 F2
   570       by (fastsimp intro!: pair_algebraI)
   571   next
   572     have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   573     proof (intro subsetI)
   574       fix x assume "x \<in> space M1 \<times> space M2"
   575       then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"
   576         by (auto simp: space)
   577       then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"
   578         using `F1 \<up> space M1` `F2 \<up> space M2`
   579         by (auto simp: max_def dest: isoton_mono_le)
   580       then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"
   581         by (intro SigmaI) (auto simp add: min_max.sup_commute)
   582       then show "x \<in> (\<Union>i. ?F i)" by auto
   583     qed
   584     then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   585       using space by (auto simp: space)
   586   next
   587     fix i show "F1 i \<times> F2 i \<subseteq> F1 (Suc i) \<times> F2 (Suc i)"
   588       using `F1 \<up> space M1` `F2 \<up> space M2` unfolding isoton_def
   589       by auto
   590   next
   591     fix i
   592     from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   593     with F1 F2 show "pair_measure (F1 i \<times> F2 i) \<noteq> \<omega>"
   594       by (simp add: pair_measure_times)
   595   qed
   596 qed
   597 
   598 sublocale pair_sigma_finite \<subseteq> sigma_finite_measure P pair_measure
   599 proof
   600   show "pair_measure {} = 0"
   601     unfolding pair_measure_def by auto
   602 
   603   show "countably_additive P pair_measure"
   604     unfolding countably_additive_def
   605   proof (intro allI impI)
   606     fix F :: "nat \<Rightarrow> ('a \<times> 'b) set"
   607     assume F: "range F \<subseteq> sets P" "disjoint_family F"
   608     from F have *: "\<And>i. F i \<in> sets P" "(\<Union>i. F i) \<in> sets P" by auto
   609     moreover from F have "\<And>i. (\<lambda>x. \<mu>2 (Pair x -` F i)) \<in> borel_measurable M1"
   610       by (intro measure_cut_measurable_fst) auto
   611     moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   612       by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   613     moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
   614       using F by (auto intro!: measurable_cut_fst)
   615     ultimately show "(\<Sum>\<^isub>\<infinity>n. pair_measure (F n)) = pair_measure (\<Union>i. F i)"
   616       by (simp add: pair_measure_alt vimage_UN M1.positive_integral_psuminf[symmetric]
   617                     M2.measure_countably_additive
   618                cong: M1.positive_integral_cong)
   619   qed
   620 
   621   from sigma_finite_up_in_pair_algebra guess F :: "nat \<Rightarrow> ('a \<times> 'c) set" .. note F = this
   622   show "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets P \<and> (\<Union>i. F i) = space P \<and> (\<forall>i. pair_measure (F i) \<noteq> \<omega>)"
   623   proof (rule exI[of _ F], intro conjI)
   624     show "range F \<subseteq> sets P" using F by auto
   625     show "(\<Union>i. F i) = space P"
   626       using F by (auto simp: space_pair_algebra isoton_def)
   627     show "\<forall>i. pair_measure (F i) \<noteq> \<omega>" using F by auto
   628   qed
   629 qed
   630 
   631 lemma (in pair_sigma_algebra) sets_swap:
   632   assumes "A \<in> sets P"
   633   shows "(\<lambda>(x, y). (y, x)) -` A \<inter> space (sigma (pair_algebra M2 M1)) \<in> sets (sigma (pair_algebra M2 M1))"
   634     (is "_ -` A \<inter> space ?Q \<in> sets ?Q")
   635 proof -
   636   have *: "(\<lambda>(x, y). (y, x)) -` A \<inter> space ?Q = (\<lambda>(x, y). (y, x)) ` A"
   637     using `A \<in> sets P` sets_into_space by (auto simp: space_pair_algebra)
   638   show ?thesis
   639     unfolding * using assms by (rule sets_pair_sigma_algebra_swap)
   640 qed
   641 
   642 lemma (in pair_sigma_finite) pair_measure_alt2:
   643   assumes "A \<in> sets P"
   644   shows "pair_measure A = M2.positive_integral (\<lambda>y. \<mu>1 ((\<lambda>x. (x, y)) -` A))"
   645     (is "_ = ?\<nu> A")
   646 proof -
   647   from sigma_finite_up_in_pair_algebra guess F :: "nat \<Rightarrow> ('a \<times> 'c) set" .. note F = this
   648   show ?thesis
   649   proof (rule measure_unique_Int_stable[where \<nu>="?\<nu>", OF Int_stable_pair_algebra],
   650          simp_all add: pair_sigma_algebra_def[symmetric])
   651     show "range F \<subseteq> sets E" "F \<up> space E" "\<And>i. pair_measure (F i) \<noteq> \<omega>"
   652       using F by auto
   653     show "measure_space P pair_measure" by default
   654     interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   655     have P: "sigma_algebra P" by default
   656     show "measure_space P ?\<nu>"
   657       apply (rule Q.measure_space_vimage[OF P])
   658       apply (rule Q.pair_sigma_algebra_swap_measurable)
   659     proof -
   660       fix A assume "A \<in> sets P"
   661       from sets_swap[OF this]
   662       show "M2.positive_integral (\<lambda>y. \<mu>1 ((\<lambda>x. (x, y)) -` A)) =
   663             Q.pair_measure ((\<lambda>(x, y). (y, x)) -` A \<inter> space Q.P)"
   664         using sets_into_space[OF `A \<in> sets P`]
   665         by (auto simp add: Q.pair_measure_alt space_pair_algebra
   666                  intro!: M2.positive_integral_cong arg_cong[where f=\<mu>1])
   667     qed
   668     fix X assume "X \<in> sets E"
   669     then obtain A B where X: "X = A \<times> B" and AB: "A \<in> sets M1" "B \<in> sets M2"
   670       unfolding pair_algebra_def by auto
   671     show "pair_measure X = ?\<nu> X"
   672     proof -
   673       from AB have "?\<nu> (A \<times> B) =
   674           M2.positive_integral (\<lambda>y. \<mu>1 A * indicator B y)"
   675         by (auto intro!: M2.positive_integral_cong)
   676       with AB show ?thesis
   677         unfolding pair_measure_times[OF AB] X
   678         by (simp add: M2.positive_integral_cmult_indicator ac_simps)
   679     qed
   680   qed fact
   681 qed
   682 
   683 section "Fubinis theorem"
   684 
   685 lemma (in pair_sigma_finite) simple_function_cut:
   686   assumes f: "simple_function f"
   687   shows "(\<lambda>x. M2.positive_integral (\<lambda> y. f (x, y))) \<in> borel_measurable M1"
   688     and "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. f (x, y)))
   689       = positive_integral f"
   690 proof -
   691   have f_borel: "f \<in> borel_measurable P"
   692     using f by (rule borel_measurable_simple_function)
   693   let "?F z" = "f -` {z} \<inter> space P"
   694   let "?F' x z" = "Pair x -` ?F z"
   695   { fix x assume "x \<in> space M1"
   696     have [simp]: "\<And>z y. indicator (?F z) (x, y) = indicator (?F' x z) y"
   697       by (auto simp: indicator_def)
   698     have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space P" using `x \<in> space M1`
   699       by (simp add: space_pair_algebra)
   700     moreover have "\<And>x z. ?F' x z \<in> sets M2" using f_borel
   701       by (intro borel_measurable_vimage measurable_cut_fst)
   702     ultimately have "M2.simple_function (\<lambda> y. f (x, y))"
   703       apply (rule_tac M2.simple_function_cong[THEN iffD2, OF _])
   704       apply (rule simple_function_indicator_representation[OF f])
   705       using `x \<in> space M1` by (auto simp del: space_sigma) }
   706   note M2_sf = this
   707   { fix x assume x: "x \<in> space M1"
   708     then have "M2.positive_integral (\<lambda> y. f (x, y)) =
   709         (\<Sum>z\<in>f ` space P. z * \<mu>2 (?F' x z))"
   710       unfolding M2.positive_integral_eq_simple_integral[OF M2_sf[OF x]]
   711       unfolding M2.simple_integral_def
   712     proof (safe intro!: setsum_mono_zero_cong_left)
   713       from f show "finite (f ` space P)" by (rule simple_functionD)
   714     next
   715       fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space P"
   716         using `x \<in> space M1` by (auto simp: space_pair_algebra)
   717     next
   718       fix x' y assume "(x', y) \<in> space P"
   719         "f (x', y) \<notin> (\<lambda>y. f (x, y)) ` space M2"
   720       then have *: "?F' x (f (x', y)) = {}"
   721         by (force simp: space_pair_algebra)
   722       show  "f (x', y) * \<mu>2 (?F' x (f (x', y))) = 0"
   723         unfolding * by simp
   724     qed (simp add: vimage_compose[symmetric] comp_def
   725                    space_pair_algebra) }
   726   note eq = this
   727   moreover have "\<And>z. ?F z \<in> sets P"
   728     by (auto intro!: f_borel borel_measurable_vimage simp del: space_sigma)
   729   moreover then have "\<And>z. (\<lambda>x. \<mu>2 (?F' x z)) \<in> borel_measurable M1"
   730     by (auto intro!: measure_cut_measurable_fst simp del: vimage_Int)
   731   ultimately
   732   show "(\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) \<in> borel_measurable M1"
   733     and "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. f (x, y)))
   734     = positive_integral f"
   735     by (auto simp del: vimage_Int cong: measurable_cong
   736              intro!: M1.borel_measurable_pextreal_setsum
   737              simp add: M1.positive_integral_setsum simple_integral_def
   738                        M1.positive_integral_cmult
   739                        M1.positive_integral_cong[OF eq]
   740                        positive_integral_eq_simple_integral[OF f]
   741                        pair_measure_alt[symmetric])
   742 qed
   743 
   744 lemma (in pair_sigma_finite) positive_integral_fst_measurable:
   745   assumes f: "f \<in> borel_measurable P"
   746   shows "(\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) \<in> borel_measurable M1"
   747       (is "?C f \<in> borel_measurable M1")
   748     and "M1.positive_integral (\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) =
   749       positive_integral f"
   750 proof -
   751   from borel_measurable_implies_simple_function_sequence[OF f]
   752   obtain F where F: "\<And>i. simple_function (F i)" "F \<up> f" by auto
   753   then have F_borel: "\<And>i. F i \<in> borel_measurable P"
   754     and F_mono: "\<And>i x. F i x \<le> F (Suc i) x"
   755     and F_SUPR: "\<And>x. (SUP i. F i x) = f x"
   756     unfolding isoton_fun_expand unfolding isoton_def le_fun_def
   757     by (auto intro: borel_measurable_simple_function)
   758   note sf = simple_function_cut[OF F(1)]
   759   then have "(\<lambda>x. SUP i. ?C (F i) x) \<in> borel_measurable M1"
   760     using F(1) by auto
   761   moreover
   762   { fix x assume "x \<in> space M1"
   763     have isotone: "(\<lambda> i y. F i (x, y)) \<up> (\<lambda>y. f (x, y))"
   764       using `F \<up> f` unfolding isoton_fun_expand
   765       by (auto simp: isoton_def)
   766     note measurable_pair_image_snd[OF F_borel`x \<in> space M1`]
   767     from M2.positive_integral_isoton[OF isotone this]
   768     have "(SUP i. ?C (F i) x) = ?C f x"
   769       by (simp add: isoton_def) }
   770   note SUPR_C = this
   771   ultimately show "?C f \<in> borel_measurable M1"
   772     by (simp cong: measurable_cong)
   773   have "positive_integral (\<lambda>x. (SUP i. F i x)) = (SUP i. positive_integral (F i))"
   774     using F_borel F_mono
   775     by (auto intro!: positive_integral_monotone_convergence_SUP[symmetric])
   776   also have "(SUP i. positive_integral (F i)) =
   777     (SUP i. M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. F i (x, y))))"
   778     unfolding sf(2) by simp
   779   also have "\<dots> = M1.positive_integral (\<lambda>x. SUP i. M2.positive_integral (\<lambda>y. F i (x, y)))"
   780     by (auto intro!: M1.positive_integral_monotone_convergence_SUP[OF _ sf(1)]
   781                      M2.positive_integral_mono F_mono)
   782   also have "\<dots> = M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. SUP i. F i (x, y)))"
   783     using F_borel F_mono
   784     by (auto intro!: M2.positive_integral_monotone_convergence_SUP
   785                      M1.positive_integral_cong measurable_pair_image_snd)
   786   finally show "M1.positive_integral (\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) =
   787       positive_integral f"
   788     unfolding F_SUPR by simp
   789 qed
   790 
   791 lemma (in pair_sigma_finite) positive_integral_product_swap:
   792   assumes f: "f \<in> borel_measurable P"
   793   shows "measure_space.positive_integral
   794     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x))) =
   795   positive_integral f"
   796 proof -
   797   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   798   have P: "sigma_algebra P" by default
   799   show ?thesis
   800     unfolding Q.positive_integral_vimage[OF P Q.pair_sigma_algebra_swap_measurable f, symmetric]
   801   proof (rule positive_integral_cong_measure)
   802     fix A
   803     assume A: "A \<in> sets P"
   804     from Q.pair_sigma_algebra_swap_measurable[THEN measurable_sets, OF this] this sets_into_space[OF this]
   805     show "Q.pair_measure ((\<lambda>(x, y). (y, x)) -` A \<inter> space Q.P) = pair_measure A"
   806       by (auto intro!: M1.positive_integral_cong arg_cong[where f=\<mu>2]
   807                simp: pair_measure_alt Q.pair_measure_alt2 space_pair_algebra)
   808   qed
   809 qed
   810 
   811 lemma (in pair_sigma_finite) positive_integral_snd_measurable:
   812   assumes f: "f \<in> borel_measurable P"
   813   shows "M2.positive_integral (\<lambda>y. M1.positive_integral (\<lambda>x. f (x, y))) =
   814       positive_integral f"
   815 proof -
   816   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   817   note pair_sigma_algebra_measurable[OF f]
   818   from Q.positive_integral_fst_measurable[OF this]
   819   have "M2.positive_integral (\<lambda>y. M1.positive_integral (\<lambda>x. f (x, y))) =
   820     Q.positive_integral (\<lambda>(x, y). f (y, x))"
   821     by simp
   822   also have "Q.positive_integral (\<lambda>(x, y). f (y, x)) = positive_integral f"
   823     unfolding positive_integral_product_swap[OF f, symmetric]
   824     by (auto intro!: Q.positive_integral_cong)
   825   finally show ?thesis .
   826 qed
   827 
   828 lemma (in pair_sigma_finite) Fubini:
   829   assumes f: "f \<in> borel_measurable P"
   830   shows "M2.positive_integral (\<lambda>y. M1.positive_integral (\<lambda>x. f (x, y))) =
   831       M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. f (x, y)))"
   832   unfolding positive_integral_snd_measurable[OF assms]
   833   unfolding positive_integral_fst_measurable[OF assms] ..
   834 
   835 lemma (in pair_sigma_finite) AE_pair:
   836   assumes "almost_everywhere (\<lambda>x. Q x)"
   837   shows "M1.almost_everywhere (\<lambda>x. M2.almost_everywhere (\<lambda>y. Q (x, y)))"
   838 proof -
   839   obtain N where N: "N \<in> sets P" "pair_measure N = 0" "{x\<in>space P. \<not> Q x} \<subseteq> N"
   840     using assms unfolding almost_everywhere_def by auto
   841   show ?thesis
   842   proof (rule M1.AE_I)
   843     from N measure_cut_measurable_fst[OF `N \<in> sets P`]
   844     show "\<mu>1 {x\<in>space M1. \<mu>2 (Pair x -` N) \<noteq> 0} = 0"
   845       by (simp add: M1.positive_integral_0_iff pair_measure_alt vimage_def)
   846     show "{x \<in> space M1. \<mu>2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   847       by (intro M1.borel_measurable_pextreal_neq_const measure_cut_measurable_fst N)
   848     { fix x assume "x \<in> space M1" "\<mu>2 (Pair x -` N) = 0"
   849       have "M2.almost_everywhere (\<lambda>y. Q (x, y))"
   850       proof (rule M2.AE_I)
   851         show "\<mu>2 (Pair x -` N) = 0" by fact
   852         show "Pair x -` N \<in> sets M2" by (intro measurable_cut_fst N)
   853         show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
   854           using N `x \<in> space M1` unfolding space_sigma space_pair_algebra by auto
   855       qed }
   856     then show "{x \<in> space M1. \<not> M2.almost_everywhere (\<lambda>y. Q (x, y))} \<subseteq> {x \<in> space M1. \<mu>2 (Pair x -` N) \<noteq> 0}"
   857       by auto
   858   qed
   859 qed
   860 
   861 lemma (in pair_sigma_algebra) measurable_product_swap:
   862   "f \<in> measurable (sigma (pair_algebra M2 M1)) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable P M"
   863 proof -
   864   interpret Q: pair_sigma_algebra M2 M1 by default
   865   show ?thesis
   866     using pair_sigma_algebra_measurable[of "\<lambda>(x,y). f (y, x)"]
   867     by (auto intro!: pair_sigma_algebra_measurable Q.pair_sigma_algebra_measurable iffI)
   868 qed
   869 
   870 lemma (in pair_sigma_finite) integrable_product_swap:
   871   assumes "integrable f"
   872   shows "measure_space.integrable
   873     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) (\<lambda>(x,y). f (y,x))"
   874 proof -
   875   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   876   have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
   877   show ?thesis unfolding *
   878     using assms unfolding Q.integrable_def integrable_def
   879     apply (subst (1 2) positive_integral_product_swap)
   880     using `integrable f` unfolding integrable_def
   881     by (auto simp: *[symmetric] Q.measurable_product_swap[symmetric])
   882 qed
   883 
   884 lemma (in pair_sigma_finite) integrable_product_swap_iff:
   885   "measure_space.integrable
   886     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) (\<lambda>(x,y). f (y,x)) \<longleftrightarrow>
   887   integrable f"
   888 proof -
   889   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   890   from Q.integrable_product_swap[of "\<lambda>(x,y). f (y,x)"] integrable_product_swap[of f]
   891   show ?thesis by auto
   892 qed
   893 
   894 lemma (in pair_sigma_finite) integral_product_swap:
   895   assumes "integrable f"
   896   shows "measure_space.integral
   897     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) (\<lambda>(x,y). f (y,x)) =
   898   integral f"
   899 proof -
   900   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   901   have *: "(\<lambda>(x,y). f (y,x)) = (\<lambda>x. f (case x of (x,y)\<Rightarrow>(y,x)))" by (auto simp: fun_eq_iff)
   902   show ?thesis
   903     unfolding integral_def Q.integral_def *
   904     apply (subst (1 2) positive_integral_product_swap)
   905     using `integrable f` unfolding integrable_def
   906     by (auto simp: *[symmetric] Q.measurable_product_swap[symmetric])
   907 qed
   908 
   909 lemma (in pair_sigma_finite) integrable_fst_measurable:
   910   assumes f: "integrable f"
   911   shows "M1.almost_everywhere (\<lambda>x. M2.integrable (\<lambda> y. f (x, y)))" (is "?AE")
   912     and "M1.integral (\<lambda> x. M2.integral (\<lambda> y. f (x, y))) = integral f" (is "?INT")
   913 proof -
   914   let "?pf x" = "Real (f x)" and "?nf x" = "Real (- f x)"
   915   have
   916     borel: "?nf \<in> borel_measurable P""?pf \<in> borel_measurable P" and
   917     int: "positive_integral ?nf \<noteq> \<omega>" "positive_integral ?pf \<noteq> \<omega>"
   918     using assms by auto
   919   have "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. Real (f (x, y)))) \<noteq> \<omega>"
   920      "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. Real (- f (x, y)))) \<noteq> \<omega>"
   921     using borel[THEN positive_integral_fst_measurable(1)] int
   922     unfolding borel[THEN positive_integral_fst_measurable(2)] by simp_all
   923   with borel[THEN positive_integral_fst_measurable(1)]
   924   have AE: "M1.almost_everywhere (\<lambda>x. M2.positive_integral (\<lambda>y. Real (f (x, y))) \<noteq> \<omega>)"
   925     "M1.almost_everywhere (\<lambda>x. M2.positive_integral (\<lambda>y. Real (- f (x, y))) \<noteq> \<omega>)"
   926     by (auto intro!: M1.positive_integral_omega_AE)
   927   then show ?AE
   928     apply (rule M1.AE_mp[OF _ M1.AE_mp])
   929     apply (rule M1.AE_cong)
   930     using assms unfolding M2.integrable_def
   931     by (auto intro!: measurable_pair_image_snd)
   932   have "M1.integrable
   933      (\<lambda>x. real (M2.positive_integral (\<lambda>xa. Real (f (x, xa)))))" (is "M1.integrable ?f")
   934   proof (unfold M1.integrable_def, intro conjI)
   935     show "?f \<in> borel_measurable M1"
   936       using borel by (auto intro!: M1.borel_measurable_real positive_integral_fst_measurable)
   937     have "M1.positive_integral (\<lambda>x. Real (?f x)) =
   938         M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>xa. Real (f (x, xa))))"
   939       apply (rule M1.positive_integral_cong_AE)
   940       apply (rule M1.AE_mp[OF AE(1)])
   941       apply (rule M1.AE_cong)
   942       by (auto simp: Real_real)
   943     then show "M1.positive_integral (\<lambda>x. Real (?f x)) \<noteq> \<omega>"
   944       using positive_integral_fst_measurable[OF borel(2)] int by simp
   945     have "M1.positive_integral (\<lambda>x. Real (- ?f x)) = M1.positive_integral (\<lambda>x. 0)"
   946       by (intro M1.positive_integral_cong) simp
   947     then show "M1.positive_integral (\<lambda>x. Real (- ?f x)) \<noteq> \<omega>" by simp
   948   qed
   949   moreover have "M1.integrable
   950      (\<lambda>x. real (M2.positive_integral (\<lambda>xa. Real (- f (x, xa)))))" (is "M1.integrable ?f")
   951   proof (unfold M1.integrable_def, intro conjI)
   952     show "?f \<in> borel_measurable M1"
   953       using borel by (auto intro!: M1.borel_measurable_real positive_integral_fst_measurable)
   954     have "M1.positive_integral (\<lambda>x. Real (?f x)) =
   955         M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>xa. Real (- f (x, xa))))"
   956       apply (rule M1.positive_integral_cong_AE)
   957       apply (rule M1.AE_mp[OF AE(2)])
   958       apply (rule M1.AE_cong)
   959       by (auto simp: Real_real)
   960     then show "M1.positive_integral (\<lambda>x. Real (?f x)) \<noteq> \<omega>"
   961       using positive_integral_fst_measurable[OF borel(1)] int by simp
   962     have "M1.positive_integral (\<lambda>x. Real (- ?f x)) = M1.positive_integral (\<lambda>x. 0)"
   963       by (intro M1.positive_integral_cong) simp
   964     then show "M1.positive_integral (\<lambda>x. Real (- ?f x)) \<noteq> \<omega>" by simp
   965   qed
   966   ultimately show ?INT
   967     unfolding M2.integral_def integral_def
   968       borel[THEN positive_integral_fst_measurable(2), symmetric]
   969     by (simp add: M1.integral_real[OF AE(1)] M1.integral_real[OF AE(2)])
   970 qed
   971 
   972 lemma (in pair_sigma_finite) integrable_snd_measurable:
   973   assumes f: "integrable f"
   974   shows "M2.almost_everywhere (\<lambda>y. M1.integrable (\<lambda>x. f (x, y)))" (is "?AE")
   975     and "M2.integral (\<lambda>y. M1.integral (\<lambda>x. f (x, y))) = integral f" (is "?INT")
   976 proof -
   977   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   978   have Q_int: "Q.integrable (\<lambda>(x, y). f (y, x))"
   979     using f unfolding integrable_product_swap_iff .
   980   show ?INT
   981     using Q.integrable_fst_measurable(2)[OF Q_int]
   982     using integral_product_swap[OF f] by simp
   983   show ?AE
   984     using Q.integrable_fst_measurable(1)[OF Q_int]
   985     by simp
   986 qed
   987 
   988 lemma (in pair_sigma_finite) Fubini_integral:
   989   assumes f: "integrable f"
   990   shows "M2.integral (\<lambda>y. M1.integral (\<lambda>x. f (x, y))) =
   991       M1.integral (\<lambda>x. M2.integral (\<lambda>y. f (x, y)))"
   992   unfolding integrable_snd_measurable[OF assms]
   993   unfolding integrable_fst_measurable[OF assms] ..
   994 
   995 section "Finite product spaces"
   996 
   997 section "Products"
   998 
   999 locale product_sigma_algebra =
  1000   fixes M :: "'i \<Rightarrow> 'a algebra"
  1001   assumes sigma_algebras: "\<And>i. sigma_algebra (M i)"
  1002 
  1003 locale finite_product_sigma_algebra = product_sigma_algebra M for M :: "'i \<Rightarrow> 'a algebra" +
  1004   fixes I :: "'i set"
  1005   assumes finite_index: "finite I"
  1006 
  1007 syntax
  1008   "_PiE"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PIE _:_./ _)" 10)
  1009 
  1010 syntax (xsymbols)
  1011   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
  1012 
  1013 syntax (HTML output)
  1014   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
  1015 
  1016 translations
  1017   "PIE x:A. B" == "CONST Pi\<^isub>E A (%x. B)"
  1018 
  1019 definition
  1020   "product_algebra M I = \<lparr> space = (\<Pi>\<^isub>E i\<in>I. space (M i)), sets = Pi\<^isub>E I ` (\<Pi> i \<in> I. sets (M i)) \<rparr>"
  1021 
  1022 abbreviation (in finite_product_sigma_algebra) "G \<equiv> product_algebra M I"
  1023 abbreviation (in finite_product_sigma_algebra) "P \<equiv> sigma G"
  1024 
  1025 sublocale product_sigma_algebra \<subseteq> M: sigma_algebra "M i" for i by (rule sigma_algebras)
  1026 
  1027 lemma (in finite_product_sigma_algebra) product_algebra_into_space:
  1028   "sets G \<subseteq> Pow (space G)"
  1029   using M.sets_into_space unfolding product_algebra_def
  1030   by auto blast
  1031 
  1032 sublocale finite_product_sigma_algebra \<subseteq> sigma_algebra P
  1033   using product_algebra_into_space by (rule sigma_algebra_sigma)
  1034 
  1035 lemma product_algebraE:
  1036   assumes "A \<in> sets (product_algebra M I)"
  1037   obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
  1038   using assms unfolding product_algebra_def by auto
  1039 
  1040 lemma product_algebraI[intro]:
  1041   assumes "E \<in> (\<Pi> i\<in>I. sets (M i))"
  1042   shows "Pi\<^isub>E I E \<in> sets (product_algebra M I)"
  1043   using assms unfolding product_algebra_def by auto
  1044 
  1045 lemma space_product_algebra[simp]:
  1046   "space (product_algebra M I) = Pi\<^isub>E I (\<lambda>i. space (M i))"
  1047   unfolding product_algebra_def by simp
  1048 
  1049 lemma product_algebra_sets_into_space:
  1050   assumes "\<And>i. i\<in>I \<Longrightarrow> sets (M i) \<subseteq> Pow (space (M i))"
  1051   shows "sets (product_algebra M I) \<subseteq> Pow (space (product_algebra M I))"
  1052   using assms by (auto simp: product_algebra_def) blast
  1053 
  1054 lemma (in finite_product_sigma_algebra) P_empty:
  1055   "I = {} \<Longrightarrow> P = \<lparr> space = {\<lambda>k. undefined}, sets = { {}, {\<lambda>k. undefined} }\<rparr>"
  1056   unfolding product_algebra_def by (simp add: sigma_def image_constant)
  1057 
  1058 lemma (in finite_product_sigma_algebra) in_P[simp, intro]:
  1059   "\<lbrakk> \<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i) \<rbrakk> \<Longrightarrow> Pi\<^isub>E I A \<in> sets P"
  1060   by (auto simp: product_algebra_def sets_sigma intro!: sigma_sets.Basic)
  1061 
  1062 lemma (in product_sigma_algebra) bij_inv_restrict_merge:
  1063   assumes [simp]: "I \<inter> J = {}"
  1064   shows "bij_inv
  1065     (space (sigma (product_algebra M (I \<union> J))))
  1066     (space (sigma (pair_algebra (product_algebra M I) (product_algebra M J))))
  1067     (\<lambda>x. (restrict x I, restrict x J)) (\<lambda>(x, y). merge I x J y)"
  1068   by (rule bij_invI) (auto simp: space_pair_algebra extensional_restrict)
  1069 
  1070 lemma (in product_sigma_algebra) bij_inv_singleton:
  1071   "bij_inv (space (sigma (product_algebra M {i}))) (space (M i))
  1072     (\<lambda>x. x i) (\<lambda>x. (\<lambda>j\<in>{i}. x))"
  1073   by (rule bij_invI) (auto simp: restrict_def extensional_def fun_eq_iff)
  1074 
  1075 lemma (in product_sigma_algebra) bij_inv_restrict_insert:
  1076   assumes [simp]: "i \<notin> I"
  1077   shows "bij_inv
  1078     (space (sigma (product_algebra M (insert i I))))
  1079     (space (sigma (pair_algebra (product_algebra M I) (M i))))
  1080     (\<lambda>x. (restrict x I, x i)) (\<lambda>(x, y). x(i := y))"
  1081   by (rule bij_invI) (auto simp: space_pair_algebra extensional_restrict)
  1082 
  1083 lemma (in product_sigma_algebra) measurable_restrict_on_generating:
  1084   assumes [simp]: "I \<inter> J = {}"
  1085   shows "(\<lambda>x. (restrict x I, restrict x J)) \<in> measurable
  1086     (product_algebra M (I \<union> J))
  1087     (pair_algebra (product_algebra M I) (product_algebra M J))"
  1088     (is "?R \<in> measurable ?IJ ?P")
  1089 proof (unfold measurable_def, intro CollectI conjI ballI)
  1090   show "?R \<in> space ?IJ \<rightarrow> space ?P" by (auto simp: space_pair_algebra)
  1091   { fix F E assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "F \<in> (\<Pi> i\<in>J. sets (M i))"
  1092     then have "Pi (I \<union> J) (merge I E J F) \<inter> (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) =
  1093         Pi\<^isub>E (I \<union> J) (merge I E J F)"
  1094       using M.sets_into_space by auto blast+ }
  1095   note this[simp]
  1096   show "\<And>A. A \<in> sets ?P \<Longrightarrow> ?R -` A \<inter> space ?IJ \<in> sets ?IJ"
  1097     by (force elim!: pair_algebraE product_algebraE
  1098               simp del: vimage_Int simp: restrict_vimage merge_vimage space_pair_algebra)
  1099   qed
  1100 
  1101 lemma (in product_sigma_algebra) measurable_merge_on_generating:
  1102   assumes [simp]: "I \<inter> J = {}"
  1103   shows "(\<lambda>(x, y). merge I x J y) \<in> measurable
  1104     (pair_algebra (product_algebra M I) (product_algebra M J))
  1105     (product_algebra M (I \<union> J))"
  1106     (is "?M \<in> measurable ?P ?IJ")
  1107 proof (unfold measurable_def, intro CollectI conjI ballI)
  1108   show "?M \<in> space ?P \<rightarrow> space ?IJ" by (auto simp: space_pair_algebra)
  1109   { fix E assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "E \<in> (\<Pi> i\<in>J. sets (M i))"
  1110     then have "Pi I E \<times> Pi J E \<inter> (\<Pi>\<^isub>E i\<in>I. space (M i)) \<times> (\<Pi>\<^isub>E i\<in>J. space (M i)) =
  1111         Pi\<^isub>E I E \<times> Pi\<^isub>E J E"
  1112       using M.sets_into_space by auto blast+ }
  1113   note this[simp]
  1114   show "\<And>A. A \<in> sets ?IJ \<Longrightarrow> ?M -` A \<inter> space ?P \<in> sets ?P"
  1115     by (force elim!: pair_algebraE product_algebraE
  1116               simp del: vimage_Int simp: restrict_vimage merge_vimage space_pair_algebra)
  1117   qed
  1118 
  1119 lemma (in product_sigma_algebra) measurable_singleton_on_generator:
  1120   "(\<lambda>x. \<lambda>j\<in>{i}. x) \<in> measurable (M i) (product_algebra M {i})"
  1121   (is "?f \<in> measurable _ ?P")
  1122 proof (unfold measurable_def, intro CollectI conjI)
  1123   show "?f \<in> space (M i) \<rightarrow> space ?P" by auto
  1124   have "\<And>E. E i \<in> sets (M i) \<Longrightarrow> ?f -` Pi\<^isub>E {i} E \<inter> space (M i) = E i"
  1125     using M.sets_into_space by auto
  1126   then show "\<forall>A \<in> sets ?P. ?f -` A \<inter> space (M i) \<in> sets (M i)"
  1127     by (auto elim!: product_algebraE)
  1128 qed
  1129 
  1130 lemma (in product_sigma_algebra) measurable_component_on_generator:
  1131   assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (product_algebra M I) (M i)"
  1132   (is "?f \<in> measurable ?P _")
  1133 proof (unfold measurable_def, intro CollectI conjI ballI)
  1134   show "?f \<in> space ?P \<rightarrow> space (M i)" using `i \<in> I` by auto
  1135   fix A assume "A \<in> sets (M i)"
  1136   moreover then have "(\<lambda>x. x i) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. space (M i)) =
  1137       (\<Pi>\<^isub>E j\<in>I. if i = j then A else space (M j))"
  1138     using M.sets_into_space `i \<in> I`
  1139     by (fastsimp dest: Pi_mem split: split_if_asm)
  1140   ultimately show "?f -` A \<inter> space ?P \<in> sets ?P"
  1141     by (auto intro!: product_algebraI)
  1142 qed
  1143 
  1144 lemma (in product_sigma_algebra) measurable_restrict_singleton_on_generating:
  1145   assumes [simp]: "i \<notin> I"
  1146   shows "(\<lambda>x. (restrict x I, x i)) \<in> measurable
  1147     (product_algebra M (insert i I))
  1148     (pair_algebra (product_algebra M I) (M i))"
  1149     (is "?R \<in> measurable ?I ?P")
  1150 proof (unfold measurable_def, intro CollectI conjI ballI)
  1151   show "?R \<in> space ?I \<rightarrow> space ?P" by (auto simp: space_pair_algebra)
  1152   { fix E F assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "F \<in> sets (M i)"
  1153     then have "(\<lambda>x. (restrict x I, x i)) -` (Pi\<^isub>E I E \<times> F) \<inter> (\<Pi>\<^isub>E i\<in>insert i I. space (M i)) =
  1154         Pi\<^isub>E (insert i I) (E(i := F))"
  1155       using M.sets_into_space using `i\<notin>I` by (auto simp: restrict_Pi_cancel) blast+ }
  1156   note this[simp]
  1157   show "\<And>A. A \<in> sets ?P \<Longrightarrow> ?R -` A \<inter> space ?I \<in> sets ?I"
  1158     by (force elim!: pair_algebraE product_algebraE
  1159               simp del: vimage_Int simp: space_pair_algebra)
  1160 qed
  1161 
  1162 lemma (in product_sigma_algebra) measurable_merge_singleton_on_generating:
  1163   assumes [simp]: "i \<notin> I"
  1164   shows "(\<lambda>(x, y). x(i := y)) \<in> measurable
  1165     (pair_algebra (product_algebra M I) (M i))
  1166     (product_algebra M (insert i I))"
  1167     (is "?M \<in> measurable ?P ?IJ")
  1168 proof (unfold measurable_def, intro CollectI conjI ballI)
  1169   show "?M \<in> space ?P \<rightarrow> space ?IJ" by (auto simp: space_pair_algebra)
  1170   { fix E assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "E i \<in> sets (M i)"
  1171     then have "(\<lambda>(x, y). x(i := y)) -` Pi\<^isub>E (insert i I) E \<inter> (\<Pi>\<^isub>E i\<in>I. space (M i)) \<times> space (M i) =
  1172         Pi\<^isub>E I E \<times> E i"
  1173       using M.sets_into_space by auto blast+ }
  1174   note this[simp]
  1175   show "\<And>A. A \<in> sets ?IJ \<Longrightarrow> ?M -` A \<inter> space ?P \<in> sets ?P"
  1176     by (force elim!: pair_algebraE product_algebraE
  1177               simp del: vimage_Int simp: space_pair_algebra)
  1178 qed
  1179 
  1180 section "Generating set generates also product algebra"
  1181 
  1182 lemma pair_sigma_algebra_sigma:
  1183   assumes 1: "S1 \<up> (space E1)" "range S1 \<subseteq> sets E1" and E1: "sets E1 \<subseteq> Pow (space E1)"
  1184   assumes 2: "S2 \<up> (space E2)" "range S2 \<subseteq> sets E2" and E2: "sets E2 \<subseteq> Pow (space E2)"
  1185   shows "sigma (pair_algebra (sigma E1) (sigma E2)) = sigma (pair_algebra E1 E2)"
  1186     (is "?S = ?E")
  1187 proof -
  1188   interpret M1: sigma_algebra "sigma E1" using E1 by (rule sigma_algebra_sigma)
  1189   interpret M2: sigma_algebra "sigma E2" using E2 by (rule sigma_algebra_sigma)
  1190   have P: "sets (pair_algebra E1 E2) \<subseteq> Pow (space E1 \<times> space E2)"
  1191     using E1 E2 by (auto simp add: pair_algebra_def)
  1192   interpret E: sigma_algebra ?E unfolding pair_algebra_def
  1193     using E1 E2 by (intro sigma_algebra_sigma) auto
  1194   { fix A assume "A \<in> sets E1"
  1195     then have "fst -` A \<inter> space ?E = A \<times> (\<Union>i. S2 i)"
  1196       using E1 2 unfolding isoton_def pair_algebra_def by auto
  1197     also have "\<dots> = (\<Union>i. A \<times> S2 i)" by auto
  1198     also have "\<dots> \<in> sets ?E" unfolding pair_algebra_def sets_sigma
  1199       using 2 `A \<in> sets E1`
  1200       by (intro sigma_sets.Union)
  1201          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
  1202     finally have "fst -` A \<inter> space ?E \<in> sets ?E" . }
  1203   moreover
  1204   { fix B assume "B \<in> sets E2"
  1205     then have "snd -` B \<inter> space ?E = (\<Union>i. S1 i) \<times> B"
  1206       using E2 1 unfolding isoton_def pair_algebra_def by auto
  1207     also have "\<dots> = (\<Union>i. S1 i \<times> B)" by auto
  1208     also have "\<dots> \<in> sets ?E"
  1209       using 1 `B \<in> sets E2` unfolding pair_algebra_def sets_sigma
  1210       by (intro sigma_sets.Union)
  1211          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
  1212     finally have "snd -` B \<inter> space ?E \<in> sets ?E" . }
  1213   ultimately have proj:
  1214     "fst \<in> measurable ?E (sigma E1) \<and> snd \<in> measurable ?E (sigma E2)"
  1215     using E1 E2 by (subst (1 2) E.measurable_iff_sigma)
  1216                    (auto simp: pair_algebra_def sets_sigma)
  1217   { fix A B assume A: "A \<in> sets (sigma E1)" and B: "B \<in> sets (sigma E2)"
  1218     with proj have "fst -` A \<inter> space ?E \<in> sets ?E" "snd -` B \<inter> space ?E \<in> sets ?E"
  1219       unfolding measurable_def by simp_all
  1220     moreover have "A \<times> B = (fst -` A \<inter> space ?E) \<inter> (snd -` B \<inter> space ?E)"
  1221       using A B M1.sets_into_space M2.sets_into_space
  1222       by (auto simp: pair_algebra_def)
  1223     ultimately have "A \<times> B \<in> sets ?E" by auto }
  1224   then have "sigma_sets (space ?E) (sets (pair_algebra (sigma E1) (sigma E2))) \<subseteq> sets ?E"
  1225     by (intro E.sigma_sets_subset) (auto simp add: pair_algebra_def sets_sigma)
  1226   then have subset: "sets ?S \<subseteq> sets ?E"
  1227     by (simp add: sets_sigma pair_algebra_def)
  1228   have "sets ?S = sets ?E"
  1229   proof (intro set_eqI iffI)
  1230     fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
  1231       unfolding sets_sigma
  1232     proof induct
  1233       case (Basic A) then show ?case
  1234         by (auto simp: pair_algebra_def sets_sigma intro: sigma_sets.Basic)
  1235     qed (auto intro: sigma_sets.intros simp: pair_algebra_def)
  1236   next
  1237     fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
  1238   qed
  1239   then show ?thesis
  1240     by (simp add: pair_algebra_def sigma_def)
  1241 qed
  1242 
  1243 lemma sigma_product_algebra_sigma_eq:
  1244   assumes "finite I"
  1245   assumes isotone: "\<And>i. i \<in> I \<Longrightarrow> (S i) \<up> (space (E i))"
  1246   assumes sets_into: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> sets (E i)"
  1247   and E: "\<And>i. sets (E i) \<subseteq> Pow (space (E i))"
  1248   shows "sigma (product_algebra (\<lambda>i. sigma (E i)) I) = sigma (product_algebra E I)"
  1249     (is "?S = ?E")
  1250 proof cases
  1251   assume "I = {}" then show ?thesis by (simp add: product_algebra_def)
  1252 next
  1253   assume "I \<noteq> {}"
  1254   interpret E: sigma_algebra "sigma (E i)" for i
  1255     using E by (rule sigma_algebra_sigma)
  1256 
  1257   have into_space[intro]: "\<And>i x A. A \<in> sets (E i) \<Longrightarrow> x i \<in> A \<Longrightarrow> x i \<in> space (E i)"
  1258     using E by auto
  1259 
  1260   interpret G: sigma_algebra ?E
  1261     unfolding product_algebra_def using E
  1262     by (intro sigma_algebra_sigma) (auto dest: Pi_mem)
  1263 
  1264   { fix A i assume "i \<in> I" and A: "A \<in> sets (E i)"
  1265     then have "(\<lambda>x. x i) -` A \<inter> space ?E = (\<Pi>\<^isub>E j\<in>I. if j = i then A else \<Union>n. S j n) \<inter> space ?E"
  1266       using isotone unfolding isoton_def product_algebra_def by (auto dest: Pi_mem)
  1267     also have "\<dots> = (\<Union>n. (\<Pi>\<^isub>E j\<in>I. if j = i then A else S j n))"
  1268       unfolding product_algebra_def
  1269       apply simp
  1270       apply (subst Pi_UN[OF `finite I`])
  1271       using isotone[THEN isoton_mono_le] apply simp
  1272       apply (simp add: PiE_Int)
  1273       apply (intro PiE_cong)
  1274       using A sets_into by (auto intro!: into_space)
  1275     also have "\<dots> \<in> sets ?E" unfolding product_algebra_def sets_sigma
  1276       using sets_into `A \<in> sets (E i)`
  1277       by (intro sigma_sets.Union)
  1278          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
  1279     finally have "(\<lambda>x. x i) -` A \<inter> space ?E \<in> sets ?E" . }
  1280   then have proj:
  1281     "\<And>i. i\<in>I \<Longrightarrow> (\<lambda>x. x i) \<in> measurable ?E (sigma (E i))"
  1282     using E by (subst G.measurable_iff_sigma)
  1283                (auto simp: product_algebra_def sets_sigma)
  1284 
  1285   { fix A assume A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (sigma (E i))"
  1286     with proj have basic: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) -` (A i) \<inter> space ?E \<in> sets ?E"
  1287       unfolding measurable_def by simp
  1288     have "Pi\<^isub>E I A = (\<Inter>i\<in>I. (\<lambda>x. x i) -` (A i) \<inter> space ?E)"
  1289       using A E.sets_into_space `I \<noteq> {}` unfolding product_algebra_def by auto blast
  1290     then have "Pi\<^isub>E I A \<in> sets ?E"
  1291       using G.finite_INT[OF `finite I` `I \<noteq> {}` basic, of "\<lambda>i. i"] by simp }
  1292   then have "sigma_sets (space ?E) (sets (product_algebra (\<lambda>i. sigma (E i)) I)) \<subseteq> sets ?E"
  1293     by (intro G.sigma_sets_subset) (auto simp add: sets_sigma product_algebra_def)
  1294   then have subset: "sets ?S \<subseteq> sets ?E"
  1295     by (simp add: sets_sigma product_algebra_def)
  1296 
  1297   have "sets ?S = sets ?E"
  1298   proof (intro set_eqI iffI)
  1299     fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
  1300       unfolding sets_sigma
  1301     proof induct
  1302       case (Basic A) then show ?case
  1303         by (auto simp: sets_sigma product_algebra_def intro: sigma_sets.Basic)
  1304     qed (auto intro: sigma_sets.intros simp: product_algebra_def)
  1305   next
  1306     fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
  1307   qed
  1308   then show ?thesis
  1309     by (simp add: product_algebra_def sigma_def)
  1310 qed
  1311 
  1312 lemma (in product_sigma_algebra) sigma_pair_algebra_sigma_eq:
  1313   "sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))) =
  1314    sigma (pair_algebra (product_algebra M I) (product_algebra M J))"
  1315   using M.sets_into_space
  1316   by (intro pair_sigma_algebra_sigma[of "\<lambda>_. \<Pi>\<^isub>E i\<in>I. space (M i)", of _ "\<lambda>_. \<Pi>\<^isub>E i\<in>J. space (M i)"])
  1317      (auto simp: isoton_const product_algebra_def, blast+)
  1318 
  1319 lemma (in product_sigma_algebra) sigma_pair_algebra_product_singleton:
  1320   "sigma (pair_algebra (sigma (product_algebra M I)) (M i)) =
  1321    sigma (pair_algebra (product_algebra M I) (M i))"
  1322   using M.sets_into_space apply (subst M.sigma_eq[symmetric])
  1323   by (intro pair_sigma_algebra_sigma[of "\<lambda>_. \<Pi>\<^isub>E i\<in>I. space (M i)" _ "\<lambda>_. space (M i)"])
  1324      (auto simp: isoton_const product_algebra_def, blast+)
  1325 
  1326 lemma (in product_sigma_algebra) measurable_restrict:
  1327   assumes [simp]: "I \<inter> J = {}"
  1328   shows "(\<lambda>x. (restrict x I, restrict x J)) \<in> measurable
  1329     (sigma (product_algebra M (I \<union> J)))
  1330     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))"
  1331   unfolding sigma_pair_algebra_sigma_eq using M.sets_into_space
  1332   by (intro measurable_sigma_sigma measurable_restrict_on_generating
  1333             pair_algebra_sets_into_space product_algebra_sets_into_space)
  1334      auto
  1335 
  1336 lemma (in product_sigma_algebra) measurable_merge:
  1337   assumes [simp]: "I \<inter> J = {}"
  1338   shows "(\<lambda>(x, y). merge I x J y) \<in> measurable
  1339     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))
  1340     (sigma (product_algebra M (I \<union> J)))"
  1341   unfolding sigma_pair_algebra_sigma_eq using M.sets_into_space
  1342   by (intro measurable_sigma_sigma measurable_merge_on_generating
  1343             pair_algebra_sets_into_space product_algebra_sets_into_space)
  1344      auto
  1345 
  1346 lemma (in product_sigma_algebra) pair_product_product_vimage_algebra:
  1347   assumes [simp]: "I \<inter> J = {}"
  1348   shows "sigma_algebra.vimage_algebra (sigma (product_algebra M (I \<union> J)))
  1349     (space (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))) (\<lambda>(x,y). merge I x J y) =
  1350     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))"
  1351   unfolding sigma_pair_algebra_sigma_eq using sets_into_space
  1352   by (intro vimage_algebra_sigma[OF bij_inv_restrict_merge[symmetric]]
  1353             pair_algebra_sets_into_space product_algebra_sets_into_space
  1354             measurable_merge_on_generating measurable_restrict_on_generating)
  1355      auto
  1356 
  1357 lemma (in product_sigma_algebra) measurable_restrict_iff:
  1358   assumes IJ[simp]: "I \<inter> J = {}"
  1359   shows "f \<in> measurable (sigma (pair_algebra
  1360       (sigma (product_algebra M I)) (sigma (product_algebra M J)))) M' \<longleftrightarrow>
  1361     (\<lambda>x. f (restrict x I, restrict x J)) \<in> measurable (sigma (product_algebra M (I \<union> J))) M'"
  1362   using M.sets_into_space
  1363   apply (subst pair_product_product_vimage_algebra[OF IJ, symmetric])
  1364   apply (subst sigma_pair_algebra_sigma_eq)
  1365   apply (subst sigma_algebra.measurable_vimage_iff_inv[OF _
  1366       bij_inv_restrict_merge[symmetric]])
  1367   apply (intro sigma_algebra_sigma product_algebra_sets_into_space)
  1368   by auto
  1369 
  1370 lemma (in product_sigma_algebra) measurable_merge_iff:
  1371   assumes IJ: "I \<inter> J = {}"
  1372   shows "f \<in> measurable (sigma (product_algebra M (I \<union> J))) M' \<longleftrightarrow>
  1373     (\<lambda>(x, y). f (merge I x J y)) \<in>
  1374       measurable (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J)))) M'"
  1375   unfolding measurable_restrict_iff[OF IJ]
  1376   by (rule measurable_cong) (auto intro!: arg_cong[where f=f] simp: extensional_restrict)
  1377 
  1378 lemma (in product_sigma_algebra) measurable_component:
  1379   assumes "i \<in> I" and f: "f \<in> measurable (M i) M'"
  1380   shows "(\<lambda>x. f (x i)) \<in> measurable (sigma (product_algebra M I)) M'"
  1381     (is "?f \<in> measurable ?P M'")
  1382 proof -
  1383   have "f \<circ> (\<lambda>x. x i) \<in> measurable ?P M'"
  1384     apply (rule measurable_comp[OF _ f])
  1385     using measurable_up_sigma[of "product_algebra M I" "M i"]
  1386     using measurable_component_on_generator[OF `i \<in> I`]
  1387     by auto
  1388   then show "?f \<in> measurable ?P M'" by (simp add: comp_def)
  1389 qed
  1390 
  1391 lemma (in product_sigma_algebra) singleton_vimage_algebra:
  1392   "sigma_algebra.vimage_algebra (sigma (product_algebra M {i})) (space (M i)) (\<lambda>x. \<lambda>j\<in>{i}. x) = M i"
  1393   using sets_into_space
  1394   by (intro vimage_algebra_sigma[of "M i", unfolded M.sigma_eq, OF bij_inv_singleton[symmetric]]
  1395             product_algebra_sets_into_space measurable_singleton_on_generator measurable_component_on_generator)
  1396      auto
  1397 
  1398 lemma (in product_sigma_algebra) measurable_component_singleton:
  1399   "(\<lambda>x. f (x i)) \<in> measurable (sigma (product_algebra M {i})) M' \<longleftrightarrow>
  1400     f \<in> measurable (M i) M'"
  1401   using sets_into_space
  1402   apply (subst singleton_vimage_algebra[symmetric])
  1403   apply (subst sigma_algebra.measurable_vimage_iff_inv[OF _ bij_inv_singleton[symmetric]])
  1404   by (auto intro!: sigma_algebra_sigma product_algebra_sets_into_space)
  1405 
  1406 lemma (in product_sigma_algebra) measurable_component_iff:
  1407   assumes "i \<in> I" and not_empty: "\<forall>i\<in>I. space (M i) \<noteq> {}"
  1408   shows "(\<lambda>x. f (x i)) \<in> measurable (sigma (product_algebra M I)) M' \<longleftrightarrow>
  1409     f \<in> measurable (M i) M'"
  1410     (is "?f \<in> measurable ?P M' \<longleftrightarrow> _")
  1411 proof
  1412   assume "f \<in> measurable (M i) M'" then show "?f \<in> measurable ?P M'"
  1413     by (rule measurable_component[OF `i \<in> I`])
  1414 next
  1415   assume f: "?f \<in> measurable ?P M'"
  1416   def t \<equiv> "\<lambda>i. SOME x. x \<in> space (M i)"
  1417   have t: "\<And>i. i\<in>I \<Longrightarrow> t i \<in> space (M i)"
  1418      unfolding t_def using not_empty by (rule_tac someI_ex) auto
  1419   have "?f \<circ> (\<lambda>x. (\<lambda>j\<in>I. if j = i then x else t j)) \<in> measurable (M i) M'"
  1420     (is "?f \<circ> ?t \<in> measurable _ _")
  1421   proof (rule measurable_comp[OF _ f])
  1422     have "?t \<in> measurable (M i) (product_algebra M I)"
  1423     proof (unfold measurable_def, intro CollectI conjI ballI)
  1424       from t show "?t \<in> space (M i) \<rightarrow> (space (product_algebra M I))" by auto
  1425     next
  1426       fix A assume A: "A \<in> sets (product_algebra M I)"
  1427       { fix E assume "E \<in> (\<Pi> i\<in>I. sets (M i))"
  1428         then have "?t -` Pi\<^isub>E I E \<inter> space (M i) = (if (\<forall>j\<in>I-{i}. t j \<in> E j) then E i else {})"
  1429           using `i \<in> I` sets_into_space by (auto dest: Pi_mem[where B=E]) }
  1430       note * = this
  1431       with A `i \<in> I` show "?t -` A \<inter> space (M i) \<in> sets (M i)"
  1432         by (auto elim!: product_algebraE simp del: vimage_Int)
  1433     qed
  1434     also have "\<dots> \<subseteq> measurable (M i) (sigma (product_algebra M I))"
  1435       using M.sets_into_space by (intro measurable_subset) (auto simp: product_algebra_def, blast)
  1436     finally show "?t \<in> measurable (M i) (sigma (product_algebra M I))" .
  1437   qed
  1438   then show "f \<in> measurable (M i) M'" unfolding comp_def using `i \<in> I` by simp
  1439 qed
  1440 
  1441 lemma (in product_sigma_algebra) measurable_singleton:
  1442   shows "f \<in> measurable (sigma (product_algebra M {i})) M' \<longleftrightarrow>
  1443     (\<lambda>x. f (\<lambda>j\<in>{i}. x)) \<in> measurable (M i) M'"
  1444   using sets_into_space unfolding measurable_component_singleton[symmetric]
  1445   by (auto intro!: measurable_cong arg_cong[where f=f] simp: fun_eq_iff extensional_def)
  1446 
  1447 
  1448 lemma (in pair_sigma_algebra) measurable_pair_split:
  1449   assumes "sigma_algebra M1'" "sigma_algebra M2'"
  1450   assumes f: "f \<in> measurable M1 M1'" and g: "g \<in> measurable M2 M2'"
  1451   shows "(\<lambda>(x, y). (f x, g y)) \<in> measurable P (sigma (pair_algebra M1' M2'))"
  1452 proof (rule measurable_sigma)
  1453   interpret M1': sigma_algebra M1' by fact
  1454   interpret M2': sigma_algebra M2' by fact
  1455   interpret Q: pair_sigma_algebra M1' M2' by default
  1456   show "sets Q.E \<subseteq> Pow (space Q.E)"
  1457     using M1'.sets_into_space M2'.sets_into_space by (auto simp: pair_algebra_def)
  1458   show "(\<lambda>(x, y). (f x, g y)) \<in> space P \<rightarrow> space Q.E"
  1459     using f g unfolding measurable_def pair_algebra_def by auto
  1460   fix A assume "A \<in> sets Q.E"
  1461   then obtain X Y where A: "A = X \<times> Y" "X \<in> sets M1'" "Y \<in> sets M2'"
  1462     unfolding pair_algebra_def by auto
  1463   then have *: "(\<lambda>(x, y). (f x, g y)) -` A \<inter> space P =
  1464       (f -` X \<inter> space M1) \<times> (g -` Y \<inter> space M2)"
  1465     by (auto simp: pair_algebra_def)
  1466   then show "(\<lambda>(x, y). (f x, g y)) -` A \<inter> space P \<in> sets P"
  1467     using f g A unfolding measurable_def *
  1468     by (auto intro!: pair_algebraI in_sigma)
  1469 qed
  1470 
  1471 lemma (in product_sigma_algebra) measurable_add_dim:
  1472   assumes "i \<notin> I" "finite I"
  1473   shows "(\<lambda>(f, y). f(i := y)) \<in> measurable (sigma (pair_algebra (sigma (product_algebra M I)) (M i)))
  1474                          (sigma (product_algebra M (insert i I)))"
  1475 proof (subst measurable_cong)
  1476   interpret I: finite_product_sigma_algebra M I by default fact
  1477   interpret i: finite_product_sigma_algebra M "{i}" by default auto
  1478   interpret Ii: pair_sigma_algebra I.P "M i" by default
  1479   interpret Ii': pair_sigma_algebra I.P i.P by default
  1480   have disj: "I \<inter> {i} = {}" using `i \<notin> I` by auto
  1481   have "(\<lambda>(x, y). (id x, \<lambda>_\<in>{i}. y)) \<in> measurable Ii.P Ii'.P"
  1482   proof (intro Ii.measurable_pair_split I.measurable_ident)
  1483     show "(\<lambda>y. \<lambda>_\<in>{i}. y) \<in> measurable (M i) i.P"
  1484       apply (rule measurable_singleton[THEN iffD1])
  1485       using i.measurable_ident unfolding id_def .
  1486   qed default
  1487   from measurable_comp[OF this measurable_merge[OF disj]]
  1488   show "(\<lambda>(x, y). merge I x {i} y) \<circ> (\<lambda>(x, y). (id x, \<lambda>_\<in>{i}. y))
  1489     \<in> measurable (sigma (pair_algebra I.P (M i))) (sigma (product_algebra M (insert i I)))"
  1490     (is "?f \<in> _") by simp
  1491   fix x assume "x \<in> space Ii.P"
  1492   with assms show "(\<lambda>(f, y). f(i := y)) x = ?f x"
  1493     by (cases x) (simp add: merge_def fun_eq_iff pair_algebra_def extensional_def)
  1494 qed
  1495 
  1496 locale product_sigma_finite =
  1497   fixes M :: "'i \<Rightarrow> 'a algebra" and \<mu> :: "'i \<Rightarrow> 'a set \<Rightarrow> pextreal"
  1498   assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i) (\<mu> i)"
  1499 
  1500 locale finite_product_sigma_finite = product_sigma_finite M \<mu> for M :: "'i \<Rightarrow> 'a algebra" and \<mu> +
  1501   fixes I :: "'i set" assumes finite_index': "finite I"
  1502 
  1503 sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" "\<mu> i" for i
  1504   by (rule sigma_finite_measures)
  1505 
  1506 sublocale product_sigma_finite \<subseteq> product_sigma_algebra
  1507   by default
  1508 
  1509 sublocale finite_product_sigma_finite \<subseteq> finite_product_sigma_algebra
  1510   by default (fact finite_index')
  1511 
  1512 lemma (in finite_product_sigma_finite) sigma_finite_pairs:
  1513   "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
  1514     (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
  1515     (\<forall>k. \<forall>i\<in>I. \<mu> i (F i k) \<noteq> \<omega>) \<and>
  1516     (\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k) \<up> space G"
  1517 proof -
  1518   have "\<forall>i::'i. \<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> sets (M i) \<and> F \<up> space (M i) \<and> (\<forall>k. \<mu> i (F k) \<noteq> \<omega>)"
  1519     using M.sigma_finite_up by simp
  1520   from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
  1521   then have "\<And>i. range (F i) \<subseteq> sets (M i)" "\<And>i. F i \<up> space (M i)" "\<And>i k. \<mu> i (F i k) \<noteq> \<omega>"
  1522     by auto
  1523   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
  1524   note space_product_algebra[simp]
  1525   show ?thesis
  1526   proof (intro exI[of _ F] conjI allI isotoneI set_eqI iffI ballI)
  1527     fix i show "range (F i) \<subseteq> sets (M i)" by fact
  1528   next
  1529     fix i k show "\<mu> i (F i k) \<noteq> \<omega>" by fact
  1530   next
  1531     fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space G"
  1532       using `\<And>i. range (F i) \<subseteq> sets (M i)` M.sets_into_space by auto blast
  1533   next
  1534     fix f assume "f \<in> space G"
  1535     with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"]
  1536       `\<And>i. F i \<up> space (M i)`[THEN isotonD(2)]
  1537       `\<And>i. F i \<up> space (M i)`[THEN isoton_mono_le]
  1538     show "f \<in> (\<Union>i. ?F i)" by auto
  1539   next
  1540     fix i show "?F i \<subseteq> ?F (Suc i)"
  1541       using `\<And>i. F i \<up> space (M i)`[THEN isotonD(1)] by auto
  1542   qed
  1543 qed
  1544 
  1545 lemma (in product_sigma_finite) product_measure_exists:
  1546   assumes "finite I"
  1547   shows "\<exists>\<nu>. (\<forall>A\<in>(\<Pi> i\<in>I. sets (M i)). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))) \<and>
  1548      sigma_finite_measure (sigma (product_algebra M I)) \<nu>"
  1549 using `finite I` proof induct
  1550   case empty then show ?case unfolding product_algebra_def
  1551     by (auto intro!: exI[of _ "\<lambda>A. if A = {} then 0 else 1"] sigma_algebra_sigma
  1552                      sigma_algebra.finite_additivity_sufficient
  1553              simp add: positive_def additive_def sets_sigma sigma_finite_measure_def
  1554                        sigma_finite_measure_axioms_def image_constant)
  1555 next
  1556   case (insert i I)
  1557   interpret finite_product_sigma_finite M \<mu> I by default fact
  1558   have "finite (insert i I)" using `finite I` by auto
  1559   interpret I': finite_product_sigma_finite M \<mu> "insert i I" by default fact
  1560   from insert obtain \<nu> where
  1561     prod: "\<And>A. A \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))" and
  1562     "sigma_finite_measure P \<nu>" by auto
  1563   interpret I: sigma_finite_measure P \<nu> by fact
  1564   interpret P: pair_sigma_finite P \<nu> "M i" "\<mu> i" ..
  1565 
  1566   let ?h = "(\<lambda>(f, y). f(i := y))"
  1567   let ?\<nu> = "\<lambda>A. P.pair_measure (?h -` A \<inter> space P.P)"
  1568   have I': "sigma_algebra I'.P" by default
  1569   interpret I': measure_space "sigma (product_algebra M (insert i I))" ?\<nu>
  1570     apply (rule P.measure_space_vimage[OF I'])
  1571     apply (rule measurable_add_dim[OF `i \<notin> I` `finite I`])
  1572     by simp
  1573   show ?case
  1574   proof (intro exI[of _ ?\<nu>] conjI ballI)
  1575     { fix A assume A: "A \<in> (\<Pi> i\<in>insert i I. sets (M i))"
  1576       then have *: "?h -` Pi\<^isub>E (insert i I) A \<inter> space P.P = Pi\<^isub>E I A \<times> A i"
  1577         using `i \<notin> I` M.sets_into_space by (auto simp: pair_algebra_def) blast
  1578       show "?\<nu> (Pi\<^isub>E (insert i I) A) = (\<Prod>i\<in>insert i I. \<mu> i (A i))"
  1579         unfolding * using A
  1580         apply (subst P.pair_measure_times)
  1581         using A apply fastsimp
  1582         using A apply fastsimp
  1583         using `i \<notin> I` `finite I` prod[of A] A by (auto simp: ac_simps) }
  1584     note product = this
  1585     show "sigma_finite_measure I'.P ?\<nu>"
  1586     proof
  1587       from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
  1588       then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
  1589         "(\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) \<up> space I'.G"
  1590         "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> \<mu> j (F j k) \<noteq> \<omega>"
  1591         by blast+
  1592       let "?F k" = "\<Pi>\<^isub>E j \<in> insert i I. F j k"
  1593       show "\<exists>F::nat \<Rightarrow> ('i \<Rightarrow> 'a) set. range F \<subseteq> sets I'.P \<and>
  1594           (\<Union>i. F i) = space I'.P \<and> (\<forall>i. ?\<nu> (F i) \<noteq> \<omega>)"
  1595       proof (intro exI[of _ ?F] conjI allI)
  1596         show "range ?F \<subseteq> sets I'.P" using F(1) by auto
  1597       next
  1598         from F(2)[THEN isotonD(2)]
  1599         show "(\<Union>i. ?F i) = space I'.P" by simp
  1600       next
  1601         fix j
  1602         show "?\<nu> (?F j) \<noteq> \<omega>"
  1603           using F `finite I`
  1604           by (subst product) (auto simp: setprod_\<omega>)
  1605       qed
  1606     qed
  1607   qed
  1608 qed
  1609 
  1610 definition (in finite_product_sigma_finite)
  1611   measure :: "('i \<Rightarrow> 'a) set \<Rightarrow> pextreal" where
  1612   "measure = (SOME \<nu>.
  1613      (\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))) \<and>
  1614      sigma_finite_measure P \<nu>)"
  1615 
  1616 sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure P measure
  1617 proof -
  1618   show "sigma_finite_measure P measure"
  1619     unfolding measure_def
  1620     by (rule someI2_ex[OF product_measure_exists[OF finite_index]]) auto
  1621 qed
  1622 
  1623 lemma (in finite_product_sigma_finite) measure_times:
  1624   assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
  1625   shows "measure (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))"
  1626 proof -
  1627   note ex = product_measure_exists[OF finite_index]
  1628   show ?thesis
  1629     unfolding measure_def
  1630   proof (rule someI2_ex[OF ex], elim conjE)
  1631     fix \<nu> assume *: "\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))"
  1632     have "Pi\<^isub>E I A = Pi\<^isub>E I (\<lambda>i\<in>I. A i)" by (auto dest: Pi_mem)
  1633     then have "\<nu> (Pi\<^isub>E I A) = \<nu> (Pi\<^isub>E I (\<lambda>i\<in>I. A i))" by simp
  1634     also have "\<dots> = (\<Prod>i\<in>I. \<mu> i ((\<lambda>i\<in>I. A i) i))" using assms * by auto
  1635     finally show "\<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))" by simp
  1636   qed
  1637 qed
  1638 
  1639 abbreviation (in product_sigma_finite)
  1640   "product_measure I \<equiv> finite_product_sigma_finite.measure M \<mu> I"
  1641 
  1642 abbreviation (in product_sigma_finite)
  1643   "product_positive_integral I \<equiv>
  1644     measure_space.positive_integral (sigma (product_algebra M I)) (product_measure I)"
  1645 
  1646 abbreviation (in product_sigma_finite)
  1647   "product_integral I \<equiv>
  1648     measure_space.integral (sigma (product_algebra M I)) (product_measure I)"
  1649 
  1650 abbreviation (in product_sigma_finite)
  1651   "product_integrable I \<equiv>
  1652     measure_space.integrable (sigma (product_algebra M I)) (product_measure I)"
  1653 
  1654 lemma (in product_sigma_finite) product_measure_empty[simp]:
  1655   "product_measure {} {\<lambda>x. undefined} = 1"
  1656 proof -
  1657   interpret finite_product_sigma_finite M \<mu> "{}" by default auto
  1658   from measure_times[of "\<lambda>x. {}"] show ?thesis by simp
  1659 qed
  1660 
  1661 lemma (in product_sigma_finite) positive_integral_empty:
  1662   "product_positive_integral {} f = f (\<lambda>k. undefined)"
  1663 proof -
  1664   interpret finite_product_sigma_finite M \<mu> "{}" by default (fact finite.emptyI)
  1665   have "\<And>A. measure (Pi\<^isub>E {} A) = 1"
  1666     using assms by (subst measure_times) auto
  1667   then show ?thesis
  1668     unfolding positive_integral_def simple_function_def simple_integral_def_raw
  1669   proof (simp add: P_empty, intro antisym)
  1670     show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> f}. f (\<lambda>k. undefined))"
  1671       by (intro le_SUPI) auto
  1672     show "(SUP f:{g. g \<le> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)"
  1673       by (intro SUP_leI) (auto simp: le_fun_def)
  1674   qed
  1675 qed
  1676 
  1677 lemma (in product_sigma_finite) measure_fold:
  1678   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1679   assumes A: "A \<in> sets (sigma (product_algebra M (I \<union> J)))"
  1680   shows "pair_sigma_finite.pair_measure
  1681      (sigma (product_algebra M I)) (product_measure I)
  1682      (sigma (product_algebra M J)) (product_measure J)
  1683      ((\<lambda>(x,y). merge I x J y) -` A \<inter> space (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))) =
  1684    product_measure (I \<union> J) A"
  1685 proof -
  1686   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1687   interpret J: finite_product_sigma_finite M \<mu> J by default fact
  1688   have "finite (I \<union> J)" using fin by auto
  1689   interpret IJ: finite_product_sigma_finite M \<mu> "I \<union> J" by default fact
  1690   interpret P: pair_sigma_finite I.P I.measure J.P J.measure by default
  1691   let ?g = "\<lambda>(x,y). merge I x J y"
  1692   let "?X S" = "?g -` S \<inter> space P.P"
  1693   from IJ.sigma_finite_pairs obtain F where
  1694     F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
  1695        "(\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) \<up> space IJ.G"
  1696        "\<And>k. \<forall>i\<in>I\<union>J. \<mu> i (F i k) \<noteq> \<omega>"
  1697     by auto
  1698   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
  1699   show "P.pair_measure (?X A) = IJ.measure A"
  1700   proof (rule measure_unique_Int_stable[OF _ _ _ _ _ _ _ _ A])
  1701     show "Int_stable IJ.G" by (simp add: PiE_Int Int_stable_def product_algebra_def) auto
  1702     show "range ?F \<subseteq> sets IJ.G" using F by (simp add: image_subset_iff product_algebra_def)
  1703     show "?F \<up> space IJ.G " using F(2) by simp
  1704     have "sigma_algebra IJ.P" by default
  1705     then show "measure_space IJ.P (\<lambda>A. P.pair_measure (?X A))"
  1706       apply (rule P.measure_space_vimage)
  1707       apply (rule measurable_merge[OF `I \<inter> J = {}`])
  1708       by simp
  1709     show "measure_space IJ.P IJ.measure" by fact
  1710   next
  1711     fix A assume "A \<in> sets IJ.G"
  1712     then obtain F where A[simp]: "A = Pi\<^isub>E (I \<union> J) F"
  1713       and F: "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i \<in> sets (M i)"
  1714       by (auto simp: product_algebra_def)
  1715     then have "?X A = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
  1716       using sets_into_space by (auto simp: space_pair_algebra) blast+
  1717     then have "P.pair_measure (?X A) = (\<Prod>i\<in>I. \<mu> i (F i)) * (\<Prod>i\<in>J. \<mu> i (F i))"
  1718       using `finite J` `finite I` F
  1719       by (simp add: P.pair_measure_times I.measure_times J.measure_times)
  1720     also have "\<dots> = (\<Prod>i\<in>I \<union> J. \<mu> i (F i))"
  1721       using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
  1722     also have "\<dots> = IJ.measure A"
  1723       using `finite J` `finite I` F unfolding A
  1724       by (intro IJ.measure_times[symmetric]) auto
  1725     finally show "P.pair_measure (?X A) = IJ.measure A" .
  1726   next
  1727     fix k
  1728     have k: "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i k \<in> sets (M i)" using F by auto
  1729     then have "?X (?F k) = (\<Pi>\<^isub>E i\<in>I. F i k) \<times> (\<Pi>\<^isub>E i\<in>J. F i k)"
  1730       using sets_into_space by (auto simp: space_pair_algebra) blast+
  1731     with k have "P.pair_measure (?X (?F k)) = (\<Prod>i\<in>I. \<mu> i (F i k)) * (\<Prod>i\<in>J. \<mu> i (F i k))"
  1732      by (simp add: P.pair_measure_times I.measure_times J.measure_times)
  1733     then show "P.pair_measure (?X (?F k)) \<noteq> \<omega>"
  1734       using `finite I` F by (simp add: setprod_\<omega>)
  1735   qed simp
  1736 qed
  1737 
  1738 lemma (in product_sigma_finite) product_positive_integral_fold:
  1739   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1740   and f: "f \<in> borel_measurable (sigma (product_algebra M (I \<union> J)))"
  1741   shows "product_positive_integral (I \<union> J) f =
  1742     product_positive_integral I (\<lambda>x. product_positive_integral J (\<lambda>y. f (merge I x J y)))"
  1743 proof -
  1744   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1745   interpret J: finite_product_sigma_finite M \<mu> J by default fact
  1746   have "finite (I \<union> J)" using fin by auto
  1747   interpret IJ: finite_product_sigma_finite M \<mu> "I \<union> J" by default fact
  1748   interpret P: pair_sigma_finite I.P I.measure J.P J.measure by default
  1749   have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable P.P"
  1750     unfolding case_prod_distrib measurable_merge_iff[OF IJ, symmetric] using f .
  1751   show ?thesis
  1752     unfolding P.positive_integral_fst_measurable[OF P_borel, simplified]
  1753     apply (subst IJ.positive_integral_cong_measure[symmetric])
  1754     apply (rule measure_fold[OF IJ fin])
  1755     apply assumption
  1756   proof (rule P.positive_integral_vimage)
  1757     show "sigma_algebra IJ.P" by default
  1758     show "(\<lambda>(x, y). merge I x J y) \<in> measurable P.P IJ.P" by (rule measurable_merge[OF IJ])
  1759     show "f \<in> borel_measurable IJ.P" using f .
  1760   qed
  1761 qed
  1762 
  1763 lemma (in product_sigma_finite) product_positive_integral_singleton:
  1764   assumes f: "f \<in> borel_measurable (M i)"
  1765   shows "product_positive_integral {i} (\<lambda>x. f (x i)) = M.positive_integral i f"
  1766 proof -
  1767   interpret I: finite_product_sigma_finite M \<mu> "{i}" by default simp
  1768   have T: "(\<lambda>x. x i) \<in> measurable (sigma (product_algebra M {i})) (M i)"
  1769     using measurable_component_singleton[of "\<lambda>x. x" i]
  1770           measurable_ident[unfolded id_def] by auto
  1771   show "I.positive_integral (\<lambda>x. f (x i)) = M.positive_integral i f"
  1772     unfolding I.positive_integral_vimage[OF sigma_algebras T f, symmetric]
  1773   proof (rule positive_integral_cong_measure)
  1774     fix A let ?P = "(\<lambda>x. x i) -` A \<inter> space (sigma (product_algebra M {i}))"
  1775     assume A: "A \<in> sets (M i)"
  1776     then have *: "?P = {i} \<rightarrow>\<^isub>E A" using sets_into_space by auto
  1777     show "product_measure {i} ?P = \<mu> i A" unfolding *
  1778       using A I.measure_times[of "\<lambda>_. A"] by auto
  1779   qed
  1780 qed
  1781 
  1782 lemma (in product_sigma_finite) product_positive_integral_insert:
  1783   assumes [simp]: "finite I" "i \<notin> I"
  1784     and f: "f \<in> borel_measurable (sigma (product_algebra M (insert i I)))"
  1785   shows "product_positive_integral (insert i I) f
  1786     = product_positive_integral I (\<lambda>x. M.positive_integral i (\<lambda>y. f (x(i:=y))))"
  1787 proof -
  1788   interpret I: finite_product_sigma_finite M \<mu> I by default auto
  1789   interpret i: finite_product_sigma_finite M \<mu> "{i}" by default auto
  1790   interpret P: pair_sigma_algebra I.P i.P ..
  1791   have IJ: "I \<inter> {i} = {}" by auto
  1792   show ?thesis
  1793     unfolding product_positive_integral_fold[OF IJ, simplified, OF f]
  1794   proof (rule I.positive_integral_cong, subst product_positive_integral_singleton)
  1795     fix x assume x: "x \<in> space I.P"
  1796     let "?f y" = "f (restrict (x(i := y)) (insert i I))"
  1797     have f'_eq: "\<And>y. ?f y = f (x(i := y))"
  1798       using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
  1799     note fP = f[unfolded measurable_merge_iff[OF IJ, simplified]]
  1800     show "?f \<in> borel_measurable (M i)"
  1801       using P.measurable_pair_image_snd[OF fP x]
  1802       unfolding measurable_singleton f'_eq by (simp add: f'_eq)
  1803     show "M.positive_integral i ?f = M.positive_integral i (\<lambda>y. f (x(i := y)))"
  1804       unfolding f'_eq by simp
  1805   qed
  1806 qed
  1807 
  1808 lemma (in product_sigma_finite) product_positive_integral_setprod:
  1809   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> pextreal"
  1810   assumes "finite I" and borel: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  1811   shows "product_positive_integral I (\<lambda>x. (\<Prod>i\<in>I. f i (x i))) =
  1812     (\<Prod>i\<in>I. M.positive_integral i (f i))"
  1813 using assms proof induct
  1814   case empty
  1815   interpret finite_product_sigma_finite M \<mu> "{}" by default auto
  1816   then show ?case by simp
  1817 next
  1818   case (insert i I)
  1819   note `finite I`[intro, simp]
  1820   interpret I: finite_product_sigma_finite M \<mu> I by default auto
  1821   have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  1822     using insert by (auto intro!: setprod_cong)
  1823   have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  1824     (\<lambda>x. (\<Prod>i\<in>J. f i (x i))) \<in> borel_measurable (sigma (product_algebra M J))"
  1825     using sets_into_space insert
  1826     by (intro sigma_algebra.borel_measurable_pextreal_setprod
  1827               sigma_algebra_sigma product_algebra_sets_into_space
  1828               measurable_component)
  1829        auto
  1830   show ?case
  1831     by (simp add: product_positive_integral_insert[OF insert(1,2) prod])
  1832        (simp add: insert I.positive_integral_cmult M.positive_integral_multc * prod subset_insertI)
  1833 qed
  1834 
  1835 lemma (in product_sigma_finite) product_integral_singleton:
  1836   assumes f: "f \<in> borel_measurable (M i)"
  1837   shows "product_integral {i} (\<lambda>x. f (x i)) = M.integral i f"
  1838 proof -
  1839   interpret I: finite_product_sigma_finite M \<mu> "{i}" by default simp
  1840   have *: "(\<lambda>x. Real (f x)) \<in> borel_measurable (M i)"
  1841     "(\<lambda>x. Real (- f x)) \<in> borel_measurable (M i)"
  1842     using assms by auto
  1843   show ?thesis
  1844     unfolding I.integral_def integral_def
  1845     unfolding *[THEN product_positive_integral_singleton] ..
  1846 qed
  1847 
  1848 lemma (in product_sigma_finite) product_integral_fold:
  1849   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1850   and f: "measure_space.integrable (sigma (product_algebra M (I \<union> J))) (product_measure (I \<union> J)) f"
  1851   shows "product_integral (I \<union> J) f =
  1852     product_integral I (\<lambda>x. product_integral J (\<lambda>y. f (merge I x J y)))"
  1853 proof -
  1854   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1855   interpret J: finite_product_sigma_finite M \<mu> J by default fact
  1856   have "finite (I \<union> J)" using fin by auto
  1857   interpret IJ: finite_product_sigma_finite M \<mu> "I \<union> J" by default fact
  1858   interpret P: pair_sigma_finite I.P I.measure J.P J.measure by default
  1859   let ?f = "\<lambda>(x,y). f (merge I x J y)"
  1860   have f_borel: "f \<in> borel_measurable IJ.P"
  1861      "(\<lambda>x. Real (f x)) \<in> borel_measurable IJ.P"
  1862      "(\<lambda>x. Real (- f x)) \<in> borel_measurable IJ.P"
  1863     using f unfolding integrable_def by auto
  1864   have f_restrict: "(\<lambda>x. f (restrict x (I \<union> J))) \<in> borel_measurable IJ.P"
  1865     by (rule measurable_cong[THEN iffD2, OF _ f_borel(1)])
  1866        (auto intro!: arg_cong[where f=f] simp: extensional_restrict)
  1867   then have f'_borel:
  1868     "(\<lambda>x. Real (?f x)) \<in> borel_measurable P.P"
  1869     "(\<lambda>x. Real (- ?f x)) \<in> borel_measurable P.P"
  1870     unfolding measurable_restrict_iff[OF IJ]
  1871     by simp_all
  1872   have PI:
  1873     "P.positive_integral (\<lambda>x. Real (?f x)) = IJ.positive_integral (\<lambda>x. Real (f x))"
  1874     "P.positive_integral (\<lambda>x. Real (- ?f x)) = IJ.positive_integral (\<lambda>x. Real (- f x))"
  1875     using f'_borel[THEN P.positive_integral_fst_measurable(2)]
  1876     using f_borel(2,3)[THEN product_positive_integral_fold[OF assms(1-3)]]
  1877     by simp_all
  1878   have "P.integrable ?f" using `IJ.integrable f`
  1879     unfolding P.integrable_def IJ.integrable_def
  1880     unfolding measurable_restrict_iff[OF IJ]
  1881     using f_restrict PI by simp_all
  1882   show ?thesis
  1883     unfolding P.integrable_fst_measurable(2)[OF `P.integrable ?f`, simplified]
  1884     unfolding IJ.integral_def P.integral_def
  1885     unfolding PI by simp
  1886 qed
  1887 
  1888 lemma (in product_sigma_finite) product_integral_insert:
  1889   assumes [simp]: "finite I" "i \<notin> I"
  1890     and f: "measure_space.integrable (sigma (product_algebra M (insert i I))) (product_measure (insert i I)) f"
  1891   shows "product_integral (insert i I) f
  1892     = product_integral I (\<lambda>x. M.integral i (\<lambda>y. f (x(i:=y))))"
  1893 proof -
  1894   interpret I: finite_product_sigma_finite M \<mu> I by default auto
  1895   interpret I': finite_product_sigma_finite M \<mu> "insert i I" by default auto
  1896   interpret i: finite_product_sigma_finite M \<mu> "{i}" by default auto
  1897   interpret P: pair_sigma_algebra I.P i.P ..
  1898   have IJ: "I \<inter> {i} = {}" by auto
  1899   show ?thesis
  1900     unfolding product_integral_fold[OF IJ, simplified, OF f]
  1901   proof (rule I.integral_cong, subst product_integral_singleton)
  1902     fix x assume x: "x \<in> space I.P"
  1903     let "?f y" = "f (restrict (x(i := y)) (insert i I))"
  1904     have f'_eq: "\<And>y. ?f y = f (x(i := y))"
  1905       using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
  1906     have "f \<in> borel_measurable I'.P" using f unfolding I'.integrable_def by auto
  1907     note fP = this[unfolded measurable_merge_iff[OF IJ, simplified]]
  1908     show "?f \<in> borel_measurable (M i)"
  1909       using P.measurable_pair_image_snd[OF fP x]
  1910       unfolding measurable_singleton f'_eq by (simp add: f'_eq)
  1911     show "M.integral i ?f = M.integral i (\<lambda>y. f (x(i := y)))"
  1912       unfolding f'_eq by simp
  1913   qed
  1914 qed
  1915 
  1916 lemma (in product_sigma_finite) product_integrable_setprod:
  1917   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
  1918   assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> M.integrable i (f i)"
  1919   shows "product_integrable I (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "product_integrable I ?f")
  1920 proof -
  1921   interpret finite_product_sigma_finite M \<mu> I by default fact
  1922   have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  1923     using integrable unfolding M.integrable_def by auto
  1924   then have borel: "?f \<in> borel_measurable P"
  1925     by (intro borel_measurable_setprod measurable_component) auto
  1926   moreover have "integrable (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
  1927   proof (unfold integrable_def, intro conjI)
  1928     show "(\<lambda>x. abs (?f x)) \<in> borel_measurable P"
  1929       using borel by auto
  1930     have "positive_integral (\<lambda>x. Real (abs (?f x))) = positive_integral (\<lambda>x. \<Prod>i\<in>I. Real (abs (f i (x i))))"
  1931       by (simp add: Real_setprod abs_setprod)
  1932     also have "\<dots> = (\<Prod>i\<in>I. M.positive_integral i (\<lambda>x. Real (abs (f i x))))"
  1933       using f by (subst product_positive_integral_setprod) auto
  1934     also have "\<dots> < \<omega>"
  1935       using integrable[THEN M.integrable_abs]
  1936       unfolding pextreal_less_\<omega> setprod_\<omega> M.integrable_def by simp
  1937     finally show "positive_integral (\<lambda>x. Real (abs (?f x))) \<noteq> \<omega>" by auto
  1938     show "positive_integral (\<lambda>x. Real (- abs (?f x))) \<noteq> \<omega>" by simp
  1939   qed
  1940   ultimately show ?thesis
  1941     by (rule integrable_abs_iff[THEN iffD1])
  1942 qed
  1943 
  1944 lemma (in product_sigma_finite) product_integral_setprod:
  1945   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
  1946   assumes "finite I" "I \<noteq> {}" and integrable: "\<And>i. i \<in> I \<Longrightarrow> M.integrable i (f i)"
  1947   shows "product_integral I (\<lambda>x. (\<Prod>i\<in>I. f i (x i))) = (\<Prod>i\<in>I. M.integral i (f i))"
  1948 using assms proof (induct rule: finite_ne_induct)
  1949   case (singleton i)
  1950   then show ?case by (simp add: product_integral_singleton integrable_def)
  1951 next
  1952   case (insert i I)
  1953   then have iI: "finite (insert i I)" by auto
  1954   then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  1955     product_integrable J (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  1956     by (intro product_integrable_setprod insert(5)) (auto intro: finite_subset)
  1957   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1958   have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  1959     using `i \<notin> I` by (auto intro!: setprod_cong)
  1960   show ?case
  1961     unfolding product_integral_insert[OF insert(1,3) prod[OF subset_refl]]
  1962     by (simp add: * insert integral_multc I.integral_cmult[OF prod] subset_insertI)
  1963 qed
  1964 
  1965 section "Products on finite spaces"
  1966 
  1967 lemma sigma_sets_pair_algebra_finite:
  1968   assumes "finite A" and "finite B"
  1969   shows "sigma_sets (A \<times> B) ((\<lambda>(x,y). x \<times> y) ` (Pow A \<times> Pow B)) = Pow (A \<times> B)"
  1970   (is "sigma_sets ?prod ?sets = _")
  1971 proof safe
  1972   have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
  1973   fix x assume subset: "x \<subseteq> A \<times> B"
  1974   hence "finite x" using fin by (rule finite_subset)
  1975   from this subset show "x \<in> sigma_sets ?prod ?sets"
  1976   proof (induct x)
  1977     case empty show ?case by (rule sigma_sets.Empty)
  1978   next
  1979     case (insert a x)
  1980     hence "{a} \<in> sigma_sets ?prod ?sets"
  1981       by (auto simp: pair_algebra_def intro!: sigma_sets.Basic)
  1982     moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
  1983     ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
  1984   qed
  1985 next
  1986   fix x a b
  1987   assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"
  1988   from sigma_sets_into_sp[OF _ this(1)] this(2)
  1989   show "a \<in> A" and "b \<in> B" by auto
  1990 qed
  1991 
  1992 locale pair_finite_sigma_algebra = M1: finite_sigma_algebra M1 + M2: finite_sigma_algebra M2 for M1 M2
  1993 
  1994 sublocale pair_finite_sigma_algebra \<subseteq> pair_sigma_algebra by default
  1995 
  1996 lemma (in pair_finite_sigma_algebra) finite_pair_sigma_algebra[simp]:
  1997   shows "P = (| space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2) |)"
  1998 proof -
  1999   show ?thesis using M1.finite_space M2.finite_space
  2000     by (simp add: sigma_def space_pair_algebra sets_pair_algebra
  2001                   sigma_sets_pair_algebra_finite M1.sets_eq_Pow M2.sets_eq_Pow)
  2002 qed
  2003 
  2004 sublocale pair_finite_sigma_algebra \<subseteq> finite_sigma_algebra P
  2005 proof
  2006   show "finite (space P)" "sets P = Pow (space P)"
  2007     using M1.finite_space M2.finite_space by auto
  2008 qed
  2009 
  2010 locale pair_finite_space = M1: finite_measure_space M1 \<mu>1 + M2: finite_measure_space M2 \<mu>2
  2011   for M1 \<mu>1 M2 \<mu>2
  2012 
  2013 sublocale pair_finite_space \<subseteq> pair_finite_sigma_algebra
  2014   by default
  2015 
  2016 sublocale pair_finite_space \<subseteq> pair_sigma_finite
  2017   by default
  2018 
  2019 lemma (in pair_finite_space) finite_pair_sigma_algebra[simp]:
  2020   shows "P = (| space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2) |)"
  2021 proof -
  2022   show ?thesis using M1.finite_space M2.finite_space
  2023     by (simp add: sigma_def space_pair_algebra sets_pair_algebra
  2024                   sigma_sets_pair_algebra_finite M1.sets_eq_Pow M2.sets_eq_Pow)
  2025 qed
  2026 
  2027 lemma (in pair_finite_space) pair_measure_Pair[simp]:
  2028   assumes "a \<in> space M1" "b \<in> space M2"
  2029   shows "pair_measure {(a, b)} = \<mu>1 {a} * \<mu>2 {b}"
  2030 proof -
  2031   have "pair_measure ({a}\<times>{b}) = \<mu>1 {a} * \<mu>2 {b}"
  2032     using M1.sets_eq_Pow M2.sets_eq_Pow assms
  2033     by (subst pair_measure_times) auto
  2034   then show ?thesis by simp
  2035 qed
  2036 
  2037 lemma (in pair_finite_space) pair_measure_singleton[simp]:
  2038   assumes "x \<in> space M1 \<times> space M2"
  2039   shows "pair_measure {x} = \<mu>1 {fst x} * \<mu>2 {snd x}"
  2040   using pair_measure_Pair assms by (cases x) auto
  2041 
  2042 sublocale pair_finite_space \<subseteq> finite_measure_space P pair_measure
  2043   by default auto
  2044 
  2045 lemma (in pair_finite_space) finite_measure_space_finite_prod_measure_alterantive:
  2046   "finite_measure_space \<lparr>space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2)\<rparr> pair_measure"
  2047   unfolding finite_pair_sigma_algebra[symmetric]
  2048   by default
  2049 
  2050 end