src/HOL/Probability/Giry_Monad.thy
changeset 59000 6eb0725503fc
parent 58608 5b7f0b5da884
child 59002 2c8b2fb54b88
equal deleted inserted replaced
58997:fc571ebb04e1 59000:6eb0725503fc
    42   with f show "emeasure (distr M M' f) (space (distr M M' f)) \<le> 1"
    42   with f show "emeasure (distr M M' f) (space (distr M M' f)) \<le> 1"
    43     by (auto simp: emeasure_distr emeasure_space_le_1)
    43     by (auto simp: emeasure_distr emeasure_space_le_1)
    44   show "space (distr M M' f) \<noteq> {}" by (simp add: assms)
    44   show "space (distr M M' f) \<noteq> {}" by (simp add: assms)
    45 qed
    45 qed
    46 
    46 
    47 lemma (in subprob_space) subprob_measure_le_1: "emeasure M X \<le> 1"
    47 lemma (in subprob_space) subprob_emeasure_le_1: "emeasure M X \<le> 1"
    48   by (rule order.trans[OF emeasure_space emeasure_space_le_1])
    48   by (rule order.trans[OF emeasure_space emeasure_space_le_1])
       
    49 
       
    50 lemma (in subprob_space) subprob_measure_le_1: "measure M X \<le> 1"
       
    51   using subprob_emeasure_le_1[of X] by (simp add: emeasure_eq_measure)
    49 
    52 
    50 locale pair_subprob_space = 
    53 locale pair_subprob_space = 
    51   pair_sigma_finite M1 M2 + M1: subprob_space M1 + M2: subprob_space M2 for M1 M2
    54   pair_sigma_finite M1 M2 + M1: subprob_space M1 + M2: subprob_space M2 for M1 M2
    52 
    55 
    53 sublocale pair_subprob_space \<subseteq> P: subprob_space "M1 \<Otimes>\<^sub>M M2"
    56 sublocale pair_subprob_space \<subseteq> P: subprob_space "M1 \<Otimes>\<^sub>M M2"
    73 
    76 
    74 lemma measurable_emeasure_subprob_algebra[measurable]: 
    77 lemma measurable_emeasure_subprob_algebra[measurable]: 
    75   "a \<in> sets A \<Longrightarrow> (\<lambda>M. emeasure M a) \<in> borel_measurable (subprob_algebra A)"
    78   "a \<in> sets A \<Longrightarrow> (\<lambda>M. emeasure M a) \<in> borel_measurable (subprob_algebra A)"
    76   by (auto intro!: measurable_Sup_sigma1 measurable_vimage_algebra1 simp: subprob_algebra_def)
    79   by (auto intro!: measurable_Sup_sigma1 measurable_vimage_algebra1 simp: subprob_algebra_def)
    77 
    80 
       
    81 lemma subprob_measurableD:
       
    82   assumes N: "N \<in> measurable M (subprob_algebra S)" and x: "x \<in> space M"
       
    83   shows "space (N x) = space S"
       
    84     and "sets (N x) = sets S"
       
    85     and "measurable (N x) K = measurable S K"
       
    86     and "measurable K (N x) = measurable K S"
       
    87   using measurable_space[OF N x]
       
    88   by (auto simp: space_subprob_algebra intro!: measurable_cong_sets dest: sets_eq_imp_space_eq)
       
    89 
    78 context
    90 context
    79   fixes K M N assumes K: "K \<in> measurable M (subprob_algebra N)"
    91   fixes K M N assumes K: "K \<in> measurable M (subprob_algebra N)"
    80 begin
    92 begin
    81 
    93 
    82 lemma subprob_space_kernel: "a \<in> space M \<Longrightarrow> subprob_space (K a)"
    94 lemma subprob_space_kernel: "a \<in> space M \<Longrightarrow> subprob_space (K a)"
   109   assume "space N = {}"
   121   assume "space N = {}"
   110   hence "sets N = {{}}" by (simp add: space_empty_iff)
   122   hence "sets N = {{}}" by (simp add: space_empty_iff)
   111   moreover have "\<And>M. subprob_space M \<Longrightarrow> sets M \<noteq> {{}}"
   123   moreover have "\<And>M. subprob_space M \<Longrightarrow> sets M \<noteq> {{}}"
   112     by (simp add: subprob_space.subprob_not_empty space_empty_iff[symmetric])
   124     by (simp add: subprob_space.subprob_not_empty space_empty_iff[symmetric])
   113   ultimately show "space (subprob_algebra N) = {}" by (auto simp: space_subprob_algebra)
   125   ultimately show "space (subprob_algebra N) = {}" by (auto simp: space_subprob_algebra)
       
   126 qed
       
   127 
       
   128 lemma nn_integral_measurable_subprob_algebra:
       
   129   assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
       
   130   shows "(\<lambda>M. integral\<^sup>N M f) \<in> borel_measurable (subprob_algebra N)" (is "_ \<in> ?B")
       
   131   using f
       
   132 proof induct
       
   133   case (cong f g)
       
   134   moreover have "(\<lambda>M'. \<integral>\<^sup>+M''. f M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. \<integral>\<^sup>+M''. g M'' \<partial>M') \<in> ?B"
       
   135     by (intro measurable_cong nn_integral_cong cong)
       
   136        (auto simp: space_subprob_algebra dest!: sets_eq_imp_space_eq)
       
   137   ultimately show ?case by simp
       
   138 next
       
   139   case (set B)
       
   140   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. indicator B M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. emeasure M' B) \<in> ?B"
       
   141     by (intro measurable_cong nn_integral_indicator) (simp add: space_subprob_algebra)
       
   142   ultimately show ?case
       
   143     by (simp add: measurable_emeasure_subprob_algebra)
       
   144 next
       
   145   case (mult f c)
       
   146   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. c * f M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. c * \<integral>\<^sup>+M''. f M'' \<partial>M') \<in> ?B"
       
   147     by (intro measurable_cong nn_integral_cmult) (simp add: space_subprob_algebra)
       
   148   ultimately show ?case
       
   149     by simp
       
   150 next
       
   151   case (add f g)
       
   152   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. f M'' + g M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. (\<integral>\<^sup>+M''. f M'' \<partial>M') + (\<integral>\<^sup>+M''. g M'' \<partial>M')) \<in> ?B"
       
   153     by (intro measurable_cong nn_integral_add) (simp_all add: space_subprob_algebra)
       
   154   ultimately show ?case
       
   155     by (simp add: ac_simps)
       
   156 next
       
   157   case (seq F)
       
   158   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. (SUP i. F i) M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. SUP i. (\<integral>\<^sup>+M''. F i M'' \<partial>M')) \<in> ?B"
       
   159     unfolding SUP_apply
       
   160     by (intro measurable_cong nn_integral_monotone_convergence_SUP) (simp_all add: space_subprob_algebra)
       
   161   ultimately show ?case
       
   162     by (simp add: ac_simps)
   114 qed
   163 qed
   115 
   164 
   116 lemma measurable_distr:
   165 lemma measurable_distr:
   117   assumes [measurable]: "f \<in> measurable M N"
   166   assumes [measurable]: "f \<in> measurable M N"
   118   shows "(\<lambda>M'. distr M' N f) \<in> measurable (subprob_algebra M) (subprob_algebra N)"
   167   shows "(\<lambda>M'. distr M' N f) \<in> measurable (subprob_algebra M) (subprob_algebra N)"
   128     also have "\<dots>"
   177     also have "\<dots>"
   129       using A by (intro measurable_emeasure_subprob_algebra) simp
   178       using A by (intro measurable_emeasure_subprob_algebra) simp
   130     finally show "(\<lambda>M'. emeasure (distr M' N f) A) \<in> borel_measurable (subprob_algebra M)" .
   179     finally show "(\<lambda>M'. emeasure (distr M' N f) A) \<in> borel_measurable (subprob_algebra M)" .
   131   qed (auto intro!: subprob_space.subprob_space_distr simp: space_subprob_algebra not_empty)
   180   qed (auto intro!: subprob_space.subprob_space_distr simp: space_subprob_algebra not_empty)
   132 qed (insert assms, auto simp: measurable_empty_iff space_subprob_algebra_empty_iff)
   181 qed (insert assms, auto simp: measurable_empty_iff space_subprob_algebra_empty_iff)
   133 
       
   134 section {* Properties of return *}
       
   135 
       
   136 definition return :: "'a measure \<Rightarrow> 'a \<Rightarrow> 'a measure" where
       
   137   "return R x = measure_of (space R) (sets R) (\<lambda>A. indicator A x)"
       
   138 
       
   139 lemma space_return[simp]: "space (return M x) = space M"
       
   140   by (simp add: return_def)
       
   141 
       
   142 lemma sets_return[simp]: "sets (return M x) = sets M"
       
   143   by (simp add: return_def)
       
   144 
       
   145 lemma measurable_return1[simp]: "measurable (return N x) L = measurable N L"
       
   146   by (simp cong: measurable_cong_sets) 
       
   147 
       
   148 lemma measurable_return2[simp]: "measurable L (return N x) = measurable L N"
       
   149   by (simp cong: measurable_cong_sets) 
       
   150 
       
   151 lemma emeasure_return[simp]:
       
   152   assumes "A \<in> sets M"
       
   153   shows "emeasure (return M x) A = indicator A x"
       
   154 proof (rule emeasure_measure_of[OF return_def])
       
   155   show "sets M \<subseteq> Pow (space M)" by (rule sets.space_closed)
       
   156   show "positive (sets (return M x)) (\<lambda>A. indicator A x)" by (simp add: positive_def)
       
   157   from assms show "A \<in> sets (return M x)" unfolding return_def by simp
       
   158   show "countably_additive (sets (return M x)) (\<lambda>A. indicator A x)"
       
   159     by (auto intro: countably_additiveI simp: suminf_indicator)
       
   160 qed
       
   161 
       
   162 lemma prob_space_return: "x \<in> space M \<Longrightarrow> prob_space (return M x)"
       
   163   by rule simp
       
   164 
       
   165 lemma subprob_space_return: "x \<in> space M \<Longrightarrow> subprob_space (return M x)"
       
   166   by (intro prob_space_return prob_space_imp_subprob_space)
       
   167 
       
   168 lemma AE_return:
       
   169   assumes [simp]: "x \<in> space M" and [measurable]: "Measurable.pred M P"
       
   170   shows "(AE y in return M x. P y) \<longleftrightarrow> P x"
       
   171 proof -
       
   172   have "(AE y in return M x. y \<notin> {x\<in>space M. \<not> P x}) \<longleftrightarrow> P x"
       
   173     by (subst AE_iff_null_sets[symmetric]) (simp_all add: null_sets_def split: split_indicator)
       
   174   also have "(AE y in return M x. y \<notin> {x\<in>space M. \<not> P x}) \<longleftrightarrow> (AE y in return M x. P y)"
       
   175     by (rule AE_cong) auto
       
   176   finally show ?thesis .
       
   177 qed
       
   178   
       
   179 lemma nn_integral_return:
       
   180   assumes "g x \<ge> 0" "x \<in> space M" "g \<in> borel_measurable M"
       
   181   shows "(\<integral>\<^sup>+ a. g a \<partial>return M x) = g x"
       
   182 proof-
       
   183   interpret prob_space "return M x" by (rule prob_space_return[OF `x \<in> space M`])
       
   184   have "(\<integral>\<^sup>+ a. g a \<partial>return M x) = (\<integral>\<^sup>+ a. g x \<partial>return M x)" using assms
       
   185     by (intro nn_integral_cong_AE) (auto simp: AE_return)
       
   186   also have "... = g x"
       
   187     using nn_integral_const[OF `g x \<ge> 0`, of "return M x"] emeasure_space_1 by simp
       
   188   finally show ?thesis .
       
   189 qed
       
   190 
       
   191 lemma return_measurable: "return N \<in> measurable N (subprob_algebra N)"
       
   192   by (rule measurable_subprob_algebra) (auto simp: subprob_space_return)
       
   193 
       
   194 lemma distr_return:
       
   195   assumes "f \<in> measurable M N" and "x \<in> space M"
       
   196   shows "distr (return M x) N f = return N (f x)"
       
   197   using assms by (intro measure_eqI) (simp_all add: indicator_def emeasure_distr)
       
   198 
       
   199 definition "select_sets M = (SOME N. sets M = sets (subprob_algebra N))"
       
   200 
       
   201 lemma select_sets1:
       
   202   "sets M = sets (subprob_algebra N) \<Longrightarrow> sets M = sets (subprob_algebra (select_sets M))"
       
   203   unfolding select_sets_def by (rule someI)
       
   204 
       
   205 lemma sets_select_sets[simp]:
       
   206   assumes sets: "sets M = sets (subprob_algebra N)"
       
   207   shows "sets (select_sets M) = sets N"
       
   208   unfolding select_sets_def
       
   209 proof (rule someI2)
       
   210   show "sets M = sets (subprob_algebra N)"
       
   211     by fact
       
   212 next
       
   213   fix L assume "sets M = sets (subprob_algebra L)"
       
   214   with sets have eq: "space (subprob_algebra N) = space (subprob_algebra L)"
       
   215     by (intro sets_eq_imp_space_eq) simp
       
   216   show "sets L = sets N"
       
   217   proof cases
       
   218     assume "space (subprob_algebra N) = {}"
       
   219     with space_subprob_algebra_empty_iff[of N] space_subprob_algebra_empty_iff[of L]
       
   220     show ?thesis
       
   221       by (simp add: eq space_empty_iff)
       
   222   next
       
   223     assume "space (subprob_algebra N) \<noteq> {}"
       
   224     with eq show ?thesis
       
   225       by (fastforce simp add: space_subprob_algebra)
       
   226   qed
       
   227 qed
       
   228 
       
   229 lemma space_select_sets[simp]:
       
   230   "sets M = sets (subprob_algebra N) \<Longrightarrow> space (select_sets M) = space N"
       
   231   by (intro sets_eq_imp_space_eq sets_select_sets)
       
   232 
       
   233 section {* Join *}
       
   234 
       
   235 definition join :: "'a measure measure \<Rightarrow> 'a measure" where
       
   236   "join M = measure_of (space (select_sets M)) (sets (select_sets M)) (\<lambda>B. \<integral>\<^sup>+ M'. emeasure M' B \<partial>M)"
       
   237 
       
   238 lemma
       
   239   shows space_join[simp]: "space (join M) = space (select_sets M)"
       
   240     and sets_join[simp]: "sets (join M) = sets (select_sets M)"
       
   241   by (simp_all add: join_def)
       
   242 
       
   243 lemma emeasure_join:
       
   244   assumes M[simp]: "sets M = sets (subprob_algebra N)" and A: "A \<in> sets N"
       
   245   shows "emeasure (join M) A = (\<integral>\<^sup>+ M'. emeasure M' A \<partial>M)"
       
   246 proof (rule emeasure_measure_of[OF join_def])
       
   247   have eq: "borel_measurable M = borel_measurable (subprob_algebra N)"
       
   248     by auto
       
   249   show "countably_additive (sets (join M)) (\<lambda>B. \<integral>\<^sup>+ M'. emeasure M' B \<partial>M)"
       
   250   proof (rule countably_additiveI)
       
   251     fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets (join M)" "disjoint_family A"
       
   252     have "(\<Sum>i. \<integral>\<^sup>+ M'. emeasure M' (A i) \<partial>M) = (\<integral>\<^sup>+M'. (\<Sum>i. emeasure M' (A i)) \<partial>M)"
       
   253       using A by (subst nn_integral_suminf) (auto simp: measurable_emeasure_subprob_algebra eq)
       
   254     also have "\<dots> = (\<integral>\<^sup>+M'. emeasure M' (\<Union>i. A i) \<partial>M)"
       
   255     proof (rule nn_integral_cong)
       
   256       fix M' assume "M' \<in> space M"
       
   257       then show "(\<Sum>i. emeasure M' (A i)) = emeasure M' (\<Union>i. A i)"
       
   258         using A sets_eq_imp_space_eq[OF M] by (simp add: suminf_emeasure space_subprob_algebra)
       
   259     qed
       
   260     finally show "(\<Sum>i. \<integral>\<^sup>+M'. emeasure M' (A i) \<partial>M) = (\<integral>\<^sup>+M'. emeasure M' (\<Union>i. A i) \<partial>M)" .
       
   261   qed
       
   262 qed (auto simp: A sets.space_closed positive_def nn_integral_nonneg)
       
   263 
       
   264 lemma nn_integral_measurable_subprob_algebra:
       
   265   assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
       
   266   shows "(\<lambda>M. integral\<^sup>N M f) \<in> borel_measurable (subprob_algebra N)" (is "_ \<in> ?B")
       
   267   using f
       
   268 proof induct
       
   269   case (cong f g)
       
   270   moreover have "(\<lambda>M'. \<integral>\<^sup>+M''. f M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. \<integral>\<^sup>+M''. g M'' \<partial>M') \<in> ?B"
       
   271     by (intro measurable_cong nn_integral_cong cong)
       
   272        (auto simp: space_subprob_algebra dest!: sets_eq_imp_space_eq)
       
   273   ultimately show ?case by simp
       
   274 next
       
   275   case (set B)
       
   276   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. indicator B M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. emeasure M' B) \<in> ?B"
       
   277     by (intro measurable_cong nn_integral_indicator) (simp add: space_subprob_algebra)
       
   278   ultimately show ?case
       
   279     by (simp add: measurable_emeasure_subprob_algebra)
       
   280 next
       
   281   case (mult f c)
       
   282   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. c * f M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. c * \<integral>\<^sup>+M''. f M'' \<partial>M') \<in> ?B"
       
   283     by (intro measurable_cong nn_integral_cmult) (simp add: space_subprob_algebra)
       
   284   ultimately show ?case
       
   285     by simp
       
   286 next
       
   287   case (add f g)
       
   288   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. f M'' + g M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. (\<integral>\<^sup>+M''. f M'' \<partial>M') + (\<integral>\<^sup>+M''. g M'' \<partial>M')) \<in> ?B"
       
   289     by (intro measurable_cong nn_integral_add) (simp_all add: space_subprob_algebra)
       
   290   ultimately show ?case
       
   291     by (simp add: ac_simps)
       
   292 next
       
   293   case (seq F)
       
   294   moreover then have "(\<lambda>M'. \<integral>\<^sup>+M''. (SUP i. F i) M'' \<partial>M') \<in> ?B \<longleftrightarrow> (\<lambda>M'. SUP i. (\<integral>\<^sup>+M''. F i M'' \<partial>M')) \<in> ?B"
       
   295     unfolding SUP_apply
       
   296     by (intro measurable_cong nn_integral_monotone_convergence_SUP) (simp_all add: space_subprob_algebra)
       
   297   ultimately show ?case
       
   298     by (simp add: ac_simps)
       
   299 qed
       
   300 
       
   301 
       
   302 lemma measurable_join:
       
   303   "join \<in> measurable (subprob_algebra (subprob_algebra N)) (subprob_algebra N)"
       
   304 proof (cases "space N \<noteq> {}", rule measurable_subprob_algebra)
       
   305   fix A assume "A \<in> sets N"
       
   306   let ?B = "borel_measurable (subprob_algebra (subprob_algebra N))"
       
   307   have "(\<lambda>M'. emeasure (join M') A) \<in> ?B \<longleftrightarrow> (\<lambda>M'. (\<integral>\<^sup>+ M''. emeasure M'' A \<partial>M')) \<in> ?B"
       
   308   proof (rule measurable_cong)
       
   309     fix M' assume "M' \<in> space (subprob_algebra (subprob_algebra N))"
       
   310     then show "emeasure (join M') A = (\<integral>\<^sup>+ M''. emeasure M'' A \<partial>M')"
       
   311       by (intro emeasure_join) (auto simp: space_subprob_algebra `A\<in>sets N`)
       
   312   qed
       
   313   also have "(\<lambda>M'. \<integral>\<^sup>+M''. emeasure M'' A \<partial>M') \<in> ?B"
       
   314     using measurable_emeasure_subprob_algebra[OF `A\<in>sets N`] emeasure_nonneg[of _ A]
       
   315     by (rule nn_integral_measurable_subprob_algebra)
       
   316   finally show "(\<lambda>M'. emeasure (join M') A) \<in> borel_measurable (subprob_algebra (subprob_algebra N))" .
       
   317 next
       
   318   assume [simp]: "space N \<noteq> {}"
       
   319   fix M assume M: "M \<in> space (subprob_algebra (subprob_algebra N))"
       
   320   then have "(\<integral>\<^sup>+M'. emeasure M' (space N) \<partial>M) \<le> (\<integral>\<^sup>+M'. 1 \<partial>M)"
       
   321     apply (intro nn_integral_mono)
       
   322     apply (auto simp: space_subprob_algebra 
       
   323                  dest!: sets_eq_imp_space_eq subprob_space.emeasure_space_le_1)
       
   324     done
       
   325   with M show "subprob_space (join M)"
       
   326     by (intro subprob_spaceI)
       
   327        (auto simp: emeasure_join space_subprob_algebra M assms dest: subprob_space.emeasure_space_le_1)
       
   328 next
       
   329   assume "\<not>(space N \<noteq> {})"
       
   330   thus ?thesis by (simp add: measurable_empty_iff space_subprob_algebra_empty_iff)
       
   331 qed (auto simp: space_subprob_algebra)
       
   332 
       
   333 lemma nn_integral_join:
       
   334   assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x" and M: "sets M = sets (subprob_algebra N)"
       
   335   shows "(\<integral>\<^sup>+x. f x \<partial>join M) = (\<integral>\<^sup>+M'. \<integral>\<^sup>+x. f x \<partial>M' \<partial>M)"
       
   336   using f
       
   337 proof induct
       
   338   case (cong f g)
       
   339   moreover have "integral\<^sup>N (join M) f = integral\<^sup>N (join M) g"
       
   340     by (intro nn_integral_cong cong) (simp add: M)
       
   341   moreover from M have "(\<integral>\<^sup>+ M'. integral\<^sup>N M' f \<partial>M) = (\<integral>\<^sup>+ M'. integral\<^sup>N M' g \<partial>M)"
       
   342     by (intro nn_integral_cong cong)
       
   343        (auto simp add: space_subprob_algebra dest!: sets_eq_imp_space_eq)
       
   344   ultimately show ?case
       
   345     by simp
       
   346 next
       
   347   case (set A)
       
   348   moreover with M have "(\<integral>\<^sup>+ M'. integral\<^sup>N M' (indicator A) \<partial>M) = (\<integral>\<^sup>+ M'. emeasure M' A \<partial>M)" 
       
   349     by (intro nn_integral_cong nn_integral_indicator)
       
   350        (auto simp: space_subprob_algebra dest!: sets_eq_imp_space_eq)
       
   351   ultimately show ?case
       
   352     using M by (simp add: emeasure_join)
       
   353 next
       
   354   case (mult f c)
       
   355   have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. c * f x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. c * \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M)"
       
   356     using mult M
       
   357     by (intro nn_integral_cong nn_integral_cmult)
       
   358        (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
       
   359   also have "\<dots> = c * (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M)"
       
   360     using nn_integral_measurable_subprob_algebra[OF mult(3,4)]
       
   361     by (intro nn_integral_cmult mult) (simp add: M)
       
   362   also have "\<dots> = c * (integral\<^sup>N (join M) f)"
       
   363     by (simp add: mult)
       
   364   also have "\<dots> = (\<integral>\<^sup>+ x. c * f x \<partial>join M)"
       
   365     using mult(2,3) by (intro nn_integral_cmult[symmetric] mult) (simp add: M)
       
   366   finally show ?case by simp
       
   367 next
       
   368   case (add f g)
       
   369   have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x + g x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. (\<integral>\<^sup>+ x. f x \<partial>M') + (\<integral>\<^sup>+ x. g x \<partial>M') \<partial>M)"
       
   370     using add M
       
   371     by (intro nn_integral_cong nn_integral_add)
       
   372        (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
       
   373   also have "\<dots> = (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M) + (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. g x \<partial>M' \<partial>M)"
       
   374     using nn_integral_measurable_subprob_algebra[OF add(1,2)]
       
   375     using nn_integral_measurable_subprob_algebra[OF add(5,6)]
       
   376     by (intro nn_integral_add add) (simp_all add: M nn_integral_nonneg)
       
   377   also have "\<dots> = (integral\<^sup>N (join M) f) + (integral\<^sup>N (join M) g)"
       
   378     by (simp add: add)
       
   379   also have "\<dots> = (\<integral>\<^sup>+ x. f x + g x \<partial>join M)"
       
   380     using add by (intro nn_integral_add[symmetric] add) (simp_all add: M)
       
   381   finally show ?case by (simp add: ac_simps)
       
   382 next
       
   383   case (seq F)
       
   384   have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. (SUP i. F i) x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. (SUP i. \<integral>\<^sup>+ x. F i x \<partial>M') \<partial>M)"
       
   385     using seq M unfolding SUP_apply
       
   386     by (intro nn_integral_cong nn_integral_monotone_convergence_SUP)
       
   387        (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
       
   388   also have "\<dots> = (SUP i. \<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. F i x \<partial>M' \<partial>M)"
       
   389     using nn_integral_measurable_subprob_algebra[OF seq(1,2)] seq
       
   390     by (intro nn_integral_monotone_convergence_SUP)
       
   391        (simp_all add: M nn_integral_nonneg incseq_nn_integral incseq_def le_fun_def nn_integral_mono )
       
   392   also have "\<dots> = (SUP i. integral\<^sup>N (join M) (F i))"
       
   393     by (simp add: seq)
       
   394   also have "\<dots> = (\<integral>\<^sup>+ x. (SUP i. F i x) \<partial>join M)"
       
   395     using seq by (intro nn_integral_monotone_convergence_SUP[symmetric] seq) (simp_all add: M)
       
   396   finally show ?case by (simp add: ac_simps)
       
   397 qed
       
   398 
       
   399 lemma join_assoc:
       
   400   assumes M: "sets M = sets (subprob_algebra (subprob_algebra N))"
       
   401   shows "join (distr M (subprob_algebra N) join) = join (join M)"
       
   402 proof (rule measure_eqI)
       
   403   fix A assume "A \<in> sets (join (distr M (subprob_algebra N) join))"
       
   404   then have A: "A \<in> sets N" by simp
       
   405   show "emeasure (join (distr M (subprob_algebra N) join)) A = emeasure (join (join M)) A"
       
   406     using measurable_join[of N]
       
   407     by (auto simp: M A nn_integral_distr emeasure_join measurable_emeasure_subprob_algebra emeasure_nonneg
       
   408                    sets_eq_imp_space_eq[OF M] space_subprob_algebra nn_integral_join[OF _ _ M]
       
   409              intro!: nn_integral_cong emeasure_join cong: measurable_cong)
       
   410 qed (simp add: M)
       
   411 
       
   412 lemma join_return: 
       
   413   assumes "sets M = sets N" and "subprob_space M"
       
   414   shows "join (return (subprob_algebra N) M) = M"
       
   415   by (rule measure_eqI)
       
   416      (simp_all add: emeasure_join emeasure_nonneg space_subprob_algebra  
       
   417                     measurable_emeasure_subprob_algebra nn_integral_return assms)
       
   418 
       
   419 lemma join_return':
       
   420   assumes "sets N = sets M"
       
   421   shows "join (distr M (subprob_algebra N) (return N)) = M"
       
   422 apply (rule measure_eqI)
       
   423 apply (simp add: assms)
       
   424 apply (subgoal_tac "return N \<in> measurable M (subprob_algebra N)")
       
   425 apply (simp add: emeasure_join nn_integral_distr measurable_emeasure_subprob_algebra assms)
       
   426 apply (subst measurable_cong_sets, rule assms[symmetric], rule refl, rule return_measurable)
       
   427 done
       
   428 
       
   429 lemma join_distr_distr:
       
   430   fixes f :: "'a \<Rightarrow> 'b" and M :: "'a measure measure" and N :: "'b measure"
       
   431   assumes "sets M = sets (subprob_algebra R)" and "f \<in> measurable R N"
       
   432   shows "join (distr M (subprob_algebra N) (\<lambda>M. distr M N f)) = distr (join M) N f" (is "?r = ?l")
       
   433 proof (rule measure_eqI)
       
   434   fix A assume "A \<in> sets ?r"
       
   435   hence A_in_N: "A \<in> sets N" by simp
       
   436 
       
   437   from assms have "f \<in> measurable (join M) N" 
       
   438       by (simp cong: measurable_cong_sets)
       
   439   moreover from assms and A_in_N have "f-`A \<inter> space R \<in> sets R" 
       
   440       by (intro measurable_sets) simp_all
       
   441   ultimately have "emeasure (distr (join M) N f) A = \<integral>\<^sup>+M'. emeasure M' (f-`A \<inter> space R) \<partial>M"
       
   442       by (simp_all add: A_in_N emeasure_distr emeasure_join assms)
       
   443   
       
   444   also have "... = \<integral>\<^sup>+ x. emeasure (distr x N f) A \<partial>M" using A_in_N
       
   445   proof (intro nn_integral_cong, subst emeasure_distr)
       
   446     fix M' assume "M' \<in> space M"
       
   447     from assms have "space M = space (subprob_algebra R)"
       
   448         using sets_eq_imp_space_eq by blast
       
   449     with `M' \<in> space M` have [simp]: "sets M' = sets R" using space_subprob_algebra by blast
       
   450     show "f \<in> measurable M' N" by (simp cong: measurable_cong_sets add: assms)
       
   451     have "space M' = space R" by (rule sets_eq_imp_space_eq) simp
       
   452     thus "emeasure M' (f -` A \<inter> space R) = emeasure M' (f -` A \<inter> space M')" by simp
       
   453   qed
       
   454 
       
   455   also have "(\<lambda>M. distr M N f) \<in> measurable M (subprob_algebra N)"
       
   456       by (simp cong: measurable_cong_sets add: assms measurable_distr)
       
   457   hence "(\<integral>\<^sup>+ x. emeasure (distr x N f) A \<partial>M) = 
       
   458              emeasure (join (distr M (subprob_algebra N) (\<lambda>M. distr M N f))) A"
       
   459       by (simp_all add: emeasure_join assms A_in_N nn_integral_distr measurable_emeasure_subprob_algebra)
       
   460   finally show "emeasure ?r A = emeasure ?l A" ..
       
   461 qed simp
       
   462 
       
   463 definition bind :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b measure) \<Rightarrow> 'b measure" where
       
   464   "bind M f = (if space M = {} then count_space {} else
       
   465     join (distr M (subprob_algebra (f (SOME x. x \<in> space M))) f))"
       
   466 
       
   467 adhoc_overloading Monad_Syntax.bind bind
       
   468 
       
   469 lemma bind_empty: 
       
   470   "space M = {} \<Longrightarrow> bind M f = count_space {}"
       
   471   by (simp add: bind_def)
       
   472 
       
   473 lemma bind_nonempty:
       
   474   "space M \<noteq> {} \<Longrightarrow> bind M f = join (distr M (subprob_algebra (f (SOME x. x \<in> space M))) f)"
       
   475   by (simp add: bind_def)
       
   476 
       
   477 lemma sets_bind_empty: "sets M = {} \<Longrightarrow> sets (bind M f) = {{}}"
       
   478   by (auto simp: bind_def)
       
   479 
       
   480 lemma space_bind_empty: "space M = {} \<Longrightarrow> space (bind M f) = {}"
       
   481   by (simp add: bind_def)
       
   482 
       
   483 lemma sets_bind[simp]: 
       
   484   assumes "f \<in> measurable M (subprob_algebra N)" and "space M \<noteq> {}"
       
   485   shows "sets (bind M f) = sets N"
       
   486     using assms(2) by (force simp: bind_nonempty intro!: sets_kernel[OF assms(1) someI_ex])
       
   487 
       
   488 lemma space_bind[simp]: 
       
   489   assumes "f \<in> measurable M (subprob_algebra N)" and "space M \<noteq> {}"
       
   490   shows "space (bind M f) = space N"
       
   491     using assms by (intro sets_eq_imp_space_eq sets_bind)
       
   492 
       
   493 lemma bind_cong: 
       
   494   assumes "\<forall>x \<in> space M. f x = g x"
       
   495   shows "bind M f = bind M g"
       
   496 proof (cases "space M = {}")
       
   497   assume "space M \<noteq> {}"
       
   498   hence "(SOME x. x \<in> space M) \<in> space M" by (rule_tac someI_ex) blast
       
   499   with assms have "f (SOME x. x \<in> space M) = g (SOME x. x \<in> space M)" by blast
       
   500   with `space M \<noteq> {}` and assms show ?thesis by (simp add: bind_nonempty cong: distr_cong)
       
   501 qed (simp add: bind_empty)
       
   502 
       
   503 lemma bind_nonempty':
       
   504   assumes "f \<in> measurable M (subprob_algebra N)" "x \<in> space M"
       
   505   shows "bind M f = join (distr M (subprob_algebra N) f)"
       
   506   using assms
       
   507   apply (subst bind_nonempty, blast)
       
   508   apply (subst subprob_algebra_cong[OF sets_kernel[OF assms(1) someI_ex]], blast)
       
   509   apply (simp add: subprob_algebra_cong[OF sets_kernel[OF assms]])
       
   510   done
       
   511 
       
   512 lemma bind_nonempty'':
       
   513   assumes "f \<in> measurable M (subprob_algebra N)" "space M \<noteq> {}"
       
   514   shows "bind M f = join (distr M (subprob_algebra N) f)"
       
   515   using assms by (auto intro: bind_nonempty')
       
   516 
       
   517 lemma emeasure_bind:
       
   518     "\<lbrakk>space M \<noteq> {}; f \<in> measurable M (subprob_algebra N);X \<in> sets N\<rbrakk>
       
   519       \<Longrightarrow> emeasure (M \<guillemotright>= f) X = \<integral>\<^sup>+x. emeasure (f x) X \<partial>M"
       
   520   by (simp add: bind_nonempty'' emeasure_join nn_integral_distr measurable_emeasure_subprob_algebra)
       
   521 
       
   522 lemma bind_return: 
       
   523   assumes "f \<in> measurable M (subprob_algebra N)" and "x \<in> space M"
       
   524   shows "bind (return M x) f = f x"
       
   525   using sets_kernel[OF assms] assms
       
   526   by (simp_all add: distr_return join_return subprob_space_kernel bind_nonempty'
       
   527                cong: subprob_algebra_cong)
       
   528 
       
   529 lemma bind_return':
       
   530   shows "bind M (return M) = M"
       
   531   by (cases "space M = {}")
       
   532      (simp_all add: bind_empty space_empty[symmetric] bind_nonempty join_return' 
       
   533                cong: subprob_algebra_cong)
       
   534 
       
   535 lemma bind_count_space_singleton:
       
   536   assumes "subprob_space (f x)"
       
   537   shows "count_space {x} \<guillemotright>= f = f x"
       
   538 proof-
       
   539   have A: "\<And>A. A \<subseteq> {x} \<Longrightarrow> A = {} \<or> A = {x}" by auto
       
   540   have "count_space {x} = return (count_space {x}) x"
       
   541     by (intro measure_eqI) (auto dest: A)
       
   542   also have "... \<guillemotright>= f = f x"
       
   543     by (subst bind_return[of _ _ "f x"]) (auto simp: space_subprob_algebra assms)
       
   544   finally show ?thesis .
       
   545 qed
       
   546 
       
   547 lemma emeasure_bind_const: 
       
   548     "space M \<noteq> {} \<Longrightarrow> X \<in> sets N \<Longrightarrow> subprob_space N \<Longrightarrow> 
       
   549          emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
       
   550   by (simp add: bind_nonempty emeasure_join nn_integral_distr 
       
   551                 space_subprob_algebra measurable_emeasure_subprob_algebra emeasure_nonneg)
       
   552 
       
   553 lemma emeasure_bind_const':
       
   554   assumes "subprob_space M" "subprob_space N"
       
   555   shows "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
       
   556 using assms
       
   557 proof (case_tac "X \<in> sets N")
       
   558   fix X assume "X \<in> sets N"
       
   559   thus "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)" using assms
       
   560       by (subst emeasure_bind_const) 
       
   561          (simp_all add: subprob_space.subprob_not_empty subprob_space.emeasure_space_le_1)
       
   562 next
       
   563   fix X assume "X \<notin> sets N"
       
   564   with assms show "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
       
   565       by (simp add: sets_bind[of _ _ N] subprob_space.subprob_not_empty
       
   566                     space_subprob_algebra emeasure_notin_sets)
       
   567 qed
       
   568 
       
   569 lemma emeasure_bind_const_prob_space:
       
   570   assumes "prob_space M" "subprob_space N"
       
   571   shows "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X"
       
   572   using assms by (simp add: emeasure_bind_const' prob_space_imp_subprob_space 
       
   573                             prob_space.emeasure_space_1)
       
   574 
       
   575 lemma bind_const': "\<lbrakk>prob_space M; subprob_space N\<rbrakk> \<Longrightarrow> M \<guillemotright>= (\<lambda>x. N) = N"
       
   576   by (intro measure_eqI) 
       
   577      (simp_all add: space_subprob_algebra prob_space.not_empty emeasure_bind_const_prob_space)
       
   578 
       
   579 lemma bind_return_distr: 
       
   580     "space M \<noteq> {} \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> bind M (return N \<circ> f) = distr M N f"
       
   581   apply (simp add: bind_nonempty)
       
   582   apply (subst subprob_algebra_cong)
       
   583   apply (rule sets_return)
       
   584   apply (subst distr_distr[symmetric])
       
   585   apply (auto intro!: return_measurable simp: distr_distr[symmetric] join_return')
       
   586   done
       
   587 
       
   588 lemma bind_assoc:
       
   589   fixes f :: "'a \<Rightarrow> 'b measure" and g :: "'b \<Rightarrow> 'c measure"
       
   590   assumes M1: "f \<in> measurable M (subprob_algebra N)" and M2: "g \<in> measurable N (subprob_algebra R)"
       
   591   shows "bind (bind M f) g = bind M (\<lambda>x. bind (f x) g)"
       
   592 proof (cases "space M = {}")
       
   593   assume [simp]: "space M \<noteq> {}"
       
   594   from assms have [simp]: "space N \<noteq> {}" "space R \<noteq> {}"
       
   595       by (auto simp: measurable_empty_iff space_subprob_algebra_empty_iff)
       
   596   from assms have sets_fx[simp]: "\<And>x. x \<in> space M \<Longrightarrow> sets (f x) = sets N"
       
   597       by (simp add: sets_kernel)
       
   598   have ex_in: "\<And>A. A \<noteq> {} \<Longrightarrow> \<exists>x. x \<in> A" by blast
       
   599   note sets_some[simp] = sets_kernel[OF M1 someI_ex[OF ex_in[OF `space M \<noteq> {}`]]]
       
   600                          sets_kernel[OF M2 someI_ex[OF ex_in[OF `space N \<noteq> {}`]]]
       
   601   note space_some[simp] = sets_eq_imp_space_eq[OF this(1)] sets_eq_imp_space_eq[OF this(2)]
       
   602 
       
   603   have "bind M (\<lambda>x. bind (f x) g) = 
       
   604         join (distr M (subprob_algebra R) (join \<circ> (\<lambda>x. (distr x (subprob_algebra R) g)) \<circ> f))"
       
   605     by (simp add: sets_eq_imp_space_eq[OF sets_fx] bind_nonempty o_def
       
   606              cong: subprob_algebra_cong distr_cong)
       
   607   also have "distr M (subprob_algebra R) (join \<circ> (\<lambda>x. (distr x (subprob_algebra R) g)) \<circ> f) =
       
   608              distr (distr (distr M (subprob_algebra N) f)
       
   609                           (subprob_algebra (subprob_algebra R))
       
   610                           (\<lambda>x. distr x (subprob_algebra R) g)) 
       
   611                    (subprob_algebra R) join"
       
   612       apply (subst distr_distr, 
       
   613              (blast intro: measurable_comp measurable_distr measurable_join M1 M2)+)+
       
   614       apply (simp add: o_assoc)
       
   615       done
       
   616   also have "join ... = bind (bind M f) g"
       
   617       by (simp add: join_assoc join_distr_distr M2 bind_nonempty cong: subprob_algebra_cong)
       
   618   finally show ?thesis ..
       
   619 qed (simp add: bind_empty)
       
   620 
   182 
   621 lemma emeasure_space_subprob_algebra[measurable]:
   183 lemma emeasure_space_subprob_algebra[measurable]:
   622   "(\<lambda>a. emeasure a (space a)) \<in> borel_measurable (subprob_algebra N)"
   184   "(\<lambda>a. emeasure a (space a)) \<in> borel_measurable (subprob_algebra N)"
   623 proof-
   185 proof-
   624   have "(\<lambda>a. emeasure a (space N)) \<in> borel_measurable (subprob_algebra N)" (is "?f \<in> ?M")
   186   have "(\<lambda>a. emeasure a (space N)) \<in> borel_measurable (subprob_algebra N)" (is "?f \<in> ?M")
   690       using union by auto
   252       using union by auto
   691     finally show ?case .
   253     finally show ?case .
   692   qed simp
   254   qed simp
   693 qed
   255 qed
   694 
   256 
   695 (* TODO: Move *)
   257 lemma restrict_space_measurable:
       
   258   assumes X: "X \<noteq> {}" "X \<in> sets K"
       
   259   assumes N: "N \<in> measurable M (subprob_algebra K)"
       
   260   shows "(\<lambda>x. restrict_space (N x) X) \<in> measurable M (subprob_algebra (restrict_space K X))"
       
   261 proof (rule measurable_subprob_algebra)
       
   262   fix a assume a: "a \<in> space M"
       
   263   from N[THEN measurable_space, OF this]
       
   264   have "subprob_space (N a)" and [simp]: "sets (N a) = sets K" "space (N a) = space K"
       
   265     by (auto simp add: space_subprob_algebra dest: sets_eq_imp_space_eq)
       
   266   then interpret subprob_space "N a"
       
   267     by simp
       
   268   show "subprob_space (restrict_space (N a) X)"
       
   269   proof
       
   270     show "space (restrict_space (N a) X) \<noteq> {}"
       
   271       using X by (auto simp add: space_restrict_space)
       
   272     show "emeasure (restrict_space (N a) X) (space (restrict_space (N a) X)) \<le> 1"
       
   273       using X by (simp add: emeasure_restrict_space space_restrict_space subprob_emeasure_le_1)
       
   274   qed
       
   275   show "sets (restrict_space (N a) X) = sets (restrict_space K X)"
       
   276     by (intro sets_restrict_space_cong) fact
       
   277 next
       
   278   fix A assume A: "A \<in> sets (restrict_space K X)"
       
   279   show "(\<lambda>a. emeasure (restrict_space (N a) X) A) \<in> borel_measurable M"
       
   280   proof (subst measurable_cong)
       
   281     fix a assume "a \<in> space M"
       
   282     from N[THEN measurable_space, OF this]
       
   283     have [simp]: "sets (N a) = sets K" "space (N a) = space K"
       
   284       by (auto simp add: space_subprob_algebra dest: sets_eq_imp_space_eq)
       
   285     show "emeasure (restrict_space (N a) X) A = emeasure (N a) (A \<inter> X)"
       
   286       using X A by (subst emeasure_restrict_space) (auto simp add: sets_restrict_space ac_simps)
       
   287   next
       
   288     show "(\<lambda>w. emeasure (N w) (A \<inter> X)) \<in> borel_measurable M"
       
   289       using A X
       
   290       by (intro measurable_compose[OF N measurable_emeasure_subprob_algebra])
       
   291          (auto simp: sets_restrict_space)
       
   292   qed
       
   293 qed
       
   294 
       
   295 section {* Properties of return *}
       
   296 
       
   297 definition return :: "'a measure \<Rightarrow> 'a \<Rightarrow> 'a measure" where
       
   298   "return R x = measure_of (space R) (sets R) (\<lambda>A. indicator A x)"
       
   299 
       
   300 lemma space_return[simp]: "space (return M x) = space M"
       
   301   by (simp add: return_def)
       
   302 
       
   303 lemma sets_return[simp]: "sets (return M x) = sets M"
       
   304   by (simp add: return_def)
       
   305 
       
   306 lemma measurable_return1[simp]: "measurable (return N x) L = measurable N L"
       
   307   by (simp cong: measurable_cong_sets) 
       
   308 
       
   309 lemma measurable_return2[simp]: "measurable L (return N x) = measurable L N"
       
   310   by (simp cong: measurable_cong_sets) 
       
   311 
       
   312 lemma return_sets_cong: "sets M = sets N \<Longrightarrow> return M = return N"
       
   313   by (auto dest: sets_eq_imp_space_eq simp: fun_eq_iff return_def)
       
   314 
       
   315 lemma return_cong: "sets A = sets B \<Longrightarrow> return A x = return B x"
       
   316   by (auto simp add: return_def dest: sets_eq_imp_space_eq)
       
   317 
       
   318 lemma emeasure_return[simp]:
       
   319   assumes "A \<in> sets M"
       
   320   shows "emeasure (return M x) A = indicator A x"
       
   321 proof (rule emeasure_measure_of[OF return_def])
       
   322   show "sets M \<subseteq> Pow (space M)" by (rule sets.space_closed)
       
   323   show "positive (sets (return M x)) (\<lambda>A. indicator A x)" by (simp add: positive_def)
       
   324   from assms show "A \<in> sets (return M x)" unfolding return_def by simp
       
   325   show "countably_additive (sets (return M x)) (\<lambda>A. indicator A x)"
       
   326     by (auto intro: countably_additiveI simp: suminf_indicator)
       
   327 qed
       
   328 
       
   329 lemma prob_space_return: "x \<in> space M \<Longrightarrow> prob_space (return M x)"
       
   330   by rule simp
       
   331 
       
   332 lemma subprob_space_return: "x \<in> space M \<Longrightarrow> subprob_space (return M x)"
       
   333   by (intro prob_space_return prob_space_imp_subprob_space)
       
   334 
       
   335 lemma subprob_space_return_ne: 
       
   336   assumes "space M \<noteq> {}" shows "subprob_space (return M x)"
       
   337 proof
       
   338   show "emeasure (return M x) (space (return M x)) \<le> 1"
       
   339     by (subst emeasure_return) (auto split: split_indicator)
       
   340 qed (simp, fact)
       
   341 
       
   342 lemma measure_return: assumes X: "X \<in> sets M" shows "measure (return M x) X = indicator X x"
       
   343   unfolding measure_def emeasure_return[OF X, of x] by (simp split: split_indicator)
       
   344 
       
   345 lemma AE_return:
       
   346   assumes [simp]: "x \<in> space M" and [measurable]: "Measurable.pred M P"
       
   347   shows "(AE y in return M x. P y) \<longleftrightarrow> P x"
       
   348 proof -
       
   349   have "(AE y in return M x. y \<notin> {x\<in>space M. \<not> P x}) \<longleftrightarrow> P x"
       
   350     by (subst AE_iff_null_sets[symmetric]) (simp_all add: null_sets_def split: split_indicator)
       
   351   also have "(AE y in return M x. y \<notin> {x\<in>space M. \<not> P x}) \<longleftrightarrow> (AE y in return M x. P y)"
       
   352     by (rule AE_cong) auto
       
   353   finally show ?thesis .
       
   354 qed
       
   355   
       
   356 lemma nn_integral_return:
       
   357   assumes "g x \<ge> 0" "x \<in> space M" "g \<in> borel_measurable M"
       
   358   shows "(\<integral>\<^sup>+ a. g a \<partial>return M x) = g x"
       
   359 proof-
       
   360   interpret prob_space "return M x" by (rule prob_space_return[OF `x \<in> space M`])
       
   361   have "(\<integral>\<^sup>+ a. g a \<partial>return M x) = (\<integral>\<^sup>+ a. g x \<partial>return M x)" using assms
       
   362     by (intro nn_integral_cong_AE) (auto simp: AE_return)
       
   363   also have "... = g x"
       
   364     using nn_integral_const[OF `g x \<ge> 0`, of "return M x"] emeasure_space_1 by simp
       
   365   finally show ?thesis .
       
   366 qed
       
   367 
       
   368 lemma integral_return:
       
   369   fixes g :: "_ \<Rightarrow> 'a :: {banach, second_countable_topology}"
       
   370   assumes "x \<in> space M" "g \<in> borel_measurable M"
       
   371   shows "(\<integral>a. g a \<partial>return M x) = g x"
       
   372 proof-
       
   373   interpret prob_space "return M x" by (rule prob_space_return[OF `x \<in> space M`])
       
   374   have "(\<integral>a. g a \<partial>return M x) = (\<integral>a. g x \<partial>return M x)" using assms
       
   375     by (intro integral_cong_AE) (auto simp: AE_return)
       
   376   then show ?thesis
       
   377     using prob_space by simp
       
   378 qed
       
   379 
       
   380 lemma return_measurable[measurable]: "return N \<in> measurable N (subprob_algebra N)"
       
   381   by (rule measurable_subprob_algebra) (auto simp: subprob_space_return)
       
   382 
       
   383 lemma distr_return:
       
   384   assumes "f \<in> measurable M N" and "x \<in> space M"
       
   385   shows "distr (return M x) N f = return N (f x)"
       
   386   using assms by (intro measure_eqI) (simp_all add: indicator_def emeasure_distr)
       
   387 
       
   388 lemma return_restrict_space:
       
   389   "\<Omega> \<in> sets M \<Longrightarrow> return (restrict_space M \<Omega>) x = restrict_space (return M x) \<Omega>"
       
   390   by (auto intro!: measure_eqI simp: sets_restrict_space emeasure_restrict_space)
       
   391 
   696 lemma measurable_distr2:
   392 lemma measurable_distr2:
   697   assumes f[measurable]: "split f \<in> measurable (L \<Otimes>\<^sub>M M) N"
   393   assumes f[measurable]: "split f \<in> measurable (L \<Otimes>\<^sub>M M) N"
   698   assumes g[measurable]: "g \<in> measurable L (subprob_algebra M)"
   394   assumes g[measurable]: "g \<in> measurable L (subprob_algebra M)"
   699   shows "(\<lambda>x. distr (g x) N (f x)) \<in> measurable L (subprob_algebra N)"
   395   shows "(\<lambda>x. distr (g x) N (f x)) \<in> measurable L (subprob_algebra N)"
   700 proof -
   396 proof -
   746     apply measurable
   442     apply measurable
   747     done
   443     done
   748   finally show ?thesis .
   444   finally show ?thesis .
   749 qed
   445 qed
   750 
   446 
   751 (* END TODO *)
   447 lemma nn_integral_measurable_subprob_algebra2:
       
   448   assumes f[measurable]: "(\<lambda>(x, y). f x y) \<in> borel_measurable (M \<Otimes>\<^sub>M N)" and [simp]: "\<And>x y. 0 \<le> f x y"
       
   449   assumes N[measurable]: "L \<in> measurable M (subprob_algebra N)"
       
   450   shows "(\<lambda>x. integral\<^sup>N (L x) (f x)) \<in> borel_measurable M"
       
   451 proof -
       
   452   have "(\<lambda>x. integral\<^sup>N (distr (L x) (M \<Otimes>\<^sub>M N) (\<lambda>y. (x, y))) (\<lambda>(x, y). f x y)) \<in> borel_measurable M"
       
   453     apply (rule measurable_compose[OF _ nn_integral_measurable_subprob_algebra])
       
   454     apply (rule measurable_distr2)
       
   455     apply measurable
       
   456     apply (simp split: prod.split)
       
   457     done
       
   458   then show "(\<lambda>x. integral\<^sup>N (L x) (f x)) \<in> borel_measurable M"
       
   459     apply (rule measurable_cong[THEN iffD1, rotated])
       
   460     apply (subst nn_integral_distr)
       
   461     apply measurable
       
   462     apply (rule subprob_measurableD(2)[OF N], assumption)
       
   463     apply measurable
       
   464     done
       
   465 qed
       
   466 
       
   467 lemma emeasure_measurable_subprob_algebra2:
       
   468   assumes A[measurable]: "(SIGMA x:space M. A x) \<in> sets (M \<Otimes>\<^sub>M N)"
       
   469   assumes L[measurable]: "L \<in> measurable M (subprob_algebra N)"
       
   470   shows "(\<lambda>x. emeasure (L x) (A x)) \<in> borel_measurable M"
       
   471 proof -
       
   472   { fix x assume "x \<in> space M"
       
   473     then have "Pair x -` Sigma (space M) A = A x"
       
   474       by auto
       
   475     with sets_Pair1[OF A, of x] have "A x \<in> sets N"
       
   476       by auto }
       
   477   note ** = this
       
   478 
       
   479   have *: "\<And>x. fst x \<in> space M \<Longrightarrow> snd x \<in> A (fst x) \<longleftrightarrow> x \<in> (SIGMA x:space M. A x)"
       
   480     by (auto simp: fun_eq_iff)
       
   481   have "(\<lambda>(x, y). indicator (A x) y::ereal) \<in> borel_measurable (M \<Otimes>\<^sub>M N)"
       
   482     apply measurable
       
   483     apply (subst measurable_cong)
       
   484     apply (rule *)
       
   485     apply (auto simp: space_pair_measure)
       
   486     done
       
   487   then have "(\<lambda>x. integral\<^sup>N (L x) (indicator (A x))) \<in> borel_measurable M"
       
   488     by (intro nn_integral_measurable_subprob_algebra2[where N=N] ereal_indicator_nonneg L)
       
   489   then show "(\<lambda>x. emeasure (L x) (A x)) \<in> borel_measurable M"
       
   490     apply (rule measurable_cong[THEN iffD1, rotated])
       
   491     apply (rule nn_integral_indicator)
       
   492     apply (simp add: subprob_measurableD[OF L] **)
       
   493     done
       
   494 qed
       
   495 
       
   496 lemma measure_measurable_subprob_algebra2:
       
   497   assumes A[measurable]: "(SIGMA x:space M. A x) \<in> sets (M \<Otimes>\<^sub>M N)"
       
   498   assumes L[measurable]: "L \<in> measurable M (subprob_algebra N)"
       
   499   shows "(\<lambda>x. measure (L x) (A x)) \<in> borel_measurable M"
       
   500   unfolding measure_def
       
   501   by (intro borel_measurable_real_of_ereal emeasure_measurable_subprob_algebra2[OF assms])
       
   502 
       
   503 definition "select_sets M = (SOME N. sets M = sets (subprob_algebra N))"
       
   504 
       
   505 lemma select_sets1:
       
   506   "sets M = sets (subprob_algebra N) \<Longrightarrow> sets M = sets (subprob_algebra (select_sets M))"
       
   507   unfolding select_sets_def by (rule someI)
       
   508 
       
   509 lemma sets_select_sets[simp]:
       
   510   assumes sets: "sets M = sets (subprob_algebra N)"
       
   511   shows "sets (select_sets M) = sets N"
       
   512   unfolding select_sets_def
       
   513 proof (rule someI2)
       
   514   show "sets M = sets (subprob_algebra N)"
       
   515     by fact
       
   516 next
       
   517   fix L assume "sets M = sets (subprob_algebra L)"
       
   518   with sets have eq: "space (subprob_algebra N) = space (subprob_algebra L)"
       
   519     by (intro sets_eq_imp_space_eq) simp
       
   520   show "sets L = sets N"
       
   521   proof cases
       
   522     assume "space (subprob_algebra N) = {}"
       
   523     with space_subprob_algebra_empty_iff[of N] space_subprob_algebra_empty_iff[of L]
       
   524     show ?thesis
       
   525       by (simp add: eq space_empty_iff)
       
   526   next
       
   527     assume "space (subprob_algebra N) \<noteq> {}"
       
   528     with eq show ?thesis
       
   529       by (fastforce simp add: space_subprob_algebra)
       
   530   qed
       
   531 qed
       
   532 
       
   533 lemma space_select_sets[simp]:
       
   534   "sets M = sets (subprob_algebra N) \<Longrightarrow> space (select_sets M) = space N"
       
   535   by (intro sets_eq_imp_space_eq sets_select_sets)
       
   536 
       
   537 section {* Join *}
       
   538 
       
   539 definition join :: "'a measure measure \<Rightarrow> 'a measure" where
       
   540   "join M = measure_of (space (select_sets M)) (sets (select_sets M)) (\<lambda>B. \<integral>\<^sup>+ M'. emeasure M' B \<partial>M)"
       
   541 
       
   542 lemma
       
   543   shows space_join[simp]: "space (join M) = space (select_sets M)"
       
   544     and sets_join[simp]: "sets (join M) = sets (select_sets M)"
       
   545   by (simp_all add: join_def)
       
   546 
       
   547 lemma emeasure_join:
       
   548   assumes M[simp]: "sets M = sets (subprob_algebra N)" and A: "A \<in> sets N"
       
   549   shows "emeasure (join M) A = (\<integral>\<^sup>+ M'. emeasure M' A \<partial>M)"
       
   550 proof (rule emeasure_measure_of[OF join_def])
       
   551   have eq: "borel_measurable M = borel_measurable (subprob_algebra N)"
       
   552     by auto
       
   553   show "countably_additive (sets (join M)) (\<lambda>B. \<integral>\<^sup>+ M'. emeasure M' B \<partial>M)"
       
   554   proof (rule countably_additiveI)
       
   555     fix A :: "nat \<Rightarrow> 'a set" assume A: "range A \<subseteq> sets (join M)" "disjoint_family A"
       
   556     have "(\<Sum>i. \<integral>\<^sup>+ M'. emeasure M' (A i) \<partial>M) = (\<integral>\<^sup>+M'. (\<Sum>i. emeasure M' (A i)) \<partial>M)"
       
   557       using A by (subst nn_integral_suminf) (auto simp: measurable_emeasure_subprob_algebra eq)
       
   558     also have "\<dots> = (\<integral>\<^sup>+M'. emeasure M' (\<Union>i. A i) \<partial>M)"
       
   559     proof (rule nn_integral_cong)
       
   560       fix M' assume "M' \<in> space M"
       
   561       then show "(\<Sum>i. emeasure M' (A i)) = emeasure M' (\<Union>i. A i)"
       
   562         using A sets_eq_imp_space_eq[OF M] by (simp add: suminf_emeasure space_subprob_algebra)
       
   563     qed
       
   564     finally show "(\<Sum>i. \<integral>\<^sup>+M'. emeasure M' (A i) \<partial>M) = (\<integral>\<^sup>+M'. emeasure M' (\<Union>i. A i) \<partial>M)" .
       
   565   qed
       
   566 qed (auto simp: A sets.space_closed positive_def nn_integral_nonneg)
       
   567 
       
   568 lemma measurable_join:
       
   569   "join \<in> measurable (subprob_algebra (subprob_algebra N)) (subprob_algebra N)"
       
   570 proof (cases "space N \<noteq> {}", rule measurable_subprob_algebra)
       
   571   fix A assume "A \<in> sets N"
       
   572   let ?B = "borel_measurable (subprob_algebra (subprob_algebra N))"
       
   573   have "(\<lambda>M'. emeasure (join M') A) \<in> ?B \<longleftrightarrow> (\<lambda>M'. (\<integral>\<^sup>+ M''. emeasure M'' A \<partial>M')) \<in> ?B"
       
   574   proof (rule measurable_cong)
       
   575     fix M' assume "M' \<in> space (subprob_algebra (subprob_algebra N))"
       
   576     then show "emeasure (join M') A = (\<integral>\<^sup>+ M''. emeasure M'' A \<partial>M')"
       
   577       by (intro emeasure_join) (auto simp: space_subprob_algebra `A\<in>sets N`)
       
   578   qed
       
   579   also have "(\<lambda>M'. \<integral>\<^sup>+M''. emeasure M'' A \<partial>M') \<in> ?B"
       
   580     using measurable_emeasure_subprob_algebra[OF `A\<in>sets N`] emeasure_nonneg[of _ A]
       
   581     by (rule nn_integral_measurable_subprob_algebra)
       
   582   finally show "(\<lambda>M'. emeasure (join M') A) \<in> borel_measurable (subprob_algebra (subprob_algebra N))" .
       
   583 next
       
   584   assume [simp]: "space N \<noteq> {}"
       
   585   fix M assume M: "M \<in> space (subprob_algebra (subprob_algebra N))"
       
   586   then have "(\<integral>\<^sup>+M'. emeasure M' (space N) \<partial>M) \<le> (\<integral>\<^sup>+M'. 1 \<partial>M)"
       
   587     apply (intro nn_integral_mono)
       
   588     apply (auto simp: space_subprob_algebra 
       
   589                  dest!: sets_eq_imp_space_eq subprob_space.emeasure_space_le_1)
       
   590     done
       
   591   with M show "subprob_space (join M)"
       
   592     by (intro subprob_spaceI)
       
   593        (auto simp: emeasure_join space_subprob_algebra M assms dest: subprob_space.emeasure_space_le_1)
       
   594 next
       
   595   assume "\<not>(space N \<noteq> {})"
       
   596   thus ?thesis by (simp add: measurable_empty_iff space_subprob_algebra_empty_iff)
       
   597 qed (auto simp: space_subprob_algebra)
       
   598 
       
   599 lemma nn_integral_join:
       
   600   assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x" and M: "sets M = sets (subprob_algebra N)"
       
   601   shows "(\<integral>\<^sup>+x. f x \<partial>join M) = (\<integral>\<^sup>+M'. \<integral>\<^sup>+x. f x \<partial>M' \<partial>M)"
       
   602   using f
       
   603 proof induct
       
   604   case (cong f g)
       
   605   moreover have "integral\<^sup>N (join M) f = integral\<^sup>N (join M) g"
       
   606     by (intro nn_integral_cong cong) (simp add: M)
       
   607   moreover from M have "(\<integral>\<^sup>+ M'. integral\<^sup>N M' f \<partial>M) = (\<integral>\<^sup>+ M'. integral\<^sup>N M' g \<partial>M)"
       
   608     by (intro nn_integral_cong cong)
       
   609        (auto simp add: space_subprob_algebra dest!: sets_eq_imp_space_eq)
       
   610   ultimately show ?case
       
   611     by simp
       
   612 next
       
   613   case (set A)
       
   614   moreover with M have "(\<integral>\<^sup>+ M'. integral\<^sup>N M' (indicator A) \<partial>M) = (\<integral>\<^sup>+ M'. emeasure M' A \<partial>M)" 
       
   615     by (intro nn_integral_cong nn_integral_indicator)
       
   616        (auto simp: space_subprob_algebra dest!: sets_eq_imp_space_eq)
       
   617   ultimately show ?case
       
   618     using M by (simp add: emeasure_join)
       
   619 next
       
   620   case (mult f c)
       
   621   have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. c * f x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. c * \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M)"
       
   622     using mult M
       
   623     by (intro nn_integral_cong nn_integral_cmult)
       
   624        (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
       
   625   also have "\<dots> = c * (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M)"
       
   626     using nn_integral_measurable_subprob_algebra[OF mult(3,4)]
       
   627     by (intro nn_integral_cmult mult) (simp add: M)
       
   628   also have "\<dots> = c * (integral\<^sup>N (join M) f)"
       
   629     by (simp add: mult)
       
   630   also have "\<dots> = (\<integral>\<^sup>+ x. c * f x \<partial>join M)"
       
   631     using mult(2,3) by (intro nn_integral_cmult[symmetric] mult) (simp add: M)
       
   632   finally show ?case by simp
       
   633 next
       
   634   case (add f g)
       
   635   have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x + g x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. (\<integral>\<^sup>+ x. f x \<partial>M') + (\<integral>\<^sup>+ x. g x \<partial>M') \<partial>M)"
       
   636     using add M
       
   637     by (intro nn_integral_cong nn_integral_add)
       
   638        (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
       
   639   also have "\<dots> = (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. f x \<partial>M' \<partial>M) + (\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. g x \<partial>M' \<partial>M)"
       
   640     using nn_integral_measurable_subprob_algebra[OF add(1,2)]
       
   641     using nn_integral_measurable_subprob_algebra[OF add(5,6)]
       
   642     by (intro nn_integral_add add) (simp_all add: M nn_integral_nonneg)
       
   643   also have "\<dots> = (integral\<^sup>N (join M) f) + (integral\<^sup>N (join M) g)"
       
   644     by (simp add: add)
       
   645   also have "\<dots> = (\<integral>\<^sup>+ x. f x + g x \<partial>join M)"
       
   646     using add by (intro nn_integral_add[symmetric] add) (simp_all add: M)
       
   647   finally show ?case by (simp add: ac_simps)
       
   648 next
       
   649   case (seq F)
       
   650   have "(\<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. (SUP i. F i) x \<partial>M' \<partial>M) = (\<integral>\<^sup>+ M'. (SUP i. \<integral>\<^sup>+ x. F i x \<partial>M') \<partial>M)"
       
   651     using seq M unfolding SUP_apply
       
   652     by (intro nn_integral_cong nn_integral_monotone_convergence_SUP)
       
   653        (auto simp add: space_subprob_algebra cong: measurable_cong dest!: sets_eq_imp_space_eq)
       
   654   also have "\<dots> = (SUP i. \<integral>\<^sup>+ M'. \<integral>\<^sup>+ x. F i x \<partial>M' \<partial>M)"
       
   655     using nn_integral_measurable_subprob_algebra[OF seq(1,2)] seq
       
   656     by (intro nn_integral_monotone_convergence_SUP)
       
   657        (simp_all add: M nn_integral_nonneg incseq_nn_integral incseq_def le_fun_def nn_integral_mono )
       
   658   also have "\<dots> = (SUP i. integral\<^sup>N (join M) (F i))"
       
   659     by (simp add: seq)
       
   660   also have "\<dots> = (\<integral>\<^sup>+ x. (SUP i. F i x) \<partial>join M)"
       
   661     using seq by (intro nn_integral_monotone_convergence_SUP[symmetric] seq) (simp_all add: M)
       
   662   finally show ?case by (simp add: ac_simps)
       
   663 qed
       
   664 
       
   665 lemma join_assoc:
       
   666   assumes M: "sets M = sets (subprob_algebra (subprob_algebra N))"
       
   667   shows "join (distr M (subprob_algebra N) join) = join (join M)"
       
   668 proof (rule measure_eqI)
       
   669   fix A assume "A \<in> sets (join (distr M (subprob_algebra N) join))"
       
   670   then have A: "A \<in> sets N" by simp
       
   671   show "emeasure (join (distr M (subprob_algebra N) join)) A = emeasure (join (join M)) A"
       
   672     using measurable_join[of N]
       
   673     by (auto simp: M A nn_integral_distr emeasure_join measurable_emeasure_subprob_algebra emeasure_nonneg
       
   674                    sets_eq_imp_space_eq[OF M] space_subprob_algebra nn_integral_join[OF _ _ M]
       
   675              intro!: nn_integral_cong emeasure_join cong: measurable_cong)
       
   676 qed (simp add: M)
       
   677 
       
   678 lemma join_return: 
       
   679   assumes "sets M = sets N" and "subprob_space M"
       
   680   shows "join (return (subprob_algebra N) M) = M"
       
   681   by (rule measure_eqI)
       
   682      (simp_all add: emeasure_join emeasure_nonneg space_subprob_algebra  
       
   683                     measurable_emeasure_subprob_algebra nn_integral_return assms)
       
   684 
       
   685 lemma join_return':
       
   686   assumes "sets N = sets M"
       
   687   shows "join (distr M (subprob_algebra N) (return N)) = M"
       
   688 apply (rule measure_eqI)
       
   689 apply (simp add: assms)
       
   690 apply (subgoal_tac "return N \<in> measurable M (subprob_algebra N)")
       
   691 apply (simp add: emeasure_join nn_integral_distr measurable_emeasure_subprob_algebra assms)
       
   692 apply (subst measurable_cong_sets, rule assms[symmetric], rule refl, rule return_measurable)
       
   693 done
       
   694 
       
   695 lemma join_distr_distr:
       
   696   fixes f :: "'a \<Rightarrow> 'b" and M :: "'a measure measure" and N :: "'b measure"
       
   697   assumes "sets M = sets (subprob_algebra R)" and "f \<in> measurable R N"
       
   698   shows "join (distr M (subprob_algebra N) (\<lambda>M. distr M N f)) = distr (join M) N f" (is "?r = ?l")
       
   699 proof (rule measure_eqI)
       
   700   fix A assume "A \<in> sets ?r"
       
   701   hence A_in_N: "A \<in> sets N" by simp
       
   702 
       
   703   from assms have "f \<in> measurable (join M) N" 
       
   704       by (simp cong: measurable_cong_sets)
       
   705   moreover from assms and A_in_N have "f-`A \<inter> space R \<in> sets R" 
       
   706       by (intro measurable_sets) simp_all
       
   707   ultimately have "emeasure (distr (join M) N f) A = \<integral>\<^sup>+M'. emeasure M' (f-`A \<inter> space R) \<partial>M"
       
   708       by (simp_all add: A_in_N emeasure_distr emeasure_join assms)
       
   709   
       
   710   also have "... = \<integral>\<^sup>+ x. emeasure (distr x N f) A \<partial>M" using A_in_N
       
   711   proof (intro nn_integral_cong, subst emeasure_distr)
       
   712     fix M' assume "M' \<in> space M"
       
   713     from assms have "space M = space (subprob_algebra R)"
       
   714         using sets_eq_imp_space_eq by blast
       
   715     with `M' \<in> space M` have [simp]: "sets M' = sets R" using space_subprob_algebra by blast
       
   716     show "f \<in> measurable M' N" by (simp cong: measurable_cong_sets add: assms)
       
   717     have "space M' = space R" by (rule sets_eq_imp_space_eq) simp
       
   718     thus "emeasure M' (f -` A \<inter> space R) = emeasure M' (f -` A \<inter> space M')" by simp
       
   719   qed
       
   720 
       
   721   also have "(\<lambda>M. distr M N f) \<in> measurable M (subprob_algebra N)"
       
   722       by (simp cong: measurable_cong_sets add: assms measurable_distr)
       
   723   hence "(\<integral>\<^sup>+ x. emeasure (distr x N f) A \<partial>M) = 
       
   724              emeasure (join (distr M (subprob_algebra N) (\<lambda>M. distr M N f))) A"
       
   725       by (simp_all add: emeasure_join assms A_in_N nn_integral_distr measurable_emeasure_subprob_algebra)
       
   726   finally show "emeasure ?r A = emeasure ?l A" ..
       
   727 qed simp
       
   728 
       
   729 definition bind :: "'a measure \<Rightarrow> ('a \<Rightarrow> 'b measure) \<Rightarrow> 'b measure" where
       
   730   "bind M f = (if space M = {} then count_space {} else
       
   731     join (distr M (subprob_algebra (f (SOME x. x \<in> space M))) f))"
       
   732 
       
   733 adhoc_overloading Monad_Syntax.bind bind
       
   734 
       
   735 lemma bind_empty: 
       
   736   "space M = {} \<Longrightarrow> bind M f = count_space {}"
       
   737   by (simp add: bind_def)
       
   738 
       
   739 lemma bind_nonempty:
       
   740   "space M \<noteq> {} \<Longrightarrow> bind M f = join (distr M (subprob_algebra (f (SOME x. x \<in> space M))) f)"
       
   741   by (simp add: bind_def)
       
   742 
       
   743 lemma sets_bind_empty: "sets M = {} \<Longrightarrow> sets (bind M f) = {{}}"
       
   744   by (auto simp: bind_def)
       
   745 
       
   746 lemma space_bind_empty: "space M = {} \<Longrightarrow> space (bind M f) = {}"
       
   747   by (simp add: bind_def)
       
   748 
       
   749 lemma sets_bind[simp]: 
       
   750   assumes "f \<in> measurable M (subprob_algebra N)" and "space M \<noteq> {}"
       
   751   shows "sets (bind M f) = sets N"
       
   752     using assms(2) by (force simp: bind_nonempty intro!: sets_kernel[OF assms(1) someI_ex])
       
   753 
       
   754 lemma space_bind[simp]: 
       
   755   assumes "f \<in> measurable M (subprob_algebra N)" and "space M \<noteq> {}"
       
   756   shows "space (bind M f) = space N"
       
   757     using assms by (intro sets_eq_imp_space_eq sets_bind)
       
   758 
       
   759 lemma bind_cong: 
       
   760   assumes "\<forall>x \<in> space M. f x = g x"
       
   761   shows "bind M f = bind M g"
       
   762 proof (cases "space M = {}")
       
   763   assume "space M \<noteq> {}"
       
   764   hence "(SOME x. x \<in> space M) \<in> space M" by (rule_tac someI_ex) blast
       
   765   with assms have "f (SOME x. x \<in> space M) = g (SOME x. x \<in> space M)" by blast
       
   766   with `space M \<noteq> {}` and assms show ?thesis by (simp add: bind_nonempty cong: distr_cong)
       
   767 qed (simp add: bind_empty)
       
   768 
       
   769 lemma bind_nonempty':
       
   770   assumes "f \<in> measurable M (subprob_algebra N)" "x \<in> space M"
       
   771   shows "bind M f = join (distr M (subprob_algebra N) f)"
       
   772   using assms
       
   773   apply (subst bind_nonempty, blast)
       
   774   apply (subst subprob_algebra_cong[OF sets_kernel[OF assms(1) someI_ex]], blast)
       
   775   apply (simp add: subprob_algebra_cong[OF sets_kernel[OF assms]])
       
   776   done
       
   777 
       
   778 lemma bind_nonempty'':
       
   779   assumes "f \<in> measurable M (subprob_algebra N)" "space M \<noteq> {}"
       
   780   shows "bind M f = join (distr M (subprob_algebra N) f)"
       
   781   using assms by (auto intro: bind_nonempty')
       
   782 
       
   783 lemma emeasure_bind:
       
   784     "\<lbrakk>space M \<noteq> {}; f \<in> measurable M (subprob_algebra N);X \<in> sets N\<rbrakk>
       
   785       \<Longrightarrow> emeasure (M \<guillemotright>= f) X = \<integral>\<^sup>+x. emeasure (f x) X \<partial>M"
       
   786   by (simp add: bind_nonempty'' emeasure_join nn_integral_distr measurable_emeasure_subprob_algebra)
       
   787 
       
   788 lemma nn_integral_bind:
       
   789   assumes f: "f \<in> borel_measurable B" "\<And>x. 0 \<le> f x"
       
   790   assumes N: "N \<in> measurable M (subprob_algebra B)"
       
   791   shows "(\<integral>\<^sup>+x. f x \<partial>(M \<guillemotright>= N)) = (\<integral>\<^sup>+x. \<integral>\<^sup>+y. f y \<partial>N x \<partial>M)"
       
   792 proof cases
       
   793   assume M: "space M \<noteq> {}" show ?thesis
       
   794     unfolding bind_nonempty''[OF N M] nn_integral_join[OF f sets_distr]
       
   795     by (rule nn_integral_distr[OF N nn_integral_measurable_subprob_algebra[OF f]])
       
   796 qed (simp add: bind_empty space_empty[of M] nn_integral_count_space)
       
   797 
       
   798 lemma AE_bind:
       
   799   assumes P[measurable]: "Measurable.pred B P"
       
   800   assumes N[measurable]: "N \<in> measurable M (subprob_algebra B)"
       
   801   shows "(AE x in M \<guillemotright>= N. P x) \<longleftrightarrow> (AE x in M. AE y in N x. P y)"
       
   802 proof cases
       
   803   assume M: "space M = {}" show ?thesis
       
   804     unfolding bind_empty[OF M] unfolding space_empty[OF M] by (simp add: AE_count_space)
       
   805 next
       
   806   assume M: "space M \<noteq> {}"
       
   807   have *: "(\<integral>\<^sup>+x. indicator {x. \<not> P x} x \<partial>(M \<guillemotright>= N)) = (\<integral>\<^sup>+x. indicator {x\<in>space B. \<not> P x} x \<partial>(M \<guillemotright>= N))"
       
   808     by (intro nn_integral_cong) (simp add: space_bind[OF N M] split: split_indicator)
       
   809 
       
   810   have "(AE x in M \<guillemotright>= N. P x) \<longleftrightarrow> (\<integral>\<^sup>+ x. integral\<^sup>N (N x) (indicator {x \<in> space B. \<not> P x}) \<partial>M) = 0"
       
   811     by (simp add: AE_iff_nn_integral sets_bind[OF N M] space_bind[OF N M] * nn_integral_bind[where B=B]
       
   812              del: nn_integral_indicator)
       
   813   also have "\<dots> = (AE x in M. AE y in N x. P y)"
       
   814     apply (subst nn_integral_0_iff_AE)
       
   815     apply (rule measurable_compose[OF N nn_integral_measurable_subprob_algebra])
       
   816     apply measurable
       
   817     apply (intro eventually_subst AE_I2)
       
   818     apply simp
       
   819     apply (subst nn_integral_0_iff_AE)
       
   820     apply (simp add: subprob_measurableD(3)[OF N])
       
   821     apply (auto simp add: ereal_indicator_le_0 subprob_measurableD(1)[OF N] intro!: eventually_subst)
       
   822     done
       
   823   finally show ?thesis .
       
   824 qed
   752 
   825 
   753 lemma measurable_bind':
   826 lemma measurable_bind':
   754   assumes M1: "f \<in> measurable M (subprob_algebra N)" and
   827   assumes M1: "f \<in> measurable M (subprob_algebra N)" and
   755           M2: "split g \<in> measurable (M \<Otimes>\<^sub>M N) (subprob_algebra R)"
   828           M2: "split g \<in> measurable (M \<Otimes>\<^sub>M N) (subprob_algebra R)"
   756   shows "(\<lambda>x. bind (f x) (g x)) \<in> measurable M (subprob_algebra R)"
   829   shows "(\<lambda>x. bind (f x) (g x)) \<in> measurable M (subprob_algebra R)"
   789     by (rule measurable_bind, rule measurable_ident_sets, rule refl, 
   862     by (rule measurable_bind, rule measurable_ident_sets, rule refl, 
   790         rule measurable_compose[OF measurable_snd assms(2)])
   863         rule measurable_compose[OF measurable_snd assms(2)])
   791   from assms(1) show "M \<in> space (subprob_algebra M)"
   864   from assms(1) show "M \<in> space (subprob_algebra M)"
   792     by (simp add: space_subprob_algebra)
   865     by (simp add: space_subprob_algebra)
   793 qed
   866 qed
       
   867 
       
   868 lemma (in prob_space) prob_space_bind: 
       
   869   assumes ae: "AE x in M. prob_space (N x)"
       
   870     and N[measurable]: "N \<in> measurable M (subprob_algebra S)"
       
   871   shows "prob_space (M \<guillemotright>= N)"
       
   872 proof
       
   873   have "emeasure (M \<guillemotright>= N) (space (M \<guillemotright>= N)) = (\<integral>\<^sup>+x. emeasure (N x) (space (N x)) \<partial>M)"
       
   874     by (subst emeasure_bind[where N=S])
       
   875        (auto simp: not_empty space_bind[OF N] subprob_measurableD[OF N] intro!: nn_integral_cong)
       
   876   also have "\<dots> = (\<integral>\<^sup>+x. 1 \<partial>M)"
       
   877     using ae by (intro nn_integral_cong_AE, eventually_elim) (rule prob_space.emeasure_space_1)
       
   878   finally show "emeasure (M \<guillemotright>= N) (space (M \<guillemotright>= N)) = 1"
       
   879     by (simp add: emeasure_space_1)
       
   880 qed
       
   881 
       
   882 lemma (in subprob_space) bind_in_space:
       
   883   "A \<in> measurable M (subprob_algebra N) \<Longrightarrow> (M \<guillemotright>= A) \<in> space (subprob_algebra N)"
       
   884   by (auto simp add: space_subprob_algebra subprob_not_empty intro!: subprob_space_bind)
       
   885      unfold_locales
       
   886 
       
   887 lemma (in subprob_space) measure_bind:
       
   888   assumes f: "f \<in> measurable M (subprob_algebra N)" and X: "X \<in> sets N"
       
   889   shows "measure (M \<guillemotright>= f) X = \<integral>x. measure (f x) X \<partial>M"
       
   890 proof -
       
   891   interpret Mf: subprob_space "M \<guillemotright>= f"
       
   892     by (rule subprob_space_bind[OF _ f]) unfold_locales
       
   893 
       
   894   { fix x assume "x \<in> space M"
       
   895     from f[THEN measurable_space, OF this] interpret subprob_space "f x"
       
   896       by (simp add: space_subprob_algebra)
       
   897     have "emeasure (f x) X = ereal (measure (f x) X)" "measure (f x) X \<le> 1"
       
   898       by (auto simp: emeasure_eq_measure subprob_measure_le_1) }
       
   899   note this[simp]
       
   900 
       
   901   have "emeasure (M \<guillemotright>= f) X = \<integral>\<^sup>+x. emeasure (f x) X \<partial>M"
       
   902     using subprob_not_empty f X by (rule emeasure_bind)
       
   903   also have "\<dots> = \<integral>\<^sup>+x. ereal (measure (f x) X) \<partial>M"
       
   904     by (intro nn_integral_cong) simp
       
   905   also have "\<dots> = \<integral>x. measure (f x) X \<partial>M"
       
   906     by (intro nn_integral_eq_integral integrable_const_bound[where B=1]
       
   907               measure_measurable_subprob_algebra2[OF _ f] pair_measureI X)
       
   908        (auto simp: measure_nonneg)
       
   909   finally show ?thesis
       
   910     by (simp add: Mf.emeasure_eq_measure)
       
   911 qed
       
   912 
       
   913 lemma emeasure_bind_const: 
       
   914     "space M \<noteq> {} \<Longrightarrow> X \<in> sets N \<Longrightarrow> subprob_space N \<Longrightarrow> 
       
   915          emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
       
   916   by (simp add: bind_nonempty emeasure_join nn_integral_distr 
       
   917                 space_subprob_algebra measurable_emeasure_subprob_algebra emeasure_nonneg)
       
   918 
       
   919 lemma emeasure_bind_const':
       
   920   assumes "subprob_space M" "subprob_space N"
       
   921   shows "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
       
   922 using assms
       
   923 proof (case_tac "X \<in> sets N")
       
   924   fix X assume "X \<in> sets N"
       
   925   thus "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)" using assms
       
   926       by (subst emeasure_bind_const) 
       
   927          (simp_all add: subprob_space.subprob_not_empty subprob_space.emeasure_space_le_1)
       
   928 next
       
   929   fix X assume "X \<notin> sets N"
       
   930   with assms show "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X * emeasure M (space M)"
       
   931       by (simp add: sets_bind[of _ _ N] subprob_space.subprob_not_empty
       
   932                     space_subprob_algebra emeasure_notin_sets)
       
   933 qed
       
   934 
       
   935 lemma emeasure_bind_const_prob_space:
       
   936   assumes "prob_space M" "subprob_space N"
       
   937   shows "emeasure (M \<guillemotright>= (\<lambda>x. N)) X = emeasure N X"
       
   938   using assms by (simp add: emeasure_bind_const' prob_space_imp_subprob_space 
       
   939                             prob_space.emeasure_space_1)
       
   940 
       
   941 lemma bind_return: 
       
   942   assumes "f \<in> measurable M (subprob_algebra N)" and "x \<in> space M"
       
   943   shows "bind (return M x) f = f x"
       
   944   using sets_kernel[OF assms] assms
       
   945   by (simp_all add: distr_return join_return subprob_space_kernel bind_nonempty'
       
   946                cong: subprob_algebra_cong)
       
   947 
       
   948 lemma bind_return':
       
   949   shows "bind M (return M) = M"
       
   950   by (cases "space M = {}")
       
   951      (simp_all add: bind_empty space_empty[symmetric] bind_nonempty join_return' 
       
   952                cong: subprob_algebra_cong)
       
   953 
       
   954 lemma distr_bind:
       
   955   assumes N: "N \<in> measurable M (subprob_algebra K)" "space M \<noteq> {}"
       
   956   assumes f: "f \<in> measurable K R"
       
   957   shows "distr (M \<guillemotright>= N) R f = (M \<guillemotright>= (\<lambda>x. distr (N x) R f))"
       
   958   unfolding bind_nonempty''[OF N]
       
   959   apply (subst bind_nonempty''[OF measurable_compose[OF N(1) measurable_distr] N(2)])
       
   960   apply (rule f)
       
   961   apply (simp add: join_distr_distr[OF _ f, symmetric])
       
   962   apply (subst distr_distr[OF measurable_distr, OF f N(1)])
       
   963   apply (simp add: comp_def)
       
   964   done
       
   965 
       
   966 lemma bind_distr:
       
   967   assumes f[measurable]: "f \<in> measurable M X"
       
   968   assumes N[measurable]: "N \<in> measurable X (subprob_algebra K)" and "space M \<noteq> {}"
       
   969   shows "(distr M X f \<guillemotright>= N) = (M \<guillemotright>= (\<lambda>x. N (f x)))"
       
   970 proof -
       
   971   have "space X \<noteq> {}" "space M \<noteq> {}"
       
   972     using `space M \<noteq> {}` f[THEN measurable_space] by auto
       
   973   then show ?thesis
       
   974     by (simp add: bind_nonempty''[where N=K] distr_distr comp_def)
       
   975 qed
       
   976 
       
   977 lemma bind_count_space_singleton:
       
   978   assumes "subprob_space (f x)"
       
   979   shows "count_space {x} \<guillemotright>= f = f x"
       
   980 proof-
       
   981   have A: "\<And>A. A \<subseteq> {x} \<Longrightarrow> A = {} \<or> A = {x}" by auto
       
   982   have "count_space {x} = return (count_space {x}) x"
       
   983     by (intro measure_eqI) (auto dest: A)
       
   984   also have "... \<guillemotright>= f = f x"
       
   985     by (subst bind_return[of _ _ "f x"]) (auto simp: space_subprob_algebra assms)
       
   986   finally show ?thesis .
       
   987 qed
       
   988 
       
   989 lemma restrict_space_bind:
       
   990   assumes N: "N \<in> measurable M (subprob_algebra K)"
       
   991   assumes "space M \<noteq> {}"
       
   992   assumes X[simp]: "X \<in> sets K" "X \<noteq> {}"
       
   993   shows "restrict_space (bind M N) X = bind M (\<lambda>x. restrict_space (N x) X)"
       
   994 proof (rule measure_eqI)
       
   995   fix A assume "A \<in> sets (restrict_space (M \<guillemotright>= N) X)"
       
   996   with X have "A \<in> sets K" "A \<subseteq> X"
       
   997     by (auto simp: sets_restrict_space sets_bind[OF assms(1,2)])
       
   998   then show "emeasure (restrict_space (M \<guillemotright>= N) X) A = emeasure (M \<guillemotright>= (\<lambda>x. restrict_space (N x) X)) A"
       
   999     using assms
       
  1000     apply (subst emeasure_restrict_space)
       
  1001     apply (simp_all add: space_bind[OF assms(1,2)] sets_bind[OF assms(1,2)] emeasure_bind[OF assms(2,1)])
       
  1002     apply (subst emeasure_bind[OF _ restrict_space_measurable[OF _ _ N]])
       
  1003     apply (auto simp: sets_restrict_space emeasure_restrict_space space_subprob_algebra
       
  1004                 intro!: nn_integral_cong dest!: measurable_space)
       
  1005     done
       
  1006 qed (simp add: sets_restrict_space sets_bind[OF assms(1,2)] sets_bind[OF restrict_space_measurable[OF assms(4,3,1)] assms(2)])
       
  1007 
       
  1008 lemma bind_const': "\<lbrakk>prob_space M; subprob_space N\<rbrakk> \<Longrightarrow> M \<guillemotright>= (\<lambda>x. N) = N"
       
  1009   by (intro measure_eqI) 
       
  1010      (simp_all add: space_subprob_algebra prob_space.not_empty emeasure_bind_const_prob_space)
       
  1011 
       
  1012 lemma bind_return_distr: 
       
  1013     "space M \<noteq> {} \<Longrightarrow> f \<in> measurable M N \<Longrightarrow> bind M (return N \<circ> f) = distr M N f"
       
  1014   apply (simp add: bind_nonempty)
       
  1015   apply (subst subprob_algebra_cong)
       
  1016   apply (rule sets_return)
       
  1017   apply (subst distr_distr[symmetric])
       
  1018   apply (auto intro!: return_measurable simp: distr_distr[symmetric] join_return')
       
  1019   done
       
  1020 
       
  1021 lemma bind_assoc:
       
  1022   fixes f :: "'a \<Rightarrow> 'b measure" and g :: "'b \<Rightarrow> 'c measure"
       
  1023   assumes M1: "f \<in> measurable M (subprob_algebra N)" and M2: "g \<in> measurable N (subprob_algebra R)"
       
  1024   shows "bind (bind M f) g = bind M (\<lambda>x. bind (f x) g)"
       
  1025 proof (cases "space M = {}")
       
  1026   assume [simp]: "space M \<noteq> {}"
       
  1027   from assms have [simp]: "space N \<noteq> {}" "space R \<noteq> {}"
       
  1028       by (auto simp: measurable_empty_iff space_subprob_algebra_empty_iff)
       
  1029   from assms have sets_fx[simp]: "\<And>x. x \<in> space M \<Longrightarrow> sets (f x) = sets N"
       
  1030       by (simp add: sets_kernel)
       
  1031   have ex_in: "\<And>A. A \<noteq> {} \<Longrightarrow> \<exists>x. x \<in> A" by blast
       
  1032   note sets_some[simp] = sets_kernel[OF M1 someI_ex[OF ex_in[OF `space M \<noteq> {}`]]]
       
  1033                          sets_kernel[OF M2 someI_ex[OF ex_in[OF `space N \<noteq> {}`]]]
       
  1034   note space_some[simp] = sets_eq_imp_space_eq[OF this(1)] sets_eq_imp_space_eq[OF this(2)]
       
  1035 
       
  1036   have "bind M (\<lambda>x. bind (f x) g) = 
       
  1037         join (distr M (subprob_algebra R) (join \<circ> (\<lambda>x. (distr x (subprob_algebra R) g)) \<circ> f))"
       
  1038     by (simp add: sets_eq_imp_space_eq[OF sets_fx] bind_nonempty o_def
       
  1039              cong: subprob_algebra_cong distr_cong)
       
  1040   also have "distr M (subprob_algebra R) (join \<circ> (\<lambda>x. (distr x (subprob_algebra R) g)) \<circ> f) =
       
  1041              distr (distr (distr M (subprob_algebra N) f)
       
  1042                           (subprob_algebra (subprob_algebra R))
       
  1043                           (\<lambda>x. distr x (subprob_algebra R) g)) 
       
  1044                    (subprob_algebra R) join"
       
  1045       apply (subst distr_distr, 
       
  1046              (blast intro: measurable_comp measurable_distr measurable_join M1 M2)+)+
       
  1047       apply (simp add: o_assoc)
       
  1048       done
       
  1049   also have "join ... = bind (bind M f) g"
       
  1050       by (simp add: join_assoc join_distr_distr M2 bind_nonempty cong: subprob_algebra_cong)
       
  1051   finally show ?thesis ..
       
  1052 qed (simp add: bind_empty)
   794 
  1053 
   795 lemma double_bind_assoc:
  1054 lemma double_bind_assoc:
   796   assumes Mg: "g \<in> measurable N (subprob_algebra N')"
  1055   assumes Mg: "g \<in> measurable N (subprob_algebra N')"
   797   assumes Mf: "f \<in> measurable M (subprob_algebra M')"
  1056   assumes Mf: "f \<in> measurable M (subprob_algebra M')"
   798   assumes Mh: "split h \<in> measurable (M \<Otimes>\<^sub>M M') N"
  1057   assumes Mh: "split h \<in> measurable (M \<Otimes>\<^sub>M M') N"
   815     have "do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y) \<guillemotright>= g} = do {x \<leftarrow> M; y \<leftarrow> f x; g (h x y)}"
  1074     have "do {x \<leftarrow> M; y \<leftarrow> f x; return N (h x y) \<guillemotright>= g} = do {x \<leftarrow> M; y \<leftarrow> f x; g (h x y)}"
   816     by (intro ballI bind_cong bind_return[OF Mg]) (auto simp: space_pair_measure)
  1075     by (intro ballI bind_cong bind_return[OF Mg]) (auto simp: space_pair_measure)
   817   finally show ?thesis ..
  1076   finally show ?thesis ..
   818 qed
  1077 qed
   819 
  1078 
       
  1079 lemma (in pair_prob_space) pair_measure_eq_bind:
       
  1080   "(M1 \<Otimes>\<^sub>M M2) = (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y))))"
       
  1081 proof (rule measure_eqI)
       
  1082   have ps_M2: "prob_space M2" by unfold_locales
       
  1083   note return_measurable[measurable]
       
  1084   have 1: "(\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y))) \<in> measurable M1 (subprob_algebra (M1 \<Otimes>\<^sub>M M2))"
       
  1085     by (auto simp add: space_subprob_algebra ps_M2
       
  1086              intro!: measurable_bind[where N=M2] measurable_const prob_space_imp_subprob_space)
       
  1087   show "sets (M1 \<Otimes>\<^sub>M M2) = sets (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y))))"
       
  1088     by (simp add: M1.not_empty sets_bind[OF 1])
       
  1089   fix A assume [measurable]: "A \<in> sets (M1 \<Otimes>\<^sub>M M2)"
       
  1090   show "emeasure (M1 \<Otimes>\<^sub>M M2) A = emeasure (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y)))) A"
       
  1091     by (auto simp: M2.emeasure_pair_measure emeasure_bind[OF _ 1] M1.not_empty
       
  1092                           emeasure_bind[where N="M1 \<Otimes>\<^sub>M M2"] M2.not_empty
       
  1093              intro!: nn_integral_cong)
       
  1094 qed
       
  1095 
       
  1096 lemma (in pair_prob_space) bind_rotate:
       
  1097   assumes C[measurable]: "(\<lambda>(x, y). C x y) \<in> measurable (M1 \<Otimes>\<^sub>M M2) (subprob_algebra N)"
       
  1098   shows "(M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. C x y))) = (M2 \<guillemotright>= (\<lambda>y. M1 \<guillemotright>= (\<lambda>x. C x y)))"
       
  1099 proof - 
       
  1100   interpret swap: pair_prob_space M2 M1 by unfold_locales
       
  1101   note measurable_bind[where N="M2", measurable]
       
  1102   note measurable_bind[where N="M1", measurable]
       
  1103   have [simp]: "M1 \<in> space (subprob_algebra M1)" "M2 \<in> space (subprob_algebra M2)"
       
  1104     by (auto simp: space_subprob_algebra) unfold_locales
       
  1105   have "(M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. C x y))) = 
       
  1106     (M1 \<guillemotright>= (\<lambda>x. M2 \<guillemotright>= (\<lambda>y. return (M1 \<Otimes>\<^sub>M M2) (x, y)))) \<guillemotright>= (\<lambda>(x, y). C x y)"
       
  1107     by (auto intro!: bind_cong simp: bind_return[where N=N] space_pair_measure bind_assoc[where N="M1 \<Otimes>\<^sub>M M2" and R=N])
       
  1108   also have "\<dots> = (distr (M2 \<Otimes>\<^sub>M M1) (M1 \<Otimes>\<^sub>M M2) (\<lambda>(x, y). (y, x))) \<guillemotright>= (\<lambda>(x, y). C x y)"
       
  1109     unfolding pair_measure_eq_bind[symmetric] distr_pair_swap[symmetric] ..
       
  1110   also have "\<dots> = (M2 \<guillemotright>= (\<lambda>x. M1 \<guillemotright>= (\<lambda>y. return (M2 \<Otimes>\<^sub>M M1) (x, y)))) \<guillemotright>= (\<lambda>(y, x). C x y)"
       
  1111     unfolding swap.pair_measure_eq_bind[symmetric]
       
  1112     by (auto simp add: space_pair_measure M1.not_empty M2.not_empty bind_distr[OF _ C] intro!: bind_cong)
       
  1113   also have "\<dots> = (M2 \<guillemotright>= (\<lambda>y. M1 \<guillemotright>= (\<lambda>x. C x y)))"
       
  1114     by (auto intro!: bind_cong simp: bind_return[where N=N] space_pair_measure bind_assoc[where N="M2 \<Otimes>\<^sub>M M1" and R=N])
       
  1115   finally show ?thesis .
       
  1116 qed
       
  1117 
   820 section {* Measures form a $\omega$-chain complete partial order *}
  1118 section {* Measures form a $\omega$-chain complete partial order *}
   821 
  1119 
   822 definition SUP_measure :: "(nat \<Rightarrow> 'a measure) \<Rightarrow> 'a measure" where
  1120 definition SUP_measure :: "(nat \<Rightarrow> 'a measure) \<Rightarrow> 'a measure" where
   823   "SUP_measure M = measure_of (\<Union>i. space (M i)) (\<Union>i. sets (M i)) (\<lambda>A. SUP i. emeasure (M i) A)"
  1121   "SUP_measure M = measure_of (\<Union>i. space (M i)) (\<Union>i. sets (M i)) (\<lambda>A. SUP i. emeasure (M i) A)"
   824 
  1122