src/HOL/Probability/Probability_Mass_Function.thy
changeset 59731 7fccaeec22f0
parent 59730 b7c394c7a619
parent 59681 f24ab09e4c37
child 60068 ef2123db900c
equal deleted inserted replaced
59730:b7c394c7a619 59731:7fccaeec22f0
   791 
   791 
   792 end
   792 end
   793 
   793 
   794 subsection \<open> Conditional Probabilities \<close>
   794 subsection \<open> Conditional Probabilities \<close>
   795 
   795 
       
   796 lemma measure_pmf_zero_iff: "measure (measure_pmf p) s = 0 \<longleftrightarrow> set_pmf p \<inter> s = {}"
       
   797   by (subst measure_pmf.prob_eq_0) (auto simp: AE_measure_pmf_iff)
       
   798 
   796 context
   799 context
   797   fixes p :: "'a pmf" and s :: "'a set"
   800   fixes p :: "'a pmf" and s :: "'a set"
   798   assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
   801   assumes not_empty: "set_pmf p \<inter> s \<noteq> {}"
   799 begin
   802 begin
   800 
   803 
   852   then show ?thesis
   855   then show ?thesis
   853     by (intro pmf_eqI) simp
   856     by (intro pmf_eqI) simp
   854 qed
   857 qed
   855 
   858 
   856 lemma bind_cond_pmf_cancel:
   859 lemma bind_cond_pmf_cancel:
   857   assumes in_S: "\<And>x. x \<in> set_pmf p \<Longrightarrow> x \<in> S x" "\<And>x. x \<in> set_pmf q \<Longrightarrow> x \<in> S x"
   860   assumes [simp]: "\<And>x. x \<in> set_pmf p \<Longrightarrow> set_pmf q \<inter> {y. R x y} \<noteq> {}"
   858   assumes S_eq: "\<And>x y. x \<in> S y \<Longrightarrow> S x = S y"
   861   assumes [simp]: "\<And>y. y \<in> set_pmf q \<Longrightarrow> set_pmf p \<inter> {x. R x y} \<noteq> {}"
   859   and same: "\<And>x. measure (measure_pmf p) (S x) = measure (measure_pmf q) (S x)"
   862   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}"
   860   shows "bind_pmf p (\<lambda>x. cond_pmf q (S x)) = q" (is "?lhs = _")
   863   shows "bind_pmf p (\<lambda>x. cond_pmf q {y. R x y}) = q"
   861 proof (rule pmf_eqI)
   864 proof (rule pmf_eqI)
   862   { fix x
   865   fix i
   863     assume "x \<in> set_pmf p"
   866   have "ereal (pmf (bind_pmf p (\<lambda>x. cond_pmf q {y. R x y})) i) =
   864     hence "set_pmf p \<inter> (S x) \<noteq> {}" using in_S by auto
   867     (\<integral>\<^sup>+x. ereal (pmf q i / measure p {x. R x i}) * ereal (indicator {x. R x i} x) \<partial>p)"
   865     hence "measure (measure_pmf p) (S x) \<noteq> 0"
   868     by (auto simp add: ereal_pmf_bind AE_measure_pmf_iff pmf_cond pmf_eq_0_set_pmf intro!: nn_integral_cong_AE)
   866       by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff)
   869   also have "\<dots> = (pmf q i * measure p {x. R x i}) / measure p {x. R x i}"
   867     with same have "measure (measure_pmf q) (S x) \<noteq> 0" by simp
   870     by (simp add: pmf_nonneg measure_nonneg zero_ereal_def[symmetric] ereal_indicator
   868     hence "set_pmf q \<inter> S x \<noteq> {}"
   871                   nn_integral_cmult measure_pmf.emeasure_eq_measure)
   869       by(auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff) }
   872   also have "\<dots> = pmf q i"
   870   note [simp] = this
   873     by (cases "pmf q i = 0") (simp_all add: pmf_eq_0_set_pmf measure_measure_pmf_not_zero)
   871 
   874   finally show "pmf (bind_pmf p (\<lambda>x. cond_pmf q {y. R x y})) i = pmf q i"
   872   fix z
   875     by simp
   873   have pmf_q_z: "z \<notin> S z \<Longrightarrow> pmf q z = 0"
       
   874     by(erule contrapos_np)(simp add: pmf_eq_0_set_pmf in_S)
       
   875 
       
   876   have "ereal (pmf ?lhs z) = \<integral>\<^sup>+ x. ereal (pmf (cond_pmf q (S x)) z) \<partial>measure_pmf p"
       
   877     by(simp add: ereal_pmf_bind)
       
   878   also have "\<dots> = \<integral>\<^sup>+ x. ereal (pmf q z / measure p (S z)) * indicator (S z) x \<partial>measure_pmf p"
       
   879     by(rule nn_integral_cong_AE)(auto simp add: AE_measure_pmf_iff pmf_cond same pmf_q_z in_S dest!: S_eq split: split_indicator)
       
   880   also have "\<dots> = pmf q z" using pmf_nonneg[of q z]
       
   881     by (subst nn_integral_cmult)(auto simp add: measure_nonneg measure_pmf.emeasure_eq_measure same measure_pmf.prob_eq_0 AE_measure_pmf_iff pmf_eq_0_set_pmf in_S)
       
   882   finally show "pmf ?lhs z = pmf q z" by simp
       
   883 qed
   876 qed
   884 
   877 
   885 subsection \<open> Relator \<close>
   878 subsection \<open> Relator \<close>
   886 
   879 
   887 inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
   880 inductive rel_pmf :: "('a \<Rightarrow> 'b \<Rightarrow> bool) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf \<Rightarrow> bool"
   888 for R p q
   881 for R p q
   889 where
   882 where
   890   "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y;
   883   "\<lbrakk> \<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y;
   891      map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
   884      map_pmf fst pq = p; map_pmf snd pq = q \<rbrakk>
   892   \<Longrightarrow> rel_pmf R p q"
   885   \<Longrightarrow> rel_pmf R p q"
       
   886 
       
   887 lemma rel_pmfI:
       
   888   assumes R: "rel_set R (set_pmf p) (set_pmf q)"
       
   889   assumes eq: "\<And>x y. x \<in> set_pmf p \<Longrightarrow> y \<in> set_pmf q \<Longrightarrow> R x y \<Longrightarrow>
       
   890     measure p {x. R x y} = measure q {y. R x y}"
       
   891   shows "rel_pmf R p q"
       
   892 proof
       
   893   let ?pq = "bind_pmf p (\<lambda>x. bind_pmf (cond_pmf q {y. R x y}) (\<lambda>y. return_pmf (x, y)))"
       
   894   have "\<And>x. x \<in> set_pmf p \<Longrightarrow> set_pmf q \<inter> {y. R x y} \<noteq> {}"
       
   895     using R by (auto simp: rel_set_def)
       
   896   then show "\<And>x y. (x, y) \<in> set_pmf ?pq \<Longrightarrow> R x y"
       
   897     by auto
       
   898   show "map_pmf fst ?pq = p"
       
   899     by (simp add: map_bind_pmf map_return_pmf bind_return_pmf')
       
   900 
       
   901   show "map_pmf snd ?pq = q"
       
   902     using R eq
       
   903     apply (simp add: bind_cond_pmf_cancel map_bind_pmf map_return_pmf bind_return_pmf')
       
   904     apply (rule bind_cond_pmf_cancel)
       
   905     apply (auto simp: rel_set_def)
       
   906     done
       
   907 qed
       
   908 
       
   909 lemma rel_pmf_imp_rel_set: "rel_pmf R p q \<Longrightarrow> rel_set R (set_pmf p) (set_pmf q)"
       
   910   by (force simp add: rel_pmf.simps rel_set_def)
       
   911 
       
   912 lemma rel_pmfD_measure:
       
   913   assumes rel_R: "rel_pmf R p q" and R: "\<And>a b. R a b \<Longrightarrow> R a y \<longleftrightarrow> R x b"
       
   914   assumes "x \<in> set_pmf p" "y \<in> set_pmf q"
       
   915   shows "measure p {x. R x y} = measure q {y. R x y}"
       
   916 proof -
       
   917   from rel_R obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
       
   918     and eq: "p = map_pmf fst pq" "q = map_pmf snd pq"
       
   919     by (auto elim: rel_pmf.cases)
       
   920   have "measure p {x. R x y} = measure pq {x. R (fst x) y}"
       
   921     by (simp add: eq map_pmf_rep_eq measure_distr)
       
   922   also have "\<dots> = measure pq {y. R x (snd y)}"
       
   923     by (intro measure_pmf.finite_measure_eq_AE)
       
   924        (auto simp: AE_measure_pmf_iff R dest!: pq)
       
   925   also have "\<dots> = measure q {y. R x y}"
       
   926     by (simp add: eq map_pmf_rep_eq measure_distr)
       
   927   finally show "measure p {x. R x y} = measure q {y. R x y}" .
       
   928 qed
       
   929 
       
   930 lemma rel_pmf_iff_measure:
       
   931   assumes "symp R" "transp R"
       
   932   shows "rel_pmf R p q \<longleftrightarrow>
       
   933     rel_set R (set_pmf p) (set_pmf q) \<and>
       
   934     (\<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})"
       
   935   by (safe intro!: rel_pmf_imp_rel_set rel_pmfI)
       
   936      (auto intro!: rel_pmfD_measure dest: sympD[OF \<open>symp R\<close>] transpD[OF \<open>transp R\<close>])
       
   937 
       
   938 lemma quotient_rel_set_disjoint:
       
   939   "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 = {}"
       
   940   using in_quotient_imp_closed[of UNIV "{(x, y). R x y}" C] 
       
   941   by (auto 0 0 simp: equivp_equiv rel_set_def set_eq_iff elim: equivpE)
       
   942      (blast dest: equivp_symp)+
       
   943 
       
   944 lemma quotientD: "equiv X R \<Longrightarrow> A \<in> X // R \<Longrightarrow> x \<in> A \<Longrightarrow> A = R `` {x}"
       
   945   by (metis Image_singleton_iff equiv_class_eq_iff quotientE)
       
   946 
       
   947 lemma rel_pmf_iff_equivp:
       
   948   assumes "equivp R"
       
   949   shows "rel_pmf R p q \<longleftrightarrow> (\<forall>C\<in>UNIV // {(x, y). R x y}. measure p C = measure q C)"
       
   950     (is "_ \<longleftrightarrow>   (\<forall>C\<in>_//?R. _)")
       
   951 proof (subst rel_pmf_iff_measure, safe)
       
   952   show "symp R" "transp R"
       
   953     using assms by (auto simp: equivp_reflp_symp_transp)
       
   954 next
       
   955   fix C assume C: "C \<in> UNIV // ?R" and R: "rel_set R (set_pmf p) (set_pmf q)"
       
   956   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}"
       
   957   
       
   958   show "measure p C = measure q C"
       
   959   proof cases
       
   960     assume "p \<inter> C = {}"
       
   961     moreover then have "q \<inter> C = {}"  
       
   962       using quotient_rel_set_disjoint[OF assms C R] by simp
       
   963     ultimately show ?thesis
       
   964       unfolding measure_pmf_zero_iff[symmetric] by simp
       
   965   next
       
   966     assume "p \<inter> C \<noteq> {}"
       
   967     moreover then have "q \<inter> C \<noteq> {}"  
       
   968       using quotient_rel_set_disjoint[OF assms C R] by simp
       
   969     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"
       
   970       by auto
       
   971     then have "R x y"
       
   972       using in_quotient_imp_in_rel[of UNIV ?R C x y] C assms
       
   973       by (simp add: equivp_equiv)
       
   974     with in_set eq have "measure p {x. R x y} = measure q {y. R x y}"
       
   975       by auto
       
   976     moreover have "{y. R x y} = C"
       
   977       using assms `x \<in> C` C quotientD[of UNIV ?R C x] by (simp add: equivp_equiv)
       
   978     moreover have "{x. R x y} = C"
       
   979       using assms `y \<in> C` C quotientD[of UNIV "?R" C y] sympD[of R]
       
   980       by (auto simp add: equivp_equiv elim: equivpE)
       
   981     ultimately show ?thesis
       
   982       by auto
       
   983   qed
       
   984 next
       
   985   assume eq: "\<forall>C\<in>UNIV // ?R. measure p C = measure q C"
       
   986   show "rel_set R (set_pmf p) (set_pmf q)"
       
   987     unfolding rel_set_def
       
   988   proof safe
       
   989     fix x assume x: "x \<in> set_pmf p"
       
   990     have "{y. R x y} \<in> UNIV // ?R"
       
   991       by (auto simp: quotient_def)
       
   992     with eq have *: "measure q {y. R x y} = measure p {y. R x y}"
       
   993       by auto
       
   994     have "measure q {y. R x y} \<noteq> 0"
       
   995       using x assms unfolding * by (auto simp: measure_pmf_zero_iff set_eq_iff dest: equivp_reflp)
       
   996     then show "\<exists>y\<in>set_pmf q. R x y"
       
   997       unfolding measure_pmf_zero_iff by auto
       
   998   next
       
   999     fix y assume y: "y \<in> set_pmf q"
       
  1000     have "{x. R x y} \<in> UNIV // ?R"
       
  1001       using assms by (auto simp: quotient_def dest: equivp_symp)
       
  1002     with eq have *: "measure p {x. R x y} = measure q {x. R x y}"
       
  1003       by auto
       
  1004     have "measure p {x. R x y} \<noteq> 0"
       
  1005       using y assms unfolding * by (auto simp: measure_pmf_zero_iff set_eq_iff dest: equivp_reflp)
       
  1006     then show "\<exists>x\<in>set_pmf p. R x y"
       
  1007       unfolding measure_pmf_zero_iff by auto
       
  1008   qed
       
  1009 
       
  1010   fix x y assume "x \<in> set_pmf p" "y \<in> set_pmf q" "R x y"
       
  1011   have "{y. R x y} \<in> UNIV // ?R" "{x. R x y} = {y. R x y}"
       
  1012     using assms `R x y` by (auto simp: quotient_def dest: equivp_symp equivp_transp)
       
  1013   with eq show "measure p {x. R x y} = measure q {y. R x y}"
       
  1014     by auto
       
  1015 qed
   893 
  1016 
   894 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
  1017 bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: rel_pmf
   895 proof -
  1018 proof -
   896   show "map_pmf id = id" by (rule map_pmf_id)
  1019   show "map_pmf id = id" by (rule map_pmf_id)
   897   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose)
  1020   show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose)
   917   { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
  1040   { fix p :: "'a pmf" and f :: "'a \<Rightarrow> 'b" and g x
   918     assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
  1041     assume p: "\<And>z. z \<in> set_pmf p \<Longrightarrow> f z = g z"
   919       and x: "x \<in> set_pmf p"
  1042       and x: "x \<in> set_pmf p"
   920     thus "f x = g x" by simp }
  1043     thus "f x = g x" by simp }
   921 
  1044 
   922   fix R :: "'a => 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
  1045   fix R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and S :: "'b \<Rightarrow> 'c \<Rightarrow> bool"
   923   { fix p q r
  1046   { fix p q r
   924     assume pq: "rel_pmf R p q"
  1047     assume pq: "rel_pmf R p q"
   925       and qr:"rel_pmf S q r"
  1048       and qr:"rel_pmf S q r"
   926     from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
  1049     from pq obtain pq where pq: "\<And>x y. (x, y) \<in> set_pmf pq \<Longrightarrow> R x y"
   927       and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
  1050       and p: "p = map_pmf fst pq" and q: "q = map_pmf snd pq" by cases auto
   928     from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
  1051     from qr obtain qr where qr: "\<And>y z. (y, z) \<in> set_pmf qr \<Longrightarrow> S y z"
   929       and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
  1052       and q': "q = map_pmf fst qr" and r: "r = map_pmf snd qr" by cases auto
   930 
  1053 
   931     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)))"
  1054     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)))"
   932     have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {(y', z). y' = y} \<noteq> {}"
  1055     have pr_welldefined: "\<And>y. y \<in> q \<Longrightarrow> qr \<inter> {yz. fst yz = y} \<noteq> {}"
   933       by (force simp: q')
  1056       by (force simp: q')
   934 
  1057 
   935     have "rel_pmf (R OO S) p r"
  1058     have "rel_pmf (R OO S) p r"
   936     proof (rule rel_pmf.intros)
  1059     proof (rule rel_pmf.intros)
   937       fix x z assume "(x, z) \<in> pr"
  1060       fix x z assume "(x, z) \<in> pr"
   938       then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
  1061       then have "\<exists>y. (x, y) \<in> pq \<and> (y, z) \<in> qr"
   939         by (auto simp: q pr_welldefined pr_def split_beta)
  1062         by (auto simp: q pr_welldefined pr_def split_beta)
   940       with pq qr show "(R OO S) x z"
  1063       with pq qr show "(R OO S) x z"
   941         by blast
  1064         by blast
   942     next
  1065     next
   943       have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {(y', z). y' = y}))"
  1066       have "map_pmf snd pr = map_pmf snd (bind_pmf q (\<lambda>y. cond_pmf qr {yz. fst yz = y}))"
   944         by (simp add: pr_def q split_beta bind_map_pmf map_pmf_def[symmetric] map_bind_pmf map_return_pmf)
  1067         by (simp add: pr_def q split_beta bind_map_pmf map_pmf_def[symmetric] map_bind_pmf map_return_pmf map_pmf_comp)
   945       then show "map_pmf snd pr = r"
  1068       then show "map_pmf snd pr = r"
   946         unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) auto
  1069         unfolding r q' bind_map_pmf by (subst (asm) bind_cond_pmf_cancel) (auto simp: eq_commute)
   947     qed (simp add: pr_def map_bind_pmf split_beta map_return_pmf map_pmf_def[symmetric] p) }
  1070     qed (simp add: pr_def map_bind_pmf split_beta map_return_pmf map_pmf_def[symmetric] p map_pmf_comp) }
   948   then show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
  1071   then show "rel_pmf R OO rel_pmf S \<le> rel_pmf (R OO S)"
   949     by(auto simp add: le_fun_def)
  1072     by(auto simp add: le_fun_def)
   950 qed (fact natLeq_card_order natLeq_cinfinite)+
  1073 qed (fact natLeq_card_order natLeq_cinfinite)+
   951 
  1074 
   952 lemma rel_pmf_conj[simp]:
  1075 lemma rel_pmf_conj[simp]:
  1135   fixes p q :: "'a pmf"
  1258   fixes p q :: "'a pmf"
  1136   assumes 1: "rel_pmf R p q"
  1259   assumes 1: "rel_pmf R p q"
  1137   assumes 2: "rel_pmf R q p"
  1260   assumes 2: "rel_pmf R q p"
  1138   and refl: "reflp R" and trans: "transp R"
  1261   and refl: "reflp R" and trans: "transp R"
  1139   shows "rel_pmf (inf R R\<inverse>\<inverse>) p q"
  1262   shows "rel_pmf (inf R R\<inverse>\<inverse>) p q"
  1140 proof
  1263 proof (subst rel_pmf_iff_equivp, safe)
  1141   let ?E = "\<lambda>x. {y. R x y \<and> R y x}"
  1264   show "equivp (inf R R\<inverse>\<inverse>)"
  1142   let ?\<mu>E = "\<lambda>x. measure q (?E x)"
  1265     using trans refl by (auto simp: equivp_reflp_symp_transp intro: sympI transpI reflpI dest: transpD reflpD)
  1143   { fix x
  1266   
  1144     have "measure p (?E x) = measure p ({y. R x y} - {y. R x y \<and> \<not> R y x})"
  1267   fix C assume "C \<in> UNIV // {(x, y). inf R R\<inverse>\<inverse> x y}"
  1145       by(auto intro!: arg_cong[where f="measure p"])
  1268   then obtain x where C: "C = {y. R x y \<and> R y x}"
  1146     also have "\<dots> = measure p {y. R x y} - measure p {y. R x y \<and> \<not> R y x}"
  1269     by (auto elim: quotientE)
  1147       by (rule measure_pmf.finite_measure_Diff) auto
  1270 
  1148     also have "measure p {y. R x y \<and> \<not> R y x} = measure q {y. R x y \<and> \<not> R y x}"
  1271   let ?R = "\<lambda>x y. R x y \<and> R y x"
  1149       using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ioi)
  1272   let ?\<mu>R = "\<lambda>y. measure q {x. ?R x y}"
  1150     also have "measure p {y. R x y} = measure q {y. R x y}"
  1273   have "measure p {y. ?R x y} = measure p ({y. R x y} - {y. R x y \<and> \<not> R y x})"
  1151       using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ici)
  1274     by(auto intro!: arg_cong[where f="measure p"])
  1152     also have "measure q {y. R x y} - measure q {y. R x y \<and> ~ R y x} =
  1275   also have "\<dots> = measure p {y. R x y} - measure p {y. R x y \<and> \<not> R y x}"
  1153       measure q ({y. R x y} - {y. R x y \<and> \<not> R y x})"
  1276     by (rule measure_pmf.finite_measure_Diff) auto
  1154       by(rule measure_pmf.finite_measure_Diff[symmetric]) auto
  1277   also have "measure p {y. R x y \<and> \<not> R y x} = measure q {y. R x y \<and> \<not> R y x}"
  1155     also have "\<dots> = ?\<mu>E x"
  1278     using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ioi)
  1156       by(auto intro!: arg_cong[where f="measure q"])
  1279   also have "measure p {y. R x y} = measure q {y. R x y}"
  1157     also note calculation }
  1280     using 1 2 refl trans by(auto intro!: Orderings.antisym measure_Ici)
  1158   note eq = this
  1281   also have "measure q {y. R x y} - measure q {y. R x y \<and> \<not> R y x} =
  1159 
  1282     measure q ({y. R x y} - {y. R x y \<and> \<not> R y x})"
  1160   def pq \<equiv> "bind_pmf p (\<lambda>x. bind_pmf (cond_pmf q (?E x)) (\<lambda>y. return_pmf (x, y)))"
  1283     by(rule measure_pmf.finite_measure_Diff[symmetric]) auto
  1161 
  1284   also have "\<dots> = ?\<mu>R x"
  1162   show "map_pmf fst pq = p"
  1285     by(auto intro!: arg_cong[where f="measure q"])
  1163     by(simp add: pq_def map_bind_pmf map_return_pmf bind_return_pmf')
  1286   finally show "measure p C = measure q C"
  1164 
  1287     by (simp add: C conj_commute)
  1165   show "map_pmf snd pq = q"
       
  1166     unfolding pq_def map_bind_pmf map_return_pmf bind_return_pmf' snd_conv
       
  1167     by(subst bind_cond_pmf_cancel)(auto simp add: reflpD[OF \<open>reflp R\<close>] eq  intro: transpD[OF \<open>transp R\<close>])
       
  1168 
       
  1169   fix x y
       
  1170   assume "(x, y) \<in> set_pmf pq"
       
  1171   moreover
       
  1172   { assume "x \<in> set_pmf p"
       
  1173     hence "measure (measure_pmf p) (?E x) \<noteq> 0"
       
  1174       by (auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff intro: reflpD[OF \<open>reflp R\<close>])
       
  1175     hence "measure (measure_pmf q) (?E x) \<noteq> 0" using eq by simp
       
  1176     hence "set_pmf q \<inter> {y. R x y \<and> R y x} \<noteq> {}"
       
  1177       by (auto simp add: measure_pmf.prob_eq_0 AE_measure_pmf_iff) }
       
  1178   ultimately show "inf R R\<inverse>\<inverse> x y"
       
  1179     by (auto simp add: pq_def)
       
  1180 qed
  1288 qed
  1181 
  1289 
  1182 lemma rel_pmf_antisym:
  1290 lemma rel_pmf_antisym:
  1183   fixes p q :: "'a pmf"
  1291   fixes p q :: "'a pmf"
  1184   assumes 1: "rel_pmf R p q"
  1292   assumes 1: "rel_pmf R p q"