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