src/HOL/Probability/Infinite_Product_Measure.thy
author wenzelm
Tue Mar 13 16:56:56 2012 +0100 (2012-03-13)
changeset 46905 6b1c0a80a57a
parent 46898 1570b30ee040
child 47694 05663f75964c
permissions -rw-r--r--
prefer abs_def over def_raw;
     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
     9 begin
    10 
    11 lemma restrict_extensional_sub[intro]: "A \<subseteq> B \<Longrightarrow> restrict f A \<in> extensional B"
    12   unfolding restrict_def extensional_def by auto
    13 
    14 lemma restrict_restrict[simp]: "restrict (restrict f A) B = restrict f (A \<inter> B)"
    15   unfolding restrict_def by (simp add: fun_eq_iff)
    16 
    17 lemma split_merge: "P (merge I x J 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)"
    18   unfolding merge_def by auto
    19 
    20 lemma extensional_merge_sub: "I \<union> J \<subseteq> K \<Longrightarrow> merge I x J y \<in> extensional K"
    21   unfolding merge_def extensional_def by auto
    22 
    23 lemma injective_vimage_restrict:
    24   assumes J: "J \<subseteq> I"
    25   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> {}"
    26   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)"
    27   shows "A = B"
    28 proof  (intro set_eqI)
    29   fix x
    30   from ne obtain y where y: "\<And>i. i \<in> I \<Longrightarrow> y i \<in> S i" by auto
    31   have "J \<inter> (I - J) = {}" by auto
    32   show "x \<in> A \<longleftrightarrow> x \<in> B"
    33   proof cases
    34     assume x: "x \<in> (\<Pi>\<^isub>E i\<in>J. S i)"
    35     have "x \<in> A \<longleftrightarrow> merge J x (I - J) y \<in> (\<lambda>x. restrict x J) -` A \<inter> (\<Pi>\<^isub>E i\<in>I. S i)"
    36       using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub split: split_merge)
    37     then show "x \<in> A \<longleftrightarrow> x \<in> B"
    38       using y x `J \<subseteq> I` by (auto simp add: Pi_iff extensional_restrict extensional_merge_sub eq split: split_merge)
    39   next
    40     assume "x \<notin> (\<Pi>\<^isub>E i\<in>J. S i)" with sets show "x \<in> A \<longleftrightarrow> x \<in> B" by auto
    41   qed
    42 qed
    43 
    44 lemma (in product_prob_space) measure_preserving_restrict:
    45   assumes "J \<noteq> {}" "J \<subseteq> K" "finite K"
    46   shows "(\<lambda>f. restrict f J) \<in> measure_preserving (\<Pi>\<^isub>M i\<in>K. M i) (\<Pi>\<^isub>M i\<in>J. M i)" (is "?R \<in> _")
    47 proof -
    48   interpret K: finite_product_prob_space M K by default fact
    49   have J: "J \<noteq> {}" "finite J" using assms by (auto simp add: finite_subset)
    50   interpret J: finite_product_prob_space M J
    51     by default (insert J, auto)
    52   from J.sigma_finite_pairs guess F .. note F = this
    53   then have [simp,intro]: "\<And>k i. k \<in> J \<Longrightarrow> F k i \<in> sets (M k)"
    54     by auto
    55   let ?F = "\<lambda>i. \<Pi>\<^isub>E k\<in>J. F k i"
    56   let ?J = "product_algebra_generator J M \<lparr> measure := measure (Pi\<^isub>M J M) \<rparr>"
    57   have "?R \<in> measure_preserving (\<Pi>\<^isub>M i\<in>K. M i) (sigma ?J)"
    58   proof (rule K.measure_preserving_Int_stable)
    59     show "Int_stable ?J"
    60       by (auto simp: Int_stable_def product_algebra_generator_def PiE_Int)
    61     show "range ?F \<subseteq> sets ?J" "incseq ?F" "(\<Union>i. ?F i) = space ?J"
    62       using F by auto
    63     show "\<And>i. measure ?J (?F i) \<noteq> \<infinity>"
    64       using F by (simp add: J.measure_times setprod_PInf)
    65     have "measure_space (Pi\<^isub>M J M)" by default
    66     then show "measure_space (sigma ?J)"
    67       by (simp add: product_algebra_def sigma_def)
    68     show "?R \<in> measure_preserving (Pi\<^isub>M K M) ?J"
    69     proof (simp add: measure_preserving_def measurable_def product_algebra_generator_def del: vimage_Int,
    70            safe intro!: restrict_extensional)
    71       fix x k assume "k \<in> J" "x \<in> (\<Pi> i\<in>K. space (M i))"
    72       then show "x k \<in> space (M k)" using `J \<subseteq> K` by auto
    73     next
    74       fix E assume "E \<in> (\<Pi> i\<in>J. sets (M i))"
    75       then have E: "\<And>j. j \<in> J \<Longrightarrow> E j \<in> sets (M j)" by auto
    76       then have *: "?R -` Pi\<^isub>E J E \<inter> (\<Pi>\<^isub>E i\<in>K. space (M i)) = (\<Pi>\<^isub>E i\<in>K. if i \<in> J then E i else space (M i))"
    77         (is "?X = Pi\<^isub>E K ?M")
    78         using `J \<subseteq> K` sets_into_space by (auto simp: Pi_iff split: split_if_asm) blast+
    79       with E show "?X \<in> sets (Pi\<^isub>M K M)"
    80         by (auto intro!: product_algebra_generatorI)
    81       have "measure (Pi\<^isub>M J M) (Pi\<^isub>E J E) = (\<Prod>i\<in>J. measure (M i) (?M i))"
    82         using E by (simp add: J.measure_times)
    83       also have "\<dots> = measure (Pi\<^isub>M K M) ?X"
    84         unfolding * using E `finite K` `J \<subseteq> K`
    85         by (auto simp: K.measure_times M.measure_space_1
    86                  cong del: setprod_cong
    87                  intro!: setprod_mono_one_left)
    88       finally show "measure (Pi\<^isub>M J M) (Pi\<^isub>E J E) = measure (Pi\<^isub>M K M) ?X" .
    89     qed
    90   qed
    91   then show ?thesis
    92     by (simp add: product_algebra_def sigma_def)
    93 qed
    94 
    95 lemma (in product_prob_space) measurable_restrict:
    96   assumes *: "J \<noteq> {}" "J \<subseteq> K" "finite K"
    97   shows "(\<lambda>f. restrict f J) \<in> measurable (\<Pi>\<^isub>M i\<in>K. M i) (\<Pi>\<^isub>M i\<in>J. M i)"
    98   using measure_preserving_restrict[OF *]
    99   by (rule measure_preservingD2)
   100 
   101 definition (in product_prob_space)
   102   "emb J K X = (\<lambda>x. restrict x K) -` X \<inter> space (Pi\<^isub>M J M)"
   103 
   104 lemma (in product_prob_space) emb_trans[simp]:
   105   "J \<subseteq> K \<Longrightarrow> K \<subseteq> L \<Longrightarrow> emb L K (emb K J X) = emb L J X"
   106   by (auto simp add: Int_absorb1 emb_def)
   107 
   108 lemma (in product_prob_space) emb_empty[simp]:
   109   "emb K J {} = {}"
   110   by (simp add: emb_def)
   111 
   112 lemma (in product_prob_space) emb_Pi:
   113   assumes "X \<in> (\<Pi> j\<in>J. sets (M j))" "J \<subseteq> K"
   114   shows "emb K J (Pi\<^isub>E J X) = (\<Pi>\<^isub>E i\<in>K. if i \<in> J then X i else space (M i))"
   115   using assms space_closed
   116   by (auto simp: emb_def Pi_iff split: split_if_asm) blast+
   117 
   118 lemma (in product_prob_space) emb_injective:
   119   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)"
   120   assumes "emb L J X = emb L J Y"
   121   shows "X = Y"
   122 proof -
   123   interpret J: finite_product_sigma_finite M J by default fact
   124   show "X = Y"
   125   proof (rule injective_vimage_restrict)
   126     show "X \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))" "Y \<subseteq> (\<Pi>\<^isub>E i\<in>J. space (M i))"
   127       using J.sets_into_space sets by auto
   128     have "\<forall>i\<in>L. \<exists>x. x \<in> space (M i)"
   129       using M.not_empty by auto
   130     from bchoice[OF this]
   131     show "(\<Pi>\<^isub>E i\<in>L. space (M i)) \<noteq> {}" by auto
   132     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))"
   133       using `emb L J X = emb L J Y` by (simp add: emb_def)
   134   qed fact
   135 qed
   136 
   137 lemma (in product_prob_space) emb_id:
   138   "B \<subseteq> (\<Pi>\<^isub>E i\<in>L. space (M i)) \<Longrightarrow> emb L L B = B"
   139   by (auto simp: emb_def Pi_iff subset_eq extensional_restrict)
   140 
   141 lemma (in product_prob_space) emb_simps:
   142   shows "emb L K (A \<union> B) = emb L K A \<union> emb L K B"
   143     and "emb L K (A \<inter> B) = emb L K A \<inter> emb L K B"
   144     and "emb L K (A - B) = emb L K A - emb L K B"
   145   by (auto simp: emb_def)
   146 
   147 lemma (in product_prob_space) measurable_emb[intro,simp]:
   148   assumes *: "J \<noteq> {}" "J \<subseteq> L" "finite L" "X \<in> sets (Pi\<^isub>M J M)"
   149   shows "emb L J X \<in> sets (Pi\<^isub>M L M)"
   150   using measurable_restrict[THEN measurable_sets, OF *] by (simp add: emb_def)
   151 
   152 lemma (in product_prob_space) measure_emb[intro,simp]:
   153   assumes *: "J \<noteq> {}" "J \<subseteq> L" "finite L" "X \<in> sets (Pi\<^isub>M J M)"
   154   shows "measure (Pi\<^isub>M L M) (emb L J X) = measure (Pi\<^isub>M J M) X"
   155   using measure_preserving_restrict[THEN measure_preservingD, OF *]
   156   by (simp add: emb_def)
   157 
   158 definition (in product_prob_space) generator :: "('i \<Rightarrow> 'a) measure_space" where
   159   "generator = \<lparr>
   160     space = (\<Pi>\<^isub>E i\<in>I. space (M i)),
   161     sets = (\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` sets (Pi\<^isub>M J M)),
   162     measure = undefined
   163   \<rparr>"
   164 
   165 lemma (in product_prob_space) generatorI:
   166   "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> sets generator"
   167   unfolding generator_def by auto
   168 
   169 lemma (in product_prob_space) generatorI':
   170   "J \<noteq> {} \<Longrightarrow> finite J \<Longrightarrow> J \<subseteq> I \<Longrightarrow> X \<in> sets (Pi\<^isub>M J M) \<Longrightarrow> emb I J X \<in> sets generator"
   171   unfolding generator_def by auto
   172 
   173 lemma (in product_sigma_finite)
   174   assumes "I \<inter> J = {}" "finite I" "finite J" and A: "A \<in> sets (Pi\<^isub>M (I \<union> J) M)"
   175   shows measure_fold_integral:
   176     "measure (Pi\<^isub>M (I \<union> J) M) A = (\<integral>\<^isup>+x. measure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M)) \<partial>Pi\<^isub>M I M)" (is ?I)
   177     and measure_fold_measurable:
   178     "(\<lambda>x. measure (Pi\<^isub>M J M) (merge I x J -` A \<inter> space (Pi\<^isub>M J M))) \<in> borel_measurable (Pi\<^isub>M I M)" (is ?B)
   179 proof -
   180   interpret I: finite_product_sigma_finite M I by default fact
   181   interpret J: finite_product_sigma_finite M J by default fact
   182   interpret IJ: pair_sigma_finite I.P J.P ..
   183   show ?I
   184     unfolding measure_fold[OF assms]
   185     apply (subst IJ.pair_measure_alt)
   186     apply (intro measurable_sets[OF _ A] measurable_merge assms)
   187     apply (auto simp: vimage_compose[symmetric] comp_def space_pair_measure
   188       intro!: I.positive_integral_cong)
   189     done
   190 
   191   have "(\<lambda>(x, y). merge I x J y) -` A \<inter> space (I.P \<Otimes>\<^isub>M J.P) \<in> sets (I.P \<Otimes>\<^isub>M J.P)"
   192     by (intro measurable_sets[OF _ A] measurable_merge assms)
   193   from IJ.measure_cut_measurable_fst[OF this]
   194   show ?B
   195     apply (auto simp: vimage_compose[symmetric] comp_def space_pair_measure)
   196     apply (subst (asm) measurable_cong)
   197     apply auto
   198     done
   199 qed
   200 
   201 definition (in product_prob_space)
   202   "\<mu>G A =
   203     (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 = measure (Pi\<^isub>M J M) X))"
   204 
   205 lemma (in product_prob_space) \<mu>G_spec:
   206   assumes J: "J \<noteq> {}" "finite J" "J \<subseteq> I" "A = emb I J X" "X \<in> sets (Pi\<^isub>M J M)"
   207   shows "\<mu>G A = measure (Pi\<^isub>M J M) X"
   208   unfolding \<mu>G_def
   209 proof (intro the_equality allI impI ballI)
   210   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)"
   211   have "measure (Pi\<^isub>M K M) Y = measure (Pi\<^isub>M (K \<union> J) M) (emb (K \<union> J) K Y)"
   212     using K J by simp
   213   also have "emb (K \<union> J) K Y = emb (K \<union> J) J X"
   214     using K J by (simp add: emb_injective[of "K \<union> J" I])
   215   also have "measure (Pi\<^isub>M (K \<union> J) M) (emb (K \<union> J) J X) = measure (Pi\<^isub>M J M) X"
   216     using K J by simp
   217   finally show "measure (Pi\<^isub>M J M) X = measure (Pi\<^isub>M K M) Y" ..
   218 qed (insert J, force)
   219 
   220 lemma (in product_prob_space) \<mu>G_eq:
   221   "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) = measure (Pi\<^isub>M J M) X"
   222   by (intro \<mu>G_spec) auto
   223 
   224 lemma (in product_prob_space) generator_Ex:
   225   assumes *: "A \<in> sets generator"
   226   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 = measure (Pi\<^isub>M J M) X"
   227 proof -
   228   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)"
   229     unfolding generator_def by auto
   230   with \<mu>G_spec[OF this] show ?thesis by auto
   231 qed
   232 
   233 lemma (in product_prob_space) generatorE:
   234   assumes A: "A \<in> sets generator"
   235   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 = measure (Pi\<^isub>M J M) X"
   236 proof -
   237   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"
   238     "\<mu>G A = measure (Pi\<^isub>M J M) X" by auto
   239   then show thesis by (intro that) auto
   240 qed
   241 
   242 lemma (in product_prob_space) merge_sets:
   243   assumes "finite J" "finite K" "J \<inter> K = {}" and A: "A \<in> sets (Pi\<^isub>M (J \<union> K) M)" and x: "x \<in> space (Pi\<^isub>M J M)"
   244   shows "merge J x K -` A \<inter> space (Pi\<^isub>M K M) \<in> sets (Pi\<^isub>M K M)"
   245 proof -
   246   interpret J: finite_product_sigma_algebra M J by default fact
   247   interpret K: finite_product_sigma_algebra M K by default fact
   248   interpret JK: pair_sigma_algebra J.P K.P ..
   249 
   250   from JK.measurable_cut_fst[OF
   251     measurable_merge[THEN measurable_sets, OF `J \<inter> K = {}`], OF A, of x] x
   252   show ?thesis
   253       by (simp add: space_pair_measure comp_def vimage_compose[symmetric])
   254 qed
   255 
   256 lemma (in product_prob_space) merge_emb:
   257   assumes "K \<subseteq> I" "J \<subseteq> I" and y: "y \<in> space (Pi\<^isub>M J M)"
   258   shows "(merge J y (I - J) -` emb I K X \<inter> space (Pi\<^isub>M I M)) =
   259     emb I (K - J) (merge J y (K - J) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M))"
   260 proof -
   261   have [simp]: "\<And>x J K L. merge J y K (restrict x L) = merge J y (K \<inter> L) x"
   262     by (auto simp: restrict_def merge_def)
   263   have [simp]: "\<And>x J K L. restrict (merge J y K x) L = merge (J \<inter> L) y (K \<inter> L) x"
   264     by (auto simp: restrict_def merge_def)
   265   have [simp]: "(I - J) \<inter> K = K - J" using `K \<subseteq> I` `J \<subseteq> I` by auto
   266   have [simp]: "(K - J) \<inter> (K \<union> J) = K - J" by auto
   267   have [simp]: "(K - J) \<inter> K = K - J" by auto
   268   from y `K \<subseteq> I` `J \<subseteq> I` show ?thesis
   269     by (simp split: split_merge add: emb_def Pi_iff extensional_merge_sub set_eq_iff) auto
   270 qed
   271 
   272 definition (in product_prob_space) infprod_algebra :: "('i \<Rightarrow> 'a) measure_space" where
   273   "infprod_algebra = sigma generator \<lparr> measure :=
   274     (SOME \<mu>. (\<forall>s\<in>sets generator. \<mu> s = \<mu>G s) \<and>
   275        prob_space \<lparr>space = space generator, sets = sets (sigma generator), measure = \<mu>\<rparr>)\<rparr>"
   276 
   277 syntax
   278   "_PiP"  :: "[pttrn, 'i set, ('b, 'd) measure_space_scheme] => ('i => 'b, 'd) measure_space_scheme"  ("(3PIP _:_./ _)" 10)
   279 
   280 syntax (xsymbols)
   281   "_PiP" :: "[pttrn, 'i set, ('b, 'd) measure_space_scheme] => ('i => 'b, 'd) measure_space_scheme"  ("(3\<Pi>\<^isub>P _\<in>_./ _)"   10)
   282 
   283 syntax (HTML output)
   284   "_PiP" :: "[pttrn, 'i set, ('b, 'd) measure_space_scheme] => ('i => 'b, 'd) measure_space_scheme"  ("(3\<Pi>\<^isub>P _\<in>_./ _)"   10)
   285 
   286 abbreviation
   287   "Pi\<^isub>P I M \<equiv> product_prob_space.infprod_algebra M I"
   288 
   289 translations
   290   "PIP x:I. M" == "CONST Pi\<^isub>P I (%x. M)"
   291 
   292 lemma (in product_prob_space) algebra_generator:
   293   assumes "I \<noteq> {}" shows "algebra generator"
   294 proof
   295   let ?G = generator
   296   show "sets ?G \<subseteq> Pow (space ?G)"
   297     by (auto simp: generator_def emb_def)
   298   from `I \<noteq> {}` obtain i where "i \<in> I" by auto
   299   then show "{} \<in> sets ?G"
   300     by (auto intro!: exI[of _ "{i}"] image_eqI[where x="\<lambda>i. {}"]
   301       simp: product_algebra_def sigma_def sigma_sets.Empty generator_def emb_def)
   302   from `i \<in> I` show "space ?G \<in> sets ?G"
   303     by (auto intro!: exI[of _ "{i}"] image_eqI[where x="Pi\<^isub>E {i} (\<lambda>i. space (M i))"]
   304       simp: generator_def emb_def)
   305   fix A assume "A \<in> sets ?G"
   306   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"
   307     by (auto simp: generator_def)
   308   fix B assume "B \<in> sets ?G"
   309   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"
   310     by (auto simp: generator_def)
   311   let ?RA = "emb (JA \<union> JB) JA XA"
   312   let ?RB = "emb (JA \<union> JB) JB XB"
   313   interpret JAB: finite_product_sigma_algebra M "JA \<union> JB"
   314     by default (insert XA XB, auto)
   315   have *: "A - B = emb I (JA \<union> JB) (?RA - ?RB)" "A \<union> B = emb I (JA \<union> JB) (?RA \<union> ?RB)"
   316     using XA A XB B by (auto simp: emb_simps)
   317   then show "A - B \<in> sets ?G" "A \<union> B \<in> sets ?G"
   318     using XA XB by (auto intro!: generatorI')
   319 qed
   320 
   321 lemma (in product_prob_space) positive_\<mu>G: 
   322   assumes "I \<noteq> {}"
   323   shows "positive generator \<mu>G"
   324 proof -
   325   interpret G!: algebra generator by (rule algebra_generator) fact
   326   show ?thesis
   327   proof (intro positive_def[THEN iffD2] conjI ballI)
   328     from generatorE[OF G.empty_sets] guess J X . note this[simp]
   329     interpret J: finite_product_sigma_finite M J by default fact
   330     have "X = {}"
   331       by (rule emb_injective[of J I]) simp_all
   332     then show "\<mu>G {} = 0" by simp
   333   next
   334     fix A assume "A \<in> sets generator"
   335     from generatorE[OF this] guess J X . note this[simp]
   336     interpret J: finite_product_sigma_finite M J by default fact
   337     show "0 \<le> \<mu>G A" by simp
   338   qed
   339 qed
   340 
   341 lemma (in product_prob_space) additive_\<mu>G: 
   342   assumes "I \<noteq> {}"
   343   shows "additive generator \<mu>G"
   344 proof -
   345   interpret G!: algebra generator by (rule algebra_generator) fact
   346   show ?thesis
   347   proof (intro additive_def[THEN iffD2] ballI impI)
   348     fix A assume "A \<in> sets generator" with generatorE guess J X . note J = this
   349     fix B assume "B \<in> sets generator" with generatorE guess K Y . note K = this
   350     assume "A \<inter> B = {}"
   351     have JK: "J \<union> K \<noteq> {}" "J \<union> K \<subseteq> I" "finite (J \<union> K)"
   352       using J K by auto
   353     interpret JK: finite_product_sigma_finite M "J \<union> K" by default fact
   354     have JK_disj: "emb (J \<union> K) J X \<inter> emb (J \<union> K) K Y = {}"
   355       apply (rule emb_injective[of "J \<union> K" I])
   356       apply (insert `A \<inter> B = {}` JK J K)
   357       apply (simp_all add: JK.Int emb_simps)
   358       done
   359     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)"
   360       using J K by simp_all
   361     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))"
   362       by (simp add: emb_simps)
   363     also have "\<dots> = measure (Pi\<^isub>M (J \<union> K) M) (emb (J \<union> K) J X \<union> emb (J \<union> K) K Y)"
   364       using JK J(1, 4) K(1, 4) by (simp add: \<mu>G_eq JK.Un)
   365     also have "\<dots> = \<mu>G A + \<mu>G B"
   366       using J K JK_disj by (simp add: JK.measure_additive[symmetric])
   367     finally show "\<mu>G (A \<union> B) = \<mu>G A + \<mu>G B" .
   368   qed
   369 qed
   370 
   371 lemma (in product_prob_space) finite_index_eq_finite_product:
   372   assumes "finite I"
   373   shows "sets (sigma generator) = sets (Pi\<^isub>M I M)"
   374 proof safe
   375   interpret I: finite_product_sigma_algebra M I by default fact
   376   have space_generator[simp]: "space generator = space (Pi\<^isub>M I M)"
   377     by (simp add: generator_def product_algebra_def)
   378   { fix A assume "A \<in> sets (sigma generator)"
   379     then show "A \<in> sets I.P" unfolding sets_sigma
   380     proof induct
   381       case (Basic A)
   382       from generatorE[OF this] guess J X . note J = this
   383       with `finite I` have "emb I J X \<in> sets I.P" by auto
   384       with `emb I J X = A` show "A \<in> sets I.P" by simp
   385     qed auto }
   386   { fix A assume A: "A \<in> sets I.P"
   387     show "A \<in> sets (sigma generator)"
   388     proof cases
   389       assume "I = {}"
   390       with I.P_empty[OF this] A
   391       have "A = space generator \<or> A = {}" 
   392         unfolding space_generator by auto
   393       then show ?thesis
   394         by (auto simp: sets_sigma simp del: space_generator
   395                  intro: sigma_sets.Empty sigma_sets_top)
   396     next
   397       assume "I \<noteq> {}"
   398       note A this
   399       moreover with I.sets_into_space have "emb I I A = A" by (intro emb_id) auto
   400       ultimately show "A \<in> sets (sigma generator)"
   401         using `finite I` unfolding sets_sigma
   402         by (intro sigma_sets.Basic generatorI[of I A]) auto
   403   qed }
   404 qed
   405 
   406 lemma (in product_prob_space) extend_\<mu>G:
   407   "\<exists>\<mu>. (\<forall>s\<in>sets generator. \<mu> s = \<mu>G s) \<and>
   408        prob_space \<lparr>space = space generator, sets = sets (sigma generator), measure = \<mu>\<rparr>"
   409 proof cases
   410   assume "finite I"
   411   interpret I: finite_product_prob_space M I by default fact
   412   show ?thesis
   413   proof (intro exI[of _ "measure (Pi\<^isub>M I M)"] ballI conjI)
   414     fix A assume "A \<in> sets generator"
   415     from generatorE[OF this] guess J X . note J = this
   416     from J(1-4) `finite I` show "measure I.P A = \<mu>G A"
   417       unfolding J(6)
   418       by (subst J(5)[symmetric]) (simp add: measure_emb)
   419   next
   420     have [simp]: "space generator = space (Pi\<^isub>M I M)"
   421       by (simp add: generator_def product_algebra_def)
   422     have "\<lparr>space = space generator, sets = sets (sigma generator), measure = measure I.P\<rparr>
   423       = I.P" (is "?P = _")
   424       by (auto intro!: measure_space.equality simp: finite_index_eq_finite_product[OF `finite I`])
   425     show "prob_space ?P"
   426     proof
   427       show "measure_space ?P" using `?P = I.P` by simp default
   428       show "measure ?P (space ?P) = 1"
   429         using I.measure_space_1 by simp
   430     qed
   431   qed
   432 next
   433   let ?G = generator
   434   assume "\<not> finite I"
   435   then have I_not_empty: "I \<noteq> {}" by auto
   436   interpret G!: algebra generator by (rule algebra_generator) fact
   437   note \<mu>G_mono =
   438     G.additive_increasing[OF positive_\<mu>G[OF I_not_empty] additive_\<mu>G[OF I_not_empty], THEN increasingD]
   439 
   440   { fix Z J assume J: "J \<noteq> {}" "finite J" "J \<subseteq> I" and Z: "Z \<in> sets ?G"
   441 
   442     from `infinite I` `finite J` obtain k where k: "k \<in> I" "k \<notin> J"
   443       by (metis rev_finite_subset subsetI)
   444     moreover from Z guess K' X' by (rule generatorE)
   445     moreover def K \<equiv> "insert k K'"
   446     moreover def X \<equiv> "emb K K' X'"
   447     ultimately have K: "K \<noteq> {}" "finite K" "K \<subseteq> I" "X \<in> sets (Pi\<^isub>M K M)" "Z = emb I K X"
   448       "K - J \<noteq> {}" "K - J \<subseteq> I" "\<mu>G Z = measure (Pi\<^isub>M K M) X"
   449       by (auto simp: subset_insertI)
   450 
   451     let ?M = "\<lambda>y. merge J y (K - J) -` emb (J \<union> K) K X \<inter> space (Pi\<^isub>M (K - J) M)"
   452     { fix y assume y: "y \<in> space (Pi\<^isub>M J M)"
   453       note * = merge_emb[OF `K \<subseteq> I` `J \<subseteq> I` y, of X]
   454       moreover
   455       have **: "?M y \<in> sets (Pi\<^isub>M (K - J) M)"
   456         using J K y by (intro merge_sets) auto
   457       ultimately
   458       have ***: "(merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<in> sets ?G"
   459         using J K by (intro generatorI) auto
   460       have "\<mu>G (merge J y (I - J) -` emb I K X \<inter> space (Pi\<^isub>M I M)) = measure (Pi\<^isub>M (K - J) M) (?M y)"
   461         unfolding * using K J by (subst \<mu>G_eq[OF _ _ _ **]) auto
   462       note * ** *** this }
   463     note merge_in_G = this
   464 
   465     have "finite (K - J)" using K by auto
   466 
   467     interpret J: finite_product_prob_space M J by default fact+
   468     interpret KmJ: finite_product_prob_space M "K - J" by default fact+
   469 
   470     have "\<mu>G Z = measure (Pi\<^isub>M (J \<union> (K - J)) M) (emb (J \<union> (K - J)) K X)"
   471       using K J by simp
   472     also have "\<dots> = (\<integral>\<^isup>+ x. measure (Pi\<^isub>M (K - J) M) (?M x) \<partial>Pi\<^isub>M J M)"
   473       using K J by (subst measure_fold_integral) auto
   474     also have "\<dots> = (\<integral>\<^isup>+ y. \<mu>G (merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M)) \<partial>Pi\<^isub>M J M)"
   475       (is "_ = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)")
   476     proof (intro J.positive_integral_cong)
   477       fix x assume x: "x \<in> space (Pi\<^isub>M J M)"
   478       with K merge_in_G(2)[OF this]
   479       show "measure (Pi\<^isub>M (K - J) M) (?M x) = \<mu>G (?MZ x)"
   480         unfolding `Z = emb I K X` merge_in_G(1)[OF x] by (subst \<mu>G_eq) auto
   481     qed
   482     finally have fold: "\<mu>G Z = (\<integral>\<^isup>+x. \<mu>G (?MZ x) \<partial>Pi\<^isub>M J M)" .
   483 
   484     { fix x assume x: "x \<in> space (Pi\<^isub>M J M)"
   485       then have "\<mu>G (?MZ x) \<le> 1"
   486         unfolding merge_in_G(4)[OF x] `Z = emb I K X`
   487         by (intro KmJ.measure_le_1 merge_in_G(2)[OF x]) }
   488     note le_1 = this
   489 
   490     let ?q = "\<lambda>y. \<mu>G (merge J y (I - J) -` Z \<inter> space (Pi\<^isub>M I M))"
   491     have "?q \<in> borel_measurable (Pi\<^isub>M J M)"
   492       unfolding `Z = emb I K X` using J K merge_in_G(3)
   493       by (simp add: merge_in_G  \<mu>G_eq measure_fold_measurable
   494                del: space_product_algebra cong: measurable_cong)
   495     note this fold le_1 merge_in_G(3) }
   496   note fold = this
   497 
   498   have "\<exists>\<mu>. (\<forall>s\<in>sets ?G. \<mu> s = \<mu>G s) \<and>
   499     measure_space \<lparr>space = space ?G, sets = sets (sigma ?G), measure = \<mu>\<rparr>"
   500     (is "\<exists>\<mu>. _ \<and> measure_space (?ms \<mu>)")
   501   proof (rule G.caratheodory_empty_continuous[OF positive_\<mu>G additive_\<mu>G])
   502     fix A assume "A \<in> sets ?G"
   503     with generatorE guess J X . note JX = this
   504     interpret JK: finite_product_prob_space M J by default fact+
   505     from JX show "\<mu>G A \<noteq> \<infinity>" by simp
   506   next
   507     fix A assume A: "range A \<subseteq> sets ?G" "decseq A" "(\<Inter>i. A i) = {}"
   508     then have "decseq (\<lambda>i. \<mu>G (A i))"
   509       by (auto intro!: \<mu>G_mono simp: decseq_def)
   510     moreover
   511     have "(INF i. \<mu>G (A i)) = 0"
   512     proof (rule ccontr)
   513       assume "(INF i. \<mu>G (A i)) \<noteq> 0" (is "?a \<noteq> 0")
   514       moreover have "0 \<le> ?a"
   515         using A positive_\<mu>G[OF I_not_empty] by (auto intro!: INF_greatest simp: positive_def)
   516       ultimately have "0 < ?a" by auto
   517 
   518       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) = measure (Pi\<^isub>M J M) X"
   519         using A by (intro allI generator_Ex) auto
   520       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)"
   521         and A': "\<And>n. A n = emb I (J' n) (X' n)"
   522         unfolding choice_iff by blast
   523       moreover def J \<equiv> "\<lambda>n. (\<Union>i\<le>n. J' i)"
   524       moreover def X \<equiv> "\<lambda>n. emb (J n) (J' n) (X' n)"
   525       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)"
   526         by auto
   527       with A' have A_eq: "\<And>n. A n = emb I (J n) (X n)" "\<And>n. A n \<in> sets ?G"
   528         unfolding J_def X_def by (subst emb_trans) (insert A, auto)
   529 
   530       have J_mono: "\<And>n m. n \<le> m \<Longrightarrow> J n \<subseteq> J m"
   531         unfolding J_def by force
   532 
   533       interpret J: finite_product_prob_space M "J i" for i by default fact+
   534 
   535       have a_le_1: "?a \<le> 1"
   536         using \<mu>G_spec[of "J 0" "A 0" "X 0"] J A_eq
   537         by (auto intro!: INF_lower2[of 0] J.measure_le_1)
   538 
   539       let ?M = "\<lambda>K Z y. merge K y (I - K) -` Z \<inter> space (Pi\<^isub>M I M)"
   540 
   541       { fix Z k assume Z: "range Z \<subseteq> sets ?G" "decseq Z" "\<forall>n. ?a / 2^k \<le> \<mu>G (Z n)"
   542         then have Z_sets: "\<And>n. Z n \<in> sets ?G" by auto
   543         fix J' assume J': "J' \<noteq> {}" "finite J'" "J' \<subseteq> I"
   544         interpret J': finite_product_prob_space M J' by default fact+
   545 
   546         let ?q = "\<lambda>n y. \<mu>G (?M J' (Z n) y)"
   547         let ?Q = "\<lambda>n. ?q n -` {?a / 2^(k+1) ..} \<inter> space (Pi\<^isub>M J' M)"
   548         { fix n
   549           have "?q n \<in> borel_measurable (Pi\<^isub>M J' M)"
   550             using Z J' by (intro fold(1)) auto
   551           then have "?Q n \<in> sets (Pi\<^isub>M J' M)"
   552             by (rule measurable_sets) auto }
   553         note Q_sets = this
   554 
   555         have "?a / 2^(k+1) \<le> (INF n. measure (Pi\<^isub>M J' M) (?Q n))"
   556         proof (intro INF_greatest)
   557           fix n
   558           have "?a / 2^k \<le> \<mu>G (Z n)" using Z by auto
   559           also have "\<dots> \<le> (\<integral>\<^isup>+ x. indicator (?Q n) x + ?a / 2^(k+1) \<partial>Pi\<^isub>M J' M)"
   560             unfolding fold(2)[OF J' `Z n \<in> sets ?G`]
   561           proof (intro J'.positive_integral_mono)
   562             fix x assume x: "x \<in> space (Pi\<^isub>M J' M)"
   563             then have "?q n x \<le> 1 + 0"
   564               using J' Z fold(3) Z_sets by auto
   565             also have "\<dots> \<le> 1 + ?a / 2^(k+1)"
   566               using `0 < ?a` by (intro add_mono) auto
   567             finally have "?q n x \<le> 1 + ?a / 2^(k+1)" .
   568             with x show "?q n x \<le> indicator (?Q n) x + ?a / 2^(k+1)"
   569               by (auto split: split_indicator simp del: power_Suc)
   570           qed
   571           also have "\<dots> = measure (Pi\<^isub>M J' M) (?Q n) + ?a / 2^(k+1)"
   572             using `0 \<le> ?a` Q_sets J'.measure_space_1
   573             by (subst J'.positive_integral_add) auto
   574           finally show "?a / 2^(k+1) \<le> measure (Pi\<^isub>M J' M) (?Q n)" using `?a \<le> 1`
   575             by (cases rule: ereal2_cases[of ?a "measure (Pi\<^isub>M J' M) (?Q n)"])
   576                (auto simp: field_simps)
   577         qed
   578         also have "\<dots> = measure (Pi\<^isub>M J' M) (\<Inter>n. ?Q n)"
   579         proof (intro J'.continuity_from_above)
   580           show "range ?Q \<subseteq> sets (Pi\<^isub>M J' M)" using Q_sets by auto
   581           show "decseq ?Q"
   582             unfolding decseq_def
   583           proof (safe intro!: vimageI[OF refl])
   584             fix m n :: nat assume "m \<le> n"
   585             fix x assume x: "x \<in> space (Pi\<^isub>M J' M)"
   586             assume "?a / 2^(k+1) \<le> ?q n x"
   587             also have "?q n x \<le> ?q m x"
   588             proof (rule \<mu>G_mono)
   589               from fold(4)[OF J', OF Z_sets x]
   590               show "?M J' (Z n) x \<in> sets ?G" "?M J' (Z m) x \<in> sets ?G" by auto
   591               show "?M J' (Z n) x \<subseteq> ?M J' (Z m) x"
   592                 using `decseq Z`[THEN decseqD, OF `m \<le> n`] by auto
   593             qed
   594             finally show "?a / 2^(k+1) \<le> ?q m x" .
   595           qed
   596         qed (intro J'.finite_measure Q_sets)
   597         finally have "(\<Inter>n. ?Q n) \<noteq> {}"
   598           using `0 < ?a` `?a \<le> 1` by (cases ?a) (auto simp: divide_le_0_iff power_le_zero_eq)
   599         then have "\<exists>w\<in>space (Pi\<^isub>M J' M). \<forall>n. ?a / 2 ^ (k + 1) \<le> ?q n w" by auto }
   600       note Ex_w = this
   601 
   602       let ?q = "\<lambda>k n y. \<mu>G (?M (J k) (A n) y)"
   603 
   604       have "\<forall>n. ?a / 2 ^ 0 \<le> \<mu>G (A n)" by (auto intro: INF_lower)
   605       from Ex_w[OF A(1,2) this J(1-3), of 0] guess w0 .. note w0 = this
   606 
   607       let ?P =
   608         "\<lambda>k wk w. w \<in> space (Pi\<^isub>M (J (Suc k)) M) \<and> restrict w (J k) = wk \<and>
   609           (\<forall>n. ?a / 2 ^ (Suc k + 1) \<le> ?q (Suc k) n w)"
   610       def w \<equiv> "nat_rec w0 (\<lambda>k wk. Eps (?P k wk))"
   611 
   612       { fix k have w: "w k \<in> space (Pi\<^isub>M (J k) M) \<and>
   613           (\<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))"
   614         proof (induct k)
   615           case 0 with w0 show ?case
   616             unfolding w_def nat_rec_0 by auto
   617         next
   618           case (Suc k)
   619           then have wk: "w k \<in> space (Pi\<^isub>M (J k) M)" by auto
   620           have "\<exists>w'. ?P k (w k) w'"
   621           proof cases
   622             assume [simp]: "J k = J (Suc k)"
   623             show ?thesis
   624             proof (intro exI[of _ "w k"] conjI allI)
   625               fix n
   626               have "?a / 2 ^ (Suc k + 1) \<le> ?a / 2 ^ (k + 1)"
   627                 using `0 < ?a` `?a \<le> 1` by (cases ?a) (auto simp: field_simps)
   628               also have "\<dots> \<le> ?q k n (w k)" using Suc by auto
   629               finally show "?a / 2 ^ (Suc k + 1) \<le> ?q (Suc k) n (w k)" by simp
   630             next
   631               show "w k \<in> space (Pi\<^isub>M (J (Suc k)) M)"
   632                 using Suc by simp
   633               then show "restrict (w k) (J k) = w k"
   634                 by (simp add: extensional_restrict)
   635             qed
   636           next
   637             assume "J k \<noteq> J (Suc k)"
   638             with J_mono[of k "Suc k"] have "J (Suc k) - J k \<noteq> {}" (is "?D \<noteq> {}") by auto
   639             have "range (\<lambda>n. ?M (J k) (A n) (w k)) \<subseteq> sets ?G"
   640               "decseq (\<lambda>n. ?M (J k) (A n) (w k))"
   641               "\<forall>n. ?a / 2 ^ (k + 1) \<le> \<mu>G (?M (J k) (A n) (w k))"
   642               using `decseq A` fold(4)[OF J(1-3) A_eq(2), of "w k" k] Suc
   643               by (auto simp: decseq_def)
   644             from Ex_w[OF this `?D \<noteq> {}`] J[of "Suc k"]
   645             obtain w' where w': "w' \<in> space (Pi\<^isub>M ?D M)"
   646               "\<forall>n. ?a / 2 ^ (Suc k + 1) \<le> \<mu>G (?M ?D (?M (J k) (A n) (w k)) w')" by auto
   647             let ?w = "merge (J k) (w k) ?D w'"
   648             have [simp]: "\<And>x. merge (J k) (w k) (I - J k) (merge ?D w' (I - ?D) x) =
   649               merge (J (Suc k)) ?w (I - (J (Suc k))) x"
   650               using J(3)[of "Suc k"] J(3)[of k] J_mono[of k "Suc k"]
   651               by (auto intro!: ext split: split_merge)
   652             have *: "\<And>n. ?M ?D (?M (J k) (A n) (w k)) w' = ?M (J (Suc k)) (A n) ?w"
   653               using w'(1) J(3)[of "Suc k"]
   654               by (auto split: split_merge intro!: extensional_merge_sub) force+
   655             show ?thesis
   656               apply (rule exI[of _ ?w])
   657               using w' J_mono[of k "Suc k"] wk unfolding *
   658               apply (auto split: split_merge intro!: extensional_merge_sub ext)
   659               apply (force simp: extensional_def)
   660               done
   661           qed
   662           then have "?P k (w k) (w (Suc k))"
   663             unfolding w_def nat_rec_Suc unfolding w_def[symmetric]
   664             by (rule someI_ex)
   665           then show ?case by auto
   666         qed
   667         moreover
   668         then have "w k \<in> space (Pi\<^isub>M (J k) M)" by auto
   669         moreover
   670         from w have "?a / 2 ^ (k + 1) \<le> ?q k k (w k)" by auto
   671         then have "?M (J k) (A k) (w k) \<noteq> {}"
   672           using positive_\<mu>G[OF I_not_empty, unfolded positive_def] `0 < ?a` `?a \<le> 1`
   673           by (cases ?a) (auto simp: divide_le_0_iff power_le_zero_eq)
   674         then obtain x where "x \<in> ?M (J k) (A k) (w k)" by auto
   675         then have "merge (J k) (w k) (I - J k) x \<in> A k" by auto
   676         then have "\<exists>x\<in>A k. restrict x (J k) = w k"
   677           using `w k \<in> space (Pi\<^isub>M (J k) M)`
   678           by (intro rev_bexI) (auto intro!: ext simp: extensional_def)
   679         ultimately have "w k \<in> space (Pi\<^isub>M (J k) M)"
   680           "\<exists>x\<in>A k. restrict x (J k) = w k"
   681           "k \<noteq> 0 \<Longrightarrow> restrict (w k) (J (k - 1)) = w (k - 1)"
   682           by auto }
   683       note w = this
   684 
   685       { fix k l i assume "k \<le> l" "i \<in> J k"
   686         { fix l have "w k i = w (k + l) i"
   687           proof (induct l)
   688             case (Suc l)
   689             from `i \<in> J k` J_mono[of k "k + l"] have "i \<in> J (k + l)" by auto
   690             with w(3)[of "k + Suc l"]
   691             have "w (k + l) i = w (k + Suc l) i"
   692               by (auto simp: restrict_def fun_eq_iff split: split_if_asm)
   693             with Suc show ?case by simp
   694           qed simp }
   695         from this[of "l - k"] `k \<le> l` have "w l i = w k i" by simp }
   696       note w_mono = this
   697 
   698       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"
   699       { fix i k assume k: "i \<in> J k"
   700         have "w k i = w (LEAST k. i \<in> J k) i"
   701           by (intro w_mono Least_le k LeastI[of _ k])
   702         then have "w' i = w k i"
   703           unfolding w'_def using k by auto }
   704       note w'_eq = this
   705       have w'_simps1: "\<And>i. i \<notin> I \<Longrightarrow> w' i = undefined"
   706         using J by (auto simp: w'_def)
   707       have w'_simps2: "\<And>i. i \<notin> (\<Union>k. J k) \<Longrightarrow> i \<in> I \<Longrightarrow> w' i \<in> space (M i)"
   708         using J by (auto simp: w'_def intro!: someI_ex[OF M.not_empty[unfolded ex_in_conv[symmetric]]])
   709       { fix i assume "i \<in> I" then have "w' i \<in> space (M i)"
   710           using w(1) by (cases "i \<in> (\<Union>k. J k)") (force simp: w'_simps2 w'_eq)+ }
   711       note w'_simps[simp] = w'_eq w'_simps1 w'_simps2 this
   712 
   713       have w': "w' \<in> space (Pi\<^isub>M I M)"
   714         using w(1) by (auto simp add: Pi_iff extensional_def)
   715 
   716       { fix n
   717         have "restrict w' (J n) = w n" using w(1)
   718           by (auto simp add: fun_eq_iff restrict_def Pi_iff extensional_def)
   719         with w[of n] obtain x where "x \<in> A n" "restrict x (J n) = restrict w' (J n)" by auto
   720         then have "w' \<in> A n" unfolding A_eq using w' by (auto simp: emb_def) }
   721       then have "w' \<in> (\<Inter>i. A i)" by auto
   722       with `(\<Inter>i. A i) = {}` show False by auto
   723     qed
   724     ultimately show "(\<lambda>i. \<mu>G (A i)) ----> 0"
   725       using LIMSEQ_ereal_INFI[of "\<lambda>i. \<mu>G (A i)"] by simp
   726   qed fact+
   727   then guess \<mu> .. note \<mu> = this
   728   show ?thesis
   729   proof (intro exI[of _ \<mu>] conjI)
   730     show "\<forall>S\<in>sets ?G. \<mu> S = \<mu>G S" using \<mu> by simp
   731     show "prob_space (?ms \<mu>)"
   732     proof
   733       show "measure_space (?ms \<mu>)" using \<mu> by simp
   734       obtain i where "i \<in> I" using I_not_empty by auto
   735       interpret i: finite_product_sigma_finite M "{i}" by default auto
   736       let ?X = "\<Pi>\<^isub>E i\<in>{i}. space (M i)"
   737       have X: "?X \<in> sets (Pi\<^isub>M {i} M)"
   738         by auto
   739       with `i \<in> I` have "emb I {i} ?X \<in> sets generator"
   740         by (intro generatorI') auto
   741       with \<mu> have "\<mu> (emb I {i} ?X) = \<mu>G (emb I {i} ?X)" by auto
   742       with \<mu>G_eq[OF _ _ _ X] `i \<in> I` 
   743       have "\<mu> (emb I {i} ?X) = measure (M i) (space (M i))"
   744         by (simp add: i.measure_times)
   745       also have "emb I {i} ?X = space (Pi\<^isub>P I M)"
   746         using `i \<in> I` by (auto simp: emb_def infprod_algebra_def generator_def)
   747       finally show "measure (?ms \<mu>) (space (?ms \<mu>)) = 1"
   748         using M.measure_space_1 by (simp add: infprod_algebra_def)
   749     qed
   750   qed
   751 qed
   752 
   753 lemma (in product_prob_space) infprod_spec:
   754   "(\<forall>s\<in>sets generator. measure (Pi\<^isub>P I M) s = \<mu>G s) \<and> prob_space (Pi\<^isub>P I M)"
   755   (is "?Q infprod_algebra")
   756   unfolding infprod_algebra_def
   757   by (rule someI2_ex[OF extend_\<mu>G])
   758      (auto simp: sigma_def generator_def)
   759 
   760 sublocale product_prob_space \<subseteq> P: prob_space "Pi\<^isub>P I M"
   761   using infprod_spec by simp
   762 
   763 lemma (in product_prob_space) measure_infprod_emb:
   764   assumes "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)"
   765   shows "\<mu> (emb I J X) = measure (Pi\<^isub>M J M) X"
   766 proof -
   767   have "emb I J X \<in> sets generator"
   768     using assms by (rule generatorI')
   769   with \<mu>G_eq[OF assms] infprod_spec show ?thesis by auto
   770 qed
   771 
   772 lemma (in product_prob_space) measurable_component:
   773   assumes "i \<in> I"
   774   shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>P I M) (M i)"
   775 proof (unfold measurable_def, safe)
   776   fix x assume "x \<in> space (Pi\<^isub>P I M)"
   777   then show "x i \<in> space (M i)"
   778     using `i \<in> I` by (auto simp: infprod_algebra_def generator_def)
   779 next
   780   fix A assume "A \<in> sets (M i)"
   781   with `i \<in> I` have
   782     "(\<Pi>\<^isub>E x \<in> {i}. A) \<in> sets (Pi\<^isub>M {i} M)"
   783     "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M) = emb I {i} (\<Pi>\<^isub>E x \<in> {i}. A)"
   784     by (auto simp: infprod_algebra_def generator_def emb_def)
   785   from generatorI[OF _ _ _ this] `i \<in> I`
   786   show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M) \<in> sets (Pi\<^isub>P I M)"
   787     unfolding infprod_algebra_def by auto
   788 qed
   789 
   790 lemma (in product_prob_space) emb_in_infprod_algebra[intro]:
   791   fixes J assumes J: "finite J" "J \<subseteq> I" and X: "X \<in> sets (Pi\<^isub>M J M)"
   792   shows "emb I J X \<in> sets (\<Pi>\<^isub>P i\<in>I. M i)"
   793 proof cases
   794   assume "J = {}"
   795   with X have "emb I J X = space (\<Pi>\<^isub>P i\<in>I. M i) \<or> emb I J X = {}"
   796     by (auto simp: emb_def infprod_algebra_def generator_def
   797                    product_algebra_def product_algebra_generator_def image_constant sigma_def)
   798   then show ?thesis by auto
   799 next
   800   assume "J \<noteq> {}"
   801   show ?thesis unfolding infprod_algebra_def
   802     by simp (intro in_sigma generatorI'  `J \<noteq> {}` J X)
   803 qed
   804 
   805 lemma (in product_prob_space) finite_measure_infprod_emb:
   806   assumes "J \<noteq> {}" "finite J" "J \<subseteq> I" "X \<in> sets (Pi\<^isub>M J M)"
   807   shows "\<mu>' (emb I J X) = finite_measure.\<mu>' (Pi\<^isub>M J M) X"
   808 proof -
   809   interpret J: finite_product_prob_space M J by default fact+
   810   from assms have "emb I J X \<in> sets (Pi\<^isub>P I M)" by auto
   811   with assms show "\<mu>' (emb I J X) = J.\<mu>' X"
   812     unfolding \<mu>'_def J.\<mu>'_def
   813     unfolding measure_infprod_emb[OF assms]
   814     by auto
   815 qed
   816 
   817 lemma (in finite_product_prob_space) finite_measure_times:
   818   assumes "\<And>i. i \<in> I \<Longrightarrow> A i \<in> sets (M i)"
   819   shows "\<mu>' (Pi\<^isub>E I A) = (\<Prod>i\<in>I. M.\<mu>' i (A i))"
   820   using assms
   821   unfolding \<mu>'_def M.\<mu>'_def
   822   by (subst measure_times[OF assms])
   823      (auto simp: finite_measure_eq M.finite_measure_eq setprod_ereal)
   824 
   825 lemma (in product_prob_space) finite_measure_infprod_emb_Pi:
   826   assumes J: "finite J" "J \<subseteq> I" "\<And>j. j \<in> J \<Longrightarrow> X j \<in> sets (M j)"
   827   shows "\<mu>' (emb I J (Pi\<^isub>E J X)) = (\<Prod>j\<in>J. M.\<mu>' j (X j))"
   828 proof cases
   829   assume "J = {}"
   830   then have "emb I J (Pi\<^isub>E J X) = space infprod_algebra"
   831     by (auto simp: infprod_algebra_def generator_def sigma_def emb_def)
   832   then show ?thesis using `J = {}` P.prob_space
   833     by simp
   834 next
   835   assume "J \<noteq> {}"
   836   interpret J: finite_product_prob_space M J by default fact+
   837   have "(\<Prod>i\<in>J. M.\<mu>' i (X i)) = J.\<mu>' (Pi\<^isub>E J X)"
   838     using J `J \<noteq> {}` by (subst J.finite_measure_times) auto
   839   also have "\<dots> = \<mu>' (emb I J (Pi\<^isub>E J X))"
   840     using J `J \<noteq> {}` by (intro finite_measure_infprod_emb[symmetric]) auto
   841   finally show ?thesis by simp
   842 qed
   843 
   844 lemma sigma_sets_mono: assumes "A \<subseteq> sigma_sets X B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
   845 proof
   846   fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
   847     by induct (insert `A \<subseteq> sigma_sets X B`, auto intro: sigma_sets.intros)
   848 qed
   849 
   850 lemma sigma_sets_mono': assumes "A \<subseteq> B" shows "sigma_sets X A \<subseteq> sigma_sets X B"
   851 proof
   852   fix x assume "x \<in> sigma_sets X A" then show "x \<in> sigma_sets X B"
   853     by induct (insert `A \<subseteq> B`, auto intro: sigma_sets.intros)
   854 qed
   855 
   856 lemma sigma_sets_superset_generator: "A \<subseteq> sigma_sets X A"
   857   by (auto intro: sigma_sets.Basic)
   858 
   859 lemma (in product_prob_space) infprod_algebra_alt:
   860   "Pi\<^isub>P I M = sigma \<lparr> space = space (Pi\<^isub>P I M),
   861     sets = (\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i))),
   862     measure = measure (Pi\<^isub>P I M) \<rparr>"
   863   (is "_ = sigma \<lparr> space = ?O, sets = ?M, measure = ?m \<rparr>")
   864 proof (rule measure_space.equality)
   865   let ?G = "\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` sets (Pi\<^isub>M J M)"
   866   have "sigma_sets ?O ?M = sigma_sets ?O ?G"
   867   proof (intro equalityI sigma_sets_mono UN_least)
   868     fix J assume J: "J \<in> {J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}"
   869     have "emb I J ` Pi\<^isub>E J ` (\<Pi> i\<in>J. sets (M i)) \<subseteq> emb I J ` sets (Pi\<^isub>M J M)" by auto
   870     also have "\<dots> \<subseteq> ?G" using J by (rule UN_upper)
   871     also have "\<dots> \<subseteq> sigma_sets ?O ?G" by (rule sigma_sets_superset_generator)
   872     finally show "emb I J ` Pi\<^isub>E J ` (\<Pi> i\<in>J. sets (M i)) \<subseteq> sigma_sets ?O ?G" .
   873     have "emb I J ` sets (Pi\<^isub>M J M) = emb I J ` sigma_sets (space (Pi\<^isub>M J M)) (Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i)))"
   874       by (simp add: sets_sigma product_algebra_generator_def product_algebra_def)
   875     also have "\<dots> = sigma_sets (space (Pi\<^isub>M I M)) (emb I J ` Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i)))"
   876       using J M.sets_into_space
   877       by (auto simp: emb_def [abs_def] intro!: sigma_sets_vimage[symmetric]) blast
   878     also have "\<dots> \<subseteq> sigma_sets (space (Pi\<^isub>M I M)) ?M"
   879       using J by (intro sigma_sets_mono') auto
   880     finally show "emb I J ` sets (Pi\<^isub>M J M) \<subseteq> sigma_sets ?O ?M"
   881       by (simp add: infprod_algebra_def generator_def)
   882   qed
   883   then show "sets (Pi\<^isub>P I M) = sets (sigma \<lparr> space = ?O, sets = ?M, measure = ?m \<rparr>)"
   884     by (simp_all add: infprod_algebra_def generator_def sets_sigma)
   885 qed simp_all
   886 
   887 lemma (in product_prob_space) infprod_algebra_alt2:
   888   "Pi\<^isub>P I M = sigma \<lparr> space = space (Pi\<^isub>P I M),
   889     sets = (\<Union>i\<in>I. (\<lambda>A. (\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M)) ` sets (M i)),
   890     measure = measure (Pi\<^isub>P I M) \<rparr>"
   891   (is "_ = ?S")
   892 proof (rule measure_space.equality)
   893   let "sigma \<lparr> space = ?O, sets = ?A, \<dots> = _ \<rparr>" = ?S
   894   let ?G = "(\<Union>J\<in>{J. J \<noteq> {} \<and> finite J \<and> J \<subseteq> I}. emb I J ` Pi\<^isub>E J ` (\<Pi> i \<in> J. sets (M i)))"
   895   have "sets (Pi\<^isub>P I M) = sigma_sets ?O ?G"
   896     by (subst infprod_algebra_alt) (simp add: sets_sigma)
   897   also have "\<dots> = sigma_sets ?O ?A"
   898   proof (intro equalityI sigma_sets_mono subsetI)
   899     interpret A: sigma_algebra ?S
   900       by (rule sigma_algebra_sigma) auto
   901     fix A assume "A \<in> ?G"
   902     then obtain J B where "finite J" "J \<noteq> {}" "J \<subseteq> I" "A = emb I J (Pi\<^isub>E J B)"
   903         and B: "\<And>i. i \<in> J \<Longrightarrow> B i \<in> sets (M i)"
   904       by auto
   905     then have A: "A = (\<Inter>j\<in>J. (\<lambda>x. x j) -` (B j) \<inter> space (Pi\<^isub>P I M))"
   906       by (auto simp: emb_def infprod_algebra_def generator_def Pi_iff)
   907     { fix j assume "j\<in>J"
   908       with `J \<subseteq> I` have "j \<in> I" by auto
   909       with `j \<in> J` B have "(\<lambda>x. x j) -` (B j) \<inter> space (Pi\<^isub>P I M) \<in> sets ?S"
   910         by (auto simp: sets_sigma intro: sigma_sets.Basic) }
   911     with `finite J` `J \<noteq> {}` have "A \<in> sets ?S"
   912       unfolding A by (intro A.finite_INT) auto
   913     then show "A \<in> sigma_sets ?O ?A" by (simp add: sets_sigma)
   914   next
   915     fix A assume "A \<in> ?A"
   916     then obtain i B where i: "i \<in> I" "B \<in> sets (M i)"
   917         and "A = (\<lambda>x. x i) -` B \<inter> space (Pi\<^isub>P I M)"
   918       by auto
   919     then have "A = emb I {i} (Pi\<^isub>E {i} (\<lambda>_. B))"
   920       by (auto simp: emb_def infprod_algebra_def generator_def Pi_iff)
   921     with i show "A \<in> sigma_sets ?O ?G"
   922       by (intro sigma_sets.Basic UN_I[where a="{i}"]) auto
   923   qed
   924   also have "\<dots> = sets ?S"
   925     by (simp add: sets_sigma)
   926   finally show "sets (Pi\<^isub>P I M) = sets ?S" .
   927 qed simp_all
   928 
   929 lemma (in product_prob_space) measurable_into_infprod_algebra:
   930   assumes "sigma_algebra N"
   931   assumes f: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. f x i) \<in> measurable N (M i)"
   932   assumes ext: "\<And>x. x \<in> space N \<Longrightarrow> f x \<in> extensional I"
   933   shows "f \<in> measurable N (Pi\<^isub>P I M)"
   934 proof -
   935   interpret N: sigma_algebra N by fact
   936   have f_in: "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. f x i) \<in> space N \<rightarrow> space (M i)"
   937     using f by (auto simp: measurable_def)
   938   { fix i A assume i: "i \<in> I" "A \<in> sets (M i)"
   939     then have "f -` (\<lambda>x. x i) -` A \<inter> f -` space infprod_algebra \<inter> space N = (\<lambda>x. f x i) -` A \<inter> space N"
   940       using f_in ext by (auto simp: infprod_algebra_def generator_def)
   941     also have "\<dots> \<in> sets N"
   942       by (rule measurable_sets f i)+
   943     finally have "f -` (\<lambda>x. x i) -` A \<inter> f -` space infprod_algebra \<inter> space N \<in> sets N" . }
   944   with f_in ext show ?thesis
   945     by (subst infprod_algebra_alt2)
   946        (auto intro!: N.measurable_sigma simp: Pi_iff infprod_algebra_def generator_def)
   947 qed
   948 
   949 lemma (in product_prob_space) measurable_singleton_infprod:
   950   assumes "i \<in> I"
   951   shows "(\<lambda>x. x i) \<in> measurable (Pi\<^isub>P I M) (M i)"
   952 proof (unfold measurable_def, intro CollectI conjI ballI)
   953   show "(\<lambda>x. x i) \<in> space (Pi\<^isub>P I M) \<rightarrow> space (M i)"
   954     using M.sets_into_space `i \<in> I`
   955     by (auto simp: infprod_algebra_def generator_def)
   956   fix A assume "A \<in> sets (M i)"
   957   have "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M) = emb I {i} (\<Pi>\<^isub>E _\<in>{i}. A)"
   958     by (auto simp: infprod_algebra_def generator_def emb_def)
   959   also have "\<dots> \<in> sets (Pi\<^isub>P I M)"
   960     using `i \<in> I` `A \<in> sets (M i)`
   961     by (intro emb_in_infprod_algebra product_algebraI) auto
   962   finally show "(\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M) \<in> sets (Pi\<^isub>P I M)" .
   963 qed
   964 
   965 lemma (in product_prob_space) sigma_product_algebra_sigma_eq:
   966   assumes M: "\<And>i. i \<in> I \<Longrightarrow> M i = sigma (E i)"
   967   shows "sets (Pi\<^isub>P I M) = sigma_sets (space (Pi\<^isub>P I M)) (\<Union>i\<in>I. (\<lambda>A. (\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M)) ` sets (E i))"
   968 proof -
   969   let ?E = "(\<Union>i\<in>I. (\<lambda>A. (\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M)) ` sets (E i))"
   970   let ?M = "(\<Union>i\<in>I. (\<lambda>A. (\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M)) ` sets (M i))"
   971   { fix i A assume "i\<in>I" "A \<in> sets (E i)"
   972     then have "A \<in> sets (M i)" using M by auto
   973     then have "A \<in> Pow (space (M i))" using M.sets_into_space by auto
   974     then have "A \<in> Pow (space (E i))" using M[OF `i \<in> I`] by auto }
   975   moreover
   976   have "\<And>i. i \<in> I \<Longrightarrow> (\<lambda>x. x i) \<in> space infprod_algebra \<rightarrow> space (E i)"
   977     by (auto simp: M infprod_algebra_def generator_def Pi_iff)
   978   ultimately have "sigma_sets (space (Pi\<^isub>P I M)) ?M \<subseteq> sigma_sets (space (Pi\<^isub>P I M)) ?E"
   979     apply (intro sigma_sets_mono UN_least)
   980     apply (simp add: sets_sigma M)
   981     apply (subst sigma_sets_vimage[symmetric])
   982     apply (auto intro!: sigma_sets_mono')
   983     done
   984   moreover have "sigma_sets (space (Pi\<^isub>P I M)) ?E \<subseteq> sigma_sets (space (Pi\<^isub>P I M)) ?M"
   985     by (intro sigma_sets_mono') (auto simp: M)
   986   ultimately show ?thesis
   987     by (subst infprod_algebra_alt2) (auto simp: sets_sigma)
   988 qed
   989 
   990 lemma (in product_prob_space) Int_proj_eq_emb:
   991   assumes "J \<noteq> {}" "J \<subseteq> I"
   992   shows "(\<Inter>i\<in>J. (\<lambda>x. x i) -` A i \<inter> space (Pi\<^isub>P I M)) = emb I J (Pi\<^isub>E J A)"
   993   using assms by (auto simp: infprod_algebra_def generator_def emb_def Pi_iff)
   994 
   995 lemma (in product_prob_space) emb_insert:
   996   "i \<notin> J \<Longrightarrow> emb I J (Pi\<^isub>E J f) \<inter> ((\<lambda>x. x i) -` A \<inter> space (Pi\<^isub>P I M)) =
   997     emb I (insert i J) (Pi\<^isub>E (insert i J) (f(i := A)))"
   998   by (auto simp: emb_def Pi_iff infprod_algebra_def generator_def split: split_if_asm)
   999 
  1000 subsection {* Sequence space *}
  1001 
  1002 locale sequence_space = product_prob_space M "UNIV :: nat set" for M
  1003 
  1004 lemma (in sequence_space) infprod_in_sets[intro]:
  1005   fixes E :: "nat \<Rightarrow> 'a set" assumes E: "\<And>i. E i \<in> sets (M i)"
  1006   shows "Pi UNIV E \<in> sets (Pi\<^isub>P UNIV M)"
  1007 proof -
  1008   have "Pi UNIV E = (\<Inter>i. emb UNIV {..i} (\<Pi>\<^isub>E j\<in>{..i}. E j))"
  1009     using E E[THEN M.sets_into_space]
  1010     by (auto simp: emb_def Pi_iff extensional_def) blast
  1011   with E show ?thesis
  1012     by (auto intro: emb_in_infprod_algebra)
  1013 qed
  1014 
  1015 lemma (in sequence_space) measure_infprod:
  1016   fixes E :: "nat \<Rightarrow> 'a set" assumes E: "\<And>i. E i \<in> sets (M i)"
  1017   shows "(\<lambda>n. \<Prod>i\<le>n. M.\<mu>' i (E i)) ----> \<mu>' (Pi UNIV E)"
  1018 proof -
  1019   let ?E = "\<lambda>n. emb UNIV {..n} (Pi\<^isub>E {.. n} E)"
  1020   { fix n :: nat
  1021     interpret n: finite_product_prob_space M "{..n}" by default auto
  1022     have "(\<Prod>i\<le>n. M.\<mu>' i (E i)) = n.\<mu>' (Pi\<^isub>E {.. n} E)"
  1023       using E by (subst n.finite_measure_times) auto
  1024     also have "\<dots> = \<mu>' (?E n)"
  1025       using E by (intro finite_measure_infprod_emb[symmetric]) auto
  1026     finally have "(\<Prod>i\<le>n. M.\<mu>' i (E i)) = \<mu>' (?E n)" . }
  1027   moreover have "Pi UNIV E = (\<Inter>n. ?E n)"
  1028     using E E[THEN M.sets_into_space]
  1029     by (auto simp: emb_def extensional_def Pi_iff) blast
  1030   moreover have "range ?E \<subseteq> sets (Pi\<^isub>P UNIV M)"
  1031     using E by auto
  1032   moreover have "decseq ?E"
  1033     by (auto simp: emb_def Pi_iff decseq_def)
  1034   ultimately show ?thesis
  1035     by (simp add: finite_continuity_from_above)
  1036 qed
  1037 
  1038 end