src/HOL/Probability/Infinite_Product_Measure.thy
author immler@in.tum.de
Fri Nov 09 14:14:45 2012 +0100 (2012-11-09)
changeset 50041 afe886a04198
parent 50040 5da32dc55cd8
child 50042 6fe18351e9dd
permissions -rw-r--r--
removed redundant/unnecessary assumptions from projective_family
     1 (*  Title:      HOL/Probability/Infinite_Product_Measure.thy
     2     Author:     Johannes Hölzl, TU München
     3 *)
     4 
     5 header {*Infinite Product Measure*}
     6 
     7 theory Infinite_Product_Measure
     8   imports Probability_Measure Caratheodory Projective_Family
     9 begin
    10 
    11 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)"
    12   unfolding merge_def by auto
    13 
    14 lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I J (x, y) \<in> extensional K"
    15   unfolding merge_def extensional_def by auto
    16 
    17 lemma injective_vimage_restrict:
    18   assumes J: "J \<subseteq> I"
    19   and sets: "A \<subseteq> (\<Pi>\<^isub>E i\<in>J. S i)" "B \<subseteq> (\<Pi>\<^isub>E i\<in>J. S i)" and ne: "(\<Pi>\<^isub>E i\<in>I. S i) \<noteq> {}"
    20   and eq: "(\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i) = (\<lambda>x. restrict x J) -` B \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
    21   shows "A = B"
    22 proof  (intro set_eqI)
    23   fix x
    24   from ne obtain y where y: "\<And>i. i \<in> I \<Longrightarrow> y i \<in> S i" by auto
    25   have "J \<inter> (I - J) = {}" by auto
    26   show "x \<in> A \<longleftrightarrow> x \<in> B"
    27   proof cases
    28     assume x: "x \<in> (\<Pi>\<^isub>E i\<in>J. S i)"
    29     have "x \<in> A \<longleftrightarrow> merge J (I - J) (x,y) \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
    30       using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub split: split_merge)
    31     then show "x \<in> A \<longleftrightarrow> x \<in> B"
    32       using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub eq split: split_merge)
    33   next
    34     assume "x \<notin> (\<Pi>\<^isub>E i\<in>J. S i)" with sets show "x \<in> A \<longleftrightarrow> x \<in> B" by auto
    35   qed
    36 qed
    37 
    38 lemma (in product_prob_space) distr_restrict:
    39   assumes "J \<noteq> {}" "J \<subseteq> K" "finite K"
    40   shows "(\<Pi>\<^isub>M i\<in>J. M i) = distr (\<Pi>\<^isub>M i\<in>K. M i) (\<Pi>\<^isub>M i\<in>J. M i) (\<lambda>f. restrict f J)" (is "?P = ?D")
    41 proof (rule measure_eqI_generator_eq)
    42   have "finite J" using `J \<subseteq> K` `finite K` by (auto simp add: finite_subset)
    43   interpret J: finite_product_prob_space M J proof qed fact
    44   interpret K: finite_product_prob_space M K proof qed fact
    45 
    46   let ?J = "{Pi\<^isub>E J E | E. \<forall>i\<in>J. E i \<in> sets (M i)}"
    47   let ?F = "\<lambda>i. \<Pi>\<^isub>E k\<in>J. space (M k)"
    48   let ?\<Omega> = "(\<Pi>\<^isub>E k\<in>J. space (M k))"
    49   show "Int_stable ?J"
    50     by (rule Int_stable_PiE)
    51   show "range ?F \<subseteq> ?J" "(\<Union>i. ?F i) = ?\<Omega>"
    52     using `finite J` by (auto intro!: prod_algebraI_finite)
    53   { fix i show "emeasure ?P (?F i) \<noteq> \<infinity>" by simp }
    54   show "?J \<subseteq> Pow ?\<Omega>" by (auto simp: Pi_iff dest: sets_into_space)
    55   show "sets (\<Pi>\<^isub>M i\<in>J. M i) = sigma_sets ?\<Omega> ?J" "sets ?D = sigma_sets ?\<Omega> ?J"
    56     using `finite J` by (simp_all add: sets_PiM prod_algebra_eq_finite Pi_iff)
    57   
    58   fix X assume "X \<in> ?J"
    59   then obtain E where [simp]: "X = Pi\<^isub>E J E" and E: "\<forall>i\<in>J. E i \<in> sets (M i)" by auto
    60   with `finite J` have X: "X \<in> sets (Pi\<^isub>M J M)"
    61     by simp
    62 
    63   have "emeasure ?P X = (\<Prod> i\<in>J. emeasure (M i) (E i))"
    64     using E by (simp add: J.measure_times)
    65   also have "\<dots> = (\<Prod> i\<in>J. emeasure (M i) (if i \<in> J then E i else space (M i)))"
    66     by simp
    67   also have "\<dots> = (\<Prod> i\<in>K. emeasure (M i) (if i \<in> J then E i else space (M i)))"
    68     using `finite K` `J \<subseteq> K`
    69     by (intro setprod_mono_one_left) (auto simp: M.emeasure_space_1)
    70   also have "\<dots> = emeasure (Pi\<^isub>M K M) (\<Pi>\<^isub>E i\<in>K. if i \<in> J then E i else space (M i))"
    71     using E by (simp add: K.measure_times)
    72   also have "(\<Pi>\<^isub>E i\<in>K. if i \<in> J then E i else space (M i)) = (\<lambda>f. restrict f J) -` Pi\<^isub>E J E \<inter> (\<Pi>\<^isub>E i\<in>K. space (M i))"
    73     using `J \<subseteq> K` sets_into_space E by (force simp:  Pi_iff split: split_if_asm)
    74   finally show "emeasure (Pi\<^isub>M J M) X = emeasure ?D X"
    75     using X `J \<subseteq> K` apply (subst emeasure_distr)
    76     by (auto intro!: measurable_restrict_subset simp: space_PiM)
    77 qed
    78 
    79 lemma (in product_prob_space) emeasure_prod_emb[simp]:
    80   assumes L: "J \<noteq> {}" "J \<subseteq> L" "finite L" and X: "X \<in> sets (Pi\<^isub>M J M)"
    81   shows "emeasure (Pi\<^isub>M L M) (prod_emb L M J X) = emeasure (Pi\<^isub>M J M) X"
    82   by (subst distr_restrict[OF L])
    83      (simp add: prod_emb_def space_PiM emeasure_distr measurable_restrict_subset L X)
    84 
    85 sublocale product_prob_space \<subseteq> projective_family I "\<lambda>J. PiM J M" M
    86 proof
    87   fix J::"'i set" assume "finite J"
    88   interpret f: finite_product_prob_space M J proof qed fact
    89   show "emeasure (Pi\<^isub>M J M) (space (Pi\<^isub>M J M)) \<noteq> \<infinity>" by simp
    90   show "\<exists>A. range A \<subseteq> sets (Pi\<^isub>M J M) \<and>
    91             (\<Union>i. A i) = space (Pi\<^isub>M J M) \<and>
    92             (\<forall>i. emeasure (Pi\<^isub>M J M) (A i) \<noteq> \<infinity>)" using sigma_finite[OF `finite J`]
    93     by (auto simp add: sigma_finite_measure_def)
    94   show "emeasure (Pi\<^isub>M J M) (space (Pi\<^isub>M J M)) = 1" by (rule f.emeasure_space_1)
    95 qed simp_all
    96 
    97 lemma (in projective_family) prod_emb_injective:
    98   assumes "J \<noteq> {}" "J \<subseteq> L" "finite J" and sets: "X \<in> sets (Pi\<^isub>M J M)" "Y \<in> sets (Pi\<^isub>M J M)"
    99   assumes "prod_emb L M J X = prod_emb L M J Y"
   100   shows "X = Y"
   101 proof (rule injective_vimage_restrict)
   102   show "X \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))" "Y \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))"
   103     using sets[THEN sets_into_space] by (auto simp: space_PiM)
   104   have "\<forall>i\<in>L. \<exists>x. x \<in> space (M i)"
   105   proof
   106     fix i assume "i \<in> L"
   107     interpret prob_space "P {i}" using prob_space by simp
   108     from not_empty show "\<exists>x. x \<in> space (M i)" by (auto simp add: proj_space space_PiM)
   109   qed
   110   from bchoice[OF this]
   111   show "(\<Pi>\<^isub>E i\<in>L. space (M i)) \<noteq> {}" by auto
   112   show "(\<lambda>x. restrict x J) -` X \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i)) = (\<lambda>x. restrict x J) -` Y \<inter> (\<Pi>\<^isub>E i\<in>L. space (M i))"
   113     using `prod_emb L M J X = prod_emb L M J Y` by (simp add: prod_emb_def)
   114 qed fact
   115 
   116 abbreviation (in projective_family)
   117   "emb L K X \<equiv> prod_emb L M K X"
   118 
   119 definition (in projective_family) generator :: "('i \<Rightarrow> 'a) set set" where
   120   "generator = (\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` sets (Pi\<^isub>M J M))"
   121 
   122 lemma (in projective_family) generatorI':
   123   "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> emb I J X \<in> generator"
   124   unfolding generator_def by auto
   125 
   126 lemma (in projective_family) algebra_generator:
   127   assumes "I \<noteq> {}" shows "algebra (\<Pi>\<^isub>E i\<in>I. space (M i)) generator" (is "algebra ?\<Omega> ?G")
   128   unfolding algebra_def algebra_axioms_def ring_of_sets_iff
   129 proof (intro conjI ballI)
   130   let ?G = generator
   131   show "?G \<subseteq> Pow ?\<Omega>"
   132     by (auto simp: generator_def prod_emb_def)
   133   from `I \<noteq> {}` obtain i where "i \<in> I" by auto
   134   then show "{} \<in> ?G"
   135     by (auto intro!: exI[of _ "{i}"] image_eqI[where x="\<lambda>i. {}"]
   136              simp: sigma_sets.Empty generator_def prod_emb_def)
   137   from `i \<in> I` show "?\<Omega> \<in> ?G"
   138     by (auto intro!: exI[of _ "{i}"] image_eqI[where x="Pi\<^isub>E {i} (\<lambda>i. space (M i))"]
   139              simp: generator_def prod_emb_def)
   140   fix A assume "A \<in> ?G"
   141   then obtain JA XA where XA: "JA \<noteq> {}" "finite JA" "JA \<subseteq> I" "XA \<in> sets (Pi\<^isub>M JA M)" and A: "A = emb I JA XA"
   142     by (auto simp: generator_def)
   143   fix B assume "B \<in> ?G"
   144   then obtain JB XB where XB: "JB \<noteq> {}" "finite JB" "JB \<subseteq> I" "XB \<in> sets (Pi\<^isub>M JB M)" and B: "B = emb I JB XB"
   145     by (auto simp: generator_def)
   146   let ?RA = "emb (JA \<union> JB) JA XA"
   147   let ?RB = "emb (JA \<union> JB) JB XB"
   148   have *: "A - B = emb I (JA \<union> JB) (?RA - ?RB)" "A \<union> B = emb I (JA \<union> JB) (?RA \<union> ?RB)"
   149     using XA A XB B by auto
   150   show "A - B \<in> ?G" "A \<union> B \<in> ?G"
   151     unfolding * using XA XB by (safe intro!: generatorI') auto
   152 qed
   153 
   154 lemma (in projective_family) sets_PiM_generator:
   155   "sets (PiM I M) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
   156 proof cases
   157   assume "I = {}" then show ?thesis
   158     unfolding generator_def
   159     by (auto simp: sets_PiM_empty sigma_sets_empty_eq cong: conj_cong)
   160 next
   161   assume "I \<noteq> {}"
   162   show ?thesis
   163   proof
   164     show "sets (Pi\<^isub>M I M) \<subseteq> sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) generator"
   165       unfolding sets_PiM
   166     proof (safe intro!: sigma_sets_subseteq)
   167       fix A assume "A \<in> prod_algebra I M" with `I \<noteq> {}` show "A \<in> generator"
   168         by (auto intro!: generatorI' sets_PiM_I_finite elim!: prod_algebraE)
   169     qed
   170   qed (auto simp: generator_def space_PiM[symmetric] intro!: sigma_sets_subset)
   171 qed
   172 
   173 lemma (in projective_family) generatorI:
   174   "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> A = emb I J X \<Longrightarrow> A \<in> generator"
   175   unfolding generator_def by auto
   176 
   177 definition (in projective_family)
   178   "\<mu>G A =
   179     (THE x. \<forall>J. J \<noteq> {} \<longrightarrow> finite J \<longrightarrow> J \<subseteq> I \<longrightarrow> (\<forall>X\<in>sets (Pi\<^isub>M J M). A = emb I J X \<longrightarrow> x = emeasure (PiP J M P) X))"
   180 
   181 lemma (in projective_family) \<mu>G_spec:
   182   assumes J: "J \<noteq> {}" "finite J" "J \<subseteq> I" "A = emb I J X" "X \<in> sets (Pi\<^isub>M J M)"
   183   shows "\<mu>G A = emeasure (PiP J M P) X"
   184   unfolding \<mu>G_def
   185 proof (intro the_equality allI impI ballI)
   186   fix K Y assume K: "K \<noteq> {}" "finite K" "K \<subseteq> I" "A = emb I K Y" "Y \<in> sets (Pi\<^isub>M K M)"
   187   have "emeasure (PiP K M P) Y = emeasure (PiP (K \<union> J) M P) (emb (K \<union> J) K Y)"
   188     using K J by simp
   189   also have "emb (K \<union> J) K Y = emb (K \<union> J) J X"
   190     using K J by (simp add: prod_emb_injective[of "K \<union> J" I])
   191   also have "emeasure (PiP (K \<union> J) M P) (emb (K \<union> J) J X) = emeasure (PiP J M P) X"
   192     using K J by simp
   193   finally show "emeasure (PiP J M P) X = emeasure (PiP K M P) Y" ..
   194 qed (insert J, force)
   195 
   196 lemma (in projective_family) \<mu>G_eq:
   197   "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> \<mu>G (emb I J X) = emeasure (PiP J M P) X"
   198   by (intro \<mu>G_spec) auto
   199 
   200 lemma (in projective_family) generator_Ex:
   201   assumes *: "A \<in> generator"
   202   shows "\<exists>J X. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I \<and> X \<in> sets (Pi\<^isub>M J M) \<and> A = emb I J X \<and> \<mu>G A = emeasure (PiP J M P) X"
   203 proof -
   204   from * obtain J X where J: "J \<noteq> {}" "finite J" "J \<subseteq> I" "A = emb I J X" "X \<in> sets (Pi\<^isub>M J M)"
   205     unfolding generator_def by auto
   206   with \<mu>G_spec[OF this] show ?thesis by auto
   207 qed
   208 
   209 lemma (in projective_family) generatorE:
   210   assumes A: "A \<in> generator"
   211   obtains J X where "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)" "emb I J X = A" "\<mu>G A = emeasure (PiP J M P) X"
   212 proof -
   213   from generator_Ex[OF A] obtain X J where "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)" "emb I J X = A"
   214     "\<mu>G A = emeasure (PiP J M P) X" by auto
   215   then show thesis by (intro that) auto
   216 qed
   217 
   218 lemma (in projective_family) merge_sets:
   219   "J \<inter> K = {} \<Longrightarrow> A \<in> sets (Pi\<^isub>M (J \<union> K) M) \<Longrightarrow> x \<in> space (Pi\<^isub>M J M) \<Longrightarrow> (\<lambda>y. merge J K (x,y)) -` A \<inter> space (Pi\<^isub>M K M) \<in> sets (Pi\<^isub>M K M)"
   220   by simp
   221 
   222 lemma (in projective_family) merge_emb:
   223   assumes "K \<subseteq> I" "J \<subseteq> I" and y: "y \<in> space (Pi\<^isub>M J M)"
   224   shows "((\<lambda>x. merge J (I - J) (y, x)) -` emb I K X \<inter> space (Pi\<^isub>M I M)) =
   225     emb I (K - J) ((\<lambda>x. merge J (K - J) (y, x)) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M))"
   226 proof -
   227   have [simp]: "\<And>x J K L. merge J K (y, restrict x L) = merge J (K \<inter> L) (y, x)"
   228     by (auto simp: restrict_def merge_def)
   229   have [simp]: "\<And>x J K L. restrict (merge J K (y, x)) L = merge (J \<inter> L) (K \<inter> L) (y, x)"
   230     by (auto simp: restrict_def merge_def)
   231   have [simp]: "(I - J) \<inter> K = K - J" using `K \<subseteq> I` `J \<subseteq> I` by auto
   232   have [simp]: "(K - J) \<inter> (K \<union> J) = K - J" by auto
   233   have [simp]: "(K - J) \<inter> K = K - J" by auto
   234   from y `K \<subseteq> I` `J \<subseteq> I` show ?thesis
   235     by (simp split: split_merge add: prod_emb_def Pi_iff extensional_merge_sub set_eq_iff space_PiM)
   236        auto
   237 qed
   238 
   239 lemma (in projective_family) positive_\<mu>G:
   240   assumes "I \<noteq> {}"
   241   shows "positive generator \<mu>G"
   242 proof -
   243   interpret G!: algebra "\<Pi>\<^isub>E i\<in>I. space (M i)" generator by (rule algebra_generator) fact
   244   show ?thesis
   245   proof (intro positive_def[THEN iffD2] conjI ballI)
   246     from generatorE[OF G.empty_sets] guess J X . note this[simp]
   247     have "X = {}"
   248       by (rule prod_emb_injective[of J I]) simp_all
   249     then show "\<mu>G {} = 0" by simp
   250   next
   251     fix A assume "A \<in> generator"
   252     from generatorE[OF this] guess J X . note this[simp]
   253     show "0 \<le> \<mu>G A" by (simp add: emeasure_nonneg)
   254   qed
   255 qed
   256 
   257 lemma (in projective_family) additive_\<mu>G:
   258   assumes "I \<noteq> {}"
   259   shows "additive generator \<mu>G"
   260 proof -
   261   interpret G!: algebra "\<Pi>\<^isub>E i\<in>I. space (M i)" generator by (rule algebra_generator) fact
   262   show ?thesis
   263   proof (intro additive_def[THEN iffD2] ballI impI)
   264     fix A assume "A \<in> generator" with generatorE guess J X . note J = this
   265     fix B assume "B \<in> generator" with generatorE guess K Y . note K = this
   266     assume "A \<inter> B = {}"
   267     have JK: "J \<union> K \<noteq> {}" "J \<union> K \<subseteq> I" "finite (J \<union> K)"
   268       using J K by auto
   269     have JK_disj: "emb (J \<union> K) J X \<inter> emb (J \<union> K) K Y = {}"
   270       apply (rule prod_emb_injective[of "J \<union> K" I])
   271       apply (insert `A \<inter> B = {}` JK J K)
   272       apply (simp_all add: Int prod_emb_Int)
   273       done
   274     have AB: "A = emb I (J \<union> K) (emb (J \<union> K) J X)" "B = emb I (J \<union> K) (emb (J \<union> K) K Y)"
   275       using J K by simp_all
   276     then have "\<mu>G (A \<union> B) = \<mu>G (emb I (J \<union> K) (emb (J \<union> K) J X \<union> emb (J \<union> K) K Y))"
   277       by simp
   278     also have "\<dots> = emeasure (PiP (J \<union> K) M P) (emb (J \<union> K) J X \<union> emb (J \<union> K) K Y)"
   279       using JK J(1, 4) K(1, 4) by (simp add: \<mu>G_eq Un del: prod_emb_Un)
   280     also have "\<dots> = \<mu>G A + \<mu>G B"
   281       using J K JK_disj by (simp add: plus_emeasure[symmetric])
   282     finally show "\<mu>G (A \<union> B) = \<mu>G A + \<mu>G B" .
   283   qed
   284 qed
   285 
   286 lemma (in product_prob_space) PiP_PiM_finite[simp]:
   287   assumes "J \<noteq> {}" "finite J" "J \<subseteq> I" shows "PiP J M (\<lambda>J. PiM J M) = PiM J M"
   288   using assms by (simp add: PiP_finite)
   289 
   290 lemma (in product_prob_space) emeasure_PiM_emb_not_empty:
   291   assumes X: "J \<noteq> {}" "J \<subseteq> I" "finite J" "\<forall>i\<in>J. X i \<in> sets (M i)"
   292   shows "emeasure (Pi\<^isub>M I M) (emb I J (Pi\<^isub>E J X)) = emeasure (Pi\<^isub>M J M) (Pi\<^isub>E J X)"
   293 proof cases
   294   assume "finite I" with X show ?thesis by simp
   295 next
   296   let ?\<Omega> = "\<Pi>\<^isub>E i\<in>I. space (M i)"
   297   let ?G = generator
   298   assume "\<not> finite I"
   299   then have I_not_empty: "I \<noteq> {}" by auto
   300   interpret G!: algebra ?\<Omega> generator by (rule algebra_generator) fact
   301   note \<mu>G_mono =
   302     G.additive_increasing[OF positive_\<mu>G[OF I_not_empty] additive_\<mu>G[OF I_not_empty], THEN increasingD]
   303 
   304   { fix Z J assume J: "J \<noteq> {}" "finite J" "J \<subseteq> I" and Z: "Z \<in> ?G"
   305 
   306     from `infinite I` `finite J` obtain k where k: "k \<in> I" "k \<notin> J"
   307       by (metis rev_finite_subset subsetI)
   308     moreover from Z guess K' X' by (rule generatorE)
   309     moreover def K \<equiv> "insert k K'"
   310     moreover def X \<equiv> "emb K K' X'"
   311     ultimately have K: "K \<noteq> {}" "finite K" "K \<subseteq> I" "X \<in> sets (Pi\<^isub>M K M)" "Z = emb I K X"
   312       "K - J \<noteq> {}" "K - J \<subseteq> I" "\<mu>G Z = emeasure (Pi\<^isub>M K M) X"
   313       by (auto simp: subset_insertI)
   314     let ?M = "\<lambda>y. (\<lambda>x. merge J (K - J) (y, x)) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M)"
   315     { fix y assume y: "y \<in> space (Pi\<^isub>M J M)"
   316       note * = merge_emb[OF `K \<subseteq> I` `J \<subseteq> I` y, of X]
   317       moreover
   318       have **: "?M y \<in> sets (Pi\<^isub>M (K - J) M)"
   319         using J K y by (intro merge_sets) auto
   320       ultimately
   321       have ***: "((\<lambda>x. merge J (I - J) (y, x)) -` Z \<inter> space (Pi\<^isub>M I M)) \<in> ?G"
   322         using J K by (intro generatorI) auto
   323       have "\<mu>G ((\<lambda>x. merge J (I - J) (y, x)) -` emb I K X \<inter> space (Pi\<^isub>M I M)) = emeasure (Pi\<^isub>M (K - J) M) (?M y)"
   324         unfolding * using K J by (subst \<mu>G_eq[OF _ _ _ **]) auto
   325       note * ** *** this }
   326     note merge_in_G = this
   327 
   328     have "finite (K - J)" using K by auto
   329 
   330     interpret J: finite_product_prob_space M J by default fact+
   331     interpret KmJ: finite_product_prob_space M "K - J" by default fact+
   332 
   333     have "\<mu>G Z = emeasure (Pi\<^isub>M (J \<union> (K - J)) M) (emb (J \<union> (K - J)) K X)"
   334       using K J by simp
   335     also have "\<dots> = (\<integral>\<^isup>+ x. emeasure (Pi\<^isub>M (K - J) M) (?M x) \<partial>Pi\<^isub>M J M)"
   336       using K J by (subst emeasure_fold_integral) auto
   337     also have "\<dots> = (\<integral>\<^isup>+ y. \<mu>G ((\<lambda>x. merge J (I - J) (y, x)) -` Z \<inter> space (Pi\<^isub>M I M)) \<partial>Pi\<^isub>M J M)"
   338       (is "_ = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)")
   339     proof (intro positive_integral_cong)
   340       fix x assume x: "x \<in> space (Pi\<^isub>M J M)"
   341       with K merge_in_G(2)[OF this]
   342       show "emeasure (Pi\<^isub>M (K - J) M) (?M x) = \<mu>G (?MZ x)"
   343         unfolding `Z = emb I K X` merge_in_G(1)[OF x] by (subst \<mu>G_eq) auto
   344     qed
   345     finally have fold: "\<mu>G Z = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)" .
   346 
   347     { fix x assume x: "x \<in> space (Pi\<^isub>M J M)"
   348       then have "\<mu>G (?MZ x) \<le> 1"
   349         unfolding merge_in_G(4)[OF x] `Z = emb I K X`
   350         by (intro KmJ.measure_le_1 merge_in_G(2)[OF x]) }
   351     note le_1 = this
   352 
   353     let ?q = "\<lambda>y. \<mu>G ((\<lambda>x. merge J (I - J) (y,x)) -` Z \<inter> space (Pi\<^isub>M I M))"
   354     have "?q \<in> borel_measurable (Pi\<^isub>M J M)"
   355       unfolding `Z = emb I K X` using J K merge_in_G(3)
   356       by (simp add: merge_in_G  \<mu>G_eq emeasure_fold_measurable cong: measurable_cong)
   357     note this fold le_1 merge_in_G(3) }
   358   note fold = this
   359 
   360   have "\<exists>\<mu>. (\<forall>s\<in>?G. \<mu> s = \<mu>G s) \<and> measure_space ?\<Omega> (sigma_sets ?\<Omega> ?G) \<mu>"
   361   proof (rule G.caratheodory_empty_continuous[OF positive_\<mu>G additive_\<mu>G])
   362     fix A assume "A \<in> ?G"
   363     with generatorE guess J X . note JX = this
   364     interpret JK: finite_product_prob_space M J by default fact+ 
   365     from JX show "\<mu>G A \<noteq> \<infinity>" by simp
   366   next
   367     fix A assume A: "range A \<subseteq> ?G" "decseq A" "(\<Inter>i. A i) = {}"
   368     then have "decseq (\<lambda>i. \<mu>G (A i))"
   369       by (auto intro!: \<mu>G_mono simp: decseq_def)
   370     moreover
   371     have "(INF i. \<mu>G (A i)) = 0"
   372     proof (rule ccontr)
   373       assume "(INF i. \<mu>G (A i)) \<noteq> 0" (is "?a \<noteq> 0")
   374       moreover have "0 \<le> ?a"
   375         using A positive_\<mu>G[OF I_not_empty] by (auto intro!: INF_greatest simp: positive_def)
   376       ultimately have "0 < ?a" by auto
   377 
   378       have "\<forall>n. \<exists>J X. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I \<and> X \<in> sets (Pi\<^isub>M J M) \<and> A n = emb I J X \<and> \<mu>G (A n) = emeasure (PiP J M (\<lambda>J. (Pi\<^isub>M J M))) X"
   379         using A by (intro allI generator_Ex) auto
   380       then obtain J' X' where J': "\<And>n. J' n \<noteq> {}" "\<And>n. finite (J' n)" "\<And>n. J' n \<subseteq> I" "\<And>n. X' n \<in> sets (Pi\<^isub>M (J' n) M)"
   381         and A': "\<And>n. A n = emb I (J' n) (X' n)"
   382         unfolding choice_iff by blast
   383       moreover def J \<equiv> "\<lambda>n. (\<Union>i\<le>n. J' i)"
   384       moreover def X \<equiv> "\<lambda>n. emb (J n) (J' n) (X' n)"
   385       ultimately have J: "\<And>n. J n \<noteq> {}" "\<And>n. finite (J n)" "\<And>n. J n \<subseteq> I" "\<And>n. X n \<in> sets (Pi\<^isub>M (J n) M)"
   386         by auto
   387       with A' have A_eq: "\<And>n. A n = emb I (J n) (X n)" "\<And>n. A n \<in> ?G"
   388         unfolding J_def X_def by (subst prod_emb_trans) (insert A, auto)
   389 
   390       have J_mono: "\<And>n m. n \<le> m \<Longrightarrow> J n \<subseteq> J m"
   391         unfolding J_def by force
   392 
   393       interpret J: finite_product_prob_space M "J i" for i by default fact+
   394 
   395       have a_le_1: "?a \<le> 1"
   396         using \<mu>G_spec[of "J 0" "A 0" "X 0"] J A_eq
   397         by (auto intro!: INF_lower2[of 0] J.measure_le_1)
   398 
   399       let ?M = "\<lambda>K Z y. (\<lambda>x. merge K (I - K) (y, x)) -` Z \<inter> space (Pi\<^isub>M I M)"
   400 
   401       { fix Z k assume Z: "range Z \<subseteq> ?G" "decseq Z" "\<forall>n. ?a / 2^k \<le> \<mu>G (Z n)"
   402         then have Z_sets: "\<And>n. Z n \<in> ?G" by auto
   403         fix J' assume J': "J' \<noteq> {}" "finite J'" "J' \<subseteq> I"
   404         interpret J': finite_product_prob_space M J' by default fact+
   405 
   406         let ?q = "\<lambda>n y. \<mu>G (?M J' (Z n) y)"
   407         let ?Q = "\<lambda>n. ?q n -` {?a / 2^(k+1) ..} \<inter> space (Pi\<^isub>M J' M)"
   408         { fix n
   409           have "?q n \<in> borel_measurable (Pi\<^isub>M J' M)"
   410             using Z J' by (intro fold(1)) auto
   411           then have "?Q n \<in> sets (Pi\<^isub>M J' M)"
   412             by (rule measurable_sets) auto }
   413         note Q_sets = this
   414 
   415         have "?a / 2^(k+1) \<le> (INF n. emeasure (Pi\<^isub>M J' M) (?Q n))"
   416         proof (intro INF_greatest)
   417           fix n
   418           have "?a / 2^k \<le> \<mu>G (Z n)" using Z by auto
   419           also have "\<dots> \<le> (\<integral>\<^isup>+ x. indicator (?Q n) x + ?a / 2^(k+1) \<partial>Pi\<^isub>M J' M)"
   420             unfolding fold(2)[OF J' `Z n \<in> ?G`]
   421           proof (intro positive_integral_mono)
   422             fix x assume x: "x \<in> space (Pi\<^isub>M J' M)"
   423             then have "?q n x \<le> 1 + 0"
   424               using J' Z fold(3) Z_sets by auto
   425             also have "\<dots> \<le> 1 + ?a / 2^(k+1)"
   426               using `0 < ?a` by (intro add_mono) auto
   427             finally have "?q n x \<le> 1 + ?a / 2^(k+1)" .
   428             with x show "?q n x \<le> indicator (?Q n) x + ?a / 2^(k+1)"
   429               by (auto split: split_indicator simp del: power_Suc)
   430           qed
   431           also have "\<dots> = emeasure (Pi\<^isub>M J' M) (?Q n) + ?a / 2^(k+1)"
   432             using `0 \<le> ?a` Q_sets J'.emeasure_space_1
   433             by (subst positive_integral_add) auto
   434           finally show "?a / 2^(k+1) \<le> emeasure (Pi\<^isub>M J' M) (?Q n)" using `?a \<le> 1`
   435             by (cases rule: ereal2_cases[of ?a "emeasure (Pi\<^isub>M J' M) (?Q n)"])
   436                (auto simp: field_simps)
   437         qed
   438         also have "\<dots> = emeasure (Pi\<^isub>M J' M) (\<Inter>n. ?Q n)"
   439         proof (intro INF_emeasure_decseq)
   440           show "range ?Q \<subseteq> sets (Pi\<^isub>M J' M)" using Q_sets by auto
   441           show "decseq ?Q"
   442             unfolding decseq_def
   443           proof (safe intro!: vimageI[OF refl])
   444             fix m n :: nat assume "m \<le> n"
   445             fix x assume x: "x \<in> space (Pi\<^isub>M J' M)"
   446             assume "?a / 2^(k+1) \<le> ?q n x"
   447             also have "?q n x \<le> ?q m x"
   448             proof (rule \<mu>G_mono)
   449               from fold(4)[OF J', OF Z_sets x]
   450               show "?M J' (Z n) x \<in> ?G" "?M J' (Z m) x \<in> ?G" by auto
   451               show "?M J' (Z n) x \<subseteq> ?M J' (Z m) x"
   452                 using `decseq Z`[THEN decseqD, OF `m \<le> n`] by auto
   453             qed
   454             finally show "?a / 2^(k+1) \<le> ?q m x" .
   455           qed
   456         qed simp
   457         finally have "(\<Inter>n. ?Q n) \<noteq> {}"
   458           using `0 < ?a` `?a \<le> 1` by (cases ?a) (auto simp: divide_le_0_iff power_le_zero_eq)
   459         then have "\<exists>w\<in>space (Pi\<^isub>M J' M). \<forall>n. ?a / 2 ^ (k + 1) \<le> ?q n w" by auto }
   460       note Ex_w = this
   461 
   462       let ?q = "\<lambda>k n y. \<mu>G (?M (J k) (A n) y)"
   463 
   464       have "\<forall>n. ?a / 2 ^ 0 \<le> \<mu>G (A n)" by (auto intro: INF_lower)
   465       from Ex_w[OF A(1,2) this J(1-3), of 0] guess w0 .. note w0 = this
   466 
   467       let ?P =
   468         "\<lambda>k wk w. w \<in> space (Pi\<^isub>M (J (Suc k)) M) \<and> restrict w (J k) = wk \<and>
   469           (\<forall>n. ?a / 2 ^ (Suc k + 1) \<le> ?q (Suc k) n w)"
   470       def w \<equiv> "nat_rec w0 (\<lambda>k wk. Eps (?P k wk))"
   471 
   472       { fix k have w: "w k \<in> space (Pi\<^isub>M (J k) M) \<and>
   473           (\<forall>n. ?a / 2 ^ (k + 1) \<le> ?q k n (w k)) \<and> (k \<noteq> 0 \<longrightarrow> restrict (w k) (J (k - 1)) = w (k - 1))"
   474         proof (induct k)
   475           case 0 with w0 show ?case
   476             unfolding w_def nat_rec_0 by auto
   477         next
   478           case (Suc k)
   479           then have wk: "w k \<in> space (Pi\<^isub>M (J k) M)" by auto
   480           have "\<exists>w'. ?P k (w k) w'"
   481           proof cases
   482             assume [simp]: "J k = J (Suc k)"
   483             show ?thesis
   484             proof (intro exI[of _ "w k"] conjI allI)
   485               fix n
   486               have "?a / 2 ^ (Suc k + 1) \<le> ?a / 2 ^ (k + 1)"
   487                 using `0 < ?a` `?a \<le> 1` by (cases ?a) (auto simp: field_simps)
   488               also have "\<dots> \<le> ?q k n (w k)" using Suc by auto
   489               finally show "?a / 2 ^ (Suc k + 1) \<le> ?q (Suc k) n (w k)" by simp
   490             next
   491               show "w k \<in> space (Pi\<^isub>M (J (Suc k)) M)"
   492                 using Suc by simp
   493               then show "restrict (w k) (J k) = w k"
   494                 by (simp add: extensional_restrict space_PiM)
   495             qed
   496           next
   497             assume "J k \<noteq> J (Suc k)"
   498             with J_mono[of k "Suc k"] have "J (Suc k) - J k \<noteq> {}" (is "?D \<noteq> {}") by auto
   499             have "range (\<lambda>n. ?M (J k) (A n) (w k)) \<subseteq> ?G"
   500               "decseq (\<lambda>n. ?M (J k) (A n) (w k))"
   501               "\<forall>n. ?a / 2 ^ (k + 1) \<le> \<mu>G (?M (J k) (A n) (w k))"
   502               using `decseq A` fold(4)[OF J(1-3) A_eq(2), of "w k" k] Suc
   503               by (auto simp: decseq_def)
   504             from Ex_w[OF this `?D \<noteq> {}`] J[of "Suc k"]
   505             obtain w' where w': "w' \<in> space (Pi\<^isub>M ?D M)"
   506               "\<forall>n. ?a / 2 ^ (Suc k + 1) \<le> \<mu>G (?M ?D (?M (J k) (A n) (w k)) w')" by auto
   507             let ?w = "merge (J k) ?D (w k, w')"
   508             have [simp]: "\<And>x. merge (J k) (I - J k) (w k, merge ?D (I - ?D) (w', x)) =
   509               merge (J (Suc k)) (I - (J (Suc k))) (?w, x)"
   510               using J(3)[of "Suc k"] J(3)[of k] J_mono[of k "Suc k"]
   511               by (auto intro!: ext split: split_merge)
   512             have *: "\<And>n. ?M ?D (?M (J k) (A n) (w k)) w' = ?M (J (Suc k)) (A n) ?w"
   513               using w'(1) J(3)[of "Suc k"]
   514               by (auto simp: space_PiM split: split_merge intro!: extensional_merge_sub) force+
   515             show ?thesis
   516               apply (rule exI[of _ ?w])
   517               using w' J_mono[of k "Suc k"] wk unfolding *
   518               apply (auto split: split_merge intro!: extensional_merge_sub ext simp: space_PiM)
   519               apply (force simp: extensional_def)
   520               done
   521           qed
   522           then have "?P k (w k) (w (Suc k))"
   523             unfolding w_def nat_rec_Suc unfolding w_def[symmetric]
   524             by (rule someI_ex)
   525           then show ?case by auto
   526         qed
   527         moreover
   528         then have "w k \<in> space (Pi\<^isub>M (J k) M)" by auto
   529         moreover
   530         from w have "?a / 2 ^ (k + 1) \<le> ?q k k (w k)" by auto
   531         then have "?M (J k) (A k) (w k) \<noteq> {}"
   532           using positive_\<mu>G[OF I_not_empty, unfolded positive_def] `0 < ?a` `?a \<le> 1`
   533           by (cases ?a) (auto simp: divide_le_0_iff power_le_zero_eq)
   534         then obtain x where "x \<in> ?M (J k) (A k) (w k)" by auto
   535         then have "merge (J k) (I - J k) (w k, x) \<in> A k" by auto
   536         then have "\<exists>x\<in>A k. restrict x (J k) = w k"
   537           using `w k \<in> space (Pi\<^isub>M (J k) M)`
   538           by (intro rev_bexI) (auto intro!: ext simp: extensional_def space_PiM)
   539         ultimately have "w k \<in> space (Pi\<^isub>M (J k) M)"
   540           "\<exists>x\<in>A k. restrict x (J k) = w k"
   541           "k \<noteq> 0 \<Longrightarrow> restrict (w k) (J (k - 1)) = w (k - 1)"
   542           by auto }
   543       note w = this
   544 
   545       { fix k l i assume "k \<le> l" "i \<in> J k"
   546         { fix l have "w k i = w (k + l) i"
   547           proof (induct l)
   548             case (Suc l)
   549             from `i \<in> J k` J_mono[of k "k + l"] have "i \<in> J (k + l)" by auto
   550             with w(3)[of "k + Suc l"]
   551             have "w (k + l) i = w (k + Suc l) i"
   552               by (auto simp: restrict_def fun_eq_iff split: split_if_asm)
   553             with Suc show ?case by simp
   554           qed simp }
   555         from this[of "l - k"] `k \<le> l` have "w l i = w k i" by simp }
   556       note w_mono = this
   557 
   558       def w' \<equiv> "\<lambda>i. if i \<in> (\<Union>k. J k) then w (LEAST k. i \<in> J k) i else if i \<in> I then (SOME x. x \<in> space (M i)) else undefined"
   559       { fix i k assume k: "i \<in> J k"
   560         have "w k i = w (LEAST k. i \<in> J k) i"
   561           by (intro w_mono Least_le k LeastI[of _ k])
   562         then have "w' i = w k i"
   563           unfolding w'_def using k by auto }
   564       note w'_eq = this
   565       have w'_simps1: "\<And>i. i \<notin> I \<Longrightarrow> w' i = undefined"
   566         using J by (auto simp: w'_def)
   567       have w'_simps2: "\<And>i. i \<notin> (\<Union>k. J k) \<Longrightarrow> i \<in> I \<Longrightarrow> w' i \<in> space (M i)"
   568         using J by (auto simp: w'_def intro!: someI_ex[OF M.not_empty[unfolded ex_in_conv[symmetric]]])
   569       { fix i assume "i \<in> I" then have "w' i \<in> space (M i)"
   570           using w(1) by (cases "i \<in> (\<Union>k. J k)") (force simp: w'_simps2 w'_eq space_PiM)+ }
   571       note w'_simps[simp] = w'_eq w'_simps1 w'_simps2 this
   572 
   573       have w': "w' \<in> space (Pi\<^isub>M I M)"
   574         using w(1) by (auto simp add: Pi_iff extensional_def space_PiM)
   575 
   576       { fix n
   577         have "restrict w' (J n) = w n" using w(1)
   578           by (auto simp add: fun_eq_iff restrict_def Pi_iff extensional_def space_PiM)
   579         with w[of n] obtain x where "x \<in> A n" "restrict x (J n) = restrict w' (J n)" by auto
   580         then have "w' \<in> A n" unfolding A_eq using w' by (auto simp: prod_emb_def space_PiM) }
   581       then have "w' \<in> (\<Inter>i. A i)" by auto
   582       with `(\<Inter>i. A i) = {}` show False by auto
   583     qed
   584     ultimately show "(\<lambda>i. \<mu>G (A i)) ----> 0"
   585       using LIMSEQ_ereal_INFI[of "\<lambda>i. \<mu>G (A i)"] by simp
   586   qed fact+
   587   then guess \<mu> .. note \<mu> = this
   588   show ?thesis
   589   proof (subst emeasure_extend_measure_Pair[OF PiM_def, of I M \<mu> J X])
   590     from assms show "(J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))"
   591       by (simp add: Pi_iff)
   592   next
   593     fix J X assume J: "(J \<noteq> {} \<or> I = {}) \<and> finite J \<and> J \<subseteq> I \<and> X \<in> (\<Pi> j\<in>J. sets (M j))"
   594     then show "emb I J (Pi\<^isub>E J X) \<in> Pow (\<Pi>\<^isub>E i\<in>I. space (M i))"
   595       by (auto simp: Pi_iff prod_emb_def dest: sets_into_space)
   596     have "emb I J (Pi\<^isub>E J X) \<in> generator"
   597       using J `I \<noteq> {}` by (intro generatorI') (auto simp: Pi_iff)
   598     then have "\<mu> (emb I J (Pi\<^isub>E J X)) = \<mu>G (emb I J (Pi\<^isub>E J X))"
   599       using \<mu> by simp
   600     also have "\<dots> = (\<Prod> j\<in>J. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j)))"
   601       using J  `I \<noteq> {}` by (subst \<mu>G_spec[OF _ _ _ refl]) (auto simp: emeasure_PiM Pi_iff)
   602     also have "\<dots> = (\<Prod>j\<in>J \<union> {i \<in> I. emeasure (M i) (space (M i)) \<noteq> 1}.
   603       if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j)))"
   604       using J `I \<noteq> {}` by (intro setprod_mono_one_right) (auto simp: M.emeasure_space_1)
   605     finally show "\<mu> (emb I J (Pi\<^isub>E J X)) = \<dots>" .
   606   next
   607     let ?F = "\<lambda>j. if j \<in> J then emeasure (M j) (X j) else emeasure (M j) (space (M j))"
   608     have "(\<Prod>j\<in>J \<union> {i \<in> I. emeasure (M i) (space (M i)) \<noteq> 1}. ?F j) = (\<Prod>j\<in>J. ?F j)"
   609       using X `I \<noteq> {}` by (intro setprod_mono_one_right) (auto simp: M.emeasure_space_1)
   610     then show "(\<Prod>j\<in>J \<union> {i \<in> I. emeasure (M i) (space (M i)) \<noteq> 1}. ?F j) =
   611       emeasure (Pi\<^isub>M J M) (Pi\<^isub>E J X)"
   612       using X by (auto simp add: emeasure_PiM) 
   613   next
   614     show "positive (sets (Pi\<^isub>M I M)) \<mu>" "countably_additive (sets (Pi\<^isub>M I M)) \<mu>"
   615       using \<mu> unfolding sets_PiM_generator by (auto simp: measure_space_def)
   616   qed
   617 qed
   618 
   619 sublocale product_prob_space \<subseteq> P: prob_space "Pi\<^isub>M I M"
   620 proof
   621   show "emeasure (Pi\<^isub>M I M) (space (Pi\<^isub>M I M)) = 1"
   622   proof cases
   623     assume "I = {}" then show ?thesis by (simp add: space_PiM_empty)
   624   next
   625     assume "I \<noteq> {}"
   626     then obtain i where "i \<in> I" by auto
   627     moreover then have "emb I {i} (\<Pi>\<^isub>E i\<in>{i}. space (M i)) = (space (Pi\<^isub>M I M))"
   628       by (auto simp: prod_emb_def space_PiM)
   629     ultimately show ?thesis
   630       using emeasure_PiM_emb_not_empty[of "{i}" "\<lambda>i. space (M i)"]
   631       by (simp add: emeasure_PiM emeasure_space_1)
   632   qed
   633 qed
   634 
   635 lemma (in product_prob_space) emeasure_PiM_emb:
   636   assumes X: "J \<subseteq> I" "finite J" "\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)"
   637   shows "emeasure (Pi\<^isub>M I M) (emb I J (Pi\<^isub>E J X)) = (\<Prod> i\<in>J. emeasure (M i) (X i))"
   638 proof cases
   639   assume "J = {}"
   640   moreover have "emb I {} {\<lambda>x. undefined} = space (Pi\<^isub>M I M)"
   641     by (auto simp: space_PiM prod_emb_def)
   642   ultimately show ?thesis
   643     by (simp add: space_PiM_empty P.emeasure_space_1)
   644 next
   645   assume "J \<noteq> {}" with X show ?thesis
   646     by (subst emeasure_PiM_emb_not_empty) (auto simp: emeasure_PiM)
   647 qed
   648 
   649 lemma (in product_prob_space) emeasure_PiM_Collect:
   650   assumes X: "J \<subseteq> I" "finite J" "\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)"
   651   shows "emeasure (Pi\<^isub>M I M) {x\<in>space (Pi\<^isub>M I M). \<forall>i\<in>J. x i \<in> X i} = (\<Prod> i\<in>J. emeasure (M i) (X i))"
   652 proof -
   653   have "{x\<in>space (Pi\<^isub>M I M). \<forall>i\<in>J. x i \<in> X i} = emb I J (Pi\<^isub>E J X)"
   654     unfolding prod_emb_def using assms by (auto simp: space_PiM Pi_iff)
   655   with emeasure_PiM_emb[OF assms] show ?thesis by simp
   656 qed
   657 
   658 lemma (in product_prob_space) emeasure_PiM_Collect_single:
   659   assumes X: "i \<in> I" "A \<in> sets (M i)"
   660   shows "emeasure (Pi\<^isub>M I M) {x\<in>space (Pi\<^isub>M I M). x i \<in> A} = emeasure (M i) A"
   661   using emeasure_PiM_Collect[of "{i}" "\<lambda>i. A"] assms
   662   by simp
   663 
   664 lemma (in product_prob_space) measure_PiM_emb:
   665   assumes "J \<subseteq> I" "finite J" "\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)"
   666   shows "measure (PiM I M) (emb I J (Pi\<^isub>E J X)) = (\<Prod> i\<in>J. measure (M i) (X i))"
   667   using emeasure_PiM_emb[OF assms]
   668   unfolding emeasure_eq_measure M.emeasure_eq_measure by (simp add: setprod_ereal)
   669 
   670 lemma sets_Collect_single':
   671   "i \<in> I \<Longrightarrow> {x\<in>space (M i). P x} \<in> sets (M i) \<Longrightarrow> {x\<in>space (PiM I M). P (x i)} \<in> sets (PiM I M)"
   672   using sets_Collect_single[of i I "{x\<in>space (M i). P x}" M]
   673   by (simp add: space_PiM Pi_iff cong: conj_cong)
   674 
   675 lemma (in finite_product_prob_space) finite_measure_PiM_emb:
   676   "(\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)) \<Longrightarrow> measure (PiM I M) (Pi\<^isub>E I A) = (\<Prod>i\<in>I. measure (M i) (A i))"
   677   using measure_PiM_emb[of I A] finite_index prod_emb_PiE_same_index[OF sets_into_space, of I A M]
   678   by auto
   679 
   680 lemma (in product_prob_space) PiM_component:
   681   assumes "i \<in> I"
   682   shows "distr (PiM I M) (M i) (\<lambda>\<omega>. \<omega> i) = M i"
   683 proof (rule measure_eqI[symmetric])
   684   fix A assume "A \<in> sets (M i)"
   685   moreover have "((\<lambda>\<omega>. \<omega> i) -` A \<inter> space (PiM I M)) = {x\<in>space (PiM I M). x i \<in> A}"
   686     by auto
   687   ultimately show "emeasure (M i) A = emeasure (distr (PiM I M) (M i) (\<lambda>\<omega>. \<omega> i)) A"
   688     by (auto simp: `i\<in>I` emeasure_distr measurable_component_singleton emeasure_PiM_Collect_single)
   689 qed simp
   690 
   691 lemma (in product_prob_space) PiM_eq:
   692   assumes "I \<noteq> {}"
   693   assumes "sets M' = sets (PiM I M)"
   694   assumes eq: "\<And>J F. finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> (\<And>j. j \<in> J \<Longrightarrow> F j \<in> sets (M j)) \<Longrightarrow>
   695     emeasure M' (prod_emb I M J (\<Pi>\<^isub>E j\<in>J. F j)) = (\<Prod>j\<in>J. emeasure (M j) (F j))"
   696   shows "M' = (PiM I M)"
   697 proof (rule measure_eqI_generator_eq[symmetric, OF Int_stable_prod_algebra prod_algebra_sets_into_space])
   698   show "sets (PiM I M) = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) (prod_algebra I M)"
   699     by (rule sets_PiM)
   700   then show "sets M' = sigma_sets (\<Pi>\<^isub>E i\<in>I. space (M i)) (prod_algebra I M)"
   701     unfolding `sets M' = sets (PiM I M)` by simp
   702 
   703   def i \<equiv> "SOME i. i \<in> I"
   704   with `I \<noteq> {}` have i: "i \<in> I"
   705     by (auto intro: someI_ex)
   706 
   707   def A \<equiv> "\<lambda>n::nat. prod_emb I M {i} (\<Pi>\<^isub>E j\<in>{i}. space (M i))"
   708   then show "range A \<subseteq> prod_algebra I M"
   709     by (auto intro!: prod_algebraI i)
   710 
   711   have A_eq: "\<And>i. A i = space (PiM I M)"
   712     by (auto simp: prod_emb_def space_PiM Pi_iff A_def i)
   713   show "(\<Union>i. A i) = (\<Pi>\<^isub>E i\<in>I. space (M i))"
   714     unfolding A_eq by (auto simp: space_PiM)
   715   show "\<And>i. emeasure (PiM I M) (A i) \<noteq> \<infinity>"
   716     unfolding A_eq P.emeasure_space_1 by simp
   717 next
   718   fix X assume X: "X \<in> prod_algebra I M"
   719   then obtain J E where X: "X = prod_emb I M J (PIE j:J. E j)"
   720     and J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)"
   721     by (force elim!: prod_algebraE)
   722   from eq[OF J] have "emeasure M' X = (\<Prod>j\<in>J. emeasure (M j) (E j))"
   723     by (simp add: X)
   724   also have "\<dots> = emeasure (PiM I M) X"
   725     unfolding X using J by (intro emeasure_PiM_emb[symmetric]) auto
   726   finally show "emeasure (PiM I M) X = emeasure M' X" ..
   727 qed
   728 
   729 subsection {* Sequence space *}
   730 
   731 lemma measurable_nat_case: "(\<lambda>(x, \<omega>). nat_case x \<omega>) \<in> measurable (M \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)) (\<Pi>\<^isub>M i\<in>UNIV. M)"
   732 proof (rule measurable_PiM_single)
   733   show "(\<lambda>(x, \<omega>). nat_case x \<omega>) \<in> space (M \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)) \<rightarrow> (UNIV \<rightarrow>\<^isub>E space M)"
   734     by (auto simp: space_pair_measure space_PiM Pi_iff split: nat.split)
   735   fix i :: nat and A assume A: "A \<in> sets M"
   736   then have *: "{\<omega> \<in> space (M \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)). prod_case nat_case \<omega> i \<in> A} =
   737     (case i of 0 \<Rightarrow> A \<times> space (\<Pi>\<^isub>M i\<in>UNIV. M) | Suc n \<Rightarrow> space M \<times> {\<omega>\<in>space (\<Pi>\<^isub>M i\<in>UNIV. M). \<omega> n \<in> A})"
   738     by (auto simp: space_PiM space_pair_measure split: nat.split dest: sets_into_space)
   739   show "{\<omega> \<in> space (M \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)). prod_case nat_case \<omega> i \<in> A} \<in> sets (M \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M))"
   740     unfolding * by (auto simp: A split: nat.split intro!: sets_Collect_single)
   741 qed
   742 
   743 lemma measurable_nat_case':
   744   assumes f: "f \<in> measurable N M" and g: "g \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)"
   745   shows "(\<lambda>x. nat_case (f x) (g x)) \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)"
   746   using measurable_compose[OF measurable_Pair[OF f g] measurable_nat_case] by simp
   747 
   748 definition comb_seq :: "nat \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a)" where
   749   "comb_seq i \<omega> \<omega>' j = (if j < i then \<omega> j else \<omega>' (j - i))"
   750 
   751 lemma split_comb_seq: "P (comb_seq i \<omega> \<omega>' j) \<longleftrightarrow> (j < i \<longrightarrow> P (\<omega> j)) \<and> (\<forall>k. j = i + k \<longrightarrow> P (\<omega>' k))"
   752   by (auto simp: comb_seq_def not_less)
   753 
   754 lemma split_comb_seq_asm: "P (comb_seq i \<omega> \<omega>' j) \<longleftrightarrow> \<not> ((j < i \<and> \<not> P (\<omega> j)) \<or> (\<exists>k. j = i + k \<and> \<not> P (\<omega>' k)))"
   755   by (auto simp: comb_seq_def)
   756 
   757 lemma measurable_comb_seq: "(\<lambda>(\<omega>, \<omega>'). comb_seq i \<omega> \<omega>') \<in> measurable ((\<Pi>\<^isub>M i\<in>UNIV. M) \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)) (\<Pi>\<^isub>M i\<in>UNIV. M)"
   758 proof (rule measurable_PiM_single)
   759   show "(\<lambda>(\<omega>, \<omega>'). comb_seq i \<omega> \<omega>') \<in> space ((\<Pi>\<^isub>M i\<in>UNIV. M) \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)) \<rightarrow> (UNIV \<rightarrow>\<^isub>E space M)"
   760     by (auto simp: space_pair_measure space_PiM Pi_iff split: split_comb_seq)
   761   fix j :: nat and A assume A: "A \<in> sets M"
   762   then have *: "{\<omega> \<in> space ((\<Pi>\<^isub>M i\<in>UNIV. M) \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)). prod_case (comb_seq i) \<omega> j \<in> A} =
   763     (if j < i then {\<omega> \<in> space (\<Pi>\<^isub>M i\<in>UNIV. M). \<omega> j \<in> A} \<times> space (\<Pi>\<^isub>M i\<in>UNIV. M)
   764               else space (\<Pi>\<^isub>M i\<in>UNIV. M) \<times> {\<omega> \<in> space (\<Pi>\<^isub>M i\<in>UNIV. M). \<omega> (j - i) \<in> A})"
   765     by (auto simp: space_PiM space_pair_measure comb_seq_def dest: sets_into_space)
   766   show "{\<omega> \<in> space ((\<Pi>\<^isub>M i\<in>UNIV. M) \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M)). prod_case (comb_seq i) \<omega> j \<in> A} \<in> sets ((\<Pi>\<^isub>M i\<in>UNIV. M) \<Otimes>\<^isub>M (\<Pi>\<^isub>M i\<in>UNIV. M))"
   767     unfolding * by (auto simp: A intro!: sets_Collect_single)
   768 qed
   769 
   770 lemma measurable_comb_seq':
   771   assumes f: "f \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)" and g: "g \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)"
   772   shows "(\<lambda>x. comb_seq i (f x) (g x)) \<in> measurable N (\<Pi>\<^isub>M i\<in>UNIV. M)"
   773   using measurable_compose[OF measurable_Pair[OF f g] measurable_comb_seq] by simp
   774 
   775 locale sequence_space = product_prob_space "\<lambda>i. M" "UNIV :: nat set" for M
   776 begin
   777 
   778 abbreviation "S \<equiv> \<Pi>\<^isub>M i\<in>UNIV::nat set. M"
   779 
   780 lemma infprod_in_sets[intro]:
   781   fixes E :: "nat \<Rightarrow> 'a set" assumes E: "\<And>i. E i \<in> sets M"
   782   shows "Pi UNIV E \<in> sets S"
   783 proof -
   784   have "Pi UNIV E = (\<Inter>i. emb UNIV {..i} (\<Pi>\<^isub>E j\<in>{..i}. E j))"
   785     using E E[THEN sets_into_space]
   786     by (auto simp: prod_emb_def Pi_iff extensional_def) blast
   787   with E show ?thesis by auto
   788 qed
   789 
   790 lemma measure_PiM_countable:
   791   fixes E :: "nat \<Rightarrow> 'a set" assumes E: "\<And>i. E i \<in> sets M"
   792   shows "(\<lambda>n. \<Prod>i\<le>n. measure M (E i)) ----> measure S (Pi UNIV E)"
   793 proof -
   794   let ?E = "\<lambda>n. emb UNIV {..n} (Pi\<^isub>E {.. n} E)"
   795   have "\<And>n. (\<Prod>i\<le>n. measure M (E i)) = measure S (?E n)"
   796     using E by (simp add: measure_PiM_emb)
   797   moreover have "Pi UNIV E = (\<Inter>n. ?E n)"
   798     using E E[THEN sets_into_space]
   799     by (auto simp: prod_emb_def extensional_def Pi_iff) blast
   800   moreover have "range ?E \<subseteq> sets S"
   801     using E by auto
   802   moreover have "decseq ?E"
   803     by (auto simp: prod_emb_def Pi_iff decseq_def)
   804   ultimately show ?thesis
   805     by (simp add: finite_Lim_measure_decseq)
   806 qed
   807 
   808 lemma nat_eq_diff_eq: 
   809   fixes a b c :: nat
   810   shows "c \<le> b \<Longrightarrow> a = b - c \<longleftrightarrow> a + c = b"
   811   by auto
   812 
   813 lemma PiM_comb_seq:
   814   "distr (S \<Otimes>\<^isub>M S) S (\<lambda>(\<omega>, \<omega>'). comb_seq i \<omega> \<omega>') = S" (is "?D = _")
   815 proof (rule PiM_eq)
   816   let ?I = "UNIV::nat set" and ?M = "\<lambda>n. M"
   817   let "distr _ _ ?f" = "?D"
   818 
   819   fix J E assume J: "finite J" "J \<subseteq> ?I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets M"
   820   let ?X = "prod_emb ?I ?M J (\<Pi>\<^isub>E j\<in>J. E j)"
   821   have "\<And>j x. j \<in> J \<Longrightarrow> x \<in> E j \<Longrightarrow> x \<in> space M"
   822     using J(3)[THEN sets_into_space] by (auto simp: space_PiM Pi_iff subset_eq)
   823   with J have "?f -` ?X \<inter> space (S \<Otimes>\<^isub>M S) =
   824     (prod_emb ?I ?M (J \<inter> {..<i}) (PIE j:J \<inter> {..<i}. E j)) \<times>
   825     (prod_emb ?I ?M ((op + i) -` J) (PIE j:(op + i) -` J. E (i + j)))" (is "_ = ?E \<times> ?F")
   826    by (auto simp: space_pair_measure space_PiM prod_emb_def all_conj_distrib Pi_iff
   827                split: split_comb_seq split_comb_seq_asm)
   828   then have "emeasure ?D ?X = emeasure (S \<Otimes>\<^isub>M S) (?E \<times> ?F)"
   829     by (subst emeasure_distr[OF measurable_comb_seq])
   830        (auto intro!: sets_PiM_I simp: split_beta' J)
   831   also have "\<dots> = emeasure S ?E * emeasure S ?F"
   832     using J by (intro P.emeasure_pair_measure_Times)  (auto intro!: sets_PiM_I finite_vimageI simp: inj_on_def)
   833   also have "emeasure S ?F = (\<Prod>j\<in>(op + i) -` J. emeasure M (E (i + j)))"
   834     using J by (intro emeasure_PiM_emb) (simp_all add: finite_vimageI inj_on_def)
   835   also have "\<dots> = (\<Prod>j\<in>J - (J \<inter> {..<i}). emeasure M (E j))"
   836     by (rule strong_setprod_reindex_cong[where f="\<lambda>x. x - i"])
   837        (auto simp: image_iff Bex_def not_less nat_eq_diff_eq ac_simps cong: conj_cong intro!: inj_onI)
   838   also have "emeasure S ?E = (\<Prod>j\<in>J \<inter> {..<i}. emeasure M (E j))"
   839     using J by (intro emeasure_PiM_emb) simp_all
   840   also have "(\<Prod>j\<in>J \<inter> {..<i}. emeasure M (E j)) * (\<Prod>j\<in>J - (J \<inter> {..<i}). emeasure M (E j)) = (\<Prod>j\<in>J. emeasure M (E j))"
   841     by (subst mult_commute) (auto simp: J setprod_subset_diff[symmetric])
   842   finally show "emeasure ?D ?X = (\<Prod>j\<in>J. emeasure M (E j))" .
   843 qed simp_all
   844 
   845 lemma PiM_iter:
   846   "distr (M \<Otimes>\<^isub>M S) S (\<lambda>(s, \<omega>). nat_case s \<omega>) = S" (is "?D = _")
   847 proof (rule PiM_eq)
   848   let ?I = "UNIV::nat set" and ?M = "\<lambda>n. M"
   849   let "distr _ _ ?f" = "?D"
   850 
   851   fix J E assume J: "finite J" "J \<subseteq> ?I" "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets M"
   852   let ?X = "prod_emb ?I ?M J (PIE j:J. E j)"
   853   have "\<And>j x. j \<in> J \<Longrightarrow> x \<in> E j \<Longrightarrow> x \<in> space M"
   854     using J(3)[THEN sets_into_space] by (auto simp: space_PiM Pi_iff subset_eq)
   855   with J have "?f -` ?X \<inter> space (M \<Otimes>\<^isub>M S) = (if 0 \<in> J then E 0 else space M) \<times>
   856     (prod_emb ?I ?M (Suc -` J) (PIE j:Suc -` J. E (Suc j)))" (is "_ = ?E \<times> ?F")
   857    by (auto simp: space_pair_measure space_PiM Pi_iff prod_emb_def all_conj_distrib
   858       split: nat.split nat.split_asm)
   859   then have "emeasure ?D ?X = emeasure (M \<Otimes>\<^isub>M S) (?E \<times> ?F)"
   860     by (subst emeasure_distr[OF measurable_nat_case])
   861        (auto intro!: sets_PiM_I simp: split_beta' J)
   862   also have "\<dots> = emeasure M ?E * emeasure S ?F"
   863     using J by (intro P.emeasure_pair_measure_Times) (auto intro!: sets_PiM_I finite_vimageI)
   864   also have "emeasure S ?F = (\<Prod>j\<in>Suc -` J. emeasure M (E (Suc j)))"
   865     using J by (intro emeasure_PiM_emb) (simp_all add: finite_vimageI)
   866   also have "\<dots> = (\<Prod>j\<in>J - {0}. emeasure M (E j))"
   867     by (rule strong_setprod_reindex_cong[where f="\<lambda>x. x - 1"])
   868        (auto simp: image_iff Bex_def not_less nat_eq_diff_eq ac_simps cong: conj_cong intro!: inj_onI)
   869   also have "emeasure M ?E * (\<Prod>j\<in>J - {0}. emeasure M (E j)) = (\<Prod>j\<in>J. emeasure M (E j))"
   870     by (auto simp: M.emeasure_space_1 setprod.remove J)
   871   finally show "emeasure ?D ?X = (\<Prod>j\<in>J. emeasure M (E j))" .
   872 qed simp_all
   873 
   874 end
   875 
   876 end