src/HOL/Multivariate_Analysis/Finite_Product_Measure.thy
changeset 63627 6ddb43c6b711
parent 63626 44ce6b524ff3
child 63631 2edc8da89edc
child 63633 2accfb71e33b
     1.1 --- a/src/HOL/Multivariate_Analysis/Finite_Product_Measure.thy	Fri Aug 05 18:34:57 2016 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,1199 +0,0 @@
     1.4 -(*  Title:      HOL/Probability/Finite_Product_Measure.thy
     1.5 -    Author:     Johannes Hölzl, TU München
     1.6 -*)
     1.7 -
     1.8 -section \<open>Finite product measures\<close>
     1.9 -
    1.10 -theory Finite_Product_Measure
    1.11 -imports Binary_Product_Measure
    1.12 -begin
    1.13 -
    1.14 -lemma PiE_choice: "(\<exists>f\<in>PiE I F. \<forall>i\<in>I. P i (f i)) \<longleftrightarrow> (\<forall>i\<in>I. \<exists>x\<in>F i. P i x)"
    1.15 -  by (auto simp: Bex_def PiE_iff Ball_def dest!: choice_iff'[THEN iffD1])
    1.16 -     (force intro: exI[of _ "restrict f I" for f])
    1.17 -
    1.18 -lemma case_prod_const: "(\<lambda>(i, j). c) = (\<lambda>_. c)"
    1.19 -  by auto
    1.20 -
    1.21 -subsubsection \<open>More about Function restricted by @{const extensional}\<close>
    1.22 -
    1.23 -definition
    1.24 -  "merge I J = (\<lambda>(x, y) i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
    1.25 -
    1.26 -lemma merge_apply[simp]:
    1.27 -  "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    1.28 -  "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    1.29 -  "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    1.30 -  "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    1.31 -  "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I J (x, y) i = undefined"
    1.32 -  unfolding merge_def by auto
    1.33 -
    1.34 -lemma merge_commute:
    1.35 -  "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) = merge J I (y, x)"
    1.36 -  by (force simp: merge_def)
    1.37 -
    1.38 -lemma Pi_cancel_merge_range[simp]:
    1.39 -  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    1.40 -  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    1.41 -  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    1.42 -  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    1.43 -  by (auto simp: Pi_def)
    1.44 -
    1.45 -lemma Pi_cancel_merge[simp]:
    1.46 -  "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    1.47 -  "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    1.48 -  "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    1.49 -  "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    1.50 -  by (auto simp: Pi_def)
    1.51 -
    1.52 -lemma extensional_merge[simp]: "merge I J (x, y) \<in> extensional (I \<union> J)"
    1.53 -  by (auto simp: extensional_def)
    1.54 -
    1.55 -lemma restrict_merge[simp]:
    1.56 -  "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    1.57 -  "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    1.58 -  "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    1.59 -  "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    1.60 -  by (auto simp: restrict_def)
    1.61 -
    1.62 -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)"
    1.63 -  unfolding merge_def by auto
    1.64 -
    1.65 -lemma PiE_cancel_merge[simp]:
    1.66 -  "I \<inter> J = {} \<Longrightarrow>
    1.67 -    merge I J (x, y) \<in> PiE (I \<union> J) B \<longleftrightarrow> x \<in> Pi I B \<and> y \<in> Pi J B"
    1.68 -  by (auto simp: PiE_def restrict_Pi_cancel)
    1.69 -
    1.70 -lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I {i} (x,y) = restrict (x(i := y i)) (insert i I)"
    1.71 -  unfolding merge_def by (auto simp: fun_eq_iff)
    1.72 -
    1.73 -lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I J (x, y) \<in> extensional K"
    1.74 -  unfolding merge_def extensional_def by auto
    1.75 -
    1.76 -lemma merge_restrict[simp]:
    1.77 -  "merge I J (restrict x I, y) = merge I J (x, y)"
    1.78 -  "merge I J (x, restrict y J) = merge I J (x, y)"
    1.79 -  unfolding merge_def by auto
    1.80 -
    1.81 -lemma merge_x_x_eq_restrict[simp]:
    1.82 -  "merge I J (x, x) = restrict x (I \<union> J)"
    1.83 -  unfolding merge_def by auto
    1.84 -
    1.85 -lemma injective_vimage_restrict:
    1.86 -  assumes J: "J \<subseteq> I"
    1.87 -  and sets: "A \<subseteq> (\<Pi>\<^sub>E i\<in>J. S i)" "B \<subseteq> (\<Pi>\<^sub>E i\<in>J. S i)" and ne: "(\<Pi>\<^sub>E i\<in>I. S i) \<noteq> {}"
    1.88 -  and eq: "(\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^sub>E i\<in>I. S i) = (\<lambda>x. restrict x J) -` B \<inter> (\<Pi>\<^sub>E i\<in>I. S i)"
    1.89 -  shows "A = B"
    1.90 -proof  (intro set_eqI)
    1.91 -  fix x
    1.92 -  from ne obtain y where y: "\<And>i. i \<in> I \<Longrightarrow> y i \<in> S i" by auto
    1.93 -  have "J \<inter> (I - J) = {}" by auto
    1.94 -  show "x \<in> A \<longleftrightarrow> x \<in> B"
    1.95 -  proof cases
    1.96 -    assume x: "x \<in> (\<Pi>\<^sub>E i\<in>J. S i)"
    1.97 -    have "x \<in> A \<longleftrightarrow> merge J (I - J) (x,y) \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^sub>E i\<in>I. S i)"
    1.98 -      using y x \<open>J \<subseteq> I\<close> PiE_cancel_merge[of "J" "I - J" x y S]
    1.99 -      by (auto simp del: PiE_cancel_merge simp add: Un_absorb1)
   1.100 -    then show "x \<in> A \<longleftrightarrow> x \<in> B"
   1.101 -      using y x \<open>J \<subseteq> I\<close> PiE_cancel_merge[of "J" "I - J" x y S]
   1.102 -      by (auto simp del: PiE_cancel_merge simp add: Un_absorb1 eq)
   1.103 -  qed (insert sets, auto)
   1.104 -qed
   1.105 -
   1.106 -lemma restrict_vimage:
   1.107 -  "I \<inter> J = {} \<Longrightarrow>
   1.108 -    (\<lambda>x. (restrict x I, restrict x J)) -` (Pi\<^sub>E I E \<times> Pi\<^sub>E J F) = Pi (I \<union> J) (merge I J (E, F))"
   1.109 -  by (auto simp: restrict_Pi_cancel PiE_def)
   1.110 -
   1.111 -lemma merge_vimage:
   1.112 -  "I \<inter> J = {} \<Longrightarrow> merge I J -` Pi\<^sub>E (I \<union> J) E = Pi I E \<times> Pi J E"
   1.113 -  by (auto simp: restrict_Pi_cancel PiE_def)
   1.114 -
   1.115 -subsection \<open>Finite product spaces\<close>
   1.116 -
   1.117 -subsubsection \<open>Products\<close>
   1.118 -
   1.119 -definition prod_emb where
   1.120 -  "prod_emb I M K X = (\<lambda>x. restrict x K) -` X \<inter> (PIE i:I. space (M i))"
   1.121 -
   1.122 -lemma prod_emb_iff:
   1.123 -  "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))"
   1.124 -  unfolding prod_emb_def PiE_def by auto
   1.125 -
   1.126 -lemma
   1.127 -  shows prod_emb_empty[simp]: "prod_emb M L K {} = {}"
   1.128 -    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"
   1.129 -    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"
   1.130 -    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))"
   1.131 -    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))"
   1.132 -    and prod_emb_Diff[simp]: "prod_emb M L K (A - B) = prod_emb M L K A - prod_emb M L K B"
   1.133 -  by (auto simp: prod_emb_def)
   1.134 -
   1.135 -lemma prod_emb_PiE: "J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow>
   1.136 -    prod_emb I M J (\<Pi>\<^sub>E i\<in>J. E i) = (\<Pi>\<^sub>E i\<in>I. if i \<in> J then E i else space (M i))"
   1.137 -  by (force simp: prod_emb_def PiE_iff if_split_mem2)
   1.138 -
   1.139 -lemma prod_emb_PiE_same_index[simp]:
   1.140 -    "(\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow> prod_emb I M I (Pi\<^sub>E I E) = Pi\<^sub>E I E"
   1.141 -  by (auto simp: prod_emb_def PiE_iff)
   1.142 -
   1.143 -lemma prod_emb_trans[simp]:
   1.144 -  "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"
   1.145 -  by (auto simp add: Int_absorb1 prod_emb_def PiE_def)
   1.146 -
   1.147 -lemma prod_emb_Pi:
   1.148 -  assumes "X \<in> (\<Pi> j\<in>J. sets (M j))" "J \<subseteq> K"
   1.149 -  shows "prod_emb K M J (Pi\<^sub>E J X) = (\<Pi>\<^sub>E i\<in>K. if i \<in> J then X i else space (M i))"
   1.150 -  using assms sets.space_closed
   1.151 -  by (auto simp: prod_emb_def PiE_iff split: if_split_asm) blast+
   1.152 -
   1.153 -lemma prod_emb_id:
   1.154 -  "B \<subseteq> (\<Pi>\<^sub>E i\<in>L. space (M i)) \<Longrightarrow> prod_emb L M L B = B"
   1.155 -  by (auto simp: prod_emb_def subset_eq extensional_restrict)
   1.156 -
   1.157 -lemma prod_emb_mono:
   1.158 -  "F \<subseteq> G \<Longrightarrow> prod_emb A M B F \<subseteq> prod_emb A M B G"
   1.159 -  by (auto simp: prod_emb_def)
   1.160 -
   1.161 -definition PiM :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) measure" where
   1.162 -  "PiM I M = extend_measure (\<Pi>\<^sub>E i\<in>I. space (M i))
   1.163 -    {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}
   1.164 -    (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^sub>E j\<in>J. X j))
   1.165 -    (\<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)))"
   1.166 -
   1.167 -definition prod_algebra :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) set set" where
   1.168 -  "prod_algebra I M = (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^sub>E j\<in>J. X j)) `
   1.169 -    {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   1.170 -
   1.171 -abbreviation
   1.172 -  "Pi\<^sub>M I M \<equiv> PiM I M"
   1.173 -
   1.174 -syntax
   1.175 -  "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^sub>M _\<in>_./ _)"  10)
   1.176 -translations
   1.177 -  "\<Pi>\<^sub>M x\<in>I. M" == "CONST PiM I (%x. M)"
   1.178 -
   1.179 -lemma extend_measure_cong:
   1.180 -  assumes "\<Omega> = \<Omega>'" "I = I'" "G = G'" "\<And>i. i \<in> I' \<Longrightarrow> \<mu> i = \<mu>' i"
   1.181 -  shows "extend_measure \<Omega> I G \<mu> = extend_measure \<Omega>' I' G' \<mu>'"
   1.182 -  unfolding extend_measure_def by (auto simp add: assms)
   1.183 -
   1.184 -lemma Pi_cong_sets:
   1.185 -    "\<lbrakk>I = J; \<And>x. x \<in> I \<Longrightarrow> M x = N x\<rbrakk> \<Longrightarrow> Pi I M = Pi J N"
   1.186 -  unfolding Pi_def by auto
   1.187 -
   1.188 -lemma PiM_cong:
   1.189 -  assumes "I = J" "\<And>x. x \<in> I \<Longrightarrow> M x = N x"
   1.190 -  shows "PiM I M = PiM J N"
   1.191 -  unfolding PiM_def
   1.192 -proof (rule extend_measure_cong, goal_cases)
   1.193 -  case 1
   1.194 -  show ?case using assms
   1.195 -    by (subst assms(1), intro PiE_cong[of J "\<lambda>i. space (M i)" "\<lambda>i. space (N i)"]) simp_all
   1.196 -next
   1.197 -  case 2
   1.198 -  have "\<And>K. K \<subseteq> J \<Longrightarrow> (\<Pi> j\<in>K. sets (M j)) = (\<Pi> j\<in>K. sets (N j))"
   1.199 -    using assms by (intro Pi_cong_sets) auto
   1.200 -  thus ?case by (auto simp: assms)
   1.201 -next
   1.202 -  case 3
   1.203 -  show ?case using assms
   1.204 -    by (intro ext) (auto simp: prod_emb_def dest: PiE_mem)
   1.205 -next
   1.206 -  case (4 x)
   1.207 -  thus ?case using assms
   1.208 -    by (auto intro!: setprod.cong split: if_split_asm)
   1.209 -qed
   1.210 -
   1.211 -
   1.212 -lemma prod_algebra_sets_into_space:
   1.213 -  "prod_algebra I M \<subseteq> Pow (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.214 -  by (auto simp: prod_emb_def prod_algebra_def)
   1.215 -
   1.216 -lemma prod_algebra_eq_finite:
   1.217 -  assumes I: "finite I"
   1.218 -  shows "prod_algebra I M = {(\<Pi>\<^sub>E i\<in>I. X i) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" (is "?L = ?R")
   1.219 -proof (intro iffI set_eqI)
   1.220 -  fix A assume "A \<in> ?L"
   1.221 -  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)"
   1.222 -    and A: "A = prod_emb I M J (PIE j:J. E j)"
   1.223 -    by (auto simp: prod_algebra_def)
   1.224 -  let ?A = "\<Pi>\<^sub>E i\<in>I. if i \<in> J then E i else space (M i)"
   1.225 -  have A: "A = ?A"
   1.226 -    unfolding A using J by (intro prod_emb_PiE sets.sets_into_space) auto
   1.227 -  show "A \<in> ?R" unfolding A using J sets.top
   1.228 -    by (intro CollectI exI[of _ "\<lambda>i. if i \<in> J then E i else space (M i)"]) simp
   1.229 -next
   1.230 -  fix A assume "A \<in> ?R"
   1.231 -  then obtain X where A: "A = (\<Pi>\<^sub>E i\<in>I. X i)" and X: "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
   1.232 -  then have A: "A = prod_emb I M I (\<Pi>\<^sub>E i\<in>I. X i)"
   1.233 -    by (simp add: prod_emb_PiE_same_index[OF sets.sets_into_space] Pi_iff)
   1.234 -  from X I show "A \<in> ?L" unfolding A
   1.235 -    by (auto simp: prod_algebra_def)
   1.236 -qed
   1.237 -
   1.238 -lemma prod_algebraI:
   1.239 -  "finite J \<Longrightarrow> (J \<noteq> {} \<or> I = {}) \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i))
   1.240 -    \<Longrightarrow> prod_emb I M J (PIE j:J. E j) \<in> prod_algebra I M"
   1.241 -  by (auto simp: prod_algebra_def)
   1.242 -
   1.243 -lemma prod_algebraI_finite:
   1.244 -  "finite I \<Longrightarrow> (\<forall>i\<in>I. E i \<in> sets (M i)) \<Longrightarrow> (Pi\<^sub>E I E) \<in> prod_algebra I M"
   1.245 -  using prod_algebraI[of I I E M] prod_emb_PiE_same_index[of I E M, OF sets.sets_into_space] by simp
   1.246 -
   1.247 -lemma Int_stable_PiE: "Int_stable {Pi\<^sub>E J E | E. \<forall>i\<in>I. E i \<in> sets (M i)}"
   1.248 -proof (safe intro!: Int_stableI)
   1.249 -  fix E F assume "\<forall>i\<in>I. E i \<in> sets (M i)" "\<forall>i\<in>I. F i \<in> sets (M i)"
   1.250 -  then show "\<exists>G. Pi\<^sub>E J E \<inter> Pi\<^sub>E J F = Pi\<^sub>E J G \<and> (\<forall>i\<in>I. G i \<in> sets (M i))"
   1.251 -    by (auto intro!: exI[of _ "\<lambda>i. E i \<inter> F i"] simp: PiE_Int)
   1.252 -qed
   1.253 -
   1.254 -lemma prod_algebraE:
   1.255 -  assumes A: "A \<in> prod_algebra I M"
   1.256 -  obtains J E where "A = prod_emb I M J (PIE j:J. E j)"
   1.257 -    "finite J" "J \<noteq> {} \<or> I = {}" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i)"
   1.258 -  using A by (auto simp: prod_algebra_def)
   1.259 -
   1.260 -lemma prod_algebraE_all:
   1.261 -  assumes A: "A \<in> prod_algebra I M"
   1.262 -  obtains E where "A = Pi\<^sub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   1.263 -proof -
   1.264 -  from A obtain E J where A: "A = prod_emb I M J (Pi\<^sub>E J E)"
   1.265 -    and J: "J \<subseteq> I" and E: "E \<in> (\<Pi> i\<in>J. sets (M i))"
   1.266 -    by (auto simp: prod_algebra_def)
   1.267 -  from E have "\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)"
   1.268 -    using sets.sets_into_space by auto
   1.269 -  then have "A = (\<Pi>\<^sub>E i\<in>I. if i\<in>J then E i else space (M i))"
   1.270 -    using A J by (auto simp: prod_emb_PiE)
   1.271 -  moreover have "(\<lambda>i. if i\<in>J then E i else space (M i)) \<in> (\<Pi> i\<in>I. sets (M i))"
   1.272 -    using sets.top E by auto
   1.273 -  ultimately show ?thesis using that by auto
   1.274 -qed
   1.275 -
   1.276 -lemma Int_stable_prod_algebra: "Int_stable (prod_algebra I M)"
   1.277 -proof (unfold Int_stable_def, safe)
   1.278 -  fix A assume "A \<in> prod_algebra I M"
   1.279 -  from prod_algebraE[OF this] guess J E . note A = this
   1.280 -  fix B assume "B \<in> prod_algebra I M"
   1.281 -  from prod_algebraE[OF this] guess K F . note B = this
   1.282 -  have "A \<inter> B = prod_emb I M (J \<union> K) (\<Pi>\<^sub>E i\<in>J \<union> K. (if i \<in> J then E i else space (M i)) \<inter>
   1.283 -      (if i \<in> K then F i else space (M i)))"
   1.284 -    unfolding A B using A(2,3,4) A(5)[THEN sets.sets_into_space] B(2,3,4)
   1.285 -      B(5)[THEN sets.sets_into_space]
   1.286 -    apply (subst (1 2 3) prod_emb_PiE)
   1.287 -    apply (simp_all add: subset_eq PiE_Int)
   1.288 -    apply blast
   1.289 -    apply (intro PiE_cong)
   1.290 -    apply auto
   1.291 -    done
   1.292 -  also have "\<dots> \<in> prod_algebra I M"
   1.293 -    using A B by (auto intro!: prod_algebraI)
   1.294 -  finally show "A \<inter> B \<in> prod_algebra I M" .
   1.295 -qed
   1.296 -
   1.297 -lemma prod_algebra_mono:
   1.298 -  assumes space: "\<And>i. i \<in> I \<Longrightarrow> space (E i) = space (F i)"
   1.299 -  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (E i) \<subseteq> sets (F i)"
   1.300 -  shows "prod_algebra I E \<subseteq> prod_algebra I F"
   1.301 -proof
   1.302 -  fix A assume "A \<in> prod_algebra I E"
   1.303 -  then obtain J G where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I"
   1.304 -    and A: "A = prod_emb I E J (\<Pi>\<^sub>E i\<in>J. G i)"
   1.305 -    and G: "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (E i)"
   1.306 -    by (auto simp: prod_algebra_def)
   1.307 -  moreover
   1.308 -  from space have "(\<Pi>\<^sub>E i\<in>I. space (E i)) = (\<Pi>\<^sub>E i\<in>I. space (F i))"
   1.309 -    by (rule PiE_cong)
   1.310 -  with A have "A = prod_emb I F J (\<Pi>\<^sub>E i\<in>J. G i)"
   1.311 -    by (simp add: prod_emb_def)
   1.312 -  moreover
   1.313 -  from sets G J have "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (F i)"
   1.314 -    by auto
   1.315 -  ultimately show "A \<in> prod_algebra I F"
   1.316 -    apply (simp add: prod_algebra_def image_iff)
   1.317 -    apply (intro exI[of _ J] exI[of _ G] conjI)
   1.318 -    apply auto
   1.319 -    done
   1.320 -qed
   1.321 -
   1.322 -lemma prod_algebra_cong:
   1.323 -  assumes "I = J" and sets: "(\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i))"
   1.324 -  shows "prod_algebra I M = prod_algebra J N"
   1.325 -proof -
   1.326 -  have space: "\<And>i. i \<in> I \<Longrightarrow> space (M i) = space (N i)"
   1.327 -    using sets_eq_imp_space_eq[OF sets] by auto
   1.328 -  with sets show ?thesis unfolding \<open>I = J\<close>
   1.329 -    by (intro antisym prod_algebra_mono) auto
   1.330 -qed
   1.331 -
   1.332 -lemma space_in_prod_algebra:
   1.333 -  "(\<Pi>\<^sub>E i\<in>I. space (M i)) \<in> prod_algebra I M"
   1.334 -proof cases
   1.335 -  assume "I = {}" then show ?thesis
   1.336 -    by (auto simp add: prod_algebra_def image_iff prod_emb_def)
   1.337 -next
   1.338 -  assume "I \<noteq> {}"
   1.339 -  then obtain i where "i \<in> I" by auto
   1.340 -  then have "(\<Pi>\<^sub>E i\<in>I. space (M i)) = prod_emb I M {i} (\<Pi>\<^sub>E i\<in>{i}. space (M i))"
   1.341 -    by (auto simp: prod_emb_def)
   1.342 -  also have "\<dots> \<in> prod_algebra I M"
   1.343 -    using \<open>i \<in> I\<close> by (intro prod_algebraI) auto
   1.344 -  finally show ?thesis .
   1.345 -qed
   1.346 -
   1.347 -lemma space_PiM: "space (\<Pi>\<^sub>M i\<in>I. M i) = (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.348 -  using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro space_extend_measure) simp
   1.349 -
   1.350 -lemma prod_emb_subset_PiM[simp]: "prod_emb I M K X \<subseteq> space (PiM I M)"
   1.351 -  by (auto simp: prod_emb_def space_PiM)
   1.352 -
   1.353 -lemma space_PiM_empty_iff[simp]: "space (PiM I M) = {} \<longleftrightarrow>  (\<exists>i\<in>I. space (M i) = {})"
   1.354 -  by (auto simp: space_PiM PiE_eq_empty_iff)
   1.355 -
   1.356 -lemma undefined_in_PiM_empty[simp]: "(\<lambda>x. undefined) \<in> space (PiM {} M)"
   1.357 -  by (auto simp: space_PiM)
   1.358 -
   1.359 -lemma sets_PiM: "sets (\<Pi>\<^sub>M i\<in>I. M i) = sigma_sets (\<Pi>\<^sub>E i\<in>I. space (M i)) (prod_algebra I M)"
   1.360 -  using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro sets_extend_measure) simp
   1.361 -
   1.362 -lemma sets_PiM_single: "sets (PiM I M) =
   1.363 -    sigma_sets (\<Pi>\<^sub>E i\<in>I. space (M i)) {{f\<in>\<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> sets (M i)}"
   1.364 -    (is "_ = sigma_sets ?\<Omega> ?R")
   1.365 -  unfolding sets_PiM
   1.366 -proof (rule sigma_sets_eqI)
   1.367 -  interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
   1.368 -  fix A assume "A \<in> prod_algebra I M"
   1.369 -  from prod_algebraE[OF this] guess J X . note X = this
   1.370 -  show "A \<in> sigma_sets ?\<Omega> ?R"
   1.371 -  proof cases
   1.372 -    assume "I = {}"
   1.373 -    with X have "A = {\<lambda>x. undefined}" by (auto simp: prod_emb_def)
   1.374 -    with \<open>I = {}\<close> show ?thesis by (auto intro!: sigma_sets_top)
   1.375 -  next
   1.376 -    assume "I \<noteq> {}"
   1.377 -    with X have "A = (\<Inter>j\<in>J. {f\<in>(\<Pi>\<^sub>E i\<in>I. space (M i)). f j \<in> X j})"
   1.378 -      by (auto simp: prod_emb_def)
   1.379 -    also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
   1.380 -      using X \<open>I \<noteq> {}\<close> by (intro R.finite_INT sigma_sets.Basic) auto
   1.381 -    finally show "A \<in> sigma_sets ?\<Omega> ?R" .
   1.382 -  qed
   1.383 -next
   1.384 -  fix A assume "A \<in> ?R"
   1.385 -  then obtain i B where A: "A = {f\<in>\<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> B}" "i \<in> I" "B \<in> sets (M i)"
   1.386 -    by auto
   1.387 -  then have "A = prod_emb I M {i} (\<Pi>\<^sub>E i\<in>{i}. B)"
   1.388 -     by (auto simp: prod_emb_def)
   1.389 -  also have "\<dots> \<in> sigma_sets ?\<Omega> (prod_algebra I M)"
   1.390 -    using A by (intro sigma_sets.Basic prod_algebraI) auto
   1.391 -  finally show "A \<in> sigma_sets ?\<Omega> (prod_algebra I M)" .
   1.392 -qed
   1.393 -
   1.394 -lemma sets_PiM_eq_proj:
   1.395 -  "I \<noteq> {} \<Longrightarrow> sets (PiM I M) = sets (SUP i:I. vimage_algebra (\<Pi>\<^sub>E i\<in>I. space (M i)) (\<lambda>x. x i) (M i))"
   1.396 -  apply (simp add: sets_PiM_single)
   1.397 -  apply (subst sets_Sup_eq[where X="\<Pi>\<^sub>E i\<in>I. space (M i)"])
   1.398 -  apply auto []
   1.399 -  apply auto []
   1.400 -  apply simp
   1.401 -  apply (subst SUP_cong[OF refl])
   1.402 -  apply (rule sets_vimage_algebra2)
   1.403 -  apply auto []
   1.404 -  apply (auto intro!: arg_cong2[where f=sigma_sets])
   1.405 -  done
   1.406 -
   1.407 -lemma
   1.408 -  shows space_PiM_empty: "space (Pi\<^sub>M {} M) = {\<lambda>k. undefined}"
   1.409 -    and sets_PiM_empty: "sets (Pi\<^sub>M {} M) = { {}, {\<lambda>k. undefined} }"
   1.410 -  by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
   1.411 -
   1.412 -lemma sets_PiM_sigma:
   1.413 -  assumes \<Omega>_cover: "\<And>i. i \<in> I \<Longrightarrow> \<exists>S\<subseteq>E i. countable S \<and> \<Omega> i = \<Union>S"
   1.414 -  assumes E: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (\<Omega> i)"
   1.415 -  assumes J: "\<And>j. j \<in> J \<Longrightarrow> finite j" "\<Union>J = I"
   1.416 -  defines "P \<equiv> {{f\<in>(\<Pi>\<^sub>E i\<in>I. \<Omega> i). \<forall>i\<in>j. f i \<in> A i} | A j. j \<in> J \<and> A \<in> Pi j E}"
   1.417 -  shows "sets (\<Pi>\<^sub>M i\<in>I. sigma (\<Omega> i) (E i)) = sets (sigma (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P)"
   1.418 -proof cases
   1.419 -  assume "I = {}"
   1.420 -  with \<open>\<Union>J = I\<close> have "P = {{\<lambda>_. undefined}} \<or> P = {}"
   1.421 -    by (auto simp: P_def)
   1.422 -  with \<open>I = {}\<close> show ?thesis
   1.423 -    by (auto simp add: sets_PiM_empty sigma_sets_empty_eq)
   1.424 -next
   1.425 -  let ?F = "\<lambda>i. {(\<lambda>x. x i) -` A \<inter> Pi\<^sub>E I \<Omega> |A. A \<in> E i}"
   1.426 -  assume "I \<noteq> {}"
   1.427 -  then have "sets (Pi\<^sub>M I (\<lambda>i. sigma (\<Omega> i) (E i))) =
   1.428 -      sets (SUP i:I. vimage_algebra (\<Pi>\<^sub>E i\<in>I. \<Omega> i) (\<lambda>x. x i) (sigma (\<Omega> i) (E i)))"
   1.429 -    by (subst sets_PiM_eq_proj) (auto simp: space_measure_of_conv)
   1.430 -  also have "\<dots> = sets (SUP i:I. sigma (Pi\<^sub>E I \<Omega>) (?F i))"
   1.431 -    using E by (intro sets_SUP_cong arg_cong[where f=sets] vimage_algebra_sigma) auto
   1.432 -  also have "\<dots> = sets (sigma (Pi\<^sub>E I \<Omega>) (\<Union>i\<in>I. ?F i))"
   1.433 -    using \<open>I \<noteq> {}\<close> by (intro arg_cong[where f=sets] SUP_sigma_sigma) auto
   1.434 -  also have "\<dots> = sets (sigma (Pi\<^sub>E I \<Omega>) P)"
   1.435 -  proof (intro arg_cong[where f=sets] sigma_eqI sigma_sets_eqI)
   1.436 -    show "(\<Union>i\<in>I. ?F i) \<subseteq> Pow (Pi\<^sub>E I \<Omega>)" "P \<subseteq> Pow (Pi\<^sub>E I \<Omega>)"
   1.437 -      by (auto simp: P_def)
   1.438 -  next
   1.439 -    interpret P: sigma_algebra "\<Pi>\<^sub>E i\<in>I. \<Omega> i" "sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P"
   1.440 -      by (auto intro!: sigma_algebra_sigma_sets simp: P_def)
   1.441 -
   1.442 -    fix Z assume "Z \<in> (\<Union>i\<in>I. ?F i)"
   1.443 -    then obtain i A where i: "i \<in> I" "A \<in> E i" and Z_def: "Z = (\<lambda>x. x i) -` A \<inter> Pi\<^sub>E I \<Omega>"
   1.444 -      by auto
   1.445 -    from \<open>i \<in> I\<close> J obtain j where j: "i \<in> j" "j \<in> J" "j \<subseteq> I" "finite j"
   1.446 -      by auto
   1.447 -    obtain S where S: "\<And>i. i \<in> j \<Longrightarrow> S i \<subseteq> E i" "\<And>i. i \<in> j \<Longrightarrow> countable (S i)"
   1.448 -      "\<And>i. i \<in> j \<Longrightarrow> \<Omega> i = \<Union>(S i)"
   1.449 -      by (metis subset_eq \<Omega>_cover \<open>j \<subseteq> I\<close>)
   1.450 -    define A' where "A' n = n(i := A)" for n
   1.451 -    then have A'_i: "\<And>n. A' n i = A"
   1.452 -      by simp
   1.453 -    { fix n assume "n \<in> Pi\<^sub>E (j - {i}) S"
   1.454 -      then have "A' n \<in> Pi j E"
   1.455 -        unfolding PiE_def Pi_def using S(1) by (auto simp: A'_def \<open>A \<in> E i\<close> )
   1.456 -      with \<open>j \<in> J\<close> have "{f \<in> Pi\<^sub>E I \<Omega>. \<forall>i\<in>j. f i \<in> A' n i} \<in> P"
   1.457 -        by (auto simp: P_def) }
   1.458 -    note A'_in_P = this
   1.459 -
   1.460 -    { fix x assume "x i \<in> A" "x \<in> Pi\<^sub>E I \<Omega>"
   1.461 -      with S(3) \<open>j \<subseteq> I\<close> have "\<forall>i\<in>j. \<exists>s\<in>S i. x i \<in> s"
   1.462 -        by (auto simp: PiE_def Pi_def)
   1.463 -      then obtain s where s: "\<And>i. i \<in> j \<Longrightarrow> s i \<in> S i" "\<And>i. i \<in> j \<Longrightarrow> x i \<in> s i"
   1.464 -        by metis
   1.465 -      with \<open>x i \<in> A\<close> have "\<exists>n\<in>PiE (j-{i}) S. \<forall>i\<in>j. x i \<in> A' n i"
   1.466 -        by (intro bexI[of _ "restrict (s(i := A)) (j-{i})"]) (auto simp: A'_def split: if_splits) }
   1.467 -    then have "Z = (\<Union>n\<in>PiE (j-{i}) S. {f\<in>(\<Pi>\<^sub>E i\<in>I. \<Omega> i). \<forall>i\<in>j. f i \<in> A' n i})"
   1.468 -      unfolding Z_def
   1.469 -      by (auto simp add: set_eq_iff ball_conj_distrib \<open>i\<in>j\<close> A'_i dest: bspec[OF _ \<open>i\<in>j\<close>]
   1.470 -               cong: conj_cong)
   1.471 -    also have "\<dots> \<in> sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P"
   1.472 -      using \<open>finite j\<close> S(2)
   1.473 -      by (intro P.countable_UN' countable_PiE) (simp_all add: image_subset_iff A'_in_P)
   1.474 -    finally show "Z \<in> sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P" .
   1.475 -  next
   1.476 -    interpret F: sigma_algebra "\<Pi>\<^sub>E i\<in>I. \<Omega> i" "sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) (\<Union>i\<in>I. ?F i)"
   1.477 -      by (auto intro!: sigma_algebra_sigma_sets)
   1.478 -
   1.479 -    fix b assume "b \<in> P"
   1.480 -    then obtain A j where b: "b = {f\<in>(\<Pi>\<^sub>E i\<in>I. \<Omega> i). \<forall>i\<in>j. f i \<in> A i}" "j \<in> J" "A \<in> Pi j E"
   1.481 -      by (auto simp: P_def)
   1.482 -    show "b \<in> sigma_sets (Pi\<^sub>E I \<Omega>) (\<Union>i\<in>I. ?F i)"
   1.483 -    proof cases
   1.484 -      assume "j = {}"
   1.485 -      with b have "b = (\<Pi>\<^sub>E i\<in>I. \<Omega> i)"
   1.486 -        by auto
   1.487 -      then show ?thesis
   1.488 -        by blast
   1.489 -    next
   1.490 -      assume "j \<noteq> {}"
   1.491 -      with J b(2,3) have eq: "b = (\<Inter>i\<in>j. ((\<lambda>x. x i) -` A i \<inter> Pi\<^sub>E I \<Omega>))"
   1.492 -        unfolding b(1)
   1.493 -        by (auto simp: PiE_def Pi_def)
   1.494 -      show ?thesis
   1.495 -        unfolding eq using \<open>A \<in> Pi j E\<close> \<open>j \<in> J\<close> J(2)
   1.496 -        by (intro F.finite_INT J \<open>j \<in> J\<close> \<open>j \<noteq> {}\<close> sigma_sets.Basic) blast
   1.497 -    qed
   1.498 -  qed
   1.499 -  finally show "?thesis" .
   1.500 -qed
   1.501 -
   1.502 -lemma sets_PiM_in_sets:
   1.503 -  assumes space: "space N = (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.504 -  assumes sets: "\<And>i A. i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> {x\<in>space N. x i \<in> A} \<in> sets N"
   1.505 -  shows "sets (\<Pi>\<^sub>M i \<in> I. M i) \<subseteq> sets N"
   1.506 -  unfolding sets_PiM_single space[symmetric]
   1.507 -  by (intro sets.sigma_sets_subset subsetI) (auto intro: sets)
   1.508 -
   1.509 -lemma sets_PiM_cong[measurable_cong]:
   1.510 -  assumes "I = J" "\<And>i. i \<in> J \<Longrightarrow> sets (M i) = sets (N i)" shows "sets (PiM I M) = sets (PiM J N)"
   1.511 -  using assms sets_eq_imp_space_eq[OF assms(2)] by (simp add: sets_PiM_single cong: PiE_cong conj_cong)
   1.512 -
   1.513 -lemma sets_PiM_I:
   1.514 -  assumes "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   1.515 -  shows "prod_emb I M J (PIE j:J. E j) \<in> sets (\<Pi>\<^sub>M i\<in>I. M i)"
   1.516 -proof cases
   1.517 -  assume "J = {}"
   1.518 -  then have "prod_emb I M J (PIE j:J. E j) = (PIE j:I. space (M j))"
   1.519 -    by (auto simp: prod_emb_def)
   1.520 -  then show ?thesis
   1.521 -    by (auto simp add: sets_PiM intro!: sigma_sets_top)
   1.522 -next
   1.523 -  assume "J \<noteq> {}" with assms show ?thesis
   1.524 -    by (force simp add: sets_PiM prod_algebra_def)
   1.525 -qed
   1.526 -
   1.527 -lemma measurable_PiM:
   1.528 -  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.529 -  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>
   1.530 -    f -` prod_emb I M J (Pi\<^sub>E J X) \<inter> space N \<in> sets N"
   1.531 -  shows "f \<in> measurable N (PiM I M)"
   1.532 -  using sets_PiM prod_algebra_sets_into_space space
   1.533 -proof (rule measurable_sigma_sets)
   1.534 -  fix A assume "A \<in> prod_algebra I M"
   1.535 -  from prod_algebraE[OF this] guess J X .
   1.536 -  with sets[of J X] show "f -` A \<inter> space N \<in> sets N" by auto
   1.537 -qed
   1.538 -
   1.539 -lemma measurable_PiM_Collect:
   1.540 -  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.541 -  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>
   1.542 -    {\<omega>\<in>space N. \<forall>i\<in>J. f \<omega> i \<in> X i} \<in> sets N"
   1.543 -  shows "f \<in> measurable N (PiM I M)"
   1.544 -  using sets_PiM prod_algebra_sets_into_space space
   1.545 -proof (rule measurable_sigma_sets)
   1.546 -  fix A assume "A \<in> prod_algebra I M"
   1.547 -  from prod_algebraE[OF this] guess J X . note X = this
   1.548 -  then have "f -` A \<inter> space N = {\<omega> \<in> space N. \<forall>i\<in>J. f \<omega> i \<in> X i}"
   1.549 -    using space by (auto simp: prod_emb_def del: PiE_I)
   1.550 -  also have "\<dots> \<in> sets N" using X(3,2,4,5) by (rule sets)
   1.551 -  finally show "f -` A \<inter> space N \<in> sets N" .
   1.552 -qed
   1.553 -
   1.554 -lemma measurable_PiM_single:
   1.555 -  assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.556 -  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"
   1.557 -  shows "f \<in> measurable N (PiM I M)"
   1.558 -  using sets_PiM_single
   1.559 -proof (rule measurable_sigma_sets)
   1.560 -  fix A assume "A \<in> {{f \<in> \<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
   1.561 -  then obtain B i where "A = {f \<in> \<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> B}" and B: "i \<in> I" "B \<in> sets (M i)"
   1.562 -    by auto
   1.563 -  with space have "f -` A \<inter> space N = {\<omega> \<in> space N. f \<omega> i \<in> B}" by auto
   1.564 -  also have "\<dots> \<in> sets N" using B by (rule sets)
   1.565 -  finally show "f -` A \<inter> space N \<in> sets N" .
   1.566 -qed (auto simp: space)
   1.567 -
   1.568 -lemma measurable_PiM_single':
   1.569 -  assumes f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> measurable N (M i)"
   1.570 -    and "(\<lambda>\<omega> i. f i \<omega>) \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.571 -  shows "(\<lambda>\<omega> i. f i \<omega>) \<in> measurable N (Pi\<^sub>M I M)"
   1.572 -proof (rule measurable_PiM_single)
   1.573 -  fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   1.574 -  then have "{\<omega> \<in> space N. f i \<omega> \<in> A} = f i -` A \<inter> space N"
   1.575 -    by auto
   1.576 -  then show "{\<omega> \<in> space N. f i \<omega> \<in> A} \<in> sets N"
   1.577 -    using A f by (auto intro!: measurable_sets)
   1.578 -qed fact
   1.579 -
   1.580 -lemma sets_PiM_I_finite[measurable]:
   1.581 -  assumes "finite I" and sets: "(\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i))"
   1.582 -  shows "(PIE j:I. E j) \<in> sets (\<Pi>\<^sub>M i\<in>I. M i)"
   1.583 -  using sets_PiM_I[of I I E M] sets.sets_into_space[OF sets] \<open>finite I\<close> sets by auto
   1.584 -
   1.585 -lemma measurable_component_singleton[measurable (raw)]:
   1.586 -  assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^sub>M I M) (M i)"
   1.587 -proof (unfold measurable_def, intro CollectI conjI ballI)
   1.588 -  fix A assume "A \<in> sets (M i)"
   1.589 -  then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^sub>M I M) = prod_emb I M {i} (\<Pi>\<^sub>E j\<in>{i}. A)"
   1.590 -    using sets.sets_into_space \<open>i \<in> I\<close>
   1.591 -    by (fastforce dest: Pi_mem simp: prod_emb_def space_PiM split: if_split_asm)
   1.592 -  then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^sub>M I M) \<in> sets (Pi\<^sub>M I M)"
   1.593 -    using \<open>A \<in> sets (M i)\<close> \<open>i \<in> I\<close> by (auto intro!: sets_PiM_I)
   1.594 -qed (insert \<open>i \<in> I\<close>, auto simp: space_PiM)
   1.595 -
   1.596 -lemma measurable_component_singleton'[measurable_dest]:
   1.597 -  assumes f: "f \<in> measurable N (Pi\<^sub>M I M)"
   1.598 -  assumes g: "g \<in> measurable L N"
   1.599 -  assumes i: "i \<in> I"
   1.600 -  shows "(\<lambda>x. (f (g x)) i) \<in> measurable L (M i)"
   1.601 -  using measurable_compose[OF measurable_compose[OF g f] measurable_component_singleton, OF i] .
   1.602 -
   1.603 -lemma measurable_PiM_component_rev:
   1.604 -  "i \<in> I \<Longrightarrow> f \<in> measurable (M i) N \<Longrightarrow> (\<lambda>x. f (x i)) \<in> measurable (PiM I M) N"
   1.605 -  by simp
   1.606 -
   1.607 -lemma measurable_case_nat[measurable (raw)]:
   1.608 -  assumes [measurable (raw)]: "i = 0 \<Longrightarrow> f \<in> measurable M N"
   1.609 -    "\<And>j. i = Suc j \<Longrightarrow> (\<lambda>x. g x j) \<in> measurable M N"
   1.610 -  shows "(\<lambda>x. case_nat (f x) (g x) i) \<in> measurable M N"
   1.611 -  by (cases i) simp_all
   1.612 -
   1.613 -lemma measurable_case_nat'[measurable (raw)]:
   1.614 -  assumes fg[measurable]: "f \<in> measurable N M" "g \<in> measurable N (\<Pi>\<^sub>M i\<in>UNIV. M)"
   1.615 -  shows "(\<lambda>x. case_nat (f x) (g x)) \<in> measurable N (\<Pi>\<^sub>M i\<in>UNIV. M)"
   1.616 -  using fg[THEN measurable_space]
   1.617 -  by (auto intro!: measurable_PiM_single' simp add: space_PiM PiE_iff split: nat.split)
   1.618 -
   1.619 -lemma measurable_add_dim[measurable]:
   1.620 -  "(\<lambda>(f, y). f(i := y)) \<in> measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M M i) (Pi\<^sub>M (insert i I) M)"
   1.621 -    (is "?f \<in> measurable ?P ?I")
   1.622 -proof (rule measurable_PiM_single)
   1.623 -  fix j A assume j: "j \<in> insert i I" and A: "A \<in> sets (M j)"
   1.624 -  have "{\<omega> \<in> space ?P. (\<lambda>(f, x). fun_upd f i x) \<omega> j \<in> A} =
   1.625 -    (if j = i then space (Pi\<^sub>M I M) \<times> A else ((\<lambda>x. x j) \<circ> fst) -` A \<inter> space ?P)"
   1.626 -    using sets.sets_into_space[OF A] by (auto simp add: space_pair_measure space_PiM)
   1.627 -  also have "\<dots> \<in> sets ?P"
   1.628 -    using A j
   1.629 -    by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   1.630 -  finally show "{\<omega> \<in> space ?P. case_prod (\<lambda>f. fun_upd f i) \<omega> j \<in> A} \<in> sets ?P" .
   1.631 -qed (auto simp: space_pair_measure space_PiM PiE_def)
   1.632 -
   1.633 -lemma measurable_fun_upd:
   1.634 -  assumes I: "I = J \<union> {i}"
   1.635 -  assumes f[measurable]: "f \<in> measurable N (PiM J M)"
   1.636 -  assumes h[measurable]: "h \<in> measurable N (M i)"
   1.637 -  shows "(\<lambda>x. (f x) (i := h x)) \<in> measurable N (PiM I M)"
   1.638 -proof (intro measurable_PiM_single')
   1.639 -  fix j assume "j \<in> I" then show "(\<lambda>\<omega>. ((f \<omega>)(i := h \<omega>)) j) \<in> measurable N (M j)"
   1.640 -    unfolding I by (cases "j = i") auto
   1.641 -next
   1.642 -  show "(\<lambda>x. (f x)(i := h x)) \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   1.643 -    using I f[THEN measurable_space] h[THEN measurable_space]
   1.644 -    by (auto simp: space_PiM PiE_iff extensional_def)
   1.645 -qed
   1.646 -
   1.647 -lemma measurable_component_update:
   1.648 -  "x \<in> space (Pi\<^sub>M I M) \<Longrightarrow> i \<notin> I \<Longrightarrow> (\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^sub>M (insert i I) M)"
   1.649 -  by simp
   1.650 -
   1.651 -lemma measurable_merge[measurable]:
   1.652 -  "merge I J \<in> measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) (Pi\<^sub>M (I \<union> J) M)"
   1.653 -    (is "?f \<in> measurable ?P ?U")
   1.654 -proof (rule measurable_PiM_single)
   1.655 -  fix i A assume A: "A \<in> sets (M i)" "i \<in> I \<union> J"
   1.656 -  then have "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} =
   1.657 -    (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)"
   1.658 -    by (auto simp: merge_def)
   1.659 -  also have "\<dots> \<in> sets ?P"
   1.660 -    using A
   1.661 -    by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   1.662 -  finally show "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} \<in> sets ?P" .
   1.663 -qed (auto simp: space_pair_measure space_PiM PiE_iff merge_def extensional_def)
   1.664 -
   1.665 -lemma measurable_restrict[measurable (raw)]:
   1.666 -  assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable N (M i)"
   1.667 -  shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable N (Pi\<^sub>M I M)"
   1.668 -proof (rule measurable_PiM_single)
   1.669 -  fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   1.670 -  then have "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} = X i -` A \<inter> space N"
   1.671 -    by auto
   1.672 -  then show "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} \<in> sets N"
   1.673 -    using A X by (auto intro!: measurable_sets)
   1.674 -qed (insert X, auto simp add: PiE_def dest: measurable_space)
   1.675 -
   1.676 -lemma measurable_abs_UNIV:
   1.677 -  "(\<And>n. (\<lambda>\<omega>. f n \<omega>) \<in> measurable M (N n)) \<Longrightarrow> (\<lambda>\<omega> n. f n \<omega>) \<in> measurable M (PiM UNIV N)"
   1.678 -  by (intro measurable_PiM_single) (auto dest: measurable_space)
   1.679 -
   1.680 -lemma measurable_restrict_subset: "J \<subseteq> L \<Longrightarrow> (\<lambda>f. restrict f J) \<in> measurable (Pi\<^sub>M L M) (Pi\<^sub>M J M)"
   1.681 -  by (intro measurable_restrict measurable_component_singleton) auto
   1.682 -
   1.683 -lemma measurable_restrict_subset':
   1.684 -  assumes "J \<subseteq> L" "\<And>x. x \<in> J \<Longrightarrow> sets (M x) = sets (N x)"
   1.685 -  shows "(\<lambda>f. restrict f J) \<in> measurable (Pi\<^sub>M L M) (Pi\<^sub>M J N)"
   1.686 -proof-
   1.687 -  from assms(1) have "(\<lambda>f. restrict f J) \<in> measurable (Pi\<^sub>M L M) (Pi\<^sub>M J M)"
   1.688 -    by (rule measurable_restrict_subset)
   1.689 -  also from assms(2) have "measurable (Pi\<^sub>M L M) (Pi\<^sub>M J M) = measurable (Pi\<^sub>M L M) (Pi\<^sub>M J N)"
   1.690 -    by (intro sets_PiM_cong measurable_cong_sets) simp_all
   1.691 -  finally show ?thesis .
   1.692 -qed
   1.693 -
   1.694 -lemma measurable_prod_emb[intro, simp]:
   1.695 -  "J \<subseteq> L \<Longrightarrow> X \<in> sets (Pi\<^sub>M J M) \<Longrightarrow> prod_emb L M J X \<in> sets (Pi\<^sub>M L M)"
   1.696 -  unfolding prod_emb_def space_PiM[symmetric]
   1.697 -  by (auto intro!: measurable_sets measurable_restrict measurable_component_singleton)
   1.698 -
   1.699 -lemma merge_in_prod_emb:
   1.700 -  assumes "y \<in> space (PiM I M)" "x \<in> X" and X: "X \<in> sets (Pi\<^sub>M J M)" and "J \<subseteq> I"
   1.701 -  shows "merge J I (x, y) \<in> prod_emb I M J X"
   1.702 -  using assms sets.sets_into_space[OF X]
   1.703 -  by (simp add: merge_def prod_emb_def subset_eq space_PiM PiE_def extensional_restrict Pi_iff
   1.704 -           cong: if_cong restrict_cong)
   1.705 -     (simp add: extensional_def)
   1.706 -
   1.707 -lemma prod_emb_eq_emptyD:
   1.708 -  assumes J: "J \<subseteq> I" and ne: "space (PiM I M) \<noteq> {}" and X: "X \<in> sets (Pi\<^sub>M J M)"
   1.709 -    and *: "prod_emb I M J X = {}"
   1.710 -  shows "X = {}"
   1.711 -proof safe
   1.712 -  fix x assume "x \<in> X"
   1.713 -  obtain \<omega> where "\<omega> \<in> space (PiM I M)"
   1.714 -    using ne by blast
   1.715 -  from merge_in_prod_emb[OF this \<open>x\<in>X\<close> X J] * show "x \<in> {}" by auto
   1.716 -qed
   1.717 -
   1.718 -lemma sets_in_Pi_aux:
   1.719 -  "finite I \<Longrightarrow> (\<And>j. j \<in> I \<Longrightarrow> {x\<in>space (M j). x \<in> F j} \<in> sets (M j)) \<Longrightarrow>
   1.720 -  {x\<in>space (PiM I M). x \<in> Pi I F} \<in> sets (PiM I M)"
   1.721 -  by (simp add: subset_eq Pi_iff)
   1.722 -
   1.723 -lemma sets_in_Pi[measurable (raw)]:
   1.724 -  "finite I \<Longrightarrow> f \<in> measurable N (PiM I M) \<Longrightarrow>
   1.725 -  (\<And>j. j \<in> I \<Longrightarrow> {x\<in>space (M j). x \<in> F j} \<in> sets (M j)) \<Longrightarrow>
   1.726 -  Measurable.pred N (\<lambda>x. f x \<in> Pi I F)"
   1.727 -  unfolding pred_def
   1.728 -  by (rule measurable_sets_Collect[of f N "PiM I M", OF _ sets_in_Pi_aux]) auto
   1.729 -
   1.730 -lemma sets_in_extensional_aux:
   1.731 -  "{x\<in>space (PiM I M). x \<in> extensional I} \<in> sets (PiM I M)"
   1.732 -proof -
   1.733 -  have "{x\<in>space (PiM I M). x \<in> extensional I} = space (PiM I M)"
   1.734 -    by (auto simp add: extensional_def space_PiM)
   1.735 -  then show ?thesis by simp
   1.736 -qed
   1.737 -
   1.738 -lemma sets_in_extensional[measurable (raw)]:
   1.739 -  "f \<in> measurable N (PiM I M) \<Longrightarrow> Measurable.pred N (\<lambda>x. f x \<in> extensional I)"
   1.740 -  unfolding pred_def
   1.741 -  by (rule measurable_sets_Collect[of f N "PiM I M", OF _ sets_in_extensional_aux]) auto
   1.742 -
   1.743 -lemma sets_PiM_I_countable:
   1.744 -  assumes I: "countable I" and E: "\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i)" shows "Pi\<^sub>E I E \<in> sets (Pi\<^sub>M I M)"
   1.745 -proof cases
   1.746 -  assume "I \<noteq> {}"
   1.747 -  then have "PiE I E = (\<Inter>i\<in>I. prod_emb I M {i} (PiE {i} E))"
   1.748 -    using E[THEN sets.sets_into_space] by (auto simp: PiE_iff prod_emb_def fun_eq_iff)
   1.749 -  also have "\<dots> \<in> sets (PiM I M)"
   1.750 -    using I \<open>I \<noteq> {}\<close> by (safe intro!: sets.countable_INT' measurable_prod_emb sets_PiM_I_finite E)
   1.751 -  finally show ?thesis .
   1.752 -qed (simp add: sets_PiM_empty)
   1.753 -
   1.754 -lemma sets_PiM_D_countable:
   1.755 -  assumes A: "A \<in> PiM I M"
   1.756 -  shows "\<exists>J\<subseteq>I. \<exists>X\<in>PiM J M. countable J \<and> A = prod_emb I M J X"
   1.757 -  using A[unfolded sets_PiM_single]
   1.758 -proof induction
   1.759 -  case (Basic A)
   1.760 -  then obtain i X where *: "i \<in> I" "X \<in> sets (M i)" and "A = {f \<in> \<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> X}"
   1.761 -    by auto
   1.762 -  then have A: "A = prod_emb I M {i} (\<Pi>\<^sub>E _\<in>{i}. X)"
   1.763 -    by (auto simp: prod_emb_def)
   1.764 -  then show ?case
   1.765 -    by (intro exI[of _ "{i}"] conjI bexI[of _ "\<Pi>\<^sub>E _\<in>{i}. X"])
   1.766 -       (auto intro: countable_finite * sets_PiM_I_finite)
   1.767 -next
   1.768 -  case Empty then show ?case
   1.769 -    by (intro exI[of _ "{}"] conjI bexI[of _ "{}"]) auto
   1.770 -next
   1.771 -  case (Compl A)
   1.772 -  then obtain J X where "J \<subseteq> I" "X \<in> sets (Pi\<^sub>M J M)" "countable J" "A = prod_emb I M J X"
   1.773 -    by auto
   1.774 -  then show ?case
   1.775 -    by (intro exI[of _ J] bexI[of _ "space (PiM J M) - X"] conjI)
   1.776 -       (auto simp add: space_PiM prod_emb_PiE intro!: sets_PiM_I_countable)
   1.777 -next
   1.778 -  case (Union K)
   1.779 -  obtain J X where J: "\<And>i. J i \<subseteq> I" "\<And>i. countable (J i)" and X: "\<And>i. X i \<in> sets (Pi\<^sub>M (J i) M)"
   1.780 -    and K: "\<And>i. K i = prod_emb I M (J i) (X i)"
   1.781 -    by (metis Union.IH)
   1.782 -  show ?case
   1.783 -  proof (intro exI[of _ "\<Union>i. J i"] bexI[of _ "\<Union>i. prod_emb (\<Union>i. J i) M (J i) (X i)"] conjI)
   1.784 -    show "(\<Union>i. J i) \<subseteq> I" "countable (\<Union>i. J i)" using J by auto
   1.785 -    with J show "UNION UNIV K = prod_emb I M (\<Union>i. J i) (\<Union>i. prod_emb (\<Union>i. J i) M (J i) (X i))"
   1.786 -      by (simp add: K[abs_def] SUP_upper)
   1.787 -  qed(auto intro: X)
   1.788 -qed
   1.789 -
   1.790 -lemma measure_eqI_PiM_finite:
   1.791 -  assumes [simp]: "finite I" "sets P = PiM I M" "sets Q = PiM I M"
   1.792 -  assumes eq: "\<And>A. (\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> P (Pi\<^sub>E I A) = Q (Pi\<^sub>E I A)"
   1.793 -  assumes A: "range A \<subseteq> prod_algebra I M" "(\<Union>i. A i) = space (PiM I M)" "\<And>i::nat. P (A i) \<noteq> \<infinity>"
   1.794 -  shows "P = Q"
   1.795 -proof (rule measure_eqI_generator_eq[OF Int_stable_prod_algebra prod_algebra_sets_into_space])
   1.796 -  show "range A \<subseteq> prod_algebra I M" "(\<Union>i. A i) = (\<Pi>\<^sub>E i\<in>I. space (M i))" "\<And>i. P (A i) \<noteq> \<infinity>"
   1.797 -    unfolding space_PiM[symmetric] by fact+
   1.798 -  fix X assume "X \<in> prod_algebra I M"
   1.799 -  then obtain J E where X: "X = prod_emb I M J (PIE j:J. E j)"
   1.800 -    and J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)"
   1.801 -    by (force elim!: prod_algebraE)
   1.802 -  then show "emeasure P X = emeasure Q X"
   1.803 -    unfolding X by (subst (1 2) prod_emb_Pi) (auto simp: eq)
   1.804 -qed (simp_all add: sets_PiM)
   1.805 -
   1.806 -lemma measure_eqI_PiM_infinite:
   1.807 -  assumes [simp]: "sets P = PiM I M" "sets Q = PiM I M"
   1.808 -  assumes eq: "\<And>A J. finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow>
   1.809 -    P (prod_emb I M J (Pi\<^sub>E J A)) = Q (prod_emb I M J (Pi\<^sub>E J A))"
   1.810 -  assumes A: "finite_measure P"
   1.811 -  shows "P = Q"
   1.812 -proof (rule measure_eqI_generator_eq[OF Int_stable_prod_algebra prod_algebra_sets_into_space])
   1.813 -  interpret finite_measure P by fact
   1.814 -  define i where "i = (SOME i. i \<in> I)"
   1.815 -  have i: "I \<noteq> {} \<Longrightarrow> i \<in> I"
   1.816 -    unfolding i_def by (rule someI_ex) auto
   1.817 -  define A where "A n =
   1.818 -    (if I = {} then prod_emb I M {} (\<Pi>\<^sub>E i\<in>{}. {}) else prod_emb I M {i} (\<Pi>\<^sub>E i\<in>{i}. space (M i)))"
   1.819 -    for n :: nat
   1.820 -  then show "range A \<subseteq> prod_algebra I M"
   1.821 -    using prod_algebraI[of "{}" I "\<lambda>i. space (M i)" M] by (auto intro!: prod_algebraI i)
   1.822 -  have "\<And>i. A i = space (PiM I M)"
   1.823 -    by (auto simp: prod_emb_def space_PiM PiE_iff A_def i ex_in_conv[symmetric] exI)
   1.824 -  then show "(\<Union>i. A i) = (\<Pi>\<^sub>E i\<in>I. space (M i))" "\<And>i. emeasure P (A i) \<noteq> \<infinity>"
   1.825 -    by (auto simp: space_PiM)
   1.826 -next
   1.827 -  fix X assume X: "X \<in> prod_algebra I M"
   1.828 -  then obtain J E where X: "X = prod_emb I M J (PIE j:J. E j)"
   1.829 -    and J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)"
   1.830 -    by (force elim!: prod_algebraE)
   1.831 -  then show "emeasure P X = emeasure Q X"
   1.832 -    by (auto intro!: eq)
   1.833 -qed (auto simp: sets_PiM)
   1.834 -
   1.835 -locale product_sigma_finite =
   1.836 -  fixes M :: "'i \<Rightarrow> 'a measure"
   1.837 -  assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
   1.838 -
   1.839 -sublocale product_sigma_finite \<subseteq> M?: sigma_finite_measure "M i" for i
   1.840 -  by (rule sigma_finite_measures)
   1.841 -
   1.842 -locale finite_product_sigma_finite = product_sigma_finite M for M :: "'i \<Rightarrow> 'a measure" +
   1.843 -  fixes I :: "'i set"
   1.844 -  assumes finite_index: "finite I"
   1.845 -
   1.846 -lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   1.847 -  "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
   1.848 -    (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
   1.849 -    (\<forall>k. \<forall>i\<in>I. emeasure (M i) (F i k) \<noteq> \<infinity>) \<and> incseq (\<lambda>k. \<Pi>\<^sub>E i\<in>I. F i k) \<and>
   1.850 -    (\<Union>k. \<Pi>\<^sub>E i\<in>I. F i k) = space (PiM I M)"
   1.851 -proof -
   1.852 -  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>)"
   1.853 -    using M.sigma_finite_incseq by metis
   1.854 -  from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   1.855 -  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>"
   1.856 -    by auto
   1.857 -  let ?F = "\<lambda>k. \<Pi>\<^sub>E i\<in>I. F i k"
   1.858 -  note space_PiM[simp]
   1.859 -  show ?thesis
   1.860 -  proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
   1.861 -    fix i show "range (F i) \<subseteq> sets (M i)" by fact
   1.862 -  next
   1.863 -    fix i k show "emeasure (M i) (F i k) \<noteq> \<infinity>" by fact
   1.864 -  next
   1.865 -    fix x assume "x \<in> (\<Union>i. ?F i)" with F(1) show "x \<in> space (PiM I M)"
   1.866 -      by (auto simp: PiE_def dest!: sets.sets_into_space)
   1.867 -  next
   1.868 -    fix f assume "f \<in> space (PiM I M)"
   1.869 -    with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
   1.870 -    show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def PiE_def)
   1.871 -  next
   1.872 -    fix i show "?F i \<subseteq> ?F (Suc i)"
   1.873 -      using \<open>\<And>i. incseq (F i)\<close>[THEN incseq_SucD] by auto
   1.874 -  qed
   1.875 -qed
   1.876 -
   1.877 -lemma emeasure_PiM_empty[simp]: "emeasure (PiM {} M) {\<lambda>_. undefined} = 1"
   1.878 -proof -
   1.879 -  let ?\<mu> = "\<lambda>A. if A = {} then 0 else (1::ennreal)"
   1.880 -  have "emeasure (Pi\<^sub>M {} M) (prod_emb {} M {} (\<Pi>\<^sub>E i\<in>{}. {})) = 1"
   1.881 -  proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   1.882 -    show "positive (PiM {} M) ?\<mu>"
   1.883 -      by (auto simp: positive_def)
   1.884 -    show "countably_additive (PiM {} M) ?\<mu>"
   1.885 -      by (rule sets.countably_additiveI_finite)
   1.886 -         (auto simp: additive_def positive_def sets_PiM_empty space_PiM_empty intro!: )
   1.887 -  qed (auto simp: prod_emb_def)
   1.888 -  also have "(prod_emb {} M {} (\<Pi>\<^sub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
   1.889 -    by (auto simp: prod_emb_def)
   1.890 -  finally show ?thesis
   1.891 -    by simp
   1.892 -qed
   1.893 -
   1.894 -lemma PiM_empty: "PiM {} M = count_space {\<lambda>_. undefined}"
   1.895 -  by (rule measure_eqI) (auto simp add: sets_PiM_empty)
   1.896 -
   1.897 -lemma (in product_sigma_finite) emeasure_PiM:
   1.898 -  "finite I \<Longrightarrow> (\<And>i. i\<in>I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (PiM I M) (Pi\<^sub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   1.899 -proof (induct I arbitrary: A rule: finite_induct)
   1.900 -  case (insert i I)
   1.901 -  interpret finite_product_sigma_finite M I by standard fact
   1.902 -  have "finite (insert i I)" using \<open>finite I\<close> by auto
   1.903 -  interpret I': finite_product_sigma_finite M "insert i I" by standard fact
   1.904 -  let ?h = "(\<lambda>(f, y). f(i := y))"
   1.905 -
   1.906 -  let ?P = "distr (Pi\<^sub>M I M \<Otimes>\<^sub>M M i) (Pi\<^sub>M (insert i I) M) ?h"
   1.907 -  let ?\<mu> = "emeasure ?P"
   1.908 -  let ?I = "{j \<in> insert i I. emeasure (M j) (space (M j)) \<noteq> 1}"
   1.909 -  let ?f = "\<lambda>J E j. if j \<in> J then emeasure (M j) (E j) else emeasure (M j) (space (M j))"
   1.910 -
   1.911 -  have "emeasure (Pi\<^sub>M (insert i I) M) (prod_emb (insert i I) M (insert i I) (Pi\<^sub>E (insert i I) A)) =
   1.912 -    (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
   1.913 -  proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   1.914 -    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))"
   1.915 -    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
   1.916 -    let ?p = "prod_emb (insert i I) M J (Pi\<^sub>E J E)"
   1.917 -    let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^sub>E j\<in>J-{i}. E j)"
   1.918 -    have "?\<mu> ?p =
   1.919 -      emeasure (Pi\<^sub>M I M \<Otimes>\<^sub>M (M i)) (?h -` ?p \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M M i))"
   1.920 -      by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
   1.921 -    also have "?h -` ?p \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M M i) = ?p' \<times> (if i \<in> J then E i else space (M i))"
   1.922 -      using J E[rule_format, THEN sets.sets_into_space]
   1.923 -      by (force simp: space_pair_measure space_PiM prod_emb_iff PiE_def Pi_iff split: if_split_asm)
   1.924 -    also have "emeasure (Pi\<^sub>M I M \<Otimes>\<^sub>M (M i)) (?p' \<times> (if i \<in> J then E i else space (M i))) =
   1.925 -      emeasure (Pi\<^sub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
   1.926 -      using J E by (intro M.emeasure_pair_measure_Times sets_PiM_I) auto
   1.927 -    also have "?p' = (\<Pi>\<^sub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
   1.928 -      using J E[rule_format, THEN sets.sets_into_space]
   1.929 -      by (auto simp: prod_emb_iff PiE_def Pi_iff split: if_split_asm) blast+
   1.930 -    also have "emeasure (Pi\<^sub>M I M) (\<Pi>\<^sub>E j\<in>I. if j \<in> J-{i} then E j else space (M j)) =
   1.931 -      (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
   1.932 -      using E by (subst insert) (auto intro!: setprod.cong)
   1.933 -    also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
   1.934 -       emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
   1.935 -      using insert by (auto simp: mult.commute intro!: arg_cong2[where f="op *"] setprod.cong)
   1.936 -    also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
   1.937 -      using insert(1,2) J E by (intro setprod.mono_neutral_right) auto
   1.938 -    finally show "?\<mu> ?p = \<dots>" .
   1.939 -
   1.940 -    show "prod_emb (insert i I) M J (Pi\<^sub>E J E) \<in> Pow (\<Pi>\<^sub>E i\<in>insert i I. space (M i))"
   1.941 -      using J E[rule_format, THEN sets.sets_into_space] by (auto simp: prod_emb_iff PiE_def)
   1.942 -  next
   1.943 -    show "positive (sets (Pi\<^sub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^sub>M (insert i I) M)) ?\<mu>"
   1.944 -      using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
   1.945 -  next
   1.946 -    show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
   1.947 -      insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
   1.948 -      using insert by auto
   1.949 -  qed (auto intro!: setprod.cong)
   1.950 -  with insert show ?case
   1.951 -    by (subst (asm) prod_emb_PiE_same_index) (auto intro!: sets.sets_into_space)
   1.952 -qed simp
   1.953 -
   1.954 -lemma (in product_sigma_finite) PiM_eqI:
   1.955 -  assumes I[simp]: "finite I" and P: "sets P = PiM I M"
   1.956 -  assumes eq: "\<And>A. (\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> P (Pi\<^sub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   1.957 -  shows "P = PiM I M"
   1.958 -proof -
   1.959 -  interpret finite_product_sigma_finite M I
   1.960 -    proof qed fact
   1.961 -  from sigma_finite_pairs guess C .. note C = this
   1.962 -  show ?thesis
   1.963 -  proof (rule measure_eqI_PiM_finite[OF I refl P, symmetric])
   1.964 -    show "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> (Pi\<^sub>M I M) (Pi\<^sub>E I A) = P (Pi\<^sub>E I A)" for A
   1.965 -      by (simp add: eq emeasure_PiM)
   1.966 -    define A where "A n = (\<Pi>\<^sub>E i\<in>I. C i n)" for n
   1.967 -    with C show "range A \<subseteq> prod_algebra I M" "\<And>i. emeasure (Pi\<^sub>M I M) (A i) \<noteq> \<infinity>" "(\<Union>i. A i) = space (PiM I M)"
   1.968 -      by (auto intro!: prod_algebraI_finite simp: emeasure_PiM subset_eq ennreal_setprod_eq_top)
   1.969 -  qed
   1.970 -qed
   1.971 -
   1.972 -lemma (in product_sigma_finite) sigma_finite:
   1.973 -  assumes "finite I"
   1.974 -  shows "sigma_finite_measure (PiM I M)"
   1.975 -proof
   1.976 -  interpret finite_product_sigma_finite M I by standard fact
   1.977 -
   1.978 -  obtain F where F: "\<And>j. countable (F j)" "\<And>j f. f \<in> F j \<Longrightarrow> f \<in> sets (M j)"
   1.979 -    "\<And>j f. f \<in> F j \<Longrightarrow> emeasure (M j) f \<noteq> \<infinity>" and
   1.980 -    in_space: "\<And>j. space (M j) = (\<Union>F j)"
   1.981 -    using sigma_finite_countable by (metis subset_eq)
   1.982 -  moreover have "(\<Union>(PiE I ` PiE I F)) = space (Pi\<^sub>M I M)"
   1.983 -    using in_space by (auto simp: space_PiM PiE_iff intro!: PiE_choice[THEN iffD2])
   1.984 -  ultimately show "\<exists>A. countable A \<and> A \<subseteq> sets (Pi\<^sub>M I M) \<and> \<Union>A = space (Pi\<^sub>M I M) \<and> (\<forall>a\<in>A. emeasure (Pi\<^sub>M I M) a \<noteq> \<infinity>)"
   1.985 -    by (intro exI[of _ "PiE I ` PiE I F"])
   1.986 -       (auto intro!: countable_PiE sets_PiM_I_finite
   1.987 -             simp: PiE_iff emeasure_PiM finite_index ennreal_setprod_eq_top)
   1.988 -qed
   1.989 -
   1.990 -sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure "Pi\<^sub>M I M"
   1.991 -  using sigma_finite[OF finite_index] .
   1.992 -
   1.993 -lemma (in finite_product_sigma_finite) measure_times:
   1.994 -  "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (Pi\<^sub>M I M) (Pi\<^sub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   1.995 -  using emeasure_PiM[OF finite_index] by auto
   1.996 -
   1.997 -lemma (in product_sigma_finite) nn_integral_empty:
   1.998 -  "0 \<le> f (\<lambda>k. undefined) \<Longrightarrow> integral\<^sup>N (Pi\<^sub>M {} M) f = f (\<lambda>k. undefined)"
   1.999 -  by (simp add: PiM_empty nn_integral_count_space_finite max.absorb2)
  1.1000 -
  1.1001 -lemma (in product_sigma_finite) distr_merge:
  1.1002 -  assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
  1.1003 -  shows "distr (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) (Pi\<^sub>M (I \<union> J) M) (merge I J) = Pi\<^sub>M (I \<union> J) M"
  1.1004 -   (is "?D = ?P")
  1.1005 -proof (rule PiM_eqI)
  1.1006 -  interpret I: finite_product_sigma_finite M I by standard fact
  1.1007 -  interpret J: finite_product_sigma_finite M J by standard fact
  1.1008 -  fix A assume A: "\<And>i. i \<in> I \<union> J \<Longrightarrow> A i \<in> sets (M i)"
  1.1009 -  have *: "(merge I J -` Pi\<^sub>E (I \<union> J) A \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)) = PiE I A \<times> PiE J A"
  1.1010 -    using A[THEN sets.sets_into_space] by (auto simp: space_PiM space_pair_measure)
  1.1011 -  from A fin show "emeasure (distr (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) (Pi\<^sub>M (I \<union> J) M) (merge I J)) (Pi\<^sub>E (I \<union> J) A) =
  1.1012 -      (\<Prod>i\<in>I \<union> J. emeasure (M i) (A i))"
  1.1013 -    by (subst emeasure_distr)
  1.1014 -       (auto simp: * J.emeasure_pair_measure_Times I.measure_times J.measure_times setprod.union_disjoint)
  1.1015 -qed (insert fin, simp_all)
  1.1016 -
  1.1017 -lemma (in product_sigma_finite) product_nn_integral_fold:
  1.1018 -  assumes IJ: "I \<inter> J = {}" "finite I" "finite J"
  1.1019 -  and f[measurable]: "f \<in> borel_measurable (Pi\<^sub>M (I \<union> J) M)"
  1.1020 -  shows "integral\<^sup>N (Pi\<^sub>M (I \<union> J) M) f =
  1.1021 -    (\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (merge I J (x, y)) \<partial>(Pi\<^sub>M J M)) \<partial>(Pi\<^sub>M I M))"
  1.1022 -proof -
  1.1023 -  interpret I: finite_product_sigma_finite M I by standard fact
  1.1024 -  interpret J: finite_product_sigma_finite M J by standard fact
  1.1025 -  interpret P: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" by standard
  1.1026 -  have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
  1.1027 -    using measurable_comp[OF measurable_merge f] by (simp add: comp_def)
  1.1028 -  show ?thesis
  1.1029 -    apply (subst distr_merge[OF IJ, symmetric])
  1.1030 -    apply (subst nn_integral_distr[OF measurable_merge])
  1.1031 -    apply measurable []
  1.1032 -    apply (subst J.nn_integral_fst[symmetric, OF P_borel])
  1.1033 -    apply simp
  1.1034 -    done
  1.1035 -qed
  1.1036 -
  1.1037 -lemma (in product_sigma_finite) distr_singleton:
  1.1038 -  "distr (Pi\<^sub>M {i} M) (M i) (\<lambda>x. x i) = M i" (is "?D = _")
  1.1039 -proof (intro measure_eqI[symmetric])
  1.1040 -  interpret I: finite_product_sigma_finite M "{i}" by standard simp
  1.1041 -  fix A assume A: "A \<in> sets (M i)"
  1.1042 -  then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^sub>M {i} M) = (\<Pi>\<^sub>E i\<in>{i}. A)"
  1.1043 -    using sets.sets_into_space by (auto simp: space_PiM)
  1.1044 -  then show "emeasure (M i) A = emeasure ?D A"
  1.1045 -    using A I.measure_times[of "\<lambda>_. A"]
  1.1046 -    by (simp add: emeasure_distr measurable_component_singleton)
  1.1047 -qed simp
  1.1048 -
  1.1049 -lemma (in product_sigma_finite) product_nn_integral_singleton:
  1.1050 -  assumes f: "f \<in> borel_measurable (M i)"
  1.1051 -  shows "integral\<^sup>N (Pi\<^sub>M {i} M) (\<lambda>x. f (x i)) = integral\<^sup>N (M i) f"
  1.1052 -proof -
  1.1053 -  interpret I: finite_product_sigma_finite M "{i}" by standard simp
  1.1054 -  from f show ?thesis
  1.1055 -    apply (subst distr_singleton[symmetric])
  1.1056 -    apply (subst nn_integral_distr[OF measurable_component_singleton])
  1.1057 -    apply simp_all
  1.1058 -    done
  1.1059 -qed
  1.1060 -
  1.1061 -lemma (in product_sigma_finite) product_nn_integral_insert:
  1.1062 -  assumes I[simp]: "finite I" "i \<notin> I"
  1.1063 -    and f: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  1.1064 -  shows "integral\<^sup>N (Pi\<^sub>M (insert i I) M) f = (\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x(i := y)) \<partial>(M i)) \<partial>(Pi\<^sub>M I M))"
  1.1065 -proof -
  1.1066 -  interpret I: finite_product_sigma_finite M I by standard auto
  1.1067 -  interpret i: finite_product_sigma_finite M "{i}" by standard auto
  1.1068 -  have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
  1.1069 -    using f by auto
  1.1070 -  show ?thesis
  1.1071 -    unfolding product_nn_integral_fold[OF IJ, unfolded insert, OF I(1) i.finite_index f]
  1.1072 -  proof (rule nn_integral_cong, subst product_nn_integral_singleton[symmetric])
  1.1073 -    fix x assume x: "x \<in> space (Pi\<^sub>M I M)"
  1.1074 -    let ?f = "\<lambda>y. f (x(i := y))"
  1.1075 -    show "?f \<in> borel_measurable (M i)"
  1.1076 -      using measurable_comp[OF measurable_component_update f, OF x \<open>i \<notin> I\<close>]
  1.1077 -      unfolding comp_def .
  1.1078 -    show "(\<integral>\<^sup>+ y. f (merge I {i} (x, y)) \<partial>Pi\<^sub>M {i} M) = (\<integral>\<^sup>+ y. f (x(i := y i)) \<partial>Pi\<^sub>M {i} M)"
  1.1079 -      using x
  1.1080 -      by (auto intro!: nn_integral_cong arg_cong[where f=f]
  1.1081 -               simp add: space_PiM extensional_def PiE_def)
  1.1082 -  qed
  1.1083 -qed
  1.1084 -
  1.1085 -lemma (in product_sigma_finite) product_nn_integral_insert_rev:
  1.1086 -  assumes I[simp]: "finite I" "i \<notin> I"
  1.1087 -    and [measurable]: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  1.1088 -  shows "integral\<^sup>N (Pi\<^sub>M (insert i I) M) f = (\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x(i := y)) \<partial>(Pi\<^sub>M I M)) \<partial>(M i))"
  1.1089 -  apply (subst product_nn_integral_insert[OF assms])
  1.1090 -  apply (rule pair_sigma_finite.Fubini')
  1.1091 -  apply intro_locales []
  1.1092 -  apply (rule sigma_finite[OF I(1)])
  1.1093 -  apply measurable
  1.1094 -  done
  1.1095 -
  1.1096 -lemma (in product_sigma_finite) product_nn_integral_setprod:
  1.1097 -  assumes "finite I" "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  1.1098 -  shows "(\<integral>\<^sup>+ x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^sub>M I M) = (\<Prod>i\<in>I. integral\<^sup>N (M i) (f i))"
  1.1099 -using assms proof (induction I)
  1.1100 -  case (insert i I)
  1.1101 -  note insert.prems[measurable]
  1.1102 -  note \<open>finite I\<close>[intro, simp]
  1.1103 -  interpret I: finite_product_sigma_finite M I by standard auto
  1.1104 -  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))"
  1.1105 -    using insert by (auto intro!: setprod.cong)
  1.1106 -  have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow> (\<lambda>x. (\<Prod>i\<in>J. f i (x i))) \<in> borel_measurable (Pi\<^sub>M J M)"
  1.1107 -    using sets.sets_into_space insert
  1.1108 -    by (intro borel_measurable_setprod_ennreal
  1.1109 -              measurable_comp[OF measurable_component_singleton, unfolded comp_def])
  1.1110 -       auto
  1.1111 -  then show ?case
  1.1112 -    apply (simp add: product_nn_integral_insert[OF insert(1,2)])
  1.1113 -    apply (simp add: insert(2-) * nn_integral_multc)
  1.1114 -    apply (subst nn_integral_cmult)
  1.1115 -    apply (auto simp add: insert(2-))
  1.1116 -    done
  1.1117 -qed (simp add: space_PiM)
  1.1118 -
  1.1119 -lemma (in product_sigma_finite) product_nn_integral_pair:
  1.1120 -  assumes [measurable]: "case_prod f \<in> borel_measurable (M x \<Otimes>\<^sub>M M y)"
  1.1121 -  assumes xy: "x \<noteq> y"
  1.1122 -  shows "(\<integral>\<^sup>+\<sigma>. f (\<sigma> x) (\<sigma> y) \<partial>PiM {x, y} M) = (\<integral>\<^sup>+z. f (fst z) (snd z) \<partial>(M x \<Otimes>\<^sub>M M y))"
  1.1123 -proof-
  1.1124 -  interpret psm: pair_sigma_finite "M x" "M y"
  1.1125 -    unfolding pair_sigma_finite_def using sigma_finite_measures by simp_all
  1.1126 -  have "{x, y} = {y, x}" by auto
  1.1127 -  also have "(\<integral>\<^sup>+\<sigma>. f (\<sigma> x) (\<sigma> y) \<partial>PiM {y, x} M) = (\<integral>\<^sup>+y. \<integral>\<^sup>+\<sigma>. f (\<sigma> x) y \<partial>PiM {x} M \<partial>M y)"
  1.1128 -    using xy by (subst product_nn_integral_insert_rev) simp_all
  1.1129 -  also have "... = (\<integral>\<^sup>+y. \<integral>\<^sup>+x. f x y \<partial>M x \<partial>M y)"
  1.1130 -    by (intro nn_integral_cong, subst product_nn_integral_singleton) simp_all
  1.1131 -  also have "... = (\<integral>\<^sup>+z. f (fst z) (snd z) \<partial>(M x \<Otimes>\<^sub>M M y))"
  1.1132 -    by (subst psm.nn_integral_snd[symmetric]) simp_all
  1.1133 -  finally show ?thesis .
  1.1134 -qed
  1.1135 -
  1.1136 -lemma (in product_sigma_finite) distr_component:
  1.1137 -  "distr (M i) (Pi\<^sub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^sub>M {i} M" (is "?D = ?P")
  1.1138 -proof (intro PiM_eqI)
  1.1139 -  fix A assume A: "\<And>ia. ia \<in> {i} \<Longrightarrow> A ia \<in> sets (M ia)"
  1.1140 -  then have "(\<lambda>x. \<lambda>i\<in>{i}. x) -` Pi\<^sub>E {i} A \<inter> space (M i) = A i"
  1.1141 -    by (auto dest: sets.sets_into_space)
  1.1142 -  with A show "emeasure (distr (M i) (Pi\<^sub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x)) (Pi\<^sub>E {i} A) = (\<Prod>i\<in>{i}. emeasure (M i) (A i))"
  1.1143 -    by (subst emeasure_distr) (auto intro!: sets_PiM_I_finite measurable_restrict)
  1.1144 -qed simp_all
  1.1145 -
  1.1146 -lemma (in product_sigma_finite)
  1.1147 -  assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^sub>M (I \<union> J) M)"
  1.1148 -  shows emeasure_fold_integral:
  1.1149 -    "emeasure (Pi\<^sub>M (I \<union> J) M) A = (\<integral>\<^sup>+x. emeasure (Pi\<^sub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^sub>M J M)) \<partial>Pi\<^sub>M I M)" (is ?I)
  1.1150 -    and emeasure_fold_measurable:
  1.1151 -    "(\<lambda>x. emeasure (Pi\<^sub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^sub>M J M))) \<in> borel_measurable (Pi\<^sub>M I M)" (is ?B)
  1.1152 -proof -
  1.1153 -  interpret I: finite_product_sigma_finite M I by standard fact
  1.1154 -  interpret J: finite_product_sigma_finite M J by standard fact
  1.1155 -  interpret IJ: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" ..
  1.1156 -  have merge: "merge I J -` A \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) \<in> sets (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
  1.1157 -    by (intro measurable_sets[OF _ A] measurable_merge assms)
  1.1158 -
  1.1159 -  show ?I
  1.1160 -    apply (subst distr_merge[symmetric, OF IJ])
  1.1161 -    apply (subst emeasure_distr[OF measurable_merge A])
  1.1162 -    apply (subst J.emeasure_pair_measure_alt[OF merge])
  1.1163 -    apply (auto intro!: nn_integral_cong arg_cong2[where f=emeasure] simp: space_pair_measure)
  1.1164 -    done
  1.1165 -
  1.1166 -  show ?B
  1.1167 -    using IJ.measurable_emeasure_Pair1[OF merge]
  1.1168 -    by (simp add: vimage_comp comp_def space_pair_measure cong: measurable_cong)
  1.1169 -qed
  1.1170 -
  1.1171 -lemma sets_Collect_single:
  1.1172 -  "i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> { x \<in> space (Pi\<^sub>M I M). x i \<in> A } \<in> sets (Pi\<^sub>M I M)"
  1.1173 -  by simp
  1.1174 -
  1.1175 -lemma pair_measure_eq_distr_PiM:
  1.1176 -  fixes M1 :: "'a measure" and M2 :: "'a measure"
  1.1177 -  assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
  1.1178 -  shows "(M1 \<Otimes>\<^sub>M M2) = distr (Pi\<^sub>M UNIV (case_bool M1 M2)) (M1 \<Otimes>\<^sub>M M2) (\<lambda>x. (x True, x False))"
  1.1179 -    (is "?P = ?D")
  1.1180 -proof (rule pair_measure_eqI[OF assms])
  1.1181 -  interpret B: product_sigma_finite "case_bool M1 M2"
  1.1182 -    unfolding product_sigma_finite_def using assms by (auto split: bool.split)
  1.1183 -  let ?B = "Pi\<^sub>M UNIV (case_bool M1 M2)"
  1.1184 -
  1.1185 -  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)"
  1.1186 -    by auto
  1.1187 -  fix A B assume A: "A \<in> sets M1" and B: "B \<in> sets M2"
  1.1188 -  have "emeasure M1 A * emeasure M2 B = (\<Prod> i\<in>UNIV. emeasure (case_bool M1 M2 i) (case_bool A B i))"
  1.1189 -    by (simp add: UNIV_bool ac_simps)
  1.1190 -  also have "\<dots> = emeasure ?B (Pi\<^sub>E UNIV (case_bool A B))"
  1.1191 -    using A B by (subst B.emeasure_PiM) (auto split: bool.split)
  1.1192 -  also have "Pi\<^sub>E UNIV (case_bool A B) = (\<lambda>x. (x True, x False)) -` (A \<times> B) \<inter> space ?B"
  1.1193 -    using A[THEN sets.sets_into_space] B[THEN sets.sets_into_space]
  1.1194 -    by (auto simp: PiE_iff all_bool_eq space_PiM split: bool.split)
  1.1195 -  finally show "emeasure M1 A * emeasure M2 B = emeasure ?D (A \<times> B)"
  1.1196 -    using A B
  1.1197 -      measurable_component_singleton[of True UNIV "case_bool M1 M2"]
  1.1198 -      measurable_component_singleton[of False UNIV "case_bool M1 M2"]
  1.1199 -    by (subst emeasure_distr) (auto simp: measurable_pair_iff)
  1.1200 -qed simp
  1.1201 -
  1.1202 -end