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