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