src/HOL/Probability/Probability_Mass_Function.thy
author Andreas Lochbihler
Fri Nov 21 12:11:44 2014 +0100 (2014-11-21)
changeset 59023 4999a616336c
parent 59002 2c8b2fb54b88
child 59024 5fcfeae84b96
permissions -rw-r--r--
register pmf as BNF
     1 (*  Title:      HOL/Probability/Probability_Mass_Function.thy
     2     Author:     Johannes Hölzl, TU München 
     3     Author:     Andreas Lochbihler, ETH Zurich
     4 *)
     5 
     6 section \<open> Probability mass function \<close>
     7 
     8 theory Probability_Mass_Function
     9 imports
    10   Giry_Monad
    11   "~~/src/HOL/Library/Multiset"
    12 begin
    13 
    14 lemma (in finite_measure) countable_support: (* replace version in pmf *)
    15   "countable {x. measure M {x} \<noteq> 0}"
    16 proof cases
    17   assume "measure M (space M) = 0"
    18   with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
    19     by auto
    20   then show ?thesis
    21     by simp
    22 next
    23   let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
    24   assume "?M \<noteq> 0"
    25   then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
    26     using reals_Archimedean[of "?m x / ?M" for x]
    27     by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
    28   have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
    29   proof (rule ccontr)
    30     fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
    31     then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
    32       by (metis infinite_arbitrarily_large)
    33     from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x" 
    34       by auto
    35     { fix x assume "x \<in> X"
    36       from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
    37       then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
    38     note singleton_sets = this
    39     have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
    40       using `?M \<noteq> 0` 
    41       by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc field_simps less_le measure_nonneg)
    42     also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
    43       by (rule setsum_mono) fact
    44     also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
    45       using singleton_sets `finite X`
    46       by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
    47     finally have "?M < measure M (\<Union>x\<in>X. {x})" .
    48     moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
    49       using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
    50     ultimately show False by simp
    51   qed
    52   show ?thesis
    53     unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
    54 qed
    55 
    56 lemma (in finite_measure) AE_support_countable:
    57   assumes [simp]: "sets M = UNIV"
    58   shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
    59 proof
    60   assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
    61   then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
    62     by auto
    63   then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) = 
    64     (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
    65     by (subst emeasure_UN_countable)
    66        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
    67   also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
    68     by (auto intro!: nn_integral_cong split: split_indicator)
    69   also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
    70     by (subst emeasure_UN_countable)
    71        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
    72   also have "\<dots> = emeasure M (space M)"
    73     using ae by (intro emeasure_eq_AE) auto
    74   finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
    75     by (simp add: emeasure_single_in_space cong: rev_conj_cong)
    76   with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
    77   have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
    78     by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
    79   then show "AE x in M. measure M {x} \<noteq> 0"
    80     by (auto simp: emeasure_eq_measure)
    81 qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
    82 
    83 subsection {* PMF as measure *}
    84 
    85 typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
    86   morphisms measure_pmf Abs_pmf
    87   by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
    88      (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
    89 
    90 declare [[coercion measure_pmf]]
    91 
    92 lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
    93   using pmf.measure_pmf[of p] by auto
    94 
    95 interpretation measure_pmf!: prob_space "measure_pmf M" for M
    96   by (rule prob_space_measure_pmf)
    97 
    98 interpretation measure_pmf!: subprob_space "measure_pmf M" for M
    99   by (rule prob_space_imp_subprob_space) unfold_locales
   100 
   101 locale pmf_as_measure
   102 begin
   103 
   104 setup_lifting type_definition_pmf
   105 
   106 end
   107 
   108 context
   109 begin
   110 
   111 interpretation pmf_as_measure .
   112 
   113 lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
   114 
   115 lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
   116 
   117 lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
   118   "\<lambda>f M. distr M (count_space UNIV) f"
   119 proof safe
   120   fix M and f :: "'a \<Rightarrow> 'b"
   121   let ?D = "distr M (count_space UNIV) f"
   122   assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
   123   interpret prob_space M by fact
   124   from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
   125   proof eventually_elim
   126     fix x
   127     have "measure M {x} \<le> measure M (f -` {f x})"
   128       by (intro finite_measure_mono) auto
   129     then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
   130       using measure_nonneg[of M "{x}"] by auto
   131   qed
   132   then show "AE x in ?D. measure ?D {x} \<noteq> 0"
   133     by (simp add: AE_distr_iff measure_distr measurable_def)
   134 qed (auto simp: measurable_def prob_space.prob_space_distr)
   135 
   136 declare [[coercion set_pmf]]
   137 
   138 lemma countable_set_pmf [simp]: "countable (set_pmf p)"
   139   by transfer (metis prob_space.finite_measure finite_measure.countable_support)
   140 
   141 lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
   142   by transfer metis
   143 
   144 lemma sets_measure_pmf_count_space: "sets (measure_pmf M) = sets (count_space UNIV)"
   145   by simp
   146 
   147 lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
   148   using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
   149 
   150 lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
   151   by (auto simp: measurable_def)
   152 
   153 lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
   154   by (intro measurable_cong_sets) simp_all
   155 
   156 lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
   157   by transfer (simp add: less_le measure_nonneg)
   158 
   159 lemma pmf_nonneg: "0 \<le> pmf p x"
   160   by transfer (simp add: measure_nonneg)
   161 
   162 lemma pmf_le_1: "pmf p x \<le> 1"
   163   by (simp add: pmf.rep_eq)
   164 
   165 lemma emeasure_pmf_single:
   166   fixes M :: "'a pmf"
   167   shows "emeasure M {x} = pmf M x"
   168   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   169 
   170 lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
   171   by transfer simp
   172 
   173 lemma emeasure_pmf_single_eq_zero_iff:
   174   fixes M :: "'a pmf"
   175   shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
   176   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   177 
   178 lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
   179 proof -
   180   { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
   181     with P have "AE x in M. x \<noteq> y"
   182       by auto
   183     with y have False
   184       by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
   185   then show ?thesis
   186     using AE_measure_pmf[of M] by auto
   187 qed
   188 
   189 lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
   190   using AE_measure_pmf[of M] by (intro notI) simp
   191 
   192 lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
   193   by transfer simp
   194 
   195 lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
   196   by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
   197 
   198 lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S"
   199 using emeasure_measure_pmf_finite[of S M]
   200 by(simp add: measure_pmf.emeasure_eq_measure)
   201 
   202 lemma nn_integral_measure_pmf_support:
   203   fixes f :: "'a \<Rightarrow> ereal"
   204   assumes f: "finite A" and nn: "\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x" "\<And>x. x \<in> set_pmf M \<Longrightarrow> x \<notin> A \<Longrightarrow> f x = 0"
   205   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
   206 proof -
   207   have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
   208     using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
   209   also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
   210     using assms by (intro nn_integral_indicator_finite) auto
   211   finally show ?thesis
   212     by (simp add: emeasure_measure_pmf_finite)
   213 qed
   214 
   215 lemma nn_integral_measure_pmf_finite:
   216   fixes f :: "'a \<Rightarrow> ereal"
   217   assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
   218   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
   219   using assms by (intro nn_integral_measure_pmf_support) auto
   220 lemma integrable_measure_pmf_finite:
   221   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
   222   shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
   223   by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
   224 
   225 lemma integral_measure_pmf:
   226   assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
   227   shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
   228 proof -
   229   have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
   230     using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
   231   also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
   232     by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
   233   finally show ?thesis .
   234 qed
   235 
   236 lemma integrable_pmf: "integrable (count_space X) (pmf M)"
   237 proof -
   238   have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
   239     by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
   240   then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
   241     by (simp add: integrable_iff_bounded pmf_nonneg)
   242   then show ?thesis
   243     by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def)
   244 qed
   245 
   246 lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
   247 proof -
   248   have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
   249     by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
   250   also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
   251     by (auto intro!: nn_integral_cong_AE split: split_indicator
   252              simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
   253                    AE_count_space set_pmf_iff)
   254   also have "\<dots> = emeasure M (X \<inter> M)"
   255     by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
   256   also have "\<dots> = emeasure M X"
   257     by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
   258   finally show ?thesis
   259     by (simp add: measure_pmf.emeasure_eq_measure)
   260 qed
   261 
   262 lemma integral_pmf_restrict:
   263   "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
   264     (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
   265   by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
   266 
   267 lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
   268 proof -
   269   have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
   270     by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
   271   then show ?thesis
   272     using measure_pmf.emeasure_space_1 by simp
   273 qed
   274 
   275 lemma in_null_sets_measure_pmfI:
   276   "A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)"
   277 using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"]
   278 by(auto simp add: null_sets_def AE_measure_pmf_iff)
   279 
   280 lemma map_pmf_id[simp]: "map_pmf id = id"
   281   by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
   282 
   283 lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
   284   by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
   285 
   286 lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
   287   using map_pmf_compose[of f g] by (simp add: comp_def)
   288 
   289 lemma map_pmf_cong:
   290   assumes "p = q"
   291   shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
   292   unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
   293   by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
   294 
   295 lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
   296   unfolding map_pmf.rep_eq by (subst emeasure_distr) auto
   297 
   298 lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
   299   unfolding map_pmf.rep_eq by (intro nn_integral_distr) auto
   300 
   301 lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f -` {x}) y \<partial>measure_pmf p)"
   302 proof(transfer fixing: f x)
   303   fix p :: "'b measure"
   304   presume "prob_space p"
   305   then interpret prob_space p .
   306   presume "sets p = UNIV"
   307   then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f -` {x}))"
   308     by(simp add: measure_distr measurable_def emeasure_eq_measure)
   309 qed simp_all
   310 
   311 lemma pmf_set_map: 
   312   fixes f :: "'a \<Rightarrow> 'b"
   313   shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
   314 proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
   315   fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
   316   assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
   317   interpret prob_space M by fact
   318   show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
   319   proof safe
   320     fix x assume "measure M (f -` {x}) \<noteq> 0"
   321     moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
   322       using ae by (intro finite_measure_eq_AE) auto
   323     ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
   324       by (metis measure_empty)
   325     then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
   326       by auto
   327   next
   328     fix x assume "measure M {x} \<noteq> 0"
   329     then have "0 < measure M {x}"
   330       using measure_nonneg[of M "{x}"] by auto
   331     also have "measure M {x} \<le> measure M (f -` {f x})"
   332       by (intro finite_measure_mono) auto
   333     finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
   334       by simp
   335   qed
   336 qed
   337 
   338 lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M"
   339   using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
   340 
   341 lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A"
   342 proof -
   343   have "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = (\<integral>\<^sup>+ x. pmf p x \<partial>count_space (A \<inter> set_pmf p))"
   344     by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
   345   also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})"
   346     by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def)
   347   also have "\<dots> = emeasure (measure_pmf p) ((\<Union>x\<in>A \<inter> set_pmf p. {x}) \<union> {x. x \<in> A \<and> x \<notin> set_pmf p})"
   348     by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI)
   349   also have "\<dots> = emeasure (measure_pmf p) A"
   350     by(auto intro: arg_cong2[where f=emeasure])
   351   finally show ?thesis .
   352 qed
   353 
   354 subsection {* PMFs as function *}
   355 
   356 context
   357   fixes f :: "'a \<Rightarrow> real"
   358   assumes nonneg: "\<And>x. 0 \<le> f x"
   359   assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   360 begin
   361 
   362 lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
   363 proof (intro conjI)
   364   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   365     by (simp split: split_indicator)
   366   show "AE x in density (count_space UNIV) (ereal \<circ> f).
   367     measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
   368     by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator)
   369   show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
   370     by default (simp add: emeasure_density prob)
   371 qed simp
   372 
   373 lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
   374 proof transfer
   375   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   376     by (simp split: split_indicator)
   377   fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
   378     by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg)
   379 qed
   380 
   381 end
   382 
   383 lemma embed_pmf_transfer:
   384   "rel_fun (eq_onp (\<lambda>f. (\<forall>x. 0 \<le> f x) \<and> (\<integral>\<^sup>+x. ereal (f x) \<partial>count_space UNIV) = 1)) pmf_as_measure.cr_pmf (\<lambda>f. density (count_space UNIV) (ereal \<circ> f)) embed_pmf"
   385   by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
   386 
   387 lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
   388 proof (transfer, elim conjE)
   389   fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
   390   assume "prob_space M" then interpret prob_space M .
   391   show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
   392   proof (rule measure_eqI)
   393     fix A :: "'a set"
   394     have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
   395       (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
   396       by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
   397     also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
   398       by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
   399     also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
   400       by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
   401          (auto simp: disjoint_family_on_def)
   402     also have "\<dots> = emeasure M A"
   403       using ae by (intro emeasure_eq_AE) auto
   404     finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
   405       using emeasure_space_1 by (simp add: emeasure_density)
   406   qed simp
   407 qed
   408 
   409 lemma td_pmf_embed_pmf:
   410   "type_definition pmf embed_pmf {f::'a \<Rightarrow> real. (\<forall>x. 0 \<le> f x) \<and> (\<integral>\<^sup>+x. ereal (f x) \<partial>count_space UNIV) = 1}"
   411   unfolding type_definition_def
   412 proof safe
   413   fix p :: "'a pmf"
   414   have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
   415     using measure_pmf.emeasure_space_1[of p] by simp
   416   then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
   417     by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
   418 
   419   show "embed_pmf (pmf p) = p"
   420     by (intro measure_pmf_inject[THEN iffD1])
   421        (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
   422 next
   423   fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   424   then show "pmf (embed_pmf f) = f"
   425     by (auto intro!: pmf_embed_pmf)
   426 qed (rule pmf_nonneg)
   427 
   428 end
   429 
   430 locale pmf_as_function
   431 begin
   432 
   433 setup_lifting td_pmf_embed_pmf
   434 
   435 lemma set_pmf_transfer[transfer_rule]: 
   436   assumes "bi_total A"
   437   shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
   438   using `bi_total A`
   439   by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
   440      metis+
   441 
   442 end
   443 
   444 context
   445 begin
   446 
   447 interpretation pmf_as_function .
   448 
   449 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
   450   by transfer auto
   451 
   452 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
   453   by (auto intro: pmf_eqI)
   454 
   455 end
   456 
   457 context
   458 begin
   459 
   460 interpretation pmf_as_function .
   461 
   462 lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
   463   "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
   464   by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
   465            split: split_max split_min)
   466 
   467 lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
   468   by transfer simp
   469 
   470 lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
   471   by transfer simp
   472 
   473 lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
   474   by (auto simp add: set_pmf_iff UNIV_bool)
   475 
   476 lemma nn_integral_bernoulli_pmf[simp]: 
   477   assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
   478   shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
   479   by (subst nn_integral_measure_pmf_support[of UNIV])
   480      (auto simp: UNIV_bool field_simps)
   481 
   482 lemma integral_bernoulli_pmf[simp]: 
   483   assumes [simp]: "0 \<le> p" "p \<le> 1"
   484   shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
   485   by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
   486 
   487 lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
   488 proof
   489   note geometric_sums[of "1 / 2"]
   490   note sums_mult[OF this, of "1 / 2"]
   491   from sums_suminf_ereal[OF this]
   492   show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
   493     by (simp add: nn_integral_count_space_nat field_simps)
   494 qed simp
   495 
   496 lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
   497   by transfer rule
   498 
   499 lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV"
   500   by (auto simp: set_pmf_iff)
   501 
   502 context
   503   fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
   504 begin
   505 
   506 lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
   507 proof
   508   show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"  
   509     using M_not_empty
   510     by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
   511                   setsum_divide_distrib[symmetric])
   512        (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
   513 qed simp
   514 
   515 lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
   516   by transfer rule
   517 
   518 lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
   519   by (auto simp: set_pmf_iff)
   520 
   521 end
   522 
   523 context
   524   fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
   525 begin
   526 
   527 lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
   528 proof
   529   show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"  
   530     using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
   531 qed simp
   532 
   533 lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
   534   by transfer rule
   535 
   536 lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
   537   using S_finite S_not_empty by (auto simp: set_pmf_iff)
   538 
   539 lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1"
   540   by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
   541 
   542 end
   543 
   544 end
   545 
   546 subsection {* Monad interpretation *}
   547 
   548 lemma measurable_measure_pmf[measurable]:
   549   "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
   550   by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
   551 
   552 lemma bind_pmf_cong:
   553   assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
   554   assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
   555   shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
   556 proof (rule measure_eqI)
   557   show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)"
   558     using assms by (subst (1 2) sets_bind) auto
   559 next
   560   fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)"
   561   then have X: "X \<in> sets N"
   562     using assms by (subst (asm) sets_bind) auto
   563   show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X"
   564     using assms
   565     by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
   566        (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
   567 qed
   568 
   569 context
   570 begin
   571 
   572 interpretation pmf_as_measure .
   573 
   574 lift_definition join_pmf :: "'a pmf pmf \<Rightarrow> 'a pmf" is "\<lambda>M. measure_pmf M \<guillemotright>= measure_pmf"
   575 proof (intro conjI)
   576   fix M :: "'a pmf pmf"
   577 
   578   have *: "measure_pmf \<in> measurable (measure_pmf M) (subprob_algebra (count_space UNIV))"
   579     using measurable_measure_pmf[of "\<lambda>x. x"] by simp
   580   
   581   interpret bind: prob_space "measure_pmf M \<guillemotright>= measure_pmf"
   582     apply (rule measure_pmf.prob_space_bind[OF _ *])
   583     apply (auto intro!: AE_I2)
   584     apply unfold_locales
   585     done
   586   show "prob_space (measure_pmf M \<guillemotright>= measure_pmf)"
   587     by intro_locales
   588   show "sets (measure_pmf M \<guillemotright>= measure_pmf) = UNIV"
   589     by (subst sets_bind[OF *]) auto
   590   have "AE x in measure_pmf M \<guillemotright>= measure_pmf. emeasure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
   591     by (auto simp add: AE_bind[OF _ *] AE_measure_pmf_iff emeasure_bind[OF _ *]
   592         nn_integral_0_iff_AE measure_pmf.emeasure_eq_measure measure_le_0_iff set_pmf_iff pmf.rep_eq)
   593   then show "AE x in measure_pmf M \<guillemotright>= measure_pmf. measure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
   594     unfolding bind.emeasure_eq_measure by simp
   595 qed
   596 
   597 lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
   598 proof (transfer fixing: N i)
   599   have N: "subprob_space (measure_pmf N)"
   600     by (rule prob_space_imp_subprob_space) intro_locales
   601   show "measure (measure_pmf N \<guillemotright>= measure_pmf) {i} = integral\<^sup>L (measure_pmf N) (\<lambda>M. measure M {i})"
   602     using measurable_measure_pmf[of "\<lambda>x. x"]
   603     by (intro subprob_space.measure_bind[where N="count_space UNIV", OF N]) auto
   604 qed (auto simp: Transfer.Rel_def rel_fun_def cr_pmf_def)
   605 
   606 lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
   607   by (auto intro!: prob_space_return simp: AE_return measure_return)
   608 
   609 lemma join_return_pmf: "join_pmf (return_pmf M) = M"
   610   by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
   611 
   612 lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)"
   613   by transfer (simp add: distr_return)
   614 
   615 lemma set_return_pmf: "set_pmf (return_pmf x) = {x}"
   616   by transfer (auto simp add: measure_return split: split_indicator)
   617 
   618 lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x"
   619   by transfer (simp add: measure_return)
   620 
   621 lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
   622   unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
   623 
   624 lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
   625   unfolding return_pmf.rep_eq by (intro emeasure_return) auto
   626 
   627 end
   628 
   629 definition "bind_pmf M f = join_pmf (map_pmf f M)"
   630 
   631 lemma (in pmf_as_measure) bind_transfer[transfer_rule]:
   632   "rel_fun pmf_as_measure.cr_pmf (rel_fun (rel_fun op = pmf_as_measure.cr_pmf) pmf_as_measure.cr_pmf) op \<guillemotright>= bind_pmf"
   633 proof (auto simp: pmf_as_measure.cr_pmf_def rel_fun_def bind_pmf_def join_pmf.rep_eq map_pmf.rep_eq)
   634   fix M f and g :: "'a \<Rightarrow> 'b pmf" assume "\<forall>x. f x = measure_pmf (g x)"
   635   then have f: "f = (\<lambda>x. measure_pmf (g x))"
   636     by auto
   637   show "measure_pmf M \<guillemotright>= f = distr (measure_pmf M) (count_space UNIV) g \<guillemotright>= measure_pmf"
   638     unfolding f by (subst bind_distr[OF _ measurable_measure_pmf]) auto
   639 qed
   640 
   641 lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
   642   by (auto intro!: integral_distr simp: bind_pmf_def pmf_join map_pmf.rep_eq)
   643 
   644 lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
   645   unfolding bind_pmf_def map_return_pmf join_return_pmf ..
   646 
   647 lemma set_bind_pmf: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
   648   apply (simp add: set_eq_iff set_pmf_iff pmf_bind)
   649   apply (subst integral_nonneg_eq_0_iff_AE)
   650   apply (auto simp: pmf_nonneg pmf_le_1 AE_measure_pmf_iff
   651               intro!: measure_pmf.integrable_const_bound[where B=1])
   652   done
   653 
   654 lemma measurable_pair_restrict_pmf2:
   655   assumes "countable A"
   656   assumes "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
   657   shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L"
   658   apply (subst measurable_cong_sets)
   659   apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
   660   apply (simp_all add: restrict_count_space)
   661   apply (subst split_eta[symmetric])
   662   unfolding measurable_split_conv
   663   apply (rule measurable_compose_countable'[OF _ measurable_snd `countable A`])
   664   apply (rule measurable_compose[OF measurable_fst])
   665   apply fact
   666   done
   667 
   668 lemma measurable_pair_restrict_pmf1:
   669   assumes "countable A"
   670   assumes "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
   671   shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
   672   apply (subst measurable_cong_sets)
   673   apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
   674   apply (simp_all add: restrict_count_space)
   675   apply (subst split_eta[symmetric])
   676   unfolding measurable_split_conv
   677   apply (rule measurable_compose_countable'[OF _ measurable_fst `countable A`])
   678   apply (rule measurable_compose[OF measurable_snd])
   679   apply fact
   680   done
   681                                 
   682 lemma bind_commute_pmf: "bind_pmf A (\<lambda>x. bind_pmf B (C x)) = bind_pmf B (\<lambda>y. bind_pmf A (\<lambda>x. C x y))"
   683   unfolding pmf_eq_iff pmf_bind
   684 proof
   685   fix i
   686   interpret B: prob_space "restrict_space B B"
   687     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
   688        (auto simp: AE_measure_pmf_iff)
   689   interpret A: prob_space "restrict_space A A"
   690     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
   691        (auto simp: AE_measure_pmf_iff)
   692 
   693   interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
   694     by unfold_locales
   695 
   696   have "(\<integral> x. \<integral> y. pmf (C x y) i \<partial>B \<partial>A) = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>A)"
   697     by (rule integral_cong) (auto intro!: integral_pmf_restrict)
   698   also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
   699     by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
   700               countable_set_pmf borel_measurable_count_space)
   701   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
   702     by (rule AB.Fubini_integral[symmetric])
   703        (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
   704              simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
   705   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
   706     by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
   707               countable_set_pmf borel_measurable_count_space)
   708   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
   709     by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
   710   finally show "(\<integral> x. \<integral> y. pmf (C x y) i \<partial>B \<partial>A) = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)" .
   711 qed
   712 
   713 
   714 context
   715 begin
   716 
   717 interpretation pmf_as_measure .
   718 
   719 lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))"
   720   by transfer simp
   721 
   722 lemma nn_integral_bind_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>bind_pmf M N) = (\<integral>\<^sup>+x. \<integral>\<^sup>+y. f y \<partial>N x \<partial>M)"
   723   using measurable_measure_pmf[of N]
   724   unfolding measure_pmf_bind
   725   apply (subst (1 3) nn_integral_max_0[symmetric])
   726   apply (intro nn_integral_bind[where B="count_space UNIV"])
   727   apply auto
   728   done
   729 
   730 lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
   731   using measurable_measure_pmf[of N]
   732   unfolding measure_pmf_bind
   733   by (subst emeasure_bind[where N="count_space UNIV"]) auto
   734 
   735 lemma bind_return_pmf': "bind_pmf N return_pmf = N"
   736 proof (transfer, clarify)
   737   fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N"
   738     by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
   739 qed
   740 
   741 lemma bind_return_pmf'': "bind_pmf N (\<lambda>x. return_pmf (f x)) = map_pmf f N"
   742 proof (transfer, clarify)
   743   fix N :: "'b measure" and f :: "'b \<Rightarrow> 'a" assume "prob_space N" "sets N = UNIV"
   744   then show "N \<guillemotright>= (\<lambda>x. return (count_space UNIV) (f x)) = distr N (count_space UNIV) f"
   745     by (subst bind_return_distr[symmetric])
   746        (auto simp: prob_space.not_empty measurable_def comp_def)
   747 qed
   748 
   749 lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
   750   by transfer
   751      (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
   752            simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
   753 
   754 end
   755 
   756 definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
   757 
   758 lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
   759   unfolding pair_pmf_def pmf_bind pmf_return
   760   apply (subst integral_measure_pmf[where A="{b}"])
   761   apply (auto simp: indicator_eq_0_iff)
   762   apply (subst integral_measure_pmf[where A="{a}"])
   763   apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
   764   done
   765 
   766 lemma set_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
   767   unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
   768 
   769 lemma bind_pair_pmf:
   770   assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
   771   shows "measure_pmf (pair_pmf A B) \<guillemotright>= M = (measure_pmf A \<guillemotright>= (\<lambda>x. measure_pmf B \<guillemotright>= (\<lambda>y. M (x, y))))"
   772     (is "?L = ?R")
   773 proof (rule measure_eqI)
   774   have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
   775     using M[THEN measurable_space] by (simp_all add: space_pair_measure)
   776 
   777   have sets_eq_N: "sets ?L = N"
   778     by (simp add: sets_bind[OF M'])
   779   show "sets ?L = sets ?R"
   780     unfolding sets_eq_N
   781     apply (subst sets_bind[where N=N])
   782     apply (rule measurable_bind)
   783     apply (rule measurable_compose[OF _ measurable_measure_pmf])
   784     apply measurable
   785     apply (auto intro!: sets_pair_measure_cong sets_measure_pmf_count_space)
   786     done
   787   fix X assume "X \<in> sets ?L"
   788   then have X[measurable]: "X \<in> sets N"
   789     unfolding sets_eq_N .
   790   then show "emeasure ?L X = emeasure ?R X"
   791     apply (simp add: emeasure_bind[OF _ M' X])
   792     unfolding pair_pmf_def measure_pmf_bind[of A]
   793     apply (subst nn_integral_bind)
   794     apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
   795     apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
   796     apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
   797     apply measurable
   798     unfolding measure_pmf_bind
   799     apply (subst nn_integral_bind)
   800     apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
   801     apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
   802     apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
   803     apply measurable
   804     apply (simp add: nn_integral_measure_pmf_finite set_return_pmf emeasure_nonneg pmf_return one_ereal_def[symmetric])
   805     apply (subst emeasure_bind[OF _ _ X])
   806     apply simp
   807     apply (rule measurable_bind[where N="count_space UNIV"])
   808     apply (rule measurable_compose[OF _ measurable_measure_pmf])
   809     apply measurable
   810     apply (rule sets_pair_measure_cong sets_measure_pmf_count_space refl)+
   811     apply (subst measurable_cong_sets[OF sets_pair_measure_cong[OF sets_measure_pmf_count_space refl] refl])
   812     apply simp
   813     apply (subst emeasure_bind[OF _ _ X])
   814     apply simp
   815     apply (rule measurable_compose[OF _ M])
   816     apply (auto simp: space_pair_measure)
   817     done
   818 qed
   819 
   820 inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
   821 for R p q
   822 where
   823   "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y; 
   824      map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
   825   \<Longrightarrow> rel_pmf R p q"
   826 
   827 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
   828 proof -
   829   show "map_pmf id = id" by (rule map_pmf_id)
   830   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
   831   show "\<And>f g::'a \<Rightarrow> 'b. \<And>p. (\<And>x. x \<in> set_pmf p \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g p"
   832     by (intro map_pmf_cong refl)
   833 
   834   show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
   835     by (rule pmf_set_map)
   836 
   837   { fix p :: "'s pmf"
   838     have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
   839       by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
   840          (auto intro: countable_set_pmf inj_on_to_nat_on)
   841     also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
   842       by (metis Field_natLeq card_of_least natLeq_Well_order)
   843     finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
   844 
   845   show "\<And>R. rel_pmf R =
   846          (BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
   847          BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
   848      by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps)
   849 
   850   { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
   851     assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
   852       and x: "x \<in> set_pmf p"
   853     thus "f x = g x" by simp }
   854 
   855   fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
   856   { fix p q r
   857     assume pq: "rel_pmf R p q"
   858       and qr:"rel_pmf S q r"
   859     from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
   860       and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
   861     from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
   862       and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
   863 
   864     have support_subset: "set_pmf pq O set_pmf qr \<subseteq> set_pmf p \<times> set_pmf r"
   865       by(auto simp add: p r set_map_pmf intro: rev_image_eqI)
   866 
   867     let ?A = "\<lambda>y. {x. (x, y) \<in> set_pmf pq}"
   868       and ?B = "\<lambda>y. {z. (y, z) \<in> set_pmf qr}"
   869 
   870 
   871     def ppp \<equiv> "\<lambda>A. \<lambda>f :: 'a \<Rightarrow> real. \<lambda>n. if n \<in> to_nat_on A ` A then f (from_nat_into A n) else 0"
   872     have [simp]: "\<And>A f n. (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> ppp A f n"
   873                  "\<And>A f n x. \<lbrakk> x \<in> A; countable A \<rbrakk> \<Longrightarrow> ppp A f (to_nat_on A x) = f x"
   874                  "\<And>A f n. n \<notin> to_nat_on A ` A \<Longrightarrow> ppp A f n = 0"
   875       by(auto simp add: ppp_def intro: from_nat_into)
   876     def rrr \<equiv> "\<lambda>A. \<lambda>f :: 'c \<Rightarrow> real. \<lambda>n. if n \<in> to_nat_on A ` A then f (from_nat_into A n) else 0"
   877     have [simp]: "\<And>A f n. (\<And>x. x \<in> A \<Longrightarrow> 0 \<le> f x) \<Longrightarrow> 0 \<le> rrr A f n"
   878                  "\<And>A f n x. \<lbrakk> x \<in> A; countable A \<rbrakk> \<Longrightarrow> rrr A f (to_nat_on A x) = f x"
   879                  "\<And>A f n. n \<notin> to_nat_on A ` A \<Longrightarrow> rrr A f n = 0"
   880       by(auto simp add: rrr_def intro: from_nat_into)
   881 
   882     def pp \<equiv> "\<lambda>y. ppp (?A y) (\<lambda>x. pmf pq (x, y))"
   883      and rr \<equiv> "\<lambda>y. rrr (?B y) (\<lambda>z. pmf qr (y, z))"
   884 
   885     have pos_p [simp]: "\<And>y n. 0 \<le> pp y n"
   886       and pos_r [simp]: "\<And>y n. 0 \<le> rr y n"
   887       by(simp_all add: pmf_nonneg pp_def rr_def)
   888     { fix y n
   889       have "pp y n \<le> 0 \<longleftrightarrow> pp y n = 0" "\<not> 0 < pp y n \<longleftrightarrow> pp y n = 0"
   890         and "min (pp y n) 0 = 0" "min 0 (pp y n) = 0"
   891         using pos_p[of y n] by(auto simp del: pos_p) }
   892     note pp_convs [simp] = this
   893     { fix y n
   894       have "rr y n \<le> 0 \<longleftrightarrow> rr y n = 0" "\<not> 0 < rr y n \<longleftrightarrow> rr y n = 0"
   895         and "min (rr y n) 0 = 0" "min 0 (rr y n) = 0"
   896         using pos_r[of y n] by(auto simp del: pos_r) }
   897     note r_convs [simp] = this
   898 
   899     have "\<And>y. ?A y \<subseteq> set_pmf p" by(auto simp add: p set_map_pmf intro: rev_image_eqI)
   900     then have [simp]: "\<And>y. countable (?A y)" by(rule countable_subset) simp
   901 
   902     have "\<And>y. ?B y \<subseteq> set_pmf r" by(auto simp add: r set_map_pmf intro: rev_image_eqI)
   903     then have [simp]: "\<And>y. countable (?B y)" by(rule countable_subset) simp
   904 
   905     let ?P = "\<lambda>y. to_nat_on (?A y)"
   906       and ?R = "\<lambda>y. to_nat_on (?B y)"
   907 
   908     have eq: "\<And>y. (\<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV) = \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
   909     proof -
   910       fix y
   911       have "(\<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV) = (\<integral>\<^sup>+ x. pp y x \<partial>count_space (?P y ` ?A y))"
   912         by(auto simp add: pp_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong)
   913       also have "\<dots> = (\<integral>\<^sup>+ x. pp y (?P y x) \<partial>count_space (?A y))"
   914         by(intro nn_integral_bij_count_space[symmetric] inj_on_imp_bij_betw inj_on_to_nat_on) simp
   915       also have "\<dots> = (\<integral>\<^sup>+ x. pmf pq (x, y) \<partial>count_space (?A y))"
   916         by(rule nn_integral_cong)(simp add: pp_def)
   917       also have "\<dots> = \<integral>\<^sup>+ x. emeasure (measure_pmf pq) {(x, y)} \<partial>count_space (?A y)"
   918         by(simp add: emeasure_pmf_single)
   919       also have "\<dots> = emeasure (measure_pmf pq) (\<Union>x\<in>?A y. {(x, y)})"
   920         by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
   921       also have "\<dots> = emeasure (measure_pmf pq) ((\<Union>x\<in>?A y. {(x, y)}) \<union> {(x, y'). x \<notin> ?A y \<and> y' = y})"
   922         by(rule emeasure_Un_null_set[symmetric])+
   923           (auto simp add: q set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
   924       also have "\<dots> = emeasure (measure_pmf pq) (snd -` {y})"
   925         by(rule arg_cong2[where f=emeasure])+auto
   926       also have "\<dots> = pmf q y" by(simp add: q ereal_pmf_map)
   927       also have "\<dots> = emeasure (measure_pmf qr) (fst -` {y})"
   928         by(simp add: q' ereal_pmf_map)
   929       also have "\<dots> = emeasure (measure_pmf qr) ((\<Union>z\<in>?B y. {(y, z)}) \<union> {(y', z). z \<notin> ?B y \<and> y' = y})"
   930         by(rule arg_cong2[where f=emeasure])+auto
   931       also have "\<dots> = emeasure (measure_pmf qr) (\<Union>z\<in>?B y. {(y, z)})"
   932         by(rule emeasure_Un_null_set)
   933           (auto simp add: q' set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
   934       also have "\<dots> = \<integral>\<^sup>+ z. emeasure (measure_pmf qr) {(y, z)} \<partial>count_space (?B y)"
   935         by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
   936       also have "\<dots> = (\<integral>\<^sup>+ z. pmf qr (y, z) \<partial>count_space (?B y))"
   937         by(simp add: emeasure_pmf_single)
   938       also have "\<dots> = (\<integral>\<^sup>+ z. rr y (?R y z) \<partial>count_space (?B y))"
   939         by(rule nn_integral_cong)(simp add: rr_def)
   940       also have "\<dots> = (\<integral>\<^sup>+ z. rr y z \<partial>count_space (?R y ` ?B y))"
   941         by(intro nn_integral_bij_count_space inj_on_imp_bij_betw inj_on_to_nat_on) simp
   942       also have "\<dots> = \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
   943         by(auto simp add: rr_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong)
   944       finally show "?thesis y" .
   945     qed
   946 
   947     def assign_aux \<equiv> "\<lambda>y remainder start weight z.
   948        if z < start then 0
   949        else if z = start then min weight remainder
   950        else if remainder + setsum (rr y) {Suc start ..<z} < weight then min (weight - remainder - setsum (rr y) {Suc start..<z}) (rr y z) else 0"
   951     hence assign_aux_alt_def: "\<And>y remainder start weight z. assign_aux y remainder start weight z = 
   952        (if z < start then 0
   953         else if z = start then min weight remainder
   954         else if remainder + setsum (rr y) {Suc start ..<z} < weight then min (weight - remainder - setsum (rr y) {Suc start..<z}) (rr y z) else 0)"
   955        by simp
   956     { fix y and remainder :: real and start and weight :: real
   957       assume weight_nonneg: "0 \<le> weight"
   958       let ?assign_aux = "assign_aux y remainder start weight"
   959       { fix z
   960         have "setsum ?assign_aux {..<z} =
   961            (if z \<le> start then 0 else if remainder + setsum (rr y) {Suc start..<z} < weight then remainder + setsum (rr y) {Suc start..<z} else weight)"
   962         proof(induction z)
   963           case (Suc z) show ?case
   964             by(auto simp add: Suc.IH assign_aux_alt_def[where z=z] not_less)(metis add.commute add.left_commute add_increasing pos_r)
   965         qed(auto simp add: assign_aux_def) }
   966       note setsum_start_assign_aux = this
   967       moreover {
   968         assume remainder_nonneg: "0 \<le> remainder"
   969         have [simp]: "\<And>z. 0 \<le> ?assign_aux z"
   970           by(simp add: assign_aux_def weight_nonneg remainder_nonneg)
   971         moreover have "\<And>z. \<lbrakk> rr y z = 0; remainder \<le> rr y start \<rbrakk> \<Longrightarrow> ?assign_aux z = 0"
   972           using remainder_nonneg weight_nonneg
   973           by(auto simp add: assign_aux_def min_def)
   974         moreover have "(\<integral>\<^sup>+ z. ?assign_aux z \<partial>count_space UNIV) = 
   975           min weight (\<integral>\<^sup>+ z. (if z < start then 0 else if z = start then remainder else rr y z) \<partial>count_space UNIV)"
   976           (is "?lhs = ?rhs" is "_ = min _ (\<integral>\<^sup>+ y. ?f y \<partial>_)")
   977         proof -
   978           have "?lhs = (SUP n. \<Sum>z<n. ereal (?assign_aux z))"
   979             by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
   980           also have "\<dots> = (SUP n. min weight (\<Sum>z<n. ?f z))"
   981           proof(rule arg_cong2[where f=SUPREMUM] ext refl)+
   982             fix n
   983             have "(\<Sum>z<n. ereal (?assign_aux z)) = min weight ((if n > start then remainder else 0) + setsum ?f {Suc start..<n})"
   984               using weight_nonneg remainder_nonneg by(simp add: setsum_start_assign_aux min_def)
   985             also have "\<dots> = min weight (setsum ?f {start..<n})"
   986               by(simp add: setsum_head_upt_Suc)
   987             also have "\<dots> = min weight (setsum ?f {..<n})"
   988               by(intro arg_cong2[where f=min] setsum.mono_neutral_left) auto
   989             finally show "(\<Sum>z<n. ereal (?assign_aux z)) = \<dots>" .
   990           qed
   991           also have "\<dots> = min weight (SUP n. setsum ?f {..<n})"
   992             unfolding inf_min[symmetric] by(subst inf_SUP) simp
   993           also have "\<dots> = ?rhs"
   994             by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP remainder_nonneg)
   995           finally show ?thesis .
   996         qed
   997         moreover note calculation }
   998       moreover note calculation }
   999     note setsum_start_assign_aux = this(1)
  1000       and assign_aux_nonneg [simp] = this(2)
  1001       and assign_aux_eq_0_outside = this(3)
  1002       and nn_integral_assign_aux = this(4)
  1003     { fix y and remainder :: real and start target
  1004       have "setsum (rr y) {Suc start..<target} \<ge> 0" by(simp add: setsum_nonneg)
  1005       moreover assume "0 \<le> remainder"
  1006       ultimately have "assign_aux y remainder start 0 target = 0"
  1007         by(auto simp add: assign_aux_def min_def) }
  1008     note assign_aux_weight_0 [simp] = this
  1009 
  1010     def find_start \<equiv> "\<lambda>y weight. if \<exists>n. weight \<le> setsum (rr y)  {..n} then Some (LEAST n. weight \<le> setsum (rr y) {..n}) else None"
  1011     have find_start_eq_Some_above:
  1012       "\<And>y weight n. find_start y weight = Some n \<Longrightarrow> weight \<le> setsum (rr y) {..n}"
  1013       by(drule sym)(auto simp add: find_start_def split: split_if_asm intro: LeastI)
  1014     { fix y weight n
  1015       assume find_start: "find_start y weight = Some n"
  1016       and weight: "0 \<le> weight"
  1017       have "setsum (rr y) {..n} \<le> rr y n + weight"
  1018       proof(rule ccontr)
  1019         assume "\<not> ?thesis"
  1020         hence "rr y n + weight < setsum (rr y) {..n}" by simp
  1021         moreover with weight obtain n' where "n = Suc n'" by(cases n) auto
  1022         ultimately have "weight \<le> setsum (rr y) {..n'}" by simp
  1023         hence "(LEAST n. weight \<le> setsum (rr y) {..n}) \<le> n'" by(rule Least_le)
  1024         moreover from find_start have "n = (LEAST n. weight \<le> setsum (rr y) {..n})"
  1025           by(auto simp add: find_start_def split: split_if_asm)
  1026         ultimately show False using \<open>n = Suc n'\<close> by auto
  1027       qed }
  1028     note find_start_eq_Some_least = this
  1029     have find_start_0 [simp]: "\<And>y. find_start y 0 = Some 0"
  1030       by(auto simp add: find_start_def intro!: exI[where x=0])
  1031     { fix y and weight :: real
  1032       assume "weight < \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
  1033       also have "(\<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV) = (SUP n. \<Sum>z<n. ereal (rr y z))"
  1034         by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
  1035       finally obtain n where "weight < (\<Sum>z<n. rr y z)" by(auto simp add: less_SUP_iff)
  1036       hence "weight \<in> dom (find_start y)"
  1037         by(auto simp add: find_start_def)(meson atMost_iff finite_atMost lessThan_iff less_imp_le order_trans pos_r setsum_mono3 subsetI) }
  1038     note in_dom_find_startI = this
  1039     { fix y and w w' :: real and m
  1040       let ?m' = "LEAST m. w' \<le> setsum (rr y) {..m}"
  1041       assume "w' \<le> w"
  1042       also  assume "find_start y w = Some m"
  1043       hence "w \<le> setsum (rr y) {..m}" by(rule find_start_eq_Some_above)
  1044       finally have "find_start y w' = Some ?m'" by(auto simp add: find_start_def)
  1045       moreover from \<open>w' \<le> setsum (rr y) {..m}\<close> have "?m' \<le> m" by(rule Least_le)
  1046       ultimately have "\<exists>m'. find_start y w' = Some m' \<and> m' \<le> m" by blast }
  1047     note find_start_mono = this[rotated]
  1048 
  1049     def assign \<equiv> "\<lambda>y x z. let used = setsum (pp y) {..<x}
  1050       in case find_start y used of None \<Rightarrow> 0
  1051          | Some start \<Rightarrow> assign_aux y (setsum (rr y) {..start} - used) start (pp y x) z"
  1052     hence assign_alt_def: "\<And>y x z. assign y x z = 
  1053       (let used = setsum (pp y) {..<x}
  1054        in case find_start y used of None \<Rightarrow> 0
  1055           | Some start \<Rightarrow> assign_aux y (setsum (rr y) {..start} - used) start (pp y x) z)"
  1056       by simp
  1057     have assign_nonneg [simp]: "\<And>y x z. 0 \<le> assign y x z"
  1058       by(simp add: assign_def diff_le_iff find_start_eq_Some_above split: option.split)
  1059     have assign_eq_0_outside: "\<And>y x z. \<lbrakk> pp y x = 0 \<or> rr y z = 0 \<rbrakk> \<Longrightarrow> assign y x z = 0"
  1060       by(auto simp add: assign_def assign_aux_eq_0_outside diff_le_iff find_start_eq_Some_above find_start_eq_Some_least setsum_nonneg split: option.split)
  1061 
  1062     { fix y x z
  1063       have "(\<Sum>n<Suc x. assign y n z) =
  1064             (case find_start y (setsum (pp y) {..<x}) of None \<Rightarrow> rr y z
  1065              | Some m \<Rightarrow> if z < m then rr y z 
  1066                          else min (rr y z) (max 0 (setsum (pp y) {..<x} + pp y x - setsum (rr y) {..<z})))"
  1067         (is "?lhs x = ?rhs x")
  1068       proof(induction x)
  1069         case 0 thus ?case 
  1070           by(auto simp add: assign_def assign_aux_def setsum_head_upt_Suc atLeast0LessThan[symmetric] not_less field_simps max_def)
  1071       next
  1072         case (Suc x)
  1073         have "?lhs (Suc x) = ?lhs x + assign y (Suc x) z" by simp
  1074         also have "?lhs x = ?rhs x" by(rule Suc.IH)
  1075         also have "?rhs x + assign y (Suc x) z = ?rhs (Suc x)"
  1076         proof(cases "find_start y (setsum (pp y) {..<Suc x})")
  1077           case None
  1078           thus ?thesis
  1079             by(auto split: option.split simp add: assign_def min_def max_def diff_le_iff setsum_nonneg not_le field_simps)
  1080               (metis add.commute add_increasing find_start_def lessThan_Suc_atMost less_imp_le option.distinct(1) setsum_lessThan_Suc)+
  1081         next
  1082           case (Some m)
  1083           have [simp]: "setsum (rr y) {..m} = rr y m + setsum (rr y) {..<m}"
  1084             by(simp add: ivl_disj_un(2)[symmetric])
  1085           from Some obtain m' where m': "find_start y (setsum (pp y) {..<x}) = Some m'" "m' \<le> m"
  1086             by(auto dest: find_start_mono[where w'2="setsum (pp y) {..<x}"])
  1087           moreover {
  1088             assume "z < m"
  1089             then have "setsum (rr y) {..z} \<le> setsum (rr y) {..<m}"
  1090               by(auto intro: setsum_mono3)
  1091             also have "\<dots> \<le> setsum (pp y) {..<Suc x}" using find_start_eq_Some_least[OF Some]
  1092               by(simp add: ivl_disj_un(2)[symmetric] setsum_nonneg)
  1093             finally have "rr y z \<le> max 0 (setsum (pp y) {..<x} + pp y x - setsum (rr y) {..<z})"
  1094               by(auto simp add: ivl_disj_un(2)[symmetric] max_def diff_le_iff simp del: r_convs)
  1095           } moreover {
  1096             assume "m \<le> z"
  1097             have "setsum (pp y) {..<Suc x} \<le> setsum (rr y) {..m}"
  1098               using Some by(rule find_start_eq_Some_above)
  1099             also have "\<dots> \<le> setsum (rr y) {..<Suc z}" using \<open>m \<le> z\<close> by(intro setsum_mono3) auto
  1100             finally have "max 0 (setsum (pp y) {..<x} + pp y x - setsum (rr y) {..<z}) \<le> rr y z" by simp
  1101             moreover have "z \<noteq> m \<Longrightarrow> setsum (rr y) {..m} + setsum (rr y) {Suc m..<z} = setsum (rr y) {..<z}"
  1102               using \<open>m \<le> z\<close>
  1103               by(subst ivl_disj_un(8)[where l="Suc m", symmetric])
  1104                 (simp_all add: setsum_Un ivl_disj_un(2)[symmetric] setsum.neutral)
  1105             moreover note calculation
  1106           } moreover {
  1107             assume "m < z"
  1108             have "setsum (pp y) {..<Suc x} \<le> setsum (rr y) {..m}"
  1109               using Some by(rule find_start_eq_Some_above)
  1110             also have "\<dots> \<le> setsum (rr y) {..<z}" using \<open>m < z\<close> by(intro setsum_mono3) auto
  1111             finally have "max 0 (setsum (pp y) {..<Suc x} - setsum (rr y) {..<z}) = 0" by simp }
  1112           moreover have "setsum (pp y) {..<Suc x} \<ge> setsum (rr y) {..<m}"
  1113             using find_start_eq_Some_least[OF Some]
  1114             by(simp add: setsum_nonneg ivl_disj_un(2)[symmetric])
  1115           moreover hence "setsum (pp y) {..<Suc (Suc x)} \<ge> setsum (rr y) {..<m}"
  1116             by(fastforce intro: order_trans)
  1117           ultimately show ?thesis using Some
  1118             by(auto simp add: assign_def assign_aux_def Let_def field_simps max_def)
  1119         qed
  1120         finally show ?case .
  1121       qed }
  1122     note setsum_assign = this
  1123 
  1124     have nn_integral_assign1: "\<And>y z. (\<integral>\<^sup>+ x. assign y x z \<partial>count_space UNIV) = rr y z"
  1125     proof -
  1126       fix y z
  1127       have "(\<integral>\<^sup>+ x. assign y x z \<partial>count_space UNIV) = (SUP n. ereal (\<Sum>x<n. assign y x z))"
  1128         by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
  1129       also have "\<dots> = rr y z"
  1130       proof(rule antisym)
  1131         show "(SUP n. ereal (\<Sum>x<n. assign y x z)) \<le> rr y z"
  1132         proof(rule SUP_least)
  1133           fix n
  1134           show "ereal (\<Sum>x<n. (assign y x z)) \<le> rr y z"
  1135             using setsum_assign[of y z "n - 1"]
  1136             by(cases n)(simp_all split: option.split)
  1137         qed
  1138         show "rr y z \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))"
  1139         proof(cases "setsum (rr y) {..z} < \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV")
  1140           case True
  1141           then obtain n where "setsum (rr y) {..z} < setsum (pp y) {..<n}"
  1142             by(auto simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP less_SUP_iff)
  1143           moreover have "\<And>k. k < z \<Longrightarrow> setsum (rr y) {..k} \<le> setsum (rr y) {..<z}"
  1144             by(auto intro: setsum_mono3)
  1145           ultimately have "rr y z \<le> (\<Sum>x<Suc n. assign y x z)"
  1146             by(subst setsum_assign)(auto split: option.split dest!: find_start_eq_Some_above simp add: ivl_disj_un(2)[symmetric] add.commute add_increasing le_diff_eq le_max_iff_disj)
  1147           also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))" 
  1148             by(rule SUP_upper) simp
  1149           finally show ?thesis by simp
  1150         next
  1151           case False
  1152           have "setsum (rr y) {..z} = \<integral>\<^sup>+ z. rr y z \<partial>count_space {..z}"
  1153             by(simp add: nn_integral_count_space_finite max_def)
  1154           also have "\<dots> \<le> \<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV"
  1155             by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono)
  1156           also have "\<dots> = \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV" by(simp add: eq)
  1157           finally have *: "setsum (rr y) {..z} = \<dots>" using False by simp
  1158           also have "\<dots> = (SUP n. ereal (\<Sum>x<n. pp y x))"
  1159             by(simp add: nn_integral_count_space_nat suminf_ereal_eq_SUP)
  1160           also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z)) + setsum (rr y) {..<z}"
  1161           proof(rule SUP_least)
  1162             fix n
  1163             have "setsum (pp y) {..<n} = \<integral>\<^sup>+ x. pp y x \<partial>count_space {..<n}"
  1164               by(simp add: nn_integral_count_space_finite max_def)
  1165             also have "\<dots> \<le> \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV"
  1166               by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono)
  1167             also have "\<dots> = setsum (rr y) {..z}" using * by simp
  1168             finally obtain k where k: "find_start y (setsum (pp y) {..<n}) = Some k"
  1169               by(fastforce simp add: find_start_def)
  1170             with \<open>ereal (setsum (pp y) {..<n}) \<le> setsum (rr y) {..z}\<close>
  1171             have "k \<le> z" by(auto simp add: find_start_def split: split_if_asm intro: Least_le)
  1172             then have "setsum (pp y) {..<n} - setsum (rr y) {..<z} \<le> ereal (\<Sum>x<Suc n. assign y x z)"
  1173               using \<open>ereal (setsum (pp y) {..<n}) \<le> setsum (rr y) {..z}\<close>
  1174               by(subst setsum_assign)(auto simp add: field_simps max_def k ivl_disj_un(2)[symmetric], metis le_add_same_cancel2 max.bounded_iff max_def pos_p)
  1175             also have "\<dots> \<le> (SUP n. ereal (\<Sum>x<n. assign y x z))"
  1176               by(rule SUP_upper) simp
  1177             finally show "ereal (\<Sum>x<n. pp y x) \<le> \<dots> + setsum (rr y) {..<z}" 
  1178               by(simp add: ereal_minus(1)[symmetric] ereal_minus_le del: ereal_minus(1))
  1179           qed
  1180           finally show ?thesis
  1181             by(simp add: ivl_disj_un(2)[symmetric] plus_ereal.simps(1)[symmetric] ereal_add_le_add_iff2 del: plus_ereal.simps(1))
  1182         qed
  1183       qed
  1184       finally show "?thesis y z" .
  1185     qed
  1186 
  1187     { fix y x
  1188       have "(\<integral>\<^sup>+ z. assign y x z \<partial>count_space UNIV) = pp y x"
  1189       proof(cases "setsum (pp y) {..<x} = \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV")
  1190         case False
  1191         let ?used = "setsum (pp y) {..<x}"
  1192         have "?used = \<integral>\<^sup>+ x. pp y x \<partial>count_space {..<x}"
  1193           by(simp add: nn_integral_count_space_finite max_def)
  1194         also have "\<dots> \<le> \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV"
  1195           by(auto simp add: nn_integral_count_space_indicator indicator_def intro!: nn_integral_mono)
  1196         finally have "?used < \<dots>" using False by auto
  1197         also note eq finally have "?used \<in> dom (find_start y)" by(rule in_dom_find_startI)
  1198         then obtain k where k: "find_start y ?used = Some k" by auto
  1199         let ?f = "\<lambda>z. if z < k then 0 else if z = k then setsum (rr y) {..k} - ?used else rr y z"
  1200         let ?g = "\<lambda>x'. if x' < x then 0 else pp y x'"
  1201         have "pp y x = ?g x" by simp
  1202         also have "?g x \<le> \<integral>\<^sup>+ x'. ?g x' \<partial>count_space UNIV" by(rule nn_integral_ge_point) simp
  1203         also {
  1204           have "?used = \<integral>\<^sup>+ x. pp y x \<partial>count_space {..<x}"
  1205             by(simp add: nn_integral_count_space_finite max_def)
  1206           also have "\<dots> = \<integral>\<^sup>+ x'. (if x' < x then pp y x' else 0) \<partial>count_space UNIV"
  1207             by(simp add: nn_integral_count_space_indicator indicator_def if_distrib zero_ereal_def cong: if_cong)
  1208           also have "(\<integral>\<^sup>+ x'. ?g x' \<partial>count_space UNIV) + \<dots> = \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV"
  1209             by(subst nn_integral_add[symmetric])(auto intro: nn_integral_cong)
  1210           also note calculation }
  1211         ultimately have "ereal (pp y x) + ?used \<le> \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV"
  1212           by (metis (no_types, lifting) ereal_add_mono order_refl)
  1213         also note eq
  1214         also have "(\<integral>\<^sup>+ z. rr y z \<partial>count_space UNIV) = (\<integral>\<^sup>+ z. ?f z \<partial>count_space UNIV) + (\<integral>\<^sup>+ z. (if z < k then rr y z else if z = k then ?used - setsum (rr y) {..<k} else 0) \<partial>count_space UNIV)"
  1215           using k by(subst nn_integral_add[symmetric])(auto intro!: nn_integral_cong simp add: ivl_disj_un(2)[symmetric] setsum_nonneg dest: find_start_eq_Some_least find_start_eq_Some_above)
  1216         also have "(\<integral>\<^sup>+ z. (if z < k then rr y z else if z = k then ?used - setsum (rr y) {..<k} else 0) \<partial>count_space UNIV) =
  1217           (\<integral>\<^sup>+ z. (if z < k then rr y z else if z = k then ?used - setsum (rr y) {..<k} else 0) \<partial>count_space {..k})"
  1218           by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_cong)
  1219         also have "\<dots> = ?used" 
  1220           using k by(auto simp add: nn_integral_count_space_finite max_def ivl_disj_un(2)[symmetric] diff_le_iff setsum_nonneg dest: find_start_eq_Some_least)
  1221         finally have "pp y x \<le> (\<integral>\<^sup>+ z. ?f z \<partial>count_space UNIV)"
  1222           by(cases "\<integral>\<^sup>+ z. ?f z \<partial>count_space UNIV") simp_all
  1223         then show ?thesis using k
  1224           by(simp add: assign_def nn_integral_assign_aux diff_le_iff find_start_eq_Some_above min_def)
  1225       next
  1226         case True
  1227         have "setsum (pp y) {..x} = \<integral>\<^sup>+ x. pp y x \<partial>count_space {..x}"
  1228           by(simp add: nn_integral_count_space_finite max_def)
  1229         also have "\<dots> \<le> \<integral>\<^sup>+ x. pp y x \<partial>count_space UNIV"
  1230           by(auto simp add: nn_integral_count_space_indicator indicator_def intro: nn_integral_mono)
  1231         also have "\<dots> = setsum (pp y) {..<x}" by(simp add: True)
  1232         finally have "pp y x = 0" by(simp add: ivl_disj_un(2)[symmetric] eq_iff del: pp_convs)
  1233         thus ?thesis
  1234           by(cases "find_start y (setsum (pp y) {..<x})")(simp_all add: assign_def diff_le_iff find_start_eq_Some_above)
  1235       qed }
  1236     note nn_integral_assign2 = this
  1237 
  1238     let ?f = "\<lambda>y x z. if x \<in> ?A y \<and> z \<in> ?B y then assign y (?P y x) (?R y z) else 0"
  1239     def f \<equiv> "\<lambda>y x z. ereal (?f y x z)"
  1240 
  1241     have pos: "\<And>y x z. 0 \<le> f y x z" by(simp add: f_def)
  1242     { fix y x z
  1243       have "f y x z \<le> 0 \<longleftrightarrow> f y x z = 0" using pos[of y x z] by simp }
  1244     note f [simp] = this
  1245     have support:
  1246       "\<And>x y z. (x, y) \<notin> set_pmf pq \<Longrightarrow> f y x z = 0"
  1247       "\<And>x y z. (y, z) \<notin> set_pmf qr \<Longrightarrow> f y x z = 0"
  1248       by(auto simp add: f_def)
  1249 
  1250     from pos support have support':
  1251       "\<And>x z. x \<notin> set_pmf p \<Longrightarrow> (\<integral>\<^sup>+ y. f y x z \<partial>count_space UNIV) = 0"
  1252       "\<And>x z. z \<notin> set_pmf r \<Longrightarrow> (\<integral>\<^sup>+ y. f y x z \<partial>count_space UNIV) = 0"
  1253     and support'':
  1254       "\<And>x y z. x \<notin> set_pmf p \<Longrightarrow> f y x z = 0"
  1255       "\<And>x y z. y \<notin> set_pmf q \<Longrightarrow> f y x z = 0"
  1256       "\<And>x y z. z \<notin> set_pmf r \<Longrightarrow> f y x z = 0"
  1257       by(auto simp add: nn_integral_0_iff_AE AE_count_space p q r set_map_pmf image_iff)(metis fst_conv snd_conv)+
  1258 
  1259     have f_x: "\<And>y z. (\<integral>\<^sup>+ x. f y x z \<partial>count_space (set_pmf p)) = pmf qr (y, z)"
  1260     proof(case_tac "z \<in> ?B y")
  1261       fix y z
  1262       assume z: "z \<in> ?B y"
  1263       have "(\<integral>\<^sup>+ x. f y x z \<partial>count_space (set_pmf p)) = (\<integral>\<^sup>+ x. ?f y x z \<partial>count_space (?A y))"
  1264         using support''(1)[of _ y z]
  1265         by(fastforce simp add: f_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong)
  1266       also have "\<dots> = \<integral>\<^sup>+ x. assign y (?P y x) (?R y z) \<partial>count_space (?A y)"
  1267         using z by(intro nn_integral_cong) simp
  1268       also have "\<dots> = \<integral>\<^sup>+ x. assign y x (?R y z) \<partial>count_space (?P y ` ?A y)"
  1269         by(intro nn_integral_bij_count_space inj_on_imp_bij_betw inj_on_to_nat_on) simp
  1270       also have "\<dots> = \<integral>\<^sup>+ x. assign y x (?R y z) \<partial>count_space UNIV"
  1271         by(auto simp add: nn_integral_count_space_indicator indicator_def assign_eq_0_outside pp_def intro!: nn_integral_cong)
  1272       also have "\<dots> = rr y (?R y z)" by(rule nn_integral_assign1)
  1273       also have "\<dots> = pmf qr (y, z)" using z by(simp add: rr_def)
  1274       finally show "?thesis y z" .
  1275     qed(auto simp add: f_def zero_ereal_def[symmetric] set_pmf_iff)
  1276 
  1277     have f_z: "\<And>x y. (\<integral>\<^sup>+ z. f y x z \<partial>count_space (set_pmf r)) = pmf pq (x, y)"
  1278     proof(case_tac "x \<in> ?A y")
  1279       fix x y
  1280       assume x: "x \<in> ?A y"
  1281       have "(\<integral>\<^sup>+ z. f y x z \<partial>count_space (set_pmf r)) = (\<integral>\<^sup>+ z. ?f y x z \<partial>count_space (?B y))"
  1282         using support''(3)[of _ y x]
  1283         by(fastforce simp add: f_def nn_integral_count_space_indicator indicator_def intro!: nn_integral_cong)
  1284       also have "\<dots> = \<integral>\<^sup>+ z. assign y (?P y x) (?R y z) \<partial>count_space (?B y)"
  1285         using x by(intro nn_integral_cong) simp
  1286       also have "\<dots> = \<integral>\<^sup>+ z. assign y (?P y x) z \<partial>count_space (?R y ` ?B y)"
  1287         by(intro nn_integral_bij_count_space inj_on_imp_bij_betw inj_on_to_nat_on) simp
  1288       also have "\<dots> = \<integral>\<^sup>+ z. assign y (?P y x) z \<partial>count_space UNIV"
  1289         by(auto simp add: nn_integral_count_space_indicator indicator_def assign_eq_0_outside rr_def intro!: nn_integral_cong)
  1290       also have "\<dots> = pp y (?P y x)" by(rule nn_integral_assign2)
  1291       also have "\<dots> = pmf pq (x, y)" using x by(simp add: pp_def)
  1292       finally show "?thesis x y" .
  1293     qed(auto simp add: f_def zero_ereal_def[symmetric] set_pmf_iff)
  1294 
  1295     let ?pr = "\<lambda>(x, z). \<integral>\<^sup>+ y. f y x z \<partial>count_space UNIV"
  1296 
  1297     have pr_pos: "\<And>xz. 0 \<le> ?pr xz"
  1298       by(auto simp add: nn_integral_nonneg)
  1299 
  1300     have pr': "?pr = (\<lambda>(x, z). \<integral>\<^sup>+ y. f y x z \<partial>count_space (set_pmf q))"
  1301       by(auto simp add: fun_eq_iff nn_integral_count_space_indicator indicator_def support'' intro: nn_integral_cong)
  1302     
  1303     have "(\<integral>\<^sup>+ xz. ?pr xz \<partial>count_space UNIV) = (\<integral>\<^sup>+ xz. ?pr xz * indicator (set_pmf p \<times> set_pmf r) xz \<partial>count_space UNIV)"
  1304       by(rule nn_integral_cong)(auto simp add: indicator_def support' intro: ccontr)
  1305     also have "\<dots> = (\<integral>\<^sup>+ xz. ?pr xz \<partial>count_space (set_pmf p \<times> set_pmf r))"
  1306       by(simp add: nn_integral_count_space_indicator)
  1307     also have "\<dots> = (\<integral>\<^sup>+ xz. ?pr xz \<partial>(count_space (set_pmf p) \<Otimes>\<^sub>M count_space (set_pmf r)))"
  1308       by(simp add: pair_measure_countable)
  1309     also have "\<dots> = (\<integral>\<^sup>+ (x, z). \<integral>\<^sup>+ y. f y x z \<partial>count_space (set_pmf q) \<partial>(count_space (set_pmf p) \<Otimes>\<^sub>M count_space (set_pmf r)))"
  1310       by(simp add: pr')
  1311     also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ z. \<integral>\<^sup>+ y. f y x z \<partial>count_space (set_pmf q) \<partial>count_space (set_pmf r) \<partial>count_space (set_pmf p))"
  1312       by(subst sigma_finite_measure.nn_integral_fst[symmetric, OF sigma_finite_measure_count_space_countable])(simp_all add: pair_measure_countable)
  1313     also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. \<integral>\<^sup>+ z. f y x z \<partial>count_space (set_pmf r) \<partial>count_space (set_pmf q) \<partial>count_space (set_pmf p))"
  1314       by(subst (2) pair_sigma_finite.Fubini')(simp_all add: pair_sigma_finite.intro sigma_finite_measure_count_space_countable pair_measure_countable)
  1315     also have "\<dots> = (\<integral>\<^sup>+ x. \<integral>\<^sup>+ y. pmf pq (x, y) \<partial>count_space (set_pmf q) \<partial>count_space (set_pmf p))"
  1316       by(simp add: f_z)
  1317     also have "\<dots> = (\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. pmf pq (x, y) \<partial>count_space (set_pmf p) \<partial>count_space (set_pmf q))"
  1318       by(subst pair_sigma_finite.Fubini')(simp_all add: pair_sigma_finite.intro sigma_finite_measure_count_space_countable pair_measure_countable)
  1319     also have "\<dots> = (\<integral>\<^sup>+ y. \<integral>\<^sup>+ x. emeasure (measure_pmf pq) {(x, y)} \<partial>count_space (set_pmf p) \<partial>count_space (set_pmf q))"
  1320       by(simp add: emeasure_pmf_single)
  1321     also have "\<dots> = (\<integral>\<^sup>+ y. emeasure (measure_pmf pq) (\<Union>x\<in>set_pmf p. {(x, y)}) \<partial>count_space (set_pmf q))"
  1322       by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
  1323     also have "\<dots> = (\<integral>\<^sup>+ y. emeasure (measure_pmf pq) ((\<Union>x\<in>set_pmf p. {(x, y)}) \<union> {(x, y'). x \<notin> set_pmf p \<and> y' = y}) \<partial>count_space (set_pmf q))"
  1324       by(rule nn_integral_cong emeasure_Un_null_set[symmetric])+
  1325         (auto simp add: p set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
  1326     also have "\<dots> = (\<integral>\<^sup>+ y. emeasure (measure_pmf pq) (snd -` {y}) \<partial>count_space (set_pmf q))"
  1327       by(rule nn_integral_cong arg_cong2[where f=emeasure])+auto
  1328     also have "\<dots> = (\<integral>\<^sup>+ y. pmf q y \<partial>count_space (set_pmf q))"
  1329       by(simp add: ereal_pmf_map q)
  1330     also have "\<dots> = (\<integral>\<^sup>+ y. pmf q y \<partial>count_space UNIV)"
  1331       by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
  1332     also have "\<dots> = 1"
  1333       by(subst nn_integral_pmf)(simp add: measure_pmf.emeasure_eq_1_AE)
  1334     finally have pr_prob: "(\<integral>\<^sup>+ xz. ?pr xz \<partial>count_space UNIV) = 1" .
  1335 
  1336     have pr_bounded: "\<And>xz. ?pr xz \<noteq> \<infinity>"
  1337     proof -
  1338       fix xz
  1339       have "?pr xz \<le> \<integral>\<^sup>+ xz. ?pr xz \<partial>count_space UNIV"
  1340         by(rule nn_integral_ge_point) simp
  1341       also have "\<dots> = 1" by(fact pr_prob)
  1342       finally show "?thesis xz" by auto
  1343     qed
  1344 
  1345     def pr \<equiv> "embed_pmf (real \<circ> ?pr)"
  1346     have pmf_pr: "\<And>xz. pmf pr xz = real (?pr xz)" using pr_pos pr_prob
  1347       unfolding pr_def by(subst pmf_embed_pmf)(auto simp add: real_of_ereal_pos ereal_real pr_bounded)
  1348 
  1349     have set_pmf_pr_subset: "set_pmf pr \<subseteq> set_pmf pq O set_pmf qr"
  1350     proof
  1351       fix xz :: "'a \<times> 'c"
  1352       obtain x z where xz: "xz = (x, z)" by(cases xz)
  1353       assume "xz \<in> set_pmf pr"
  1354       with xz have "pmf pr (x, z) \<noteq> 0" by(simp add: set_pmf_iff)
  1355       hence "\<exists>y. f y x z \<noteq> 0" by(rule contrapos_np)(simp add: pmf_pr)
  1356       then obtain y where y: "f y x z \<noteq> 0" ..
  1357       then have "(x, y) \<in> set_pmf pq" "(y, z) \<in> set_pmf qr" 
  1358         using support by fastforce+
  1359       then show "xz \<in> set_pmf pq O set_pmf qr" using xz by auto
  1360     qed
  1361     hence "\<And>x z. (x, z) \<in> set_pmf pr \<Longrightarrow> (R OO S) x z" using pq qr by blast
  1362     moreover
  1363     have "map_pmf fst pr = p"
  1364     proof(rule pmf_eqI)
  1365       fix x
  1366       have "pmf (map_pmf fst pr) x = emeasure (measure_pmf pr) (fst -` {x})"
  1367         by(simp add: ereal_pmf_map)
  1368       also have "\<dots> = \<integral>\<^sup>+ xz. pmf pr xz \<partial>count_space (fst -` {x})"
  1369         by(simp add: nn_integral_pmf)
  1370       also have "\<dots> = \<integral>\<^sup>+ xz. ?pr xz \<partial>count_space (fst -` {x})"
  1371         by(simp add: pmf_pr ereal_real pr_bounded pr_pos)
  1372       also have "\<dots> =  \<integral>\<^sup>+ xz. ?pr xz \<partial>count_space {x} \<Otimes>\<^sub>M count_space (set_pmf r)"
  1373         by(auto simp add: nn_integral_count_space_indicator indicator_def support' pair_measure_countable intro!: nn_integral_cong)
  1374       also have "\<dots> = \<integral>\<^sup>+ z. \<integral>\<^sup>+ x. ?pr (x, z) \<partial>count_space {x} \<partial>count_space (set_pmf r)"
  1375         by(subst pair_sigma_finite.nn_integral_snd[symmetric])(simp_all add: pair_measure_countable pair_sigma_finite.intro sigma_finite_measure_count_space_countable)
  1376       also have "\<dots> = \<integral>\<^sup>+ z. ?pr (x, z) \<partial>count_space (set_pmf r)"
  1377         using pr_pos by(clarsimp simp add: nn_integral_count_space_finite max_def)
  1378       also have "\<dots> = \<integral>\<^sup>+ z. \<integral>\<^sup>+ y. f y x z \<partial>count_space (set_pmf q) \<partial>count_space (set_pmf r)"
  1379         by(simp add: pr')
  1380       also have "\<dots> =  \<integral>\<^sup>+ y. \<integral>\<^sup>+ z. f y x z \<partial>count_space (set_pmf r) \<partial>count_space (set_pmf q)"
  1381         by(subst pair_sigma_finite.Fubini')(simp_all add: pair_sigma_finite.intro sigma_finite_measure_count_space_countable pair_measure_countable)
  1382       also have "\<dots> = \<integral>\<^sup>+ y. pmf pq (x, y) \<partial>count_space (set_pmf q)"
  1383         by(simp add: f_z)
  1384       also have "\<dots> = \<integral>\<^sup>+ y. emeasure (measure_pmf pq) {(x, y)} \<partial>count_space (set_pmf q)"
  1385         by(simp add: emeasure_pmf_single)
  1386       also have "\<dots> = emeasure (measure_pmf pq) (\<Union>y\<in>set_pmf q. {(x, y)})"
  1387         by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
  1388       also have "\<dots> = emeasure (measure_pmf pq) ((\<Union>y\<in>set_pmf q. {(x, y)}) \<union> {(x', y). y \<notin> set_pmf q \<and> x' = x})"
  1389         by(rule emeasure_Un_null_set[symmetric])+
  1390           (auto simp add: q set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
  1391       also have "\<dots> = emeasure (measure_pmf pq) (fst -` {x})"
  1392         by(rule arg_cong2[where f=emeasure])+auto
  1393       also have "\<dots> = pmf p x" by(simp add: ereal_pmf_map p)
  1394       finally show "pmf (map_pmf fst pr) x = pmf p x" by simp
  1395     qed
  1396     moreover
  1397     have "map_pmf snd pr = r"
  1398     proof(rule pmf_eqI)
  1399       fix z
  1400       have "pmf (map_pmf snd pr) z = emeasure (measure_pmf pr) (snd -` {z})"
  1401         by(simp add: ereal_pmf_map)
  1402       also have "\<dots> = \<integral>\<^sup>+ xz. pmf pr xz \<partial>count_space (snd -` {z})"
  1403         by(simp add: nn_integral_pmf)
  1404       also have "\<dots> = \<integral>\<^sup>+ xz. ?pr xz \<partial>count_space (snd -` {z})"
  1405         by(simp add: pmf_pr ereal_real pr_bounded pr_pos)
  1406       also have "\<dots> =  \<integral>\<^sup>+ xz. ?pr xz \<partial>count_space (set_pmf p) \<Otimes>\<^sub>M count_space {z}"
  1407         by(auto simp add: nn_integral_count_space_indicator indicator_def support' pair_measure_countable intro!: nn_integral_cong)
  1408       also have "\<dots> = \<integral>\<^sup>+ x. \<integral>\<^sup>+ z. ?pr (x, z) \<partial>count_space {z} \<partial>count_space (set_pmf p)"
  1409         by(subst sigma_finite_measure.nn_integral_fst[symmetric])(simp_all add: pair_measure_countable sigma_finite_measure_count_space_countable)
  1410       also have "\<dots> = \<integral>\<^sup>+ x. ?pr (x, z) \<partial>count_space (set_pmf p)"
  1411         using pr_pos by(clarsimp simp add: nn_integral_count_space_finite max_def)
  1412       also have "\<dots> = \<integral>\<^sup>+ x. \<integral>\<^sup>+ y. f y x z \<partial>count_space (set_pmf q) \<partial>count_space (set_pmf p)"
  1413         by(simp add: pr')
  1414       also have "\<dots> =  \<integral>\<^sup>+ y. \<integral>\<^sup>+ x. f y x z \<partial>count_space (set_pmf p) \<partial>count_space (set_pmf q)"
  1415         by(subst pair_sigma_finite.Fubini')(simp_all add: pair_sigma_finite.intro sigma_finite_measure_count_space_countable pair_measure_countable)
  1416       also have "\<dots> = \<integral>\<^sup>+ y. pmf qr (y, z) \<partial>count_space (set_pmf q)"
  1417         by(simp add: f_x)
  1418       also have "\<dots> = \<integral>\<^sup>+ y. emeasure (measure_pmf qr) {(y, z)} \<partial>count_space (set_pmf q)"
  1419         by(simp add: emeasure_pmf_single)
  1420       also have "\<dots> = emeasure (measure_pmf qr) (\<Union>y\<in>set_pmf q. {(y, z)})"
  1421         by(subst emeasure_UN_countable)(simp_all add: disjoint_family_on_def)
  1422       also have "\<dots> = emeasure (measure_pmf qr) ((\<Union>y\<in>set_pmf q. {(y, z)}) \<union> {(y, z'). y \<notin> set_pmf q \<and> z' = z})"
  1423         by(rule emeasure_Un_null_set[symmetric])+
  1424           (auto simp add: q' set_map_pmf split_beta intro!: in_null_sets_measure_pmfI intro: rev_image_eqI)
  1425       also have "\<dots> = emeasure (measure_pmf qr) (snd -` {z})"
  1426         by(rule arg_cong2[where f=emeasure])+auto
  1427       also have "\<dots> = pmf r z" by(simp add: ereal_pmf_map r)
  1428       finally show "pmf (map_pmf snd pr) z = pmf r z" by simp
  1429     qed
  1430     ultimately have "rel_pmf (R OO S) p r" .. }
  1431   then show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
  1432     by(auto simp add: le_fun_def)
  1433 qed (fact natLeq_card_order natLeq_cinfinite)+
  1434 
  1435 end
  1436