src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Fri Feb 19 13:40:50 2016 +0100 (2016-02-19)
changeset 62378 85ed00c1fe7c
parent 62324 ae44f16dcea5
child 62390 842917225d56
permissions -rw-r--r--
generalize more theorems to support enat and ennreal
     1 (*  Title:      HOL/Probability/Probability_Mass_Function.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Andreas Lochbihler, ETH Zurich
     4 *)
     5 
     6 section \<open> Probability mass function \<close>
     7 
     8 theory Probability_Mass_Function
     9 imports
    10   Giry_Monad
    11   "~~/src/HOL/Library/Multiset"
    12 begin
    13 
    14 lemma AE_emeasure_singleton:
    15   assumes x: "emeasure M {x} \<noteq> 0" and ae: "AE x in M. P x" shows "P x"
    16 proof -
    17   from x have x_M: "{x} \<in> sets M"
    18     by (auto intro: emeasure_notin_sets)
    19   from ae obtain N where N: "{x\<in>space M. \<not> P x} \<subseteq> N" "emeasure M N = 0" "N \<in> sets M"
    20     by (auto elim: AE_E)
    21   { assume "\<not> P x"
    22     with x_M[THEN sets.sets_into_space] N have "emeasure M {x} \<le> emeasure M N"
    23       by (intro emeasure_mono) auto
    24     with x N have False
    25       by (auto simp: emeasure_le_0_iff) }
    26   then show "P x" by auto
    27 qed
    28 
    29 lemma AE_measure_singleton: "measure M {x} \<noteq> 0 \<Longrightarrow> AE x in M. P x \<Longrightarrow> P x"
    30   by (metis AE_emeasure_singleton measure_def emeasure_empty measure_empty)
    31 
    32 lemma ereal_divide': "b \<noteq> 0 \<Longrightarrow> ereal (a / b) = ereal a / ereal b"
    33   using ereal_divide[of a b] by simp
    34 
    35 lemma (in finite_measure) AE_support_countable:
    36   assumes [simp]: "sets M = UNIV"
    37   shows "(AE x in M. measure M {x} \<noteq> 0) \<longleftrightarrow> (\<exists>S. countable S \<and> (AE x in M. x \<in> S))"
    38 proof
    39   assume "\<exists>S. countable S \<and> (AE x in M. x \<in> S)"
    40   then obtain S where S[intro]: "countable S" and ae: "AE x in M. x \<in> S"
    41     by auto
    42   then have "emeasure M (\<Union>x\<in>{x\<in>S. emeasure M {x} \<noteq> 0}. {x}) =
    43     (\<integral>\<^sup>+ x. emeasure M {x} * indicator {x\<in>S. emeasure M {x} \<noteq> 0} x \<partial>count_space UNIV)"
    44     by (subst emeasure_UN_countable)
    45        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
    46   also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} * indicator S x \<partial>count_space UNIV)"
    47     by (auto intro!: nn_integral_cong split: split_indicator)
    48   also have "\<dots> = emeasure M (\<Union>x\<in>S. {x})"
    49     by (subst emeasure_UN_countable)
    50        (auto simp: disjoint_family_on_def nn_integral_restrict_space[symmetric] restrict_count_space)
    51   also have "\<dots> = emeasure M (space M)"
    52     using ae by (intro emeasure_eq_AE) auto
    53   finally have "emeasure M {x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0} = emeasure M (space M)"
    54     by (simp add: emeasure_single_in_space cong: rev_conj_cong)
    55   with finite_measure_compl[of "{x \<in> space M. x\<in>S \<and> emeasure M {x} \<noteq> 0}"]
    56   have "AE x in M. x \<in> S \<and> emeasure M {x} \<noteq> 0"
    57     by (intro AE_I[OF order_refl]) (auto simp: emeasure_eq_measure set_diff_eq cong: conj_cong)
    58   then show "AE x in M. measure M {x} \<noteq> 0"
    59     by (auto simp: emeasure_eq_measure)
    60 qed (auto intro!: exI[of _ "{x. measure M {x} \<noteq> 0}"] countable_support)
    61 
    62 subsection \<open> PMF as measure \<close>
    63 
    64 typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
    65   morphisms measure_pmf Abs_pmf
    66   by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
    67      (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
    68 
    69 declare [[coercion measure_pmf]]
    70 
    71 lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
    72   using pmf.measure_pmf[of p] by auto
    73 
    74 interpretation measure_pmf: prob_space "measure_pmf M" for M
    75   by (rule prob_space_measure_pmf)
    76 
    77 interpretation measure_pmf: subprob_space "measure_pmf M" for M
    78   by (rule prob_space_imp_subprob_space) unfold_locales
    79 
    80 lemma subprob_space_measure_pmf: "subprob_space (measure_pmf x)"
    81   by unfold_locales
    82 
    83 locale pmf_as_measure
    84 begin
    85 
    86 setup_lifting type_definition_pmf
    87 
    88 end
    89 
    90 context
    91 begin
    92 
    93 interpretation pmf_as_measure .
    94 
    95 lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
    96   by transfer blast
    97 
    98 lemma sets_measure_pmf_count_space[measurable_cong]:
    99   "sets (measure_pmf M) = sets (count_space UNIV)"
   100   by simp
   101 
   102 lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
   103   using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
   104 
   105 lemma measure_pmf_UNIV [simp]: "measure (measure_pmf p) UNIV = 1"
   106 using measure_pmf.prob_space[of p] by simp
   107 
   108 lemma measure_pmf_in_subprob_algebra[measurable (raw)]: "measure_pmf x \<in> space (subprob_algebra (count_space UNIV))"
   109   by (simp add: space_subprob_algebra subprob_space_measure_pmf)
   110 
   111 lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
   112   by (auto simp: measurable_def)
   113 
   114 lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
   115   by (intro measurable_cong_sets) simp_all
   116 
   117 lemma measurable_pair_restrict_pmf2:
   118   assumes "countable A"
   119   assumes [measurable]: "\<And>y. y \<in> A \<Longrightarrow> (\<lambda>x. f (x, y)) \<in> measurable M L"
   120   shows "f \<in> measurable (M \<Otimes>\<^sub>M restrict_space (measure_pmf N) A) L" (is "f \<in> measurable ?M _")
   121 proof -
   122   have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
   123     by (simp add: restrict_count_space)
   124 
   125   show ?thesis
   126     by (intro measurable_compose_countable'[where f="\<lambda>a b. f (fst b, a)" and g=snd and I=A,
   127                                             unfolded prod.collapse] assms)
   128         measurable
   129 qed
   130 
   131 lemma measurable_pair_restrict_pmf1:
   132   assumes "countable A"
   133   assumes [measurable]: "\<And>x. x \<in> A \<Longrightarrow> (\<lambda>y. f (x, y)) \<in> measurable N L"
   134   shows "f \<in> measurable (restrict_space (measure_pmf M) A \<Otimes>\<^sub>M N) L"
   135 proof -
   136   have [measurable_cong]: "sets (restrict_space (count_space UNIV) A) = sets (count_space A)"
   137     by (simp add: restrict_count_space)
   138 
   139   show ?thesis
   140     by (intro measurable_compose_countable'[where f="\<lambda>a b. f (a, snd b)" and g=fst and I=A,
   141                                             unfolded prod.collapse] assms)
   142         measurable
   143 qed
   144 
   145 lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
   146 
   147 lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
   148 declare [[coercion set_pmf]]
   149 
   150 lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
   151   by transfer simp
   152 
   153 lemma emeasure_pmf_single_eq_zero_iff:
   154   fixes M :: "'a pmf"
   155   shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
   156   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   157 
   158 lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
   159   using AE_measure_singleton[of M] AE_measure_pmf[of M]
   160   by (auto simp: set_pmf.rep_eq)
   161 
   162 lemma AE_pmfI: "(\<And>y. y \<in> set_pmf M \<Longrightarrow> P y) \<Longrightarrow> almost_everywhere (measure_pmf M) P"
   163 by(simp add: AE_measure_pmf_iff)
   164 
   165 lemma countable_set_pmf [simp]: "countable (set_pmf p)"
   166   by transfer (metis prob_space.finite_measure finite_measure.countable_support)
   167 
   168 lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
   169   by transfer (simp add: less_le measure_nonneg)
   170 
   171 lemma pmf_nonneg: "0 \<le> pmf p x"
   172   by transfer (simp add: measure_nonneg)
   173 
   174 lemma pmf_le_1: "pmf p x \<le> 1"
   175   by (simp add: pmf.rep_eq)
   176 
   177 lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
   178   using AE_measure_pmf[of M] by (intro notI) simp
   179 
   180 lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
   181   by transfer simp
   182 
   183 lemma set_pmf_eq: "set_pmf M = {x. pmf M x \<noteq> 0}"
   184   by (auto simp: set_pmf_iff)
   185 
   186 lemma emeasure_pmf_single:
   187   fixes M :: "'a pmf"
   188   shows "emeasure M {x} = pmf M x"
   189   by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
   190 
   191 lemma measure_pmf_single: "measure (measure_pmf M) {x} = pmf M x"
   192 using emeasure_pmf_single[of M x] by(simp add: measure_pmf.emeasure_eq_measure)
   193 
   194 lemma emeasure_measure_pmf_finite: "finite S \<Longrightarrow> emeasure (measure_pmf M) S = (\<Sum>s\<in>S. pmf M s)"
   195   by (subst emeasure_eq_setsum_singleton) (auto simp: emeasure_pmf_single)
   196 
   197 lemma measure_measure_pmf_finite: "finite S \<Longrightarrow> measure (measure_pmf M) S = setsum (pmf M) S"
   198   using emeasure_measure_pmf_finite[of S M] by(simp add: measure_pmf.emeasure_eq_measure)
   199 
   200 lemma nn_integral_measure_pmf_support:
   201   fixes f :: "'a \<Rightarrow> ereal"
   202   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"
   203   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>A. f x * pmf M x)"
   204 proof -
   205   have "(\<integral>\<^sup>+x. f x \<partial>M) = (\<integral>\<^sup>+x. f x * indicator A x \<partial>M)"
   206     using nn by (intro nn_integral_cong_AE) (auto simp: AE_measure_pmf_iff split: split_indicator)
   207   also have "\<dots> = (\<Sum>x\<in>A. f x * emeasure M {x})"
   208     using assms by (intro nn_integral_indicator_finite) auto
   209   finally show ?thesis
   210     by (simp add: emeasure_measure_pmf_finite)
   211 qed
   212 
   213 lemma nn_integral_measure_pmf_finite:
   214   fixes f :: "'a \<Rightarrow> ereal"
   215   assumes f: "finite (set_pmf M)" and nn: "\<And>x. x \<in> set_pmf M \<Longrightarrow> 0 \<le> f x"
   216   shows "(\<integral>\<^sup>+x. f x \<partial>measure_pmf M) = (\<Sum>x\<in>set_pmf M. f x * pmf M x)"
   217   using assms by (intro nn_integral_measure_pmf_support) auto
   218 lemma integrable_measure_pmf_finite:
   219   fixes f :: "'a \<Rightarrow> 'b::{banach, second_countable_topology}"
   220   shows "finite (set_pmf M) \<Longrightarrow> integrable M f"
   221   by (auto intro!: integrableI_bounded simp: nn_integral_measure_pmf_finite)
   222 
   223 lemma integral_measure_pmf:
   224   assumes [simp]: "finite A" and "\<And>a. a \<in> set_pmf M \<Longrightarrow> f a \<noteq> 0 \<Longrightarrow> a \<in> A"
   225   shows "(\<integral>x. f x \<partial>measure_pmf M) = (\<Sum>a\<in>A. f a * pmf M a)"
   226 proof -
   227   have "(\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x * indicator A x \<partial>measure_pmf M)"
   228     using assms(2) by (intro integral_cong_AE) (auto split: split_indicator simp: AE_measure_pmf_iff)
   229   also have "\<dots> = (\<Sum>a\<in>A. f a * pmf M a)"
   230     by (subst integral_indicator_finite_real) (auto simp: measure_def emeasure_measure_pmf_finite)
   231   finally show ?thesis .
   232 qed
   233 
   234 lemma integrable_pmf: "integrable (count_space X) (pmf M)"
   235 proof -
   236   have " (\<integral>\<^sup>+ x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+ x. pmf M x \<partial>count_space (M \<inter> X))"
   237     by (auto simp add: nn_integral_count_space_indicator set_pmf_iff intro!: nn_integral_cong split: split_indicator)
   238   then have "integrable (count_space X) (pmf M) = integrable (count_space (M \<inter> X)) (pmf M)"
   239     by (simp add: integrable_iff_bounded pmf_nonneg)
   240   then show ?thesis
   241     by (simp add: pmf.rep_eq measure_pmf.integrable_measure disjoint_family_on_def)
   242 qed
   243 
   244 lemma integral_pmf: "(\<integral>x. pmf M x \<partial>count_space X) = measure M X"
   245 proof -
   246   have "(\<integral>x. pmf M x \<partial>count_space X) = (\<integral>\<^sup>+x. pmf M x \<partial>count_space X)"
   247     by (simp add: pmf_nonneg integrable_pmf nn_integral_eq_integral)
   248   also have "\<dots> = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space (X \<inter> M))"
   249     by (auto intro!: nn_integral_cong_AE split: split_indicator
   250              simp: pmf.rep_eq measure_pmf.emeasure_eq_measure nn_integral_count_space_indicator
   251                    AE_count_space set_pmf_iff)
   252   also have "\<dots> = emeasure M (X \<inter> M)"
   253     by (rule emeasure_countable_singleton[symmetric]) (auto intro: countable_set_pmf)
   254   also have "\<dots> = emeasure M X"
   255     by (auto intro!: emeasure_eq_AE simp: AE_measure_pmf_iff)
   256   finally show ?thesis
   257     by (simp add: measure_pmf.emeasure_eq_measure)
   258 qed
   259 
   260 lemma integral_pmf_restrict:
   261   "(f::'a \<Rightarrow> 'b::{banach, second_countable_topology}) \<in> borel_measurable (count_space UNIV) \<Longrightarrow>
   262     (\<integral>x. f x \<partial>measure_pmf M) = (\<integral>x. f x \<partial>restrict_space M M)"
   263   by (auto intro!: integral_cong_AE simp add: integral_restrict_space AE_measure_pmf_iff)
   264 
   265 lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
   266 proof -
   267   have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
   268     by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
   269   then show ?thesis
   270     using measure_pmf.emeasure_space_1 by simp
   271 qed
   272 
   273 lemma emeasure_pmf_UNIV [simp]: "emeasure (measure_pmf M) UNIV = 1"
   274 using measure_pmf.emeasure_space_1[of M] by simp
   275 
   276 lemma in_null_sets_measure_pmfI:
   277   "A \<inter> set_pmf p = {} \<Longrightarrow> A \<in> null_sets (measure_pmf p)"
   278 using emeasure_eq_0_AE[where ?P="\<lambda>x. x \<in> A" and M="measure_pmf p"]
   279 by(auto simp add: null_sets_def AE_measure_pmf_iff)
   280 
   281 lemma measure_subprob: "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
   282   by (simp add: space_subprob_algebra subprob_space_measure_pmf)
   283 
   284 subsection \<open> Monad Interpretation \<close>
   285 
   286 lemma measurable_measure_pmf[measurable]:
   287   "(\<lambda>x. measure_pmf (M x)) \<in> measurable (count_space UNIV) (subprob_algebra (count_space UNIV))"
   288   by (auto simp: space_subprob_algebra intro!: prob_space_imp_subprob_space) unfold_locales
   289 
   290 lemma bind_measure_pmf_cong:
   291   assumes "\<And>x. A x \<in> space (subprob_algebra N)" "\<And>x. B x \<in> space (subprob_algebra N)"
   292   assumes "\<And>i. i \<in> set_pmf x \<Longrightarrow> A i = B i"
   293   shows "bind (measure_pmf x) A = bind (measure_pmf x) B"
   294 proof (rule measure_eqI)
   295   show "sets (measure_pmf x \<bind> A) = sets (measure_pmf x \<bind> B)"
   296     using assms by (subst (1 2) sets_bind) (auto simp: space_subprob_algebra)
   297 next
   298   fix X assume "X \<in> sets (measure_pmf x \<bind> A)"
   299   then have X: "X \<in> sets N"
   300     using assms by (subst (asm) sets_bind) (auto simp: space_subprob_algebra)
   301   show "emeasure (measure_pmf x \<bind> A) X = emeasure (measure_pmf x \<bind> B) X"
   302     using assms
   303     by (subst (1 2) emeasure_bind[where N=N, OF _ _ X])
   304        (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
   305 qed
   306 
   307 lift_definition bind_pmf :: "'a pmf \<Rightarrow> ('a \<Rightarrow> 'b pmf ) \<Rightarrow> 'b pmf" is bind
   308 proof (clarify, intro conjI)
   309   fix f :: "'a measure" and g :: "'a \<Rightarrow> 'b measure"
   310   assume "prob_space f"
   311   then interpret f: prob_space f .
   312   assume "sets f = UNIV" and ae_f: "AE x in f. measure f {x} \<noteq> 0"
   313   then have s_f[simp]: "sets f = sets (count_space UNIV)"
   314     by simp
   315   assume g: "\<And>x. prob_space (g x) \<and> sets (g x) = UNIV \<and> (AE y in g x. measure (g x) {y} \<noteq> 0)"
   316   then have g: "\<And>x. prob_space (g x)" and s_g[simp]: "\<And>x. sets (g x) = sets (count_space UNIV)"
   317     and ae_g: "\<And>x. AE y in g x. measure (g x) {y} \<noteq> 0"
   318     by auto
   319 
   320   have [measurable]: "g \<in> measurable f (subprob_algebra (count_space UNIV))"
   321     by (auto simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space g)
   322 
   323   show "prob_space (f \<bind> g)"
   324     using g by (intro f.prob_space_bind[where S="count_space UNIV"]) auto
   325   then interpret fg: prob_space "f \<bind> g" .
   326   show [simp]: "sets (f \<bind> g) = UNIV"
   327     using sets_eq_imp_space_eq[OF s_f]
   328     by (subst sets_bind[where N="count_space UNIV"]) auto
   329   show "AE x in f \<bind> g. measure (f \<bind> g) {x} \<noteq> 0"
   330     apply (simp add: fg.prob_eq_0 AE_bind[where B="count_space UNIV"])
   331     using ae_f
   332     apply eventually_elim
   333     using ae_g
   334     apply eventually_elim
   335     apply (auto dest: AE_measure_singleton)
   336     done
   337 qed
   338 
   339 lemma ereal_pmf_bind: "pmf (bind_pmf N f) i = (\<integral>\<^sup>+x. pmf (f x) i \<partial>measure_pmf N)"
   340   unfolding pmf.rep_eq bind_pmf.rep_eq
   341   by (auto simp: measure_pmf.measure_bind[where N="count_space UNIV"] measure_subprob measure_nonneg
   342            intro!: nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
   343 
   344 lemma pmf_bind: "pmf (bind_pmf N f) i = (\<integral>x. pmf (f x) i \<partial>measure_pmf N)"
   345   using ereal_pmf_bind[of N f i]
   346   by (subst (asm) nn_integral_eq_integral)
   347      (auto simp: pmf_nonneg pmf_le_1
   348            intro!: nn_integral_eq_integral[symmetric] measure_pmf.integrable_const_bound[where B=1])
   349 
   350 lemma bind_pmf_const[simp]: "bind_pmf M (\<lambda>x. c) = c"
   351   by transfer (simp add: bind_const' prob_space_imp_subprob_space)
   352 
   353 lemma set_bind_pmf[simp]: "set_pmf (bind_pmf M N) = (\<Union>M\<in>set_pmf M. set_pmf (N M))"
   354   unfolding set_pmf_eq ereal_eq_0(1)[symmetric] ereal_pmf_bind
   355   by (auto simp add: nn_integral_0_iff_AE AE_measure_pmf_iff set_pmf_eq not_le less_le pmf_nonneg)
   356 
   357 lemma bind_pmf_cong:
   358   assumes "p = q"
   359   shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> bind_pmf p f = bind_pmf q g"
   360   unfolding \<open>p = q\<close>[symmetric] measure_pmf_inject[symmetric] bind_pmf.rep_eq
   361   by (auto simp: AE_measure_pmf_iff Pi_iff space_subprob_algebra subprob_space_measure_pmf
   362                  sets_bind[where N="count_space UNIV"] emeasure_bind[where N="count_space UNIV"]
   363            intro!: nn_integral_cong_AE measure_eqI)
   364 
   365 lemma bind_pmf_cong_simp:
   366   "p = q \<Longrightarrow> (\<And>x. x \<in> set_pmf q =simp=> f x = g x) \<Longrightarrow> bind_pmf p f = bind_pmf q g"
   367   by (simp add: simp_implies_def cong: bind_pmf_cong)
   368 
   369 lemma measure_pmf_bind: "measure_pmf (bind_pmf M f) = (measure_pmf M \<bind> (\<lambda>x. measure_pmf (f x)))"
   370   by transfer simp
   371 
   372 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)"
   373   using measurable_measure_pmf[of N]
   374   unfolding measure_pmf_bind
   375   apply (subst (1 3) nn_integral_max_0[symmetric])
   376   apply (intro nn_integral_bind[where B="count_space UNIV"])
   377   apply auto
   378   done
   379 
   380 lemma emeasure_bind_pmf[simp]: "emeasure (bind_pmf M N) X = (\<integral>\<^sup>+x. emeasure (N x) X \<partial>M)"
   381   using measurable_measure_pmf[of N]
   382   unfolding measure_pmf_bind
   383   by (subst emeasure_bind[where N="count_space UNIV"]) auto
   384 
   385 lift_definition return_pmf :: "'a \<Rightarrow> 'a pmf" is "return (count_space UNIV)"
   386   by (auto intro!: prob_space_return simp: AE_return measure_return)
   387 
   388 lemma bind_return_pmf: "bind_pmf (return_pmf x) f = f x"
   389   by transfer
   390      (auto intro!: prob_space_imp_subprob_space bind_return[where N="count_space UNIV"]
   391            simp: space_subprob_algebra)
   392 
   393 lemma set_return_pmf[simp]: "set_pmf (return_pmf x) = {x}"
   394   by transfer (auto simp add: measure_return split: split_indicator)
   395 
   396 lemma bind_return_pmf': "bind_pmf N return_pmf = N"
   397 proof (transfer, clarify)
   398   fix N :: "'a measure" assume "sets N = UNIV" then show "N \<bind> return (count_space UNIV) = N"
   399     by (subst return_sets_cong[where N=N]) (simp_all add: bind_return')
   400 qed
   401 
   402 lemma bind_assoc_pmf: "bind_pmf (bind_pmf A B) C = bind_pmf A (\<lambda>x. bind_pmf (B x) C)"
   403   by transfer
   404      (auto intro!: bind_assoc[where N="count_space UNIV" and R="count_space UNIV"]
   405            simp: measurable_def space_subprob_algebra prob_space_imp_subprob_space)
   406 
   407 definition "map_pmf f M = bind_pmf M (\<lambda>x. return_pmf (f x))"
   408 
   409 lemma map_bind_pmf: "map_pmf f (bind_pmf M g) = bind_pmf M (\<lambda>x. map_pmf f (g x))"
   410   by (simp add: map_pmf_def bind_assoc_pmf)
   411 
   412 lemma bind_map_pmf: "bind_pmf (map_pmf f M) g = bind_pmf M (\<lambda>x. g (f x))"
   413   by (simp add: map_pmf_def bind_assoc_pmf bind_return_pmf)
   414 
   415 lemma map_pmf_transfer[transfer_rule]:
   416   "rel_fun op = (rel_fun cr_pmf cr_pmf) (\<lambda>f M. distr M (count_space UNIV) f) map_pmf"
   417 proof -
   418   have "rel_fun op = (rel_fun pmf_as_measure.cr_pmf pmf_as_measure.cr_pmf)
   419      (\<lambda>f M. M \<bind> (return (count_space UNIV) o f)) map_pmf"
   420     unfolding map_pmf_def[abs_def] comp_def by transfer_prover
   421   then show ?thesis
   422     by (force simp: rel_fun_def cr_pmf_def bind_return_distr)
   423 qed
   424 
   425 lemma map_pmf_rep_eq:
   426   "measure_pmf (map_pmf f M) = distr (measure_pmf M) (count_space UNIV) f"
   427   unfolding map_pmf_def bind_pmf.rep_eq comp_def return_pmf.rep_eq
   428   using bind_return_distr[of M f "count_space UNIV"] by (simp add: comp_def)
   429 
   430 lemma map_pmf_id[simp]: "map_pmf id = id"
   431   by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
   432 
   433 lemma map_pmf_ident[simp]: "map_pmf (\<lambda>x. x) = (\<lambda>x. x)"
   434   using map_pmf_id unfolding id_def .
   435 
   436 lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
   437   by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def)
   438 
   439 lemma map_pmf_comp: "map_pmf f (map_pmf g M) = map_pmf (\<lambda>x. f (g x)) M"
   440   using map_pmf_compose[of f g] by (simp add: comp_def)
   441 
   442 lemma map_pmf_cong: "p = q \<Longrightarrow> (\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
   443   unfolding map_pmf_def by (rule bind_pmf_cong) auto
   444 
   445 lemma pmf_set_map: "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
   446   by (auto simp add: comp_def fun_eq_iff map_pmf_def)
   447 
   448 lemma set_map_pmf[simp]: "set_pmf (map_pmf f M) = f`set_pmf M"
   449   using pmf_set_map[of f] by (auto simp: comp_def fun_eq_iff)
   450 
   451 lemma emeasure_map_pmf[simp]: "emeasure (map_pmf f M) X = emeasure M (f -` X)"
   452   unfolding map_pmf_rep_eq by (subst emeasure_distr) auto
   453 
   454 lemma measure_map_pmf[simp]: "measure (map_pmf f M) X = measure M (f -` X)"
   455 using emeasure_map_pmf[of f M X] by(simp add: measure_pmf.emeasure_eq_measure)
   456 
   457 lemma nn_integral_map_pmf[simp]: "(\<integral>\<^sup>+x. f x \<partial>map_pmf g M) = (\<integral>\<^sup>+x. f (g x) \<partial>M)"
   458   unfolding map_pmf_rep_eq by (intro nn_integral_distr) auto
   459 
   460 lemma ereal_pmf_map: "pmf (map_pmf f p) x = (\<integral>\<^sup>+ y. indicator (f -` {x}) y \<partial>measure_pmf p)"
   461 proof (transfer fixing: f x)
   462   fix p :: "'b measure"
   463   presume "prob_space p"
   464   then interpret prob_space p .
   465   presume "sets p = UNIV"
   466   then show "ereal (measure (distr p (count_space UNIV) f) {x}) = integral\<^sup>N p (indicator (f -` {x}))"
   467     by(simp add: measure_distr measurable_def emeasure_eq_measure)
   468 qed simp_all
   469 
   470 lemma nn_integral_pmf: "(\<integral>\<^sup>+ x. pmf p x \<partial>count_space A) = emeasure (measure_pmf p) A"
   471 proof -
   472   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))"
   473     by(auto simp add: nn_integral_count_space_indicator indicator_def set_pmf_iff intro: nn_integral_cong)
   474   also have "\<dots> = emeasure (measure_pmf p) (\<Union>x\<in>A \<inter> set_pmf p. {x})"
   475     by(subst emeasure_UN_countable)(auto simp add: emeasure_pmf_single disjoint_family_on_def)
   476   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})"
   477     by(rule emeasure_Un_null_set[symmetric])(auto intro: in_null_sets_measure_pmfI)
   478   also have "\<dots> = emeasure (measure_pmf p) A"
   479     by(auto intro: arg_cong2[where f=emeasure])
   480   finally show ?thesis .
   481 qed
   482 
   483 lemma map_return_pmf [simp]: "map_pmf f (return_pmf x) = return_pmf (f x)"
   484   by transfer (simp add: distr_return)
   485 
   486 lemma map_pmf_const[simp]: "map_pmf (\<lambda>_. c) M = return_pmf c"
   487   by transfer (auto simp: prob_space.distr_const)
   488 
   489 lemma pmf_return [simp]: "pmf (return_pmf x) y = indicator {y} x"
   490   by transfer (simp add: measure_return)
   491 
   492 lemma nn_integral_return_pmf[simp]: "0 \<le> f x \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>return_pmf x) = f x"
   493   unfolding return_pmf.rep_eq by (intro nn_integral_return) auto
   494 
   495 lemma emeasure_return_pmf[simp]: "emeasure (return_pmf x) X = indicator X x"
   496   unfolding return_pmf.rep_eq by (intro emeasure_return) auto
   497 
   498 lemma return_pmf_inj[simp]: "return_pmf x = return_pmf y \<longleftrightarrow> x = y"
   499   by (metis insertI1 set_return_pmf singletonD)
   500 
   501 lemma map_pmf_eq_return_pmf_iff:
   502   "map_pmf f p = return_pmf x \<longleftrightarrow> (\<forall>y \<in> set_pmf p. f y = x)"
   503 proof
   504   assume "map_pmf f p = return_pmf x"
   505   then have "set_pmf (map_pmf f p) = set_pmf (return_pmf x)" by simp
   506   then show "\<forall>y \<in> set_pmf p. f y = x" by auto
   507 next
   508   assume "\<forall>y \<in> set_pmf p. f y = x"
   509   then show "map_pmf f p = return_pmf x"
   510     unfolding map_pmf_const[symmetric, of _ p] by (intro map_pmf_cong) auto
   511 qed
   512 
   513 definition "pair_pmf A B = bind_pmf A (\<lambda>x. bind_pmf B (\<lambda>y. return_pmf (x, y)))"
   514 
   515 lemma pmf_pair: "pmf (pair_pmf M N) (a, b) = pmf M a * pmf N b"
   516   unfolding pair_pmf_def pmf_bind pmf_return
   517   apply (subst integral_measure_pmf[where A="{b}"])
   518   apply (auto simp: indicator_eq_0_iff)
   519   apply (subst integral_measure_pmf[where A="{a}"])
   520   apply (auto simp: indicator_eq_0_iff setsum_nonneg_eq_0_iff pmf_nonneg)
   521   done
   522 
   523 lemma set_pair_pmf[simp]: "set_pmf (pair_pmf A B) = set_pmf A \<times> set_pmf B"
   524   unfolding pair_pmf_def set_bind_pmf set_return_pmf by auto
   525 
   526 lemma measure_pmf_in_subprob_space[measurable (raw)]:
   527   "measure_pmf M \<in> space (subprob_algebra (count_space UNIV))"
   528   by (simp add: space_subprob_algebra) intro_locales
   529 
   530 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)"
   531 proof -
   532   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)"
   533     by (subst nn_integral_max_0[symmetric])
   534        (auto simp: AE_measure_pmf_iff intro!: nn_integral_cong_AE)
   535   also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) * indicator (A \<times> B) (a, b) \<partial>B \<partial>A)"
   536     by (simp add: pair_pmf_def)
   537   also have "\<dots> = (\<integral>\<^sup>+a. \<integral>\<^sup>+b. max 0 (f (a, b)) \<partial>B \<partial>A)"
   538     by (auto intro!: nn_integral_cong_AE simp: AE_measure_pmf_iff)
   539   finally show ?thesis
   540     unfolding nn_integral_max_0 .
   541 qed
   542 
   543 lemma bind_pair_pmf:
   544   assumes M[measurable]: "M \<in> measurable (count_space UNIV \<Otimes>\<^sub>M count_space UNIV) (subprob_algebra N)"
   545   shows "measure_pmf (pair_pmf A B) \<bind> M = (measure_pmf A \<bind> (\<lambda>x. measure_pmf B \<bind> (\<lambda>y. M (x, y))))"
   546     (is "?L = ?R")
   547 proof (rule measure_eqI)
   548   have M'[measurable]: "M \<in> measurable (pair_pmf A B) (subprob_algebra N)"
   549     using M[THEN measurable_space] by (simp_all add: space_pair_measure)
   550 
   551   note measurable_bind[where N="count_space UNIV", measurable]
   552   note measure_pmf_in_subprob_space[simp]
   553 
   554   have sets_eq_N: "sets ?L = N"
   555     by (subst sets_bind[OF sets_kernel[OF M']]) auto
   556   show "sets ?L = sets ?R"
   557     using measurable_space[OF M]
   558     by (simp add: sets_eq_N space_pair_measure space_subprob_algebra)
   559   fix X assume "X \<in> sets ?L"
   560   then have X[measurable]: "X \<in> sets N"
   561     unfolding sets_eq_N .
   562   then show "emeasure ?L X = emeasure ?R X"
   563     apply (simp add: emeasure_bind[OF _ M' X])
   564     apply (simp add: nn_integral_bind[where B="count_space UNIV"] pair_pmf_def measure_pmf_bind[of A]
   565                      nn_integral_measure_pmf_finite emeasure_nonneg one_ereal_def[symmetric])
   566     apply (subst emeasure_bind[OF _ _ X])
   567     apply measurable
   568     apply (subst emeasure_bind[OF _ _ X])
   569     apply measurable
   570     done
   571 qed
   572 
   573 lemma map_fst_pair_pmf: "map_pmf fst (pair_pmf A B) = A"
   574   by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
   575 
   576 lemma map_snd_pair_pmf: "map_pmf snd (pair_pmf A B) = B"
   577   by (simp add: pair_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
   578 
   579 lemma nn_integral_pmf':
   580   "inj_on f A \<Longrightarrow> (\<integral>\<^sup>+x. pmf p (f x) \<partial>count_space A) = emeasure p (f ` A)"
   581   by (subst nn_integral_bij_count_space[where g=f and B="f`A"])
   582      (auto simp: bij_betw_def nn_integral_pmf)
   583 
   584 lemma pmf_le_0_iff[simp]: "pmf M p \<le> 0 \<longleftrightarrow> pmf M p = 0"
   585   using pmf_nonneg[of M p] by simp
   586 
   587 lemma min_pmf_0[simp]: "min (pmf M p) 0 = 0" "min 0 (pmf M p) = 0"
   588   using pmf_nonneg[of M p] by simp_all
   589 
   590 lemma pmf_eq_0_set_pmf: "pmf M p = 0 \<longleftrightarrow> p \<notin> set_pmf M"
   591   unfolding set_pmf_iff by simp
   592 
   593 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"
   594   by (auto simp: pmf.rep_eq map_pmf_rep_eq measure_distr AE_measure_pmf_iff inj_onD
   595            intro!: measure_pmf.finite_measure_eq_AE)
   596 
   597 lemma pmf_map_inj': "inj f \<Longrightarrow> pmf (map_pmf f M) (f x) = pmf M x"
   598 apply(cases "x \<in> set_pmf M")
   599  apply(simp add: pmf_map_inj[OF subset_inj_on])
   600 apply(simp add: pmf_eq_0_set_pmf[symmetric])
   601 apply(auto simp add: pmf_eq_0_set_pmf dest: injD)
   602 done
   603 
   604 lemma pmf_map_outside: "x \<notin> f ` set_pmf M \<Longrightarrow> pmf (map_pmf f M) x = 0"
   605 unfolding pmf_eq_0_set_pmf by simp
   606 
   607 subsection \<open> PMFs as function \<close>
   608 
   609 context
   610   fixes f :: "'a \<Rightarrow> real"
   611   assumes nonneg: "\<And>x. 0 \<le> f x"
   612   assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   613 begin
   614 
   615 lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
   616 proof (intro conjI)
   617   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   618     by (simp split: split_indicator)
   619   show "AE x in density (count_space UNIV) (ereal \<circ> f).
   620     measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
   621     by (simp add: AE_density nonneg measure_def emeasure_density max_def)
   622   show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
   623     by standard (simp add: emeasure_density prob)
   624 qed simp
   625 
   626 lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
   627 proof transfer
   628   have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
   629     by (simp split: split_indicator)
   630   fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
   631     by transfer (simp add: measure_def emeasure_density nonneg max_def)
   632 qed
   633 
   634 lemma set_embed_pmf: "set_pmf embed_pmf = {x. f x \<noteq> 0}"
   635 by(auto simp add: set_pmf_eq assms pmf_embed_pmf)
   636 
   637 end
   638 
   639 lemma embed_pmf_transfer:
   640   "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"
   641   by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
   642 
   643 lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
   644 proof (transfer, elim conjE)
   645   fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
   646   assume "prob_space M" then interpret prob_space M .
   647   show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
   648   proof (rule measure_eqI)
   649     fix A :: "'a set"
   650     have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) =
   651       (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
   652       by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
   653     also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
   654       by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
   655     also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
   656       by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
   657          (auto simp: disjoint_family_on_def)
   658     also have "\<dots> = emeasure M A"
   659       using ae by (intro emeasure_eq_AE) auto
   660     finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
   661       using emeasure_space_1 by (simp add: emeasure_density)
   662   qed simp
   663 qed
   664 
   665 lemma td_pmf_embed_pmf:
   666   "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}"
   667   unfolding type_definition_def
   668 proof safe
   669   fix p :: "'a pmf"
   670   have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
   671     using measure_pmf.emeasure_space_1[of p] by simp
   672   then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
   673     by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
   674 
   675   show "embed_pmf (pmf p) = p"
   676     by (intro measure_pmf_inject[THEN iffD1])
   677        (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
   678 next
   679   fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
   680   then show "pmf (embed_pmf f) = f"
   681     by (auto intro!: pmf_embed_pmf)
   682 qed (rule pmf_nonneg)
   683 
   684 end
   685 
   686 lemma nn_integral_measure_pmf: "(\<integral>\<^sup>+ x. f x \<partial>measure_pmf p) = \<integral>\<^sup>+ x. ereal (pmf p x) * f x \<partial>count_space UNIV"
   687 by(simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg)
   688 
   689 locale pmf_as_function
   690 begin
   691 
   692 setup_lifting td_pmf_embed_pmf
   693 
   694 lemma set_pmf_transfer[transfer_rule]:
   695   assumes "bi_total A"
   696   shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"
   697   using \<open>bi_total A\<close>
   698   by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
   699      metis+
   700 
   701 end
   702 
   703 context
   704 begin
   705 
   706 interpretation pmf_as_function .
   707 
   708 lemma pmf_eqI: "(\<And>i. pmf M i = pmf N i) \<Longrightarrow> M = N"
   709   by transfer auto
   710 
   711 lemma pmf_eq_iff: "M = N \<longleftrightarrow> (\<forall>i. pmf M i = pmf N i)"
   712   by (auto intro: pmf_eqI)
   713 
   714 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))"
   715   unfolding pmf_eq_iff pmf_bind
   716 proof
   717   fix i
   718   interpret B: prob_space "restrict_space B B"
   719     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
   720        (auto simp: AE_measure_pmf_iff)
   721   interpret A: prob_space "restrict_space A A"
   722     by (intro prob_space_restrict_space measure_pmf.emeasure_eq_1_AE)
   723        (auto simp: AE_measure_pmf_iff)
   724 
   725   interpret AB: pair_prob_space "restrict_space A A" "restrict_space B B"
   726     by unfold_locales
   727 
   728   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)"
   729     by (rule integral_cong) (auto intro!: integral_pmf_restrict)
   730   also have "\<dots> = (\<integral> x. (\<integral> y. pmf (C x y) i \<partial>restrict_space B B) \<partial>restrict_space A A)"
   731     by (intro integral_pmf_restrict B.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
   732               countable_set_pmf borel_measurable_count_space)
   733   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>restrict_space B B)"
   734     by (rule AB.Fubini_integral[symmetric])
   735        (auto intro!: AB.integrable_const_bound[where B=1] measurable_pair_restrict_pmf2
   736              simp: pmf_nonneg pmf_le_1 measurable_restrict_space1)
   737   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>restrict_space A A \<partial>B)"
   738     by (intro integral_pmf_restrict[symmetric] A.borel_measurable_lebesgue_integral measurable_pair_restrict_pmf2
   739               countable_set_pmf borel_measurable_count_space)
   740   also have "\<dots> = (\<integral> y. \<integral> x. pmf (C x y) i \<partial>A \<partial>B)"
   741     by (rule integral_cong) (auto intro!: integral_pmf_restrict[symmetric])
   742   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)" .
   743 qed
   744 
   745 lemma pair_map_pmf1: "pair_pmf (map_pmf f A) B = map_pmf (apfst f) (pair_pmf A B)"
   746 proof (safe intro!: pmf_eqI)
   747   fix a :: "'a" and b :: "'b"
   748   have [simp]: "\<And>c d. indicator (apfst f -` {(a, b)}) (c, d) = indicator (f -` {a}) c * (indicator {b} d::ereal)"
   749     by (auto split: split_indicator)
   750 
   751   have "ereal (pmf (pair_pmf (map_pmf f A) B) (a, b)) =
   752          ereal (pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b))"
   753     unfolding pmf_pair ereal_pmf_map
   754     by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_multc pmf_nonneg
   755                   emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
   756   then show "pmf (pair_pmf (map_pmf f A) B) (a, b) = pmf (map_pmf (apfst f) (pair_pmf A B)) (a, b)"
   757     by simp
   758 qed
   759 
   760 lemma pair_map_pmf2: "pair_pmf A (map_pmf f B) = map_pmf (apsnd f) (pair_pmf A B)"
   761 proof (safe intro!: pmf_eqI)
   762   fix a :: "'a" and b :: "'b"
   763   have [simp]: "\<And>c d. indicator (apsnd f -` {(a, b)}) (c, d) = indicator {a} c * (indicator (f -` {b}) d::ereal)"
   764     by (auto split: split_indicator)
   765 
   766   have "ereal (pmf (pair_pmf A (map_pmf f B)) (a, b)) =
   767          ereal (pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b))"
   768     unfolding pmf_pair ereal_pmf_map
   769     by (simp add: nn_integral_pair_pmf' max_def emeasure_pmf_single nn_integral_cmult nn_integral_multc pmf_nonneg
   770                   emeasure_map_pmf[symmetric] del: emeasure_map_pmf)
   771   then show "pmf (pair_pmf A (map_pmf f B)) (a, b) = pmf (map_pmf (apsnd f) (pair_pmf A B)) (a, b)"
   772     by simp
   773 qed
   774 
   775 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)"
   776   by (simp add: pair_map_pmf2 pair_map_pmf1 map_pmf_comp split_beta')
   777 
   778 end
   779 
   780 lemma pair_return_pmf1: "pair_pmf (return_pmf x) y = map_pmf (Pair x) y"
   781 by(simp add: pair_pmf_def bind_return_pmf map_pmf_def)
   782 
   783 lemma pair_return_pmf2: "pair_pmf x (return_pmf y) = map_pmf (\<lambda>x. (x, y)) x"
   784 by(simp add: pair_pmf_def bind_return_pmf map_pmf_def)
   785 
   786 lemma pair_pair_pmf: "pair_pmf (pair_pmf u v) w = map_pmf (\<lambda>(x, (y, z)). ((x, y), z)) (pair_pmf u (pair_pmf v w))"
   787 by(simp add: pair_pmf_def bind_return_pmf map_pmf_def bind_assoc_pmf)
   788 
   789 lemma pair_commute_pmf: "pair_pmf x y = map_pmf (\<lambda>(x, y). (y, x)) (pair_pmf y x)"
   790 unfolding pair_pmf_def by(subst bind_commute_pmf)(simp add: map_pmf_def bind_assoc_pmf bind_return_pmf)
   791 
   792 lemma set_pmf_subset_singleton: "set_pmf p \<subseteq> {x} \<longleftrightarrow> p = return_pmf x"
   793 proof(intro iffI pmf_eqI)
   794   fix i
   795   assume x: "set_pmf p \<subseteq> {x}"
   796   hence *: "set_pmf p = {x}" using set_pmf_not_empty[of p] by auto
   797   have "ereal (pmf p x) = \<integral>\<^sup>+ i. indicator {x} i \<partial>p" by(simp add: emeasure_pmf_single)
   798   also have "\<dots> = \<integral>\<^sup>+ i. 1 \<partial>p" by(rule nn_integral_cong_AE)(simp add: AE_measure_pmf_iff * )
   799   also have "\<dots> = 1" by simp
   800   finally show "pmf p i = pmf (return_pmf x) i" using x
   801     by(auto split: split_indicator simp add: pmf_eq_0_set_pmf)
   802 qed auto
   803 
   804 lemma bind_eq_return_pmf:
   805   "bind_pmf p f = return_pmf x \<longleftrightarrow> (\<forall>y\<in>set_pmf p. f y = return_pmf x)"
   806   (is "?lhs \<longleftrightarrow> ?rhs")
   807 proof(intro iffI strip)
   808   fix y
   809   assume y: "y \<in> set_pmf p"
   810   assume "?lhs"
   811   hence "set_pmf (bind_pmf p f) = {x}" by simp
   812   hence "(\<Union>y\<in>set_pmf p. set_pmf (f y)) = {x}" by simp
   813   hence "set_pmf (f y) \<subseteq> {x}" using y by auto
   814   thus "f y = return_pmf x" by(simp add: set_pmf_subset_singleton)
   815 next
   816   assume *: ?rhs
   817   show ?lhs
   818   proof(rule pmf_eqI)
   819     fix i
   820     have "ereal (pmf (bind_pmf p f) i) = \<integral>\<^sup>+ y. ereal (pmf (f y) i) \<partial>p" by(simp add: ereal_pmf_bind)
   821     also have "\<dots> = \<integral>\<^sup>+ y. ereal (pmf (return_pmf x) i) \<partial>p"
   822       by(rule nn_integral_cong_AE)(simp add: AE_measure_pmf_iff * )
   823     also have "\<dots> = ereal (pmf (return_pmf x) i)" by simp
   824     finally show "pmf (bind_pmf p f) i = pmf (return_pmf x) i" by simp
   825   qed
   826 qed
   827 
   828 lemma pmf_False_conv_True: "pmf p False = 1 - pmf p True"
   829 proof -
   830   have "pmf p False + pmf p True = measure p {False} + measure p {True}"
   831     by(simp add: measure_pmf_single)
   832   also have "\<dots> = measure p ({False} \<union> {True})"
   833     by(subst measure_pmf.finite_measure_Union) simp_all
   834   also have "{False} \<union> {True} = space p" by auto
   835   finally show ?thesis by simp
   836 qed
   837 
   838 lemma pmf_True_conv_False: "pmf p True = 1 - pmf p False"
   839 by(simp add: pmf_False_conv_True)
   840 
   841 subsection \<open> Conditional Probabilities \<close>
   842 
   843 lemma measure_pmf_zero_iff: "measure (measure_pmf p) s = 0 \<longleftrightarrow> set_pmf p \<inter> s = {}"
   844   by (subst measure_pmf.prob_eq_0) (auto simp: AE_measure_pmf_iff)
   845 
   846 context
   847   fixes p :: "'a pmf" and s :: "'a set"
   848   assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
   849 begin
   850 
   851 interpretation pmf_as_measure .
   852 
   853 lemma emeasure_measure_pmf_not_zero: "emeasure (measure_pmf p) s \<noteq> 0"
   854 proof
   855   assume "emeasure (measure_pmf p) s = 0"
   856   then have "AE x in measure_pmf p. x \<notin> s"
   857     by (rule AE_I[rotated]) auto
   858   with not_empty show False
   859     by (auto simp: AE_measure_pmf_iff)
   860 qed
   861 
   862 lemma measure_measure_pmf_not_zero: "measure (measure_pmf p) s \<noteq> 0"
   863   using emeasure_measure_pmf_not_zero unfolding measure_pmf.emeasure_eq_measure by simp
   864 
   865 lift_definition cond_pmf :: "'a pmf" is
   866   "uniform_measure (measure_pmf p) s"
   867 proof (intro conjI)
   868   show "prob_space (uniform_measure (measure_pmf p) s)"
   869     by (intro prob_space_uniform_measure) (auto simp: emeasure_measure_pmf_not_zero)
   870   show "AE x in uniform_measure (measure_pmf p) s. measure (uniform_measure (measure_pmf p) s) {x} \<noteq> 0"
   871     by (simp add: emeasure_measure_pmf_not_zero measure_measure_pmf_not_zero AE_uniform_measure
   872                   AE_measure_pmf_iff set_pmf.rep_eq)
   873 qed simp
   874 
   875 lemma pmf_cond: "pmf cond_pmf x = (if x \<in> s then pmf p x / measure p s else 0)"
   876   by transfer (simp add: emeasure_measure_pmf_not_zero pmf.rep_eq)
   877 
   878 lemma set_cond_pmf[simp]: "set_pmf cond_pmf = set_pmf p \<inter> s"
   879   by (auto simp add: set_pmf_iff pmf_cond measure_measure_pmf_not_zero split: split_if_asm)
   880 
   881 end
   882 
   883 lemma cond_map_pmf:
   884   assumes "set_pmf p \<inter> f -` s \<noteq> {}"
   885   shows "cond_pmf (map_pmf f p) s = map_pmf f (cond_pmf p (f -` s))"
   886 proof -
   887   have *: "set_pmf (map_pmf f p) \<inter> s \<noteq> {}"
   888     using assms by auto
   889   { fix x
   890     have "ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x) =
   891       emeasure p (f -` s \<inter> f -` {x}) / emeasure p (f -` s)"
   892       unfolding ereal_pmf_map cond_pmf.rep_eq[OF assms] by (simp add: nn_integral_uniform_measure)
   893     also have "f -` s \<inter> f -` {x} = (if x \<in> s then f -` {x} else {})"
   894       by auto
   895     also have "emeasure p (if x \<in> s then f -` {x} else {}) / emeasure p (f -` s) =
   896       ereal (pmf (cond_pmf (map_pmf f p) s) x)"
   897       using measure_measure_pmf_not_zero[OF *]
   898       by (simp add: pmf_cond[OF *] ereal_divide' ereal_pmf_map measure_pmf.emeasure_eq_measure[symmetric]
   899                del: ereal_divide)
   900     finally have "ereal (pmf (cond_pmf (map_pmf f p) s) x) = ereal (pmf (map_pmf f (cond_pmf p (f -` s))) x)"
   901       by simp }
   902   then show ?thesis
   903     by (intro pmf_eqI) simp
   904 qed
   905 
   906 lemma bind_cond_pmf_cancel:
   907   assumes [simp]: "\<And>x. x \<in> set_pmf p \<Longrightarrow> set_pmf q \<inter> {y. R x y} \<noteq> {}"
   908   assumes [simp]: "\<And>y. y \<in> set_pmf q \<Longrightarrow> set_pmf p \<inter> {x. R x y} \<noteq> {}"
   909   assumes [simp]: "\<And>x y. x \<in> set_pmf p \<Longrightarrow> y \<in> set_pmf q \<Longrightarrow> R x y \<Longrightarrow> measure q {y. R x y} = measure p {x. R x y}"
   910   shows "bind_pmf p (\<lambda>x. cond_pmf q {y. R x y}) = q"
   911 proof (rule pmf_eqI)
   912   fix i
   913   have "ereal (pmf (bind_pmf p (\<lambda>x. cond_pmf q {y. R x y})) i) =
   914     (\<integral>\<^sup>+x. ereal (pmf q i / measure p {x. R x i}) * ereal (indicator {x. R x i} x) \<partial>p)"
   915     by (auto simp add: ereal_pmf_bind AE_measure_pmf_iff pmf_cond pmf_eq_0_set_pmf intro!: nn_integral_cong_AE)
   916   also have "\<dots> = (pmf q i * measure p {x. R x i}) / measure p {x. R x i}"
   917     by (simp add: pmf_nonneg measure_nonneg zero_ereal_def[symmetric] ereal_indicator
   918                   nn_integral_cmult measure_pmf.emeasure_eq_measure)
   919   also have "\<dots> = pmf q i"
   920     by (cases "pmf q i = 0") (simp_all add: pmf_eq_0_set_pmf measure_measure_pmf_not_zero)
   921   finally show "pmf (bind_pmf p (\<lambda>x. cond_pmf q {y. R x y})) i = pmf q i"
   922     by simp
   923 qed
   924 
   925 subsection \<open> Relator \<close>
   926 
   927 inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
   928 for R p q
   929 where
   930   "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y;
   931      map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
   932   \<Longrightarrow> rel_pmf R p q"
   933 
   934 lemma rel_pmfI:
   935   assumes R: "rel_set R (set_pmf p) (set_pmf q)"
   936   assumes eq: "\<And>x y. x \<in> set_pmf p \<Longrightarrow> y \<in> set_pmf q \<Longrightarrow> R x y \<Longrightarrow>
   937     measure p {x. R x y} = measure q {y. R x y}"
   938   shows "rel_pmf R p q"
   939 proof
   940   let ?pq = "bind_pmf p (\<lambda>x. bind_pmf (cond_pmf q {y. R x y}) (\<lambda>y. return_pmf (x, y)))"
   941   have "\<And>x. x \<in> set_pmf p \<Longrightarrow> set_pmf q \<inter> {y. R x y} \<noteq> {}"
   942     using R by (auto simp: rel_set_def)
   943   then show "\<And>x y. (x, y) \<in> set_pmf ?pq \<Longrightarrow> R x y"
   944     by auto
   945   show "map_pmf fst ?pq = p"
   946     by (simp add: map_bind_pmf bind_return_pmf')
   947 
   948   show "map_pmf snd ?pq = q"
   949     using R eq
   950     apply (simp add: bind_cond_pmf_cancel map_bind_pmf bind_return_pmf')
   951     apply (rule bind_cond_pmf_cancel)
   952     apply (auto simp: rel_set_def)
   953     done
   954 qed
   955 
   956 lemma rel_pmf_imp_rel_set: "rel_pmf R p q \<Longrightarrow> rel_set R (set_pmf p) (set_pmf q)"
   957   by (force simp add: rel_pmf.simps rel_set_def)
   958 
   959 lemma rel_pmfD_measure:
   960   assumes rel_R: "rel_pmf R p q" and R: "\<And>a b. R a b \<Longrightarrow> R a y \<longleftrightarrow> R x b"
   961   assumes "x \<in> set_pmf p" "y \<in> set_pmf q"
   962   shows "measure p {x. R x y} = measure q {y. R x y}"
   963 proof -
   964   from rel_R obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
   965     and eq: "p = map_pmf fst pq" "q = map_pmf snd pq"
   966     by (auto elim: rel_pmf.cases)
   967   have "measure p {x. R x y} = measure pq {x. R (fst x) y}"
   968     by (simp add: eq map_pmf_rep_eq measure_distr)
   969   also have "\<dots> = measure pq {y. R x (snd y)}"
   970     by (intro measure_pmf.finite_measure_eq_AE)
   971        (auto simp: AE_measure_pmf_iff R dest!: pq)
   972   also have "\<dots> = measure q {y. R x y}"
   973     by (simp add: eq map_pmf_rep_eq measure_distr)
   974   finally show "measure p {x. R x y} = measure q {y. R x y}" .
   975 qed
   976 
   977 lemma rel_pmf_measureD:
   978   assumes "rel_pmf R p q"
   979   shows "measure (measure_pmf p) A \<le> measure (measure_pmf q) {y. \<exists>x\<in>A. R x y}" (is "?lhs \<le> ?rhs")
   980 using assms
   981 proof cases
   982   fix pq
   983   assume R: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
   984     and p[symmetric]: "map_pmf fst pq = p"
   985     and q[symmetric]: "map_pmf snd pq = q"
   986   have "?lhs = measure (measure_pmf pq) (fst -` A)" by(simp add: p)
   987   also have "\<dots> \<le> measure (measure_pmf pq) {y. \<exists>x\<in>A. R x (snd y)}"
   988     by(rule measure_pmf.finite_measure_mono_AE)(auto 4 3 simp add: AE_measure_pmf_iff dest: R)
   989   also have "\<dots> = ?rhs" by(simp add: q)
   990   finally show ?thesis .
   991 qed
   992 
   993 lemma rel_pmf_iff_measure:
   994   assumes "symp R" "transp R"
   995   shows "rel_pmf R p q \<longleftrightarrow>
   996     rel_set R (set_pmf p) (set_pmf q) \<and>
   997     (\<forall>x\<in>set_pmf p. \<forall>y\<in>set_pmf q. R x y \<longrightarrow> measure p {x. R x y} = measure q {y. R x y})"
   998   by (safe intro!: rel_pmf_imp_rel_set rel_pmfI)
   999      (auto intro!: rel_pmfD_measure dest: sympD[OF \<open>symp R\<close>] transpD[OF \<open>transp R\<close>])
  1000 
  1001 lemma quotient_rel_set_disjoint:
  1002   "equivp R \<Longrightarrow> C \<in> UNIV // {(x, y). R x y} \<Longrightarrow> rel_set R A B \<Longrightarrow> A \<inter> C = {} \<longleftrightarrow> B \<inter> C = {}"
  1003   using in_quotient_imp_closed[of UNIV "{(x, y). R x y}" C]
  1004   by (auto 0 0 simp: equivp_equiv rel_set_def set_eq_iff elim: equivpE)
  1005      (blast dest: equivp_symp)+
  1006 
  1007 lemma quotientD: "equiv X R \<Longrightarrow> A \<in> X // R \<Longrightarrow> x \<in> A \<Longrightarrow> A = R `` {x}"
  1008   by (metis Image_singleton_iff equiv_class_eq_iff quotientE)
  1009 
  1010 lemma rel_pmf_iff_equivp:
  1011   assumes "equivp R"
  1012   shows "rel_pmf R p q \<longleftrightarrow> (\<forall>C\<in>UNIV // {(x, y). R x y}. measure p C = measure q C)"
  1013     (is "_ \<longleftrightarrow>   (\<forall>C\<in>_//?R. _)")
  1014 proof (subst rel_pmf_iff_measure, safe)
  1015   show "symp R" "transp R"
  1016     using assms by (auto simp: equivp_reflp_symp_transp)
  1017 next
  1018   fix C assume C: "C \<in> UNIV // ?R" and R: "rel_set R (set_pmf p) (set_pmf q)"
  1019   assume eq: "\<forall>x\<in>set_pmf p. \<forall>y\<in>set_pmf q. R x y \<longrightarrow> measure p {x. R x y} = measure q {y. R x y}"
  1020 
  1021   show "measure p C = measure q C"
  1022   proof cases
  1023     assume "p \<inter> C = {}"
  1024     moreover then have "q \<inter> C = {}"
  1025       using quotient_rel_set_disjoint[OF assms C R] by simp
  1026     ultimately show ?thesis
  1027       unfolding measure_pmf_zero_iff[symmetric] by simp
  1028   next
  1029     assume "p \<inter> C \<noteq> {}"
  1030     moreover then have "q \<inter> C \<noteq> {}"
  1031       using quotient_rel_set_disjoint[OF assms C R] by simp
  1032     ultimately obtain x y where in_set: "x \<in> set_pmf p" "y \<in> set_pmf q" and in_C: "x \<in> C" "y \<in> C"
  1033       by auto
  1034     then have "R x y"
  1035       using in_quotient_imp_in_rel[of UNIV ?R C x y] C assms
  1036       by (simp add: equivp_equiv)
  1037     with in_set eq have "measure p {x. R x y} = measure q {y. R x y}"
  1038       by auto
  1039     moreover have "{y. R x y} = C"
  1040       using assms \<open>x \<in> C\<close> C quotientD[of UNIV ?R C x] by (simp add: equivp_equiv)
  1041     moreover have "{x. R x y} = C"
  1042       using assms \<open>y \<in> C\<close> C quotientD[of UNIV "?R" C y] sympD[of R]
  1043       by (auto simp add: equivp_equiv elim: equivpE)
  1044     ultimately show ?thesis
  1045       by auto
  1046   qed
  1047 next
  1048   assume eq: "\<forall>C\<in>UNIV // ?R. measure p C = measure q C"
  1049   show "rel_set R (set_pmf p) (set_pmf q)"
  1050     unfolding rel_set_def
  1051   proof safe
  1052     fix x assume x: "x \<in> set_pmf p"
  1053     have "{y. R x y} \<in> UNIV // ?R"
  1054       by (auto simp: quotient_def)
  1055     with eq have *: "measure q {y. R x y} = measure p {y. R x y}"
  1056       by auto
  1057     have "measure q {y. R x y} \<noteq> 0"
  1058       using x assms unfolding * by (auto simp: measure_pmf_zero_iff set_eq_iff dest: equivp_reflp)
  1059     then show "\<exists>y\<in>set_pmf q. R x y"
  1060       unfolding measure_pmf_zero_iff by auto
  1061   next
  1062     fix y assume y: "y \<in> set_pmf q"
  1063     have "{x. R x y} \<in> UNIV // ?R"
  1064       using assms by (auto simp: quotient_def dest: equivp_symp)
  1065     with eq have *: "measure p {x. R x y} = measure q {x. R x y}"
  1066       by auto
  1067     have "measure p {x. R x y} \<noteq> 0"
  1068       using y assms unfolding * by (auto simp: measure_pmf_zero_iff set_eq_iff dest: equivp_reflp)
  1069     then show "\<exists>x\<in>set_pmf p. R x y"
  1070       unfolding measure_pmf_zero_iff by auto
  1071   qed
  1072 
  1073   fix x y assume "x \<in> set_pmf p" "y \<in> set_pmf q" "R x y"
  1074   have "{y. R x y} \<in> UNIV // ?R" "{x. R x y} = {y. R x y}"
  1075     using assms \<open>R x y\<close> by (auto simp: quotient_def dest: equivp_symp equivp_transp)
  1076   with eq show "measure p {x. R x y} = measure q {y. R x y}"
  1077     by auto
  1078 qed
  1079 
  1080 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
  1081 proof -
  1082   show "map_pmf id = id" by (rule map_pmf_id)
  1083   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose)
  1084   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"
  1085     by (intro map_pmf_cong refl)
  1086 
  1087   show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
  1088     by (rule pmf_set_map)
  1089 
  1090   show "(card_of (set_pmf p), natLeq) \<in> ordLeq" for p :: "'s pmf"
  1091   proof -
  1092     have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
  1093       by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
  1094          (auto intro: countable_set_pmf)
  1095     also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
  1096       by (metis Field_natLeq card_of_least natLeq_Well_order)
  1097     finally show ?thesis .
  1098   qed
  1099 
  1100   show "\<And>R. rel_pmf R = (\<lambda>x y. \<exists>z. set_pmf z \<subseteq> {(x, y). R x y} \<and>
  1101     map_pmf fst z = x \<and> map_pmf snd z = y)"
  1102      by (auto simp add: fun_eq_iff rel_pmf.simps)
  1103 
  1104   show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
  1105     for R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
  1106   proof -
  1107     { fix p q r
  1108       assume pq: "rel_pmf R p q"
  1109         and qr:"rel_pmf S q r"
  1110       from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
  1111         and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
  1112       from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
  1113         and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
  1114 
  1115       def pr \<equiv> "bind_pmf pq (\<lambda>xy. bind_pmf (cond_pmf qr {yz. fst yz = snd xy}) (\<lambda>yz. return_pmf (fst xy, snd yz)))"
  1116       have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {yz. fst yz = y} \<noteq> {}"
  1117         by (force simp: q')
  1118 
  1119       have "rel_pmf (R OO S) p r"
  1120       proof (rule rel_pmf.intros)
  1121         fix x z assume "(x, z) \<in> pr"
  1122         then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
  1123           by (auto simp: q pr_welldefined pr_def split_beta)
  1124         with pq qr show "(R OO S) x z"
  1125           by blast
  1126       next
  1127         have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {yz. fst yz = y}))"
  1128           by (simp add: pr_def q split_beta bind_map_pmf map_pmf_def[symmetric] map_bind_pmf map_pmf_comp)
  1129         then show "map_pmf snd pr = r"
  1130           unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) (auto simp: eq_commute)
  1131       qed (simp add: pr_def map_bind_pmf split_beta map_pmf_def[symmetric] p map_pmf_comp)
  1132     }
  1133     then show ?thesis
  1134       by(auto simp add: le_fun_def)
  1135   qed
  1136 qed (fact natLeq_card_order natLeq_cinfinite)+
  1137 
  1138 lemma map_pmf_idI: "(\<And>x. x \<in> set_pmf p \<Longrightarrow> f x = x) \<Longrightarrow> map_pmf f p = p"
  1139 by(simp cong: pmf.map_cong)
  1140 
  1141 lemma rel_pmf_conj[simp]:
  1142   "rel_pmf (\<lambda>x y. P \<and> Q x y) x y \<longleftrightarrow> P \<and> rel_pmf Q x y"
  1143   "rel_pmf (\<lambda>x y. Q x y \<and> P) x y \<longleftrightarrow> P \<and> rel_pmf Q x y"
  1144   using set_pmf_not_empty by (fastforce simp: pmf.in_rel subset_eq)+
  1145 
  1146 lemma rel_pmf_top[simp]: "rel_pmf top = top"
  1147   by (auto simp: pmf.in_rel[abs_def] fun_eq_iff map_fst_pair_pmf map_snd_pair_pmf
  1148            intro: exI[of _ "pair_pmf x y" for x y])
  1149 
  1150 lemma rel_pmf_return_pmf1: "rel_pmf R (return_pmf x) M \<longleftrightarrow> (\<forall>a\<in>M. R x a)"
  1151 proof safe
  1152   fix a assume "a \<in> M" "rel_pmf R (return_pmf x) M"
  1153   then obtain pq where *: "\<And>a b. (a, b) \<in> set_pmf pq \<Longrightarrow> R a b"
  1154     and eq: "return_pmf x = map_pmf fst pq" "M = map_pmf snd pq"
  1155     by (force elim: rel_pmf.cases)
  1156   moreover have "set_pmf (return_pmf x) = {x}"
  1157     by simp
  1158   with \<open>a \<in> M\<close> have "(x, a) \<in> pq"
  1159     by (force simp: eq)
  1160   with * show "R x a"
  1161     by auto
  1162 qed (auto intro!: rel_pmf.intros[where pq="pair_pmf (return_pmf x) M"]
  1163           simp: map_fst_pair_pmf map_snd_pair_pmf)
  1164 
  1165 lemma rel_pmf_return_pmf2: "rel_pmf R M (return_pmf x) \<longleftrightarrow> (\<forall>a\<in>M. R a x)"
  1166   by (subst pmf.rel_flip[symmetric]) (simp add: rel_pmf_return_pmf1)
  1167 
  1168 lemma rel_return_pmf[simp]: "rel_pmf R (return_pmf x1) (return_pmf x2) = R x1 x2"
  1169   unfolding rel_pmf_return_pmf2 set_return_pmf by simp
  1170 
  1171 lemma rel_pmf_False[simp]: "rel_pmf (\<lambda>x y. False) x y = False"
  1172   unfolding pmf.in_rel fun_eq_iff using set_pmf_not_empty by fastforce
  1173 
  1174 lemma rel_pmf_rel_prod:
  1175   "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'"
  1176 proof safe
  1177   assume "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
  1178   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"
  1179     and eq: "map_pmf fst pq = pair_pmf A A'" "map_pmf snd pq = pair_pmf B B'"
  1180     by (force elim: rel_pmf.cases)
  1181   show "rel_pmf R A B"
  1182   proof (rule rel_pmf.intros)
  1183     let ?f = "\<lambda>(a, b). (fst a, fst b)"
  1184     have [simp]: "(\<lambda>x. fst (?f x)) = fst o fst" "(\<lambda>x. snd (?f x)) = fst o snd"
  1185       by auto
  1186 
  1187     show "map_pmf fst (map_pmf ?f pq) = A"
  1188       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
  1189     show "map_pmf snd (map_pmf ?f pq) = B"
  1190       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_fst_pair_pmf)
  1191 
  1192     fix a b assume "(a, b) \<in> set_pmf (map_pmf ?f pq)"
  1193     then obtain c d where "((a, c), (b, d)) \<in> set_pmf pq"
  1194       by auto
  1195     from pq[OF this] show "R a b" ..
  1196   qed
  1197   show "rel_pmf S A' B'"
  1198   proof (rule rel_pmf.intros)
  1199     let ?f = "\<lambda>(a, b). (snd a, snd b)"
  1200     have [simp]: "(\<lambda>x. fst (?f x)) = snd o fst" "(\<lambda>x. snd (?f x)) = snd o snd"
  1201       by auto
  1202 
  1203     show "map_pmf fst (map_pmf ?f pq) = A'"
  1204       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
  1205     show "map_pmf snd (map_pmf ?f pq) = B'"
  1206       by (simp add: map_pmf_comp pmf.map_comp[symmetric] eq map_snd_pair_pmf)
  1207 
  1208     fix c d assume "(c, d) \<in> set_pmf (map_pmf ?f pq)"
  1209     then obtain a b where "((a, c), (b, d)) \<in> set_pmf pq"
  1210       by auto
  1211     from pq[OF this] show "S c d" ..
  1212   qed
  1213 next
  1214   assume "rel_pmf R A B" "rel_pmf S A' B'"
  1215   then obtain Rpq Spq
  1216     where Rpq: "\<And>a b. (a, b) \<in> set_pmf Rpq \<Longrightarrow> R a b"
  1217         "map_pmf fst Rpq = A" "map_pmf snd Rpq = B"
  1218       and Spq: "\<And>a b. (a, b) \<in> set_pmf Spq \<Longrightarrow> S a b"
  1219         "map_pmf fst Spq = A'" "map_pmf snd Spq = B'"
  1220     by (force elim: rel_pmf.cases)
  1221 
  1222   let ?f = "(\<lambda>((a, c), (b, d)). ((a, b), (c, d)))"
  1223   let ?pq = "map_pmf ?f (pair_pmf Rpq Spq)"
  1224   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))"
  1225     by auto
  1226 
  1227   show "rel_pmf (rel_prod R S) (pair_pmf A A') (pair_pmf B B')"
  1228     by (rule rel_pmf.intros[where pq="?pq"])
  1229        (auto simp: map_snd_pair_pmf map_fst_pair_pmf map_pmf_comp Rpq Spq
  1230                    map_pair)
  1231 qed
  1232 
  1233 lemma rel_pmf_reflI:
  1234   assumes "\<And>x. x \<in> set_pmf p \<Longrightarrow> P x x"
  1235   shows "rel_pmf P p p"
  1236   by (rule rel_pmf.intros[where pq="map_pmf (\<lambda>x. (x, x)) p"])
  1237      (auto simp add: pmf.map_comp o_def assms)
  1238 
  1239 lemma rel_pmf_bij_betw:
  1240   assumes f: "bij_betw f (set_pmf p) (set_pmf q)"
  1241   and eq: "\<And>x. x \<in> set_pmf p \<Longrightarrow> pmf p x = pmf q (f x)"
  1242   shows "rel_pmf (\<lambda>x y. f x = y) p q"
  1243 proof(rule rel_pmf.intros)
  1244   let ?pq = "map_pmf (\<lambda>x. (x, f x)) p"
  1245   show "map_pmf fst ?pq = p" by(simp add: pmf.map_comp o_def)
  1246 
  1247   have "map_pmf f p = q"
  1248   proof(rule pmf_eqI)
  1249     fix i
  1250     show "pmf (map_pmf f p) i = pmf q i"
  1251     proof(cases "i \<in> set_pmf q")
  1252       case True
  1253       with f obtain j where "i = f j" "j \<in> set_pmf p"
  1254         by(auto simp add: bij_betw_def image_iff)
  1255       thus ?thesis using f by(simp add: bij_betw_def pmf_map_inj eq)
  1256     next
  1257       case False thus ?thesis
  1258         by(subst pmf_map_outside)(auto simp add: set_pmf_iff eq[symmetric])
  1259     qed
  1260   qed
  1261   then show "map_pmf snd ?pq = q" by(simp add: pmf.map_comp o_def)
  1262 qed auto
  1263 
  1264 context
  1265 begin
  1266 
  1267 interpretation pmf_as_measure .
  1268 
  1269 definition "join_pmf M = bind_pmf M (\<lambda>x. x)"
  1270 
  1271 lemma bind_eq_join_pmf: "bind_pmf M f = join_pmf (map_pmf f M)"
  1272   unfolding join_pmf_def bind_map_pmf ..
  1273 
  1274 lemma join_eq_bind_pmf: "join_pmf M = bind_pmf M id"
  1275   by (simp add: join_pmf_def id_def)
  1276 
  1277 lemma pmf_join: "pmf (join_pmf N) i = (\<integral>M. pmf M i \<partial>measure_pmf N)"
  1278   unfolding join_pmf_def pmf_bind ..
  1279 
  1280 lemma ereal_pmf_join: "ereal (pmf (join_pmf N) i) = (\<integral>\<^sup>+M. pmf M i \<partial>measure_pmf N)"
  1281   unfolding join_pmf_def ereal_pmf_bind ..
  1282 
  1283 lemma set_pmf_join_pmf[simp]: "set_pmf (join_pmf f) = (\<Union>p\<in>set_pmf f. set_pmf p)"
  1284   by (simp add: join_pmf_def)
  1285 
  1286 lemma join_return_pmf: "join_pmf (return_pmf M) = M"
  1287   by (simp add: integral_return pmf_eq_iff pmf_join return_pmf.rep_eq)
  1288 
  1289 lemma map_join_pmf: "map_pmf f (join_pmf AA) = join_pmf (map_pmf (map_pmf f) AA)"
  1290   by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf)
  1291 
  1292 lemma join_map_return_pmf: "join_pmf (map_pmf return_pmf A) = A"
  1293   by (simp add: join_pmf_def map_pmf_def bind_assoc_pmf bind_return_pmf bind_return_pmf')
  1294 
  1295 end
  1296 
  1297 lemma rel_pmf_joinI:
  1298   assumes "rel_pmf (rel_pmf P) p q"
  1299   shows "rel_pmf P (join_pmf p) (join_pmf q)"
  1300 proof -
  1301   from assms obtain pq where p: "p = map_pmf fst pq"
  1302     and q: "q = map_pmf snd pq"
  1303     and P: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> rel_pmf P x y"
  1304     by cases auto
  1305   from P obtain PQ
  1306     where PQ: "\<And>x y a b. \<lbrakk> (x, y) \<in> set_pmf pq; (a, b) \<in> set_pmf (PQ x y) \<rbrakk> \<Longrightarrow> P a b"
  1307     and x: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf fst (PQ x y) = x"
  1308     and y: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> map_pmf snd (PQ x y) = y"
  1309     by(metis rel_pmf.simps)
  1310 
  1311   let ?r = "bind_pmf pq (\<lambda>(x, y). PQ x y)"
  1312   have "\<And>a b. (a, b) \<in> set_pmf ?r \<Longrightarrow> P a b" by (auto intro: PQ)
  1313   moreover have "map_pmf fst ?r = join_pmf p" "map_pmf snd ?r = join_pmf q"
  1314     by (simp_all add: p q x y join_pmf_def map_bind_pmf bind_map_pmf split_def cong: bind_pmf_cong)
  1315   ultimately show ?thesis ..
  1316 qed
  1317 
  1318 lemma rel_pmf_bindI:
  1319   assumes pq: "rel_pmf R p q"
  1320   and fg: "\<And>x y. R x y \<Longrightarrow> rel_pmf P (f x) (g y)"
  1321   shows "rel_pmf P (bind_pmf p f) (bind_pmf q g)"
  1322   unfolding bind_eq_join_pmf
  1323   by (rule rel_pmf_joinI)
  1324      (auto simp add: pmf.rel_map intro: pmf.rel_mono[THEN le_funD, THEN le_funD, THEN le_boolD, THEN mp, OF _ pq] fg)
  1325 
  1326 text \<open>
  1327   Proof that @{const rel_pmf} preserves orders.
  1328   Antisymmetry proof follows Thm. 1 in N. Saheb-Djahromi, Cpo's of measures for nondeterminism,
  1329   Theoretical Computer Science 12(1):19--37, 1980,
  1330   @{url "http://dx.doi.org/10.1016/0304-3975(80)90003-1"}
  1331 \<close>
  1332 
  1333 lemma
  1334   assumes *: "rel_pmf R p q"
  1335   and refl: "reflp R" and trans: "transp R"
  1336   shows measure_Ici: "measure p {y. R x y} \<le> measure q {y. R x y}" (is ?thesis1)
  1337   and measure_Ioi: "measure p {y. R x y \<and> \<not> R y x} \<le> measure q {y. R x y \<and> \<not> R y x}" (is ?thesis2)
  1338 proof -
  1339   from * obtain pq
  1340     where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
  1341     and p: "p = map_pmf fst pq"
  1342     and q: "q = map_pmf snd pq"
  1343     by cases auto
  1344   show ?thesis1 ?thesis2 unfolding p q map_pmf_rep_eq using refl trans
  1345     by(auto 4 3 simp add: measure_distr reflpD AE_measure_pmf_iff intro!: measure_pmf.finite_measure_mono_AE dest!: pq elim: transpE)
  1346 qed
  1347 
  1348 lemma rel_pmf_inf:
  1349   fixes p q :: "'a pmf"
  1350   assumes 1: "rel_pmf R p q"
  1351   assumes 2: "rel_pmf R q p"
  1352   and refl: "reflp R" and trans: "transp R"
  1353   shows "rel_pmf (inf R R\<inverse>\<inverse>) p q"
  1354 proof (subst rel_pmf_iff_equivp, safe)
  1355   show "equivp (inf R R\<inverse>\<inverse>)"
  1356     using trans refl by (auto simp: equivp_reflp_symp_transp intro: sympI transpI reflpI dest: transpD reflpD)
  1357 
  1358   fix C assume "C \<in> UNIV // {(x, y). inf R R\<inverse>\<inverse> x y}"
  1359   then obtain x where C: "C = {y. R x y \<and> R y x}"
  1360     by (auto elim: quotientE)
  1361 
  1362   let ?R = "\<lambda>x y. R x y \<and> R y x"
  1363   let ?\<mu>R = "\<lambda>y. measure q {x. ?R x y}"
  1364   have "measure p {y. ?R x y} = measure p ({y. R x y} - {y. R x y \<and> \<not> R y x})"
  1365     by(auto intro!: arg_cong[where f="measure p"])
  1366   also have "\<dots> = measure p {y. R x y} - measure p {y. R x y \<and> \<not> R y x}"
  1367     by (rule measure_pmf.finite_measure_Diff) auto
  1368   also have "measure p {y. R x y \<and> \<not> R y x} = measure q {y. R x y \<and> \<not> R y x}"
  1369     using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ioi)
  1370   also have "measure p {y. R x y} = measure q {y. R x y}"
  1371     using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ici)
  1372   also have "measure q {y. R x y} - measure q {y. R x y \<and> \<not> R y x} =
  1373     measure q ({y. R x y} - {y. R x y \<and> \<not> R y x})"
  1374     by(rule measure_pmf.finite_measure_Diff[symmetric]) auto
  1375   also have "\<dots> = ?\<mu>R x"
  1376     by(auto intro!: arg_cong[where f="measure q"])
  1377   finally show "measure p C = measure q C"
  1378     by (simp add: C conj_commute)
  1379 qed
  1380 
  1381 lemma rel_pmf_antisym:
  1382   fixes p q :: "'a pmf"
  1383   assumes 1: "rel_pmf R p q"
  1384   assumes 2: "rel_pmf R q p"
  1385   and refl: "reflp R" and trans: "transp R" and antisym: "antisymP R"
  1386   shows "p = q"
  1387 proof -
  1388   from 1 2 refl trans have "rel_pmf (inf R R\<inverse>\<inverse>) p q" by(rule rel_pmf_inf)
  1389   also have "inf R R\<inverse>\<inverse> = op ="
  1390     using refl antisym by (auto intro!: ext simp add: reflpD dest: antisymD)
  1391   finally show ?thesis unfolding pmf.rel_eq .
  1392 qed
  1393 
  1394 lemma reflp_rel_pmf: "reflp R \<Longrightarrow> reflp (rel_pmf R)"
  1395 by(blast intro: reflpI rel_pmf_reflI reflpD)
  1396 
  1397 lemma antisymP_rel_pmf:
  1398   "\<lbrakk> reflp R; transp R; antisymP R \<rbrakk>
  1399   \<Longrightarrow> antisymP (rel_pmf R)"
  1400 by(rule antisymI)(blast intro: rel_pmf_antisym)
  1401 
  1402 lemma transp_rel_pmf:
  1403   assumes "transp R"
  1404   shows "transp (rel_pmf R)"
  1405 proof (rule transpI)
  1406   fix x y z
  1407   assume "rel_pmf R x y" and "rel_pmf R y z"
  1408   hence "rel_pmf (R OO R) x z" by (simp add: pmf.rel_compp relcompp.relcompI)
  1409   thus "rel_pmf R x z"
  1410     using assms by (metis (no_types) pmf.rel_mono rev_predicate2D transp_relcompp_less_eq)
  1411 qed
  1412 
  1413 subsection \<open> Distributions \<close>
  1414 
  1415 context
  1416 begin
  1417 
  1418 interpretation pmf_as_function .
  1419 
  1420 subsubsection \<open> Bernoulli Distribution \<close>
  1421 
  1422 lift_definition bernoulli_pmf :: "real \<Rightarrow> bool pmf" is
  1423   "\<lambda>p b. ((\<lambda>p. if b then p else 1 - p) \<circ> min 1 \<circ> max 0) p"
  1424   by (auto simp: nn_integral_count_space_finite[where A="{False, True}"] UNIV_bool
  1425            split: split_max split_min)
  1426 
  1427 lemma pmf_bernoulli_True[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) True = p"
  1428   by transfer simp
  1429 
  1430 lemma pmf_bernoulli_False[simp]: "0 \<le> p \<Longrightarrow> p \<le> 1 \<Longrightarrow> pmf (bernoulli_pmf p) False = 1 - p"
  1431   by transfer simp
  1432 
  1433 lemma set_pmf_bernoulli: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (bernoulli_pmf p) = UNIV"
  1434   by (auto simp add: set_pmf_iff UNIV_bool)
  1435 
  1436 lemma nn_integral_bernoulli_pmf[simp]:
  1437   assumes [simp]: "0 \<le> p" "p \<le> 1" "\<And>x. 0 \<le> f x"
  1438   shows "(\<integral>\<^sup>+x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
  1439   by (subst nn_integral_measure_pmf_support[of UNIV])
  1440      (auto simp: UNIV_bool field_simps)
  1441 
  1442 lemma integral_bernoulli_pmf[simp]:
  1443   assumes [simp]: "0 \<le> p" "p \<le> 1"
  1444   shows "(\<integral>x. f x \<partial>bernoulli_pmf p) = f True * p + f False * (1 - p)"
  1445   by (subst integral_measure_pmf[of UNIV]) (auto simp: UNIV_bool)
  1446 
  1447 lemma pmf_bernoulli_half [simp]: "pmf (bernoulli_pmf (1 / 2)) x = 1 / 2"
  1448 by(cases x) simp_all
  1449 
  1450 lemma measure_pmf_bernoulli_half: "measure_pmf (bernoulli_pmf (1 / 2)) = uniform_count_measure UNIV"
  1451 by(rule measure_eqI)(simp_all add: nn_integral_pmf[symmetric] emeasure_uniform_count_measure nn_integral_count_space_finite sets_uniform_count_measure)
  1452 
  1453 subsubsection \<open> Geometric Distribution \<close>
  1454 
  1455 context
  1456   fixes p :: real assumes p[arith]: "0 < p" "p \<le> 1"
  1457 begin
  1458 
  1459 lift_definition geometric_pmf :: "nat pmf" is "\<lambda>n. (1 - p)^n * p"
  1460 proof
  1461   have "(\<Sum>i. ereal (p * (1 - p) ^ i)) = ereal (p * (1 / (1 - (1 - p))))"
  1462     by (intro sums_suminf_ereal sums_mult geometric_sums) auto
  1463   then show "(\<integral>\<^sup>+ x. ereal ((1 - p)^x * p) \<partial>count_space UNIV) = 1"
  1464     by (simp add: nn_integral_count_space_nat field_simps)
  1465 qed simp
  1466 
  1467 lemma pmf_geometric[simp]: "pmf geometric_pmf n = (1 - p)^n * p"
  1468   by transfer rule
  1469 
  1470 end
  1471 
  1472 lemma set_pmf_geometric: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (geometric_pmf p) = UNIV"
  1473   by (auto simp: set_pmf_iff)
  1474 
  1475 subsubsection \<open> Uniform Multiset Distribution \<close>
  1476 
  1477 context
  1478   fixes M :: "'a multiset" assumes M_not_empty: "M \<noteq> {#}"
  1479 begin
  1480 
  1481 lift_definition pmf_of_multiset :: "'a pmf" is "\<lambda>x. count M x / size M"
  1482 proof
  1483   show "(\<integral>\<^sup>+ x. ereal (real (count M x) / real (size M)) \<partial>count_space UNIV) = 1"
  1484     using M_not_empty
  1485     by (simp add: zero_less_divide_iff nn_integral_count_space nonempty_has_size
  1486                   setsum_divide_distrib[symmetric])
  1487        (auto simp: size_multiset_overloaded_eq intro!: setsum.cong)
  1488 qed simp
  1489 
  1490 lemma pmf_of_multiset[simp]: "pmf pmf_of_multiset x = count M x / size M"
  1491   by transfer rule
  1492 
  1493 lemma set_pmf_of_multiset[simp]: "set_pmf pmf_of_multiset = set_mset M"
  1494   by (auto simp: set_pmf_iff)
  1495 
  1496 end
  1497 
  1498 subsubsection \<open> Uniform Distribution \<close>
  1499 
  1500 context
  1501   fixes S :: "'a set" assumes S_not_empty: "S \<noteq> {}" and S_finite: "finite S"
  1502 begin
  1503 
  1504 lift_definition pmf_of_set :: "'a pmf" is "\<lambda>x. indicator S x / card S"
  1505 proof
  1506   show "(\<integral>\<^sup>+ x. ereal (indicator S x / real (card S)) \<partial>count_space UNIV) = 1"
  1507     using S_not_empty S_finite by (subst nn_integral_count_space'[of S]) auto
  1508 qed simp
  1509 
  1510 lemma pmf_of_set[simp]: "pmf pmf_of_set x = indicator S x / card S"
  1511   by transfer rule
  1512 
  1513 lemma set_pmf_of_set[simp]: "set_pmf pmf_of_set = S"
  1514   using S_finite S_not_empty by (auto simp: set_pmf_iff)
  1515 
  1516 lemma emeasure_pmf_of_set_space[simp]: "emeasure pmf_of_set S = 1"
  1517   by (rule measure_pmf.emeasure_eq_1_AE) (auto simp: AE_measure_pmf_iff)
  1518 
  1519 lemma nn_integral_pmf_of_set':
  1520   "(\<And>x. x \<in> S \<Longrightarrow> f x \<ge> 0) \<Longrightarrow> nn_integral (measure_pmf pmf_of_set) f = setsum f S / card S"
  1521 apply(subst nn_integral_measure_pmf_finite, simp_all add: S_finite)
  1522 apply(simp add: setsum_ereal_left_distrib[symmetric])
  1523 apply(subst ereal_divide', simp add: S_not_empty S_finite)
  1524 apply(simp add: ereal_times_divide_eq one_ereal_def[symmetric])
  1525 done
  1526 
  1527 lemma nn_integral_pmf_of_set:
  1528   "nn_integral (measure_pmf pmf_of_set) f = setsum (max 0 \<circ> f) S / card S"
  1529 apply(subst nn_integral_max_0[symmetric])
  1530 apply(subst nn_integral_pmf_of_set')
  1531 apply simp_all
  1532 done
  1533 
  1534 lemma integral_pmf_of_set:
  1535   "integral\<^sup>L (measure_pmf pmf_of_set) f = setsum f S / card S"
  1536 apply(simp add: real_lebesgue_integral_def integrable_measure_pmf_finite nn_integral_pmf_of_set S_finite)
  1537 apply(subst real_of_ereal_minus')
  1538  apply(simp add: ereal_max_0 S_finite del: ereal_max)
  1539 apply(simp add: ereal_max_0 S_finite S_not_empty del: ereal_max)
  1540 apply(simp add: field_simps S_finite S_not_empty)
  1541 apply(subst setsum.distrib[symmetric])
  1542 apply(rule setsum.cong; simp_all)
  1543 done
  1544 
  1545 lemma emeasure_pmf_of_set:
  1546   "emeasure (measure_pmf pmf_of_set) A = card (S \<inter> A) / card S"
  1547 apply(subst nn_integral_indicator[symmetric], simp)
  1548 apply(subst nn_integral_pmf_of_set)
  1549 apply(simp add: o_def max_def ereal_indicator[symmetric] S_not_empty S_finite real_of_nat_indicator[symmetric] of_nat_setsum[symmetric] setsum_indicator_eq_card del: of_nat_setsum)
  1550 done
  1551 
  1552 end
  1553 
  1554 lemma pmf_of_set_singleton: "pmf_of_set {x} = return_pmf x"
  1555 by(rule pmf_eqI)(simp add: indicator_def)
  1556 
  1557 lemma map_pmf_of_set_inj:
  1558   assumes f: "inj_on f A"
  1559   and [simp]: "A \<noteq> {}" "finite A"
  1560   shows "map_pmf f (pmf_of_set A) = pmf_of_set (f ` A)" (is "?lhs = ?rhs")
  1561 proof(rule pmf_eqI)
  1562   fix i
  1563   show "pmf ?lhs i = pmf ?rhs i"
  1564   proof(cases "i \<in> f ` A")
  1565     case True
  1566     then obtain i' where "i = f i'" "i' \<in> A" by auto
  1567     thus ?thesis using f by(simp add: card_image pmf_map_inj)
  1568   next
  1569     case False
  1570     hence "pmf ?lhs i = 0" by(simp add: pmf_eq_0_set_pmf set_map_pmf)
  1571     moreover have "pmf ?rhs i = 0" using False by simp
  1572     ultimately show ?thesis by simp
  1573   qed
  1574 qed
  1575 
  1576 lemma bernoulli_pmf_half_conv_pmf_of_set: "bernoulli_pmf (1 / 2) = pmf_of_set UNIV"
  1577 by(rule pmf_eqI) simp_all
  1578 
  1579 
  1580 
  1581 lemma measure_pmf_of_set:
  1582   assumes "S \<noteq> {}" "finite S"
  1583   shows "measure (measure_pmf (pmf_of_set S)) A = card (S \<inter> A) / card S"
  1584 using emeasure_pmf_of_set[OF assms, of A]
  1585 unfolding measure_pmf.emeasure_eq_measure by simp
  1586 
  1587 subsubsection \<open> Poisson Distribution \<close>
  1588 
  1589 context
  1590   fixes rate :: real assumes rate_pos: "0 < rate"
  1591 begin
  1592 
  1593 lift_definition poisson_pmf :: "nat pmf" is "\<lambda>k. rate ^ k / fact k * exp (-rate)"
  1594 proof  (* by Manuel Eberl *)
  1595   have summable: "summable (\<lambda>x::nat. rate ^ x / fact x)" using summable_exp
  1596     by (simp add: field_simps divide_inverse [symmetric])
  1597   have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x * exp (-rate) \<partial>count_space UNIV) =
  1598           exp (-rate) * (\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV)"
  1599     by (simp add: field_simps nn_integral_cmult[symmetric])
  1600   also from rate_pos have "(\<integral>\<^sup>+(x::nat). rate ^ x / fact x \<partial>count_space UNIV) = (\<Sum>x. rate ^ x / fact x)"
  1601     by (simp_all add: nn_integral_count_space_nat suminf_ereal summable suminf_ereal_finite)
  1602   also have "... = exp rate" unfolding exp_def
  1603     by (simp add: field_simps divide_inverse [symmetric])
  1604   also have "ereal (exp (-rate)) * ereal (exp rate) = 1"
  1605     by (simp add: mult_exp_exp)
  1606   finally show "(\<integral>\<^sup>+ x. ereal (rate ^ x / (fact x) * exp (- rate)) \<partial>count_space UNIV) = 1" .
  1607 qed (simp add: rate_pos[THEN less_imp_le])
  1608 
  1609 lemma pmf_poisson[simp]: "pmf poisson_pmf k = rate ^ k / fact k * exp (-rate)"
  1610   by transfer rule
  1611 
  1612 lemma set_pmf_poisson[simp]: "set_pmf poisson_pmf = UNIV"
  1613   using rate_pos by (auto simp: set_pmf_iff)
  1614 
  1615 end
  1616 
  1617 subsubsection \<open> Binomial Distribution \<close>
  1618 
  1619 context
  1620   fixes n :: nat and p :: real assumes p_nonneg: "0 \<le> p" and p_le_1: "p \<le> 1"
  1621 begin
  1622 
  1623 lift_definition binomial_pmf :: "nat pmf" is "\<lambda>k. (n choose k) * p^k * (1 - p)^(n - k)"
  1624 proof
  1625   have "(\<integral>\<^sup>+k. ereal (real (n choose k) * p ^ k * (1 - p) ^ (n - k)) \<partial>count_space UNIV) =
  1626     ereal (\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k))"
  1627     using p_le_1 p_nonneg by (subst nn_integral_count_space') auto
  1628   also have "(\<Sum>k\<le>n. real (n choose k) * p ^ k * (1 - p) ^ (n - k)) = (p + (1 - p)) ^ n"
  1629     by (subst binomial_ring) (simp add: atLeast0AtMost)
  1630   finally show "(\<integral>\<^sup>+ x. ereal (real (n choose x) * p ^ x * (1 - p) ^ (n - x)) \<partial>count_space UNIV) = 1"
  1631     by simp
  1632 qed (insert p_nonneg p_le_1, simp)
  1633 
  1634 lemma pmf_binomial[simp]: "pmf binomial_pmf k = (n choose k) * p^k * (1 - p)^(n - k)"
  1635   by transfer rule
  1636 
  1637 lemma set_pmf_binomial_eq: "set_pmf binomial_pmf = (if p = 0 then {0} else if p = 1 then {n} else {.. n})"
  1638   using p_nonneg p_le_1 unfolding set_eq_iff set_pmf_iff pmf_binomial by (auto simp: set_pmf_iff)
  1639 
  1640 end
  1641 
  1642 end
  1643 
  1644 lemma set_pmf_binomial_0[simp]: "set_pmf (binomial_pmf n 0) = {0}"
  1645   by (simp add: set_pmf_binomial_eq)
  1646 
  1647 lemma set_pmf_binomial_1[simp]: "set_pmf (binomial_pmf n 1) = {n}"
  1648   by (simp add: set_pmf_binomial_eq)
  1649 
  1650 lemma set_pmf_binomial[simp]: "0 < p \<Longrightarrow> p < 1 \<Longrightarrow> set_pmf (binomial_pmf n p) = {..n}"
  1651   by (simp add: set_pmf_binomial_eq)
  1652 
  1653 context begin interpretation lifting_syntax .
  1654 
  1655 lemma bind_pmf_parametric [transfer_rule]:
  1656   "(rel_pmf A ===> (A ===> rel_pmf B) ===> rel_pmf B) bind_pmf bind_pmf"
  1657 by(blast intro: rel_pmf_bindI dest: rel_funD)
  1658 
  1659 lemma return_pmf_parametric [transfer_rule]: "(A ===> rel_pmf A) return_pmf return_pmf"
  1660 by(rule rel_funI) simp
  1661 
  1662 end
  1663 
  1664 end