src/HOL/Probability/Finite_Product_Measure.thy
author haftmann
Tue Oct 13 09:21:15 2015 +0200 (2015-10-13)
changeset 61424 c3658c18b7bc
parent 61378 3e04c9ca001a
child 61565 352c73a689da
permissions -rw-r--r--
prod_case as canonical name for product type eliminator
     1 (*  Title:      HOL/Probability/Finite_Product_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 section {*Finite product measures*}
     6 
     7 theory Finite_Product_Measure
     8 imports Binary_Product_Measure
     9 begin
    10 
    11 lemma PiE_choice: "(\<exists>f\<in>PiE I F. \<forall>i\<in>I. P i (f i)) \<longleftrightarrow> (\<forall>i\<in>I. \<exists>x\<in>F i. P i x)"
    12   by (auto simp: Bex_def PiE_iff Ball_def dest!: choice_iff'[THEN iffD1])
    13      (force intro: exI[of _ "restrict f I" for f])
    14 
    15 lemma case_prod_const: "(\<lambda>(i, j). c) = (\<lambda>_. c)"
    16   by auto
    17 
    18 subsubsection {* More about Function restricted by @{const extensional}  *}
    19 
    20 definition
    21   "merge I J = (\<lambda>(x, y) i. if i \<in> I then x i else if i \<in> J then y i else undefined)"
    22 
    23 lemma merge_apply[simp]:
    24   "I \<inter> J = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    25   "I \<inter> J = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    26   "J \<inter> I = {} \<Longrightarrow> i \<in> I \<Longrightarrow> merge I J (x, y) i = x i"
    27   "J \<inter> I = {} \<Longrightarrow> i \<in> J \<Longrightarrow> merge I J (x, y) i = y i"
    28   "i \<notin> I \<Longrightarrow> i \<notin> J \<Longrightarrow> merge I J (x, y) i = undefined"
    29   unfolding merge_def by auto
    30 
    31 lemma merge_commute:
    32   "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) = merge J I (y, x)"
    33   by (force simp: merge_def)
    34 
    35 lemma Pi_cancel_merge_range[simp]:
    36   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    37   "I \<inter> J = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    38   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge I J (A, B)) \<longleftrightarrow> x \<in> Pi I A"
    39   "J \<inter> I = {} \<Longrightarrow> x \<in> Pi I (merge J I (B, A)) \<longleftrightarrow> x \<in> Pi I A"
    40   by (auto simp: Pi_def)
    41 
    42 lemma Pi_cancel_merge[simp]:
    43   "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    44   "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi I B \<longleftrightarrow> x \<in> Pi I B"
    45   "I \<inter> J = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    46   "J \<inter> I = {} \<Longrightarrow> merge I J (x, y) \<in> Pi J B \<longleftrightarrow> y \<in> Pi J B"
    47   by (auto simp: Pi_def)
    48 
    49 lemma extensional_merge[simp]: "merge I J (x, y) \<in> extensional (I \<union> J)"
    50   by (auto simp: extensional_def)
    51 
    52 lemma restrict_merge[simp]:
    53   "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    54   "I \<inter> J = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    55   "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) I = restrict x I"
    56   "J \<inter> I = {} \<Longrightarrow> restrict (merge I J (x, y)) J = restrict y J"
    57   by (auto simp: restrict_def)
    58 
    59 lemma split_merge: "P (merge I J (x,y) i) \<longleftrightarrow> (i \<in> I \<longrightarrow> P (x i)) \<and> (i \<in> J - I \<longrightarrow> P (y i)) \<and> (i \<notin> I \<union> J \<longrightarrow> P undefined)"
    60   unfolding merge_def by auto
    61 
    62 lemma PiE_cancel_merge[simp]:
    63   "I \<inter> J = {} \<Longrightarrow>
    64     merge I J (x, y) \<in> PiE (I \<union> J) B \<longleftrightarrow> x \<in> Pi I B \<and> y \<in> Pi J B"
    65   by (auto simp: PiE_def restrict_Pi_cancel)
    66 
    67 lemma merge_singleton[simp]: "i \<notin> I \<Longrightarrow> merge I {i} (x,y) = restrict (x(i := y i)) (insert i I)"
    68   unfolding merge_def by (auto simp: fun_eq_iff)
    69 
    70 lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I J (x, y) \<in> extensional K"
    71   unfolding merge_def extensional_def by auto
    72 
    73 lemma merge_restrict[simp]:
    74   "merge I J (restrict x I, y) = merge I J (x, y)"
    75   "merge I J (x, restrict y J) = merge I J (x, y)"
    76   unfolding merge_def by auto
    77 
    78 lemma merge_x_x_eq_restrict[simp]:
    79   "merge I J (x, x) = restrict x (I \<union> J)"
    80   unfolding merge_def by auto
    81 
    82 lemma injective_vimage_restrict:
    83   assumes J: "J \<subseteq> I"
    84   and sets: "A \<subseteq> (\<Pi>\<^sub>E i\<in>J. S i)" "B \<subseteq> (\<Pi>\<^sub>E i\<in>J. S i)" and ne: "(\<Pi>\<^sub>E i\<in>I. S i) \<noteq> {}"
    85   and eq: "(\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^sub>E i\<in>I. S i) = (\<lambda>x. restrict x J) -` B \<inter> (\<Pi>\<^sub>E i\<in>I. S i)"
    86   shows "A = B"
    87 proof  (intro set_eqI)
    88   fix x
    89   from ne obtain y where y: "\<And>i. i \<in> I \<Longrightarrow> y i \<in> S i" by auto
    90   have "J \<inter> (I - J) = {}" by auto
    91   show "x \<in> A \<longleftrightarrow> x \<in> B"
    92   proof cases
    93     assume x: "x \<in> (\<Pi>\<^sub>E i\<in>J. S i)"
    94     have "x \<in> A \<longleftrightarrow> merge J (I - J) (x,y) \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^sub>E i\<in>I. S i)"
    95       using y x `J \<subseteq> I` PiE_cancel_merge[of "J" "I - J" x y S]
    96       by (auto simp del: PiE_cancel_merge simp add: Un_absorb1)
    97     then show "x \<in> A \<longleftrightarrow> x \<in> B"
    98       using y x `J \<subseteq> I` PiE_cancel_merge[of "J" "I - J" x y S]
    99       by (auto simp del: PiE_cancel_merge simp add: Un_absorb1 eq)
   100   qed (insert sets, auto)
   101 qed
   102 
   103 lemma restrict_vimage:
   104   "I \<inter> J = {} \<Longrightarrow>
   105     (\<lambda>x. (restrict x I, restrict x J)) -` (Pi\<^sub>E I E \<times> Pi\<^sub>E J F) = Pi (I \<union> J) (merge I J (E, F))"
   106   by (auto simp: restrict_Pi_cancel PiE_def)
   107 
   108 lemma merge_vimage:
   109   "I \<inter> J = {} \<Longrightarrow> merge I J -` Pi\<^sub>E (I \<union> J) E = Pi I E \<times> Pi J E"
   110   by (auto simp: restrict_Pi_cancel PiE_def)
   111 
   112 subsection {* Finite product spaces *}
   113 
   114 subsubsection {* Products *}
   115 
   116 definition prod_emb where
   117   "prod_emb I M K X = (\<lambda>x. restrict x K) -` X \<inter> (PIE i:I. space (M i))"
   118 
   119 lemma prod_emb_iff: 
   120   "f \<in> prod_emb I M K X \<longleftrightarrow> f \<in> extensional I \<and> (restrict f K \<in> X) \<and> (\<forall>i\<in>I. f i \<in> space (M i))"
   121   unfolding prod_emb_def PiE_def by auto
   122 
   123 lemma
   124   shows prod_emb_empty[simp]: "prod_emb M L K {} = {}"
   125     and prod_emb_Un[simp]: "prod_emb M L K (A \<union> B) = prod_emb M L K A \<union> prod_emb M L K B"
   126     and prod_emb_Int: "prod_emb M L K (A \<inter> B) = prod_emb M L K A \<inter> prod_emb M L K B"
   127     and prod_emb_UN[simp]: "prod_emb M L K (\<Union>i\<in>I. F i) = (\<Union>i\<in>I. prod_emb M L K (F i))"
   128     and prod_emb_INT[simp]: "I \<noteq> {} \<Longrightarrow> prod_emb M L K (\<Inter>i\<in>I. F i) = (\<Inter>i\<in>I. prod_emb M L K (F i))"
   129     and prod_emb_Diff[simp]: "prod_emb M L K (A - B) = prod_emb M L K A - prod_emb M L K B"
   130   by (auto simp: prod_emb_def)
   131 
   132 lemma prod_emb_PiE: "J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow>
   133     prod_emb I M J (\<Pi>\<^sub>E i\<in>J. E i) = (\<Pi>\<^sub>E i\<in>I. if i \<in> J then E i else space (M i))"
   134   by (force simp: prod_emb_def PiE_iff split_if_mem2)
   135 
   136 lemma prod_emb_PiE_same_index[simp]:
   137     "(\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> space (M i)) \<Longrightarrow> prod_emb I M I (Pi\<^sub>E I E) = Pi\<^sub>E I E"
   138   by (auto simp: prod_emb_def PiE_iff)
   139 
   140 lemma prod_emb_trans[simp]:
   141   "J \<subseteq> K \<Longrightarrow> K \<subseteq> L \<Longrightarrow> prod_emb L M K (prod_emb K M J X) = prod_emb L M J X"
   142   by (auto simp add: Int_absorb1 prod_emb_def PiE_def)
   143 
   144 lemma prod_emb_Pi:
   145   assumes "X \<in> (\<Pi> j\<in>J. sets (M j))" "J \<subseteq> K"
   146   shows "prod_emb K M J (Pi\<^sub>E J X) = (\<Pi>\<^sub>E i\<in>K. if i \<in> J then X i else space (M i))"
   147   using assms sets.space_closed
   148   by (auto simp: prod_emb_def PiE_iff split: split_if_asm) blast+
   149 
   150 lemma prod_emb_id:
   151   "B \<subseteq> (\<Pi>\<^sub>E i\<in>L. space (M i)) \<Longrightarrow> prod_emb L M L B = B"
   152   by (auto simp: prod_emb_def subset_eq extensional_restrict)
   153 
   154 lemma prod_emb_mono:
   155   "F \<subseteq> G \<Longrightarrow> prod_emb A M B F \<subseteq> prod_emb A M B G"
   156   by (auto simp: prod_emb_def)
   157 
   158 definition PiM :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) measure" where
   159   "PiM I M = extend_measure (\<Pi>\<^sub>E i\<in>I. space (M i))
   160     {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}
   161     (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^sub>E j\<in>J. X j))
   162     (\<lambda>(J, X). \<Prod>j\<in>J \<union> {i\<in>I. emeasure (M i) (space (M i)) \<noteq> 1}. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j)))"
   163 
   164 definition prod_algebra :: "'i set \<Rightarrow> ('i \<Rightarrow> 'a measure) \<Rightarrow> ('i \<Rightarrow> 'a) set set" where
   165   "prod_algebra I M = (\<lambda>(J, X). prod_emb I M J (\<Pi>\<^sub>E j\<in>J. X j)) `
   166     {(J, X). (J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))}"
   167 
   168 abbreviation
   169   "Pi\<^sub>M I M \<equiv> PiM I M"
   170 
   171 syntax
   172   "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3PIM _:_./ _)" 10)
   173 syntax (xsymbols)
   174   "_PiM" :: "pttrn \<Rightarrow> 'i set \<Rightarrow> 'a measure \<Rightarrow> ('i => 'a) measure"  ("(3\<Pi>\<^sub>M _\<in>_./ _)"  10)
   175 translations
   176   "PIM x:I. M" == "CONST PiM I (%x. M)"
   177 
   178 lemma extend_measure_cong:
   179   assumes "\<Omega> = \<Omega>'" "I = I'" "G = G'" "\<And>i. i \<in> I' \<Longrightarrow> \<mu> i = \<mu>' i"
   180   shows "extend_measure \<Omega> I G \<mu> = extend_measure \<Omega>' I' G' \<mu>'"
   181   unfolding extend_measure_def by (auto simp add: assms)
   182 
   183 lemma Pi_cong_sets:
   184     "\<lbrakk>I = J; \<And>x. x \<in> I \<Longrightarrow> M x = N x\<rbrakk> \<Longrightarrow> Pi I M = Pi J N"
   185   unfolding Pi_def by auto 
   186 
   187 lemma PiM_cong:
   188   assumes "I = J" "\<And>x. x \<in> I \<Longrightarrow> M x = N x"
   189   shows "PiM I M = PiM J N"
   190   unfolding PiM_def
   191 proof (rule extend_measure_cong, goal_cases)
   192   case 1
   193   show ?case using assms
   194     by (subst assms(1), intro PiE_cong[of J "\<lambda>i. space (M i)" "\<lambda>i. space (N i)"]) simp_all
   195 next
   196   case 2
   197   have "\<And>K. K \<subseteq> J \<Longrightarrow> (\<Pi> j\<in>K. sets (M j)) = (\<Pi> j\<in>K. sets (N j))"
   198     using assms by (intro Pi_cong_sets) auto
   199   thus ?case by (auto simp: assms)
   200 next
   201   case 3
   202   show ?case using assms 
   203     by (intro ext) (auto simp: prod_emb_def dest: PiE_mem)
   204 next
   205   case (4 x)
   206   thus ?case using assms 
   207     by (auto intro!: setprod.cong split: split_if_asm)
   208 qed
   209 
   210 
   211 lemma prod_algebra_sets_into_space:
   212   "prod_algebra I M \<subseteq> Pow (\<Pi>\<^sub>E i\<in>I. space (M i))"
   213   by (auto simp: prod_emb_def prod_algebra_def)
   214 
   215 lemma prod_algebra_eq_finite:
   216   assumes I: "finite I"
   217   shows "prod_algebra I M = {(\<Pi>\<^sub>E i\<in>I. X i) |X. X \<in> (\<Pi> j\<in>I. sets (M j))}" (is "?L = ?R")
   218 proof (intro iffI set_eqI)
   219   fix A assume "A \<in> ?L"
   220   then obtain J E where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   221     and A: "A = prod_emb I M J (PIE j:J. E j)"
   222     by (auto simp: prod_algebra_def)
   223   let ?A = "\<Pi>\<^sub>E i\<in>I. if i \<in> J then E i else space (M i)"
   224   have A: "A = ?A"
   225     unfolding A using J by (intro prod_emb_PiE sets.sets_into_space) auto
   226   show "A \<in> ?R" unfolding A using J sets.top
   227     by (intro CollectI exI[of _ "\<lambda>i. if i \<in> J then E i else space (M i)"]) simp
   228 next
   229   fix A assume "A \<in> ?R"
   230   then obtain X where A: "A = (\<Pi>\<^sub>E i\<in>I. X i)" and X: "X \<in> (\<Pi> j\<in>I. sets (M j))" by auto
   231   then have A: "A = prod_emb I M I (\<Pi>\<^sub>E i\<in>I. X i)"
   232     by (simp add: prod_emb_PiE_same_index[OF sets.sets_into_space] Pi_iff)
   233   from X I show "A \<in> ?L" unfolding A
   234     by (auto simp: prod_algebra_def)
   235 qed
   236 
   237 lemma prod_algebraI:
   238   "finite J \<Longrightarrow> (J \<noteq> {} \<or> I = {}) \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i))
   239     \<Longrightarrow> prod_emb I M J (PIE j:J. E j) \<in> prod_algebra I M"
   240   by (auto simp: prod_algebra_def)
   241 
   242 lemma prod_algebraI_finite:
   243   "finite I \<Longrightarrow> (\<forall>i\<in>I. E i \<in> sets (M i)) \<Longrightarrow> (Pi\<^sub>E I E) \<in> prod_algebra I M"
   244   using prod_algebraI[of I I E M] prod_emb_PiE_same_index[of I E M, OF sets.sets_into_space] by simp
   245 
   246 lemma Int_stable_PiE: "Int_stable {Pi\<^sub>E J E | E. \<forall>i\<in>I. E i \<in> sets (M i)}"
   247 proof (safe intro!: Int_stableI)
   248   fix E F assume "\<forall>i\<in>I. E i \<in> sets (M i)" "\<forall>i\<in>I. F i \<in> sets (M i)"
   249   then show "\<exists>G. Pi\<^sub>E J E \<inter> Pi\<^sub>E J F = Pi\<^sub>E J G \<and> (\<forall>i\<in>I. G i \<in> sets (M i))"
   250     by (auto intro!: exI[of _ "\<lambda>i. E i \<inter> F i"] simp: PiE_Int)
   251 qed
   252 
   253 lemma prod_algebraE:
   254   assumes A: "A \<in> prod_algebra I M"
   255   obtains J E where "A = prod_emb I M J (PIE j:J. E j)"
   256     "finite J" "J \<noteq> {} \<or> I = {}" "J \<subseteq> I" "\<And>i. i \<in> J \<Longrightarrow> E i \<in> sets (M i)" 
   257   using A by (auto simp: prod_algebra_def)
   258 
   259 lemma prod_algebraE_all:
   260   assumes A: "A \<in> prod_algebra I M"
   261   obtains E where "A = Pi\<^sub>E I E" "E \<in> (\<Pi> i\<in>I. sets (M i))"
   262 proof -
   263   from A obtain E J where A: "A = prod_emb I M J (Pi\<^sub>E J E)"
   264     and J: "J \<subseteq> I" and E: "E \<in> (\<Pi> i\<in>J. sets (M i))"
   265     by (auto simp: prod_algebra_def)
   266   from E have "\<And>i. i \<in> J \<Longrightarrow> E i \<subseteq> space (M i)"
   267     using sets.sets_into_space by auto
   268   then have "A = (\<Pi>\<^sub>E i\<in>I. if i\<in>J then E i else space (M i))"
   269     using A J by (auto simp: prod_emb_PiE)
   270   moreover have "(\<lambda>i. if i\<in>J then E i else space (M i)) \<in> (\<Pi> i\<in>I. sets (M i))"
   271     using sets.top E by auto
   272   ultimately show ?thesis using that by auto
   273 qed
   274 
   275 lemma Int_stable_prod_algebra: "Int_stable (prod_algebra I M)"
   276 proof (unfold Int_stable_def, safe)
   277   fix A assume "A \<in> prod_algebra I M"
   278   from prod_algebraE[OF this] guess J E . note A = this
   279   fix B assume "B \<in> prod_algebra I M"
   280   from prod_algebraE[OF this] guess K F . note B = this
   281   have "A \<inter> B = prod_emb I M (J \<union> K) (\<Pi>\<^sub>E i\<in>J \<union> K. (if i \<in> J then E i else space (M i)) \<inter> 
   282       (if i \<in> K then F i else space (M i)))"
   283     unfolding A B using A(2,3,4) A(5)[THEN sets.sets_into_space] B(2,3,4)
   284       B(5)[THEN sets.sets_into_space]
   285     apply (subst (1 2 3) prod_emb_PiE)
   286     apply (simp_all add: subset_eq PiE_Int)
   287     apply blast
   288     apply (intro PiE_cong)
   289     apply auto
   290     done
   291   also have "\<dots> \<in> prod_algebra I M"
   292     using A B by (auto intro!: prod_algebraI)
   293   finally show "A \<inter> B \<in> prod_algebra I M" .
   294 qed
   295 
   296 lemma prod_algebra_mono:
   297   assumes space: "\<And>i. i \<in> I \<Longrightarrow> space (E i) = space (F i)"
   298   assumes sets: "\<And>i. i \<in> I \<Longrightarrow> sets (E i) \<subseteq> sets (F i)"
   299   shows "prod_algebra I E \<subseteq> prod_algebra I F"
   300 proof
   301   fix A assume "A \<in> prod_algebra I E"
   302   then obtain J G where J: "J \<noteq> {} \<or> I = {}" "finite J" "J \<subseteq> I"
   303     and A: "A = prod_emb I E J (\<Pi>\<^sub>E i\<in>J. G i)"
   304     and G: "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (E i)"
   305     by (auto simp: prod_algebra_def)
   306   moreover
   307   from space have "(\<Pi>\<^sub>E i\<in>I. space (E i)) = (\<Pi>\<^sub>E i\<in>I. space (F i))"
   308     by (rule PiE_cong)
   309   with A have "A = prod_emb I F J (\<Pi>\<^sub>E i\<in>J. G i)"
   310     by (simp add: prod_emb_def)
   311   moreover
   312   from sets G J have "\<And>i. i \<in> J \<Longrightarrow> G i \<in> sets (F i)"
   313     by auto
   314   ultimately show "A \<in> prod_algebra I F"
   315     apply (simp add: prod_algebra_def image_iff)
   316     apply (intro exI[of _ J] exI[of _ G] conjI)
   317     apply auto
   318     done
   319 qed
   320 
   321 lemma prod_algebra_cong:
   322   assumes "I = J" and sets: "(\<And>i. i \<in> I \<Longrightarrow> sets (M i) = sets (N i))"
   323   shows "prod_algebra I M = prod_algebra J N"
   324 proof -
   325   have space: "\<And>i. i \<in> I \<Longrightarrow> space (M i) = space (N i)"
   326     using sets_eq_imp_space_eq[OF sets] by auto
   327   with sets show ?thesis unfolding `I = J`
   328     by (intro antisym prod_algebra_mono) auto
   329 qed
   330 
   331 lemma space_in_prod_algebra:
   332   "(\<Pi>\<^sub>E i\<in>I. space (M i)) \<in> prod_algebra I M"
   333 proof cases
   334   assume "I = {}" then show ?thesis
   335     by (auto simp add: prod_algebra_def image_iff prod_emb_def)
   336 next
   337   assume "I \<noteq> {}"
   338   then obtain i where "i \<in> I" by auto
   339   then have "(\<Pi>\<^sub>E i\<in>I. space (M i)) = prod_emb I M {i} (\<Pi>\<^sub>E i\<in>{i}. space (M i))"
   340     by (auto simp: prod_emb_def)
   341   also have "\<dots> \<in> prod_algebra I M"
   342     using `i \<in> I` by (intro prod_algebraI) auto
   343   finally show ?thesis .
   344 qed
   345 
   346 lemma space_PiM: "space (\<Pi>\<^sub>M i\<in>I. M i) = (\<Pi>\<^sub>E i\<in>I. space (M i))"
   347   using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro space_extend_measure) simp
   348 
   349 lemma prod_emb_subset_PiM[simp]: "prod_emb I M K X \<subseteq> space (PiM I M)"
   350   by (auto simp: prod_emb_def space_PiM)
   351 
   352 lemma space_PiM_empty_iff[simp]: "space (PiM I M) = {} \<longleftrightarrow>  (\<exists>i\<in>I. space (M i) = {})"
   353   by (auto simp: space_PiM PiE_eq_empty_iff)
   354 
   355 lemma undefined_in_PiM_empty[simp]: "(\<lambda>x. undefined) \<in> space (PiM {} M)"
   356   by (auto simp: space_PiM)
   357 
   358 lemma sets_PiM: "sets (\<Pi>\<^sub>M i\<in>I. M i) = sigma_sets (\<Pi>\<^sub>E i\<in>I. space (M i)) (prod_algebra I M)"
   359   using prod_algebra_sets_into_space unfolding PiM_def prod_algebra_def by (intro sets_extend_measure) simp
   360 
   361 lemma sets_PiM_single: "sets (PiM I M) =
   362     sigma_sets (\<Pi>\<^sub>E i\<in>I. space (M i)) {{f\<in>\<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> A} | i A. i \<in> I \<and> A \<in> sets (M i)}"
   363     (is "_ = sigma_sets ?\<Omega> ?R")
   364   unfolding sets_PiM
   365 proof (rule sigma_sets_eqI)
   366   interpret R: sigma_algebra ?\<Omega> "sigma_sets ?\<Omega> ?R" by (rule sigma_algebra_sigma_sets) auto
   367   fix A assume "A \<in> prod_algebra I M"
   368   from prod_algebraE[OF this] guess J X . note X = this
   369   show "A \<in> sigma_sets ?\<Omega> ?R"
   370   proof cases
   371     assume "I = {}"
   372     with X have "A = {\<lambda>x. undefined}" by (auto simp: prod_emb_def)
   373     with `I = {}` show ?thesis by (auto intro!: sigma_sets_top)
   374   next
   375     assume "I \<noteq> {}"
   376     with X have "A = (\<Inter>j\<in>J. {f\<in>(\<Pi>\<^sub>E i\<in>I. space (M i)). f j \<in> X j})"
   377       by (auto simp: prod_emb_def)
   378     also have "\<dots> \<in> sigma_sets ?\<Omega> ?R"
   379       using X `I \<noteq> {}` by (intro R.finite_INT sigma_sets.Basic) auto
   380     finally show "A \<in> sigma_sets ?\<Omega> ?R" .
   381   qed
   382 next
   383   fix A assume "A \<in> ?R"
   384   then obtain i B where A: "A = {f\<in>\<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> B}" "i \<in> I" "B \<in> sets (M i)" 
   385     by auto
   386   then have "A = prod_emb I M {i} (\<Pi>\<^sub>E i\<in>{i}. B)"
   387      by (auto simp: prod_emb_def)
   388   also have "\<dots> \<in> sigma_sets ?\<Omega> (prod_algebra I M)"
   389     using A by (intro sigma_sets.Basic prod_algebraI) auto
   390   finally show "A \<in> sigma_sets ?\<Omega> (prod_algebra I M)" .
   391 qed
   392 
   393 lemma sets_PiM_eq_proj:
   394   "I \<noteq> {} \<Longrightarrow> sets (PiM I M) = sets (\<Squnion>\<^sub>\<sigma> i\<in>I. vimage_algebra (\<Pi>\<^sub>E i\<in>I. space (M i)) (\<lambda>x. x i) (M i))"
   395   apply (simp add: sets_PiM_single sets_Sup_sigma)
   396   apply (subst SUP_cong[OF refl])
   397   apply (rule sets_vimage_algebra2)
   398   apply auto []
   399   apply (auto intro!: arg_cong2[where f=sigma_sets])
   400   done
   401 
   402 lemma
   403   shows space_PiM_empty: "space (Pi\<^sub>M {} M) = {\<lambda>k. undefined}"
   404     and sets_PiM_empty: "sets (Pi\<^sub>M {} M) = { {}, {\<lambda>k. undefined} }"
   405   by (simp_all add: space_PiM sets_PiM_single image_constant sigma_sets_empty_eq)
   406 
   407 lemma sets_PiM_sigma:
   408   assumes \<Omega>_cover: "\<And>i. i \<in> I \<Longrightarrow> \<exists>S\<subseteq>E i. countable S \<and> \<Omega> i = \<Union>S"
   409   assumes E: "\<And>i. i \<in> I \<Longrightarrow> E i \<subseteq> Pow (\<Omega> i)"
   410   assumes J: "\<And>j. j \<in> J \<Longrightarrow> finite j" "\<Union>J = I"
   411   defines "P \<equiv> {{f\<in>(\<Pi>\<^sub>E i\<in>I. \<Omega> i). \<forall>i\<in>j. f i \<in> A i} | A j. j \<in> J \<and> A \<in> Pi j E}"
   412   shows "sets (\<Pi>\<^sub>M i\<in>I. sigma (\<Omega> i) (E i)) = sets (sigma (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P)"
   413 proof cases
   414   assume "I = {}" 
   415   with `\<Union>J = I` have "P = {{\<lambda>_. undefined}} \<or> P = {}"
   416     by (auto simp: P_def)
   417   with `I = {}` show ?thesis
   418     by (auto simp add: sets_PiM_empty sigma_sets_empty_eq)
   419 next
   420   let ?F = "\<lambda>i. {(\<lambda>x. x i) -` A \<inter> Pi\<^sub>E I \<Omega> |A. A \<in> E i}"
   421   assume "I \<noteq> {}"
   422   then have "sets (Pi\<^sub>M I (\<lambda>i. sigma (\<Omega> i) (E i))) = 
   423       sets (\<Squnion>\<^sub>\<sigma> i\<in>I. vimage_algebra (\<Pi>\<^sub>E i\<in>I. \<Omega> i) (\<lambda>x. x i) (sigma (\<Omega> i) (E i)))"
   424     by (subst sets_PiM_eq_proj) (auto simp: space_measure_of_conv)
   425   also have "\<dots> = sets (\<Squnion>\<^sub>\<sigma> i\<in>I. sigma (Pi\<^sub>E I \<Omega>) (?F i))"
   426     using E by (intro SUP_sigma_cong arg_cong[where f=sets] vimage_algebra_sigma) auto
   427   also have "\<dots> = sets (sigma (Pi\<^sub>E I \<Omega>) (\<Union>i\<in>I. ?F i))"
   428     using `I \<noteq> {}` by (intro arg_cong[where f=sets] SUP_sigma_sigma) auto
   429   also have "\<dots> = sets (sigma (Pi\<^sub>E I \<Omega>) P)"
   430   proof (intro arg_cong[where f=sets] sigma_eqI sigma_sets_eqI)
   431     show "(\<Union>i\<in>I. ?F i) \<subseteq> Pow (Pi\<^sub>E I \<Omega>)" "P \<subseteq> Pow (Pi\<^sub>E I \<Omega>)"
   432       by (auto simp: P_def)
   433   next
   434     interpret P: sigma_algebra "\<Pi>\<^sub>E i\<in>I. \<Omega> i" "sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P"
   435       by (auto intro!: sigma_algebra_sigma_sets simp: P_def)
   436 
   437     fix Z assume "Z \<in> (\<Union>i\<in>I. ?F i)"
   438     then obtain i A where i: "i \<in> I" "A \<in> E i" and Z_def: "Z = (\<lambda>x. x i) -` A \<inter> Pi\<^sub>E I \<Omega>"
   439       by auto
   440     from `i \<in> I` J obtain j where j: "i \<in> j" "j \<in> J" "j \<subseteq> I" "finite j"
   441       by auto
   442     obtain S where S: "\<And>i. i \<in> j \<Longrightarrow> S i \<subseteq> E i" "\<And>i. i \<in> j \<Longrightarrow> countable (S i)"
   443       "\<And>i. i \<in> j \<Longrightarrow> \<Omega> i = \<Union>(S i)"
   444       by (metis subset_eq \<Omega>_cover `j \<subseteq> I`)
   445     def A' \<equiv> "\<lambda>n. n(i := A)"
   446     then have A'_i: "\<And>n. A' n i = A"
   447       by simp
   448     { fix n assume "n \<in> Pi\<^sub>E (j - {i}) S"
   449       then have "A' n \<in> Pi j E"
   450         unfolding PiE_def Pi_def using S(1) by (auto simp: A'_def `A \<in> E i` )
   451       with `j \<in> J` have "{f \<in> Pi\<^sub>E I \<Omega>. \<forall>i\<in>j. f i \<in> A' n i} \<in> P"
   452         by (auto simp: P_def) }
   453     note A'_in_P = this
   454 
   455     { fix x assume "x i \<in> A" "x \<in> Pi\<^sub>E I \<Omega>"
   456       with S(3) `j \<subseteq> I` have "\<forall>i\<in>j. \<exists>s\<in>S i. x i \<in> s"
   457         by (auto simp: PiE_def Pi_def)
   458       then obtain s where s: "\<And>i. i \<in> j \<Longrightarrow> s i \<in> S i" "\<And>i. i \<in> j \<Longrightarrow> x i \<in> s i"
   459         by metis
   460       with `x i \<in> A` have "\<exists>n\<in>PiE (j-{i}) S. \<forall>i\<in>j. x i \<in> A' n i"
   461         by (intro bexI[of _ "restrict (s(i := A)) (j-{i})"]) (auto simp: A'_def split: if_splits) }
   462     then have "Z = (\<Union>n\<in>PiE (j-{i}) S. {f\<in>(\<Pi>\<^sub>E i\<in>I. \<Omega> i). \<forall>i\<in>j. f i \<in> A' n i})"
   463       unfolding Z_def
   464       by (auto simp add: set_eq_iff ball_conj_distrib `i\<in>j` A'_i dest: bspec[OF _ `i\<in>j`]
   465                cong: conj_cong)
   466     also have "\<dots> \<in> sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P"
   467       using `finite j` S(2)
   468       by (intro P.countable_UN' countable_PiE) (simp_all add: image_subset_iff A'_in_P)
   469     finally show "Z \<in> sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) P" .
   470   next
   471     interpret F: sigma_algebra "\<Pi>\<^sub>E i\<in>I. \<Omega> i" "sigma_sets (\<Pi>\<^sub>E i\<in>I. \<Omega> i) (\<Union>i\<in>I. ?F i)"
   472       by (auto intro!: sigma_algebra_sigma_sets)
   473 
   474     fix b assume "b \<in> P"
   475     then obtain A j where b: "b = {f\<in>(\<Pi>\<^sub>E i\<in>I. \<Omega> i). \<forall>i\<in>j. f i \<in> A i}" "j \<in> J" "A \<in> Pi j E"
   476       by (auto simp: P_def)
   477     show "b \<in> sigma_sets (Pi\<^sub>E I \<Omega>) (\<Union>i\<in>I. ?F i)"
   478     proof cases
   479       assume "j = {}"
   480       with b have "b = (\<Pi>\<^sub>E i\<in>I. \<Omega> i)"
   481         by auto
   482       then show ?thesis
   483         by blast
   484     next
   485       assume "j \<noteq> {}"
   486       with J b(2,3) have eq: "b = (\<Inter>i\<in>j. ((\<lambda>x. x i) -` A i \<inter> Pi\<^sub>E I \<Omega>))"
   487         unfolding b(1)
   488         by (auto simp: PiE_def Pi_def)
   489       show ?thesis
   490         unfolding eq using `A \<in> Pi j E` `j \<in> J` J(2)
   491         by (intro F.finite_INT J `j \<in> J` `j \<noteq> {}` sigma_sets.Basic) blast
   492     qed
   493   qed
   494   finally show "?thesis" .
   495 qed
   496 
   497 lemma sets_PiM_in_sets:
   498   assumes space: "space N = (\<Pi>\<^sub>E i\<in>I. space (M i))"
   499   assumes sets: "\<And>i A. i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> {x\<in>space N. x i \<in> A} \<in> sets N"
   500   shows "sets (\<Pi>\<^sub>M i \<in> I. M i) \<subseteq> sets N"
   501   unfolding sets_PiM_single space[symmetric]
   502   by (intro sets.sigma_sets_subset subsetI) (auto intro: sets)
   503 
   504 lemma sets_PiM_cong[measurable_cong]:
   505   assumes "I = J" "\<And>i. i \<in> J \<Longrightarrow> sets (M i) = sets (N i)" shows "sets (PiM I M) = sets (PiM J N)"
   506   using assms sets_eq_imp_space_eq[OF assms(2)] by (simp add: sets_PiM_single cong: PiE_cong conj_cong)
   507 
   508 lemma sets_PiM_I:
   509   assumes "finite J" "J \<subseteq> I" "\<forall>i\<in>J. E i \<in> sets (M i)"
   510   shows "prod_emb I M J (PIE j:J. E j) \<in> sets (PIM i:I. M i)"
   511 proof cases
   512   assume "J = {}"
   513   then have "prod_emb I M J (PIE j:J. E j) = (PIE j:I. space (M j))"
   514     by (auto simp: prod_emb_def)
   515   then show ?thesis
   516     by (auto simp add: sets_PiM intro!: sigma_sets_top)
   517 next
   518   assume "J \<noteq> {}" with assms show ?thesis
   519     by (force simp add: sets_PiM prod_algebra_def)
   520 qed
   521 
   522 lemma measurable_PiM:
   523   assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   524   assumes sets: "\<And>X J. J \<noteq> {} \<or> I = {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)) \<Longrightarrow>
   525     f -` prod_emb I M J (Pi\<^sub>E J X) \<inter> space N \<in> sets N" 
   526   shows "f \<in> measurable N (PiM I M)"
   527   using sets_PiM prod_algebra_sets_into_space space
   528 proof (rule measurable_sigma_sets)
   529   fix A assume "A \<in> prod_algebra I M"
   530   from prod_algebraE[OF this] guess J X .
   531   with sets[of J X] show "f -` A \<inter> space N \<in> sets N" by auto
   532 qed
   533 
   534 lemma measurable_PiM_Collect:
   535   assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   536   assumes sets: "\<And>X J. J \<noteq> {} \<or> I = {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)) \<Longrightarrow>
   537     {\<omega>\<in>space N. \<forall>i\<in>J. f \<omega> i \<in> X i} \<in> sets N" 
   538   shows "f \<in> measurable N (PiM I M)"
   539   using sets_PiM prod_algebra_sets_into_space space
   540 proof (rule measurable_sigma_sets)
   541   fix A assume "A \<in> prod_algebra I M"
   542   from prod_algebraE[OF this] guess J X . note X = this
   543   then have "f -` A \<inter> space N = {\<omega> \<in> space N. \<forall>i\<in>J. f \<omega> i \<in> X i}"
   544     using space by (auto simp: prod_emb_def del: PiE_I)
   545   also have "\<dots> \<in> sets N" using X(3,2,4,5) by (rule sets)
   546   finally show "f -` A \<inter> space N \<in> sets N" .
   547 qed
   548 
   549 lemma measurable_PiM_single:
   550   assumes space: "f \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   551   assumes sets: "\<And>A i. i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> {\<omega> \<in> space N. f \<omega> i \<in> A} \<in> sets N" 
   552   shows "f \<in> measurable N (PiM I M)"
   553   using sets_PiM_single
   554 proof (rule measurable_sigma_sets)
   555   fix A assume "A \<in> {{f \<in> \<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> A} |i A. i \<in> I \<and> A \<in> sets (M i)}"
   556   then obtain B i where "A = {f \<in> \<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> B}" and B: "i \<in> I" "B \<in> sets (M i)"
   557     by auto
   558   with space have "f -` A \<inter> space N = {\<omega> \<in> space N. f \<omega> i \<in> B}" by auto
   559   also have "\<dots> \<in> sets N" using B by (rule sets)
   560   finally show "f -` A \<inter> space N \<in> sets N" .
   561 qed (auto simp: space)
   562 
   563 lemma measurable_PiM_single':
   564   assumes f: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> measurable N (M i)"
   565     and "(\<lambda>\<omega> i. f i \<omega>) \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   566   shows "(\<lambda>\<omega> i. f i \<omega>) \<in> measurable N (Pi\<^sub>M I M)"
   567 proof (rule measurable_PiM_single)
   568   fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   569   then have "{\<omega> \<in> space N. f i \<omega> \<in> A} = f i -` A \<inter> space N"
   570     by auto
   571   then show "{\<omega> \<in> space N. f i \<omega> \<in> A} \<in> sets N"
   572     using A f by (auto intro!: measurable_sets)
   573 qed fact
   574 
   575 lemma sets_PiM_I_finite[measurable]:
   576   assumes "finite I" and sets: "(\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i))"
   577   shows "(PIE j:I. E j) \<in> sets (PIM i:I. M i)"
   578   using sets_PiM_I[of I I E M] sets.sets_into_space[OF sets] `finite I` sets by auto
   579 
   580 lemma measurable_component_singleton[measurable (raw)]:
   581   assumes "i \<in> I" shows "(\<lambda>x. x i) \<in> measurable (Pi\<^sub>M I M) (M i)"
   582 proof (unfold measurable_def, intro CollectI conjI ballI)
   583   fix A assume "A \<in> sets (M i)"
   584   then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^sub>M I M) = prod_emb I M {i} (\<Pi>\<^sub>E j\<in>{i}. A)"
   585     using sets.sets_into_space `i \<in> I`
   586     by (fastforce dest: Pi_mem simp: prod_emb_def space_PiM split: split_if_asm)
   587   then show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^sub>M I M) \<in> sets (Pi\<^sub>M I M)"
   588     using `A \<in> sets (M i)` `i \<in> I` by (auto intro!: sets_PiM_I)
   589 qed (insert `i \<in> I`, auto simp: space_PiM)
   590 
   591 lemma measurable_component_singleton'[measurable_dest]:
   592   assumes f: "f \<in> measurable N (Pi\<^sub>M I M)"
   593   assumes g: "g \<in> measurable L N"
   594   assumes i: "i \<in> I"
   595   shows "(\<lambda>x. (f (g x)) i) \<in> measurable L (M i)"
   596   using measurable_compose[OF measurable_compose[OF g f] measurable_component_singleton, OF i] .
   597 
   598 lemma measurable_PiM_component_rev:
   599   "i \<in> I \<Longrightarrow> f \<in> measurable (M i) N \<Longrightarrow> (\<lambda>x. f (x i)) \<in> measurable (PiM I M) N"
   600   by simp
   601 
   602 lemma measurable_case_nat[measurable (raw)]:
   603   assumes [measurable (raw)]: "i = 0 \<Longrightarrow> f \<in> measurable M N"
   604     "\<And>j. i = Suc j \<Longrightarrow> (\<lambda>x. g x j) \<in> measurable M N"
   605   shows "(\<lambda>x. case_nat (f x) (g x) i) \<in> measurable M N"
   606   by (cases i) simp_all
   607  
   608 lemma measurable_case_nat'[measurable (raw)]:
   609   assumes fg[measurable]: "f \<in> measurable N M" "g \<in> measurable N (\<Pi>\<^sub>M i\<in>UNIV. M)"
   610   shows "(\<lambda>x. case_nat (f x) (g x)) \<in> measurable N (\<Pi>\<^sub>M i\<in>UNIV. M)"
   611   using fg[THEN measurable_space]
   612   by (auto intro!: measurable_PiM_single' simp add: space_PiM PiE_iff split: nat.split)
   613 
   614 lemma measurable_add_dim[measurable]:
   615   "(\<lambda>(f, y). f(i := y)) \<in> measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M M i) (Pi\<^sub>M (insert i I) M)"
   616     (is "?f \<in> measurable ?P ?I")
   617 proof (rule measurable_PiM_single)
   618   fix j A assume j: "j \<in> insert i I" and A: "A \<in> sets (M j)"
   619   have "{\<omega> \<in> space ?P. (\<lambda>(f, x). fun_upd f i x) \<omega> j \<in> A} =
   620     (if j = i then space (Pi\<^sub>M I M) \<times> A else ((\<lambda>x. x j) \<circ> fst) -` A \<inter> space ?P)"
   621     using sets.sets_into_space[OF A] by (auto simp add: space_pair_measure space_PiM)
   622   also have "\<dots> \<in> sets ?P"
   623     using A j
   624     by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   625   finally show "{\<omega> \<in> space ?P. case_prod (\<lambda>f. fun_upd f i) \<omega> j \<in> A} \<in> sets ?P" .
   626 qed (auto simp: space_pair_measure space_PiM PiE_def)
   627 
   628 lemma measurable_fun_upd:
   629   assumes I: "I = J \<union> {i}"
   630   assumes f[measurable]: "f \<in> measurable N (PiM J M)"
   631   assumes h[measurable]: "h \<in> measurable N (M i)"
   632   shows "(\<lambda>x. (f x) (i := h x)) \<in> measurable N (PiM I M)"
   633 proof (intro measurable_PiM_single')
   634   fix j assume "j \<in> I" then show "(\<lambda>\<omega>. ((f \<omega>)(i := h \<omega>)) j) \<in> measurable N (M j)"
   635     unfolding I by (cases "j = i") auto
   636 next
   637   show "(\<lambda>x. (f x)(i := h x)) \<in> space N \<rightarrow> (\<Pi>\<^sub>E i\<in>I. space (M i))"
   638     using I f[THEN measurable_space] h[THEN measurable_space]
   639     by (auto simp: space_PiM PiE_iff extensional_def)
   640 qed
   641 
   642 lemma measurable_component_update:
   643   "x \<in> space (Pi\<^sub>M I M) \<Longrightarrow> i \<notin> I \<Longrightarrow> (\<lambda>v. x(i := v)) \<in> measurable (M i) (Pi\<^sub>M (insert i I) M)"
   644   by simp
   645 
   646 lemma measurable_merge[measurable]:
   647   "merge I J \<in> measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) (Pi\<^sub>M (I \<union> J) M)"
   648     (is "?f \<in> measurable ?P ?U")
   649 proof (rule measurable_PiM_single)
   650   fix i A assume A: "A \<in> sets (M i)" "i \<in> I \<union> J"
   651   then have "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} =
   652     (if i \<in> I then ((\<lambda>x. x i) \<circ> fst) -` A \<inter> space ?P else ((\<lambda>x. x i) \<circ> snd) -` A \<inter> space ?P)"
   653     by (auto simp: merge_def)
   654   also have "\<dots> \<in> sets ?P"
   655     using A
   656     by (auto intro!: measurable_sets[OF measurable_comp, OF _ measurable_component_singleton])
   657   finally show "{\<omega> \<in> space ?P. merge I J \<omega> i \<in> A} \<in> sets ?P" .
   658 qed (auto simp: space_pair_measure space_PiM PiE_iff merge_def extensional_def)
   659 
   660 lemma measurable_restrict[measurable (raw)]:
   661   assumes X: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> measurable N (M i)"
   662   shows "(\<lambda>x. \<lambda>i\<in>I. X i x) \<in> measurable N (Pi\<^sub>M I M)"
   663 proof (rule measurable_PiM_single)
   664   fix A i assume A: "i \<in> I" "A \<in> sets (M i)"
   665   then have "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} = X i -` A \<inter> space N"
   666     by auto
   667   then show "{\<omega> \<in> space N. (\<lambda>i\<in>I. X i \<omega>) i \<in> A} \<in> sets N"
   668     using A X by (auto intro!: measurable_sets)
   669 qed (insert X, auto simp add: PiE_def dest: measurable_space)
   670 
   671 lemma measurable_abs_UNIV: 
   672   "(\<And>n. (\<lambda>\<omega>. f n \<omega>) \<in> measurable M (N n)) \<Longrightarrow> (\<lambda>\<omega> n. f n \<omega>) \<in> measurable M (PiM UNIV N)"
   673   by (intro measurable_PiM_single) (auto dest: measurable_space)
   674 
   675 lemma measurable_restrict_subset: "J \<subseteq> L \<Longrightarrow> (\<lambda>f. restrict f J) \<in> measurable (Pi\<^sub>M L M) (Pi\<^sub>M J M)"
   676   by (intro measurable_restrict measurable_component_singleton) auto
   677 
   678 lemma measurable_restrict_subset':
   679   assumes "J \<subseteq> L" "\<And>x. x \<in> J \<Longrightarrow> sets (M x) = sets (N x)"
   680   shows "(\<lambda>f. restrict f J) \<in> measurable (Pi\<^sub>M L M) (Pi\<^sub>M J N)"
   681 proof-
   682   from assms(1) have "(\<lambda>f. restrict f J) \<in> measurable (Pi\<^sub>M L M) (Pi\<^sub>M J M)"
   683     by (rule measurable_restrict_subset)
   684   also from assms(2) have "measurable (Pi\<^sub>M L M) (Pi\<^sub>M J M) = measurable (Pi\<^sub>M L M) (Pi\<^sub>M J N)"
   685     by (intro sets_PiM_cong measurable_cong_sets) simp_all
   686   finally show ?thesis .
   687 qed
   688 
   689 lemma measurable_prod_emb[intro, simp]:
   690   "J \<subseteq> L \<Longrightarrow> X \<in> sets (Pi\<^sub>M J M) \<Longrightarrow> prod_emb L M J X \<in> sets (Pi\<^sub>M L M)"
   691   unfolding prod_emb_def space_PiM[symmetric]
   692   by (auto intro!: measurable_sets measurable_restrict measurable_component_singleton)
   693 
   694 lemma merge_in_prod_emb:
   695   assumes "y \<in> space (PiM I M)" "x \<in> X" and X: "X \<in> sets (Pi\<^sub>M J M)" and "J \<subseteq> I"
   696   shows "merge J I (x, y) \<in> prod_emb I M J X"
   697   using assms sets.sets_into_space[OF X]
   698   by (simp add: merge_def prod_emb_def subset_eq space_PiM PiE_def extensional_restrict Pi_iff
   699            cong: if_cong restrict_cong)
   700      (simp add: extensional_def)
   701 
   702 lemma prod_emb_eq_emptyD:
   703   assumes J: "J \<subseteq> I" and ne: "space (PiM I M) \<noteq> {}" and X: "X \<in> sets (Pi\<^sub>M J M)"
   704     and *: "prod_emb I M J X = {}"
   705   shows "X = {}"
   706 proof safe
   707   fix x assume "x \<in> X"
   708   obtain \<omega> where "\<omega> \<in> space (PiM I M)"
   709     using ne by blast
   710   from merge_in_prod_emb[OF this \<open>x\<in>X\<close> X J] * show "x \<in> {}" by auto 
   711 qed
   712 
   713 lemma sets_in_Pi_aux:
   714   "finite I \<Longrightarrow> (\<And>j. j \<in> I \<Longrightarrow> {x\<in>space (M j). x \<in> F j} \<in> sets (M j)) \<Longrightarrow>
   715   {x\<in>space (PiM I M). x \<in> Pi I F} \<in> sets (PiM I M)"
   716   by (simp add: subset_eq Pi_iff)
   717 
   718 lemma sets_in_Pi[measurable (raw)]:
   719   "finite I \<Longrightarrow> f \<in> measurable N (PiM I M) \<Longrightarrow>
   720   (\<And>j. j \<in> I \<Longrightarrow> {x\<in>space (M j). x \<in> F j} \<in> sets (M j)) \<Longrightarrow>
   721   Measurable.pred N (\<lambda>x. f x \<in> Pi I F)"
   722   unfolding pred_def
   723   by (rule measurable_sets_Collect[of f N "PiM I M", OF _ sets_in_Pi_aux]) auto
   724 
   725 lemma sets_in_extensional_aux:
   726   "{x\<in>space (PiM I M). x \<in> extensional I} \<in> sets (PiM I M)"
   727 proof -
   728   have "{x\<in>space (PiM I M). x \<in> extensional I} = space (PiM I M)"
   729     by (auto simp add: extensional_def space_PiM)
   730   then show ?thesis by simp
   731 qed
   732 
   733 lemma sets_in_extensional[measurable (raw)]:
   734   "f \<in> measurable N (PiM I M) \<Longrightarrow> Measurable.pred N (\<lambda>x. f x \<in> extensional I)"
   735   unfolding pred_def
   736   by (rule measurable_sets_Collect[of f N "PiM I M", OF _ sets_in_extensional_aux]) auto
   737 
   738 lemma sets_PiM_I_countable:
   739   assumes I: "countable I" and E: "\<And>i. i \<in> I \<Longrightarrow> E i \<in> sets (M i)" shows "Pi\<^sub>E I E \<in> sets (Pi\<^sub>M I M)"
   740 proof cases
   741   assume "I \<noteq> {}"
   742   then have "PiE I E = (\<Inter>i\<in>I. prod_emb I M {i} (PiE {i} E))"
   743     using E[THEN sets.sets_into_space] by (auto simp: PiE_iff prod_emb_def fun_eq_iff)
   744   also have "\<dots> \<in> sets (PiM I M)"
   745     using I \<open>I \<noteq> {}\<close> by (safe intro!: sets.countable_INT' measurable_prod_emb sets_PiM_I_finite E)
   746   finally show ?thesis .
   747 qed (simp add: sets_PiM_empty)
   748 
   749 lemma sets_PiM_D_countable:
   750   assumes A: "A \<in> PiM I M"
   751   shows "\<exists>J\<subseteq>I. \<exists>X\<in>PiM J M. countable J \<and> A = prod_emb I M J X"
   752   using A[unfolded sets_PiM_single]
   753 proof induction
   754   case (Basic A)
   755   then obtain i X where *: "i \<in> I" "X \<in> sets (M i)" and "A = {f \<in> \<Pi>\<^sub>E i\<in>I. space (M i). f i \<in> X}"
   756     by auto
   757   then have A: "A = prod_emb I M {i} (\<Pi>\<^sub>E _\<in>{i}. X)"
   758     by (auto simp: prod_emb_def)
   759   then show ?case
   760     by (intro exI[of _ "{i}"] conjI bexI[of _ "\<Pi>\<^sub>E _\<in>{i}. X"])
   761        (auto intro: countable_finite * sets_PiM_I_finite)
   762 next
   763   case Empty then show ?case
   764     by (intro exI[of _ "{}"] conjI bexI[of _ "{}"]) auto
   765 next
   766   case (Compl A)
   767   then obtain J X where "J \<subseteq> I" "X \<in> sets (Pi\<^sub>M J M)" "countable J" "A = prod_emb I M J X"
   768     by auto
   769   then show ?case
   770     by (intro exI[of _ J] bexI[of _ "space (PiM J M) - X"] conjI)
   771        (auto simp add: space_PiM prod_emb_PiE intro!: sets_PiM_I_countable)
   772 next
   773   case (Union K)
   774   obtain J X where J: "\<And>i. J i \<subseteq> I" "\<And>i. countable (J i)" and X: "\<And>i. X i \<in> sets (Pi\<^sub>M (J i) M)"
   775     and K: "\<And>i. K i = prod_emb I M (J i) (X i)"
   776     by (metis Union.IH)
   777   show ?case
   778   proof (intro exI[of _ "\<Union>i. J i"] bexI[of _ "\<Union>i. prod_emb (\<Union>i. J i) M (J i) (X i)"] conjI)
   779     show "(\<Union>i. J i) \<subseteq> I" "countable (\<Union>i. J i)" using J by auto
   780     with J show "UNION UNIV K = prod_emb I M (\<Union>i. J i) (\<Union>i. prod_emb (\<Union>i. J i) M (J i) (X i))" 
   781       by (simp add: K[abs_def] SUP_upper)
   782   qed(auto intro: X)
   783 qed
   784 
   785 lemma measure_eqI_PiM_finite:
   786   assumes [simp]: "finite I" "sets P = PiM I M" "sets Q = PiM I M"
   787   assumes eq: "\<And>A. (\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> P (Pi\<^sub>E I A) = Q (Pi\<^sub>E I A)"
   788   assumes A: "range A \<subseteq> prod_algebra I M" "(\<Union>i. A i) = space (PiM I M)" "\<And>i::nat. P (A i) \<noteq> \<infinity>"
   789   shows "P = Q"
   790 proof (rule measure_eqI_generator_eq[OF Int_stable_prod_algebra prod_algebra_sets_into_space])
   791   show "range A \<subseteq> prod_algebra I M" "(\<Union>i. A i) = (\<Pi>\<^sub>E i\<in>I. space (M i))" "\<And>i. P (A i) \<noteq> \<infinity>"
   792     unfolding space_PiM[symmetric] by fact+
   793   fix X assume "X \<in> prod_algebra I M"
   794   then obtain J E where X: "X = prod_emb I M J (PIE j:J. E j)"
   795     and J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)"
   796     by (force elim!: prod_algebraE)
   797   then show "emeasure P X = emeasure Q X"
   798     unfolding X by (subst (1 2) prod_emb_Pi) (auto simp: eq)
   799 qed (simp_all add: sets_PiM)
   800 
   801 lemma measure_eqI_PiM_infinite:
   802   assumes [simp]: "sets P = PiM I M" "sets Q = PiM I M"
   803   assumes eq: "\<And>A J. finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow>
   804     P (prod_emb I M J (Pi\<^sub>E J A)) = Q (prod_emb I M J (Pi\<^sub>E J A))"
   805   assumes A: "finite_measure P"
   806   shows "P = Q"
   807 proof (rule measure_eqI_generator_eq[OF Int_stable_prod_algebra prod_algebra_sets_into_space])
   808   interpret finite_measure P by fact
   809   def i \<equiv> "SOME i. i \<in> I"
   810   have i: "I \<noteq> {} \<Longrightarrow> i \<in> I"
   811     unfolding i_def by (rule someI_ex) auto
   812   def A \<equiv> "\<lambda>n::nat. if I = {} then prod_emb I M {} (\<Pi>\<^sub>E i\<in>{}. {}) else prod_emb I M {i} (\<Pi>\<^sub>E i\<in>{i}. space (M i))"
   813   then show "range A \<subseteq> prod_algebra I M"
   814     using prod_algebraI[of "{}" I "\<lambda>i. space (M i)" M] by (auto intro!: prod_algebraI i)
   815   have "\<And>i. A i = space (PiM I M)"
   816     by (auto simp: prod_emb_def space_PiM PiE_iff A_def i ex_in_conv[symmetric] exI)
   817   then show "(\<Union>i. A i) = (\<Pi>\<^sub>E i\<in>I. space (M i))" "\<And>i. emeasure P (A i) \<noteq> \<infinity>"
   818     by (auto simp: space_PiM)
   819 next
   820   fix X assume X: "X \<in> prod_algebra I M"
   821   then obtain J E where X: "X = prod_emb I M J (PIE j:J. E j)"
   822     and J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)"
   823     by (force elim!: prod_algebraE)
   824   then show "emeasure P X = emeasure Q X"
   825     by (auto intro!: eq)
   826 qed (auto simp: sets_PiM)
   827 
   828 locale product_sigma_finite =
   829   fixes M :: "'i \<Rightarrow> 'a measure"
   830   assumes sigma_finite_measures: "\<And>i. sigma_finite_measure (M i)"
   831 
   832 sublocale product_sigma_finite \<subseteq> M: sigma_finite_measure "M i" for i
   833   by (rule sigma_finite_measures)
   834 
   835 locale finite_product_sigma_finite = product_sigma_finite M for M :: "'i \<Rightarrow> 'a measure" +
   836   fixes I :: "'i set"
   837   assumes finite_index: "finite I"
   838 
   839 lemma (in finite_product_sigma_finite) sigma_finite_pairs:
   840   "\<exists>F::'i \<Rightarrow> nat \<Rightarrow> 'a set.
   841     (\<forall>i\<in>I. range (F i) \<subseteq> sets (M i)) \<and>
   842     (\<forall>k. \<forall>i\<in>I. emeasure (M i) (F i k) \<noteq> \<infinity>) \<and> incseq (\<lambda>k. \<Pi>\<^sub>E i\<in>I. F i k) \<and>
   843     (\<Union>k. \<Pi>\<^sub>E i\<in>I. F i k) = space (PiM I M)"
   844 proof -
   845   have "\<forall>i::'i. \<exists>F::nat \<Rightarrow> 'a set. range F \<subseteq> sets (M i) \<and> incseq F \<and> (\<Union>i. F i) = space (M i) \<and> (\<forall>k. emeasure (M i) (F k) \<noteq> \<infinity>)"
   846     using M.sigma_finite_incseq by metis
   847   from choice[OF this] guess F :: "'i \<Rightarrow> nat \<Rightarrow> 'a set" ..
   848   then have F: "\<And>i. range (F i) \<subseteq> sets (M i)" "\<And>i. incseq (F i)" "\<And>i. (\<Union>j. F i j) = space (M i)" "\<And>i k. emeasure (M i) (F i k) \<noteq> \<infinity>"
   849     by auto
   850   let ?F = "\<lambda>k. \<Pi>\<^sub>E i\<in>I. F i k"
   851   note space_PiM[simp]
   852   show ?thesis
   853   proof (intro exI[of _ F] conjI allI incseq_SucI set_eqI iffI ballI)
   854     fix i show "range (F i) \<subseteq> sets (M i)" by fact
   855   next
   856     fix i k show "emeasure (M i) (F i k) \<noteq> \<infinity>" by fact
   857   next
   858     fix x assume "x \<in> (\<Union>i. ?F i)" with F(1) show "x \<in> space (PiM I M)"
   859       by (auto simp: PiE_def dest!: sets.sets_into_space)
   860   next
   861     fix f assume "f \<in> space (PiM I M)"
   862     with Pi_UN[OF finite_index, of "\<lambda>k i. F i k"] F
   863     show "f \<in> (\<Union>i. ?F i)" by (auto simp: incseq_def PiE_def)
   864   next
   865     fix i show "?F i \<subseteq> ?F (Suc i)"
   866       using `\<And>i. incseq (F i)`[THEN incseq_SucD] by auto
   867   qed
   868 qed
   869 
   870 lemma emeasure_PiM_empty[simp]: "emeasure (PiM {} M) {\<lambda>_. undefined} = 1"
   871 proof -
   872   let ?\<mu> = "\<lambda>A. if A = {} then 0 else (1::ereal)"
   873   have "emeasure (Pi\<^sub>M {} M) (prod_emb {} M {} (\<Pi>\<^sub>E i\<in>{}. {})) = 1"
   874   proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   875     show "positive (PiM {} M) ?\<mu>"
   876       by (auto simp: positive_def)
   877     show "countably_additive (PiM {} M) ?\<mu>"
   878       by (rule sets.countably_additiveI_finite)
   879          (auto simp: additive_def positive_def sets_PiM_empty space_PiM_empty intro!: )
   880   qed (auto simp: prod_emb_def)
   881   also have "(prod_emb {} M {} (\<Pi>\<^sub>E i\<in>{}. {})) = {\<lambda>_. undefined}"
   882     by (auto simp: prod_emb_def)
   883   finally show ?thesis
   884     by simp
   885 qed
   886 
   887 lemma PiM_empty: "PiM {} M = count_space {\<lambda>_. undefined}"
   888   by (rule measure_eqI) (auto simp add: sets_PiM_empty one_ereal_def)
   889 
   890 lemma (in product_sigma_finite) emeasure_PiM:
   891   "finite I \<Longrightarrow> (\<And>i. i\<in>I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (PiM I M) (Pi\<^sub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   892 proof (induct I arbitrary: A rule: finite_induct)
   893   case (insert i I)
   894   interpret finite_product_sigma_finite M I by standard fact
   895   have "finite (insert i I)" using `finite I` by auto
   896   interpret I': finite_product_sigma_finite M "insert i I" by standard fact
   897   let ?h = "(\<lambda>(f, y). f(i := y))"
   898 
   899   let ?P = "distr (Pi\<^sub>M I M \<Otimes>\<^sub>M M i) (Pi\<^sub>M (insert i I) M) ?h"
   900   let ?\<mu> = "emeasure ?P"
   901   let ?I = "{j \<in> insert i I. emeasure (M j) (space (M j)) \<noteq> 1}"
   902   let ?f = "\<lambda>J E j. if j \<in> J then emeasure (M j) (E j) else emeasure (M j) (space (M j))"
   903 
   904   have "emeasure (Pi\<^sub>M (insert i I) M) (prod_emb (insert i I) M (insert i I) (Pi\<^sub>E (insert i I) A)) =
   905     (\<Prod>i\<in>insert i I. emeasure (M i) (A i))"
   906   proof (subst emeasure_extend_measure_Pair[OF PiM_def])
   907     fix J E assume "(J \<noteq> {} \<or> insert i I = {}) \<and> finite J \<and> J \<subseteq> insert i I \<and> E \<in> (\<Pi> j\<in>J. sets (M j))"
   908     then have J: "J \<noteq> {}" "finite J" "J \<subseteq> insert i I" and E: "\<forall>j\<in>J. E j \<in> sets (M j)" by auto
   909     let ?p = "prod_emb (insert i I) M J (Pi\<^sub>E J E)"
   910     let ?p' = "prod_emb I M (J - {i}) (\<Pi>\<^sub>E j\<in>J-{i}. E j)"
   911     have "?\<mu> ?p =
   912       emeasure (Pi\<^sub>M I M \<Otimes>\<^sub>M (M i)) (?h -` ?p \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M M i))"
   913       by (intro emeasure_distr measurable_add_dim sets_PiM_I) fact+
   914     also have "?h -` ?p \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M M i) = ?p' \<times> (if i \<in> J then E i else space (M i))"
   915       using J E[rule_format, THEN sets.sets_into_space]
   916       by (force simp: space_pair_measure space_PiM prod_emb_iff PiE_def Pi_iff split: split_if_asm)
   917     also have "emeasure (Pi\<^sub>M I M \<Otimes>\<^sub>M (M i)) (?p' \<times> (if i \<in> J then E i else space (M i))) =
   918       emeasure (Pi\<^sub>M I M) ?p' * emeasure (M i) (if i \<in> J then (E i) else space (M i))"
   919       using J E by (intro M.emeasure_pair_measure_Times sets_PiM_I) auto
   920     also have "?p' = (\<Pi>\<^sub>E j\<in>I. if j \<in> J-{i} then E j else space (M j))"
   921       using J E[rule_format, THEN sets.sets_into_space]
   922       by (auto simp: prod_emb_iff PiE_def Pi_iff split: split_if_asm) blast+
   923     also have "emeasure (Pi\<^sub>M I M) (\<Pi>\<^sub>E j\<in>I. if j \<in> J-{i} then E j else space (M j)) =
   924       (\<Prod> j\<in>I. if j \<in> J-{i} then emeasure (M j) (E j) else emeasure (M j) (space (M j)))"
   925       using E by (subst insert) (auto intro!: setprod.cong)
   926     also have "(\<Prod>j\<in>I. if j \<in> J - {i} then emeasure (M j) (E j) else emeasure (M j) (space (M j))) *
   927        emeasure (M i) (if i \<in> J then E i else space (M i)) = (\<Prod>j\<in>insert i I. ?f J E j)"
   928       using insert by (auto simp: mult.commute intro!: arg_cong2[where f="op *"] setprod.cong)
   929     also have "\<dots> = (\<Prod>j\<in>J \<union> ?I. ?f J E j)"
   930       using insert(1,2) J E by (intro setprod.mono_neutral_right) auto
   931     finally show "?\<mu> ?p = \<dots>" .
   932 
   933     show "prod_emb (insert i I) M J (Pi\<^sub>E J E) \<in> Pow (\<Pi>\<^sub>E i\<in>insert i I. space (M i))"
   934       using J E[rule_format, THEN sets.sets_into_space] by (auto simp: prod_emb_iff PiE_def)
   935   next
   936     show "positive (sets (Pi\<^sub>M (insert i I) M)) ?\<mu>" "countably_additive (sets (Pi\<^sub>M (insert i I) M)) ?\<mu>"
   937       using emeasure_positive[of ?P] emeasure_countably_additive[of ?P] by simp_all
   938   next
   939     show "(insert i I \<noteq> {} \<or> insert i I = {}) \<and> finite (insert i I) \<and>
   940       insert i I \<subseteq> insert i I \<and> A \<in> (\<Pi> j\<in>insert i I. sets (M j))"
   941       using insert by auto
   942   qed (auto intro!: setprod.cong)
   943   with insert show ?case
   944     by (subst (asm) prod_emb_PiE_same_index) (auto intro!: sets.sets_into_space)
   945 qed simp
   946 
   947 lemma (in product_sigma_finite) PiM_eqI:
   948   assumes I[simp]: "finite I" and P: "sets P = PiM I M"
   949   assumes eq: "\<And>A. (\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> P (Pi\<^sub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   950   shows "P = PiM I M"
   951 proof -
   952   interpret finite_product_sigma_finite M I
   953     proof qed fact
   954   from sigma_finite_pairs guess C .. note C = this
   955   show ?thesis
   956   proof (rule measure_eqI_PiM_finite[OF I refl P, symmetric])
   957     show "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> (Pi\<^sub>M I M) (Pi\<^sub>E I A) = P (Pi\<^sub>E I A)" for A
   958       by (simp add: eq emeasure_PiM)
   959     def A \<equiv> "\<lambda>n. \<Pi>\<^sub>E i\<in>I. C i n"
   960     with C show "range A \<subseteq> prod_algebra I M" "\<And>i. emeasure (Pi\<^sub>M I M) (A i) \<noteq> \<infinity>" "(\<Union>i. A i) = space (PiM I M)"
   961       by (auto intro!: prod_algebraI_finite simp: emeasure_PiM subset_eq setprod_PInf emeasure_nonneg)
   962   qed
   963 qed
   964 
   965 lemma (in product_sigma_finite) sigma_finite: 
   966   assumes "finite I"
   967   shows "sigma_finite_measure (PiM I M)"
   968 proof
   969   interpret finite_product_sigma_finite M I by standard fact
   970 
   971   obtain F where F: "\<And>j. countable (F j)" "\<And>j f. f \<in> F j \<Longrightarrow> f \<in> sets (M j)"
   972     "\<And>j f. f \<in> F j \<Longrightarrow> emeasure (M j) f \<noteq> \<infinity>" and
   973     in_space: "\<And>j. space (M j) = (\<Union>F j)"
   974     using sigma_finite_countable by (metis subset_eq)
   975   moreover have "(\<Union>(PiE I ` PiE I F)) = space (Pi\<^sub>M I M)"
   976     using in_space by (auto simp: space_PiM PiE_iff intro!: PiE_choice[THEN iffD2])
   977   ultimately show "\<exists>A. countable A \<and> A \<subseteq> sets (Pi\<^sub>M I M) \<and> \<Union>A = space (Pi\<^sub>M I M) \<and> (\<forall>a\<in>A. emeasure (Pi\<^sub>M I M) a \<noteq> \<infinity>)"
   978     by (intro exI[of _ "PiE I ` PiE I F"])
   979        (auto intro!: countable_PiE sets_PiM_I_finite
   980              simp: PiE_iff emeasure_PiM finite_index setprod_PInf emeasure_nonneg)
   981 qed
   982 
   983 sublocale finite_product_sigma_finite \<subseteq> sigma_finite_measure "Pi\<^sub>M I M"
   984   using sigma_finite[OF finite_index] .
   985 
   986 lemma (in finite_product_sigma_finite) measure_times:
   987   "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> emeasure (Pi\<^sub>M I M) (Pi\<^sub>E I A) = (\<Prod>i\<in>I. emeasure (M i) (A i))"
   988   using emeasure_PiM[OF finite_index] by auto
   989 
   990 lemma (in product_sigma_finite) nn_integral_empty:
   991   "0 \<le> f (\<lambda>k. undefined) \<Longrightarrow> integral\<^sup>N (Pi\<^sub>M {} M) f = f (\<lambda>k. undefined)"
   992   by (simp add: PiM_empty nn_integral_count_space_finite max.absorb2)
   993 
   994 lemma (in product_sigma_finite) distr_merge:
   995   assumes IJ[simp]: "I \<inter> J = {}" and fin: "finite I" "finite J"
   996   shows "distr (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) (Pi\<^sub>M (I \<union> J) M) (merge I J) = Pi\<^sub>M (I \<union> J) M"
   997    (is "?D = ?P")
   998 proof (rule PiM_eqI)
   999   interpret I: finite_product_sigma_finite M I by standard fact
  1000   interpret J: finite_product_sigma_finite M J by standard fact
  1001   fix A assume A: "\<And>i. i \<in> I \<union> J \<Longrightarrow> A i \<in> sets (M i)"
  1002   have *: "(merge I J -` Pi\<^sub>E (I \<union> J) A \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)) = PiE I A \<times> PiE J A"
  1003     using A[THEN sets.sets_into_space] by (auto simp: space_PiM space_pair_measure)
  1004   from A fin show "emeasure (distr (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) (Pi\<^sub>M (I \<union> J) M) (merge I J)) (Pi\<^sub>E (I \<union> J) A) =
  1005       (\<Prod>i\<in>I \<union> J. emeasure (M i) (A i))"
  1006     by (subst emeasure_distr)
  1007        (auto simp: * J.emeasure_pair_measure_Times I.measure_times J.measure_times setprod.union_disjoint)
  1008 qed (insert fin, simp_all)
  1009 
  1010 lemma (in product_sigma_finite) product_nn_integral_fold:
  1011   assumes IJ: "I \<inter> J = {}" "finite I" "finite J"
  1012   and f: "f \<in> borel_measurable (Pi\<^sub>M (I \<union> J) M)"
  1013   shows "integral\<^sup>N (Pi\<^sub>M (I \<union> J) M) f =
  1014     (\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (merge I J (x, y)) \<partial>(Pi\<^sub>M J M)) \<partial>(Pi\<^sub>M I M))"
  1015 proof -
  1016   interpret I: finite_product_sigma_finite M I by standard fact
  1017   interpret J: finite_product_sigma_finite M J by standard fact
  1018   interpret P: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" by standard
  1019   have P_borel: "(\<lambda>x. f (merge I J x)) \<in> borel_measurable (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
  1020     using measurable_comp[OF measurable_merge f] by (simp add: comp_def)
  1021   show ?thesis
  1022     apply (subst distr_merge[OF IJ, symmetric])
  1023     apply (subst nn_integral_distr[OF measurable_merge f])
  1024     apply (subst J.nn_integral_fst[symmetric, OF P_borel])
  1025     apply simp
  1026     done
  1027 qed
  1028 
  1029 lemma (in product_sigma_finite) distr_singleton:
  1030   "distr (Pi\<^sub>M {i} M) (M i) (\<lambda>x. x i) = M i" (is "?D = _")
  1031 proof (intro measure_eqI[symmetric])
  1032   interpret I: finite_product_sigma_finite M "{i}" by standard simp
  1033   fix A assume A: "A \<in> sets (M i)"
  1034   then have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^sub>M {i} M) = (\<Pi>\<^sub>E i\<in>{i}. A)"
  1035     using sets.sets_into_space by (auto simp: space_PiM)
  1036   then show "emeasure (M i) A = emeasure ?D A"
  1037     using A I.measure_times[of "\<lambda>_. A"]
  1038     by (simp add: emeasure_distr measurable_component_singleton)
  1039 qed simp
  1040 
  1041 lemma (in product_sigma_finite) product_nn_integral_singleton:
  1042   assumes f: "f \<in> borel_measurable (M i)"
  1043   shows "integral\<^sup>N (Pi\<^sub>M {i} M) (\<lambda>x. f (x i)) = integral\<^sup>N (M i) f"
  1044 proof -
  1045   interpret I: finite_product_sigma_finite M "{i}" by standard simp
  1046   from f show ?thesis
  1047     apply (subst distr_singleton[symmetric])
  1048     apply (subst nn_integral_distr[OF measurable_component_singleton])
  1049     apply simp_all
  1050     done
  1051 qed
  1052 
  1053 lemma (in product_sigma_finite) product_nn_integral_insert:
  1054   assumes I[simp]: "finite I" "i \<notin> I"
  1055     and f: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  1056   shows "integral\<^sup>N (Pi\<^sub>M (insert i I) M) f = (\<integral>\<^sup>+ x. (\<integral>\<^sup>+ y. f (x(i := y)) \<partial>(M i)) \<partial>(Pi\<^sub>M I M))"
  1057 proof -
  1058   interpret I: finite_product_sigma_finite M I by standard auto
  1059   interpret i: finite_product_sigma_finite M "{i}" by standard auto
  1060   have IJ: "I \<inter> {i} = {}" and insert: "I \<union> {i} = insert i I"
  1061     using f by auto
  1062   show ?thesis
  1063     unfolding product_nn_integral_fold[OF IJ, unfolded insert, OF I(1) i.finite_index f]
  1064   proof (rule nn_integral_cong, subst product_nn_integral_singleton[symmetric])
  1065     fix x assume x: "x \<in> space (Pi\<^sub>M I M)"
  1066     let ?f = "\<lambda>y. f (x(i := y))"
  1067     show "?f \<in> borel_measurable (M i)"
  1068       using measurable_comp[OF measurable_component_update f, OF x `i \<notin> I`]
  1069       unfolding comp_def .
  1070     show "(\<integral>\<^sup>+ y. f (merge I {i} (x, y)) \<partial>Pi\<^sub>M {i} M) = (\<integral>\<^sup>+ y. f (x(i := y i)) \<partial>Pi\<^sub>M {i} M)"
  1071       using x
  1072       by (auto intro!: nn_integral_cong arg_cong[where f=f]
  1073                simp add: space_PiM extensional_def PiE_def)
  1074   qed
  1075 qed
  1076 
  1077 lemma (in product_sigma_finite) product_nn_integral_insert_rev:
  1078   assumes I[simp]: "finite I" "i \<notin> I"
  1079     and [measurable]: "f \<in> borel_measurable (Pi\<^sub>M (insert i I) M)"
  1080   shows "integral\<^sup>N (Pi\<^sub>M (insert i I) M) f = (\<integral>\<^sup>+ y. (\<integral>\<^sup>+ x. f (x(i := y)) \<partial>(Pi\<^sub>M I M)) \<partial>(M i))"
  1081   apply (subst product_nn_integral_insert[OF assms])
  1082   apply (rule pair_sigma_finite.Fubini')
  1083   apply intro_locales []
  1084   apply (rule sigma_finite[OF I(1)])
  1085   apply measurable
  1086   done
  1087 
  1088 lemma (in product_sigma_finite) product_nn_integral_setprod:
  1089   fixes f :: "'i \<Rightarrow> 'a \<Rightarrow> ereal"
  1090   assumes "finite I" and borel: "\<And>i. i \<in> I \<Longrightarrow> f i \<in> borel_measurable (M i)"
  1091   and pos: "\<And>i x. i \<in> I \<Longrightarrow> 0 \<le> f i x"
  1092   shows "(\<integral>\<^sup>+ x. (\<Prod>i\<in>I. f i (x i)) \<partial>Pi\<^sub>M I M) = (\<Prod>i\<in>I. integral\<^sup>N (M i) (f i))"
  1093 using assms proof induct
  1094   case (insert i I)
  1095   note `finite I`[intro, simp]
  1096   interpret I: finite_product_sigma_finite M I by standard auto
  1097   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))"
  1098     using insert by (auto intro!: setprod.cong)
  1099   have prod: "\<And>J. J \<subseteq> insert i I \<Longrightarrow> (\<lambda>x. (\<Prod>i\<in>J. f i (x i))) \<in> borel_measurable (Pi\<^sub>M J M)"
  1100     using sets.sets_into_space insert
  1101     by (intro borel_measurable_ereal_setprod
  1102               measurable_comp[OF measurable_component_singleton, unfolded comp_def])
  1103        auto
  1104   then show ?case
  1105     apply (simp add: product_nn_integral_insert[OF insert(1,2) prod])
  1106     apply (simp add: insert(2-) * pos borel setprod_ereal_pos nn_integral_multc)
  1107     apply (subst nn_integral_cmult)
  1108     apply (auto simp add: pos borel insert(2-) setprod_ereal_pos nn_integral_nonneg)
  1109     done
  1110 qed (simp add: space_PiM)
  1111 
  1112 lemma (in product_sigma_finite) product_nn_integral_pair:
  1113   assumes [measurable]: "case_prod f \<in> borel_measurable (M x \<Otimes>\<^sub>M M y)"
  1114   assumes xy: "x \<noteq> y"
  1115   shows "(\<integral>\<^sup>+\<sigma>. f (\<sigma> x) (\<sigma> y) \<partial>PiM {x, y} M) = (\<integral>\<^sup>+z. f (fst z) (snd z) \<partial>(M x \<Otimes>\<^sub>M M y))"
  1116 proof-
  1117   interpret psm: pair_sigma_finite "M x" "M y"
  1118     unfolding pair_sigma_finite_def using sigma_finite_measures by simp_all
  1119   have "{x, y} = {y, x}" by auto
  1120   also have "(\<integral>\<^sup>+\<sigma>. f (\<sigma> x) (\<sigma> y) \<partial>PiM {y, x} M) = (\<integral>\<^sup>+y. \<integral>\<^sup>+\<sigma>. f (\<sigma> x) y \<partial>PiM {x} M \<partial>M y)"
  1121     using xy by (subst product_nn_integral_insert_rev) simp_all
  1122   also have "... = (\<integral>\<^sup>+y. \<integral>\<^sup>+x. f x y \<partial>M x \<partial>M y)"
  1123     by (intro nn_integral_cong, subst product_nn_integral_singleton) simp_all
  1124   also have "... = (\<integral>\<^sup>+z. f (fst z) (snd z) \<partial>(M x \<Otimes>\<^sub>M M y))"
  1125     by (subst psm.nn_integral_snd[symmetric]) simp_all
  1126   finally show ?thesis .
  1127 qed
  1128 
  1129 lemma (in product_sigma_finite) distr_component:
  1130   "distr (M i) (Pi\<^sub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x) = Pi\<^sub>M {i} M" (is "?D = ?P")
  1131 proof (intro PiM_eqI)
  1132   fix A assume "\<And>ia. ia \<in> {i} \<Longrightarrow> A ia \<in> sets (M ia)"
  1133   moreover then have "(\<lambda>x. \<lambda>i\<in>{i}. x) -` Pi\<^sub>E {i} A \<inter> space (M i) = A i"
  1134     by (auto dest: sets.sets_into_space)
  1135   ultimately show "emeasure (distr (M i) (Pi\<^sub>M {i} M) (\<lambda>x. \<lambda>i\<in>{i}. x)) (Pi\<^sub>E {i} A) = (\<Prod>i\<in>{i}. emeasure (M i) (A i))"
  1136     by (subst emeasure_distr) (auto intro!: sets_PiM_I_finite measurable_restrict)
  1137 qed simp_all
  1138 
  1139 lemma (in product_sigma_finite)
  1140   assumes IJ: "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^sub>M (I \<union> J) M)"
  1141   shows emeasure_fold_integral:
  1142     "emeasure (Pi\<^sub>M (I \<union> J) M) A = (\<integral>\<^sup>+x. emeasure (Pi\<^sub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^sub>M J M)) \<partial>Pi\<^sub>M I M)" (is ?I)
  1143     and emeasure_fold_measurable:
  1144     "(\<lambda>x. emeasure (Pi\<^sub>M J M) ((\<lambda>y. merge I J (x, y)) -` A \<inter> space (Pi\<^sub>M J M))) \<in> borel_measurable (Pi\<^sub>M I M)" (is ?B)
  1145 proof -
  1146   interpret I: finite_product_sigma_finite M I by standard fact
  1147   interpret J: finite_product_sigma_finite M J by standard fact
  1148   interpret IJ: pair_sigma_finite "Pi\<^sub>M I M" "Pi\<^sub>M J M" ..
  1149   have merge: "merge I J -` A \<inter> space (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M) \<in> sets (Pi\<^sub>M I M \<Otimes>\<^sub>M Pi\<^sub>M J M)"
  1150     by (intro measurable_sets[OF _ A] measurable_merge assms)
  1151 
  1152   show ?I
  1153     apply (subst distr_merge[symmetric, OF IJ])
  1154     apply (subst emeasure_distr[OF measurable_merge A])
  1155     apply (subst J.emeasure_pair_measure_alt[OF merge])
  1156     apply (auto intro!: nn_integral_cong arg_cong2[where f=emeasure] simp: space_pair_measure)
  1157     done
  1158 
  1159   show ?B
  1160     using IJ.measurable_emeasure_Pair1[OF merge]
  1161     by (simp add: vimage_comp comp_def space_pair_measure cong: measurable_cong)
  1162 qed
  1163 
  1164 lemma sets_Collect_single:
  1165   "i \<in> I \<Longrightarrow> A \<in> sets (M i) \<Longrightarrow> { x \<in> space (Pi\<^sub>M I M). x i \<in> A } \<in> sets (Pi\<^sub>M I M)"
  1166   by simp
  1167 
  1168 lemma pair_measure_eq_distr_PiM:
  1169   fixes M1 :: "'a measure" and M2 :: "'a measure"
  1170   assumes "sigma_finite_measure M1" "sigma_finite_measure M2"
  1171   shows "(M1 \<Otimes>\<^sub>M M2) = distr (Pi\<^sub>M UNIV (case_bool M1 M2)) (M1 \<Otimes>\<^sub>M M2) (\<lambda>x. (x True, x False))"
  1172     (is "?P = ?D")
  1173 proof (rule pair_measure_eqI[OF assms])
  1174   interpret B: product_sigma_finite "case_bool M1 M2"
  1175     unfolding product_sigma_finite_def using assms by (auto split: bool.split)
  1176   let ?B = "Pi\<^sub>M UNIV (case_bool M1 M2)"
  1177 
  1178   have [simp]: "fst \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x True)" "snd \<circ> (\<lambda>x. (x True, x False)) = (\<lambda>x. x False)"
  1179     by auto
  1180   fix A B assume A: "A \<in> sets M1" and B: "B \<in> sets M2"
  1181   have "emeasure M1 A * emeasure M2 B = (\<Prod> i\<in>UNIV. emeasure (case_bool M1 M2 i) (case_bool A B i))"
  1182     by (simp add: UNIV_bool ac_simps)
  1183   also have "\<dots> = emeasure ?B (Pi\<^sub>E UNIV (case_bool A B))"
  1184     using A B by (subst B.emeasure_PiM) (auto split: bool.split)
  1185   also have "Pi\<^sub>E UNIV (case_bool A B) = (\<lambda>x. (x True, x False)) -` (A \<times> B) \<inter> space ?B"
  1186     using A[THEN sets.sets_into_space] B[THEN sets.sets_into_space]
  1187     by (auto simp: PiE_iff all_bool_eq space_PiM split: bool.split)
  1188   finally show "emeasure M1 A * emeasure M2 B = emeasure ?D (A \<times> B)"
  1189     using A B
  1190       measurable_component_singleton[of True UNIV "case_bool M1 M2"]
  1191       measurable_component_singleton[of False UNIV "case_bool M1 M2"]
  1192     by (subst emeasure_distr) (auto simp: measurable_pair_iff)
  1193 qed simp
  1194 
  1195 end