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