src/HOL/Probability/Borel_Space.thy
author huffman
Fri Aug 26 15:00:00 2011 -0700 (2011-08-26)
changeset 44537 c10485a6a7af
parent 44282 f0de18b62d63
child 44666 8670a39d4420
permissions -rw-r--r--
make HOL-Probability respect set/pred distinction
     1 (*  Title:      HOL/Probability/Borel_Space.thy
     2     Author:     Johannes Hölzl, TU München
     3     Author:     Armin Heller, TU München
     4 *)
     5 
     6 header {*Borel spaces*}
     7 
     8 theory Borel_Space
     9   imports Sigma_Algebra Multivariate_Analysis
    10 begin
    11 
    12 section "Generic Borel spaces"
    13 
    14 definition "borel = sigma \<lparr> space = UNIV::'a::topological_space set, sets = {S. open S}\<rparr>"
    15 abbreviation "borel_measurable M \<equiv> measurable M borel"
    16 
    17 interpretation borel: sigma_algebra borel
    18   by (auto simp: borel_def intro!: sigma_algebra_sigma)
    19 
    20 lemma in_borel_measurable:
    21    "f \<in> borel_measurable M \<longleftrightarrow>
    22     (\<forall>S \<in> sets (sigma \<lparr> space = UNIV, sets = {S. open S}\<rparr>).
    23       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 borel_open[simp]:
    36   assumes "open A" shows "A \<in> sets borel"
    37 proof -
    38   have "A \<in> {S. open S}" unfolding mem_Collect_eq using assms .
    39   thus ?thesis unfolding borel_def sigma_def by (auto intro!: sigma_sets.Basic)
    40 qed
    41 
    42 lemma borel_closed[simp]:
    43   assumes "closed A" shows "A \<in> sets borel"
    44 proof -
    45   have "space borel - (- A) \<in> sets borel"
    46     using assms unfolding closed_def by (blast intro: borel_open)
    47   thus ?thesis by simp
    48 qed
    49 
    50 lemma borel_comp[intro,simp]: "A \<in> sets borel \<Longrightarrow> - A \<in> sets borel"
    51   unfolding Compl_eq_Diff_UNIV by (intro borel.Diff) auto
    52 
    53 lemma (in sigma_algebra) borel_measurable_vimage:
    54   fixes f :: "'a \<Rightarrow> 'x::t2_space"
    55   assumes borel: "f \<in> borel_measurable M"
    56   shows "f -` {x} \<inter> space M \<in> sets M"
    57 proof (cases "x \<in> f ` space M")
    58   case True then obtain y where "x = f y" by auto
    59   from closed_singleton[of "f y"]
    60   have "{f y} \<in> sets borel" by (rule borel_closed)
    61   with assms show ?thesis
    62     unfolding in_borel_measurable_borel `x = f y` by auto
    63 next
    64   case False hence "f -` {x} \<inter> space M = {}" by auto
    65   thus ?thesis by auto
    66 qed
    67 
    68 lemma (in sigma_algebra) borel_measurableI:
    69   fixes f :: "'a \<Rightarrow> 'x\<Colon>topological_space"
    70   assumes "\<And>S. open S \<Longrightarrow> f -` S \<inter> space M \<in> sets M"
    71   shows "f \<in> borel_measurable M"
    72   unfolding borel_def
    73 proof (rule measurable_sigma, simp_all)
    74   fix S :: "'x set" assume "open S" thus "f -` S \<inter> space M \<in> sets M"
    75     using assms[of S] by simp
    76 qed
    77 
    78 lemma borel_singleton[simp, intro]:
    79   fixes x :: "'a::t1_space"
    80   shows "A \<in> sets borel \<Longrightarrow> insert x A \<in> sets borel"
    81   proof (rule borel.insert_in_sets)
    82     show "{x} \<in> sets borel"
    83       using closed_singleton[of x] by (rule borel_closed)
    84   qed simp
    85 
    86 lemma (in sigma_algebra) borel_measurable_const[simp, intro]:
    87   "(\<lambda>x. c) \<in> borel_measurable M"
    88   by (auto intro!: measurable_const)
    89 
    90 lemma (in sigma_algebra) borel_measurable_indicator[simp, intro!]:
    91   assumes A: "A \<in> sets M"
    92   shows "indicator A \<in> borel_measurable M"
    93   unfolding indicator_def_raw using A
    94   by (auto intro!: measurable_If_set borel_measurable_const)
    95 
    96 lemma (in sigma_algebra) borel_measurable_indicator_iff:
    97   "(indicator A :: 'a \<Rightarrow> 'x::{t1_space, zero_neq_one}) \<in> borel_measurable M \<longleftrightarrow> A \<inter> space M \<in> sets M"
    98     (is "?I \<in> borel_measurable M \<longleftrightarrow> _")
    99 proof
   100   assume "?I \<in> borel_measurable M"
   101   then have "?I -` {1} \<inter> space M \<in> sets M"
   102     unfolding measurable_def by auto
   103   also have "?I -` {1} \<inter> space M = A \<inter> space M"
   104     unfolding indicator_def_raw by auto
   105   finally show "A \<inter> space M \<in> sets M" .
   106 next
   107   assume "A \<inter> space M \<in> sets M"
   108   moreover have "?I \<in> borel_measurable M \<longleftrightarrow>
   109     (indicator (A \<inter> space M) :: 'a \<Rightarrow> 'x) \<in> borel_measurable M"
   110     by (intro measurable_cong) (auto simp: indicator_def)
   111   ultimately show "?I \<in> borel_measurable M" by auto
   112 qed
   113 
   114 lemma (in sigma_algebra) borel_measurable_restricted:
   115   fixes f :: "'a \<Rightarrow> ereal" assumes "A \<in> sets M"
   116   shows "f \<in> borel_measurable (restricted_space A) \<longleftrightarrow>
   117     (\<lambda>x. f x * indicator A x) \<in> borel_measurable M"
   118     (is "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable M")
   119 proof -
   120   interpret R: sigma_algebra ?R by (rule restricted_sigma_algebra[OF `A \<in> sets M`])
   121   have *: "f \<in> borel_measurable ?R \<longleftrightarrow> ?f \<in> borel_measurable ?R"
   122     by (auto intro!: measurable_cong)
   123   show ?thesis unfolding *
   124     unfolding in_borel_measurable_borel
   125   proof (simp, safe)
   126     fix S :: "ereal set" assume "S \<in> sets borel"
   127       "\<forall>S\<in>sets borel. ?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   128     then have "?f -` S \<inter> A \<in> op \<inter> A ` sets M" by auto
   129     then have f: "?f -` S \<inter> A \<in> sets M"
   130       using `A \<in> sets M` sets_into_space by fastsimp
   131     show "?f -` S \<inter> space M \<in> sets M"
   132     proof cases
   133       assume "0 \<in> S"
   134       then have "?f -` S \<inter> space M = ?f -` S \<inter> A \<union> (space M - A)"
   135         using `A \<in> sets M` sets_into_space by auto
   136       then show ?thesis using f `A \<in> sets M` by (auto intro!: Un Diff)
   137     next
   138       assume "0 \<notin> S"
   139       then have "?f -` S \<inter> space M = ?f -` S \<inter> A"
   140         using `A \<in> sets M` sets_into_space
   141         by (auto simp: indicator_def split: split_if_asm)
   142       then show ?thesis using f by auto
   143     qed
   144   next
   145     fix S :: "ereal set" assume "S \<in> sets borel"
   146       "\<forall>S\<in>sets borel. ?f -` S \<inter> space M \<in> sets M"
   147     then have f: "?f -` S \<inter> space M \<in> sets M" by auto
   148     then show "?f -` S \<inter> A \<in> op \<inter> A ` sets M"
   149       using `A \<in> sets M` sets_into_space
   150       apply (simp add: image_iff)
   151       apply (rule bexI[OF _ f])
   152       by auto
   153   qed
   154 qed
   155 
   156 lemma (in sigma_algebra) borel_measurable_subalgebra:
   157   assumes "sets N \<subseteq> sets M" "space N = space M" "f \<in> borel_measurable N"
   158   shows "f \<in> borel_measurable M"
   159   using assms unfolding measurable_def by auto
   160 
   161 section "Borel spaces on euclidean spaces"
   162 
   163 lemma lessThan_borel[simp, intro]:
   164   fixes a :: "'a\<Colon>ordered_euclidean_space"
   165   shows "{..< a} \<in> sets borel"
   166   by (blast intro: borel_open)
   167 
   168 lemma greaterThan_borel[simp, intro]:
   169   fixes a :: "'a\<Colon>ordered_euclidean_space"
   170   shows "{a <..} \<in> sets borel"
   171   by (blast intro: borel_open)
   172 
   173 lemma greaterThanLessThan_borel[simp, intro]:
   174   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   175   shows "{a<..<b} \<in> sets borel"
   176   by (blast intro: borel_open)
   177 
   178 lemma atMost_borel[simp, intro]:
   179   fixes a :: "'a\<Colon>ordered_euclidean_space"
   180   shows "{..a} \<in> sets borel"
   181   by (blast intro: borel_closed)
   182 
   183 lemma atLeast_borel[simp, intro]:
   184   fixes a :: "'a\<Colon>ordered_euclidean_space"
   185   shows "{a..} \<in> sets borel"
   186   by (blast intro: borel_closed)
   187 
   188 lemma atLeastAtMost_borel[simp, intro]:
   189   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   190   shows "{a..b} \<in> sets borel"
   191   by (blast intro: borel_closed)
   192 
   193 lemma greaterThanAtMost_borel[simp, intro]:
   194   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   195   shows "{a<..b} \<in> sets borel"
   196   unfolding greaterThanAtMost_def by blast
   197 
   198 lemma atLeastLessThan_borel[simp, intro]:
   199   fixes a b :: "'a\<Colon>ordered_euclidean_space"
   200   shows "{a..<b} \<in> sets borel"
   201   unfolding atLeastLessThan_def by blast
   202 
   203 lemma hafspace_less_borel[simp, intro]:
   204   fixes a :: real
   205   shows "{x::'a::euclidean_space. a < x $$ i} \<in> sets borel"
   206   by (auto intro!: borel_open open_halfspace_component_gt)
   207 
   208 lemma hafspace_greater_borel[simp, intro]:
   209   fixes a :: real
   210   shows "{x::'a::euclidean_space. x $$ i < a} \<in> sets borel"
   211   by (auto intro!: borel_open open_halfspace_component_lt)
   212 
   213 lemma hafspace_less_eq_borel[simp, intro]:
   214   fixes a :: real
   215   shows "{x::'a::euclidean_space. a \<le> x $$ i} \<in> sets borel"
   216   by (auto intro!: borel_closed closed_halfspace_component_ge)
   217 
   218 lemma hafspace_greater_eq_borel[simp, intro]:
   219   fixes a :: real
   220   shows "{x::'a::euclidean_space. x $$ i \<le> a} \<in> sets borel"
   221   by (auto intro!: borel_closed closed_halfspace_component_le)
   222 
   223 lemma (in sigma_algebra) borel_measurable_less[simp, intro]:
   224   fixes f :: "'a \<Rightarrow> real"
   225   assumes f: "f \<in> borel_measurable M"
   226   assumes g: "g \<in> borel_measurable M"
   227   shows "{w \<in> space M. f w < g w} \<in> sets M"
   228 proof -
   229   have "{w \<in> space M. f w < g w} =
   230         (\<Union>r. (f -` {..< of_rat r} \<inter> space M) \<inter> (g -` {of_rat r <..} \<inter> space M))"
   231     using Rats_dense_in_real by (auto simp add: Rats_def)
   232   then show ?thesis using f g
   233     by simp (blast intro: measurable_sets)
   234 qed
   235 
   236 lemma (in sigma_algebra) borel_measurable_le[simp, intro]:
   237   fixes f :: "'a \<Rightarrow> real"
   238   assumes f: "f \<in> borel_measurable M"
   239   assumes g: "g \<in> borel_measurable M"
   240   shows "{w \<in> space M. f w \<le> g w} \<in> sets M"
   241 proof -
   242   have "{w \<in> space M. f w \<le> g w} = space M - {w \<in> space M. g w < f w}"
   243     by auto
   244   thus ?thesis using f g
   245     by simp blast
   246 qed
   247 
   248 lemma (in sigma_algebra) borel_measurable_eq[simp, intro]:
   249   fixes f :: "'a \<Rightarrow> real"
   250   assumes f: "f \<in> borel_measurable M"
   251   assumes g: "g \<in> borel_measurable M"
   252   shows "{w \<in> space M. f w = g w} \<in> sets M"
   253 proof -
   254   have "{w \<in> space M. f w = g w} =
   255         {w \<in> space M. f w \<le> g w} \<inter> {w \<in> space M. g w \<le> f w}"
   256     by auto
   257   thus ?thesis using f g by auto
   258 qed
   259 
   260 lemma (in sigma_algebra) borel_measurable_neq[simp, intro]:
   261   fixes f :: "'a \<Rightarrow> real"
   262   assumes f: "f \<in> borel_measurable M"
   263   assumes g: "g \<in> borel_measurable M"
   264   shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
   265 proof -
   266   have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}"
   267     by auto
   268   thus ?thesis using f g by auto
   269 qed
   270 
   271 subsection "Borel space equals sigma algebras over intervals"
   272 
   273 lemma rational_boxes:
   274   fixes x :: "'a\<Colon>ordered_euclidean_space"
   275   assumes "0 < e"
   276   shows "\<exists>a b. (\<forall>i. a $$ i \<in> \<rat>) \<and> (\<forall>i. b $$ i \<in> \<rat>) \<and> x \<in> {a <..< b} \<and> {a <..< b} \<subseteq> ball x e"
   277 proof -
   278   def e' \<equiv> "e / (2 * sqrt (real (DIM ('a))))"
   279   then have e: "0 < e'" using assms by (auto intro!: divide_pos_pos)
   280   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> y < x $$ i \<and> x $$ i - y < e'" (is "\<forall>i. ?th i")
   281   proof
   282     fix i from Rats_dense_in_real[of "x $$ i - e'" "x $$ i"] e
   283     show "?th i" by auto
   284   qed
   285   from choice[OF this] guess a .. note a = this
   286   have "\<forall>i. \<exists>y. y \<in> \<rat> \<and> x $$ i < y \<and> y - x $$ i < e'" (is "\<forall>i. ?th i")
   287   proof
   288     fix i from Rats_dense_in_real[of "x $$ i" "x $$ i + e'"] e
   289     show "?th i" by auto
   290   qed
   291   from choice[OF this] guess b .. note b = this
   292   { fix y :: 'a assume *: "Chi a < y" "y < Chi b"
   293     have "dist x y = sqrt (\<Sum>i<DIM('a). (dist (x $$ i) (y $$ i))\<twosuperior>)"
   294       unfolding setL2_def[symmetric] by (rule euclidean_dist_l2)
   295     also have "\<dots> < sqrt (\<Sum>i<DIM('a). e^2 / real (DIM('a)))"
   296     proof (rule real_sqrt_less_mono, rule setsum_strict_mono)
   297       fix i assume i: "i \<in> {..<DIM('a)}"
   298       have "a i < y$$i \<and> y$$i < b i" using * i eucl_less[where 'a='a] by auto
   299       moreover have "a i < x$$i" "x$$i - a i < e'" using a by auto
   300       moreover have "x$$i < b i" "b i - x$$i < e'" using b by auto
   301       ultimately have "\<bar>x$$i - y$$i\<bar> < 2 * e'" by auto
   302       then have "dist (x $$ i) (y $$ i) < e/sqrt (real (DIM('a)))"
   303         unfolding e'_def by (auto simp: dist_real_def)
   304       then have "(dist (x $$ i) (y $$ i))\<twosuperior> < (e/sqrt (real (DIM('a))))\<twosuperior>"
   305         by (rule power_strict_mono) auto
   306       then show "(dist (x $$ i) (y $$ i))\<twosuperior> < e\<twosuperior> / real DIM('a)"
   307         by (simp add: power_divide)
   308     qed auto
   309     also have "\<dots> = e" using `0 < e` by (simp add: real_eq_of_nat DIM_positive)
   310     finally have "dist x y < e" . }
   311   with a b show ?thesis
   312     apply (rule_tac exI[of _ "Chi a"])
   313     apply (rule_tac exI[of _ "Chi b"])
   314     using eucl_less[where 'a='a] by auto
   315 qed
   316 
   317 lemma ex_rat_list:
   318   fixes x :: "'a\<Colon>ordered_euclidean_space"
   319   assumes "\<And> i. x $$ i \<in> \<rat>"
   320   shows "\<exists> r. length r = DIM('a) \<and> (\<forall> i < DIM('a). of_rat (r ! i) = x $$ i)"
   321 proof -
   322   have "\<forall>i. \<exists>r. x $$ i = of_rat r" using assms unfolding Rats_def by blast
   323   from choice[OF this] guess r ..
   324   then show ?thesis by (auto intro!: exI[of _ "map r [0 ..< DIM('a)]"])
   325 qed
   326 
   327 lemma open_UNION:
   328   fixes M :: "'a\<Colon>ordered_euclidean_space set"
   329   assumes "open M"
   330   shows "M = UNION {(a, b) | a b. {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)} \<subseteq> M}
   331                    (\<lambda> (a, b). {Chi (of_rat \<circ> op ! a) <..< Chi (of_rat \<circ> op ! b)})"
   332     (is "M = UNION ?idx ?box")
   333 proof safe
   334   fix x assume "x \<in> M"
   335   obtain e where e: "e > 0" "ball x e \<subseteq> M"
   336     using openE[OF assms `x \<in> M`] by auto
   337   then obtain a b where ab: "x \<in> {a <..< b}" "\<And>i. a $$ i \<in> \<rat>" "\<And>i. b $$ i \<in> \<rat>" "{a <..< b} \<subseteq> ball x e"
   338     using rational_boxes[OF e(1)] by blast
   339   then obtain p q where pq: "length p = DIM ('a)"
   340                             "length q = DIM ('a)"
   341                             "\<forall> i < DIM ('a). of_rat (p ! i) = a $$ i \<and> of_rat (q ! i) = b $$ i"
   342     using ex_rat_list[OF ab(2)] ex_rat_list[OF ab(3)] by blast
   343   hence p: "Chi (of_rat \<circ> op ! p) = a"
   344     using euclidean_eq[of "Chi (of_rat \<circ> op ! p)" a]
   345     unfolding o_def by auto
   346   from pq have q: "Chi (of_rat \<circ> op ! q) = b"
   347     using euclidean_eq[of "Chi (of_rat \<circ> op ! q)" b]
   348     unfolding o_def by auto
   349   have "x \<in> ?box (p, q)"
   350     using p q ab by auto
   351   thus "x \<in> UNION ?idx ?box" using ab e p q exI[of _ p] exI[of _ q] by auto
   352 qed auto
   353 
   354 lemma halfspace_span_open:
   355   "sigma_sets UNIV (range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a}))
   356     \<subseteq> sets borel"
   357   by (auto intro!: borel.sigma_sets_subset[simplified] borel_open
   358                    open_halfspace_component_lt)
   359 
   360 lemma halfspace_lt_in_halfspace:
   361   "{x\<Colon>'a. x $$ i < a} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   362   by (auto intro!: sigma_sets.Basic simp: sets_sigma)
   363 
   364 lemma halfspace_gt_in_halfspace:
   365   "{x\<Colon>'a. a < x $$ i} \<in> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>)"
   366   (is "?set \<in> sets ?SIGMA")
   367 proof -
   368   interpret sigma_algebra "?SIGMA"
   369     by (intro sigma_algebra_sigma_sets) (simp_all add: sets_sigma)
   370   have *: "?set = (\<Union>n. space ?SIGMA - {x\<Colon>'a. x $$ i < a + 1 / real (Suc n)})"
   371   proof (safe, simp_all add: not_less)
   372     fix x assume "a < x $$ i"
   373     with reals_Archimedean[of "x $$ i - a"]
   374     obtain n where "a + 1 / real (Suc n) < x $$ i"
   375       by (auto simp: inverse_eq_divide field_simps)
   376     then show "\<exists>n. a + 1 / real (Suc n) \<le> x $$ i"
   377       by (blast intro: less_imp_le)
   378   next
   379     fix x n
   380     have "a < a + 1 / real (Suc n)" by auto
   381     also assume "\<dots> \<le> x"
   382     finally show "a < x" .
   383   qed
   384   show "?set \<in> sets ?SIGMA" unfolding *
   385     by (safe intro!: countable_UN Diff halfspace_lt_in_halfspace)
   386 qed
   387 
   388 lemma open_span_halfspace:
   389   "sets borel \<subseteq> sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x $$ i < a})\<rparr>)"
   390     (is "_ \<subseteq> sets ?SIGMA")
   391 proof -
   392   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) simp
   393   then interpret sigma_algebra ?SIGMA .
   394   { fix S :: "'a set" assume "S \<in> {S. open S}"
   395     then have "open S" unfolding mem_Collect_eq .
   396     from open_UNION[OF this]
   397     obtain I where *: "S =
   398       (\<Union>(a, b)\<in>I.
   399           (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   400           (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   401       unfolding greaterThanLessThan_def
   402       unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   403       unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   404       by blast
   405     have "S \<in> sets ?SIGMA"
   406       unfolding *
   407       by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace) }
   408   then show ?thesis unfolding borel_def
   409     by (intro sets_sigma_subset) auto
   410 qed
   411 
   412 lemma halfspace_span_halfspace_le:
   413   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   414    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. x $$ i \<le> a})\<rparr>)"
   415   (is "_ \<subseteq> sets ?SIGMA")
   416 proof -
   417   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   418   then interpret sigma_algebra ?SIGMA .
   419   { fix a i
   420     have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   421     proof (safe, simp_all)
   422       fix x::'a assume *: "x$$i < a"
   423       with reals_Archimedean[of "a - x$$i"]
   424       obtain n where "x $$ i < a - 1 / (real (Suc n))"
   425         by (auto simp: field_simps inverse_eq_divide)
   426       then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   427         by (blast intro: less_imp_le)
   428     next
   429       fix x::'a and n
   430       assume "x$$i \<le> a - 1 / real (Suc n)"
   431       also have "\<dots> < a" by auto
   432       finally show "x$$i < a" .
   433     qed
   434     have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   435       by (safe intro!: countable_UN)
   436          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   437   then show ?thesis by (intro sets_sigma_subset) auto
   438 qed
   439 
   440 lemma halfspace_span_halfspace_ge:
   441   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   442    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a \<le> x $$ i})\<rparr>)"
   443   (is "_ \<subseteq> sets ?SIGMA")
   444 proof -
   445   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   446   then interpret sigma_algebra ?SIGMA .
   447   { fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   448     have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   449       by (safe intro!: Diff)
   450          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   451   then show ?thesis by (intro sets_sigma_subset) auto
   452 qed
   453 
   454 lemma halfspace_le_span_halfspace_gt:
   455   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   456    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a < x $$ i})\<rparr>)"
   457   (is "_ \<subseteq> sets ?SIGMA")
   458 proof -
   459   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   460   then interpret sigma_algebra ?SIGMA .
   461   { fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   462     have "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   463       by (safe intro!: Diff)
   464          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   465   then show ?thesis by (intro sets_sigma_subset) auto
   466 qed
   467 
   468 lemma halfspace_le_span_atMost:
   469   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   470    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>)"
   471   (is "_ \<subseteq> sets ?SIGMA")
   472 proof -
   473   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   474   then interpret sigma_algebra ?SIGMA .
   475   have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   476   proof cases
   477     fix a i assume "i < DIM('a)"
   478     then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
   479     proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   480       fix x
   481       from real_arch_simple[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"] guess k::nat ..
   482       then have "\<And>i. i < DIM('a) \<Longrightarrow> x$$i \<le> real k"
   483         by (subst (asm) Max_le_iff) auto
   484       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
   485         by (auto intro!: exI[of _ k])
   486     qed
   487     show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   488       by (safe intro!: countable_UN)
   489          (auto simp: sets_sigma intro!: sigma_sets.Basic)
   490   next
   491     fix a i assume "\<not> i < DIM('a)"
   492     then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   493       using top by auto
   494   qed
   495   then show ?thesis by (intro sets_sigma_subset) auto
   496 qed
   497 
   498 lemma halfspace_le_span_greaterThan:
   499   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   500    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {a<..})\<rparr>)"
   501   (is "_ \<subseteq> sets ?SIGMA")
   502 proof -
   503   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   504   then interpret sigma_algebra ?SIGMA .
   505   have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   506   proof cases
   507     fix a i assume "i < DIM('a)"
   508     have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   509     also have *: "{x::'a. a < x$$i} = (\<Union>k::nat. {(\<chi>\<chi> n. if n = i then a else -real k) <..})" using `i <DIM('a)`
   510     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   511       fix x
   512       from real_arch_lt[of "Max ((\<lambda>i. -x$$i)`{..<DIM('a)})"]
   513       guess k::nat .. note k = this
   514       { fix i assume "i < DIM('a)"
   515         then have "-x$$i < real k"
   516           using k by (subst (asm) Max_less_iff) auto
   517         then have "- real k < x$$i" by simp }
   518       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
   519         by (auto intro!: exI[of _ k])
   520     qed
   521     finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   522       apply (simp only:)
   523       apply (safe intro!: countable_UN Diff)
   524       by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   525   next
   526     fix a i assume "\<not> i < DIM('a)"
   527     then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   528       using top by auto
   529   qed
   530   then show ?thesis by (intro sets_sigma_subset) auto
   531 qed
   532 
   533 lemma halfspace_le_span_lessThan:
   534   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i})\<rparr>) \<subseteq>
   535    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..<a})\<rparr>)"
   536   (is "_ \<subseteq> sets ?SIGMA")
   537 proof -
   538   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   539   then interpret sigma_algebra ?SIGMA .
   540   have "\<And>a i. {x. a \<le> x$$i} \<in> sets ?SIGMA"
   541   proof cases
   542     fix a i assume "i < DIM('a)"
   543     have "{x::'a. a \<le> x$$i} = space ?SIGMA - {x::'a. x$$i < a}" by auto
   544     also have *: "{x::'a. x$$i < a} = (\<Union>k::nat. {..< (\<chi>\<chi> n. if n = i then a else real k)})" using `i <DIM('a)`
   545     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   546       fix x
   547       from real_arch_lt[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"]
   548       guess k::nat .. note k = this
   549       { fix i assume "i < DIM('a)"
   550         then have "x$$i < real k"
   551           using k by (subst (asm) Max_less_iff) auto
   552         then have "x$$i < real k" by simp }
   553       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia < real k"
   554         by (auto intro!: exI[of _ k])
   555     qed
   556     finally show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   557       apply (simp only:)
   558       apply (safe intro!: countable_UN Diff)
   559       by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   560   next
   561     fix a i assume "\<not> i < DIM('a)"
   562     then show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   563       using top by auto
   564   qed
   565   then show ?thesis by (intro sets_sigma_subset) auto
   566 qed
   567 
   568 lemma atMost_span_atLeastAtMost:
   569   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>) \<subseteq>
   570    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a,b). {a..b})\<rparr>)"
   571   (is "_ \<subseteq> sets ?SIGMA")
   572 proof -
   573   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   574   then interpret sigma_algebra ?SIGMA .
   575   { fix a::'a
   576     have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   577     proof (safe, simp_all add: eucl_le[where 'a='a])
   578       fix x
   579       from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   580       guess k::nat .. note k = this
   581       { fix i assume "i < DIM('a)"
   582         with k have "- x$$i \<le> real k"
   583           by (subst (asm) Max_le_iff) (auto simp: field_simps)
   584         then have "- real k \<le> x$$i" by simp }
   585       then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   586         by (auto intro!: exI[of _ k])
   587     qed
   588     have "{..a} \<in> sets ?SIGMA" unfolding *
   589       by (safe intro!: countable_UN)
   590          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   591   then show ?thesis by (intro sets_sigma_subset) auto
   592 qed
   593 
   594 lemma borel_eq_atMost:
   595   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})\<rparr>)"
   596     (is "_ = ?SIGMA")
   597 proof (intro algebra.equality antisym)
   598   show "sets borel \<subseteq> sets ?SIGMA"
   599     using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
   600     by auto
   601   show "sets ?SIGMA \<subseteq> sets borel"
   602     by (rule borel.sets_sigma_subset) auto
   603 qed auto
   604 
   605 lemma borel_eq_atLeastAtMost:
   606   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})\<rparr>)"
   607    (is "_ = ?SIGMA")
   608 proof (intro algebra.equality antisym)
   609   show "sets borel \<subseteq> sets ?SIGMA"
   610     using atMost_span_atLeastAtMost halfspace_le_span_atMost
   611       halfspace_span_halfspace_le open_span_halfspace
   612     by auto
   613   show "sets ?SIGMA \<subseteq> sets borel"
   614     by (rule borel.sets_sigma_subset) auto
   615 qed auto
   616 
   617 lemma borel_eq_greaterThan:
   618   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})\<rparr>)"
   619    (is "_ = ?SIGMA")
   620 proof (intro algebra.equality antisym)
   621   show "sets borel \<subseteq> sets ?SIGMA"
   622     using halfspace_le_span_greaterThan
   623       halfspace_span_halfspace_le open_span_halfspace
   624     by auto
   625   show "sets ?SIGMA \<subseteq> sets borel"
   626     by (rule borel.sets_sigma_subset) auto
   627 qed auto
   628 
   629 lemma borel_eq_lessThan:
   630   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {..< a})\<rparr>)"
   631    (is "_ = ?SIGMA")
   632 proof (intro algebra.equality antisym)
   633   show "sets borel \<subseteq> sets ?SIGMA"
   634     using halfspace_le_span_lessThan
   635       halfspace_span_halfspace_ge open_span_halfspace
   636     by auto
   637   show "sets ?SIGMA \<subseteq> sets borel"
   638     by (rule borel.sets_sigma_subset) auto
   639 qed auto
   640 
   641 lemma borel_eq_greaterThanLessThan:
   642   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})\<rparr>)"
   643     (is "_ = ?SIGMA")
   644 proof (intro algebra.equality antisym)
   645   show "sets ?SIGMA \<subseteq> sets borel"
   646     by (rule borel.sets_sigma_subset) auto
   647   show "sets borel \<subseteq> sets ?SIGMA"
   648   proof -
   649     have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   650     then interpret sigma_algebra ?SIGMA .
   651     { fix M :: "'a set" assume "M \<in> {S. open S}"
   652       then have "open M" by simp
   653       have "M \<in> sets ?SIGMA"
   654         apply (subst open_UNION[OF `open M`])
   655         apply (safe intro!: countable_UN)
   656         by (auto simp add: sigma_def intro!: sigma_sets.Basic) }
   657     then show ?thesis
   658       unfolding borel_def by (intro sets_sigma_subset) auto
   659   qed
   660 qed auto
   661 
   662 lemma borel_eq_atLeastLessThan:
   663   "borel = sigma \<lparr>space=UNIV, sets=range (\<lambda>(a, b). {a ..< b :: real})\<rparr>" (is "_ = ?S")
   664 proof (intro algebra.equality antisym)
   665   interpret sigma_algebra ?S
   666     by (rule sigma_algebra_sigma) auto
   667   show "sets borel \<subseteq> sets ?S"
   668     unfolding borel_eq_lessThan
   669   proof (intro sets_sigma_subset subsetI)
   670     have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   671     fix A :: "real set" assume "A \<in> sets \<lparr>space = UNIV, sets = range lessThan\<rparr>"
   672     then obtain x where "A = {..< x}" by auto
   673     then have "A = (\<Union>i::nat. {-real i ..< x})"
   674       by (auto simp: move_uminus real_arch_simple)
   675     then show "A \<in> sets ?S"
   676       by (auto simp: sets_sigma intro!: sigma_sets.intros)
   677   qed simp
   678   show "sets ?S \<subseteq> sets borel"
   679     by (intro borel.sets_sigma_subset) auto
   680 qed simp_all
   681 
   682 lemma borel_eq_halfspace_le:
   683   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})\<rparr>)"
   684    (is "_ = ?SIGMA")
   685 proof (intro algebra.equality antisym)
   686   show "sets borel \<subseteq> sets ?SIGMA"
   687     using open_span_halfspace halfspace_span_halfspace_le by auto
   688   show "sets ?SIGMA \<subseteq> sets borel"
   689     by (rule borel.sets_sigma_subset) auto
   690 qed auto
   691 
   692 lemma borel_eq_halfspace_less:
   693   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})\<rparr>)"
   694    (is "_ = ?SIGMA")
   695 proof (intro algebra.equality antisym)
   696   show "sets borel \<subseteq> sets ?SIGMA"
   697     using open_span_halfspace .
   698   show "sets ?SIGMA \<subseteq> sets borel"
   699     by (rule borel.sets_sigma_subset) auto
   700 qed auto
   701 
   702 lemma borel_eq_halfspace_gt:
   703   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})\<rparr>)"
   704    (is "_ = ?SIGMA")
   705 proof (intro algebra.equality antisym)
   706   show "sets borel \<subseteq> sets ?SIGMA"
   707     using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
   708   show "sets ?SIGMA \<subseteq> sets borel"
   709     by (rule borel.sets_sigma_subset) auto
   710 qed auto
   711 
   712 lemma borel_eq_halfspace_ge:
   713   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})\<rparr>)"
   714    (is "_ = ?SIGMA")
   715 proof (intro algebra.equality antisym)
   716   show "sets borel \<subseteq> sets ?SIGMA"
   717     using halfspace_span_halfspace_ge open_span_halfspace by auto
   718   show "sets ?SIGMA \<subseteq> sets borel"
   719     by (rule borel.sets_sigma_subset) auto
   720 qed auto
   721 
   722 lemma (in sigma_algebra) borel_measurable_halfspacesI:
   723   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   724   assumes "borel = (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   725   and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
   726   and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   727   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   728 proof safe
   729   fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   730   then show "S a i \<in> sets M" unfolding assms
   731     by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
   732 next
   733   assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   734   { fix a i have "S a i \<in> sets M"
   735     proof cases
   736       assume "i < DIM('c)"
   737       with a show ?thesis unfolding assms(2) by simp
   738     next
   739       assume "\<not> i < DIM('c)"
   740       from assms(3)[OF this] show ?thesis .
   741     qed }
   742   then have "f \<in> measurable M (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   743     by (auto intro!: measurable_sigma simp: assms(2))
   744   then show "f \<in> borel_measurable M" unfolding measurable_def
   745     unfolding assms(1) by simp
   746 qed
   747 
   748 lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
   749   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   750   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   751   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   752 
   753 lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
   754   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   755   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   756   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   757 
   758 lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
   759   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   760   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   761   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   762 
   763 lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
   764   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   765   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   766   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_gt]) auto
   767 
   768 lemma (in sigma_algebra) borel_measurable_iff_le:
   769   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   770   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   771 
   772 lemma (in sigma_algebra) borel_measurable_iff_less:
   773   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   774   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   775 
   776 lemma (in sigma_algebra) borel_measurable_iff_ge:
   777   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   778   using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
   779 
   780 lemma (in sigma_algebra) borel_measurable_iff_greater:
   781   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   782   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   783 
   784 lemma borel_measurable_euclidean_component:
   785   "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
   786   unfolding borel_def[where 'a=real]
   787 proof (rule borel.measurable_sigma, simp_all)
   788   fix S::"real set" assume "open S"
   789   from open_vimage_euclidean_component[OF this]
   790   show "(\<lambda>x. x $$ i) -` S \<in> sets borel"
   791     by (auto intro: borel_open)
   792 qed
   793 
   794 lemma (in sigma_algebra) borel_measurable_euclidean_space:
   795   fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   796   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   797 proof safe
   798   fix i assume "f \<in> borel_measurable M"
   799   then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
   800     using measurable_comp[of f _ _ "\<lambda>x. x $$ i", unfolded comp_def]
   801     by (auto intro: borel_measurable_euclidean_component)
   802 next
   803   assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
   804   then show "f \<in> borel_measurable M"
   805     unfolding borel_measurable_iff_halfspace_le by auto
   806 qed
   807 
   808 subsection "Borel measurable operators"
   809 
   810 lemma (in sigma_algebra) affine_borel_measurable_vector:
   811   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   812   assumes "f \<in> borel_measurable M"
   813   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   814 proof (rule borel_measurableI)
   815   fix S :: "'x set" assume "open S"
   816   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   817   proof cases
   818     assume "b \<noteq> 0"
   819     with `open S` have "open ((\<lambda>x. (- a + x) /\<^sub>R b) ` S)" (is "open ?S")
   820       by (auto intro!: open_affinity simp: scaleR_add_right)
   821     hence "?S \<in> sets borel"
   822       unfolding borel_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
   823     moreover
   824     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   825       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   826     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   827       by auto
   828   qed simp
   829 qed
   830 
   831 lemma (in sigma_algebra) affine_borel_measurable:
   832   fixes g :: "'a \<Rightarrow> real"
   833   assumes g: "g \<in> borel_measurable M"
   834   shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   835   using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   836 
   837 lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
   838   fixes f :: "'a \<Rightarrow> real"
   839   assumes f: "f \<in> borel_measurable M"
   840   assumes g: "g \<in> borel_measurable M"
   841   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   842 proof -
   843   have 1: "\<And>a. {w\<in>space M. a \<le> f w + g w} = {w \<in> space M. a + g w * -1 \<le> f w}"
   844     by auto
   845   have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
   846     by (rule affine_borel_measurable [OF g])
   847   then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
   848     by auto
   849   then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
   850     by (simp add: 1)
   851   then show ?thesis
   852     by (simp add: borel_measurable_iff_ge)
   853 qed
   854 
   855 lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
   856   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   857   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   858   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   859 proof cases
   860   assume "finite S"
   861   thus ?thesis using assms by induct auto
   862 qed simp
   863 
   864 lemma (in sigma_algebra) borel_measurable_square:
   865   fixes f :: "'a \<Rightarrow> real"
   866   assumes f: "f \<in> borel_measurable M"
   867   shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   868 proof -
   869   {
   870     fix a
   871     have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
   872     proof (cases rule: linorder_cases [of a 0])
   873       case less
   874       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
   875         by auto (metis less order_le_less_trans power2_less_0)
   876       also have "... \<in> sets M"
   877         by (rule empty_sets)
   878       finally show ?thesis .
   879     next
   880       case equal
   881       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   882              {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
   883         by auto
   884       also have "... \<in> sets M"
   885         apply (insert f)
   886         apply (rule Int)
   887         apply (simp add: borel_measurable_iff_le)
   888         apply (simp add: borel_measurable_iff_ge)
   889         done
   890       finally show ?thesis .
   891     next
   892       case greater
   893       have "\<forall>x. (f x ^ 2 \<le> sqrt a ^ 2) = (- sqrt a  \<le> f x & f x \<le> sqrt a)"
   894         by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
   895                   real_sqrt_le_iff real_sqrt_power)
   896       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   897              {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
   898         using greater by auto
   899       also have "... \<in> sets M"
   900         apply (insert f)
   901         apply (rule Int)
   902         apply (simp add: borel_measurable_iff_ge)
   903         apply (simp add: borel_measurable_iff_le)
   904         done
   905       finally show ?thesis .
   906     qed
   907   }
   908   thus ?thesis by (auto simp add: borel_measurable_iff_le)
   909 qed
   910 
   911 lemma times_eq_sum_squares:
   912    fixes x::real
   913    shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   914 by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   915 
   916 lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
   917   fixes g :: "'a \<Rightarrow> real"
   918   assumes g: "g \<in> borel_measurable M"
   919   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   920 proof -
   921   have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
   922     by simp
   923   also have "... \<in> borel_measurable M"
   924     by (fast intro: affine_borel_measurable g)
   925   finally show ?thesis .
   926 qed
   927 
   928 lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
   929   fixes f :: "'a \<Rightarrow> real"
   930   assumes f: "f \<in> borel_measurable M"
   931   assumes g: "g \<in> borel_measurable M"
   932   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   933 proof -
   934   have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
   935     using assms by (fast intro: affine_borel_measurable borel_measurable_square)
   936   have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
   937         (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
   938     by (simp add: minus_divide_right)
   939   also have "... \<in> borel_measurable M"
   940     using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
   941   finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
   942   show ?thesis
   943     apply (simp add: times_eq_sum_squares diff_minus)
   944     using 1 2 by simp
   945 qed
   946 
   947 lemma (in sigma_algebra) borel_measurable_setprod[simp, intro]:
   948   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   949   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   950   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   951 proof cases
   952   assume "finite S"
   953   thus ?thesis using assms by induct auto
   954 qed simp
   955 
   956 lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
   957   fixes f :: "'a \<Rightarrow> real"
   958   assumes f: "f \<in> borel_measurable M"
   959   assumes g: "g \<in> borel_measurable M"
   960   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   961   unfolding diff_minus using assms by fast
   962 
   963 lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
   964   fixes f :: "'a \<Rightarrow> real"
   965   assumes "f \<in> borel_measurable M"
   966   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   967   unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
   968 proof safe
   969   fix a :: real
   970   have *: "{w \<in> space M. a \<le> 1 / f w} =
   971       ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
   972       ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
   973       ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
   974   show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
   975     by (auto intro!: Int Un)
   976 qed
   977 
   978 lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
   979   fixes f :: "'a \<Rightarrow> real"
   980   assumes "f \<in> borel_measurable M"
   981   and "g \<in> borel_measurable M"
   982   shows "(\<lambda>x. f x / g x) \<in> borel_measurable M"
   983   unfolding field_divide_inverse
   984   by (rule borel_measurable_inverse borel_measurable_times assms)+
   985 
   986 lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
   987   fixes f g :: "'a \<Rightarrow> real"
   988   assumes "f \<in> borel_measurable M"
   989   assumes "g \<in> borel_measurable M"
   990   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   991   unfolding borel_measurable_iff_le
   992 proof safe
   993   fix a
   994   have "{x \<in> space M. max (g x) (f x) \<le> a} =
   995     {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
   996   thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
   997     using assms unfolding borel_measurable_iff_le
   998     by (auto intro!: Int)
   999 qed
  1000 
  1001 lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
  1002   fixes f g :: "'a \<Rightarrow> real"
  1003   assumes "f \<in> borel_measurable M"
  1004   assumes "g \<in> borel_measurable M"
  1005   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1006   unfolding borel_measurable_iff_ge
  1007 proof safe
  1008   fix a
  1009   have "{x \<in> space M. a \<le> min (g x) (f x)} =
  1010     {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
  1011   thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
  1012     using assms unfolding borel_measurable_iff_ge
  1013     by (auto intro!: Int)
  1014 qed
  1015 
  1016 lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
  1017   assumes "f \<in> borel_measurable M"
  1018   shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  1019 proof -
  1020   have *: "\<And>x. \<bar>f x\<bar> = max 0 (f x) + max 0 (- f x)" by (simp add: max_def)
  1021   show ?thesis unfolding * using assms by auto
  1022 qed
  1023 
  1024 lemma borel_measurable_nth[simp, intro]:
  1025   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1026   using borel_measurable_euclidean_component
  1027   unfolding nth_conv_component by auto
  1028 
  1029 lemma borel_measurable_continuous_on1:
  1030   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
  1031   assumes "continuous_on UNIV f"
  1032   shows "f \<in> borel_measurable borel"
  1033   apply(rule borel.borel_measurableI)
  1034   using continuous_open_preimage[OF assms] unfolding vimage_def by auto
  1035 
  1036 lemma borel_measurable_continuous_on:
  1037   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
  1038   assumes cont: "continuous_on A f" "open A"
  1039   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
  1040 proof (rule borel.borel_measurableI)
  1041   fix S :: "'b set" assume "open S"
  1042   then have "open {x\<in>A. f x \<in> S}"
  1043     by (intro continuous_open_preimage[OF cont]) auto
  1044   then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
  1045   have "?f -` S \<inter> space borel = 
  1046     {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
  1047     by (auto split: split_if_asm)
  1048   also have "\<dots> \<in> sets borel"
  1049     using * `open A` by (auto simp del: space_borel intro!: borel.Un)
  1050   finally show "?f -` S \<inter> space borel \<in> sets borel" .
  1051 qed
  1052 
  1053 lemma (in sigma_algebra) convex_measurable:
  1054   fixes a b :: real
  1055   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
  1056   assumes q: "convex_on { a <..< b} q"
  1057   shows "q \<circ> X \<in> borel_measurable M"
  1058 proof -
  1059   have "(\<lambda>x. if x \<in> {a <..< b} then q x else 0) \<in> borel_measurable borel"
  1060   proof (rule borel_measurable_continuous_on)
  1061     show "open {a<..<b}" by auto
  1062     from this q show "continuous_on {a<..<b} q"
  1063       by (rule convex_on_continuous)
  1064   qed
  1065   then have "(\<lambda>x. if x \<in> {a <..< b} then q x else 0) \<circ> X \<in> borel_measurable M" (is ?qX)
  1066     using X by (intro measurable_comp) auto
  1067   moreover have "?qX \<longleftrightarrow> q \<circ> X \<in> borel_measurable M"
  1068     using X by (intro measurable_cong) auto
  1069   ultimately show ?thesis by simp
  1070 qed
  1071 
  1072 lemma borel_measurable_borel_log: assumes "1 < b" shows "log b \<in> borel_measurable borel"
  1073 proof -
  1074   { fix x :: real assume x: "x \<le> 0"
  1075     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
  1076     from this[of x] x this[of 0] have "log b 0 = log b x"
  1077       by (auto simp: ln_def log_def) }
  1078   note log_imp = this
  1079   have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) \<in> borel_measurable borel"
  1080   proof (rule borel_measurable_continuous_on)
  1081     show "continuous_on {0<..} (log b)"
  1082       by (auto intro!: continuous_at_imp_continuous_on DERIV_log DERIV_isCont
  1083                simp: continuous_isCont[symmetric])
  1084     show "open ({0<..}::real set)" by auto
  1085   qed
  1086   also have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) = log b"
  1087     by (simp add: fun_eq_iff not_less log_imp)
  1088   finally show ?thesis .
  1089 qed
  1090 
  1091 lemma (in sigma_algebra) borel_measurable_log[simp,intro]:
  1092   assumes f: "f \<in> borel_measurable M" and "1 < b"
  1093   shows "(\<lambda>x. log b (f x)) \<in> borel_measurable M"
  1094   using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
  1095   by (simp add: comp_def)
  1096 
  1097 subsection "Borel space on the extended reals"
  1098 
  1099 lemma borel_measurable_ereal_borel:
  1100   "ereal \<in> borel_measurable borel"
  1101   unfolding borel_def[where 'a=ereal]
  1102 proof (rule borel.measurable_sigma)
  1103   fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = {S. open S} \<rparr>"
  1104   then have "open X" by simp
  1105   then have "open (ereal -` X \<inter> space borel)"
  1106     by (simp add: open_ereal_vimage)
  1107   then show "ereal -` X \<inter> space borel \<in> sets borel" by auto
  1108 qed auto
  1109 
  1110 lemma (in sigma_algebra) borel_measurable_ereal[simp, intro]:
  1111   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1112   using measurable_comp[OF f borel_measurable_ereal_borel] unfolding comp_def .
  1113 
  1114 lemma borel_measurable_real_of_ereal_borel:
  1115   "(real :: ereal \<Rightarrow> real) \<in> borel_measurable borel"
  1116   unfolding borel_def[where 'a=real]
  1117 proof (rule borel.measurable_sigma)
  1118   fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = {S. open S} \<rparr>"
  1119   then have "open B" by simp
  1120   have *: "ereal -` real -` (B - {0}) = B - {0}" by auto
  1121   have open_real: "open (real -` (B - {0}) :: ereal set)"
  1122     unfolding open_ereal_def * using `open B` by auto
  1123   show "(real -` B \<inter> space borel :: ereal set) \<in> sets borel"
  1124   proof cases
  1125     assume "0 \<in> B"
  1126     then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0::ereal}"
  1127       by (auto simp add: real_of_ereal_eq_0)
  1128     then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
  1129       using open_real by auto
  1130   next
  1131     assume "0 \<notin> B"
  1132     then have *: "(real -` B :: ereal set) = real -` (B - {0})"
  1133       by (auto simp add: real_of_ereal_eq_0)
  1134     then show "(real -` B :: ereal set) \<inter> space borel \<in> sets borel"
  1135       using open_real by auto
  1136   qed
  1137 qed auto
  1138 
  1139 lemma (in sigma_algebra) borel_measurable_real_of_ereal[simp, intro]:
  1140   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: ereal)) \<in> borel_measurable M"
  1141   using measurable_comp[OF f borel_measurable_real_of_ereal_borel] unfolding comp_def .
  1142 
  1143 lemma (in sigma_algebra) borel_measurable_ereal_iff:
  1144   shows "(\<lambda>x. ereal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1145 proof
  1146   assume "(\<lambda>x. ereal (f x)) \<in> borel_measurable M"
  1147   from borel_measurable_real_of_ereal[OF this]
  1148   show "f \<in> borel_measurable M" by auto
  1149 qed auto
  1150 
  1151 lemma (in sigma_algebra) borel_measurable_ereal_iff_real:
  1152   fixes f :: "'a \<Rightarrow> ereal"
  1153   shows "f \<in> borel_measurable M \<longleftrightarrow>
  1154     ((\<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)"
  1155 proof safe
  1156   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"
  1157   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
  1158   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
  1159   let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else ereal (real (f x))"
  1160   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1161   also have "?f = f" by (auto simp: fun_eq_iff ereal_real)
  1162   finally show "f \<in> borel_measurable M" .
  1163 qed (auto intro: measurable_sets borel_measurable_real_of_ereal)
  1164 
  1165 lemma (in sigma_algebra) less_eq_ge_measurable:
  1166   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1167   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1168 proof
  1169   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1170   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1171   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1172 next
  1173   assume "f -` {..a} \<inter> space M \<in> sets M"
  1174   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1175   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1176 qed
  1177 
  1178 lemma (in sigma_algebra) greater_eq_le_measurable:
  1179   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1180   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
  1181 proof
  1182   assume "f -` {a ..} \<inter> space M \<in> sets M"
  1183   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
  1184   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
  1185 next
  1186   assume "f -` {..< a} \<inter> space M \<in> sets M"
  1187   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
  1188   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  1189 qed
  1190 
  1191 lemma (in sigma_algebra) borel_measurable_uminus_borel_ereal:
  1192   "(uminus :: ereal \<Rightarrow> ereal) \<in> borel_measurable borel"
  1193 proof (subst borel_def, rule borel.measurable_sigma)
  1194   fix X :: "ereal set" assume "X \<in> sets \<lparr>space = UNIV, sets = {S. open S}\<rparr>"
  1195   then have "open X" by simp
  1196   have "uminus -` X = uminus ` X" by (force simp: image_iff)
  1197   then have "open (uminus -` X)" using `open X` ereal_open_uminus by auto
  1198   then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
  1199 qed auto
  1200 
  1201 lemma (in sigma_algebra) borel_measurable_uminus_ereal[intro]:
  1202   assumes "f \<in> borel_measurable M"
  1203   shows "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M"
  1204   using measurable_comp[OF assms borel_measurable_uminus_borel_ereal] by (simp add: comp_def)
  1205 
  1206 lemma (in sigma_algebra) borel_measurable_uminus_eq_ereal[simp]:
  1207   "(\<lambda>x. - f x :: ereal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1208 proof
  1209   assume ?l from borel_measurable_uminus_ereal[OF this] show ?r by simp
  1210 qed auto
  1211 
  1212 lemma (in sigma_algebra) borel_measurable_eq_atMost_ereal:
  1213   fixes f :: "'a \<Rightarrow> ereal"
  1214   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1215 proof (intro iffI allI)
  1216   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
  1217   show "f \<in> borel_measurable M"
  1218     unfolding borel_measurable_ereal_iff_real borel_measurable_iff_le
  1219   proof (intro conjI allI)
  1220     fix a :: real
  1221     { fix x :: ereal assume *: "\<forall>i::nat. real i < x"
  1222       have "x = \<infinity>"
  1223       proof (rule ereal_top)
  1224         fix B from real_arch_lt[of B] guess n ..
  1225         then have "ereal B < real n" by auto
  1226         with * show "B \<le> x" by (metis less_trans less_imp_le)
  1227       qed }
  1228     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
  1229       by (auto simp: not_le)
  1230     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos by (auto simp del: UN_simps intro!: Diff)
  1231     moreover
  1232     have "{-\<infinity>::ereal} = {..-\<infinity>}" by auto
  1233     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
  1234     moreover have "{x\<in>space M. f x \<le> ereal a} \<in> sets M"
  1235       using pos[of "ereal a"] by (simp add: vimage_def Int_def conj_commute)
  1236     moreover have "{w \<in> space M. real (f w) \<le> a} =
  1237       (if a < 0 then {w \<in> space M. f w \<le> ereal a} - f -` {-\<infinity>} \<inter> space M
  1238       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")
  1239       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
  1240     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
  1241   qed
  1242 qed (simp add: measurable_sets)
  1243 
  1244 lemma (in sigma_algebra) borel_measurable_eq_atLeast_ereal:
  1245   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1246 proof
  1247   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
  1248   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
  1249     by (auto simp: ereal_uminus_le_reorder)
  1250   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
  1251     unfolding borel_measurable_eq_atMost_ereal by auto
  1252   then show "f \<in> borel_measurable M" by simp
  1253 qed (simp add: measurable_sets)
  1254 
  1255 lemma (in sigma_algebra) borel_measurable_ereal_iff_less:
  1256   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1257   unfolding borel_measurable_eq_atLeast_ereal greater_eq_le_measurable ..
  1258 
  1259 lemma (in sigma_algebra) borel_measurable_ereal_iff_ge:
  1260   "(f::'a \<Rightarrow> ereal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1261   unfolding borel_measurable_eq_atMost_ereal less_eq_ge_measurable ..
  1262 
  1263 lemma (in sigma_algebra) borel_measurable_ereal_eq_const:
  1264   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
  1265   shows "{x\<in>space M. f x = c} \<in> sets M"
  1266 proof -
  1267   have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
  1268   then show ?thesis using assms by (auto intro!: measurable_sets)
  1269 qed
  1270 
  1271 lemma (in sigma_algebra) borel_measurable_ereal_neq_const:
  1272   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
  1273   shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1274 proof -
  1275   have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
  1276   then show ?thesis using assms by (auto intro!: measurable_sets)
  1277 qed
  1278 
  1279 lemma (in sigma_algebra) borel_measurable_ereal_le[intro,simp]:
  1280   fixes f g :: "'a \<Rightarrow> ereal"
  1281   assumes f: "f \<in> borel_measurable M"
  1282   assumes g: "g \<in> borel_measurable M"
  1283   shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  1284 proof -
  1285   have "{x \<in> space M. f x \<le> g x} =
  1286     {x \<in> space M. real (f x) \<le> real (g x)} - (f -` {\<infinity>, -\<infinity>} \<inter> space M \<union> g -` {\<infinity>, -\<infinity>} \<inter> space M) \<union>
  1287     f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
  1288   proof (intro set_eqI)
  1289     fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: ereal2_cases[of "f x" "g x"]) auto
  1290   qed
  1291   with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
  1292 qed
  1293 
  1294 lemma (in sigma_algebra) borel_measurable_ereal_less[intro,simp]:
  1295   fixes f :: "'a \<Rightarrow> ereal"
  1296   assumes f: "f \<in> borel_measurable M"
  1297   assumes g: "g \<in> borel_measurable M"
  1298   shows "{x \<in> space M. f x < g x} \<in> sets M"
  1299 proof -
  1300   have "{x \<in> space M. f x < g x} = space M - {x \<in> space M. g x \<le> f x}" by auto
  1301   then show ?thesis using g f by auto
  1302 qed
  1303 
  1304 lemma (in sigma_algebra) borel_measurable_ereal_eq[intro,simp]:
  1305   fixes f :: "'a \<Rightarrow> ereal"
  1306   assumes f: "f \<in> borel_measurable M"
  1307   assumes g: "g \<in> borel_measurable M"
  1308   shows "{w \<in> space M. f w = g w} \<in> sets M"
  1309 proof -
  1310   have "{x \<in> space M. f x = g x} = {x \<in> space M. g x \<le> f x} \<inter> {x \<in> space M. f x \<le> g x}" by auto
  1311   then show ?thesis using g f by auto
  1312 qed
  1313 
  1314 lemma (in sigma_algebra) borel_measurable_ereal_neq[intro,simp]:
  1315   fixes f :: "'a \<Rightarrow> ereal"
  1316   assumes f: "f \<in> borel_measurable M"
  1317   assumes g: "g \<in> borel_measurable M"
  1318   shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
  1319 proof -
  1320   have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
  1321   thus ?thesis using f g by auto
  1322 qed
  1323 
  1324 lemma (in sigma_algebra) split_sets:
  1325   "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
  1326   "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
  1327   by auto
  1328 
  1329 lemma (in sigma_algebra) borel_measurable_ereal_add[intro, simp]:
  1330   fixes f :: "'a \<Rightarrow> ereal"
  1331   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1332   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1333 proof -
  1334   { fix x assume "x \<in> space M" then have "f x + g x =
  1335       (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
  1336         else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
  1337         else ereal (real (f x) + real (g x)))"
  1338       by (cases rule: ereal2_cases[of "f x" "g x"]) auto }
  1339   with assms show ?thesis
  1340     by (auto cong: measurable_cong simp: split_sets
  1341              intro!: Un measurable_If measurable_sets)
  1342 qed
  1343 
  1344 lemma (in sigma_algebra) borel_measurable_ereal_setsum[simp, intro]:
  1345   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1346   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1347   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1348 proof cases
  1349   assume "finite S"
  1350   thus ?thesis using assms
  1351     by induct auto
  1352 qed (simp add: borel_measurable_const)
  1353 
  1354 lemma (in sigma_algebra) borel_measurable_ereal_abs[intro, simp]:
  1355   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M"
  1356   shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1357 proof -
  1358   { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
  1359   then show ?thesis using assms by (auto intro!: measurable_If)
  1360 qed
  1361 
  1362 lemma (in sigma_algebra) borel_measurable_ereal_times[intro, simp]:
  1363   fixes f :: "'a \<Rightarrow> ereal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1364   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1365 proof -
  1366   { fix f g :: "'a \<Rightarrow> ereal"
  1367     assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1368       and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
  1369     { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
  1370         else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
  1371         else ereal (real (f x) * real (g x)))"
  1372       apply (cases rule: ereal2_cases[of "f x" "g x"])
  1373       using pos[of x] by auto }
  1374     with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1375       by (auto cong: measurable_cong simp: split_sets
  1376                intro!: Un measurable_If measurable_sets) }
  1377   note pos_times = this
  1378   have *: "(\<lambda>x. f x * g x) =
  1379     (\<lambda>x. if 0 \<le> f x \<and> 0 \<le> g x \<or> f x < 0 \<and> g x < 0 then \<bar>f x\<bar> * \<bar>g x\<bar> else - (\<bar>f x\<bar> * \<bar>g x\<bar>))"
  1380     by (auto simp: fun_eq_iff)
  1381   show ?thesis using assms unfolding *
  1382     by (intro measurable_If pos_times borel_measurable_uminus_ereal)
  1383        (auto simp: split_sets intro!: Int)
  1384 qed
  1385 
  1386 lemma (in sigma_algebra) borel_measurable_ereal_setprod[simp, intro]:
  1387   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> ereal"
  1388   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1389   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1390 proof cases
  1391   assume "finite S"
  1392   thus ?thesis using assms by induct auto
  1393 qed simp
  1394 
  1395 lemma (in sigma_algebra) borel_measurable_ereal_min[simp, intro]:
  1396   fixes f g :: "'a \<Rightarrow> ereal"
  1397   assumes "f \<in> borel_measurable M"
  1398   assumes "g \<in> borel_measurable M"
  1399   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1400   using assms unfolding min_def by (auto intro!: measurable_If)
  1401 
  1402 lemma (in sigma_algebra) borel_measurable_ereal_max[simp, intro]:
  1403   fixes f g :: "'a \<Rightarrow> ereal"
  1404   assumes "f \<in> borel_measurable M"
  1405   and "g \<in> borel_measurable M"
  1406   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1407   using assms unfolding max_def by (auto intro!: measurable_If)
  1408 
  1409 lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
  1410   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1411   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1412   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1413   unfolding borel_measurable_ereal_iff_ge
  1414 proof
  1415   fix a
  1416   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1417     by (auto simp: less_SUP_iff SUPR_apply)
  1418   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1419     using assms by auto
  1420 qed
  1421 
  1422 lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
  1423   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> ereal"
  1424   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1425   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1426   unfolding borel_measurable_ereal_iff_less
  1427 proof
  1428   fix a
  1429   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1430     by (auto simp: INF_less_iff INFI_apply)
  1431   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1432     using assms by auto
  1433 qed
  1434 
  1435 lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
  1436   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1437   assumes "\<And>i. f i \<in> borel_measurable M"
  1438   shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1439   unfolding liminf_SUPR_INFI using assms by auto
  1440 
  1441 lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
  1442   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1443   assumes "\<And>i. f i \<in> borel_measurable M"
  1444   shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1445   unfolding limsup_INFI_SUPR using assms by auto
  1446 
  1447 lemma (in sigma_algebra) borel_measurable_ereal_diff[simp, intro]:
  1448   fixes f g :: "'a \<Rightarrow> ereal"
  1449   assumes "f \<in> borel_measurable M"
  1450   assumes "g \<in> borel_measurable M"
  1451   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1452   unfolding minus_ereal_def using assms by auto
  1453 
  1454 lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
  1455   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> ereal"
  1456   assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
  1457   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1458   apply (subst measurable_cong)
  1459   apply (subst suminf_ereal_eq_SUPR)
  1460   apply (rule pos)
  1461   using assms by auto
  1462 
  1463 section "LIMSEQ is borel measurable"
  1464 
  1465 lemma (in sigma_algebra) borel_measurable_LIMSEQ:
  1466   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1467   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1468   and u: "\<And>i. u i \<in> borel_measurable M"
  1469   shows "u' \<in> borel_measurable M"
  1470 proof -
  1471   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. ereal (u n x)) = ereal (u' x)"
  1472     using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_ereal)
  1473   moreover from u have "(\<lambda>x. liminf (\<lambda>n. ereal (u n x))) \<in> borel_measurable M"
  1474     by auto
  1475   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_ereal_iff)
  1476 qed
  1477 
  1478 end