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