src/HOL/Probability/Borel_Space.thy
author immler
Tue Nov 27 13:48:40 2012 +0100 (2012-11-27)
changeset 50245 dea9363887a6
parent 50244 de72bbe42190
child 50387 3d8863c41fe8
permissions -rw-r--r--
based countable topological basis on Countable_Set
     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_countable_basis:
   466   fixes B::"'a::topological_space set set"
   467   assumes "countable B"
   468   assumes "topological_basis B"
   469   shows "borel = sigma UNIV B"
   470   unfolding borel_def
   471 proof (intro sigma_eqI sigma_sets_eqI, safe)
   472   interpret countable_basis using assms by unfold_locales
   473   fix X::"'a set" assume "open X"
   474   from open_countable_basisE[OF this] guess B' . note B' = this
   475   show "X \<in> sigma_sets UNIV B"
   476   proof cases
   477     assume "B' \<noteq> {}"
   478     thus "X \<in> sigma_sets UNIV B" using assms B'
   479       by (metis from_nat_into Union_image_eq countable_subset range_from_nat_into
   480         in_mono sigma_sets.Basic sigma_sets.Union)
   481   qed (simp add: sigma_sets.Empty B')
   482 next
   483   fix b assume "b \<in> B"
   484   hence "open b" by (rule topological_basis_open[OF assms(2)])
   485   thus "b \<in> sigma_sets UNIV (Collect open)" by auto
   486 qed simp_all
   487 
   488 lemma borel_eq_union_closed_basis:
   489   "borel = sigma UNIV union_closed_basis"
   490   by (rule borel_eq_countable_basis[OF countable_union_closed_basis basis_union_closed_basis])
   491 
   492 lemma borel_measurable_halfspacesI:
   493   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   494   assumes F: "borel = sigma UNIV (range F)"
   495   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
   496   and S: "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   497   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   498 proof safe
   499   fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   500   then show "S a i \<in> sets M" unfolding assms
   501     by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1))
   502 next
   503   assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   504   { fix a i have "S a i \<in> sets M"
   505     proof cases
   506       assume "i < DIM('c)"
   507       with a show ?thesis unfolding assms(2) by simp
   508     next
   509       assume "\<not> i < DIM('c)"
   510       from S[OF this] show ?thesis .
   511     qed }
   512   then show "f \<in> borel_measurable M"
   513     by (auto intro!: measurable_measure_of simp: S_eq F)
   514 qed
   515 
   516 lemma borel_measurable_iff_halfspace_le:
   517   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   518   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   519   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   520 
   521 lemma borel_measurable_iff_halfspace_less:
   522   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   523   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   524   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   525 
   526 lemma borel_measurable_iff_halfspace_ge:
   527   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   528   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   529   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   530 
   531 lemma borel_measurable_iff_halfspace_greater:
   532   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   533   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   534   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
   535 
   536 lemma borel_measurable_iff_le:
   537   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   538   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   539 
   540 lemma borel_measurable_iff_less:
   541   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   542   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   543 
   544 lemma borel_measurable_iff_ge:
   545   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   546   using borel_measurable_iff_halfspace_ge[where 'c=real]
   547   by simp
   548 
   549 lemma borel_measurable_iff_greater:
   550   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   551   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   552 
   553 lemma borel_measurable_euclidean_space:
   554   fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   555   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   556 proof safe
   557   fix i assume "f \<in> borel_measurable M"
   558   then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
   559     by (auto intro: borel_measurable_euclidean_component)
   560 next
   561   assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
   562   then show "f \<in> borel_measurable M"
   563     unfolding borel_measurable_iff_halfspace_le by auto
   564 qed
   565 
   566 subsection "Borel measurable operators"
   567 
   568 lemma borel_measurable_continuous_on:
   569   fixes f :: "'a::topological_space \<Rightarrow> 'b::topological_space"
   570   assumes f: "continuous_on UNIV f" and g: "g \<in> borel_measurable M"
   571   shows "(\<lambda>x. f (g x)) \<in> borel_measurable M"
   572   using measurable_comp[OF g borel_measurable_continuous_on1[OF f]] by (simp add: comp_def)
   573 
   574 lemma borel_measurable_continuous_on_open':
   575   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   576   assumes cont: "continuous_on A f" "open A"
   577   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
   578 proof (rule borel_measurableI)
   579   fix S :: "'b set" assume "open S"
   580   then have "open {x\<in>A. f x \<in> S}"
   581     by (intro continuous_open_preimage[OF cont]) auto
   582   then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
   583   have "?f -` S \<inter> space borel = 
   584     {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
   585     by (auto split: split_if_asm)
   586   also have "\<dots> \<in> sets borel"
   587     using * `open A` by auto
   588   finally show "?f -` S \<inter> space borel \<in> sets borel" .
   589 qed
   590 
   591 lemma borel_measurable_continuous_on_open:
   592   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
   593   assumes cont: "continuous_on A f" "open A"
   594   assumes g: "g \<in> borel_measurable M"
   595   shows "(\<lambda>x. if g x \<in> A then f (g x) else c) \<in> borel_measurable M"
   596   using measurable_comp[OF g borel_measurable_continuous_on_open'[OF cont], of c]
   597   by (simp add: comp_def)
   598 
   599 lemma borel_measurable_uminus[measurable (raw)]:
   600   fixes g :: "'a \<Rightarrow> real"
   601   assumes g: "g \<in> borel_measurable M"
   602   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   603   by (rule borel_measurable_continuous_on[OF _ g]) (auto intro: continuous_on_minus continuous_on_id)
   604 
   605 lemma euclidean_component_prod:
   606   fixes x :: "'a :: euclidean_space \<times> 'b :: euclidean_space"
   607   shows "x $$ i = (if i < DIM('a) then fst x $$ i else snd x $$ (i - DIM('a)))"
   608   unfolding euclidean_component_def basis_prod_def inner_prod_def by auto
   609 
   610 lemma borel_measurable_Pair[measurable (raw)]:
   611   fixes f :: "'a \<Rightarrow> 'b::ordered_euclidean_space" and g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   612   assumes f: "f \<in> borel_measurable M"
   613   assumes g: "g \<in> borel_measurable M"
   614   shows "(\<lambda>x. (f x, g x)) \<in> borel_measurable M"
   615 proof (intro borel_measurable_iff_halfspace_le[THEN iffD2] allI impI)
   616   fix i and a :: real assume i: "i < DIM('b \<times> 'c)"
   617   have [simp]: "\<And>P A B C. {w. (P \<longrightarrow> A w \<and> B w) \<and> (\<not> P \<longrightarrow> A w \<and> C w)} = 
   618     {w. A w \<and> (P \<longrightarrow> B w) \<and> (\<not> P \<longrightarrow> C w)}" by auto
   619   from i f g show "{w \<in> space M. (f w, g w) $$ i \<le> a} \<in> sets M"
   620     by (auto simp: euclidean_component_prod)
   621 qed
   622 
   623 lemma continuous_on_fst: "continuous_on UNIV fst"
   624 proof -
   625   have [simp]: "range fst = UNIV" by (auto simp: image_iff)
   626   show ?thesis
   627     using closed_vimage_fst
   628     by (auto simp: continuous_on_closed closed_closedin vimage_def)
   629 qed
   630 
   631 lemma continuous_on_snd: "continuous_on UNIV snd"
   632 proof -
   633   have [simp]: "range snd = UNIV" by (auto simp: image_iff)
   634   show ?thesis
   635     using closed_vimage_snd
   636     by (auto simp: continuous_on_closed closed_closedin vimage_def)
   637 qed
   638 
   639 lemma borel_measurable_continuous_Pair:
   640   fixes f :: "'a \<Rightarrow> 'b::ordered_euclidean_space" and g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   641   assumes [measurable]: "f \<in> borel_measurable M"
   642   assumes [measurable]: "g \<in> borel_measurable M"
   643   assumes H: "continuous_on UNIV (\<lambda>x. H (fst x) (snd x))"
   644   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
   645 proof -
   646   have eq: "(\<lambda>x. H (f x) (g x)) = (\<lambda>x. (\<lambda>x. H (fst x) (snd x)) (f x, g x))" by auto
   647   show ?thesis
   648     unfolding eq by (rule borel_measurable_continuous_on[OF H]) auto
   649 qed
   650 
   651 lemma borel_measurable_add[measurable (raw)]:
   652   fixes f g :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   653   assumes f: "f \<in> borel_measurable M"
   654   assumes g: "g \<in> borel_measurable M"
   655   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   656   using f g
   657   by (rule borel_measurable_continuous_Pair)
   658      (auto intro: continuous_on_fst continuous_on_snd continuous_on_add)
   659 
   660 lemma borel_measurable_setsum[measurable (raw)]:
   661   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   662   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   663   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   664 proof cases
   665   assume "finite S"
   666   thus ?thesis using assms by induct auto
   667 qed simp
   668 
   669 lemma borel_measurable_diff[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   unfolding diff_minus using assms by simp
   675 
   676 lemma borel_measurable_times[measurable (raw)]:
   677   fixes f :: "'a \<Rightarrow> real"
   678   assumes f: "f \<in> borel_measurable M"
   679   assumes g: "g \<in> borel_measurable M"
   680   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   681   using f g
   682   by (rule borel_measurable_continuous_Pair)
   683      (auto intro: continuous_on_fst continuous_on_snd continuous_on_mult)
   684 
   685 lemma continuous_on_dist:
   686   fixes f :: "'a :: t2_space \<Rightarrow> 'b :: metric_space"
   687   shows "continuous_on A f \<Longrightarrow> continuous_on A g \<Longrightarrow> continuous_on A (\<lambda>x. dist (f x) (g x))"
   688   unfolding continuous_on_eq_continuous_within by (auto simp: continuous_dist)
   689 
   690 lemma borel_measurable_dist[measurable (raw)]:
   691   fixes g f :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   692   assumes f: "f \<in> borel_measurable M"
   693   assumes g: "g \<in> borel_measurable M"
   694   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   695   using f g
   696   by (rule borel_measurable_continuous_Pair)
   697      (intro continuous_on_dist continuous_on_fst continuous_on_snd)
   698   
   699 lemma borel_measurable_scaleR[measurable (raw)]:
   700   fixes g :: "'a \<Rightarrow> 'b::ordered_euclidean_space"
   701   assumes f: "f \<in> borel_measurable M"
   702   assumes g: "g \<in> borel_measurable M"
   703   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
   704   by (rule borel_measurable_continuous_Pair[OF f g])
   705      (auto intro!: continuous_on_scaleR continuous_on_fst continuous_on_snd)
   706 
   707 lemma affine_borel_measurable_vector:
   708   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   709   assumes "f \<in> borel_measurable M"
   710   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   711 proof (rule borel_measurableI)
   712   fix S :: "'x set" assume "open S"
   713   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   714   proof cases
   715     assume "b \<noteq> 0"
   716     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   717       by (auto intro!: open_affinity simp: scaleR_add_right)
   718     hence "?S \<in> sets borel" by auto
   719     moreover
   720     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   721       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   722     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   723       by auto
   724   qed simp
   725 qed
   726 
   727 lemma borel_measurable_const_scaleR[measurable (raw)]:
   728   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
   729   using affine_borel_measurable_vector[of f M 0 b] by simp
   730 
   731 lemma borel_measurable_const_add[measurable (raw)]:
   732   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
   733   using affine_borel_measurable_vector[of f M a 1] by simp
   734 
   735 lemma borel_measurable_setprod[measurable (raw)]:
   736   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   737   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   738   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   739 proof cases
   740   assume "finite S"
   741   thus ?thesis using assms by induct auto
   742 qed simp
   743 
   744 lemma borel_measurable_inverse[measurable (raw)]:
   745   fixes f :: "'a \<Rightarrow> real"
   746   assumes f: "f \<in> borel_measurable M"
   747   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   748 proof -
   749   have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) \<in> borel_measurable borel"
   750     by (intro borel_measurable_continuous_on_open' continuous_on_inverse continuous_on_id) auto
   751   also have "(\<lambda>x::real. if x \<in> UNIV - {0} then inverse x else 0) = inverse" by (intro ext) auto
   752   finally show ?thesis using f by simp
   753 qed
   754 
   755 lemma borel_measurable_divide[measurable (raw)]:
   756   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. f x / g x::real) \<in> borel_measurable M"
   757   by (simp add: field_divide_inverse)
   758 
   759 lemma borel_measurable_max[measurable (raw)]:
   760   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. max (g x) (f x) :: real) \<in> borel_measurable M"
   761   by (simp add: max_def)
   762 
   763 lemma borel_measurable_min[measurable (raw)]:
   764   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. min (g x) (f x) :: real) \<in> borel_measurable M"
   765   by (simp add: min_def)
   766 
   767 lemma borel_measurable_abs[measurable (raw)]:
   768   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   769   unfolding abs_real_def by simp
   770 
   771 lemma borel_measurable_nth[measurable (raw)]:
   772   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
   773   by (simp add: nth_conv_component)
   774 
   775 lemma convex_measurable:
   776   fixes a b :: real
   777   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
   778   assumes q: "convex_on { a <..< b} q"
   779   shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
   780 proof -
   781   have "(\<lambda>x. if X x \<in> {a <..< b} then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
   782   proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
   783     show "open {a<..<b}" by auto
   784     from this q show "continuous_on {a<..<b} q"
   785       by (rule convex_on_continuous)
   786   qed
   787   also have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
   788     using X by (intro measurable_cong) auto
   789   finally show ?thesis .
   790 qed
   791 
   792 lemma borel_measurable_ln[measurable (raw)]:
   793   assumes f: "f \<in> borel_measurable M"
   794   shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
   795 proof -
   796   { fix x :: real assume x: "x \<le> 0"
   797     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
   798     from this[of x] x this[of 0] have "ln 0 = ln x"
   799       by (auto simp: ln_def) }
   800   note ln_imp = this
   801   have "(\<lambda>x. if f x \<in> {0<..} then ln (f x) else ln 0) \<in> borel_measurable M"
   802   proof (rule borel_measurable_continuous_on_open[OF _ _ f])
   803     show "continuous_on {0<..} ln"
   804       by (auto intro!: continuous_at_imp_continuous_on DERIV_ln DERIV_isCont
   805                simp: continuous_isCont[symmetric])
   806     show "open ({0<..}::real set)" by auto
   807   qed
   808   also have "(\<lambda>x. if x \<in> {0<..} then ln x else ln 0) = ln"
   809     by (simp add: fun_eq_iff not_less ln_imp)
   810   finally show ?thesis .
   811 qed
   812 
   813 lemma borel_measurable_log[measurable (raw)]:
   814   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
   815   unfolding log_def by auto
   816 
   817 lemma measurable_count_space_eq2_countable:
   818   fixes f :: "'a => 'c::countable"
   819   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))"
   820 proof -
   821   { fix X assume "X \<subseteq> A" "f \<in> space M \<rightarrow> A"
   822     then have "f -` X \<inter> space M = (\<Union>a\<in>X. f -` {a} \<inter> space M)"
   823       by auto
   824     moreover assume "\<And>a. a\<in>A \<Longrightarrow> f -` {a} \<inter> space M \<in> sets M"
   825     ultimately have "f -` X \<inter> space M \<in> sets M"
   826       using `X \<subseteq> A` by (simp add: subset_eq del: UN_simps) }
   827   then show ?thesis
   828     unfolding measurable_def by auto
   829 qed
   830 
   831 lemma measurable_real_floor[measurable]:
   832   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   833 proof -
   834   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
   835     by (auto intro: floor_eq2)
   836   then show ?thesis
   837     by (auto simp: vimage_def measurable_count_space_eq2_countable)
   838 qed
   839 
   840 lemma measurable_real_natfloor[measurable]:
   841   "(natfloor :: real \<Rightarrow> nat) \<in> measurable borel (count_space UNIV)"
   842   by (simp add: natfloor_def[abs_def])
   843 
   844 lemma measurable_real_ceiling[measurable]:
   845   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   846   unfolding ceiling_def[abs_def] by simp
   847 
   848 lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
   849   by simp
   850 
   851 lemma borel_measurable_real_natfloor:
   852   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
   853   by simp
   854 
   855 subsection "Borel space on the extended reals"
   856 
   857 lemma borel_measurable_ereal[measurable (raw)]:
   858   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   859   using continuous_on_ereal f by (rule borel_measurable_continuous_on)
   860 
   861 lemma borel_measurable_real_of_ereal[measurable (raw)]:
   862   fixes f :: "'a \<Rightarrow> ereal" 
   863   assumes f: "f \<in> borel_measurable M"
   864   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   865 proof -
   866   have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
   867     using continuous_on_real
   868     by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
   869   also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
   870     by auto
   871   finally show ?thesis .
   872 qed
   873 
   874 lemma borel_measurable_ereal_cases:
   875   fixes f :: "'a \<Rightarrow> ereal" 
   876   assumes f: "f \<in> borel_measurable M"
   877   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
   878   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
   879 proof -
   880   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)))"
   881   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
   882   with f H show ?thesis by simp
   883 qed
   884 
   885 lemma
   886   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
   887   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   888     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   889     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   890   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
   891 
   892 lemma borel_measurable_uminus_eq_ereal[simp]:
   893   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   894 proof
   895   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   896 qed auto
   897 
   898 lemma set_Collect_ereal2:
   899   fixes f g :: "'a \<Rightarrow> ereal" 
   900   assumes f: "f \<in> borel_measurable M"
   901   assumes g: "g \<in> borel_measurable M"
   902   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
   903     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
   904     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
   905     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
   906     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
   907   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
   908 proof -
   909   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)))"
   910   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"
   911   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   912   note * = this
   913   from assms show ?thesis
   914     by (subst *) (simp del: space_borel split del: split_if)
   915 qed
   916 
   917 lemma [measurable]:
   918   fixes f g :: "'a \<Rightarrow> ereal"
   919   assumes f: "f \<in> borel_measurable M"
   920   assumes g: "g \<in> borel_measurable M"
   921   shows borel_measurable_ereal_le: "{x \<in> space M. f x \<le> g x} \<in> sets M"
   922     and borel_measurable_ereal_less: "{x \<in> space M. f x < g x} \<in> sets M"
   923     and borel_measurable_ereal_eq: "{w \<in> space M. f w = g w} \<in> sets M"
   924   using f g by (simp_all add: set_Collect_ereal2)
   925 
   926 lemma borel_measurable_ereal_neq:
   927   "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"
   928   by simp
   929 
   930 lemma borel_measurable_ereal_iff:
   931   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   932 proof
   933   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   934   from borel_measurable_real_of_ereal[OF this]
   935   show "f \<in> borel_measurable M" by auto
   936 qed auto
   937 
   938 lemma borel_measurable_ereal_iff_real:
   939   fixes f :: "'a \<Rightarrow> ereal"
   940   shows "f \<in> borel_measurable M \<longleftrightarrow>
   941     ((\<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)"
   942 proof safe
   943   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"
   944   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
   945   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
   946   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
   947   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   948   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
   949   finally show "f \<in> borel_measurable M" .
   950 qed simp_all
   951 
   952 lemma borel_measurable_eq_atMost_ereal:
   953   fixes f :: "'a \<Rightarrow> ereal"
   954   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   955 proof (intro iffI allI)
   956   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   957   show "f \<in> borel_measurable M"
   958     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
   959   proof (intro conjI allI)
   960     fix a :: real
   961     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
   962       have "x = \<infinity>"
   963       proof (rule ereal_top)
   964         fix B from reals_Archimedean2[of B] guess n ..
   965         then have "ereal B < real n" by auto
   966         with * show "B \<le> x" by (metis less_trans less_imp_le)
   967       qed }
   968     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
   969       by (auto simp: not_le)
   970     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos
   971       by (auto simp del: UN_simps)
   972     moreover
   973     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
   974     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
   975     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
   976       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
   977     moreover have "{w \<in> space M. real (f w) \<le> a} =
   978       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
   979       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")
   980       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
   981     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   982   qed
   983 qed (simp add: measurable_sets)
   984 
   985 lemma borel_measurable_eq_atLeast_ereal:
   986   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   987 proof
   988   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
   989   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
   990     by (auto simp: ereal_uminus_le_reorder)
   991   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
   992     unfolding borel_measurable_eq_atMost_ereal by auto
   993   then show "f \<in> borel_measurable M" by simp
   994 qed (simp add: measurable_sets)
   995 
   996 lemma greater_eq_le_measurable:
   997   fixes f :: "'a \<Rightarrow> 'c::linorder"
   998   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   999 proof
  1000   assume "f -` {a ..} \<inter> space M \<in> sets M"
  1001   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
  1002   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
  1003 next
  1004   assume "f -` {..< a} \<inter> space M \<in> sets M"
  1005   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
  1006   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  1007 qed
  1008 
  1009 lemma borel_measurable_ereal_iff_less:
  1010   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1011   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
  1012 
  1013 lemma less_eq_ge_measurable:
  1014   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1015   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1016 proof
  1017   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1018   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1019   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1020 next
  1021   assume "f -` {..a} \<inter> space M \<in> sets M"
  1022   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1023   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1024 qed
  1025 
  1026 lemma borel_measurable_ereal_iff_ge:
  1027   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1028   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  1029 
  1030 lemma borel_measurable_ereal2:
  1031   fixes f g :: "'a \<Rightarrow> ereal" 
  1032   assumes f: "f \<in> borel_measurable M"
  1033   assumes g: "g \<in> borel_measurable M"
  1034   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
  1035     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1036     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1037     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1038     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
  1039   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1040 proof -
  1041   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)))"
  1042   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"
  1043   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1044   note * = this
  1045   from assms show ?thesis unfolding * by simp
  1046 qed
  1047 
  1048 lemma
  1049   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
  1050   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
  1051     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1052   using f by auto
  1053 
  1054 lemma [measurable(raw)]:
  1055   fixes f :: "'a \<Rightarrow> ereal"
  1056   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1057   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1058     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1059     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1060     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1061   by (simp_all add: borel_measurable_ereal2 min_def max_def)
  1062 
  1063 lemma [measurable(raw)]:
  1064   fixes f g :: "'a \<Rightarrow> ereal"
  1065   assumes "f \<in> borel_measurable M"
  1066   assumes "g \<in> borel_measurable M"
  1067   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1068     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1069   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1070 
  1071 lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1072   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1073   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1074   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1075 proof cases
  1076   assume "finite S"
  1077   thus ?thesis using assms
  1078     by induct auto
  1079 qed simp
  1080 
  1081 lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1082   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1083   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1084   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1085 proof cases
  1086   assume "finite S"
  1087   thus ?thesis using assms by induct auto
  1088 qed simp
  1089 
  1090 lemma borel_measurable_SUP[measurable (raw)]:
  1091   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1092   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1093   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1094   unfolding borel_measurable_ereal_iff_ge
  1095 proof
  1096   fix a
  1097   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1098     by (auto simp: less_SUP_iff)
  1099   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1100     using assms by auto
  1101 qed
  1102 
  1103 lemma borel_measurable_INF[measurable (raw)]:
  1104   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1105   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1106   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1107   unfolding borel_measurable_ereal_iff_less
  1108 proof
  1109   fix a
  1110   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1111     by (auto simp: INF_less_iff)
  1112   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1113     using assms by auto
  1114 qed
  1115 
  1116 lemma [measurable (raw)]:
  1117   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1118   assumes "\<And>i. f i \<in> borel_measurable M"
  1119   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1120     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1121   unfolding liminf_SUPR_INFI limsup_INFI_SUPR using assms by auto
  1122 
  1123 lemma sets_Collect_eventually_sequentially[measurable]:
  1124   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
  1125   unfolding eventually_sequentially by simp
  1126 
  1127 lemma sets_Collect_ereal_convergent[measurable]: 
  1128   fixes f :: "nat \<Rightarrow> 'a => ereal"
  1129   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1130   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
  1131   unfolding convergent_ereal by auto
  1132 
  1133 lemma borel_measurable_extreal_lim[measurable (raw)]:
  1134   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1135   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1136   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1137 proof -
  1138   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))"
  1139     using convergent_ereal_limsup by (simp add: lim_def convergent_def)
  1140   then show ?thesis
  1141     by simp
  1142 qed
  1143 
  1144 lemma borel_measurable_ereal_LIMSEQ:
  1145   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1146   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1147   and u: "\<And>i. u i \<in> borel_measurable M"
  1148   shows "u' \<in> borel_measurable M"
  1149 proof -
  1150   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
  1151     using u' by (simp add: lim_imp_Liminf[symmetric])
  1152   with u show ?thesis by (simp cong: measurable_cong)
  1153 qed
  1154 
  1155 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1156   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1157   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1158   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1159   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1160 
  1161 section "LIMSEQ is borel measurable"
  1162 
  1163 lemma borel_measurable_LIMSEQ:
  1164   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1165   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1166   and u: "\<And>i. u i \<in> borel_measurable M"
  1167   shows "u' \<in> borel_measurable M"
  1168 proof -
  1169   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1170     using u' by (simp add: lim_imp_Liminf)
  1171   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1172     by auto
  1173   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1174 qed
  1175 
  1176 lemma sets_Collect_Cauchy[measurable]: 
  1177   fixes f :: "nat \<Rightarrow> 'a => real"
  1178   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1179   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1180   unfolding Cauchy_iff2 using f by auto
  1181 
  1182 lemma borel_measurable_lim[measurable (raw)]:
  1183   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1184   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1185   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1186 proof -
  1187   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1188   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1189     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1190   have "u' \<in> borel_measurable M"
  1191   proof (rule borel_measurable_LIMSEQ)
  1192     fix x
  1193     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1194       by (cases "Cauchy (\<lambda>i. f i x)")
  1195          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1196     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
  1197       unfolding u'_def 
  1198       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1199   qed measurable
  1200   then show ?thesis
  1201     unfolding * by measurable
  1202 qed
  1203 
  1204 lemma borel_measurable_suminf[measurable (raw)]:
  1205   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1206   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1207   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1208   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1209 
  1210 end