src/HOL/Probability/Probability_Mass_Function.thy
 author hoelzl Fri Nov 14 13:18:33 2014 +0100 (2014-11-14) changeset 59002 2c8b2fb54b88 parent 59000 6eb0725503fc child 59023 4999a616336c permissions -rw-r--r--
cleaning up some theorem names; remove unnecessary assumptions; more complete pmf theory
```     1 (*  Title:      HOL/Probability/Probability_Mass_Function.thy
```
```     2     Author:     Johannes Hölzl, TU München *)
```
```     3
```
```     4 section \<open> Probability mass function \<close>
```
```     5
```
```     6 theory Probability_Mass_Function
```
```     7 imports
```
```     8   Giry_Monad
```
```     9   "~~/src/HOL/Library/Multiset"
```
```    10 begin
```
```    11
```
```    12 lemma (in finite_measure) countable_support: (* replace version in pmf *)
```
```    13   "countable {x. measure M {x} \<noteq> 0}"
```
```    14 proof cases
```
```    15   assume "measure M (space M) = 0"
```
```    16   with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
```
```    17     by auto
```
```    18   then show ?thesis
```
```    19     by simp
```
```    20 next
```
```    21   let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
```
```    22   assume "?M \<noteq> 0"
```
```    23   then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
```
```    24     using reals_Archimedean[of "?m x / ?M" for x]
```
```    25     by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
```
```    26   have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
```
```    27   proof (rule ccontr)
```
```    28     fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
```
```    29     then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
```
```    30       by (metis infinite_arbitrarily_large)
```
```    31     from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x"
```
```    32       by auto
```
```    33     { fix x assume "x \<in> X"
```
```    34       from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
```
```    35       then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
```
```    36     note singleton_sets = this
```
```    37     have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
```
```    38       using `?M \<noteq> 0`
```
```    39       by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc field_simps less_le measure_nonneg)
```
```    40     also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
```
```    41       by (rule setsum_mono) fact
```
```    42     also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
```
```    43       using singleton_sets `finite X`
```
```    44       by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
```
```    45     finally have "?M < measure M (\<Union>x\<in>X. {x})" .
```
```    46     moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
```
```    47       using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
```
```    48     ultimately show False by simp
```
```    49   qed
```
```    50   show ?thesis
```
```    51     unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
```
```    52 qed
```
```    53
```
```    54 lemma (in finite_measure) AE_support_countable:
```
```    55   assumes [simp]: "sets M = UNIV"
```
```    56   shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
```
```    57 proof
```
```    58   assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
```
```    59   then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
```
```    60     by auto
```
```    61   then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) =
```
```    62     (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
```
```    63     by (subst emeasure_UN_countable)
```
```    64        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
```
```    65   also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
```
```    66     by (auto intro!: nn_integral_cong split: split_indicator)
```
```    67   also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
```
```    68     by (subst emeasure_UN_countable)
```
```    69        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
```
```    70   also have "\<dots> = emeasure M (space M)"
```
```    71     using ae by (intro emeasure_eq_AE) auto
```
```    72   finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
```
```    73     by (simp add: emeasure_single_in_space cong: rev_conj_cong)
```
```    74   with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
```
```    75   have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
```
```    76     by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
```
```    77   then show "AE x in M. measure M {x} \<noteq> 0"
```
```    78     by (auto simp: emeasure_eq_measure)
```
```    79 qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
```
```    80
```
```    81 subsection {* PMF as measure *}
```
```    82
```
```    83 typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
```
```    84   morphisms measure_pmf Abs_pmf
```
```    85   by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
```
```    86      (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
```
```    87
```
```    88 declare [[coercion measure_pmf]]
```
```    89
```
```    90 lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
```
```    91   using pmf.measure_pmf[of p] by auto
```
```    92
```
```    93 interpretation measure_pmf!: prob_space "measure_pmf M" for M
```
```    94   by (rule prob_space_measure_pmf)
```
```    95
```
```    96 interpretation measure_pmf!: subprob_space "measure_pmf M" for M
```
```    97   by (rule prob_space_imp_subprob_space) unfold_locales
```
```    98
```
```    99 locale pmf_as_measure
```
```   100 begin
```
```   101
```
```   102 setup_lifting type_definition_pmf
```
```   103
```
```   104 end
```
```   105
```
```   106 context
```
```   107 begin
```
```   108
```
```   109 interpretation pmf_as_measure .
```
```   110
```
```   111 lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
```
```   112
```
```   113 lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
```
```   114
```
```   115 lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
```
```   116   "\<lambda>f M. distr M (count_space UNIV) f"
```
```   117 proof safe
```
```   118   fix M and f :: "'a \<Rightarrow> 'b"
```
```   119   let ?D = "distr M (count_space UNIV) f"
```
```   120   assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
```
```   121   interpret prob_space M by fact
```
```   122   from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
```
```   123   proof eventually_elim
```
```   124     fix x
```
```   125     have "measure M {x} \<le> measure M (f -` {f x})"
```
```   126       by (intro finite_measure_mono) auto
```
```   127     then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
```
```   128       using measure_nonneg[of M "{x}"] by auto
```
```   129   qed
```
```   130   then show "AE x in ?D. measure ?D {x} \<noteq> 0"
```
```   131     by (simp add: AE_distr_iff measure_distr measurable_def)
```
```   132 qed (auto simp: measurable_def prob_space.prob_space_distr)
```
```   133
```
```   134 declare [[coercion set_pmf]]
```
```   135
```
```   136 lemma countable_set_pmf: "countable (set_pmf p)"
```
```   137   by transfer (metis prob_space.finite_measure finite_measure.countable_support)
```
```   138
```
```   139 lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
```
```   140   by transfer metis
```
```   141
```
```   142 lemma sets_measure_pmf_count_space: "sets (measure_pmf M) = sets (count_space UNIV)"
```
```   143   by simp
```
```   144
```
```   145 lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
```
```   146   using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
```
```   147
```
```   148 lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
```
```   149   by (auto simp: measurable_def)
```
```   150
```
```   151 lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
```
```   152   by (intro measurable_cong_sets) simp_all
```
```   153
```
```   154 lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
```
```   155   by transfer (simp add: less_le measure_nonneg)
```
```   156
```
```   157 lemma pmf_nonneg: "0 \<le> pmf p x"
```
```   158   by transfer (simp add: measure_nonneg)
```
```   159
```
```   160 lemma pmf_le_1: "pmf p x \<le> 1"
```
```   161   by (simp add: pmf.rep_eq)
```
```   162
```
```   163 lemma emeasure_pmf_single:
```
```   164   fixes M :: "'a pmf"
```
```   165   shows "emeasure M {x} = pmf M x"
```
```   166   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
```
```   167
```
```   168 lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
```
```   169   by transfer simp
```
```   170
```
```   171 lemma emeasure_pmf_single_eq_zero_iff:
```
```   172   fixes M :: "'a pmf"
```
```   173   shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
```
```   174   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
```
```   175
```
```   176 lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
```
```   177 proof -
```
```   178   { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
```
```   179     with P have "AE x in M. x \<noteq> y"
```
```   180       by auto
```
```   181     with y have False
```
```   182       by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
```
```   183   then show ?thesis
```
```   184     using AE_measure_pmf[of M] by auto
```
```   185 qed
```
```   186
```
```   187 lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
```
```   188   using AE_measure_pmf[of M] by (intro notI) simp
```
```   189
```
```   190 lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
```
```   191   by transfer simp
```
```   192
```
```   193 lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
```
```   194   by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
```
```   195
```
```   196 lemma nn_integral_measure_pmf_support:
```
```   197   fixes f :: "'a \<Rightarrow> ereal"
```
```   198   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"
```
```   199   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
```
```   200 proof -
```
```   201   have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
```
```   202     using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
```
```   203   also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
```
```   204     using assms by (intro nn_integral_indicator_finite) auto
```
```   205   finally show ?thesis
```
```   206     by (simp add: emeasure_measure_pmf_finite)
```
```   207 qed
```
```   208
```
```   209 lemma nn_integral_measure_pmf_finite:
```
```   210   fixes f :: "'a \<Rightarrow> ereal"
```
```   211   assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
```
```   212   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
```
```   213   using assms by (intro nn_integral_measure_pmf_support) auto
```
```   214 lemma integrable_measure_pmf_finite:
```
```   215   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
```
```   216   shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
```
```   217   by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
```
```   218
```
```   219 lemma integral_measure_pmf:
```
```   220   assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
```
```   221   shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
```
```   222 proof -
```
```   223   have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
```
```   224     using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
```
```   225   also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
```
```   226     by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
```
```   227   finally show ?thesis .
```
```   228 qed
```
```   229
```
```   230 lemma integrable_pmf: "integrable (count_space X) (pmf M)"
```
```   231 proof -
```
```   232   have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
```
```   233     by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
```
```   234   then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
```
```   235     by (simp add: integrable_iff_bounded pmf_nonneg)
```
```   236   then show ?thesis
```
```   237     by (simp add: pmf.rep_eq measure_pmf.integrable_measure countable_set_pmf disjoint_family_on_def)
```
```   238 qed
```
```   239
```
```   240 lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
```
```   241 proof -
```
```   242   have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
```
```   243     by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
```
```   244   also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
```
```   245     by (auto intro!: nn_integral_cong_AE split: split_indicator
```
```   246              simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
```
```   247                    AE_count_space set_pmf_iff)
```
```   248   also have "\<dots> = emeasure M (X \<inter> M)"
```
```   249     by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
```
```   250   also have "\<dots> = emeasure M X"
```
```   251     by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
```
```   252   finally show ?thesis
```
```   253     by (simp add: measure_pmf.emeasure_eq_measure)
```
```   254 qed
```
```   255
```
```   256 lemma integral_pmf_restrict:
```
```   257   "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
```
```   258     (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
```
```   259   by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
```
```   260
```
```   261 lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
```
```   262 proof -
```
```   263   have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
```
```   264     by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
```
```   265   then show ?thesis
```
```   266     using measure_pmf.emeasure_space_1 by simp
```
```   267 qed
```
```   268
```
```   269 lemma map_pmf_id[simp]: "map_pmf id = id"
```
```   270   by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
```
```   271
```
```   272 lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
```
```   273   by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def)
```
```   274
```
```   275 lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
```
```   276   using map_pmf_compose[of f g] by (simp add: comp_def)
```
```   277
```
```   278 lemma map_pmf_cong:
```
```   279   assumes "p = q"
```
```   280   shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
```
```   281   unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
```
```   282   by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
```
```   283
```
```   284 lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
```
```   285   unfolding map_pmf.rep_eq by (subst emeasure_distr) auto
```
```   286
```
```   287 lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
```
```   288   unfolding map_pmf.rep_eq by (intro nn_integral_distr) auto
```
```   289
```
```   290 lemma pmf_set_map:
```
```   291   fixes f :: "'a \<Rightarrow> 'b"
```
```   292   shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
```
```   293 proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
```
```   294   fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
```
```   295   assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
```
```   296   interpret prob_space M by fact
```
```   297   show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
```
```   298   proof safe
```
```   299     fix x assume "measure M (f -` {x}) \<noteq> 0"
```
```   300     moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
```
```   301       using ae by (intro finite_measure_eq_AE) auto
```
```   302     ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
```
```   303       by (metis measure_empty)
```
```   304     then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
```
```   305       by auto
```
```   306   next
```
```   307     fix x assume "measure M {x} \<noteq> 0"
```
```   308     then have "0 < measure M {x}"
```
```   309       using measure_nonneg[of M "{x}"] by auto
```
```   310     also have "measure M {x} \<le> measure M (f -` {f x})"
```
```   311       by (intro finite_measure_mono) auto
```
```   312     finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
```
```   313       by simp
```
```   314   qed
```
```   315 qed
```
```   316
```
```   317 lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M"
```
```   318   using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
```
```   319
```
```   320 subsection {* PMFs as function *}
```
```   321
```
```   322 context
```
```   323   fixes f :: "'a \<Rightarrow> real"
```
```   324   assumes nonneg: "\<And>x. 0 \<le> f x"
```
```   325   assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
```
```   326 begin
```
```   327
```
```   328 lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
```
```   329 proof (intro conjI)
```
```   330   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
```
```   331     by (simp split: split_indicator)
```
```   332   show "AE x in density (count_space UNIV) (ereal \<circ> f).
```
```   333     measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
```
```   334     by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator)
```
```   335   show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
```
```   336     by default (simp add: emeasure_density prob)
```
```   337 qed simp
```
```   338
```
```   339 lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
```
```   340 proof transfer
```
```   341   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
```
```   342     by (simp split: split_indicator)
```
```   343   fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
```
```   344     by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg)
```
```   345 qed
```
```   346
```
```   347 end
```
```   348
```
```   349 lemma embed_pmf_transfer:
```
```   350   "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"
```
```   351   by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
```
```   352
```
```   353 lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
```
```   354 proof (transfer, elim conjE)
```
```   355   fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
```
```   356   assume "prob_space M" then interpret prob_space M .
```
```   357   show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
```
```   358   proof (rule measure_eqI)
```
```   359     fix A :: "'a set"
```
```   360     have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) =
```
```   361       (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
```
```   362       by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
```
```   363     also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
```
```   364       by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
```
```   365     also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
```
```   366       by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
```
```   367          (auto simp: disjoint_family_on_def)
```
```   368     also have "\<dots> = emeasure M A"
```
```   369       using ae by (intro emeasure_eq_AE) auto
```
```   370     finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
```
```   371       using emeasure_space_1 by (simp add: emeasure_density)
```
```   372   qed simp
```
```   373 qed
```
```   374
```
```   375 lemma td_pmf_embed_pmf:
```
```   376   "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}"
```
```   377   unfolding type_definition_def
```
```   378 proof safe
```
```   379   fix p :: "'a pmf"
```
```   380   have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
```
```   381     using measure_pmf.emeasure_space_1[of p] by simp
```
```   382   then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
```
```   383     by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
```
```   384
```
```   385   show "embed_pmf (pmf p) = p"
```
```   386     by (intro measure_pmf_inject[THEN iffD1])
```
```   387        (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
```
```   388 next
```
```   389   fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
```
```   390   then show "pmf (embed_pmf f) = f"
```
```   391     by (auto intro!: pmf_embed_pmf)
```
```   392 qed (rule pmf_nonneg)
```
```   393
```
```   394 end
```
```   395
```
```   396 locale pmf_as_function
```
```   397 begin
```
```   398
```
```   399 setup_lifting td_pmf_embed_pmf
```
```   400
```
```   401 lemma set_pmf_transfer[transfer_rule]:
```
```   402   assumes "bi_total A"
```
```   403   shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"
```
```   404   using `bi_total A`
```
```   405   by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
```
```   406      metis+
```
```   407
```
```   408 end
```
```   409
```
```   410 context
```
```   411 begin
```
```   412
```
```   413 interpretation pmf_as_function .
```
```   414
```
```   415 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
```
```   416   by transfer auto
```
```   417
```
```   418 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
```
```   419   by (auto intro: pmf_eqI)
```
```   420
```
```   421 end
```
```   422
```
```   423 context
```
```   424 begin
```
```   425
```
```   426 interpretation pmf_as_function .
```
```   427
```
```   428 lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
```
```   429   "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
```
```   430   by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
```
```   431            split: split_max split_min)
```
```   432
```
```   433 lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
```
```   434   by transfer simp
```
```   435
```
```   436 lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
```
```   437   by transfer simp
```
```   438
```
```   439 lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
```
```   440   by (auto simp add: set_pmf_iff UNIV_bool)
```
```   441
```
```   442 lemma nn_integral_bernoulli_pmf[simp]:
```
```   443   assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
```
```   444   shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
```
```   445   by (subst nn_integral_measure_pmf_support[of UNIV])
```
```   446      (auto simp: UNIV_bool field_simps)
```
```   447
```
```   448 lemma integral_bernoulli_pmf[simp]:
```
```   449   assumes [simp]: "0 \<le> p" "p \<le> 1"
```
```   450   shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
```
```   451   by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
```
```   452
```
```   453 lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
```
```   454 proof
```
```   455   note geometric_sums[of "1 / 2"]
```
```   456   note sums_mult[OF this, of "1 / 2"]
```
```   457   from sums_suminf_ereal[OF this]
```
```   458   show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
```
```   459     by (simp add: nn_integral_count_space_nat field_simps)
```
```   460 qed simp
```
```   461
```
```   462 lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
```
```   463   by transfer rule
```
```   464
```
```   465 lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV"
```
```   466   by (auto simp: set_pmf_iff)
```
```   467
```
```   468 context
```
```   469   fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
```
```   470 begin
```
```   471
```
```   472 lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
```
```   473 proof
```
```   474   show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"
```
```   475     using M_not_empty
```
```   476     by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
```
```   477                   setsum_divide_distrib[symmetric])
```
```   478        (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
```
```   479 qed simp
```
```   480
```
```   481 lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
```
```   482   by transfer rule
```
```   483
```
```   484 lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
```
```   485   by (auto simp: set_pmf_iff)
```
```   486
```
```   487 end
```
```   488
```
```   489 context
```
```   490   fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
```
```   491 begin
```
```   492
```
```   493 lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
```
```   494 proof
```
```   495   show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"
```
```   496     using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
```
```   497 qed simp
```
```   498
```
```   499 lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
```
```   500   by transfer rule
```
```   501
```
```   502 lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
```
```   503   using S_finite S_not_empty by (auto simp: set_pmf_iff)
```
```   504
```
```   505 lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1"
```
```   506   by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
```
```   507
```
```   508 end
```
```   509
```
```   510 end
```
```   511
```
```   512 subsection {* Monad interpretation *}
```
```   513
```
```   514 lemma measurable_measure_pmf[measurable]:
```
```   515   "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
```
```   516   by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
```
```   517
```
```   518 lemma bind_pmf_cong:
```
```   519   assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
```
```   520   assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
```
```   521   shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
```
```   522 proof (rule measure_eqI)
```
```   523   show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)"
```
```   524     using assms by (subst (1 2) sets_bind) auto
```
```   525 next
```
```   526   fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)"
```
```   527   then have X: "X \<in> sets N"
```
```   528     using assms by (subst (asm) sets_bind) auto
```
```   529   show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X"
```
```   530     using assms
```
```   531     by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
```
```   532        (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
```
```   533 qed
```
```   534
```
```   535 context
```
```   536 begin
```
```   537
```
```   538 interpretation pmf_as_measure .
```
```   539
```
```   540 lift_definition join_pmf :: "'a pmf pmf \<Rightarrow> 'a pmf" is "\<lambda>M. measure_pmf M \<guillemotright>= measure_pmf"
```
```   541 proof (intro conjI)
```
```   542   fix M :: "'a pmf pmf"
```
```   543
```
```   544   have *: "measure_pmf \<in> measurable (measure_pmf M) (subprob_algebra (count_space UNIV))"
```
```   545     using measurable_measure_pmf[of "\<lambda>x. x"] by simp
```
```   546
```
```   547   interpret bind: prob_space "measure_pmf M \<guillemotright>= measure_pmf"
```
```   548     apply (rule measure_pmf.prob_space_bind[OF _ *])
```
```   549     apply (auto intro!: AE_I2)
```
```   550     apply unfold_locales
```
```   551     done
```
```   552   show "prob_space (measure_pmf M \<guillemotright>= measure_pmf)"
```
```   553     by intro_locales
```
```   554   show "sets (measure_pmf M \<guillemotright>= measure_pmf) = UNIV"
```
```   555     by (subst sets_bind[OF *]) auto
```
```   556   have "AE x in measure_pmf M \<guillemotright>= measure_pmf. emeasure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
```
```   557     by (auto simp add: AE_bind[OF _ *] AE_measure_pmf_iff emeasure_bind[OF _ *]
```
```   558         nn_integral_0_iff_AE measure_pmf.emeasure_eq_measure measure_le_0_iff set_pmf_iff pmf.rep_eq)
```
```   559   then show "AE x in measure_pmf M \<guillemotright>= measure_pmf. measure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
```
```   560     unfolding bind.emeasure_eq_measure by simp
```
```   561 qed
```
```   562
```
```   563 lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
```
```   564 proof (transfer fixing: N i)
```
```   565   have N: "subprob_space (measure_pmf N)"
```
```   566     by (rule prob_space_imp_subprob_space) intro_locales
```
```   567   show "measure (measure_pmf N \<guillemotright>= measure_pmf) {i} = integral\<^sup>L (measure_pmf N) (\<lambda>M. measure M {i})"
```
```   568     using measurable_measure_pmf[of "\<lambda>x. x"]
```
```   569     by (intro subprob_space.measure_bind[where N="count_space UNIV", OF N]) auto
```
```   570 qed (auto simp: Transfer.Rel_def rel_fun_def cr_pmf_def)
```
```   571
```
```   572 lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
```
```   573   by (auto intro!: prob_space_return simp: AE_return measure_return)
```
```   574
```
```   575 lemma join_return_pmf: "join_pmf (return_pmf M) = M"
```
```   576   by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
```
```   577
```
```   578 lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)"
```
```   579   by transfer (simp add: distr_return)
```
```   580
```
```   581 lemma set_return_pmf: "set_pmf (return_pmf x) = {x}"
```
```   582   by transfer (auto simp add: measure_return split: split_indicator)
```
```   583
```
```   584 lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x"
```
```   585   by transfer (simp add: measure_return)
```
```   586
```
```   587 lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
```
```   588   unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
```
```   589
```
```   590 lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
```
```   591   unfolding return_pmf.rep_eq by (intro emeasure_return) auto
```
```   592
```
```   593 end
```
```   594
```
```   595 definition "bind_pmf M f = join_pmf (map_pmf f M)"
```
```   596
```
```   597 lemma (in pmf_as_measure) bind_transfer[transfer_rule]:
```
```   598   "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"
```
```   599 proof (auto simp: pmf_as_measure.cr_pmf_def rel_fun_def bind_pmf_def join_pmf.rep_eq map_pmf.rep_eq)
```
```   600   fix M f and g :: "'a \<Rightarrow> 'b pmf" assume "\<forall>x. f x = measure_pmf (g x)"
```
```   601   then have f: "f = (\<lambda>x. measure_pmf (g x))"
```
```   602     by auto
```
```   603   show "measure_pmf M \<guillemotright>= f = distr (measure_pmf M) (count_space UNIV) g \<guillemotright>= measure_pmf"
```
```   604     unfolding f by (subst bind_distr[OF _ measurable_measure_pmf]) auto
```
```   605 qed
```
```   606
```
```   607 lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
```
```   608   by (auto intro!: integral_distr simp: bind_pmf_def pmf_join map_pmf.rep_eq)
```
```   609
```
```   610 lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
```
```   611   unfolding bind_pmf_def map_return_pmf join_return_pmf ..
```
```   612
```
```   613 lemma set_bind_pmf: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
```
```   614   apply (simp add: set_eq_iff set_pmf_iff pmf_bind)
```
```   615   apply (subst integral_nonneg_eq_0_iff_AE)
```
```   616   apply (auto simp: pmf_nonneg pmf_le_1 AE_measure_pmf_iff
```
```   617               intro!: measure_pmf.integrable_const_bound[where B=1])
```
```   618   done
```
```   619
```
```   620 lemma measurable_pair_restrict_pmf2:
```
```   621   assumes "countable A"
```
```   622   assumes "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
```
```   623   shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L"
```
```   624   apply (subst measurable_cong_sets)
```
```   625   apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
```
```   626   apply (simp_all add: restrict_count_space)
```
```   627   apply (subst split_eta[symmetric])
```
```   628   unfolding measurable_split_conv
```
```   629   apply (rule measurable_compose_countable'[OF _ measurable_snd `countable A`])
```
```   630   apply (rule measurable_compose[OF measurable_fst])
```
```   631   apply fact
```
```   632   done
```
```   633
```
```   634 lemma measurable_pair_restrict_pmf1:
```
```   635   assumes "countable A"
```
```   636   assumes "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
```
```   637   shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
```
```   638   apply (subst measurable_cong_sets)
```
```   639   apply (rule sets_pair_measure_cong sets_restrict_space_cong sets_measure_pmf_count_space refl)+
```
```   640   apply (simp_all add: restrict_count_space)
```
```   641   apply (subst split_eta[symmetric])
```
```   642   unfolding measurable_split_conv
```
```   643   apply (rule measurable_compose_countable'[OF _ measurable_fst `countable A`])
```
```   644   apply (rule measurable_compose[OF measurable_snd])
```
```   645   apply fact
```
```   646   done
```
```   647
```
```   648 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))"
```
```   649   unfolding pmf_eq_iff pmf_bind
```
```   650 proof
```
```   651   fix i
```
```   652   interpret B: prob_space "restrict_space B B"
```
```   653     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
```
```   654        (auto simp: AE_measure_pmf_iff)
```
```   655   interpret A: prob_space "restrict_space A A"
```
```   656     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
```
```   657        (auto simp: AE_measure_pmf_iff)
```
```   658
```
```   659   interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
```
```   660     by unfold_locales
```
```   661
```
```   662   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)"
```
```   663     by (rule integral_cong) (auto intro!: integral_pmf_restrict)
```
```   664   also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
```
```   665     by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
```
```   666               countable_set_pmf borel_measurable_count_space)
```
```   667   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
```
```   668     by (rule AB.Fubini_integral[symmetric])
```
```   669        (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
```
```   670              simp: pmf_nonneg pmf_le_1 countable_set_pmf measurable_restrict_space1)
```
```   671   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
```
```   672     by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
```
```   673               countable_set_pmf borel_measurable_count_space)
```
```   674   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
```
```   675     by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
```
```   676   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)" .
```
```   677 qed
```
```   678
```
```   679
```
```   680 context
```
```   681 begin
```
```   682
```
```   683 interpretation pmf_as_measure .
```
```   684
```
```   685 lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))"
```
```   686   by transfer simp
```
```   687
```
```   688 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)"
```
```   689   using measurable_measure_pmf[of N]
```
```   690   unfolding measure_pmf_bind
```
```   691   apply (subst (1 3) nn_integral_max_0[symmetric])
```
```   692   apply (intro nn_integral_bind[where B="count_space UNIV"])
```
```   693   apply auto
```
```   694   done
```
```   695
```
```   696 lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
```
```   697   using measurable_measure_pmf[of N]
```
```   698   unfolding measure_pmf_bind
```
```   699   by (subst emeasure_bind[where N="count_space UNIV"]) auto
```
```   700
```
```   701 lemma bind_return_pmf': "bind_pmf N return_pmf = N"
```
```   702 proof (transfer, clarify)
```
```   703   fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N"
```
```   704     by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
```
```   705 qed
```
```   706
```
```   707 lemma bind_return_pmf'': "bind_pmf N (\<lambda>x. return_pmf (f x)) = map_pmf f N"
```
```   708 proof (transfer, clarify)
```
```   709   fix N :: "'b measure" and f :: "'b \<Rightarrow> 'a" assume "prob_space N" "sets N = UNIV"
```
```   710   then show "N \<guillemotright>= (\<lambda>x. return (count_space UNIV) (f x)) = distr N (count_space UNIV) f"
```
```   711     by (subst bind_return_distr[symmetric])
```
```   712        (auto simp: prob_space.not_empty measurable_def comp_def)
```
```   713 qed
```
```   714
```
```   715 lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
```
```   716   by transfer
```
```   717      (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
```
```   718            simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
```
```   719
```
```   720 end
```
```   721
```
```   722 definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
```
```   723
```
```   724 lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
```
```   725   unfolding pair_pmf_def pmf_bind pmf_return
```
```   726   apply (subst integral_measure_pmf[where A="{b}"])
```
```   727   apply (auto simp: indicator_eq_0_iff)
```
```   728   apply (subst integral_measure_pmf[where A="{a}"])
```
```   729   apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
```
```   730   done
```
```   731
```
```   732 lemma set_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
```
```   733   unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
```
```   734
```
```   735 lemma bind_pair_pmf:
```
```   736   assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
```
```   737   shows "measure_pmf (pair_pmf A B) \<guillemotright>= M = (measure_pmf A \<guillemotright>= (\<lambda>x. measure_pmf B \<guillemotright>= (\<lambda>y. M (x, y))))"
```
```   738     (is "?L = ?R")
```
```   739 proof (rule measure_eqI)
```
```   740   have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
```
```   741     using M[THEN measurable_space] by (simp_all add: space_pair_measure)
```
```   742
```
```   743   have sets_eq_N: "sets ?L = N"
```
```   744     by (simp add: sets_bind[OF M'])
```
```   745   show "sets ?L = sets ?R"
```
```   746     unfolding sets_eq_N
```
```   747     apply (subst sets_bind[where N=N])
```
```   748     apply (rule measurable_bind)
```
```   749     apply (rule measurable_compose[OF _ measurable_measure_pmf])
```
```   750     apply measurable
```
```   751     apply (auto intro!: sets_pair_measure_cong sets_measure_pmf_count_space)
```
```   752     done
```
```   753   fix X assume "X \<in> sets ?L"
```
```   754   then have X[measurable]: "X \<in> sets N"
```
```   755     unfolding sets_eq_N .
```
```   756   then show "emeasure ?L X = emeasure ?R X"
```
```   757     apply (simp add: emeasure_bind[OF _ M' X])
```
```   758     unfolding pair_pmf_def measure_pmf_bind[of A]
```
```   759     apply (subst nn_integral_bind)
```
```   760     apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
```
```   761     apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
```
```   762     apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
```
```   763     apply measurable
```
```   764     unfolding measure_pmf_bind
```
```   765     apply (subst nn_integral_bind)
```
```   766     apply (rule measurable_compose[OF M' measurable_emeasure_subprob_algebra, OF X])
```
```   767     apply (subst measurable_cong_sets[OF sets_measure_pmf_count_space refl])
```
```   768     apply (subst subprob_algebra_cong[OF sets_measure_pmf_count_space])
```
```   769     apply measurable
```
```   770     apply (simp add: nn_integral_measure_pmf_finite set_return_pmf emeasure_nonneg pmf_return one_ereal_def[symmetric])
```
```   771     apply (subst emeasure_bind[OF _ _ X])
```
```   772     apply simp
```
```   773     apply (rule measurable_bind[where N="count_space UNIV"])
```
```   774     apply (rule measurable_compose[OF _ measurable_measure_pmf])
```
```   775     apply measurable
```
```   776     apply (rule sets_pair_measure_cong sets_measure_pmf_count_space refl)+
```
```   777     apply (subst measurable_cong_sets[OF sets_pair_measure_cong[OF sets_measure_pmf_count_space refl] refl])
```
```   778     apply simp
```
```   779     apply (subst emeasure_bind[OF _ _ X])
```
```   780     apply simp
```
```   781     apply (rule measurable_compose[OF _ M])
```
```   782     apply (auto simp: space_pair_measure)
```
```   783     done
```
```   784 qed
```
```   785
```
```   786
```
```   787 (*
```
```   788
```
```   789 definition
```
```   790   "rel_pmf P d1 d2 \<longleftrightarrow> (\<exists>p3. (\<forall>(x, y) \<in> set_pmf p3. P x y) \<and> map_pmf fst p3 = d1 \<and> map_pmf snd p3 = d2)"
```
```   791
```
```   792 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: pmf_rel
```
```   793 proof -
```
```   794   show "map_pmf id = id" by (rule map_pmf_id)
```
```   795   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose)
```
```   796   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"
```
```   797     by (intro map_pmg_cong refl)
```
```   798
```
```   799   show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
```
```   800     by (rule pmf_set_map)
```
```   801
```
```   802   { fix p :: "'s pmf"
```
```   803     have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
```
```   804       by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
```
```   805          (auto intro: countable_set_pmf inj_on_to_nat_on)
```
```   806     also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
```
```   807       by (metis Field_natLeq card_of_least natLeq_Well_order)
```
```   808     finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
```
```   809
```
```   810   show "\<And>R. pmf_rel R =
```
```   811          (BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
```
```   812          BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
```
```   813      by (auto simp add: fun_eq_iff pmf_rel_def BNF_Util.Grp_def OO_def)
```
```   814
```
```   815   { let ?f = "map_pmf fst" and ?s = "map_pmf snd"
```
```   816     fix R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and A assume "\<And>x y. (x, y) \<in> set_pmf A \<Longrightarrow> R x y"
```
```   817     fix S :: "'b \<Rightarrow> 'c \<Rightarrow> bool" and B assume "\<And>y z. (y, z) \<in> set_pmf B \<Longrightarrow> S y z"
```
```   818     assume "?f B = ?s A"
```
```   819     have "\<exists>C. (\<forall>(x, z)\<in>set_pmf C. \<exists>y. R x y \<and> S y z) \<and> ?f C = ?f A \<and> ?s C = ?s B"
```
```   820       sorry }
```
```   821 oops
```
```   822   then show "\<And>R::'a \<Rightarrow> 'b \<Rightarrow> bool. \<And>S::'b \<Rightarrow> 'c \<Rightarrow> bool. pmf_rel R OO pmf_rel S \<le> pmf_rel (R OO S)"
```
```   823       by (auto simp add: subset_eq pmf_rel_def fun_eq_iff OO_def Ball_def)
```
```   824 qed (fact natLeq_card_order natLeq_cinfinite)+
```
```   825
```
```   826 notepad
```
```   827 begin
```
```   828   fix x y :: "nat \<Rightarrow> real"
```
```   829   def IJz \<equiv> "rec_nat ((0, 0), \<lambda>_. 0) (\<lambda>n ((I, J), z).
```
```   830     let a = x I - (\<Sum>j<J. z (I, j)) ; b = y J - (\<Sum>i<I. z (i, J)) in
```
```   831       ((if a \<le> b then I + 1 else I, if b \<le> a then J + 1 else J), z((I, J) := min a b)))"
```
```   832   def I == "fst \<circ> fst \<circ> IJz" def J == "snd \<circ> fst \<circ> IJz" def z == "snd \<circ> IJz"
```
```   833   let ?a = "\<lambda>n. x (I n) - (\<Sum>j<J n. z n (I n, j))" and ?b = "\<lambda>n. y (J n) - (\<Sum>i<I n. z n (i, J n))"
```
```   834   have IJz_0[simp]: "\<And>p. z 0 p = 0" "I 0 = 0" "J 0 = 0"
```
```   835     by (simp_all add: I_def J_def z_def IJz_def)
```
```   836   have z_Suc[simp]: "\<And>n. z (Suc n) = (z n)((I n, J n) := min (?a n) (?b n))"
```
```   837     by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
```
```   838   have I_Suc[simp]: "\<And>n. I (Suc n) = (if ?a n \<le> ?b n then I n + 1 else I n)"
```
```   839     by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
```
```   840   have J_Suc[simp]: "\<And>n. J (Suc n) = (if ?b n \<le> ?a n then J n + 1 else J n)"
```
```   841     by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
```
```   842
```
```   843   { fix N have "\<And>p. z N p \<noteq> 0 \<Longrightarrow> \<exists>n<N. p = (I n, J n)"
```
```   844       by (induct N) (auto simp add: less_Suc_eq split: split_if_asm) }
```
```   845
```
```   846   { fix i n assume "i < I n"
```
```   847     then have "(\<Sum>j. z n (i, j)) = x i"
```
```   848     oops
```
```   849 *)
```
```   850
```
```   851 end
```
```   852
```