src/HOL/Probability/Finite_Product_Measure.thy
author hoelzl
Fri Nov 16 18:45:57 2012 +0100 (2012-11-16)
changeset 50104 de19856feb54
parent 50099 a58bb401af80
child 50123 69b35a75caf3
permissions -rw-r--r--
move theorems to be more generally useable
     1 (*  Title:      HOL/Probability/Finite_Product_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 header {*Finite product measures*}
     6 
     7 theory Finite_Product_Measure
     8 imports Binary_Product_Measure
     9 begin
    10 
    11 lemma split_const: "(\<lambda>(i, j). c) = (\<lambda>_. c)"
    12   by auto
    13 
    14 abbreviation
    15   "Pi\<^isub>E A B \<equiv> Pi A B \<inter> extensional A"
    16 
    17 syntax
    18   "_PiE"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PIE _:_./ _)" 10)
    19 
    20 syntax (xsymbols)
    21   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
    22 
    23 syntax (HTML output)
    24   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
    25 
    26 translations
    27   "PIE x:A. B" == "CONST Pi\<^isub>E A (%x. B)"
    28 
    29 abbreviation
    30   funcset_extensional :: "['a set, 'b set] => ('a => 'b) set"
    31     (infixr "->\<^isub>E" 60) where
    32   "A ->\<^isub>E B \<equiv> Pi\<^isub>E A (%_. B)"
    33 
    34 notation (xsymbols)
    35   funcset_extensional  (infixr "\<rightarrow>\<^isub>E" 60)
    36 
    37 lemma extensional_insert[intro, simp]:
    38   assumes "a \<in> extensional (insert i I)"
    39   shows "a(i := b) \<in> extensional (insert i I)"
    40   using assms unfolding extensional_def by auto
    41 
    42 lemma extensional_Int[simp]:
    43   "extensional I \<inter> extensional I' = extensional (I \<inter> I')"
    44   unfolding extensional_def by auto
    45 
    46 lemma extensional_UNIV[simp]: "extensional UNIV = UNIV"
    47   by (auto simp: extensional_def)
    48 
    49 lemma restrict_extensional_sub[intro]: "A \<subseteq> B \<Longrightarrow> restrict f A \<in> extensional B"
    50   unfolding restrict_def extensional_def by auto
    51 
    52 lemma restrict_restrict[simp]: "restrict (restrict f A) B = restrict f (A \<inter> B)"
    53   unfolding restrict_def by (simp add: fun_eq_iff)
    54 
    55 definition
    56   "merge I J = (\<lambda>(x, y) i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
    57 
    58 lemma merge_apply[simp]:
    59   "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    60   "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    61   "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    62   "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    63   "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I J (x, y) i = undefined"
    64   unfolding merge_def by auto
    65 
    66 lemma merge_commute:
    67   "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) = merge J I (y, x)"
    68   by (force simp: merge_def)
    69 
    70 lemma Pi_cancel_merge_range[simp]:
    71   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    72   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    73   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    74   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    75   by (auto simp: Pi_def)
    76 
    77 lemma Pi_cancel_merge[simp]:
    78   "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    79   "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    80   "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    81   "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    82   by (auto simp: Pi_def)
    83 
    84 lemma extensional_merge[simp]: "merge I J (x, y) \<in> extensional (I \<union> J)"
    85   by (auto simp: extensional_def)
    86 
    87 lemma restrict_Pi_cancel: "restrict x I \<in> Pi I A \<longleftrightarrow> x \<in> Pi I A"
    88   by (auto simp: restrict_def Pi_def)
    89 
    90 lemma restrict_merge[simp]:
    91   "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    92   "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    93   "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    94   "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    95   by (auto simp: restrict_def)
    96 
    97 lemma split_merge: "P (merge I J (x,y) i) \<longleftrightarrow> (i \<in> I \<longrightarrow> P (x i)) \<and> (i \<in> J - I \<longrightarrow> P (y i)) \<and> (i \<notin> I \<union> J \<longrightarrow> P undefined)"
    98   unfolding merge_def by auto
    99 
   100 lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I J (x, y) \<in> extensional K"
   101   unfolding merge_def extensional_def by auto
   102 
   103 lemma injective_vimage_restrict:
   104   assumes J: "J \<subseteq> I"
   105   and sets: "A \<subseteq> (\<Pi>\<^isub>E i\<in>J. S i)" "B \<subseteq> (\<Pi>\<^isub>E i\<in>J. S i)" and ne: "(\<Pi>\<^isub>E i\<in>I. S i) \<noteq> {}"
   106   and eq: "(\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i) = (\<lambda>x. restrict x J) -` B \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
   107   shows "A = B"
   108 proof  (intro set_eqI)
   109   fix x
   110   from ne obtain y where y: "\<And>i. i \<in> I \<Longrightarrow> y i \<in> S i" by auto
   111   have "J \<inter> (I - J) = {}" by auto
   112   show "x \<in> A \<longleftrightarrow> x \<in> B"
   113   proof cases
   114     assume x: "x \<in> (\<Pi>\<^isub>E i\<in>J. S i)"
   115     have "x \<in> A \<longleftrightarrow> merge J (I - J) (x,y) \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
   116       using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub split: split_merge)
   117     then show "x \<in> A \<longleftrightarrow> x \<in> B"
   118       using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub eq split: split_merge)
   119   next
   120     assume "x \<notin> (\<Pi>\<^isub>E i\<in>J. S i)" with sets show "x \<in> A \<longleftrightarrow> x \<in> B" by auto
   121   qed
   122 qed
   123 
   124 lemma extensional_insert_undefined[intro, simp]:
   125   assumes "a \<in> extensional (insert i I)"
   126   shows "a(i := undefined) \<in> extensional I"
   127   using assms unfolding extensional_def by auto
   128 
   129 lemma extensional_insert_cancel[intro, simp]:
   130   assumes "a \<in> extensional I"
   131   shows "a \<in> extensional (insert i I)"
   132   using assms unfolding extensional_def by auto
   133 
   134 lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I {i} (x,y) = restrict (x(i := y i)) (insert i I)"
   135   unfolding merge_def by (auto simp: fun_eq_iff)
   136 
   137 lemma Pi_Int: "Pi I E \<inter> Pi I F = (\<Pi> i\<in>I. E i \<inter> F i)"
   138   by auto
   139 
   140 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)"
   141   by auto
   142 
   143 lemma Pi_cancel_fupd_range[simp]: "i \<notin> I \<Longrightarrow> x \<in> Pi I (B(i := b)) \<longleftrightarrow> x \<in> Pi I B"
   144   by (auto simp: Pi_def)
   145 
   146 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"
   147   by (auto simp: Pi_def)
   148 
   149 lemma Pi_split_domain[simp]: "x \<in> Pi (I \<union> J) X \<longleftrightarrow> x \<in> Pi I X \<and> x \<in> Pi J X"
   150   by (auto simp: Pi_def)
   151 
   152 lemma Pi_cancel_fupd[simp]: "i \<notin> I \<Longrightarrow> x(i := a) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
   153   by (auto simp: Pi_def)
   154 
   155 lemma restrict_vimage:
   156   assumes "I \<inter> J = {}"
   157   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 J (E, F))"
   158   using assms by (auto simp: restrict_Pi_cancel)
   159 
   160 lemma merge_vimage:
   161   assumes "I \<inter> J = {}"
   162   shows "merge I J -` Pi\<^isub>E (I \<union> J) E = Pi I E \<times> Pi J E"
   163   using assms by (auto simp: restrict_Pi_cancel)
   164 
   165 lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
   166   by (auto simp: restrict_def)
   167 
   168 lemma merge_restrict[simp]:
   169   "merge I J (restrict x I, y) = merge I J (x, y)"
   170   "merge I J (x, restrict y J) = merge I J (x, y)"
   171   unfolding merge_def by auto
   172 
   173 lemma merge_x_x_eq_restrict[simp]:
   174   "merge I J (x, x) = restrict x (I \<union> J)"
   175   unfolding merge_def by auto
   176 
   177 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"
   178   apply auto
   179   apply (drule_tac x=x in Pi_mem)
   180   apply (simp_all split: split_if_asm)
   181   apply (drule_tac x=i in Pi_mem)
   182   apply (auto dest!: Pi_mem)
   183   done
   184 
   185 lemma Pi_UN:
   186   fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
   187   assumes "finite I" and mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
   188   shows "(\<Union>n. Pi I (A n)) = (\<Pi> i\<in>I. \<Union>n. A n i)"
   189 proof (intro set_eqI iffI)
   190   fix f assume "f \<in> (\<Pi> i\<in>I. \<Union>n. A n i)"
   191   then have "\<forall>i\<in>I. \<exists>n. f i \<in> A n i" by auto
   192   from bchoice[OF this] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
   193   obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
   194     using `finite I` finite_nat_set_iff_bounded_le[of "n`I"] by auto
   195   have "f \<in> Pi I (A k)"
   196   proof (intro Pi_I)
   197     fix i assume "i \<in> I"
   198     from mono[OF this, of "n i" k] k[OF this] n[OF this]
   199     show "f i \<in> A k i" by auto
   200   qed
   201   then show "f \<in> (\<Union>n. Pi I (A n))" by auto
   202 qed auto
   203 
   204 lemma PiE_cong:
   205   assumes "\<And>i. i\<in>I \<Longrightarrow> A i = B i"
   206   shows "Pi\<^isub>E I A = Pi\<^isub>E I B"
   207   using assms by (auto intro!: Pi_cong)
   208 
   209 lemma restrict_upd[simp]:
   210   "i \<notin> I \<Longrightarrow> (restrict f I)(i := y) = restrict (f(i := y)) (insert i I)"
   211   by (auto simp: fun_eq_iff)
   212 
   213 lemma Pi_eq_subset:
   214   assumes ne: "\<And>i. i \<in> I \<Longrightarrow> F i \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> F' i \<noteq> {}"
   215   assumes eq: "Pi\<^isub>E I F = Pi\<^isub>E I F'" and "i \<in> I"
   216   shows "F i \<subseteq> F' i"
   217 proof
   218   fix x assume "x \<in> F i"
   219   with ne have "\<forall>j. \<exists>y. ((j \<in> I \<longrightarrow> y \<in> F j \<and> (i = j \<longrightarrow> x = y)) \<and> (j \<notin> I \<longrightarrow> y = undefined))" by auto
   220   from choice[OF this] guess f .. note f = this
   221   then have "f \<in> Pi\<^isub>E I F" by (auto simp: extensional_def)
   222   then have "f \<in> Pi\<^isub>E I F'" using assms by simp
   223   then show "x \<in> F' i" using f `i \<in> I` by auto
   224 qed
   225 
   226 lemma Pi_eq_iff_not_empty:
   227   assumes ne: "\<And>i. i \<in> I \<Longrightarrow> F i \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> F' i \<noteq> {}"
   228   shows "Pi\<^isub>E I F = Pi\<^isub>E I F' \<longleftrightarrow> (\<forall>i\<in>I. F i = F' i)"
   229 proof (intro iffI ballI)
   230   fix i assume eq: "Pi\<^isub>E I F = Pi\<^isub>E I F'" and i: "i \<in> I"
   231   show "F i = F' i"
   232     using Pi_eq_subset[of I F F', OF ne eq i]
   233     using Pi_eq_subset[of I F' F, OF ne(2,1) eq[symmetric] i]
   234     by auto
   235 qed auto
   236 
   237 lemma Pi_eq_empty_iff:
   238   "Pi\<^isub>E I F = {} \<longleftrightarrow> (\<exists>i\<in>I. F i = {})"
   239 proof
   240   assume "Pi\<^isub>E I F = {}"
   241   show "\<exists>i\<in>I. F i = {}"
   242   proof (rule ccontr)
   243     assume "\<not> ?thesis"
   244     then have "\<forall>i. \<exists>y. (i \<in> I \<longrightarrow> y \<in> F i) \<and> (i \<notin> I \<longrightarrow> y = undefined)" by auto
   245     from choice[OF this] guess f ..
   246     then have "f \<in> Pi\<^isub>E I F" by (auto simp: extensional_def)
   247     with `Pi\<^isub>E I F = {}` show False by auto
   248   qed
   249 qed auto
   250 
   251 lemma Pi_eq_iff:
   252   "Pi\<^isub>E I F = Pi\<^isub>E I F' \<longleftrightarrow> (\<forall>i\<in>I. F i = F' i) \<or> ((\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {}))"
   253 proof (intro iffI disjCI)
   254   assume eq[simp]: "Pi\<^isub>E I F = Pi\<^isub>E I F'"
   255   assume "\<not> ((\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {}))"
   256   then have "(\<forall>i\<in>I. F i \<noteq> {}) \<and> (\<forall>i\<in>I. F' i \<noteq> {})"
   257     using Pi_eq_empty_iff[of I F] Pi_eq_empty_iff[of I F'] by auto
   258   with Pi_eq_iff_not_empty[of I F F'] show "\<forall>i\<in>I. F i = F' i" by auto
   259 next
   260   assume "(\<forall>i\<in>I. F i = F' i) \<or> (\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {})"
   261   then show "Pi\<^isub>E I F = Pi\<^isub>E I F'"
   262     using Pi_eq_empty_iff[of I F] Pi_eq_empty_iff[of I F'] by auto
   263 qed
   264 
   265 lemma funset_eq_UN_fun_upd_I:
   266   assumes *: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f(a := d) \<in> F A"
   267   and **: "\<And>f. f \<in> F (insert a A) \<Longrightarrow> f a \<in> G (f(a:=d))"
   268   and ***: "\<And>f x. \<lbrakk> f \<in> F A ; x \<in> G f \<rbrakk> \<Longrightarrow> f(a:=x) \<in> F (insert a A)"
   269   shows "F (insert a A) = (\<Union>f\<in>F A. fun_upd f a ` (G f))"
   270 proof safe
   271   fix f assume f: "f \<in> F (insert a A)"
   272   show "f \<in> (\<Union>f\<in>F A. fun_upd f a ` G f)"
   273   proof (rule UN_I[of "f(a := d)"])
   274     show "f(a := d) \<in> F A" using *[OF f] .
   275     show "f \<in> fun_upd (f(a:=d)) a ` G (f(a:=d))"
   276     proof (rule image_eqI[of _ _ "f a"])
   277       show "f a \<in> G (f(a := d))" using **[OF f] .
   278     qed simp
   279   qed
   280 next
   281   fix f x assume "f \<in> F A" "x \<in> G f"
   282   from ***[OF this] show "f(a := x) \<in> F (insert a A)" .
   283 qed
   284 
   285 lemma extensional_funcset_insert_eq[simp]:
   286   assumes "a \<notin> A"
   287   shows "extensional (insert a A) \<inter> (insert a A \<rightarrow> B) = (\<Union>f \<in> extensional A \<inter> (A \<rightarrow> B). (\<lambda>b. f(a := b)) ` B)"
   288   apply (rule funset_eq_UN_fun_upd_I)
   289   using assms
   290   by (auto intro!: inj_onI dest: inj_onD split: split_if_asm simp: extensional_def)
   291 
   292 lemma finite_extensional_funcset[simp, intro]:
   293   assumes "finite A" "finite B"
   294   shows "finite (extensional A \<inter> (A \<rightarrow> B))"
   295   using assms by induct auto
   296 
   297 lemma finite_PiE[simp, intro]:
   298   assumes fin: "finite A" "\<And>i. i \<in> A \<Longrightarrow> finite (B i)"
   299   shows "finite (Pi\<^isub>E A B)"
   300 proof -
   301   have *: "(Pi\<^isub>E A B) \<subseteq> extensional A \<inter> (A \<rightarrow> (\<Union>i\<in>A. B i))" by auto
   302   show ?thesis
   303     using fin by (intro finite_subset[OF *] finite_extensional_funcset) auto
   304 qed
   305 
   306 section "Finite product spaces"
   307 
   308 section "Products"
   309 
   310 definition prod_emb where
   311   "prod_emb I M K X = (\<lambda>x. restrict x K) -` X \<inter> (PIE i:I. space (M i))"
   312 
   313 lemma prod_emb_iff: 
   314   "f \<in> prod_emb I M K X \<longleftrightarrow> f \<in> extensional I \<and> (restrict f K \<in> X) \<and> (\<forall>i\<in>I. f i \<in> space (M i))"
   315   unfolding prod_emb_def by auto
   316 
   317 lemma
   318   shows prod_emb_empty[simp]: "prod_emb M L K {} = {}"
   319     and prod_emb_Un[simp]: "prod_emb M L K (A \<union> B) = prod_emb M L K A \<union> prod_emb M L K B"
   320     and prod_emb_Int: "prod_emb M L K (A \<inter> B) = prod_emb M L K A \<inter> prod_emb M L K B"
   321     and prod_emb_UN[simp]: "prod_emb M L K (\<Union>i\<in>I. F i) = (\<Union>i\<in>I. prod_emb M L K (F i))"
   322     and prod_emb_INT[simp]: "I \<noteq> {} \<Longrightarrow> prod_emb M L K (\<Inter>i\<in>I. F i) = (\<Inter>i\<in>I. prod_emb M L K (F i))"
   323     and prod_emb_Diff[simp]: "prod_emb M L K (A - B) = prod_emb M L K A - prod_emb M L K B"
   324   by (auto simp: prod_emb_def)
   325 
   326 lemma prod_emb_PiE: "J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow>
   327     prod_emb I M J (\<Pi>\<^isub>E i\<in>J. E i) = (\<Pi>\<^isub>E i\<in>I. if i \<in> J then E i else space (M i))"
   328   by (force simp: prod_emb_def Pi_iff split_if_mem2)
   329 
   330 lemma prod_emb_PiE_same_index[simp]: "(\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow> prod_emb I M I (Pi\<^isub>E I E) = Pi\<^isub>E I E"
   331   by (auto simp: prod_emb_def Pi_iff)
   332 
   333 lemma prod_emb_trans[simp]:
   334   "J \<subseteq> K \<Longrightarrow> K \<subseteq> L \<Longrightarrow> prod_emb L M K (prod_emb K M J X) = prod_emb L M J X"
   335   by (auto simp add: Int_absorb1 prod_emb_def)
   336 
   337 lemma prod_emb_Pi:
   338   assumes "X \<in> (\<Pi> j\<in>J. sets (M j))" "J \<subseteq> K"
   339   shows "prod_emb K M J (Pi\<^isub>E J X) = (\<Pi>\<^isub>E i\<in>K. if i \<in> J then X i else space (M i))"
   340   using assms space_closed
   341   by (auto simp: prod_emb_def Pi_iff split: split_if_asm) blast+
   342 
   343 lemma prod_emb_id:
   344   "B \<subseteq> (\<Pi>\<^isub>E i\<in>L. space (M i)) \<Longrightarrow> prod_emb L M L B = B"
   345   by (auto simp: prod_emb_def Pi_iff subset_eq extensional_restrict)
   346 
   347 lemma prod_emb_mono:
   348   "F \<subseteq> G \<Longrightarrow> prod_emb A M B F \<subseteq> prod_emb A M B G"
   349   by (auto simp: prod_emb_def)
   350 
   351 definition PiM :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) measure" where
   352   "PiM I M = extend_measure (\<Pi>\<^isub>E i\<in>I. space (M i))
   353     {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}
   354     (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^isub>E j\<in>J. X j))
   355     (\<lambda>(J, X). \<Prod>j\<in>J \<union> {i\<in>I. emeasure (M i) (space (M i)) \<noteq> 1}. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j)))"
   356 
   357 definition prod_algebra :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) set set" where
   358   "prod_algebra I M = (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^isub>E j\<in>J. X j)) `
   359     {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   360 
   361 abbreviation
   362   "Pi\<^isub>M I M \<equiv> PiM I M"
   363 
   364 syntax
   365   "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3PIM _:_./ _)" 10)
   366 
   367 syntax (xsymbols)
   368   "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"  10)
   369 
   370 syntax (HTML output)
   371   "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"  10)
   372 
   373 translations
   374   "PIM x:I. M" == "CONST PiM I (%x. M)"
   375 
   376 lemma prod_algebra_sets_into_space:
   377   "prod_algebra I M \<subseteq> Pow (\<Pi>\<^isub>E i\<in>I. space (M i))"
   378   using assms by (auto simp: prod_emb_def prod_algebra_def)
   379 
   380 lemma prod_algebra_eq_finite:
   381   assumes I: "finite I"
   382   shows "prod_algebra I M = {(\<Pi>\<^isub>E i\<in>I. X i) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" (is "?L = ?R")
   383 proof (intro iffI set_eqI)
   384   fix A assume "A \<in> ?L"
   385   then obtain J E where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   386     and A: "A = prod_emb I M J (PIE j:J. E j)"
   387     by (auto simp: prod_algebra_def)
   388   let ?A = "\<Pi>\<^isub>E i\<in>I. if i \<in> J then E i else space (M i)"
   389   have A: "A = ?A"
   390     unfolding A using J by (intro prod_emb_PiE sets_into_space) auto
   391   show "A \<in> ?R" unfolding A using J top
   392     by (intro CollectI exI[of _ "\<lambda>i. if i \<in> J then E i else space (M i)"]) simp
   393 next
   394   fix A assume "A \<in> ?R"
   395   then obtain X where "A = (\<Pi>\<^isub>E i\<in>I. X i)" and X: "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
   396   then have A: "A = prod_emb I M I (\<Pi>\<^isub>E i\<in>I. X i)"
   397     using sets_into_space by (force simp: prod_emb_def Pi_iff)
   398   from X I show "A \<in> ?L" unfolding A
   399     by (auto simp: prod_algebra_def)
   400 qed
   401 
   402 lemma prod_algebraI:
   403   "finite J \<Longrightarrow> (J \<noteq> {} \<or> I = {}) \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i))
   404     \<Longrightarrow> prod_emb I M J (PIE j:J. E j) \<in> prod_algebra I M"
   405   by (auto simp: prod_algebra_def Pi_iff)
   406 
   407 lemma prod_algebraI_finite:
   408   "finite I \<Longrightarrow> (\<forall>i\<in>I. E i \<in> sets (M i)) \<Longrightarrow> (Pi\<^isub>E I E) \<in> prod_algebra I M"
   409   using prod_algebraI[of I I E M] prod_emb_PiE_same_index[of I E M, OF sets_into_space] by simp
   410 
   411 lemma Int_stable_PiE: "Int_stable {Pi\<^isub>E J E | E. \<forall>i\<in>I. E i \<in> sets (M i)}"
   412 proof (safe intro!: Int_stableI)
   413   fix E F assume "\<forall>i\<in>I. E i \<in> sets (M i)" "\<forall>i\<in>I. F i \<in> sets (M i)"
   414   then show "\<exists>G. Pi\<^isub>E J E \<inter> Pi\<^isub>E J F = Pi\<^isub>E J G \<and> (\<forall>i\<in>I. G i \<in> sets (M i))"
   415     by (auto intro!: exI[of _ "\<lambda>i. E i \<inter> F i"])
   416 qed
   417 
   418 lemma prod_algebraE:
   419   assumes A: "A \<in> prod_algebra I M"
   420   obtains J E where "A = prod_emb I M J (PIE j:J. E j)"
   421     "finite J" "J \<noteq> {} \<or> I = {}" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i)" 
   422   using A by (auto simp: prod_algebra_def)
   423 
   424 lemma prod_algebraE_all:
   425   assumes A: "A \<in> prod_algebra I M"
   426   obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   427 proof -
   428   from A obtain E J where A: "A = prod_emb I M J (Pi\<^isub>E J E)"
   429     and J: "J \<subseteq> I" and E: "E \<in> (\<Pi> i\<in>J. sets (M i))"
   430     by (auto simp: prod_algebra_def)
   431   from E have "\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)"
   432     using sets_into_space by auto
   433   then have "A = (\<Pi>\<^isub>E i\<in>I. if i\<in>J then E i else space (M i))"
   434     using A J by (auto simp: prod_emb_PiE)
   435   moreover then have "(\<lambda>i. if i\<in>J then E i else space (M i)) \<in> (\<Pi> i\<in>I. sets (M i))"
   436     using top E by auto
   437   ultimately show ?thesis using that by auto
   438 qed
   439 
   440 lemma Int_stable_prod_algebra: "Int_stable (prod_algebra I M)"
   441 proof (unfold Int_stable_def, safe)
   442   fix A assume "A \<in> prod_algebra I M"
   443   from prod_algebraE[OF this] guess J E . note A = this
   444   fix B assume "B \<in> prod_algebra I M"
   445   from prod_algebraE[OF this] guess K F . note B = this
   446   have "A \<inter> B = prod_emb I M (J \<union> K) (\<Pi>\<^isub>E i\<in>J \<union> K. (if i \<in> J then E i else space (M i)) \<inter> 
   447       (if i \<in> K then F i else space (M i)))"
   448     unfolding A B using A(2,3,4) A(5)[THEN sets_into_space] B(2,3,4) B(5)[THEN sets_into_space]
   449     apply (subst (1 2 3) prod_emb_PiE)
   450     apply (simp_all add: subset_eq PiE_Int)
   451     apply blast
   452     apply (intro PiE_cong)
   453     apply auto
   454     done
   455   also have "\<dots> \<in> prod_algebra I M"
   456     using A B by (auto intro!: prod_algebraI)
   457   finally show "A \<inter> B \<in> prod_algebra I M" .
   458 qed
   459 
   460 lemma prod_algebra_mono:
   461   assumes space: "\<And>i. i \<in> I \<Longrightarrow> space (E i) = space (F i)"
   462   assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (E i) \<subseteq> sets (F i)"
   463   shows "prod_algebra I E \<subseteq> prod_algebra I F"
   464 proof
   465   fix A assume "A \<in> prod_algebra I E"
   466   then obtain J G where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I"
   467     and A: "A = prod_emb I E J (\<Pi>\<^isub>E i\<in>J. G i)"
   468     and G: "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (E i)"
   469     by (auto simp: prod_algebra_def)
   470   moreover
   471   from space have "(\<Pi>\<^isub>E i\<in>I. space (E i)) = (\<Pi>\<^isub>E i\<in>I. space (F i))"
   472     by (rule PiE_cong)
   473   with A have "A = prod_emb I F J (\<Pi>\<^isub>E i\<in>J. G i)"
   474     by (simp add: prod_emb_def)
   475   moreover
   476   from sets G J have "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (F i)"
   477     by auto
   478   ultimately show "A \<in> prod_algebra I F"
   479     apply (simp add: prod_algebra_def image_iff)
   480     apply (intro exI[of _ J] exI[of _ G] conjI)
   481     apply auto
   482     done
   483 qed
   484 
   485 lemma prod_algebra_cong:
   486   assumes "I = J" and sets: "(\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i))"
   487   shows "prod_algebra I M = prod_algebra J N"
   488 proof -
   489   have space: "\<And>i. i \<in> I \<Longrightarrow> space (M i) = space (N i)"
   490     using sets_eq_imp_space_eq[OF sets] by auto
   491   with sets show ?thesis unfolding `I = J`
   492     by (intro antisym prod_algebra_mono) auto
   493 qed
   494 
   495 lemma space_in_prod_algebra:
   496   "(\<Pi>\<^isub>E i\<in>I. space (M i)) \<in> prod_algebra I M"
   497 proof cases
   498   assume "I = {}" then show ?thesis
   499     by (auto simp add: prod_algebra_def image_iff prod_emb_def)
   500 next
   501   assume "I \<noteq> {}"
   502   then obtain i where "i \<in> I" by auto
   503   then have "(\<Pi>\<^isub>E i\<in>I. space (M i)) = prod_emb I M {i} (\<Pi>\<^isub>E i\<in>{i}. space (M i))"
   504     by (auto simp: prod_emb_def Pi_iff)
   505   also have "\<dots> \<in> prod_algebra I M"
   506     using `i \<in> I` by (intro prod_algebraI) auto
   507   finally show ?thesis .
   508 qed
   509 
   510 lemma space_PiM: "space (\<Pi>\<^isub>M i\<in>I. M i) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   511   using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro space_extend_measure) simp
   512 
   513 lemma sets_PiM: "sets (\<Pi>\<^isub>M i\<in>I. M i) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) (prod_algebra I M)"
   514   using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro sets_extend_measure) simp
   515 
   516 lemma sets_PiM_single: "sets (PiM I M) =
   517     sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) {{f\<in>\<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> sets (M i)}"
   518     (is "_ = sigma_sets ?\<Omega> ?R")
   519   unfolding sets_PiM
   520 proof (rule sigma_sets_eqI)
   521   interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
   522   fix A assume "A \<in> prod_algebra I M"
   523   from prod_algebraE[OF this] guess J X . note X = this
   524   show "A \<in> sigma_sets ?\<Omega> ?R"
   525   proof cases
   526     assume "I = {}"
   527     with X have "A = {\<lambda>x. undefined}" by (auto simp: prod_emb_def)
   528     with `I = {}` show ?thesis by (auto intro!: sigma_sets_top)
   529   next
   530     assume "I \<noteq> {}"
   531     with X have "A = (\<Inter>j\<in>J. {f\<in>(\<Pi>\<^isub>E i\<in>I. space (M i)). f j \<in> X j})"
   532       using sets_into_space[OF X(5)]
   533       by (auto simp: prod_emb_PiE[OF _ sets_into_space] Pi_iff split: split_if_asm) blast
   534     also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
   535       using X `I \<noteq> {}` by (intro R.finite_INT sigma_sets.Basic) auto
   536     finally show "A \<in> sigma_sets ?\<Omega> ?R" .
   537   qed
   538 next
   539   fix A assume "A \<in> ?R"
   540   then obtain i B where A: "A = {f\<in>\<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> B}" "i \<in> I" "B \<in> sets (M i)" 
   541     by auto
   542   then have "A = prod_emb I M {i} (\<Pi>\<^isub>E i\<in>{i}. B)"
   543     using sets_into_space[OF A(3)]
   544     apply (subst prod_emb_PiE)
   545     apply (auto simp: Pi_iff split: split_if_asm)
   546     apply blast
   547     done
   548   also have "\<dots> \<in> sigma_sets ?\<Omega> (prod_algebra I M)"
   549     using A by (intro sigma_sets.Basic prod_algebraI) auto
   550   finally show "A \<in> sigma_sets ?\<Omega> (prod_algebra I M)" .
   551 qed
   552 
   553 lemma sets_PiM_I:
   554   assumes "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   555   shows "prod_emb I M J (PIE j:J. E j) \<in> sets (PIM i:I. M i)"
   556 proof cases
   557   assume "J = {}"
   558   then have "prod_emb I M J (PIE j:J. E j) = (PIE j:I. space (M j))"
   559     by (auto simp: prod_emb_def)
   560   then show ?thesis
   561     by (auto simp add: sets_PiM intro!: sigma_sets_top)
   562 next
   563   assume "J \<noteq> {}" with assms show ?thesis
   564     by (force simp add: sets_PiM prod_algebra_def)
   565 qed
   566 
   567 lemma measurable_PiM:
   568   assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   569   assumes sets: "\<And>X J. J \<noteq> {} \<or> I = {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)) \<Longrightarrow>
   570     f -` prod_emb I M J (Pi\<^isub>E J X) \<inter> space N \<in> sets N" 
   571   shows "f \<in> measurable N (PiM I M)"
   572   using sets_PiM prod_algebra_sets_into_space space
   573 proof (rule measurable_sigma_sets)
   574   fix A assume "A \<in> prod_algebra I M"
   575   from prod_algebraE[OF this] guess J X .
   576   with sets[of J X] show "f -` A \<inter> space N \<in> sets N" by auto
   577 qed
   578 
   579 lemma measurable_PiM_Collect:
   580   assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   581   assumes sets: "\<And>X J. J \<noteq> {} \<or> I = {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)) \<Longrightarrow>
   582     {\<omega>\<in>space N. \<forall>i\<in>J. f \<omega> i \<in> X i} \<in> sets N" 
   583   shows "f \<in> measurable N (PiM I M)"
   584   using sets_PiM prod_algebra_sets_into_space space
   585 proof (rule measurable_sigma_sets)
   586   fix A assume "A \<in> prod_algebra I M"
   587   from prod_algebraE[OF this] guess J X . note X = this
   588   have "f -` A \<inter> space N = {\<omega> \<in> space N. \<forall>i\<in>J. f \<omega> i \<in> X i}"
   589     using sets_into_space[OF X(5)] X(2-) space unfolding X(1)
   590     by (subst prod_emb_PiE) (auto simp: Pi_iff split: split_if_asm)
   591   also have "\<dots> \<in> sets N" using X(3,2,4,5) by (rule sets)
   592   finally show "f -` A \<inter> space N \<in> sets N" .
   593 qed
   594 
   595 lemma measurable_PiM_single:
   596   assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   597   assumes sets: "\<And>A i. i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> {\<omega> \<in> space N. f \<omega> i \<in> A} \<in> sets N" 
   598   shows "f \<in> measurable N (PiM I M)"
   599   using sets_PiM_single
   600 proof (rule measurable_sigma_sets)
   601   fix A assume "A \<in> {{f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
   602   then obtain B i where "A = {f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> B}" and B: "i \<in> I" "B \<in> sets (M i)"
   603     by auto
   604   with space have "f -` A \<inter> space N = {\<omega> \<in> space N. f \<omega> i \<in> B}" by auto
   605   also have "\<dots> \<in> sets N" using B by (rule sets)
   606   finally show "f -` A \<inter> space N \<in> sets N" .
   607 qed (auto simp: space)
   608 
   609 lemma measurable_PiM_single':
   610   assumes f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> measurable N (M i)"
   611     and "(\<lambda>\<omega> i. f i \<omega>) \<in> space N \<rightarrow> (\<Pi>\<^isub>E i\<in>I. space (M i))"
   612   shows "(\<lambda>\<omega> i. f i \<omega>) \<in> measurable N (Pi\<^isub>M I M)"
   613 proof (rule measurable_PiM_single)
   614   fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   615   then have "{\<omega> \<in> space N. f i \<omega> \<in> A} = f i -` A \<inter> space N"
   616     by auto
   617   then show "{\<omega> \<in> space N. f i \<omega> \<in> A} \<in> sets N"
   618     using A f by (auto intro!: measurable_sets)
   619 qed fact
   620 
   621 lemma sets_PiM_I_finite[measurable]:
   622   assumes "finite I" and sets: "(\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i))"
   623   shows "(PIE j:I. E j) \<in> sets (PIM i:I. M i)"
   624   using sets_PiM_I[of I I E M] sets_into_space[OF sets] `finite I` sets by auto
   625 
   626 lemma measurable_component_singleton:
   627   assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>M I M) (M i)"
   628 proof (unfold measurable_def, intro CollectI conjI ballI)
   629   fix A assume "A \<in> sets (M i)"
   630   then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) = prod_emb I M {i} (\<Pi>\<^isub>E j\<in>{i}. A)"
   631     using sets_into_space `i \<in> I`
   632     by (fastforce dest: Pi_mem simp: prod_emb_def space_PiM split: split_if_asm)
   633   then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) \<in> sets (Pi\<^isub>M I M)"
   634     using `A \<in> sets (M i)` `i \<in> I` by (auto intro!: sets_PiM_I)
   635 qed (insert `i \<in> I`, auto simp: space_PiM)
   636 
   637 lemma measurable_component_singleton'[measurable_app]:
   638   assumes f: "f \<in> measurable N (Pi\<^isub>M I M)"
   639   assumes i: "i \<in> I"
   640   shows "(\<lambda>x. (f x) i) \<in> measurable N (M i)"
   641   using measurable_compose[OF f measurable_component_singleton, OF i] .
   642 
   643 lemma measurable_PiM_component_rev[measurable (raw)]:
   644   "i \<in> I \<Longrightarrow> f \<in> measurable (M i) N \<Longrightarrow> (\<lambda>x. f (x i)) \<in> measurable (PiM I M) N"
   645   by simp
   646 
   647 lemma measurable_nat_case[measurable (raw)]:
   648   assumes [measurable (raw)]: "i = 0 \<Longrightarrow> f \<in> measurable M N"
   649     "\<And>j. i = Suc j \<Longrightarrow> (\<lambda>x. g x j) \<in> measurable M N"
   650   shows "(\<lambda>x. nat_case (f x) (g x) i) \<in> measurable M N"
   651   by (cases i) simp_all
   652 
   653 lemma measurable_nat_case'[measurable (raw)]:
   654   assumes fg[measurable]: "f \<in> measurable N M" "g \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)"
   655   shows "(\<lambda>x. nat_case (f x) (g x)) \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)"
   656   using fg[THEN measurable_space]
   657   by (auto intro!: measurable_PiM_single' simp add: space_PiM Pi_iff split: nat.split)
   658 
   659 lemma measurable_add_dim[measurable]:
   660   "(\<lambda>(f, y). f(i := y)) \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M)"
   661     (is "?f \<in> measurable ?P ?I")
   662 proof (rule measurable_PiM_single)
   663   fix j A assume j: "j \<in> insert i I" and A: "A \<in> sets (M j)"
   664   have "{\<omega> \<in> space ?P. (\<lambda>(f, x). fun_upd f i x) \<omega> j \<in> A} =
   665     (if j = i then space (Pi\<^isub>M I M) \<times> A else ((\<lambda>x. x j) \<circ> fst) -` A \<inter> space ?P)"
   666     using sets_into_space[OF A] by (auto simp add: space_pair_measure space_PiM)
   667   also have "\<dots> \<in> sets ?P"
   668     using A j
   669     by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   670   finally show "{\<omega> \<in> space ?P. prod_case (\<lambda>f. fun_upd f i) \<omega> j \<in> A} \<in> sets ?P" .
   671 qed (auto simp: space_pair_measure space_PiM)
   672 
   673 lemma measurable_component_update:
   674   "x \<in> space (Pi\<^isub>M I M) \<Longrightarrow> i \<notin> I \<Longrightarrow> (\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)"
   675   by simp
   676 
   677 lemma measurable_merge[measurable]:
   678   "merge I J \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M)"
   679     (is "?f \<in> measurable ?P ?U")
   680 proof (rule measurable_PiM_single)
   681   fix i A assume A: "A \<in> sets (M i)" "i \<in> I \<union> J"
   682   then have "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} =
   683     (if i \<in> I then ((\<lambda>x. x i) \<circ> fst) -` A \<inter> space ?P else ((\<lambda>x. x i) \<circ> snd) -` A \<inter> space ?P)"
   684     by (auto simp: merge_def)
   685   also have "\<dots> \<in> sets ?P"
   686     using A
   687     by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   688   finally show "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} \<in> sets ?P" .
   689 qed (auto simp: space_pair_measure space_PiM Pi_iff merge_def extensional_def)
   690 
   691 lemma measurable_restrict[measurable (raw)]:
   692   assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable N (M i)"
   693   shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable N (Pi\<^isub>M I M)"
   694 proof (rule measurable_PiM_single)
   695   fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   696   then have "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} = X i -` A \<inter> space N"
   697     by auto
   698   then show "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} \<in> sets N"
   699     using A X by (auto intro!: measurable_sets)
   700 qed (insert X, auto dest: measurable_space)
   701 
   702 lemma measurable_restrict_subset: "J \<subseteq> L \<Longrightarrow> (\<lambda>f. restrict f J) \<in> measurable (Pi\<^isub>M L M) (Pi\<^isub>M J M)"
   703   by (intro measurable_restrict measurable_component_singleton) auto
   704 
   705 lemma measurable_prod_emb[intro, simp]:
   706   "J \<subseteq> L \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> prod_emb L M J X \<in> sets (Pi\<^isub>M L M)"
   707   unfolding prod_emb_def space_PiM[symmetric]
   708   by (auto intro!: measurable_sets measurable_restrict measurable_component_singleton)
   709 
   710 lemma sets_in_Pi_aux:
   711   "finite I \<Longrightarrow> (\<And>j. j \<in> I \<Longrightarrow> {x\<in>space (M j). x \<in> F j} \<in> sets (M j)) \<Longrightarrow>
   712   {x\<in>space (PiM I M). x \<in> Pi I F} \<in> sets (PiM I M)"
   713   by (simp add: subset_eq Pi_iff)
   714 
   715 lemma sets_in_Pi[measurable (raw)]:
   716   "finite I \<Longrightarrow> f \<in> measurable N (PiM I M) \<Longrightarrow>
   717   (\<And>j. j \<in> I \<Longrightarrow> {x\<in>space (M j). x \<in> F j} \<in> sets (M j)) \<Longrightarrow>
   718   Sigma_Algebra.pred N (\<lambda>x. f x \<in> Pi I F)"
   719   unfolding pred_def
   720   by (rule measurable_sets_Collect[of f N "PiM I M", OF _ sets_in_Pi_aux]) auto
   721 
   722 lemma sets_in_extensional_aux:
   723   "{x\<in>space (PiM I M). x \<in> extensional I} \<in> sets (PiM I M)"
   724 proof -
   725   have "{x\<in>space (PiM I M). x \<in> extensional I} = space (PiM I M)"
   726     by (auto simp add: extensional_def space_PiM)
   727   then show ?thesis by simp
   728 qed
   729 
   730 lemma sets_in_extensional[measurable (raw)]:
   731   "f \<in> measurable N (PiM I M) \<Longrightarrow> Sigma_Algebra.pred N (\<lambda>x. f x \<in> extensional I)"
   732   unfolding pred_def
   733   by (rule measurable_sets_Collect[of f N "PiM I M", OF _ sets_in_extensional_aux]) auto
   734 
   735 locale product_sigma_finite =
   736   fixes M :: "'i \<Rightarrow> 'a measure"
   737   assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
   738 
   739 sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" for i
   740   by (rule sigma_finite_measures)
   741 
   742 locale finite_product_sigma_finite = product_sigma_finite M for M :: "'i \<Rightarrow> 'a measure" +
   743   fixes I :: "'i set"
   744   assumes finite_index: "finite I"
   745 
   746 lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   747   "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
   748     (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
   749     (\<forall>k. \<forall>i\<in>I. emeasure (M i) (F i k) \<noteq> \<infinity>) \<and> incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k) \<and>
   750     (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space (PiM I M)"
   751 proof -
   752   have "\<forall>i::'i. \<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> sets (M i) \<and> incseq F \<and> (\<Union>i. F i) = space (M i) \<and> (\<forall>k. emeasure (M i) (F k) \<noteq> \<infinity>)"
   753     using M.sigma_finite_incseq by metis
   754   from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   755   then have F: "\<And>i. range (F i) \<subseteq> sets (M i)" "\<And>i. incseq (F i)" "\<And>i. (\<Union>j. F i j) = space (M i)" "\<And>i k. emeasure (M i) (F i k) \<noteq> \<infinity>"
   756     by auto
   757   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
   758   note space_PiM[simp]
   759   show ?thesis
   760   proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
   761     fix i show "range (F i) \<subseteq> sets (M i)" by fact
   762   next
   763     fix i k show "emeasure (M i) (F i k) \<noteq> \<infinity>" by fact
   764   next
   765     fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space (PiM I M)"
   766       using `\<And>i. range (F i) \<subseteq> sets (M i)` sets_into_space
   767       by auto blast
   768   next
   769     fix f assume "f \<in> space (PiM I M)"
   770     with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
   771     show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def)
   772   next
   773     fix i show "?F i \<subseteq> ?F (Suc i)"
   774       using `\<And>i. incseq (F i)`[THEN incseq_SucD] by auto
   775   qed
   776 qed
   777 
   778 lemma
   779   shows space_PiM_empty: "space (Pi\<^isub>M {} M) = {\<lambda>k. undefined}"
   780     and sets_PiM_empty: "sets (Pi\<^isub>M {} M) = { {}, {\<lambda>k. undefined} }"
   781   by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
   782 
   783 lemma emeasure_PiM_empty[simp]: "emeasure (PiM {} M) {\<lambda>_. undefined} = 1"
   784 proof -
   785   let ?\<mu> = "\<lambda>A. if A = {} then 0 else (1::ereal)"
   786   have "emeasure (Pi\<^isub>M {} M) (prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = 1"
   787   proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   788     show "positive (PiM {} M) ?\<mu>"
   789       by (auto simp: positive_def)
   790     show "countably_additive (PiM {} M) ?\<mu>"
   791       by (rule countably_additiveI_finite)
   792          (auto simp: additive_def positive_def sets_PiM_empty space_PiM_empty intro!: )
   793   qed (auto simp: prod_emb_def)
   794   also have "(prod_emb {} M {} (\<Pi>\<^isub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
   795     by (auto simp: prod_emb_def)
   796   finally show ?thesis
   797     by simp
   798 qed
   799 
   800 lemma PiM_empty: "PiM {} M = count_space {\<lambda>_. undefined}"
   801   by (rule measure_eqI) (auto simp add: sets_PiM_empty one_ereal_def)
   802 
   803 lemma (in product_sigma_finite) emeasure_PiM:
   804   "finite I \<Longrightarrow> (\<And>i. i\<in>I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (PiM I M) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   805 proof (induct I arbitrary: A rule: finite_induct)
   806   case (insert i I)
   807   interpret finite_product_sigma_finite M I by default fact
   808   have "finite (insert i I)" using `finite I` by auto
   809   interpret I': finite_product_sigma_finite M "insert i I" by default fact
   810   let ?h = "(\<lambda>(f, y). f(i := y))"
   811 
   812   let ?P = "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) (Pi\<^isub>M (insert i I) M) ?h"
   813   let ?\<mu> = "emeasure ?P"
   814   let ?I = "{j \<in> insert i I. emeasure (M j) (space (M j)) \<noteq> 1}"
   815   let ?f = "\<lambda>J E j. if j \<in> J then emeasure (M j) (E j) else emeasure (M j) (space (M j))"
   816 
   817   have "emeasure (Pi\<^isub>M (insert i I) M) (prod_emb (insert i I) M (insert i I) (Pi\<^isub>E (insert i I) A)) =
   818     (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
   819   proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   820     fix J E assume "(J \<noteq> {} \<or> insert i I = {}) \<and> finite J \<and> J \<subseteq> insert i I \<and> E \<in> (\<Pi> j\<in>J. sets (M j))"
   821     then have J: "J \<noteq> {}" "finite J" "J \<subseteq> insert i I" and E: "\<forall>j\<in>J. E j \<in> sets (M j)" by auto
   822     let ?p = "prod_emb (insert i I) M J (Pi\<^isub>E J E)"
   823     let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^isub>E j\<in>J-{i}. E j)"
   824     have "?\<mu> ?p =
   825       emeasure (Pi\<^isub>M I M \<Otimes>\<^isub>M (M i)) (?h -` ?p \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i))"
   826       by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
   827     also have "?h -` ?p \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = ?p' \<times> (if i \<in> J then E i else space (M i))"
   828       using J E[rule_format, THEN sets_into_space]
   829       by (force simp: space_pair_measure space_PiM Pi_iff prod_emb_iff split: split_if_asm)
   830     also have "emeasure (Pi\<^isub>M I M \<Otimes>\<^isub>M (M i)) (?p' \<times> (if i \<in> J then E i else space (M i))) =
   831       emeasure (Pi\<^isub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
   832       using J E by (intro M.emeasure_pair_measure_Times sets_PiM_I) auto
   833     also have "?p' = (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
   834       using J E[rule_format, THEN sets_into_space]
   835       by (auto simp: prod_emb_iff Pi_iff split: split_if_asm) blast+
   836     also have "emeasure (Pi\<^isub>M I M) (\<Pi>\<^isub>E j\<in>I. if j \<in> J-{i} then E j else space (M j)) =
   837       (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
   838       using E by (subst insert) (auto intro!: setprod_cong)
   839     also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
   840        emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
   841       using insert by (auto simp: mult_commute intro!: arg_cong2[where f="op *"] setprod_cong)
   842     also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
   843       using insert(1,2) J E by (intro setprod_mono_one_right) auto
   844     finally show "?\<mu> ?p = \<dots>" .
   845 
   846     show "prod_emb (insert i I) M J (Pi\<^isub>E J E) \<in> Pow (\<Pi>\<^isub>E i\<in>insert i I. space (M i))"
   847       using J E[rule_format, THEN sets_into_space] by (auto simp: prod_emb_iff)
   848   next
   849     show "positive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^isub>M (insert i I) M)) ?\<mu>"
   850       using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
   851   next
   852     show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
   853       insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
   854       using insert by auto
   855   qed (auto intro!: setprod_cong)
   856   with insert show ?case
   857     by (subst (asm) prod_emb_PiE_same_index) (auto intro!: sets_into_space)
   858 qed simp
   859 
   860 lemma (in product_sigma_finite) sigma_finite: 
   861   assumes "finite I"
   862   shows "sigma_finite_measure (PiM I M)"
   863 proof -
   864   interpret finite_product_sigma_finite M I by default fact
   865 
   866   from sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   867   then have F: "\<And>j. j \<in> I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   868     "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> I. F j k)"
   869     "(\<Union>k. \<Pi>\<^isub>E j \<in> I. F j k) = space (Pi\<^isub>M I M)"
   870     "\<And>k. \<And>j. j \<in> I \<Longrightarrow> emeasure (M j) (F j k) \<noteq> \<infinity>"
   871     by blast+
   872   let ?F = "\<lambda>k. \<Pi>\<^isub>E j \<in> I. F j k"
   873 
   874   show ?thesis
   875   proof (unfold_locales, intro exI[of _ ?F] conjI allI)
   876     show "range ?F \<subseteq> sets (Pi\<^isub>M I M)" using F(1) `finite I` by auto
   877   next
   878     from F(3) show "(\<Union>i. ?F i) = space (Pi\<^isub>M I M)" by simp
   879   next
   880     fix j
   881     from F `finite I` setprod_PInf[of I, OF emeasure_nonneg, of M]
   882     show "emeasure (\<Pi>\<^isub>M i\<in>I. M i) (?F j) \<noteq> \<infinity>"
   883       by (subst emeasure_PiM) auto
   884   qed
   885 qed
   886 
   887 sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure "Pi\<^isub>M I M"
   888   using sigma_finite[OF finite_index] .
   889 
   890 lemma (in finite_product_sigma_finite) measure_times:
   891   "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (Pi\<^isub>M I M) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   892   using emeasure_PiM[OF finite_index] by auto
   893 
   894 lemma (in product_sigma_finite) positive_integral_empty:
   895   assumes pos: "0 \<le> f (\<lambda>k. undefined)"
   896   shows "integral\<^isup>P (Pi\<^isub>M {} M) f = f (\<lambda>k. undefined)"
   897 proof -
   898   interpret finite_product_sigma_finite M "{}" by default (fact finite.emptyI)
   899   have "\<And>A. emeasure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
   900     using assms by (subst measure_times) auto
   901   then show ?thesis
   902     unfolding positive_integral_def simple_function_def simple_integral_def[abs_def]
   903   proof (simp add: space_PiM_empty sets_PiM_empty, intro antisym)
   904     show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined))"
   905       by (intro SUP_upper) (auto simp: le_fun_def split: split_max)
   906     show "(SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)" using pos
   907       by (intro SUP_least) (auto simp: le_fun_def simp: max_def split: split_if_asm)
   908   qed
   909 qed
   910 
   911 lemma (in product_sigma_finite) distr_merge:
   912   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   913   shows "distr (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M) (merge I J) = Pi\<^isub>M (I \<union> J) M"
   914    (is "?D = ?P")
   915 proof -
   916   interpret I: finite_product_sigma_finite M I by default fact
   917   interpret J: finite_product_sigma_finite M J by default fact
   918   have "finite (I \<union> J)" using fin by auto
   919   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   920   interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   921   let ?g = "merge I J"
   922 
   923   from IJ.sigma_finite_pairs obtain F where
   924     F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
   925        "incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k)"
   926        "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space ?P"
   927        "\<And>k. \<forall>i\<in>I\<union>J. emeasure (M i) (F i k) \<noteq> \<infinity>"
   928     by auto
   929   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
   930   
   931   show ?thesis
   932   proof (rule measure_eqI_generator_eq[symmetric])
   933     show "Int_stable (prod_algebra (I \<union> J) M)"
   934       by (rule Int_stable_prod_algebra)
   935     show "prod_algebra (I \<union> J) M \<subseteq> Pow (\<Pi>\<^isub>E i \<in> I \<union> J. space (M i))"
   936       by (rule prod_algebra_sets_into_space)
   937     show "sets ?P = sigma_sets (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) (prod_algebra (I \<union> J) M)"
   938       by (rule sets_PiM)
   939     then show "sets ?D = sigma_sets (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i)) (prod_algebra (I \<union> J) M)"
   940       by simp
   941 
   942     show "range ?F \<subseteq> prod_algebra (I \<union> J) M" using F
   943       using fin by (auto simp: prod_algebra_eq_finite)
   944     show "(\<Union>i. \<Pi>\<^isub>E ia\<in>I \<union> J. F ia i) = (\<Pi>\<^isub>E i\<in>I \<union> J. space (M i))"
   945       using F(3) by (simp add: space_PiM)
   946   next
   947     fix k
   948     from F `finite I` setprod_PInf[of "I \<union> J", OF emeasure_nonneg, of M]
   949     show "emeasure ?P (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
   950   next
   951     fix A assume A: "A \<in> prod_algebra (I \<union> J) M"
   952     with fin obtain F where A_eq: "A = (Pi\<^isub>E (I \<union> J) F)" and F: "\<forall>i\<in>J. F i \<in> sets (M i)" "\<forall>i\<in>I. F i \<in> sets (M i)"
   953       by (auto simp add: prod_algebra_eq_finite)
   954     let ?B = "Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M"
   955     let ?X = "?g -` A \<inter> space ?B"
   956     have "Pi\<^isub>E I F \<subseteq> space (Pi\<^isub>M I M)" "Pi\<^isub>E J F \<subseteq> space (Pi\<^isub>M J M)"
   957       using F[rule_format, THEN sets_into_space] by (force simp: space_PiM)+
   958     then have X: "?X = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
   959       unfolding A_eq by (subst merge_vimage) (auto simp: space_pair_measure space_PiM)
   960     have "emeasure ?D A = emeasure ?B ?X"
   961       using A by (intro emeasure_distr measurable_merge) (auto simp: sets_PiM)
   962     also have "emeasure ?B ?X = (\<Prod>i\<in>I. emeasure (M i) (F i)) * (\<Prod>i\<in>J. emeasure (M i) (F i))"
   963       using `finite J` `finite I` F unfolding X
   964       by (simp add: J.emeasure_pair_measure_Times I.measure_times J.measure_times Pi_iff)
   965     also have "\<dots> = (\<Prod>i\<in>I \<union> J. emeasure (M i) (F i))"
   966       using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
   967     also have "\<dots> = emeasure ?P (Pi\<^isub>E (I \<union> J) F)"
   968       using `finite J` `finite I` F unfolding A
   969       by (intro IJ.measure_times[symmetric]) auto
   970     finally show "emeasure ?P A = emeasure ?D A" using A_eq by simp
   971   qed
   972 qed
   973 
   974 lemma (in product_sigma_finite) product_positive_integral_fold:
   975   assumes IJ: "I \<inter> J = {}" "finite I" "finite J"
   976   and f: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   977   shows "integral\<^isup>P (Pi\<^isub>M (I \<union> J) M) f =
   978     (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I J (x, y)) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
   979 proof -
   980   interpret I: finite_product_sigma_finite M I by default fact
   981   interpret J: finite_product_sigma_finite M J by default fact
   982   interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   983   have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
   984     using measurable_comp[OF measurable_merge f] by (simp add: comp_def)
   985   show ?thesis
   986     apply (subst distr_merge[OF IJ, symmetric])
   987     apply (subst positive_integral_distr[OF measurable_merge f])
   988     apply (subst J.positive_integral_fst_measurable(2)[symmetric, OF P_borel])
   989     apply simp
   990     done
   991 qed
   992 
   993 lemma (in product_sigma_finite) distr_singleton:
   994   "distr (Pi\<^isub>M {i} M) (M i) (\<lambda>x. x i) = M i" (is "?D = _")
   995 proof (intro measure_eqI[symmetric])
   996   interpret I: finite_product_sigma_finite M "{i}" by default simp
   997   fix A assume A: "A \<in> sets (M i)"
   998   moreover then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M {i} M) = (\<Pi>\<^isub>E i\<in>{i}. A)"
   999     using sets_into_space by (auto simp: space_PiM)
  1000   ultimately show "emeasure (M i) A = emeasure ?D A"
  1001     using A I.measure_times[of "\<lambda>_. A"]
  1002     by (simp add: emeasure_distr measurable_component_singleton)
  1003 qed simp
  1004 
  1005 lemma (in product_sigma_finite) product_positive_integral_singleton:
  1006   assumes f: "f \<in> borel_measurable (M i)"
  1007   shows "integral\<^isup>P (Pi\<^isub>M {i} M) (\<lambda>x. f (x i)) = integral\<^isup>P (M i) f"
  1008 proof -
  1009   interpret I: finite_product_sigma_finite M "{i}" by default simp
  1010   from f show ?thesis
  1011     apply (subst distr_singleton[symmetric])
  1012     apply (subst positive_integral_distr[OF measurable_component_singleton])
  1013     apply simp_all
  1014     done
  1015 qed
  1016 
  1017 lemma (in product_sigma_finite) product_positive_integral_insert:
  1018   assumes I[simp]: "finite I" "i \<notin> I"
  1019     and f: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
  1020   shows "integral\<^isup>P (Pi\<^isub>M (insert i I) M) f = (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (x(i := y)) \<partial>(M i)) \<partial>(Pi\<^isub>M I M))"
  1021 proof -
  1022   interpret I: finite_product_sigma_finite M I by default auto
  1023   interpret i: finite_product_sigma_finite M "{i}" by default auto
  1024   have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
  1025     using f by auto
  1026   show ?thesis
  1027     unfolding product_positive_integral_fold[OF IJ, unfolded insert, OF I(1) i.finite_index f]
  1028   proof (rule positive_integral_cong, subst product_positive_integral_singleton[symmetric])
  1029     fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
  1030     let ?f = "\<lambda>y. f (x(i := y))"
  1031     show "?f \<in> borel_measurable (M i)"
  1032       using measurable_comp[OF measurable_component_update f, OF x `i \<notin> I`]
  1033       unfolding comp_def .
  1034     show "(\<integral>\<^isup>+ y. f (merge I {i} (x, y)) \<partial>Pi\<^isub>M {i} M) = (\<integral>\<^isup>+ y. f (x(i := y i)) \<partial>Pi\<^isub>M {i} M)"
  1035       using x
  1036       by (auto intro!: positive_integral_cong arg_cong[where f=f]
  1037                simp add: space_PiM extensional_def)
  1038   qed
  1039 qed
  1040 
  1041 lemma (in product_sigma_finite) product_positive_integral_setprod:
  1042   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> ereal"
  1043   assumes "finite I" and borel: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  1044   and pos: "\<And>i x. i \<in> I \<Longrightarrow> 0 \<le> f i x"
  1045   shows "(\<integral>\<^isup>+ x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^isub>M I M) = (\<Prod>i\<in>I. integral\<^isup>P (M i) (f i))"
  1046 using assms proof induct
  1047   case (insert i I)
  1048   note `finite I`[intro, simp]
  1049   interpret I: finite_product_sigma_finite M I by default auto
  1050   have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  1051     using insert by (auto intro!: setprod_cong)
  1052   have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow> (\<lambda>x. (\<Prod>i\<in>J. f i (x i))) \<in> borel_measurable (Pi\<^isub>M J M)"
  1053     using sets_into_space insert
  1054     by (intro borel_measurable_ereal_setprod
  1055               measurable_comp[OF measurable_component_singleton, unfolded comp_def])
  1056        auto
  1057   then show ?case
  1058     apply (simp add: product_positive_integral_insert[OF insert(1,2) prod])
  1059     apply (simp add: insert(2-) * pos borel setprod_ereal_pos positive_integral_multc)
  1060     apply (subst positive_integral_cmult)
  1061     apply (auto simp add: pos borel insert(2-) setprod_ereal_pos positive_integral_positive)
  1062     done
  1063 qed (simp add: space_PiM)
  1064 
  1065 lemma (in product_sigma_finite) product_integral_singleton:
  1066   assumes f: "f \<in> borel_measurable (M i)"
  1067   shows "(\<integral>x. f (x i) \<partial>Pi\<^isub>M {i} M) = integral\<^isup>L (M i) f"
  1068 proof -
  1069   interpret I: finite_product_sigma_finite M "{i}" by default simp
  1070   have *: "(\<lambda>x. ereal (f x)) \<in> borel_measurable (M i)"
  1071     "(\<lambda>x. ereal (- f x)) \<in> borel_measurable (M i)"
  1072     using assms by auto
  1073   show ?thesis
  1074     unfolding lebesgue_integral_def *[THEN product_positive_integral_singleton] ..
  1075 qed
  1076 lemma (in product_sigma_finite) distr_component:
  1077   "distr (M i) (Pi\<^isub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^isub>M {i} M" (is "?D = ?P")
  1078 proof (intro measure_eqI[symmetric])
  1079   interpret I: finite_product_sigma_finite M "{i}" by default simp
  1080 
  1081   have eq: "\<And>x. x \<in> extensional {i} \<Longrightarrow> (\<lambda>j\<in>{i}. x i) = x"
  1082     by (auto simp: extensional_def restrict_def)
  1083 
  1084   fix A assume A: "A \<in> sets ?P"
  1085   then have "emeasure ?P A = (\<integral>\<^isup>+x. indicator A x \<partial>?P)" 
  1086     by simp
  1087   also have "\<dots> = (\<integral>\<^isup>+x. indicator ((\<lambda>x. \<lambda>i\<in>{i}. x) -` A \<inter> space (M i)) (x i) \<partial>PiM {i} M)" 
  1088     by (intro positive_integral_cong) (auto simp: space_PiM indicator_def simp: eq)
  1089   also have "\<dots> = emeasure ?D A"
  1090     using A by (simp add: product_positive_integral_singleton emeasure_distr)
  1091   finally show "emeasure (Pi\<^isub>M {i} M) A = emeasure ?D A" .
  1092 qed simp
  1093 
  1094 lemma (in product_sigma_finite) product_integral_fold:
  1095   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1096   and f: "integrable (Pi\<^isub>M (I \<union> J) M) f"
  1097   shows "integral\<^isup>L (Pi\<^isub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I J (x, y)) \<partial>Pi\<^isub>M J M) \<partial>Pi\<^isub>M I M)"
  1098 proof -
  1099   interpret I: finite_product_sigma_finite M I by default fact
  1100   interpret J: finite_product_sigma_finite M J by default fact
  1101   have "finite (I \<union> J)" using fin by auto
  1102   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
  1103   interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
  1104   let ?M = "merge I J"
  1105   let ?f = "\<lambda>x. f (?M x)"
  1106   from f have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
  1107     by auto
  1108   have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
  1109     using measurable_comp[OF measurable_merge f_borel] by (simp add: comp_def)
  1110   have f_int: "integrable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) ?f"
  1111     by (rule integrable_distr[OF measurable_merge]) (simp add: distr_merge[OF IJ fin] f)
  1112   show ?thesis
  1113     apply (subst distr_merge[symmetric, OF IJ fin])
  1114     apply (subst integral_distr[OF measurable_merge f_borel])
  1115     apply (subst P.integrable_fst_measurable(2)[symmetric, OF f_int])
  1116     apply simp
  1117     done
  1118 qed
  1119 
  1120 lemma (in product_sigma_finite)
  1121   assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
  1122   shows emeasure_fold_integral:
  1123     "emeasure (Pi\<^isub>M (I \<union> J) M) A = (\<integral>\<^isup>+x. emeasure (Pi\<^isub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^isub>M J M)) \<partial>Pi\<^isub>M I M)" (is ?I)
  1124     and emeasure_fold_measurable:
  1125     "(\<lambda>x. emeasure (Pi\<^isub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^isub>M J M))) \<in> borel_measurable (Pi\<^isub>M I M)" (is ?B)
  1126 proof -
  1127   interpret I: finite_product_sigma_finite M I by default fact
  1128   interpret J: finite_product_sigma_finite M J by default fact
  1129   interpret IJ: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" ..
  1130   have merge: "merge I J -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) \<in> sets (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M)"
  1131     by (intro measurable_sets[OF _ A] measurable_merge assms)
  1132 
  1133   show ?I
  1134     apply (subst distr_merge[symmetric, OF IJ])
  1135     apply (subst emeasure_distr[OF measurable_merge A])
  1136     apply (subst J.emeasure_pair_measure_alt[OF merge])
  1137     apply (auto intro!: positive_integral_cong arg_cong2[where f=emeasure] simp: space_pair_measure)
  1138     done
  1139 
  1140   show ?B
  1141     using IJ.measurable_emeasure_Pair1[OF merge]
  1142     by (simp add: vimage_compose[symmetric] comp_def space_pair_measure cong: measurable_cong)
  1143 qed
  1144 
  1145 lemma (in product_sigma_finite) product_integral_insert:
  1146   assumes I: "finite I" "i \<notin> I"
  1147     and f: "integrable (Pi\<^isub>M (insert i I) M) f"
  1148   shows "integral\<^isup>L (Pi\<^isub>M (insert i I) M) f = (\<integral>x. (\<integral>y. f (x(i:=y)) \<partial>M i) \<partial>Pi\<^isub>M I M)"
  1149 proof -
  1150   have "integral\<^isup>L (Pi\<^isub>M (insert i I) M) f = integral\<^isup>L (Pi\<^isub>M (I \<union> {i}) M) f"
  1151     by simp
  1152   also have "\<dots> = (\<integral>x. (\<integral>y. f (merge I {i} (x,y)) \<partial>Pi\<^isub>M {i} M) \<partial>Pi\<^isub>M I M)"
  1153     using f I by (intro product_integral_fold) auto
  1154   also have "\<dots> = (\<integral>x. (\<integral>y. f (x(i := y)) \<partial>M i) \<partial>Pi\<^isub>M I M)"
  1155   proof (rule integral_cong, subst product_integral_singleton[symmetric])
  1156     fix x assume x: "x \<in> space (Pi\<^isub>M I M)"
  1157     have f_borel: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
  1158       using f by auto
  1159     show "(\<lambda>y. f (x(i := y))) \<in> borel_measurable (M i)"
  1160       using measurable_comp[OF measurable_component_update f_borel, OF x `i \<notin> I`]
  1161       unfolding comp_def .
  1162     from x I show "(\<integral> y. f (merge I {i} (x,y)) \<partial>Pi\<^isub>M {i} M) = (\<integral> xa. f (x(i := xa i)) \<partial>Pi\<^isub>M {i} M)"
  1163       by (auto intro!: integral_cong arg_cong[where f=f] simp: merge_def space_PiM extensional_def)
  1164   qed
  1165   finally show ?thesis .
  1166 qed
  1167 
  1168 lemma (in product_sigma_finite) product_integrable_setprod:
  1169   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
  1170   assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  1171   shows "integrable (Pi\<^isub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
  1172 proof -
  1173   interpret finite_product_sigma_finite M I by default fact
  1174   have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  1175     using integrable unfolding integrable_def by auto
  1176   have borel: "?f \<in> borel_measurable (Pi\<^isub>M I M)"
  1177     using measurable_comp[OF measurable_component_singleton[of _ I M] f] by (auto simp: comp_def)
  1178   moreover have "integrable (Pi\<^isub>M I M) (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
  1179   proof (unfold integrable_def, intro conjI)
  1180     show "(\<lambda>x. abs (?f x)) \<in> borel_measurable (Pi\<^isub>M I M)"
  1181       using borel by auto
  1182     have "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>Pi\<^isub>M I M) = (\<integral>\<^isup>+x. (\<Prod>i\<in>I. ereal (abs (f i (x i)))) \<partial>Pi\<^isub>M I M)"
  1183       by (simp add: setprod_ereal abs_setprod)
  1184     also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^isup>+x. ereal (abs (f i x)) \<partial>M i))"
  1185       using f by (subst product_positive_integral_setprod) auto
  1186     also have "\<dots> < \<infinity>"
  1187       using integrable[THEN integrable_abs]
  1188       by (simp add: setprod_PInf integrable_def positive_integral_positive)
  1189     finally show "(\<integral>\<^isup>+x. ereal (abs (?f x)) \<partial>(Pi\<^isub>M I M)) \<noteq> \<infinity>" by auto
  1190     have "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^isub>M I M)) = (\<integral>\<^isup>+x. 0 \<partial>(Pi\<^isub>M I M))"
  1191       by (intro positive_integral_cong_pos) auto
  1192     then show "(\<integral>\<^isup>+x. ereal (- abs (?f x)) \<partial>(Pi\<^isub>M I M)) \<noteq> \<infinity>" by simp
  1193   qed
  1194   ultimately show ?thesis
  1195     by (rule integrable_abs_iff[THEN iffD1])
  1196 qed
  1197 
  1198 lemma (in product_sigma_finite) product_integral_setprod:
  1199   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
  1200   assumes "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
  1201   shows "(\<integral>x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^isub>M I M) = (\<Prod>i\<in>I. integral\<^isup>L (M i) (f i))"
  1202 using assms proof induct
  1203   case empty
  1204   interpret finite_measure "Pi\<^isub>M {} M"
  1205     by rule (simp add: space_PiM)
  1206   show ?case by (simp add: space_PiM measure_def)
  1207 next
  1208   case (insert i I)
  1209   then have iI: "finite (insert i I)" by auto
  1210   then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  1211     integrable (Pi\<^isub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  1212     by (intro product_integrable_setprod insert(4)) (auto intro: finite_subset)
  1213   interpret I: finite_product_sigma_finite M I by default fact
  1214   have *: "\<And>x y. (\<Prod>j\<in>I. f j (if j = i then y else x j)) = (\<Prod>j\<in>I. f j (x j))"
  1215     using `i \<notin> I` by (auto intro!: setprod_cong)
  1216   show ?case
  1217     unfolding product_integral_insert[OF insert(1,2) prod[OF subset_refl]]
  1218     by (simp add: * insert integral_multc integral_cmult[OF prod] subset_insertI)
  1219 qed
  1220 
  1221 lemma sets_Collect_single:
  1222   "i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> { x \<in> space (Pi\<^isub>M I M). x i \<in> A } \<in> sets (Pi\<^isub>M I M)"
  1223   by simp
  1224 
  1225 lemma sigma_prod_algebra_sigma_eq_infinite:
  1226   fixes E :: "'i \<Rightarrow> 'a set set"
  1227   assumes S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
  1228     and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
  1229   assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
  1230     and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
  1231   defines "P == {{f\<in>\<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> E i}"
  1232   shows "sets (PiM I M) = sigma_sets (space (PiM I M)) P"
  1233 proof
  1234   let ?P = "sigma (space (Pi\<^isub>M I M)) P"
  1235   have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>M I M))"
  1236     using E_closed by (auto simp: space_PiM P_def Pi_iff subset_eq)
  1237   then have space_P: "space ?P = (\<Pi>\<^isub>E i\<in>I. space (M i))"
  1238     by (simp add: space_PiM)
  1239   have "sets (PiM I M) =
  1240       sigma_sets (space ?P) {{f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
  1241     using sets_PiM_single[of I M] by (simp add: space_P)
  1242   also have "\<dots> \<subseteq> sets (sigma (space (PiM I M)) P)"
  1243   proof (safe intro!: sigma_sets_subset)
  1244     fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
  1245     then have "(\<lambda>x. x i) \<in> measurable ?P (sigma (space (M i)) (E i))"
  1246       apply (subst measurable_iff_measure_of)
  1247       apply (simp_all add: P_closed)
  1248       using E_closed
  1249       apply (force simp: subset_eq space_PiM)
  1250       apply (force simp: subset_eq space_PiM)
  1251       apply (auto simp: P_def intro!: sigma_sets.Basic exI[of _ i])
  1252       apply (rule_tac x=Aa in exI)
  1253       apply (auto simp: space_PiM)
  1254       done
  1255     from measurable_sets[OF this, of A] A `i \<in> I` E_closed
  1256     have "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  1257       by (simp add: E_generates)
  1258     also have "(\<lambda>x. x i) -` A \<inter> space ?P = {f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A}"
  1259       using P_closed by (auto simp: space_PiM)
  1260     finally show "\<dots> \<in> sets ?P" .
  1261   qed
  1262   finally show "sets (PiM I M) \<subseteq> sigma_sets (space (PiM I M)) P"
  1263     by (simp add: P_closed)
  1264   show "sigma_sets (space (PiM I M)) P \<subseteq> sets (PiM I M)"
  1265     unfolding P_def space_PiM[symmetric]
  1266     by (intro sigma_sets_subset) (auto simp: E_generates sets_Collect_single)
  1267 qed
  1268 
  1269 lemma sigma_prod_algebra_sigma_eq:
  1270   fixes E :: "'i \<Rightarrow> 'a set set" and S :: "'i \<Rightarrow> nat \<Rightarrow> 'a set"
  1271   assumes "finite I"
  1272   assumes S_union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (M i)"
  1273     and S_in_E: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> E i"
  1274   assumes E_closed: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (space (M i))"
  1275     and E_generates: "\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sigma_sets (space (M i)) (E i)"
  1276   defines "P == { Pi\<^isub>E I F | F. \<forall>i\<in>I. F i \<in> E i }"
  1277   shows "sets (PiM I M) = sigma_sets (space (PiM I M)) P"
  1278 proof
  1279   let ?P = "sigma (space (Pi\<^isub>M I M)) P"
  1280   from `finite I`[THEN ex_bij_betw_finite_nat] guess T ..
  1281   then have T: "\<And>i. i \<in> I \<Longrightarrow> T i < card I" "\<And>i. i\<in>I \<Longrightarrow> the_inv_into I T (T i) = i"
  1282     by (auto simp add: bij_betw_def set_eq_iff image_iff the_inv_into_f_f)
  1283   have P_closed: "P \<subseteq> Pow (space (Pi\<^isub>M I M))"
  1284     using E_closed by (auto simp: space_PiM P_def Pi_iff subset_eq)
  1285   then have space_P: "space ?P = (\<Pi>\<^isub>E i\<in>I. space (M i))"
  1286     by (simp add: space_PiM)
  1287   have "sets (PiM I M) =
  1288       sigma_sets (space ?P) {{f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
  1289     using sets_PiM_single[of I M] by (simp add: space_P)
  1290   also have "\<dots> \<subseteq> sets (sigma (space (PiM I M)) P)"
  1291   proof (safe intro!: sigma_sets_subset)
  1292     fix i A assume "i \<in> I" and A: "A \<in> sets (M i)"
  1293     have "(\<lambda>x. x i) \<in> measurable ?P (sigma (space (M i)) (E i))"
  1294     proof (subst measurable_iff_measure_of)
  1295       show "E i \<subseteq> Pow (space (M i))" using `i \<in> I` by fact
  1296       from space_P `i \<in> I` show "(\<lambda>x. x i) \<in> space ?P \<rightarrow> space (M i)"
  1297         by (auto simp: Pi_iff)
  1298       show "\<forall>A\<in>E i. (\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  1299       proof
  1300         fix A assume A: "A \<in> E i"
  1301         then have "(\<lambda>x. x i) -` A \<inter> space ?P = (\<Pi>\<^isub>E j\<in>I. if i = j then A else space (M j))"
  1302           using E_closed `i \<in> I` by (auto simp: space_P Pi_iff subset_eq split: split_if_asm)
  1303         also have "\<dots> = (\<Pi>\<^isub>E j\<in>I. \<Union>n. if i = j then A else S j n)"
  1304           by (intro PiE_cong) (simp add: S_union)
  1305         also have "\<dots> = (\<Union>xs\<in>{xs. length xs = card I}. \<Pi>\<^isub>E j\<in>I. if i = j then A else S j (xs ! T j))"
  1306           using T
  1307           apply (auto simp: Pi_iff bchoice_iff)
  1308           apply (rule_tac x="map (\<lambda>n. f (the_inv_into I T n)) [0..<card I]" in exI)
  1309           apply (auto simp: bij_betw_def)
  1310           done
  1311         also have "\<dots> \<in> sets ?P"
  1312         proof (safe intro!: countable_UN)
  1313           fix xs show "(\<Pi>\<^isub>E j\<in>I. if i = j then A else S j (xs ! T j)) \<in> sets ?P"
  1314             using A S_in_E
  1315             by (simp add: P_closed)
  1316                (auto simp: P_def subset_eq intro!: exI[of _ "\<lambda>j. if i = j then A else S j (xs ! T j)"])
  1317         qed
  1318         finally show "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  1319           using P_closed by simp
  1320       qed
  1321     qed
  1322     from measurable_sets[OF this, of A] A `i \<in> I` E_closed
  1323     have "(\<lambda>x. x i) -` A \<inter> space ?P \<in> sets ?P"
  1324       by (simp add: E_generates)
  1325     also have "(\<lambda>x. x i) -` A \<inter> space ?P = {f \<in> \<Pi>\<^isub>E i\<in>I. space (M i). f i \<in> A}"
  1326       using P_closed by (auto simp: space_PiM)
  1327     finally show "\<dots> \<in> sets ?P" .
  1328   qed
  1329   finally show "sets (PiM I M) \<subseteq> sigma_sets (space (PiM I M)) P"
  1330     by (simp add: P_closed)
  1331   show "sigma_sets (space (PiM I M)) P \<subseteq> sets (PiM I M)"
  1332     using `finite I`
  1333     by (auto intro!: sigma_sets_subset sets_PiM_I_finite simp: E_generates P_def)
  1334 qed
  1335 
  1336 lemma pair_measure_eq_distr_PiM:
  1337   fixes M1 :: "'a measure" and M2 :: "'a measure"
  1338   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
  1339   shows "(M1 \<Otimes>\<^isub>M M2) = distr (Pi\<^isub>M UNIV (bool_case M1 M2)) (M1 \<Otimes>\<^isub>M M2) (\<lambda>x. (x True, x False))"
  1340     (is "?P = ?D")
  1341 proof (rule pair_measure_eqI[OF assms])
  1342   interpret B: product_sigma_finite "bool_case M1 M2"
  1343     unfolding product_sigma_finite_def using assms by (auto split: bool.split)
  1344   let ?B = "Pi\<^isub>M UNIV (bool_case M1 M2)"
  1345 
  1346   have [simp]: "fst \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x True)" "snd \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x False)"
  1347     by auto
  1348   fix A B assume A: "A \<in> sets M1" and B: "B \<in> sets M2"
  1349   have "emeasure M1 A * emeasure M2 B = (\<Prod> i\<in>UNIV. emeasure (bool_case M1 M2 i) (bool_case A B i))"
  1350     by (simp add: UNIV_bool ac_simps)
  1351   also have "\<dots> = emeasure ?B (Pi\<^isub>E UNIV (bool_case A B))"
  1352     using A B by (subst B.emeasure_PiM) (auto split: bool.split)
  1353   also have "Pi\<^isub>E UNIV (bool_case A B) = (\<lambda>x. (x True, x False)) -` (A \<times> B) \<inter> space ?B"
  1354     using A[THEN sets_into_space] B[THEN sets_into_space]
  1355     by (auto simp: Pi_iff all_bool_eq space_PiM split: bool.split)
  1356   finally show "emeasure M1 A * emeasure M2 B = emeasure ?D (A \<times> B)"
  1357     using A B
  1358       measurable_component_singleton[of True UNIV "bool_case M1 M2"]
  1359       measurable_component_singleton[of False UNIV "bool_case M1 M2"]
  1360     by (subst emeasure_distr) (auto simp: measurable_pair_iff)
  1361 qed simp
  1362 
  1363 end