src/HOL/Probability/Borel_Space.thy
author hoelzl
Fri Mar 22 10:41:43 2013 +0100 (2013-03-22)
changeset 51478 270b21f3ae0a
parent 51351 dd1dd470690b
child 51683 baefa3b461c2
permissions -rw-r--r--
move continuous and continuous_on to the HOL image; isCont is an abbreviation for continuous (at x) (isCont is now restricted to a T2 space)
     1 (*  Title:      HOL/Probability/Borel_Space.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Armin Heller, TU München
     4 *)
     5 
     6 header {*Borel spaces*}
     7 
     8 theory Borel_Space
     9 imports
    10   Measurable
    11   "~~/src/HOL/Multivariate_Analysis/Multivariate_Analysis"
    12 begin
    13 
    14 section "Generic Borel spaces"
    15 
    16 definition borel :: "'a::topological_space measure" where
    17   "borel = sigma UNIV {S. open S}"
    18 
    19 abbreviation "borel_measurable M \<equiv> measurable M borel"
    20 
    21 lemma in_borel_measurable:
    22    "f \<in> borel_measurable M \<longleftrightarrow>
    23     (\<forall>S \<in> sigma_sets UNIV {S. open S}. f -` S \<inter> space M \<in> sets M)"
    24   by (auto simp add: measurable_def borel_def)
    25 
    26 lemma in_borel_measurable_borel:
    27    "f \<in> borel_measurable M \<longleftrightarrow>
    28     (\<forall>S \<in> sets borel.
    29       f -` S \<inter> space M \<in> sets M)"
    30   by (auto simp add: measurable_def borel_def)
    31 
    32 lemma space_borel[simp]: "space borel = UNIV"
    33   unfolding borel_def by auto
    34 
    35 lemma space_in_borel[measurable]: "UNIV \<in> sets borel"
    36   unfolding borel_def by auto
    37 
    38 lemma pred_Collect_borel[measurable (raw)]: "Measurable.pred borel P \<Longrightarrow> {x. P x} \<in> sets borel"
    39   unfolding borel_def pred_def by auto
    40 
    41 lemma borel_open[measurable (raw generic)]:
    42   assumes "open A" shows "A \<in> sets borel"
    43 proof -
    44   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
    45   thus ?thesis unfolding borel_def by auto
    46 qed
    47 
    48 lemma borel_closed[measurable (raw generic)]:
    49   assumes "closed A" shows "A \<in> sets borel"
    50 proof -
    51   have "space borel - (- A) \<in> sets borel"
    52     using assms unfolding closed_def by (blast intro: borel_open)
    53   thus ?thesis by simp
    54 qed
    55 
    56 lemma borel_singleton[measurable]:
    57   "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets (borel :: 'a::t1_space measure)"
    58   unfolding insert_def by (rule sets.Un) auto
    59 
    60 lemma borel_comp[measurable]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
    61   unfolding Compl_eq_Diff_UNIV by simp
    62 
    63 lemma borel_measurable_vimage:
    64   fixes f :: "'a \<Rightarrow> 'x::t2_space"
    65   assumes borel[measurable]: "f \<in> borel_measurable M"
    66   shows "f -` {x} \<inter> space M \<in> sets M"
    67   by simp
    68 
    69 lemma borel_measurableI:
    70   fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    71   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    72   shows "f \<in> borel_measurable M"
    73   unfolding borel_def
    74 proof (rule measurable_measure_of, simp_all)
    75   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
    76     using assms[of S] by simp
    77 qed
    78 
    79 lemma borel_measurable_const:
    80   "(\<lambda>x. c) \<in> borel_measurable M"
    81   by auto
    82 
    83 lemma borel_measurable_indicator:
    84   assumes A: "A \<in> sets M"
    85   shows "indicator A \<in> borel_measurable M"
    86   unfolding indicator_def [abs_def] using A
    87   by (auto intro!: measurable_If_set)
    88 
    89 lemma borel_measurable_count_space[measurable (raw)]:
    90   "f \<in> borel_measurable (count_space S)"
    91   unfolding measurable_def by auto
    92 
    93 lemma borel_measurable_indicator'[measurable (raw)]:
    94   assumes [measurable]: "{x\<in>space M. f x \<in> A x} \<in> sets M"
    95   shows "(\<lambda>x. indicator (A x) (f x)) \<in> borel_measurable M"
    96   unfolding indicator_def[abs_def]
    97   by (auto intro!: measurable_If)
    98 
    99 lemma borel_measurable_indicator_iff:
   100   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
   101     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
   102 proof
   103   assume "?I \<in> borel_measurable M"
   104   then have "?I -` {1} \<inter> space M \<in> sets M"
   105     unfolding measurable_def by auto
   106   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   107     unfolding indicator_def [abs_def] by auto
   108   finally show "A \<inter> space M \<in> sets M" .
   109 next
   110   assume "A \<inter> space M \<in> sets M"
   111   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   112     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   113     by (intro measurable_cong) (auto simp: indicator_def)
   114   ultimately show "?I \<in> borel_measurable M" by auto
   115 qed
   116 
   117 lemma borel_measurable_subalgebra:
   118   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   119   shows "f \<in> borel_measurable M"
   120   using assms unfolding measurable_def by auto
   121 
   122 lemma borel_measurable_continuous_on1:
   123   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   124   assumes "continuous_on UNIV f"
   125   shows "f \<in> borel_measurable borel"
   126   apply(rule borel_measurableI)
   127   using continuous_open_preimage[OF assms] unfolding vimage_def by auto
   128 
   129 lemma borel_eq_countable_basis:
   130   fixes B::"'a::topological_space set set"
   131   assumes "countable B"
   132   assumes "topological_basis B"
   133   shows "borel = sigma UNIV B"
   134   unfolding borel_def
   135 proof (intro sigma_eqI sigma_sets_eqI, safe)
   136   interpret countable_basis using assms by unfold_locales
   137   fix X::"'a set" assume "open X"
   138   from open_countable_basisE[OF this] guess B' . note B' = this
   139   show "X \<in> sigma_sets UNIV B"
   140   proof cases
   141     assume "B' \<noteq> {}"
   142     thus "X \<in> sigma_sets UNIV B" using assms B'
   143       by (metis from_nat_into Union_image_eq countable_subset range_from_nat_into
   144         in_mono sigma_sets.Basic sigma_sets.Union)
   145   qed (simp add: sigma_sets.Empty B')
   146 next
   147   fix b assume "b \<in> B"
   148   hence "open b" by (rule topological_basis_open[OF assms(2)])
   149   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   150 qed simp_all
   151 
   152 lemma borel_measurable_Pair[measurable (raw)]:
   153   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   154   assumes f[measurable]: "f \<in> borel_measurable M"
   155   assumes g[measurable]: "g \<in> borel_measurable M"
   156   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   157 proof (subst borel_eq_countable_basis)
   158   let ?B = "SOME B::'b set set. countable B \<and> topological_basis B"
   159   let ?C = "SOME B::'c set set. countable B \<and> topological_basis B"
   160   let ?P = "(\<lambda>(b, c). b \<times> c) ` (?B \<times> ?C)"
   161   show "countable ?P" "topological_basis ?P"
   162     by (auto intro!: countable_basis topological_basis_prod is_basis)
   163 
   164   show "(\<lambda>x. (f x, g x)) \<in> measurable M (sigma UNIV ?P)"
   165   proof (rule measurable_measure_of)
   166     fix S assume "S \<in> ?P"
   167     then obtain b c where "b \<in> ?B" "c \<in> ?C" and S: "S = b \<times> c" by auto
   168     then have borel: "open b" "open c"
   169       by (auto intro: is_basis topological_basis_open)
   170     have "(\<lambda>x. (f x, g x)) -` S \<inter> space M = (f -` b \<inter> space M) \<inter> (g -` c \<inter> space M)"
   171       unfolding S by auto
   172     also have "\<dots> \<in> sets M"
   173       using borel by simp
   174     finally show "(\<lambda>x. (f x, g x)) -` S \<inter> space M \<in> sets M" .
   175   qed auto
   176 qed
   177 
   178 lemma borel_measurable_continuous_on:
   179   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   180   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   181   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   182   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   183 
   184 lemma borel_measurable_continuous_on_open':
   185   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   186   assumes cont: "continuous_on A f" "open A"
   187   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
   188 proof (rule borel_measurableI)
   189   fix S :: "'b set" assume "open S"
   190   then have "open {x\<in>A. f x \<in> S}"
   191     by (intro continuous_open_preimage[OF cont]) auto
   192   then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
   193   have "?f -` S \<inter> space borel = 
   194     {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
   195     by (auto split: split_if_asm)
   196   also have "\<dots> \<in> sets borel"
   197     using * `open A` by auto
   198   finally show "?f -` S \<inter> space borel \<in> sets borel" .
   199 qed
   200 
   201 lemma borel_measurable_continuous_on_open:
   202   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   203   assumes cont: "continuous_on A f" "open A"
   204   assumes g: "g \<in> borel_measurable M"
   205   shows "(\<lambda>x. if g x \<in> A then f (g x) else c) \<in> borel_measurable M"
   206   using measurable_comp[OF g borel_measurable_continuous_on_open'[OF cont], of c]
   207   by (simp add: comp_def)
   208 
   209 lemma continuous_on_fst: "continuous_on UNIV fst"
   210 proof -
   211   have [simp]: "range fst = UNIV" by (auto simp: image_iff)
   212   show ?thesis
   213     using closed_vimage_fst
   214     by (auto simp: continuous_on_closed closed_closedin vimage_def)
   215 qed
   216 
   217 lemma continuous_on_snd: "continuous_on UNIV snd"
   218 proof -
   219   have [simp]: "range snd = UNIV" by (auto simp: image_iff)
   220   show ?thesis
   221     using closed_vimage_snd
   222     by (auto simp: continuous_on_closed closed_closedin vimage_def)
   223 qed
   224 
   225 lemma borel_measurable_continuous_Pair:
   226   fixes f :: "'a \<Rightarrow> 'b::second_countable_topology" and g :: "'a \<Rightarrow> 'c::second_countable_topology"
   227   assumes [measurable]: "f \<in> borel_measurable M"
   228   assumes [measurable]: "g \<in> borel_measurable M"
   229   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   230   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   231 proof -
   232   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   233   show ?thesis
   234     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   235 qed
   236 
   237 section "Borel spaces on euclidean spaces"
   238 
   239 lemma borel_measurable_inner[measurable (raw)]:
   240   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_inner}"
   241   assumes "f \<in> borel_measurable M"
   242   assumes "g \<in> borel_measurable M"
   243   shows "(\<lambda>x. f x \<bullet> g x) \<in> borel_measurable M"
   244   using assms
   245   by (rule borel_measurable_continuous_Pair)
   246      (intro continuous_on_inner continuous_on_snd continuous_on_fst)
   247 
   248 lemma [measurable]:
   249   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   250   shows lessThan_borel: "{..< a} \<in> sets borel"
   251     and greaterThan_borel: "{a <..} \<in> sets borel"
   252     and greaterThanLessThan_borel: "{a<..<b} \<in> sets borel"
   253     and atMost_borel: "{..a} \<in> sets borel"
   254     and atLeast_borel: "{a..} \<in> sets borel"
   255     and atLeastAtMost_borel: "{a..b} \<in> sets borel"
   256     and greaterThanAtMost_borel: "{a<..b} \<in> sets borel"
   257     and atLeastLessThan_borel: "{a..<b} \<in> sets borel"
   258   unfolding greaterThanAtMost_def atLeastLessThan_def
   259   by (blast intro: borel_open borel_closed)+
   260 
   261 lemma borel_measurable_less[measurable]:
   262   fixes f :: "'a \<Rightarrow> real"
   263   assumes f: "f \<in> borel_measurable M"
   264   assumes g: "g \<in> borel_measurable M"
   265   shows "{w \<in> space M. f w < g w} \<in> sets M"
   266 proof -
   267   have "{w \<in> space M. f w < g w} = {x \<in> space M. \<exists>r. f x < of_rat r \<and> of_rat r < g x}"
   268     using Rats_dense_in_real by (auto simp add: Rats_def)
   269   with f g show ?thesis
   270     by simp
   271 qed
   272 
   273 lemma
   274   fixes f :: "'a \<Rightarrow> real"
   275   assumes f[measurable]: "f \<in> borel_measurable M"
   276   assumes g[measurable]: "g \<in> borel_measurable M"
   277   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   278     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   279     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   280   unfolding eq_iff not_less[symmetric]
   281   by measurable
   282 
   283 lemma 
   284   shows hafspace_less_borel: "{x::'a::euclidean_space. a < x \<bullet> i} \<in> sets borel"
   285     and hafspace_greater_borel: "{x::'a::euclidean_space. x \<bullet> i < a} \<in> sets borel"
   286     and hafspace_less_eq_borel: "{x::'a::euclidean_space. a \<le> x \<bullet> i} \<in> sets borel"
   287     and hafspace_greater_eq_borel: "{x::'a::euclidean_space. x \<bullet> i \<le> a} \<in> sets borel"
   288   by simp_all
   289 
   290 subsection "Borel space equals sigma algebras over intervals"
   291 
   292 lemma borel_sigma_sets_subset:
   293   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   294   using sets.sigma_sets_subset[of A borel] by simp
   295 
   296 lemma borel_eq_sigmaI1:
   297   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   298   assumes borel_eq: "borel = sigma UNIV X"
   299   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   300   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   301   shows "borel = sigma UNIV (F ` A)"
   302   unfolding borel_def
   303 proof (intro sigma_eqI antisym)
   304   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   305     unfolding borel_def by simp
   306   also have "\<dots> = sigma_sets UNIV X"
   307     unfolding borel_eq by simp
   308   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   309     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   310   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   311   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   312     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   313 qed auto
   314 
   315 lemma borel_eq_sigmaI2:
   316   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   317     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   318   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   319   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   320   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   321   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   322   using assms
   323   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   324 
   325 lemma borel_eq_sigmaI3:
   326   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   327   assumes borel_eq: "borel = sigma UNIV X"
   328   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   329   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   330   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   331   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   332 
   333 lemma borel_eq_sigmaI4:
   334   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   335     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   336   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   337   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   338   assumes F: "\<And>i. F i \<in> sets borel"
   339   shows "borel = sigma UNIV (range F)"
   340   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   341 
   342 lemma borel_eq_sigmaI5:
   343   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   344   assumes borel_eq: "borel = sigma UNIV (range G)"
   345   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   346   assumes F: "\<And>i j. F i j \<in> sets borel"
   347   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   348   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   349 
   350 lemma borel_eq_box:
   351   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a \<Colon> euclidean_space set))"
   352     (is "_ = ?SIGMA")
   353 proof (rule borel_eq_sigmaI1[OF borel_def])
   354   fix M :: "'a set" assume "M \<in> {S. open S}"
   355   then have "open M" by simp
   356   show "M \<in> ?SIGMA"
   357     apply (subst open_UNION_box[OF `open M`])
   358     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
   359     apply (auto intro: countable_rat)
   360     done
   361 qed (auto simp: box_def)
   362 
   363 lemma borel_eq_greaterThanLessThan:
   364   "borel = sigma UNIV (range (\<lambda> (a, b). {a <..< b} :: 'a \<Colon> ordered_euclidean_space set))"
   365   unfolding borel_eq_box apply (rule arg_cong2[where f=sigma])
   366   by (auto simp: box_def image_iff mem_interval set_eq_iff simp del: greaterThanLessThan_iff)
   367 
   368 lemma halfspace_gt_in_halfspace:
   369   assumes i: "i \<in> A"
   370   shows "{x\<Colon>'a. a < x \<bullet> i} \<in> 
   371     sigma_sets UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
   372   (is "?set \<in> ?SIGMA")
   373 proof -
   374   interpret sigma_algebra UNIV ?SIGMA
   375     by (intro sigma_algebra_sigma_sets) simp_all
   376   have *: "?set = (\<Union>n. UNIV - {x\<Colon>'a. x \<bullet> i < a + 1 / real (Suc n)})"
   377   proof (safe, simp_all add: not_less)
   378     fix x :: 'a assume "a < x \<bullet> i"
   379     with reals_Archimedean[of "x \<bullet> i - a"]
   380     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
   381       by (auto simp: inverse_eq_divide field_simps)
   382     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
   383       by (blast intro: less_imp_le)
   384   next
   385     fix x n
   386     have "a < a + 1 / real (Suc n)" by auto
   387     also assume "\<dots> \<le> x"
   388     finally show "a < x" .
   389   qed
   390   show "?set \<in> ?SIGMA" unfolding *
   391     by (auto del: Diff intro!: Diff i)
   392 qed
   393 
   394 lemma borel_eq_halfspace_less:
   395   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
   396   (is "_ = ?SIGMA")
   397 proof (rule borel_eq_sigmaI2[OF borel_eq_box])
   398   fix a b :: 'a
   399   have "box a b = {x\<in>space ?SIGMA. \<forall>i\<in>Basis. a \<bullet> i < x \<bullet> i \<and> x \<bullet> i < b \<bullet> i}"
   400     by (auto simp: box_def)
   401   also have "\<dots> \<in> sets ?SIGMA"
   402     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
   403        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
   404   finally show "box a b \<in> sets ?SIGMA" .
   405 qed auto
   406 
   407 lemma borel_eq_halfspace_le:
   408   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
   409   (is "_ = ?SIGMA")
   410 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   411   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   412   then have i: "i \<in> Basis" by auto
   413   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
   414   proof (safe, simp_all)
   415     fix x::'a assume *: "x\<bullet>i < a"
   416     with reals_Archimedean[of "a - x\<bullet>i"]
   417     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
   418       by (auto simp: field_simps inverse_eq_divide)
   419     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
   420       by (blast intro: less_imp_le)
   421   next
   422     fix x::'a and n
   423     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
   424     also have "\<dots> < a" by auto
   425     finally show "x\<bullet>i < a" .
   426   qed
   427   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   428     by (safe intro!: sets.countable_UN) (auto intro: i)
   429 qed auto
   430 
   431 lemma borel_eq_halfspace_ge:
   432   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
   433   (is "_ = ?SIGMA")
   434 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   435   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
   436   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
   437   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   438     using i by (safe intro!: sets.compl_sets) auto
   439 qed auto
   440 
   441 lemma borel_eq_halfspace_greater:
   442   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
   443   (is "_ = ?SIGMA")
   444 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
   445   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
   446   then have i: "i \<in> Basis" by auto
   447   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
   448   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   449     by (safe intro!: sets.compl_sets) (auto intro: i)
   450 qed auto
   451 
   452 lemma borel_eq_atMost:
   453   "borel = sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))"
   454   (is "_ = ?SIGMA")
   455 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   456   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   457   then have "i \<in> Basis" by auto
   458   then have *: "{x::'a. x\<bullet>i \<le> a} = (\<Union>k::nat. {.. (\<Sum>n\<in>Basis. (if n = i then a else real k)*\<^sub>R n)})"
   459   proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   460     fix x :: 'a
   461     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
   462     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
   463       by (subst (asm) Max_le_iff) auto
   464     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
   465       by (auto intro!: exI[of _ k])
   466   qed
   467   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   468     by (safe intro!: sets.countable_UN) auto
   469 qed auto
   470 
   471 lemma borel_eq_greaterThan:
   472   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {a<..}))"
   473   (is "_ = ?SIGMA")
   474 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   475   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   476   then have i: "i \<in> Basis" by auto
   477   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
   478   also have *: "{x::'a. a < x\<bullet>i} =
   479       (\<Union>k::nat. {\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n <..})" using i
   480   proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   481     fix x :: 'a
   482     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
   483     guess k::nat .. note k = this
   484     { fix i :: 'a assume "i \<in> Basis"
   485       then have "-x\<bullet>i < real k"
   486         using k by (subst (asm) Max_less_iff) auto
   487       then have "- real k < x\<bullet>i" by simp }
   488     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
   489       by (auto intro!: exI[of _ k])
   490   qed
   491   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
   492     apply (simp only:)
   493     apply (safe intro!: sets.countable_UN sets.Diff)
   494     apply (auto intro: sigma_sets_top)
   495     done
   496 qed auto
   497 
   498 lemma borel_eq_lessThan:
   499   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {..<a}))"
   500   (is "_ = ?SIGMA")
   501 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
   502   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   503   then have i: "i \<in> Basis" by auto
   504   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
   505   also have *: "{x::'a. x\<bullet>i < a} = (\<Union>k::nat. {..< \<Sum>n\<in>Basis. (if n = i then a else real k) *\<^sub>R n})" using `i\<in> Basis`
   506   proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   507     fix x :: 'a
   508     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
   509     guess k::nat .. note k = this
   510     { fix i :: 'a assume "i \<in> Basis"
   511       then have "x\<bullet>i < real k"
   512         using k by (subst (asm) Max_less_iff) auto
   513       then have "x\<bullet>i < real k" by simp }
   514     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
   515       by (auto intro!: exI[of _ k])
   516   qed
   517   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
   518     apply (simp only:)
   519     apply (safe intro!: sets.countable_UN sets.Diff)
   520     apply (auto intro: sigma_sets_top)
   521     done
   522 qed auto
   523 
   524 lemma borel_eq_atLeastAtMost:
   525   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} \<Colon>'a\<Colon>ordered_euclidean_space set))"
   526   (is "_ = ?SIGMA")
   527 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
   528   fix a::'a
   529   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   530   proof (safe, simp_all add: eucl_le[where 'a='a])
   531     fix x :: 'a
   532     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
   533     guess k::nat .. note k = this
   534     { fix i :: 'a assume "i \<in> Basis"
   535       with k have "- x\<bullet>i \<le> real k"
   536         by (subst (asm) Max_le_iff) (auto simp: field_simps)
   537       then have "- real k \<le> x\<bullet>i" by simp }
   538     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
   539       by (auto intro!: exI[of _ k])
   540   qed
   541   show "{..a} \<in> ?SIGMA" unfolding *
   542     by (safe intro!: sets.countable_UN)
   543        (auto intro!: sigma_sets_top)
   544 qed auto
   545 
   546 lemma borel_eq_atLeastLessThan:
   547   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
   548 proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
   549   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   550   fix x :: real
   551   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
   552     by (auto simp: move_uminus real_arch_simple)
   553   then show "{..< x} \<in> ?SIGMA"
   554     by (auto intro: sigma_sets.intros)
   555 qed auto
   556 
   557 lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   558   unfolding borel_def
   559 proof (intro sigma_eqI sigma_sets_eqI, safe)
   560   fix x :: "'a set" assume "open x"
   561   hence "x = UNIV - (UNIV - x)" by auto
   562   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   563     by (rule sigma_sets.Compl)
   564        (auto intro!: sigma_sets.Basic simp: `open x`)
   565   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   566 next
   567   fix x :: "'a set" assume "closed x"
   568   hence "x = UNIV - (UNIV - x)" by auto
   569   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   570     by (rule sigma_sets.Compl)
   571        (auto intro!: sigma_sets.Basic simp: `closed x`)
   572   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   573 qed simp_all
   574 
   575 lemma borel_measurable_halfspacesI:
   576   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   577   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
   578   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
   579   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
   580 proof safe
   581   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
   582   then show "S a i \<in> sets M" unfolding assms
   583     by (auto intro!: measurable_sets simp: assms(1))
   584 next
   585   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
   586   then show "f \<in> borel_measurable M"
   587     by (auto intro!: measurable_measure_of simp: S_eq F)
   588 qed
   589 
   590 lemma borel_measurable_iff_halfspace_le:
   591   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   592   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i \<le> a} \<in> sets M)"
   593   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   594 
   595 lemma borel_measurable_iff_halfspace_less:
   596   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   597   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. f w \<bullet> i < a} \<in> sets M)"
   598   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   599 
   600 lemma borel_measurable_iff_halfspace_ge:
   601   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   602   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a \<le> f w \<bullet> i} \<in> sets M)"
   603   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   604 
   605 lemma borel_measurable_iff_halfspace_greater:
   606   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   607   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. \<forall>a. {w \<in> space M. a < f w \<bullet> i} \<in> sets M)"
   608   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
   609 
   610 lemma borel_measurable_iff_le:
   611   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   612   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   613 
   614 lemma borel_measurable_iff_less:
   615   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   616   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   617 
   618 lemma borel_measurable_iff_ge:
   619   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   620   using borel_measurable_iff_halfspace_ge[where 'c=real]
   621   by simp
   622 
   623 lemma borel_measurable_iff_greater:
   624   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   625   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   626 
   627 lemma borel_measurable_euclidean_space:
   628   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   629   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
   630 proof safe
   631   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
   632   then show "f \<in> borel_measurable M"
   633     by (subst borel_measurable_iff_halfspace_le) auto
   634 qed auto
   635 
   636 subsection "Borel measurable operators"
   637 
   638 lemma borel_measurable_uminus[measurable (raw)]:
   639   fixes g :: "'a \<Rightarrow> real"
   640   assumes g: "g \<in> borel_measurable M"
   641   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   642   by (rule borel_measurable_continuous_on[OF _ g]) (auto intro: continuous_on_minus continuous_on_id)
   643 
   644 lemma borel_measurable_add[measurable (raw)]:
   645   fixes f g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   646   assumes f: "f \<in> borel_measurable M"
   647   assumes g: "g \<in> borel_measurable M"
   648   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   649   using f g
   650   by (rule borel_measurable_continuous_Pair)
   651      (auto intro: continuous_on_fst continuous_on_snd continuous_on_add)
   652 
   653 lemma borel_measurable_setsum[measurable (raw)]:
   654   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   655   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   656   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   657 proof cases
   658   assume "finite S"
   659   thus ?thesis using assms by induct auto
   660 qed simp
   661 
   662 lemma borel_measurable_diff[measurable (raw)]:
   663   fixes f :: "'a \<Rightarrow> real"
   664   assumes f: "f \<in> borel_measurable M"
   665   assumes g: "g \<in> borel_measurable M"
   666   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   667   unfolding diff_minus using assms by simp
   668 
   669 lemma borel_measurable_times[measurable (raw)]:
   670   fixes f :: "'a \<Rightarrow> real"
   671   assumes f: "f \<in> borel_measurable M"
   672   assumes g: "g \<in> borel_measurable M"
   673   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   674   using f g
   675   by (rule borel_measurable_continuous_Pair)
   676      (auto intro: continuous_on_fst continuous_on_snd continuous_on_mult)
   677 
   678 lemma borel_measurable_dist[measurable (raw)]:
   679   fixes g f :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   680   assumes f: "f \<in> borel_measurable M"
   681   assumes g: "g \<in> borel_measurable M"
   682   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   683   using f g
   684   by (rule borel_measurable_continuous_Pair)
   685      (intro continuous_on_dist continuous_on_fst continuous_on_snd)
   686   
   687 lemma borel_measurable_scaleR[measurable (raw)]:
   688   fixes g :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   689   assumes f: "f \<in> borel_measurable M"
   690   assumes g: "g \<in> borel_measurable M"
   691   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
   692   by (rule borel_measurable_continuous_Pair[OF f g])
   693      (auto intro!: continuous_on_scaleR continuous_on_fst continuous_on_snd)
   694 
   695 lemma affine_borel_measurable_vector:
   696   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   697   assumes "f \<in> borel_measurable M"
   698   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   699 proof (rule borel_measurableI)
   700   fix S :: "'x set" assume "open S"
   701   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   702   proof cases
   703     assume "b \<noteq> 0"
   704     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   705       by (auto intro!: open_affinity simp: scaleR_add_right)
   706     hence "?S \<in> sets borel" by auto
   707     moreover
   708     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   709       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   710     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   711       by auto
   712   qed simp
   713 qed
   714 
   715 lemma borel_measurable_const_scaleR[measurable (raw)]:
   716   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
   717   using affine_borel_measurable_vector[of f M 0 b] by simp
   718 
   719 lemma borel_measurable_const_add[measurable (raw)]:
   720   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
   721   using affine_borel_measurable_vector[of f M a 1] by simp
   722 
   723 lemma borel_measurable_setprod[measurable (raw)]:
   724   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   725   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   726   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   727 proof cases
   728   assume "finite S"
   729   thus ?thesis using assms by induct auto
   730 qed simp
   731 
   732 lemma borel_measurable_inverse[measurable (raw)]:
   733   fixes f :: "'a \<Rightarrow> real"
   734   assumes f: "f \<in> borel_measurable M"
   735   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   736 proof -
   737   have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) \<in> borel_measurable borel"
   738     by (intro borel_measurable_continuous_on_open' continuous_on_inverse continuous_on_id) auto
   739   also have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) = inverse" by (intro ext) auto
   740   finally show ?thesis using f by simp
   741 qed
   742 
   743 lemma borel_measurable_divide[measurable (raw)]:
   744   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x::real) \<in> borel_measurable M"
   745   by (simp add: field_divide_inverse)
   746 
   747 lemma borel_measurable_max[measurable (raw)]:
   748   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: real) \<in> borel_measurable M"
   749   by (simp add: max_def)
   750 
   751 lemma borel_measurable_min[measurable (raw)]:
   752   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: real) \<in> borel_measurable M"
   753   by (simp add: min_def)
   754 
   755 lemma borel_measurable_abs[measurable (raw)]:
   756   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   757   unfolding abs_real_def by simp
   758 
   759 lemma borel_measurable_nth[measurable (raw)]:
   760   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
   761   by (simp add: cart_eq_inner_axis)
   762 
   763 lemma convex_measurable:
   764   fixes a b :: real
   765   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   766   assumes q: "convex_on { a <..< b} q"
   767   shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
   768 proof -
   769   have "(\<lambda>x. if X x \<in> {a <..< b} then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
   770   proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
   771     show "open {a<..<b}" by auto
   772     from this q show "continuous_on {a<..<b} q"
   773       by (rule convex_on_continuous)
   774   qed
   775   also have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
   776     using X by (intro measurable_cong) auto
   777   finally show ?thesis .
   778 qed
   779 
   780 lemma borel_measurable_ln[measurable (raw)]:
   781   assumes f: "f \<in> borel_measurable M"
   782   shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
   783 proof -
   784   { fix x :: real assume x: "x \<le> 0"
   785     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
   786     from this[of x] x this[of 0] have "ln 0 = ln x"
   787       by (auto simp: ln_def) }
   788   note ln_imp = this
   789   have "(\<lambda>x. if f x \<in> {0<..} then ln (f x) else ln 0) \<in> borel_measurable M"
   790   proof (rule borel_measurable_continuous_on_open[OF _ _ f])
   791     show "continuous_on {0<..} ln"
   792       by (auto intro!: continuous_at_imp_continuous_on DERIV_ln DERIV_isCont)
   793     show "open ({0<..}::real set)" by auto
   794   qed
   795   also have "(\<lambda>x. if x \<in> {0<..} then ln x else ln 0) = ln"
   796     by (simp add: fun_eq_iff not_less ln_imp)
   797   finally show ?thesis .
   798 qed
   799 
   800 lemma borel_measurable_log[measurable (raw)]:
   801   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
   802   unfolding log_def by auto
   803 
   804 lemma borel_measurable_exp[measurable]: "exp \<in> borel_measurable borel"
   805   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
   806 
   807 lemma measurable_count_space_eq2_countable:
   808   fixes f :: "'a => 'c::countable"
   809   shows "f \<in> measurable M (count_space A) \<longleftrightarrow> (f \<in> space M \<rightarrow> A \<and> (\<forall>a\<in>A. f -` {a} \<inter> space M \<in> sets M))"
   810 proof -
   811   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
   812     then have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)"
   813       by auto
   814     moreover assume "\<And>a. a\<in>A \<Longrightarrow> f -` {a} \<inter> space M \<in> sets M"
   815     ultimately have "f -` X \<inter> space M \<in> sets M"
   816       using `X \<subseteq> A` by (simp add: subset_eq del: UN_simps) }
   817   then show ?thesis
   818     unfolding measurable_def by auto
   819 qed
   820 
   821 lemma measurable_real_floor[measurable]:
   822   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   823 proof -
   824   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
   825     by (auto intro: floor_eq2)
   826   then show ?thesis
   827     by (auto simp: vimage_def measurable_count_space_eq2_countable)
   828 qed
   829 
   830 lemma measurable_real_natfloor[measurable]:
   831   "(natfloor :: real \<Rightarrow> nat) \<in> measurable borel (count_space UNIV)"
   832   by (simp add: natfloor_def[abs_def])
   833 
   834 lemma measurable_real_ceiling[measurable]:
   835   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   836   unfolding ceiling_def[abs_def] by simp
   837 
   838 lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
   839   by simp
   840 
   841 lemma borel_measurable_real_natfloor:
   842   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
   843   by simp
   844 
   845 subsection "Borel space on the extended reals"
   846 
   847 lemma borel_measurable_ereal[measurable (raw)]:
   848   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   849   using continuous_on_ereal f by (rule borel_measurable_continuous_on)
   850 
   851 lemma borel_measurable_real_of_ereal[measurable (raw)]:
   852   fixes f :: "'a \<Rightarrow> ereal" 
   853   assumes f: "f \<in> borel_measurable M"
   854   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   855 proof -
   856   have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
   857     using continuous_on_real
   858     by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
   859   also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
   860     by auto
   861   finally show ?thesis .
   862 qed
   863 
   864 lemma borel_measurable_ereal_cases:
   865   fixes f :: "'a \<Rightarrow> ereal" 
   866   assumes f: "f \<in> borel_measurable M"
   867   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
   868   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
   869 proof -
   870   let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real (f x)))"
   871   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
   872   with f H show ?thesis by simp
   873 qed
   874 
   875 lemma
   876   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
   877   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   878     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   879     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   880   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
   881 
   882 lemma borel_measurable_uminus_eq_ereal[simp]:
   883   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   884 proof
   885   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   886 qed auto
   887 
   888 lemma set_Collect_ereal2:
   889   fixes f g :: "'a \<Rightarrow> ereal" 
   890   assumes f: "f \<in> borel_measurable M"
   891   assumes g: "g \<in> borel_measurable M"
   892   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
   893     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
   894     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
   895     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
   896     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
   897   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
   898 proof -
   899   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = -\<infinity> then H y (-\<infinity>) else H y (ereal (real (g x)))"
   900   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = -\<infinity> then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
   901   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   902   note * = this
   903   from assms show ?thesis
   904     by (subst *) (simp del: space_borel split del: split_if)
   905 qed
   906 
   907 lemma [measurable]:
   908   fixes f g :: "'a \<Rightarrow> ereal"
   909   assumes f: "f \<in> borel_measurable M"
   910   assumes g: "g \<in> borel_measurable M"
   911   shows borel_measurable_ereal_le: "{x \<in> space M. f x \<le> g x} \<in> sets M"
   912     and borel_measurable_ereal_less: "{x \<in> space M. f x < g x} \<in> sets M"
   913     and borel_measurable_ereal_eq: "{w \<in> space M. f w = g w} \<in> sets M"
   914   using f g by (simp_all add: set_Collect_ereal2)
   915 
   916 lemma borel_measurable_ereal_neq:
   917   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> {w \<in> space M. f w \<noteq> (g w :: ereal)} \<in> sets M"
   918   by simp
   919 
   920 lemma borel_measurable_ereal_iff:
   921   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   922 proof
   923   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   924   from borel_measurable_real_of_ereal[OF this]
   925   show "f \<in> borel_measurable M" by auto
   926 qed auto
   927 
   928 lemma borel_measurable_ereal_iff_real:
   929   fixes f :: "'a \<Rightarrow> ereal"
   930   shows "f \<in> borel_measurable M \<longleftrightarrow>
   931     ((\<lambda>x. real (f x)) \<in> borel_measurable M \<and> f -` {\<infinity>} \<inter> space M \<in> sets M \<and> f -` {-\<infinity>} \<inter> space M \<in> sets M)"
   932 proof safe
   933   assume *: "(\<lambda>x. real (f x)) \<in> borel_measurable M" "f -` {\<infinity>} \<inter> space M \<in> sets M" "f -` {-\<infinity>} \<inter> space M \<in> sets M"
   934   have "f -` {\<infinity>} \<inter> space M = {x\<in>space M. f x = \<infinity>}" "f -` {-\<infinity>} \<inter> space M = {x\<in>space M. f x = -\<infinity>}" by auto
   935   with * have **: "{x\<in>space M. f x = \<infinity>} \<in> sets M" "{x\<in>space M. f x = -\<infinity>} \<in> sets M" by simp_all
   936   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
   937   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   938   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
   939   finally show "f \<in> borel_measurable M" .
   940 qed simp_all
   941 
   942 lemma borel_measurable_eq_atMost_ereal:
   943   fixes f :: "'a \<Rightarrow> ereal"
   944   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   945 proof (intro iffI allI)
   946   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   947   show "f \<in> borel_measurable M"
   948     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
   949   proof (intro conjI allI)
   950     fix a :: real
   951     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
   952       have "x = \<infinity>"
   953       proof (rule ereal_top)
   954         fix B from reals_Archimedean2[of B] guess n ..
   955         then have "ereal B < real n" by auto
   956         with * show "B \<le> x" by (metis less_trans less_imp_le)
   957       qed }
   958     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
   959       by (auto simp: not_le)
   960     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos
   961       by (auto simp del: UN_simps)
   962     moreover
   963     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
   964     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
   965     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
   966       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
   967     moreover have "{w \<in> space M. real (f w) \<le> a} =
   968       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
   969       else {w \<in> space M. f w \<le> ereal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
   970       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
   971     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   972   qed
   973 qed (simp add: measurable_sets)
   974 
   975 lemma borel_measurable_eq_atLeast_ereal:
   976   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   977 proof
   978   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
   979   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
   980     by (auto simp: ereal_uminus_le_reorder)
   981   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
   982     unfolding borel_measurable_eq_atMost_ereal by auto
   983   then show "f \<in> borel_measurable M" by simp
   984 qed (simp add: measurable_sets)
   985 
   986 lemma greater_eq_le_measurable:
   987   fixes f :: "'a \<Rightarrow> 'c::linorder"
   988   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   989 proof
   990   assume "f -` {a ..} \<inter> space M \<in> sets M"
   991   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
   992   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
   993 next
   994   assume "f -` {..< a} \<inter> space M \<in> sets M"
   995   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
   996   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
   997 qed
   998 
   999 lemma borel_measurable_ereal_iff_less:
  1000   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1001   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
  1002 
  1003 lemma less_eq_ge_measurable:
  1004   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1005   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1006 proof
  1007   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1008   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1009   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1010 next
  1011   assume "f -` {..a} \<inter> space M \<in> sets M"
  1012   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1013   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1014 qed
  1015 
  1016 lemma borel_measurable_ereal_iff_ge:
  1017   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1018   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  1019 
  1020 lemma borel_measurable_ereal2:
  1021   fixes f g :: "'a \<Rightarrow> ereal" 
  1022   assumes f: "f \<in> borel_measurable M"
  1023   assumes g: "g \<in> borel_measurable M"
  1024   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
  1025     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1026     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1027     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1028     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
  1029   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1030 proof -
  1031   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = - \<infinity> then H y (-\<infinity>) else H y (ereal (real (g x)))"
  1032   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = - \<infinity> then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
  1033   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1034   note * = this
  1035   from assms show ?thesis unfolding * by simp
  1036 qed
  1037 
  1038 lemma
  1039   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
  1040   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
  1041     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1042   using f by auto
  1043 
  1044 lemma [measurable(raw)]:
  1045   fixes f :: "'a \<Rightarrow> ereal"
  1046   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1047   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1048     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1049     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1050     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1051   by (simp_all add: borel_measurable_ereal2 min_def max_def)
  1052 
  1053 lemma [measurable(raw)]:
  1054   fixes f g :: "'a \<Rightarrow> ereal"
  1055   assumes "f \<in> borel_measurable M"
  1056   assumes "g \<in> borel_measurable M"
  1057   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1058     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1059   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1060 
  1061 lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1062   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1063   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1064   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1065 proof cases
  1066   assume "finite S"
  1067   thus ?thesis using assms
  1068     by induct auto
  1069 qed simp
  1070 
  1071 lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1072   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1073   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1074   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1075 proof cases
  1076   assume "finite S"
  1077   thus ?thesis using assms by induct auto
  1078 qed simp
  1079 
  1080 lemma borel_measurable_SUP[measurable (raw)]:
  1081   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1082   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1083   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1084   unfolding borel_measurable_ereal_iff_ge
  1085 proof
  1086   fix a
  1087   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1088     by (auto simp: less_SUP_iff)
  1089   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1090     using assms by auto
  1091 qed
  1092 
  1093 lemma borel_measurable_INF[measurable (raw)]:
  1094   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1095   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1096   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1097   unfolding borel_measurable_ereal_iff_less
  1098 proof
  1099   fix a
  1100   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1101     by (auto simp: INF_less_iff)
  1102   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1103     using assms by auto
  1104 qed
  1105 
  1106 lemma [measurable (raw)]:
  1107   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1108   assumes "\<And>i. f i \<in> borel_measurable M"
  1109   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1110     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1111   unfolding liminf_SUPR_INFI limsup_INFI_SUPR using assms by auto
  1112 
  1113 lemma sets_Collect_eventually_sequentially[measurable]:
  1114   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
  1115   unfolding eventually_sequentially by simp
  1116 
  1117 lemma sets_Collect_ereal_convergent[measurable]: 
  1118   fixes f :: "nat \<Rightarrow> 'a => ereal"
  1119   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1120   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
  1121   unfolding convergent_ereal by auto
  1122 
  1123 lemma borel_measurable_extreal_lim[measurable (raw)]:
  1124   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1125   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1126   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1127 proof -
  1128   have "\<And>x. lim (\<lambda>i. f i x) = (if convergent (\<lambda>i. f i x) then limsup (\<lambda>i. f i x) else (THE i. False))"
  1129     by (simp add: lim_def convergent_def convergent_limsup_cl)
  1130   then show ?thesis
  1131     by simp
  1132 qed
  1133 
  1134 lemma borel_measurable_ereal_LIMSEQ:
  1135   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1136   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1137   and u: "\<And>i. u i \<in> borel_measurable M"
  1138   shows "u' \<in> borel_measurable M"
  1139 proof -
  1140   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
  1141     using u' by (simp add: lim_imp_Liminf[symmetric])
  1142   with u show ?thesis by (simp cong: measurable_cong)
  1143 qed
  1144 
  1145 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1146   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1147   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1148   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1149   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1150 
  1151 section "LIMSEQ is borel measurable"
  1152 
  1153 lemma borel_measurable_LIMSEQ:
  1154   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1155   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1156   and u: "\<And>i. u i \<in> borel_measurable M"
  1157   shows "u' \<in> borel_measurable M"
  1158 proof -
  1159   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1160     using u' by (simp add: lim_imp_Liminf)
  1161   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1162     by auto
  1163   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1164 qed
  1165 
  1166 lemma sets_Collect_Cauchy[measurable]: 
  1167   fixes f :: "nat \<Rightarrow> 'a => real"
  1168   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1169   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1170   unfolding Cauchy_iff2 using f by auto
  1171 
  1172 lemma borel_measurable_lim[measurable (raw)]:
  1173   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1174   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1175   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1176 proof -
  1177   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1178   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1179     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1180   have "u' \<in> borel_measurable M"
  1181   proof (rule borel_measurable_LIMSEQ)
  1182     fix x
  1183     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1184       by (cases "Cauchy (\<lambda>i. f i x)")
  1185          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1186     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
  1187       unfolding u'_def 
  1188       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1189   qed measurable
  1190   then show ?thesis
  1191     unfolding * by measurable
  1192 qed
  1193 
  1194 lemma borel_measurable_suminf[measurable (raw)]:
  1195   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1196   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1197   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1198   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1199 
  1200 end