src/HOL/Probability/Finite_Product_Measure.thy
changeset 42146 5b52c6a9c627
parent 42067 66c8281349ec
child 42950 6e5c2a3c69da
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/HOL/Probability/Finite_Product_Measure.thy	Tue Mar 29 14:27:39 2011 +0200
     1.3 @@ -0,0 +1,1014 @@
     1.4 +(*  Title:      HOL/Probability/Finite_Product_Measure.thy
     1.5 +    Author:     Johannes Hölzl, TU München
     1.6 +*)
     1.7 +
     1.8 +header {*Finite product measures*}
     1.9 +
    1.10 +theory Finite_Product_Measure
    1.11 +imports Binary_Product_Measure
    1.12 +begin
    1.13 +
    1.14 +lemma Pi_iff: "f \<in> Pi I X \<longleftrightarrow> (\<forall>i\<in>I. f i \<in> X i)"
    1.15 +  unfolding Pi_def by auto
    1.16 +
    1.17 +abbreviation
    1.18 +  "Pi\<^isub>E A B \<equiv> Pi A B \<inter> extensional A"
    1.19 +
    1.20 +syntax
    1.21 +  "_PiE"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PIE _:_./ _)" 10)
    1.22 +
    1.23 +syntax (xsymbols)
    1.24 +  "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
    1.25 +
    1.26 +syntax (HTML output)
    1.27 +  "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
    1.28 +
    1.29 +translations
    1.30 +  "PIE x:A. B" == "CONST Pi\<^isub>E A (%x. B)"
    1.31 +
    1.32 +abbreviation
    1.33 +  funcset_extensional :: "['a set, 'b set] => ('a => 'b) set"
    1.34 +    (infixr "->\<^isub>E" 60) where
    1.35 +  "A ->\<^isub>E B \<equiv> Pi\<^isub>E A (%_. B)"
    1.36 +
    1.37 +notation (xsymbols)
    1.38 +  funcset_extensional  (infixr "\<rightarrow>\<^isub>E" 60)
    1.39 +
    1.40 +lemma extensional_empty[simp]: "extensional {} = {\<lambda>x. undefined}"
    1.41 +  by safe (auto simp add: extensional_def fun_eq_iff)
    1.42 +
    1.43 +lemma extensional_insert[intro, simp]:
    1.44 +  assumes "a \<in> extensional (insert i I)"
    1.45 +  shows "a(i := b) \<in> extensional (insert i I)"
    1.46 +  using assms unfolding extensional_def by auto
    1.47 +
    1.48 +lemma extensional_Int[simp]:
    1.49 +  "extensional I \<inter> extensional I' = extensional (I \<inter> I')"
    1.50 +  unfolding extensional_def by auto
    1.51 +
    1.52 +definition
    1.53 +  "merge I x J y = (\<lambda>i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
    1.54 +
    1.55 +lemma merge_apply[simp]:
    1.56 +  "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    1.57 +  "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    1.58 +  "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    1.59 +  "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    1.60 +  "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I x J y i = undefined"
    1.61 +  unfolding merge_def by auto
    1.62 +
    1.63 +lemma merge_commute:
    1.64 +  "I \<inter> J = {} \<Longrightarrow> merge I x J y = merge J y I x"
    1.65 +  by (auto simp: merge_def intro!: ext)
    1.66 +
    1.67 +lemma Pi_cancel_merge_range[simp]:
    1.68 +  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    1.69 +  "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    1.70 +  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    1.71 +  "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    1.72 +  by (auto simp: Pi_def)
    1.73 +
    1.74 +lemma Pi_cancel_merge[simp]:
    1.75 +  "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    1.76 +  "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    1.77 +  "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    1.78 +  "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    1.79 +  by (auto simp: Pi_def)
    1.80 +
    1.81 +lemma extensional_merge[simp]: "merge I x J y \<in> extensional (I \<union> J)"
    1.82 +  by (auto simp: extensional_def)
    1.83 +
    1.84 +lemma restrict_Pi_cancel: "restrict x I \<in> Pi I A \<longleftrightarrow> x \<in> Pi I A"
    1.85 +  by (auto simp: restrict_def Pi_def)
    1.86 +
    1.87 +lemma restrict_merge[simp]:
    1.88 +  "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    1.89 +  "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    1.90 +  "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    1.91 +  "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    1.92 +  by (auto simp: restrict_def intro!: ext)
    1.93 +
    1.94 +lemma extensional_insert_undefined[intro, simp]:
    1.95 +  assumes "a \<in> extensional (insert i I)"
    1.96 +  shows "a(i := undefined) \<in> extensional I"
    1.97 +  using assms unfolding extensional_def by auto
    1.98 +
    1.99 +lemma extensional_insert_cancel[intro, simp]:
   1.100 +  assumes "a \<in> extensional I"
   1.101 +  shows "a \<in> extensional (insert i I)"
   1.102 +  using assms unfolding extensional_def by auto
   1.103 +
   1.104 +lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I x {i} y = restrict (x(i := y i)) (insert i I)"
   1.105 +  unfolding merge_def by (auto simp: fun_eq_iff)
   1.106 +
   1.107 +lemma Pi_Int: "Pi I E \<inter> Pi I F = (\<Pi> i\<in>I. E i \<inter> F i)"
   1.108 +  by auto
   1.109 +
   1.110 +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)"
   1.111 +  by auto
   1.112 +
   1.113 +lemma Pi_cancel_fupd_range[simp]: "i \<notin> I \<Longrightarrow> x \<in> Pi I (B(i := b)) \<longleftrightarrow> x \<in> Pi I B"
   1.114 +  by (auto simp: Pi_def)
   1.115 +
   1.116 +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"
   1.117 +  by (auto simp: Pi_def)
   1.118 +
   1.119 +lemma Pi_split_domain[simp]: "x \<in> Pi (I \<union> J) X \<longleftrightarrow> x \<in> Pi I X \<and> x \<in> Pi J X"
   1.120 +  by (auto simp: Pi_def)
   1.121 +
   1.122 +lemma Pi_cancel_fupd[simp]: "i \<notin> I \<Longrightarrow> x(i := a) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
   1.123 +  by (auto simp: Pi_def)
   1.124 +
   1.125 +lemma restrict_vimage:
   1.126 +  assumes "I \<inter> J = {}"
   1.127 +  shows "(\<lambda>x. (restrict x I, restrict x J)) -` (Pi\<^isub>E I E \<times> Pi\<^isub>E J F) = Pi (I \<union> J) (merge I E J F)"
   1.128 +  using assms by (auto simp: restrict_Pi_cancel)
   1.129 +
   1.130 +lemma merge_vimage:
   1.131 +  assumes "I \<inter> J = {}"
   1.132 +  shows "(\<lambda>(x,y). merge I x J y) -` Pi\<^isub>E (I \<union> J) E = Pi I E \<times> Pi J E"
   1.133 +  using assms by (auto simp: restrict_Pi_cancel)
   1.134 +
   1.135 +lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
   1.136 +  by (auto simp: restrict_def intro!: ext)
   1.137 +
   1.138 +lemma merge_restrict[simp]:
   1.139 +  "merge I (restrict x I) J y = merge I x J y"
   1.140 +  "merge I x J (restrict y J) = merge I x J y"
   1.141 +  unfolding merge_def by (auto intro!: ext)
   1.142 +
   1.143 +lemma merge_x_x_eq_restrict[simp]:
   1.144 +  "merge I x J x = restrict x (I \<union> J)"
   1.145 +  unfolding merge_def by (auto intro!: ext)
   1.146 +
   1.147 +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"
   1.148 +  apply auto
   1.149 +  apply (drule_tac x=x in Pi_mem)
   1.150 +  apply (simp_all split: split_if_asm)
   1.151 +  apply (drule_tac x=i in Pi_mem)
   1.152 +  apply (auto dest!: Pi_mem)
   1.153 +  done
   1.154 +
   1.155 +lemma Pi_UN:
   1.156 +  fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
   1.157 +  assumes "finite I" and mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
   1.158 +  shows "(\<Union>n. Pi I (A n)) = (\<Pi> i\<in>I. \<Union>n. A n i)"
   1.159 +proof (intro set_eqI iffI)
   1.160 +  fix f assume "f \<in> (\<Pi> i\<in>I. \<Union>n. A n i)"
   1.161 +  then have "\<forall>i\<in>I. \<exists>n. f i \<in> A n i" by auto
   1.162 +  from bchoice[OF this] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
   1.163 +  obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
   1.164 +    using `finite I` finite_nat_set_iff_bounded_le[of "n`I"] by auto
   1.165 +  have "f \<in> Pi I (A k)"
   1.166 +  proof (intro Pi_I)
   1.167 +    fix i assume "i \<in> I"
   1.168 +    from mono[OF this, of "n i" k] k[OF this] n[OF this]
   1.169 +    show "f i \<in> A k i" by auto
   1.170 +  qed
   1.171 +  then show "f \<in> (\<Union>n. Pi I (A n))" by auto
   1.172 +qed auto
   1.173 +
   1.174 +lemma PiE_cong:
   1.175 +  assumes "\<And>i. i\<in>I \<Longrightarrow> A i = B i"
   1.176 +  shows "Pi\<^isub>E I A = Pi\<^isub>E I B"
   1.177 +  using assms by (auto intro!: Pi_cong)
   1.178 +
   1.179 +lemma restrict_upd[simp]:
   1.180 +  "i \<notin> I \<Longrightarrow> (restrict f I)(i := y) = restrict (f(i := y)) (insert i I)"
   1.181 +  by (auto simp: fun_eq_iff)
   1.182 +
   1.183 +lemma Pi_eq_subset:
   1.184 +  assumes ne: "\<And>i. i \<in> I \<Longrightarrow> F i \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> F' i \<noteq> {}"
   1.185 +  assumes eq: "Pi\<^isub>E I F = Pi\<^isub>E I F'" and "i \<in> I"
   1.186 +  shows "F i \<subseteq> F' i"
   1.187 +proof
   1.188 +  fix x assume "x \<in> F i"
   1.189 +  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
   1.190 +  from choice[OF this] guess f .. note f = this
   1.191 +  then have "f \<in> Pi\<^isub>E I F" by (auto simp: extensional_def)
   1.192 +  then have "f \<in> Pi\<^isub>E I F'" using assms by simp
   1.193 +  then show "x \<in> F' i" using f `i \<in> I` by auto
   1.194 +qed
   1.195 +
   1.196 +lemma Pi_eq_iff_not_empty:
   1.197 +  assumes ne: "\<And>i. i \<in> I \<Longrightarrow> F i \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> F' i \<noteq> {}"
   1.198 +  shows "Pi\<^isub>E I F = Pi\<^isub>E I F' \<longleftrightarrow> (\<forall>i\<in>I. F i = F' i)"
   1.199 +proof (intro iffI ballI)
   1.200 +  fix i assume eq: "Pi\<^isub>E I F = Pi\<^isub>E I F'" and i: "i \<in> I"
   1.201 +  show "F i = F' i"
   1.202 +    using Pi_eq_subset[of I F F', OF ne eq i]
   1.203 +    using Pi_eq_subset[of I F' F, OF ne(2,1) eq[symmetric] i]
   1.204 +    by auto
   1.205 +qed auto
   1.206 +
   1.207 +lemma Pi_eq_empty_iff:
   1.208 +  "Pi\<^isub>E I F = {} \<longleftrightarrow> (\<exists>i\<in>I. F i = {})"
   1.209 +proof
   1.210 +  assume "Pi\<^isub>E I F = {}"
   1.211 +  show "\<exists>i\<in>I. F i = {}"
   1.212 +  proof (rule ccontr)
   1.213 +    assume "\<not> ?thesis"
   1.214 +    then have "\<forall>i. \<exists>y. (i \<in> I \<longrightarrow> y \<in> F i) \<and> (i \<notin> I \<longrightarrow> y = undefined)" by auto
   1.215 +    from choice[OF this] guess f ..
   1.216 +    then have "f \<in> Pi\<^isub>E I F" by (auto simp: extensional_def)
   1.217 +    with `Pi\<^isub>E I F = {}` show False by auto
   1.218 +  qed
   1.219 +qed auto
   1.220 +
   1.221 +lemma Pi_eq_iff:
   1.222 +  "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 = {}))"
   1.223 +proof (intro iffI disjCI)
   1.224 +  assume eq[simp]: "Pi\<^isub>E I F = Pi\<^isub>E I F'"
   1.225 +  assume "\<not> ((\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {}))"
   1.226 +  then have "(\<forall>i\<in>I. F i \<noteq> {}) \<and> (\<forall>i\<in>I. F' i \<noteq> {})"
   1.227 +    using Pi_eq_empty_iff[of I F] Pi_eq_empty_iff[of I F'] by auto
   1.228 +  with Pi_eq_iff_not_empty[of I F F'] show "\<forall>i\<in>I. F i = F' i" by auto
   1.229 +next
   1.230 +  assume "(\<forall>i\<in>I. F i = F' i) \<or> (\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {})"
   1.231 +  then show "Pi\<^isub>E I F = Pi\<^isub>E I F'"
   1.232 +    using Pi_eq_empty_iff[of I F] Pi_eq_empty_iff[of I F'] by auto
   1.233 +qed
   1.234 +
   1.235 +section "Finite product spaces"
   1.236 +
   1.237 +section "Products"
   1.238 +
   1.239 +locale product_sigma_algebra =
   1.240 +  fixes M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme"
   1.241 +  assumes sigma_algebras: "\<And>i. sigma_algebra (M i)"
   1.242 +
   1.243 +locale finite_product_sigma_algebra = product_sigma_algebra M
   1.244 +  for M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme" +
   1.245 +  fixes I :: "'i set"
   1.246 +  assumes finite_index: "finite I"
   1.247 +
   1.248 +definition
   1.249 +  "product_algebra_generator I M = \<lparr> space = (\<Pi>\<^isub>E i \<in> I. space (M i)),
   1.250 +    sets = Pi\<^isub>E I ` (\<Pi> i \<in> I. sets (M i)),
   1.251 +    measure = \<lambda>A. (\<Prod>i\<in>I. measure (M i) ((SOME F. A = Pi\<^isub>E I F) i)) \<rparr>"
   1.252 +
   1.253 +definition product_algebra_def:
   1.254 +  "Pi\<^isub>M I M = sigma (product_algebra_generator I M)
   1.255 +    \<lparr> measure := (SOME \<mu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<mu> \<rparr>) \<and>
   1.256 +      (\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<mu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. measure (M i) (A i))))\<rparr>"
   1.257 +
   1.258 +syntax
   1.259 +  "_PiM"  :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   1.260 +              ('i => 'a, 'b) measure_space_scheme"  ("(3PIM _:_./ _)" 10)
   1.261 +
   1.262 +syntax (xsymbols)
   1.263 +  "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   1.264 +             ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
   1.265 +
   1.266 +syntax (HTML output)
   1.267 +  "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   1.268 +             ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
   1.269 +
   1.270 +translations
   1.271 +  "PIM x:I. M" == "CONST Pi\<^isub>M I (%x. M)"
   1.272 +
   1.273 +abbreviation (in finite_product_sigma_algebra) "G \<equiv> product_algebra_generator I M"
   1.274 +abbreviation (in finite_product_sigma_algebra) "P \<equiv> Pi\<^isub>M I M"
   1.275 +
   1.276 +sublocale product_sigma_algebra \<subseteq> M: sigma_algebra "M i" for i by (rule sigma_algebras)
   1.277 +
   1.278 +lemma sigma_into_space:
   1.279 +  assumes "sets M \<subseteq> Pow (space M)"
   1.280 +  shows "sets (sigma M) \<subseteq> Pow (space M)"
   1.281 +  using sigma_sets_into_sp[OF assms] unfolding sigma_def by auto
   1.282 +
   1.283 +lemma (in product_sigma_algebra) product_algebra_generator_into_space:
   1.284 +  "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
   1.285 +  using M.sets_into_space unfolding product_algebra_generator_def
   1.286 +  by auto blast
   1.287 +
   1.288 +lemma (in product_sigma_algebra) product_algebra_into_space:
   1.289 +  "sets (Pi\<^isub>M I M) \<subseteq> Pow (space (Pi\<^isub>M I M))"
   1.290 +  using product_algebra_generator_into_space
   1.291 +  by (auto intro!: sigma_into_space simp add: product_algebra_def)
   1.292 +
   1.293 +lemma (in product_sigma_algebra) sigma_algebra_product_algebra: "sigma_algebra (Pi\<^isub>M I M)"
   1.294 +  using product_algebra_generator_into_space unfolding product_algebra_def
   1.295 +  by (rule sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) simp_all
   1.296 +
   1.297 +sublocale finite_product_sigma_algebra \<subseteq> sigma_algebra P
   1.298 +  using sigma_algebra_product_algebra .
   1.299 +
   1.300 +lemma product_algebraE:
   1.301 +  assumes "A \<in> sets (product_algebra_generator I M)"
   1.302 +  obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   1.303 +  using assms unfolding product_algebra_generator_def by auto
   1.304 +
   1.305 +lemma product_algebra_generatorI[intro]:
   1.306 +  assumes "E \<in> (\<Pi> i\<in>I. sets (M i))"
   1.307 +  shows "Pi\<^isub>E I E \<in> sets (product_algebra_generator I M)"
   1.308 +  using assms unfolding product_algebra_generator_def by auto
   1.309 +
   1.310 +lemma space_product_algebra_generator[simp]:
   1.311 +  "space (product_algebra_generator I M) = Pi\<^isub>E I (\<lambda>i. space (M i))"
   1.312 +  unfolding product_algebra_generator_def by simp
   1.313 +
   1.314 +lemma space_product_algebra[simp]:
   1.315 +  "space (Pi\<^isub>M I M) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   1.316 +  unfolding product_algebra_def product_algebra_generator_def by simp
   1.317 +
   1.318 +lemma sets_product_algebra:
   1.319 +  "sets (Pi\<^isub>M I M) = sets (sigma (product_algebra_generator I M))"
   1.320 +  unfolding product_algebra_def sigma_def by simp
   1.321 +
   1.322 +lemma product_algebra_generator_sets_into_space:
   1.323 +  assumes "\<And>i. i\<in>I \<Longrightarrow> sets (M i) \<subseteq> Pow (space (M i))"
   1.324 +  shows "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
   1.325 +  using assms by (auto simp: product_algebra_generator_def) blast
   1.326 +
   1.327 +lemma (in finite_product_sigma_algebra) in_P[simp, intro]:
   1.328 +  "\<lbrakk> \<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i) \<rbrakk> \<Longrightarrow> Pi\<^isub>E I A \<in> sets P"
   1.329 +  by (auto simp: sets_product_algebra)
   1.330 +
   1.331 +section "Generating set generates also product algebra"
   1.332 +
   1.333 +lemma sigma_product_algebra_sigma_eq:
   1.334 +  assumes "finite I"
   1.335 +  assumes mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
   1.336 +  assumes union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (E i)"
   1.337 +  assumes sets_into: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> sets (E i)"
   1.338 +  and E: "\<And>i. sets (E i) \<subseteq> Pow (space (E i))"
   1.339 +  shows "sets (\<Pi>\<^isub>M i\<in>I. sigma (E i)) = sets (\<Pi>\<^isub>M i\<in>I. E i)"
   1.340 +    (is "sets ?S = sets ?E")
   1.341 +proof cases
   1.342 +  assume "I = {}" then show ?thesis
   1.343 +    by (simp add: product_algebra_def product_algebra_generator_def)
   1.344 +next
   1.345 +  assume "I \<noteq> {}"
   1.346 +  interpret E: sigma_algebra "sigma (E i)" for i
   1.347 +    using E by (rule sigma_algebra_sigma)
   1.348 +  have into_space[intro]: "\<And>i x A. A \<in> sets (E i) \<Longrightarrow> x i \<in> A \<Longrightarrow> x i \<in> space (E i)"
   1.349 +    using E by auto
   1.350 +  interpret G: sigma_algebra ?E
   1.351 +    unfolding product_algebra_def product_algebra_generator_def using E
   1.352 +    by (intro sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) (auto dest: Pi_mem)
   1.353 +  { fix A i assume "i \<in> I" and A: "A \<in> sets (E i)"
   1.354 +    then have "(\<lambda>x. x i) -` A \<inter> space ?E = (\<Pi>\<^isub>E j\<in>I. if j = i then A else \<Union>n. S j n) \<inter> space ?E"
   1.355 +      using mono union unfolding incseq_Suc_iff space_product_algebra
   1.356 +      by (auto dest: Pi_mem)
   1.357 +    also have "\<dots> = (\<Union>n. (\<Pi>\<^isub>E j\<in>I. if j = i then A else S j n))"
   1.358 +      unfolding space_product_algebra
   1.359 +      apply simp
   1.360 +      apply (subst Pi_UN[OF `finite I`])
   1.361 +      using mono[THEN incseqD] apply simp
   1.362 +      apply (simp add: PiE_Int)
   1.363 +      apply (intro PiE_cong)
   1.364 +      using A sets_into by (auto intro!: into_space)
   1.365 +    also have "\<dots> \<in> sets ?E"
   1.366 +      using sets_into `A \<in> sets (E i)`
   1.367 +      unfolding sets_product_algebra sets_sigma
   1.368 +      by (intro sigma_sets.Union)
   1.369 +         (auto simp: image_subset_iff intro!: sigma_sets.Basic)
   1.370 +    finally have "(\<lambda>x. x i) -` A \<inter> space ?E \<in> sets ?E" . }
   1.371 +  then have proj:
   1.372 +    "\<And>i. i\<in>I \<Longrightarrow> (\<lambda>x. x i) \<in> measurable ?E (sigma (E i))"
   1.373 +    using E by (subst G.measurable_iff_sigma)
   1.374 +               (auto simp: sets_product_algebra sets_sigma)
   1.375 +  { fix A assume A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (sigma (E i))"
   1.376 +    with proj have basic: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) -` (A i) \<inter> space ?E \<in> sets ?E"
   1.377 +      unfolding measurable_def by simp
   1.378 +    have "Pi\<^isub>E I A = (\<Inter>i\<in>I. (\<lambda>x. x i) -` (A i) \<inter> space ?E)"
   1.379 +      using A E.sets_into_space `I \<noteq> {}` unfolding product_algebra_def by auto blast
   1.380 +    then have "Pi\<^isub>E I A \<in> sets ?E"
   1.381 +      using G.finite_INT[OF `finite I` `I \<noteq> {}` basic, of "\<lambda>i. i"] by simp }
   1.382 +  then have "sigma_sets (space ?E) (sets (product_algebra_generator I (\<lambda>i. sigma (E i)))) \<subseteq> sets ?E"
   1.383 +    by (intro G.sigma_sets_subset) (auto simp add: product_algebra_generator_def)
   1.384 +  then have subset: "sets ?S \<subseteq> sets ?E"
   1.385 +    by (simp add: sets_sigma sets_product_algebra)
   1.386 +  show "sets ?S = sets ?E"
   1.387 +  proof (intro set_eqI iffI)
   1.388 +    fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
   1.389 +      unfolding sets_sigma sets_product_algebra
   1.390 +    proof induct
   1.391 +      case (Basic A) then show ?case
   1.392 +        by (auto simp: sets_sigma product_algebra_generator_def intro: sigma_sets.Basic)
   1.393 +    qed (auto intro: sigma_sets.intros simp: product_algebra_generator_def)
   1.394 +  next
   1.395 +    fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
   1.396 +  qed
   1.397 +qed
   1.398 +
   1.399 +lemma product_algebraI[intro]:
   1.400 +    "E \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> Pi\<^isub>E I E \<in> sets (Pi\<^isub>M I M)"
   1.401 +  using assms unfolding product_algebra_def by (auto intro: product_algebra_generatorI)
   1.402 +
   1.403 +lemma (in product_sigma_algebra) measurable_component_update:
   1.404 +  assumes "x \<in> space (Pi\<^isub>M I M)" and "i \<notin> I"
   1.405 +  shows "(\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)" (is "?f \<in> _")
   1.406 +  unfolding product_algebra_def apply simp
   1.407 +proof (intro measurable_sigma)
   1.408 +  let ?G = "product_algebra_generator (insert i I) M"
   1.409 +  show "sets ?G \<subseteq> Pow (space ?G)" using product_algebra_generator_into_space .
   1.410 +  show "?f \<in> space (M i) \<rightarrow> space ?G"
   1.411 +    using M.sets_into_space assms by auto
   1.412 +  fix A assume "A \<in> sets ?G"
   1.413 +  from product_algebraE[OF this] guess E . note E = this
   1.414 +  then have "?f -` A \<inter> space (M i) = E i \<or> ?f -` A \<inter> space (M i) = {}"
   1.415 +    using M.sets_into_space assms by auto
   1.416 +  then show "?f -` A \<inter> space (M i) \<in> sets (M i)"
   1.417 +    using E by (auto intro!: product_algebraI)
   1.418 +qed
   1.419 +
   1.420 +lemma (in product_sigma_algebra) measurable_add_dim:
   1.421 +  assumes "i \<notin> I"
   1.422 +  shows "(\<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)"
   1.423 +proof -
   1.424 +  let ?f = "(\<lambda>(f, y). f(i := y))" and ?G = "product_algebra_generator (insert i I) M"
   1.425 +  interpret Ii: pair_sigma_algebra "Pi\<^isub>M I M" "M i"
   1.426 +    unfolding pair_sigma_algebra_def
   1.427 +    by (intro sigma_algebra_product_algebra sigma_algebras conjI)
   1.428 +  have "?f \<in> measurable Ii.P (sigma ?G)"
   1.429 +  proof (rule Ii.measurable_sigma)
   1.430 +    show "sets ?G \<subseteq> Pow (space ?G)"
   1.431 +      using product_algebra_generator_into_space .
   1.432 +    show "?f \<in> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) \<rightarrow> space ?G"
   1.433 +      by (auto simp: space_pair_measure)
   1.434 +  next
   1.435 +    fix A assume "A \<in> sets ?G"
   1.436 +    then obtain F where "A = Pi\<^isub>E (insert i I) F"
   1.437 +      and F: "\<And>j. j \<in> I \<Longrightarrow> F j \<in> sets (M j)" "F i \<in> sets (M i)"
   1.438 +      by (auto elim!: product_algebraE)
   1.439 +    then have "?f -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = Pi\<^isub>E I F \<times> (F i)"
   1.440 +      using sets_into_space `i \<notin> I`
   1.441 +      by (auto simp add: space_pair_measure) blast+
   1.442 +    then show "?f -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) \<in> sets (Pi\<^isub>M I M \<Otimes>\<^isub>M M i)"
   1.443 +      using F by (auto intro!: pair_measureI)
   1.444 +  qed
   1.445 +  then show ?thesis
   1.446 +    by (simp add: product_algebra_def)
   1.447 +qed
   1.448 +
   1.449 +lemma (in product_sigma_algebra) measurable_merge:
   1.450 +  assumes [simp]: "I \<inter> J = {}"
   1.451 +  shows "(\<lambda>(x, y). merge I x J y) \<in> measurable (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M)"
   1.452 +proof -
   1.453 +  let ?I = "Pi\<^isub>M I M" and ?J = "Pi\<^isub>M J M"
   1.454 +  interpret P: sigma_algebra "?I \<Otimes>\<^isub>M ?J"
   1.455 +    by (intro sigma_algebra_pair_measure product_algebra_into_space)
   1.456 +  let ?f = "\<lambda>(x, y). merge I x J y"
   1.457 +  let ?G = "product_algebra_generator (I \<union> J) M"
   1.458 +  have "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (sigma ?G)"
   1.459 +  proof (rule P.measurable_sigma)
   1.460 +    fix A assume "A \<in> sets ?G"
   1.461 +    from product_algebraE[OF this]
   1.462 +    obtain E where E: "A = Pi\<^isub>E (I \<union> J) E" "E \<in> (\<Pi> i\<in>I \<union> J. sets (M i))" .
   1.463 +    then have *: "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) = Pi\<^isub>E I E \<times> Pi\<^isub>E J E"
   1.464 +      using sets_into_space `I \<inter> J = {}`
   1.465 +      by (auto simp add: space_pair_measure) fast+
   1.466 +    then show "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) \<in> sets (?I \<Otimes>\<^isub>M ?J)"
   1.467 +      using E unfolding * by (auto intro!: pair_measureI in_sigma product_algebra_generatorI
   1.468 +        simp: product_algebra_def)
   1.469 +  qed (insert product_algebra_generator_into_space, auto simp: space_pair_measure)
   1.470 +  then show "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (Pi\<^isub>M (I \<union> J) M)"
   1.471 +    unfolding product_algebra_def[of "I \<union> J"] by simp
   1.472 +qed
   1.473 +
   1.474 +lemma (in product_sigma_algebra) measurable_component_singleton:
   1.475 +  assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>M I M) (M i)"
   1.476 +proof (unfold measurable_def, intro CollectI conjI ballI)
   1.477 +  fix A assume "A \<in> sets (M i)"
   1.478 +  then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) = (\<Pi>\<^isub>E j\<in>I. if i = j then A else space (M j))"
   1.479 +    using M.sets_into_space `i \<in> I` by (fastsimp dest: Pi_mem split: split_if_asm)
   1.480 +  then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) \<in> sets (Pi\<^isub>M I M)"
   1.481 +    using `A \<in> sets (M i)` by (auto intro!: product_algebraI)
   1.482 +qed (insert `i \<in> I`, auto)
   1.483 +
   1.484 +locale product_sigma_finite =
   1.485 +  fixes M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme"
   1.486 +  assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
   1.487 +
   1.488 +locale finite_product_sigma_finite = product_sigma_finite M
   1.489 +  for M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme" +
   1.490 +  fixes I :: "'i set" assumes finite_index'[intro]: "finite I"
   1.491 +
   1.492 +sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" for i
   1.493 +  by (rule sigma_finite_measures)
   1.494 +
   1.495 +sublocale product_sigma_finite \<subseteq> product_sigma_algebra
   1.496 +  by default
   1.497 +
   1.498 +sublocale finite_product_sigma_finite \<subseteq> finite_product_sigma_algebra
   1.499 +  by default (fact finite_index')
   1.500 +
   1.501 +lemma setprod_extreal_0:
   1.502 +  fixes f :: "'a \<Rightarrow> extreal"
   1.503 +  shows "(\<Prod>i\<in>A. f i) = 0 \<longleftrightarrow> (finite A \<and> (\<exists>i\<in>A. f i = 0))"
   1.504 +proof cases
   1.505 +  assume "finite A"
   1.506 +  then show ?thesis by (induct A) auto
   1.507 +qed auto
   1.508 +
   1.509 +lemma setprod_extreal_pos:
   1.510 +  fixes f :: "'a \<Rightarrow> extreal" assumes pos: "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i" shows "0 \<le> (\<Prod>i\<in>I. f i)"
   1.511 +proof cases
   1.512 +  assume "finite I" from this pos show ?thesis by induct auto
   1.513 +qed simp
   1.514 +
   1.515 +lemma setprod_PInf:
   1.516 +  assumes "\<And>i. i \<in> I \<Longrightarrow> 0 \<le> f i"
   1.517 +  shows "(\<Prod>i\<in>I. f i) = \<infinity> \<longleftrightarrow> finite I \<and> (\<exists>i\<in>I. f i = \<infinity>) \<and> (\<forall>i\<in>I. f i \<noteq> 0)"
   1.518 +proof cases
   1.519 +  assume "finite I" from this assms show ?thesis
   1.520 +  proof (induct I)
   1.521 +    case (insert i I)
   1.522 +    then have pos: "0 \<le> f i" "0 \<le> setprod f I" by (auto intro!: setprod_extreal_pos)
   1.523 +    from insert have "(\<Prod>j\<in>insert i I. f j) = \<infinity> \<longleftrightarrow> setprod f I * f i = \<infinity>" by auto
   1.524 +    also have "\<dots> \<longleftrightarrow> (setprod f I = \<infinity> \<or> f i = \<infinity>) \<and> f i \<noteq> 0 \<and> setprod f I \<noteq> 0"
   1.525 +      using setprod_extreal_pos[of I f] pos
   1.526 +      by (cases rule: extreal2_cases[of "f i" "setprod f I"]) auto
   1.527 +    also have "\<dots> \<longleftrightarrow> finite (insert i I) \<and> (\<exists>j\<in>insert i I. f j = \<infinity>) \<and> (\<forall>j\<in>insert i I. f j \<noteq> 0)"
   1.528 +      using insert by (auto simp: setprod_extreal_0)
   1.529 +    finally show ?case .
   1.530 +  qed simp
   1.531 +qed simp
   1.532 +
   1.533 +lemma setprod_extreal: "(\<Prod>i\<in>A. extreal (f i)) = extreal (setprod f A)"
   1.534 +proof cases
   1.535 +  assume "finite A" then show ?thesis
   1.536 +    by induct (auto simp: one_extreal_def)
   1.537 +qed (simp add: one_extreal_def)
   1.538 +
   1.539 +lemma (in finite_product_sigma_finite) product_algebra_generator_measure:
   1.540 +  assumes "Pi\<^isub>E I F \<in> sets G"
   1.541 +  shows "measure G (Pi\<^isub>E I F) = (\<Prod>i\<in>I. M.\<mu> i (F i))"
   1.542 +proof cases
   1.543 +  assume ne: "\<forall>i\<in>I. F i \<noteq> {}"
   1.544 +  have "\<forall>i\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') i = F i"
   1.545 +    by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
   1.546 +       (insert ne, auto simp: Pi_eq_iff)
   1.547 +  then show ?thesis
   1.548 +    unfolding product_algebra_generator_def by simp
   1.549 +next
   1.550 +  assume empty: "\<not> (\<forall>j\<in>I. F j \<noteq> {})"
   1.551 +  then have "(\<Prod>j\<in>I. M.\<mu> j (F j)) = 0"
   1.552 +    by (auto simp: setprod_extreal_0 intro!: bexI)
   1.553 +  moreover
   1.554 +  have "\<exists>j\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j = {}"
   1.555 +    by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
   1.556 +       (insert empty, auto simp: Pi_eq_empty_iff[symmetric])
   1.557 +  then have "(\<Prod>j\<in>I. M.\<mu> j ((SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j)) = 0"
   1.558 +    by (auto simp: setprod_extreal_0 intro!: bexI)
   1.559 +  ultimately show ?thesis
   1.560 +    unfolding product_algebra_generator_def by simp
   1.561 +qed
   1.562 +
   1.563 +lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   1.564 +  "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
   1.565 +    (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
   1.566 +    (\<forall>k. \<forall>i\<in>I. \<mu> i (F i k) \<noteq> \<infinity>) \<and> incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k) \<and>
   1.567 +    (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space G"
   1.568 +proof -
   1.569 +  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. \<mu> i (F k) \<noteq> \<infinity>)"
   1.570 +    using M.sigma_finite_up by simp
   1.571 +  from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   1.572 +  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. \<mu> i (F i k) \<noteq> \<infinity>"
   1.573 +    by auto
   1.574 +  let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
   1.575 +  note space_product_algebra[simp]
   1.576 +  show ?thesis
   1.577 +  proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
   1.578 +    fix i show "range (F i) \<subseteq> sets (M i)" by fact
   1.579 +  next
   1.580 +    fix i k show "\<mu> i (F i k) \<noteq> \<infinity>" by fact
   1.581 +  next
   1.582 +    fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space G"
   1.583 +      using `\<And>i. range (F i) \<subseteq> sets (M i)` M.sets_into_space
   1.584 +      by (force simp: image_subset_iff)
   1.585 +  next
   1.586 +    fix f assume "f \<in> space G"
   1.587 +    with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
   1.588 +    show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def)
   1.589 +  next
   1.590 +    fix i show "?F i \<subseteq> ?F (Suc i)"
   1.591 +      using `\<And>i. incseq (F i)`[THEN incseq_SucD] by auto
   1.592 +  qed
   1.593 +qed
   1.594 +
   1.595 +lemma sets_pair_cancel_measure[simp]:
   1.596 +  "sets (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) = sets (M1 \<Otimes>\<^isub>M M2)"
   1.597 +  "sets (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) = sets (M1 \<Otimes>\<^isub>M M2)"
   1.598 +  unfolding pair_measure_def pair_measure_generator_def sets_sigma
   1.599 +  by simp_all
   1.600 +
   1.601 +lemma measurable_pair_cancel_measure[simp]:
   1.602 +  "measurable (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
   1.603 +  "measurable (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
   1.604 +  "measurable M (M1\<lparr>measure := m3\<rparr> \<Otimes>\<^isub>M M2) = measurable M (M1 \<Otimes>\<^isub>M M2)"
   1.605 +  "measurable M (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m4\<rparr>) = measurable M (M1 \<Otimes>\<^isub>M M2)"
   1.606 +  unfolding measurable_def by (auto simp add: space_pair_measure)
   1.607 +
   1.608 +lemma (in product_sigma_finite) product_measure_exists:
   1.609 +  assumes "finite I"
   1.610 +  shows "\<exists>\<nu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<nu> \<rparr>) \<and>
   1.611 +    (\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i)))"
   1.612 +using `finite I` proof induct
   1.613 +  case empty
   1.614 +  interpret finite_product_sigma_finite M "{}" by default simp
   1.615 +  let ?\<nu> = "(\<lambda>A. if A = {} then 0 else 1) :: 'd set \<Rightarrow> extreal"
   1.616 +  show ?case
   1.617 +  proof (intro exI conjI ballI)
   1.618 +    have "sigma_algebra (sigma G \<lparr>measure := ?\<nu>\<rparr>)"
   1.619 +      by (rule sigma_algebra_cong) (simp_all add: product_algebra_def)
   1.620 +    then have "measure_space (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
   1.621 +      by (rule finite_additivity_sufficient)
   1.622 +         (simp_all add: positive_def additive_def sets_sigma
   1.623 +                        product_algebra_generator_def image_constant)
   1.624 +    then show "sigma_finite_measure (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
   1.625 +      by (auto intro!: exI[of _ "\<lambda>i. {\<lambda>_. undefined}"]
   1.626 +               simp: sigma_finite_measure_def sigma_finite_measure_axioms_def
   1.627 +                     product_algebra_generator_def)
   1.628 +  qed auto
   1.629 +next
   1.630 +  case (insert i I)
   1.631 +  interpret finite_product_sigma_finite M I by default fact
   1.632 +  have "finite (insert i I)" using `finite I` by auto
   1.633 +  interpret I': finite_product_sigma_finite M "insert i I" by default fact
   1.634 +  from insert obtain \<nu> where
   1.635 +    prod: "\<And>A. A \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))" and
   1.636 +    "sigma_finite_measure (sigma G\<lparr> measure := \<nu> \<rparr>)" by auto
   1.637 +  then interpret I: sigma_finite_measure "P\<lparr> measure := \<nu>\<rparr>" unfolding product_algebra_def by simp
   1.638 +  interpret P: pair_sigma_finite "P\<lparr> measure := \<nu>\<rparr>" "M i" ..
   1.639 +  let ?h = "(\<lambda>(f, y). f(i := y))"
   1.640 +  let ?\<nu> = "\<lambda>A. P.\<mu> (?h -` A \<inter> space P.P)"
   1.641 +  have I': "sigma_algebra (I'.P\<lparr> measure := ?\<nu> \<rparr>)"
   1.642 +    by (rule I'.sigma_algebra_cong) simp_all
   1.643 +  interpret I'': measure_space "I'.P\<lparr> measure := ?\<nu> \<rparr>"
   1.644 +    using measurable_add_dim[OF `i \<notin> I`]
   1.645 +    by (intro P.measure_space_vimage[OF I']) (auto simp add: measure_preserving_def)
   1.646 +  show ?case
   1.647 +  proof (intro exI[of _ ?\<nu>] conjI ballI)
   1.648 +    let "?m A" = "measure (Pi\<^isub>M I M\<lparr>measure := \<nu>\<rparr> \<Otimes>\<^isub>M M i) (?h -` A \<inter> space P.P)"
   1.649 +    { fix A assume A: "A \<in> (\<Pi> i\<in>insert i I. sets (M i))"
   1.650 +      then have *: "?h -` Pi\<^isub>E (insert i I) A \<inter> space P.P = Pi\<^isub>E I A \<times> A i"
   1.651 +        using `i \<notin> I` M.sets_into_space by (auto simp: space_pair_measure space_product_algebra) blast
   1.652 +      show "?m (Pi\<^isub>E (insert i I) A) = (\<Prod>i\<in>insert i I. M.\<mu> i (A i))"
   1.653 +        unfolding * using A
   1.654 +        apply (subst P.pair_measure_times)
   1.655 +        using A apply fastsimp
   1.656 +        using A apply fastsimp
   1.657 +        using `i \<notin> I` `finite I` prod[of A] A by (auto simp: ac_simps) }
   1.658 +    note product = this
   1.659 +    have *: "sigma I'.G\<lparr> measure := ?\<nu> \<rparr> = I'.P\<lparr> measure := ?\<nu> \<rparr>"
   1.660 +      by (simp add: product_algebra_def)
   1.661 +    show "sigma_finite_measure (sigma I'.G\<lparr> measure := ?\<nu> \<rparr>)"
   1.662 +    proof (unfold *, default, simp)
   1.663 +      from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   1.664 +      then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   1.665 +        "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
   1.666 +        "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space I'.G"
   1.667 +        "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> \<mu> j (F j k) \<noteq> \<infinity>"
   1.668 +        by blast+
   1.669 +      let "?F k" = "\<Pi>\<^isub>E j \<in> insert i I. F j k"
   1.670 +      show "\<exists>F::nat \<Rightarrow> ('i \<Rightarrow> 'a) set. range F \<subseteq> sets I'.P \<and>
   1.671 +          (\<Union>i. F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i)) \<and> (\<forall>i. ?m (F i) \<noteq> \<infinity>)"
   1.672 +      proof (intro exI[of _ ?F] conjI allI)
   1.673 +        show "range ?F \<subseteq> sets I'.P" using F(1) by auto
   1.674 +      next
   1.675 +        from F(3) show "(\<Union>i. ?F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i))" by simp
   1.676 +      next
   1.677 +        fix j
   1.678 +        have "\<And>k. k \<in> insert i I \<Longrightarrow> 0 \<le> measure (M k) (F k j)"
   1.679 +          using F(1) by auto
   1.680 +        with F `finite I` setprod_PInf[of "insert i I", OF this] show "?\<nu> (?F j) \<noteq> \<infinity>"
   1.681 +          by (subst product) auto
   1.682 +      qed
   1.683 +    qed
   1.684 +  qed
   1.685 +qed
   1.686 +
   1.687 +sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure P
   1.688 +  unfolding product_algebra_def
   1.689 +  using product_measure_exists[OF finite_index]
   1.690 +  by (rule someI2_ex) auto
   1.691 +
   1.692 +lemma (in finite_product_sigma_finite) measure_times:
   1.693 +  assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
   1.694 +  shows "\<mu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   1.695 +  unfolding product_algebra_def
   1.696 +  using product_measure_exists[OF finite_index]
   1.697 +  proof (rule someI2_ex, elim conjE)
   1.698 +    fix \<nu> assume *: "\<forall>A\<in>\<Pi> i\<in>I. sets (M i). \<nu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   1.699 +    have "Pi\<^isub>E I A = Pi\<^isub>E I (\<lambda>i\<in>I. A i)" by (auto dest: Pi_mem)
   1.700 +    then have "\<nu> (Pi\<^isub>E I A) = \<nu> (Pi\<^isub>E I (\<lambda>i\<in>I. A i))" by simp
   1.701 +    also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i ((\<lambda>i\<in>I. A i) i))" using assms * by auto
   1.702 +    finally show "measure (sigma G\<lparr>measure := \<nu>\<rparr>) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   1.703 +      by (simp add: sigma_def)
   1.704 +  qed
   1.705 +
   1.706 +lemma (in product_sigma_finite) product_measure_empty[simp]:
   1.707 +  "measure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
   1.708 +proof -
   1.709 +  interpret finite_product_sigma_finite M "{}" by default auto
   1.710 +  from measure_times[of "\<lambda>x. {}"] show ?thesis by simp
   1.711 +qed
   1.712 +
   1.713 +lemma (in finite_product_sigma_algebra) P_empty:
   1.714 +  assumes "I = {}"
   1.715 +  shows "space P = {\<lambda>k. undefined}" "sets P = { {}, {\<lambda>k. undefined} }"
   1.716 +  unfolding product_algebra_def product_algebra_generator_def `I = {}`
   1.717 +  by (simp_all add: sigma_def image_constant)
   1.718 +
   1.719 +lemma (in product_sigma_finite) positive_integral_empty:
   1.720 +  assumes pos: "0 \<le> f (\<lambda>k. undefined)"
   1.721 +  shows "integral\<^isup>P (Pi\<^isub>M {} M) f = f (\<lambda>k. undefined)"
   1.722 +proof -
   1.723 +  interpret finite_product_sigma_finite M "{}" by default (fact finite.emptyI)
   1.724 +  have "\<And>A. measure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
   1.725 +    using assms by (subst measure_times) auto
   1.726 +  then show ?thesis
   1.727 +    unfolding positive_integral_def simple_function_def simple_integral_def_raw
   1.728 +  proof (simp add: P_empty, intro antisym)
   1.729 +    show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined))"
   1.730 +      by (intro le_SUPI) (auto simp: le_fun_def split: split_max)
   1.731 +    show "(SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)" using pos
   1.732 +      by (intro SUP_leI) (auto simp: le_fun_def simp: max_def split: split_if_asm)
   1.733 +  qed
   1.734 +qed
   1.735 +
   1.736 +lemma (in product_sigma_finite) measure_fold:
   1.737 +  assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   1.738 +  assumes A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
   1.739 +  shows "measure (Pi\<^isub>M (I \<union> J) M) A =
   1.740 +    measure (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) ((\<lambda>(x,y). merge I x J y) -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M))"
   1.741 +proof -
   1.742 +  interpret I: finite_product_sigma_finite M I by default fact
   1.743 +  interpret J: finite_product_sigma_finite M J by default fact
   1.744 +  have "finite (I \<union> J)" using fin by auto
   1.745 +  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   1.746 +  interpret P: pair_sigma_finite I.P J.P by default
   1.747 +  let ?g = "\<lambda>(x,y). merge I x J y"
   1.748 +  let "?X S" = "?g -` S \<inter> space P.P"
   1.749 +  from IJ.sigma_finite_pairs obtain F where
   1.750 +    F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
   1.751 +       "incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k)"
   1.752 +       "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space IJ.G"
   1.753 +       "\<And>k. \<forall>i\<in>I\<union>J. \<mu> i (F i k) \<noteq> \<infinity>"
   1.754 +    by auto
   1.755 +  let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
   1.756 +  show "IJ.\<mu> A = P.\<mu> (?X A)"
   1.757 +  proof (rule measure_unique_Int_stable_vimage)
   1.758 +    show "measure_space IJ.P" "measure_space P.P" by default
   1.759 +    show "sets (sigma IJ.G) = sets IJ.P" "space IJ.G = space IJ.P" "A \<in> sets (sigma IJ.G)"
   1.760 +      using A unfolding product_algebra_def by auto
   1.761 +  next
   1.762 +    show "Int_stable IJ.G"
   1.763 +      by (simp add: PiE_Int Int_stable_def product_algebra_def
   1.764 +                    product_algebra_generator_def)
   1.765 +          auto
   1.766 +    show "range ?F \<subseteq> sets IJ.G" using F
   1.767 +      by (simp add: image_subset_iff product_algebra_def
   1.768 +                    product_algebra_generator_def)
   1.769 +    show "incseq ?F" "(\<Union>i. ?F i) = space IJ.G " by fact+
   1.770 +  next
   1.771 +    fix k
   1.772 +    have "\<And>j. j \<in> I \<union> J \<Longrightarrow> 0 \<le> measure (M j) (F j k)"
   1.773 +      using F(1) by auto
   1.774 +    with F `finite I` setprod_PInf[of "I \<union> J", OF this]
   1.775 +    show "IJ.\<mu> (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
   1.776 +  next
   1.777 +    fix A assume "A \<in> sets IJ.G"
   1.778 +    then obtain F where A: "A = Pi\<^isub>E (I \<union> J) F"
   1.779 +      and F: "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i \<in> sets (M i)"
   1.780 +      by (auto simp: product_algebra_generator_def)
   1.781 +    then have X: "?X A = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
   1.782 +      using sets_into_space by (auto simp: space_pair_measure) blast+
   1.783 +    then have "P.\<mu> (?X A) = (\<Prod>i\<in>I. \<mu> i (F i)) * (\<Prod>i\<in>J. \<mu> i (F i))"
   1.784 +      using `finite J` `finite I` F
   1.785 +      by (simp add: P.pair_measure_times I.measure_times J.measure_times)
   1.786 +    also have "\<dots> = (\<Prod>i\<in>I \<union> J. \<mu> i (F i))"
   1.787 +      using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
   1.788 +    also have "\<dots> = IJ.\<mu> A"
   1.789 +      using `finite J` `finite I` F unfolding A
   1.790 +      by (intro IJ.measure_times[symmetric]) auto
   1.791 +    finally show "IJ.\<mu> A = P.\<mu> (?X A)" by (rule sym)
   1.792 +  qed (rule measurable_merge[OF IJ])
   1.793 +qed
   1.794 +
   1.795 +lemma (in product_sigma_finite) measure_preserving_merge:
   1.796 +  assumes IJ: "I \<inter> J = {}" and "finite I" "finite J"
   1.797 +  shows "(\<lambda>(x,y). merge I x J y) \<in> measure_preserving (Pi\<^isub>M I M \<Otimes>\<^isub>M Pi\<^isub>M J M) (Pi\<^isub>M (I \<union> J) M)"
   1.798 +  by (intro measure_preservingI measurable_merge[OF IJ] measure_fold[symmetric] assms)
   1.799 +
   1.800 +lemma (in product_sigma_finite) product_positive_integral_fold:
   1.801 +  assumes IJ[simp]: "I \<inter> J = {}" "finite I" "finite J"
   1.802 +  and f: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   1.803 +  shows "integral\<^isup>P (Pi\<^isub>M (I \<union> J) M) f =
   1.804 +    (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I x J y) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
   1.805 +proof -
   1.806 +  interpret I: finite_product_sigma_finite M I by default fact
   1.807 +  interpret J: finite_product_sigma_finite M J by default fact
   1.808 +  interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   1.809 +  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default simp
   1.810 +  have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable P.P"
   1.811 +    using measurable_comp[OF measurable_merge[OF IJ(1)] f] by (simp add: comp_def)
   1.812 +  show ?thesis
   1.813 +    unfolding P.positive_integral_fst_measurable[OF P_borel, simplified]
   1.814 +  proof (rule P.positive_integral_vimage)
   1.815 +    show "sigma_algebra IJ.P" by default
   1.816 +    show "(\<lambda>(x, y). merge I x J y) \<in> measure_preserving P.P IJ.P"
   1.817 +      using IJ by (rule measure_preserving_merge)
   1.818 +    show "f \<in> borel_measurable IJ.P" using f by simp
   1.819 +  qed
   1.820 +qed
   1.821 +
   1.822 +lemma (in product_sigma_finite) measure_preserving_component_singelton:
   1.823 +  "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
   1.824 +proof (intro measure_preservingI measurable_component_singleton)
   1.825 +  interpret I: finite_product_sigma_finite M "{i}" by default simp
   1.826 +  fix A let ?P = "(\<lambda>x. x i) -` A \<inter> space I.P"
   1.827 +  assume A: "A \<in> sets (M i)"
   1.828 +  then have *: "?P = {i} \<rightarrow>\<^isub>E A" using sets_into_space by auto
   1.829 +  show "I.\<mu> ?P = M.\<mu> i A" unfolding *
   1.830 +    using A I.measure_times[of "\<lambda>_. A"] by auto
   1.831 +qed auto
   1.832 +
   1.833 +lemma (in product_sigma_finite) product_positive_integral_singleton:
   1.834 +  assumes f: "f \<in> borel_measurable (M i)"
   1.835 +  shows "integral\<^isup>P (Pi\<^isub>M {i} M) (\<lambda>x. f (x i)) = integral\<^isup>P (M i) f"
   1.836 +proof -
   1.837 +  interpret I: finite_product_sigma_finite M "{i}" by default simp
   1.838 +  show ?thesis
   1.839 +  proof (rule I.positive_integral_vimage[symmetric])
   1.840 +    show "sigma_algebra (M i)" by (rule sigma_algebras)
   1.841 +    show "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
   1.842 +      by (rule measure_preserving_component_singelton)
   1.843 +    show "f \<in> borel_measurable (M i)" by fact
   1.844 +  qed
   1.845 +qed
   1.846 +
   1.847 +lemma (in product_sigma_finite) product_positive_integral_insert:
   1.848 +  assumes [simp]: "finite I" "i \<notin> I"
   1.849 +    and f: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
   1.850 +  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))"
   1.851 +proof -
   1.852 +  interpret I: finite_product_sigma_finite M I by default auto
   1.853 +  interpret i: finite_product_sigma_finite M "{i}" by default auto
   1.854 +  interpret P: pair_sigma_algebra I.P i.P ..
   1.855 +  have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
   1.856 +    using f by auto
   1.857 +  show ?thesis
   1.858 +    unfolding product_positive_integral_fold[OF IJ, unfolded insert, simplified, OF f]
   1.859 +  proof (rule I.positive_integral_cong, subst product_positive_integral_singleton)
   1.860 +    fix x assume x: "x \<in> space I.P"
   1.861 +    let "?f y" = "f (restrict (x(i := y)) (insert i I))"
   1.862 +    have f'_eq: "\<And>y. ?f y = f (x(i := y))"
   1.863 +      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
   1.864 +    show "?f \<in> borel_measurable (M i)" unfolding f'_eq
   1.865 +      using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
   1.866 +      by (simp add: comp_def)
   1.867 +    show "integral\<^isup>P (M i) ?f = \<integral>\<^isup>+ y. f (x(i:=y)) \<partial>M i"
   1.868 +      unfolding f'_eq by simp
   1.869 +  qed
   1.870 +qed
   1.871 +
   1.872 +lemma (in product_sigma_finite) product_positive_integral_setprod:
   1.873 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> extreal"
   1.874 +  assumes "finite I" and borel: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
   1.875 +  and pos: "\<And>i x. i \<in> I \<Longrightarrow> 0 \<le> f i x"
   1.876 +  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))"
   1.877 +using assms proof induct
   1.878 +  case empty
   1.879 +  interpret finite_product_sigma_finite M "{}" by default auto
   1.880 +  then show ?case by simp
   1.881 +next
   1.882 +  case (insert i I)
   1.883 +  note `finite I`[intro, simp]
   1.884 +  interpret I: finite_product_sigma_finite M I by default auto
   1.885 +  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.886 +    using insert by (auto intro!: setprod_cong)
   1.887 +  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)"
   1.888 +    using sets_into_space insert
   1.889 +    by (intro sigma_algebra.borel_measurable_extreal_setprod sigma_algebra_product_algebra
   1.890 +              measurable_comp[OF measurable_component_singleton, unfolded comp_def])
   1.891 +       auto
   1.892 +  then show ?case
   1.893 +    apply (simp add: product_positive_integral_insert[OF insert(1,2) prod])
   1.894 +    apply (simp add: insert * pos borel setprod_extreal_pos M.positive_integral_multc)
   1.895 +    apply (subst I.positive_integral_cmult)
   1.896 +    apply (auto simp add: pos borel insert setprod_extreal_pos positive_integral_positive)
   1.897 +    done
   1.898 +qed
   1.899 +
   1.900 +lemma (in product_sigma_finite) product_integral_singleton:
   1.901 +  assumes f: "f \<in> borel_measurable (M i)"
   1.902 +  shows "(\<integral>x. f (x i) \<partial>Pi\<^isub>M {i} M) = integral\<^isup>L (M i) f"
   1.903 +proof -
   1.904 +  interpret I: finite_product_sigma_finite M "{i}" by default simp
   1.905 +  have *: "(\<lambda>x. extreal (f x)) \<in> borel_measurable (M i)"
   1.906 +    "(\<lambda>x. extreal (- f x)) \<in> borel_measurable (M i)"
   1.907 +    using assms by auto
   1.908 +  show ?thesis
   1.909 +    unfolding lebesgue_integral_def *[THEN product_positive_integral_singleton] ..
   1.910 +qed
   1.911 +
   1.912 +lemma (in product_sigma_finite) product_integral_fold:
   1.913 +  assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   1.914 +  and f: "integrable (Pi\<^isub>M (I \<union> J) M) f"
   1.915 +  shows "integral\<^isup>L (Pi\<^isub>M (I \<union> J) M) f = (\<integral>x. (\<integral>y. f (merge I x J y) \<partial>Pi\<^isub>M J M) \<partial>Pi\<^isub>M I M)"
   1.916 +proof -
   1.917 +  interpret I: finite_product_sigma_finite M I by default fact
   1.918 +  interpret J: finite_product_sigma_finite M J by default fact
   1.919 +  have "finite (I \<union> J)" using fin by auto
   1.920 +  interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   1.921 +  interpret P: pair_sigma_finite I.P J.P by default
   1.922 +  let ?M = "\<lambda>(x, y). merge I x J y"
   1.923 +  let ?f = "\<lambda>x. f (?M x)"
   1.924 +  show ?thesis
   1.925 +  proof (subst P.integrable_fst_measurable(2)[of ?f, simplified])
   1.926 +    have 1: "sigma_algebra IJ.P" by default
   1.927 +    have 2: "?M \<in> measure_preserving P.P IJ.P" using measure_preserving_merge[OF assms(1,2,3)] .
   1.928 +    have 3: "integrable (Pi\<^isub>M (I \<union> J) M) f" by fact
   1.929 +    then have 4: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   1.930 +      by (simp add: integrable_def)
   1.931 +    show "integrable P.P ?f"
   1.932 +      by (rule P.integrable_vimage[where f=f, OF 1 2 3])
   1.933 +    show "integral\<^isup>L IJ.P f = integral\<^isup>L P.P ?f"
   1.934 +      by (rule P.integral_vimage[where f=f, OF 1 2 4])
   1.935 +  qed
   1.936 +qed
   1.937 +
   1.938 +lemma (in product_sigma_finite) product_integral_insert:
   1.939 +  assumes [simp]: "finite I" "i \<notin> I"
   1.940 +    and f: "integrable (Pi\<^isub>M (insert i I) M) f"
   1.941 +  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)"
   1.942 +proof -
   1.943 +  interpret I: finite_product_sigma_finite M I by default auto
   1.944 +  interpret I': finite_product_sigma_finite M "insert i I" by default auto
   1.945 +  interpret i: finite_product_sigma_finite M "{i}" by default auto
   1.946 +  interpret P: pair_sigma_finite I.P i.P ..
   1.947 +  have IJ: "I \<inter> {i} = {}" by auto
   1.948 +  show ?thesis
   1.949 +    unfolding product_integral_fold[OF IJ, simplified, OF f]
   1.950 +  proof (rule I.integral_cong, subst product_integral_singleton)
   1.951 +    fix x assume x: "x \<in> space I.P"
   1.952 +    let "?f y" = "f (restrict (x(i := y)) (insert i I))"
   1.953 +    have f'_eq: "\<And>y. ?f y = f (x(i := y))"
   1.954 +      using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
   1.955 +    have f: "f \<in> borel_measurable I'.P" using f unfolding integrable_def by auto
   1.956 +    show "?f \<in> borel_measurable (M i)"
   1.957 +      unfolding measurable_cong[OF f'_eq]
   1.958 +      using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
   1.959 +      by (simp add: comp_def)
   1.960 +    show "integral\<^isup>L (M i) ?f = integral\<^isup>L (M i) (\<lambda>y. f (x(i := y)))"
   1.961 +      unfolding f'_eq by simp
   1.962 +  qed
   1.963 +qed
   1.964 +
   1.965 +lemma (in product_sigma_finite) product_integrable_setprod:
   1.966 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   1.967 +  assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   1.968 +  shows "integrable (Pi\<^isub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
   1.969 +proof -
   1.970 +  interpret finite_product_sigma_finite M I by default fact
   1.971 +  have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
   1.972 +    using integrable unfolding integrable_def by auto
   1.973 +  then have borel: "?f \<in> borel_measurable P"
   1.974 +    using measurable_comp[OF measurable_component_singleton f]
   1.975 +    by (auto intro!: borel_measurable_setprod simp: comp_def)
   1.976 +  moreover have "integrable (Pi\<^isub>M I M) (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
   1.977 +  proof (unfold integrable_def, intro conjI)
   1.978 +    show "(\<lambda>x. abs (?f x)) \<in> borel_measurable P"
   1.979 +      using borel by auto
   1.980 +    have "(\<integral>\<^isup>+x. extreal (abs (?f x)) \<partial>P) = (\<integral>\<^isup>+x. (\<Prod>i\<in>I. extreal (abs (f i (x i)))) \<partial>P)"
   1.981 +      by (simp add: setprod_extreal abs_setprod)
   1.982 +    also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^isup>+x. extreal (abs (f i x)) \<partial>M i))"
   1.983 +      using f by (subst product_positive_integral_setprod) auto
   1.984 +    also have "\<dots> < \<infinity>"
   1.985 +      using integrable[THEN M.integrable_abs]
   1.986 +      by (simp add: setprod_PInf integrable_def M.positive_integral_positive)
   1.987 +    finally show "(\<integral>\<^isup>+x. extreal (abs (?f x)) \<partial>P) \<noteq> \<infinity>" by auto
   1.988 +    have "(\<integral>\<^isup>+x. extreal (- abs (?f x)) \<partial>P) = (\<integral>\<^isup>+x. 0 \<partial>P)"
   1.989 +      by (intro positive_integral_cong_pos) auto
   1.990 +    then show "(\<integral>\<^isup>+x. extreal (- abs (?f x)) \<partial>P) \<noteq> \<infinity>" by simp
   1.991 +  qed
   1.992 +  ultimately show ?thesis
   1.993 +    by (rule integrable_abs_iff[THEN iffD1])
   1.994 +qed
   1.995 +
   1.996 +lemma (in product_sigma_finite) product_integral_setprod:
   1.997 +  fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   1.998 +  assumes "finite I" "I \<noteq> {}" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   1.999 +  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))"
  1.1000 +using assms proof (induct rule: finite_ne_induct)
  1.1001 +  case (singleton i)
  1.1002 +  then show ?case by (simp add: product_integral_singleton integrable_def)
  1.1003 +next
  1.1004 +  case (insert i I)
  1.1005 +  then have iI: "finite (insert i I)" by auto
  1.1006 +  then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
  1.1007 +    integrable (Pi\<^isub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
  1.1008 +    by (intro product_integrable_setprod insert(5)) (auto intro: finite_subset)
  1.1009 +  interpret I: finite_product_sigma_finite M I by default fact
  1.1010 +  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.1011 +    using `i \<notin> I` by (auto intro!: setprod_cong)
  1.1012 +  show ?case
  1.1013 +    unfolding product_integral_insert[OF insert(1,3) prod[OF subset_refl]]
  1.1014 +    by (simp add: * insert integral_multc I.integral_cmult[OF prod] subset_insertI)
  1.1015 +qed
  1.1016 +
  1.1017 +end
  1.1018 \ No newline at end of file