src/HOL/Probability/Finite_Product_Measure.thy
author hoelzl
Mon May 23 19:21:05 2011 +0200 (2011-05-23)
changeset 42950 6e5c2a3c69da
parent 42146 5b52c6a9c627
child 42988 d8f3fc934ff6
permissions -rw-r--r--
move lemmas to Extended_Reals and Extended_Real_Limits
     1 (*  Title:      HOL/Probability/Finite_Product_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 header {*Finite product measures*}
     6 
     7 theory Finite_Product_Measure
     8 imports Binary_Product_Measure
     9 begin
    10 
    11 lemma Pi_iff: "f \<in> Pi I X \<longleftrightarrow> (\<forall>i\<in>I. f i \<in> X i)"
    12   unfolding Pi_def by auto
    13 
    14 abbreviation
    15   "Pi\<^isub>E A B \<equiv> Pi A B \<inter> extensional A"
    16 
    17 syntax
    18   "_PiE"  :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3PIE _:_./ _)" 10)
    19 
    20 syntax (xsymbols)
    21   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
    22 
    23 syntax (HTML output)
    24   "_PiE" :: "[pttrn, 'a set, 'b set] => ('a => 'b) set"  ("(3\<Pi>\<^isub>E _\<in>_./ _)"   10)
    25 
    26 translations
    27   "PIE x:A. B" == "CONST Pi\<^isub>E A (%x. B)"
    28 
    29 abbreviation
    30   funcset_extensional :: "['a set, 'b set] => ('a => 'b) set"
    31     (infixr "->\<^isub>E" 60) where
    32   "A ->\<^isub>E B \<equiv> Pi\<^isub>E A (%_. B)"
    33 
    34 notation (xsymbols)
    35   funcset_extensional  (infixr "\<rightarrow>\<^isub>E" 60)
    36 
    37 lemma extensional_empty[simp]: "extensional {} = {\<lambda>x. undefined}"
    38   by safe (auto simp add: extensional_def fun_eq_iff)
    39 
    40 lemma extensional_insert[intro, simp]:
    41   assumes "a \<in> extensional (insert i I)"
    42   shows "a(i := b) \<in> extensional (insert i I)"
    43   using assms unfolding extensional_def by auto
    44 
    45 lemma extensional_Int[simp]:
    46   "extensional I \<inter> extensional I' = extensional (I \<inter> I')"
    47   unfolding extensional_def by auto
    48 
    49 definition
    50   "merge I x J y = (\<lambda>i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
    51 
    52 lemma merge_apply[simp]:
    53   "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    54   "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    55   "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I x J y i = x i"
    56   "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I x J y i = y i"
    57   "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I x J y i = undefined"
    58   unfolding merge_def by auto
    59 
    60 lemma merge_commute:
    61   "I \<inter> J = {} \<Longrightarrow> merge I x J y = merge J y I x"
    62   by (auto simp: merge_def intro!: ext)
    63 
    64 lemma Pi_cancel_merge_range[simp]:
    65   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    66   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    67   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I A J B) \<longleftrightarrow> x \<in> Pi I A"
    68   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J B I A) \<longleftrightarrow> x \<in> Pi I A"
    69   by (auto simp: Pi_def)
    70 
    71 lemma Pi_cancel_merge[simp]:
    72   "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    73   "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    74   "I \<inter> J = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    75   "J \<inter> I = {} \<Longrightarrow> merge I x J y \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    76   by (auto simp: Pi_def)
    77 
    78 lemma extensional_merge[simp]: "merge I x J y \<in> extensional (I \<union> J)"
    79   by (auto simp: extensional_def)
    80 
    81 lemma restrict_Pi_cancel: "restrict x I \<in> Pi I A \<longleftrightarrow> x \<in> Pi I A"
    82   by (auto simp: restrict_def Pi_def)
    83 
    84 lemma restrict_merge[simp]:
    85   "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    86   "I \<inter> J = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    87   "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) I = restrict x I"
    88   "J \<inter> I = {} \<Longrightarrow> restrict (merge I x J y) J = restrict y J"
    89   by (auto simp: restrict_def intro!: ext)
    90 
    91 lemma extensional_insert_undefined[intro, simp]:
    92   assumes "a \<in> extensional (insert i I)"
    93   shows "a(i := undefined) \<in> extensional I"
    94   using assms unfolding extensional_def by auto
    95 
    96 lemma extensional_insert_cancel[intro, simp]:
    97   assumes "a \<in> extensional I"
    98   shows "a \<in> extensional (insert i I)"
    99   using assms unfolding extensional_def by auto
   100 
   101 lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I x {i} y = restrict (x(i := y i)) (insert i I)"
   102   unfolding merge_def by (auto simp: fun_eq_iff)
   103 
   104 lemma Pi_Int: "Pi I E \<inter> Pi I F = (\<Pi> i\<in>I. E i \<inter> F i)"
   105   by auto
   106 
   107 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)"
   108   by auto
   109 
   110 lemma Pi_cancel_fupd_range[simp]: "i \<notin> I \<Longrightarrow> x \<in> Pi I (B(i := b)) \<longleftrightarrow> x \<in> Pi I B"
   111   by (auto simp: Pi_def)
   112 
   113 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"
   114   by (auto simp: Pi_def)
   115 
   116 lemma Pi_split_domain[simp]: "x \<in> Pi (I \<union> J) X \<longleftrightarrow> x \<in> Pi I X \<and> x \<in> Pi J X"
   117   by (auto simp: Pi_def)
   118 
   119 lemma Pi_cancel_fupd[simp]: "i \<notin> I \<Longrightarrow> x(i := a) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
   120   by (auto simp: Pi_def)
   121 
   122 lemma restrict_vimage:
   123   assumes "I \<inter> J = {}"
   124   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)"
   125   using assms by (auto simp: restrict_Pi_cancel)
   126 
   127 lemma merge_vimage:
   128   assumes "I \<inter> J = {}"
   129   shows "(\<lambda>(x,y). merge I x J y) -` Pi\<^isub>E (I \<union> J) E = Pi I E \<times> Pi J E"
   130   using assms by (auto simp: restrict_Pi_cancel)
   131 
   132 lemma restrict_fupd[simp]: "i \<notin> I \<Longrightarrow> restrict (f (i := x)) I = restrict f I"
   133   by (auto simp: restrict_def intro!: ext)
   134 
   135 lemma merge_restrict[simp]:
   136   "merge I (restrict x I) J y = merge I x J y"
   137   "merge I x J (restrict y J) = merge I x J y"
   138   unfolding merge_def by (auto intro!: ext)
   139 
   140 lemma merge_x_x_eq_restrict[simp]:
   141   "merge I x J x = restrict x (I \<union> J)"
   142   unfolding merge_def by (auto intro!: ext)
   143 
   144 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"
   145   apply auto
   146   apply (drule_tac x=x in Pi_mem)
   147   apply (simp_all split: split_if_asm)
   148   apply (drule_tac x=i in Pi_mem)
   149   apply (auto dest!: Pi_mem)
   150   done
   151 
   152 lemma Pi_UN:
   153   fixes A :: "nat \<Rightarrow> 'i \<Rightarrow> 'a set"
   154   assumes "finite I" and mono: "\<And>i n m. i \<in> I \<Longrightarrow> n \<le> m \<Longrightarrow> A n i \<subseteq> A m i"
   155   shows "(\<Union>n. Pi I (A n)) = (\<Pi> i\<in>I. \<Union>n. A n i)"
   156 proof (intro set_eqI iffI)
   157   fix f assume "f \<in> (\<Pi> i\<in>I. \<Union>n. A n i)"
   158   then have "\<forall>i\<in>I. \<exists>n. f i \<in> A n i" by auto
   159   from bchoice[OF this] obtain n where n: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> (A (n i) i)" by auto
   160   obtain k where k: "\<And>i. i \<in> I \<Longrightarrow> n i \<le> k"
   161     using `finite I` finite_nat_set_iff_bounded_le[of "n`I"] by auto
   162   have "f \<in> Pi I (A k)"
   163   proof (intro Pi_I)
   164     fix i assume "i \<in> I"
   165     from mono[OF this, of "n i" k] k[OF this] n[OF this]
   166     show "f i \<in> A k i" by auto
   167   qed
   168   then show "f \<in> (\<Union>n. Pi I (A n))" by auto
   169 qed auto
   170 
   171 lemma PiE_cong:
   172   assumes "\<And>i. i\<in>I \<Longrightarrow> A i = B i"
   173   shows "Pi\<^isub>E I A = Pi\<^isub>E I B"
   174   using assms by (auto intro!: Pi_cong)
   175 
   176 lemma restrict_upd[simp]:
   177   "i \<notin> I \<Longrightarrow> (restrict f I)(i := y) = restrict (f(i := y)) (insert i I)"
   178   by (auto simp: fun_eq_iff)
   179 
   180 lemma Pi_eq_subset:
   181   assumes ne: "\<And>i. i \<in> I \<Longrightarrow> F i \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> F' i \<noteq> {}"
   182   assumes eq: "Pi\<^isub>E I F = Pi\<^isub>E I F'" and "i \<in> I"
   183   shows "F i \<subseteq> F' i"
   184 proof
   185   fix x assume "x \<in> F i"
   186   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
   187   from choice[OF this] guess f .. note f = this
   188   then have "f \<in> Pi\<^isub>E I F" by (auto simp: extensional_def)
   189   then have "f \<in> Pi\<^isub>E I F'" using assms by simp
   190   then show "x \<in> F' i" using f `i \<in> I` by auto
   191 qed
   192 
   193 lemma Pi_eq_iff_not_empty:
   194   assumes ne: "\<And>i. i \<in> I \<Longrightarrow> F i \<noteq> {}" "\<And>i. i \<in> I \<Longrightarrow> F' i \<noteq> {}"
   195   shows "Pi\<^isub>E I F = Pi\<^isub>E I F' \<longleftrightarrow> (\<forall>i\<in>I. F i = F' i)"
   196 proof (intro iffI ballI)
   197   fix i assume eq: "Pi\<^isub>E I F = Pi\<^isub>E I F'" and i: "i \<in> I"
   198   show "F i = F' i"
   199     using Pi_eq_subset[of I F F', OF ne eq i]
   200     using Pi_eq_subset[of I F' F, OF ne(2,1) eq[symmetric] i]
   201     by auto
   202 qed auto
   203 
   204 lemma Pi_eq_empty_iff:
   205   "Pi\<^isub>E I F = {} \<longleftrightarrow> (\<exists>i\<in>I. F i = {})"
   206 proof
   207   assume "Pi\<^isub>E I F = {}"
   208   show "\<exists>i\<in>I. F i = {}"
   209   proof (rule ccontr)
   210     assume "\<not> ?thesis"
   211     then have "\<forall>i. \<exists>y. (i \<in> I \<longrightarrow> y \<in> F i) \<and> (i \<notin> I \<longrightarrow> y = undefined)" by auto
   212     from choice[OF this] guess f ..
   213     then have "f \<in> Pi\<^isub>E I F" by (auto simp: extensional_def)
   214     with `Pi\<^isub>E I F = {}` show False by auto
   215   qed
   216 qed auto
   217 
   218 lemma Pi_eq_iff:
   219   "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 = {}))"
   220 proof (intro iffI disjCI)
   221   assume eq[simp]: "Pi\<^isub>E I F = Pi\<^isub>E I F'"
   222   assume "\<not> ((\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {}))"
   223   then have "(\<forall>i\<in>I. F i \<noteq> {}) \<and> (\<forall>i\<in>I. F' i \<noteq> {})"
   224     using Pi_eq_empty_iff[of I F] Pi_eq_empty_iff[of I F'] by auto
   225   with Pi_eq_iff_not_empty[of I F F'] show "\<forall>i\<in>I. F i = F' i" by auto
   226 next
   227   assume "(\<forall>i\<in>I. F i = F' i) \<or> (\<exists>i\<in>I. F i = {}) \<and> (\<exists>i\<in>I. F' i = {})"
   228   then show "Pi\<^isub>E I F = Pi\<^isub>E I F'"
   229     using Pi_eq_empty_iff[of I F] Pi_eq_empty_iff[of I F'] by auto
   230 qed
   231 
   232 section "Finite product spaces"
   233 
   234 section "Products"
   235 
   236 locale product_sigma_algebra =
   237   fixes M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme"
   238   assumes sigma_algebras: "\<And>i. sigma_algebra (M i)"
   239 
   240 locale finite_product_sigma_algebra = product_sigma_algebra M
   241   for M :: "'i \<Rightarrow> ('a, 'b) measure_space_scheme" +
   242   fixes I :: "'i set"
   243   assumes finite_index: "finite I"
   244 
   245 definition
   246   "product_algebra_generator I M = \<lparr> space = (\<Pi>\<^isub>E i \<in> I. space (M i)),
   247     sets = Pi\<^isub>E I ` (\<Pi> i \<in> I. sets (M i)),
   248     measure = \<lambda>A. (\<Prod>i\<in>I. measure (M i) ((SOME F. A = Pi\<^isub>E I F) i)) \<rparr>"
   249 
   250 definition product_algebra_def:
   251   "Pi\<^isub>M I M = sigma (product_algebra_generator I M)
   252     \<lparr> measure := (SOME \<mu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<mu> \<rparr>) \<and>
   253       (\<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>"
   254 
   255 syntax
   256   "_PiM"  :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   257               ('i => 'a, 'b) measure_space_scheme"  ("(3PIM _:_./ _)" 10)
   258 
   259 syntax (xsymbols)
   260   "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   261              ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
   262 
   263 syntax (HTML output)
   264   "_PiM" :: "[pttrn, 'i set, ('a, 'b) measure_space_scheme] =>
   265              ('i => 'a, 'b) measure_space_scheme"  ("(3\<Pi>\<^isub>M _\<in>_./ _)"   10)
   266 
   267 translations
   268   "PIM x:I. M" == "CONST Pi\<^isub>M I (%x. M)"
   269 
   270 abbreviation (in finite_product_sigma_algebra) "G \<equiv> product_algebra_generator I M"
   271 abbreviation (in finite_product_sigma_algebra) "P \<equiv> Pi\<^isub>M I M"
   272 
   273 sublocale product_sigma_algebra \<subseteq> M: sigma_algebra "M i" for i by (rule sigma_algebras)
   274 
   275 lemma sigma_into_space:
   276   assumes "sets M \<subseteq> Pow (space M)"
   277   shows "sets (sigma M) \<subseteq> Pow (space M)"
   278   using sigma_sets_into_sp[OF assms] unfolding sigma_def by auto
   279 
   280 lemma (in product_sigma_algebra) product_algebra_generator_into_space:
   281   "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
   282   using M.sets_into_space unfolding product_algebra_generator_def
   283   by auto blast
   284 
   285 lemma (in product_sigma_algebra) product_algebra_into_space:
   286   "sets (Pi\<^isub>M I M) \<subseteq> Pow (space (Pi\<^isub>M I M))"
   287   using product_algebra_generator_into_space
   288   by (auto intro!: sigma_into_space simp add: product_algebra_def)
   289 
   290 lemma (in product_sigma_algebra) sigma_algebra_product_algebra: "sigma_algebra (Pi\<^isub>M I M)"
   291   using product_algebra_generator_into_space unfolding product_algebra_def
   292   by (rule sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) simp_all
   293 
   294 sublocale finite_product_sigma_algebra \<subseteq> sigma_algebra P
   295   using sigma_algebra_product_algebra .
   296 
   297 lemma product_algebraE:
   298   assumes "A \<in> sets (product_algebra_generator I M)"
   299   obtains E where "A = Pi\<^isub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   300   using assms unfolding product_algebra_generator_def by auto
   301 
   302 lemma product_algebra_generatorI[intro]:
   303   assumes "E \<in> (\<Pi> i\<in>I. sets (M i))"
   304   shows "Pi\<^isub>E I E \<in> sets (product_algebra_generator I M)"
   305   using assms unfolding product_algebra_generator_def by auto
   306 
   307 lemma space_product_algebra_generator[simp]:
   308   "space (product_algebra_generator I M) = Pi\<^isub>E I (\<lambda>i. space (M i))"
   309   unfolding product_algebra_generator_def by simp
   310 
   311 lemma space_product_algebra[simp]:
   312   "space (Pi\<^isub>M I M) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   313   unfolding product_algebra_def product_algebra_generator_def by simp
   314 
   315 lemma sets_product_algebra:
   316   "sets (Pi\<^isub>M I M) = sets (sigma (product_algebra_generator I M))"
   317   unfolding product_algebra_def sigma_def by simp
   318 
   319 lemma product_algebra_generator_sets_into_space:
   320   assumes "\<And>i. i\<in>I \<Longrightarrow> sets (M i) \<subseteq> Pow (space (M i))"
   321   shows "sets (product_algebra_generator I M) \<subseteq> Pow (space (product_algebra_generator I M))"
   322   using assms by (auto simp: product_algebra_generator_def) blast
   323 
   324 lemma (in finite_product_sigma_algebra) in_P[simp, intro]:
   325   "\<lbrakk> \<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i) \<rbrakk> \<Longrightarrow> Pi\<^isub>E I A \<in> sets P"
   326   by (auto simp: sets_product_algebra)
   327 
   328 section "Generating set generates also product algebra"
   329 
   330 lemma sigma_product_algebra_sigma_eq:
   331   assumes "finite I"
   332   assumes mono: "\<And>i. i \<in> I \<Longrightarrow> incseq (S i)"
   333   assumes union: "\<And>i. i \<in> I \<Longrightarrow> (\<Union>j. S i j) = space (E i)"
   334   assumes sets_into: "\<And>i. i \<in> I \<Longrightarrow> range (S i) \<subseteq> sets (E i)"
   335   and E: "\<And>i. sets (E i) \<subseteq> Pow (space (E i))"
   336   shows "sets (\<Pi>\<^isub>M i\<in>I. sigma (E i)) = sets (\<Pi>\<^isub>M i\<in>I. E i)"
   337     (is "sets ?S = sets ?E")
   338 proof cases
   339   assume "I = {}" then show ?thesis
   340     by (simp add: product_algebra_def product_algebra_generator_def)
   341 next
   342   assume "I \<noteq> {}"
   343   interpret E: sigma_algebra "sigma (E i)" for i
   344     using E by (rule sigma_algebra_sigma)
   345   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)"
   346     using E by auto
   347   interpret G: sigma_algebra ?E
   348     unfolding product_algebra_def product_algebra_generator_def using E
   349     by (intro sigma_algebra.sigma_algebra_cong[OF sigma_algebra_sigma]) (auto dest: Pi_mem)
   350   { fix A i assume "i \<in> I" and A: "A \<in> sets (E i)"
   351     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"
   352       using mono union unfolding incseq_Suc_iff space_product_algebra
   353       by (auto dest: Pi_mem)
   354     also have "\<dots> = (\<Union>n. (\<Pi>\<^isub>E j\<in>I. if j = i then A else S j n))"
   355       unfolding space_product_algebra
   356       apply simp
   357       apply (subst Pi_UN[OF `finite I`])
   358       using mono[THEN incseqD] apply simp
   359       apply (simp add: PiE_Int)
   360       apply (intro PiE_cong)
   361       using A sets_into by (auto intro!: into_space)
   362     also have "\<dots> \<in> sets ?E"
   363       using sets_into `A \<in> sets (E i)`
   364       unfolding sets_product_algebra sets_sigma
   365       by (intro sigma_sets.Union)
   366          (auto simp: image_subset_iff intro!: sigma_sets.Basic)
   367     finally have "(\<lambda>x. x i) -` A \<inter> space ?E \<in> sets ?E" . }
   368   then have proj:
   369     "\<And>i. i\<in>I \<Longrightarrow> (\<lambda>x. x i) \<in> measurable ?E (sigma (E i))"
   370     using E by (subst G.measurable_iff_sigma)
   371                (auto simp: sets_product_algebra sets_sigma)
   372   { fix A assume A: "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (sigma (E i))"
   373     with proj have basic: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) -` (A i) \<inter> space ?E \<in> sets ?E"
   374       unfolding measurable_def by simp
   375     have "Pi\<^isub>E I A = (\<Inter>i\<in>I. (\<lambda>x. x i) -` (A i) \<inter> space ?E)"
   376       using A E.sets_into_space `I \<noteq> {}` unfolding product_algebra_def by auto blast
   377     then have "Pi\<^isub>E I A \<in> sets ?E"
   378       using G.finite_INT[OF `finite I` `I \<noteq> {}` basic, of "\<lambda>i. i"] by simp }
   379   then have "sigma_sets (space ?E) (sets (product_algebra_generator I (\<lambda>i. sigma (E i)))) \<subseteq> sets ?E"
   380     by (intro G.sigma_sets_subset) (auto simp add: product_algebra_generator_def)
   381   then have subset: "sets ?S \<subseteq> sets ?E"
   382     by (simp add: sets_sigma sets_product_algebra)
   383   show "sets ?S = sets ?E"
   384   proof (intro set_eqI iffI)
   385     fix A assume "A \<in> sets ?E" then show "A \<in> sets ?S"
   386       unfolding sets_sigma sets_product_algebra
   387     proof induct
   388       case (Basic A) then show ?case
   389         by (auto simp: sets_sigma product_algebra_generator_def intro: sigma_sets.Basic)
   390     qed (auto intro: sigma_sets.intros simp: product_algebra_generator_def)
   391   next
   392     fix A assume "A \<in> sets ?S" then show "A \<in> sets ?E" using subset by auto
   393   qed
   394 qed
   395 
   396 lemma product_algebraI[intro]:
   397     "E \<in> (\<Pi> i\<in>I. sets (M i)) \<Longrightarrow> Pi\<^isub>E I E \<in> sets (Pi\<^isub>M I M)"
   398   using assms unfolding product_algebra_def by (auto intro: product_algebra_generatorI)
   399 
   400 lemma (in product_sigma_algebra) measurable_component_update:
   401   assumes "x \<in> space (Pi\<^isub>M I M)" and "i \<notin> I"
   402   shows "(\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^isub>M (insert i I) M)" (is "?f \<in> _")
   403   unfolding product_algebra_def apply simp
   404 proof (intro measurable_sigma)
   405   let ?G = "product_algebra_generator (insert i I) M"
   406   show "sets ?G \<subseteq> Pow (space ?G)" using product_algebra_generator_into_space .
   407   show "?f \<in> space (M i) \<rightarrow> space ?G"
   408     using M.sets_into_space assms by auto
   409   fix A assume "A \<in> sets ?G"
   410   from product_algebraE[OF this] guess E . note E = this
   411   then have "?f -` A \<inter> space (M i) = E i \<or> ?f -` A \<inter> space (M i) = {}"
   412     using M.sets_into_space assms by auto
   413   then show "?f -` A \<inter> space (M i) \<in> sets (M i)"
   414     using E by (auto intro!: product_algebraI)
   415 qed
   416 
   417 lemma (in product_sigma_algebra) measurable_add_dim:
   418   assumes "i \<notin> I"
   419   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)"
   420 proof -
   421   let ?f = "(\<lambda>(f, y). f(i := y))" and ?G = "product_algebra_generator (insert i I) M"
   422   interpret Ii: pair_sigma_algebra "Pi\<^isub>M I M" "M i"
   423     unfolding pair_sigma_algebra_def
   424     by (intro sigma_algebra_product_algebra sigma_algebras conjI)
   425   have "?f \<in> measurable Ii.P (sigma ?G)"
   426   proof (rule Ii.measurable_sigma)
   427     show "sets ?G \<subseteq> Pow (space ?G)"
   428       using product_algebra_generator_into_space .
   429     show "?f \<in> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) \<rightarrow> space ?G"
   430       by (auto simp: space_pair_measure)
   431   next
   432     fix A assume "A \<in> sets ?G"
   433     then obtain F where "A = Pi\<^isub>E (insert i I) F"
   434       and F: "\<And>j. j \<in> I \<Longrightarrow> F j \<in> sets (M j)" "F i \<in> sets (M i)"
   435       by (auto elim!: product_algebraE)
   436     then have "?f -` A \<inter> space (Pi\<^isub>M I M \<Otimes>\<^isub>M M i) = Pi\<^isub>E I F \<times> (F i)"
   437       using sets_into_space `i \<notin> I`
   438       by (auto simp add: space_pair_measure) blast+
   439     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)"
   440       using F by (auto intro!: pair_measureI)
   441   qed
   442   then show ?thesis
   443     by (simp add: product_algebra_def)
   444 qed
   445 
   446 lemma (in product_sigma_algebra) measurable_merge:
   447   assumes [simp]: "I \<inter> J = {}"
   448   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)"
   449 proof -
   450   let ?I = "Pi\<^isub>M I M" and ?J = "Pi\<^isub>M J M"
   451   interpret P: sigma_algebra "?I \<Otimes>\<^isub>M ?J"
   452     by (intro sigma_algebra_pair_measure product_algebra_into_space)
   453   let ?f = "\<lambda>(x, y). merge I x J y"
   454   let ?G = "product_algebra_generator (I \<union> J) M"
   455   have "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (sigma ?G)"
   456   proof (rule P.measurable_sigma)
   457     fix A assume "A \<in> sets ?G"
   458     from product_algebraE[OF this]
   459     obtain E where E: "A = Pi\<^isub>E (I \<union> J) E" "E \<in> (\<Pi> i\<in>I \<union> J. sets (M i))" .
   460     then have *: "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) = Pi\<^isub>E I E \<times> Pi\<^isub>E J E"
   461       using sets_into_space `I \<inter> J = {}`
   462       by (auto simp add: space_pair_measure) fast+
   463     then show "?f -` A \<inter> space (?I \<Otimes>\<^isub>M ?J) \<in> sets (?I \<Otimes>\<^isub>M ?J)"
   464       using E unfolding * by (auto intro!: pair_measureI in_sigma product_algebra_generatorI
   465         simp: product_algebra_def)
   466   qed (insert product_algebra_generator_into_space, auto simp: space_pair_measure)
   467   then show "?f \<in> measurable (?I \<Otimes>\<^isub>M ?J) (Pi\<^isub>M (I \<union> J) M)"
   468     unfolding product_algebra_def[of "I \<union> J"] by simp
   469 qed
   470 
   471 lemma (in product_sigma_algebra) measurable_component_singleton:
   472   assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>M I M) (M i)"
   473 proof (unfold measurable_def, intro CollectI conjI ballI)
   474   fix A assume "A \<in> sets (M i)"
   475   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))"
   476     using M.sets_into_space `i \<in> I` by (fastsimp dest: Pi_mem split: split_if_asm)
   477   then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>M I M) \<in> sets (Pi\<^isub>M I M)"
   478     using `A \<in> sets (M i)` by (auto intro!: product_algebraI)
   479 qed (insert `i \<in> I`, auto)
   480 
   481 locale product_sigma_finite =
   482   fixes M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme"
   483   assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
   484 
   485 locale finite_product_sigma_finite = product_sigma_finite M
   486   for M :: "'i \<Rightarrow> ('a,'b) measure_space_scheme" +
   487   fixes I :: "'i set" assumes finite_index'[intro]: "finite I"
   488 
   489 sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" for i
   490   by (rule sigma_finite_measures)
   491 
   492 sublocale product_sigma_finite \<subseteq> product_sigma_algebra
   493   by default
   494 
   495 sublocale finite_product_sigma_finite \<subseteq> finite_product_sigma_algebra
   496   by default (fact finite_index')
   497 
   498 lemma (in finite_product_sigma_finite) product_algebra_generator_measure:
   499   assumes "Pi\<^isub>E I F \<in> sets G"
   500   shows "measure G (Pi\<^isub>E I F) = (\<Prod>i\<in>I. M.\<mu> i (F i))"
   501 proof cases
   502   assume ne: "\<forall>i\<in>I. F i \<noteq> {}"
   503   have "\<forall>i\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') i = F i"
   504     by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
   505        (insert ne, auto simp: Pi_eq_iff)
   506   then show ?thesis
   507     unfolding product_algebra_generator_def by simp
   508 next
   509   assume empty: "\<not> (\<forall>j\<in>I. F j \<noteq> {})"
   510   then have "(\<Prod>j\<in>I. M.\<mu> j (F j)) = 0"
   511     by (auto simp: setprod_extreal_0 intro!: bexI)
   512   moreover
   513   have "\<exists>j\<in>I. (SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j = {}"
   514     by (rule someI2[where P="\<lambda>F'. Pi\<^isub>E I F = Pi\<^isub>E I F'"])
   515        (insert empty, auto simp: Pi_eq_empty_iff[symmetric])
   516   then have "(\<Prod>j\<in>I. M.\<mu> j ((SOME F'. Pi\<^isub>E I F = Pi\<^isub>E I F') j)) = 0"
   517     by (auto simp: setprod_extreal_0 intro!: bexI)
   518   ultimately show ?thesis
   519     unfolding product_algebra_generator_def by simp
   520 qed
   521 
   522 lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   523   "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
   524     (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
   525     (\<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>
   526     (\<Union>k. \<Pi>\<^isub>E i\<in>I. F i k) = space G"
   527 proof -
   528   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>)"
   529     using M.sigma_finite_up by simp
   530   from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   531   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>"
   532     by auto
   533   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I. F i k"
   534   note space_product_algebra[simp]
   535   show ?thesis
   536   proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
   537     fix i show "range (F i) \<subseteq> sets (M i)" by fact
   538   next
   539     fix i k show "\<mu> i (F i k) \<noteq> \<infinity>" by fact
   540   next
   541     fix A assume "A \<in> (\<Union>i. ?F i)" then show "A \<in> space G"
   542       using `\<And>i. range (F i) \<subseteq> sets (M i)` M.sets_into_space
   543       by (force simp: image_subset_iff)
   544   next
   545     fix f assume "f \<in> space G"
   546     with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
   547     show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def)
   548   next
   549     fix i show "?F i \<subseteq> ?F (Suc i)"
   550       using `\<And>i. incseq (F i)`[THEN incseq_SucD] by auto
   551   qed
   552 qed
   553 
   554 lemma sets_pair_cancel_measure[simp]:
   555   "sets (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) = sets (M1 \<Otimes>\<^isub>M M2)"
   556   "sets (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) = sets (M1 \<Otimes>\<^isub>M M2)"
   557   unfolding pair_measure_def pair_measure_generator_def sets_sigma
   558   by simp_all
   559 
   560 lemma measurable_pair_cancel_measure[simp]:
   561   "measurable (M1\<lparr>measure := m1\<rparr> \<Otimes>\<^isub>M M2) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
   562   "measurable (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m2\<rparr>) M = measurable (M1 \<Otimes>\<^isub>M M2) M"
   563   "measurable M (M1\<lparr>measure := m3\<rparr> \<Otimes>\<^isub>M M2) = measurable M (M1 \<Otimes>\<^isub>M M2)"
   564   "measurable M (M1 \<Otimes>\<^isub>M M2\<lparr>measure := m4\<rparr>) = measurable M (M1 \<Otimes>\<^isub>M M2)"
   565   unfolding measurable_def by (auto simp add: space_pair_measure)
   566 
   567 lemma (in product_sigma_finite) product_measure_exists:
   568   assumes "finite I"
   569   shows "\<exists>\<nu>. sigma_finite_measure (sigma (product_algebra_generator I M) \<lparr> measure := \<nu> \<rparr>) \<and>
   570     (\<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)))"
   571 using `finite I` proof induct
   572   case empty
   573   interpret finite_product_sigma_finite M "{}" by default simp
   574   let ?\<nu> = "(\<lambda>A. if A = {} then 0 else 1) :: 'd set \<Rightarrow> extreal"
   575   show ?case
   576   proof (intro exI conjI ballI)
   577     have "sigma_algebra (sigma G \<lparr>measure := ?\<nu>\<rparr>)"
   578       by (rule sigma_algebra_cong) (simp_all add: product_algebra_def)
   579     then have "measure_space (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
   580       by (rule finite_additivity_sufficient)
   581          (simp_all add: positive_def additive_def sets_sigma
   582                         product_algebra_generator_def image_constant)
   583     then show "sigma_finite_measure (sigma G\<lparr>measure := ?\<nu>\<rparr>)"
   584       by (auto intro!: exI[of _ "\<lambda>i. {\<lambda>_. undefined}"]
   585                simp: sigma_finite_measure_def sigma_finite_measure_axioms_def
   586                      product_algebra_generator_def)
   587   qed auto
   588 next
   589   case (insert i I)
   590   interpret finite_product_sigma_finite M I by default fact
   591   have "finite (insert i I)" using `finite I` by auto
   592   interpret I': finite_product_sigma_finite M "insert i I" by default fact
   593   from insert obtain \<nu> where
   594     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
   595     "sigma_finite_measure (sigma G\<lparr> measure := \<nu> \<rparr>)" by auto
   596   then interpret I: sigma_finite_measure "P\<lparr> measure := \<nu>\<rparr>" unfolding product_algebra_def by simp
   597   interpret P: pair_sigma_finite "P\<lparr> measure := \<nu>\<rparr>" "M i" ..
   598   let ?h = "(\<lambda>(f, y). f(i := y))"
   599   let ?\<nu> = "\<lambda>A. P.\<mu> (?h -` A \<inter> space P.P)"
   600   have I': "sigma_algebra (I'.P\<lparr> measure := ?\<nu> \<rparr>)"
   601     by (rule I'.sigma_algebra_cong) simp_all
   602   interpret I'': measure_space "I'.P\<lparr> measure := ?\<nu> \<rparr>"
   603     using measurable_add_dim[OF `i \<notin> I`]
   604     by (intro P.measure_space_vimage[OF I']) (auto simp add: measure_preserving_def)
   605   show ?case
   606   proof (intro exI[of _ ?\<nu>] conjI ballI)
   607     let "?m A" = "measure (Pi\<^isub>M I M\<lparr>measure := \<nu>\<rparr> \<Otimes>\<^isub>M M i) (?h -` A \<inter> space P.P)"
   608     { fix A assume A: "A \<in> (\<Pi> i\<in>insert i I. sets (M i))"
   609       then have *: "?h -` Pi\<^isub>E (insert i I) A \<inter> space P.P = Pi\<^isub>E I A \<times> A i"
   610         using `i \<notin> I` M.sets_into_space by (auto simp: space_pair_measure space_product_algebra) blast
   611       show "?m (Pi\<^isub>E (insert i I) A) = (\<Prod>i\<in>insert i I. M.\<mu> i (A i))"
   612         unfolding * using A
   613         apply (subst P.pair_measure_times)
   614         using A apply fastsimp
   615         using A apply fastsimp
   616         using `i \<notin> I` `finite I` prod[of A] A by (auto simp: ac_simps) }
   617     note product = this
   618     have *: "sigma I'.G\<lparr> measure := ?\<nu> \<rparr> = I'.P\<lparr> measure := ?\<nu> \<rparr>"
   619       by (simp add: product_algebra_def)
   620     show "sigma_finite_measure (sigma I'.G\<lparr> measure := ?\<nu> \<rparr>)"
   621     proof (unfold *, default, simp)
   622       from I'.sigma_finite_pairs guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   623       then have F: "\<And>j. j \<in> insert i I \<Longrightarrow> range (F j) \<subseteq> sets (M j)"
   624         "incseq (\<lambda>k. \<Pi>\<^isub>E j \<in> insert i I. F j k)"
   625         "(\<Union>k. \<Pi>\<^isub>E j \<in> insert i I. F j k) = space I'.G"
   626         "\<And>k. \<And>j. j \<in> insert i I \<Longrightarrow> \<mu> j (F j k) \<noteq> \<infinity>"
   627         by blast+
   628       let "?F k" = "\<Pi>\<^isub>E j \<in> insert i I. F j k"
   629       show "\<exists>F::nat \<Rightarrow> ('i \<Rightarrow> 'a) set. range F \<subseteq> sets I'.P \<and>
   630           (\<Union>i. F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i)) \<and> (\<forall>i. ?m (F i) \<noteq> \<infinity>)"
   631       proof (intro exI[of _ ?F] conjI allI)
   632         show "range ?F \<subseteq> sets I'.P" using F(1) by auto
   633       next
   634         from F(3) show "(\<Union>i. ?F i) = (\<Pi>\<^isub>E i\<in>insert i I. space (M i))" by simp
   635       next
   636         fix j
   637         have "\<And>k. k \<in> insert i I \<Longrightarrow> 0 \<le> measure (M k) (F k j)"
   638           using F(1) by auto
   639         with F `finite I` setprod_PInf[of "insert i I", OF this] show "?\<nu> (?F j) \<noteq> \<infinity>"
   640           by (subst product) auto
   641       qed
   642     qed
   643   qed
   644 qed
   645 
   646 sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure P
   647   unfolding product_algebra_def
   648   using product_measure_exists[OF finite_index]
   649   by (rule someI2_ex) auto
   650 
   651 lemma (in finite_product_sigma_finite) measure_times:
   652   assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
   653   shows "\<mu> (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   654   unfolding product_algebra_def
   655   using product_measure_exists[OF finite_index]
   656   proof (rule someI2_ex, elim conjE)
   657     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))"
   658     have "Pi\<^isub>E I A = Pi\<^isub>E I (\<lambda>i\<in>I. A i)" by (auto dest: Pi_mem)
   659     then have "\<nu> (Pi\<^isub>E I A) = \<nu> (Pi\<^isub>E I (\<lambda>i\<in>I. A i))" by simp
   660     also have "\<dots> = (\<Prod>i\<in>I. M.\<mu> i ((\<lambda>i\<in>I. A i) i))" using assms * by auto
   661     finally show "measure (sigma G\<lparr>measure := \<nu>\<rparr>) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu> i (A i))"
   662       by (simp add: sigma_def)
   663   qed
   664 
   665 lemma (in product_sigma_finite) product_measure_empty[simp]:
   666   "measure (Pi\<^isub>M {} M) {\<lambda>x. undefined} = 1"
   667 proof -
   668   interpret finite_product_sigma_finite M "{}" by default auto
   669   from measure_times[of "\<lambda>x. {}"] show ?thesis by simp
   670 qed
   671 
   672 lemma (in finite_product_sigma_algebra) P_empty:
   673   assumes "I = {}"
   674   shows "space P = {\<lambda>k. undefined}" "sets P = { {}, {\<lambda>k. undefined} }"
   675   unfolding product_algebra_def product_algebra_generator_def `I = {}`
   676   by (simp_all add: sigma_def image_constant)
   677 
   678 lemma (in product_sigma_finite) positive_integral_empty:
   679   assumes pos: "0 \<le> f (\<lambda>k. undefined)"
   680   shows "integral\<^isup>P (Pi\<^isub>M {} M) f = f (\<lambda>k. undefined)"
   681 proof -
   682   interpret finite_product_sigma_finite M "{}" by default (fact finite.emptyI)
   683   have "\<And>A. measure (Pi\<^isub>M {} M) (Pi\<^isub>E {} A) = 1"
   684     using assms by (subst measure_times) auto
   685   then show ?thesis
   686     unfolding positive_integral_def simple_function_def simple_integral_def_raw
   687   proof (simp add: P_empty, intro antisym)
   688     show "f (\<lambda>k. undefined) \<le> (SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined))"
   689       by (intro le_SUPI) (auto simp: le_fun_def split: split_max)
   690     show "(SUP f:{g. g \<le> max 0 \<circ> f}. f (\<lambda>k. undefined)) \<le> f (\<lambda>k. undefined)" using pos
   691       by (intro SUP_leI) (auto simp: le_fun_def simp: max_def split: split_if_asm)
   692   qed
   693 qed
   694 
   695 lemma (in product_sigma_finite) measure_fold:
   696   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   697   assumes A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
   698   shows "measure (Pi\<^isub>M (I \<union> J) M) A =
   699     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))"
   700 proof -
   701   interpret I: finite_product_sigma_finite M I by default fact
   702   interpret J: finite_product_sigma_finite M J by default fact
   703   have "finite (I \<union> J)" using fin by auto
   704   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   705   interpret P: pair_sigma_finite I.P J.P by default
   706   let ?g = "\<lambda>(x,y). merge I x J y"
   707   let "?X S" = "?g -` S \<inter> space P.P"
   708   from IJ.sigma_finite_pairs obtain F where
   709     F: "\<And>i. i\<in> I \<union> J \<Longrightarrow> range (F i) \<subseteq> sets (M i)"
   710        "incseq (\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k)"
   711        "(\<Union>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k) = space IJ.G"
   712        "\<And>k. \<forall>i\<in>I\<union>J. \<mu> i (F i k) \<noteq> \<infinity>"
   713     by auto
   714   let ?F = "\<lambda>k. \<Pi>\<^isub>E i\<in>I \<union> J. F i k"
   715   show "IJ.\<mu> A = P.\<mu> (?X A)"
   716   proof (rule measure_unique_Int_stable_vimage)
   717     show "measure_space IJ.P" "measure_space P.P" by default
   718     show "sets (sigma IJ.G) = sets IJ.P" "space IJ.G = space IJ.P" "A \<in> sets (sigma IJ.G)"
   719       using A unfolding product_algebra_def by auto
   720   next
   721     show "Int_stable IJ.G"
   722       by (simp add: PiE_Int Int_stable_def product_algebra_def
   723                     product_algebra_generator_def)
   724           auto
   725     show "range ?F \<subseteq> sets IJ.G" using F
   726       by (simp add: image_subset_iff product_algebra_def
   727                     product_algebra_generator_def)
   728     show "incseq ?F" "(\<Union>i. ?F i) = space IJ.G " by fact+
   729   next
   730     fix k
   731     have "\<And>j. j \<in> I \<union> J \<Longrightarrow> 0 \<le> measure (M j) (F j k)"
   732       using F(1) by auto
   733     with F `finite I` setprod_PInf[of "I \<union> J", OF this]
   734     show "IJ.\<mu> (?F k) \<noteq> \<infinity>" by (subst IJ.measure_times) auto
   735   next
   736     fix A assume "A \<in> sets IJ.G"
   737     then obtain F where A: "A = Pi\<^isub>E (I \<union> J) F"
   738       and F: "\<And>i. i \<in> I \<union> J \<Longrightarrow> F i \<in> sets (M i)"
   739       by (auto simp: product_algebra_generator_def)
   740     then have X: "?X A = (Pi\<^isub>E I F \<times> Pi\<^isub>E J F)"
   741       using sets_into_space by (auto simp: space_pair_measure) blast+
   742     then have "P.\<mu> (?X A) = (\<Prod>i\<in>I. \<mu> i (F i)) * (\<Prod>i\<in>J. \<mu> i (F i))"
   743       using `finite J` `finite I` F
   744       by (simp add: P.pair_measure_times I.measure_times J.measure_times)
   745     also have "\<dots> = (\<Prod>i\<in>I \<union> J. \<mu> i (F i))"
   746       using `finite J` `finite I` `I \<inter> J = {}`  by (simp add: setprod_Un_one)
   747     also have "\<dots> = IJ.\<mu> A"
   748       using `finite J` `finite I` F unfolding A
   749       by (intro IJ.measure_times[symmetric]) auto
   750     finally show "IJ.\<mu> A = P.\<mu> (?X A)" by (rule sym)
   751   qed (rule measurable_merge[OF IJ])
   752 qed
   753 
   754 lemma (in product_sigma_finite) measure_preserving_merge:
   755   assumes IJ: "I \<inter> J = {}" and "finite I" "finite J"
   756   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)"
   757   by (intro measure_preservingI measurable_merge[OF IJ] measure_fold[symmetric] assms)
   758 
   759 lemma (in product_sigma_finite) product_positive_integral_fold:
   760   assumes IJ[simp]: "I \<inter> J = {}" "finite I" "finite J"
   761   and f: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   762   shows "integral\<^isup>P (Pi\<^isub>M (I \<union> J) M) f =
   763     (\<integral>\<^isup>+ x. (\<integral>\<^isup>+ y. f (merge I x J y) \<partial>(Pi\<^isub>M J M)) \<partial>(Pi\<^isub>M I M))"
   764 proof -
   765   interpret I: finite_product_sigma_finite M I by default fact
   766   interpret J: finite_product_sigma_finite M J by default fact
   767   interpret P: pair_sigma_finite "Pi\<^isub>M I M" "Pi\<^isub>M J M" by default
   768   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default simp
   769   have P_borel: "(\<lambda>x. f (case x of (x, y) \<Rightarrow> merge I x J y)) \<in> borel_measurable P.P"
   770     using measurable_comp[OF measurable_merge[OF IJ(1)] f] by (simp add: comp_def)
   771   show ?thesis
   772     unfolding P.positive_integral_fst_measurable[OF P_borel, simplified]
   773   proof (rule P.positive_integral_vimage)
   774     show "sigma_algebra IJ.P" by default
   775     show "(\<lambda>(x, y). merge I x J y) \<in> measure_preserving P.P IJ.P"
   776       using IJ by (rule measure_preserving_merge)
   777     show "f \<in> borel_measurable IJ.P" using f by simp
   778   qed
   779 qed
   780 
   781 lemma (in product_sigma_finite) measure_preserving_component_singelton:
   782   "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
   783 proof (intro measure_preservingI measurable_component_singleton)
   784   interpret I: finite_product_sigma_finite M "{i}" by default simp
   785   fix A let ?P = "(\<lambda>x. x i) -` A \<inter> space I.P"
   786   assume A: "A \<in> sets (M i)"
   787   then have *: "?P = {i} \<rightarrow>\<^isub>E A" using sets_into_space by auto
   788   show "I.\<mu> ?P = M.\<mu> i A" unfolding *
   789     using A I.measure_times[of "\<lambda>_. A"] by auto
   790 qed auto
   791 
   792 lemma (in product_sigma_finite) product_positive_integral_singleton:
   793   assumes f: "f \<in> borel_measurable (M i)"
   794   shows "integral\<^isup>P (Pi\<^isub>M {i} M) (\<lambda>x. f (x i)) = integral\<^isup>P (M i) f"
   795 proof -
   796   interpret I: finite_product_sigma_finite M "{i}" by default simp
   797   show ?thesis
   798   proof (rule I.positive_integral_vimage[symmetric])
   799     show "sigma_algebra (M i)" by (rule sigma_algebras)
   800     show "(\<lambda>x. x i) \<in> measure_preserving (Pi\<^isub>M {i} M) (M i)"
   801       by (rule measure_preserving_component_singelton)
   802     show "f \<in> borel_measurable (M i)" by fact
   803   qed
   804 qed
   805 
   806 lemma (in product_sigma_finite) product_positive_integral_insert:
   807   assumes [simp]: "finite I" "i \<notin> I"
   808     and f: "f \<in> borel_measurable (Pi\<^isub>M (insert i I) M)"
   809   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))"
   810 proof -
   811   interpret I: finite_product_sigma_finite M I by default auto
   812   interpret i: finite_product_sigma_finite M "{i}" by default auto
   813   interpret P: pair_sigma_algebra I.P i.P ..
   814   have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
   815     using f by auto
   816   show ?thesis
   817     unfolding product_positive_integral_fold[OF IJ, unfolded insert, simplified, OF f]
   818   proof (rule I.positive_integral_cong, subst product_positive_integral_singleton)
   819     fix x assume x: "x \<in> space I.P"
   820     let "?f y" = "f (restrict (x(i := y)) (insert i I))"
   821     have f'_eq: "\<And>y. ?f y = f (x(i := y))"
   822       using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
   823     show "?f \<in> borel_measurable (M i)" unfolding f'_eq
   824       using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
   825       by (simp add: comp_def)
   826     show "integral\<^isup>P (M i) ?f = \<integral>\<^isup>+ y. f (x(i:=y)) \<partial>M i"
   827       unfolding f'_eq by simp
   828   qed
   829 qed
   830 
   831 lemma (in product_sigma_finite) product_positive_integral_setprod:
   832   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> extreal"
   833   assumes "finite I" and borel: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
   834   and pos: "\<And>i x. i \<in> I \<Longrightarrow> 0 \<le> f i x"
   835   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))"
   836 using assms proof induct
   837   case empty
   838   interpret finite_product_sigma_finite M "{}" by default auto
   839   then show ?case by simp
   840 next
   841   case (insert i I)
   842   note `finite I`[intro, simp]
   843   interpret I: finite_product_sigma_finite M I by default auto
   844   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))"
   845     using insert by (auto intro!: setprod_cong)
   846   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)"
   847     using sets_into_space insert
   848     by (intro sigma_algebra.borel_measurable_extreal_setprod sigma_algebra_product_algebra
   849               measurable_comp[OF measurable_component_singleton, unfolded comp_def])
   850        auto
   851   then show ?case
   852     apply (simp add: product_positive_integral_insert[OF insert(1,2) prod])
   853     apply (simp add: insert * pos borel setprod_extreal_pos M.positive_integral_multc)
   854     apply (subst I.positive_integral_cmult)
   855     apply (auto simp add: pos borel insert setprod_extreal_pos positive_integral_positive)
   856     done
   857 qed
   858 
   859 lemma (in product_sigma_finite) product_integral_singleton:
   860   assumes f: "f \<in> borel_measurable (M i)"
   861   shows "(\<integral>x. f (x i) \<partial>Pi\<^isub>M {i} M) = integral\<^isup>L (M i) f"
   862 proof -
   863   interpret I: finite_product_sigma_finite M "{i}" by default simp
   864   have *: "(\<lambda>x. extreal (f x)) \<in> borel_measurable (M i)"
   865     "(\<lambda>x. extreal (- f x)) \<in> borel_measurable (M i)"
   866     using assms by auto
   867   show ?thesis
   868     unfolding lebesgue_integral_def *[THEN product_positive_integral_singleton] ..
   869 qed
   870 
   871 lemma (in product_sigma_finite) product_integral_fold:
   872   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   873   and f: "integrable (Pi\<^isub>M (I \<union> J) M) f"
   874   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)"
   875 proof -
   876   interpret I: finite_product_sigma_finite M I by default fact
   877   interpret J: finite_product_sigma_finite M J by default fact
   878   have "finite (I \<union> J)" using fin by auto
   879   interpret IJ: finite_product_sigma_finite M "I \<union> J" by default fact
   880   interpret P: pair_sigma_finite I.P J.P by default
   881   let ?M = "\<lambda>(x, y). merge I x J y"
   882   let ?f = "\<lambda>x. f (?M x)"
   883   show ?thesis
   884   proof (subst P.integrable_fst_measurable(2)[of ?f, simplified])
   885     have 1: "sigma_algebra IJ.P" by default
   886     have 2: "?M \<in> measure_preserving P.P IJ.P" using measure_preserving_merge[OF assms(1,2,3)] .
   887     have 3: "integrable (Pi\<^isub>M (I \<union> J) M) f" by fact
   888     then have 4: "f \<in> borel_measurable (Pi\<^isub>M (I \<union> J) M)"
   889       by (simp add: integrable_def)
   890     show "integrable P.P ?f"
   891       by (rule P.integrable_vimage[where f=f, OF 1 2 3])
   892     show "integral\<^isup>L IJ.P f = integral\<^isup>L P.P ?f"
   893       by (rule P.integral_vimage[where f=f, OF 1 2 4])
   894   qed
   895 qed
   896 
   897 lemma (in product_sigma_finite) product_integral_insert:
   898   assumes [simp]: "finite I" "i \<notin> I"
   899     and f: "integrable (Pi\<^isub>M (insert i I) M) f"
   900   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)"
   901 proof -
   902   interpret I: finite_product_sigma_finite M I by default auto
   903   interpret I': finite_product_sigma_finite M "insert i I" by default auto
   904   interpret i: finite_product_sigma_finite M "{i}" by default auto
   905   interpret P: pair_sigma_finite I.P i.P ..
   906   have IJ: "I \<inter> {i} = {}" by auto
   907   show ?thesis
   908     unfolding product_integral_fold[OF IJ, simplified, OF f]
   909   proof (rule I.integral_cong, subst product_integral_singleton)
   910     fix x assume x: "x \<in> space I.P"
   911     let "?f y" = "f (restrict (x(i := y)) (insert i I))"
   912     have f'_eq: "\<And>y. ?f y = f (x(i := y))"
   913       using x by (auto intro!: arg_cong[where f=f] simp: fun_eq_iff extensional_def)
   914     have f: "f \<in> borel_measurable I'.P" using f unfolding integrable_def by auto
   915     show "?f \<in> borel_measurable (M i)"
   916       unfolding measurable_cong[OF f'_eq]
   917       using measurable_comp[OF measurable_component_update f] x `i \<notin> I`
   918       by (simp add: comp_def)
   919     show "integral\<^isup>L (M i) ?f = integral\<^isup>L (M i) (\<lambda>y. f (x(i := y)))"
   920       unfolding f'_eq by simp
   921   qed
   922 qed
   923 
   924 lemma (in product_sigma_finite) product_integrable_setprod:
   925   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   926   assumes [simp]: "finite I" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   927   shows "integrable (Pi\<^isub>M I M) (\<lambda>x. (\<Prod>i\<in>I. f i (x i)))" (is "integrable _ ?f")
   928 proof -
   929   interpret finite_product_sigma_finite M I by default fact
   930   have f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
   931     using integrable unfolding integrable_def by auto
   932   then have borel: "?f \<in> borel_measurable P"
   933     using measurable_comp[OF measurable_component_singleton f]
   934     by (auto intro!: borel_measurable_setprod simp: comp_def)
   935   moreover have "integrable (Pi\<^isub>M I M) (\<lambda>x. \<bar>\<Prod>i\<in>I. f i (x i)\<bar>)"
   936   proof (unfold integrable_def, intro conjI)
   937     show "(\<lambda>x. abs (?f x)) \<in> borel_measurable P"
   938       using borel by auto
   939     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)"
   940       by (simp add: setprod_extreal abs_setprod)
   941     also have "\<dots> = (\<Prod>i\<in>I. (\<integral>\<^isup>+x. extreal (abs (f i x)) \<partial>M i))"
   942       using f by (subst product_positive_integral_setprod) auto
   943     also have "\<dots> < \<infinity>"
   944       using integrable[THEN M.integrable_abs]
   945       by (simp add: setprod_PInf integrable_def M.positive_integral_positive)
   946     finally show "(\<integral>\<^isup>+x. extreal (abs (?f x)) \<partial>P) \<noteq> \<infinity>" by auto
   947     have "(\<integral>\<^isup>+x. extreal (- abs (?f x)) \<partial>P) = (\<integral>\<^isup>+x. 0 \<partial>P)"
   948       by (intro positive_integral_cong_pos) auto
   949     then show "(\<integral>\<^isup>+x. extreal (- abs (?f x)) \<partial>P) \<noteq> \<infinity>" by simp
   950   qed
   951   ultimately show ?thesis
   952     by (rule integrable_abs_iff[THEN iffD1])
   953 qed
   954 
   955 lemma (in product_sigma_finite) product_integral_setprod:
   956   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> real"
   957   assumes "finite I" "I \<noteq> {}" and integrable: "\<And>i. i \<in> I \<Longrightarrow> integrable (M i) (f i)"
   958   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))"
   959 using assms proof (induct rule: finite_ne_induct)
   960   case (singleton i)
   961   then show ?case by (simp add: product_integral_singleton integrable_def)
   962 next
   963   case (insert i I)
   964   then have iI: "finite (insert i I)" by auto
   965   then have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow>
   966     integrable (Pi\<^isub>M J M) (\<lambda>x. (\<Prod>i\<in>J. f i (x i)))"
   967     by (intro product_integrable_setprod insert(5)) (auto intro: finite_subset)
   968   interpret I: finite_product_sigma_finite M I by default fact
   969   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))"
   970     using `i \<notin> I` by (auto intro!: setprod_cong)
   971   show ?case
   972     unfolding product_integral_insert[OF insert(1,3) prod[OF subset_refl]]
   973     by (simp add: * insert integral_multc I.integral_cmult[OF prod] subset_insertI)
   974 qed
   975 
   976 end