src/HOL/Probability/Borel_Space.thy
author hoelzl
Mon May 19 13:44:13 2014 +0200 (2014-05-19)
changeset 56994 8d5e5ec1cac3
parent 56993 e5366291d6aa
child 57036 22568fb89165
permissions -rw-r--r--
fixed document generation for HOL-Probability
     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 subsection {* 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 subsection {* 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_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 notation
   241   eucl_less (infix "<e" 50)
   242 
   243 lemma box_oc: "{x. a <e x \<and> x \<le> b} = {x. a <e x} \<inter> {..b}"
   244   and box_co: "{x. a \<le> x \<and> x <e b} = {a..} \<inter> {x. x <e b}"
   245   by auto
   246 
   247 lemma eucl_ivals[measurable]:
   248   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   249   shows "{x. x <e a} \<in> sets borel"
   250     and "{x. a <e x} \<in> sets borel"
   251     and "box a b \<in> sets borel"
   252     and "{..a} \<in> sets borel"
   253     and "{a..} \<in> sets borel"
   254     and "{a..b} \<in> sets borel"
   255     and  "{x. a <e x \<and> x \<le> b} \<in> sets borel"
   256     and "{x. a \<le> x \<and>  x <e b} \<in> sets borel"
   257   unfolding box_oc box_co
   258   by (auto intro: borel_open borel_closed)
   259 
   260 lemma open_Collect_less:
   261   fixes f g :: "'i::topological_space \<Rightarrow> 'a :: {dense_linorder, linorder_topology}"
   262   assumes "continuous_on UNIV f"
   263   assumes "continuous_on UNIV g"
   264   shows "open {x. f x < g x}"
   265 proof -
   266   have "open (\<Union>y. {x \<in> UNIV. f x \<in> {..< y}} \<inter> {x \<in> UNIV. g x \<in> {y <..}})" (is "open ?X")
   267     by (intro open_UN ballI open_Int continuous_open_preimage assms) auto
   268   also have "?X = {x. f x < g x}"
   269     by (auto intro: dense)
   270   finally show ?thesis .
   271 qed
   272 
   273 lemma closed_Collect_le:
   274   fixes f g :: "'i::topological_space \<Rightarrow> 'a :: {dense_linorder, linorder_topology}"
   275   assumes f: "continuous_on UNIV f"
   276   assumes g: "continuous_on UNIV g"
   277   shows "closed {x. f x \<le> g x}"
   278   using open_Collect_less[OF g f] unfolding not_less[symmetric] Collect_neg_eq open_closed .
   279 
   280 lemma borel_measurable_less[measurable]:
   281   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, dense_linorder, linorder_topology}"
   282   assumes "f \<in> borel_measurable M"
   283   assumes "g \<in> borel_measurable M"
   284   shows "{w \<in> space M. f w < g w} \<in> sets M"
   285 proof -
   286   have "{w \<in> space M. f w < g w} = (\<lambda>x. (f x, g x)) -` {x. fst x < snd x} \<inter> space M"
   287     by auto
   288   also have "\<dots> \<in> sets M"
   289     by (intro measurable_sets[OF borel_measurable_Pair borel_open, OF assms open_Collect_less]
   290               continuous_intros)
   291   finally show ?thesis .
   292 qed
   293 
   294 lemma
   295   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, dense_linorder, linorder_topology}"
   296   assumes f[measurable]: "f \<in> borel_measurable M"
   297   assumes g[measurable]: "g \<in> borel_measurable M"
   298   shows borel_measurable_le[measurable]: "{w \<in> space M. f w \<le> g w} \<in> sets M"
   299     and borel_measurable_eq[measurable]: "{w \<in> space M. f w = g w} \<in> sets M"
   300     and borel_measurable_neq: "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   301   unfolding eq_iff not_less[symmetric]
   302   by measurable
   303 
   304 lemma 
   305   fixes i :: "'a::{second_countable_topology, real_inner}"
   306   shows hafspace_less_borel: "{x. a < x \<bullet> i} \<in> sets borel"
   307     and hafspace_greater_borel: "{x. x \<bullet> i < a} \<in> sets borel"
   308     and hafspace_less_eq_borel: "{x. a \<le> x \<bullet> i} \<in> sets borel"
   309     and hafspace_greater_eq_borel: "{x. x \<bullet> i \<le> a} \<in> sets borel"
   310   by simp_all
   311 
   312 subsection "Borel space equals sigma algebras over intervals"
   313 
   314 lemma borel_sigma_sets_subset:
   315   "A \<subseteq> sets borel \<Longrightarrow> sigma_sets UNIV A \<subseteq> sets borel"
   316   using sets.sigma_sets_subset[of A borel] by simp
   317 
   318 lemma borel_eq_sigmaI1:
   319   fixes F :: "'i \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   320   assumes borel_eq: "borel = sigma UNIV X"
   321   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV (F ` A))"
   322   assumes F: "\<And>i. i \<in> A \<Longrightarrow> F i \<in> sets borel"
   323   shows "borel = sigma UNIV (F ` A)"
   324   unfolding borel_def
   325 proof (intro sigma_eqI antisym)
   326   have borel_rev_eq: "sigma_sets UNIV {S::'a set. open S} = sets borel"
   327     unfolding borel_def by simp
   328   also have "\<dots> = sigma_sets UNIV X"
   329     unfolding borel_eq by simp
   330   also have "\<dots> \<subseteq> sigma_sets UNIV (F`A)"
   331     using X by (intro sigma_algebra.sigma_sets_subset[OF sigma_algebra_sigma_sets]) auto
   332   finally show "sigma_sets UNIV {S. open S} \<subseteq> sigma_sets UNIV (F`A)" .
   333   show "sigma_sets UNIV (F`A) \<subseteq> sigma_sets UNIV {S. open S}"
   334     unfolding borel_rev_eq using F by (intro borel_sigma_sets_subset) auto
   335 qed auto
   336 
   337 lemma borel_eq_sigmaI2:
   338   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set"
   339     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   340   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`B)"
   341   assumes X: "\<And>i j. (i, j) \<in> B \<Longrightarrow> G i j \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   342   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   343   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   344   using assms
   345   by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` B" and F="(\<lambda>(i, j). F i j)"]) auto
   346 
   347 lemma borel_eq_sigmaI3:
   348   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and X :: "'a::topological_space set set"
   349   assumes borel_eq: "borel = sigma UNIV X"
   350   assumes X: "\<And>x. x \<in> X \<Longrightarrow> x \<in> sets (sigma UNIV ((\<lambda>(i, j). F i j) ` A))"
   351   assumes F: "\<And>i j. (i, j) \<in> A \<Longrightarrow> F i j \<in> sets borel"
   352   shows "borel = sigma UNIV ((\<lambda>(i, j). F i j) ` A)"
   353   using assms by (intro borel_eq_sigmaI1[where X=X and F="(\<lambda>(i, j). F i j)"]) auto
   354 
   355 lemma borel_eq_sigmaI4:
   356   fixes F :: "'i \<Rightarrow> 'a::topological_space set"
   357     and G :: "'l \<Rightarrow> 'k \<Rightarrow> 'a::topological_space set"
   358   assumes borel_eq: "borel = sigma UNIV ((\<lambda>(i, j). G i j)`A)"
   359   assumes X: "\<And>i j. (i, j) \<in> A \<Longrightarrow> G i j \<in> sets (sigma UNIV (range F))"
   360   assumes F: "\<And>i. F i \<in> sets borel"
   361   shows "borel = sigma UNIV (range F)"
   362   using assms by (intro borel_eq_sigmaI1[where X="(\<lambda>(i, j). G i j) ` A" and F=F]) auto
   363 
   364 lemma borel_eq_sigmaI5:
   365   fixes F :: "'i \<Rightarrow> 'j \<Rightarrow> 'a::topological_space set" and G :: "'l \<Rightarrow> 'a::topological_space set"
   366   assumes borel_eq: "borel = sigma UNIV (range G)"
   367   assumes X: "\<And>i. G i \<in> sets (sigma UNIV (range (\<lambda>(i, j). F i j)))"
   368   assumes F: "\<And>i j. F i j \<in> sets borel"
   369   shows "borel = sigma UNIV (range (\<lambda>(i, j). F i j))"
   370   using assms by (intro borel_eq_sigmaI1[where X="range G" and F="(\<lambda>(i, j). F i j)"]) auto
   371 
   372 lemma borel_eq_box:
   373   "borel = sigma UNIV (range (\<lambda> (a, b). box a b :: 'a \<Colon> euclidean_space set))"
   374     (is "_ = ?SIGMA")
   375 proof (rule borel_eq_sigmaI1[OF borel_def])
   376   fix M :: "'a set" assume "M \<in> {S. open S}"
   377   then have "open M" by simp
   378   show "M \<in> ?SIGMA"
   379     apply (subst open_UNION_box[OF `open M`])
   380     apply (safe intro!: sets.countable_UN' countable_PiE countable_Collect)
   381     apply (auto intro: countable_rat)
   382     done
   383 qed (auto simp: box_def)
   384 
   385 lemma halfspace_gt_in_halfspace:
   386   assumes i: "i \<in> A"
   387   shows "{x\<Colon>'a. a < x \<bullet> i} \<in> 
   388     sigma_sets UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> A))"
   389   (is "?set \<in> ?SIGMA")
   390 proof -
   391   interpret sigma_algebra UNIV ?SIGMA
   392     by (intro sigma_algebra_sigma_sets) simp_all
   393   have *: "?set = (\<Union>n. UNIV - {x\<Colon>'a. x \<bullet> i < a + 1 / real (Suc n)})"
   394   proof (safe, simp_all add: not_less)
   395     fix x :: 'a assume "a < x \<bullet> i"
   396     with reals_Archimedean[of "x \<bullet> i - a"]
   397     obtain n where "a + 1 / real (Suc n) < x \<bullet> i"
   398       by (auto simp: inverse_eq_divide field_simps)
   399     then show "\<exists>n. a + 1 / real (Suc n) \<le> x \<bullet> i"
   400       by (blast intro: less_imp_le)
   401   next
   402     fix x n
   403     have "a < a + 1 / real (Suc n)" by auto
   404     also assume "\<dots> \<le> x"
   405     finally show "a < x" .
   406   qed
   407   show "?set \<in> ?SIGMA" unfolding *
   408     by (auto del: Diff intro!: Diff i)
   409 qed
   410 
   411 lemma borel_eq_halfspace_less:
   412   "borel = sigma UNIV ((\<lambda>(a, i). {x::'a::euclidean_space. x \<bullet> i < a}) ` (UNIV \<times> Basis))"
   413   (is "_ = ?SIGMA")
   414 proof (rule borel_eq_sigmaI2[OF borel_eq_box])
   415   fix a b :: 'a
   416   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}"
   417     by (auto simp: box_def)
   418   also have "\<dots> \<in> sets ?SIGMA"
   419     by (intro sets.sets_Collect_conj sets.sets_Collect_finite_All sets.sets_Collect_const)
   420        (auto intro!: halfspace_gt_in_halfspace countable_PiE countable_rat)
   421   finally show "box a b \<in> sets ?SIGMA" .
   422 qed auto
   423 
   424 lemma borel_eq_halfspace_le:
   425   "borel = sigma UNIV ((\<lambda> (a, i). {x::'a::euclidean_space. x \<bullet> i \<le> a}) ` (UNIV \<times> Basis))"
   426   (is "_ = ?SIGMA")
   427 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   428   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   429   then have i: "i \<in> Basis" by auto
   430   have *: "{x::'a. x\<bullet>i < a} = (\<Union>n. {x. x\<bullet>i \<le> a - 1/real (Suc n)})"
   431   proof (safe, simp_all)
   432     fix x::'a assume *: "x\<bullet>i < a"
   433     with reals_Archimedean[of "a - x\<bullet>i"]
   434     obtain n where "x \<bullet> i < a - 1 / (real (Suc n))"
   435       by (auto simp: field_simps inverse_eq_divide)
   436     then show "\<exists>n. x \<bullet> i \<le> a - 1 / (real (Suc n))"
   437       by (blast intro: less_imp_le)
   438   next
   439     fix x::'a and n
   440     assume "x\<bullet>i \<le> a - 1 / real (Suc n)"
   441     also have "\<dots> < a" by auto
   442     finally show "x\<bullet>i < a" .
   443   qed
   444   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   445     by (safe intro!: sets.countable_UN) (auto intro: i)
   446 qed auto
   447 
   448 lemma borel_eq_halfspace_ge:
   449   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a \<le> x \<bullet> i}) ` (UNIV \<times> Basis))"
   450   (is "_ = ?SIGMA")
   451 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_less])
   452   fix a :: real and i :: 'a assume i: "(a, i) \<in> UNIV \<times> Basis"
   453   have *: "{x::'a. x\<bullet>i < a} = space ?SIGMA - {x::'a. a \<le> x\<bullet>i}" by auto
   454   show "{x. x\<bullet>i < a} \<in> ?SIGMA" unfolding *
   455     using i by (safe intro!: sets.compl_sets) auto
   456 qed auto
   457 
   458 lemma borel_eq_halfspace_greater:
   459   "borel = sigma UNIV ((\<lambda> (a, i). {x\<Colon>'a\<Colon>euclidean_space. a < x \<bullet> i}) ` (UNIV \<times> Basis))"
   460   (is "_ = ?SIGMA")
   461 proof (rule borel_eq_sigmaI2[OF borel_eq_halfspace_le])
   462   fix a :: real and i :: 'a assume "(a, i) \<in> (UNIV \<times> Basis)"
   463   then have i: "i \<in> Basis" by auto
   464   have *: "{x::'a. x\<bullet>i \<le> a} = space ?SIGMA - {x::'a. a < x\<bullet>i}" by auto
   465   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   466     by (safe intro!: sets.compl_sets) (auto intro: i)
   467 qed auto
   468 
   469 lemma borel_eq_atMost:
   470   "borel = sigma UNIV (range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space}))"
   471   (is "_ = ?SIGMA")
   472 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   473   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   474   then have "i \<in> Basis" by auto
   475   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)})"
   476   proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   477     fix x :: 'a
   478     from real_arch_simple[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"] guess k::nat ..
   479     then have "\<And>i. i \<in> Basis \<Longrightarrow> x\<bullet>i \<le> real k"
   480       by (subst (asm) Max_le_iff) auto
   481     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia \<le> real k"
   482       by (auto intro!: exI[of _ k])
   483   qed
   484   show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA" unfolding *
   485     by (safe intro!: sets.countable_UN) auto
   486 qed auto
   487 
   488 lemma borel_eq_greaterThan:
   489   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {x. a <e x}))"
   490   (is "_ = ?SIGMA")
   491 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_le])
   492   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   493   then have i: "i \<in> Basis" by auto
   494   have "{x::'a. x\<bullet>i \<le> a} = UNIV - {x::'a. a < x\<bullet>i}" by auto
   495   also have *: "{x::'a. a < x\<bullet>i} =
   496       (\<Union>k::nat. {x. (\<Sum>n\<in>Basis. (if n = i then a else -real k) *\<^sub>R n) <e x})" using i
   497   proof (safe, simp_all add: eucl_less_def split: split_if_asm)
   498     fix x :: 'a
   499     from reals_Archimedean2[of "Max ((\<lambda>i. -x\<bullet>i)`Basis)"]
   500     guess k::nat .. note k = this
   501     { fix i :: 'a assume "i \<in> Basis"
   502       then have "-x\<bullet>i < real k"
   503         using k by (subst (asm) Max_less_iff) auto
   504       then have "- real k < x\<bullet>i" by simp }
   505     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> -real k < x \<bullet> ia"
   506       by (auto intro!: exI[of _ k])
   507   qed
   508   finally show "{x. x\<bullet>i \<le> a} \<in> ?SIGMA"
   509     apply (simp only:)
   510     apply (safe intro!: sets.countable_UN sets.Diff)
   511     apply (auto intro: sigma_sets_top)
   512     done
   513 qed auto
   514 
   515 lemma borel_eq_lessThan:
   516   "borel = sigma UNIV (range (\<lambda>a\<Colon>'a\<Colon>ordered_euclidean_space. {x. x <e a}))"
   517   (is "_ = ?SIGMA")
   518 proof (rule borel_eq_sigmaI4[OF borel_eq_halfspace_ge])
   519   fix a :: real and i :: 'a assume "(a, i) \<in> UNIV \<times> Basis"
   520   then have i: "i \<in> Basis" by auto
   521   have "{x::'a. a \<le> x\<bullet>i} = UNIV - {x::'a. x\<bullet>i < a}" by auto
   522   also have *: "{x::'a. x\<bullet>i < a} = (\<Union>k::nat. {x. x <e (\<Sum>n\<in>Basis. (if n = i then a else real k) *\<^sub>R n)})" using `i\<in> Basis`
   523   proof (safe, simp_all add: eucl_less_def split: split_if_asm)
   524     fix x :: 'a
   525     from reals_Archimedean2[of "Max ((\<lambda>i. x\<bullet>i)`Basis)"]
   526     guess k::nat .. note k = this
   527     { fix i :: 'a assume "i \<in> Basis"
   528       then have "x\<bullet>i < real k"
   529         using k by (subst (asm) Max_less_iff) auto
   530       then have "x\<bullet>i < real k" by simp }
   531     then show "\<exists>k::nat. \<forall>ia\<in>Basis. ia \<noteq> i \<longrightarrow> x \<bullet> ia < real k"
   532       by (auto intro!: exI[of _ k])
   533   qed
   534   finally show "{x. a \<le> x\<bullet>i} \<in> ?SIGMA"
   535     apply (simp only:)
   536     apply (safe intro!: sets.countable_UN sets.Diff)
   537     apply (auto intro: sigma_sets_top )
   538     done
   539 qed auto
   540 
   541 lemma borel_eq_atLeastAtMost:
   542   "borel = sigma UNIV (range (\<lambda>(a,b). {a..b} \<Colon>'a\<Colon>ordered_euclidean_space set))"
   543   (is "_ = ?SIGMA")
   544 proof (rule borel_eq_sigmaI5[OF borel_eq_atMost])
   545   fix a::'a
   546   have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   547   proof (safe, simp_all add: eucl_le[where 'a='a])
   548     fix x :: 'a
   549     from real_arch_simple[of "Max ((\<lambda>i. - x\<bullet>i)`Basis)"]
   550     guess k::nat .. note k = this
   551     { fix i :: 'a assume "i \<in> Basis"
   552       with k have "- x\<bullet>i \<le> real k"
   553         by (subst (asm) Max_le_iff) (auto simp: field_simps)
   554       then have "- real k \<le> x\<bullet>i" by simp }
   555     then show "\<exists>n::nat. \<forall>i\<in>Basis. - real n \<le> x \<bullet> i"
   556       by (auto intro!: exI[of _ k])
   557   qed
   558   show "{..a} \<in> ?SIGMA" unfolding *
   559     by (safe intro!: sets.countable_UN)
   560        (auto intro!: sigma_sets_top)
   561 qed auto
   562 
   563 lemma eucl_lessThan: "{x::real. x <e a} = lessThan a"
   564   by (simp add: eucl_less_def lessThan_def)
   565 
   566 lemma borel_eq_atLeastLessThan:
   567   "borel = sigma UNIV (range (\<lambda>(a, b). {a ..< b :: real}))" (is "_ = ?SIGMA")
   568 proof (rule borel_eq_sigmaI5[OF borel_eq_lessThan])
   569   have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   570   fix x :: real
   571   have "{..<x} = (\<Union>i::nat. {-real i ..< x})"
   572     by (auto simp: move_uminus real_arch_simple)
   573   then show "{y. y <e x} \<in> ?SIGMA"
   574     by (auto intro: sigma_sets.intros simp: eucl_lessThan)
   575 qed auto
   576 
   577 lemma borel_eq_closed: "borel = sigma UNIV (Collect closed)"
   578   unfolding borel_def
   579 proof (intro sigma_eqI sigma_sets_eqI, safe)
   580   fix x :: "'a set" assume "open x"
   581   hence "x = UNIV - (UNIV - x)" by auto
   582   also have "\<dots> \<in> sigma_sets UNIV (Collect closed)"
   583     by (rule sigma_sets.Compl)
   584        (auto intro!: sigma_sets.Basic simp: `open x`)
   585   finally show "x \<in> sigma_sets UNIV (Collect closed)" by simp
   586 next
   587   fix x :: "'a set" assume "closed x"
   588   hence "x = UNIV - (UNIV - x)" by auto
   589   also have "\<dots> \<in> sigma_sets UNIV (Collect open)"
   590     by (rule sigma_sets.Compl)
   591        (auto intro!: sigma_sets.Basic simp: `closed x`)
   592   finally show "x \<in> sigma_sets UNIV (Collect open)" by simp
   593 qed simp_all
   594 
   595 lemma borel_measurable_halfspacesI:
   596   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   597   assumes F: "borel = sigma UNIV (F ` (UNIV \<times> Basis))"
   598   and S_eq: "\<And>a i. S a i = f -` F (a,i) \<inter> space M" 
   599   shows "f \<in> borel_measurable M = (\<forall>i\<in>Basis. \<forall>a::real. S a i \<in> sets M)"
   600 proof safe
   601   fix a :: real and i :: 'b assume i: "i \<in> Basis" and f: "f \<in> borel_measurable M"
   602   then show "S a i \<in> sets M" unfolding assms
   603     by (auto intro!: measurable_sets simp: assms(1))
   604 next
   605   assume a: "\<forall>i\<in>Basis. \<forall>a. S a i \<in> sets M"
   606   then show "f \<in> borel_measurable M"
   607     by (auto intro!: measurable_measure_of simp: S_eq F)
   608 qed
   609 
   610 lemma borel_measurable_iff_halfspace_le:
   611   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   612   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)"
   613   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   614 
   615 lemma borel_measurable_iff_halfspace_less:
   616   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   617   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)"
   618   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   619 
   620 lemma borel_measurable_iff_halfspace_ge:
   621   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   622   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)"
   623   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   624 
   625 lemma borel_measurable_iff_halfspace_greater:
   626   fixes f :: "'a \<Rightarrow> 'c\<Colon>euclidean_space"
   627   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)"
   628   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_greater]) auto
   629 
   630 lemma borel_measurable_iff_le:
   631   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   632   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   633 
   634 lemma borel_measurable_iff_less:
   635   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   636   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   637 
   638 lemma borel_measurable_iff_ge:
   639   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   640   using borel_measurable_iff_halfspace_ge[where 'c=real]
   641   by simp
   642 
   643 lemma borel_measurable_iff_greater:
   644   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   645   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   646 
   647 lemma borel_measurable_euclidean_space:
   648   fixes f :: "'a \<Rightarrow> 'c::euclidean_space"
   649   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M)"
   650 proof safe
   651   assume f: "\<forall>i\<in>Basis. (\<lambda>x. f x \<bullet> i) \<in> borel_measurable M"
   652   then show "f \<in> borel_measurable M"
   653     by (subst borel_measurable_iff_halfspace_le) auto
   654 qed auto
   655 
   656 subsection "Borel measurable operators"
   657 
   658 lemma borel_measurable_norm[measurable]: "norm \<in> borel_measurable borel"
   659   by (intro borel_measurable_continuous_on1 continuous_intros)
   660 
   661 lemma borel_measurable_uminus[measurable (raw)]:
   662   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   663   assumes g: "g \<in> borel_measurable M"
   664   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   665   by (rule borel_measurable_continuous_on[OF _ g]) (intro continuous_intros)
   666 
   667 lemma borel_measurable_add[measurable (raw)]:
   668   fixes f g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   669   assumes f: "f \<in> borel_measurable M"
   670   assumes g: "g \<in> borel_measurable M"
   671   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   672   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   673 
   674 lemma borel_measurable_setsum[measurable (raw)]:
   675   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   676   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   677   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   678 proof cases
   679   assume "finite S"
   680   thus ?thesis using assms by induct auto
   681 qed simp
   682 
   683 lemma borel_measurable_diff[measurable (raw)]:
   684   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   685   assumes f: "f \<in> borel_measurable M"
   686   assumes g: "g \<in> borel_measurable M"
   687   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   688   using borel_measurable_add [of f M "- g"] assms by (simp add: fun_Compl_def)
   689 
   690 lemma borel_measurable_times[measurable (raw)]:
   691   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_algebra}"
   692   assumes f: "f \<in> borel_measurable M"
   693   assumes g: "g \<in> borel_measurable M"
   694   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   695   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   696 
   697 lemma borel_measurable_setprod[measurable (raw)]:
   698   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> 'b::{second_countable_topology, real_normed_field}"
   699   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   700   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   701 proof cases
   702   assume "finite S"
   703   thus ?thesis using assms by induct auto
   704 qed simp
   705 
   706 lemma borel_measurable_dist[measurable (raw)]:
   707   fixes g f :: "'a \<Rightarrow> 'b::{second_countable_topology, metric_space}"
   708   assumes f: "f \<in> borel_measurable M"
   709   assumes g: "g \<in> borel_measurable M"
   710   shows "(\<lambda>x. dist (f x) (g x)) \<in> borel_measurable M"
   711   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   712   
   713 lemma borel_measurable_scaleR[measurable (raw)]:
   714   fixes g :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_vector}"
   715   assumes f: "f \<in> borel_measurable M"
   716   assumes g: "g \<in> borel_measurable M"
   717   shows "(\<lambda>x. f x *\<^sub>R g x) \<in> borel_measurable M"
   718   using f g by (rule borel_measurable_continuous_Pair) (intro continuous_intros)
   719 
   720 lemma affine_borel_measurable_vector:
   721   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   722   assumes "f \<in> borel_measurable M"
   723   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   724 proof (rule borel_measurableI)
   725   fix S :: "'x set" assume "open S"
   726   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   727   proof cases
   728     assume "b \<noteq> 0"
   729     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   730       using open_affinity [of S "inverse b" "- a /\<^sub>R b"]
   731       by (auto simp: algebra_simps)
   732     hence "?S \<in> sets borel" by auto
   733     moreover
   734     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   735       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   736     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   737       by auto
   738   qed simp
   739 qed
   740 
   741 lemma borel_measurable_const_scaleR[measurable (raw)]:
   742   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. b *\<^sub>R f x ::'a::real_normed_vector) \<in> borel_measurable M"
   743   using affine_borel_measurable_vector[of f M 0 b] by simp
   744 
   745 lemma borel_measurable_const_add[measurable (raw)]:
   746   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. a + f x ::'a::real_normed_vector) \<in> borel_measurable M"
   747   using affine_borel_measurable_vector[of f M a 1] by simp
   748 
   749 lemma borel_measurable_inverse[measurable (raw)]:
   750   fixes f :: "'a \<Rightarrow> 'b::{second_countable_topology, real_normed_div_algebra}"
   751   assumes f: "f \<in> borel_measurable M"
   752   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   753 proof -
   754   have "(\<lambda>x::'b. if x \<in> UNIV - {0} then inverse x else inverse 0) \<in> borel_measurable borel"
   755     by (intro borel_measurable_continuous_on_open' continuous_intros) auto
   756   also have "(\<lambda>x::'b. if x \<in> UNIV - {0} then inverse x else inverse 0) = inverse"
   757     by (intro ext) auto
   758   finally show ?thesis using f by simp
   759 qed
   760 
   761 lemma borel_measurable_divide[measurable (raw)]:
   762   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow>
   763     (\<lambda>x. f x / g x::'b::{second_countable_topology, real_normed_field}) \<in> borel_measurable M"
   764   by (simp add: field_divide_inverse)
   765 
   766 lemma borel_measurable_max[measurable (raw)]:
   767   "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"
   768   by (simp add: max_def)
   769 
   770 lemma borel_measurable_min[measurable (raw)]:
   771   "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"
   772   by (simp add: min_def)
   773 
   774 lemma borel_measurable_abs[measurable (raw)]:
   775   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
   776   unfolding abs_real_def by simp
   777 
   778 lemma borel_measurable_nth[measurable (raw)]:
   779   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
   780   by (simp add: cart_eq_inner_axis)
   781 
   782 lemma convex_measurable:
   783   fixes A :: "'a :: ordered_euclidean_space set"
   784   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> A" "open A"
   785   assumes q: "convex_on A q"
   786   shows "(\<lambda>x. q (X x)) \<in> borel_measurable M"
   787 proof -
   788   have "(\<lambda>x. if X x \<in> A then q (X x) else 0) \<in> borel_measurable M" (is "?qX")
   789   proof (rule borel_measurable_continuous_on_open[OF _ _ X(1)])
   790     show "open A" by fact
   791     from this q show "continuous_on A q"
   792       by (rule convex_on_continuous)
   793   qed
   794   also have "?qX \<longleftrightarrow> (\<lambda>x. q (X x)) \<in> borel_measurable M"
   795     using X by (intro measurable_cong) auto
   796   finally show ?thesis .
   797 qed
   798 
   799 lemma borel_measurable_ln[measurable (raw)]:
   800   assumes f: "f \<in> borel_measurable M"
   801   shows "(\<lambda>x. ln (f x)) \<in> borel_measurable M"
   802 proof -
   803   { fix x :: real assume x: "x \<le> 0"
   804     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
   805     from this[of x] x this[of 0] have "ln 0 = ln x"
   806       by (auto simp: ln_def) }
   807   note ln_imp = this
   808   have "(\<lambda>x. if f x \<in> {0<..} then ln (f x) else ln 0) \<in> borel_measurable M"
   809   proof (rule borel_measurable_continuous_on_open[OF _ _ f])
   810     show "continuous_on {0<..} ln"
   811       by (auto intro!: continuous_at_imp_continuous_on DERIV_ln DERIV_isCont)
   812     show "open ({0<..}::real set)" by auto
   813   qed
   814   also have "(\<lambda>x. if x \<in> {0<..} then ln x else ln 0) = ln"
   815     by (simp add: fun_eq_iff not_less ln_imp)
   816   finally show ?thesis .
   817 qed
   818 
   819 lemma borel_measurable_log[measurable (raw)]:
   820   "f \<in> borel_measurable M \<Longrightarrow> g \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. log (g x) (f x)) \<in> borel_measurable M"
   821   unfolding log_def by auto
   822 
   823 lemma borel_measurable_exp[measurable]: "exp \<in> borel_measurable borel"
   824   by (intro borel_measurable_continuous_on1 continuous_at_imp_continuous_on ballI isCont_exp)
   825 
   826 lemma measurable_real_floor[measurable]:
   827   "(floor :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   828 proof -
   829   have "\<And>a x. \<lfloor>x\<rfloor> = a \<longleftrightarrow> (real a \<le> x \<and> x < real (a + 1))"
   830     by (auto intro: floor_eq2)
   831   then show ?thesis
   832     by (auto simp: vimage_def measurable_count_space_eq2_countable)
   833 qed
   834 
   835 lemma measurable_real_natfloor[measurable]:
   836   "(natfloor :: real \<Rightarrow> nat) \<in> measurable borel (count_space UNIV)"
   837   by (simp add: natfloor_def[abs_def])
   838 
   839 lemma measurable_real_ceiling[measurable]:
   840   "(ceiling :: real \<Rightarrow> int) \<in> measurable borel (count_space UNIV)"
   841   unfolding ceiling_def[abs_def] by simp
   842 
   843 lemma borel_measurable_real_floor: "(\<lambda>x::real. real \<lfloor>x\<rfloor>) \<in> borel_measurable borel"
   844   by simp
   845 
   846 lemma borel_measurable_real_natfloor:
   847   "f \<in> borel_measurable M \<Longrightarrow> (\<lambda>x. real (natfloor (f x))) \<in> borel_measurable M"
   848   by simp
   849 
   850 subsection "Borel space on the extended reals"
   851 
   852 lemma borel_measurable_ereal[measurable (raw)]:
   853   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   854   using continuous_on_ereal f by (rule borel_measurable_continuous_on)
   855 
   856 lemma borel_measurable_real_of_ereal[measurable (raw)]:
   857   fixes f :: "'a \<Rightarrow> ereal" 
   858   assumes f: "f \<in> borel_measurable M"
   859   shows "(\<lambda>x. real (f x)) \<in> borel_measurable M"
   860 proof -
   861   have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) \<in> borel_measurable M"
   862     using continuous_on_real
   863     by (rule borel_measurable_continuous_on_open[OF _ _ f]) auto
   864   also have "(\<lambda>x. if f x \<in> UNIV - { \<infinity>, - \<infinity> } then real (f x) else 0) = (\<lambda>x. real (f x))"
   865     by auto
   866   finally show ?thesis .
   867 qed
   868 
   869 lemma borel_measurable_ereal_cases:
   870   fixes f :: "'a \<Rightarrow> ereal" 
   871   assumes f: "f \<in> borel_measurable M"
   872   assumes H: "(\<lambda>x. H (ereal (real (f x)))) \<in> borel_measurable M"
   873   shows "(\<lambda>x. H (f x)) \<in> borel_measurable M"
   874 proof -
   875   let ?F = "\<lambda>x. if f x = \<infinity> then H \<infinity> else if f x = - \<infinity> then H (-\<infinity>) else H (ereal (real (f x)))"
   876   { fix x have "H (f x) = ?F x" by (cases "f x") auto }
   877   with f H show ?thesis by simp
   878 qed
   879 
   880 lemma
   881   fixes f :: "'a \<Rightarrow> ereal" assumes f[measurable]: "f \<in> borel_measurable M"
   882   shows borel_measurable_ereal_abs[measurable(raw)]: "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
   883     and borel_measurable_ereal_inverse[measurable(raw)]: "(\<lambda>x. inverse (f x) :: ereal) \<in> borel_measurable M"
   884     and borel_measurable_uminus_ereal[measurable(raw)]: "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
   885   by (auto simp del: abs_real_of_ereal simp: borel_measurable_ereal_cases[OF f] measurable_If)
   886 
   887 lemma borel_measurable_uminus_eq_ereal[simp]:
   888   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
   889 proof
   890   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
   891 qed auto
   892 
   893 lemma set_Collect_ereal2:
   894   fixes f g :: "'a \<Rightarrow> ereal" 
   895   assumes f: "f \<in> borel_measurable M"
   896   assumes g: "g \<in> borel_measurable M"
   897   assumes H: "{x \<in> space M. H (ereal (real (f x))) (ereal (real (g x)))} \<in> sets M"
   898     "{x \<in> space borel. H (-\<infinity>) (ereal x)} \<in> sets borel"
   899     "{x \<in> space borel. H (\<infinity>) (ereal x)} \<in> sets borel"
   900     "{x \<in> space borel. H (ereal x) (-\<infinity>)} \<in> sets borel"
   901     "{x \<in> space borel. H (ereal x) (\<infinity>)} \<in> sets borel"
   902   shows "{x \<in> space M. H (f x) (g x)} \<in> sets M"
   903 proof -
   904   let ?G = "\<lambda>y x. if g x = \<infinity> then H y \<infinity> else if g x = -\<infinity> then H y (-\<infinity>) else H y (ereal (real (g x)))"
   905   let ?F = "\<lambda>x. if f x = \<infinity> then ?G \<infinity> x else if f x = -\<infinity> then ?G (-\<infinity>) x else ?G (ereal (real (f x))) x"
   906   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
   907   note * = this
   908   from assms show ?thesis
   909     by (subst *) (simp del: space_borel split del: split_if)
   910 qed
   911 
   912 lemma borel_measurable_ereal_iff:
   913   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
   914 proof
   915   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
   916   from borel_measurable_real_of_ereal[OF this]
   917   show "f \<in> borel_measurable M" by auto
   918 qed auto
   919 
   920 lemma borel_measurable_ereal_iff_real:
   921   fixes f :: "'a \<Rightarrow> ereal"
   922   shows "f \<in> borel_measurable M \<longleftrightarrow>
   923     ((\<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)"
   924 proof safe
   925   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"
   926   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
   927   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
   928   let ?f = "\<lambda>x. if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
   929   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
   930   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
   931   finally show "f \<in> borel_measurable M" .
   932 qed simp_all
   933 
   934 lemma borel_measurable_eq_atMost_ereal:
   935   fixes f :: "'a \<Rightarrow> ereal"
   936   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
   937 proof (intro iffI allI)
   938   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
   939   show "f \<in> borel_measurable M"
   940     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
   941   proof (intro conjI allI)
   942     fix a :: real
   943     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
   944       have "x = \<infinity>"
   945       proof (rule ereal_top)
   946         fix B from reals_Archimedean2[of B] guess n ..
   947         then have "ereal B < real n" by auto
   948         with * show "B \<le> x" by (metis less_trans less_imp_le)
   949       qed }
   950     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
   951       by (auto simp: not_le)
   952     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos
   953       by (auto simp del: UN_simps)
   954     moreover
   955     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
   956     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
   957     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
   958       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
   959     moreover have "{w \<in> space M. real (f w) \<le> a} =
   960       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
   961       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")
   962       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
   963     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
   964   qed
   965 qed (simp add: measurable_sets)
   966 
   967 lemma borel_measurable_eq_atLeast_ereal:
   968   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
   969 proof
   970   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
   971   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
   972     by (auto simp: ereal_uminus_le_reorder)
   973   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
   974     unfolding borel_measurable_eq_atMost_ereal by auto
   975   then show "f \<in> borel_measurable M" by simp
   976 qed (simp add: measurable_sets)
   977 
   978 lemma greater_eq_le_measurable:
   979   fixes f :: "'a \<Rightarrow> 'c::linorder"
   980   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
   981 proof
   982   assume "f -` {a ..} \<inter> space M \<in> sets M"
   983   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
   984   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
   985 next
   986   assume "f -` {..< a} \<inter> space M \<in> sets M"
   987   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
   988   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
   989 qed
   990 
   991 lemma borel_measurable_ereal_iff_less:
   992   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
   993   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
   994 
   995 lemma less_eq_ge_measurable:
   996   fixes f :: "'a \<Rightarrow> 'c::linorder"
   997   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
   998 proof
   999   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1000   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1001   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1002 next
  1003   assume "f -` {..a} \<inter> space M \<in> sets M"
  1004   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1005   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1006 qed
  1007 
  1008 lemma borel_measurable_ereal_iff_ge:
  1009   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1010   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  1011 
  1012 lemma borel_measurable_ereal2:
  1013   fixes f g :: "'a \<Rightarrow> ereal" 
  1014   assumes f: "f \<in> borel_measurable M"
  1015   assumes g: "g \<in> borel_measurable M"
  1016   assumes H: "(\<lambda>x. H (ereal (real (f x))) (ereal (real (g x)))) \<in> borel_measurable M"
  1017     "(\<lambda>x. H (-\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1018     "(\<lambda>x. H (\<infinity>) (ereal (real (g x)))) \<in> borel_measurable M"
  1019     "(\<lambda>x. H (ereal (real (f x))) (-\<infinity>)) \<in> borel_measurable M"
  1020     "(\<lambda>x. H (ereal (real (f x))) (\<infinity>)) \<in> borel_measurable M"
  1021   shows "(\<lambda>x. H (f x) (g x)) \<in> borel_measurable M"
  1022 proof -
  1023   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)))"
  1024   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"
  1025   { fix x have "H (f x) (g x) = ?F x" by (cases "f x" "g x" rule: ereal2_cases) auto }
  1026   note * = this
  1027   from assms show ?thesis unfolding * by simp
  1028 qed
  1029 
  1030 lemma
  1031   fixes f :: "'a \<Rightarrow> ereal" assumes f: "f \<in> borel_measurable M"
  1032   shows borel_measurable_ereal_eq_const: "{x\<in>space M. f x = c} \<in> sets M"
  1033     and borel_measurable_ereal_neq_const: "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1034   using f by auto
  1035 
  1036 lemma [measurable(raw)]:
  1037   fixes f :: "'a \<Rightarrow> ereal"
  1038   assumes [measurable]: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1039   shows borel_measurable_ereal_add: "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1040     and borel_measurable_ereal_times: "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1041     and borel_measurable_ereal_min: "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1042     and borel_measurable_ereal_max: "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1043   by (simp_all add: borel_measurable_ereal2 min_def max_def)
  1044 
  1045 lemma [measurable(raw)]:
  1046   fixes f g :: "'a \<Rightarrow> ereal"
  1047   assumes "f \<in> borel_measurable M"
  1048   assumes "g \<in> borel_measurable M"
  1049   shows borel_measurable_ereal_diff: "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1050     and borel_measurable_ereal_divide: "(\<lambda>x. f x / g x) \<in> borel_measurable M"
  1051   using assms by (simp_all add: minus_ereal_def divide_ereal_def)
  1052 
  1053 lemma borel_measurable_ereal_setsum[measurable (raw)]:
  1054   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1055   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1056   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1057 proof cases
  1058   assume "finite S"
  1059   thus ?thesis using assms
  1060     by induct auto
  1061 qed simp
  1062 
  1063 lemma borel_measurable_ereal_setprod[measurable (raw)]:
  1064   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1065   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1066   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1067 proof cases
  1068   assume "finite S"
  1069   thus ?thesis using assms by induct auto
  1070 qed simp
  1071 
  1072 lemma borel_measurable_SUP[measurable (raw)]:
  1073   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1074   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1075   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1076   unfolding borel_measurable_ereal_iff_ge
  1077 proof
  1078   fix a
  1079   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1080     by (auto simp: less_SUP_iff)
  1081   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1082     using assms by auto
  1083 qed
  1084 
  1085 lemma borel_measurable_INF[measurable (raw)]:
  1086   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1087   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1088   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1089   unfolding borel_measurable_ereal_iff_less
  1090 proof
  1091   fix a
  1092   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1093     by (auto simp: INF_less_iff)
  1094   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1095     using assms by auto
  1096 qed
  1097 
  1098 lemma [measurable (raw)]:
  1099   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1100   assumes "\<And>i. f i \<in> borel_measurable M"
  1101   shows borel_measurable_liminf: "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1102     and borel_measurable_limsup: "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1103   unfolding liminf_SUP_INF limsup_INF_SUP using assms by auto
  1104 
  1105 lemma sets_Collect_eventually_sequentially[measurable]:
  1106   "(\<And>i. {x\<in>space M. P x i} \<in> sets M) \<Longrightarrow> {x\<in>space M. eventually (P x) sequentially} \<in> sets M"
  1107   unfolding eventually_sequentially by simp
  1108 
  1109 lemma sets_Collect_ereal_convergent[measurable]: 
  1110   fixes f :: "nat \<Rightarrow> 'a => ereal"
  1111   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1112   shows "{x\<in>space M. convergent (\<lambda>i. f i x)} \<in> sets M"
  1113   unfolding convergent_ereal by auto
  1114 
  1115 lemma borel_measurable_extreal_lim[measurable (raw)]:
  1116   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1117   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1118   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1119 proof -
  1120   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))"
  1121     by (simp add: lim_def convergent_def convergent_limsup_cl)
  1122   then show ?thesis
  1123     by simp
  1124 qed
  1125 
  1126 lemma borel_measurable_ereal_LIMSEQ:
  1127   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1128   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1129   and u: "\<And>i. u i \<in> borel_measurable M"
  1130   shows "u' \<in> borel_measurable M"
  1131 proof -
  1132   have "\<And>x. x \<in> space M \<Longrightarrow> u' x = liminf (\<lambda>n. u n x)"
  1133     using u' by (simp add: lim_imp_Liminf[symmetric])
  1134   with u show ?thesis by (simp cong: measurable_cong)
  1135 qed
  1136 
  1137 lemma borel_measurable_extreal_suminf[measurable (raw)]:
  1138   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1139   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1140   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1141   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1142 
  1143 subsection {* LIMSEQ is borel measurable *}
  1144 
  1145 lemma borel_measurable_LIMSEQ:
  1146   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1147   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1148   and u: "\<And>i. u i \<in> borel_measurable M"
  1149   shows "u' \<in> borel_measurable M"
  1150 proof -
  1151   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1152     using u' by (simp add: lim_imp_Liminf)
  1153   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1154     by auto
  1155   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1156 qed
  1157 
  1158 lemma borel_measurable_LIMSEQ_metric:
  1159   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> 'b :: metric_space"
  1160   assumes [measurable]: "\<And>i. f i \<in> borel_measurable M"
  1161   assumes lim: "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. f i x) ----> g x"
  1162   shows "g \<in> borel_measurable M"
  1163   unfolding borel_eq_closed
  1164 proof (safe intro!: measurable_measure_of)
  1165   fix A :: "'b set" assume "closed A" 
  1166 
  1167   have [measurable]: "(\<lambda>x. infdist (g x) A) \<in> borel_measurable M"
  1168   proof (rule borel_measurable_LIMSEQ)
  1169     show "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. infdist (f i x) A) ----> infdist (g x) A"
  1170       by (intro tendsto_infdist lim)
  1171     show "\<And>i. (\<lambda>x. infdist (f i x) A) \<in> borel_measurable M"
  1172       by (intro borel_measurable_continuous_on[where f="\<lambda>x. infdist x A"]
  1173         continuous_at_imp_continuous_on ballI continuous_infdist isCont_ident) auto
  1174   qed
  1175 
  1176   show "g -` A \<inter> space M \<in> sets M"
  1177   proof cases
  1178     assume "A \<noteq> {}"
  1179     then have "\<And>x. infdist x A = 0 \<longleftrightarrow> x \<in> A"
  1180       using `closed A` by (simp add: in_closed_iff_infdist_zero)
  1181     then have "g -` A \<inter> space M = {x\<in>space M. infdist (g x) A = 0}"
  1182       by auto
  1183     also have "\<dots> \<in> sets M"
  1184       by measurable
  1185     finally show ?thesis .
  1186   qed simp
  1187 qed auto
  1188 
  1189 lemma sets_Collect_Cauchy[measurable]: 
  1190   fixes f :: "nat \<Rightarrow> 'a => real"
  1191   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1192   shows "{x\<in>space M. Cauchy (\<lambda>i. f i x)} \<in> sets M"
  1193   unfolding Cauchy_iff2 using f by auto
  1194 
  1195 lemma borel_measurable_lim[measurable (raw)]:
  1196   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1197   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1198   shows "(\<lambda>x. lim (\<lambda>i. f i x)) \<in> borel_measurable M"
  1199 proof -
  1200   def u' \<equiv> "\<lambda>x. lim (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1201   then have *: "\<And>x. lim (\<lambda>i. f i x) = (if Cauchy (\<lambda>i. f i x) then u' x else (THE x. False))"
  1202     by (auto simp: lim_def convergent_eq_cauchy[symmetric])
  1203   have "u' \<in> borel_measurable M"
  1204   proof (rule borel_measurable_LIMSEQ)
  1205     fix x
  1206     have "convergent (\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0)"
  1207       by (cases "Cauchy (\<lambda>i. f i x)")
  1208          (auto simp add: convergent_eq_cauchy[symmetric] convergent_def)
  1209     then show "(\<lambda>i. if Cauchy (\<lambda>i. f i x) then f i x else 0) ----> u' x"
  1210       unfolding u'_def 
  1211       by (rule convergent_LIMSEQ_iff[THEN iffD1])
  1212   qed measurable
  1213   then show ?thesis
  1214     unfolding * by measurable
  1215 qed
  1216 
  1217 lemma borel_measurable_suminf[measurable (raw)]:
  1218   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1219   assumes f[measurable]: "\<And>i. f i \<in> borel_measurable M"
  1220   shows "(\<lambda>x. suminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1221   unfolding suminf_def sums_def[abs_def] lim_def[symmetric] by simp
  1222 
  1223 no_notation
  1224   eucl_less (infix "<e" 50)
  1225 
  1226 end