HOL-Probability: more about probability, prepare for Markov processes in the AFP
authorhoelzl
Fri, 30 Sep 2016 16:08:38 +0200
changeset 64008 17a20ca86d62
parent 64007 041cda506fb2
child 64009 a5d293f1af80
HOL-Probability: more about probability, prepare for Markov processes in the AFP
src/HOL/Analysis/Binary_Product_Measure.thy
src/HOL/Analysis/Bochner_Integration.thy
src/HOL/Analysis/Borel_Space.thy
src/HOL/Analysis/Derivative.thy
src/HOL/Analysis/Finite_Product_Measure.thy
src/HOL/Analysis/Lebesgue_Measure.thy
src/HOL/Analysis/Measurable.thy
src/HOL/Analysis/Measure_Space.thy
src/HOL/Analysis/Nonnegative_Lebesgue_Integration.thy
src/HOL/Analysis/Sigma_Algebra.thy
src/HOL/Library/Countable_Set.thy
src/HOL/Library/Extended_Nonnegative_Real.thy
src/HOL/Probability/Giry_Monad.thy
src/HOL/Probability/Infinite_Product_Measure.thy
src/HOL/Probability/Information.thy
src/HOL/Probability/Probability_Mass_Function.thy
src/HOL/Probability/Probability_Measure.thy
src/HOL/Probability/Stream_Space.thy
src/HOL/Topological_Spaces.thy
--- a/src/HOL/Analysis/Binary_Product_Measure.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Binary_Product_Measure.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -686,6 +686,53 @@
   show "a \<in> A" and "b \<in> B" by auto
 qed
 
+lemma sets_pair_eq:
+  assumes Ea: "Ea \<subseteq> Pow (space A)" "sets A = sigma_sets (space A) Ea"
+    and Ca: "countable Ca" "Ca \<subseteq> Ea" "\<Union>Ca = space A"
+    and Eb: "Eb \<subseteq> Pow (space B)" "sets B = sigma_sets (space B) Eb"
+    and Cb: "countable Cb" "Cb \<subseteq> Eb" "\<Union>Cb = space B"
+  shows "sets (A \<Otimes>\<^sub>M B) = sets (sigma (space A \<times> space B) { a \<times> b | a b. a \<in> Ea \<and> b \<in> Eb })"
+    (is "_ = sets (sigma ?\<Omega> ?E)")
+proof
+  show "sets (sigma ?\<Omega> ?E) \<subseteq> sets (A \<Otimes>\<^sub>M B)"
+    using Ea(1) Eb(1) by (subst sigma_le_sets) (auto simp: Ea(2) Eb(2))
+  have "?E \<subseteq> Pow ?\<Omega>"
+    using Ea(1) Eb(1) by auto
+  then have E: "a \<in> Ea \<Longrightarrow> b \<in> Eb \<Longrightarrow> a \<times> b \<in> sets (sigma ?\<Omega> ?E)" for a b
+    by auto
+  have "sets (A \<Otimes>\<^sub>M B) \<subseteq> sets (Sup {vimage_algebra ?\<Omega> fst A, vimage_algebra ?\<Omega> snd B})"
+    unfolding sets_pair_eq_sets_fst_snd ..
+  also have "vimage_algebra ?\<Omega> fst A = vimage_algebra ?\<Omega> fst (sigma (space A) Ea)"
+    by (intro vimage_algebra_cong[OF refl refl]) (simp add: Ea)
+  also have "\<dots> = sigma ?\<Omega> {fst -` A \<inter> ?\<Omega> |A. A \<in> Ea}"
+    by (intro Ea vimage_algebra_sigma) auto
+  also have "vimage_algebra ?\<Omega> snd B = vimage_algebra ?\<Omega> snd (sigma (space B) Eb)"
+    by (intro vimage_algebra_cong[OF refl refl]) (simp add: Eb)
+  also have "\<dots> = sigma ?\<Omega> {snd -` A \<inter> ?\<Omega> |A. A \<in> Eb}"
+    by (intro Eb vimage_algebra_sigma) auto
+  also have "{sigma ?\<Omega> {fst -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Ea}, sigma ?\<Omega> {snd -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Eb}} =
+    sigma ?\<Omega> ` {{fst -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Ea}, {snd -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Eb}}"
+    by auto
+  also have "sets (SUP S:{{fst -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Ea}, {snd -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Eb}}. sigma ?\<Omega> S) =
+    sets (sigma ?\<Omega> (\<Union>{{fst -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Ea}, {snd -` Aa \<inter> ?\<Omega> |Aa. Aa \<in> Eb}}))"
+    using Ea(1) Eb(1) by (intro sets_Sup_sigma) auto
+  also have "\<dots> \<subseteq> sets (sigma ?\<Omega> ?E)"
+  proof (subst sigma_le_sets, safe intro!: space_in_measure_of)
+    fix a assume "a \<in> Ea"
+    then have "fst -` a \<inter> ?\<Omega> = (\<Union>b\<in>Cb. a \<times> b)"
+      using Cb(3)[symmetric] Ea(1) by auto
+    then show "fst -` a \<inter> ?\<Omega> \<in> sets (sigma ?\<Omega> ?E)"
+      using Cb \<open>a \<in> Ea\<close> by (auto intro!: sets.countable_UN' E)
+  next
+    fix b assume "b \<in> Eb"
+    then have "snd -` b \<inter> ?\<Omega> = (\<Union>a\<in>Ca. a \<times> b)"
+      using Ca(3)[symmetric] Eb(1) by auto
+    then show "snd -` b \<inter> ?\<Omega> \<in> sets (sigma ?\<Omega> ?E)"
+      using Ca \<open>b \<in> Eb\<close> by (auto intro!: sets.countable_UN' E)
+  qed
+  finally show "sets (A \<Otimes>\<^sub>M B) \<subseteq> sets (sigma ?\<Omega> ?E)" .
+qed
+
 lemma borel_prod:
   "(borel \<Otimes>\<^sub>M borel) = (borel :: ('a::second_countable_topology \<times> 'b::second_countable_topology) measure)"
   (is "?P = ?B")
--- a/src/HOL/Analysis/Bochner_Integration.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Bochner_Integration.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -951,6 +951,10 @@
   unfolding integrable.simps
   by (intro has_bochner_integral_cong_AE arg_cong[where f=Ex] ext)
 
+lemma integrable_cong_AE_imp:
+  "integrable M g \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (AE x in M. g x = f x) \<Longrightarrow> integrable M f"
+  using integrable_cong_AE[of f M g] by (auto simp: eq_commute)
+
 lemma integral_cong:
   "M = N \<Longrightarrow> (\<And>x. x \<in> space N \<Longrightarrow> f x = g x) \<Longrightarrow> integral\<^sup>L M f = integral\<^sup>L N g"
   by (simp cong: has_bochner_integral_cong cong del: if_weak_cong add: lebesgue_integral_def)
@@ -1682,6 +1686,16 @@
   finally show ?thesis .
 qed
 
+lemma nn_integral_eq_integrable:
+  assumes f: "f \<in> M \<rightarrow>\<^sub>M borel" "AE x in M. 0 \<le> f x" and "0 \<le> x"
+  shows "(\<integral>\<^sup>+x. f x \<partial>M) = ennreal x \<longleftrightarrow> (integrable M f \<and> integral\<^sup>L M f = x)"
+proof (safe intro!: nn_integral_eq_integral assms)
+  assume *: "(\<integral>\<^sup>+x. f x \<partial>M) = ennreal x"
+  with integrableI_nn_integral_finite[OF f this] nn_integral_eq_integral[of M f, OF _ f(2)]
+  show "integrable M f" "integral\<^sup>L M f = x"
+    by (simp_all add: * assms integral_nonneg_AE)
+qed
+
 lemma
   fixes f :: "_ \<Rightarrow> _ \<Rightarrow> 'a :: {banach, second_countable_topology}"
   assumes integrable[measurable]: "\<And>i. integrable M (f i)"
@@ -2227,6 +2241,27 @@
   shows "integrable (count_space UNIV) f \<Longrightarrow> integral\<^sup>L (count_space UNIV) f = (\<Sum>x. f x)"
   using sums_integral_count_space_nat by (rule sums_unique)
 
+lemma integrable_bij_count_space:
+  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
+  assumes g: "bij_betw g A B"
+  shows "integrable (count_space A) (\<lambda>x. f (g x)) \<longleftrightarrow> integrable (count_space B) f"
+  unfolding integrable_iff_bounded by (subst nn_integral_bij_count_space[OF g]) auto
+
+lemma integral_bij_count_space:
+  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
+  assumes g: "bij_betw g A B"
+  shows "integral\<^sup>L (count_space A) (\<lambda>x. f (g x)) = integral\<^sup>L (count_space B) f"
+  using g[THEN bij_betw_imp_funcset]
+  apply (subst distr_bij_count_space[OF g, symmetric])
+  apply (intro integral_distr[symmetric])
+  apply auto
+  done
+
+lemma has_bochner_integral_count_space_nat:
+  fixes f :: "nat \<Rightarrow> _::{banach,second_countable_topology}"
+  shows "has_bochner_integral (count_space UNIV) f x \<Longrightarrow> f sums x"
+  unfolding has_bochner_integral_iff by (auto intro!: sums_integral_count_space_nat)
+
 subsection \<open>Point measure\<close>
 
 lemma lebesgue_integral_point_measure_finite:
--- a/src/HOL/Analysis/Borel_Space.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Borel_Space.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -1500,6 +1500,9 @@
   apply auto
   done
 
+lemma measurable_of_bool[measurable]: "of_bool \<in> count_space UNIV \<rightarrow>\<^sub>M borel"
+  by simp
+
 subsection "Borel space on the extended reals"
 
 lemma borel_measurable_ereal[measurable (raw)]:
@@ -1909,6 +1912,56 @@
   shows "mono f \<Longrightarrow> f \<in> borel_measurable borel"
   using borel_measurable_mono_on_fnc[of f UNIV] by (simp add: mono_def mono_on_def)
 
+lemma measurable_bdd_below_real[measurable (raw)]:
+  fixes F :: "'a \<Rightarrow> 'i \<Rightarrow> real"
+  assumes [simp]: "countable I" and [measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> M \<rightarrow>\<^sub>M borel"
+  shows "Measurable.pred M (\<lambda>x. bdd_below ((\<lambda>i. F i x)`I))"
+proof (subst measurable_cong)
+  show "bdd_below ((\<lambda>i. F i x)`I) \<longleftrightarrow> (\<exists>q\<in>\<int>. \<forall>i\<in>I. q \<le> F i x)" for x
+    by (auto simp: bdd_below_def intro!: bexI[of _ "of_int (floor _)"] intro: order_trans of_int_floor_le)
+  show "Measurable.pred M (\<lambda>w. \<exists>q\<in>\<int>. \<forall>i\<in>I. q \<le> F i w)"
+    using countable_int by measurable
+qed
+
+lemma borel_measurable_cINF_real[measurable (raw)]:
+  fixes F :: "_ \<Rightarrow> _ \<Rightarrow> real"
+  assumes [simp]: "countable I"
+  assumes F[measurable]: "\<And>i. i \<in> I \<Longrightarrow> F i \<in> borel_measurable M"
+  shows "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable M"
+proof (rule measurable_piecewise_restrict)
+  let ?\<Omega> = "{x\<in>space M. bdd_below ((\<lambda>i. F i x)`I)}"
+  show "countable {?\<Omega>, - ?\<Omega>}" "space M \<subseteq> \<Union>{?\<Omega>, - ?\<Omega>}" "\<And>X. X \<in> {?\<Omega>, - ?\<Omega>} \<Longrightarrow> X \<inter> space M \<in> sets M"
+    by auto
+  fix X assume "X \<in> {?\<Omega>, - ?\<Omega>}" then show "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable (restrict_space M X)"
+  proof safe
+    show "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable (restrict_space M ?\<Omega>)"
+      by (intro borel_measurable_cINF measurable_restrict_space1 F)
+         (auto simp: space_restrict_space)
+    show "(\<lambda>x. INF i:I. F i x) \<in> borel_measurable (restrict_space M (-?\<Omega>))"
+    proof (subst measurable_cong)
+      fix x assume "x \<in> space (restrict_space M (-?\<Omega>))"
+      then have "\<not> (\<forall>i\<in>I. - F i x \<le> y)" for y
+        by (auto simp: space_restrict_space bdd_above_def bdd_above_uminus[symmetric])
+      then show "(INF i:I. F i x) = - (THE x. False)"
+        by (auto simp: space_restrict_space Inf_real_def Sup_real_def Least_def simp del: Set.ball_simps(10))
+    qed simp
+  qed
+qed
+
+lemma borel_Ici: "borel = sigma UNIV (range (\<lambda>x::real. {x ..}))"
+proof (safe intro!: borel_eq_sigmaI1[OF borel_Iio])
+  fix x :: real
+  have eq: "{..<x} = space (sigma UNIV (range atLeast)) - {x ..}"
+    by auto
+  show "{..<x} \<in> sets (sigma UNIV (range atLeast))"
+    unfolding eq by (intro sets.compl_sets) auto
+qed auto
+
+lemma borel_measurable_pred_less[measurable (raw)]:
+  fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, linorder_topology}"
+  shows "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> Measurable.pred M (\<lambda>w. f w < g w)"
+  unfolding Measurable.pred_def by (rule borel_measurable_less)
+
 no_notation
   eucl_less (infix "<e" 50)
 
--- a/src/HOL/Analysis/Derivative.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Derivative.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -2370,6 +2370,16 @@
   shows "(\<exists>c. (f has_real_derivative c) F) = (\<exists>D. (f has_derivative D) F)"
   by (metis has_field_derivative_def has_real_derivative)
 
+lemma has_vector_derivative_cong_ev:
+  assumes *: "eventually (\<lambda>x. x \<in> s \<longrightarrow> f x = g x) (nhds x)" "f x = g x"
+  shows "(f has_vector_derivative f') (at x within s) = (g has_vector_derivative f') (at x within s)"
+  unfolding has_vector_derivative_def has_derivative_def
+  using *
+  apply (cases "at x within s \<noteq> bot")
+  apply (intro refl conj_cong filterlim_cong)
+  apply (auto simp: netlimit_within eventually_at_filter elim: eventually_mono)
+  done
+
 definition deriv :: "('a \<Rightarrow> 'a::real_normed_field) \<Rightarrow> 'a \<Rightarrow> 'a" where
   "deriv f x \<equiv> SOME D. DERIV f x :> D"
 
--- a/src/HOL/Analysis/Finite_Product_Measure.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Finite_Product_Measure.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -1196,4 +1196,14 @@
     by (subst emeasure_distr) (auto simp: measurable_pair_iff)
 qed simp
 
+lemma infprod_in_sets[intro]:
+  fixes E :: "nat \<Rightarrow> 'a set" assumes E: "\<And>i. E i \<in> sets (M i)"
+  shows "Pi UNIV E \<in> sets (\<Pi>\<^sub>M i\<in>UNIV::nat set. M i)"
+proof -
+  have "Pi UNIV E = (\<Inter>i. prod_emb UNIV M {..i} (\<Pi>\<^sub>E j\<in>{..i}. E j))"
+    using E E[THEN sets.sets_into_space]
+    by (auto simp: prod_emb_def Pi_iff extensional_def)
+  with E show ?thesis by auto
+qed
+
 end
--- a/src/HOL/Analysis/Lebesgue_Measure.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Lebesgue_Measure.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -11,6 +11,24 @@
   imports Finite_Product_Measure Bochner_Integration Caratheodory Complete_Measure Summation_Tests Regularity
 begin
 
+lemma measure_eqI_lessThan:
+  fixes M N :: "real measure"
+  assumes sets: "sets M = sets borel" "sets N = sets borel"
+  assumes fin: "\<And>x. emeasure M {x <..} < \<infinity>"
+  assumes "\<And>x. emeasure M {x <..} = emeasure N {x <..}"
+  shows "M = N"
+proof (rule measure_eqI_generator_eq_countable)
+  let ?LT = "\<lambda>a::real. {a <..}" let ?E = "range ?LT"
+  show "Int_stable ?E"
+    by (auto simp: Int_stable_def lessThan_Int_lessThan)
+
+  show "?E \<subseteq> Pow UNIV" "sets M = sigma_sets UNIV ?E" "sets N = sigma_sets UNIV ?E"
+    unfolding sets borel_Ioi by auto
+
+  show "?LT`Rats \<subseteq> ?E" "(\<Union>i\<in>Rats. ?LT i) = UNIV" "\<And>a. a \<in> ?LT`Rats \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
+    using fin by (auto intro: Rats_no_bot_less simp: less_top)
+qed (auto intro: assms countable_rat)
+
 subsection \<open>Every right continuous and nondecreasing function gives rise to a measure\<close>
 
 definition interval_measure :: "(real \<Rightarrow> real) \<Rightarrow> real measure" where
--- a/src/HOL/Analysis/Measurable.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Measurable.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -618,6 +618,24 @@
   shows "Measurable.pred M ((R ^^ n) T)"
   by (induct n) (auto intro: assms)
 
+lemma measurable_compose_countable_restrict:
+  assumes P: "countable {i. P i}"
+    and f: "f \<in> M \<rightarrow>\<^sub>M count_space UNIV"
+    and Q: "\<And>i. P i \<Longrightarrow> pred M (Q i)"
+  shows "pred M (\<lambda>x. P (f x) \<and> Q (f x) x)"
+proof -
+  have P_f: "{x \<in> space M. P (f x)} \<in> sets M"
+    unfolding pred_def[symmetric] by (rule measurable_compose[OF f]) simp
+  have "pred (restrict_space M {x\<in>space M. P (f x)}) (\<lambda>x. Q (f x) x)"
+  proof (rule measurable_compose_countable'[where g=f, OF _ _ P])
+    show "f \<in> restrict_space M {x\<in>space M. P (f x)} \<rightarrow>\<^sub>M count_space {i. P i}"
+      by (rule measurable_count_space_extend[OF subset_UNIV])
+         (auto simp: space_restrict_space intro!: measurable_restrict_space1 f)
+  qed (auto intro!: measurable_restrict_space1 Q)
+  then show ?thesis
+    unfolding pred_restrict_space[OF P_f] by (simp cong: measurable_cong)
+qed
+
 hide_const (open) pred
 
 end
--- a/src/HOL/Analysis/Measure_Space.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Measure_Space.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -838,6 +838,38 @@
   qed
 qed
 
+lemma space_empty: "space M = {} \<Longrightarrow> M = count_space {}"
+  by (rule measure_eqI) (simp_all add: space_empty_iff)
+
+lemma measure_eqI_generator_eq_countable:
+  fixes M N :: "'a measure" and E :: "'a set set" and A :: "'a set set"
+  assumes E: "Int_stable E" "E \<subseteq> Pow \<Omega>" "\<And>X. X \<in> E \<Longrightarrow> emeasure M X = emeasure N X"
+    and sets: "sets M = sigma_sets \<Omega> E" "sets N = sigma_sets \<Omega> E"
+  and A: "A \<subseteq> E" "(\<Union>A) = \<Omega>" "countable A" "\<And>a. a \<in> A \<Longrightarrow> emeasure M a \<noteq> \<infinity>"
+  shows "M = N"
+proof cases
+  assume "\<Omega> = {}"
+  have *: "sigma_sets \<Omega> E = sets (sigma \<Omega> E)"
+    using E(2) by simp
+  have "space M = \<Omega>" "space N = \<Omega>"
+    using sets E(2) unfolding * by (auto dest: sets_eq_imp_space_eq simp del: sets_measure_of)
+  then show "M = N"
+    unfolding \<open>\<Omega> = {}\<close> by (auto dest: space_empty)
+next
+  assume "\<Omega> \<noteq> {}" with \<open>\<Union>A = \<Omega>\<close> have "A \<noteq> {}" by auto
+  from this \<open>countable A\<close> have rng: "range (from_nat_into A) = A"
+    by (rule range_from_nat_into)
+  show "M = N"
+  proof (rule measure_eqI_generator_eq[OF E sets])
+    show "range (from_nat_into A) \<subseteq> E"
+      unfolding rng using \<open>A \<subseteq> E\<close> .
+    show "(\<Union>i. from_nat_into A i) = \<Omega>"
+      unfolding rng using \<open>\<Union>A = \<Omega>\<close> .
+    show "emeasure M (from_nat_into A i) \<noteq> \<infinity>" for i
+      using rng by (intro A) auto
+  qed
+qed
+
 lemma measure_of_of_measure: "measure_of (space M) (sets M) (emeasure M) = M"
 proof (intro measure_eqI emeasure_measure_of_sigma)
   show "sigma_algebra (space M) (sets M)" ..
@@ -1097,6 +1129,9 @@
   "(\<And>x. x \<in> space M \<Longrightarrow> P x \<longleftrightarrow> Q x) \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> (AE x in M. Q x)"
   by auto
 
+lemma AE_cong_strong: "M = N \<Longrightarrow> (\<And>x. x \<in> space N =simp=> P x = Q x) \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> (AE x in N. Q x)"
+  by (auto simp: simp_implies_def)
+
 lemma AE_all_countable:
   "(AE x in M. \<forall>i. P i x) \<longleftrightarrow> (\<forall>i::'i::countable. AE x in M. P i x)"
 proof
@@ -2135,9 +2170,6 @@
   qed simp
 qed (simp add: emeasure_notin_sets)
 
-lemma space_empty: "space M = {} \<Longrightarrow> M = count_space {}"
-  by (rule measure_eqI) (simp_all add: space_empty_iff)
-
 lemma null_sets_count_space: "null_sets (count_space A) = { {} }"
   unfolding null_sets_def by (auto simp add: emeasure_count_space_eq_0)
 
--- a/src/HOL/Analysis/Nonnegative_Lebesgue_Integration.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Nonnegative_Lebesgue_Integration.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -1692,6 +1692,16 @@
     by (simp add: ** nn_integral_suminf from_nat_into)
 qed
 
+lemma of_bool_Bex_eq_nn_integral:
+  assumes unique: "\<And>x y. x \<in> X \<Longrightarrow> y \<in> X \<Longrightarrow> P x \<Longrightarrow> P y \<Longrightarrow> x = y"
+  shows "of_bool (\<exists>y\<in>X. P y) = (\<integral>\<^sup>+y. of_bool (P y) \<partial>count_space X)"
+proof cases
+  assume "\<exists>y\<in>X. P y"
+  then obtain y where "P y" "y \<in> X" by auto
+  then show ?thesis
+    by (subst nn_integral_count_space'[where A="{y}"]) (auto dest: unique)
+qed (auto cong: nn_integral_cong_simp)
+
 lemma emeasure_UN_countable:
   assumes sets[measurable]: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> sets M" and I[simp]: "countable I"
   assumes disj: "disjoint_family_on X I"
--- a/src/HOL/Analysis/Sigma_Algebra.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Analysis/Sigma_Algebra.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -1244,6 +1244,10 @@
 lemma (in algebra) Int_stable: "Int_stable M"
   unfolding Int_stable_def by auto
 
+lemma Int_stableI_image:
+  "(\<And>i j. i \<in> I \<Longrightarrow> j \<in> I \<Longrightarrow> \<exists>k\<in>I. A i \<inter> A j = A k) \<Longrightarrow> Int_stable (A ` I)"
+  by (auto simp: Int_stable_def image_def)
+
 lemma Int_stableI:
   "(\<And>a b. a \<in> A \<Longrightarrow> b \<in> A \<Longrightarrow> a \<inter> b \<in> A) \<Longrightarrow> Int_stable A"
   unfolding Int_stable_def by auto
@@ -1574,6 +1578,9 @@
 using assms
 by(simp_all add: sets_measure_of_conv space_measure_of_conv)
 
+lemma space_in_measure_of[simp]: "\<Omega> \<in> sets (measure_of \<Omega> M \<mu>)"
+  by (subst sets_measure_of_conv) (auto simp: sigma_sets_top)
+
 lemma (in sigma_algebra) sets_measure_of_eq[simp]: "sets (measure_of \<Omega> M \<mu>) = M"
   using space_closed by (auto intro!: sigma_sets_eq)
 
@@ -2259,4 +2266,7 @@
   by (rule measurable_restrict_countable[OF X])
      (auto simp: eq[symmetric] space_restrict_space cong: measurable_cong' intro: f measurable_restrict_space1)
 
+lemma measurable_count_space_extend: "A \<subseteq> B \<Longrightarrow> f \<in> space M \<rightarrow> A \<Longrightarrow> f \<in> M \<rightarrow>\<^sub>M count_space B \<Longrightarrow> f \<in> M \<rightarrow>\<^sub>M count_space A"
+  by (auto simp: measurable_def)
+
 end
--- a/src/HOL/Library/Countable_Set.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Library/Countable_Set.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -284,6 +284,9 @@
 lemma countable_Collect_finite: "countable (Collect (finite::'a::countable set\<Rightarrow>bool))"
   by (simp add: Collect_finite_eq_lists)
 
+lemma countable_int: "countable \<int>"
+  unfolding Ints_def by auto
+
 lemma countable_rat: "countable \<rat>"
   unfolding Rats_def by auto
 
--- a/src/HOL/Library/Extended_Nonnegative_Real.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Library/Extended_Nonnegative_Real.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -220,6 +220,11 @@
   shows "summable f \<Longrightarrow> finite I \<Longrightarrow> \<forall>m\<in>- I. 0 \<le> f m \<Longrightarrow> setsum f I \<le> suminf f"
   by (rule sums_le[OF _ sums_If_finite_set summable_sums]) auto
 
+lemma suminf_eq_SUP_real:
+  assumes X: "summable X" "\<And>i. 0 \<le> X i" shows "suminf X = (SUP i. \<Sum>n<i. X n::real)"
+  by (intro LIMSEQ_unique[OF summable_LIMSEQ] X LIMSEQ_incseq_SUP)
+     (auto intro!: bdd_aboveI2[where M="\<Sum>i. X i"] setsum_le_suminf X monoI setsum_mono3)
+
 subsection \<open>Defining the extended non-negative reals\<close>
 
 text \<open>Basic definitions and type class setup\<close>
--- a/src/HOL/Probability/Giry_Monad.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Probability/Giry_Monad.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -28,6 +28,9 @@
   show "subprob_space M" by standard fact+
 qed
 
+lemma (in subprob_space) emeasure_subprob_space_less_top: "emeasure M A \<noteq> top"
+  using emeasure_finite[of A] .
+
 lemma prob_space_imp_subprob_space:
   "prob_space M \<Longrightarrow> subprob_space M"
   by (rule subprob_spaceI) (simp_all add: prob_space.emeasure_space_1 prob_space.not_empty)
@@ -245,6 +248,43 @@
     by (auto dest: subprob_space_kernel sets_kernel)
 qed
 
+lemma measurable_subprob_algebra_generated:
+  assumes eq: "sets N = sigma_sets \<Omega> G" and "Int_stable G" "G \<subseteq> Pow \<Omega>"
+  assumes subsp: "\<And>a. a \<in> space M \<Longrightarrow> subprob_space (K a)"
+  assumes sets: "\<And>a. a \<in> space M \<Longrightarrow> sets (K a) = sets N"
+  assumes "\<And>A. A \<in> G \<Longrightarrow> (\<lambda>a. emeasure (K a) A) \<in> borel_measurable M"
+  assumes \<Omega>: "(\<lambda>a. emeasure (K a) \<Omega>) \<in> borel_measurable M"
+  shows "K \<in> measurable M (subprob_algebra N)"
+proof (rule measurable_subprob_algebra)
+  fix a assume "a \<in> space M" then show "subprob_space (K a)" "sets (K a) = sets N" by fact+
+next
+  interpret G: sigma_algebra \<Omega> "sigma_sets \<Omega> G"
+    using \<open>G \<subseteq> Pow \<Omega>\<close> by (rule sigma_algebra_sigma_sets)
+  fix A assume "A \<in> sets N" with assms(2,3) show "(\<lambda>a. emeasure (K a) A) \<in> borel_measurable M"
+    unfolding \<open>sets N = sigma_sets \<Omega> G\<close>
+  proof (induction rule: sigma_sets_induct_disjoint)
+    case (basic A) then show ?case by fact
+  next
+    case empty then show ?case by simp
+  next
+    case (compl A)
+    have "(\<lambda>a. emeasure (K a) (\<Omega> - A)) \<in> borel_measurable M \<longleftrightarrow>
+      (\<lambda>a. emeasure (K a) \<Omega> - emeasure (K a) A) \<in> borel_measurable M"
+      using G.top G.sets_into_space sets eq compl subprob_space.emeasure_subprob_space_less_top[OF subsp]
+      by (intro measurable_cong emeasure_Diff) auto
+    with compl \<Omega> show ?case
+      by simp
+  next
+    case (union F)
+    moreover have "(\<lambda>a. emeasure (K a) (\<Union>i. F i)) \<in> borel_measurable M \<longleftrightarrow>
+        (\<lambda>a. \<Sum>i. emeasure (K a) (F i)) \<in> borel_measurable M"
+      using sets union eq
+      by (intro measurable_cong suminf_emeasure[symmetric]) auto
+    ultimately show ?case
+      by auto
+  qed
+qed
+
 lemma space_subprob_algebra_empty_iff:
   "space (subprob_algebra N) = {} \<longleftrightarrow> space N = {}"
 proof
@@ -1080,7 +1120,7 @@
   shows "space (bind M f) = space N"
   using assms by (intro sets_eq_imp_space_eq sets_bind)
 
-lemma bind_cong:
+lemma bind_cong_All:
   assumes "\<forall>x \<in> space M. f x = g x"
   shows "bind M f = bind M g"
 proof (cases "space M = {}")
@@ -1090,6 +1130,10 @@
   with \<open>space M \<noteq> {}\<close> and assms show ?thesis by (simp add: bind_nonempty cong: distr_cong)
 qed (simp add: bind_empty)
 
+lemma bind_cong:
+  "M = N \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> bind M f = bind N g"
+  using bind_cong_All[of M f g] by auto
+
 lemma bind_nonempty':
   assumes "f \<in> measurable M (subprob_algebra N)" "x \<in> space M"
   shows "bind M f = join (distr M (subprob_algebra N) f)"
@@ -1121,8 +1165,8 @@
 qed (simp add: bind_empty space_empty[of M] nn_integral_count_space)
 
 lemma AE_bind:
+  assumes N[measurable]: "N \<in> measurable M (subprob_algebra B)"
   assumes P[measurable]: "Measurable.pred B P"
-  assumes N[measurable]: "N \<in> measurable M (subprob_algebra B)"
   shows "(AE x in M \<bind> N. P x) \<longleftrightarrow> (AE x in M. AE y in N x. P y)"
 proof cases
   assume M: "space M = {}" show ?thesis
@@ -1454,7 +1498,7 @@
   also from Mh have "\<And>x. x \<in> space M \<Longrightarrow> h x \<in> measurable M' N" by measurable
   hence "do {x \<leftarrow> M; do {y \<leftarrow> f x; return N (h x y)} \<bind> g} =
             do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y) \<bind> g}"
-    apply (intro ballI bind_cong bind_assoc)
+    apply (intro ballI bind_cong refl bind_assoc)
     apply (subst measurable_cong_sets[OF sets_kernel[OF Mf] refl], simp)
     apply (rule measurable_compose[OF _ return_measurable], auto intro: Mg)
     done
@@ -1522,4 +1566,150 @@
   "null_measure M \<in> space (subprob_algebra M) \<longleftrightarrow> space M \<noteq> {}"
 by(simp add: space_subprob_algebra subprob_space_null_measure_iff)
 
+subsection \<open>Giry monad on probability spaces\<close>
+
+definition prob_algebra :: "'a measure \<Rightarrow> 'a measure measure" where
+  "prob_algebra K = restrict_space (subprob_algebra K) {M. prob_space M}"
+
+lemma space_prob_algebra: "space (prob_algebra M) = {N. sets N = sets M \<and> prob_space N}"
+  unfolding prob_algebra_def by (auto simp: space_subprob_algebra space_restrict_space prob_space_imp_subprob_space)
+
+lemma measurable_measure_prob_algebra[measurable]:
+  "a \<in> sets A \<Longrightarrow> (\<lambda>M. Sigma_Algebra.measure M a) \<in> prob_algebra A \<rightarrow>\<^sub>M borel"
+  unfolding prob_algebra_def by (intro measurable_restrict_space1 measurable_measure_subprob_algebra)
+
+lemma measurable_prob_algebraD:
+  "f \<in> N \<rightarrow>\<^sub>M prob_algebra M \<Longrightarrow> f \<in> N \<rightarrow>\<^sub>M subprob_algebra M"
+  unfolding prob_algebra_def measurable_restrict_space2_iff by auto
+
+lemma measure_measurable_prob_algebra2:
+  "Sigma (space M) A \<in> sets (M \<Otimes>\<^sub>M N) \<Longrightarrow> L \<in> M \<rightarrow>\<^sub>M prob_algebra N \<Longrightarrow>
+    (\<lambda>x. Sigma_Algebra.measure (L x) (A x)) \<in> borel_measurable M"
+  using measure_measurable_subprob_algebra2[of M A N L] by (auto intro: measurable_prob_algebraD)
+
+lemma measurable_prob_algebraI:
+  "(\<And>x. x \<in> space N \<Longrightarrow> prob_space (f x)) \<Longrightarrow> f \<in> N \<rightarrow>\<^sub>M subprob_algebra M \<Longrightarrow> f \<in> N \<rightarrow>\<^sub>M prob_algebra M"
+  unfolding prob_algebra_def by (intro measurable_restrict_space2) auto
+
+lemma measurable_distr_prob_space:
+  assumes f: "f \<in> M \<rightarrow>\<^sub>M N"
+  shows "(\<lambda>M'. distr M' N f) \<in> prob_algebra M \<rightarrow>\<^sub>M prob_algebra N"
+  unfolding prob_algebra_def measurable_restrict_space2_iff
+proof (intro conjI measurable_restrict_space1 measurable_distr f)
+  show "(\<lambda>M'. distr M' N f) \<in> space (restrict_space (subprob_algebra M) (Collect prob_space)) \<rightarrow> Collect prob_space"
+    using f by (auto simp: space_restrict_space space_subprob_algebra intro!: prob_space.prob_space_distr)
+qed
+
+lemma measurable_return_prob_space[measurable]: "return N \<in> N \<rightarrow>\<^sub>M prob_algebra N"
+  by (rule measurable_prob_algebraI) (auto simp: prob_space_return)
+
+lemma measurable_distr_prob_space2[measurable (raw)]:
+  assumes f: "g \<in> L \<rightarrow>\<^sub>M prob_algebra M" "(\<lambda>(x, y). f x y) \<in> L \<Otimes>\<^sub>M M \<rightarrow>\<^sub>M N"
+  shows "(\<lambda>x. distr (g x) N (f x)) \<in> L \<rightarrow>\<^sub>M prob_algebra N"
+  unfolding prob_algebra_def measurable_restrict_space2_iff
+proof (intro conjI measurable_restrict_space1 measurable_distr2[where M=M] f measurable_prob_algebraD)
+  show "(\<lambda>x. distr (g x) N (f x)) \<in> space L \<rightarrow> Collect prob_space"
+    using f subprob_measurableD[OF measurable_prob_algebraD[OF f(1)]]
+    by (auto simp: measurable_restrict_space2_iff prob_algebra_def
+             intro!: prob_space.prob_space_distr)
+qed
+
+lemma measurable_bind_prob_space:
+  assumes f: "f \<in> M \<rightarrow>\<^sub>M prob_algebra N" and g: "g \<in> N \<rightarrow>\<^sub>M prob_algebra R"
+  shows "(\<lambda>x. bind (f x) g) \<in> M \<rightarrow>\<^sub>M prob_algebra R"
+  unfolding prob_algebra_def measurable_restrict_space2_iff
+proof (intro conjI measurable_restrict_space1 measurable_bind2[where N=N] f g measurable_prob_algebraD)
+  show "(\<lambda>x. f x \<bind> g) \<in> space M \<rightarrow> Collect prob_space"
+    using g f subprob_measurableD[OF measurable_prob_algebraD[OF f]]
+    by (auto simp: measurable_restrict_space2_iff prob_algebra_def
+                intro!: prob_space.prob_space_bind[where S=R] AE_I2)
+qed
+
+lemma measurable_bind_prob_space2[measurable (raw)]:
+  assumes f: "f \<in> M \<rightarrow>\<^sub>M prob_algebra N" and g: "(\<lambda>(x, y). g x y) \<in> (M \<Otimes>\<^sub>M N) \<rightarrow>\<^sub>M prob_algebra R"
+  shows "(\<lambda>x. bind (f x) (g x)) \<in> M \<rightarrow>\<^sub>M prob_algebra R"
+  unfolding prob_algebra_def measurable_restrict_space2_iff
+proof (intro conjI measurable_restrict_space1 measurable_bind[where N=N] f g measurable_prob_algebraD)
+  show "(\<lambda>x. f x \<bind> g x) \<in> space M \<rightarrow> Collect prob_space"
+    using g f subprob_measurableD[OF measurable_prob_algebraD[OF f]]
+      using measurable_space[OF g]
+    by (auto simp: measurable_restrict_space2_iff prob_algebra_def space_pair_measure Pi_iff
+                intro!: prob_space.prob_space_bind[where S=R] AE_I2)
+qed (insert g, simp)
+
+
+lemma measurable_prob_algebra_generated:
+  assumes eq: "sets N = sigma_sets \<Omega> G" and "Int_stable G" "G \<subseteq> Pow \<Omega>"
+  assumes subsp: "\<And>a. a \<in> space M \<Longrightarrow> prob_space (K a)"
+  assumes sets: "\<And>a. a \<in> space M \<Longrightarrow> sets (K a) = sets N"
+  assumes "\<And>A. A \<in> G \<Longrightarrow> (\<lambda>a. emeasure (K a) A) \<in> borel_measurable M"
+  shows "K \<in> measurable M (prob_algebra N)"
+  unfolding measurable_restrict_space2_iff prob_algebra_def
+proof
+  show "K \<in> M \<rightarrow>\<^sub>M subprob_algebra N"
+  proof (rule measurable_subprob_algebra_generated[OF assms(1,2,3) _ assms(5,6)])
+    fix a assume "a \<in> space M" then show "subprob_space (K a)"
+      using subsp[of a] by (intro prob_space_imp_subprob_space)
+  next
+    have "(\<lambda>a. emeasure (K a) \<Omega>) \<in> borel_measurable M \<longleftrightarrow> (\<lambda>a. 1::ennreal) \<in> borel_measurable M"
+      using sets_eq_imp_space_eq[of "sigma \<Omega> G" N] \<open>G \<subseteq> Pow \<Omega>\<close> eq sets_eq_imp_space_eq[OF sets]
+        prob_space.emeasure_space_1[OF subsp]
+      by (intro measurable_cong) auto
+    then show "(\<lambda>a. emeasure (K a) \<Omega>) \<in> borel_measurable M" by simp
+  qed
+qed (insert subsp, auto)
+
+lemma in_space_prob_algebra:
+  "x \<in> space (prob_algebra M) \<Longrightarrow> emeasure x (space M) = 1"
+  unfolding prob_algebra_def space_restrict_space space_subprob_algebra
+  by (auto dest!: prob_space.emeasure_space_1 sets_eq_imp_space_eq)
+
+lemma prob_space_pair:
+  assumes "prob_space M" "prob_space N" shows "prob_space (M \<Otimes>\<^sub>M N)"
+proof -
+  interpret M: prob_space M by fact
+  interpret N: prob_space N by fact
+  interpret P: pair_prob_space M N proof qed
+  show ?thesis
+    by unfold_locales
+qed
+
+lemma measurable_pair_prob[measurable]:
+  "f \<in> M \<rightarrow>\<^sub>M prob_algebra N \<Longrightarrow> g \<in> M \<rightarrow>\<^sub>M prob_algebra L \<Longrightarrow> (\<lambda>x. f x \<Otimes>\<^sub>M g x) \<in> M \<rightarrow>\<^sub>M prob_algebra (N \<Otimes>\<^sub>M L)"
+  unfolding prob_algebra_def measurable_restrict_space2_iff
+  by (auto intro!: measurable_pair_measure prob_space_pair)
+
+lemma emeasure_bind_prob_algebra:
+  assumes A: "A \<in> space (prob_algebra N)"
+  assumes B: "B \<in> N \<rightarrow>\<^sub>M prob_algebra L"
+  assumes X: "X \<in> sets L"
+  shows "emeasure (bind A B) X = (\<integral>\<^sup>+x. emeasure (B x) X \<partial>A)"
+  using A B
+  by (intro emeasure_bind[OF _ _ X])
+     (auto simp: space_prob_algebra measurable_prob_algebraD cong: measurable_cong_sets intro!: prob_space.not_empty)
+
+lemma prob_space_bind':
+  assumes A: "A \<in> space (prob_algebra M)" and B: "B \<in> M \<rightarrow>\<^sub>M prob_algebra N" shows "prob_space (A \<bind> B)"
+  using measurable_bind_prob_space[OF measurable_const, OF A B, THEN measurable_space, of undefined "count_space UNIV"]
+  by (simp add: space_prob_algebra)
+
+lemma sets_bind':
+  assumes A: "A \<in> space (prob_algebra M)" and B: "B \<in> M \<rightarrow>\<^sub>M prob_algebra N" shows "sets (A \<bind> B) = sets N"
+  using measurable_bind_prob_space[OF measurable_const, OF A B, THEN measurable_space, of undefined "count_space UNIV"]
+  by (simp add: space_prob_algebra)
+
+lemma bind_cong_AE:
+  assumes M: "M \<in> space (prob_algebra L)"
+    and f: "f \<in> L \<rightarrow>\<^sub>M prob_algebra N" and g: "g \<in> L \<rightarrow>\<^sub>M prob_algebra N"
+    and ae: "AE x in M. f x = g x"
+  shows "bind M f = bind M g"
+proof (rule measure_eqI)
+  show "sets (M \<bind> f) = sets (M \<bind> g)"
+    unfolding sets_bind'[OF M f] sets_bind'[OF M g] ..
+  show "A \<in> sets (M \<bind> f) \<Longrightarrow> emeasure (M \<bind> f) A = emeasure (M \<bind> g) A" for A
+    unfolding sets_bind'[OF M f]
+    using emeasure_bind_prob_algebra[OF M f, of A] emeasure_bind_prob_algebra[OF M g, of A] ae
+    by (auto intro: nn_integral_cong_AE)
+qed
+
 end
--- a/src/HOL/Probability/Infinite_Product_Measure.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Probability/Infinite_Product_Measure.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -63,6 +63,21 @@
     using emeasure_PiM_emb[of "{}" "\<lambda>_. {}"] by (simp add: *)
 qed
 
+lemma prob_space_PiM:
+  assumes M: "\<And>i. i \<in> I \<Longrightarrow> prob_space (M i)" shows "prob_space (PiM I M)"
+proof -
+  let ?M = "\<lambda>i. if i \<in> I then M i else count_space {undefined}"
+  interpret M': prob_space "?M i" for i
+    using M by (cases "i \<in> I") (auto intro!: prob_spaceI)
+  interpret product_prob_space ?M I
+    by unfold_locales
+  have "prob_space (\<Pi>\<^sub>M i\<in>I. ?M i)"
+    by unfold_locales
+  also have "(\<Pi>\<^sub>M i\<in>I. ?M i) = (\<Pi>\<^sub>M i\<in>I. M i)"
+    by (intro PiM_cong) auto
+  finally show ?thesis .
+qed
+
 lemma (in product_prob_space) emeasure_PiM_Collect:
   assumes X: "J \<subseteq> I" "finite J" "\<And>i. i \<in> J \<Longrightarrow> X i \<in> sets (M i)"
   shows "emeasure (Pi\<^sub>M I M) {x\<in>space (Pi\<^sub>M I M). \<forall>i\<in>J. x i \<in> X i} = (\<Prod> i\<in>J. emeasure (M i) (X i))"
@@ -123,6 +138,107 @@
   apply simp_all
   done
 
+lemma emeasure_PiM_emb:
+  assumes M: "\<And>i. i \<in> I \<Longrightarrow> prob_space (M i)"
+  assumes J: "J \<subseteq> I" "finite J" and A: "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M i)"
+  shows "emeasure (Pi\<^sub>M I M) (prod_emb I M J (Pi\<^sub>E J A)) = (\<Prod>i\<in>J. emeasure (M i) (A i))"
+proof -
+  let ?M = "\<lambda>i. if i \<in> I then M i else count_space {undefined}"
+  interpret M': prob_space "?M i" for i
+    using M by (cases "i \<in> I") (auto intro!: prob_spaceI)
+  interpret P: product_prob_space ?M I
+    by unfold_locales
+  have "emeasure (Pi\<^sub>M I M) (prod_emb I M J (Pi\<^sub>E J A)) = emeasure (Pi\<^sub>M I ?M) (P.emb I J (Pi\<^sub>E J A))"
+    by (auto simp: prod_emb_def PiE_iff intro!: arg_cong2[where f=emeasure] PiM_cong)
+  also have "\<dots> = (\<Prod>i\<in>J. emeasure (M i) (A i))"
+    using J A by (subst P.emeasure_PiM_emb[OF J]) (auto intro!: setprod.cong)
+  finally show ?thesis .
+qed
+
+lemma distr_pair_PiM_eq_PiM:
+  fixes i' :: "'i" and I :: "'i set" and M :: "'i \<Rightarrow> 'a measure"
+  assumes M: "\<And>i. i \<in> I \<Longrightarrow> prob_space (M i)" "prob_space (M i')"
+  shows "distr (M i' \<Otimes>\<^sub>M (\<Pi>\<^sub>M i\<in>I. M i)) (\<Pi>\<^sub>M i\<in>insert i' I. M i) (\<lambda>(x, X). X(i' := x)) =
+    (\<Pi>\<^sub>M i\<in>insert i' I. M i)" (is "?L = _")
+proof (rule measure_eqI_PiM_infinite[symmetric, OF refl])
+  interpret M': prob_space "M i'" by fact
+  interpret I: prob_space "(\<Pi>\<^sub>M i\<in>I. M i)"
+    using M by (intro prob_space_PiM) auto
+  interpret I': prob_space "(\<Pi>\<^sub>M i\<in>insert i' I. M i)"
+    using M by (intro prob_space_PiM) auto
+  show "finite_measure (\<Pi>\<^sub>M i\<in>insert i' I. M i)"
+    by unfold_locales
+  fix J A assume J: "finite J" "J \<subseteq> insert i' I" and A: "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M i)"
+  let ?X = "prod_emb (insert i' I) M J (Pi\<^sub>E J A)"
+  have "Pi\<^sub>M (insert i' I) M ?X = (\<Prod>i\<in>J. M i (A i))"
+    using M J A by (intro emeasure_PiM_emb) auto
+  also have "\<dots> = M i' (if i' \<in> J then (A i') else space (M i')) * (\<Prod>i\<in>J-{i'}. M i (A i))"
+    using setprod.insert_remove[of J "\<lambda>i. M i (A i)" i'] J M'.emeasure_space_1
+    by (cases "i' \<in> J") (auto simp: insert_absorb)
+  also have "(\<Prod>i\<in>J-{i'}. M i (A i)) = Pi\<^sub>M I M (prod_emb I M (J - {i'}) (Pi\<^sub>E (J - {i'}) A))"
+    using M J A by (intro emeasure_PiM_emb[symmetric]) auto
+  also have "M i' (if i' \<in> J then (A i') else space (M i')) * \<dots> =
+    (M i' \<Otimes>\<^sub>M Pi\<^sub>M I M) ((if i' \<in> J then (A i') else space (M i')) \<times> prod_emb I M (J - {i'}) (Pi\<^sub>E (J - {i'}) A))"
+    using J A by (intro I.emeasure_pair_measure_Times[symmetric] sets_PiM_I) auto
+  also have "((if i' \<in> J then (A i') else space (M i')) \<times> prod_emb I M (J - {i'}) (Pi\<^sub>E (J - {i'}) A)) =
+    (\<lambda>(x, X). X(i' := x)) -` ?X \<inter> space (M i' \<Otimes>\<^sub>M Pi\<^sub>M I M)"
+    using A[of i', THEN sets.sets_into_space] unfolding set_eq_iff
+    by (simp add: prod_emb_def space_pair_measure space_PiM PiE_fun_upd ac_simps cong: conj_cong)
+       (auto simp add: Pi_iff Ball_def all_conj_distrib)
+  finally show "Pi\<^sub>M (insert i' I) M ?X = ?L ?X"
+    using J A by (simp add: emeasure_distr)
+qed simp
+
+lemma distr_PiM_reindex:
+  assumes M: "\<And>i. i \<in> K \<Longrightarrow> prob_space (M i)"
+  assumes f: "inj_on f I" "f \<in> I \<rightarrow> K"
+  shows "distr (Pi\<^sub>M K M) (\<Pi>\<^sub>M i\<in>I. M (f i)) (\<lambda>\<omega>. \<lambda>n\<in>I. \<omega> (f n)) = (\<Pi>\<^sub>M i\<in>I. M (f i))"
+    (is "distr ?K ?I ?t = ?I")
+proof (rule measure_eqI_PiM_infinite[symmetric, OF refl])
+  interpret prob_space ?I
+    using f M by (intro prob_space_PiM) auto
+  show "finite_measure ?I"
+    by unfold_locales
+  fix A J assume J: "finite J" "J \<subseteq> I" and A: "\<And>i. i \<in> J \<Longrightarrow> A i \<in> sets (M (f i))"
+  have [simp]: "i \<in> J \<Longrightarrow> the_inv_into I f (f i) = i" for i
+    using J f by (intro the_inv_into_f_f) auto
+  have "?I (prod_emb I (\<lambda>i. M (f i)) J (Pi\<^sub>E J A)) = (\<Prod>j\<in>J. M (f j) (A j))"
+    using f J A by (intro emeasure_PiM_emb M) auto
+  also have "\<dots> = (\<Prod>j\<in>f`J. M j (A (the_inv_into I f j)))"
+    using f J by (subst setprod.reindex) (auto intro!: setprod.cong intro: inj_on_subset simp: the_inv_into_f_f)
+  also have "\<dots> = ?K (prod_emb K M (f`J) (\<Pi>\<^sub>E j\<in>f`J. A (the_inv_into I f j)))"
+    using f J A by (intro emeasure_PiM_emb[symmetric] M) (auto simp: the_inv_into_f_f)
+  also have "prod_emb K M (f`J) (\<Pi>\<^sub>E j\<in>f`J. A (the_inv_into I f j)) = ?t -` prod_emb I (\<lambda>i. M (f i)) J (Pi\<^sub>E J A) \<inter> space ?K"
+    using f J A by (auto simp: prod_emb_def space_PiM Pi_iff PiE_iff Int_absorb1)
+  also have "?K \<dots> = distr ?K ?I ?t (prod_emb I (\<lambda>i. M (f i)) J (Pi\<^sub>E J A))"
+    using f J A by (intro emeasure_distr[symmetric] sets_PiM_I) (auto simp: Pi_iff)
+  finally show "?I (prod_emb I (\<lambda>i. M (f i)) J (Pi\<^sub>E J A)) = distr ?K ?I ?t (prod_emb I (\<lambda>i. M (f i)) J (Pi\<^sub>E J A))" .
+qed simp
+
+lemma distr_PiM_component:
+  assumes M: "\<And>i. i \<in> I \<Longrightarrow> prob_space (M i)"
+  assumes "i \<in> I"
+  shows "distr (Pi\<^sub>M I M) (M i) (\<lambda>\<omega>. \<omega> i) = M i"
+proof -
+  have *: "(\<lambda>\<omega>. \<omega> i) -` A \<inter> space (Pi\<^sub>M I M) = prod_emb I M {i} (\<Pi>\<^sub>E i'\<in>{i}. A)" for A
+    by (auto simp: prod_emb_def space_PiM)
+  show ?thesis
+    apply (intro measure_eqI)
+    apply (auto simp add: emeasure_distr \<open>i\<in>I\<close> * emeasure_PiM_emb M)
+    apply (subst emeasure_PiM_emb)
+    apply (simp_all add: M \<open>i\<in>I\<close>)
+    done
+qed
+
+lemma AE_PiM_component:
+  "(\<And>i. i \<in> I \<Longrightarrow> prob_space (M i)) \<Longrightarrow> i \<in> I \<Longrightarrow> AE x in M i. P x \<Longrightarrow> AE x in PiM I M. P (x i)"
+  using AE_distrD[of "\<lambda>x. x i" "PiM I M" "M i"]
+  by (subst (asm) distr_PiM_component[of I _ i]) (auto intro: AE_distrD[of "\<lambda>x. x i" _ _ P])
+
+lemma decseq_emb_PiE:
+  "incseq J \<Longrightarrow> decseq (\<lambda>i. prod_emb I M (J i) (\<Pi>\<^sub>E j\<in>J i. X j))"
+  by (fastforce simp: decseq_def prod_emb_def incseq_def Pi_iff)
+
 subsection \<open>Sequence space\<close>
 
 definition comb_seq :: "nat \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a) \<Rightarrow> (nat \<Rightarrow> 'a)" where
--- a/src/HOL/Probability/Information.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Probability/Information.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -389,10 +389,6 @@
     done
 qed
 
-lemma integrable_cong_AE_imp:
-  "integrable M g \<Longrightarrow> f \<in> borel_measurable M \<Longrightarrow> (AE x in M. g x = f x) \<Longrightarrow> integrable M f"
-  using integrable_cong_AE[of f M g] by (auto simp: eq_commute)
-
 lemma (in information_space) finite_entropy_integrable:
   "finite_entropy S X Px \<Longrightarrow> integrable S (\<lambda>x. Px x * log b (Px x))"
   unfolding finite_entropy_def by auto
--- a/src/HOL/Probability/Probability_Mass_Function.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Probability/Probability_Mass_Function.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -246,7 +246,7 @@
   shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
   by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite ennreal_mult_less_top)
 
-lemma integral_measure_pmf:
+lemma integral_measure_pmf_real:
   assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
   shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
 proof -
@@ -572,9 +572,9 @@
 
 lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
   unfolding pair_pmf_def pmf_bind pmf_return
-  apply (subst integral_measure_pmf[where A="{b}"])
+  apply (subst integral_measure_pmf_real[where A="{b}"])
   apply (auto simp: indicator_eq_0_iff)
-  apply (subst integral_measure_pmf[where A="{a}"])
+  apply (subst integral_measure_pmf_real[where A="{a}"])
   apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
   done
 
@@ -658,7 +658,10 @@
 done
 
 lemma pmf_map_outside: "x \<notin> f ` set_pmf M \<Longrightarrow> pmf (map_pmf f M) x = 0"
-unfolding pmf_eq_0_set_pmf by simp
+  unfolding pmf_eq_0_set_pmf by simp
+
+lemma measurable_set_pmf[measurable]: "Measurable.pred (count_space UNIV) (\<lambda>x. x \<in> set_pmf M)"
+  by simp
 
 subsection \<open> PMFs as function \<close>
 
@@ -742,6 +745,107 @@
 lemma nn_integral_measure_pmf: "(\<integral>\<^sup>+ x. f x \<partial>measure_pmf p) = \<integral>\<^sup>+ x. ennreal (pmf p x) * f x \<partial>count_space UNIV"
 by(simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg)
 
+lemma integral_measure_pmf:
+  fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
+  assumes A: "finite A"
+  shows "(\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A) \<Longrightarrow> (LINT x|M. f x) = (\<Sum>a\<in>A. pmf M a *\<^sub>R f a)"
+  unfolding measure_pmf_eq_density
+  apply (simp add: integral_density)
+  apply (subst lebesgue_integral_count_space_finite_support)
+  apply (auto intro!: finite_subset[OF _ \<open>finite A\<close>] setsum.mono_neutral_left simp: pmf_eq_0_set_pmf)
+  done
+
+lemma continuous_on_LINT_pmf: -- \<open>This is dominated convergence!?\<close>
+  fixes f :: "'i \<Rightarrow> 'a::topological_space \<Rightarrow> 'b::{banach, second_countable_topology}"
+  assumes f: "\<And>i. i \<in> set_pmf M \<Longrightarrow> continuous_on A (f i)"
+    and bnd: "\<And>a i. a \<in> A \<Longrightarrow> i \<in> set_pmf M \<Longrightarrow> norm (f i a) \<le> B"
+  shows "continuous_on A (\<lambda>a. LINT i|M. f i a)"
+proof cases
+  assume "finite M" with f show ?thesis
+    using integral_measure_pmf[OF \<open>finite M\<close>]
+    by (subst integral_measure_pmf[OF \<open>finite M\<close>])
+       (auto intro!: continuous_on_setsum continuous_on_scaleR continuous_on_const)
+next
+  assume "infinite M"
+  let ?f = "\<lambda>i x. pmf (map_pmf (to_nat_on M) M) i *\<^sub>R f (from_nat_into M i) x"
+
+  show ?thesis
+  proof (rule uniform_limit_theorem)
+    show "\<forall>\<^sub>F n in sequentially. continuous_on A (\<lambda>a. \<Sum>i<n. ?f i a)"
+      by (intro always_eventually allI continuous_on_setsum continuous_on_scaleR continuous_on_const f
+                from_nat_into set_pmf_not_empty)
+    show "uniform_limit A (\<lambda>n a. \<Sum>i<n. ?f i a) (\<lambda>a. LINT i|M. f i a) sequentially"
+    proof (subst uniform_limit_cong[where g="\<lambda>n a. \<Sum>i<n. ?f i a"])
+      fix a assume "a \<in> A"
+      have 1: "(LINT i|M. f i a) = (LINT i|map_pmf (to_nat_on M) M. f (from_nat_into M i) a)"
+        by (auto intro!: integral_cong_AE AE_pmfI)
+      have 2: "\<dots> = (LINT i|count_space UNIV. pmf (map_pmf (to_nat_on M) M) i *\<^sub>R f (from_nat_into M i) a)"
+        by (simp add: measure_pmf_eq_density integral_density)
+      have "(\<lambda>n. ?f n a) sums (LINT i|M. f i a)"
+        unfolding 1 2
+      proof (intro sums_integral_count_space_nat)
+        have A: "integrable M (\<lambda>i. f i a)"
+          using \<open>a\<in>A\<close> by (auto intro!: measure_pmf.integrable_const_bound AE_pmfI bnd)
+        have "integrable (map_pmf (to_nat_on M) M) (\<lambda>i. f (from_nat_into M i) a)"
+          by (auto simp add: map_pmf_rep_eq integrable_distr_eq intro!: AE_pmfI integrable_cong_AE_imp[OF A])
+        then show "integrable (count_space UNIV) (\<lambda>n. ?f n a)"
+          by (simp add: measure_pmf_eq_density integrable_density)
+      qed
+      then show "(LINT i|M. f i a) = (\<Sum> n. ?f n a)"
+        by (simp add: sums_unique)
+    next
+      show "uniform_limit A (\<lambda>n a. \<Sum>i<n. ?f i a) (\<lambda>a. (\<Sum> n. ?f n a)) sequentially"
+      proof (rule weierstrass_m_test)
+        fix n a assume "a\<in>A"
+        then show "norm (?f n a) \<le> pmf (map_pmf (to_nat_on M) M) n * B"
+          using bnd by (auto intro!: mult_mono simp: from_nat_into set_pmf_not_empty)
+      next
+        have "integrable (map_pmf (to_nat_on M) M) (\<lambda>n. B)"
+          by auto
+        then show "summable (\<lambda>n. pmf (map_pmf (to_nat_on (set_pmf M)) M) n * B)"
+          by (simp add: measure_pmf_eq_density integrable_density integrable_count_space_nat_iff summable_rabs_cancel)
+      qed
+    qed simp
+  qed simp
+qed
+
+lemma continuous_on_LBINT:
+  fixes f :: "real \<Rightarrow> real"
+  assumes f: "\<And>b. a \<le> b \<Longrightarrow> set_integrable lborel {a..b} f"
+  shows "continuous_on UNIV (\<lambda>b. LBINT x:{a..b}. f x)"
+proof (subst set_borel_integral_eq_integral)
+  { fix b :: real assume "a \<le> b"
+    from f[OF this] have "continuous_on {a..b} (\<lambda>b. integral {a..b} f)"
+      by (intro indefinite_integral_continuous set_borel_integral_eq_integral) }
+  note * = this
+
+  have "continuous_on (\<Union>b\<in>{a..}. {a <..< b}) (\<lambda>b. integral {a..b} f)"
+  proof (intro continuous_on_open_UN)
+    show "b \<in> {a..} \<Longrightarrow> continuous_on {a<..<b} (\<lambda>b. integral {a..b} f)" for b
+      using *[of b] by (rule continuous_on_subset) auto
+  qed simp
+  also have "(\<Union>b\<in>{a..}. {a <..< b}) = {a <..}"
+    by (auto simp: lt_ex gt_ex less_imp_le) (simp add: Bex_def less_imp_le gt_ex cong: rev_conj_cong)
+  finally have "continuous_on {a+1 ..} (\<lambda>b. integral {a..b} f)"
+    by (rule continuous_on_subset) auto
+  moreover have "continuous_on {a..a+1} (\<lambda>b. integral {a..b} f)"
+    by (rule *) simp
+  moreover
+  have "x \<le> a \<Longrightarrow> {a..x} = (if a = x then {a} else {})" for x
+    by auto
+  then have "continuous_on {..a} (\<lambda>b. integral {a..b} f)"
+    by (subst continuous_on_cong[OF refl, where g="\<lambda>x. 0"]) (auto intro!: continuous_on_const)
+  ultimately have "continuous_on ({..a} \<union> {a..a+1} \<union> {a+1 ..}) (\<lambda>b. integral {a..b} f)"
+    by (intro continuous_on_closed_Un) auto
+  also have "{..a} \<union> {a..a+1} \<union> {a+1 ..} = UNIV"
+    by auto
+  finally show "continuous_on UNIV (\<lambda>b. integral {a..b} f)"
+    by auto
+next
+  show "set_integrable lborel {a..b} f" for b
+    using f by (cases "a \<le> b") auto
+qed
+
 locale pmf_as_function
 begin
 
--- a/src/HOL/Probability/Probability_Measure.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Probability/Probability_Measure.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -508,8 +508,6 @@
   "distributed M N X f \<longleftrightarrow>
   distr M N X = density N f \<and> f \<in> borel_measurable N \<and> X \<in> measurable M N"
 
-term distributed
-
 lemma
   assumes "distributed M N X f"
   shows distributed_distr_eq_density: "distr M N X = density N f"
--- a/src/HOL/Probability/Stream_Space.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Probability/Stream_Space.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -109,6 +109,10 @@
   shows "(\<lambda>x. stake n (f x) @- g x) \<in> measurable N (stream_space M)"
   using f by (induction n arbitrary: f) simp_all
 
+lemma measurable_case_stream_replace[measurable (raw)]:
+  "(\<lambda>x. f x (shd (g x)) (stl (g x))) \<in> measurable M N \<Longrightarrow> (\<lambda>x. case_stream (f x) (g x)) \<in> measurable M N"
+  unfolding stream.case_eq_if .
+
 lemma measurable_ev_at[measurable]:
   assumes [measurable]: "Measurable.pred (stream_space M) P"
   shows "Measurable.pred (stream_space M) (ev_at P n)"
@@ -442,4 +446,212 @@
     by (cases "xs = []") (auto simp: * space_stream_space del: in_listsD)
 qed (auto simp: * ae sets_M del: in_listsD intro!: streams_sets)
 
+primrec scylinder :: "'a set \<Rightarrow> 'a set list \<Rightarrow> 'a stream set"
+where
+  "scylinder S [] = streams S"
+| "scylinder S (A # As) = {\<omega>\<in>streams S. shd \<omega> \<in> A \<and> stl \<omega> \<in> scylinder S As}"
+
+lemma scylinder_streams: "scylinder S xs \<subseteq> streams S"
+  by (induction xs) auto
+
+lemma sets_scylinder: "(\<forall>x\<in>set xs. x \<in> sets S) \<Longrightarrow> scylinder (space S) xs \<in> sets (stream_space S)"
+  by (induction xs) (auto simp: space_stream_space[symmetric])
+
+lemma stream_space_eq_scylinder:
+  assumes P: "prob_space M" "prob_space N"
+  assumes "Int_stable G" and S: "sets S = sets (sigma (space S) G)"
+    and C: "countable C" "C \<subseteq> G" "\<Union>C = space S" and G: "G \<subseteq> Pow (space S)"
+  assumes sets_M: "sets M = sets (stream_space S)"
+  assumes sets_N: "sets N = sets (stream_space S)"
+  assumes *: "\<And>xs. xs \<noteq> [] \<Longrightarrow> xs \<in> lists G \<Longrightarrow> emeasure M (scylinder (space S) xs) = emeasure N (scylinder (space S) xs)"
+  shows "M = N"
+proof (rule measure_eqI_generator_eq)
+  interpret M: prob_space M by fact
+  interpret N: prob_space N by fact
+
+  let ?G = "scylinder (space S) ` lists G"
+  show sc_Pow: "?G \<subseteq> Pow (streams (space S))"
+    using scylinder_streams by auto
+
+  have "sets (stream_space S) = sets (sigma (streams (space S)) ?G)"
+    (is "?S = sets ?R")
+  proof (rule antisym)
+    let ?V = "\<lambda>i. vimage_algebra (streams (space S)) (\<lambda>s. s !! i) S"
+    show "?S \<subseteq> sets ?R"
+      unfolding sets_stream_space_eq
+    proof (safe intro!: sets_Sup_in_sets del: subsetI equalityI)
+      fix i :: nat
+      show "space (?V i) = space ?R"
+        using scylinder_streams by (subst space_measure_of) (auto simp: )
+      { fix A assume "A \<in> G"
+        then have "scylinder (space S) (replicate i (space S) @ [A]) = (\<lambda>s. s !! i) -` A \<inter> streams (space S)"
+          by (induction i) (auto simp add: streams_shd streams_stl cong: conj_cong)
+        also have "scylinder (space S) (replicate i (space S) @ [A]) = (\<Union>xs\<in>{xs\<in>lists C. length xs = i}. scylinder (space S) (xs @ [A]))"
+          apply (induction i)
+          apply auto []
+          apply (simp add: length_Suc_conv set_eq_iff ex_simps(1,2)[symmetric] cong: conj_cong del: ex_simps(1,2))
+          apply rule
+          subgoal for i x
+            apply (cases x)
+            apply (subst (2) C(3)[symmetric])
+            apply (simp del: ex_simps(1,2) add: ex_simps(1,2)[symmetric] ac_simps Bex_def)
+            apply auto
+            done
+          done
+        finally have "(\<lambda>s. s !! i) -` A \<inter> streams (space S) = (\<Union>xs\<in>{xs\<in>lists C. length xs = i}. scylinder (space S) (xs @ [A]))"
+          ..
+        also have "\<dots> \<in> ?R"
+          using C(2) \<open>A\<in>G\<close>
+          by (intro sets.countable_UN' countable_Collect countable_lists C)
+             (auto intro!: in_measure_of[OF sc_Pow] imageI)
+        finally have "(\<lambda>s. s !! i) -` A \<inter> streams (space S) \<in> ?R" . }
+      then show "sets (?V i) \<subseteq> ?R"
+        apply (subst vimage_algebra_cong[OF refl refl S])
+        apply (subst vimage_algebra_sigma[OF G])
+        apply (simp add: streams_iff_snth) []
+        apply (subst sigma_le_sets)
+        apply auto
+        done
+    qed
+    have "G \<subseteq> sets S"
+      unfolding S using G by auto
+    with C(2) show "sets ?R \<subseteq> ?S"
+      unfolding sigma_le_sets[OF sc_Pow] by (auto intro!: sets_scylinder)
+  qed
+  then show "sets M = sigma_sets (streams (space S)) (scylinder (space S) ` lists G)"
+    "sets N = sigma_sets (streams (space S)) (scylinder (space S) ` lists G)"
+    unfolding sets_M sets_N by (simp_all add: sc_Pow)
+
+  show "Int_stable ?G"
+  proof (rule Int_stableI_image)
+    fix xs ys assume "xs \<in> lists G" "ys \<in> lists G"
+    then show "\<exists>zs\<in>lists G. scylinder (space S) xs \<inter> scylinder (space S) ys = scylinder (space S) zs"
+    proof (induction xs arbitrary: ys)
+      case Nil then show ?case
+        by (auto simp add: Int_absorb1 scylinder_streams)
+    next
+      case xs: (Cons x xs)
+      show ?case
+      proof (cases ys)
+        case Nil with xs.hyps show ?thesis
+          by (auto simp add: Int_absorb2 scylinder_streams intro!: bexI[of _ "x#xs"])
+      next
+        case ys: (Cons y ys')
+        with xs.IH[of ys'] xs.prems obtain zs where
+          "zs \<in> lists G" and eq: "scylinder (space S) xs \<inter> scylinder (space S) ys' = scylinder (space S) zs"
+          by auto
+        show ?thesis
+        proof (intro bexI[of _ "(x \<inter> y)#zs"])
+          show "x \<inter> y # zs \<in> lists G"
+            using \<open>zs\<in>lists G\<close> \<open>x\<in>G\<close> \<open>ys\<in>lists G\<close> ys \<open>Int_stable G\<close>[THEN Int_stableD, of x y] by auto
+          show "scylinder (space S) (x # xs) \<inter> scylinder (space S) ys = scylinder (space S) (x \<inter> y # zs)"
+            by (auto simp add: eq[symmetric] ys)
+        qed
+      qed
+    qed
+  qed
+
+  show "range (\<lambda>_::nat. streams (space S)) \<subseteq> scylinder (space S) ` lists G"
+    "(\<Union>i. streams (space S)) = streams (space S)"
+    "emeasure M (streams (space S)) \<noteq> \<infinity>"
+    by (auto intro!: image_eqI[of _ _ "[]"])
+
+  fix X assume "X \<in> scylinder (space S) ` lists G"
+  then obtain xs where xs: "xs \<in> lists G" and eq: "X = scylinder (space S) xs"
+    by auto
+  then show "emeasure M X = emeasure N X"
+  proof (cases "xs = []")
+    assume "xs = []" then show ?thesis
+      unfolding eq
+      using sets_M[THEN sets_eq_imp_space_eq] sets_N[THEN sets_eq_imp_space_eq]
+         M.emeasure_space_1 N.emeasure_space_1
+      by (simp add: space_stream_space[symmetric])
+  next
+    assume "xs \<noteq> []" with xs show ?thesis
+      unfolding eq by (intro *)
+  qed
+qed
+
+lemma stream_space_coinduct:
+  fixes R :: "'a stream measure \<Rightarrow> 'a stream measure \<Rightarrow> bool"
+  assumes "R A B"
+  assumes R: "\<And>A B. R A B \<Longrightarrow> \<exists>K\<in>space (prob_algebra M).
+    \<exists>A'\<in>M \<rightarrow>\<^sub>M prob_algebra (stream_space M). \<exists>B'\<in>M \<rightarrow>\<^sub>M prob_algebra (stream_space M).
+    (AE y in K. R (A' y) (B' y) \<or> A' y = B' y) \<and>
+    A = do { y \<leftarrow> K; \<omega> \<leftarrow> A' y; return (stream_space M) (y ## \<omega>) } \<and>
+    B = do { y \<leftarrow> K; \<omega> \<leftarrow> B' y; return (stream_space M) (y ## \<omega>) }"
+  shows "A = B"
+proof (rule stream_space_eq_scylinder)
+  let ?step = "\<lambda>K L. do { y \<leftarrow> K; \<omega> \<leftarrow> L y; return (stream_space M) (y ## \<omega>) }"
+  { fix K A A' assume K: "K \<in> space (prob_algebra M)"
+      and A'[measurable]: "A' \<in> M \<rightarrow>\<^sub>M prob_algebra (stream_space M)" and A_eq: "A = ?step K A'"
+    have ps: "prob_space A"
+      unfolding A_eq by (rule prob_space_bind'[OF K]) measurable
+    have "sets A = sets (stream_space M)"
+      unfolding A_eq by (rule sets_bind'[OF K]) measurable
+    note ps this }
+  note ** = this
+
+  { fix A B assume "R A B"
+    obtain K A' B' where K: "K \<in> space (prob_algebra M)"
+      and A': "A' \<in> M \<rightarrow>\<^sub>M prob_algebra (stream_space M)" "A = ?step K A'"
+      and B': "B' \<in> M \<rightarrow>\<^sub>M prob_algebra (stream_space M)" "B = ?step K B'"
+      using R[OF \<open>R A B\<close>] by blast
+    have "prob_space A" "prob_space B" "sets A = sets (stream_space M)" "sets B = sets (stream_space M)"
+      using **[OF K A'] **[OF K B'] by auto }
+  note R_D = this
+
+  show "prob_space A" "prob_space B" "sets A = sets (stream_space M)" "sets B = sets (stream_space M)"
+    using R_D[OF \<open>R A B\<close>] by auto
+
+  show "Int_stable (sets M)" "sets M = sets (sigma (space M) (sets M))" "countable {space M}"
+    "{space M} \<subseteq> sets M" "\<Union>{space M} = space M" "sets M \<subseteq> Pow (space M)"
+    using sets.space_closed[of M] by (auto simp: Int_stable_def)
+
+  { fix A As L K assume K[measurable]: "K \<in> space (prob_algebra M)" and A: "A \<in> sets M" "As \<in> lists (sets M)"
+      and L[measurable]: "L \<in> M \<rightarrow>\<^sub>M prob_algebra (stream_space M)"
+    from A have [measurable]: "\<forall>x\<in>set (A # As). x \<in> sets M" "\<forall>x\<in>set As. x \<in> sets M"
+      by auto
+    have [simp]: "space K = space M" "sets K = sets M"
+      using K by (auto simp: space_prob_algebra intro!: sets_eq_imp_space_eq)
+    have [simp]: "x \<in> space M \<Longrightarrow> sets (L x) = sets (stream_space M)" for x
+      using measurable_space[OF L] by (auto simp: space_prob_algebra)
+    note sets_scylinder[measurable]
+    have *: "indicator (scylinder (space M) (A # As)) (x ## \<omega>) =
+        (indicator A x * indicator (scylinder (space M) As) \<omega> :: ennreal)" for \<omega> x
+      using scylinder_streams[of "space M" As] \<open>A \<in> sets M\<close>[THEN sets.sets_into_space]
+      by (auto split: split_indicator)
+    have "emeasure (?step K L) (scylinder (space M) (A # As)) = (\<integral>\<^sup>+y. L y (scylinder (space M) As) * indicator A y \<partial>K)"
+      apply (subst emeasure_bind_prob_algebra[OF K])
+      apply measurable
+      apply (rule nn_integral_cong)
+      apply (subst emeasure_bind_prob_algebra[OF L[THEN measurable_space]])
+      apply (simp_all add: ac_simps * nn_integral_cmult_indicator del: scylinder.simps)
+      apply measurable
+      done }
+  note emeasure_step = this
+
+  fix Xs assume "Xs \<in> lists (sets M)"
+  from this \<open>R A B\<close> show "emeasure A (scylinder (space M) Xs) = emeasure B (scylinder (space M) Xs)"
+  proof (induction Xs arbitrary: A B)
+    case (Cons X Xs)
+    obtain K A' B' where K: "K \<in> space (prob_algebra M)"
+      and A'[measurable]: "A' \<in> M \<rightarrow>\<^sub>M prob_algebra (stream_space M)" and A: "A = ?step K A'"
+      and B'[measurable]: "B' \<in> M \<rightarrow>\<^sub>M prob_algebra (stream_space M)" and B: "B = ?step K B'"
+      and AE_R: "AE x in K. R (A' x) (B' x) \<or> A' x = B' x"
+      using R[OF \<open>R A B\<close>] by blast
+
+    show ?case
+      unfolding A B emeasure_step[OF K Cons.hyps A'] emeasure_step[OF K Cons.hyps B']
+      apply (rule nn_integral_cong_AE)
+      using AE_R by eventually_elim (auto simp add: Cons.IH)
+  next
+    case Nil
+    note R_D[OF this]
+    from this(1,2)[THEN prob_space.emeasure_space_1] this(3,4)[THEN sets_eq_imp_space_eq]
+    show ?case
+      by (simp add: space_stream_space)
+  qed
+qed
+
 end
--- a/src/HOL/Topological_Spaces.thy	Mon Oct 03 14:09:26 2016 +0100
+++ b/src/HOL/Topological_Spaces.thy	Fri Sep 30 16:08:38 2016 +0200
@@ -699,7 +699,7 @@
   assumes "filterlim f (nhds c) F"
   assumes "eventually (\<lambda>x. f x \<in> A - {c}) F"
   shows   "filterlim f (at c within A) F"
-  using assms by (simp add: filterlim_at) 
+  using assms by (simp add: filterlim_at)
 
 lemma filterlim_atI:
   assumes "filterlim f (nhds c) F"
@@ -1644,6 +1644,10 @@
   unfolding continuous_on_def
   by (intro ball_cong filterlim_cong) (auto simp: eventually_at_filter)
 
+lemma continuous_on_strong_cong:
+  "s = t \<Longrightarrow> (\<And>x. x \<in> t =simp=> f x = g x) \<Longrightarrow> continuous_on s f \<longleftrightarrow> continuous_on t g"
+  unfolding simp_implies_def by (rule continuous_on_cong)
+
 lemma continuous_on_topological:
   "continuous_on s f \<longleftrightarrow>
     (\<forall>x\<in>s. \<forall>B. open B \<longrightarrow> f x \<in> B \<longrightarrow> (\<exists>A. open A \<and> x \<in> A \<and> (\<forall>y\<in>s. y \<in> A \<longrightarrow> f y \<in> B)))"