src/HOL/Probability/Probability_Mass_Function.thy
author hoelzl
Tue Oct 21 17:00:42 2014 +0200 (2014-10-21)
changeset 58730 b3fd0628f849
parent 58606 9c66f7c541fb
child 59000 6eb0725503fc
permissions -rw-r--r--
add transfer rule for set_pmf
hoelzl@58606
     1
(*  Title:      HOL/Probability/Probability_Mass_Function.thy
hoelzl@58606
     2
    Author:     Johannes Hölzl, TU München *)
hoelzl@58606
     3
hoelzl@58587
     4
theory Probability_Mass_Function
hoelzl@58587
     5
  imports Probability_Measure
hoelzl@58587
     6
begin
hoelzl@58587
     7
hoelzl@58587
     8
lemma (in prob_space) countable_support:
hoelzl@58587
     9
  "countable {x. measure M {x} \<noteq> 0}"
hoelzl@58587
    10
proof -
hoelzl@58587
    11
  let ?m = "\<lambda>x. measure M {x}"
hoelzl@58587
    12
  have *: "{x. ?m x \<noteq> 0} = (\<Union>n. {x. inverse (real (Suc n)) < ?m x})"
hoelzl@58587
    13
    by (auto intro!: measure_nonneg reals_Archimedean order_le_neq_trans)
hoelzl@58587
    14
  have **: "\<And>n. finite {x. inverse (Suc n) < ?m x}"
hoelzl@58587
    15
  proof (rule ccontr)
hoelzl@58587
    16
    fix n assume "infinite {x. inverse (Suc n) < ?m x}" (is "infinite ?X")
hoelzl@58587
    17
    then obtain X where "finite X" "card X = Suc (Suc n)" "X \<subseteq> ?X"
hoelzl@58606
    18
      by (metis infinite_arbitrarily_large)
hoelzl@58587
    19
    from this(3) have *: "\<And>x. x \<in> X \<Longrightarrow> 1 / Suc n \<le> ?m x" 
hoelzl@58587
    20
      by (auto simp: inverse_eq_divide)
hoelzl@58587
    21
    { fix x assume "x \<in> X"
hoelzl@58587
    22
      from *[OF this] have "?m x \<noteq> 0" by auto
hoelzl@58587
    23
      then have "{x} \<in> sets M" by (auto dest: measure_notin_sets) }
hoelzl@58587
    24
    note singleton_sets = this
hoelzl@58587
    25
    have "1 < (\<Sum>x\<in>X. 1 / Suc n)"
hoelzl@58587
    26
      by (simp add: `card X = Suc (Suc n)` real_eq_of_nat[symmetric] real_of_nat_Suc)
hoelzl@58587
    27
    also have "\<dots> \<le> (\<Sum>x\<in>X. ?m x)"
hoelzl@58587
    28
      by (rule setsum_mono) fact
hoelzl@58587
    29
    also have "\<dots> = measure M (\<Union>x\<in>X. {x})"
hoelzl@58587
    30
      using singleton_sets `finite X`
hoelzl@58587
    31
      by (intro finite_measure_finite_Union[symmetric]) (auto simp: disjoint_family_on_def)
hoelzl@58587
    32
    finally show False
hoelzl@58587
    33
      using prob_le_1[of "\<Union>x\<in>X. {x}"] by arith
hoelzl@58587
    34
  qed
hoelzl@58587
    35
  show ?thesis
hoelzl@58587
    36
    unfolding * by (intro countable_UN countableI_type countable_finite[OF **])
hoelzl@58587
    37
qed
hoelzl@58587
    38
hoelzl@58587
    39
typedef 'a pmf = "{M :: 'a measure. prob_space M \<and> sets M = UNIV \<and> (AE x in M. measure M {x} \<noteq> 0)}"
hoelzl@58587
    40
  morphisms measure_pmf Abs_pmf
hoelzl@58606
    41
  by (intro exI[of _ "uniform_measure (count_space UNIV) {undefined}"])
hoelzl@58606
    42
     (auto intro!: prob_space_uniform_measure AE_uniform_measureI)
hoelzl@58587
    43
hoelzl@58587
    44
declare [[coercion measure_pmf]]
hoelzl@58587
    45
hoelzl@58587
    46
lemma prob_space_measure_pmf: "prob_space (measure_pmf p)"
hoelzl@58587
    47
  using pmf.measure_pmf[of p] by auto
hoelzl@58587
    48
hoelzl@58587
    49
interpretation measure_pmf!: prob_space "measure_pmf M" for M
hoelzl@58587
    50
  by (rule prob_space_measure_pmf)
hoelzl@58587
    51
hoelzl@58587
    52
locale pmf_as_measure
hoelzl@58587
    53
begin
hoelzl@58587
    54
hoelzl@58587
    55
setup_lifting type_definition_pmf
hoelzl@58587
    56
hoelzl@58587
    57
end
hoelzl@58587
    58
hoelzl@58587
    59
context
hoelzl@58587
    60
begin
hoelzl@58587
    61
hoelzl@58587
    62
interpretation pmf_as_measure .
hoelzl@58587
    63
hoelzl@58587
    64
lift_definition pmf :: "'a pmf \<Rightarrow> 'a \<Rightarrow> real" is "\<lambda>M x. measure M {x}" .
hoelzl@58587
    65
hoelzl@58587
    66
lift_definition set_pmf :: "'a pmf \<Rightarrow> 'a set" is "\<lambda>M. {x. measure M {x} \<noteq> 0}" .
hoelzl@58587
    67
hoelzl@58587
    68
lift_definition map_pmf :: "('a \<Rightarrow> 'b) \<Rightarrow> 'a pmf \<Rightarrow> 'b pmf" is
hoelzl@58587
    69
  "\<lambda>f M. distr M (count_space UNIV) f"
hoelzl@58587
    70
proof safe
hoelzl@58587
    71
  fix M and f :: "'a \<Rightarrow> 'b"
hoelzl@58587
    72
  let ?D = "distr M (count_space UNIV) f"
hoelzl@58587
    73
  assume "prob_space M" and [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
hoelzl@58587
    74
  interpret prob_space M by fact
hoelzl@58587
    75
  from ae have "AE x in M. measure M (f -` {f x}) \<noteq> 0"
hoelzl@58587
    76
  proof eventually_elim
hoelzl@58587
    77
    fix x
hoelzl@58587
    78
    have "measure M {x} \<le> measure M (f -` {f x})"
hoelzl@58587
    79
      by (intro finite_measure_mono) auto
hoelzl@58587
    80
    then show "measure M {x} \<noteq> 0 \<Longrightarrow> measure M (f -` {f x}) \<noteq> 0"
hoelzl@58587
    81
      using measure_nonneg[of M "{x}"] by auto
hoelzl@58587
    82
  qed
hoelzl@58587
    83
  then show "AE x in ?D. measure ?D {x} \<noteq> 0"
hoelzl@58587
    84
    by (simp add: AE_distr_iff measure_distr measurable_def)
hoelzl@58587
    85
qed (auto simp: measurable_def prob_space.prob_space_distr)
hoelzl@58587
    86
hoelzl@58587
    87
declare [[coercion set_pmf]]
hoelzl@58587
    88
hoelzl@58587
    89
lemma countable_set_pmf: "countable (set_pmf p)"
hoelzl@58587
    90
  by transfer (metis prob_space.countable_support)
hoelzl@58587
    91
hoelzl@58587
    92
lemma sets_measure_pmf[simp]: "sets (measure_pmf p) = UNIV"
hoelzl@58587
    93
  by transfer metis
hoelzl@58587
    94
hoelzl@58587
    95
lemma space_measure_pmf[simp]: "space (measure_pmf p) = UNIV"
hoelzl@58587
    96
  using sets_eq_imp_space_eq[of "measure_pmf p" "count_space UNIV"] by simp
hoelzl@58587
    97
hoelzl@58587
    98
lemma measurable_pmf_measure1[simp]: "measurable (M :: 'a pmf) N = UNIV \<rightarrow> space N"
hoelzl@58587
    99
  by (auto simp: measurable_def)
hoelzl@58587
   100
hoelzl@58587
   101
lemma measurable_pmf_measure2[simp]: "measurable N (M :: 'a pmf) = measurable N (count_space UNIV)"
hoelzl@58587
   102
  by (intro measurable_cong_sets) simp_all
hoelzl@58587
   103
hoelzl@58587
   104
lemma pmf_positive: "x \<in> set_pmf p \<Longrightarrow> 0 < pmf p x"
hoelzl@58587
   105
  by transfer (simp add: less_le measure_nonneg)
hoelzl@58587
   106
hoelzl@58587
   107
lemma pmf_nonneg: "0 \<le> pmf p x"
hoelzl@58587
   108
  by transfer (simp add: measure_nonneg)
hoelzl@58587
   109
hoelzl@58587
   110
lemma emeasure_pmf_single:
hoelzl@58587
   111
  fixes M :: "'a pmf"
hoelzl@58587
   112
  shows "emeasure M {x} = pmf M x"
hoelzl@58587
   113
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
hoelzl@58587
   114
hoelzl@58587
   115
lemma AE_measure_pmf: "AE x in (M::'a pmf). x \<in> M"
hoelzl@58587
   116
  by transfer simp
hoelzl@58587
   117
hoelzl@58587
   118
lemma emeasure_pmf_single_eq_zero_iff:
hoelzl@58587
   119
  fixes M :: "'a pmf"
hoelzl@58587
   120
  shows "emeasure M {y} = 0 \<longleftrightarrow> y \<notin> M"
hoelzl@58587
   121
  by transfer (simp add: finite_measure.emeasure_eq_measure[OF prob_space.finite_measure])
hoelzl@58587
   122
hoelzl@58587
   123
lemma AE_measure_pmf_iff: "(AE x in measure_pmf M. P x) \<longleftrightarrow> (\<forall>y\<in>M. P y)"
hoelzl@58587
   124
proof -
hoelzl@58587
   125
  { fix y assume y: "y \<in> M" and P: "AE x in M. P x" "\<not> P y"
hoelzl@58587
   126
    with P have "AE x in M. x \<noteq> y"
hoelzl@58587
   127
      by auto
hoelzl@58587
   128
    with y have False
hoelzl@58587
   129
      by (simp add: emeasure_pmf_single_eq_zero_iff AE_iff_measurable[OF _ refl]) }
hoelzl@58587
   130
  then show ?thesis
hoelzl@58587
   131
    using AE_measure_pmf[of M] by auto
hoelzl@58587
   132
qed
hoelzl@58587
   133
hoelzl@58587
   134
lemma measure_pmf_eq_density: "measure_pmf p = density (count_space UNIV) (pmf p)"
hoelzl@58587
   135
proof (transfer, elim conjE)
hoelzl@58587
   136
  fix M :: "'a measure" assume [simp]: "sets M = UNIV" and ae: "AE x in M. measure M {x} \<noteq> 0"
hoelzl@58587
   137
  assume "prob_space M" then interpret prob_space M .
hoelzl@58587
   138
  show "M = density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))"
hoelzl@58587
   139
  proof (rule measure_eqI)
hoelzl@58587
   140
    fix A :: "'a set"
hoelzl@58587
   141
    have "(\<integral>\<^sup>+ x. ereal (measure M {x}) * indicator A x \<partial>count_space UNIV) = 
hoelzl@58587
   142
      (\<integral>\<^sup>+ x. emeasure M {x} * indicator (A \<inter> {x. measure M {x} \<noteq> 0}) x \<partial>count_space UNIV)"
hoelzl@58587
   143
      by (auto intro!: nn_integral_cong simp: emeasure_eq_measure split: split_indicator)
hoelzl@58587
   144
    also have "\<dots> = (\<integral>\<^sup>+ x. emeasure M {x} \<partial>count_space (A \<inter> {x. measure M {x} \<noteq> 0}))"
hoelzl@58587
   145
      by (subst nn_integral_restrict_space[symmetric]) (auto simp: restrict_count_space)
hoelzl@58587
   146
    also have "\<dots> = emeasure M (\<Union>x\<in>(A \<inter> {x. measure M {x} \<noteq> 0}). {x})"
hoelzl@58587
   147
      by (intro emeasure_UN_countable[symmetric] countable_Int2 countable_support)
hoelzl@58587
   148
         (auto simp: disjoint_family_on_def)
hoelzl@58587
   149
    also have "\<dots> = emeasure M A"
hoelzl@58587
   150
      using ae by (intro emeasure_eq_AE) auto
hoelzl@58587
   151
    finally show " emeasure M A = emeasure (density (count_space UNIV) (\<lambda>x. ereal (measure M {x}))) A"
hoelzl@58587
   152
      using emeasure_space_1 by (simp add: emeasure_density)
hoelzl@58587
   153
  qed simp
hoelzl@58587
   154
qed
hoelzl@58587
   155
hoelzl@58587
   156
lemma set_pmf_not_empty: "set_pmf M \<noteq> {}"
hoelzl@58587
   157
  using AE_measure_pmf[of M] by (intro notI) simp
hoelzl@58587
   158
hoelzl@58587
   159
lemma set_pmf_iff: "x \<in> set_pmf M \<longleftrightarrow> pmf M x \<noteq> 0"
hoelzl@58587
   160
  by transfer simp
hoelzl@58587
   161
hoelzl@58587
   162
lemma emeasure_pmf: "emeasure (M::'a pmf) M = 1"
hoelzl@58587
   163
proof -
hoelzl@58587
   164
  have "emeasure (M::'a pmf) M = emeasure (M::'a pmf) (space M)"
hoelzl@58587
   165
    by (intro emeasure_eq_AE) (simp_all add: AE_measure_pmf)
hoelzl@58587
   166
  then show ?thesis
hoelzl@58587
   167
    using measure_pmf.emeasure_space_1 by simp
hoelzl@58587
   168
qed
hoelzl@58587
   169
hoelzl@58587
   170
lemma map_pmf_id[simp]: "map_pmf id = id"
hoelzl@58587
   171
  by (rule, transfer) (auto simp: emeasure_distr measurable_def intro!: measure_eqI)
hoelzl@58587
   172
hoelzl@58587
   173
lemma map_pmf_compose: "map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g"
hoelzl@58587
   174
  by (rule, transfer) (simp add: distr_distr[symmetric, where N="count_space UNIV"] measurable_def) 
hoelzl@58587
   175
hoelzl@58587
   176
lemma map_pmf_cong:
hoelzl@58587
   177
  assumes "p = q"
hoelzl@58587
   178
  shows "(\<And>x. x \<in> set_pmf q \<Longrightarrow> f x = g x) \<Longrightarrow> map_pmf f p = map_pmf g q"
hoelzl@58587
   179
  unfolding `p = q`[symmetric] measure_pmf_inject[symmetric] map_pmf.rep_eq
hoelzl@58587
   180
  by (auto simp add: emeasure_distr AE_measure_pmf_iff intro!: emeasure_eq_AE measure_eqI)
hoelzl@58587
   181
hoelzl@58587
   182
lemma pmf_set_map: 
hoelzl@58587
   183
  fixes f :: "'a \<Rightarrow> 'b"
hoelzl@58587
   184
  shows "set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@58587
   185
proof (rule, transfer, clarsimp simp add: measure_distr measurable_def)
hoelzl@58587
   186
  fix f :: "'a \<Rightarrow> 'b" and M :: "'a measure"
hoelzl@58587
   187
  assume "prob_space M" and ae: "AE x in M. measure M {x} \<noteq> 0" and [simp]: "sets M = UNIV"
hoelzl@58587
   188
  interpret prob_space M by fact
hoelzl@58587
   189
  show "{x. measure M (f -` {x}) \<noteq> 0} = f ` {x. measure M {x} \<noteq> 0}"
hoelzl@58587
   190
  proof safe
hoelzl@58587
   191
    fix x assume "measure M (f -` {x}) \<noteq> 0"
hoelzl@58587
   192
    moreover have "measure M (f -` {x}) = measure M {y. f y = x \<and> measure M {y} \<noteq> 0}"
hoelzl@58587
   193
      using ae by (intro finite_measure_eq_AE) auto
hoelzl@58587
   194
    ultimately have "{y. f y = x \<and> measure M {y} \<noteq> 0} \<noteq> {}"
hoelzl@58587
   195
      by (metis measure_empty)
hoelzl@58587
   196
    then show "x \<in> f ` {x. measure M {x} \<noteq> 0}"
hoelzl@58587
   197
      by auto
hoelzl@58587
   198
  next
hoelzl@58587
   199
    fix x assume "measure M {x} \<noteq> 0"
hoelzl@58587
   200
    then have "0 < measure M {x}"
hoelzl@58587
   201
      using measure_nonneg[of M "{x}"] by auto
hoelzl@58587
   202
    also have "measure M {x} \<le> measure M (f -` {f x})"
hoelzl@58587
   203
      by (intro finite_measure_mono) auto
hoelzl@58587
   204
    finally show "measure M (f -` {f x}) = 0 \<Longrightarrow> False"
hoelzl@58587
   205
      by simp
hoelzl@58587
   206
  qed
hoelzl@58587
   207
qed
hoelzl@58587
   208
hoelzl@58587
   209
context
hoelzl@58587
   210
  fixes f :: "'a \<Rightarrow> real"
hoelzl@58587
   211
  assumes nonneg: "\<And>x. 0 \<le> f x"
hoelzl@58587
   212
  assumes prob: "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   213
begin
hoelzl@58587
   214
hoelzl@58587
   215
lift_definition embed_pmf :: "'a pmf" is "density (count_space UNIV) (ereal \<circ> f)"
hoelzl@58587
   216
proof (intro conjI)
hoelzl@58587
   217
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   218
    by (simp split: split_indicator)
hoelzl@58587
   219
  show "AE x in density (count_space UNIV) (ereal \<circ> f).
hoelzl@58587
   220
    measure (density (count_space UNIV) (ereal \<circ> f)) {x} \<noteq> 0"
hoelzl@58587
   221
    by (simp add: AE_density nonneg emeasure_density measure_def nn_integral_cmult_indicator)
hoelzl@58587
   222
  show "prob_space (density (count_space UNIV) (ereal \<circ> f))"
hoelzl@58587
   223
    by default (simp add: emeasure_density prob)
hoelzl@58587
   224
qed simp
hoelzl@58587
   225
hoelzl@58587
   226
lemma pmf_embed_pmf: "pmf embed_pmf x = f x"
hoelzl@58587
   227
proof transfer
hoelzl@58587
   228
  have *[simp]: "\<And>x y. ereal (f y) * indicator {x} y = ereal (f x) * indicator {x} y"
hoelzl@58587
   229
    by (simp split: split_indicator)
hoelzl@58587
   230
  fix x show "measure (density (count_space UNIV) (ereal \<circ> f)) {x} = f x"
hoelzl@58587
   231
    by transfer (simp add: measure_def emeasure_density nn_integral_cmult_indicator nonneg)
hoelzl@58587
   232
qed
hoelzl@58587
   233
hoelzl@58587
   234
end
hoelzl@58587
   235
hoelzl@58587
   236
lemma embed_pmf_transfer:
hoelzl@58730
   237
  "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"
hoelzl@58587
   238
  by (auto simp: rel_fun_def eq_onp_def embed_pmf.transfer)
hoelzl@58587
   239
hoelzl@58587
   240
lemma td_pmf_embed_pmf:
hoelzl@58587
   241
  "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}"
hoelzl@58587
   242
  unfolding type_definition_def
hoelzl@58587
   243
proof safe
hoelzl@58587
   244
  fix p :: "'a pmf"
hoelzl@58587
   245
  have "(\<integral>\<^sup>+ x. 1 \<partial>measure_pmf p) = 1"
hoelzl@58587
   246
    using measure_pmf.emeasure_space_1[of p] by simp
hoelzl@58587
   247
  then show *: "(\<integral>\<^sup>+ x. ereal (pmf p x) \<partial>count_space UNIV) = 1"
hoelzl@58587
   248
    by (simp add: measure_pmf_eq_density nn_integral_density pmf_nonneg del: nn_integral_const)
hoelzl@58587
   249
hoelzl@58587
   250
  show "embed_pmf (pmf p) = p"
hoelzl@58587
   251
    by (intro measure_pmf_inject[THEN iffD1])
hoelzl@58587
   252
       (simp add: * embed_pmf.rep_eq pmf_nonneg measure_pmf_eq_density[of p] comp_def)
hoelzl@58587
   253
next
hoelzl@58587
   254
  fix f :: "'a \<Rightarrow> real" assume "\<forall>x. 0 \<le> f x" "(\<integral>\<^sup>+x. f x \<partial>count_space UNIV) = 1"
hoelzl@58587
   255
  then show "pmf (embed_pmf f) = f"
hoelzl@58587
   256
    by (auto intro!: pmf_embed_pmf)
hoelzl@58587
   257
qed (rule pmf_nonneg)
hoelzl@58587
   258
hoelzl@58587
   259
end
hoelzl@58587
   260
hoelzl@58587
   261
locale pmf_as_function
hoelzl@58587
   262
begin
hoelzl@58587
   263
hoelzl@58587
   264
setup_lifting td_pmf_embed_pmf
hoelzl@58587
   265
hoelzl@58730
   266
lemma set_pmf_transfer[transfer_rule]: 
hoelzl@58730
   267
  assumes "bi_total A"
hoelzl@58730
   268
  shows "rel_fun (pcr_pmf A) (rel_set A) (\<lambda>f. {x. f x \<noteq> 0}) set_pmf"  
hoelzl@58730
   269
  using `bi_total A`
hoelzl@58730
   270
  by (auto simp: pcr_pmf_def cr_pmf_def rel_fun_def rel_set_def bi_total_def Bex_def set_pmf_iff)
hoelzl@58730
   271
     metis+
hoelzl@58730
   272
hoelzl@58587
   273
end 
hoelzl@58587
   274
hoelzl@58587
   275
(*
hoelzl@58587
   276
hoelzl@58587
   277
definition
hoelzl@58587
   278
  "rel_pmf P d1 d2 \<longleftrightarrow> (\<exists>p3. (\<forall>(x, y) \<in> set_pmf p3. P x y) \<and> map_pmf fst p3 = d1 \<and> map_pmf snd p3 = d2)"
hoelzl@58587
   279
hoelzl@58587
   280
lift_definition pmf_join :: "real \<Rightarrow> 'a pmf \<Rightarrow> 'a pmf \<Rightarrow> 'a pmf" is
hoelzl@58587
   281
  "\<lambda>p M1 M2. density (count_space UNIV) (\<lambda>x. p * measure M1 {x} + (1 - p) * measure M2 {x})"
hoelzl@58587
   282
sorry
hoelzl@58587
   283
hoelzl@58587
   284
lift_definition pmf_single :: "'a \<Rightarrow> 'a pmf" is
hoelzl@58587
   285
  "\<lambda>x. uniform_measure (count_space UNIV) {x}"
hoelzl@58587
   286
sorry
hoelzl@58587
   287
hoelzl@58587
   288
bnf pmf: "'a pmf" map: map_pmf sets: set_pmf bd : "natLeq" rel: pmf_rel
hoelzl@58587
   289
proof -
hoelzl@58587
   290
  show "map_pmf id = id" by (rule map_pmf_id)
hoelzl@58587
   291
  show "\<And>f g. map_pmf (f \<circ> g) = map_pmf f \<circ> map_pmf g" by (rule map_pmf_compose) 
hoelzl@58587
   292
  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"
hoelzl@58587
   293
    by (intro map_pmg_cong refl)
hoelzl@58587
   294
hoelzl@58587
   295
  show "\<And>f::'a \<Rightarrow> 'b. set_pmf \<circ> map_pmf f = op ` f \<circ> set_pmf"
hoelzl@58587
   296
    by (rule pmf_set_map)
hoelzl@58587
   297
hoelzl@58587
   298
  { fix p :: "'s pmf"
hoelzl@58587
   299
    have "(card_of (set_pmf p), card_of (UNIV :: nat set)) \<in> ordLeq"
hoelzl@58587
   300
      by (rule card_of_ordLeqI[where f="to_nat_on (set_pmf p)"])
hoelzl@58587
   301
         (auto intro: countable_set_pmf inj_on_to_nat_on)
hoelzl@58587
   302
    also have "(card_of (UNIV :: nat set), natLeq) \<in> ordLeq"
hoelzl@58587
   303
      by (metis Field_natLeq card_of_least natLeq_Well_order)
hoelzl@58587
   304
    finally show "(card_of (set_pmf p), natLeq) \<in> ordLeq" . }
hoelzl@58587
   305
hoelzl@58587
   306
  show "\<And>R. pmf_rel R =
hoelzl@58587
   307
         (BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf fst))\<inverse>\<inverse> OO
hoelzl@58587
   308
         BNF_Util.Grp {x. set_pmf x \<subseteq> {(x, y). R x y}} (map_pmf snd)"
hoelzl@58587
   309
     by (auto simp add: fun_eq_iff pmf_rel_def BNF_Util.Grp_def OO_def)
hoelzl@58587
   310
hoelzl@58587
   311
  { let ?f = "map_pmf fst" and ?s = "map_pmf snd"
hoelzl@58587
   312
    fix R :: "'a \<Rightarrow> 'b \<Rightarrow> bool" and A assume "\<And>x y. (x, y) \<in> set_pmf A \<Longrightarrow> R x y"
hoelzl@58587
   313
    fix S :: "'b \<Rightarrow> 'c \<Rightarrow> bool" and B assume "\<And>y z. (y, z) \<in> set_pmf B \<Longrightarrow> S y z"
hoelzl@58587
   314
    assume "?f B = ?s A"
hoelzl@58587
   315
    have "\<exists>C. (\<forall>(x, z)\<in>set_pmf C. \<exists>y. R x y \<and> S y z) \<and> ?f C = ?f A \<and> ?s C = ?s B"
hoelzl@58587
   316
      sorry }
hoelzl@58587
   317
oops
hoelzl@58587
   318
  then show "\<And>R::'a \<Rightarrow> 'b \<Rightarrow> bool. \<And>S::'b \<Rightarrow> 'c \<Rightarrow> bool. pmf_rel R OO pmf_rel S \<le> pmf_rel (R OO S)"
hoelzl@58587
   319
      by (auto simp add: subset_eq pmf_rel_def fun_eq_iff OO_def Ball_def)
hoelzl@58587
   320
qed (fact natLeq_card_order natLeq_cinfinite)+
hoelzl@58587
   321
hoelzl@58587
   322
notepad
hoelzl@58587
   323
begin
hoelzl@58587
   324
  fix x y :: "nat \<Rightarrow> real"
hoelzl@58587
   325
  def IJz \<equiv> "rec_nat ((0, 0), \<lambda>_. 0) (\<lambda>n ((I, J), z).
hoelzl@58587
   326
    let a = x I - (\<Sum>j<J. z (I, j)) ; b = y J - (\<Sum>i<I. z (i, J)) in
hoelzl@58587
   327
      ((if a \<le> b then I + 1 else I, if b \<le> a then J + 1 else J), z((I, J) := min a b)))"
hoelzl@58587
   328
  def I == "fst \<circ> fst \<circ> IJz" def J == "snd \<circ> fst \<circ> IJz" def z == "snd \<circ> IJz"
hoelzl@58587
   329
  let ?a = "\<lambda>n. x (I n) - (\<Sum>j<J n. z n (I n, j))" and ?b = "\<lambda>n. y (J n) - (\<Sum>i<I n. z n (i, J n))"
hoelzl@58587
   330
  have IJz_0[simp]: "\<And>p. z 0 p = 0" "I 0 = 0" "J 0 = 0"
hoelzl@58587
   331
    by (simp_all add: I_def J_def z_def IJz_def)
hoelzl@58587
   332
  have z_Suc[simp]: "\<And>n. z (Suc n) = (z n)((I n, J n) := min (?a n) (?b n))"
hoelzl@58587
   333
    by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
hoelzl@58587
   334
  have I_Suc[simp]: "\<And>n. I (Suc n) = (if ?a n \<le> ?b n then I n + 1 else I n)"
hoelzl@58587
   335
    by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
hoelzl@58587
   336
  have J_Suc[simp]: "\<And>n. J (Suc n) = (if ?b n \<le> ?a n then J n + 1 else J n)"
hoelzl@58587
   337
    by (simp add: z_def I_def J_def IJz_def Let_def split_beta)
hoelzl@58587
   338
  
hoelzl@58587
   339
  { fix N have "\<And>p. z N p \<noteq> 0 \<Longrightarrow> \<exists>n<N. p = (I n, J n)"
hoelzl@58587
   340
      by (induct N) (auto simp add: less_Suc_eq split: split_if_asm) }
hoelzl@58587
   341
  
hoelzl@58587
   342
  { fix i n assume "i < I n"
hoelzl@58587
   343
    then have "(\<Sum>j. z n (i, j)) = x i" 
hoelzl@58587
   344
    oops
hoelzl@58587
   345
*)
hoelzl@58587
   346
hoelzl@58587
   347
end
hoelzl@58587
   348