src/HOL/Probability/Borel_Space.thy
author hoelzl
Thu May 26 20:49:56 2011 +0200 (2011-05-26)
changeset 42990 3706951a6421
parent 42950 6e5c2a3c69da
child 43920 cedb5cb948fd
permissions -rw-r--r--
composition of convex and measurable function is measurable
     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 = open\<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 = open\<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> open" unfolding mem_def 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 "S \<in> open" thus "f -` S \<inter> space M \<in> sets M"
    75     using assms[of S] by (simp add: mem_def)
    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> extreal" 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 :: "extreal 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 :: "extreal 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> open" then have "open S" unfolding mem_def .
   395     from open_UNION[OF this]
   396     obtain I where *: "S =
   397       (\<Union>(a, b)\<in>I.
   398           (\<Inter> i<DIM('a). {x. (Chi (real_of_rat \<circ> op ! a)::'a) $$ i < x $$ i}) \<inter>
   399           (\<Inter> i<DIM('a). {x. x $$ i < (Chi (real_of_rat \<circ> op ! b)::'a) $$ i}))"
   400       unfolding greaterThanLessThan_def
   401       unfolding eucl_greaterThan_eq_halfspaces[where 'a='a]
   402       unfolding eucl_lessThan_eq_halfspaces[where 'a='a]
   403       by blast
   404     have "S \<in> sets ?SIGMA"
   405       unfolding *
   406       by (auto intro!: countable_UN Int countable_INT halfspace_lt_in_halfspace halfspace_gt_in_halfspace) }
   407   then show ?thesis unfolding borel_def
   408     by (intro sets_sigma_subset) auto
   409 qed
   410 
   411 lemma halfspace_span_halfspace_le:
   412   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   413    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. x $$ i \<le> a})\<rparr>)"
   414   (is "_ \<subseteq> sets ?SIGMA")
   415 proof -
   416   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   417   then interpret sigma_algebra ?SIGMA .
   418   { fix a i
   419     have *: "{x::'a. x$$i < a} = (\<Union>n. {x. x$$i \<le> a - 1/real (Suc n)})"
   420     proof (safe, simp_all)
   421       fix x::'a assume *: "x$$i < a"
   422       with reals_Archimedean[of "a - x$$i"]
   423       obtain n where "x $$ i < a - 1 / (real (Suc n))"
   424         by (auto simp: field_simps inverse_eq_divide)
   425       then show "\<exists>n. x $$ i \<le> a - 1 / (real (Suc n))"
   426         by (blast intro: less_imp_le)
   427     next
   428       fix x::'a and n
   429       assume "x$$i \<le> a - 1 / real (Suc n)"
   430       also have "\<dots> < a" by auto
   431       finally show "x$$i < a" .
   432     qed
   433     have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   434       by (safe intro!: countable_UN)
   435          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   436   then show ?thesis by (intro sets_sigma_subset) auto
   437 qed
   438 
   439 lemma halfspace_span_halfspace_ge:
   440   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i < a})\<rparr>) \<subseteq>
   441    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a \<le> x $$ i})\<rparr>)"
   442   (is "_ \<subseteq> sets ?SIGMA")
   443 proof -
   444   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   445   then interpret sigma_algebra ?SIGMA .
   446   { fix a i have *: "{x::'a. x$$i < a} = space ?SIGMA - {x::'a. a \<le> x$$i}" by auto
   447     have "{x. x$$i < a} \<in> sets ?SIGMA" unfolding *
   448       by (safe intro!: Diff)
   449          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   450   then show ?thesis by (intro sets_sigma_subset) auto
   451 qed
   452 
   453 lemma halfspace_le_span_halfspace_gt:
   454   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   455    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x. a < x $$ i})\<rparr>)"
   456   (is "_ \<subseteq> sets ?SIGMA")
   457 proof -
   458   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   459   then interpret sigma_algebra ?SIGMA .
   460   { fix a i have *: "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   461     have "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   462       by (safe intro!: Diff)
   463          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   464   then show ?thesis by (intro sets_sigma_subset) auto
   465 qed
   466 
   467 lemma halfspace_le_span_atMost:
   468   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   469    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>)"
   470   (is "_ \<subseteq> sets ?SIGMA")
   471 proof -
   472   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   473   then interpret sigma_algebra ?SIGMA .
   474   have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   475   proof cases
   476     fix a i assume "i < DIM('a)"
   477     then have *: "{x::'a. x$$i \<le> a} = (\<Union>k::nat. {.. (\<chi>\<chi> n. if n = i then a else real k)})"
   478     proof (safe, simp_all add: eucl_le[where 'a='a] split: split_if_asm)
   479       fix x
   480       from real_arch_simple[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"] guess k::nat ..
   481       then have "\<And>i. i < DIM('a) \<Longrightarrow> x$$i \<le> real k"
   482         by (subst (asm) Max_le_iff) auto
   483       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia \<le> real k"
   484         by (auto intro!: exI[of _ k])
   485     qed
   486     show "{x. x$$i \<le> a} \<in> sets ?SIGMA" unfolding *
   487       by (safe intro!: countable_UN)
   488          (auto simp: sets_sigma intro!: sigma_sets.Basic)
   489   next
   490     fix a i assume "\<not> i < DIM('a)"
   491     then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   492       using top by auto
   493   qed
   494   then show ?thesis by (intro sets_sigma_subset) auto
   495 qed
   496 
   497 lemma halfspace_le_span_greaterThan:
   498   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. x $$ i \<le> a})\<rparr>) \<subseteq>
   499    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {a<..})\<rparr>)"
   500   (is "_ \<subseteq> sets ?SIGMA")
   501 proof -
   502   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   503   then interpret sigma_algebra ?SIGMA .
   504   have "\<And>a i. {x. x$$i \<le> a} \<in> sets ?SIGMA"
   505   proof cases
   506     fix a i assume "i < DIM('a)"
   507     have "{x::'a. x$$i \<le> a} = space ?SIGMA - {x::'a. a < x$$i}" by auto
   508     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)`
   509     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   510       fix x
   511       from real_arch_lt[of "Max ((\<lambda>i. -x$$i)`{..<DIM('a)})"]
   512       guess k::nat .. note k = this
   513       { fix i assume "i < DIM('a)"
   514         then have "-x$$i < real k"
   515           using k by (subst (asm) Max_less_iff) auto
   516         then have "- real k < x$$i" by simp }
   517       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> -real k < x $$ ia"
   518         by (auto intro!: exI[of _ k])
   519     qed
   520     finally show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   521       apply (simp only:)
   522       apply (safe intro!: countable_UN Diff)
   523       by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   524   next
   525     fix a i assume "\<not> i < DIM('a)"
   526     then show "{x. x$$i \<le> a} \<in> sets ?SIGMA"
   527       using top by auto
   528   qed
   529   then show ?thesis by (intro sets_sigma_subset) auto
   530 qed
   531 
   532 lemma halfspace_le_span_lessThan:
   533   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x\<Colon>'a\<Colon>ordered_euclidean_space. a \<le> x $$ i})\<rparr>) \<subseteq>
   534    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..<a})\<rparr>)"
   535   (is "_ \<subseteq> sets ?SIGMA")
   536 proof -
   537   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   538   then interpret sigma_algebra ?SIGMA .
   539   have "\<And>a i. {x. a \<le> x$$i} \<in> sets ?SIGMA"
   540   proof cases
   541     fix a i assume "i < DIM('a)"
   542     have "{x::'a. a \<le> x$$i} = space ?SIGMA - {x::'a. x$$i < a}" by auto
   543     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)`
   544     proof (safe, simp_all add: eucl_less[where 'a='a] split: split_if_asm)
   545       fix x
   546       from real_arch_lt[of "Max ((\<lambda>i. x$$i)`{..<DIM('a)})"]
   547       guess k::nat .. note k = this
   548       { fix i assume "i < DIM('a)"
   549         then have "x$$i < real k"
   550           using k by (subst (asm) Max_less_iff) auto
   551         then have "x$$i < real k" by simp }
   552       then show "\<exists>k::nat. \<forall>ia. ia \<noteq> i \<longrightarrow> ia < DIM('a) \<longrightarrow> x $$ ia < real k"
   553         by (auto intro!: exI[of _ k])
   554     qed
   555     finally show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   556       apply (simp only:)
   557       apply (safe intro!: countable_UN Diff)
   558       by (auto simp: sets_sigma intro!: sigma_sets.Basic)
   559   next
   560     fix a i assume "\<not> i < DIM('a)"
   561     then show "{x. a \<le> x$$i} \<in> sets ?SIGMA"
   562       using top by auto
   563   qed
   564   then show ?thesis by (intro sets_sigma_subset) auto
   565 qed
   566 
   567 lemma atMost_span_atLeastAtMost:
   568   "sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>a. {..a\<Colon>'a\<Colon>ordered_euclidean_space})\<rparr>) \<subseteq>
   569    sets (sigma \<lparr>space=UNIV, sets=range (\<lambda>(a,b). {a..b})\<rparr>)"
   570   (is "_ \<subseteq> sets ?SIGMA")
   571 proof -
   572   have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   573   then interpret sigma_algebra ?SIGMA .
   574   { fix a::'a
   575     have *: "{..a} = (\<Union>n::nat. {- real n *\<^sub>R One .. a})"
   576     proof (safe, simp_all add: eucl_le[where 'a='a])
   577       fix x
   578       from real_arch_simple[of "Max ((\<lambda>i. - x$$i)`{..<DIM('a)})"]
   579       guess k::nat .. note k = this
   580       { fix i assume "i < DIM('a)"
   581         with k have "- x$$i \<le> real k"
   582           by (subst (asm) Max_le_iff) (auto simp: field_simps)
   583         then have "- real k \<le> x$$i" by simp }
   584       then show "\<exists>n::nat. \<forall>i<DIM('a). - real n \<le> x $$ i"
   585         by (auto intro!: exI[of _ k])
   586     qed
   587     have "{..a} \<in> sets ?SIGMA" unfolding *
   588       by (safe intro!: countable_UN)
   589          (auto simp: sets_sigma intro!: sigma_sets.Basic) }
   590   then show ?thesis by (intro sets_sigma_subset) auto
   591 qed
   592 
   593 lemma borel_eq_atMost:
   594   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> a. {.. a::'a\<Colon>ordered_euclidean_space})\<rparr>)"
   595     (is "_ = ?SIGMA")
   596 proof (intro algebra.equality antisym)
   597   show "sets borel \<subseteq> sets ?SIGMA"
   598     using halfspace_le_span_atMost halfspace_span_halfspace_le open_span_halfspace
   599     by auto
   600   show "sets ?SIGMA \<subseteq> sets borel"
   601     by (rule borel.sets_sigma_subset) auto
   602 qed auto
   603 
   604 lemma borel_eq_atLeastAtMost:
   605   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space, b). {a .. b})\<rparr>)"
   606    (is "_ = ?SIGMA")
   607 proof (intro algebra.equality antisym)
   608   show "sets borel \<subseteq> sets ?SIGMA"
   609     using atMost_span_atLeastAtMost halfspace_le_span_atMost
   610       halfspace_span_halfspace_le open_span_halfspace
   611     by auto
   612   show "sets ?SIGMA \<subseteq> sets borel"
   613     by (rule borel.sets_sigma_subset) auto
   614 qed auto
   615 
   616 lemma borel_eq_greaterThan:
   617   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {a <..})\<rparr>)"
   618    (is "_ = ?SIGMA")
   619 proof (intro algebra.equality antisym)
   620   show "sets borel \<subseteq> sets ?SIGMA"
   621     using halfspace_le_span_greaterThan
   622       halfspace_span_halfspace_le open_span_halfspace
   623     by auto
   624   show "sets ?SIGMA \<subseteq> sets borel"
   625     by (rule borel.sets_sigma_subset) auto
   626 qed auto
   627 
   628 lemma borel_eq_lessThan:
   629   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a :: 'a\<Colon>ordered_euclidean_space). {..< a})\<rparr>)"
   630    (is "_ = ?SIGMA")
   631 proof (intro algebra.equality antisym)
   632   show "sets borel \<subseteq> sets ?SIGMA"
   633     using halfspace_le_span_lessThan
   634       halfspace_span_halfspace_ge open_span_halfspace
   635     by auto
   636   show "sets ?SIGMA \<subseteq> sets borel"
   637     by (rule borel.sets_sigma_subset) auto
   638 qed auto
   639 
   640 lemma borel_eq_greaterThanLessThan:
   641   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, b). {a <..< (b :: 'a \<Colon> ordered_euclidean_space)})\<rparr>)"
   642     (is "_ = ?SIGMA")
   643 proof (intro algebra.equality antisym)
   644   show "sets ?SIGMA \<subseteq> sets borel"
   645     by (rule borel.sets_sigma_subset) auto
   646   show "sets borel \<subseteq> sets ?SIGMA"
   647   proof -
   648     have "sigma_algebra ?SIGMA" by (rule sigma_algebra_sigma) auto
   649     then interpret sigma_algebra ?SIGMA .
   650     { fix M :: "'a set" assume "M \<in> open"
   651       then have "open M" by (simp add: mem_def)
   652       have "M \<in> sets ?SIGMA"
   653         apply (subst open_UNION[OF `open M`])
   654         apply (safe intro!: countable_UN)
   655         by (auto simp add: sigma_def intro!: sigma_sets.Basic) }
   656     then show ?thesis
   657       unfolding borel_def by (intro sets_sigma_subset) auto
   658   qed
   659 qed auto
   660 
   661 lemma borel_eq_atLeastLessThan:
   662   "borel = sigma \<lparr>space=UNIV, sets=range (\<lambda>(a, b). {a ..< b :: real})\<rparr>" (is "_ = ?S")
   663 proof (intro algebra.equality antisym)
   664   interpret sigma_algebra ?S
   665     by (rule sigma_algebra_sigma) auto
   666   show "sets borel \<subseteq> sets ?S"
   667     unfolding borel_eq_lessThan
   668   proof (intro sets_sigma_subset subsetI)
   669     have move_uminus: "\<And>x y::real. -x \<le> y \<longleftrightarrow> -y \<le> x" by auto
   670     fix A :: "real set" assume "A \<in> sets \<lparr>space = UNIV, sets = range lessThan\<rparr>"
   671     then obtain x where "A = {..< x}" by auto
   672     then have "A = (\<Union>i::nat. {-real i ..< x})"
   673       by (auto simp: move_uminus real_arch_simple)
   674     then show "A \<in> sets ?S"
   675       by (auto simp: sets_sigma intro!: sigma_sets.intros)
   676   qed simp
   677   show "sets ?S \<subseteq> sets borel"
   678     by (intro borel.sets_sigma_subset) auto
   679 qed simp_all
   680 
   681 lemma borel_eq_halfspace_le:
   682   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i \<le> a})\<rparr>)"
   683    (is "_ = ?SIGMA")
   684 proof (intro algebra.equality antisym)
   685   show "sets borel \<subseteq> sets ?SIGMA"
   686     using open_span_halfspace halfspace_span_halfspace_le by auto
   687   show "sets ?SIGMA \<subseteq> sets borel"
   688     by (rule borel.sets_sigma_subset) auto
   689 qed auto
   690 
   691 lemma borel_eq_halfspace_less:
   692   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. x$$i < a})\<rparr>)"
   693    (is "_ = ?SIGMA")
   694 proof (intro algebra.equality antisym)
   695   show "sets borel \<subseteq> sets ?SIGMA"
   696     using open_span_halfspace .
   697   show "sets ?SIGMA \<subseteq> sets borel"
   698     by (rule borel.sets_sigma_subset) auto
   699 qed auto
   700 
   701 lemma borel_eq_halfspace_gt:
   702   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a < x$$i})\<rparr>)"
   703    (is "_ = ?SIGMA")
   704 proof (intro algebra.equality antisym)
   705   show "sets borel \<subseteq> sets ?SIGMA"
   706     using halfspace_le_span_halfspace_gt open_span_halfspace halfspace_span_halfspace_le by auto
   707   show "sets ?SIGMA \<subseteq> sets borel"
   708     by (rule borel.sets_sigma_subset) auto
   709 qed auto
   710 
   711 lemma borel_eq_halfspace_ge:
   712   "borel = (sigma \<lparr>space=UNIV, sets=range (\<lambda> (a, i). {x::'a::ordered_euclidean_space. a \<le> x$$i})\<rparr>)"
   713    (is "_ = ?SIGMA")
   714 proof (intro algebra.equality antisym)
   715   show "sets borel \<subseteq> sets ?SIGMA"
   716     using halfspace_span_halfspace_ge open_span_halfspace by auto
   717   show "sets ?SIGMA \<subseteq> sets borel"
   718     by (rule borel.sets_sigma_subset) auto
   719 qed auto
   720 
   721 lemma (in sigma_algebra) borel_measurable_halfspacesI:
   722   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   723   assumes "borel = (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   724   and "\<And>a i. S a i = f -` F (a,i) \<inter> space M"
   725   and "\<And>a i. \<not> i < DIM('c) \<Longrightarrow> S a i \<in> sets M"
   726   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a::real. S a i \<in> sets M)"
   727 proof safe
   728   fix a :: real and i assume i: "i < DIM('c)" and f: "f \<in> borel_measurable M"
   729   then show "S a i \<in> sets M" unfolding assms
   730     by (auto intro!: measurable_sets sigma_sets.Basic simp: assms(1) sigma_def)
   731 next
   732   assume a: "\<forall>i<DIM('c). \<forall>a. S a i \<in> sets M"
   733   { fix a i have "S a i \<in> sets M"
   734     proof cases
   735       assume "i < DIM('c)"
   736       with a show ?thesis unfolding assms(2) by simp
   737     next
   738       assume "\<not> i < DIM('c)"
   739       from assms(3)[OF this] show ?thesis .
   740     qed }
   741   then have "f \<in> measurable M (sigma \<lparr>space=UNIV, sets=range F\<rparr>)"
   742     by (auto intro!: measurable_sigma simp: assms(2))
   743   then show "f \<in> borel_measurable M" unfolding measurable_def
   744     unfolding assms(1) by simp
   745 qed
   746 
   747 lemma (in sigma_algebra) borel_measurable_iff_halfspace_le:
   748   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   749   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i \<le> a} \<in> sets M)"
   750   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_le]) auto
   751 
   752 lemma (in sigma_algebra) borel_measurable_iff_halfspace_less:
   753   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   754   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. f w $$ i < a} \<in> sets M)"
   755   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_less]) auto
   756 
   757 lemma (in sigma_algebra) borel_measurable_iff_halfspace_ge:
   758   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   759   shows "f \<in> borel_measurable M = (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a \<le> f w $$ i} \<in> sets M)"
   760   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_ge]) auto
   761 
   762 lemma (in sigma_algebra) borel_measurable_iff_halfspace_greater:
   763   fixes f :: "'a \<Rightarrow> 'c\<Colon>ordered_euclidean_space"
   764   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). \<forall>a. {w \<in> space M. a < f w $$ i} \<in> sets M)"
   765   by (rule borel_measurable_halfspacesI[OF borel_eq_halfspace_gt]) auto
   766 
   767 lemma (in sigma_algebra) borel_measurable_iff_le:
   768   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w \<le> a} \<in> sets M)"
   769   using borel_measurable_iff_halfspace_le[where 'c=real] by simp
   770 
   771 lemma (in sigma_algebra) borel_measurable_iff_less:
   772   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. f w < a} \<in> sets M)"
   773   using borel_measurable_iff_halfspace_less[where 'c=real] by simp
   774 
   775 lemma (in sigma_algebra) borel_measurable_iff_ge:
   776   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a \<le> f w} \<in> sets M)"
   777   using borel_measurable_iff_halfspace_ge[where 'c=real] by simp
   778 
   779 lemma (in sigma_algebra) borel_measurable_iff_greater:
   780   "(f::'a \<Rightarrow> real) \<in> borel_measurable M = (\<forall>a. {w \<in> space M. a < f w} \<in> sets M)"
   781   using borel_measurable_iff_halfspace_greater[where 'c=real] by simp
   782 
   783 lemma borel_measurable_euclidean_component:
   784   "(\<lambda>x::'a::euclidean_space. x $$ i) \<in> borel_measurable borel"
   785   unfolding borel_def[where 'a=real]
   786 proof (rule borel.measurable_sigma, simp_all)
   787   fix S::"real set" assume "S \<in> open" then have "open S" unfolding mem_def .
   788   from open_vimage_euclidean_component[OF this]
   789   show "(\<lambda>x. x $$ i) -` S \<in> sets borel"
   790     by (auto intro: borel_open)
   791 qed
   792 
   793 lemma (in sigma_algebra) borel_measurable_euclidean_space:
   794   fixes f :: "'a \<Rightarrow> 'c::ordered_euclidean_space"
   795   shows "f \<in> borel_measurable M \<longleftrightarrow> (\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M)"
   796 proof safe
   797   fix i assume "f \<in> borel_measurable M"
   798   then show "(\<lambda>x. f x $$ i) \<in> borel_measurable M"
   799     using measurable_comp[of f _ _ "\<lambda>x. x $$ i", unfolded comp_def]
   800     by (auto intro: borel_measurable_euclidean_component)
   801 next
   802   assume f: "\<forall>i<DIM('c). (\<lambda>x. f x $$ i) \<in> borel_measurable M"
   803   then show "f \<in> borel_measurable M"
   804     unfolding borel_measurable_iff_halfspace_le by auto
   805 qed
   806 
   807 subsection "Borel measurable operators"
   808 
   809 lemma (in sigma_algebra) affine_borel_measurable_vector:
   810   fixes f :: "'a \<Rightarrow> 'x::real_normed_vector"
   811   assumes "f \<in> borel_measurable M"
   812   shows "(\<lambda>x. a + b *\<^sub>R f x) \<in> borel_measurable M"
   813 proof (rule borel_measurableI)
   814   fix S :: "'x set" assume "open S"
   815   show "(\<lambda>x. a + b *\<^sub>R f x) -` S \<inter> space M \<in> sets M"
   816   proof cases
   817     assume "b \<noteq> 0"
   818     with `open S` have "((\<lambda>x. (- a + x) /\<^sub>R b) ` S) \<in> open" (is "?S \<in> open")
   819       by (auto intro!: open_affinity simp: scaleR.add_right mem_def)
   820     hence "?S \<in> sets borel"
   821       unfolding borel_def by (auto simp: sigma_def intro!: sigma_sets.Basic)
   822     moreover
   823     from `b \<noteq> 0` have "(\<lambda>x. a + b *\<^sub>R f x) -` S = f -` ?S"
   824       apply auto by (rule_tac x="a + b *\<^sub>R f x" in image_eqI, simp_all)
   825     ultimately show ?thesis using assms unfolding in_borel_measurable_borel
   826       by auto
   827   qed simp
   828 qed
   829 
   830 lemma (in sigma_algebra) affine_borel_measurable:
   831   fixes g :: "'a \<Rightarrow> real"
   832   assumes g: "g \<in> borel_measurable M"
   833   shows "(\<lambda>x. a + (g x) * b) \<in> borel_measurable M"
   834   using affine_borel_measurable_vector[OF assms] by (simp add: mult_commute)
   835 
   836 lemma (in sigma_algebra) borel_measurable_add[simp, intro]:
   837   fixes f :: "'a \<Rightarrow> real"
   838   assumes f: "f \<in> borel_measurable M"
   839   assumes g: "g \<in> borel_measurable M"
   840   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
   841 proof -
   842   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}"
   843     by auto
   844   have "\<And>a. (\<lambda>w. a + (g w) * -1) \<in> borel_measurable M"
   845     by (rule affine_borel_measurable [OF g])
   846   then have "\<And>a. {w \<in> space M. (\<lambda>w. a + (g w) * -1)(w) \<le> f w} \<in> sets M" using f
   847     by auto
   848   then have "\<And>a. {w \<in> space M. a \<le> f w + g w} \<in> sets M"
   849     by (simp add: 1)
   850   then show ?thesis
   851     by (simp add: borel_measurable_iff_ge)
   852 qed
   853 
   854 lemma (in sigma_algebra) borel_measurable_setsum[simp, intro]:
   855   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   856   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   857   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
   858 proof cases
   859   assume "finite S"
   860   thus ?thesis using assms by induct auto
   861 qed simp
   862 
   863 lemma (in sigma_algebra) borel_measurable_square:
   864   fixes f :: "'a \<Rightarrow> real"
   865   assumes f: "f \<in> borel_measurable M"
   866   shows "(\<lambda>x. (f x)^2) \<in> borel_measurable M"
   867 proof -
   868   {
   869     fix a
   870     have "{w \<in> space M. (f w)\<twosuperior> \<le> a} \<in> sets M"
   871     proof (cases rule: linorder_cases [of a 0])
   872       case less
   873       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} = {}"
   874         by auto (metis less order_le_less_trans power2_less_0)
   875       also have "... \<in> sets M"
   876         by (rule empty_sets)
   877       finally show ?thesis .
   878     next
   879       case equal
   880       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   881              {w \<in> space M. f w \<le> 0} \<inter> {w \<in> space M. 0 \<le> f w}"
   882         by auto
   883       also have "... \<in> sets M"
   884         apply (insert f)
   885         apply (rule Int)
   886         apply (simp add: borel_measurable_iff_le)
   887         apply (simp add: borel_measurable_iff_ge)
   888         done
   889       finally show ?thesis .
   890     next
   891       case greater
   892       have "\<forall>x. (f x ^ 2 \<le> sqrt a ^ 2) = (- sqrt a  \<le> f x & f x \<le> sqrt a)"
   893         by (metis abs_le_interval_iff abs_of_pos greater real_sqrt_abs
   894                   real_sqrt_le_iff real_sqrt_power)
   895       hence "{w \<in> space M. (f w)\<twosuperior> \<le> a} =
   896              {w \<in> space M. -(sqrt a) \<le> f w} \<inter> {w \<in> space M. f w \<le> sqrt a}"
   897         using greater by auto
   898       also have "... \<in> sets M"
   899         apply (insert f)
   900         apply (rule Int)
   901         apply (simp add: borel_measurable_iff_ge)
   902         apply (simp add: borel_measurable_iff_le)
   903         done
   904       finally show ?thesis .
   905     qed
   906   }
   907   thus ?thesis by (auto simp add: borel_measurable_iff_le)
   908 qed
   909 
   910 lemma times_eq_sum_squares:
   911    fixes x::real
   912    shows"x*y = ((x+y)^2)/4 - ((x-y)^ 2)/4"
   913 by (simp add: power2_eq_square ring_distribs diff_divide_distrib [symmetric])
   914 
   915 lemma (in sigma_algebra) borel_measurable_uminus[simp, intro]:
   916   fixes g :: "'a \<Rightarrow> real"
   917   assumes g: "g \<in> borel_measurable M"
   918   shows "(\<lambda>x. - g x) \<in> borel_measurable M"
   919 proof -
   920   have "(\<lambda>x. - g x) = (\<lambda>x. 0 + (g x) * -1)"
   921     by simp
   922   also have "... \<in> borel_measurable M"
   923     by (fast intro: affine_borel_measurable g)
   924   finally show ?thesis .
   925 qed
   926 
   927 lemma (in sigma_algebra) borel_measurable_times[simp, intro]:
   928   fixes f :: "'a \<Rightarrow> real"
   929   assumes f: "f \<in> borel_measurable M"
   930   assumes g: "g \<in> borel_measurable M"
   931   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
   932 proof -
   933   have 1: "(\<lambda>x. 0 + (f x + g x)\<twosuperior> * inverse 4) \<in> borel_measurable M"
   934     using assms by (fast intro: affine_borel_measurable borel_measurable_square)
   935   have "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) =
   936         (\<lambda>x. 0 + ((f x + -g x) ^ 2 * inverse -4))"
   937     by (simp add: minus_divide_right)
   938   also have "... \<in> borel_measurable M"
   939     using f g by (fast intro: affine_borel_measurable borel_measurable_square f g)
   940   finally have 2: "(\<lambda>x. -((f x + -g x) ^ 2 * inverse 4)) \<in> borel_measurable M" .
   941   show ?thesis
   942     apply (simp add: times_eq_sum_squares diff_minus)
   943     using 1 2 by simp
   944 qed
   945 
   946 lemma (in sigma_algebra) borel_measurable_setprod[simp, intro]:
   947   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> real"
   948   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
   949   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
   950 proof cases
   951   assume "finite S"
   952   thus ?thesis using assms by induct auto
   953 qed simp
   954 
   955 lemma (in sigma_algebra) borel_measurable_diff[simp, intro]:
   956   fixes f :: "'a \<Rightarrow> real"
   957   assumes f: "f \<in> borel_measurable M"
   958   assumes g: "g \<in> borel_measurable M"
   959   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
   960   unfolding diff_minus using assms by fast
   961 
   962 lemma (in sigma_algebra) borel_measurable_inverse[simp, intro]:
   963   fixes f :: "'a \<Rightarrow> real"
   964   assumes "f \<in> borel_measurable M"
   965   shows "(\<lambda>x. inverse (f x)) \<in> borel_measurable M"
   966   unfolding borel_measurable_iff_ge unfolding inverse_eq_divide
   967 proof safe
   968   fix a :: real
   969   have *: "{w \<in> space M. a \<le> 1 / f w} =
   970       ({w \<in> space M. 0 < f w} \<inter> {w \<in> space M. a * f w \<le> 1}) \<union>
   971       ({w \<in> space M. f w < 0} \<inter> {w \<in> space M. 1 \<le> a * f w}) \<union>
   972       ({w \<in> space M. f w = 0} \<inter> {w \<in> space M. a \<le> 0})" by (auto simp: le_divide_eq)
   973   show "{w \<in> space M. a \<le> 1 / f w} \<in> sets M" using assms unfolding *
   974     by (auto intro!: Int Un)
   975 qed
   976 
   977 lemma (in sigma_algebra) borel_measurable_divide[simp, intro]:
   978   fixes f :: "'a \<Rightarrow> real"
   979   assumes "f \<in> borel_measurable M"
   980   and "g \<in> borel_measurable M"
   981   shows "(\<lambda>x. f x / g x) \<in> borel_measurable M"
   982   unfolding field_divide_inverse
   983   by (rule borel_measurable_inverse borel_measurable_times assms)+
   984 
   985 lemma (in sigma_algebra) borel_measurable_max[intro, simp]:
   986   fixes f g :: "'a \<Rightarrow> real"
   987   assumes "f \<in> borel_measurable M"
   988   assumes "g \<in> borel_measurable M"
   989   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
   990   unfolding borel_measurable_iff_le
   991 proof safe
   992   fix a
   993   have "{x \<in> space M. max (g x) (f x) \<le> a} =
   994     {x \<in> space M. g x \<le> a} \<inter> {x \<in> space M. f x \<le> a}" by auto
   995   thus "{x \<in> space M. max (g x) (f x) \<le> a} \<in> sets M"
   996     using assms unfolding borel_measurable_iff_le
   997     by (auto intro!: Int)
   998 qed
   999 
  1000 lemma (in sigma_algebra) borel_measurable_min[intro, simp]:
  1001   fixes f g :: "'a \<Rightarrow> real"
  1002   assumes "f \<in> borel_measurable M"
  1003   assumes "g \<in> borel_measurable M"
  1004   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1005   unfolding borel_measurable_iff_ge
  1006 proof safe
  1007   fix a
  1008   have "{x \<in> space M. a \<le> min (g x) (f x)} =
  1009     {x \<in> space M. a \<le> g x} \<inter> {x \<in> space M. a \<le> f x}" by auto
  1010   thus "{x \<in> space M. a \<le> min (g x) (f x)} \<in> sets M"
  1011     using assms unfolding borel_measurable_iff_ge
  1012     by (auto intro!: Int)
  1013 qed
  1014 
  1015 lemma (in sigma_algebra) borel_measurable_abs[simp, intro]:
  1016   assumes "f \<in> borel_measurable M"
  1017   shows "(\<lambda>x. \<bar>f x :: real\<bar>) \<in> borel_measurable M"
  1018 proof -
  1019   have *: "\<And>x. \<bar>f x\<bar> = max 0 (f x) + max 0 (- f x)" by (simp add: max_def)
  1020   show ?thesis unfolding * using assms by auto
  1021 qed
  1022 
  1023 lemma borel_measurable_nth[simp, intro]:
  1024   "(\<lambda>x::real^'n. x $ i) \<in> borel_measurable borel"
  1025   using borel_measurable_euclidean_component
  1026   unfolding nth_conv_component by auto
  1027 
  1028 lemma borel_measurable_continuous_on1:
  1029   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
  1030   assumes "continuous_on UNIV f"
  1031   shows "f \<in> borel_measurable borel"
  1032   apply(rule borel.borel_measurableI)
  1033   using continuous_open_preimage[OF assms] unfolding vimage_def by auto
  1034 
  1035 lemma borel_measurable_continuous_on:
  1036   fixes f :: "'a::topological_space \<Rightarrow> 'b::t1_space"
  1037   assumes cont: "continuous_on A f" "open A"
  1038   shows "(\<lambda>x. if x \<in> A then f x else c) \<in> borel_measurable borel" (is "?f \<in> _")
  1039 proof (rule borel.borel_measurableI)
  1040   fix S :: "'b set" assume "open S"
  1041   then have "open {x\<in>A. f x \<in> S}"
  1042     by (intro continuous_open_preimage[OF cont]) auto
  1043   then have *: "{x\<in>A. f x \<in> S} \<in> sets borel" by auto
  1044   have "?f -` S \<inter> space borel = 
  1045     {x\<in>A. f x \<in> S} \<union> (if c \<in> S then space borel - A else {})"
  1046     by (auto split: split_if_asm)
  1047   also have "\<dots> \<in> sets borel"
  1048     using * `open A` by (auto simp del: space_borel intro!: borel.Un)
  1049   finally show "?f -` S \<inter> space borel \<in> sets borel" .
  1050 qed
  1051 
  1052 lemma (in sigma_algebra) convex_measurable:
  1053   fixes a b :: real
  1054   assumes X: "X \<in> borel_measurable M" "X ` space M \<subseteq> { a <..< b}"
  1055   assumes q: "convex_on { a <..< b} q"
  1056   shows "q \<circ> X \<in> borel_measurable M"
  1057 proof -
  1058   have "(\<lambda>x. if x \<in> {a <..< b} then q x else 0) \<in> borel_measurable borel"
  1059   proof (rule borel_measurable_continuous_on)
  1060     show "open {a<..<b}" by auto
  1061     from this q show "continuous_on {a<..<b} q"
  1062       by (rule convex_on_continuous)
  1063   qed
  1064   then have "(\<lambda>x. if x \<in> {a <..< b} then q x else 0) \<circ> X \<in> borel_measurable M" (is ?qX)
  1065     using X by (intro measurable_comp) auto
  1066   moreover have "?qX \<longleftrightarrow> q \<circ> X \<in> borel_measurable M"
  1067     using X by (intro measurable_cong) auto
  1068   ultimately show ?thesis by simp
  1069 qed
  1070 
  1071 lemma borel_measurable_borel_log: assumes "1 < b" shows "log b \<in> borel_measurable borel"
  1072 proof -
  1073   { fix x :: real assume x: "x \<le> 0"
  1074     { fix x::real assume "x \<le> 0" then have "\<And>u. exp u = x \<longleftrightarrow> False" by auto }
  1075     from this[of x] x this[of 0] have "log b 0 = log b x"
  1076       by (auto simp: ln_def log_def) }
  1077   note log_imp = this
  1078   have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) \<in> borel_measurable borel"
  1079   proof (rule borel_measurable_continuous_on)
  1080     show "continuous_on {0<..} (log b)"
  1081       by (auto intro!: continuous_at_imp_continuous_on DERIV_log DERIV_isCont
  1082                simp: continuous_isCont[symmetric])
  1083     show "open ({0<..}::real set)" by auto
  1084   qed
  1085   also have "(\<lambda>x. if x \<in> {0<..} then log b x else log b 0) = log b"
  1086     by (simp add: fun_eq_iff not_less log_imp)
  1087   finally show ?thesis .
  1088 qed
  1089 
  1090 lemma (in sigma_algebra) borel_measurable_log[simp,intro]:
  1091   assumes f: "f \<in> borel_measurable M" and "1 < b"
  1092   shows "(\<lambda>x. log b (f x)) \<in> borel_measurable M"
  1093   using measurable_comp[OF f borel_measurable_borel_log[OF `1 < b`]]
  1094   by (simp add: comp_def)
  1095 
  1096 subsection "Borel space on the extended reals"
  1097 
  1098 lemma borel_measurable_extreal_borel:
  1099   "extreal \<in> borel_measurable borel"
  1100   unfolding borel_def[where 'a=extreal]
  1101 proof (rule borel.measurable_sigma)
  1102   fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
  1103   then have "open X" by (auto simp: mem_def)
  1104   then have "open (extreal -` X \<inter> space borel)"
  1105     by (simp add: open_extreal_vimage)
  1106   then show "extreal -` X \<inter> space borel \<in> sets borel" by auto
  1107 qed auto
  1108 
  1109 lemma (in sigma_algebra) borel_measurable_extreal[simp, intro]:
  1110   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
  1111   using measurable_comp[OF f borel_measurable_extreal_borel] unfolding comp_def .
  1112 
  1113 lemma borel_measurable_real_of_extreal_borel:
  1114   "(real :: extreal \<Rightarrow> real) \<in> borel_measurable borel"
  1115   unfolding borel_def[where 'a=real]
  1116 proof (rule borel.measurable_sigma)
  1117   fix B :: "real set" assume "B \<in> sets \<lparr>space = UNIV, sets = open \<rparr>"
  1118   then have "open B" by (auto simp: mem_def)
  1119   have *: "extreal -` real -` (B - {0}) = B - {0}" by auto
  1120   have open_real: "open (real -` (B - {0}) :: extreal set)"
  1121     unfolding open_extreal_def * using `open B` by auto
  1122   show "(real -` B \<inter> space borel :: extreal set) \<in> sets borel"
  1123   proof cases
  1124     assume "0 \<in> B"
  1125     then have *: "real -` B = real -` (B - {0}) \<union> {-\<infinity>, \<infinity>, 0}"
  1126       by (auto simp add: real_of_extreal_eq_0)
  1127     then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
  1128       using open_real by auto
  1129   next
  1130     assume "0 \<notin> B"
  1131     then have *: "(real -` B :: extreal set) = real -` (B - {0})"
  1132       by (auto simp add: real_of_extreal_eq_0)
  1133     then show "(real -` B :: extreal set) \<inter> space borel \<in> sets borel"
  1134       using open_real by auto
  1135   qed
  1136 qed auto
  1137 
  1138 lemma (in sigma_algebra) borel_measurable_real_of_extreal[simp, intro]:
  1139   assumes f: "f \<in> borel_measurable M" shows "(\<lambda>x. real (f x :: extreal)) \<in> borel_measurable M"
  1140   using measurable_comp[OF f borel_measurable_real_of_extreal_borel] unfolding comp_def .
  1141 
  1142 lemma (in sigma_algebra) borel_measurable_extreal_iff:
  1143   shows "(\<lambda>x. extreal (f x)) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M"
  1144 proof
  1145   assume "(\<lambda>x. extreal (f x)) \<in> borel_measurable M"
  1146   from borel_measurable_real_of_extreal[OF this]
  1147   show "f \<in> borel_measurable M" by auto
  1148 qed auto
  1149 
  1150 lemma (in sigma_algebra) borel_measurable_extreal_iff_real:
  1151   "f \<in> borel_measurable M \<longleftrightarrow>
  1152     ((\<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)"
  1153 proof safe
  1154   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"
  1155   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
  1156   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
  1157   let "?f x" = "if f x = \<infinity> then \<infinity> else if f x = -\<infinity> then -\<infinity> else extreal (real (f x))"
  1158   have "?f \<in> borel_measurable M" using * ** by (intro measurable_If) auto
  1159   also have "?f = f" by (auto simp: fun_eq_iff extreal_real)
  1160   finally show "f \<in> borel_measurable M" .
  1161 qed (auto intro: measurable_sets borel_measurable_real_of_extreal)
  1162 
  1163 lemma (in sigma_algebra) less_eq_ge_measurable:
  1164   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1165   shows "f -` {a <..} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {..a} \<inter> space M \<in> sets M"
  1166 proof
  1167   assume "f -` {a <..} \<inter> space M \<in> sets M"
  1168   moreover have "f -` {..a} \<inter> space M = space M - f -` {a <..} \<inter> space M" by auto
  1169   ultimately show "f -` {..a} \<inter> space M \<in> sets M" by auto
  1170 next
  1171   assume "f -` {..a} \<inter> space M \<in> sets M"
  1172   moreover have "f -` {a <..} \<inter> space M = space M - f -` {..a} \<inter> space M" by auto
  1173   ultimately show "f -` {a <..} \<inter> space M \<in> sets M" by auto
  1174 qed
  1175 
  1176 lemma (in sigma_algebra) greater_eq_le_measurable:
  1177   fixes f :: "'a \<Rightarrow> 'c::linorder"
  1178   shows "f -` {..< a} \<inter> space M \<in> sets M \<longleftrightarrow> f -` {a ..} \<inter> space M \<in> sets M"
  1179 proof
  1180   assume "f -` {a ..} \<inter> space M \<in> sets M"
  1181   moreover have "f -` {..< a} \<inter> space M = space M - f -` {a ..} \<inter> space M" by auto
  1182   ultimately show "f -` {..< a} \<inter> space M \<in> sets M" by auto
  1183 next
  1184   assume "f -` {..< a} \<inter> space M \<in> sets M"
  1185   moreover have "f -` {a ..} \<inter> space M = space M - f -` {..< a} \<inter> space M" by auto
  1186   ultimately show "f -` {a ..} \<inter> space M \<in> sets M" by auto
  1187 qed
  1188 
  1189 lemma (in sigma_algebra) borel_measurable_uminus_borel_extreal:
  1190   "(uminus :: extreal \<Rightarrow> extreal) \<in> borel_measurable borel"
  1191 proof (subst borel_def, rule borel.measurable_sigma)
  1192   fix X :: "extreal set" assume "X \<in> sets \<lparr>space = UNIV, sets = open\<rparr>"
  1193   then have "open X" by (simp add: mem_def)
  1194   have "uminus -` X = uminus ` X" by (force simp: image_iff)
  1195   then have "open (uminus -` X)" using `open X` extreal_open_uminus by auto
  1196   then show "uminus -` X \<inter> space borel \<in> sets borel" by auto
  1197 qed auto
  1198 
  1199 lemma (in sigma_algebra) borel_measurable_uminus_extreal[intro]:
  1200   assumes "f \<in> borel_measurable M"
  1201   shows "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M"
  1202   using measurable_comp[OF assms borel_measurable_uminus_borel_extreal] by (simp add: comp_def)
  1203 
  1204 lemma (in sigma_algebra) borel_measurable_uminus_eq_extreal[simp]:
  1205   "(\<lambda>x. - f x :: extreal) \<in> borel_measurable M \<longleftrightarrow> f \<in> borel_measurable M" (is "?l = ?r")
  1206 proof
  1207   assume ?l from borel_measurable_uminus_extreal[OF this] show ?r by simp
  1208 qed auto
  1209 
  1210 lemma (in sigma_algebra) borel_measurable_eq_atMost_extreal:
  1211   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..a} \<inter> space M \<in> sets M)"
  1212 proof (intro iffI allI)
  1213   assume pos[rule_format]: "\<forall>a. f -` {..a} \<inter> space M \<in> sets M"
  1214   show "f \<in> borel_measurable M"
  1215     unfolding borel_measurable_extreal_iff_real borel_measurable_iff_le
  1216   proof (intro conjI allI)
  1217     fix a :: real
  1218     { fix x :: extreal assume *: "\<forall>i::nat. real i < x"
  1219       have "x = \<infinity>"
  1220       proof (rule extreal_top)
  1221         fix B from real_arch_lt[of B] guess n ..
  1222         then have "extreal B < real n" by auto
  1223         with * show "B \<le> x" by (metis less_trans less_imp_le)
  1224       qed }
  1225     then have "f -` {\<infinity>} \<inter> space M = space M - (\<Union>i::nat. f -` {.. real i} \<inter> space M)"
  1226       by (auto simp: not_le)
  1227     then show "f -` {\<infinity>} \<inter> space M \<in> sets M" using pos by (auto simp del: UN_simps intro!: Diff)
  1228     moreover
  1229     have "{-\<infinity>} = {..-\<infinity>}" by auto
  1230     then show "f -` {-\<infinity>} \<inter> space M \<in> sets M" using pos by auto
  1231     moreover have "{x\<in>space M. f x \<le> extreal a} \<in> sets M"
  1232       using pos[of "extreal a"] by (simp add: vimage_def Int_def conj_commute)
  1233     moreover have "{w \<in> space M. real (f w) \<le> a} =
  1234       (if a < 0 then {w \<in> space M. f w \<le> extreal a} - f -` {-\<infinity>} \<inter> space M
  1235       else {w \<in> space M. f w \<le> extreal a} \<union> (f -` {\<infinity>} \<inter> space M) \<union> (f -` {-\<infinity>} \<inter> space M))" (is "?l = ?r")
  1236       proof (intro set_eqI) fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases "f x") auto qed
  1237     ultimately show "{w \<in> space M. real (f w) \<le> a} \<in> sets M" by auto
  1238   qed
  1239 qed (simp add: measurable_sets)
  1240 
  1241 lemma (in sigma_algebra) borel_measurable_eq_atLeast_extreal:
  1242   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a..} \<inter> space M \<in> sets M)"
  1243 proof
  1244   assume pos: "\<forall>a. f -` {a..} \<inter> space M \<in> sets M"
  1245   moreover have "\<And>a. (\<lambda>x. - f x) -` {..a} = f -` {-a ..}"
  1246     by (auto simp: extreal_uminus_le_reorder)
  1247   ultimately have "(\<lambda>x. - f x) \<in> borel_measurable M"
  1248     unfolding borel_measurable_eq_atMost_extreal by auto
  1249   then show "f \<in> borel_measurable M" by simp
  1250 qed (simp add: measurable_sets)
  1251 
  1252 lemma (in sigma_algebra) borel_measurable_extreal_iff_less:
  1253   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {..< a} \<inter> space M \<in> sets M)"
  1254   unfolding borel_measurable_eq_atLeast_extreal greater_eq_le_measurable ..
  1255 
  1256 lemma (in sigma_algebra) borel_measurable_extreal_iff_ge:
  1257   "(f::'a \<Rightarrow> extreal) \<in> borel_measurable M \<longleftrightarrow> (\<forall>a. f -` {a <..} \<inter> space M \<in> sets M)"
  1258   unfolding borel_measurable_eq_atMost_extreal less_eq_ge_measurable ..
  1259 
  1260 lemma (in sigma_algebra) borel_measurable_extreal_eq_const:
  1261   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
  1262   shows "{x\<in>space M. f x = c} \<in> sets M"
  1263 proof -
  1264   have "{x\<in>space M. f x = c} = (f -` {c} \<inter> space M)" by auto
  1265   then show ?thesis using assms by (auto intro!: measurable_sets)
  1266 qed
  1267 
  1268 lemma (in sigma_algebra) borel_measurable_extreal_neq_const:
  1269   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
  1270   shows "{x\<in>space M. f x \<noteq> c} \<in> sets M"
  1271 proof -
  1272   have "{x\<in>space M. f x \<noteq> c} = space M - (f -` {c} \<inter> space M)" by auto
  1273   then show ?thesis using assms by (auto intro!: measurable_sets)
  1274 qed
  1275 
  1276 lemma (in sigma_algebra) borel_measurable_extreal_le[intro,simp]:
  1277   fixes f g :: "'a \<Rightarrow> extreal"
  1278   assumes f: "f \<in> borel_measurable M"
  1279   assumes g: "g \<in> borel_measurable M"
  1280   shows "{x \<in> space M. f x \<le> g x} \<in> sets M"
  1281 proof -
  1282   have "{x \<in> space M. f x \<le> g x} =
  1283     {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>
  1284     f -` {-\<infinity>} \<inter> space M \<union> g -` {\<infinity>} \<inter> space M" (is "?l = ?r")
  1285   proof (intro set_eqI)
  1286     fix x show "x \<in> ?l \<longleftrightarrow> x \<in> ?r" by (cases rule: extreal2_cases[of "f x" "g x"]) auto
  1287   qed
  1288   with f g show ?thesis by (auto intro!: Un simp: measurable_sets)
  1289 qed
  1290 
  1291 lemma (in sigma_algebra) borel_measurable_extreal_less[intro,simp]:
  1292   fixes f :: "'a \<Rightarrow> extreal"
  1293   assumes f: "f \<in> borel_measurable M"
  1294   assumes g: "g \<in> borel_measurable M"
  1295   shows "{x \<in> space M. f x < g x} \<in> sets M"
  1296 proof -
  1297   have "{x \<in> space M. f x < g x} = space M - {x \<in> space M. g x \<le> f x}" by auto
  1298   then show ?thesis using g f by auto
  1299 qed
  1300 
  1301 lemma (in sigma_algebra) borel_measurable_extreal_eq[intro,simp]:
  1302   fixes f :: "'a \<Rightarrow> extreal"
  1303   assumes f: "f \<in> borel_measurable M"
  1304   assumes g: "g \<in> borel_measurable M"
  1305   shows "{w \<in> space M. f w = g w} \<in> sets M"
  1306 proof -
  1307   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
  1308   then show ?thesis using g f by auto
  1309 qed
  1310 
  1311 lemma (in sigma_algebra) borel_measurable_extreal_neq[intro,simp]:
  1312   fixes f :: "'a \<Rightarrow> extreal"
  1313   assumes f: "f \<in> borel_measurable M"
  1314   assumes g: "g \<in> borel_measurable M"
  1315   shows "{w \<in> space M. f w \<noteq> g w} \<in> sets M"
  1316 proof -
  1317   have "{w \<in> space M. f w \<noteq> g w} = space M - {w \<in> space M. f w = g w}" by auto
  1318   thus ?thesis using f g by auto
  1319 qed
  1320 
  1321 lemma (in sigma_algebra) split_sets:
  1322   "{x\<in>space M. P x \<or> Q x} = {x\<in>space M. P x} \<union> {x\<in>space M. Q x}"
  1323   "{x\<in>space M. P x \<and> Q x} = {x\<in>space M. P x} \<inter> {x\<in>space M. Q x}"
  1324   by auto
  1325 
  1326 lemma (in sigma_algebra) borel_measurable_extreal_add[intro, simp]:
  1327   fixes f :: "'a \<Rightarrow> extreal"
  1328   assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1329   shows "(\<lambda>x. f x + g x) \<in> borel_measurable M"
  1330 proof -
  1331   { fix x assume "x \<in> space M" then have "f x + g x =
  1332       (if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
  1333         else if f x = -\<infinity> \<or> g x = -\<infinity> then -\<infinity>
  1334         else extreal (real (f x) + real (g x)))"
  1335       by (cases rule: extreal2_cases[of "f x" "g x"]) auto }
  1336   with assms show ?thesis
  1337     by (auto cong: measurable_cong simp: split_sets
  1338              intro!: Un measurable_If measurable_sets)
  1339 qed
  1340 
  1341 lemma (in sigma_algebra) borel_measurable_extreal_setsum[simp, intro]:
  1342   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
  1343   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1344   shows "(\<lambda>x. \<Sum>i\<in>S. f i x) \<in> borel_measurable M"
  1345 proof cases
  1346   assume "finite S"
  1347   thus ?thesis using assms
  1348     by induct auto
  1349 qed (simp add: borel_measurable_const)
  1350 
  1351 lemma (in sigma_algebra) borel_measurable_extreal_abs[intro, simp]:
  1352   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M"
  1353   shows "(\<lambda>x. \<bar>f x\<bar>) \<in> borel_measurable M"
  1354 proof -
  1355   { fix x have "\<bar>f x\<bar> = (if 0 \<le> f x then f x else - f x)" by auto }
  1356   then show ?thesis using assms by (auto intro!: measurable_If)
  1357 qed
  1358 
  1359 lemma (in sigma_algebra) borel_measurable_extreal_times[intro, simp]:
  1360   fixes f :: "'a \<Rightarrow> extreal" assumes "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1361   shows "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1362 proof -
  1363   { fix f g :: "'a \<Rightarrow> extreal"
  1364     assume b: "f \<in> borel_measurable M" "g \<in> borel_measurable M"
  1365       and pos: "\<And>x. 0 \<le> f x" "\<And>x. 0 \<le> g x"
  1366     { fix x have *: "f x * g x = (if f x = 0 \<or> g x = 0 then 0
  1367         else if f x = \<infinity> \<or> g x = \<infinity> then \<infinity>
  1368         else extreal (real (f x) * real (g x)))"
  1369       apply (cases rule: extreal2_cases[of "f x" "g x"])
  1370       using pos[of x] by auto }
  1371     with b have "(\<lambda>x. f x * g x) \<in> borel_measurable M"
  1372       by (auto cong: measurable_cong simp: split_sets
  1373                intro!: Un measurable_If measurable_sets) }
  1374   note pos_times = this
  1375   have *: "(\<lambda>x. f x * g x) =
  1376     (\<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>))"
  1377     by (auto simp: fun_eq_iff)
  1378   show ?thesis using assms unfolding *
  1379     by (intro measurable_If pos_times borel_measurable_uminus_extreal)
  1380        (auto simp: split_sets intro!: Int)
  1381 qed
  1382 
  1383 lemma (in sigma_algebra) borel_measurable_extreal_setprod[simp, intro]:
  1384   fixes f :: "'c \<Rightarrow> 'a \<Rightarrow> extreal"
  1385   assumes "\<And>i. i \<in> S \<Longrightarrow> f i \<in> borel_measurable M"
  1386   shows "(\<lambda>x. \<Prod>i\<in>S. f i x) \<in> borel_measurable M"
  1387 proof cases
  1388   assume "finite S"
  1389   thus ?thesis using assms by induct auto
  1390 qed simp
  1391 
  1392 lemma (in sigma_algebra) borel_measurable_extreal_min[simp, intro]:
  1393   fixes f g :: "'a \<Rightarrow> extreal"
  1394   assumes "f \<in> borel_measurable M"
  1395   assumes "g \<in> borel_measurable M"
  1396   shows "(\<lambda>x. min (g x) (f x)) \<in> borel_measurable M"
  1397   using assms unfolding min_def by (auto intro!: measurable_If)
  1398 
  1399 lemma (in sigma_algebra) borel_measurable_extreal_max[simp, intro]:
  1400   fixes f g :: "'a \<Rightarrow> extreal"
  1401   assumes "f \<in> borel_measurable M"
  1402   and "g \<in> borel_measurable M"
  1403   shows "(\<lambda>x. max (g x) (f x)) \<in> borel_measurable M"
  1404   using assms unfolding max_def by (auto intro!: measurable_If)
  1405 
  1406 lemma (in sigma_algebra) borel_measurable_SUP[simp, intro]:
  1407   fixes f :: "'d\<Colon>countable \<Rightarrow> 'a \<Rightarrow> extreal"
  1408   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1409   shows "(\<lambda>x. SUP i : A. f i x) \<in> borel_measurable M" (is "?sup \<in> borel_measurable M")
  1410   unfolding borel_measurable_extreal_iff_ge
  1411 proof
  1412   fix a
  1413   have "?sup -` {a<..} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. a < f i x})"
  1414     by (auto simp: less_SUP_iff SUPR_apply)
  1415   then show "?sup -` {a<..} \<inter> space M \<in> sets M"
  1416     using assms by auto
  1417 qed
  1418 
  1419 lemma (in sigma_algebra) borel_measurable_INF[simp, intro]:
  1420   fixes f :: "'d :: countable \<Rightarrow> 'a \<Rightarrow> extreal"
  1421   assumes "\<And>i. i \<in> A \<Longrightarrow> f i \<in> borel_measurable M"
  1422   shows "(\<lambda>x. INF i : A. f i x) \<in> borel_measurable M" (is "?inf \<in> borel_measurable M")
  1423   unfolding borel_measurable_extreal_iff_less
  1424 proof
  1425   fix a
  1426   have "?inf -` {..<a} \<inter> space M = (\<Union>i\<in>A. {x\<in>space M. f i x < a})"
  1427     by (auto simp: INF_less_iff INFI_apply)
  1428   then show "?inf -` {..<a} \<inter> space M \<in> sets M"
  1429     using assms by auto
  1430 qed
  1431 
  1432 lemma (in sigma_algebra) borel_measurable_liminf[simp, intro]:
  1433   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1434   assumes "\<And>i. f i \<in> borel_measurable M"
  1435   shows "(\<lambda>x. liminf (\<lambda>i. f i x)) \<in> borel_measurable M"
  1436   unfolding liminf_SUPR_INFI using assms by auto
  1437 
  1438 lemma (in sigma_algebra) borel_measurable_limsup[simp, intro]:
  1439   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1440   assumes "\<And>i. f i \<in> borel_measurable M"
  1441   shows "(\<lambda>x. limsup (\<lambda>i. f i x)) \<in> borel_measurable M"
  1442   unfolding limsup_INFI_SUPR using assms by auto
  1443 
  1444 lemma (in sigma_algebra) borel_measurable_extreal_diff[simp, intro]:
  1445   fixes f g :: "'a \<Rightarrow> extreal"
  1446   assumes "f \<in> borel_measurable M"
  1447   assumes "g \<in> borel_measurable M"
  1448   shows "(\<lambda>x. f x - g x) \<in> borel_measurable M"
  1449   unfolding minus_extreal_def using assms by auto
  1450 
  1451 lemma (in sigma_algebra) borel_measurable_psuminf[simp, intro]:
  1452   fixes f :: "nat \<Rightarrow> 'a \<Rightarrow> extreal"
  1453   assumes "\<And>i. f i \<in> borel_measurable M" and pos: "\<And>i x. x \<in> space M \<Longrightarrow> 0 \<le> f i x"
  1454   shows "(\<lambda>x. (\<Sum>i. f i x)) \<in> borel_measurable M"
  1455   apply (subst measurable_cong)
  1456   apply (subst suminf_extreal_eq_SUPR)
  1457   apply (rule pos)
  1458   using assms by auto
  1459 
  1460 section "LIMSEQ is borel measurable"
  1461 
  1462 lemma (in sigma_algebra) borel_measurable_LIMSEQ:
  1463   fixes u :: "nat \<Rightarrow> 'a \<Rightarrow> real"
  1464   assumes u': "\<And>x. x \<in> space M \<Longrightarrow> (\<lambda>i. u i x) ----> u' x"
  1465   and u: "\<And>i. u i \<in> borel_measurable M"
  1466   shows "u' \<in> borel_measurable M"
  1467 proof -
  1468   have "\<And>x. x \<in> space M \<Longrightarrow> liminf (\<lambda>n. extreal (u n x)) = extreal (u' x)"
  1469     using u' by (simp add: lim_imp_Liminf trivial_limit_sequentially lim_extreal)
  1470   moreover from u have "(\<lambda>x. liminf (\<lambda>n. extreal (u n x))) \<in> borel_measurable M"
  1471     by auto
  1472   ultimately show ?thesis by (simp cong: measurable_cong add: borel_measurable_extreal_iff)
  1473 qed
  1474 
  1475 end