src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Tue Feb 10 13:50:30 2015 +0100 (2015-02-10)
changeset 59495 03944a830c4a
parent 59494 054f9c9d73ea
child 59496 6faf024a1893
permissions -rw-r--r--
add bind_cond_pmf_cancel
     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/Number_Theory/Binomial"
    12   "~~/src/HOL/Library/Multiset"
    13 begin
    14 
    15 lemma ereal_divide': "b \<noteq> 0 \<Longrightarrow> ereal (a / b) = ereal a / ereal b"
    16   using ereal_divide[of a b] by simp
    17 
    18 lemma (in finite_measure) countable_support:
    19   "countable {x. measure M {x} \<noteq> 0}"
    20 proof cases
    21   assume "measure M (space M) = 0"
    22   with bounded_measure measure_le_0_iff have "{x. measure M {x} \<noteq> 0} = {}"
    23     by auto
    24   then show ?thesis
    25     by simp
    26 next
    27   let ?M = "measure M (space M)" and ?m = "\<lambda>x. measure M {x}"
    28   assume "?M \<noteq> 0"
    29   then have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. ?M / Suc n < ?m x})"
    30     using reals_Archimedean[of "?m x / ?M" for x]
    31     by (auto simp: field_simps not_le[symmetric] measure_nonneg divide_le_0_iff measure_le_0_iff)
    32   have **: "\<And>n. finite {x. ?M / Suc n < ?m x}"
    33   proof (rule ccontr)
    34     fix n assume "infinite {x. ?M / Suc n < ?m x}" (is "infinite ?X")
    35     then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
    36       by (metis infinite_arbitrarily_large)
    37     from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> ?M / Suc n \<le> ?m x" 
    38       by auto
    39     { fix x assume "x \<in> X"
    40       from `?M \<noteq> 0` *[OF this] have "?m x \<noteq> 0" by (auto simp: field_simps measure_le_0_iff)
    41       then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
    42     note singleton_sets = this
    43     have "?M < (\<Sum>x\<in>X. ?M / Suc n)"
    44       using `?M \<noteq> 0` 
    45       by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc field_simps less_le measure_nonneg)
    46     also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
    47       by (rule setsum_mono) fact
    48     also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
    49       using singleton_sets `finite X`
    50       by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
    51     finally have "?M < measure M (\<Union>x\<in>X. {x})" .
    52     moreover have "measure M (\<Union>x\<in>X. {x}) \<le> ?M"
    53       using singleton_sets[THEN sets.sets_into_space] by (intro finite_measure_mono) auto
    54     ultimately show False by simp
    55   qed
    56   show ?thesis
    57     unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
    58 qed
    59 
    60 lemma (in finite_measure) AE_support_countable:
    61   assumes [simp]: "sets M = UNIV"
    62   shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
    63 proof
    64   assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
    65   then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
    66     by auto
    67   then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) = 
    68     (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
    69     by (subst emeasure_UN_countable)
    70        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
    71   also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
    72     by (auto intro!: nn_integral_cong split: split_indicator)
    73   also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
    74     by (subst emeasure_UN_countable)
    75        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
    76   also have "\<dots> = emeasure M (space M)"
    77     using ae by (intro emeasure_eq_AE) auto
    78   finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
    79     by (simp add: emeasure_single_in_space cong: rev_conj_cong)
    80   with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
    81   have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
    82     by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
    83   then show "AE x in M. measure M {x} \<noteq> 0"
    84     by (auto simp: emeasure_eq_measure)
    85 qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
    86 
    87 subsection {* PMF as measure *}
    88 
    89 typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
    90   morphisms measure_pmf Abs_pmf
    91   by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
    92      (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
    93 
    94 declare [[coercion measure_pmf]]
    95 
    96 lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
    97   using pmf.measure_pmf[of p] by auto
    98 
    99 interpretation measure_pmf!: prob_space "measure_pmf M" for M
   100   by (rule prob_space_measure_pmf)
   101 
   102 interpretation measure_pmf!: subprob_space "measure_pmf M" for M
   103   by (rule prob_space_imp_subprob_space) unfold_locales
   104 
   105 lemma subprob_space_measure_pmf: "subprob_space (measure_pmf x)"
   106   by unfold_locales
   107 
   108 locale pmf_as_measure
   109 begin
   110 
   111 setup_lifting type_definition_pmf
   112 
   113 end
   114 
   115 context
   116 begin
   117 
   118 interpretation pmf_as_measure .
   119 
   120 lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
   121 
   122 lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
   123 
   124 lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
   125   "\<lambda>f M. distr M (count_space UNIV) f"
   126 proof safe
   127   fix M and f :: "'a \<Rightarrow> 'b"
   128   let ?D = "distr M (count_space UNIV) f"
   129   assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
   130   interpret prob_space M by fact
   131   from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
   132   proof eventually_elim
   133     fix x
   134     have "measure M {x} \<le> measure M (f -` {f x})"
   135       by (intro finite_measure_mono) auto
   136     then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
   137       using measure_nonneg[of M "{x}"] by auto
   138   qed
   139   then show "AE x in ?D. measure ?D {x} \<noteq> 0"
   140     by (simp add: AE_distr_iff measure_distr measurable_def)
   141 qed (auto simp: measurable_def prob_space.prob_space_distr)
   142 
   143 declare [[coercion set_pmf]]
   144 
   145 lemma countable_set_pmf [simp]: "countable (set_pmf p)"
   146   by transfer (metis prob_space.finite_measure finite_measure.countable_support)
   147 
   148 lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
   149   by transfer metis
   150 
   151 lemma sets_measure_pmf_count_space[measurable_cong]:
   152   "sets (measure_pmf M) = sets (count_space UNIV)"
   153   by simp
   154 
   155 lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
   156   using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
   157 
   158 lemma measure_pmf_in_subprob_algebra[measurable (raw)]: "measure_pmf x \<in> space (subprob_algebra (count_space UNIV))"
   159   by (simp add: space_subprob_algebra subprob_space_measure_pmf)
   160 
   161 lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
   162   by (auto simp: measurable_def)
   163 
   164 lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
   165   by (intro measurable_cong_sets) simp_all
   166 
   167 lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
   168   by transfer (simp add: less_le measure_nonneg)
   169 
   170 lemma pmf_nonneg: "0 \<le> pmf p x"
   171   by transfer (simp add: measure_nonneg)
   172 
   173 lemma pmf_le_1: "pmf p x \<le> 1"
   174   by (simp add: pmf.rep_eq)
   175 
   176 lemma emeasure_pmf_single:
   177   fixes M :: "'a pmf"
   178   shows "emeasure M {x} = pmf M x"
   179   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   180 
   181 lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
   182   by transfer simp
   183 
   184 lemma emeasure_pmf_single_eq_zero_iff:
   185   fixes M :: "'a pmf"
   186   shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
   187   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   188 
   189 lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
   190 proof -
   191   { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
   192     with P have "AE x in M. x \<noteq> y"
   193       by auto
   194     with y have False
   195       by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
   196   then show ?thesis
   197     using AE_measure_pmf[of M] by auto
   198 qed
   199 
   200 lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
   201   using AE_measure_pmf[of M] by (intro notI) simp
   202 
   203 lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
   204   by transfer simp
   205 
   206 lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
   207   by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
   208 
   209 lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S"
   210   using emeasure_measure_pmf_finite[of S M] by(simp add: measure_pmf.emeasure_eq_measure)
   211 
   212 lemma nn_integral_measure_pmf_support:
   213   fixes f :: "'a \<Rightarrow> ereal"
   214   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"
   215   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
   216 proof -
   217   have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
   218     using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
   219   also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
   220     using assms by (intro nn_integral_indicator_finite) auto
   221   finally show ?thesis
   222     by (simp add: emeasure_measure_pmf_finite)
   223 qed
   224 
   225 lemma nn_integral_measure_pmf_finite:
   226   fixes f :: "'a \<Rightarrow> ereal"
   227   assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
   228   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
   229   using assms by (intro nn_integral_measure_pmf_support) auto
   230 lemma integrable_measure_pmf_finite:
   231   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
   232   shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
   233   by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
   234 
   235 lemma integral_measure_pmf:
   236   assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
   237   shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
   238 proof -
   239   have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
   240     using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
   241   also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
   242     by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
   243   finally show ?thesis .
   244 qed
   245 
   246 lemma integrable_pmf: "integrable (count_space X) (pmf M)"
   247 proof -
   248   have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
   249     by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
   250   then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
   251     by (simp add: integrable_iff_bounded pmf_nonneg)
   252   then show ?thesis
   253     by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def)
   254 qed
   255 
   256 lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
   257 proof -
   258   have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
   259     by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
   260   also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
   261     by (auto intro!: nn_integral_cong_AE split: split_indicator
   262              simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
   263                    AE_count_space set_pmf_iff)
   264   also have "\<dots> = emeasure M (X \<inter> M)"
   265     by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
   266   also have "\<dots> = emeasure M X"
   267     by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
   268   finally show ?thesis
   269     by (simp add: measure_pmf.emeasure_eq_measure)
   270 qed
   271 
   272 lemma integral_pmf_restrict:
   273   "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
   274     (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
   275   by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
   276 
   277 lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
   278 proof -
   279   have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
   280     by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
   281   then show ?thesis
   282     using measure_pmf.emeasure_space_1 by simp
   283 qed
   284 
   285 lemma in_null_sets_measure_pmfI:
   286   "A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)"
   287 using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"]
   288 by(auto simp add: null_sets_def AE_measure_pmf_iff)
   289 
   290 lemma map_pmf_id[simp]: "map_pmf id = id"
   291   by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
   292 
   293 lemma map_pmf_ident[simp]: "map_pmf (\<lambda>x. x) = (\<lambda>x. x)"
   294   using map_pmf_id unfolding id_def .
   295 
   296 lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
   297   by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
   298 
   299 lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
   300   using map_pmf_compose[of f g] by (simp add: comp_def)
   301 
   302 lemma map_pmf_cong:
   303   assumes "p = q"
   304   shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
   305   unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
   306   by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
   307 
   308 lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
   309   unfolding map_pmf.rep_eq by (subst emeasure_distr) auto
   310 
   311 lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
   312   unfolding map_pmf.rep_eq by (intro nn_integral_distr) auto
   313 
   314 lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f -` {x}) y \<partial>measure_pmf p)"
   315 proof(transfer fixing: f x)
   316   fix p :: "'b measure"
   317   presume "prob_space p"
   318   then interpret prob_space p .
   319   presume "sets p = UNIV"
   320   then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f -` {x}))"
   321     by(simp add: measure_distr measurable_def emeasure_eq_measure)
   322 qed simp_all
   323 
   324 lemma pmf_set_map: 
   325   fixes f :: "'a \<Rightarrow> 'b"
   326   shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
   327 proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
   328   fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
   329   assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
   330   interpret prob_space M by fact
   331   show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
   332   proof safe
   333     fix x assume "measure M (f -` {x}) \<noteq> 0"
   334     moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
   335       using ae by (intro finite_measure_eq_AE) auto
   336     ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
   337       by (metis measure_empty)
   338     then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
   339       by auto
   340   next
   341     fix x assume "measure M {x} \<noteq> 0"
   342     then have "0 < measure M {x}"
   343       using measure_nonneg[of M "{x}"] by auto
   344     also have "measure M {x} \<le> measure M (f -` {f x})"
   345       by (intro finite_measure_mono) auto
   346     finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
   347       by simp
   348   qed
   349 qed
   350 
   351 lemma set_map_pmf: "set_pmf (map_pmf f M) = f`set_pmf M"
   352   using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
   353 
   354 lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A"
   355 proof -
   356   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))"
   357     by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
   358   also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})"
   359     by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def)
   360   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})"
   361     by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI)
   362   also have "\<dots> = emeasure (measure_pmf p) A"
   363     by(auto intro: arg_cong2[where f=emeasure])
   364   finally show ?thesis .
   365 qed
   366 
   367 subsection {* PMFs as function *}
   368 
   369 context
   370   fixes f :: "'a \<Rightarrow> real"
   371   assumes nonneg: "\<And>x. 0 \<le> f x"
   372   assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   373 begin
   374 
   375 lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
   376 proof (intro conjI)
   377   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   378     by (simp split: split_indicator)
   379   show "AE x in density (count_space UNIV) (ereal \<circ> f).
   380     measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
   381     by (simp add: AE_density nonneg measure_def emeasure_density max_def)
   382   show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
   383     by default (simp add: emeasure_density prob)
   384 qed simp
   385 
   386 lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
   387 proof transfer
   388   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   389     by (simp split: split_indicator)
   390   fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
   391     by transfer (simp add: measure_def emeasure_density nonneg max_def)
   392 qed
   393 
   394 end
   395 
   396 lemma embed_pmf_transfer:
   397   "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"
   398   by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
   399 
   400 lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
   401 proof (transfer, elim conjE)
   402   fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
   403   assume "prob_space M" then interpret prob_space M .
   404   show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
   405   proof (rule measure_eqI)
   406     fix A :: "'a set"
   407     have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
   408       (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
   409       by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
   410     also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
   411       by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
   412     also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
   413       by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
   414          (auto simp: disjoint_family_on_def)
   415     also have "\<dots> = emeasure M A"
   416       using ae by (intro emeasure_eq_AE) auto
   417     finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
   418       using emeasure_space_1 by (simp add: emeasure_density)
   419   qed simp
   420 qed
   421 
   422 lemma td_pmf_embed_pmf:
   423   "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}"
   424   unfolding type_definition_def
   425 proof safe
   426   fix p :: "'a pmf"
   427   have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
   428     using measure_pmf.emeasure_space_1[of p] by simp
   429   then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
   430     by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
   431 
   432   show "embed_pmf (pmf p) = p"
   433     by (intro measure_pmf_inject[THEN iffD1])
   434        (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
   435 next
   436   fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   437   then show "pmf (embed_pmf f) = f"
   438     by (auto intro!: pmf_embed_pmf)
   439 qed (rule pmf_nonneg)
   440 
   441 end
   442 
   443 locale pmf_as_function
   444 begin
   445 
   446 setup_lifting td_pmf_embed_pmf
   447 
   448 lemma set_pmf_transfer[transfer_rule]: 
   449   assumes "bi_total A"
   450   shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
   451   using `bi_total A`
   452   by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
   453      metis+
   454 
   455 end
   456 
   457 context
   458 begin
   459 
   460 interpretation pmf_as_function .
   461 
   462 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
   463   by transfer auto
   464 
   465 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
   466   by (auto intro: pmf_eqI)
   467 
   468 end
   469 
   470 context
   471 begin
   472 
   473 interpretation pmf_as_function .
   474 
   475 subsubsection \<open> Bernoulli Distribution \<close>
   476 
   477 lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
   478   "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
   479   by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
   480            split: split_max split_min)
   481 
   482 lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
   483   by transfer simp
   484 
   485 lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
   486   by transfer simp
   487 
   488 lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
   489   by (auto simp add: set_pmf_iff UNIV_bool)
   490 
   491 lemma nn_integral_bernoulli_pmf[simp]: 
   492   assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
   493   shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
   494   by (subst nn_integral_measure_pmf_support[of UNIV])
   495      (auto simp: UNIV_bool field_simps)
   496 
   497 lemma integral_bernoulli_pmf[simp]: 
   498   assumes [simp]: "0 \<le> p" "p \<le> 1"
   499   shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
   500   by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
   501 
   502 subsubsection \<open> Geometric Distribution \<close>
   503 
   504 lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. 1 / 2^Suc n"
   505 proof
   506   note geometric_sums[of "1 / 2"]
   507   note sums_mult[OF this, of "1 / 2"]
   508   from sums_suminf_ereal[OF this]
   509   show "(\<integral>\<^sup>+ x. ereal (1 / 2 ^ Suc x) \<partial>count_space UNIV) = 1"
   510     by (simp add: nn_integral_count_space_nat field_simps)
   511 qed simp
   512 
   513 lemma pmf_geometric[simp]: "pmf geometric_pmf n = 1 / 2^Suc n"
   514   by transfer rule
   515 
   516 lemma set_pmf_geometric[simp]: "set_pmf geometric_pmf = UNIV"
   517   by (auto simp: set_pmf_iff)
   518 
   519 subsubsection \<open> Uniform Multiset Distribution \<close>
   520 
   521 context
   522   fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
   523 begin
   524 
   525 lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
   526 proof
   527   show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"  
   528     using M_not_empty
   529     by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
   530                   setsum_divide_distrib[symmetric])
   531        (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
   532 qed simp
   533 
   534 lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
   535   by transfer rule
   536 
   537 lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_of M"
   538   by (auto simp: set_pmf_iff)
   539 
   540 end
   541 
   542 subsubsection \<open> Uniform Distribution \<close>
   543 
   544 context
   545   fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
   546 begin
   547 
   548 lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
   549 proof
   550   show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"  
   551     using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
   552 qed simp
   553 
   554 lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
   555   by transfer rule
   556 
   557 lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
   558   using S_finite S_not_empty by (auto simp: set_pmf_iff)
   559 
   560 lemma emeasure_pmf_of_set[simp]: "emeasure pmf_of_set S = 1"
   561   by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
   562 
   563 end
   564 
   565 subsubsection \<open> Poisson Distribution \<close>
   566 
   567 context
   568   fixes rate :: real assumes rate_pos: "0 < rate"
   569 begin
   570 
   571 lift_definition poisson_pmf :: "nat pmf" is "\<lambda>k. rate ^ k / fact k * exp (-rate)"
   572 proof
   573   (* Proof by Manuel Eberl *)
   574 
   575   have summable: "summable (\<lambda>x::nat. rate ^ x / fact x)" using summable_exp
   576     by (simp add: field_simps field_divide_inverse[symmetric])
   577   have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x * exp (-rate) \<partial>count_space UNIV) =
   578           exp (-rate) * (\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV)"
   579     by (simp add: field_simps nn_integral_cmult[symmetric])
   580   also from rate_pos have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV) = (\<Sum>x. rate ^ x / fact x)"
   581     by (simp_all add: nn_integral_count_space_nat suminf_ereal summable suminf_ereal_finite)
   582   also have "... = exp rate" unfolding exp_def
   583     by (simp add: field_simps field_divide_inverse[symmetric] transfer_int_nat_factorial)
   584   also have "ereal (exp (-rate)) * ereal (exp rate) = 1"
   585     by (simp add: mult_exp_exp)
   586   finally show "(\<integral>\<^sup>+ x. ereal (rate ^ x / real (fact x) * exp (- rate)) \<partial>count_space UNIV) = 1" .
   587 qed (simp add: rate_pos[THEN less_imp_le])
   588 
   589 lemma pmf_poisson[simp]: "pmf poisson_pmf k = rate ^ k / fact k * exp (-rate)"
   590   by transfer rule
   591 
   592 lemma set_pmf_poisson[simp]: "set_pmf poisson_pmf = UNIV"
   593   using rate_pos by (auto simp: set_pmf_iff)
   594 
   595 end
   596 
   597 subsubsection \<open> Binomial Distribution \<close>
   598 
   599 context
   600   fixes n :: nat and p :: real assumes p_nonneg: "0 \<le> p" and p_le_1: "p \<le> 1"
   601 begin
   602 
   603 lift_definition binomial_pmf :: "nat pmf" is "\<lambda>k. (n choose k) * p^k * (1 - p)^(n - k)"
   604 proof
   605   have "(\<integral>\<^sup>+k. ereal (real (n choose k) * p ^ k * (1 - p) ^ (n - k)) \<partial>count_space UNIV) =
   606     ereal (\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k))"
   607     using p_le_1 p_nonneg by (subst nn_integral_count_space') auto
   608   also have "(\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k)) = (p + (1 - p)) ^ n"
   609     by (subst binomial_ring) (simp add: atLeast0AtMost real_of_nat_def)
   610   finally show "(\<integral>\<^sup>+ x. ereal (real (n choose x) * p ^ x * (1 - p) ^ (n - x)) \<partial>count_space UNIV) = 1"
   611     by simp
   612 qed (insert p_nonneg p_le_1, simp)
   613 
   614 lemma pmf_binomial[simp]: "pmf binomial_pmf k = (n choose k) * p^k * (1 - p)^(n - k)"
   615   by transfer rule
   616 
   617 lemma set_pmf_binomial_eq: "set_pmf binomial_pmf = (if p = 0 then {0} else if p = 1 then {n} else {.. n})"
   618   using p_nonneg p_le_1 unfolding set_eq_iff set_pmf_iff pmf_binomial by (auto simp: set_pmf_iff)
   619 
   620 end
   621 
   622 end
   623 
   624 lemma set_pmf_binomial_0[simp]: "set_pmf (binomial_pmf n 0) = {0}"
   625   by (simp add: set_pmf_binomial_eq)
   626 
   627 lemma set_pmf_binomial_1[simp]: "set_pmf (binomial_pmf n 1) = {n}"
   628   by (simp add: set_pmf_binomial_eq)
   629 
   630 lemma set_pmf_binomial[simp]: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (binomial_pmf n p) = {..n}"
   631   by (simp add: set_pmf_binomial_eq)
   632 
   633 subsection \<open> Monad Interpretation \<close>
   634 
   635 lemma measurable_measure_pmf[measurable]:
   636   "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
   637   by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
   638 
   639 lemma bind_pmf_cong:
   640   assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
   641   assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
   642   shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
   643 proof (rule measure_eqI)
   644   show "sets (measure_pmf x \<guillemotright>= A) = sets (measure_pmf x \<guillemotright>= B)"
   645     using assms by (subst (1 2) sets_bind) (auto simp: space_subprob_algebra)
   646 next
   647   fix X assume "X \<in> sets (measure_pmf x \<guillemotright>= A)"
   648   then have X: "X \<in> sets N"
   649     using assms by (subst (asm) sets_bind) (auto simp: space_subprob_algebra)
   650   show "emeasure (measure_pmf x \<guillemotright>= A) X = emeasure (measure_pmf x \<guillemotright>= B) X"
   651     using assms
   652     by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
   653        (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
   654 qed
   655 
   656 context
   657 begin
   658 
   659 interpretation pmf_as_measure .
   660 
   661 lift_definition join_pmf :: "'a pmf pmf \<Rightarrow> 'a pmf" is "\<lambda>M. measure_pmf M \<guillemotright>= measure_pmf"
   662 proof (intro conjI)
   663   fix M :: "'a pmf pmf"
   664 
   665   interpret bind: prob_space "measure_pmf M \<guillemotright>= measure_pmf"
   666     apply (intro measure_pmf.prob_space_bind[where S="count_space UNIV"] AE_I2)
   667     apply (auto intro!: subprob_space_measure_pmf simp: space_subprob_algebra)
   668     apply unfold_locales
   669     done
   670   show "prob_space (measure_pmf M \<guillemotright>= measure_pmf)"
   671     by intro_locales
   672   show "sets (measure_pmf M \<guillemotright>= measure_pmf) = UNIV"
   673     by (subst sets_bind) auto
   674   have "AE x in measure_pmf M \<guillemotright>= measure_pmf. emeasure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
   675     by (auto simp: AE_bind[where B="count_space UNIV"] measure_pmf_in_subprob_algebra
   676                    emeasure_bind[where N="count_space UNIV"] AE_measure_pmf_iff nn_integral_0_iff_AE
   677                    measure_pmf.emeasure_eq_measure measure_le_0_iff set_pmf_iff pmf.rep_eq)
   678   then show "AE x in measure_pmf M \<guillemotright>= measure_pmf. measure (measure_pmf M \<guillemotright>= measure_pmf) {x} \<noteq> 0"
   679     unfolding bind.emeasure_eq_measure by simp
   680 qed
   681 
   682 lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
   683 proof (transfer fixing: N i)
   684   have N: "subprob_space (measure_pmf N)"
   685     by (rule prob_space_imp_subprob_space) intro_locales
   686   show "measure (measure_pmf N \<guillemotright>= measure_pmf) {i} = integral\<^sup>L (measure_pmf N) (\<lambda>M. measure M {i})"
   687     using measurable_measure_pmf[of "\<lambda>x. x"]
   688     by (intro subprob_space.measure_bind[where N="count_space UNIV", OF N]) auto
   689 qed (auto simp: Transfer.Rel_def rel_fun_def cr_pmf_def)
   690 
   691 lemma ereal_pmf_join: "ereal (pmf (join_pmf N) i) = (\<integral>\<^sup>+M. pmf M i \<partial>measure_pmf N)"
   692   unfolding pmf_join
   693   by (intro nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
   694      (auto simp: pmf_le_1 pmf_nonneg)
   695 
   696 lemma set_pmf_join_pmf: "set_pmf (join_pmf f) = (\<Union>p\<in>set_pmf f. set_pmf p)"
   697 apply(simp add: set_eq_iff set_pmf_iff pmf_join)
   698 apply(subst integral_nonneg_eq_0_iff_AE)
   699 apply(auto simp add: pmf_le_1 pmf_nonneg AE_measure_pmf_iff intro!: measure_pmf.integrable_const_bound[where B=1])
   700 done
   701 
   702 lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
   703   by (auto intro!: prob_space_return simp: AE_return measure_return)
   704 
   705 lemma join_return_pmf: "join_pmf (return_pmf M) = M"
   706   by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
   707 
   708 lemma map_return_pmf: "map_pmf f (return_pmf x) = return_pmf (f x)"
   709   by transfer (simp add: distr_return)
   710 
   711 lemma map_pmf_const[simp]: "map_pmf (\<lambda>_. c) M = return_pmf c"
   712   by transfer (auto simp: prob_space.distr_const)
   713 
   714 lemma set_return_pmf: "set_pmf (return_pmf x) = {x}"
   715   by transfer (auto simp add: measure_return split: split_indicator)
   716 
   717 lemma pmf_return: "pmf (return_pmf x) y = indicator {y} x"
   718   by transfer (simp add: measure_return)
   719 
   720 lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
   721   unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
   722 
   723 lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
   724   unfolding return_pmf.rep_eq by (intro emeasure_return) auto
   725 
   726 end
   727 
   728 lemma return_pmf_inj[simp]: "return_pmf x = return_pmf y \<longleftrightarrow> x = y"
   729   by (metis insertI1 set_return_pmf singletonD)
   730 
   731 definition "bind_pmf M f = join_pmf (map_pmf f M)"
   732 
   733 lemma (in pmf_as_measure) bind_transfer[transfer_rule]:
   734   "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"
   735 proof (auto simp: pmf_as_measure.cr_pmf_def rel_fun_def bind_pmf_def join_pmf.rep_eq map_pmf.rep_eq)
   736   fix M f and g :: "'a \<Rightarrow> 'b pmf" assume "\<forall>x. f x = measure_pmf (g x)"
   737   then have f: "f = (\<lambda>x. measure_pmf (g x))"
   738     by auto
   739   show "measure_pmf M \<guillemotright>= f = distr (measure_pmf M) (count_space UNIV) g \<guillemotright>= measure_pmf"
   740     unfolding f by (subst bind_distr[OF _ measurable_measure_pmf]) auto
   741 qed
   742 
   743 lemma ereal_pmf_bind: "pmf (bind_pmf N f) i = (\<integral>\<^sup>+x. pmf (f x) i \<partial>measure_pmf N)"
   744   by (auto intro!: nn_integral_distr simp: bind_pmf_def ereal_pmf_join map_pmf.rep_eq)
   745 
   746 lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
   747   by (auto intro!: integral_distr simp: bind_pmf_def pmf_join map_pmf.rep_eq)
   748 
   749 lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
   750   unfolding bind_pmf_def map_return_pmf join_return_pmf ..
   751 
   752 lemma join_eq_bind_pmf: "join_pmf M = bind_pmf M id"
   753   by (simp add: bind_pmf_def)
   754 
   755 lemma bind_pmf_const[simp]: "bind_pmf M (\<lambda>x. c) = c"
   756   unfolding bind_pmf_def map_pmf_const join_return_pmf ..
   757 
   758 lemma set_bind_pmf: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
   759   apply (simp add: set_eq_iff set_pmf_iff pmf_bind)
   760   apply (subst integral_nonneg_eq_0_iff_AE)
   761   apply (auto simp: pmf_nonneg pmf_le_1 AE_measure_pmf_iff
   762               intro!: measure_pmf.integrable_const_bound[where B=1])
   763   done
   764 
   765 
   766 lemma measurable_pair_restrict_pmf2:
   767   assumes "countable A"
   768   assumes [measurable]: "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
   769   shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L" (is "f \<in> measurable ?M _")
   770 proof -
   771   have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
   772     by (simp add: restrict_count_space)
   773 
   774   show ?thesis
   775     by (intro measurable_compose_countable'[where f="\<lambda>a b. f (fst b, a)" and g=snd and I=A,
   776                                             unfolded pair_collapse] assms)
   777         measurable
   778 qed
   779 
   780 lemma measurable_pair_restrict_pmf1:
   781   assumes "countable A"
   782   assumes [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
   783   shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
   784 proof -
   785   have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
   786     by (simp add: restrict_count_space)
   787 
   788   show ?thesis
   789     by (intro measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A,
   790                                             unfolded pair_collapse] assms)
   791         measurable
   792 qed
   793                                 
   794 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))"
   795   unfolding pmf_eq_iff pmf_bind
   796 proof
   797   fix i
   798   interpret B: prob_space "restrict_space B B"
   799     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
   800        (auto simp: AE_measure_pmf_iff)
   801   interpret A: prob_space "restrict_space A A"
   802     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
   803        (auto simp: AE_measure_pmf_iff)
   804 
   805   interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
   806     by unfold_locales
   807 
   808   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)"
   809     by (rule integral_cong) (auto intro!: integral_pmf_restrict)
   810   also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
   811     by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
   812               countable_set_pmf borel_measurable_count_space)
   813   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
   814     by (rule AB.Fubini_integral[symmetric])
   815        (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
   816              simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
   817   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
   818     by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
   819               countable_set_pmf borel_measurable_count_space)
   820   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
   821     by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
   822   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)" .
   823 qed
   824 
   825 
   826 context
   827 begin
   828 
   829 interpretation pmf_as_measure .
   830 
   831 lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<guillemotright>= (\<lambda>x. measure_pmf (f x)))"
   832   by transfer simp
   833 
   834 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)"
   835   using measurable_measure_pmf[of N]
   836   unfolding measure_pmf_bind
   837   apply (subst (1 3) nn_integral_max_0[symmetric])
   838   apply (intro nn_integral_bind[where B="count_space UNIV"])
   839   apply auto
   840   done
   841 
   842 lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
   843   using measurable_measure_pmf[of N]
   844   unfolding measure_pmf_bind
   845   by (subst emeasure_bind[where N="count_space UNIV"]) auto
   846 
   847 lemma bind_return_pmf': "bind_pmf N return_pmf = N"
   848 proof (transfer, clarify)
   849   fix N :: "'a measure" assume "sets N = UNIV" then show "N \<guillemotright>= return (count_space UNIV) = N"
   850     by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
   851 qed
   852 
   853 lemma bind_return_pmf'': "bind_pmf N (\<lambda>x. return_pmf (f x)) = map_pmf f N"
   854 proof (transfer, clarify)
   855   fix N :: "'b measure" and f :: "'b \<Rightarrow> 'a" assume "prob_space N" "sets N = UNIV"
   856   then show "N \<guillemotright>= (\<lambda>x. return (count_space UNIV) (f x)) = distr N (count_space UNIV) f"
   857     by (subst bind_return_distr[symmetric])
   858        (auto simp: prob_space.not_empty measurable_def comp_def)
   859 qed
   860 
   861 lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
   862   by transfer
   863      (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
   864            simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
   865 
   866 end
   867 
   868 lemma map_bind_pmf: "map_pmf f (bind_pmf M g) = bind_pmf M (\<lambda>x. map_pmf f (g x))"
   869   unfolding bind_return_pmf''[symmetric] bind_assoc_pmf[of M] ..
   870 
   871 lemma bind_map_pmf: "bind_pmf (map_pmf f M) g = bind_pmf M (\<lambda>x. g (f x))"
   872   unfolding bind_return_pmf''[symmetric] bind_assoc_pmf bind_return_pmf ..
   873 
   874 lemma map_join_pmf: "map_pmf f (join_pmf AA) = join_pmf (map_pmf (map_pmf f) AA)"
   875   unfolding bind_pmf_def[symmetric]
   876   unfolding bind_return_pmf''[symmetric] join_eq_bind_pmf bind_assoc_pmf
   877   by (simp add: bind_return_pmf'')
   878 
   879 definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
   880 
   881 lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
   882   unfolding pair_pmf_def pmf_bind pmf_return
   883   apply (subst integral_measure_pmf[where A="{b}"])
   884   apply (auto simp: indicator_eq_0_iff)
   885   apply (subst integral_measure_pmf[where A="{a}"])
   886   apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
   887   done
   888 
   889 lemma set_pair_pmf: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
   890   unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
   891 
   892 lemma measure_pmf_in_subprob_space[measurable (raw)]:
   893   "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
   894   by (simp add: space_subprob_algebra) intro_locales
   895 
   896 lemma nn_integral_pair_pmf': "(\<integral>\<^sup>+x. f x \<partial>pair_pmf A B) = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. f (a, b) \<partial>B \<partial>A)"
   897 proof -
   898   have "(\<integral>\<^sup>+x. f x \<partial>pair_pmf A B) = (\<integral>\<^sup>+x. max 0 (f x) * indicator (A \<times> B) x \<partial>pair_pmf A B)"
   899     by (subst nn_integral_max_0[symmetric])
   900        (auto simp: AE_measure_pmf_iff set_pair_pmf intro!: nn_integral_cong_AE)
   901   also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) * indicator (A \<times> B) (a, b) \<partial>B \<partial>A)"
   902     by (simp add: pair_pmf_def)
   903   also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) \<partial>B \<partial>A)"
   904     by (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
   905   finally show ?thesis
   906     unfolding nn_integral_max_0 .
   907 qed
   908 
   909 lemma pair_map_pmf1: "pair_pmf (map_pmf f A) B = map_pmf (apfst f) (pair_pmf A B)"
   910 proof (safe intro!: pmf_eqI)
   911   fix a :: "'a" and b :: "'b"
   912   have [simp]: "\<And>c d. indicator (apfst f -` {(a, b)}) (c, d) = indicator (f -` {a}) c * (indicator {b} d::ereal)"
   913     by (auto split: split_indicator)
   914 
   915   have "ereal (pmf (pair_pmf (map_pmf f A) B) (a, b)) =
   916          ereal (pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b))"
   917     unfolding pmf_pair ereal_pmf_map
   918     by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_multc pmf_nonneg
   919                   emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
   920   then show "pmf (pair_pmf (map_pmf f A) B) (a, b) = pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b)"
   921     by simp
   922 qed
   923 
   924 lemma pair_map_pmf2: "pair_pmf A (map_pmf f B) = map_pmf (apsnd f) (pair_pmf A B)"
   925 proof (safe intro!: pmf_eqI)
   926   fix a :: "'a" and b :: "'b"
   927   have [simp]: "\<And>c d. indicator (apsnd f -` {(a, b)}) (c, d) = indicator {a} c * (indicator (f -` {b}) d::ereal)"
   928     by (auto split: split_indicator)
   929 
   930   have "ereal (pmf (pair_pmf A (map_pmf f B)) (a, b)) =
   931          ereal (pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b))"
   932     unfolding pmf_pair ereal_pmf_map
   933     by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_cmult nn_integral_multc pmf_nonneg
   934                   emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
   935   then show "pmf (pair_pmf A (map_pmf f B)) (a, b) = pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b)"
   936     by simp
   937 qed
   938 
   939 lemma map_pair: "map_pmf (\<lambda>(a, b). (f a, g b)) (pair_pmf A B) = pair_pmf (map_pmf f A) (map_pmf g B)"
   940   by (simp add: pair_map_pmf2 pair_map_pmf1 map_pmf_comp split_beta')
   941 
   942 lemma bind_pair_pmf:
   943   assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
   944   shows "measure_pmf (pair_pmf A B) \<guillemotright>= M = (measure_pmf A \<guillemotright>= (\<lambda>x. measure_pmf B \<guillemotright>= (\<lambda>y. M (x, y))))"
   945     (is "?L = ?R")
   946 proof (rule measure_eqI)
   947   have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
   948     using M[THEN measurable_space] by (simp_all add: space_pair_measure)
   949 
   950   note measurable_bind[where N="count_space UNIV", measurable]
   951   note measure_pmf_in_subprob_space[simp]
   952 
   953   have sets_eq_N: "sets ?L = N"
   954     by (subst sets_bind[OF sets_kernel[OF M']]) auto
   955   show "sets ?L = sets ?R"
   956     using measurable_space[OF M]
   957     by (simp add: sets_eq_N space_pair_measure space_subprob_algebra)
   958   fix X assume "X \<in> sets ?L"
   959   then have X[measurable]: "X \<in> sets N"
   960     unfolding sets_eq_N .
   961   then show "emeasure ?L X = emeasure ?R X"
   962     apply (simp add: emeasure_bind[OF _ M' X])
   963     apply (simp add: nn_integral_bind[where B="count_space UNIV"] pair_pmf_def measure_pmf_bind[of A]
   964       nn_integral_measure_pmf_finite set_return_pmf emeasure_nonneg pmf_return one_ereal_def[symmetric])
   965     apply (subst emeasure_bind[OF _ _ X])
   966     apply measurable
   967     apply (subst emeasure_bind[OF _ _ X])
   968     apply measurable
   969     done
   970 qed
   971 
   972 lemma join_map_return_pmf: "join_pmf (map_pmf return_pmf A) = A"
   973   unfolding bind_pmf_def[symmetric] bind_return_pmf' ..
   974 
   975 lemma map_fst_pair_pmf: "map_pmf fst (pair_pmf A B) = A"
   976   by (simp add: pair_pmf_def bind_return_pmf''[symmetric] bind_assoc_pmf bind_return_pmf bind_return_pmf')
   977 
   978 lemma map_snd_pair_pmf: "map_pmf snd (pair_pmf A B) = B"
   979   by (simp add: pair_pmf_def bind_return_pmf''[symmetric] bind_assoc_pmf bind_return_pmf bind_return_pmf')
   980 
   981 lemma nn_integral_pmf':
   982   "inj_on f A \<Longrightarrow> (\<integral>\<^sup>+x. pmf p (f x) \<partial>count_space A) = emeasure p (f ` A)"
   983   by (subst nn_integral_bij_count_space[where g=f and B="f`A"])
   984      (auto simp: bij_betw_def nn_integral_pmf)
   985 
   986 lemma pmf_le_0_iff[simp]: "pmf M p \<le> 0 \<longleftrightarrow> pmf M p = 0"
   987   using pmf_nonneg[of M p] by simp
   988 
   989 lemma min_pmf_0[simp]: "min (pmf M p) 0 = 0" "min 0 (pmf M p) = 0"
   990   using pmf_nonneg[of M p] by simp_all
   991 
   992 lemma pmf_eq_0_set_pmf: "pmf M p = 0 \<longleftrightarrow> p \<notin> set_pmf M"
   993   unfolding set_pmf_iff by simp
   994 
   995 lemma pmf_map_inj: "inj_on f (set_pmf M) \<Longrightarrow> x \<in> set_pmf M \<Longrightarrow> pmf (map_pmf f M) (f x) = pmf M x"
   996   by (auto simp: pmf.rep_eq map_pmf.rep_eq measure_distr AE_measure_pmf_iff inj_onD
   997            intro!: measure_pmf.finite_measure_eq_AE)
   998 
   999 subsection \<open> Conditional Probabilities \<close>
  1000 
  1001 context
  1002   fixes p :: "'a pmf" and s :: "'a set"
  1003   assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
  1004 begin
  1005 
  1006 interpretation pmf_as_measure .
  1007 
  1008 lemma emeasure_measure_pmf_not_zero: "emeasure (measure_pmf p) s \<noteq> 0"
  1009 proof
  1010   assume "emeasure (measure_pmf p) s = 0"
  1011   then have "AE x in measure_pmf p. x \<notin> s"
  1012     by (rule AE_I[rotated]) auto
  1013   with not_empty show False
  1014     by (auto simp: AE_measure_pmf_iff)
  1015 qed
  1016 
  1017 lemma measure_measure_pmf_not_zero: "measure (measure_pmf p) s \<noteq> 0"
  1018   using emeasure_measure_pmf_not_zero unfolding measure_pmf.emeasure_eq_measure by simp
  1019 
  1020 lift_definition cond_pmf :: "'a pmf" is
  1021   "uniform_measure (measure_pmf p) s"
  1022 proof (intro conjI)
  1023   show "prob_space (uniform_measure (measure_pmf p) s)"
  1024     by (intro prob_space_uniform_measure) (auto simp: emeasure_measure_pmf_not_zero)
  1025   show "AE x in uniform_measure (measure_pmf p) s. measure (uniform_measure (measure_pmf p) s) {x} \<noteq> 0"
  1026     by (simp add: emeasure_measure_pmf_not_zero measure_measure_pmf_not_zero AE_uniform_measure
  1027                   AE_measure_pmf_iff set_pmf.rep_eq)
  1028 qed simp
  1029 
  1030 lemma pmf_cond: "pmf cond_pmf x = (if x \<in> s then pmf p x / measure p s else 0)"
  1031   by transfer (simp add: emeasure_measure_pmf_not_zero pmf.rep_eq)
  1032 
  1033 lemma set_cond_pmf: "set_pmf cond_pmf = set_pmf p \<inter> s"
  1034   by (auto simp add: set_pmf_iff pmf_cond measure_measure_pmf_not_zero split: split_if_asm)
  1035 
  1036 end
  1037 
  1038 lemma cond_map_pmf:
  1039   assumes "set_pmf p \<inter> f -` s \<noteq> {}"
  1040   shows "cond_pmf (map_pmf f p) s = map_pmf f (cond_pmf p (f -` s))"
  1041 proof -
  1042   have *: "set_pmf (map_pmf f p) \<inter> s \<noteq> {}"
  1043     using assms by (simp add: set_map_pmf) auto
  1044   { fix x
  1045     have "ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x) =
  1046       emeasure p (f -` s \<inter> f -` {x}) / emeasure p (f -` s)"
  1047       unfolding ereal_pmf_map cond_pmf.rep_eq[OF assms] by (simp add: nn_integral_uniform_measure)
  1048     also have "f -` s \<inter> f -` {x} = (if x \<in> s then f -` {x} else {})"
  1049       by auto
  1050     also have "emeasure p (if x \<in> s then f -` {x} else {}) / emeasure p (f -` s) =
  1051       ereal (pmf (cond_pmf (map_pmf f p) s) x)"
  1052       using measure_measure_pmf_not_zero[OF *]
  1053       by (simp add: pmf_cond[OF *] ereal_divide' ereal_pmf_map measure_pmf.emeasure_eq_measure[symmetric]
  1054                del: ereal_divide)
  1055     finally have "ereal (pmf (cond_pmf (map_pmf f p) s) x) = ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x)"
  1056       by simp }
  1057   then show ?thesis
  1058     by (intro pmf_eqI) simp
  1059 qed
  1060 
  1061 lemma bind_cond_pmf_cancel:
  1062   assumes in_S: "\<And>x. x \<in> set_pmf p \<Longrightarrow> x \<in> S x"
  1063   assumes S_eq: "\<And>x y. x \<in> S y \<Longrightarrow> S x = S y"
  1064   shows "bind_pmf p (\<lambda>x. cond_pmf p (S x)) = p"
  1065 proof (rule pmf_eqI)
  1066   have [simp]: "\<And>x. x \<in> p \<Longrightarrow> p \<inter> (S x) \<noteq> {}"
  1067     using in_S by auto
  1068   fix z
  1069   have pmf_le: "pmf p z \<le> measure p (S z)"
  1070   proof cases
  1071     assume "z \<in> p" from in_S[OF this] show ?thesis
  1072       by (auto intro!: measure_pmf.finite_measure_mono simp: pmf.rep_eq)
  1073   qed (simp add: set_pmf_iff measure_nonneg)
  1074 
  1075   have "ereal (pmf (bind_pmf p (\<lambda>x. cond_pmf p (S x))) z) =
  1076     (\<integral>\<^sup>+ x. ereal (pmf p z / measure p (S z)) * indicator (S z) x \<partial>p)"
  1077     by (subst ereal_pmf_bind)
  1078        (auto intro!: nn_integral_cong_AE dest!: S_eq split: split_indicator
  1079              simp: AE_measure_pmf_iff pmf_cond pmf_eq_0_set_pmf in_S)
  1080   also have "\<dots> = pmf p z"
  1081     using pmf_le pmf_nonneg[of p z]
  1082     by (subst nn_integral_cmult) (simp_all add: measure_nonneg measure_pmf.emeasure_eq_measure)
  1083   finally show "pmf (bind_pmf p (\<lambda>x. cond_pmf p (S x))) z = pmf p z"
  1084     by simp
  1085 qed
  1086 
  1087 inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
  1088 for R p q
  1089 where
  1090   "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y; 
  1091      map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
  1092   \<Longrightarrow> rel_pmf R p q"
  1093 
  1094 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
  1095 proof -
  1096   show "map_pmf id = id" by (rule map_pmf_id)
  1097   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
  1098   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"
  1099     by (intro map_pmf_cong refl)
  1100 
  1101   show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
  1102     by (rule pmf_set_map)
  1103 
  1104   { fix p :: "'s pmf"
  1105     have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
  1106       by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
  1107          (auto intro: countable_set_pmf)
  1108     also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
  1109       by (metis Field_natLeq card_of_least natLeq_Well_order)
  1110     finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
  1111 
  1112   show "\<And>R. rel_pmf R =
  1113          (BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
  1114          BNF_Def.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
  1115      by (auto simp add: fun_eq_iff BNF_Def.Grp_def OO_def rel_pmf.simps)
  1116 
  1117   { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
  1118     assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
  1119       and x: "x \<in> set_pmf p"
  1120     thus "f x = g x" by simp }
  1121 
  1122   fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
  1123   { fix p q r
  1124     assume pq: "rel_pmf R p q"
  1125       and qr:"rel_pmf S q r"
  1126     from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
  1127       and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
  1128     from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
  1129       and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
  1130 
  1131     def pr \<equiv> "bind_pmf pq (\<lambda>(x, y). bind_pmf (cond_pmf qr {(y', z). y' = y}) (\<lambda>(y', z). return_pmf (x, z)))"
  1132     have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {(y', z). y' = y} \<noteq> {}"
  1133       by (force simp: q' set_map_pmf)
  1134 
  1135     have "rel_pmf (R OO S) p r"
  1136     proof (rule rel_pmf.intros)
  1137       fix x z assume "(x, z) \<in> pr"
  1138       then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
  1139         by (auto simp: q pr_welldefined pr_def set_bind_pmf split_beta set_return_pmf set_cond_pmf set_map_pmf)
  1140       with pq qr show "(R OO S) x z"
  1141         by blast
  1142     next
  1143       have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {(y', z). y' = y}))"
  1144         by (simp add: pr_def q split_beta bind_map_pmf bind_return_pmf'' map_bind_pmf map_return_pmf)
  1145       then show "map_pmf snd pr = r"
  1146         unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) auto
  1147     qed (simp add: pr_def map_bind_pmf split_beta map_return_pmf bind_return_pmf'' p) }
  1148   then show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
  1149     by(auto simp add: le_fun_def)
  1150 qed (fact natLeq_card_order natLeq_cinfinite)+
  1151 
  1152 lemma rel_pmf_return_pmf1: "rel_pmf R (return_pmf x) M \<longleftrightarrow> (\<forall>a\<in>M. R x a)"
  1153 proof safe
  1154   fix a assume "a \<in> M" "rel_pmf R (return_pmf x) M"
  1155   then obtain pq where *: "\<And>a b. (a, b) \<in> set_pmf pq \<Longrightarrow> R a b"
  1156     and eq: "return_pmf x = map_pmf fst pq" "M = map_pmf snd pq"
  1157     by (force elim: rel_pmf.cases)
  1158   moreover have "set_pmf (return_pmf x) = {x}"
  1159     by (simp add: set_return_pmf)
  1160   with `a \<in> M` have "(x, a) \<in> pq"
  1161     by (force simp: eq set_map_pmf)
  1162   with * show "R x a"
  1163     by auto
  1164 qed (auto intro!: rel_pmf.intros[where pq="pair_pmf (return_pmf x) M"]
  1165           simp: map_fst_pair_pmf map_snd_pair_pmf set_pair_pmf set_return_pmf)
  1166 
  1167 lemma rel_pmf_return_pmf2: "rel_pmf R M (return_pmf x) \<longleftrightarrow> (\<forall>a\<in>M. R a x)"
  1168   by (subst pmf.rel_flip[symmetric]) (simp add: rel_pmf_return_pmf1)
  1169 
  1170 lemma rel_return_pmf[simp]: "rel_pmf R (return_pmf x1) (return_pmf x2) = R x1 x2"
  1171   unfolding rel_pmf_return_pmf2 set_return_pmf by simp
  1172 
  1173 lemma rel_pmf_False[simp]: "rel_pmf (\<lambda>x y. False) x y = False"
  1174   unfolding pmf.in_rel fun_eq_iff using set_pmf_not_empty by fastforce
  1175 
  1176 lemma rel_pmf_rel_prod:
  1177   "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B') \<longleftrightarrow> rel_pmf R A B \<and> rel_pmf S A' B'"
  1178 proof safe
  1179   assume "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
  1180   then obtain pq where pq: "\<And>a b c d. ((a, c), (b, d)) \<in> set_pmf pq \<Longrightarrow> R a b \<and> S c d"
  1181     and eq: "map_pmf fst pq = pair_pmf A A'" "map_pmf snd pq = pair_pmf B B'"
  1182     by (force elim: rel_pmf.cases)
  1183   show "rel_pmf R A B"
  1184   proof (rule rel_pmf.intros)
  1185     let ?f = "\<lambda>(a, b). (fst a, fst b)"
  1186     have [simp]: "(\<lambda>x. fst (?f x)) = fst o fst" "(\<lambda>x. snd (?f x)) = fst o snd"
  1187       by auto
  1188 
  1189     show "map_pmf fst (map_pmf ?f pq) = A"
  1190       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
  1191     show "map_pmf snd (map_pmf ?f pq) = B"
  1192       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
  1193 
  1194     fix a b assume "(a, b) \<in> set_pmf (map_pmf ?f pq)"
  1195     then obtain c d where "((a, c), (b, d)) \<in> set_pmf pq"
  1196       by (auto simp: set_map_pmf)
  1197     from pq[OF this] show "R a b" ..
  1198   qed
  1199   show "rel_pmf S A' B'"
  1200   proof (rule rel_pmf.intros)
  1201     let ?f = "\<lambda>(a, b). (snd a, snd b)"
  1202     have [simp]: "(\<lambda>x. fst (?f x)) = snd o fst" "(\<lambda>x. snd (?f x)) = snd o snd"
  1203       by auto
  1204 
  1205     show "map_pmf fst (map_pmf ?f pq) = A'"
  1206       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
  1207     show "map_pmf snd (map_pmf ?f pq) = B'"
  1208       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
  1209 
  1210     fix c d assume "(c, d) \<in> set_pmf (map_pmf ?f pq)"
  1211     then obtain a b where "((a, c), (b, d)) \<in> set_pmf pq"
  1212       by (auto simp: set_map_pmf)
  1213     from pq[OF this] show "S c d" ..
  1214   qed
  1215 next
  1216   assume "rel_pmf R A B" "rel_pmf S A' B'"
  1217   then obtain Rpq Spq
  1218     where Rpq: "\<And>a b. (a, b) \<in> set_pmf Rpq \<Longrightarrow> R a b"
  1219         "map_pmf fst Rpq = A" "map_pmf snd Rpq = B"
  1220       and Spq: "\<And>a b. (a, b) \<in> set_pmf Spq \<Longrightarrow> S a b"
  1221         "map_pmf fst Spq = A'" "map_pmf snd Spq = B'"
  1222     by (force elim: rel_pmf.cases)
  1223 
  1224   let ?f = "(\<lambda>((a, c), (b, d)). ((a, b), (c, d)))"
  1225   let ?pq = "map_pmf ?f (pair_pmf Rpq Spq)"
  1226   have [simp]: "(\<lambda>x. fst (?f x)) = (\<lambda>(a, b). (fst a, fst b))" "(\<lambda>x. snd (?f x)) = (\<lambda>(a, b). (snd a, snd b))"
  1227     by auto
  1228 
  1229   show "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
  1230     by (rule rel_pmf.intros[where pq="?pq"])
  1231        (auto simp: map_snd_pair_pmf map_fst_pair_pmf set_pair_pmf set_map_pmf map_pmf_comp Rpq Spq
  1232                    map_pair)
  1233 qed
  1234 
  1235 end
  1236