src/HOL/Probability/Product_Measure.thy
author hoelzl
Wed Dec 08 16:15:14 2010 +0100 (2010-12-08)
changeset 41095 c335d880ff82
parent 41026 bea75746dc9d
child 41096 843c40bbc379
permissions -rw-r--r--
cleanup bijectivity btw. product spaces and pairs
     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 lemma (in pair_sigma_algebra) pair_sigma_algebra_swap:
   527   "sigma (pair_algebra M2 M1) = (vimage_algebra (space M2 \<times> space M1) (\<lambda>(x, y). (y, x)))"
   528   unfolding vimage_algebra_def
   529   apply (simp add: sets_sigma)
   530   apply (subst sigma_sets_vimage[symmetric])
   531   apply (fastsimp simp: pair_algebra_def)
   532   using M1.sets_into_space M2.sets_into_space apply (fastsimp simp: pair_algebra_def)
   533 proof -
   534   have "(\<lambda>X. (\<lambda>(x, y). (y, x)) -` X \<inter> space M2 \<times> space M1) ` sets E
   535     = sets (pair_algebra M2 M1)" (is "?S = _")
   536     by (rule pair_algebra_swap)
   537   then show "sigma (pair_algebra M2 M1) = \<lparr>space = space M2 \<times> space M1,
   538        sets = sigma_sets (space M2 \<times> space M1) ?S\<rparr>"
   539     by (simp add: pair_algebra_def sigma_def)
   540 qed
   541 
   542 definition (in pair_sigma_finite)
   543   "pair_measure A = M1.positive_integral (\<lambda>x.
   544     M2.positive_integral (\<lambda>y. indicator A (x, y)))"
   545 
   546 lemma (in pair_sigma_finite) pair_measure_alt:
   547   assumes "A \<in> sets P"
   548   shows "pair_measure A = M1.positive_integral (\<lambda>x. \<mu>2 (Pair x -` A))"
   549   unfolding pair_measure_def
   550 proof (rule M1.positive_integral_cong)
   551   fix x assume "x \<in> space M1"
   552   have *: "\<And>y. indicator A (x, y) = (indicator (Pair x -` A) y :: pextreal)"
   553     unfolding indicator_def by auto
   554   show "M2.positive_integral (\<lambda>y. indicator A (x, y)) = \<mu>2 (Pair x -` A)"
   555     unfolding *
   556     apply (subst M2.positive_integral_indicator)
   557     apply (rule measurable_cut_fst[OF assms])
   558     by simp
   559 qed
   560 
   561 lemma (in pair_sigma_finite) pair_measure_times:
   562   assumes A: "A \<in> sets M1" and "B \<in> sets M2"
   563   shows "pair_measure (A \<times> B) = \<mu>1 A * \<mu>2 B"
   564 proof -
   565   from assms have "pair_measure (A \<times> B) =
   566       M1.positive_integral (\<lambda>x. \<mu>2 B * indicator A x)"
   567     by (auto intro!: M1.positive_integral_cong simp: pair_measure_alt)
   568   with assms show ?thesis
   569     by (simp add: M1.positive_integral_cmult_indicator ac_simps)
   570 qed
   571 
   572 lemma (in pair_sigma_finite) sigma_finite_up_in_pair_algebra:
   573   "\<exists>F::nat \<Rightarrow> ('a \<times> 'b) set. range F \<subseteq> sets E \<and> F \<up> space E \<and>
   574     (\<forall>i. pair_measure (F i) \<noteq> \<omega>)"
   575 proof -
   576   obtain F1 :: "nat \<Rightarrow> 'a set" and F2 :: "nat \<Rightarrow> 'b set" where
   577     F1: "range F1 \<subseteq> sets M1" "F1 \<up> space M1" "\<And>i. \<mu>1 (F1 i) \<noteq> \<omega>" and
   578     F2: "range F2 \<subseteq> sets M2" "F2 \<up> space M2" "\<And>i. \<mu>2 (F2 i) \<noteq> \<omega>"
   579     using M1.sigma_finite_up M2.sigma_finite_up by auto
   580   then have space: "space M1 = (\<Union>i. F1 i)" "space M2 = (\<Union>i. F2 i)"
   581     unfolding isoton_def by auto
   582   let ?F = "\<lambda>i. F1 i \<times> F2 i"
   583   show ?thesis unfolding isoton_def space_pair_algebra
   584   proof (intro exI[of _ ?F] conjI allI)
   585     show "range ?F \<subseteq> sets E" using F1 F2
   586       by (fastsimp intro!: pair_algebraI)
   587   next
   588     have "space M1 \<times> space M2 \<subseteq> (\<Union>i. ?F i)"
   589     proof (intro subsetI)
   590       fix x assume "x \<in> space M1 \<times> space M2"
   591       then obtain i j where "fst x \<in> F1 i" "snd x \<in> F2 j"
   592         by (auto simp: space)
   593       then have "fst x \<in> F1 (max i j)" "snd x \<in> F2 (max j i)"
   594         using `F1 \<up> space M1` `F2 \<up> space M2`
   595         by (auto simp: max_def dest: isoton_mono_le)
   596       then have "(fst x, snd x) \<in> F1 (max i j) \<times> F2 (max i j)"
   597         by (intro SigmaI) (auto simp add: min_max.sup_commute)
   598       then show "x \<in> (\<Union>i. ?F i)" by auto
   599     qed
   600     then show "(\<Union>i. ?F i) = space M1 \<times> space M2"
   601       using space by (auto simp: space)
   602   next
   603     fix i show "F1 i \<times> F2 i \<subseteq> F1 (Suc i) \<times> F2 (Suc i)"
   604       using `F1 \<up> space M1` `F2 \<up> space M2` unfolding isoton_def
   605       by auto
   606   next
   607     fix i
   608     from F1 F2 have "F1 i \<in> sets M1" "F2 i \<in> sets M2" by auto
   609     with F1 F2 show "pair_measure (F1 i \<times> F2 i) \<noteq> \<omega>"
   610       by (simp add: pair_measure_times)
   611   qed
   612 qed
   613 
   614 sublocale pair_sigma_finite \<subseteq> sigma_finite_measure P pair_measure
   615 proof
   616   show "pair_measure {} = 0"
   617     unfolding pair_measure_def by auto
   618 
   619   show "countably_additive P pair_measure"
   620     unfolding countably_additive_def
   621   proof (intro allI impI)
   622     fix F :: "nat \<Rightarrow> ('a \<times> 'b) set"
   623     assume F: "range F \<subseteq> sets P" "disjoint_family F"
   624     from F have *: "\<And>i. F i \<in> sets P" "(\<Union>i. F i) \<in> sets P" by auto
   625     moreover from F have "\<And>i. (\<lambda>x. \<mu>2 (Pair x -` F i)) \<in> borel_measurable M1"
   626       by (intro measure_cut_measurable_fst) auto
   627     moreover have "\<And>x. disjoint_family (\<lambda>i. Pair x -` F i)"
   628       by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   629     moreover have "\<And>x. x \<in> space M1 \<Longrightarrow> range (\<lambda>i. Pair x -` F i) \<subseteq> sets M2"
   630       using F by (auto intro!: measurable_cut_fst)
   631     ultimately show "(\<Sum>\<^isub>\<infinity>n. pair_measure (F n)) = pair_measure (\<Union>i. F i)"
   632       by (simp add: pair_measure_alt vimage_UN M1.positive_integral_psuminf[symmetric]
   633                     M2.measure_countably_additive
   634                cong: M1.positive_integral_cong)
   635   qed
   636 
   637   from sigma_finite_up_in_pair_algebra guess F :: "nat \<Rightarrow> ('a \<times> 'c) set" .. note F = this
   638   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>)"
   639   proof (rule exI[of _ F], intro conjI)
   640     show "range F \<subseteq> sets P" using F by auto
   641     show "(\<Union>i. F i) = space P"
   642       using F by (auto simp: space_pair_algebra isoton_def)
   643     show "\<forall>i. pair_measure (F i) \<noteq> \<omega>" using F by auto
   644   qed
   645 qed
   646 
   647 lemma (in pair_sigma_finite) pair_measure_alt2:
   648   assumes "A \<in> sets P"
   649   shows "pair_measure A = M2.positive_integral (\<lambda>y. \<mu>1 ((\<lambda>x. (x, y)) -` A))"
   650     (is "_ = ?\<nu> A")
   651 proof -
   652   from sigma_finite_up_in_pair_algebra guess F :: "nat \<Rightarrow> ('a \<times> 'c) set" .. note F = this
   653   show ?thesis
   654   proof (rule measure_unique_Int_stable[where \<nu>="?\<nu>", OF Int_stable_pair_algebra],
   655          simp_all add: pair_sigma_algebra_def[symmetric])
   656     show "range F \<subseteq> sets E" "F \<up> space E" "\<And>i. pair_measure (F i) \<noteq> \<omega>"
   657       using F by auto
   658     show "measure_space P pair_measure" by default
   659   next
   660     show "measure_space P ?\<nu>"
   661     proof
   662       show "?\<nu> {} = 0" by auto
   663       show "countably_additive P ?\<nu>"
   664         unfolding countably_additive_def
   665       proof (intro allI impI)
   666         fix F :: "nat \<Rightarrow> ('a \<times> 'b) set"
   667         assume F: "range F \<subseteq> sets P" "disjoint_family F"
   668         from F have *: "\<And>i. F i \<in> sets P" "(\<Union>i. F i) \<in> sets P" by auto
   669         moreover from F have "\<And>i. (\<lambda>y. \<mu>1 ((\<lambda>x. (x, y)) -` F i)) \<in> borel_measurable M2"
   670           by (intro measure_cut_measurable_snd) auto
   671         moreover have "\<And>y. disjoint_family (\<lambda>i. (\<lambda>x. (x, y)) -` F i)"
   672           by (intro disjoint_family_on_bisimulation[OF F(2)]) auto
   673         moreover have "\<And>y. y \<in> space M2 \<Longrightarrow> range (\<lambda>i. (\<lambda>x. (x, y)) -` F i) \<subseteq> sets M1"
   674           using F by (auto intro!: measurable_cut_snd)
   675         ultimately show "(\<Sum>\<^isub>\<infinity>n. ?\<nu> (F n)) = ?\<nu> (\<Union>i. F i)"
   676           by (simp add: vimage_UN M2.positive_integral_psuminf[symmetric]
   677                         M1.measure_countably_additive
   678                    cong: M2.positive_integral_cong)
   679       qed
   680     qed
   681   next
   682     fix X assume "X \<in> sets E"
   683     then obtain A B where X: "X = A \<times> B" and AB: "A \<in> sets M1" "B \<in> sets M2"
   684       unfolding pair_algebra_def by auto
   685     show "pair_measure X = ?\<nu> X"
   686     proof -
   687       from AB have "?\<nu> (A \<times> B) =
   688           M2.positive_integral (\<lambda>y. \<mu>1 A * indicator B y)"
   689         by (auto intro!: M2.positive_integral_cong)
   690       with AB show ?thesis
   691         unfolding pair_measure_times[OF AB] X
   692         by (simp add: M2.positive_integral_cmult_indicator ac_simps)
   693     qed
   694   qed fact
   695 qed
   696 
   697 section "Fubinis theorem"
   698 
   699 lemma (in pair_sigma_finite) simple_function_cut:
   700   assumes f: "simple_function f"
   701   shows "(\<lambda>x. M2.positive_integral (\<lambda> y. f (x, y))) \<in> borel_measurable M1"
   702     and "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. f (x, y)))
   703       = positive_integral f"
   704 proof -
   705   have f_borel: "f \<in> borel_measurable P"
   706     using f by (rule borel_measurable_simple_function)
   707   let "?F z" = "f -` {z} \<inter> space P"
   708   let "?F' x z" = "Pair x -` ?F z"
   709   { fix x assume "x \<in> space M1"
   710     have [simp]: "\<And>z y. indicator (?F z) (x, y) = indicator (?F' x z) y"
   711       by (auto simp: indicator_def)
   712     have "\<And>y. y \<in> space M2 \<Longrightarrow> (x, y) \<in> space P" using `x \<in> space M1`
   713       by (simp add: space_pair_algebra)
   714     moreover have "\<And>x z. ?F' x z \<in> sets M2" using f_borel
   715       by (intro borel_measurable_vimage measurable_cut_fst)
   716     ultimately have "M2.simple_function (\<lambda> y. f (x, y))"
   717       apply (rule_tac M2.simple_function_cong[THEN iffD2, OF _])
   718       apply (rule simple_function_indicator_representation[OF f])
   719       using `x \<in> space M1` by (auto simp del: space_sigma) }
   720   note M2_sf = this
   721   { fix x assume x: "x \<in> space M1"
   722     then have "M2.positive_integral (\<lambda> y. f (x, y)) =
   723         (\<Sum>z\<in>f ` space P. z * \<mu>2 (?F' x z))"
   724       unfolding M2.positive_integral_eq_simple_integral[OF M2_sf[OF x]]
   725       unfolding M2.simple_integral_def
   726     proof (safe intro!: setsum_mono_zero_cong_left)
   727       from f show "finite (f ` space P)" by (rule simple_functionD)
   728     next
   729       fix y assume "y \<in> space M2" then show "f (x, y) \<in> f ` space P"
   730         using `x \<in> space M1` by (auto simp: space_pair_algebra)
   731     next
   732       fix x' y assume "(x', y) \<in> space P"
   733         "f (x', y) \<notin> (\<lambda>y. f (x, y)) ` space M2"
   734       then have *: "?F' x (f (x', y)) = {}"
   735         by (force simp: space_pair_algebra)
   736       show  "f (x', y) * \<mu>2 (?F' x (f (x', y))) = 0"
   737         unfolding * by simp
   738     qed (simp add: vimage_compose[symmetric] comp_def
   739                    space_pair_algebra) }
   740   note eq = this
   741   moreover have "\<And>z. ?F z \<in> sets P"
   742     by (auto intro!: f_borel borel_measurable_vimage simp del: space_sigma)
   743   moreover then have "\<And>z. (\<lambda>x. \<mu>2 (?F' x z)) \<in> borel_measurable M1"
   744     by (auto intro!: measure_cut_measurable_fst simp del: vimage_Int)
   745   ultimately
   746   show "(\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) \<in> borel_measurable M1"
   747     and "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. f (x, y)))
   748     = positive_integral f"
   749     by (auto simp del: vimage_Int cong: measurable_cong
   750              intro!: M1.borel_measurable_pextreal_setsum
   751              simp add: M1.positive_integral_setsum simple_integral_def
   752                        M1.positive_integral_cmult
   753                        M1.positive_integral_cong[OF eq]
   754                        positive_integral_eq_simple_integral[OF f]
   755                        pair_measure_alt[symmetric])
   756 qed
   757 
   758 lemma (in pair_sigma_finite) positive_integral_fst_measurable:
   759   assumes f: "f \<in> borel_measurable P"
   760   shows "(\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) \<in> borel_measurable M1"
   761       (is "?C f \<in> borel_measurable M1")
   762     and "M1.positive_integral (\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) =
   763       positive_integral f"
   764 proof -
   765   from borel_measurable_implies_simple_function_sequence[OF f]
   766   obtain F where F: "\<And>i. simple_function (F i)" "F \<up> f" by auto
   767   then have F_borel: "\<And>i. F i \<in> borel_measurable P"
   768     and F_mono: "\<And>i x. F i x \<le> F (Suc i) x"
   769     and F_SUPR: "\<And>x. (SUP i. F i x) = f x"
   770     unfolding isoton_def le_fun_def SUPR_fun_expand
   771     by (auto intro: borel_measurable_simple_function)
   772   note sf = simple_function_cut[OF F(1)]
   773   then have "(SUP i. ?C (F i)) \<in> borel_measurable M1"
   774     using F(1) by (auto intro!: M1.borel_measurable_SUP)
   775   moreover
   776   { fix x assume "x \<in> space M1"
   777     have isotone: "(\<lambda> i y. F i (x, y)) \<up> (\<lambda>y. f (x, y))"
   778       using `F \<up> f` unfolding isoton_fun_expand
   779       by (auto simp: isoton_def)
   780     note measurable_pair_image_snd[OF F_borel`x \<in> space M1`]
   781     from M2.positive_integral_isoton[OF isotone this]
   782     have "(SUP i. ?C (F i) x) = ?C f x"
   783       by (simp add: isoton_def) }
   784   note SUPR_C = this
   785   ultimately show "?C f \<in> borel_measurable M1"
   786     unfolding SUPR_fun_expand by (simp cong: measurable_cong)
   787   have "positive_integral (\<lambda>x. SUP i. F i x) = (SUP i. positive_integral (F i))"
   788     using F_borel F_mono
   789     by (auto intro!: positive_integral_monotone_convergence_SUP[symmetric])
   790   also have "(SUP i. positive_integral (F i)) =
   791     (SUP i. M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. F i (x, y))))"
   792     unfolding sf(2) by simp
   793   also have "\<dots> = M1.positive_integral (\<lambda>x. SUP i. M2.positive_integral (\<lambda>y. F i (x, y)))"
   794     by (auto intro!: M1.positive_integral_monotone_convergence_SUP[OF _ sf(1)]
   795                      M2.positive_integral_mono F_mono)
   796   also have "\<dots> = M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. SUP i. F i (x, y)))"
   797     using F_borel F_mono
   798     by (auto intro!: M2.positive_integral_monotone_convergence_SUP
   799                      M1.positive_integral_cong measurable_pair_image_snd)
   800   finally show "M1.positive_integral (\<lambda> x. M2.positive_integral (\<lambda> y. f (x, y))) =
   801       positive_integral f"
   802     unfolding F_SUPR by simp
   803 qed
   804 
   805 lemma (in pair_sigma_finite) positive_integral_snd_measurable:
   806   assumes f: "f \<in> borel_measurable P"
   807   shows "M2.positive_integral (\<lambda>y. M1.positive_integral (\<lambda>x. f (x, y))) =
   808       positive_integral f"
   809 proof -
   810   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   811   have s: "\<And>x y. (case (x, y) of (x, y) \<Rightarrow> f (y, x)) = f (y, x)" by simp
   812   have t: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> (y, x))) = (\<lambda>(x, y). f (y, x))" by (auto simp: fun_eq_iff)
   813   have bij: "bij_betw (\<lambda>(x, y). (y, x)) (space M2 \<times> space M1) (space P)"
   814     by (auto intro!: inj_onI simp: space_pair_algebra bij_betw_def)
   815   note pair_sigma_algebra_measurable[OF f]
   816   from Q.positive_integral_fst_measurable[OF this]
   817   have "M2.positive_integral (\<lambda>y. M1.positive_integral (\<lambda>x. f (x, y))) =
   818     Q.positive_integral (\<lambda>(x, y). f (y, x))"
   819     by simp
   820   also have "\<dots> = positive_integral f"
   821     unfolding positive_integral_vimage[OF bij, of f] t
   822     unfolding pair_sigma_algebra_swap[symmetric]
   823   proof (rule Q.positive_integral_cong_measure[symmetric])
   824     fix A assume "A \<in> sets Q.P"
   825     from this Q.sets_pair_sigma_algebra_swap[OF this]
   826     show "pair_measure ((\<lambda>(x, y). (y, x)) ` A) = Q.pair_measure A"
   827       by (auto intro!: M1.positive_integral_cong arg_cong[where f=\<mu>2]
   828                simp: pair_measure_alt Q.pair_measure_alt2)
   829   qed
   830   finally show ?thesis .
   831 qed
   832 
   833 lemma (in pair_sigma_finite) Fubini:
   834   assumes f: "f \<in> borel_measurable P"
   835   shows "M2.positive_integral (\<lambda>y. M1.positive_integral (\<lambda>x. f (x, y))) =
   836       M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. f (x, y)))"
   837   unfolding positive_integral_snd_measurable[OF assms]
   838   unfolding positive_integral_fst_measurable[OF assms] ..
   839 
   840 lemma (in pair_sigma_finite) AE_pair:
   841   assumes "almost_everywhere (\<lambda>x. Q x)"
   842   shows "M1.almost_everywhere (\<lambda>x. M2.almost_everywhere (\<lambda>y. Q (x, y)))"
   843 proof -
   844   obtain N where N: "N \<in> sets P" "pair_measure N = 0" "{x\<in>space P. \<not> Q x} \<subseteq> N"
   845     using assms unfolding almost_everywhere_def by auto
   846   show ?thesis
   847   proof (rule M1.AE_I)
   848     from N measure_cut_measurable_fst[OF `N \<in> sets P`]
   849     show "\<mu>1 {x\<in>space M1. \<mu>2 (Pair x -` N) \<noteq> 0} = 0"
   850       by (simp add: M1.positive_integral_0_iff pair_measure_alt vimage_def)
   851     show "{x \<in> space M1. \<mu>2 (Pair x -` N) \<noteq> 0} \<in> sets M1"
   852       by (intro M1.borel_measurable_pextreal_neq_const measure_cut_measurable_fst N)
   853     { fix x assume "x \<in> space M1" "\<mu>2 (Pair x -` N) = 0"
   854       have "M2.almost_everywhere (\<lambda>y. Q (x, y))"
   855       proof (rule M2.AE_I)
   856         show "\<mu>2 (Pair x -` N) = 0" by fact
   857         show "Pair x -` N \<in> sets M2" by (intro measurable_cut_fst N)
   858         show "{y \<in> space M2. \<not> Q (x, y)} \<subseteq> Pair x -` N"
   859           using N `x \<in> space M1` unfolding space_sigma space_pair_algebra by auto
   860       qed }
   861     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}"
   862       by auto
   863   qed
   864 qed
   865 
   866 lemma (in pair_sigma_finite) positive_integral_product_swap:
   867   "measure_space.positive_integral
   868     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) f =
   869   positive_integral (\<lambda>(x,y). f (y,x))"
   870 proof -
   871   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   872   have t: "(\<lambda>y. case case y of (y, x) \<Rightarrow> (x, y) of (x, y) \<Rightarrow> f (y, x)) = f"
   873     by (auto simp: fun_eq_iff)
   874   have bij: "bij_betw (\<lambda>(x, y). (y, x)) (space M2 \<times> space M1) (space P)"
   875     by (auto intro!: inj_onI simp: space_pair_algebra bij_betw_def)
   876   show ?thesis
   877     unfolding positive_integral_vimage[OF bij, of "\<lambda>(y,x). f (x,y)"]
   878     unfolding pair_sigma_algebra_swap[symmetric] t
   879   proof (rule Q.positive_integral_cong_measure[symmetric])
   880     fix A assume "A \<in> sets Q.P"
   881     from this Q.sets_pair_sigma_algebra_swap[OF this]
   882     show "pair_measure ((\<lambda>(x, y). (y, x)) ` A) = Q.pair_measure A"
   883       by (auto intro!: M1.positive_integral_cong arg_cong[where f=\<mu>2]
   884                simp: pair_measure_alt Q.pair_measure_alt2)
   885   qed
   886 qed
   887 
   888 lemma (in pair_sigma_algebra) measurable_product_swap:
   889   "f \<in> measurable (sigma (pair_algebra M2 M1)) M \<longleftrightarrow> (\<lambda>(x,y). f (y,x)) \<in> measurable P M"
   890 proof -
   891   interpret Q: pair_sigma_algebra M2 M1 by default
   892   show ?thesis
   893     using pair_sigma_algebra_measurable[of "\<lambda>(x,y). f (y, x)"]
   894     by (auto intro!: pair_sigma_algebra_measurable Q.pair_sigma_algebra_measurable iffI)
   895 qed
   896 
   897 lemma (in pair_sigma_finite) integrable_product_swap:
   898   "measure_space.integrable
   899     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) f \<longleftrightarrow>
   900   integrable (\<lambda>(x,y). f (y,x))"
   901 proof -
   902   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   903   show ?thesis
   904     unfolding Q.integrable_def integrable_def
   905     unfolding positive_integral_product_swap
   906     unfolding measurable_product_swap
   907     by (simp add: case_prod_distrib)
   908 qed
   909 
   910 lemma (in pair_sigma_finite) integral_product_swap:
   911   "measure_space.integral
   912     (sigma (pair_algebra M2 M1)) (pair_sigma_finite.pair_measure M2 \<mu>2 M1 \<mu>1) f =
   913   integral (\<lambda>(x,y). f (y,x))"
   914 proof -
   915   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   916   show ?thesis
   917     unfolding integral_def Q.integral_def positive_integral_product_swap
   918     by (simp add: case_prod_distrib)
   919 qed
   920 
   921 lemma (in pair_sigma_finite) integrable_fst_measurable:
   922   assumes f: "integrable f"
   923   shows "M1.almost_everywhere (\<lambda>x. M2.integrable (\<lambda> y. f (x, y)))" (is "?AE")
   924     and "M1.integral (\<lambda> x. M2.integral (\<lambda> y. f (x, y))) = integral f" (is "?INT")
   925 proof -
   926   let "?pf x" = "Real (f x)" and "?nf x" = "Real (- f x)"
   927   have
   928     borel: "?nf \<in> borel_measurable P""?pf \<in> borel_measurable P" and
   929     int: "positive_integral ?nf \<noteq> \<omega>" "positive_integral ?pf \<noteq> \<omega>"
   930     using assms by auto
   931   have "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. Real (f (x, y)))) \<noteq> \<omega>"
   932      "M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>y. Real (- f (x, y)))) \<noteq> \<omega>"
   933     using borel[THEN positive_integral_fst_measurable(1)] int
   934     unfolding borel[THEN positive_integral_fst_measurable(2)] by simp_all
   935   with borel[THEN positive_integral_fst_measurable(1)]
   936   have AE: "M1.almost_everywhere (\<lambda>x. M2.positive_integral (\<lambda>y. Real (f (x, y))) \<noteq> \<omega>)"
   937     "M1.almost_everywhere (\<lambda>x. M2.positive_integral (\<lambda>y. Real (- f (x, y))) \<noteq> \<omega>)"
   938     by (auto intro!: M1.positive_integral_omega_AE)
   939   then show ?AE
   940     apply (rule M1.AE_mp[OF _ M1.AE_mp])
   941     apply (rule M1.AE_cong)
   942     using assms unfolding M2.integrable_def
   943     by (auto intro!: measurable_pair_image_snd)
   944   have "M1.integrable
   945      (\<lambda>x. real (M2.positive_integral (\<lambda>xa. Real (f (x, xa)))))" (is "M1.integrable ?f")
   946   proof (unfold M1.integrable_def, intro conjI)
   947     show "?f \<in> borel_measurable M1"
   948       using borel by (auto intro!: M1.borel_measurable_real positive_integral_fst_measurable)
   949     have "M1.positive_integral (\<lambda>x. Real (?f x)) =
   950         M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>xa. Real (f (x, xa))))"
   951       apply (rule M1.positive_integral_cong_AE)
   952       apply (rule M1.AE_mp[OF AE(1)])
   953       apply (rule M1.AE_cong)
   954       by (auto simp: Real_real)
   955     then show "M1.positive_integral (\<lambda>x. Real (?f x)) \<noteq> \<omega>"
   956       using positive_integral_fst_measurable[OF borel(2)] int by simp
   957     have "M1.positive_integral (\<lambda>x. Real (- ?f x)) = M1.positive_integral (\<lambda>x. 0)"
   958       by (intro M1.positive_integral_cong) simp
   959     then show "M1.positive_integral (\<lambda>x. Real (- ?f x)) \<noteq> \<omega>" by simp
   960   qed
   961   moreover have "M1.integrable
   962      (\<lambda>x. real (M2.positive_integral (\<lambda>xa. Real (- f (x, xa)))))" (is "M1.integrable ?f")
   963   proof (unfold M1.integrable_def, intro conjI)
   964     show "?f \<in> borel_measurable M1"
   965       using borel by (auto intro!: M1.borel_measurable_real positive_integral_fst_measurable)
   966     have "M1.positive_integral (\<lambda>x. Real (?f x)) =
   967         M1.positive_integral (\<lambda>x. M2.positive_integral (\<lambda>xa. Real (- f (x, xa))))"
   968       apply (rule M1.positive_integral_cong_AE)
   969       apply (rule M1.AE_mp[OF AE(2)])
   970       apply (rule M1.AE_cong)
   971       by (auto simp: Real_real)
   972     then show "M1.positive_integral (\<lambda>x. Real (?f x)) \<noteq> \<omega>"
   973       using positive_integral_fst_measurable[OF borel(1)] int by simp
   974     have "M1.positive_integral (\<lambda>x. Real (- ?f x)) = M1.positive_integral (\<lambda>x. 0)"
   975       by (intro M1.positive_integral_cong) simp
   976     then show "M1.positive_integral (\<lambda>x. Real (- ?f x)) \<noteq> \<omega>" by simp
   977   qed
   978   ultimately show ?INT
   979     unfolding M2.integral_def integral_def
   980       borel[THEN positive_integral_fst_measurable(2), symmetric]
   981     by (simp add: M1.integral_real[OF AE(1)] M1.integral_real[OF AE(2)])
   982 qed
   983 
   984 lemma (in pair_sigma_finite) integrable_snd_measurable:
   985   assumes f: "integrable f"
   986   shows "M2.almost_everywhere (\<lambda>y. M1.integrable (\<lambda>x. f (x, y)))" (is "?AE")
   987     and "M2.integral (\<lambda>y. M1.integral (\<lambda>x. f (x, y))) = integral f" (is "?INT")
   988 proof -
   989   interpret Q: pair_sigma_finite M2 \<mu>2 M1 \<mu>1 by default
   990   have Q_int: "Q.integrable (\<lambda>(x, y). f (y, x))"
   991     using f unfolding integrable_product_swap by simp
   992   show ?INT
   993     using Q.integrable_fst_measurable(2)[OF Q_int]
   994     unfolding integral_product_swap by simp
   995   show ?AE
   996     using Q.integrable_fst_measurable(1)[OF Q_int]
   997     by simp
   998 qed
   999 
  1000 lemma (in pair_sigma_finite) Fubini_integral:
  1001   assumes f: "integrable f"
  1002   shows "M2.integral (\<lambda>y. M1.integral (\<lambda>x. f (x, y))) =
  1003       M1.integral (\<lambda>x. M2.integral (\<lambda>y. f (x, y)))"
  1004   unfolding integrable_snd_measurable[OF assms]
  1005   unfolding integrable_fst_measurable[OF assms] ..
  1006 
  1007 section "Finite product spaces"
  1008 
  1009 section "Products"
  1010 
  1011 locale product_sigma_algebra =
  1012   fixes M :: "'i \<Rightarrow> 'a algebra"
  1013   assumes sigma_algebras: "\<And>i. sigma_algebra (M i)"
  1014 
  1015 locale finite_product_sigma_algebra = product_sigma_algebra M for M :: "'i \<Rightarrow> 'a algebra" +
  1016   fixes I :: "'i set"
  1017   assumes finite_index: "finite I"
  1018 
  1019 syntax
  1020   "_PiE"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PIE _:_./ _)" 10)
  1021 
  1022 syntax (xsymbols)
  1023   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
  1024 
  1025 syntax (HTML output)
  1026   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
  1027 
  1028 translations
  1029   "PIE x:A. B" == "CONST Pi\<^isub>E A (%x. B)"
  1030 
  1031 definition
  1032   "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>"
  1033 
  1034 abbreviation (in finite_product_sigma_algebra) "G \<equiv> product_algebra M I"
  1035 abbreviation (in finite_product_sigma_algebra) "P \<equiv> sigma G"
  1036 
  1037 sublocale product_sigma_algebra \<subseteq> M: sigma_algebra "M i" for i by (rule sigma_algebras)
  1038 
  1039 lemma (in finite_product_sigma_algebra) product_algebra_into_space:
  1040   "sets G \<subseteq> Pow (space G)"
  1041   using M.sets_into_space unfolding product_algebra_def
  1042   by auto blast
  1043 
  1044 sublocale finite_product_sigma_algebra \<subseteq> sigma_algebra P
  1045   using product_algebra_into_space by (rule sigma_algebra_sigma)
  1046 
  1047 lemma product_algebraE:
  1048   assumes "A \<in> sets (product_algebra M I)"
  1049   obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
  1050   using assms unfolding product_algebra_def by auto
  1051 
  1052 lemma product_algebraI[intro]:
  1053   assumes "E \<in> (\<Pi> i\<in>I. sets (M i))"
  1054   shows "Pi\<^isub>E I E \<in> sets (product_algebra M I)"
  1055   using assms unfolding product_algebra_def by auto
  1056 
  1057 lemma space_product_algebra[simp]:
  1058   "space (product_algebra M I) = Pi\<^isub>E I (\<lambda>i. space (M i))"
  1059   unfolding product_algebra_def by simp
  1060 
  1061 lemma product_algebra_sets_into_space:
  1062   assumes "\<And>i. i\<in>I \<Longrightarrow> sets (M i) \<subseteq> Pow (space (M i))"
  1063   shows "sets (product_algebra M I) \<subseteq> Pow (space (product_algebra M I))"
  1064   using assms by (auto simp: product_algebra_def) blast
  1065 
  1066 lemma (in finite_product_sigma_algebra) P_empty:
  1067   "I = {} \<Longrightarrow> P = \<lparr> space = {\<lambda>k. undefined}, sets = { {}, {\<lambda>k. undefined} }\<rparr>"
  1068   unfolding product_algebra_def by (simp add: sigma_def image_constant)
  1069 
  1070 lemma (in finite_product_sigma_algebra) in_P[simp, intro]:
  1071   "\<lbrakk> \<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i) \<rbrakk> \<Longrightarrow> Pi\<^isub>E I A \<in> sets P"
  1072   by (auto simp: product_algebra_def sets_sigma intro!: sigma_sets.Basic)
  1073 
  1074 lemma (in product_sigma_algebra) bij_inv_restrict_merge:
  1075   assumes [simp]: "I \<inter> J = {}"
  1076   shows "bij_inv
  1077     (space (sigma (product_algebra M (I \<union> J))))
  1078     (space (sigma (pair_algebra (product_algebra M I) (product_algebra M J))))
  1079     (\<lambda>x. (restrict x I, restrict x J)) (\<lambda>(x, y). merge I x J y)"
  1080   by (rule bij_invI) (auto simp: space_pair_algebra extensional_restrict)
  1081 
  1082 lemma (in product_sigma_algebra) bij_inv_singleton:
  1083   "bij_inv (space (sigma (product_algebra M {i}))) (space (M i))
  1084     (\<lambda>x. x i) (\<lambda>x. (\<lambda>j\<in>{i}. x))"
  1085   by (rule bij_invI) (auto simp: restrict_def extensional_def fun_eq_iff)
  1086 
  1087 lemma (in product_sigma_algebra) bij_inv_restrict_insert:
  1088   assumes [simp]: "i \<notin> I"
  1089   shows "bij_inv
  1090     (space (sigma (product_algebra M (insert i I))))
  1091     (space (sigma (pair_algebra (product_algebra M I) (M i))))
  1092     (\<lambda>x. (restrict x I, x i)) (\<lambda>(x, y). x(i := y))"
  1093   by (rule bij_invI) (auto simp: space_pair_algebra extensional_restrict)
  1094 
  1095 lemma (in product_sigma_algebra) measurable_restrict_on_generating:
  1096   assumes [simp]: "I \<inter> J = {}"
  1097   shows "(\<lambda>x. (restrict x I, restrict x J)) \<in> measurable
  1098     (product_algebra M (I \<union> J))
  1099     (pair_algebra (product_algebra M I) (product_algebra M J))"
  1100     (is "?R \<in> measurable ?IJ ?P")
  1101 proof (unfold measurable_def, intro CollectI conjI ballI)
  1102   show "?R \<in> space ?IJ \<rightarrow> space ?P" by (auto simp: space_pair_algebra)
  1103   { fix F E assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "F \<in> (\<Pi> i\<in>J. sets (M i))"
  1104     then have "Pi (I \<union> J) (merge I E J F) \<inter> (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) =
  1105         Pi\<^isub>E (I \<union> J) (merge I E J F)"
  1106       using M.sets_into_space by auto blast+ }
  1107   note this[simp]
  1108   show "\<And>A. A \<in> sets ?P \<Longrightarrow> ?R -` A \<inter> space ?IJ \<in> sets ?IJ"
  1109     by (force elim!: pair_algebraE product_algebraE
  1110               simp del: vimage_Int simp: restrict_vimage merge_vimage space_pair_algebra)
  1111   qed
  1112 
  1113 lemma (in product_sigma_algebra) measurable_merge_on_generating:
  1114   assumes [simp]: "I \<inter> J = {}"
  1115   shows "(\<lambda>(x, y). merge I x J y) \<in> measurable
  1116     (pair_algebra (product_algebra M I) (product_algebra M J))
  1117     (product_algebra M (I \<union> J))"
  1118     (is "?M \<in> measurable ?P ?IJ")
  1119 proof (unfold measurable_def, intro CollectI conjI ballI)
  1120   show "?M \<in> space ?P \<rightarrow> space ?IJ" by (auto simp: space_pair_algebra)
  1121   { fix E assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "E \<in> (\<Pi> i\<in>J. sets (M i))"
  1122     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)) =
  1123         Pi\<^isub>E I E \<times> Pi\<^isub>E J E"
  1124       using M.sets_into_space by auto blast+ }
  1125   note this[simp]
  1126   show "\<And>A. A \<in> sets ?IJ \<Longrightarrow> ?M -` A \<inter> space ?P \<in> sets ?P"
  1127     by (force elim!: pair_algebraE product_algebraE
  1128               simp del: vimage_Int simp: restrict_vimage merge_vimage space_pair_algebra)
  1129   qed
  1130 
  1131 lemma (in product_sigma_algebra) measurable_singleton_on_generator:
  1132   "(\<lambda>x. \<lambda>j\<in>{i}. x) \<in> measurable (M i) (product_algebra M {i})"
  1133   (is "?f \<in> measurable _ ?P")
  1134 proof (unfold measurable_def, intro CollectI conjI)
  1135   show "?f \<in> space (M i) \<rightarrow> space ?P" by auto
  1136   have "\<And>E. E i \<in> sets (M i) \<Longrightarrow> ?f -` Pi\<^isub>E {i} E \<inter> space (M i) = E i"
  1137     using M.sets_into_space by auto
  1138   then show "\<forall>A \<in> sets ?P. ?f -` A \<inter> space (M i) \<in> sets (M i)"
  1139     by (auto elim!: product_algebraE)
  1140 qed
  1141 
  1142 lemma (in product_sigma_algebra) measurable_component_on_generator:
  1143   assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (product_algebra M I) (M i)"
  1144   (is "?f \<in> measurable ?P _")
  1145 proof (unfold measurable_def, intro CollectI conjI ballI)
  1146   show "?f \<in> space ?P \<rightarrow> space (M i)" using `i \<in> I` by auto
  1147   fix A assume "A \<in> sets (M i)"
  1148   moreover then have "(\<lambda>x. x i) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. space (M i)) =
  1149       (\<Pi>\<^isub>E j\<in>I. if i = j then A else space (M j))"
  1150     using M.sets_into_space `i \<in> I`
  1151     by (fastsimp dest: Pi_mem split: split_if_asm)
  1152   ultimately show "?f -` A \<inter> space ?P \<in> sets ?P"
  1153     by (auto intro!: product_algebraI)
  1154 qed
  1155 
  1156 lemma (in product_sigma_algebra) measurable_restrict_singleton_on_generating:
  1157   assumes [simp]: "i \<notin> I"
  1158   shows "(\<lambda>x. (restrict x I, x i)) \<in> measurable
  1159     (product_algebra M (insert i I))
  1160     (pair_algebra (product_algebra M I) (M i))"
  1161     (is "?R \<in> measurable ?I ?P")
  1162 proof (unfold measurable_def, intro CollectI conjI ballI)
  1163   show "?R \<in> space ?I \<rightarrow> space ?P" by (auto simp: space_pair_algebra)
  1164   { fix E F assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "F \<in> sets (M i)"
  1165     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)) =
  1166         Pi\<^isub>E (insert i I) (E(i := F))"
  1167       using M.sets_into_space using `i\<notin>I` by (auto simp: restrict_Pi_cancel) blast+ }
  1168   note this[simp]
  1169   show "\<And>A. A \<in> sets ?P \<Longrightarrow> ?R -` A \<inter> space ?I \<in> sets ?I"
  1170     by (force elim!: pair_algebraE product_algebraE
  1171               simp del: vimage_Int simp: space_pair_algebra)
  1172 qed
  1173 
  1174 lemma (in product_sigma_algebra) measurable_merge_singleton_on_generating:
  1175   assumes [simp]: "i \<notin> I"
  1176   shows "(\<lambda>(x, y). x(i := y)) \<in> measurable
  1177     (pair_algebra (product_algebra M I) (M i))
  1178     (product_algebra M (insert i I))"
  1179     (is "?M \<in> measurable ?P ?IJ")
  1180 proof (unfold measurable_def, intro CollectI conjI ballI)
  1181   show "?M \<in> space ?P \<rightarrow> space ?IJ" by (auto simp: space_pair_algebra)
  1182   { fix E assume "E \<in> (\<Pi> i\<in>I. sets (M i))" "E i \<in> sets (M i)"
  1183     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) =
  1184         Pi\<^isub>E I E \<times> E i"
  1185       using M.sets_into_space by auto blast+ }
  1186   note this[simp]
  1187   show "\<And>A. A \<in> sets ?IJ \<Longrightarrow> ?M -` A \<inter> space ?P \<in> sets ?P"
  1188     by (force elim!: pair_algebraE product_algebraE
  1189               simp del: vimage_Int simp: space_pair_algebra)
  1190 qed
  1191 
  1192 section "Generating set generates also product algebra"
  1193 
  1194 lemma pair_sigma_algebra_sigma:
  1195   assumes 1: "S1 \<up> (space E1)" "range S1 \<subseteq> sets E1" and E1: "sets E1 \<subseteq> Pow (space E1)"
  1196   assumes 2: "S2 \<up> (space E2)" "range S2 \<subseteq> sets E2" and E2: "sets E2 \<subseteq> Pow (space E2)"
  1197   shows "sigma (pair_algebra (sigma E1) (sigma E2)) = sigma (pair_algebra E1 E2)"
  1198     (is "?S = ?E")
  1199 proof -
  1200   interpret M1: sigma_algebra "sigma E1" using E1 by (rule sigma_algebra_sigma)
  1201   interpret M2: sigma_algebra "sigma E2" using E2 by (rule sigma_algebra_sigma)
  1202   have P: "sets (pair_algebra E1 E2) \<subseteq> Pow (space E1 \<times> space E2)"
  1203     using E1 E2 by (auto simp add: pair_algebra_def)
  1204   interpret E: sigma_algebra ?E unfolding pair_algebra_def
  1205     using E1 E2 by (intro sigma_algebra_sigma) auto
  1206   { fix A assume "A \<in> sets E1"
  1207     then have "fst -` A \<inter> space ?E = A \<times> (\<Union>i. S2 i)"
  1208       using E1 2 unfolding isoton_def pair_algebra_def by auto
  1209     also have "\<dots> = (\<Union>i. A \<times> S2 i)" by auto
  1210     also have "\<dots> \<in> sets ?E" unfolding pair_algebra_def sets_sigma
  1211       using 2 `A \<in> sets E1`
  1212       by (intro sigma_sets.Union)
  1213          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
  1214     finally have "fst -` A \<inter> space ?E \<in> sets ?E" . }
  1215   moreover
  1216   { fix B assume "B \<in> sets E2"
  1217     then have "snd -` B \<inter> space ?E = (\<Union>i. S1 i) \<times> B"
  1218       using E2 1 unfolding isoton_def pair_algebra_def by auto
  1219     also have "\<dots> = (\<Union>i. S1 i \<times> B)" by auto
  1220     also have "\<dots> \<in> sets ?E"
  1221       using 1 `B \<in> sets E2` unfolding pair_algebra_def sets_sigma
  1222       by (intro sigma_sets.Union)
  1223          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
  1224     finally have "snd -` B \<inter> space ?E \<in> sets ?E" . }
  1225   ultimately have proj:
  1226     "fst \<in> measurable ?E (sigma E1) \<and> snd \<in> measurable ?E (sigma E2)"
  1227     using E1 E2 by (subst (1 2) E.measurable_iff_sigma)
  1228                    (auto simp: pair_algebra_def sets_sigma)
  1229   { fix A B assume A: "A \<in> sets (sigma E1)" and B: "B \<in> sets (sigma E2)"
  1230     with proj have "fst -` A \<inter> space ?E \<in> sets ?E" "snd -` B \<inter> space ?E \<in> sets ?E"
  1231       unfolding measurable_def by simp_all
  1232     moreover have "A \<times> B = (fst -` A \<inter> space ?E) \<inter> (snd -` B \<inter> space ?E)"
  1233       using A B M1.sets_into_space M2.sets_into_space
  1234       by (auto simp: pair_algebra_def)
  1235     ultimately have "A \<times> B \<in> sets ?E" by auto }
  1236   then have "sigma_sets (space ?E) (sets (pair_algebra (sigma E1) (sigma E2))) \<subseteq> sets ?E"
  1237     by (intro E.sigma_sets_subset) (auto simp add: pair_algebra_def sets_sigma)
  1238   then have subset: "sets ?S \<subseteq> sets ?E"
  1239     by (simp add: sets_sigma pair_algebra_def)
  1240   have "sets ?S = sets ?E"
  1241   proof (intro set_eqI iffI)
  1242     fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
  1243       unfolding sets_sigma
  1244     proof induct
  1245       case (Basic A) then show ?case
  1246         by (auto simp: pair_algebra_def sets_sigma intro: sigma_sets.Basic)
  1247     qed (auto intro: sigma_sets.intros simp: pair_algebra_def)
  1248   next
  1249     fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
  1250   qed
  1251   then show ?thesis
  1252     by (simp add: pair_algebra_def sigma_def)
  1253 qed
  1254 
  1255 lemma sigma_product_algebra_sigma_eq:
  1256   assumes "finite I"
  1257   assumes isotone: "\<And>i. i \<in> I \<Longrightarrow> (S i) \<up> (space (E i))"
  1258   assumes sets_into: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> sets (E i)"
  1259   and E: "\<And>i. sets (E i) \<subseteq> Pow (space (E i))"
  1260   shows "sigma (product_algebra (\<lambda>i. sigma (E i)) I) = sigma (product_algebra E I)"
  1261     (is "?S = ?E")
  1262 proof cases
  1263   assume "I = {}" then show ?thesis by (simp add: product_algebra_def)
  1264 next
  1265   assume "I \<noteq> {}"
  1266   interpret E: sigma_algebra "sigma (E i)" for i
  1267     using E by (rule sigma_algebra_sigma)
  1268 
  1269   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)"
  1270     using E by auto
  1271 
  1272   interpret G: sigma_algebra ?E
  1273     unfolding product_algebra_def using E
  1274     by (intro sigma_algebra_sigma) (auto dest: Pi_mem)
  1275 
  1276   { fix A i assume "i \<in> I" and A: "A \<in> sets (E i)"
  1277     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"
  1278       using isotone unfolding isoton_def product_algebra_def by (auto dest: Pi_mem)
  1279     also have "\<dots> = (\<Union>n. (\<Pi>\<^isub>E j\<in>I. if j = i then A else S j n))"
  1280       unfolding product_algebra_def
  1281       apply simp
  1282       apply (subst Pi_UN[OF `finite I`])
  1283       using isotone[THEN isoton_mono_le] apply simp
  1284       apply (simp add: PiE_Int)
  1285       apply (intro PiE_cong)
  1286       using A sets_into by (auto intro!: into_space)
  1287     also have "\<dots> \<in> sets ?E" unfolding product_algebra_def sets_sigma
  1288       using sets_into `A \<in> sets (E i)`
  1289       by (intro sigma_sets.Union)
  1290          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
  1291     finally have "(\<lambda>x. x i) -` A \<inter> space ?E \<in> sets ?E" . }
  1292   then have proj:
  1293     "\<And>i. i\<in>I \<Longrightarrow> (\<lambda>x. x i) \<in> measurable ?E (sigma (E i))"
  1294     using E by (subst G.measurable_iff_sigma)
  1295                (auto simp: product_algebra_def sets_sigma)
  1296 
  1297   { fix A assume A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (sigma (E i))"
  1298     with proj have basic: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) -` (A i) \<inter> space ?E \<in> sets ?E"
  1299       unfolding measurable_def by simp
  1300     have "Pi\<^isub>E I A = (\<Inter>i\<in>I. (\<lambda>x. x i) -` (A i) \<inter> space ?E)"
  1301       using A E.sets_into_space `I \<noteq> {}` unfolding product_algebra_def by auto blast
  1302     then have "Pi\<^isub>E I A \<in> sets ?E"
  1303       using G.finite_INT[OF `finite I` `I \<noteq> {}` basic, of "\<lambda>i. i"] by simp }
  1304   then have "sigma_sets (space ?E) (sets (product_algebra (\<lambda>i. sigma (E i)) I)) \<subseteq> sets ?E"
  1305     by (intro G.sigma_sets_subset) (auto simp add: sets_sigma product_algebra_def)
  1306   then have subset: "sets ?S \<subseteq> sets ?E"
  1307     by (simp add: sets_sigma product_algebra_def)
  1308 
  1309   have "sets ?S = sets ?E"
  1310   proof (intro set_eqI iffI)
  1311     fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
  1312       unfolding sets_sigma
  1313     proof induct
  1314       case (Basic A) then show ?case
  1315         by (auto simp: sets_sigma product_algebra_def intro: sigma_sets.Basic)
  1316     qed (auto intro: sigma_sets.intros simp: product_algebra_def)
  1317   next
  1318     fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
  1319   qed
  1320   then show ?thesis
  1321     by (simp add: product_algebra_def sigma_def)
  1322 qed
  1323 
  1324 lemma (in product_sigma_algebra) sigma_pair_algebra_sigma_eq:
  1325   "sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))) =
  1326    sigma (pair_algebra (product_algebra M I) (product_algebra M J))"
  1327   using M.sets_into_space
  1328   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)"])
  1329      (auto simp: isoton_const product_algebra_def, blast+)
  1330 
  1331 lemma (in product_sigma_algebra) sigma_pair_algebra_product_singleton:
  1332   "sigma (pair_algebra (sigma (product_algebra M I)) (M i)) =
  1333    sigma (pair_algebra (product_algebra M I) (M i))"
  1334   using M.sets_into_space apply (subst M.sigma_eq[symmetric])
  1335   by (intro pair_sigma_algebra_sigma[of "\<lambda>_. \<Pi>\<^isub>E i\<in>I. space (M i)" _ "\<lambda>_. space (M i)"])
  1336      (auto simp: isoton_const product_algebra_def, blast+)
  1337 
  1338 lemma (in product_sigma_algebra) measurable_restrict:
  1339   assumes [simp]: "I \<inter> J = {}"
  1340   shows "(\<lambda>x. (restrict x I, restrict x J)) \<in> measurable
  1341     (sigma (product_algebra M (I \<union> J)))
  1342     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))"
  1343   unfolding sigma_pair_algebra_sigma_eq using M.sets_into_space
  1344   by (intro measurable_sigma_sigma measurable_restrict_on_generating
  1345             pair_algebra_sets_into_space product_algebra_sets_into_space)
  1346      auto
  1347 
  1348 lemma (in product_sigma_algebra) measurable_merge:
  1349   assumes [simp]: "I \<inter> J = {}"
  1350   shows "(\<lambda>(x, y). merge I x J y) \<in> measurable
  1351     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))
  1352     (sigma (product_algebra M (I \<union> J)))"
  1353   unfolding sigma_pair_algebra_sigma_eq using M.sets_into_space
  1354   by (intro measurable_sigma_sigma measurable_merge_on_generating
  1355             pair_algebra_sets_into_space product_algebra_sets_into_space)
  1356      auto
  1357 
  1358 lemma (in product_sigma_algebra) product_product_vimage_algebra:
  1359   assumes [simp]: "I \<inter> J = {}"
  1360   shows "sigma_algebra.vimage_algebra
  1361     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))
  1362     (space (sigma (product_algebra M (I \<union> J)))) (\<lambda>x. ((\<lambda>i\<in>I. x i), (\<lambda>i\<in>J. x i))) =
  1363     sigma (product_algebra M (I \<union> J))"
  1364   unfolding sigma_pair_algebra_sigma_eq using sets_into_space
  1365   by (intro vimage_algebra_sigma[OF bij_inv_restrict_merge]
  1366             pair_algebra_sets_into_space product_algebra_sets_into_space
  1367             measurable_merge_on_generating measurable_restrict_on_generating)
  1368      auto
  1369 
  1370 lemma (in product_sigma_algebra) pair_product_product_vimage_algebra:
  1371   assumes [simp]: "I \<inter> J = {}"
  1372   shows "sigma_algebra.vimage_algebra (sigma (product_algebra M (I \<union> J)))
  1373     (space (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))) (\<lambda>(x,y). merge I x J y) =
  1374     (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J))))"
  1375   unfolding sigma_pair_algebra_sigma_eq using sets_into_space
  1376   by (intro vimage_algebra_sigma[OF bij_inv_restrict_merge[symmetric]]
  1377             pair_algebra_sets_into_space product_algebra_sets_into_space
  1378             measurable_merge_on_generating measurable_restrict_on_generating)
  1379      auto
  1380 
  1381 lemma (in product_sigma_algebra) pair_product_singleton_vimage_algebra:
  1382   assumes [simp]: "i \<notin> I"
  1383   shows "sigma_algebra.vimage_algebra (sigma (pair_algebra (sigma (product_algebra M I)) (M i)))
  1384     (space (sigma (product_algebra M (insert i I)))) (\<lambda>x. (restrict x I, x i)) =
  1385     (sigma (product_algebra M (insert i I)))"
  1386   unfolding sigma_pair_algebra_product_singleton using sets_into_space
  1387   by (intro vimage_algebra_sigma[OF bij_inv_restrict_insert]
  1388             pair_algebra_sets_into_space product_algebra_sets_into_space
  1389             measurable_merge_singleton_on_generating measurable_restrict_singleton_on_generating)
  1390       auto
  1391 
  1392 lemma (in product_sigma_algebra) singleton_vimage_algebra:
  1393   "sigma_algebra.vimage_algebra (sigma (product_algebra M {i})) (space (M i)) (\<lambda>x. \<lambda>j\<in>{i}. x) = M i"
  1394   using sets_into_space
  1395   by (intro vimage_algebra_sigma[of "M i", unfolded M.sigma_eq, OF bij_inv_singleton[symmetric]]
  1396              product_algebra_sets_into_space measurable_singleton_on_generator measurable_component_on_generator)
  1397      auto
  1398 
  1399 lemma (in product_sigma_algebra) measurable_restrict_iff:
  1400   assumes IJ[simp]: "I \<inter> J = {}"
  1401   shows "f \<in> measurable (sigma (pair_algebra
  1402       (sigma (product_algebra M I)) (sigma (product_algebra M J)))) M' \<longleftrightarrow>
  1403     (\<lambda>x. f (restrict x I, restrict x J)) \<in> measurable (sigma (product_algebra M (I \<union> J))) M'"
  1404   using M.sets_into_space
  1405   apply (subst pair_product_product_vimage_algebra[OF IJ, symmetric])
  1406   apply (subst sigma_pair_algebra_sigma_eq)
  1407   apply (subst sigma_algebra.measurable_vimage_iff_inv[OF _
  1408       bij_inv_restrict_merge[symmetric]])
  1409   apply (intro sigma_algebra_sigma product_algebra_sets_into_space)
  1410   by auto
  1411 
  1412 lemma (in product_sigma_algebra) measurable_merge_iff:
  1413   assumes IJ: "I \<inter> J = {}"
  1414   shows "f \<in> measurable (sigma (product_algebra M (I \<union> J))) M' \<longleftrightarrow>
  1415     (\<lambda>(x, y). f (merge I x J y)) \<in>
  1416       measurable (sigma (pair_algebra (sigma (product_algebra M I)) (sigma (product_algebra M J)))) M'"
  1417   unfolding measurable_restrict_iff[OF IJ]
  1418   by (rule measurable_cong) (auto intro!: arg_cong[where f=f] simp: extensional_restrict)
  1419 
  1420 lemma (in product_sigma_algebra) measurable_component:
  1421   assumes "i \<in> I" and f: "f \<in> measurable (M i) M'"
  1422   shows "(\<lambda>x. f (x i)) \<in> measurable (sigma (product_algebra M I)) M'"
  1423     (is "?f \<in> measurable ?P M'")
  1424 proof -
  1425   have "f \<circ> (\<lambda>x. x i) \<in> measurable ?P M'"
  1426     apply (rule measurable_comp[OF _ f])
  1427     using measurable_up_sigma[of "product_algebra M I" "M i"]
  1428     using measurable_component_on_generator[OF `i \<in> I`]
  1429     by auto
  1430   then show "?f \<in> measurable ?P M'" by (simp add: comp_def)
  1431 qed
  1432 
  1433 lemma (in product_sigma_algebra) measurable_component_singleton:
  1434   "(\<lambda>x. f (x i)) \<in> measurable (sigma (product_algebra M {i})) M' \<longleftrightarrow>
  1435     f \<in> measurable (M i) M'"
  1436   using sets_into_space
  1437   apply (subst singleton_vimage_algebra[symmetric])
  1438   apply (subst sigma_algebra.measurable_vimage_iff_inv[OF _ bij_inv_singleton[symmetric]])
  1439   by (auto intro!: sigma_algebra_sigma product_algebra_sets_into_space)
  1440 
  1441 lemma (in product_sigma_algebra) measurable_component_iff:
  1442   assumes "i \<in> I" and not_empty: "\<forall>i\<in>I. space (M i) \<noteq> {}"
  1443   shows "(\<lambda>x. f (x i)) \<in> measurable (sigma (product_algebra M I)) M' \<longleftrightarrow>
  1444     f \<in> measurable (M i) M'"
  1445     (is "?f \<in> measurable ?P M' \<longleftrightarrow> _")
  1446 proof
  1447   assume "f \<in> measurable (M i) M'" then show "?f \<in> measurable ?P M'"
  1448     by (rule measurable_component[OF `i \<in> I`])
  1449 next
  1450   assume f: "?f \<in> measurable ?P M'"
  1451   def t \<equiv> "\<lambda>i. SOME x. x \<in> space (M i)"
  1452   have t: "\<And>i. i\<in>I \<Longrightarrow> t i \<in> space (M i)"
  1453      unfolding t_def using not_empty by (rule_tac someI_ex) auto
  1454   have "?f \<circ> (\<lambda>x. (\<lambda>j\<in>I. if j = i then x else t j)) \<in> measurable (M i) M'"
  1455     (is "?f \<circ> ?t \<in> measurable _ _")
  1456   proof (rule measurable_comp[OF _ f])
  1457     have "?t \<in> measurable (M i) (product_algebra M I)"
  1458     proof (unfold measurable_def, intro CollectI conjI ballI)
  1459       from t show "?t \<in> space (M i) \<rightarrow> (space (product_algebra M I))" by auto
  1460     next
  1461       fix A assume A: "A \<in> sets (product_algebra M I)"
  1462       { fix E assume "E \<in> (\<Pi> i\<in>I. sets (M i))"
  1463         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 {})"
  1464           using `i \<in> I` sets_into_space by (auto dest: Pi_mem[where B=E]) }
  1465       note * = this
  1466       with A `i \<in> I` show "?t -` A \<inter> space (M i) \<in> sets (M i)"
  1467         by (auto elim!: product_algebraE simp del: vimage_Int)
  1468     qed
  1469     also have "\<dots> \<subseteq> measurable (M i) (sigma (product_algebra M I))"
  1470       using M.sets_into_space by (intro measurable_subset) (auto simp: product_algebra_def, blast)
  1471     finally show "?t \<in> measurable (M i) (sigma (product_algebra M I))" .
  1472   qed
  1473   then show "f \<in> measurable (M i) M'" unfolding comp_def using `i \<in> I` by simp
  1474 qed
  1475 
  1476 lemma (in product_sigma_algebra) measurable_singleton:
  1477   shows "f \<in> measurable (sigma (product_algebra M {i})) M' \<longleftrightarrow>
  1478     (\<lambda>x. f (\<lambda>j\<in>{i}. x)) \<in> measurable (M i) M'"
  1479   using sets_into_space unfolding measurable_component_singleton[symmetric]
  1480   by (auto intro!: measurable_cong arg_cong[where f=f] simp: fun_eq_iff extensional_def)
  1481 
  1482 locale product_sigma_finite =
  1483   fixes M :: "'i \<Rightarrow> 'a algebra" and \<mu> :: "'i \<Rightarrow> 'a set \<Rightarrow> pextreal"
  1484   assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i) (\<mu> i)"
  1485 
  1486 locale finite_product_sigma_finite = product_sigma_finite M \<mu> for M :: "'i \<Rightarrow> 'a algebra" and \<mu> +
  1487   fixes I :: "'i set" assumes finite_index': "finite I"
  1488 
  1489 sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" "\<mu> i" for i
  1490   by (rule sigma_finite_measures)
  1491 
  1492 sublocale product_sigma_finite \<subseteq> product_sigma_algebra
  1493   by default
  1494 
  1495 sublocale finite_product_sigma_finite \<subseteq> finite_product_sigma_algebra
  1496   by default (fact finite_index')
  1497 
  1498 lemma (in finite_product_sigma_finite) sigma_finite_pairs:
  1499   "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
  1500     (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
  1501     (\<forall>k. \<forall>i\<in>I. \<mu> i (F i k) \<noteq> \<omega>) \<and>
  1502     (\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k) \<up> space G"
  1503 proof -
  1504   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>)"
  1505     using M.sigma_finite_up by simp
  1506   from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
  1507   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>"
  1508     by auto
  1509   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
  1510   note space_product_algebra[simp]
  1511   show ?thesis
  1512   proof (intro exI[of _ F] conjI allI isotoneI set_eqI iffI ballI)
  1513     fix i show "range (F i) \<subseteq> sets (M i)" by fact
  1514   next
  1515     fix i k show "\<mu> i (F i k) \<noteq> \<omega>" by fact
  1516   next
  1517     fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space G"
  1518       using `\<And>i. range (F i) \<subseteq> sets (M i)` M.sets_into_space by auto blast
  1519   next
  1520     fix f assume "f \<in> space G"
  1521     with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"]
  1522       `\<And>i. F i \<up> space (M i)`[THEN isotonD(2)]
  1523       `\<And>i. F i \<up> space (M i)`[THEN isoton_mono_le]
  1524     show "f \<in> (\<Union>i. ?F i)" by auto
  1525   next
  1526     fix i show "?F i \<subseteq> ?F (Suc i)"
  1527       using `\<And>i. F i \<up> space (M i)`[THEN isotonD(1)] by auto
  1528   qed
  1529 qed
  1530 
  1531 lemma (in product_sigma_finite) product_measure_exists:
  1532   assumes "finite I"
  1533   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>
  1534      sigma_finite_measure (sigma (product_algebra M I)) \<nu>"
  1535 using `finite I` proof induct
  1536   case empty then show ?case unfolding product_algebra_def
  1537     by (auto intro!: exI[of _ "\<lambda>A. if A = {} then 0 else 1"] sigma_algebra_sigma
  1538                      sigma_algebra.finite_additivity_sufficient
  1539              simp add: positive_def additive_def sets_sigma sigma_finite_measure_def
  1540                        sigma_finite_measure_axioms_def image_constant)
  1541 next
  1542   case (insert i I)
  1543   interpret finite_product_sigma_finite M \<mu> I by default fact
  1544   have "finite (insert i I)" using `finite I` by auto
  1545   interpret I': finite_product_sigma_finite M \<mu> "insert i I" by default fact
  1546   from insert obtain \<nu> where
  1547     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
  1548     "sigma_finite_measure P \<nu>" by auto
  1549   interpret I: sigma_finite_measure P \<nu> by fact
  1550   interpret P: pair_sigma_finite P \<nu> "M i" "\<mu> i" ..
  1551 
  1552   let ?h = "\<lambda>x. (restrict x I, x i)"
  1553   let ?\<nu> = "\<lambda>A. P.pair_measure (?h ` A)"
  1554   interpret I': measure_space "sigma (product_algebra M (insert i I))" ?\<nu>
  1555     apply (subst pair_product_singleton_vimage_algebra[OF `i \<notin> I`, symmetric])
  1556     apply (intro P.measure_space_isomorphic bij_inv_bij_betw)
  1557     unfolding sigma_pair_algebra_product_singleton
  1558     by (rule bij_inv_restrict_insert[OF `i \<notin> I`])
  1559   show ?case
  1560   proof (intro exI[of _ ?\<nu>] conjI ballI)
  1561     { fix A assume A: "A \<in> (\<Pi> i\<in>insert i I. sets (M i))"
  1562       moreover then have "A \<in> (\<Pi> i\<in>I. sets (M i))" by auto
  1563       moreover have "(\<lambda>x. (restrict x I, x i)) ` Pi\<^isub>E (insert i I) A = Pi\<^isub>E I A \<times> A i"
  1564         using `i \<notin> I`
  1565         apply auto
  1566         apply (rule_tac x="a(i:=b)" in image_eqI)
  1567         apply (auto simp: extensional_def fun_eq_iff)
  1568         done
  1569       ultimately show "?\<nu> (Pi\<^isub>E (insert i I) A) = (\<Prod>i\<in>insert i I. \<mu> i (A i))"
  1570         apply simp
  1571         apply (subst P.pair_measure_times)
  1572         apply fastsimp
  1573         apply fastsimp
  1574         using `i \<notin> I` `finite I` prod[of A] by (auto simp: ac_simps) }
  1575     note product = this
  1576     show "sigma_finite_measure I'.P ?\<nu>"
  1577     proof
  1578       from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
  1579       then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
  1580         "(\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) \<up> space I'.G"
  1581         "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> \<mu> j (F j k) \<noteq> \<omega>"
  1582         by blast+
  1583       let "?F k" = "\<Pi>\<^isub>E j \<in> insert i I. F j k"
  1584       show "\<exists>F::nat \<Rightarrow> ('i \<Rightarrow> 'a) set. range F \<subseteq> sets I'.P \<and>
  1585           (\<Union>i. F i) = space I'.P \<and> (\<forall>i. ?\<nu> (F i) \<noteq> \<omega>)"
  1586       proof (intro exI[of _ ?F] conjI allI)
  1587         show "range ?F \<subseteq> sets I'.P" using F(1) by auto
  1588       next
  1589         from F(2)[THEN isotonD(2)]
  1590         show "(\<Union>i. ?F i) = space I'.P" by simp
  1591       next
  1592         fix j
  1593         show "?\<nu> (?F j) \<noteq> \<omega>"
  1594           using F `finite I`
  1595           by (subst product) (auto simp: setprod_\<omega>)
  1596       qed
  1597     qed
  1598   qed
  1599 qed
  1600 
  1601 definition (in finite_product_sigma_finite)
  1602   measure :: "('i \<Rightarrow> 'a) set \<Rightarrow> pextreal" where
  1603   "measure = (SOME \<nu>.
  1604      (\<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>
  1605      sigma_finite_measure P \<nu>)"
  1606 
  1607 sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure P measure
  1608 proof -
  1609   show "sigma_finite_measure P measure"
  1610     unfolding measure_def
  1611     by (rule someI2_ex[OF product_measure_exists[OF finite_index]]) auto
  1612 qed
  1613 
  1614 lemma (in finite_product_sigma_finite) measure_times:
  1615   assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
  1616   shows "measure (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))"
  1617 proof -
  1618   note ex = product_measure_exists[OF finite_index]
  1619   show ?thesis
  1620     unfolding measure_def
  1621   proof (rule someI2_ex[OF ex], elim conjE)
  1622     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))"
  1623     have "Pi\<^isub>E I A = Pi\<^isub>E I (\<lambda>i\<in>I. A i)" by (auto dest: Pi_mem)
  1624     then have "\<nu> (Pi\<^isub>E I A) = \<nu> (Pi\<^isub>E I (\<lambda>i\<in>I. A i))" by simp
  1625     also have "\<dots> = (\<Prod>i\<in>I. \<mu> i ((\<lambda>i\<in>I. A i) i))" using assms * by auto
  1626     finally show "\<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. \<mu> i (A i))" by simp
  1627   qed
  1628 qed
  1629 
  1630 abbreviation (in product_sigma_finite)
  1631   "product_measure I \<equiv> finite_product_sigma_finite.measure M \<mu> I"
  1632 
  1633 abbreviation (in product_sigma_finite)
  1634   "product_positive_integral I \<equiv>
  1635     measure_space.positive_integral (sigma (product_algebra M I)) (product_measure I)"
  1636 
  1637 abbreviation (in product_sigma_finite)
  1638   "product_integral I \<equiv>
  1639     measure_space.integral (sigma (product_algebra M I)) (product_measure I)"
  1640 
  1641 lemma (in product_sigma_finite) positive_integral_empty:
  1642   "product_positive_integral {} f = f (\<lambda>k. undefined)"
  1643 proof -
  1644   interpret finite_product_sigma_finite M \<mu> "{}" by default (fact finite.emptyI)
  1645   have "\<And>A. measure (Pi\<^isub>E {} A) = 1"
  1646     using assms by (subst measure_times) auto
  1647   then show ?thesis
  1648     unfolding positive_integral_def simple_function_def simple_integral_def_raw
  1649   proof (simp add: P_empty, intro antisym)
  1650     show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> f}. f (\<lambda>k. undefined))"
  1651       by (intro le_SUPI) auto
  1652     show "(SUP f:{g. g \<le> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)"
  1653       by (intro SUP_leI) (auto simp: le_fun_def)
  1654   qed
  1655 qed
  1656 
  1657 lemma (in product_sigma_finite) measure_fold:
  1658   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1659   assumes A: "A \<in> sets (sigma (product_algebra M (I \<union> J)))"
  1660   shows "pair_sigma_finite.pair_measure
  1661      (sigma (product_algebra M I)) (product_measure I)
  1662      (sigma (product_algebra M J)) (product_measure J)
  1663      ((\<lambda>x. ((\<lambda>i\<in>I. x i), (\<lambda>i\<in>J. x i))) ` A) =
  1664    product_measure (I \<union> J) A"
  1665 proof -
  1666   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1667   interpret J: finite_product_sigma_finite M \<mu> J by default fact
  1668   have "finite (I \<union> J)" using fin by auto
  1669   interpret IJ: finite_product_sigma_finite M \<mu> "I \<union> J" by default fact
  1670   interpret P: pair_sigma_finite I.P I.measure J.P J.measure by default
  1671   let ?f = "\<lambda>x. ((\<lambda>i\<in>I. x i), (\<lambda>i\<in>J. x i))"
  1672     from IJ.sigma_finite_pairs obtain F where
  1673       F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
  1674          "(\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) \<up> space IJ.G"
  1675          "\<And>k. \<forall>i\<in>I\<union>J. \<mu> i (F i k) \<noteq> \<omega>"
  1676       by auto
  1677     let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
  1678   have split_f_image[simp]: "\<And>F. ?f ` (Pi\<^isub>E (I \<union> J) F) = (Pi\<^isub>E I F) \<times> (Pi\<^isub>E J F)"
  1679     apply auto apply (rule_tac x="merge I a J b" in image_eqI)
  1680     by (auto dest: extensional_restrict)
  1681     show "P.pair_measure (?f ` A) = IJ.measure A"
  1682   proof (rule measure_unique_Int_stable[OF _ _ _ _ _ _ _ _ A])
  1683       show "Int_stable IJ.G" by (simp add: PiE_Int Int_stable_def product_algebra_def) auto
  1684       show "range ?F \<subseteq> sets IJ.G" using F by (simp add: image_subset_iff product_algebra_def)
  1685       show "?F \<up> space IJ.G " using F(2) by simp
  1686       show "measure_space IJ.P (\<lambda>A. P.pair_measure (?f ` A))"
  1687       apply (subst product_product_vimage_algebra[OF IJ, symmetric])
  1688       apply (intro P.measure_space_isomorphic bij_inv_bij_betw)
  1689       unfolding sigma_pair_algebra_sigma_eq
  1690       by (rule bij_inv_restrict_merge[OF `I \<inter> J = {}`])
  1691       show "measure_space IJ.P IJ.measure" by fact
  1692     next
  1693       fix A assume "A \<in> sets IJ.G"
  1694       then obtain F where A[simp]: "A = Pi\<^isub>E (I \<union> J) F" "F \<in> (\<Pi> i\<in>I \<union> J. sets (M i))"
  1695         by (auto simp: product_algebra_def)
  1696       then have F: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> sets (M i)" "\<And>i. i \<in> J \<Longrightarrow> F i \<in> sets (M i)"
  1697         by auto
  1698       have "P.pair_measure (?f ` A) = (\<Prod>i\<in>I. \<mu> i (F i)) * (\<Prod>i\<in>J. \<mu> i (F i))"
  1699         using `finite J` `finite I` F
  1700         by (simp add: P.pair_measure_times I.measure_times J.measure_times)
  1701       also have "\<dots> = (\<Prod>i\<in>I \<union> J. \<mu> i (F i))"
  1702         using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
  1703       also have "\<dots> = IJ.measure A"
  1704         using `finite J` `finite I` F unfolding A
  1705         by (intro IJ.measure_times[symmetric]) auto
  1706       finally show "P.pair_measure (?f ` A) = IJ.measure A" .
  1707     next
  1708       fix k
  1709       have "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i k \<in> sets (M i)" using F by auto
  1710       then have "P.pair_measure (?f ` ?F k) = (\<Prod>i\<in>I. \<mu> i (F i k)) * (\<Prod>i\<in>J. \<mu> i (F i k))"
  1711         by (simp add: P.pair_measure_times I.measure_times J.measure_times)
  1712       then show "P.pair_measure (?f ` ?F k) \<noteq> \<omega>"
  1713         using `finite I` F by (simp add: setprod_\<omega>)
  1714     qed simp
  1715   qed
  1716 
  1717 lemma (in product_sigma_finite) product_positive_integral_fold:
  1718   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1719   and f: "f \<in> borel_measurable (sigma (product_algebra M (I \<union> J)))"
  1720   shows "product_positive_integral (I \<union> J) f =
  1721     product_positive_integral I (\<lambda>x. product_positive_integral J (\<lambda>y. f (merge I x J y)))"
  1722 proof -
  1723   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1724   interpret J: finite_product_sigma_finite M \<mu> J by default fact
  1725   have "finite (I \<union> J)" using fin by auto
  1726   interpret IJ: finite_product_sigma_finite M \<mu> "I \<union> J" by default fact
  1727   interpret P: pair_sigma_finite I.P I.measure J.P J.measure by default
  1728   let ?f = "\<lambda>x. ((\<lambda>i\<in>I. x i), (\<lambda>i\<in>J. x i))"
  1729   have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable P.P"
  1730     unfolding case_prod_distrib measurable_merge_iff[OF IJ, symmetric] using f .
  1731   have bij: "bij_betw ?f (space IJ.P) (space P.P)"
  1732     unfolding sigma_pair_algebra_sigma_eq
  1733     by (intro bij_inv_bij_betw) (rule bij_inv_restrict_merge[OF IJ])
  1734   have "IJ.positive_integral f =  IJ.positive_integral (\<lambda>x. f (restrict x (I \<union> J)))"
  1735     by (auto intro!: IJ.positive_integral_cong arg_cong[where f=f] dest!: extensional_restrict)
  1736   also have "\<dots> = I.positive_integral (\<lambda>x. J.positive_integral (\<lambda>y. f (merge I x J y)))"
  1737     unfolding P.positive_integral_fst_measurable[OF P_borel, simplified]
  1738     unfolding P.positive_integral_vimage[OF bij]
  1739     unfolding product_product_vimage_algebra[OF IJ]
  1740     apply simp
  1741     apply (rule IJ.positive_integral_cong_measure[symmetric])
  1742     apply (rule measure_fold)
  1743     using assms by auto
  1744   finally show ?thesis .
  1745 qed
  1746 
  1747 lemma (in product_sigma_finite) product_positive_integral_singleton:
  1748   assumes f: "f \<in> borel_measurable (M i)"
  1749   shows "product_positive_integral {i} (\<lambda>x. f (x i)) = M.positive_integral i f"
  1750 proof -
  1751   interpret I: finite_product_sigma_finite M \<mu> "{i}" by default simp
  1752   have bij: "bij_betw (\<lambda>x. \<lambda>j\<in>{i}. x) (space (M i)) (space I.P)"
  1753     by (auto intro!: bij_betwI ext simp: extensional_def)
  1754   have *: "(\<lambda>x. (\<lambda>x. \<lambda>j\<in>{i}. x) -` Pi\<^isub>E {i} x \<inter> space (M i)) ` (\<Pi> i\<in>{i}. sets (M i)) = sets (M i)"
  1755   proof (subst image_cong, rule refl)
  1756     fix x assume "x \<in> (\<Pi> i\<in>{i}. sets (M i))"
  1757     then show "(\<lambda>x. \<lambda>j\<in>{i}. x) -` Pi\<^isub>E {i} x \<inter> space (M i) = x i"
  1758       using sets_into_space by auto
  1759   qed auto
  1760   have vimage: "I.vimage_algebra (space (M i)) (\<lambda>x. \<lambda>j\<in>{i}. x) = M i"
  1761     unfolding I.vimage_algebra_def
  1762     unfolding product_sigma_algebra_def sets_sigma
  1763     apply (subst sigma_sets_vimage[symmetric])
  1764     apply (simp_all add: image_image sigma_sets_eq product_algebra_def * del: vimage_Int)
  1765     using sets_into_space by blast
  1766   show "I.positive_integral (\<lambda>x. f (x i)) = M.positive_integral i f"
  1767     unfolding I.positive_integral_vimage[OF bij]
  1768     unfolding vimage
  1769     apply (subst positive_integral_cong_measure)
  1770   proof -
  1771     fix A assume A: "A \<in> sets (M i)"
  1772     have "(\<lambda>x. \<lambda>j\<in>{i}. x) ` A = (\<Pi>\<^isub>E i\<in>{i}. A)"
  1773       by (auto intro!: image_eqI ext[where 'b='a] simp: extensional_def)
  1774     with A show "product_measure {i} ((\<lambda>x. \<lambda>j\<in>{i}. x) ` A) = \<mu> i A"
  1775       using I.measure_times[of "\<lambda>i. A"] by simp
  1776   qed simp
  1777 qed
  1778 
  1779 lemma (in product_sigma_finite) product_integral_singleton:
  1780   assumes f: "f \<in> borel_measurable (M i)"
  1781   shows "product_integral {i} (\<lambda>x. f (x i)) = M.integral i f"
  1782 proof -
  1783   interpret I: finite_product_sigma_finite M \<mu> "{i}" by default simp
  1784   have *: "(\<lambda>x. Real (f x)) \<in> borel_measurable (M i)"
  1785     "(\<lambda>x. Real (- f x)) \<in> borel_measurable (M i)"
  1786     using assms by auto
  1787   show ?thesis
  1788     unfolding I.integral_def integral_def
  1789     unfolding *[THEN product_positive_integral_singleton] ..
  1790 qed
  1791 
  1792 lemma (in product_sigma_finite) product_integral_fold:
  1793   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1794   and f: "measure_space.integrable (sigma (product_algebra M (I \<union> J))) (product_measure (I \<union> J)) f"
  1795   shows "product_integral (I \<union> J) f =
  1796     product_integral I (\<lambda>x. product_integral J (\<lambda>y. f (merge I x J y)))"
  1797 proof -
  1798   interpret I: finite_product_sigma_finite M \<mu> I by default fact
  1799   interpret J: finite_product_sigma_finite M \<mu> J by default fact
  1800   have "finite (I \<union> J)" using fin by auto
  1801   interpret IJ: finite_product_sigma_finite M \<mu> "I \<union> J" by default fact
  1802   interpret P: pair_sigma_finite I.P I.measure J.P J.measure by default
  1803   let ?f = "\<lambda>(x,y). f (merge I x J y)"
  1804   have f_borel: "f \<in> borel_measurable IJ.P"
  1805      "(\<lambda>x. Real (f x)) \<in> borel_measurable IJ.P"
  1806      "(\<lambda>x. Real (- f x)) \<in> borel_measurable IJ.P"
  1807     using f unfolding integrable_def by auto
  1808   have f_restrict: "(\<lambda>x. f (restrict x (I \<union> J))) \<in> borel_measurable IJ.P"
  1809     by (rule measurable_cong[THEN iffD2, OF _ f_borel(1)])
  1810        (auto intro!: arg_cong[where f=f] simp: extensional_restrict)
  1811   then have f'_borel:
  1812     "(\<lambda>x. Real (?f x)) \<in> borel_measurable P.P"
  1813     "(\<lambda>x. Real (- ?f x)) \<in> borel_measurable P.P"
  1814     unfolding measurable_restrict_iff[OF IJ]
  1815     by simp_all
  1816   have PI:
  1817     "P.positive_integral (\<lambda>x. Real (?f x)) = IJ.positive_integral (\<lambda>x. Real (f x))"
  1818     "P.positive_integral (\<lambda>x. Real (- ?f x)) = IJ.positive_integral (\<lambda>x. Real (- f x))"
  1819     using f'_borel[THEN P.positive_integral_fst_measurable(2)]
  1820     using f_borel(2,3)[THEN product_positive_integral_fold[OF assms(1-3)]]
  1821     by simp_all
  1822   have "P.integrable ?f" using `IJ.integrable f`
  1823     unfolding P.integrable_def IJ.integrable_def
  1824     unfolding measurable_restrict_iff[OF IJ]
  1825     using f_restrict PI by simp_all
  1826   show ?thesis
  1827     unfolding P.integrable_fst_measurable(2)[OF `P.integrable ?f`, simplified]
  1828     unfolding IJ.integral_def P.integral_def
  1829     unfolding PI by simp
  1830 qed
  1831 
  1832 section "Products on finite spaces"
  1833 
  1834 lemma sigma_sets_pair_algebra_finite:
  1835   assumes "finite A" and "finite B"
  1836   shows "sigma_sets (A \<times> B) ((\<lambda>(x,y). x \<times> y) ` (Pow A \<times> Pow B)) = Pow (A \<times> B)"
  1837   (is "sigma_sets ?prod ?sets = _")
  1838 proof safe
  1839   have fin: "finite (A \<times> B)" using assms by (rule finite_cartesian_product)
  1840   fix x assume subset: "x \<subseteq> A \<times> B"
  1841   hence "finite x" using fin by (rule finite_subset)
  1842   from this subset show "x \<in> sigma_sets ?prod ?sets"
  1843   proof (induct x)
  1844     case empty show ?case by (rule sigma_sets.Empty)
  1845   next
  1846     case (insert a x)
  1847     hence "{a} \<in> sigma_sets ?prod ?sets"
  1848       by (auto simp: pair_algebra_def intro!: sigma_sets.Basic)
  1849     moreover have "x \<in> sigma_sets ?prod ?sets" using insert by auto
  1850     ultimately show ?case unfolding insert_is_Un[of a x] by (rule sigma_sets_Un)
  1851   qed
  1852 next
  1853   fix x a b
  1854   assume "x \<in> sigma_sets ?prod ?sets" and "(a, b) \<in> x"
  1855   from sigma_sets_into_sp[OF _ this(1)] this(2)
  1856   show "a \<in> A" and "b \<in> B" by auto
  1857 qed
  1858 
  1859 locale pair_finite_sigma_algebra = M1: finite_sigma_algebra M1 + M2: finite_sigma_algebra M2 for M1 M2
  1860 
  1861 sublocale pair_finite_sigma_algebra \<subseteq> pair_sigma_algebra by default
  1862 
  1863 lemma (in pair_finite_sigma_algebra) finite_pair_sigma_algebra[simp]:
  1864   shows "P = (| space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2) |)"
  1865 proof -
  1866   show ?thesis using M1.finite_space M2.finite_space
  1867     by (simp add: sigma_def space_pair_algebra sets_pair_algebra
  1868                   sigma_sets_pair_algebra_finite M1.sets_eq_Pow M2.sets_eq_Pow)
  1869 qed
  1870 
  1871 sublocale pair_finite_sigma_algebra \<subseteq> finite_sigma_algebra P
  1872 proof
  1873   show "finite (space P)" "sets P = Pow (space P)"
  1874     using M1.finite_space M2.finite_space by auto
  1875 qed
  1876 
  1877 locale pair_finite_space = M1: finite_measure_space M1 \<mu>1 + M2: finite_measure_space M2 \<mu>2
  1878   for M1 \<mu>1 M2 \<mu>2
  1879 
  1880 sublocale pair_finite_space \<subseteq> pair_finite_sigma_algebra
  1881   by default
  1882 
  1883 sublocale pair_finite_space \<subseteq> pair_sigma_finite
  1884   by default
  1885 
  1886 lemma (in pair_finite_space) finite_pair_sigma_algebra[simp]:
  1887   shows "P = (| space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2) |)"
  1888 proof -
  1889   show ?thesis using M1.finite_space M2.finite_space
  1890     by (simp add: sigma_def space_pair_algebra sets_pair_algebra
  1891                   sigma_sets_pair_algebra_finite M1.sets_eq_Pow M2.sets_eq_Pow)
  1892 qed
  1893 
  1894 lemma (in pair_finite_space) pair_measure_Pair[simp]:
  1895   assumes "a \<in> space M1" "b \<in> space M2"
  1896   shows "pair_measure {(a, b)} = \<mu>1 {a} * \<mu>2 {b}"
  1897 proof -
  1898   have "pair_measure ({a}\<times>{b}) = \<mu>1 {a} * \<mu>2 {b}"
  1899     using M1.sets_eq_Pow M2.sets_eq_Pow assms
  1900     by (subst pair_measure_times) auto
  1901   then show ?thesis by simp
  1902 qed
  1903 
  1904 lemma (in pair_finite_space) pair_measure_singleton[simp]:
  1905   assumes "x \<in> space M1 \<times> space M2"
  1906   shows "pair_measure {x} = \<mu>1 {fst x} * \<mu>2 {snd x}"
  1907   using pair_measure_Pair assms by (cases x) auto
  1908 
  1909 sublocale pair_finite_space \<subseteq> finite_measure_space P pair_measure
  1910   by default auto
  1911 
  1912 lemma (in pair_finite_space) finite_measure_space_finite_prod_measure_alterantive:
  1913   "finite_measure_space \<lparr>space = space M1 \<times> space M2, sets = Pow (space M1 \<times> space M2)\<rparr> pair_measure"
  1914   unfolding finite_pair_sigma_algebra[symmetric]
  1915   by default
  1916 
  1917 end