src/HOL/Probability/Nonnegative_Lebesgue_Integration.thy
author hoelzl
Tue May 20 19:24:39 2014 +0200 (2014-05-20)
changeset 57025 e7fd64f82876
parent 56996 891e992e510f
child 57137 f174712d0a84
permissions -rw-r--r--
add various lemmas
hoelzl@56993
     1
(*  Title:      HOL/Probability/Nonnegative_Lebesgue_Integration.thy
hoelzl@42067
     2
    Author:     Johannes Hölzl, TU München
hoelzl@42067
     3
    Author:     Armin Heller, TU München
hoelzl@42067
     4
*)
hoelzl@38656
     5
hoelzl@56993
     6
header {* Lebesgue Integration for Nonnegative Functions *}
hoelzl@35582
     7
hoelzl@56993
     8
theory Nonnegative_Lebesgue_Integration
hoelzl@47694
     9
  imports Measure_Space Borel_Space
hoelzl@35582
    10
begin
hoelzl@35582
    11
hoelzl@56949
    12
lemma indicator_less_ereal[simp]:
hoelzl@56949
    13
  "indicator A x \<le> (indicator B x::ereal) \<longleftrightarrow> (x \<in> A \<longrightarrow> x \<in> B)"
hoelzl@56949
    14
  by (simp add: indicator_def not_le)
hoelzl@41981
    15
hoelzl@56994
    16
subsection "Simple function"
hoelzl@35582
    17
hoelzl@38656
    18
text {*
hoelzl@38656
    19
hoelzl@56996
    20
Our simple functions are not restricted to nonnegative real numbers. Instead
hoelzl@38656
    21
they are just functions with a finite range and are measurable when singleton
hoelzl@38656
    22
sets are measurable.
hoelzl@35582
    23
hoelzl@38656
    24
*}
hoelzl@38656
    25
hoelzl@41689
    26
definition "simple_function M g \<longleftrightarrow>
hoelzl@38656
    27
    finite (g ` space M) \<and>
hoelzl@38656
    28
    (\<forall>x \<in> g ` space M. g -` {x} \<inter> space M \<in> sets M)"
hoelzl@36624
    29
hoelzl@47694
    30
lemma simple_functionD:
hoelzl@41689
    31
  assumes "simple_function M g"
hoelzl@40875
    32
  shows "finite (g ` space M)" and "g -` X \<inter> space M \<in> sets M"
hoelzl@40871
    33
proof -
hoelzl@40871
    34
  show "finite (g ` space M)"
hoelzl@40871
    35
    using assms unfolding simple_function_def by auto
hoelzl@40875
    36
  have "g -` X \<inter> space M = g -` (X \<inter> g`space M) \<inter> space M" by auto
hoelzl@40875
    37
  also have "\<dots> = (\<Union>x\<in>X \<inter> g`space M. g-`{x} \<inter> space M)" by auto
hoelzl@40875
    38
  finally show "g -` X \<inter> space M \<in> sets M" using assms
hoelzl@50002
    39
    by (auto simp del: UN_simps simp: simple_function_def)
hoelzl@40871
    40
qed
hoelzl@36624
    41
hoelzl@56949
    42
lemma measurable_simple_function[measurable_dest]:
hoelzl@56949
    43
  "simple_function M f \<Longrightarrow> f \<in> measurable M (count_space UNIV)"
hoelzl@56949
    44
  unfolding simple_function_def measurable_def
hoelzl@56949
    45
proof safe
hoelzl@56949
    46
  fix A assume "finite (f ` space M)" "\<forall>x\<in>f ` space M. f -` {x} \<inter> space M \<in> sets M"
hoelzl@56949
    47
  then have "(\<Union>x\<in>f ` space M. if x \<in> A then f -` {x} \<inter> space M else {}) \<in> sets M"
hoelzl@56949
    48
    by (intro sets.finite_UN) auto
hoelzl@56949
    49
  also have "(\<Union>x\<in>f ` space M. if x \<in> A then f -` {x} \<inter> space M else {}) = f -` A \<inter> space M"
hoelzl@56949
    50
    by (auto split: split_if_asm)
hoelzl@56949
    51
  finally show "f -` A \<inter> space M \<in> sets M" .
hoelzl@56949
    52
qed simp
hoelzl@56949
    53
hoelzl@56949
    54
lemma borel_measurable_simple_function:
hoelzl@56949
    55
  "simple_function M f \<Longrightarrow> f \<in> borel_measurable M"
hoelzl@56949
    56
  by (auto dest!: measurable_simple_function simp: measurable_def)
hoelzl@56949
    57
hoelzl@47694
    58
lemma simple_function_measurable2[intro]:
hoelzl@41981
    59
  assumes "simple_function M f" "simple_function M g"
hoelzl@41981
    60
  shows "f -` A \<inter> g -` B \<inter> space M \<in> sets M"
hoelzl@41981
    61
proof -
hoelzl@41981
    62
  have "f -` A \<inter> g -` B \<inter> space M = (f -` A \<inter> space M) \<inter> (g -` B \<inter> space M)"
hoelzl@41981
    63
    by auto
hoelzl@41981
    64
  then show ?thesis using assms[THEN simple_functionD(2)] by auto
hoelzl@41981
    65
qed
hoelzl@41981
    66
hoelzl@47694
    67
lemma simple_function_indicator_representation:
hoelzl@43920
    68
  fixes f ::"'a \<Rightarrow> ereal"
hoelzl@41689
    69
  assumes f: "simple_function M f" and x: "x \<in> space M"
hoelzl@38656
    70
  shows "f x = (\<Sum>y \<in> f ` space M. y * indicator (f -` {y} \<inter> space M) x)"
hoelzl@38656
    71
  (is "?l = ?r")
hoelzl@38656
    72
proof -
hoelzl@38705
    73
  have "?r = (\<Sum>y \<in> f ` space M.
hoelzl@38656
    74
    (if y = f x then y * indicator (f -` {y} \<inter> space M) x else 0))"
hoelzl@38656
    75
    by (auto intro!: setsum_cong2)
hoelzl@38656
    76
  also have "... =  f x *  indicator (f -` {f x} \<inter> space M) x"
hoelzl@38656
    77
    using assms by (auto dest: simple_functionD simp: setsum_delta)
hoelzl@38656
    78
  also have "... = f x" using x by (auto simp: indicator_def)
hoelzl@38656
    79
  finally show ?thesis by auto
hoelzl@38656
    80
qed
hoelzl@36624
    81
hoelzl@47694
    82
lemma simple_function_notspace:
hoelzl@43920
    83
  "simple_function M (\<lambda>x. h x * indicator (- space M) x::ereal)" (is "simple_function M ?h")
hoelzl@35692
    84
proof -
hoelzl@38656
    85
  have "?h ` space M \<subseteq> {0}" unfolding indicator_def by auto
hoelzl@38656
    86
  hence [simp, intro]: "finite (?h ` space M)" by (auto intro: finite_subset)
hoelzl@38656
    87
  have "?h -` {0} \<inter> space M = space M" by auto
hoelzl@38656
    88
  thus ?thesis unfolding simple_function_def by auto
hoelzl@38656
    89
qed
hoelzl@38656
    90
hoelzl@47694
    91
lemma simple_function_cong:
hoelzl@38656
    92
  assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
hoelzl@41689
    93
  shows "simple_function M f \<longleftrightarrow> simple_function M g"
hoelzl@38656
    94
proof -
hoelzl@38656
    95
  have "f ` space M = g ` space M"
hoelzl@38656
    96
    "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
hoelzl@38656
    97
    using assms by (auto intro!: image_eqI)
hoelzl@38656
    98
  thus ?thesis unfolding simple_function_def using assms by simp
hoelzl@38656
    99
qed
hoelzl@38656
   100
hoelzl@47694
   101
lemma simple_function_cong_algebra:
hoelzl@41689
   102
  assumes "sets N = sets M" "space N = space M"
hoelzl@41689
   103
  shows "simple_function M f \<longleftrightarrow> simple_function N f"
hoelzl@41689
   104
  unfolding simple_function_def assms ..
hoelzl@41689
   105
hoelzl@47694
   106
lemma simple_function_borel_measurable:
hoelzl@41981
   107
  fixes f :: "'a \<Rightarrow> 'x::{t2_space}"
hoelzl@38656
   108
  assumes "f \<in> borel_measurable M" and "finite (f ` space M)"
hoelzl@41689
   109
  shows "simple_function M f"
hoelzl@38656
   110
  using assms unfolding simple_function_def
hoelzl@38656
   111
  by (auto intro: borel_measurable_vimage)
hoelzl@38656
   112
hoelzl@56949
   113
lemma simple_function_eq_measurable:
hoelzl@43920
   114
  fixes f :: "'a \<Rightarrow> ereal"
hoelzl@56949
   115
  shows "simple_function M f \<longleftrightarrow> finite (f`space M) \<and> f \<in> measurable M (count_space UNIV)"
hoelzl@56949
   116
  using simple_function_borel_measurable[of f] measurable_simple_function[of M f]
nipkow@44890
   117
  by (fastforce simp: simple_function_def)
hoelzl@41981
   118
hoelzl@47694
   119
lemma simple_function_const[intro, simp]:
hoelzl@41689
   120
  "simple_function M (\<lambda>x. c)"
hoelzl@38656
   121
  by (auto intro: finite_subset simp: simple_function_def)
hoelzl@47694
   122
lemma simple_function_compose[intro, simp]:
hoelzl@41689
   123
  assumes "simple_function M f"
hoelzl@41689
   124
  shows "simple_function M (g \<circ> f)"
hoelzl@38656
   125
  unfolding simple_function_def
hoelzl@38656
   126
proof safe
hoelzl@38656
   127
  show "finite ((g \<circ> f) ` space M)"
haftmann@56154
   128
    using assms unfolding simple_function_def by (auto simp: image_comp [symmetric])
hoelzl@38656
   129
next
hoelzl@38656
   130
  fix x assume "x \<in> space M"
hoelzl@38656
   131
  let ?G = "g -` {g (f x)} \<inter> (f`space M)"
hoelzl@38656
   132
  have *: "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M =
hoelzl@38656
   133
    (\<Union>x\<in>?G. f -` {x} \<inter> space M)" by auto
hoelzl@38656
   134
  show "(g \<circ> f) -` {(g \<circ> f) x} \<inter> space M \<in> sets M"
hoelzl@38656
   135
    using assms unfolding simple_function_def *
immler@50244
   136
    by (rule_tac sets.finite_UN) auto
hoelzl@38656
   137
qed
hoelzl@38656
   138
hoelzl@47694
   139
lemma simple_function_indicator[intro, simp]:
hoelzl@38656
   140
  assumes "A \<in> sets M"
hoelzl@41689
   141
  shows "simple_function M (indicator A)"
hoelzl@35692
   142
proof -
hoelzl@38656
   143
  have "indicator A ` space M \<subseteq> {0, 1}" (is "?S \<subseteq> _")
hoelzl@38656
   144
    by (auto simp: indicator_def)
hoelzl@38656
   145
  hence "finite ?S" by (rule finite_subset) simp
hoelzl@38656
   146
  moreover have "- A \<inter> space M = space M - A" by auto
hoelzl@38656
   147
  ultimately show ?thesis unfolding simple_function_def
wenzelm@46905
   148
    using assms by (auto simp: indicator_def [abs_def])
hoelzl@35692
   149
qed
hoelzl@35692
   150
hoelzl@47694
   151
lemma simple_function_Pair[intro, simp]:
hoelzl@41689
   152
  assumes "simple_function M f"
hoelzl@41689
   153
  assumes "simple_function M g"
hoelzl@41689
   154
  shows "simple_function M (\<lambda>x. (f x, g x))" (is "simple_function M ?p")
hoelzl@38656
   155
  unfolding simple_function_def
hoelzl@38656
   156
proof safe
hoelzl@38656
   157
  show "finite (?p ` space M)"
hoelzl@38656
   158
    using assms unfolding simple_function_def
hoelzl@38656
   159
    by (rule_tac finite_subset[of _ "f`space M \<times> g`space M"]) auto
hoelzl@38656
   160
next
hoelzl@38656
   161
  fix x assume "x \<in> space M"
hoelzl@38656
   162
  have "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M =
hoelzl@38656
   163
      (f -` {f x} \<inter> space M) \<inter> (g -` {g x} \<inter> space M)"
hoelzl@38656
   164
    by auto
hoelzl@38656
   165
  with `x \<in> space M` show "(\<lambda>x. (f x, g x)) -` {(f x, g x)} \<inter> space M \<in> sets M"
hoelzl@38656
   166
    using assms unfolding simple_function_def by auto
hoelzl@38656
   167
qed
hoelzl@35692
   168
hoelzl@47694
   169
lemma simple_function_compose1:
hoelzl@41689
   170
  assumes "simple_function M f"
hoelzl@41689
   171
  shows "simple_function M (\<lambda>x. g (f x))"
hoelzl@38656
   172
  using simple_function_compose[OF assms, of g]
hoelzl@38656
   173
  by (simp add: comp_def)
hoelzl@35582
   174
hoelzl@47694
   175
lemma simple_function_compose2:
hoelzl@41689
   176
  assumes "simple_function M f" and "simple_function M g"
hoelzl@41689
   177
  shows "simple_function M (\<lambda>x. h (f x) (g x))"
hoelzl@38656
   178
proof -
hoelzl@41689
   179
  have "simple_function M ((\<lambda>(x, y). h x y) \<circ> (\<lambda>x. (f x, g x)))"
hoelzl@38656
   180
    using assms by auto
hoelzl@38656
   181
  thus ?thesis by (simp_all add: comp_def)
hoelzl@38656
   182
qed
hoelzl@35582
   183
hoelzl@47694
   184
lemmas simple_function_add[intro, simp] = simple_function_compose2[where h="op +"]
hoelzl@38656
   185
  and simple_function_diff[intro, simp] = simple_function_compose2[where h="op -"]
hoelzl@38656
   186
  and simple_function_uminus[intro, simp] = simple_function_compose[where g="uminus"]
hoelzl@38656
   187
  and simple_function_mult[intro, simp] = simple_function_compose2[where h="op *"]
hoelzl@38656
   188
  and simple_function_div[intro, simp] = simple_function_compose2[where h="op /"]
hoelzl@38656
   189
  and simple_function_inverse[intro, simp] = simple_function_compose[where g="inverse"]
hoelzl@41981
   190
  and simple_function_max[intro, simp] = simple_function_compose2[where h=max]
hoelzl@38656
   191
hoelzl@47694
   192
lemma simple_function_setsum[intro, simp]:
hoelzl@41689
   193
  assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
hoelzl@41689
   194
  shows "simple_function M (\<lambda>x. \<Sum>i\<in>P. f i x)"
hoelzl@38656
   195
proof cases
hoelzl@38656
   196
  assume "finite P" from this assms show ?thesis by induct auto
hoelzl@38656
   197
qed auto
hoelzl@35582
   198
hoelzl@56949
   199
lemma simple_function_ereal[intro, simp]: 
hoelzl@41981
   200
  fixes f g :: "'a \<Rightarrow> real" assumes sf: "simple_function M f"
hoelzl@56949
   201
  shows "simple_function M (\<lambda>x. ereal (f x))"
hoelzl@41981
   202
  by (auto intro!: simple_function_compose1[OF sf])
hoelzl@41981
   203
hoelzl@56949
   204
lemma simple_function_real_of_nat[intro, simp]: 
hoelzl@41981
   205
  fixes f g :: "'a \<Rightarrow> nat" assumes sf: "simple_function M f"
hoelzl@56949
   206
  shows "simple_function M (\<lambda>x. real (f x))"
hoelzl@41981
   207
  by (auto intro!: simple_function_compose1[OF sf])
hoelzl@35582
   208
hoelzl@47694
   209
lemma borel_measurable_implies_simple_function_sequence:
hoelzl@43920
   210
  fixes u :: "'a \<Rightarrow> ereal"
hoelzl@38656
   211
  assumes u: "u \<in> borel_measurable M"
hoelzl@41981
   212
  shows "\<exists>f. incseq f \<and> (\<forall>i. \<infinity> \<notin> range (f i) \<and> simple_function M (f i)) \<and>
hoelzl@41981
   213
             (\<forall>x. (SUP i. f i x) = max 0 (u x)) \<and> (\<forall>i x. 0 \<le> f i x)"
hoelzl@35582
   214
proof -
hoelzl@41981
   215
  def f \<equiv> "\<lambda>x i. if real i \<le> u x then i * 2 ^ i else natfloor (real (u x) * 2 ^ i)"
hoelzl@41981
   216
  { fix x j have "f x j \<le> j * 2 ^ j" unfolding f_def
hoelzl@41981
   217
    proof (split split_if, intro conjI impI)
hoelzl@41981
   218
      assume "\<not> real j \<le> u x"
hoelzl@41981
   219
      then have "natfloor (real (u x) * 2 ^ j) \<le> natfloor (j * 2 ^ j)"
nipkow@56536
   220
         by (cases "u x") (auto intro!: natfloor_mono)
hoelzl@41981
   221
      moreover have "real (natfloor (j * 2 ^ j)) \<le> j * 2^j"
nipkow@56536
   222
        by (intro real_natfloor_le) auto
hoelzl@41981
   223
      ultimately show "natfloor (real (u x) * 2 ^ j) \<le> j * 2 ^ j"
hoelzl@41981
   224
        unfolding real_of_nat_le_iff by auto
hoelzl@41981
   225
    qed auto }
hoelzl@38656
   226
  note f_upper = this
hoelzl@35582
   227
hoelzl@41981
   228
  have real_f:
hoelzl@41981
   229
    "\<And>i x. real (f x i) = (if real i \<le> u x then i * 2 ^ i else real (natfloor (real (u x) * 2 ^ i)))"
hoelzl@41981
   230
    unfolding f_def by auto
hoelzl@35582
   231
wenzelm@46731
   232
  let ?g = "\<lambda>j x. real (f x j) / 2^j :: ereal"
hoelzl@41981
   233
  show ?thesis
hoelzl@41981
   234
  proof (intro exI[of _ ?g] conjI allI ballI)
hoelzl@41981
   235
    fix i
hoelzl@41981
   236
    have "simple_function M (\<lambda>x. real (f x i))"
hoelzl@41981
   237
    proof (intro simple_function_borel_measurable)
hoelzl@41981
   238
      show "(\<lambda>x. real (f x i)) \<in> borel_measurable M"
hoelzl@50021
   239
        using u by (auto simp: real_f)
hoelzl@41981
   240
      have "(\<lambda>x. real (f x i))`space M \<subseteq> real`{..i*2^i}"
hoelzl@41981
   241
        using f_upper[of _ i] by auto
hoelzl@41981
   242
      then show "finite ((\<lambda>x. real (f x i))`space M)"
hoelzl@41981
   243
        by (rule finite_subset) auto
hoelzl@41981
   244
    qed
hoelzl@41981
   245
    then show "simple_function M (?g i)"
hoelzl@43920
   246
      by (auto intro: simple_function_ereal simple_function_div)
hoelzl@41981
   247
  next
hoelzl@41981
   248
    show "incseq ?g"
hoelzl@43920
   249
    proof (intro incseq_ereal incseq_SucI le_funI)
hoelzl@41981
   250
      fix x and i :: nat
hoelzl@41981
   251
      have "f x i * 2 \<le> f x (Suc i)" unfolding f_def
hoelzl@41981
   252
      proof ((split split_if)+, intro conjI impI)
hoelzl@43920
   253
        assume "ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
hoelzl@41981
   254
        then show "i * 2 ^ i * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)"
hoelzl@41981
   255
          by (cases "u x") (auto intro!: le_natfloor)
hoelzl@38656
   256
      next
hoelzl@43920
   257
        assume "\<not> ereal (real i) \<le> u x" "ereal (real (Suc i)) \<le> u x"
hoelzl@41981
   258
        then show "natfloor (real (u x) * 2 ^ i) * 2 \<le> Suc i * 2 ^ Suc i"
hoelzl@41981
   259
          by (cases "u x") auto
hoelzl@41981
   260
      next
hoelzl@43920
   261
        assume "\<not> ereal (real i) \<le> u x" "\<not> ereal (real (Suc i)) \<le> u x"
hoelzl@41981
   262
        have "natfloor (real (u x) * 2 ^ i) * 2 = natfloor (real (u x) * 2 ^ i) * natfloor 2"
hoelzl@41981
   263
          by simp
hoelzl@41981
   264
        also have "\<dots> \<le> natfloor (real (u x) * 2 ^ i * 2)"
hoelzl@41981
   265
        proof cases
hoelzl@41981
   266
          assume "0 \<le> u x" then show ?thesis
bulwahn@46671
   267
            by (intro le_mult_natfloor) 
hoelzl@41981
   268
        next
hoelzl@41981
   269
          assume "\<not> 0 \<le> u x" then show ?thesis
hoelzl@41981
   270
            by (cases "u x") (auto simp: natfloor_neg mult_nonpos_nonneg)
hoelzl@38656
   271
        qed
hoelzl@41981
   272
        also have "\<dots> = natfloor (real (u x) * 2 ^ Suc i)"
hoelzl@41981
   273
          by (simp add: ac_simps)
hoelzl@41981
   274
        finally show "natfloor (real (u x) * 2 ^ i) * 2 \<le> natfloor (real (u x) * 2 ^ Suc i)" .
hoelzl@41981
   275
      qed simp
hoelzl@41981
   276
      then show "?g i x \<le> ?g (Suc i) x"
hoelzl@41981
   277
        by (auto simp: field_simps)
hoelzl@35582
   278
    qed
hoelzl@38656
   279
  next
hoelzl@41981
   280
    fix x show "(SUP i. ?g i x) = max 0 (u x)"
hoelzl@51000
   281
    proof (rule SUP_eqI)
hoelzl@41981
   282
      fix i show "?g i x \<le> max 0 (u x)" unfolding max_def f_def
hoelzl@41981
   283
        by (cases "u x") (auto simp: field_simps real_natfloor_le natfloor_neg
nipkow@56536
   284
                                     mult_nonpos_nonneg)
hoelzl@41981
   285
    next
hoelzl@41981
   286
      fix y assume *: "\<And>i. i \<in> UNIV \<Longrightarrow> ?g i x \<le> y"
hoelzl@56571
   287
      have "\<And>i. 0 \<le> ?g i x" by auto
hoelzl@41981
   288
      from order_trans[OF this *] have "0 \<le> y" by simp
hoelzl@41981
   289
      show "max 0 (u x) \<le> y"
hoelzl@41981
   290
      proof (cases y)
hoelzl@41981
   291
        case (real r)
hoelzl@41981
   292
        with * have *: "\<And>i. f x i \<le> r * 2^i" by (auto simp: divide_le_eq)
huffman@44666
   293
        from reals_Archimedean2[of r] * have "u x \<noteq> \<infinity>" by (auto simp: f_def) (metis less_le_not_le)
hoelzl@43920
   294
        then have "\<exists>p. max 0 (u x) = ereal p \<and> 0 \<le> p" by (cases "u x") (auto simp: max_def)
hoelzl@41981
   295
        then guess p .. note ux = this
huffman@44666
   296
        obtain m :: nat where m: "p < real m" using reals_Archimedean2 ..
hoelzl@41981
   297
        have "p \<le> r"
hoelzl@41981
   298
        proof (rule ccontr)
hoelzl@41981
   299
          assume "\<not> p \<le> r"
hoelzl@41981
   300
          with LIMSEQ_inverse_realpow_zero[unfolded LIMSEQ_iff, rule_format, of 2 "p - r"]
nipkow@56536
   301
          obtain N where "\<forall>n\<ge>N. r * 2^n < p * 2^n - 1" by (auto simp: field_simps)
hoelzl@41981
   302
          then have "r * 2^max N m < p * 2^max N m - 1" by simp
hoelzl@41981
   303
          moreover
hoelzl@41981
   304
          have "real (natfloor (p * 2 ^ max N m)) \<le> r * 2 ^ max N m"
hoelzl@41981
   305
            using *[of "max N m"] m unfolding real_f using ux
nipkow@56536
   306
            by (cases "0 \<le> u x") (simp_all add: max_def split: split_if_asm)
hoelzl@41981
   307
          then have "p * 2 ^ max N m - 1 < r * 2 ^ max N m"
hoelzl@41981
   308
            by (metis real_natfloor_gt_diff_one less_le_trans)
hoelzl@41981
   309
          ultimately show False by auto
hoelzl@38656
   310
        qed
hoelzl@41981
   311
        then show "max 0 (u x) \<le> y" using real ux by simp
hoelzl@41981
   312
      qed (insert `0 \<le> y`, auto)
hoelzl@41981
   313
    qed
hoelzl@56571
   314
  qed auto
hoelzl@41981
   315
qed
hoelzl@35582
   316
hoelzl@47694
   317
lemma borel_measurable_implies_simple_function_sequence':
hoelzl@43920
   318
  fixes u :: "'a \<Rightarrow> ereal"
hoelzl@41981
   319
  assumes u: "u \<in> borel_measurable M"
hoelzl@41981
   320
  obtains f where "\<And>i. simple_function M (f i)" "incseq f" "\<And>i. \<infinity> \<notin> range (f i)"
hoelzl@41981
   321
    "\<And>x. (SUP i. f i x) = max 0 (u x)" "\<And>i x. 0 \<le> f i x"
hoelzl@41981
   322
  using borel_measurable_implies_simple_function_sequence[OF u] by auto
hoelzl@41981
   323
hoelzl@49796
   324
lemma simple_function_induct[consumes 1, case_names cong set mult add, induct set: simple_function]:
hoelzl@49796
   325
  fixes u :: "'a \<Rightarrow> ereal"
hoelzl@49796
   326
  assumes u: "simple_function M u"
hoelzl@49796
   327
  assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (AE x in M. f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
hoelzl@49796
   328
  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
hoelzl@49796
   329
  assumes mult: "\<And>u c. P u \<Longrightarrow> P (\<lambda>x. c * u x)"
hoelzl@49796
   330
  assumes add: "\<And>u v. P u \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
hoelzl@49796
   331
  shows "P u"
hoelzl@49796
   332
proof (rule cong)
hoelzl@49796
   333
  from AE_space show "AE x in M. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x"
hoelzl@49796
   334
  proof eventually_elim
hoelzl@49796
   335
    fix x assume x: "x \<in> space M"
hoelzl@49796
   336
    from simple_function_indicator_representation[OF u x]
hoelzl@49796
   337
    show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
hoelzl@49796
   338
  qed
hoelzl@49796
   339
next
hoelzl@49796
   340
  from u have "finite (u ` space M)"
hoelzl@49796
   341
    unfolding simple_function_def by auto
hoelzl@49796
   342
  then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
hoelzl@49796
   343
  proof induct
hoelzl@49796
   344
    case empty show ?case
hoelzl@49796
   345
      using set[of "{}"] by (simp add: indicator_def[abs_def])
hoelzl@49796
   346
  qed (auto intro!: add mult set simple_functionD u)
hoelzl@49796
   347
next
hoelzl@49796
   348
  show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
hoelzl@49796
   349
    apply (subst simple_function_cong)
hoelzl@49796
   350
    apply (rule simple_function_indicator_representation[symmetric])
hoelzl@49796
   351
    apply (auto intro: u)
hoelzl@49796
   352
    done
hoelzl@49796
   353
qed fact
hoelzl@49796
   354
hoelzl@49796
   355
lemma simple_function_induct_nn[consumes 2, case_names cong set mult add]:
hoelzl@49796
   356
  fixes u :: "'a \<Rightarrow> ereal"
hoelzl@49799
   357
  assumes u: "simple_function M u" and nn: "\<And>x. 0 \<le> u x"
hoelzl@49799
   358
  assumes cong: "\<And>f g. simple_function M f \<Longrightarrow> simple_function M g \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P f \<Longrightarrow> P g"
hoelzl@49796
   359
  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
hoelzl@49797
   360
  assumes mult: "\<And>u c. 0 \<le> c \<Longrightarrow> simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
hoelzl@56993
   361
  assumes add: "\<And>u v. simple_function M u \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> P u \<Longrightarrow> simple_function M v \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x = 0 \<or> v x = 0) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
hoelzl@49796
   362
  shows "P u"
hoelzl@49796
   363
proof -
hoelzl@49796
   364
  show ?thesis
hoelzl@49796
   365
  proof (rule cong)
hoelzl@49799
   366
    fix x assume x: "x \<in> space M"
hoelzl@49799
   367
    from simple_function_indicator_representation[OF u x]
hoelzl@49799
   368
    show "(\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x) = u x" ..
hoelzl@49796
   369
  next
hoelzl@49799
   370
    show "simple_function M (\<lambda>x. (\<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x))"
hoelzl@49796
   371
      apply (subst simple_function_cong)
hoelzl@49796
   372
      apply (rule simple_function_indicator_representation[symmetric])
hoelzl@49799
   373
      apply (auto intro: u)
hoelzl@49796
   374
      done
hoelzl@49796
   375
  next
hoelzl@56993
   376
    
hoelzl@49799
   377
    from u nn have "finite (u ` space M)" "\<And>x. x \<in> u ` space M \<Longrightarrow> 0 \<le> x"
hoelzl@49796
   378
      unfolding simple_function_def by auto
hoelzl@49799
   379
    then show "P (\<lambda>x. \<Sum>y\<in>u ` space M. y * indicator (u -` {y} \<inter> space M) x)"
hoelzl@49796
   380
    proof induct
hoelzl@49796
   381
      case empty show ?case
hoelzl@49796
   382
        using set[of "{}"] by (simp add: indicator_def[abs_def])
hoelzl@56993
   383
    next
hoelzl@56993
   384
      case (insert x S)
hoelzl@56993
   385
      { fix z have "(\<Sum>y\<in>S. y * indicator (u -` {y} \<inter> space M) z) = 0 \<or>
hoelzl@56993
   386
          x * indicator (u -` {x} \<inter> space M) z = 0"
hoelzl@56993
   387
          using insert by (subst setsum_ereal_0) (auto simp: indicator_def) }
hoelzl@56993
   388
      note disj = this
hoelzl@56993
   389
      from insert show ?case
hoelzl@56993
   390
        by (auto intro!: add mult set simple_functionD u setsum_nonneg simple_function_setsum disj)
hoelzl@56993
   391
    qed
hoelzl@49796
   392
  qed fact
hoelzl@49796
   393
qed
hoelzl@49796
   394
hoelzl@49796
   395
lemma borel_measurable_induct[consumes 2, case_names cong set mult add seq, induct set: borel_measurable]:
hoelzl@49796
   396
  fixes u :: "'a \<Rightarrow> ereal"
hoelzl@49799
   397
  assumes u: "u \<in> borel_measurable M" "\<And>x. 0 \<le> u x"
hoelzl@49799
   398
  assumes cong: "\<And>f g. f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> f x = g x) \<Longrightarrow> P g \<Longrightarrow> P f"
hoelzl@49796
   399
  assumes set: "\<And>A. A \<in> sets M \<Longrightarrow> P (indicator A)"
hoelzl@56993
   400
  assumes mult': "\<And>u c. 0 \<le> c \<Longrightarrow> c < \<infinity> \<Longrightarrow> u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x < \<infinity>) \<Longrightarrow> P u \<Longrightarrow> P (\<lambda>x. c * u x)"
hoelzl@56993
   401
  assumes add: "\<And>u v. u \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> u x) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x < \<infinity>) \<Longrightarrow> P u \<Longrightarrow> v \<in> borel_measurable M \<Longrightarrow> (\<And>x. 0 \<le> v x) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> v x < \<infinity>) \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x = 0 \<or> v x = 0) \<Longrightarrow> P v \<Longrightarrow> P (\<lambda>x. v x + u x)"
hoelzl@56993
   402
  assumes seq: "\<And>U. (\<And>i. U i \<in> borel_measurable M) \<Longrightarrow> (\<And>i x. 0 \<le> U i x) \<Longrightarrow> (\<And>i x. x \<in> space M \<Longrightarrow> U i x < \<infinity>) \<Longrightarrow>  (\<And>i. P (U i)) \<Longrightarrow> incseq U \<Longrightarrow> u = (SUP i. U i) \<Longrightarrow> P (SUP i. U i)"
hoelzl@49796
   403
  shows "P u"
hoelzl@49796
   404
  using u
hoelzl@49796
   405
proof (induct rule: borel_measurable_implies_simple_function_sequence')
hoelzl@49797
   406
  fix U assume U: "\<And>i. simple_function M (U i)" "incseq U" "\<And>i. \<infinity> \<notin> range (U i)" and
hoelzl@49796
   407
    sup: "\<And>x. (SUP i. U i x) = max 0 (u x)" and nn: "\<And>i x. 0 \<le> U i x"
hoelzl@49799
   408
  have u_eq: "u = (SUP i. U i)"
hoelzl@49796
   409
    using nn u sup by (auto simp: max_def)
hoelzl@56993
   410
hoelzl@56993
   411
  have not_inf: "\<And>x i. x \<in> space M \<Longrightarrow> U i x < \<infinity>"
hoelzl@56993
   412
    using U by (auto simp: image_iff eq_commute)
hoelzl@49796
   413
  
hoelzl@49797
   414
  from U have "\<And>i. U i \<in> borel_measurable M"
hoelzl@49797
   415
    by (simp add: borel_measurable_simple_function)
hoelzl@49797
   416
hoelzl@49799
   417
  show "P u"
hoelzl@49796
   418
    unfolding u_eq
hoelzl@49796
   419
  proof (rule seq)
hoelzl@49796
   420
    fix i show "P (U i)"
hoelzl@56993
   421
      using `simple_function M (U i)` nn[of i] not_inf[of _ i]
hoelzl@56993
   422
    proof (induct rule: simple_function_induct_nn)
hoelzl@56993
   423
      case (mult u c)
hoelzl@56993
   424
      show ?case
hoelzl@56993
   425
      proof cases
hoelzl@56993
   426
        assume "c = 0 \<or> space M = {} \<or> (\<forall>x\<in>space M. u x = 0)"
hoelzl@56993
   427
        with mult(2) show ?thesis
hoelzl@56993
   428
          by (intro cong[of "\<lambda>x. c * u x" "indicator {}"] set)
hoelzl@56993
   429
             (auto dest!: borel_measurable_simple_function)
hoelzl@56993
   430
      next
hoelzl@56993
   431
        assume "\<not> (c = 0 \<or> space M = {} \<or> (\<forall>x\<in>space M. u x = 0))"
hoelzl@56993
   432
        with mult obtain x where u_fin: "\<And>x. x \<in> space M \<Longrightarrow> u x < \<infinity>"
hoelzl@56993
   433
          and x: "x \<in> space M" "u x \<noteq> 0" "c \<noteq> 0"
hoelzl@56993
   434
          by auto
hoelzl@56993
   435
        with mult have "P u"
hoelzl@56993
   436
          by auto
hoelzl@56993
   437
        from x mult(5)[OF `x \<in> space M`] mult(1) mult(3)[of x] have "c < \<infinity>"
hoelzl@56993
   438
          by auto
hoelzl@56993
   439
        with u_fin mult
hoelzl@56993
   440
        show ?thesis
hoelzl@56993
   441
          by (intro mult') (auto dest!: borel_measurable_simple_function)
hoelzl@56993
   442
      qed
hoelzl@56993
   443
    qed (auto intro: cong intro!: set add dest!: borel_measurable_simple_function)
hoelzl@49797
   444
  qed fact+
hoelzl@49796
   445
qed
hoelzl@49796
   446
hoelzl@47694
   447
lemma simple_function_If_set:
hoelzl@41981
   448
  assumes sf: "simple_function M f" "simple_function M g" and A: "A \<inter> space M \<in> sets M"
hoelzl@41981
   449
  shows "simple_function M (\<lambda>x. if x \<in> A then f x else g x)" (is "simple_function M ?IF")
hoelzl@41981
   450
proof -
hoelzl@41981
   451
  def F \<equiv> "\<lambda>x. f -` {x} \<inter> space M" and G \<equiv> "\<lambda>x. g -` {x} \<inter> space M"
hoelzl@41981
   452
  show ?thesis unfolding simple_function_def
hoelzl@41981
   453
  proof safe
hoelzl@41981
   454
    have "?IF ` space M \<subseteq> f ` space M \<union> g ` space M" by auto
hoelzl@41981
   455
    from finite_subset[OF this] assms
hoelzl@41981
   456
    show "finite (?IF ` space M)" unfolding simple_function_def by auto
hoelzl@41981
   457
  next
hoelzl@41981
   458
    fix x assume "x \<in> space M"
hoelzl@41981
   459
    then have *: "?IF -` {?IF x} \<inter> space M = (if x \<in> A
hoelzl@41981
   460
      then ((F (f x) \<inter> (A \<inter> space M)) \<union> (G (f x) - (G (f x) \<inter> (A \<inter> space M))))
hoelzl@41981
   461
      else ((F (g x) \<inter> (A \<inter> space M)) \<union> (G (g x) - (G (g x) \<inter> (A \<inter> space M)))))"
immler@50244
   462
      using sets.sets_into_space[OF A] by (auto split: split_if_asm simp: G_def F_def)
hoelzl@41981
   463
    have [intro]: "\<And>x. F x \<in> sets M" "\<And>x. G x \<in> sets M"
hoelzl@41981
   464
      unfolding F_def G_def using sf[THEN simple_functionD(2)] by auto
hoelzl@41981
   465
    show "?IF -` {?IF x} \<inter> space M \<in> sets M" unfolding * using A by auto
hoelzl@35582
   466
  qed
hoelzl@35582
   467
qed
hoelzl@35582
   468
hoelzl@47694
   469
lemma simple_function_If:
hoelzl@41981
   470
  assumes sf: "simple_function M f" "simple_function M g" and P: "{x\<in>space M. P x} \<in> sets M"
hoelzl@41981
   471
  shows "simple_function M (\<lambda>x. if P x then f x else g x)"
hoelzl@35582
   472
proof -
hoelzl@41981
   473
  have "{x\<in>space M. P x} = {x. P x} \<inter> space M" by auto
hoelzl@41981
   474
  with simple_function_If_set[OF sf, of "{x. P x}"] P show ?thesis by simp
hoelzl@38656
   475
qed
hoelzl@38656
   476
hoelzl@47694
   477
lemma simple_function_subalgebra:
hoelzl@41689
   478
  assumes "simple_function N f"
hoelzl@41689
   479
  and N_subalgebra: "sets N \<subseteq> sets M" "space N = space M"
hoelzl@41689
   480
  shows "simple_function M f"
hoelzl@41689
   481
  using assms unfolding simple_function_def by auto
hoelzl@39092
   482
hoelzl@47694
   483
lemma simple_function_comp:
hoelzl@47694
   484
  assumes T: "T \<in> measurable M M'"
hoelzl@41689
   485
    and f: "simple_function M' f"
hoelzl@41689
   486
  shows "simple_function M (\<lambda>x. f (T x))"
hoelzl@41661
   487
proof (intro simple_function_def[THEN iffD2] conjI ballI)
hoelzl@41661
   488
  have "(\<lambda>x. f (T x)) ` space M \<subseteq> f ` space M'"
hoelzl@41661
   489
    using T unfolding measurable_def by auto
hoelzl@41661
   490
  then show "finite ((\<lambda>x. f (T x)) ` space M)"
hoelzl@41689
   491
    using f unfolding simple_function_def by (auto intro: finite_subset)
hoelzl@41661
   492
  fix i assume i: "i \<in> (\<lambda>x. f (T x)) ` space M"
hoelzl@41661
   493
  then have "i \<in> f ` space M'"
hoelzl@41661
   494
    using T unfolding measurable_def by auto
hoelzl@41661
   495
  then have "f -` {i} \<inter> space M' \<in> sets M'"
hoelzl@41689
   496
    using f unfolding simple_function_def by auto
hoelzl@41661
   497
  then have "T -` (f -` {i} \<inter> space M') \<inter> space M \<in> sets M"
hoelzl@41661
   498
    using T unfolding measurable_def by auto
hoelzl@41661
   499
  also have "T -` (f -` {i} \<inter> space M') \<inter> space M = (\<lambda>x. f (T x)) -` {i} \<inter> space M"
hoelzl@41661
   500
    using T unfolding measurable_def by auto
hoelzl@41661
   501
  finally show "(\<lambda>x. f (T x)) -` {i} \<inter> space M \<in> sets M" .
hoelzl@40859
   502
qed
hoelzl@40859
   503
hoelzl@56994
   504
subsection "Simple integral"
hoelzl@38656
   505
wenzelm@53015
   506
definition simple_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^sup>S") where
wenzelm@53015
   507
  "integral\<^sup>S M f = (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M))"
hoelzl@41689
   508
hoelzl@41689
   509
syntax
wenzelm@53015
   510
  "_simple_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^sup>S _. _ \<partial>_" [60,61] 110)
hoelzl@41689
   511
hoelzl@41689
   512
translations
wenzelm@53015
   513
  "\<integral>\<^sup>S x. f \<partial>M" == "CONST simple_integral M (%x. f)"
hoelzl@35582
   514
hoelzl@47694
   515
lemma simple_integral_cong:
hoelzl@38656
   516
  assumes "\<And>t. t \<in> space M \<Longrightarrow> f t = g t"
wenzelm@53015
   517
  shows "integral\<^sup>S M f = integral\<^sup>S M g"
hoelzl@38656
   518
proof -
hoelzl@38656
   519
  have "f ` space M = g ` space M"
hoelzl@38656
   520
    "\<And>x. f -` {x} \<inter> space M = g -` {x} \<inter> space M"
hoelzl@38656
   521
    using assms by (auto intro!: image_eqI)
hoelzl@38656
   522
  thus ?thesis unfolding simple_integral_def by simp
hoelzl@38656
   523
qed
hoelzl@38656
   524
hoelzl@47694
   525
lemma simple_integral_const[simp]:
wenzelm@53015
   526
  "(\<integral>\<^sup>Sx. c \<partial>M) = c * (emeasure M) (space M)"
hoelzl@38656
   527
proof (cases "space M = {}")
hoelzl@38656
   528
  case True thus ?thesis unfolding simple_integral_def by simp
hoelzl@38656
   529
next
hoelzl@38656
   530
  case False hence "(\<lambda>x. c) ` space M = {c}" by auto
hoelzl@38656
   531
  thus ?thesis unfolding simple_integral_def by simp
hoelzl@35582
   532
qed
hoelzl@35582
   533
hoelzl@47694
   534
lemma simple_function_partition:
hoelzl@41981
   535
  assumes f: "simple_function M f" and g: "simple_function M g"
hoelzl@56949
   536
  assumes sub: "\<And>x y. x \<in> space M \<Longrightarrow> y \<in> space M \<Longrightarrow> g x = g y \<Longrightarrow> f x = f y"
hoelzl@56949
   537
  assumes v: "\<And>x. x \<in> space M \<Longrightarrow> f x = v (g x)"
hoelzl@56949
   538
  shows "integral\<^sup>S M f = (\<Sum>y\<in>g ` space M. v y * emeasure M {x\<in>space M. g x = y})"
hoelzl@56949
   539
    (is "_ = ?r")
hoelzl@56949
   540
proof -
hoelzl@56949
   541
  from f g have [simp]: "finite (f`space M)" "finite (g`space M)"
hoelzl@56949
   542
    by (auto simp: simple_function_def)
hoelzl@56949
   543
  from f g have [measurable]: "f \<in> measurable M (count_space UNIV)" "g \<in> measurable M (count_space UNIV)"
hoelzl@56949
   544
    by (auto intro: measurable_simple_function)
hoelzl@35582
   545
hoelzl@56949
   546
  { fix y assume "y \<in> space M"
hoelzl@56949
   547
    then have "f ` space M \<inter> {i. \<exists>x\<in>space M. i = f x \<and> g y = g x} = {v (g y)}"
hoelzl@56949
   548
      by (auto cong: sub simp: v[symmetric]) }
hoelzl@56949
   549
  note eq = this
hoelzl@35582
   550
hoelzl@56949
   551
  have "integral\<^sup>S M f =
hoelzl@56949
   552
    (\<Sum>y\<in>f`space M. y * (\<Sum>z\<in>g`space M. 
hoelzl@56949
   553
      if \<exists>x\<in>space M. y = f x \<and> z = g x then emeasure M {x\<in>space M. g x = z} else 0))"
hoelzl@56949
   554
    unfolding simple_integral_def
hoelzl@56949
   555
  proof (safe intro!: setsum_cong ereal_left_mult_cong)
hoelzl@56949
   556
    fix y assume y: "y \<in> space M" "f y \<noteq> 0"
hoelzl@56949
   557
    have [simp]: "g ` space M \<inter> {z. \<exists>x\<in>space M. f y = f x \<and> z = g x} = 
hoelzl@56949
   558
        {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
hoelzl@56949
   559
      by auto
hoelzl@56949
   560
    have eq:"(\<Union>i\<in>{z. \<exists>x\<in>space M. f y = f x \<and> z = g x}. {x \<in> space M. g x = i}) =
hoelzl@56949
   561
        f -` {f y} \<inter> space M"
hoelzl@56949
   562
      by (auto simp: eq_commute cong: sub rev_conj_cong)
hoelzl@56949
   563
    have "finite (g`space M)" by simp
hoelzl@56949
   564
    then have "finite {z. \<exists>x\<in>space M. f y = f x \<and> z = g x}"
hoelzl@56949
   565
      by (rule rev_finite_subset) auto
hoelzl@56949
   566
    then show "emeasure M (f -` {f y} \<inter> space M) =
hoelzl@56949
   567
      (\<Sum>z\<in>g ` space M. if \<exists>x\<in>space M. f y = f x \<and> z = g x then emeasure M {x \<in> space M. g x = z} else 0)"
hoelzl@56949
   568
      apply (simp add: setsum_cases)
hoelzl@56949
   569
      apply (subst setsum_emeasure)
hoelzl@56949
   570
      apply (auto simp: disjoint_family_on_def eq)
hoelzl@56949
   571
      done
hoelzl@38656
   572
  qed
hoelzl@56949
   573
  also have "\<dots> = (\<Sum>y\<in>f`space M. (\<Sum>z\<in>g`space M. 
hoelzl@56949
   574
      if \<exists>x\<in>space M. y = f x \<and> z = g x then y * emeasure M {x\<in>space M. g x = z} else 0))"
hoelzl@56949
   575
    by (auto intro!: setsum_cong simp: setsum_ereal_right_distrib emeasure_nonneg)
hoelzl@56949
   576
  also have "\<dots> = ?r"
hoelzl@56949
   577
    by (subst setsum_commute)
hoelzl@56949
   578
       (auto intro!: setsum_cong simp: setsum_cases scaleR_setsum_right[symmetric] eq)
hoelzl@56949
   579
  finally show "integral\<^sup>S M f = ?r" .
hoelzl@35582
   580
qed
hoelzl@35582
   581
hoelzl@47694
   582
lemma simple_integral_add[simp]:
hoelzl@41981
   583
  assumes f: "simple_function M f" and "\<And>x. 0 \<le> f x" and g: "simple_function M g" and "\<And>x. 0 \<le> g x"
wenzelm@53015
   584
  shows "(\<integral>\<^sup>Sx. f x + g x \<partial>M) = integral\<^sup>S M f + integral\<^sup>S M g"
hoelzl@35582
   585
proof -
hoelzl@56949
   586
  have "(\<integral>\<^sup>Sx. f x + g x \<partial>M) =
hoelzl@56949
   587
    (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. (fst y + snd y) * emeasure M {x\<in>space M. (f x, g x) = y})"
hoelzl@56949
   588
    by (intro simple_function_partition) (auto intro: f g)
hoelzl@56949
   589
  also have "\<dots> = (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. fst y * emeasure M {x\<in>space M. (f x, g x) = y}) +
hoelzl@56949
   590
    (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. snd y * emeasure M {x\<in>space M. (f x, g x) = y})"
hoelzl@56949
   591
    using assms(2,4) by (auto intro!: setsum_cong ereal_left_distrib simp: setsum_addf[symmetric])
hoelzl@56949
   592
  also have "(\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. fst y * emeasure M {x\<in>space M. (f x, g x) = y}) = (\<integral>\<^sup>Sx. f x \<partial>M)"
hoelzl@56949
   593
    by (intro simple_function_partition[symmetric]) (auto intro: f g)
hoelzl@56949
   594
  also have "(\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. snd y * emeasure M {x\<in>space M. (f x, g x) = y}) = (\<integral>\<^sup>Sx. g x \<partial>M)"
hoelzl@56949
   595
    by (intro simple_function_partition[symmetric]) (auto intro: f g)
hoelzl@56949
   596
  finally show ?thesis .
hoelzl@35582
   597
qed
hoelzl@35582
   598
hoelzl@47694
   599
lemma simple_integral_setsum[simp]:
hoelzl@41981
   600
  assumes "\<And>i x. i \<in> P \<Longrightarrow> 0 \<le> f i x"
hoelzl@41689
   601
  assumes "\<And>i. i \<in> P \<Longrightarrow> simple_function M (f i)"
wenzelm@53015
   602
  shows "(\<integral>\<^sup>Sx. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^sup>S M (f i))"
hoelzl@38656
   603
proof cases
hoelzl@38656
   604
  assume "finite P"
hoelzl@38656
   605
  from this assms show ?thesis
hoelzl@41981
   606
    by induct (auto simp: simple_function_setsum simple_integral_add setsum_nonneg)
hoelzl@38656
   607
qed auto
hoelzl@38656
   608
hoelzl@47694
   609
lemma simple_integral_mult[simp]:
hoelzl@41981
   610
  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x"
wenzelm@53015
   611
  shows "(\<integral>\<^sup>Sx. c * f x \<partial>M) = c * integral\<^sup>S M f"
hoelzl@38656
   612
proof -
hoelzl@56949
   613
  have "(\<integral>\<^sup>Sx. c * f x \<partial>M) = (\<Sum>y\<in>f ` space M. (c * y) * emeasure M {x\<in>space M. f x = y})"
hoelzl@56949
   614
    using f by (intro simple_function_partition) auto
hoelzl@56949
   615
  also have "\<dots> = c * integral\<^sup>S M f"
hoelzl@56949
   616
    using f unfolding simple_integral_def
hoelzl@56949
   617
    by (subst setsum_ereal_right_distrib) (auto simp: emeasure_nonneg mult_assoc Int_def conj_commute)
hoelzl@56949
   618
  finally show ?thesis .
hoelzl@40871
   619
qed
hoelzl@40871
   620
hoelzl@47694
   621
lemma simple_integral_mono_AE:
hoelzl@56949
   622
  assumes f[measurable]: "simple_function M f" and g[measurable]: "simple_function M g"
hoelzl@47694
   623
  and mono: "AE x in M. f x \<le> g x"
wenzelm@53015
   624
  shows "integral\<^sup>S M f \<le> integral\<^sup>S M g"
hoelzl@40859
   625
proof -
hoelzl@56949
   626
  let ?\<mu> = "\<lambda>P. emeasure M {x\<in>space M. P x}"
hoelzl@56949
   627
  have "integral\<^sup>S M f = (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. fst y * ?\<mu> (\<lambda>x. (f x, g x) = y))"
hoelzl@56949
   628
    using f g by (intro simple_function_partition) auto
hoelzl@56949
   629
  also have "\<dots> \<le> (\<Sum>y\<in>(\<lambda>x. (f x, g x))`space M. snd y * ?\<mu> (\<lambda>x. (f x, g x) = y))"
hoelzl@56949
   630
  proof (clarsimp intro!: setsum_mono)
hoelzl@40859
   631
    fix x assume "x \<in> space M"
hoelzl@56949
   632
    let ?M = "?\<mu> (\<lambda>y. f y = f x \<and> g y = g x)"
hoelzl@56949
   633
    show "f x * ?M \<le> g x * ?M"
hoelzl@56949
   634
    proof cases
hoelzl@56949
   635
      assume "?M \<noteq> 0"
hoelzl@56949
   636
      then have "0 < ?M"
hoelzl@56949
   637
        by (simp add: less_le emeasure_nonneg)
hoelzl@56949
   638
      also have "\<dots> \<le> ?\<mu> (\<lambda>y. f x \<le> g x)"
hoelzl@56949
   639
        using mono by (intro emeasure_mono_AE) auto
hoelzl@56949
   640
      finally have "\<not> \<not> f x \<le> g x"
hoelzl@56949
   641
        by (intro notI) auto
hoelzl@56949
   642
      then show ?thesis
hoelzl@56949
   643
        by (intro ereal_mult_right_mono) auto
hoelzl@56949
   644
    qed simp
hoelzl@40859
   645
  qed
hoelzl@56949
   646
  also have "\<dots> = integral\<^sup>S M g"
hoelzl@56949
   647
    using f g by (intro simple_function_partition[symmetric]) auto
hoelzl@56949
   648
  finally show ?thesis .
hoelzl@40859
   649
qed
hoelzl@40859
   650
hoelzl@47694
   651
lemma simple_integral_mono:
hoelzl@41689
   652
  assumes "simple_function M f" and "simple_function M g"
hoelzl@38656
   653
  and mono: "\<And> x. x \<in> space M \<Longrightarrow> f x \<le> g x"
wenzelm@53015
   654
  shows "integral\<^sup>S M f \<le> integral\<^sup>S M g"
hoelzl@41705
   655
  using assms by (intro simple_integral_mono_AE) auto
hoelzl@35582
   656
hoelzl@47694
   657
lemma simple_integral_cong_AE:
hoelzl@41981
   658
  assumes "simple_function M f" and "simple_function M g"
hoelzl@47694
   659
  and "AE x in M. f x = g x"
wenzelm@53015
   660
  shows "integral\<^sup>S M f = integral\<^sup>S M g"
hoelzl@40859
   661
  using assms by (auto simp: eq_iff intro!: simple_integral_mono_AE)
hoelzl@40859
   662
hoelzl@47694
   663
lemma simple_integral_cong':
hoelzl@41689
   664
  assumes sf: "simple_function M f" "simple_function M g"
hoelzl@47694
   665
  and mea: "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0"
wenzelm@53015
   666
  shows "integral\<^sup>S M f = integral\<^sup>S M g"
hoelzl@40859
   667
proof (intro simple_integral_cong_AE sf AE_I)
hoelzl@47694
   668
  show "(emeasure M) {x\<in>space M. f x \<noteq> g x} = 0" by fact
hoelzl@40859
   669
  show "{x \<in> space M. f x \<noteq> g x} \<in> sets M"
hoelzl@40859
   670
    using sf[THEN borel_measurable_simple_function] by auto
hoelzl@40859
   671
qed simp
hoelzl@40859
   672
hoelzl@47694
   673
lemma simple_integral_indicator:
hoelzl@56949
   674
  assumes A: "A \<in> sets M"
hoelzl@49796
   675
  assumes f: "simple_function M f"
wenzelm@53015
   676
  shows "(\<integral>\<^sup>Sx. f x * indicator A x \<partial>M) =
hoelzl@56949
   677
    (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M \<inter> A))"
hoelzl@56949
   678
proof -
hoelzl@56949
   679
  have eq: "(\<lambda>x. (f x, indicator A x)) ` space M \<inter> {x. snd x = 1} = (\<lambda>x. (f x, 1::ereal))`A"
hoelzl@56949
   680
    using A[THEN sets.sets_into_space] by (auto simp: indicator_def image_iff split: split_if_asm)
hoelzl@56949
   681
  have eq2: "\<And>x. f x \<notin> f ` A \<Longrightarrow> f -` {f x} \<inter> space M \<inter> A = {}"
hoelzl@56949
   682
    by (auto simp: image_iff)
hoelzl@56949
   683
hoelzl@56949
   684
  have "(\<integral>\<^sup>Sx. f x * indicator A x \<partial>M) =
hoelzl@56949
   685
    (\<Sum>y\<in>(\<lambda>x. (f x, indicator A x))`space M. (fst y * snd y) * emeasure M {x\<in>space M. (f x, indicator A x) = y})"
hoelzl@56949
   686
    using assms by (intro simple_function_partition) auto
hoelzl@56949
   687
  also have "\<dots> = (\<Sum>y\<in>(\<lambda>x. (f x, indicator A x::ereal))`space M.
hoelzl@56949
   688
    if snd y = 1 then fst y * emeasure M (f -` {fst y} \<inter> space M \<inter> A) else 0)"
hoelzl@56949
   689
    by (auto simp: indicator_def split: split_if_asm intro!: arg_cong2[where f="op *"] arg_cong2[where f=emeasure] setsum_cong)
hoelzl@56949
   690
  also have "\<dots> = (\<Sum>y\<in>(\<lambda>x. (f x, 1::ereal))`A. fst y * emeasure M (f -` {fst y} \<inter> space M \<inter> A))"
hoelzl@56949
   691
    using assms by (subst setsum_cases) (auto intro!: simple_functionD(1) simp: eq)
hoelzl@56949
   692
  also have "\<dots> = (\<Sum>y\<in>fst`(\<lambda>x. (f x, 1::ereal))`A. y * emeasure M (f -` {y} \<inter> space M \<inter> A))"
hoelzl@56949
   693
    by (subst setsum_reindex[where f=fst]) (auto simp: inj_on_def)
hoelzl@56949
   694
  also have "\<dots> = (\<Sum>x \<in> f ` space M. x * emeasure M (f -` {x} \<inter> space M \<inter> A))"
hoelzl@56949
   695
    using A[THEN sets.sets_into_space]
hoelzl@56949
   696
    by (intro setsum_mono_zero_cong_left simple_functionD f) (auto simp: image_comp comp_def eq2)
hoelzl@56949
   697
  finally show ?thesis .
hoelzl@38656
   698
qed
hoelzl@35582
   699
hoelzl@47694
   700
lemma simple_integral_indicator_only[simp]:
hoelzl@38656
   701
  assumes "A \<in> sets M"
wenzelm@53015
   702
  shows "integral\<^sup>S M (indicator A) = emeasure M A"
hoelzl@56949
   703
  using simple_integral_indicator[OF assms, of "\<lambda>x. 1"] sets.sets_into_space[OF assms]
hoelzl@56949
   704
  by (simp_all add: image_constant_conv Int_absorb1 split: split_if_asm)
hoelzl@35582
   705
hoelzl@47694
   706
lemma simple_integral_null_set:
hoelzl@47694
   707
  assumes "simple_function M u" "\<And>x. 0 \<le> u x" and "N \<in> null_sets M"
wenzelm@53015
   708
  shows "(\<integral>\<^sup>Sx. u x * indicator N x \<partial>M) = 0"
hoelzl@38656
   709
proof -
hoelzl@47694
   710
  have "AE x in M. indicator N x = (0 :: ereal)"
hoelzl@47694
   711
    using `N \<in> null_sets M` by (auto simp: indicator_def intro!: AE_I[of _ _ N])
wenzelm@53015
   712
  then have "(\<integral>\<^sup>Sx. u x * indicator N x \<partial>M) = (\<integral>\<^sup>Sx. 0 \<partial>M)"
hoelzl@41981
   713
    using assms apply (intro simple_integral_cong_AE) by auto
hoelzl@40859
   714
  then show ?thesis by simp
hoelzl@38656
   715
qed
hoelzl@35582
   716
hoelzl@47694
   717
lemma simple_integral_cong_AE_mult_indicator:
hoelzl@47694
   718
  assumes sf: "simple_function M f" and eq: "AE x in M. x \<in> S" and "S \<in> sets M"
wenzelm@53015
   719
  shows "integral\<^sup>S M f = (\<integral>\<^sup>Sx. f x * indicator S x \<partial>M)"
hoelzl@41705
   720
  using assms by (intro simple_integral_cong_AE) auto
hoelzl@35582
   721
hoelzl@47694
   722
lemma simple_integral_cmult_indicator:
hoelzl@41981
   723
  assumes A: "A \<in> sets M"
hoelzl@56949
   724
  shows "(\<integral>\<^sup>Sx. c * indicator A x \<partial>M) = c * emeasure M A"
hoelzl@41981
   725
  using simple_integral_mult[OF simple_function_indicator[OF A]]
hoelzl@41981
   726
  unfolding simple_integral_indicator_only[OF A] by simp
hoelzl@41981
   727
hoelzl@56996
   728
lemma simple_integral_nonneg:
hoelzl@47694
   729
  assumes f: "simple_function M f" and ae: "AE x in M. 0 \<le> f x"
wenzelm@53015
   730
  shows "0 \<le> integral\<^sup>S M f"
hoelzl@41981
   731
proof -
wenzelm@53015
   732
  have "integral\<^sup>S M (\<lambda>x. 0) \<le> integral\<^sup>S M f"
hoelzl@41981
   733
    using simple_integral_mono_AE[OF _ f ae] by auto
hoelzl@41981
   734
  then show ?thesis by simp
hoelzl@41981
   735
qed
hoelzl@41981
   736
hoelzl@56994
   737
subsection {* Integral on nonnegative functions *}
hoelzl@41689
   738
hoelzl@56996
   739
definition nn_integral :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> ereal" ("integral\<^sup>N") where
hoelzl@56996
   740
  "integral\<^sup>N M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f}. integral\<^sup>S M g)"
hoelzl@35692
   741
hoelzl@41689
   742
syntax
hoelzl@56996
   743
  "_nn_integral" :: "pttrn \<Rightarrow> ereal \<Rightarrow> 'a measure \<Rightarrow> ereal" ("\<integral>\<^sup>+ _. _ \<partial>_" [60,61] 110)
hoelzl@41689
   744
hoelzl@41689
   745
translations
hoelzl@56996
   746
  "\<integral>\<^sup>+x. f \<partial>M" == "CONST nn_integral M (\<lambda>x. f)"
hoelzl@40872
   747
hoelzl@57025
   748
lemma nn_integral_nonneg: "0 \<le> integral\<^sup>N M f"
hoelzl@56996
   749
  by (auto intro!: SUP_upper2[of "\<lambda>x. 0"] simp: nn_integral_def le_fun_def)
hoelzl@40873
   750
hoelzl@56996
   751
lemma nn_integral_not_MInfty[simp]: "integral\<^sup>N M f \<noteq> -\<infinity>"
hoelzl@56996
   752
  using nn_integral_nonneg[of M f] by auto
hoelzl@47694
   753
hoelzl@56996
   754
lemma nn_integral_def_finite:
hoelzl@56996
   755
  "integral\<^sup>N M f = (SUP g : {g. simple_function M g \<and> g \<le> max 0 \<circ> f \<and> range g \<subseteq> {0 ..< \<infinity>}}. integral\<^sup>S M g)"
haftmann@56218
   756
    (is "_ = SUPREMUM ?A ?f")
hoelzl@56996
   757
  unfolding nn_integral_def
hoelzl@44928
   758
proof (safe intro!: antisym SUP_least)
hoelzl@41981
   759
  fix g assume g: "simple_function M g" "g \<le> max 0 \<circ> f"
hoelzl@41981
   760
  let ?G = "{x \<in> space M. \<not> g x \<noteq> \<infinity>}"
hoelzl@41981
   761
  note gM = g(1)[THEN borel_measurable_simple_function]
wenzelm@50252
   762
  have \<mu>_G_pos: "0 \<le> (emeasure M) ?G" using gM by auto
wenzelm@46731
   763
  let ?g = "\<lambda>y x. if g x = \<infinity> then y else max 0 (g x)"
hoelzl@41981
   764
  from g gM have g_in_A: "\<And>y. 0 \<le> y \<Longrightarrow> y \<noteq> \<infinity> \<Longrightarrow> ?g y \<in> ?A"
hoelzl@41981
   765
    apply (safe intro!: simple_function_max simple_function_If)
hoelzl@41981
   766
    apply (force simp: max_def le_fun_def split: split_if_asm)+
hoelzl@41981
   767
    done
haftmann@56218
   768
  show "integral\<^sup>S M g \<le> SUPREMUM ?A ?f"
hoelzl@41981
   769
  proof cases
hoelzl@41981
   770
    have g0: "?g 0 \<in> ?A" by (intro g_in_A) auto
hoelzl@47694
   771
    assume "(emeasure M) ?G = 0"
hoelzl@47694
   772
    with gM have "AE x in M. x \<notin> ?G"
hoelzl@47694
   773
      by (auto simp add: AE_iff_null intro!: null_setsI)
hoelzl@41981
   774
    with gM g show ?thesis
hoelzl@44928
   775
      by (intro SUP_upper2[OF g0] simple_integral_mono_AE)
hoelzl@41981
   776
         (auto simp: max_def intro!: simple_function_If)
hoelzl@41981
   777
  next
wenzelm@50252
   778
    assume \<mu>_G: "(emeasure M) ?G \<noteq> 0"
haftmann@56218
   779
    have "SUPREMUM ?A (integral\<^sup>S M) = \<infinity>"
hoelzl@41981
   780
    proof (intro SUP_PInfty)
hoelzl@41981
   781
      fix n :: nat
hoelzl@47694
   782
      let ?y = "ereal (real n) / (if (emeasure M) ?G = \<infinity> then 1 else (emeasure M) ?G)"
wenzelm@50252
   783
      have "0 \<le> ?y" "?y \<noteq> \<infinity>" using \<mu>_G \<mu>_G_pos by (auto simp: ereal_divide_eq)
hoelzl@41981
   784
      then have "?g ?y \<in> ?A" by (rule g_in_A)
hoelzl@47694
   785
      have "real n \<le> ?y * (emeasure M) ?G"
wenzelm@50252
   786
        using \<mu>_G \<mu>_G_pos by (cases "(emeasure M) ?G") (auto simp: field_simps)
wenzelm@53015
   787
      also have "\<dots> = (\<integral>\<^sup>Sx. ?y * indicator ?G x \<partial>M)"
hoelzl@41981
   788
        using `0 \<le> ?y` `?g ?y \<in> ?A` gM
hoelzl@41981
   789
        by (subst simple_integral_cmult_indicator) auto
wenzelm@53015
   790
      also have "\<dots> \<le> integral\<^sup>S M (?g ?y)" using `?g ?y \<in> ?A` gM
hoelzl@41981
   791
        by (intro simple_integral_mono) auto
wenzelm@53015
   792
      finally show "\<exists>i\<in>?A. real n \<le> integral\<^sup>S M i"
hoelzl@41981
   793
        using `?g ?y \<in> ?A` by blast
hoelzl@41981
   794
    qed
hoelzl@41981
   795
    then show ?thesis by simp
hoelzl@41981
   796
  qed
hoelzl@44928
   797
qed (auto intro: SUP_upper)
hoelzl@40873
   798
hoelzl@56996
   799
lemma nn_integral_mono_AE:
hoelzl@56996
   800
  assumes ae: "AE x in M. u x \<le> v x" shows "integral\<^sup>N M u \<le> integral\<^sup>N M v"
hoelzl@56996
   801
  unfolding nn_integral_def
hoelzl@41981
   802
proof (safe intro!: SUP_mono)
hoelzl@41981
   803
  fix n assume n: "simple_function M n" "n \<le> max 0 \<circ> u"
hoelzl@41981
   804
  from ae[THEN AE_E] guess N . note N = this
hoelzl@47694
   805
  then have ae_N: "AE x in M. x \<notin> N" by (auto intro: AE_not_in)
wenzelm@46731
   806
  let ?n = "\<lambda>x. n x * indicator (space M - N) x"
hoelzl@47694
   807
  have "AE x in M. n x \<le> ?n x" "simple_function M ?n"
hoelzl@41981
   808
    using n N ae_N by auto
hoelzl@41981
   809
  moreover
hoelzl@41981
   810
  { fix x have "?n x \<le> max 0 (v x)"
hoelzl@41981
   811
    proof cases
hoelzl@41981
   812
      assume x: "x \<in> space M - N"
hoelzl@41981
   813
      with N have "u x \<le> v x" by auto
hoelzl@41981
   814
      with n(2)[THEN le_funD, of x] x show ?thesis
hoelzl@41981
   815
        by (auto simp: max_def split: split_if_asm)
hoelzl@41981
   816
    qed simp }
hoelzl@41981
   817
  then have "?n \<le> max 0 \<circ> v" by (auto simp: le_funI)
wenzelm@53015
   818
  moreover have "integral\<^sup>S M n \<le> integral\<^sup>S M ?n"
hoelzl@41981
   819
    using ae_N N n by (auto intro!: simple_integral_mono_AE)
wenzelm@53015
   820
  ultimately show "\<exists>m\<in>{g. simple_function M g \<and> g \<le> max 0 \<circ> v}. integral\<^sup>S M n \<le> integral\<^sup>S M m"
hoelzl@41981
   821
    by force
hoelzl@38656
   822
qed
hoelzl@38656
   823
hoelzl@56996
   824
lemma nn_integral_mono:
hoelzl@56996
   825
  "(\<And>x. x \<in> space M \<Longrightarrow> u x \<le> v x) \<Longrightarrow> integral\<^sup>N M u \<le> integral\<^sup>N M v"
hoelzl@56996
   826
  by (auto intro: nn_integral_mono_AE)
hoelzl@40859
   827
hoelzl@56996
   828
lemma nn_integral_cong_AE:
hoelzl@56996
   829
  "AE x in M. u x = v x \<Longrightarrow> integral\<^sup>N M u = integral\<^sup>N M v"
hoelzl@56996
   830
  by (auto simp: eq_iff intro!: nn_integral_mono_AE)
hoelzl@40859
   831
hoelzl@56996
   832
lemma nn_integral_cong:
hoelzl@56996
   833
  "(\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^sup>N M u = integral\<^sup>N M v"
hoelzl@56996
   834
  by (auto intro: nn_integral_cong_AE)
hoelzl@40859
   835
hoelzl@56996
   836
lemma nn_integral_cong_strong:
hoelzl@56996
   837
  "M = N \<Longrightarrow> (\<And>x. x \<in> space M \<Longrightarrow> u x = v x) \<Longrightarrow> integral\<^sup>N M u = integral\<^sup>N N v"
hoelzl@56996
   838
  by (auto intro: nn_integral_cong)
hoelzl@56993
   839
hoelzl@56996
   840
lemma nn_integral_eq_simple_integral:
hoelzl@56996
   841
  assumes f: "simple_function M f" "\<And>x. 0 \<le> f x" shows "integral\<^sup>N M f = integral\<^sup>S M f"
hoelzl@41981
   842
proof -
wenzelm@46731
   843
  let ?f = "\<lambda>x. f x * indicator (space M) x"
hoelzl@41981
   844
  have f': "simple_function M ?f" using f by auto
hoelzl@41981
   845
  with f(2) have [simp]: "max 0 \<circ> ?f = ?f"
hoelzl@41981
   846
    by (auto simp: fun_eq_iff max_def split: split_indicator)
hoelzl@56996
   847
  have "integral\<^sup>N M ?f \<le> integral\<^sup>S M ?f" using f'
hoelzl@56996
   848
    by (force intro!: SUP_least simple_integral_mono simp: le_fun_def nn_integral_def)
hoelzl@56996
   849
  moreover have "integral\<^sup>S M ?f \<le> integral\<^sup>N M ?f"
hoelzl@56996
   850
    unfolding nn_integral_def
hoelzl@44928
   851
    using f' by (auto intro!: SUP_upper)
hoelzl@41981
   852
  ultimately show ?thesis
hoelzl@56996
   853
    by (simp cong: nn_integral_cong simple_integral_cong)
hoelzl@41981
   854
qed
hoelzl@41981
   855
hoelzl@56996
   856
lemma nn_integral_eq_simple_integral_AE:
hoelzl@56996
   857
  assumes f: "simple_function M f" "AE x in M. 0 \<le> f x" shows "integral\<^sup>N M f = integral\<^sup>S M f"
hoelzl@41981
   858
proof -
hoelzl@47694
   859
  have "AE x in M. f x = max 0 (f x)" using f by (auto split: split_max)
hoelzl@56996
   860
  with f have "integral\<^sup>N M f = integral\<^sup>S M (\<lambda>x. max 0 (f x))"
hoelzl@56996
   861
    by (simp cong: nn_integral_cong_AE simple_integral_cong_AE
hoelzl@56996
   862
             add: nn_integral_eq_simple_integral)
hoelzl@41981
   863
  with assms show ?thesis
hoelzl@41981
   864
    by (auto intro!: simple_integral_cong_AE split: split_max)
hoelzl@41981
   865
qed
hoelzl@40873
   866
hoelzl@56996
   867
lemma nn_integral_SUP_approx:
hoelzl@41981
   868
  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
hoelzl@41981
   869
  and u: "simple_function M u" "u \<le> (SUP i. f i)" "u`space M \<subseteq> {0..<\<infinity>}"
hoelzl@56996
   870
  shows "integral\<^sup>S M u \<le> (SUP i. integral\<^sup>N M (f i))" (is "_ \<le> ?S")
hoelzl@43920
   871
proof (rule ereal_le_mult_one_interval)
hoelzl@56996
   872
  have "0 \<le> (SUP i. integral\<^sup>N M (f i))"
hoelzl@56996
   873
    using f(3) by (auto intro!: SUP_upper2 nn_integral_nonneg)
hoelzl@56996
   874
  then show "(SUP i. integral\<^sup>N M (f i)) \<noteq> -\<infinity>" by auto
hoelzl@41981
   875
  have u_range: "\<And>x. x \<in> space M \<Longrightarrow> 0 \<le> u x \<and> u x \<noteq> \<infinity>"
hoelzl@41981
   876
    using u(3) by auto
hoelzl@43920
   877
  fix a :: ereal assume "0 < a" "a < 1"
hoelzl@38656
   878
  hence "a \<noteq> 0" by auto
wenzelm@46731
   879
  let ?B = "\<lambda>i. {x \<in> space M. a * u x \<le> f i x}"
hoelzl@38656
   880
  have B: "\<And>i. ?B i \<in> sets M"
hoelzl@56949
   881
    using f `simple_function M u`[THEN borel_measurable_simple_function] by auto
hoelzl@38656
   882
wenzelm@46731
   883
  let ?uB = "\<lambda>i x. u x * indicator (?B i) x"
hoelzl@38656
   884
hoelzl@38656
   885
  { fix i have "?B i \<subseteq> ?B (Suc i)"
hoelzl@38656
   886
    proof safe
hoelzl@38656
   887
      fix i x assume "a * u x \<le> f i x"
hoelzl@38656
   888
      also have "\<dots> \<le> f (Suc i) x"
hoelzl@41981
   889
        using `incseq f`[THEN incseq_SucD] unfolding le_fun_def by auto
hoelzl@38656
   890
      finally show "a * u x \<le> f (Suc i) x" .
hoelzl@38656
   891
    qed }
hoelzl@38656
   892
  note B_mono = this
hoelzl@35582
   893
immler@50244
   894
  note B_u = sets.Int[OF u(1)[THEN simple_functionD(2)] B]
hoelzl@38656
   895
wenzelm@46731
   896
  let ?B' = "\<lambda>i n. (u -` {i} \<inter> space M) \<inter> ?B n"
hoelzl@47694
   897
  have measure_conv: "\<And>i. (emeasure M) (u -` {i} \<inter> space M) = (SUP n. (emeasure M) (?B' i n))"
hoelzl@41981
   898
  proof -
hoelzl@41981
   899
    fix i
hoelzl@41981
   900
    have 1: "range (?B' i) \<subseteq> sets M" using B_u by auto
hoelzl@41981
   901
    have 2: "incseq (?B' i)" using B_mono by (auto intro!: incseq_SucI)
hoelzl@41981
   902
    have "(\<Union>n. ?B' i n) = u -` {i} \<inter> space M"
hoelzl@41981
   903
    proof safe
hoelzl@41981
   904
      fix x i assume x: "x \<in> space M"
hoelzl@41981
   905
      show "x \<in> (\<Union>i. ?B' (u x) i)"
hoelzl@41981
   906
      proof cases
hoelzl@41981
   907
        assume "u x = 0" thus ?thesis using `x \<in> space M` f(3) by simp
hoelzl@41981
   908
      next
hoelzl@41981
   909
        assume "u x \<noteq> 0"
hoelzl@41981
   910
        with `a < 1` u_range[OF `x \<in> space M`]
hoelzl@41981
   911
        have "a * u x < 1 * u x"
hoelzl@43920
   912
          by (intro ereal_mult_strict_right_mono) (auto simp: image_iff)
noschinl@46884
   913
        also have "\<dots> \<le> (SUP i. f i x)" using u(2) by (auto simp: le_fun_def)
hoelzl@44928
   914
        finally obtain i where "a * u x < f i x" unfolding SUP_def
haftmann@56166
   915
          by (auto simp add: less_SUP_iff)
hoelzl@41981
   916
        hence "a * u x \<le> f i x" by auto
hoelzl@41981
   917
        thus ?thesis using `x \<in> space M` by auto
hoelzl@41981
   918
      qed
hoelzl@40859
   919
    qed
hoelzl@47694
   920
    then show "?thesis i" using SUP_emeasure_incseq[OF 1 2] by simp
hoelzl@41981
   921
  qed
hoelzl@38656
   922
wenzelm@53015
   923
  have "integral\<^sup>S M u = (SUP i. integral\<^sup>S M (?uB i))"
hoelzl@41689
   924
    unfolding simple_integral_indicator[OF B `simple_function M u`]
haftmann@56212
   925
  proof (subst SUP_ereal_setsum, safe)
hoelzl@38656
   926
    fix x n assume "x \<in> space M"
hoelzl@47694
   927
    with u_range show "incseq (\<lambda>i. u x * (emeasure M) (?B' (u x) i))" "\<And>i. 0 \<le> u x * (emeasure M) (?B' (u x) i)"
hoelzl@47694
   928
      using B_mono B_u by (auto intro!: emeasure_mono ereal_mult_left_mono incseq_SucI simp: ereal_zero_le_0_iff)
hoelzl@38656
   929
  next
wenzelm@53015
   930
    show "integral\<^sup>S M u = (\<Sum>i\<in>u ` space M. SUP n. i * (emeasure M) (?B' i n))"
hoelzl@41981
   931
      using measure_conv u_range B_u unfolding simple_integral_def
haftmann@56212
   932
      by (auto intro!: setsum_cong SUP_ereal_cmult [symmetric])
hoelzl@38656
   933
  qed
hoelzl@38656
   934
  moreover
wenzelm@53015
   935
  have "a * (SUP i. integral\<^sup>S M (?uB i)) \<le> ?S"
haftmann@56212
   936
    apply (subst SUP_ereal_cmult [symmetric])
hoelzl@38705
   937
  proof (safe intro!: SUP_mono bexI)
hoelzl@38656
   938
    fix i
wenzelm@53015
   939
    have "a * integral\<^sup>S M (?uB i) = (\<integral>\<^sup>Sx. a * ?uB i x \<partial>M)"
hoelzl@41981
   940
      using B `simple_function M u` u_range
hoelzl@41981
   941
      by (subst simple_integral_mult) (auto split: split_indicator)
hoelzl@56996
   942
    also have "\<dots> \<le> integral\<^sup>N M (f i)"
hoelzl@38656
   943
    proof -
hoelzl@41981
   944
      have *: "simple_function M (\<lambda>x. a * ?uB i x)" using B `0 < a` u(1) by auto
hoelzl@41981
   945
      show ?thesis using f(3) * u_range `0 < a`
hoelzl@56996
   946
        by (subst nn_integral_eq_simple_integral[symmetric])
hoelzl@56996
   947
           (auto intro!: nn_integral_mono split: split_indicator)
hoelzl@38656
   948
    qed
hoelzl@56996
   949
    finally show "a * integral\<^sup>S M (?uB i) \<le> integral\<^sup>N M (f i)"
hoelzl@38656
   950
      by auto
hoelzl@41981
   951
  next
wenzelm@53015
   952
    fix i show "0 \<le> \<integral>\<^sup>S x. ?uB i x \<partial>M" using B `0 < a` u(1) u_range
hoelzl@56996
   953
      by (intro simple_integral_nonneg) (auto split: split_indicator)
hoelzl@41981
   954
  qed (insert `0 < a`, auto)
wenzelm@53015
   955
  ultimately show "a * integral\<^sup>S M u \<le> ?S" by simp
hoelzl@35582
   956
qed
hoelzl@35582
   957
hoelzl@56996
   958
lemma incseq_nn_integral:
hoelzl@56996
   959
  assumes "incseq f" shows "incseq (\<lambda>i. integral\<^sup>N M (f i))"
hoelzl@41981
   960
proof -
hoelzl@41981
   961
  have "\<And>i x. f i x \<le> f (Suc i) x"
hoelzl@41981
   962
    using assms by (auto dest!: incseq_SucD simp: le_fun_def)
hoelzl@41981
   963
  then show ?thesis
hoelzl@56996
   964
    by (auto intro!: incseq_SucI nn_integral_mono)
hoelzl@41981
   965
qed
hoelzl@41981
   966
hoelzl@35582
   967
text {* Beppo-Levi monotone convergence theorem *}
hoelzl@56996
   968
lemma nn_integral_monotone_convergence_SUP:
hoelzl@41981
   969
  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>i x. 0 \<le> f i x"
hoelzl@56996
   970
  shows "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^sup>N M (f i))"
hoelzl@41981
   971
proof (rule antisym)
hoelzl@56996
   972
  show "(SUP j. integral\<^sup>N M (f j)) \<le> (\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M)"
hoelzl@56996
   973
    by (auto intro!: SUP_least SUP_upper nn_integral_mono)
hoelzl@38656
   974
next
hoelzl@56996
   975
  show "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) \<le> (SUP j. integral\<^sup>N M (f j))"
hoelzl@56996
   976
    unfolding nn_integral_def_finite[of _ "\<lambda>x. SUP i. f i x"]
hoelzl@44928
   977
  proof (safe intro!: SUP_least)
hoelzl@41981
   978
    fix g assume g: "simple_function M g"
wenzelm@53374
   979
      and *: "g \<le> max 0 \<circ> (\<lambda>x. SUP i. f i x)" "range g \<subseteq> {0..<\<infinity>}"
wenzelm@53374
   980
    then have "\<And>x. 0 \<le> (SUP i. f i x)" and g': "g`space M \<subseteq> {0..<\<infinity>}"
hoelzl@44928
   981
      using f by (auto intro!: SUP_upper2)
hoelzl@56996
   982
    with * show "integral\<^sup>S M g \<le> (SUP j. integral\<^sup>N M (f j))"
hoelzl@56996
   983
      by (intro  nn_integral_SUP_approx[OF f g _ g'])
noschinl@46884
   984
         (auto simp: le_fun_def max_def)
hoelzl@35582
   985
  qed
hoelzl@35582
   986
qed
hoelzl@35582
   987
hoelzl@56996
   988
lemma nn_integral_monotone_convergence_SUP_AE:
hoelzl@47694
   989
  assumes f: "\<And>i. AE x in M. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x" "\<And>i. f i \<in> borel_measurable M"
hoelzl@56996
   990
  shows "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^sup>N M (f i))"
hoelzl@40859
   991
proof -
hoelzl@47694
   992
  from f have "AE x in M. \<forall>i. f i x \<le> f (Suc i) x \<and> 0 \<le> f i x"
hoelzl@41981
   993
    by (simp add: AE_all_countable)
hoelzl@41981
   994
  from this[THEN AE_E] guess N . note N = this
wenzelm@46731
   995
  let ?f = "\<lambda>i x. if x \<in> space M - N then f i x else 0"
hoelzl@47694
   996
  have f_eq: "AE x in M. \<forall>i. ?f i x = f i x" using N by (auto intro!: AE_I[of _ _ N])
wenzelm@53015
   997
  then have "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (\<integral>\<^sup>+ x. (SUP i. ?f i x) \<partial>M)"
hoelzl@56996
   998
    by (auto intro!: nn_integral_cong_AE)
wenzelm@53015
   999
  also have "\<dots> = (SUP i. (\<integral>\<^sup>+ x. ?f i x \<partial>M))"
hoelzl@56996
  1000
  proof (rule nn_integral_monotone_convergence_SUP)
hoelzl@41981
  1001
    show "incseq ?f" using N(1) by (force intro!: incseq_SucI le_funI)
hoelzl@41981
  1002
    { fix i show "(\<lambda>x. if x \<in> space M - N then f i x else 0) \<in> borel_measurable M"
hoelzl@41981
  1003
        using f N(3) by (intro measurable_If_set) auto
hoelzl@41981
  1004
      fix x show "0 \<le> ?f i x"
hoelzl@41981
  1005
        using N(1) by auto }
hoelzl@40859
  1006
  qed
wenzelm@53015
  1007
  also have "\<dots> = (SUP i. (\<integral>\<^sup>+ x. f i x \<partial>M))"
hoelzl@56996
  1008
    using f_eq by (force intro!: arg_cong[where f="SUPREMUM UNIV"] nn_integral_cong_AE ext)
hoelzl@41981
  1009
  finally show ?thesis .
hoelzl@41981
  1010
qed
hoelzl@41981
  1011
hoelzl@56996
  1012
lemma nn_integral_monotone_convergence_SUP_AE_incseq:
hoelzl@47694
  1013
  assumes f: "incseq f" "\<And>i. AE x in M. 0 \<le> f i x" and borel: "\<And>i. f i \<in> borel_measurable M"
hoelzl@56996
  1014
  shows "(\<integral>\<^sup>+ x. (SUP i. f i x) \<partial>M) = (SUP i. integral\<^sup>N M (f i))"
hoelzl@41981
  1015
  using f[unfolded incseq_Suc_iff le_fun_def]
hoelzl@56996
  1016
  by (intro nn_integral_monotone_convergence_SUP_AE[OF _ borel])
hoelzl@41981
  1017
     auto
hoelzl@41981
  1018
hoelzl@56996
  1019
lemma nn_integral_monotone_convergence_simple:
hoelzl@41981
  1020
  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
wenzelm@53015
  1021
  shows "(SUP i. integral\<^sup>S M (f i)) = (\<integral>\<^sup>+x. (SUP i. f i x) \<partial>M)"
hoelzl@56996
  1022
  using assms unfolding nn_integral_monotone_convergence_SUP[OF f(1)
hoelzl@41981
  1023
    f(3)[THEN borel_measurable_simple_function] f(2)]
hoelzl@56996
  1024
  by (auto intro!: nn_integral_eq_simple_integral[symmetric] arg_cong[where f="SUPREMUM UNIV"] ext)
hoelzl@41981
  1025
hoelzl@56996
  1026
lemma nn_integral_max_0:
hoelzl@56996
  1027
  "(\<integral>\<^sup>+x. max 0 (f x) \<partial>M) = integral\<^sup>N M f"
hoelzl@56996
  1028
  by (simp add: le_fun_def nn_integral_def)
hoelzl@41981
  1029
hoelzl@56996
  1030
lemma nn_integral_cong_pos:
hoelzl@41981
  1031
  assumes "\<And>x. x \<in> space M \<Longrightarrow> f x \<le> 0 \<and> g x \<le> 0 \<or> f x = g x"
hoelzl@56996
  1032
  shows "integral\<^sup>N M f = integral\<^sup>N M g"
hoelzl@41981
  1033
proof -
hoelzl@56996
  1034
  have "integral\<^sup>N M (\<lambda>x. max 0 (f x)) = integral\<^sup>N M (\<lambda>x. max 0 (g x))"
hoelzl@56996
  1035
  proof (intro nn_integral_cong)
hoelzl@41981
  1036
    fix x assume "x \<in> space M"
hoelzl@41981
  1037
    from assms[OF this] show "max 0 (f x) = max 0 (g x)"
hoelzl@41981
  1038
      by (auto split: split_max)
hoelzl@41981
  1039
  qed
hoelzl@56996
  1040
  then show ?thesis by (simp add: nn_integral_max_0)
hoelzl@40859
  1041
qed
hoelzl@40859
  1042
hoelzl@47694
  1043
lemma SUP_simple_integral_sequences:
hoelzl@41981
  1044
  assumes f: "incseq f" "\<And>i x. 0 \<le> f i x" "\<And>i. simple_function M (f i)"
hoelzl@41981
  1045
  and g: "incseq g" "\<And>i x. 0 \<le> g i x" "\<And>i. simple_function M (g i)"
hoelzl@47694
  1046
  and eq: "AE x in M. (SUP i. f i x) = (SUP i. g i x)"
wenzelm@53015
  1047
  shows "(SUP i. integral\<^sup>S M (f i)) = (SUP i. integral\<^sup>S M (g i))"
haftmann@56218
  1048
    (is "SUPREMUM _ ?F = SUPREMUM _ ?G")
hoelzl@38656
  1049
proof -
wenzelm@53015
  1050
  have "(SUP i. integral\<^sup>S M (f i)) = (\<integral>\<^sup>+x. (SUP i. f i x) \<partial>M)"
hoelzl@56996
  1051
    using f by (rule nn_integral_monotone_convergence_simple)
wenzelm@53015
  1052
  also have "\<dots> = (\<integral>\<^sup>+x. (SUP i. g i x) \<partial>M)"
hoelzl@56996
  1053
    unfolding eq[THEN nn_integral_cong_AE] ..
hoelzl@38656
  1054
  also have "\<dots> = (SUP i. ?G i)"
hoelzl@56996
  1055
    using g by (rule nn_integral_monotone_convergence_simple[symmetric])
hoelzl@41981
  1056
  finally show ?thesis by simp
hoelzl@38656
  1057
qed
hoelzl@38656
  1058
hoelzl@56996
  1059
lemma nn_integral_const[simp]:
wenzelm@53015
  1060
  "0 \<le> c \<Longrightarrow> (\<integral>\<^sup>+ x. c \<partial>M) = c * (emeasure M) (space M)"
hoelzl@56996
  1061
  by (subst nn_integral_eq_simple_integral) auto
hoelzl@38656
  1062
hoelzl@56996
  1063
lemma nn_integral_linear:
hoelzl@41981
  1064
  assumes f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" and "0 \<le> a"
hoelzl@41981
  1065
  and g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
hoelzl@56996
  1066
  shows "(\<integral>\<^sup>+ x. a * f x + g x \<partial>M) = a * integral\<^sup>N M f + integral\<^sup>N M g"
hoelzl@56996
  1067
    (is "integral\<^sup>N M ?L = _")
hoelzl@35582
  1068
proof -
hoelzl@41981
  1069
  from borel_measurable_implies_simple_function_sequence'[OF f(1)] guess u .
hoelzl@56996
  1070
  note u = nn_integral_monotone_convergence_simple[OF this(2,5,1)] this
hoelzl@41981
  1071
  from borel_measurable_implies_simple_function_sequence'[OF g(1)] guess v .
hoelzl@56996
  1072
  note v = nn_integral_monotone_convergence_simple[OF this(2,5,1)] this
wenzelm@46731
  1073
  let ?L' = "\<lambda>i x. a * u i x + v i x"
hoelzl@38656
  1074
hoelzl@41981
  1075
  have "?L \<in> borel_measurable M" using assms by auto
hoelzl@38656
  1076
  from borel_measurable_implies_simple_function_sequence'[OF this] guess l .
hoelzl@56996
  1077
  note l = nn_integral_monotone_convergence_simple[OF this(2,5,1)] this
hoelzl@41981
  1078
wenzelm@53015
  1079
  have inc: "incseq (\<lambda>i. a * integral\<^sup>S M (u i))" "incseq (\<lambda>i. integral\<^sup>S M (v i))"
hoelzl@41981
  1080
    using u v `0 \<le> a`
hoelzl@41981
  1081
    by (auto simp: incseq_Suc_iff le_fun_def
hoelzl@43920
  1082
             intro!: add_mono ereal_mult_left_mono simple_integral_mono)
wenzelm@53015
  1083
  have pos: "\<And>i. 0 \<le> integral\<^sup>S M (u i)" "\<And>i. 0 \<le> integral\<^sup>S M (v i)" "\<And>i. 0 \<le> a * integral\<^sup>S M (u i)"
hoelzl@56996
  1084
    using u v `0 \<le> a` by (auto simp: simple_integral_nonneg)
wenzelm@53015
  1085
  { fix i from pos[of i] have "a * integral\<^sup>S M (u i) \<noteq> -\<infinity>" "integral\<^sup>S M (v i) \<noteq> -\<infinity>"
hoelzl@41981
  1086
      by (auto split: split_if_asm) }
hoelzl@41981
  1087
  note not_MInf = this
hoelzl@41981
  1088
wenzelm@53015
  1089
  have l': "(SUP i. integral\<^sup>S M (l i)) = (SUP i. integral\<^sup>S M (?L' i))"
hoelzl@41981
  1090
  proof (rule SUP_simple_integral_sequences[OF l(3,6,2)])
hoelzl@41981
  1091
    show "incseq ?L'" "\<And>i x. 0 \<le> ?L' i x" "\<And>i. simple_function M (?L' i)"
hoelzl@41981
  1092
      using u v  `0 \<le> a` unfolding incseq_Suc_iff le_fun_def
nipkow@56537
  1093
      by (auto intro!: add_mono ereal_mult_left_mono)
hoelzl@41981
  1094
    { fix x
hoelzl@41981
  1095
      { fix i have "a * u i x \<noteq> -\<infinity>" "v i x \<noteq> -\<infinity>" "u i x \<noteq> -\<infinity>" using `0 \<le> a` u(6)[of i x] v(6)[of i x]
hoelzl@41981
  1096
          by auto }
hoelzl@41981
  1097
      then have "(SUP i. a * u i x + v i x) = a * (SUP i. u i x) + (SUP i. v i x)"
hoelzl@41981
  1098
        using `0 \<le> a` u(3) v(3) u(6)[of _ x] v(6)[of _ x]
haftmann@56212
  1099
        by (subst SUP_ereal_cmult [symmetric, OF u(6) `0 \<le> a`])
haftmann@56212
  1100
           (auto intro!: SUP_ereal_add
nipkow@56537
  1101
                 simp: incseq_Suc_iff le_fun_def add_mono ereal_mult_left_mono) }
hoelzl@47694
  1102
    then show "AE x in M. (SUP i. l i x) = (SUP i. ?L' i x)"
hoelzl@41981
  1103
      unfolding l(5) using `0 \<le> a` u(5) v(5) l(5) f(2) g(2)
nipkow@56537
  1104
      by (intro AE_I2) (auto split: split_max)
hoelzl@38656
  1105
  qed
wenzelm@53015
  1106
  also have "\<dots> = (SUP i. a * integral\<^sup>S M (u i) + integral\<^sup>S M (v i))"
haftmann@56218
  1107
    using u(2, 6) v(2, 6) `0 \<le> a` by (auto intro!: arg_cong[where f="SUPREMUM UNIV"] ext)
wenzelm@53015
  1108
  finally have "(\<integral>\<^sup>+ x. max 0 (a * f x + g x) \<partial>M) = a * (\<integral>\<^sup>+x. max 0 (f x) \<partial>M) + (\<integral>\<^sup>+x. max 0 (g x) \<partial>M)"
hoelzl@41981
  1109
    unfolding l(5)[symmetric] u(5)[symmetric] v(5)[symmetric]
hoelzl@41981
  1110
    unfolding l(1)[symmetric] u(1)[symmetric] v(1)[symmetric]
haftmann@56212
  1111
    apply (subst SUP_ereal_cmult [symmetric, OF pos(1) `0 \<le> a`])
haftmann@56212
  1112
    apply (subst SUP_ereal_add [symmetric, OF inc not_MInf]) .
hoelzl@56996
  1113
  then show ?thesis by (simp add: nn_integral_max_0)
hoelzl@38656
  1114
qed
hoelzl@38656
  1115
hoelzl@56996
  1116
lemma nn_integral_cmult:
hoelzl@49775
  1117
  assumes f: "f \<in> borel_measurable M" "0 \<le> c"
hoelzl@56996
  1118
  shows "(\<integral>\<^sup>+ x. c * f x \<partial>M) = c * integral\<^sup>N M f"
hoelzl@41981
  1119
proof -
hoelzl@41981
  1120
  have [simp]: "\<And>x. c * max 0 (f x) = max 0 (c * f x)" using `0 \<le> c`
hoelzl@43920
  1121
    by (auto split: split_max simp: ereal_zero_le_0_iff)
wenzelm@53015
  1122
  have "(\<integral>\<^sup>+ x. c * f x \<partial>M) = (\<integral>\<^sup>+ x. c * max 0 (f x) \<partial>M)"
hoelzl@56996
  1123
    by (simp add: nn_integral_max_0)
hoelzl@41981
  1124
  then show ?thesis
hoelzl@56996
  1125
    using nn_integral_linear[OF _ _ `0 \<le> c`, of "\<lambda>x. max 0 (f x)" _ "\<lambda>x. 0"] f
hoelzl@56996
  1126
    by (auto simp: nn_integral_max_0)
hoelzl@41981
  1127
qed
hoelzl@38656
  1128
hoelzl@56996
  1129
lemma nn_integral_multc:
hoelzl@49775
  1130
  assumes "f \<in> borel_measurable M" "0 \<le> c"
hoelzl@56996
  1131
  shows "(\<integral>\<^sup>+ x. f x * c \<partial>M) = integral\<^sup>N M f * c"
hoelzl@56996
  1132
  unfolding mult_commute[of _ c] nn_integral_cmult[OF assms] by simp
hoelzl@41096
  1133
hoelzl@56996
  1134
lemma nn_integral_indicator[simp]:
wenzelm@53015
  1135
  "A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+ x. indicator A x\<partial>M) = (emeasure M) A"
hoelzl@56996
  1136
  by (subst nn_integral_eq_simple_integral)
hoelzl@49775
  1137
     (auto simp: simple_integral_indicator)
hoelzl@38656
  1138
hoelzl@56996
  1139
lemma nn_integral_cmult_indicator:
wenzelm@53015
  1140
  "0 \<le> c \<Longrightarrow> A \<in> sets M \<Longrightarrow> (\<integral>\<^sup>+ x. c * indicator A x \<partial>M) = c * (emeasure M) A"
hoelzl@56996
  1141
  by (subst nn_integral_eq_simple_integral)
hoelzl@41544
  1142
     (auto simp: simple_function_indicator simple_integral_indicator)
hoelzl@38656
  1143
hoelzl@56996
  1144
lemma nn_integral_indicator':
hoelzl@50097
  1145
  assumes [measurable]: "A \<inter> space M \<in> sets M"
wenzelm@53015
  1146
  shows "(\<integral>\<^sup>+ x. indicator A x \<partial>M) = emeasure M (A \<inter> space M)"
hoelzl@50097
  1147
proof -
wenzelm@53015
  1148
  have "(\<integral>\<^sup>+ x. indicator A x \<partial>M) = (\<integral>\<^sup>+ x. indicator (A \<inter> space M) x \<partial>M)"
hoelzl@56996
  1149
    by (intro nn_integral_cong) (simp split: split_indicator)
hoelzl@50097
  1150
  also have "\<dots> = emeasure M (A \<inter> space M)"
hoelzl@50097
  1151
    by simp
hoelzl@50097
  1152
  finally show ?thesis .
hoelzl@50097
  1153
qed
hoelzl@50097
  1154
hoelzl@56996
  1155
lemma nn_integral_add:
hoelzl@47694
  1156
  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
hoelzl@47694
  1157
  and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
hoelzl@56996
  1158
  shows "(\<integral>\<^sup>+ x. f x + g x \<partial>M) = integral\<^sup>N M f + integral\<^sup>N M g"
hoelzl@41981
  1159
proof -
hoelzl@47694
  1160
  have ae: "AE x in M. max 0 (f x) + max 0 (g x) = max 0 (f x + g x)"
nipkow@56537
  1161
    using assms by (auto split: split_max)
wenzelm@53015
  1162
  have "(\<integral>\<^sup>+ x. f x + g x \<partial>M) = (\<integral>\<^sup>+ x. max 0 (f x + g x) \<partial>M)"
hoelzl@56996
  1163
    by (simp add: nn_integral_max_0)
wenzelm@53015
  1164
  also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) + max 0 (g x) \<partial>M)"
hoelzl@56996
  1165
    unfolding ae[THEN nn_integral_cong_AE] ..
wenzelm@53015
  1166
  also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) \<partial>M) + (\<integral>\<^sup>+ x. max 0 (g x) \<partial>M)"
hoelzl@56996
  1167
    using nn_integral_linear[of "\<lambda>x. max 0 (f x)" _ 1 "\<lambda>x. max 0 (g x)"] f g
hoelzl@41981
  1168
    by auto
hoelzl@41981
  1169
  finally show ?thesis
hoelzl@56996
  1170
    by (simp add: nn_integral_max_0)
hoelzl@41981
  1171
qed
hoelzl@38656
  1172
hoelzl@56996
  1173
lemma nn_integral_setsum:
hoelzl@47694
  1174
  assumes "\<And>i. i\<in>P \<Longrightarrow> f i \<in> borel_measurable M" "\<And>i. i\<in>P \<Longrightarrow> AE x in M. 0 \<le> f i x"
hoelzl@56996
  1175
  shows "(\<integral>\<^sup>+ x. (\<Sum>i\<in>P. f i x) \<partial>M) = (\<Sum>i\<in>P. integral\<^sup>N M (f i))"
hoelzl@38656
  1176
proof cases
hoelzl@41981
  1177
  assume f: "finite P"
hoelzl@47694
  1178
  from assms have "AE x in M. \<forall>i\<in>P. 0 \<le> f i x" unfolding AE_finite_all[OF f] by auto
hoelzl@41981
  1179
  from f this assms(1) show ?thesis
hoelzl@38656
  1180
  proof induct
hoelzl@38656
  1181
    case (insert i P)
hoelzl@47694
  1182
    then have "f i \<in> borel_measurable M" "AE x in M. 0 \<le> f i x"
hoelzl@47694
  1183
      "(\<lambda>x. \<Sum>i\<in>P. f i x) \<in> borel_measurable M" "AE x in M. 0 \<le> (\<Sum>i\<in>P. f i x)"
hoelzl@50002
  1184
      by (auto intro!: setsum_nonneg)
hoelzl@56996
  1185
    from nn_integral_add[OF this]
hoelzl@38656
  1186
    show ?case using insert by auto
hoelzl@38656
  1187
  qed simp
hoelzl@38656
  1188
qed simp
hoelzl@38656
  1189
hoelzl@56996
  1190
lemma nn_integral_Markov_inequality:
hoelzl@49775
  1191
  assumes u: "u \<in> borel_measurable M" "AE x in M. 0 \<le> u x" and "A \<in> sets M" and c: "0 \<le> c"
wenzelm@53015
  1192
  shows "(emeasure M) ({x\<in>space M. 1 \<le> c * u x} \<inter> A) \<le> c * (\<integral>\<^sup>+ x. u x * indicator A x \<partial>M)"
hoelzl@47694
  1193
    (is "(emeasure M) ?A \<le> _ * ?PI")
hoelzl@41981
  1194
proof -
hoelzl@41981
  1195
  have "?A \<in> sets M"
hoelzl@41981
  1196
    using `A \<in> sets M` u by auto
wenzelm@53015
  1197
  hence "(emeasure M) ?A = (\<integral>\<^sup>+ x. indicator ?A x \<partial>M)"
hoelzl@56996
  1198
    using nn_integral_indicator by simp
wenzelm@53015
  1199
  also have "\<dots> \<le> (\<integral>\<^sup>+ x. c * (u x * indicator A x) \<partial>M)" using u c
hoelzl@56996
  1200
    by (auto intro!: nn_integral_mono_AE
hoelzl@43920
  1201
      simp: indicator_def ereal_zero_le_0_iff)
wenzelm@53015
  1202
  also have "\<dots> = c * (\<integral>\<^sup>+ x. u x * indicator A x \<partial>M)"
hoelzl@41981
  1203
    using assms
hoelzl@56996
  1204
    by (auto intro!: nn_integral_cmult simp: ereal_zero_le_0_iff)
hoelzl@41981
  1205
  finally show ?thesis .
hoelzl@41981
  1206
qed
hoelzl@41981
  1207
hoelzl@56996
  1208
lemma nn_integral_noteq_infinite:
hoelzl@47694
  1209
  assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
hoelzl@56996
  1210
  and "integral\<^sup>N M g \<noteq> \<infinity>"
hoelzl@47694
  1211
  shows "AE x in M. g x \<noteq> \<infinity>"
hoelzl@41981
  1212
proof (rule ccontr)
hoelzl@47694
  1213
  assume c: "\<not> (AE x in M. g x \<noteq> \<infinity>)"
hoelzl@47694
  1214
  have "(emeasure M) {x\<in>space M. g x = \<infinity>} \<noteq> 0"
hoelzl@47694
  1215
    using c g by (auto simp add: AE_iff_null)
hoelzl@47694
  1216
  moreover have "0 \<le> (emeasure M) {x\<in>space M. g x = \<infinity>}" using g by (auto intro: measurable_sets)
hoelzl@47694
  1217
  ultimately have "0 < (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
hoelzl@47694
  1218
  then have "\<infinity> = \<infinity> * (emeasure M) {x\<in>space M. g x = \<infinity>}" by auto
wenzelm@53015
  1219
  also have "\<dots> \<le> (\<integral>\<^sup>+x. \<infinity> * indicator {x\<in>space M. g x = \<infinity>} x \<partial>M)"
hoelzl@56996
  1220
    using g by (subst nn_integral_cmult_indicator) auto
hoelzl@56996
  1221
  also have "\<dots> \<le> integral\<^sup>N M g"
hoelzl@56996
  1222
    using assms by (auto intro!: nn_integral_mono_AE simp: indicator_def)
hoelzl@56996
  1223
  finally show False using `integral\<^sup>N M g \<noteq> \<infinity>` by auto
hoelzl@41981
  1224
qed
hoelzl@41981
  1225
hoelzl@56996
  1226
lemma nn_integral_PInf:
hoelzl@56949
  1227
  assumes f: "f \<in> borel_measurable M"
hoelzl@56996
  1228
  and not_Inf: "integral\<^sup>N M f \<noteq> \<infinity>"
hoelzl@56949
  1229
  shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
hoelzl@56949
  1230
proof -
hoelzl@56949
  1231
  have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) = (\<integral>\<^sup>+ x. \<infinity> * indicator (f -` {\<infinity>} \<inter> space M) x \<partial>M)"
hoelzl@56996
  1232
    using f by (subst nn_integral_cmult_indicator) (auto simp: measurable_sets)
hoelzl@56996
  1233
  also have "\<dots> \<le> integral\<^sup>N M (\<lambda>x. max 0 (f x))"
hoelzl@56996
  1234
    by (auto intro!: nn_integral_mono simp: indicator_def max_def)
hoelzl@56996
  1235
  finally have "\<infinity> * (emeasure M) (f -` {\<infinity>} \<inter> space M) \<le> integral\<^sup>N M f"
hoelzl@56996
  1236
    by (simp add: nn_integral_max_0)
hoelzl@56949
  1237
  moreover have "0 \<le> (emeasure M) (f -` {\<infinity>} \<inter> space M)"
hoelzl@56949
  1238
    by (rule emeasure_nonneg)
hoelzl@56949
  1239
  ultimately show ?thesis
hoelzl@56949
  1240
    using assms by (auto split: split_if_asm)
hoelzl@56949
  1241
qed
hoelzl@56949
  1242
hoelzl@56996
  1243
lemma nn_integral_PInf_AE:
hoelzl@56996
  1244
  assumes "f \<in> borel_measurable M" "integral\<^sup>N M f \<noteq> \<infinity>" shows "AE x in M. f x \<noteq> \<infinity>"
hoelzl@56949
  1245
proof (rule AE_I)
hoelzl@56949
  1246
  show "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
hoelzl@56996
  1247
    by (rule nn_integral_PInf[OF assms])
hoelzl@56949
  1248
  show "f -` {\<infinity>} \<inter> space M \<in> sets M"
hoelzl@56949
  1249
    using assms by (auto intro: borel_measurable_vimage)
hoelzl@56949
  1250
qed auto
hoelzl@56949
  1251
hoelzl@56949
  1252
lemma simple_integral_PInf:
hoelzl@56949
  1253
  assumes "simple_function M f" "\<And>x. 0 \<le> f x"
hoelzl@56949
  1254
  and "integral\<^sup>S M f \<noteq> \<infinity>"
hoelzl@56949
  1255
  shows "(emeasure M) (f -` {\<infinity>} \<inter> space M) = 0"
hoelzl@56996
  1256
proof (rule nn_integral_PInf)
hoelzl@56949
  1257
  show "f \<in> borel_measurable M" using assms by (auto intro: borel_measurable_simple_function)
hoelzl@56996
  1258
  show "integral\<^sup>N M f \<noteq> \<infinity>"
hoelzl@56996
  1259
    using assms by (simp add: nn_integral_eq_simple_integral)
hoelzl@56949
  1260
qed
hoelzl@56949
  1261
hoelzl@56996
  1262
lemma nn_integral_diff:
hoelzl@41981
  1263
  assumes f: "f \<in> borel_measurable M"
hoelzl@47694
  1264
  and g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
hoelzl@56996
  1265
  and fin: "integral\<^sup>N M g \<noteq> \<infinity>"
hoelzl@47694
  1266
  and mono: "AE x in M. g x \<le> f x"
hoelzl@56996
  1267
  shows "(\<integral>\<^sup>+ x. f x - g x \<partial>M) = integral\<^sup>N M f - integral\<^sup>N M g"
hoelzl@38656
  1268
proof -
hoelzl@47694
  1269
  have diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M" "AE x in M. 0 \<le> f x - g x"
hoelzl@43920
  1270
    using assms by (auto intro: ereal_diff_positive)
hoelzl@47694
  1271
  have pos_f: "AE x in M. 0 \<le> f x" using mono g by auto
hoelzl@43920
  1272
  { fix a b :: ereal assume "0 \<le> a" "a \<noteq> \<infinity>" "0 \<le> b" "a \<le> b" then have "b - a + a = b"
hoelzl@43920
  1273
      by (cases rule: ereal2_cases[of a b]) auto }
hoelzl@41981
  1274
  note * = this
hoelzl@47694
  1275
  then have "AE x in M. f x = f x - g x + g x"
hoelzl@56996
  1276
    using mono nn_integral_noteq_infinite[OF g fin] assms by auto
hoelzl@56996
  1277
  then have **: "integral\<^sup>N M f = (\<integral>\<^sup>+x. f x - g x \<partial>M) + integral\<^sup>N M g"
hoelzl@56996
  1278
    unfolding nn_integral_add[OF diff g, symmetric]
hoelzl@56996
  1279
    by (rule nn_integral_cong_AE)
hoelzl@41981
  1280
  show ?thesis unfolding **
hoelzl@56996
  1281
    using fin nn_integral_nonneg[of M g]
hoelzl@56996
  1282
    by (cases rule: ereal2_cases[of "\<integral>\<^sup>+ x. f x - g x \<partial>M" "integral\<^sup>N M g"]) auto
hoelzl@38656
  1283
qed
hoelzl@38656
  1284
hoelzl@56996
  1285
lemma nn_integral_suminf:
hoelzl@47694
  1286
  assumes f: "\<And>i. f i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> f i x"
hoelzl@56996
  1287
  shows "(\<integral>\<^sup>+ x. (\<Sum>i. f i x) \<partial>M) = (\<Sum>i. integral\<^sup>N M (f i))"
hoelzl@38656
  1288
proof -
hoelzl@47694
  1289
  have all_pos: "AE x in M. \<forall>i. 0 \<le> f i x"
hoelzl@41981
  1290
    using assms by (auto simp: AE_all_countable)
hoelzl@56996
  1291
  have "(\<Sum>i. integral\<^sup>N M (f i)) = (SUP n. \<Sum>i<n. integral\<^sup>N M (f i))"
hoelzl@56996
  1292
    using nn_integral_nonneg by (rule suminf_ereal_eq_SUP)
wenzelm@53015
  1293
  also have "\<dots> = (SUP n. \<integral>\<^sup>+x. (\<Sum>i<n. f i x) \<partial>M)"
hoelzl@56996
  1294
    unfolding nn_integral_setsum[OF f] ..
wenzelm@53015
  1295
  also have "\<dots> = \<integral>\<^sup>+x. (SUP n. \<Sum>i<n. f i x) \<partial>M" using f all_pos
hoelzl@56996
  1296
    by (intro nn_integral_monotone_convergence_SUP_AE[symmetric])
hoelzl@41981
  1297
       (elim AE_mp, auto simp: setsum_nonneg simp del: setsum_lessThan_Suc intro!: AE_I2 setsum_mono3)
wenzelm@53015
  1298
  also have "\<dots> = \<integral>\<^sup>+x. (\<Sum>i. f i x) \<partial>M" using all_pos
hoelzl@56996
  1299
    by (intro nn_integral_cong_AE) (auto simp: suminf_ereal_eq_SUP)
hoelzl@41981
  1300
  finally show ?thesis by simp
hoelzl@38656
  1301
qed
hoelzl@38656
  1302
hoelzl@56996
  1303
lemma nn_integral_mult_bounded_inf:
hoelzl@56993
  1304
  assumes f: "f \<in> borel_measurable M" "(\<integral>\<^sup>+x. f x \<partial>M) < \<infinity>"
hoelzl@56993
  1305
    and c: "0 \<le> c" "c \<noteq> \<infinity>" and ae: "AE x in M. g x \<le> c * f x"
hoelzl@56993
  1306
  shows "(\<integral>\<^sup>+x. g x \<partial>M) < \<infinity>"
hoelzl@56993
  1307
proof -
hoelzl@56993
  1308
  have "(\<integral>\<^sup>+x. g x \<partial>M) \<le> (\<integral>\<^sup>+x. c * f x \<partial>M)"
hoelzl@56996
  1309
    by (intro nn_integral_mono_AE ae)
hoelzl@56993
  1310
  also have "(\<integral>\<^sup>+x. c * f x \<partial>M) < \<infinity>"
hoelzl@56996
  1311
    using c f by (subst nn_integral_cmult) auto
hoelzl@56993
  1312
  finally show ?thesis .
hoelzl@56993
  1313
qed
hoelzl@56993
  1314
hoelzl@38656
  1315
text {* Fatou's lemma: convergence theorem on limes inferior *}
hoelzl@56993
  1316
hoelzl@56996
  1317
lemma nn_integral_liminf:
hoelzl@43920
  1318
  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@47694
  1319
  assumes u: "\<And>i. u i \<in> borel_measurable M" "\<And>i. AE x in M. 0 \<le> u i x"
hoelzl@56996
  1320
  shows "(\<integral>\<^sup>+ x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^sup>N M (u n))"
hoelzl@38656
  1321
proof -
hoelzl@47694
  1322
  have pos: "AE x in M. \<forall>i. 0 \<le> u i x" using u by (auto simp: AE_all_countable)
wenzelm@53015
  1323
  have "(\<integral>\<^sup>+ x. liminf (\<lambda>n. u n x) \<partial>M) =
wenzelm@53015
  1324
    (SUP n. \<integral>\<^sup>+ x. (INF i:{n..}. u i x) \<partial>M)"
haftmann@56212
  1325
    unfolding liminf_SUP_INF using pos u
hoelzl@56996
  1326
    by (intro nn_integral_monotone_convergence_SUP_AE)
hoelzl@44937
  1327
       (elim AE_mp, auto intro!: AE_I2 intro: INF_greatest INF_superset_mono)
hoelzl@56996
  1328
  also have "\<dots> \<le> liminf (\<lambda>n. integral\<^sup>N M (u n))"
haftmann@56212
  1329
    unfolding liminf_SUP_INF
hoelzl@56996
  1330
    by (auto intro!: SUP_mono exI INF_greatest nn_integral_mono INF_lower)
hoelzl@38656
  1331
  finally show ?thesis .
hoelzl@35582
  1332
qed
hoelzl@35582
  1333
hoelzl@56993
  1334
lemma le_Limsup:
hoelzl@56993
  1335
  "F \<noteq> bot \<Longrightarrow> eventually (\<lambda>x. c \<le> g x) F \<Longrightarrow> c \<le> Limsup F g"
hoelzl@56993
  1336
  using Limsup_mono[of "\<lambda>_. c" g F] by (simp add: Limsup_const)
hoelzl@56993
  1337
hoelzl@56993
  1338
lemma Limsup_le:
hoelzl@56993
  1339
  "F \<noteq> bot \<Longrightarrow> eventually (\<lambda>x. f x \<le> c) F \<Longrightarrow> Limsup F f \<le> c"
hoelzl@56993
  1340
  using Limsup_mono[of f "\<lambda>_. c" F] by (simp add: Limsup_const)
hoelzl@56993
  1341
hoelzl@56993
  1342
lemma ereal_mono_minus_cancel:
hoelzl@56993
  1343
  fixes a b c :: ereal
hoelzl@56993
  1344
  shows "c - a \<le> c - b \<Longrightarrow> 0 \<le> c \<Longrightarrow> c < \<infinity> \<Longrightarrow> b \<le> a"
hoelzl@56993
  1345
  by (cases a b c rule: ereal3_cases) auto
hoelzl@56993
  1346
hoelzl@56996
  1347
lemma nn_integral_limsup:
hoelzl@56993
  1348
  fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
hoelzl@56993
  1349
  assumes [measurable]: "\<And>i. u i \<in> borel_measurable M" "w \<in> borel_measurable M"
hoelzl@56993
  1350
  assumes bounds: "\<And>i. AE x in M. 0 \<le> u i x" "\<And>i. AE x in M. u i x \<le> w x" and w: "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
hoelzl@56996
  1351
  shows "limsup (\<lambda>n. integral\<^sup>N M (u n)) \<le> (\<integral>\<^sup>+ x. limsup (\<lambda>n. u n x) \<partial>M)"
hoelzl@56993
  1352
proof -
hoelzl@56993
  1353
  have bnd: "AE x in M. \<forall>i. 0 \<le> u i x \<and> u i x \<le> w x"
hoelzl@56993
  1354
    using bounds by (auto simp: AE_all_countable)
hoelzl@56993
  1355
hoelzl@56993
  1356
  from bounds[of 0] have w_nonneg: "AE x in M. 0 \<le> w x"
hoelzl@56993
  1357
    by auto
hoelzl@56993
  1358
hoelzl@56993
  1359
  have "(\<integral>\<^sup>+x. w x \<partial>M) - (\<integral>\<^sup>+x. limsup (\<lambda>n. u n x) \<partial>M) = (\<integral>\<^sup>+x. w x - limsup (\<lambda>n. u n x) \<partial>M)"
hoelzl@56996
  1360
  proof (intro nn_integral_diff[symmetric])
hoelzl@56993
  1361
    show "AE x in M. 0 \<le> limsup (\<lambda>n. u n x)"
hoelzl@56993
  1362
      using bnd by (auto intro!: le_Limsup)
hoelzl@56993
  1363
    show "AE x in M. limsup (\<lambda>n. u n x) \<le> w x"
hoelzl@56993
  1364
      using bnd by (auto intro!: Limsup_le)
hoelzl@56993
  1365
    then have "(\<integral>\<^sup>+x. limsup (\<lambda>n. u n x) \<partial>M) < \<infinity>"
hoelzl@56996
  1366
      by (intro nn_integral_mult_bounded_inf[OF _ w, of 1]) auto
hoelzl@56993
  1367
    then show "(\<integral>\<^sup>+x. limsup (\<lambda>n. u n x) \<partial>M) \<noteq> \<infinity>"
hoelzl@56993
  1368
      by simp
hoelzl@56993
  1369
  qed auto
hoelzl@56993
  1370
  also have "\<dots> = (\<integral>\<^sup>+x. liminf (\<lambda>n. w x - u n x) \<partial>M)"
hoelzl@56993
  1371
    using w_nonneg
hoelzl@56996
  1372
    by (intro nn_integral_cong_AE, eventually_elim)
hoelzl@56993
  1373
       (auto intro!: liminf_ereal_cminus[symmetric])
hoelzl@56993
  1374
  also have "\<dots> \<le> liminf (\<lambda>n. \<integral>\<^sup>+x. w x - u n x \<partial>M)"
hoelzl@56996
  1375
  proof (rule nn_integral_liminf)
hoelzl@56993
  1376
    fix i show "AE x in M. 0 \<le> w x - u i x"
hoelzl@56993
  1377
      using bounds[of i] by eventually_elim (auto intro: ereal_diff_positive)
hoelzl@56993
  1378
  qed simp
hoelzl@56993
  1379
  also have "(\<lambda>n. \<integral>\<^sup>+x. w x - u n x \<partial>M) = (\<lambda>n. (\<integral>\<^sup>+x. w x \<partial>M) - (\<integral>\<^sup>+x. u n x \<partial>M))"
hoelzl@56996
  1380
  proof (intro ext nn_integral_diff)
hoelzl@56993
  1381
    fix i have "(\<integral>\<^sup>+x. u i x \<partial>M) < \<infinity>"
hoelzl@56996
  1382
      using bounds by (intro nn_integral_mult_bounded_inf[OF _ w, of 1]) auto
hoelzl@56993
  1383
    then show "(\<integral>\<^sup>+x. u i x \<partial>M) \<noteq> \<infinity>" by simp
hoelzl@56993
  1384
  qed (insert bounds, auto)
hoelzl@56993
  1385
  also have "liminf (\<lambda>n. (\<integral>\<^sup>+x. w x \<partial>M) - (\<integral>\<^sup>+x. u n x \<partial>M)) = (\<integral>\<^sup>+x. w x \<partial>M) - limsup (\<lambda>n. \<integral>\<^sup>+x. u n x \<partial>M)"
hoelzl@56993
  1386
    using w by (intro liminf_ereal_cminus) auto
hoelzl@56993
  1387
  finally show ?thesis
hoelzl@56996
  1388
    by (rule ereal_mono_minus_cancel) (intro w nn_integral_nonneg)+
hoelzl@56993
  1389
qed
hoelzl@56993
  1390
hoelzl@57025
  1391
lemma nn_integral_LIMSEQ:
hoelzl@57025
  1392
  assumes f: "incseq f" "\<And>i. f i \<in> borel_measurable M" "\<And>n x. 0 \<le> f n x"
hoelzl@57025
  1393
    and u: "\<And>x. (\<lambda>i. f i x) ----> u x"
hoelzl@57025
  1394
  shows "(\<lambda>n. integral\<^sup>N M (f n)) ----> integral\<^sup>N M u"
hoelzl@57025
  1395
proof -
hoelzl@57025
  1396
  have "(\<lambda>n. integral\<^sup>N M (f n)) ----> (SUP n. integral\<^sup>N M (f n))"
hoelzl@57025
  1397
    using f by (intro LIMSEQ_SUP[of "\<lambda>n. integral\<^sup>N M (f n)"] incseq_nn_integral)
hoelzl@57025
  1398
  also have "(SUP n. integral\<^sup>N M (f n)) = integral\<^sup>N M (\<lambda>x. SUP n. f n x)"
hoelzl@57025
  1399
    using f by (intro nn_integral_monotone_convergence_SUP[symmetric])
hoelzl@57025
  1400
  also have "integral\<^sup>N M (\<lambda>x. SUP n. f n x) = integral\<^sup>N M (\<lambda>x. u x)"
hoelzl@57025
  1401
    using f by (subst SUP_Lim_ereal[OF _ u]) (auto simp: incseq_def le_fun_def)
hoelzl@57025
  1402
  finally show ?thesis .
hoelzl@57025
  1403
qed
hoelzl@57025
  1404
hoelzl@56996
  1405
lemma nn_integral_dominated_convergence:
hoelzl@56993
  1406
  assumes [measurable]:
hoelzl@56993
  1407
       "\<And>i. u i \<in> borel_measurable M" "u' \<in> borel_measurable M" "w \<in> borel_measurable M"
hoelzl@56993
  1408
    and bound: "\<And>j. AE x in M. 0 \<le> u j x" "\<And>j. AE x in M. u j x \<le> w x"
hoelzl@56993
  1409
    and w: "(\<integral>\<^sup>+x. w x \<partial>M) < \<infinity>"
hoelzl@56993
  1410
    and u': "AE x in M. (\<lambda>i. u i x) ----> u' x"
hoelzl@56993
  1411
  shows "(\<lambda>i. (\<integral>\<^sup>+x. u i x \<partial>M)) ----> (\<integral>\<^sup>+x. u' x \<partial>M)"
hoelzl@56993
  1412
proof -
hoelzl@56996
  1413
  have "limsup (\<lambda>n. integral\<^sup>N M (u n)) \<le> (\<integral>\<^sup>+ x. limsup (\<lambda>n. u n x) \<partial>M)"
hoelzl@56996
  1414
    by (intro nn_integral_limsup[OF _ _ bound w]) auto
hoelzl@56993
  1415
  moreover have "(\<integral>\<^sup>+ x. limsup (\<lambda>n. u n x) \<partial>M) = (\<integral>\<^sup>+ x. u' x \<partial>M)"
hoelzl@56996
  1416
    using u' by (intro nn_integral_cong_AE, eventually_elim) (metis tendsto_iff_Liminf_eq_Limsup sequentially_bot)
hoelzl@56993
  1417
  moreover have "(\<integral>\<^sup>+ x. liminf (\<lambda>n. u n x) \<partial>M) = (\<integral>\<^sup>+ x. u' x \<partial>M)"
hoelzl@56996
  1418
    using u' by (intro nn_integral_cong_AE, eventually_elim) (metis tendsto_iff_Liminf_eq_Limsup sequentially_bot)
hoelzl@56996
  1419
  moreover have "(\<integral>\<^sup>+x. liminf (\<lambda>n. u n x) \<partial>M) \<le> liminf (\<lambda>n. integral\<^sup>N M (u n))"
hoelzl@56996
  1420
    by (intro nn_integral_liminf[OF _ bound(1)]) auto
hoelzl@56996
  1421
  moreover have "liminf (\<lambda>n. integral\<^sup>N M (u n)) \<le> limsup (\<lambda>n. integral\<^sup>N M (u n))"
hoelzl@56993
  1422
    by (intro Liminf_le_Limsup sequentially_bot)
hoelzl@56993
  1423
  ultimately show ?thesis
hoelzl@56993
  1424
    by (intro Liminf_eq_Limsup) auto
hoelzl@56993
  1425
qed
hoelzl@56993
  1426
hoelzl@56996
  1427
lemma nn_integral_null_set:
wenzelm@53015
  1428
  assumes "N \<in> null_sets M" shows "(\<integral>\<^sup>+ x. u x * indicator N x \<partial>M) = 0"
hoelzl@38656
  1429
proof -
wenzelm@53015
  1430
  have "(\<integral>\<^sup>+ x. u x * indicator N x \<partial>M) = (\<integral>\<^sup>+ x. 0 \<partial>M)"
hoelzl@56996
  1431
  proof (intro nn_integral_cong_AE AE_I)
hoelzl@40859
  1432
    show "{x \<in> space M. u x * indicator N x \<noteq> 0} \<subseteq> N"
hoelzl@40859
  1433
      by (auto simp: indicator_def)
hoelzl@47694
  1434
    show "(emeasure M) N = 0" "N \<in> sets M"
hoelzl@40859
  1435
      using assms by auto
hoelzl@35582
  1436
  qed
hoelzl@40859
  1437
  then show ?thesis by simp
hoelzl@38656
  1438
qed
hoelzl@35582
  1439
hoelzl@56996
  1440
lemma nn_integral_0_iff:
hoelzl@47694
  1441
  assumes u: "u \<in> borel_measurable M" and pos: "AE x in M. 0 \<le> u x"
hoelzl@56996
  1442
  shows "integral\<^sup>N M u = 0 \<longleftrightarrow> emeasure M {x\<in>space M. u x \<noteq> 0} = 0"
hoelzl@47694
  1443
    (is "_ \<longleftrightarrow> (emeasure M) ?A = 0")
hoelzl@35582
  1444
proof -
hoelzl@56996
  1445
  have u_eq: "(\<integral>\<^sup>+ x. u x * indicator ?A x \<partial>M) = integral\<^sup>N M u"
hoelzl@56996
  1446
    by (auto intro!: nn_integral_cong simp: indicator_def)
hoelzl@38656
  1447
  show ?thesis
hoelzl@38656
  1448
  proof
hoelzl@47694
  1449
    assume "(emeasure M) ?A = 0"
hoelzl@56996
  1450
    with nn_integral_null_set[of ?A M u] u
hoelzl@56996
  1451
    show "integral\<^sup>N M u = 0" by (simp add: u_eq null_sets_def)
hoelzl@38656
  1452
  next
hoelzl@43920
  1453
    { fix r :: ereal and n :: nat assume gt_1: "1 \<le> real n * r"
hoelzl@43920
  1454
      then have "0 < real n * r" by (cases r) (auto split: split_if_asm simp: one_ereal_def)
hoelzl@43920
  1455
      then have "0 \<le> r" by (auto simp add: ereal_zero_less_0_iff) }
hoelzl@41981
  1456
    note gt_1 = this
hoelzl@56996
  1457
    assume *: "integral\<^sup>N M u = 0"
wenzelm@46731
  1458
    let ?M = "\<lambda>n. {x \<in> space M. 1 \<le> real (n::nat) * u x}"
hoelzl@47694
  1459
    have "0 = (SUP n. (emeasure M) (?M n \<inter> ?A))"
hoelzl@38656
  1460
    proof -
hoelzl@41981
  1461
      { fix n :: nat
hoelzl@56996
  1462
        from nn_integral_Markov_inequality[OF u pos, of ?A "ereal (real n)"]
hoelzl@47694
  1463
        have "(emeasure M) (?M n \<inter> ?A) \<le> 0" unfolding u_eq * using u by simp
hoelzl@47694
  1464
        moreover have "0 \<le> (emeasure M) (?M n \<inter> ?A)" using u by auto
hoelzl@47694
  1465
        ultimately have "(emeasure M) (?M n \<inter> ?A) = 0" by auto }
hoelzl@38656
  1466
      thus ?thesis by simp
hoelzl@35582
  1467
    qed
hoelzl@47694
  1468
    also have "\<dots> = (emeasure M) (\<Union>n. ?M n \<inter> ?A)"
hoelzl@47694
  1469
    proof (safe intro!: SUP_emeasure_incseq)
hoelzl@38656
  1470
      fix n show "?M n \<inter> ?A \<in> sets M"
immler@50244
  1471
        using u by (auto intro!: sets.Int)
hoelzl@38656
  1472
    next
hoelzl@41981
  1473
      show "incseq (\<lambda>n. {x \<in> space M. 1 \<le> real n * u x} \<inter> {x \<in> space M. u x \<noteq> 0})"
hoelzl@41981
  1474
      proof (safe intro!: incseq_SucI)
hoelzl@41981
  1475
        fix n :: nat and x
hoelzl@41981
  1476
        assume *: "1 \<le> real n * u x"
wenzelm@53374
  1477
        also from gt_1[OF *] have "real n * u x \<le> real (Suc n) * u x"
hoelzl@43920
  1478
          using `0 \<le> u x` by (auto intro!: ereal_mult_right_mono)
hoelzl@41981
  1479
        finally show "1 \<le> real (Suc n) * u x" by auto
hoelzl@41981
  1480
      qed
hoelzl@38656
  1481
    qed
hoelzl@47694
  1482
    also have "\<dots> = (emeasure M) {x\<in>space M. 0 < u x}"
hoelzl@47694
  1483
    proof (safe intro!: arg_cong[where f="(emeasure M)"] dest!: gt_1)
hoelzl@41981
  1484
      fix x assume "0 < u x" and [simp, intro]: "x \<in> space M"
hoelzl@38656
  1485
      show "x \<in> (\<Union>n. ?M n \<inter> ?A)"
hoelzl@38656
  1486
      proof (cases "u x")
hoelzl@41981
  1487
        case (real r) with `0 < u x` have "0 < r" by auto
hoelzl@41981
  1488
        obtain j :: nat where "1 / r \<le> real j" using real_arch_simple ..
hoelzl@41981
  1489
        hence "1 / r * r \<le> real j * r" unfolding mult_le_cancel_right using `0 < r` by auto
hoelzl@41981
  1490
        hence "1 \<le> real j * r" using real `0 < r` by auto
hoelzl@43920
  1491
        thus ?thesis using `0 < r` real by (auto simp: one_ereal_def)
hoelzl@41981
  1492
      qed (insert `0 < u x`, auto)
hoelzl@41981
  1493
    qed auto
hoelzl@47694
  1494
    finally have "(emeasure M) {x\<in>space M. 0 < u x} = 0" by simp
hoelzl@41981
  1495
    moreover
hoelzl@47694
  1496
    from pos have "AE x in M. \<not> (u x < 0)" by auto
hoelzl@47694
  1497
    then have "(emeasure M) {x\<in>space M. u x < 0} = 0"
hoelzl@47694
  1498
      using AE_iff_null[of M] u by auto
hoelzl@47694
  1499
    moreover have "(emeasure M) {x\<in>space M. u x \<noteq> 0} = (emeasure M) {x\<in>space M. u x < 0} + (emeasure M) {x\<in>space M. 0 < u x}"
hoelzl@47694
  1500
      using u by (subst plus_emeasure) (auto intro!: arg_cong[where f="emeasure M"])
hoelzl@47694
  1501
    ultimately show "(emeasure M) ?A = 0" by simp
hoelzl@35582
  1502
  qed
hoelzl@35582
  1503
qed
hoelzl@35582
  1504
hoelzl@56996
  1505
lemma nn_integral_0_iff_AE:
hoelzl@41705
  1506
  assumes u: "u \<in> borel_measurable M"
hoelzl@56996
  1507
  shows "integral\<^sup>N M u = 0 \<longleftrightarrow> (AE x in M. u x \<le> 0)"
hoelzl@41705
  1508
proof -
hoelzl@41981
  1509
  have sets: "{x\<in>space M. max 0 (u x) \<noteq> 0} \<in> sets M"
hoelzl@41705
  1510
    using u by auto
hoelzl@56996
  1511
  from nn_integral_0_iff[of "\<lambda>x. max 0 (u x)"]
hoelzl@56996
  1512
  have "integral\<^sup>N M u = 0 \<longleftrightarrow> (AE x in M. max 0 (u x) = 0)"
hoelzl@56996
  1513
    unfolding nn_integral_max_0
hoelzl@47694
  1514
    using AE_iff_null[OF sets] u by auto
hoelzl@47694
  1515
  also have "\<dots> \<longleftrightarrow> (AE x in M. u x \<le> 0)" by (auto split: split_max)
hoelzl@41981
  1516
  finally show ?thesis .
hoelzl@41705
  1517
qed
hoelzl@41705
  1518
hoelzl@56996
  1519
lemma AE_iff_nn_integral: 
hoelzl@56996
  1520
  "{x\<in>space M. P x} \<in> sets M \<Longrightarrow> (AE x in M. P x) \<longleftrightarrow> integral\<^sup>N M (indicator {x. \<not> P x}) = 0"
hoelzl@56996
  1521
  by (subst nn_integral_0_iff_AE) (auto simp: one_ereal_def zero_ereal_def
immler@50244
  1522
    sets.sets_Collect_neg indicator_def[abs_def] measurable_If)
hoelzl@50001
  1523
hoelzl@56996
  1524
lemma nn_integral_const_If:
wenzelm@53015
  1525
  "(\<integral>\<^sup>+x. a \<partial>M) = (if 0 \<le> a then a * (emeasure M) (space M) else 0)"
hoelzl@56996
  1526
  by (auto intro!: nn_integral_0_iff_AE[THEN iffD2])
hoelzl@42991
  1527
hoelzl@56996
  1528
lemma nn_integral_subalgebra:
hoelzl@49799
  1529
  assumes f: "f \<in> borel_measurable N" "\<And>x. 0 \<le> f x"
hoelzl@47694
  1530
  and N: "sets N \<subseteq> sets M" "space N = space M" "\<And>A. A \<in> sets N \<Longrightarrow> emeasure N A = emeasure M A"
hoelzl@56996
  1531
  shows "integral\<^sup>N N f = integral\<^sup>N M f"
hoelzl@39092
  1532
proof -
hoelzl@49799
  1533
  have [simp]: "\<And>f :: 'a \<Rightarrow> ereal. f \<in> borel_measurable N \<Longrightarrow> f \<in> borel_measurable M"
hoelzl@49799
  1534
    using N by (auto simp: measurable_def)
hoelzl@49799
  1535
  have [simp]: "\<And>P. (AE x in N. P x) \<Longrightarrow> (AE x in M. P x)"
hoelzl@49799
  1536
    using N by (auto simp add: eventually_ae_filter null_sets_def)
hoelzl@49799
  1537
  have [simp]: "\<And>A. A \<in> sets N \<Longrightarrow> A \<in> sets M"
hoelzl@49799
  1538
    using N by auto
hoelzl@49799
  1539
  from f show ?thesis
hoelzl@49799
  1540
    apply induct
hoelzl@56996
  1541
    apply (simp_all add: nn_integral_add nn_integral_cmult nn_integral_monotone_convergence_SUP N)
hoelzl@56996
  1542
    apply (auto intro!: nn_integral_cong cong: nn_integral_cong simp: N(2)[symmetric])
hoelzl@49799
  1543
    done
hoelzl@39092
  1544
qed
hoelzl@39092
  1545
hoelzl@56996
  1546
lemma nn_integral_nat_function:
hoelzl@50097
  1547
  fixes f :: "'a \<Rightarrow> nat"
hoelzl@50097
  1548
  assumes "f \<in> measurable M (count_space UNIV)"
wenzelm@53015
  1549
  shows "(\<integral>\<^sup>+x. ereal (of_nat (f x)) \<partial>M) = (\<Sum>t. emeasure M {x\<in>space M. t < f x})"
hoelzl@50097
  1550
proof -
hoelzl@50097
  1551
  def F \<equiv> "\<lambda>i. {x\<in>space M. i < f x}"
hoelzl@50097
  1552
  with assms have [measurable]: "\<And>i. F i \<in> sets M"
hoelzl@50097
  1553
    by auto
hoelzl@50097
  1554
hoelzl@50097
  1555
  { fix x assume "x \<in> space M"
hoelzl@50097
  1556
    have "(\<lambda>i. if i < f x then 1 else 0) sums (of_nat (f x)::real)"
hoelzl@50097
  1557
      using sums_If_finite[of "\<lambda>i. i < f x" "\<lambda>_. 1::real"] by simp
hoelzl@50097
  1558
    then have "(\<lambda>i. ereal(if i < f x then 1 else 0)) sums (ereal(of_nat(f x)))"
hoelzl@50097
  1559
      unfolding sums_ereal .
hoelzl@50097
  1560
    moreover have "\<And>i. ereal (if i < f x then 1 else 0) = indicator (F i) x"
hoelzl@50097
  1561
      using `x \<in> space M` by (simp add: one_ereal_def F_def)
hoelzl@50097
  1562
    ultimately have "ereal(of_nat(f x)) = (\<Sum>i. indicator (F i) x)"
hoelzl@50097
  1563
      by (simp add: sums_iff) }
wenzelm@53015
  1564
  then have "(\<integral>\<^sup>+x. ereal (of_nat (f x)) \<partial>M) = (\<integral>\<^sup>+x. (\<Sum>i. indicator (F i) x) \<partial>M)"
hoelzl@56996
  1565
    by (simp cong: nn_integral_cong)
hoelzl@50097
  1566
  also have "\<dots> = (\<Sum>i. emeasure M (F i))"
hoelzl@56996
  1567
    by (simp add: nn_integral_suminf)
hoelzl@50097
  1568
  finally show ?thesis
hoelzl@50097
  1569
    by (simp add: F_def)
hoelzl@50097
  1570
qed
hoelzl@50097
  1571
hoelzl@56994
  1572
subsection {* Integral under concrete measures *}
hoelzl@56994
  1573
hoelzl@56994
  1574
subsubsection {* Distributions *}
hoelzl@47694
  1575
hoelzl@56996
  1576
lemma nn_integral_distr':
hoelzl@49797
  1577
  assumes T: "T \<in> measurable M M'"
hoelzl@49799
  1578
  and f: "f \<in> borel_measurable (distr M M' T)" "\<And>x. 0 \<le> f x"
hoelzl@56996
  1579
  shows "integral\<^sup>N (distr M M' T) f = (\<integral>\<^sup>+ x. f (T x) \<partial>M)"
hoelzl@49797
  1580
  using f 
hoelzl@49797
  1581
proof induct
hoelzl@49797
  1582
  case (cong f g)
hoelzl@49799
  1583
  with T show ?case
hoelzl@56996
  1584
    apply (subst nn_integral_cong[of _ f g])
hoelzl@49799
  1585
    apply simp
hoelzl@56996
  1586
    apply (subst nn_integral_cong[of _ "\<lambda>x. f (T x)" "\<lambda>x. g (T x)"])
hoelzl@49799
  1587
    apply (simp add: measurable_def Pi_iff)
hoelzl@49799
  1588
    apply simp
hoelzl@49797
  1589
    done
hoelzl@49797
  1590
next
hoelzl@49797
  1591
  case (set A)
hoelzl@49797
  1592
  then have eq: "\<And>x. x \<in> space M \<Longrightarrow> indicator A (T x) = indicator (T -` A \<inter> space M) x"
hoelzl@49797
  1593
    by (auto simp: indicator_def)
hoelzl@49797
  1594
  from set T show ?case
hoelzl@56996
  1595
    by (subst nn_integral_cong[OF eq])
hoelzl@56996
  1596
       (auto simp add: emeasure_distr intro!: nn_integral_indicator[symmetric] measurable_sets)
hoelzl@56996
  1597
qed (simp_all add: measurable_compose[OF T] T nn_integral_cmult nn_integral_add
hoelzl@56996
  1598
                   nn_integral_monotone_convergence_SUP le_fun_def incseq_def)
hoelzl@47694
  1599
hoelzl@56996
  1600
lemma nn_integral_distr:
hoelzl@56996
  1601
  "T \<in> measurable M M' \<Longrightarrow> f \<in> borel_measurable M' \<Longrightarrow> integral\<^sup>N (distr M M' T) f = (\<integral>\<^sup>+ x. f (T x) \<partial>M)"
hoelzl@56996
  1602
  by (subst (1 2) nn_integral_max_0[symmetric])
hoelzl@56996
  1603
     (simp add: nn_integral_distr')
hoelzl@35692
  1604
hoelzl@56994
  1605
subsubsection {* Counting space *}
hoelzl@47694
  1606
hoelzl@47694
  1607
lemma simple_function_count_space[simp]:
hoelzl@47694
  1608
  "simple_function (count_space A) f \<longleftrightarrow> finite (f ` A)"
hoelzl@47694
  1609
  unfolding simple_function_def by simp
hoelzl@47694
  1610
hoelzl@56996
  1611
lemma nn_integral_count_space:
hoelzl@47694
  1612
  assumes A: "finite {a\<in>A. 0 < f a}"
hoelzl@56996
  1613
  shows "integral\<^sup>N (count_space A) f = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
hoelzl@35582
  1614
proof -
wenzelm@53015
  1615
  have *: "(\<integral>\<^sup>+x. max 0 (f x) \<partial>count_space A) =
wenzelm@53015
  1616
    (\<integral>\<^sup>+ x. (\<Sum>a|a\<in>A \<and> 0 < f a. f a * indicator {a} x) \<partial>count_space A)"
hoelzl@56996
  1617
    by (auto intro!: nn_integral_cong
hoelzl@47694
  1618
             simp add: indicator_def if_distrib setsum_cases[OF A] max_def le_less)
wenzelm@53015
  1619
  also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. \<integral>\<^sup>+ x. f a * indicator {a} x \<partial>count_space A)"
hoelzl@56996
  1620
    by (subst nn_integral_setsum)
hoelzl@47694
  1621
       (simp_all add: AE_count_space ereal_zero_le_0_iff less_imp_le)
hoelzl@47694
  1622
  also have "\<dots> = (\<Sum>a|a\<in>A \<and> 0 < f a. f a)"
hoelzl@56996
  1623
    by (auto intro!: setsum_cong simp: nn_integral_cmult_indicator one_ereal_def[symmetric])
hoelzl@56996
  1624
  finally show ?thesis by (simp add: nn_integral_max_0)
hoelzl@47694
  1625
qed
hoelzl@47694
  1626
hoelzl@56996
  1627
lemma nn_integral_count_space_finite:
wenzelm@53015
  1628
    "finite A \<Longrightarrow> (\<integral>\<^sup>+x. f x \<partial>count_space A) = (\<Sum>a\<in>A. max 0 (f a))"
hoelzl@56996
  1629
  by (subst nn_integral_max_0[symmetric])
hoelzl@56996
  1630
     (auto intro!: setsum_mono_zero_left simp: nn_integral_count_space less_le)
hoelzl@47694
  1631
hoelzl@54418
  1632
lemma emeasure_UN_countable:
hoelzl@54418
  1633
  assumes sets: "\<And>i. i \<in> I \<Longrightarrow> X i \<in> sets M" and I: "countable I" 
hoelzl@54418
  1634
  assumes disj: "disjoint_family_on X I"
hoelzl@54418
  1635
  shows "emeasure M (UNION I X) = (\<integral>\<^sup>+i. emeasure M (X i) \<partial>count_space I)"
hoelzl@54418
  1636
proof cases
hoelzl@54418
  1637
  assume "finite I" with sets disj show ?thesis
hoelzl@54418
  1638
    by (subst setsum_emeasure[symmetric])
hoelzl@56996
  1639
       (auto intro!: setsum_cong simp add: max_def subset_eq nn_integral_count_space_finite emeasure_nonneg)
hoelzl@54418
  1640
next
hoelzl@54418
  1641
  assume f: "\<not> finite I"
hoelzl@54418
  1642
  then have [intro]: "I \<noteq> {}" by auto
hoelzl@54418
  1643
  from from_nat_into_inj_infinite[OF I f] from_nat_into[OF this] disj
hoelzl@54418
  1644
  have disj2: "disjoint_family (\<lambda>i. X (from_nat_into I i))"
hoelzl@54418
  1645
    unfolding disjoint_family_on_def by metis
hoelzl@54418
  1646
hoelzl@54418
  1647
  from f have "bij_betw (from_nat_into I) UNIV I"
hoelzl@54418
  1648
    using bij_betw_from_nat_into[OF I] by simp
hoelzl@54418
  1649
  then have "(\<Union>i\<in>I. X i) = (\<Union>i. (X \<circ> from_nat_into I) i)"
haftmann@56154
  1650
    unfolding SUP_def image_comp [symmetric] by (simp add: bij_betw_def)
hoelzl@54418
  1651
  then have "emeasure M (UNION I X) = emeasure M (\<Union>i. X (from_nat_into I i))"
hoelzl@54418
  1652
    by simp
hoelzl@54418
  1653
  also have "\<dots> = (\<Sum>i. emeasure M (X (from_nat_into I i)))"
hoelzl@54418
  1654
    by (intro suminf_emeasure[symmetric] disj disj2) (auto intro!: sets from_nat_into[OF `I \<noteq> {}`])
hoelzl@54418
  1655
  also have "\<dots> = (\<Sum>n. \<integral>\<^sup>+i. emeasure M (X i) * indicator {from_nat_into I n} i \<partial>count_space I)"
hoelzl@54418
  1656
  proof (intro arg_cong[where f=suminf] ext)
hoelzl@54418
  1657
    fix i
hoelzl@54418
  1658
    have eq: "{a \<in> I. 0 < emeasure M (X a) * indicator {from_nat_into I i} a}
hoelzl@54418
  1659
     = (if 0 < emeasure M (X (from_nat_into I i)) then {from_nat_into I i} else {})"
hoelzl@54418
  1660
     using ereal_0_less_1
hoelzl@54418
  1661
     by (auto simp: ereal_zero_less_0_iff indicator_def from_nat_into `I \<noteq> {}` simp del: ereal_0_less_1)
hoelzl@54418
  1662
    have "(\<integral>\<^sup>+ ia. emeasure M (X ia) * indicator {from_nat_into I i} ia \<partial>count_space I) =
hoelzl@54418
  1663
      (if 0 < emeasure M (X (from_nat_into I i)) then emeasure M (X (from_nat_into I i)) else 0)"
hoelzl@56996
  1664
      by (subst nn_integral_count_space) (simp_all add: eq)
hoelzl@54418
  1665
    also have "\<dots> = emeasure M (X (from_nat_into I i))"
hoelzl@54418
  1666
      by (simp add: less_le emeasure_nonneg)
hoelzl@54418
  1667
    finally show "emeasure M (X (from_nat_into I i)) =
hoelzl@54418
  1668
         \<integral>\<^sup>+ ia. emeasure M (X ia) * indicator {from_nat_into I i} ia \<partial>count_space I" ..
hoelzl@54418
  1669
  qed
hoelzl@54418
  1670
  also have "\<dots> = (\<integral>\<^sup>+i. emeasure M (X i) \<partial>count_space I)"
hoelzl@56996
  1671
    apply (subst nn_integral_suminf[symmetric])
hoelzl@56996
  1672
    apply (auto simp: emeasure_nonneg intro!: nn_integral_cong)
hoelzl@54418
  1673
  proof -
hoelzl@54418
  1674
    fix x assume "x \<in> I"
hoelzl@54418
  1675
    then have "(\<Sum>i. emeasure M (X x) * indicator {from_nat_into I i} x) = (\<Sum>i\<in>{to_nat_on I x}. emeasure M (X x) * indicator {from_nat_into I i} x)"
hoelzl@54418
  1676
      by (intro suminf_finite) (auto simp: indicator_def I f)
hoelzl@54418
  1677
    also have "\<dots> = emeasure M (X x)"
hoelzl@54418
  1678
      by (simp add: I f `x\<in>I`)
hoelzl@54418
  1679
    finally show "(\<Sum>i. emeasure M (X x) * indicator {from_nat_into I i} x) = emeasure M (X x)" .
hoelzl@54418
  1680
  qed
hoelzl@54418
  1681
  finally show ?thesis .
hoelzl@54418
  1682
qed
hoelzl@54418
  1683
hoelzl@57025
  1684
lemma nn_integral_count_space_nat:
hoelzl@57025
  1685
  fixes f :: "nat \<Rightarrow> ereal"
hoelzl@57025
  1686
  assumes nonneg: "\<And>i. 0 \<le> f i"
hoelzl@57025
  1687
  shows "(\<integral>\<^sup>+i. f i \<partial>count_space UNIV) = (\<Sum>i. f i)"
hoelzl@57025
  1688
proof -
hoelzl@57025
  1689
  have "(\<integral>\<^sup>+i. f i \<partial>count_space UNIV) =
hoelzl@57025
  1690
    (\<integral>\<^sup>+i. (\<Sum>j. f j * indicator {j} i) \<partial>count_space UNIV)"
hoelzl@57025
  1691
  proof (intro nn_integral_cong)
hoelzl@57025
  1692
    fix i
hoelzl@57025
  1693
    have "f i = (\<Sum>j\<in>{i}. f j * indicator {j} i)"
hoelzl@57025
  1694
      by simp
hoelzl@57025
  1695
    also have "\<dots> = (\<Sum>j. f j * indicator {j} i)"
hoelzl@57025
  1696
      by (rule suminf_finite[symmetric]) auto
hoelzl@57025
  1697
    finally show "f i = (\<Sum>j. f j * indicator {j} i)" .
hoelzl@57025
  1698
  qed
hoelzl@57025
  1699
  also have "\<dots> = (\<Sum>j. (\<integral>\<^sup>+i. f j * indicator {j} i \<partial>count_space UNIV))"
hoelzl@57025
  1700
    by (rule nn_integral_suminf) (auto simp: nonneg)
hoelzl@57025
  1701
  also have "\<dots> = (\<Sum>j. f j)"
hoelzl@57025
  1702
    by (simp add: nonneg nn_integral_cmult_indicator one_ereal_def[symmetric])
hoelzl@57025
  1703
  finally show ?thesis .
hoelzl@57025
  1704
qed
hoelzl@57025
  1705
hoelzl@57025
  1706
lemma emeasure_countable_singleton:
hoelzl@57025
  1707
  assumes sets: "\<And>x. x \<in> X \<Longrightarrow> {x} \<in> sets M" and X: "countable X"
hoelzl@57025
  1708
  shows "emeasure M X = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space X)"
hoelzl@57025
  1709
proof -
hoelzl@57025
  1710
  have "emeasure M (\<Union>i\<in>X. {i}) = (\<integral>\<^sup>+x. emeasure M {x} \<partial>count_space X)"
hoelzl@57025
  1711
    using assms by (intro emeasure_UN_countable) (auto simp: disjoint_family_on_def)
hoelzl@57025
  1712
  also have "(\<Union>i\<in>X. {i}) = X" by auto
hoelzl@57025
  1713
  finally show ?thesis .
hoelzl@57025
  1714
qed
hoelzl@57025
  1715
hoelzl@57025
  1716
lemma measure_eqI_countable:
hoelzl@57025
  1717
  assumes [simp]: "sets M = Pow A" "sets N = Pow A" and A: "countable A"
hoelzl@57025
  1718
  assumes eq: "\<And>a. a \<in> A \<Longrightarrow> emeasure M {a} = emeasure N {a}"
hoelzl@57025
  1719
  shows "M = N"
hoelzl@57025
  1720
proof (rule measure_eqI)
hoelzl@57025
  1721
  fix X assume "X \<in> sets M"
hoelzl@57025
  1722
  then have X: "X \<subseteq> A" by auto
hoelzl@57025
  1723
  moreover with A have "countable X" by (auto dest: countable_subset)
hoelzl@57025
  1724
  ultimately have
hoelzl@57025
  1725
    "emeasure M X = (\<integral>\<^sup>+a. emeasure M {a} \<partial>count_space X)"
hoelzl@57025
  1726
    "emeasure N X = (\<integral>\<^sup>+a. emeasure N {a} \<partial>count_space X)"
hoelzl@57025
  1727
    by (auto intro!: emeasure_countable_singleton)
hoelzl@57025
  1728
  moreover have "(\<integral>\<^sup>+a. emeasure M {a} \<partial>count_space X) = (\<integral>\<^sup>+a. emeasure N {a} \<partial>count_space X)"
hoelzl@57025
  1729
    using X by (intro nn_integral_cong eq) auto
hoelzl@57025
  1730
  ultimately show "emeasure M X = emeasure N X"
hoelzl@57025
  1731
    by simp
hoelzl@57025
  1732
qed simp
hoelzl@57025
  1733
hoelzl@56994
  1734
subsubsection {* Measures with Restricted Space *}
hoelzl@54417
  1735
hoelzl@56996
  1736
lemma nn_integral_restrict_space:
hoelzl@54417
  1737
  assumes \<Omega>: "\<Omega> \<in> sets M" and f: "f \<in> borel_measurable M" "\<And>x. 0 \<le> f x" "\<And>x. x \<in> space M - \<Omega> \<Longrightarrow> f x = 0"
hoelzl@56996
  1738
  shows "nn_integral (restrict_space M \<Omega>) f = nn_integral M f"
hoelzl@54417
  1739
using f proof (induct rule: borel_measurable_induct)
hoelzl@54417
  1740
  case (cong f g) then show ?case
hoelzl@56996
  1741
    using nn_integral_cong[of M f g] nn_integral_cong[of "restrict_space M \<Omega>" f g]
hoelzl@54417
  1742
      sets.sets_into_space[OF `\<Omega> \<in> sets M`]
hoelzl@54417
  1743
    by (simp add: subset_eq space_restrict_space)
hoelzl@54417
  1744
next
hoelzl@54417
  1745
  case (set A)
hoelzl@54417
  1746
  then have "A \<subseteq> \<Omega>"
hoelzl@54417
  1747
    unfolding indicator_eq_0_iff by (auto dest: sets.sets_into_space)
hoelzl@54417
  1748
  with set `\<Omega> \<in> sets M` sets.sets_into_space[OF `\<Omega> \<in> sets M`] show ?case
hoelzl@56996
  1749
    by (subst nn_integral_indicator')
hoelzl@54417
  1750
       (auto simp add: sets_restrict_space_iff space_restrict_space
hoelzl@54417
  1751
                  emeasure_restrict_space Int_absorb2
hoelzl@54417
  1752
                dest: sets.sets_into_space)
hoelzl@54417
  1753
next
hoelzl@54417
  1754
  case (mult f c) then show ?case
hoelzl@56996
  1755
    by (cases "c = 0") (simp_all add: measurable_restrict_space1 \<Omega> nn_integral_cmult)
hoelzl@54417
  1756
next
hoelzl@54417
  1757
  case (add f g) then show ?case
hoelzl@56996
  1758
    by (simp add: measurable_restrict_space1 \<Omega> nn_integral_add ereal_add_nonneg_eq_0_iff)
hoelzl@54417
  1759
next
hoelzl@54417
  1760
  case (seq F) then show ?case
hoelzl@56996
  1761
    by (auto simp add: SUP_eq_iff measurable_restrict_space1 \<Omega> nn_integral_monotone_convergence_SUP)
hoelzl@54417
  1762
qed
hoelzl@54417
  1763
hoelzl@56994
  1764
subsubsection {* Measure spaces with an associated density *}
hoelzl@47694
  1765
hoelzl@47694
  1766
definition density :: "'a measure \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
wenzelm@53015
  1767
  "density M f = measure_of (space M) (sets M) (\<lambda>A. \<integral>\<^sup>+ x. f x * indicator A x \<partial>M)"
hoelzl@35582
  1768
hoelzl@47694
  1769
lemma 
hoelzl@47694
  1770
  shows sets_density[simp]: "sets (density M f) = sets M"
hoelzl@47694
  1771
    and space_density[simp]: "space (density M f) = space M"
hoelzl@47694
  1772
  by (auto simp: density_def)
hoelzl@47694
  1773
hoelzl@50003
  1774
(* FIXME: add conversion to simplify space, sets and measurable *)
hoelzl@50003
  1775
lemma space_density_imp[measurable_dest]:
hoelzl@50003
  1776
  "\<And>x M f. x \<in> space (density M f) \<Longrightarrow> x \<in> space M" by auto
hoelzl@50003
  1777
hoelzl@47694
  1778
lemma 
hoelzl@47694
  1779
  shows measurable_density_eq1[simp]: "g \<in> measurable (density Mg f) Mg' \<longleftrightarrow> g \<in> measurable Mg Mg'"
hoelzl@47694
  1780
    and measurable_density_eq2[simp]: "h \<in> measurable Mh (density Mh' f) \<longleftrightarrow> h \<in> measurable Mh Mh'"
hoelzl@47694
  1781
    and simple_function_density_eq[simp]: "simple_function (density Mu f) u \<longleftrightarrow> simple_function Mu u"
hoelzl@47694
  1782
  unfolding measurable_def simple_function_def by simp_all
hoelzl@47694
  1783
hoelzl@47694
  1784
lemma density_cong: "f \<in> borel_measurable M \<Longrightarrow> f' \<in> borel_measurable M \<Longrightarrow>
hoelzl@47694
  1785
  (AE x in M. f x = f' x) \<Longrightarrow> density M f = density M f'"
hoelzl@56996
  1786
  unfolding density_def by (auto intro!: measure_of_eq nn_integral_cong_AE sets.space_closed)
hoelzl@47694
  1787
hoelzl@47694
  1788
lemma density_max_0: "density M f = density M (\<lambda>x. max 0 (f x))"
hoelzl@47694
  1789
proof -
hoelzl@47694
  1790
  have "\<And>x A. max 0 (f x) * indicator A x = max 0 (f x * indicator A x)"
hoelzl@47694
  1791
    by (auto simp: indicator_def)
hoelzl@47694
  1792
  then show ?thesis
hoelzl@56996
  1793
    unfolding density_def by (simp add: nn_integral_max_0)
hoelzl@47694
  1794
qed
hoelzl@47694
  1795
hoelzl@47694
  1796
lemma density_ereal_max_0: "density M (\<lambda>x. ereal (f x)) = density M (\<lambda>x. ereal (max 0 (f x)))"
hoelzl@47694
  1797
  by (subst density_max_0) (auto intro!: arg_cong[where f="density M"] split: split_max)
hoelzl@38656
  1798
hoelzl@47694
  1799
lemma emeasure_density:
hoelzl@50002
  1800
  assumes f[measurable]: "f \<in> borel_measurable M" and A[measurable]: "A \<in> sets M"
wenzelm@53015
  1801
  shows "emeasure (density M f) A = (\<integral>\<^sup>+ x. f x * indicator A x \<partial>M)"
hoelzl@47694
  1802
    (is "_ = ?\<mu> A")
hoelzl@47694
  1803
  unfolding density_def
hoelzl@47694
  1804
proof (rule emeasure_measure_of_sigma)
hoelzl@47694
  1805
  show "sigma_algebra (space M) (sets M)" ..
hoelzl@47694
  1806
  show "positive (sets M) ?\<mu>"
hoelzl@56996
  1807
    using f by (auto simp: positive_def intro!: nn_integral_nonneg)
wenzelm@53015
  1808
  have \<mu>_eq: "?\<mu> = (\<lambda>A. \<integral>\<^sup>+ x. max 0 (f x) * indicator A x \<partial>M)" (is "?\<mu> = ?\<mu>'")
hoelzl@56996
  1809
    apply (subst nn_integral_max_0[symmetric])
hoelzl@56996
  1810
    apply (intro ext nn_integral_cong_AE AE_I2)
hoelzl@47694
  1811
    apply (auto simp: indicator_def)
hoelzl@47694
  1812
    done
hoelzl@47694
  1813
  show "countably_additive (sets M) ?\<mu>"
hoelzl@47694
  1814
    unfolding \<mu>_eq
hoelzl@47694
  1815
  proof (intro countably_additiveI)
hoelzl@47694
  1816
    fix A :: "nat \<Rightarrow> 'a set" assume "range A \<subseteq> sets M"
hoelzl@50002
  1817
    then have "\<And>i. A i \<in> sets M" by auto
hoelzl@47694
  1818
    then have *: "\<And>i. (\<lambda>x. max 0 (f x) * indicator (A i) x) \<in> borel_measurable M"
hoelzl@50002
  1819
      by (auto simp: set_eq_iff)
hoelzl@47694
  1820
    assume disj: "disjoint_family A"
wenzelm@53015
  1821
    have "(\<Sum>n. ?\<mu>' (A n)) = (\<integral>\<^sup>+ x. (\<Sum>n. max 0 (f x) * indicator (A n) x) \<partial>M)"
hoelzl@56996
  1822
      using f * by (simp add: nn_integral_suminf)
wenzelm@53015
  1823
    also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) * (\<Sum>n. indicator (A n) x) \<partial>M)" using f
hoelzl@56996
  1824
      by (auto intro!: suminf_cmult_ereal nn_integral_cong_AE)
wenzelm@53015
  1825
    also have "\<dots> = (\<integral>\<^sup>+ x. max 0 (f x) * indicator (\<Union>n. A n) x \<partial>M)"
hoelzl@47694
  1826
      unfolding suminf_indicator[OF disj] ..
hoelzl@47694
  1827
    finally show "(\<Sum>n. ?\<mu>' (A n)) = ?\<mu>' (\<Union>x. A x)" by simp
hoelzl@47694
  1828
  qed
hoelzl@47694
  1829
qed fact
hoelzl@38656
  1830
hoelzl@47694
  1831
lemma null_sets_density_iff:
hoelzl@47694
  1832
  assumes f: "f \<in> borel_measurable M"
hoelzl@47694
  1833
  shows "A \<in> null_sets (density M f) \<longleftrightarrow> A \<in> sets M \<and> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
hoelzl@47694
  1834
proof -
hoelzl@47694
  1835
  { assume "A \<in> sets M"
wenzelm@53015
  1836
    have eq: "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = (\<integral>\<^sup>+x. max 0 (f x) * indicator A x \<partial>M)"
hoelzl@56996
  1837
      apply (subst nn_integral_max_0[symmetric])
hoelzl@56996
  1838
      apply (intro nn_integral_cong)
hoelzl@47694
  1839
      apply (auto simp: indicator_def)
hoelzl@47694
  1840
      done
wenzelm@53015
  1841
    have "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> 
hoelzl@47694
  1842
      emeasure M {x \<in> space M. max 0 (f x) * indicator A x \<noteq> 0} = 0"
hoelzl@47694
  1843
      unfolding eq
hoelzl@47694
  1844
      using f `A \<in> sets M`
hoelzl@56996
  1845
      by (intro nn_integral_0_iff) auto
hoelzl@47694
  1846
    also have "\<dots> \<longleftrightarrow> (AE x in M. max 0 (f x) * indicator A x = 0)"
hoelzl@47694
  1847
      using f `A \<in> sets M`
hoelzl@50002
  1848
      by (intro AE_iff_measurable[OF _ refl, symmetric]) auto
hoelzl@47694
  1849
    also have "(AE x in M. max 0 (f x) * indicator A x = 0) \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)"
hoelzl@47694
  1850
      by (auto simp add: indicator_def max_def split: split_if_asm)
wenzelm@53015
  1851
    finally have "(\<integral>\<^sup>+x. f x * indicator A x \<partial>M) = 0 \<longleftrightarrow> (AE x in M. x \<in> A \<longrightarrow> f x \<le> 0)" . }
hoelzl@47694
  1852
  with f show ?thesis
hoelzl@47694
  1853
    by (simp add: null_sets_def emeasure_density cong: conj_cong)
hoelzl@47694
  1854
qed
hoelzl@47694
  1855
hoelzl@47694
  1856
lemma AE_density:
hoelzl@47694
  1857
  assumes f: "f \<in> borel_measurable M"
hoelzl@47694
  1858
  shows "(AE x in density M f. P x) \<longleftrightarrow> (AE x in M. 0 < f x \<longrightarrow> P x)"
hoelzl@47694
  1859
proof
hoelzl@47694
  1860
  assume "AE x in density M f. P x"
hoelzl@47694
  1861
  with f obtain N where "{x \<in> space M. \<not> P x} \<subseteq> N" "N \<in> sets M" and ae: "AE x in M. x \<in> N \<longrightarrow> f x \<le> 0"
hoelzl@47694
  1862
    by (auto simp: eventually_ae_filter null_sets_density_iff)
hoelzl@47694
  1863
  then have "AE x in M. x \<notin> N \<longrightarrow> P x" by auto
hoelzl@47694
  1864
  with ae show "AE x in M. 0 < f x \<longrightarrow> P x"
hoelzl@47694
  1865
    by (rule eventually_elim2) auto
hoelzl@47694
  1866
next
hoelzl@47694
  1867
  fix N assume ae: "AE x in M. 0 < f x \<longrightarrow> P x"
hoelzl@47694
  1868
  then obtain N where "{x \<in> space M. \<not> (0 < f x \<longrightarrow> P x)} \<subseteq> N" "N \<in> null_sets M"
hoelzl@47694
  1869
    by (auto simp: eventually_ae_filter)
hoelzl@47694
  1870
  then have *: "{x \<in> space (density M f). \<not> P x} \<subseteq> N \<union> {x\<in>space M. \<not> 0 < f x}"
hoelzl@47694
  1871
    "N \<union> {x\<in>space M. \<not> 0 < f x} \<in> sets M" and ae2: "AE x in M. x \<notin> N"
immler@50244
  1872
    using f by (auto simp: subset_eq intro!: sets.sets_Collect_neg AE_not_in)
hoelzl@47694
  1873
  show "AE x in density M f. P x"
hoelzl@47694
  1874
    using ae2
hoelzl@47694
  1875
    unfolding eventually_ae_filter[of _ "density M f"] Bex_def null_sets_density_iff[OF f]
hoelzl@47694
  1876
    by (intro exI[of _ "N \<union> {x\<in>space M. \<not> 0 < f x}"] conjI *)
hoelzl@47694
  1877
       (auto elim: eventually_elim2)
hoelzl@35582
  1878
qed
hoelzl@35582
  1879
hoelzl@56996
  1880
lemma nn_integral_density':
hoelzl@47694
  1881
  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
hoelzl@49799
  1882
  assumes g: "g \<in> borel_measurable M" "\<And>x. 0 \<le> g x"
hoelzl@56996
  1883
  shows "integral\<^sup>N (density M f) g = (\<integral>\<^sup>+ x. f x * g x \<partial>M)"
hoelzl@49798
  1884
using g proof induct
hoelzl@49798
  1885
  case (cong u v)
hoelzl@49799
  1886
  then show ?case
hoelzl@56996
  1887
    apply (subst nn_integral_cong[OF cong(3)])
hoelzl@56996
  1888
    apply (simp_all cong: nn_integral_cong)
hoelzl@49798
  1889
    done
hoelzl@49798
  1890
next
hoelzl@49798
  1891
  case (set A) then show ?case
hoelzl@49798
  1892
    by (simp add: emeasure_density f)
hoelzl@49798
  1893
next
hoelzl@49798
  1894
  case (mult u c)
hoelzl@49798
  1895
  moreover have "\<And>x. f x * (c * u x) = c * (f x * u x)" by (simp add: field_simps)
hoelzl@49798
  1896
  ultimately show ?case
hoelzl@56996
  1897
    using f by (simp add: nn_integral_cmult)
hoelzl@49798
  1898
next
hoelzl@49798
  1899
  case (add u v)
wenzelm@53374
  1900
  then have "\<And>x. f x * (v x + u x) = f x * v x + f x * u x"
hoelzl@49798
  1901
    by (simp add: ereal_right_distrib)
wenzelm@53374
  1902
  with add f show ?case
hoelzl@56996
  1903
    by (auto simp add: nn_integral_add ereal_zero_le_0_iff intro!: nn_integral_add[symmetric])
hoelzl@49798
  1904
next
hoelzl@49798
  1905
  case (seq U)
hoelzl@49798
  1906
  from f(2) have eq: "AE x in M. f x * (SUP i. U i x) = (SUP i. f x * U i x)"
haftmann@56212
  1907
    by eventually_elim (simp add: SUP_ereal_cmult seq)
hoelzl@49798
  1908
  from seq f show ?case
hoelzl@56996
  1909
    apply (simp add: nn_integral_monotone_convergence_SUP)
hoelzl@56996
  1910
    apply (subst nn_integral_cong_AE[OF eq])
hoelzl@56996
  1911
    apply (subst nn_integral_monotone_convergence_SUP_AE)
hoelzl@49798
  1912
    apply (auto simp: incseq_def le_fun_def intro!: ereal_mult_left_mono)
hoelzl@49798
  1913
    done
hoelzl@47694
  1914
qed
hoelzl@38705
  1915
hoelzl@56996
  1916
lemma nn_integral_density:
hoelzl@49798
  1917
  "f \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow> g' \<in> borel_measurable M \<Longrightarrow> 
hoelzl@56996
  1918
    integral\<^sup>N (density M f) g' = (\<integral>\<^sup>+ x. f x * g' x \<partial>M)"
hoelzl@56996
  1919
  by (subst (1 2) nn_integral_max_0[symmetric])
hoelzl@56996
  1920
     (auto intro!: nn_integral_cong_AE
hoelzl@56996
  1921
           simp: measurable_If max_def ereal_zero_le_0_iff nn_integral_density')
hoelzl@49798
  1922
hoelzl@47694
  1923
lemma emeasure_restricted:
hoelzl@47694
  1924
  assumes S: "S \<in> sets M" and X: "X \<in> sets M"
hoelzl@47694
  1925
  shows "emeasure (density M (indicator S)) X = emeasure M (S \<inter> X)"
hoelzl@38705
  1926
proof -
wenzelm@53015
  1927
  have "emeasure (density M (indicator S)) X = (\<integral>\<^sup>+x. indicator S x * indicator X x \<partial>M)"
hoelzl@47694
  1928
    using S X by (simp add: emeasure_density)
wenzelm@53015
  1929
  also have "\<dots> = (\<integral>\<^sup>+x. indicator (S \<inter> X) x \<partial>M)"
hoelzl@56996
  1930
    by (auto intro!: nn_integral_cong simp: indicator_def)
hoelzl@47694
  1931
  also have "\<dots> = emeasure M (S \<inter> X)"
immler@50244
  1932
    using S X by (simp add: sets.Int)
hoelzl@47694
  1933
  finally show ?thesis .
hoelzl@47694
  1934
qed
hoelzl@47694
  1935
hoelzl@47694
  1936
lemma measure_restricted:
hoelzl@47694
  1937
  "S \<in> sets M \<Longrightarrow> X \<in> sets M \<Longrightarrow> measure (density M (indicator S)) X = measure M (S \<inter> X)"
hoelzl@47694
  1938
  by (simp add: emeasure_restricted measure_def)
hoelzl@47694
  1939
hoelzl@47694
  1940
lemma (in finite_measure) finite_measure_restricted:
hoelzl@47694
  1941
  "S \<in> sets M \<Longrightarrow> finite_measure (density M (indicator S))"
hoelzl@47694
  1942
  by default (simp add: emeasure_restricted)
hoelzl@47694
  1943
hoelzl@47694
  1944
lemma emeasure_density_const:
hoelzl@47694
  1945
  "A \<in> sets M \<Longrightarrow> 0 \<le> c \<Longrightarrow> emeasure (density M (\<lambda>_. c)) A = c * emeasure M A"
hoelzl@56996
  1946
  by (auto simp: nn_integral_cmult_indicator emeasure_density)
hoelzl@47694
  1947
hoelzl@47694
  1948
lemma measure_density_const:
hoelzl@47694
  1949
  "A \<in> sets M \<Longrightarrow> 0 < c \<Longrightarrow> c \<noteq> \<infinity> \<Longrightarrow> measure (density M (\<lambda>_. c)) A = real c * measure M A"
hoelzl@47694
  1950
  by (auto simp: emeasure_density_const measure_def)
hoelzl@47694
  1951
hoelzl@47694
  1952
lemma density_density_eq:
hoelzl@47694
  1953
   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> AE x in M. 0 \<le> f x \<Longrightarrow>
hoelzl@47694
  1954
   density (density M f) g = density M (\<lambda>x. f x * g x)"
hoelzl@56996
  1955
  by (auto intro!: measure_eqI simp: emeasure_density nn_integral_density ac_simps)
hoelzl@47694
  1956
hoelzl@47694
  1957
lemma distr_density_distr:
hoelzl@47694
  1958
  assumes T: "T \<in> measurable M M'" and T': "T' \<in> measurable M' M"
hoelzl@47694
  1959
    and inv: "\<forall>x\<in>space M. T' (T x) = x"
hoelzl@47694
  1960
  assumes f: "f \<in> borel_measurable M'"
hoelzl@47694
  1961
  shows "distr (density (distr M M' T) f) M T' = density M (f \<circ> T)" (is "?R = ?L")
hoelzl@47694
  1962
proof (rule measure_eqI)
hoelzl@47694
  1963
  fix A assume A: "A \<in> sets ?R"
hoelzl@47694
  1964
  { fix x assume "x \<in> space M"
immler@50244
  1965
    with sets.sets_into_space[OF A]
hoelzl@47694
  1966
    have "indicator (T' -` A \<inter> space M') (T x) = (indicator A x :: ereal)"
hoelzl@47694
  1967
      using T inv by (auto simp: indicator_def measurable_space) }
hoelzl@47694
  1968
  with A T T' f show "emeasure ?R A = emeasure ?L A"
hoelzl@47694
  1969
    by (simp add: measurable_comp emeasure_density emeasure_distr
hoelzl@56996
  1970
                  nn_integral_distr measurable_sets cong: nn_integral_cong)
hoelzl@47694
  1971
qed simp
hoelzl@47694
  1972
hoelzl@47694
  1973
lemma density_density_divide:
hoelzl@47694
  1974
  fixes f g :: "'a \<Rightarrow> real"
hoelzl@47694
  1975
  assumes f: "f \<in> borel_measurable M" "AE x in M. 0 \<le> f x"
hoelzl@47694
  1976
  assumes g: "g \<in> borel_measurable M" "AE x in M. 0 \<le> g x"
hoelzl@47694
  1977
  assumes ac: "AE x in M. f x = 0 \<longrightarrow> g x = 0"
hoelzl@47694
  1978
  shows "density (density M f) (\<lambda>x. g x / f x) = density M g"
hoelzl@47694
  1979
proof -
hoelzl@47694
  1980
  have "density M g = density M (\<lambda>x. f x * (g x / f x))"
hoelzl@47694
  1981
    using f g ac by (auto intro!: density_cong measurable_If)
hoelzl@47694
  1982
  then show ?thesis
hoelzl@47694
  1983
    using f g by (subst density_density_eq) auto
hoelzl@38705
  1984
qed
hoelzl@38705
  1985
hoelzl@56994
  1986
subsubsection {* Point measure *}
hoelzl@47694
  1987
hoelzl@47694
  1988
definition point_measure :: "'a set \<Rightarrow> ('a \<Rightarrow> ereal) \<Rightarrow> 'a measure" where
hoelzl@47694
  1989
  "point_measure A f = density (count_space A) f"
hoelzl@47694
  1990
hoelzl@47694
  1991
lemma
hoelzl@47694
  1992
  shows space_point_measure: "space (point_measure A f) = A"
hoelzl@47694
  1993
    and sets_point_measure: "sets (point_measure A f) = Pow A"
hoelzl@47694
  1994
  by (auto simp: point_measure_def)
hoelzl@47694
  1995
hoelzl@47694
  1996
lemma measurable_point_measure_eq1[simp]:
hoelzl@47694
  1997
  "g \<in> measurable (point_measure A f) M \<longleftrightarrow> g \<in> A \<rightarrow> space M"
hoelzl@47694
  1998
  unfolding point_measure_def by simp
hoelzl@47694
  1999
hoelzl@47694
  2000
lemma measurable_point_measure_eq2_finite[simp]:
hoelzl@47694
  2001
  "finite A \<Longrightarrow>
hoelzl@47694
  2002
   g \<in> measurable M (point_measure A f) \<longleftrightarrow>
hoelzl@47694
  2003
    (g \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. g -` {a} \<inter> space M \<in> sets M))"
hoelzl@50002
  2004
  unfolding point_measure_def by (simp add: measurable_count_space_eq2)
hoelzl@47694
  2005
hoelzl@47694
  2006
lemma simple_function_point_measure[simp]:
hoelzl@47694
  2007
  "simple_function (point_measure A f) g \<longleftrightarrow> finite (g ` A)"
hoelzl@47694
  2008
  by (simp add: point_measure_def)
hoelzl@47694
  2009
hoelzl@47694
  2010
lemma emeasure_point_measure:
hoelzl@47694
  2011
  assumes A: "finite {a\<in>X. 0 < f a}" "X \<subseteq> A"
hoelzl@47694
  2012
  shows "emeasure (point_measure A f) X = (\<Sum>a|a\<in>X \<and> 0 < f a. f a)"
hoelzl@35977
  2013
proof -
hoelzl@47694
  2014
  have "{a. (a \<in> X \<longrightarrow> a \<in> A \<and> 0 < f a) \<and> a \<in> X} = {a\<in>X. 0 < f a}"
hoelzl@47694
  2015
    using `X \<subseteq> A` by auto
hoelzl@47694
  2016
  with A show ?thesis
hoelzl@56996
  2017
    by (simp add: emeasure_density nn_integral_count_space ereal_zero_le_0_iff
hoelzl@47694
  2018
                  point_measure_def indicator_def)
hoelzl@35977
  2019
qed
hoelzl@35977
  2020
hoelzl@47694
  2021
lemma emeasure_point_measure_finite:
hoelzl@49795
  2022
  "finite A \<Longrightarrow> (\<And>i. i \<in> A \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a\<in>X. f a)"
hoelzl@47694
  2023
  by (subst emeasure_point_measure) (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
hoelzl@47694
  2024
hoelzl@49795
  2025
lemma emeasure_point_measure_finite2:
hoelzl@49795
  2026
  "X \<subseteq> A \<Longrightarrow> finite X \<Longrightarrow> (\<And>i. i \<in> X \<Longrightarrow> 0 \<le> f i) \<Longrightarrow> emeasure (point_measure A f) X = (\<Sum>a\<in>X. f a)"
hoelzl@49795
  2027
  by (subst emeasure_point_measure)
hoelzl@49795
  2028
     (auto dest: finite_subset intro!: setsum_mono_zero_left simp: less_le)
hoelzl@49795
  2029
hoelzl@47694
  2030
lemma null_sets_point_measure_iff:
hoelzl@47694
  2031
  "X \<in> null_sets (point_measure A f) \<longleftrightarrow> X \<subseteq> A \<and> (\<forall>x\<in>X. f x \<le> 0)"
hoelzl@47694
  2032
 by (auto simp: AE_count_space null_sets_density_iff point_measure_def)
hoelzl@47694
  2033
hoelzl@47694
  2034
lemma AE_point_measure:
hoelzl@47694
  2035
  "(AE x in point_measure A f. P x) \<longleftrightarrow> (\<forall>x\<in>A. 0 < f x \<longrightarrow> P x)"
hoelzl@47694
  2036
  unfolding point_measure_def
hoelzl@47694
  2037
  by (subst AE_density) (auto simp: AE_density AE_count_space point_measure_def)
hoelzl@47694
  2038
hoelzl@56996
  2039
lemma nn_integral_point_measure:
hoelzl@47694
  2040
  "finite {a\<in>A. 0 < f a \<and> 0 < g a} \<Longrightarrow>
hoelzl@56996
  2041
    integral\<^sup>N (point_measure A f) g = (\<Sum>a|a\<in>A \<and> 0 < f a \<and> 0 < g a. f a * g a)"
hoelzl@47694
  2042
  unfolding point_measure_def
hoelzl@47694
  2043
  apply (subst density_max_0)
hoelzl@56996
  2044
  apply (subst nn_integral_density)
hoelzl@56996
  2045
  apply (simp_all add: AE_count_space nn_integral_density)
hoelzl@56996
  2046
  apply (subst nn_integral_count_space )
hoelzl@47694
  2047
  apply (auto intro!: setsum_cong simp: max_def ereal_zero_less_0_iff)
hoelzl@47694
  2048
  apply (rule finite_subset)
hoelzl@47694
  2049
  prefer 2
hoelzl@47694
  2050
  apply assumption
hoelzl@47694
  2051
  apply auto
hoelzl@47694
  2052
  done
hoelzl@47694
  2053
hoelzl@56996
  2054
lemma nn_integral_point_measure_finite:
hoelzl@47694
  2055
  "finite A \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> f a) \<Longrightarrow> (\<And>a. a \<in> A \<Longrightarrow> 0 \<le> g a) \<Longrightarrow>
hoelzl@56996
  2056
    integral\<^sup>N (point_measure A f) g = (\<Sum>a\<in>A. f a * g a)"
hoelzl@56996
  2057
  by (subst nn_integral_point_measure) (auto intro!: setsum_mono_zero_left simp: less_le)
hoelzl@47694
  2058
hoelzl@56994
  2059
subsubsection {* Uniform measure *}
hoelzl@47694
  2060
hoelzl@47694
  2061
definition "uniform_measure M A = density M (\<lambda>x. indicator A x / emeasure M A)"
hoelzl@47694
  2062
hoelzl@47694
  2063
lemma
hoelzl@47694
  2064
  shows sets_uniform_measure[simp]: "sets (uniform_measure M A) = sets M"
hoelzl@47694
  2065
    and space_uniform_measure[simp]: "space (uniform_measure M A) = space M"
hoelzl@47694
  2066
  by (auto simp: uniform_measure_def)
hoelzl@47694
  2067
hoelzl@47694
  2068
lemma emeasure_uniform_measure[simp]:
hoelzl@47694
  2069
  assumes A: "A \<in> sets M" and B: "B \<in> sets M"
hoelzl@47694
  2070
  shows "emeasure (uniform_measure M A) B = emeasure M (A \<inter> B) / emeasure M A"
hoelzl@47694
  2071
proof -
wenzelm@53015
  2072
  from A B have "emeasure (uniform_measure M A) B = (\<integral>\<^sup>+x. (1 / emeasure M A) * indicator (A \<inter> B) x \<partial>M)"
hoelzl@47694
  2073
    by (auto simp add: uniform_measure_def emeasure_density split: split_indicator
hoelzl@56996
  2074
             intro!: nn_integral_cong)
hoelzl@47694
  2075
  also have "\<dots> = emeasure M (A \<inter> B) / emeasure M A"
hoelzl@47694
  2076
    using A B
hoelzl@56996
  2077
    by (subst nn_integral_cmult_indicator) (simp_all add: sets.Int emeasure_nonneg)
hoelzl@47694
  2078
  finally show ?thesis .
hoelzl@47694
  2079
qed
hoelzl@47694
  2080
hoelzl@47694
  2081
lemma measure_uniform_measure[simp]:
hoelzl@47694
  2082
  assumes A: "emeasure M A \<noteq> 0" "emeasure M A \<noteq> \<infinity>" and B: "B \<in> sets M"
hoelzl@47694
  2083
  shows "measure (uniform_measure M A) B = measure M (A \<inter> B) / measure M A"
hoelzl@47694
  2084
  using emeasure_uniform_measure[OF emeasure_neq_0_sets[OF A(1)] B] A
hoelzl@47694
  2085
  by (cases "emeasure M A" "emeasure M (A \<inter> B)" rule: ereal2_cases) (simp_all add: measure_def)
hoelzl@47694
  2086
hoelzl@56994
  2087
subsubsection {* Uniform count measure *}
hoelzl@47694
  2088
hoelzl@47694
  2089
definition "uniform_count_measure A = point_measure A (\<lambda>x. 1 / card A)"
hoelzl@47694
  2090
 
hoelzl@47694
  2091
lemma 
hoelzl@47694
  2092
  shows space_uniform_count_measure: "space (uniform_count_measure A) = A"
hoelzl@47694
  2093
    and sets_uniform_count_measure: "sets (uniform_count_measure A) = Pow A"
hoelzl@47694
  2094
    unfolding uniform_count_measure_def by (auto simp: space_point_measure sets_point_measure)
hoelzl@47694
  2095
 
hoelzl@47694
  2096
lemma emeasure_uniform_count_measure:
hoelzl@47694
  2097
  "finite A \<Longrightarrow> X \<subseteq> A \<Longrightarrow> emeasure (uniform_count_measure A) X = card X / card A"
hoelzl@47694
  2098
  by (simp add: real_eq_of_nat emeasure_point_measure_finite uniform_count_measure_def)
hoelzl@47694
  2099
 
hoelzl@47694
  2100
lemma measure_uniform_count_measure:
hoelzl@47694
  2101
  "finite A \<Longrightarrow> X \<subseteq> A \<Longrightarrow> measure (uniform_count_measure A) X = card X / card A"
hoelzl@47694
  2102
  by (simp add: real_eq_of_nat emeasure_point_measure_finite uniform_count_measure_def measure_def)
hoelzl@47694
  2103
hoelzl@35748
  2104
end