src/HOL/Probability/Borel_Space.thy
author immler
Wed Feb 13 16:35:07 2013 +0100 (2013-02-13)
changeset 51106 5746e671ea70
parent 50882 a382bf90867e
child 51351 dd1dd470690b
permissions -rw-r--r--
eliminated union_closed_basis; cleanup Fin_Map
     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 continuous_on_dist:
   679   fixes f :: "'a :: t2_space \<Rightarrow> 'b :: metric_space"
   680   shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. dist (f x) (g x))"
   681   unfolding continuous_on_eq_continuous_within by (auto simp: continuous_dist)
   682 
   683 lemma borel_measurable_dist[measurable (raw)]:
   684   fixes g f :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   685   assumes f: "f \<in> borel_measurable M"
   686   assumes g: "g \<in> borel_measurable M"
   687   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   688   using f g
   689   by (rule borel_measurable_continuous_Pair)
   690      (intro continuous_on_dist continuous_on_fst continuous_on_snd)
   691   
   692 lemma borel_measurable_scaleR[measurable (raw)]:
   693   fixes g :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   694   assumes f: "f \<in> borel_measurable M"
   695   assumes g: "g \<in> borel_measurable M"
   696   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
   697   by (rule borel_measurable_continuous_Pair[OF f g])
   698      (auto intro!: continuous_on_scaleR continuous_on_fst continuous_on_snd)
   699 
   700 lemma affine_borel_measurable_vector:
   701   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   702   assumes "f \<in> borel_measurable M"
   703   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   704 proof (rule borel_measurableI)
   705   fix S :: "'x set" assume "open S"
   706   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   707   proof cases
   708     assume "b \<noteq> 0"
   709     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   710       by (auto intro!: open_affinity simp: scaleR_add_right)
   711     hence "?S \<in> sets borel" by auto
   712     moreover
   713     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   714       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   715     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   716       by auto
   717   qed simp
   718 qed
   719 
   720 lemma borel_measurable_const_scaleR[measurable (raw)]:
   721   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
   722   using affine_borel_measurable_vector[of f M 0 b] by simp
   723 
   724 lemma borel_measurable_const_add[measurable (raw)]:
   725   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
   726   using affine_borel_measurable_vector[of f M a 1] by simp
   727 
   728 lemma borel_measurable_setprod[measurable (raw)]:
   729   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   730   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   731   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   732 proof cases
   733   assume "finite S"
   734   thus ?thesis using assms by induct auto
   735 qed simp
   736 
   737 lemma borel_measurable_inverse[measurable (raw)]:
   738   fixes f :: "'a \<Rightarrow> real"
   739   assumes f: "f \<in> borel_measurable M"
   740   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   741 proof -
   742   have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) \<in> borel_measurable borel"
   743     by (intro borel_measurable_continuous_on_open' continuous_on_inverse continuous_on_id) auto
   744   also have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) = inverse" by (intro ext) auto
   745   finally show ?thesis using f by simp
   746 qed
   747 
   748 lemma borel_measurable_divide[measurable (raw)]:
   749   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x::real) \<in> borel_measurable M"
   750   by (simp add: field_divide_inverse)
   751 
   752 lemma borel_measurable_max[measurable (raw)]:
   753   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: real) \<in> borel_measurable M"
   754   by (simp add: max_def)
   755 
   756 lemma borel_measurable_min[measurable (raw)]:
   757   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: real) \<in> borel_measurable M"
   758   by (simp add: min_def)
   759 
   760 lemma borel_measurable_abs[measurable (raw)]:
   761   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   762   unfolding abs_real_def by simp
   763 
   764 lemma borel_measurable_nth[measurable (raw)]:
   765   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
   766   by (simp add: cart_eq_inner_axis)
   767 
   768 lemma convex_measurable:
   769   fixes a b :: real
   770   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   771   assumes q: "convex_on { a <..< b} q"
   772   shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
   773 proof -
   774   have "(\<lambda>x. if X x \<in> {a <..< b} then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
   775   proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
   776     show "open {a<..<b}" by auto
   777     from this q show "continuous_on {a<..<b} q"
   778       by (rule convex_on_continuous)
   779   qed
   780   also have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
   781     using X by (intro measurable_cong) auto
   782   finally show ?thesis .
   783 qed
   784 
   785 lemma borel_measurable_ln[measurable (raw)]:
   786   assumes f: "f \<in> borel_measurable M"
   787   shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
   788 proof -
   789   { fix x :: real assume x: "x \<le> 0"
   790     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
   791     from this[of x] x this[of 0] have "ln 0 = ln x"
   792       by (auto simp: ln_def) }
   793   note ln_imp = this
   794   have "(\<lambda>x. if f x \<in> {0<..} then ln (f x) else ln 0) \<in> borel_measurable M"
   795   proof (rule borel_measurable_continuous_on_open[OF _ _ f])
   796     show "continuous_on {0<..} ln"
   797       by (auto intro!: continuous_at_imp_continuous_on DERIV_ln DERIV_isCont
   798                simp: continuous_isCont[symmetric])
   799     show "open ({0<..}::real set)" by auto
   800   qed
   801   also have "(\<lambda>x. if x \<in> {0<..} then ln x else ln 0) = ln"
   802     by (simp add: fun_eq_iff not_less ln_imp)
   803   finally show ?thesis .
   804 qed
   805 
   806 lemma borel_measurable_log[measurable (raw)]:
   807   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
   808   unfolding log_def by auto
   809 
   810 lemma borel_measurable_exp[measurable]: "exp \<in> borel_measurable borel"
   811   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI
   812             continuous_isCont[THEN iffD1] isCont_exp)
   813 
   814 lemma measurable_count_space_eq2_countable:
   815   fixes f :: "'a => 'c::countable"
   816   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))"
   817 proof -
   818   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
   819     then have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)"
   820       by auto
   821     moreover assume "\<And>a. a\<in>A \<Longrightarrow> f -` {a} \<inter> space M \<in> sets M"
   822     ultimately have "f -` X \<inter> space M \<in> sets M"
   823       using `X \<subseteq> A` by (simp add: subset_eq del: UN_simps) }
   824   then show ?thesis
   825     unfolding measurable_def by auto
   826 qed
   827 
   828 lemma measurable_real_floor[measurable]:
   829   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   830 proof -
   831   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
   832     by (auto intro: floor_eq2)
   833   then show ?thesis
   834     by (auto simp: vimage_def measurable_count_space_eq2_countable)
   835 qed
   836 
   837 lemma measurable_real_natfloor[measurable]:
   838   "(natfloor :: real \<Rightarrow> nat) \<in> measurable borel (count_space UNIV)"
   839   by (simp add: natfloor_def[abs_def])
   840 
   841 lemma measurable_real_ceiling[measurable]:
   842   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   843   unfolding ceiling_def[abs_def] by simp
   844 
   845 lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
   846   by simp
   847 
   848 lemma borel_measurable_real_natfloor:
   849   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
   850   by simp
   851 
   852 subsection "Borel space on the extended reals"
   853 
   854 lemma borel_measurable_ereal[measurable (raw)]:
   855   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   856   using continuous_on_ereal f by (rule borel_measurable_continuous_on)
   857 
   858 lemma borel_measurable_real_of_ereal[measurable (raw)]:
   859   fixes f :: "'a \<Rightarrow> ereal" 
   860   assumes f: "f \<in> borel_measurable M"
   861   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   862 proof -
   863   have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
   864     using continuous_on_real
   865     by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
   866   also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
   867     by auto
   868   finally show ?thesis .
   869 qed
   870 
   871 lemma borel_measurable_ereal_cases:
   872   fixes f :: "'a \<Rightarrow> ereal" 
   873   assumes f: "f \<in> borel_measurable M"
   874   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
   875   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
   876 proof -
   877   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)))"
   878   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
   879   with f H show ?thesis by simp
   880 qed
   881 
   882 lemma
   883   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
   884   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   885     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   886     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   887   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
   888 
   889 lemma borel_measurable_uminus_eq_ereal[simp]:
   890   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   891 proof
   892   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   893 qed auto
   894 
   895 lemma set_Collect_ereal2:
   896   fixes f g :: "'a \<Rightarrow> ereal" 
   897   assumes f: "f \<in> borel_measurable M"
   898   assumes g: "g \<in> borel_measurable M"
   899   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
   900     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
   901     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
   902     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
   903     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
   904   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
   905 proof -
   906   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)))"
   907   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"
   908   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   909   note * = this
   910   from assms show ?thesis
   911     by (subst *) (simp del: space_borel split del: split_if)
   912 qed
   913 
   914 lemma [measurable]:
   915   fixes f g :: "'a \<Rightarrow> ereal"
   916   assumes f: "f \<in> borel_measurable M"
   917   assumes g: "g \<in> borel_measurable M"
   918   shows borel_measurable_ereal_le: "{x \<in> space M. f x \<le> g x} \<in> sets M"
   919     and borel_measurable_ereal_less: "{x \<in> space M. f x < g x} \<in> sets M"
   920     and borel_measurable_ereal_eq: "{w \<in> space M. f w = g w} \<in> sets M"
   921   using f g by (simp_all add: set_Collect_ereal2)
   922 
   923 lemma borel_measurable_ereal_neq:
   924   "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"
   925   by simp
   926 
   927 lemma borel_measurable_ereal_iff:
   928   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   929 proof
   930   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   931   from borel_measurable_real_of_ereal[OF this]
   932   show "f \<in> borel_measurable M" by auto
   933 qed auto
   934 
   935 lemma borel_measurable_ereal_iff_real:
   936   fixes f :: "'a \<Rightarrow> ereal"
   937   shows "f \<in> borel_measurable M \<longleftrightarrow>
   938     ((\<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)"
   939 proof safe
   940   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"
   941   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
   942   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
   943   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
   944   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   945   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
   946   finally show "f \<in> borel_measurable M" .
   947 qed simp_all
   948 
   949 lemma borel_measurable_eq_atMost_ereal:
   950   fixes f :: "'a \<Rightarrow> ereal"
   951   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   952 proof (intro iffI allI)
   953   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   954   show "f \<in> borel_measurable M"
   955     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
   956   proof (intro conjI allI)
   957     fix a :: real
   958     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
   959       have "x = \<infinity>"
   960       proof (rule ereal_top)
   961         fix B from reals_Archimedean2[of B] guess n ..
   962         then have "ereal B < real n" by auto
   963         with * show "B \<le> x" by (metis less_trans less_imp_le)
   964       qed }
   965     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
   966       by (auto simp: not_le)
   967     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos
   968       by (auto simp del: UN_simps)
   969     moreover
   970     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
   971     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
   972     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
   973       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
   974     moreover have "{w \<in> space M. real (f w) \<le> a} =
   975       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
   976       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")
   977       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
   978     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   979   qed
   980 qed (simp add: measurable_sets)
   981 
   982 lemma borel_measurable_eq_atLeast_ereal:
   983   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   984 proof
   985   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
   986   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
   987     by (auto simp: ereal_uminus_le_reorder)
   988   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
   989     unfolding borel_measurable_eq_atMost_ereal by auto
   990   then show "f \<in> borel_measurable M" by simp
   991 qed (simp add: measurable_sets)
   992 
   993 lemma greater_eq_le_measurable:
   994   fixes f :: "'a \<Rightarrow> 'c::linorder"
   995   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   996 proof
   997   assume "f -` {a ..} \<inter> space M \<in> sets M"
   998   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
   999   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
  1000 next
  1001   assume "f -` {..< a} \<inter> space M \<in> sets M"
  1002   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
  1003   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  1004 qed
  1005 
  1006 lemma borel_measurable_ereal_iff_less:
  1007   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1008   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
  1009 
  1010 lemma less_eq_ge_measurable:
  1011   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1012   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1013 proof
  1014   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1015   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1016   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1017 next
  1018   assume "f -` {..a} \<inter> space M \<in> sets M"
  1019   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1020   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1021 qed
  1022 
  1023 lemma borel_measurable_ereal_iff_ge:
  1024   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1025   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  1026 
  1027 lemma borel_measurable_ereal2:
  1028   fixes f g :: "'a \<Rightarrow> ereal" 
  1029   assumes f: "f \<in> borel_measurable M"
  1030   assumes g: "g \<in> borel_measurable M"
  1031   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
  1032     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1033     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1034     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1035     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
  1036   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1037 proof -
  1038   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)))"
  1039   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"
  1040   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1041   note * = this
  1042   from assms show ?thesis unfolding * by simp
  1043 qed
  1044 
  1045 lemma
  1046   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
  1047   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
  1048     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1049   using f by auto
  1050 
  1051 lemma [measurable(raw)]:
  1052   fixes f :: "'a \<Rightarrow> ereal"
  1053   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1054   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1055     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1056     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1057     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1058   by (simp_all add: borel_measurable_ereal2 min_def max_def)
  1059 
  1060 lemma [measurable(raw)]:
  1061   fixes f g :: "'a \<Rightarrow> ereal"
  1062   assumes "f \<in> borel_measurable M"
  1063   assumes "g \<in> borel_measurable M"
  1064   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1065     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1066   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1067 
  1068 lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1069   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1070   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1071   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1072 proof cases
  1073   assume "finite S"
  1074   thus ?thesis using assms
  1075     by induct auto
  1076 qed simp
  1077 
  1078 lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1079   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1080   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1081   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1082 proof cases
  1083   assume "finite S"
  1084   thus ?thesis using assms by induct auto
  1085 qed simp
  1086 
  1087 lemma borel_measurable_SUP[measurable (raw)]:
  1088   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1089   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1090   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1091   unfolding borel_measurable_ereal_iff_ge
  1092 proof
  1093   fix a
  1094   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1095     by (auto simp: less_SUP_iff)
  1096   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1097     using assms by auto
  1098 qed
  1099 
  1100 lemma borel_measurable_INF[measurable (raw)]:
  1101   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1102   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1103   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1104   unfolding borel_measurable_ereal_iff_less
  1105 proof
  1106   fix a
  1107   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1108     by (auto simp: INF_less_iff)
  1109   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1110     using assms by auto
  1111 qed
  1112 
  1113 lemma [measurable (raw)]:
  1114   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1115   assumes "\<And>i. f i \<in> borel_measurable M"
  1116   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1117     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1118   unfolding liminf_SUPR_INFI limsup_INFI_SUPR using assms by auto
  1119 
  1120 lemma sets_Collect_eventually_sequentially[measurable]:
  1121   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
  1122   unfolding eventually_sequentially by simp
  1123 
  1124 lemma sets_Collect_ereal_convergent[measurable]: 
  1125   fixes f :: "nat \<Rightarrow> 'a => ereal"
  1126   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1127   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
  1128   unfolding convergent_ereal by auto
  1129 
  1130 lemma borel_measurable_extreal_lim[measurable (raw)]:
  1131   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1132   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1133   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1134 proof -
  1135   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))"
  1136     using convergent_ereal_limsup by (simp add: lim_def convergent_def)
  1137   then show ?thesis
  1138     by simp
  1139 qed
  1140 
  1141 lemma borel_measurable_ereal_LIMSEQ:
  1142   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1143   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1144   and u: "\<And>i. u i \<in> borel_measurable M"
  1145   shows "u' \<in> borel_measurable M"
  1146 proof -
  1147   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
  1148     using u' by (simp add: lim_imp_Liminf[symmetric])
  1149   with u show ?thesis by (simp cong: measurable_cong)
  1150 qed
  1151 
  1152 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1153   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1154   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1155   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1156   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1157 
  1158 section "LIMSEQ is borel measurable"
  1159 
  1160 lemma borel_measurable_LIMSEQ:
  1161   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1162   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1163   and u: "\<And>i. u i \<in> borel_measurable M"
  1164   shows "u' \<in> borel_measurable M"
  1165 proof -
  1166   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1167     using u' by (simp add: lim_imp_Liminf)
  1168   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1169     by auto
  1170   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1171 qed
  1172 
  1173 lemma sets_Collect_Cauchy[measurable]: 
  1174   fixes f :: "nat \<Rightarrow> 'a => real"
  1175   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1176   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1177   unfolding Cauchy_iff2 using f by auto
  1178 
  1179 lemma borel_measurable_lim[measurable (raw)]:
  1180   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1181   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1182   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1183 proof -
  1184   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1185   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1186     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1187   have "u' \<in> borel_measurable M"
  1188   proof (rule borel_measurable_LIMSEQ)
  1189     fix x
  1190     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1191       by (cases "Cauchy (\<lambda>i. f i x)")
  1192          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1193     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
  1194       unfolding u'_def 
  1195       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1196   qed measurable
  1197   then show ?thesis
  1198     unfolding * by measurable
  1199 qed
  1200 
  1201 lemma borel_measurable_suminf[measurable (raw)]:
  1202   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1203   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1204   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1205   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1206 
  1207 end